Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2022 The Chromium Authors |
Samuel Groß | cc95616f | 2022-04-28 09:48:25 | [diff] [blame] | 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 | |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 8 | #include <stdint.h> |
| 9 | |
| 10 | #include <optional> |
| 11 | |
Samuel Groß | cc95616f | 2022-04-28 09:48:25 | [diff] [blame] | 12 | #include "base/base_export.h" |
| 13 | #include "base/containers/span.h" |
| 14 | #include "base/memory/platform_shared_memory_handle.h" |
Samuel Groß | cc95616f | 2022-04-28 09:48:25 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | |
| 18 | // Interface to implement mapping and unmapping of shared memory regions into |
| 19 | // the virtual address space. The default implementation, |
| 20 | // |PlatformSharedMemoryMapper| uses the platform-specific APIs to map the |
| 21 | // region anywhere in the address space. Other implementations can be used for |
| 22 | // example to always map the regions into an existing address space reservation. |
| 23 | // Implementations of this interface should generally be statically allocated |
| 24 | // as SharedMemoryMappings keep a reference to their mapper. |
| 25 | class BASE_EXPORT SharedMemoryMapper { |
| 26 | public: |
| 27 | // Returns the default shared memory mapper. |
| 28 | static SharedMemoryMapper* GetDefaultInstance(); |
| 29 | |
| 30 | // Maps the shared memory region identified through the provided platform |
| 31 | // handle into the caller's address space. |
Arthur Sonzogni | e5fff99c | 2024-02-21 15:58:24 | [diff] [blame] | 32 | virtual std::optional<span<uint8_t>> Map( |
Samuel Groß | cc95616f | 2022-04-28 09:48:25 | [diff] [blame] | 33 | subtle::PlatformSharedMemoryHandle handle, |
| 34 | bool write_allowed, |
| 35 | uint64_t offset, |
| 36 | size_t size) = 0; |
| 37 | |
| 38 | // Unmaps the specified region of shared memory from the caller's address |
| 39 | // space. |
| 40 | virtual void Unmap(span<uint8_t> mapping) = 0; |
| 41 | }; |
| 42 | |
| 43 | } // namespace base |
| 44 | |
| 45 | #endif // BASE_MEMORY_SHARED_MEMORY_MAPPER_H_ |