1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef BASE_MEMORY_SHARED_MEMORY_MAPPER_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_MAPPER_H_ 7 8 #include "base/base_export.h" 9 #include "base/containers/span.h" 10 #include "base/memory/platform_shared_memory_handle.h" 11 #include "third_party/abseil-cpp/absl/types/optional.h" 12 13 #include <stdint.h> 14 15 namespace base { 16 17 // Interface to implement mapping and unmapping of shared memory regions into 18 // the virtual address space. The default implementation, 19 // |PlatformSharedMemoryMapper| uses the platform-specific APIs to map the 20 // region anywhere in the address space. Other implementations can be used for 21 // example to always map the regions into an existing address space reservation. 22 // Implementations of this interface should generally be statically allocated 23 // as SharedMemoryMappings keep a reference to their mapper. 24 class BASE_EXPORT SharedMemoryMapper { 25 public: 26 // Returns the default shared memory mapper. 27 static SharedMemoryMapper* GetDefaultInstance(); 28 29 // Maps the shared memory region identified through the provided platform 30 // handle into the caller's address space. 31 virtual absl::optional<span<uint8_t>> Map( 32 subtle::PlatformSharedMemoryHandle handle, 33 bool write_allowed, 34 uint64_t offset, 35 size_t size) = 0; 36 37 // Unmaps the specified region of shared memory from the caller's address 38 // space. 39 virtual void Unmap(span<uint8_t> mapping) = 0; 40 }; 41 42 } // namespace base 43 44 #endif // BASE_MEMORY_SHARED_MEMORY_MAPPER_H_ 45