1 /* 2 * Copyright (C) 2021 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_SHARED_MEMORY_WINDOWS_H_ 18 #define SRC_TRACING_IPC_SHARED_MEMORY_WINDOWS_H_ 19 20 #include "perfetto/base/build_config.h" 21 22 #if PERFETTO_BUILDFLAG(PERFETTO_OS_WIN) 23 24 #include <string> 25 26 #include "perfetto/base/build_config.h" 27 #include "perfetto/ext/base/scoped_file.h" 28 #include "perfetto/ext/tracing/core/shared_memory.h" 29 30 namespace perfetto { 31 32 // Implements the SharedMemory and its factory for the Windows IPC transport. 33 // This used only for standalone builds and NOT in chromium, which instead uses 34 // a custom Mojo wrapper (MojoSharedMemory in chromium's //services/tracing/). 35 class SharedMemoryWindows : public SharedMemory { 36 public: 37 class Factory : public SharedMemory::Factory { 38 public: 39 ~Factory() override; 40 std::unique_ptr<SharedMemory> CreateSharedMemory(size_t) override; 41 }; 42 43 // Create a brand new SHM region. 44 enum Flags { kNone = 0, kInheritableHandles }; 45 static std::unique_ptr<SharedMemoryWindows> Create( 46 size_t size, Flags flags=Flags::kNone); 47 static std::unique_ptr<SharedMemoryWindows> Attach(const std::string& key); 48 static std::unique_ptr<SharedMemoryWindows> AttachToHandleWithKey( 49 base::ScopedPlatformHandle fd, const std::string& key); 50 ~SharedMemoryWindows() override; key()51 const std::string& key() const { return key_; } handle()52 const base::ScopedPlatformHandle& handle() const { return handle_; } 53 54 // SharedMemory implementation. 55 using SharedMemory::start; // Equal priority to const and non-const versions start()56 const void* start() const override { return start_; } size()57 size_t size() const override { return size_; } 58 59 private: 60 SharedMemoryWindows(void* start, 61 size_t size, 62 std::string, 63 base::ScopedPlatformHandle); 64 SharedMemoryWindows(const SharedMemoryWindows&) = delete; 65 SharedMemoryWindows& operator=(const SharedMemoryWindows&) = delete; 66 67 void* const start_; 68 const size_t size_; 69 std::string key_; 70 base::ScopedPlatformHandle handle_; 71 }; 72 73 } // namespace perfetto 74 75 #endif // OS_WIN 76 77 #endif // SRC_TRACING_IPC_SHARED_MEMORY_WINDOWS_H_ 78