xref: /aosp_15_r20/external/perfetto/src/tracing/core/in_process_shared_memory.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef SRC_TRACING_CORE_IN_PROCESS_SHARED_MEMORY_H_
18 #define SRC_TRACING_CORE_IN_PROCESS_SHARED_MEMORY_H_
19 
20 #include <memory>
21 
22 #include "perfetto/ext/base/paged_memory.h"
23 #include "perfetto/ext/tracing/core/shared_memory.h"
24 
25 namespace perfetto {
26 
27 // An implementation of the ShareMemory interface that allocates memory that can
28 // only be shared intra-process.
29 class InProcessSharedMemory : public SharedMemory {
30  public:
31   static constexpr size_t kDefaultSize = 128 * 1024;
32 
33   // Default ctor used for intra-process shmem between a producer and the
34   // service.
InProcessSharedMemory(size_t size)35   explicit InProcessSharedMemory(size_t size)
36       : mem_(base::PagedMemory::Allocate(size)) {}
37   ~InProcessSharedMemory() override;
38 
39   static std::unique_ptr<InProcessSharedMemory> Create(
40       size_t size = kDefaultSize) {
41     return std::make_unique<InProcessSharedMemory>(size);
42   }
43 
44   // SharedMemory implementation.
45   using SharedMemory::start;  // Equal priority to const and non-const versions
46   const void* start() const override;
47   size_t size() const override;
48 
49   class Factory : public SharedMemory::Factory {
50    public:
51     ~Factory() override;
CreateSharedMemory(size_t size)52     std::unique_ptr<SharedMemory> CreateSharedMemory(size_t size) override {
53       return InProcessSharedMemory::Create(size);
54     }
55   };
56 
57  private:
58   base::PagedMemory mem_;
59 };
60 
61 }  // namespace perfetto
62 
63 #endif  // SRC_TRACING_CORE_IN_PROCESS_SHARED_MEMORY_H_
64