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 INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_H_ 18 #define INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_H_ 19 20 #include <stddef.h> 21 22 #include <memory> 23 #include <utility> 24 25 #include "perfetto/base/export.h" 26 #include "perfetto/base/platform_handle.h" 27 28 namespace perfetto { 29 30 // An abstract interface that models the shared memory region shared between 31 // Service and Producer. The concrete implementation of this is up to the 32 // transport layer. This can be as simple as a malloc()-ed buffer, if both 33 // Producer and Service are hosted in the same process, or some posix shared 34 // memory for the out-of-process case (see src/unix_rpc). 35 // Both this class and the Factory are subclassed by the transport layer, which 36 // will attach platform specific fields to it (e.g., a unix file descriptor). 37 class PERFETTO_EXPORT_COMPONENT SharedMemory { 38 public: 39 class PERFETTO_EXPORT_COMPONENT Factory { 40 public: 41 virtual ~Factory(); 42 virtual std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) = 0; 43 }; 44 45 // The transport layer is expected to tear down the resource associated to 46 // this object region when destroyed. 47 virtual ~SharedMemory(); 48 49 // Read/write and read-only access to underlying buffer. The non-const method 50 // is implemented in terms of the const one so subclasses need only provide a 51 // single implementation; implementing in the opposite order would be unsafe 52 // since subclasses could effectively mutate state from inside a const method. 53 // 54 // N.B. This signature implements "deep const" that ties the constness of this 55 // object to the constness of the underlying buffer, as opposed to "shallow 56 // const" that would have the signature `void* start() const;`; this is less 57 // flexible for callers but prevents corner cases where it's transitively 58 // possible to change this object's state via the controlled memory. start()59 void* start() { return const_cast<void*>(std::as_const(*this).start()); } 60 virtual const void* start() const = 0; 61 62 63 virtual size_t size() const = 0; 64 }; 65 66 } // namespace perfetto 67 68 #endif // INCLUDE_PERFETTO_EXT_TRACING_CORE_SHARED_MEMORY_H_ 69