shared_memory_mapping.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. #ifndef BASE_MEMORY_SHARED_MEMORY_MAPPING_H_
  5. #define BASE_MEMORY_SHARED_MEMORY_MAPPING_H_
  6. #include <cstddef>
  7. #include <type_traits>
  8. #include "base/containers/buffer_iterator.h"
  9. #include "base/containers/span.h"
  10. #include "base/macros.h"
  11. #include "base/unguessable_token.h"
  12. namespace base {
  13. namespace subtle {
  14. class PlatformSharedMemoryRegion;
  15. } // namespace subtle
  16. // Base class for scoped handles to a shared memory mapping created from a
  17. // shared memory region. Created shared memory mappings remain valid even if the
  18. // creator region is transferred or destroyed.
  19. //
  20. // Each mapping has an UnguessableToken that identifies the shared memory region
  21. // it was created from. This is used for memory metrics, to avoid overcounting
  22. // shared memory.
  23. class BASE_EXPORT SharedMemoryMapping {
  24. public:
  25. // Default constructor initializes an invalid instance.
  26. SharedMemoryMapping();
  27. // Move operations are allowed.
  28. SharedMemoryMapping(SharedMemoryMapping&& mapping) noexcept;
  29. SharedMemoryMapping& operator=(SharedMemoryMapping&& mapping) noexcept;
  30. // Unmaps the region if the mapping is valid.
  31. virtual ~SharedMemoryMapping();
  32. // Returns true iff the mapping is valid. False means there is no
  33. // corresponding area of memory.
  34. bool IsValid() const { return memory_ != nullptr; }
  35. // Returns the logical size of the mapping in bytes. This is precisely the
  36. // size requested by whoever created the mapping, and it is always less than
  37. // or equal to |mapped_size()|. This is undefined for invalid instances.
  38. size_t size() const {
  39. DCHECK(IsValid());
  40. return size_;
  41. }
  42. // Returns the actual size of the mapping in bytes. This is always at least
  43. // as large as |size()| but may be larger due to platform mapping alignment
  44. // constraints. This is undefined for invalid instances.
  45. size_t mapped_size() const {
  46. DCHECK(IsValid());
  47. return mapped_size_;
  48. }
  49. // Returns 128-bit GUID of the region this mapping belongs to.
  50. const UnguessableToken& guid() const {
  51. DCHECK(IsValid());
  52. return guid_;
  53. }
  54. protected:
  55. SharedMemoryMapping(void* address,
  56. size_t size,
  57. size_t mapped_size,
  58. const UnguessableToken& guid);
  59. void* raw_memory_ptr() const { return memory_; }
  60. private:
  61. friend class SharedMemoryTracker;
  62. void Unmap();
  63. void* memory_ = nullptr;
  64. size_t size_ = 0;
  65. size_t mapped_size_ = 0;
  66. UnguessableToken guid_;
  67. DISALLOW_COPY_AND_ASSIGN(SharedMemoryMapping);
  68. };
  69. // Class modeling a read-only mapping of a shared memory region into the
  70. // current process' address space. This is created by ReadOnlySharedMemoryRegion
  71. // instances.
  72. class BASE_EXPORT ReadOnlySharedMemoryMapping : public SharedMemoryMapping {
  73. public:
  74. // Default constructor initializes an invalid instance.
  75. ReadOnlySharedMemoryMapping();
  76. // Move operations are allowed.
  77. ReadOnlySharedMemoryMapping(ReadOnlySharedMemoryMapping&&) noexcept;
  78. ReadOnlySharedMemoryMapping& operator=(
  79. ReadOnlySharedMemoryMapping&&) noexcept;
  80. // Returns the base address of the mapping. This is read-only memory. This is
  81. // page-aligned. This is nullptr for invalid instances.
  82. const void* memory() const { return raw_memory_ptr(); }
  83. // Returns a pointer to a page-aligned const T if the mapping is valid and
  84. // large enough to contain a T, or nullptr otherwise.
  85. template <typename T>
  86. const T* GetMemoryAs() const {
  87. static_assert(std::is_trivially_copyable<T>::value,
  88. "Copying non-trivially-copyable object across memory spaces "
  89. "is dangerous");
  90. if (!IsValid())
  91. return nullptr;
  92. if (sizeof(T) > size())
  93. return nullptr;
  94. return static_cast<const T*>(raw_memory_ptr());
  95. }
  96. // Returns a span of const T. The number of elements is autodeduced from the
  97. // size of the shared memory mapping. The number of elements may be
  98. // autodeduced as zero, i.e. the mapping is invalid or the size of the mapping
  99. // isn't large enough to contain even one T: in that case, an empty span
  100. // will be returned. The first element, if any, is guaranteed to be
  101. // page-aligned.
  102. template <typename T>
  103. span<const T> GetMemoryAsSpan() const {
  104. static_assert(std::is_trivially_copyable<T>::value,
  105. "Copying non-trivially-copyable object across memory spaces "
  106. "is dangerous");
  107. if (!IsValid())
  108. return span<const T>();
  109. size_t count = size() / sizeof(T);
  110. return GetMemoryAsSpan<T>(count);
  111. }
  112. // Returns a span of const T with |count| elements if the mapping is valid and
  113. // large enough to contain |count| elements, or an empty span otherwise. The
  114. // first element, if any, is guaranteed to be page-aligned.
  115. template <typename T>
  116. span<const T> GetMemoryAsSpan(size_t count) const {
  117. static_assert(std::is_trivially_copyable<T>::value,
  118. "Copying non-trivially-copyable object across memory spaces "
  119. "is dangerous");
  120. if (!IsValid())
  121. return span<const T>();
  122. if (size() / sizeof(T) < count)
  123. return span<const T>();
  124. return span<const T>(static_cast<const T*>(raw_memory_ptr()), count);
  125. }
  126. // Returns a BufferIterator of const T.
  127. template <typename T>
  128. BufferIterator<const T> GetMemoryAsBufferIterator() const {
  129. return BufferIterator<const T>(GetMemoryAsSpan<T>());
  130. }
  131. private:
  132. friend class ReadOnlySharedMemoryRegion;
  133. ReadOnlySharedMemoryMapping(void* address,
  134. size_t size,
  135. size_t mapped_size,
  136. const UnguessableToken& guid);
  137. DISALLOW_COPY_AND_ASSIGN(ReadOnlySharedMemoryMapping);
  138. };
  139. // Class modeling a writable mapping of a shared memory region into the
  140. // current process' address space. This is created by *SharedMemoryRegion
  141. // instances.
  142. class BASE_EXPORT WritableSharedMemoryMapping : public SharedMemoryMapping {
  143. public:
  144. // Default constructor initializes an invalid instance.
  145. WritableSharedMemoryMapping();
  146. // Move operations are allowed.
  147. WritableSharedMemoryMapping(WritableSharedMemoryMapping&&) noexcept;
  148. WritableSharedMemoryMapping& operator=(
  149. WritableSharedMemoryMapping&&) noexcept;
  150. // Returns the base address of the mapping. This is writable memory. This is
  151. // page-aligned. This is nullptr for invalid instances.
  152. void* memory() const { return raw_memory_ptr(); }
  153. // Returns a pointer to a page-aligned T if the mapping is valid and large
  154. // enough to contain a T, or nullptr otherwise.
  155. template <typename T>
  156. T* GetMemoryAs() const {
  157. static_assert(std::is_trivially_copyable<T>::value,
  158. "Copying non-trivially-copyable object across memory spaces "
  159. "is dangerous");
  160. if (!IsValid())
  161. return nullptr;
  162. if (sizeof(T) > size())
  163. return nullptr;
  164. return static_cast<T*>(raw_memory_ptr());
  165. }
  166. // Returns a span of T. The number of elements is autodeduced from the size of
  167. // the shared memory mapping. The number of elements may be autodeduced as
  168. // zero, i.e. the mapping is invalid or the size of the mapping isn't large
  169. // enough to contain even one T: in that case, an empty span will be returned.
  170. // The first element, if any, is guaranteed to be page-aligned.
  171. template <typename T>
  172. span<T> GetMemoryAsSpan() const {
  173. static_assert(std::is_trivially_copyable<T>::value,
  174. "Copying non-trivially-copyable object across memory spaces "
  175. "is dangerous");
  176. if (!IsValid())
  177. return span<T>();
  178. size_t count = size() / sizeof(T);
  179. return GetMemoryAsSpan<T>(count);
  180. }
  181. // Returns a span of T with |count| elements if the mapping is valid and large
  182. // enough to contain |count| elements, or an empty span otherwise. The first
  183. // element, if any, is guaranteed to be page-aligned.
  184. template <typename T>
  185. span<T> GetMemoryAsSpan(size_t count) const {
  186. static_assert(std::is_trivially_copyable<T>::value,
  187. "Copying non-trivially-copyable object across memory spaces "
  188. "is dangerous");
  189. if (!IsValid())
  190. return span<T>();
  191. if (size() / sizeof(T) < count)
  192. return span<T>();
  193. return span<T>(static_cast<T*>(raw_memory_ptr()), count);
  194. }
  195. // Returns a BufferIterator of T.
  196. template <typename T>
  197. BufferIterator<T> GetMemoryAsBufferIterator() {
  198. return BufferIterator<T>(GetMemoryAsSpan<T>());
  199. }
  200. private:
  201. friend WritableSharedMemoryMapping MapAtForTesting(
  202. subtle::PlatformSharedMemoryRegion* region,
  203. off_t offset,
  204. size_t size);
  205. friend class ReadOnlySharedMemoryRegion;
  206. friend class WritableSharedMemoryRegion;
  207. friend class UnsafeSharedMemoryRegion;
  208. WritableSharedMemoryMapping(void* address,
  209. size_t size,
  210. size_t mapped_size,
  211. const UnguessableToken& guid);
  212. DISALLOW_COPY_AND_ASSIGN(WritableSharedMemoryMapping);
  213. };
  214. } // namespace base
  215. #endif // BASE_MEMORY_SHARED_MEMORY_MAPPING_H_