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_IPC_POSIX_SHARED_MEMORY_H_ 18 #define SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_ 19 20 #include <stddef.h> 21 22 #include <memory> 23 24 #include "perfetto/base/scoped_file.h" 25 #include "perfetto/tracing/core/shared_memory.h" 26 27 namespace perfetto { 28 29 // Implements the SharedMemory and its factory for the posix-based transport. 30 class PosixSharedMemory : public SharedMemory { 31 public: 32 class Factory : public SharedMemory::Factory { 33 public: 34 ~Factory() override; 35 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override; 36 }; 37 38 // Create a brand new SHM region (the service uses this). 39 static std::unique_ptr<PosixSharedMemory> Create(size_t size); 40 41 // Mmaps a file descriptor to an existing SHM region (the producer uses this). 42 static std::unique_ptr<PosixSharedMemory> AttachToFd(base::ScopedFile); 43 44 ~PosixSharedMemory() override; 45 fd()46 int fd() const { return fd_.get(); } 47 48 // SharedMemory implementation. start()49 void* start() const override { return start_; } size()50 size_t size() const override { return size_; } 51 52 private: 53 static std::unique_ptr<PosixSharedMemory> MapFD(base::ScopedFile, size_t); 54 55 PosixSharedMemory(void* start, size_t size, base::ScopedFile); 56 PosixSharedMemory(const PosixSharedMemory&) = delete; 57 PosixSharedMemory& operator=(const PosixSharedMemory&) = delete; 58 59 void* const start_; 60 const size_t size_; 61 base::ScopedFile fd_; 62 }; 63 64 } // namespace perfetto 65 66 #endif // SRC_TRACING_IPC_POSIX_SHARED_MEMORY_H_ 67