xref: /aosp_15_r20/external/cronet/base/memory/shared_memory_mapper.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
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 <stdint.h>
9 
10 #include <optional>
11 
12 #include "base/base_export.h"
13 #include "base/containers/span.h"
14 #include "base/memory/platform_shared_memory_handle.h"
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.
32   virtual std::optional<span<uint8_t>> Map(
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_
46