xref: /aosp_15_r20/external/perfetto/src/tracing/test/test_shared_memory.h (revision 6dbdd20afdafa5e3ca9b8809fa73465d530080dc)
1 /*
2  * Copyright (C) 2017 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_TEST_TEST_SHARED_MEMORY_H_
18 #define SRC_TRACING_TEST_TEST_SHARED_MEMORY_H_
19 
20 #include <stddef.h>
21 
22 #include <memory>
23 
24 #include "perfetto/ext/tracing/core/shared_memory.h"
25 #include "src/tracing/core/in_process_shared_memory.h"
26 
27 namespace perfetto {
28 
29 // A dummy implementation of shared memory for single process unittests
30 // (just a wrapper around malloc() that fits the SharedMemory API).
31 using TestSharedMemory = InProcessSharedMemory;
32 
33 // An implementation of the SharedMemory that doesn't own any memory, but just
34 // points to memory owned by another SharedMemory.
35 //
36 // This is useful to test two components that own separate SharedMemory that
37 // really point to the same memory underneath without setting up real posix
38 // shared memory.
39 class TestRefSharedMemory : public SharedMemory {
40  public:
41   // N.B. `*mem` must outlive `*this`.
TestRefSharedMemory(SharedMemory * mem)42   explicit TestRefSharedMemory(SharedMemory* mem)
43       : start_(mem->start()), size_(mem->size()) {}
44   ~TestRefSharedMemory() override;
45 
Create(SharedMemory * mem)46   static std::unique_ptr<TestRefSharedMemory> Create(SharedMemory* mem) {
47     return std::make_unique<TestRefSharedMemory>(mem);
48   }
49 
50   // SharedMemory implementation.
51   using SharedMemory::start;  // Equal priority to const and non-const versions
52   const void* start() const override;
53   size_t size() const override;
54 
55  private:
56   void* start_;
57   size_t size_;
58 };
59 
60 }  // namespace perfetto
61 
62 #endif  // SRC_TRACING_TEST_TEST_SHARED_MEMORY_H_
63