• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_WRITABLE_SHARED_MEMORY_REGION_H_
6 #define BASE_MEMORY_WRITABLE_SHARED_MEMORY_REGION_H_
7 
8 #include "base/macros.h"
9 #include "base/memory/platform_shared_memory_region.h"
10 #include "base/memory/read_only_shared_memory_region.h"
11 #include "base/memory/shared_memory_mapping.h"
12 #include "base/memory/unsafe_shared_memory_region.h"
13 
14 namespace base {
15 
16 // Scoped move-only handle to a region of platform shared memory. The instance
17 // owns the platform handle it wraps. Mappings created by this region are
18 // writable. These mappings remain valid even after the region handle is moved
19 // or destroyed.
20 //
21 // This region can be locked to read-only access by converting it to a
22 // ReadOnlySharedMemoryRegion. However, unlike ReadOnlySharedMemoryRegion and
23 // UnsafeSharedMemoryRegion, ownership of this region (while writable) is unique
24 // and may only be transferred, not duplicated.
25 class BASE_EXPORT WritableSharedMemoryRegion {
26  public:
27   using MappingType = WritableSharedMemoryMapping;
28   // Creates a new WritableSharedMemoryRegion instance of a given
29   // size that can be used for mapping writable shared memory into the virtual
30   // address space.
31   static WritableSharedMemoryRegion Create(size_t size);
32 
33   // Returns a WritableSharedMemoryRegion built from a platform handle that was
34   // taken from another WritableSharedMemoryRegion instance. Returns an invalid
35   // region iff the |handle| is invalid. CHECK-fails if the |handle| isn't
36   // writable.
37   // This should be used only by the code passing handles across process
38   // boundaries.
39   static WritableSharedMemoryRegion Deserialize(
40       subtle::PlatformSharedMemoryRegion handle);
41 
42   // Extracts a platform handle from the region. Ownership is transferred to the
43   // returned region object.
44   // This should be used only for sending the handle from the current
45   // process to another.
46   static subtle::PlatformSharedMemoryRegion TakeHandleForSerialization(
47       WritableSharedMemoryRegion region);
48 
49   // Makes the region read-only. No new writable mappings of the region can be
50   // created after this call. Returns an invalid region on failure.
51   static ReadOnlySharedMemoryRegion ConvertToReadOnly(
52       WritableSharedMemoryRegion region);
53 
54   // Makes the region unsafe. The region cannot be converted to read-only after
55   // this call. Returns an invalid region on failure.
56   static UnsafeSharedMemoryRegion ConvertToUnsafe(
57       WritableSharedMemoryRegion region);
58 
59   // Default constructor initializes an invalid instance.
60   WritableSharedMemoryRegion();
61 
62   // Move operations are allowed.
63   WritableSharedMemoryRegion(WritableSharedMemoryRegion&&);
64   WritableSharedMemoryRegion& operator=(WritableSharedMemoryRegion&&);
65 
66   // Destructor closes shared memory region if valid.
67   // All created mappings will remain valid.
68   ~WritableSharedMemoryRegion();
69 
70   // Maps the shared memory region into the caller's address space with write
71   // access. The mapped address is guaranteed to have an alignment of
72   // at least |subtle::PlatformSharedMemoryRegion::kMapMinimumAlignment|.
73   // Returns a valid WritableSharedMemoryMapping instance on success, invalid
74   // otherwise.
75   WritableSharedMemoryMapping Map() const;
76 
77   // Same as above, but maps only |size| bytes of the shared memory block
78   // starting with the given |offset|. |offset| must be aligned to value of
79   // |SysInfo::VMAllocationGranularity()|. Returns an invalid mapping if
80   // requested bytes are out of the region limits.
81   WritableSharedMemoryMapping MapAt(off_t offset, size_t size) const;
82 
83   // Whether underlying platform handles are valid.
84   bool IsValid() const;
85 
86   // Returns the maximum mapping size that can be created from this region.
GetSize()87   size_t GetSize() const {
88     DCHECK(IsValid());
89     return handle_.GetSize();
90   }
91 
92   // Returns 128-bit GUID of the region.
GetGUID()93   const UnguessableToken& GetGUID() const {
94     DCHECK(IsValid());
95     return handle_.GetGUID();
96   }
97 
98  private:
99   explicit WritableSharedMemoryRegion(
100       subtle::PlatformSharedMemoryRegion handle);
101 
102   subtle::PlatformSharedMemoryRegion handle_;
103 
104   DISALLOW_COPY_AND_ASSIGN(WritableSharedMemoryRegion);
105 };
106 
107 }  // namespace base
108 
109 #endif  // BASE_MEMORY_WRITABLE_SHARED_MEMORY_REGION_H_
110