1 // Copyright 2018 The Chromium Authors. All rights reserved. 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_MAPPING_H_ 6 #define BASE_MEMORY_SHARED_MEMORY_MAPPING_H_ 7 8 #include <cstddef> 9 10 #include "base/macros.h" 11 #include "base/unguessable_token.h" 12 13 namespace base { 14 15 namespace subtle { 16 class PlatformSharedMemoryRegion; 17 } // namespace subtle 18 19 // Base class for scoped handles to a shared memory mapping created from a 20 // shared memory region. Created shared memory mappings remain valid even if the 21 // creator region is transferred or destroyed. 22 // 23 // Each mapping has an UnguessableToken that identifies the shared memory region 24 // it was created from. This is used for memory metrics, to avoid overcounting 25 // shared memory. 26 class BASE_EXPORT SharedMemoryMapping { 27 public: 28 // Default constructor initializes an invalid instance. 29 SharedMemoryMapping(); 30 31 // Move operations are allowed. 32 SharedMemoryMapping(SharedMemoryMapping&& mapping); 33 SharedMemoryMapping& operator=(SharedMemoryMapping&& mapping); 34 35 // Unmaps the region if the mapping is valid. 36 virtual ~SharedMemoryMapping(); 37 38 // Returns true iff the mapping is valid. False means there is no 39 // corresponding area of memory. IsValid()40 bool IsValid() const { return memory_ != nullptr; } 41 42 // Returns the logical size of the mapping in bytes. This is precisely the 43 // size requested by whoever created the mapping, and it is always less than 44 // or equal to |mapped_size()|. This is undefined for invalid instances. size()45 size_t size() const { 46 DCHECK(IsValid()); 47 return size_; 48 } 49 50 // Returns the actual size of the mapping in bytes. This is always at least 51 // as large as |size()| but may be larger due to platform mapping alignment 52 // constraints. This is undefined for invalid instances. mapped_size()53 size_t mapped_size() const { 54 DCHECK(IsValid()); 55 return mapped_size_; 56 } 57 58 // Returns 128-bit GUID of the region this mapping belongs to. guid()59 const UnguessableToken& guid() const { 60 DCHECK(IsValid()); 61 return guid_; 62 } 63 64 protected: 65 SharedMemoryMapping(void* address, 66 size_t size, 67 size_t mapped_size, 68 const UnguessableToken& guid); raw_memory_ptr()69 void* raw_memory_ptr() const { return memory_; } 70 71 private: 72 friend class SharedMemoryTracker; 73 74 void Unmap(); 75 76 void* memory_ = nullptr; 77 size_t size_ = 0; 78 size_t mapped_size_ = 0; 79 UnguessableToken guid_; 80 81 DISALLOW_COPY_AND_ASSIGN(SharedMemoryMapping); 82 }; 83 84 // Class modeling a read-only mapping of a shared memory region into the 85 // current process' address space. This is created by ReadOnlySharedMemoryRegion 86 // instances. 87 class BASE_EXPORT ReadOnlySharedMemoryMapping : public SharedMemoryMapping { 88 public: 89 // Default constructor initializes an invalid instance. 90 ReadOnlySharedMemoryMapping(); 91 92 // Move operations are allowed. 93 ReadOnlySharedMemoryMapping(ReadOnlySharedMemoryMapping&&); 94 ReadOnlySharedMemoryMapping& operator=(ReadOnlySharedMemoryMapping&&); 95 96 // Returns the base address of the mapping. This is read-only memory. This is 97 // page-aligned. This is nullptr for invalid instances. memory()98 const void* memory() const { return raw_memory_ptr(); } 99 100 private: 101 friend class ReadOnlySharedMemoryRegion; 102 ReadOnlySharedMemoryMapping(void* address, 103 size_t size, 104 size_t mapped_size, 105 const UnguessableToken& guid); 106 107 DISALLOW_COPY_AND_ASSIGN(ReadOnlySharedMemoryMapping); 108 }; 109 110 // Class modeling a writable mapping of a shared memory region into the 111 // current process' address space. This is created by *SharedMemoryRegion 112 // instances. 113 class BASE_EXPORT WritableSharedMemoryMapping : public SharedMemoryMapping { 114 public: 115 // Default constructor initializes an invalid instance. 116 WritableSharedMemoryMapping(); 117 118 // Move operations are allowed. 119 WritableSharedMemoryMapping(WritableSharedMemoryMapping&&); 120 WritableSharedMemoryMapping& operator=(WritableSharedMemoryMapping&&); 121 122 // Returns the base address of the mapping. This is writable memory. This is 123 // page-aligned. This is nullptr for invalid instances. memory()124 void* memory() const { return raw_memory_ptr(); } 125 126 private: 127 friend WritableSharedMemoryMapping MapAtForTesting( 128 subtle::PlatformSharedMemoryRegion* region, 129 off_t offset, 130 size_t size); 131 friend class ReadOnlySharedMemoryRegion; 132 friend class WritableSharedMemoryRegion; 133 friend class UnsafeSharedMemoryRegion; 134 WritableSharedMemoryMapping(void* address, 135 size_t size, 136 size_t mapped_size, 137 const UnguessableToken& guid); 138 139 DISALLOW_COPY_AND_ASSIGN(WritableSharedMemoryMapping); 140 }; 141 142 } // namespace base 143 144 #endif // BASE_MEMORY_SHARED_MEMORY_MAPPING_H_ 145