OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 6 #define BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include "base/unguessable_token.h" | 10 #include "base/unguessable_token.h" |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
64 // Duplicates the underlying OS resource. Using the return value as a | 64 // Duplicates the underlying OS resource. Using the return value as a |
65 // parameter to an IPC message will cause the IPC subsystem to consume the OS | 65 // parameter to an IPC message will cause the IPC subsystem to consume the OS |
66 // resource. | 66 // resource. |
67 SharedMemoryHandle Duplicate() const; | 67 SharedMemoryHandle Duplicate() const; |
68 | 68 |
69 // Uniques identifies the shared memory region that the underlying OS resource | 69 // Uniques identifies the shared memory region that the underlying OS resource |
70 // points to. Multiple SharedMemoryHandles that point to the same shared | 70 // points to. Multiple SharedMemoryHandles that point to the same shared |
71 // memory region will have the same GUID. Preserved across IPC. | 71 // memory region will have the same GUID. Preserved across IPC. |
72 base::UnguessableToken GetGUID() const; | 72 base::UnguessableToken GetGUID() const; |
73 | 73 |
| 74 // Returns the size of the memory region that SharedMemoryHandle points to. |
| 75 size_t GetSize() const; |
| 76 |
74 #if defined(OS_MACOSX) && !defined(OS_IOS) | 77 #if defined(OS_MACOSX) && !defined(OS_IOS) |
75 enum Type { | 78 enum Type { |
76 // The SharedMemoryHandle is backed by a POSIX fd. | 79 // The SharedMemoryHandle is backed by a POSIX fd. |
77 POSIX, | 80 POSIX, |
78 // The SharedMemoryHandle is backed by the Mach primitive "memory object". | 81 // The SharedMemoryHandle is backed by the Mach primitive "memory object". |
79 MACH, | 82 MACH, |
80 }; | 83 }; |
81 | 84 |
82 // Constructs a SharedMemoryHandle backed by the components of a | 85 // Constructs a SharedMemoryHandle backed by the components of a |
83 // FileDescriptor. The newly created instance has the same ownership semantics | 86 // FileDescriptor. The newly created instance has the same ownership semantics |
84 // as base::FileDescriptor. This typically means that the SharedMemoryHandle | 87 // as base::FileDescriptor. This typically means that the SharedMemoryHandle |
85 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's | 88 // takes ownership of the |fd| if |auto_close| is true. Unfortunately, it's |
86 // common for existing code to make shallow copies of SharedMemoryHandle, and | 89 // common for existing code to make shallow copies of SharedMemoryHandle, and |
87 // the one that is finally passed into a base::SharedMemory is the one that | 90 // the one that is finally passed into a base::SharedMemory is the one that |
88 // "consumes" the fd. | 91 // "consumes" the fd. |
89 // |guid| uniquely identifies the shared memory region pointed to by the | 92 // |guid| uniquely identifies the shared memory region pointed to by the |
90 // underlying OS resource. If |file_descriptor| is associated with another | 93 // underlying OS resource. If |file_descriptor| is associated with another |
91 // SharedMemoryHandle, the caller must pass the |guid| of that | 94 // SharedMemoryHandle, the caller must pass the |guid| of that |
92 // SharedMemoryHandle. Otherwise, the caller should generate a new | 95 // SharedMemoryHandle. Otherwise, the caller should generate a new |
93 // UnguessableToken. | 96 // UnguessableToken. |
| 97 // |size| refers to the size of the memory region pointed to by |
| 98 // file_descriptor.fd. Passing the wrong |size| has no immediate consequence, |
| 99 // but may cause errors when trying to map the SharedMemoryHandle at a later |
| 100 // point in time. |
94 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, | 101 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, |
| 102 size_t size, |
95 const base::UnguessableToken& guid); | 103 const base::UnguessableToken& guid); |
96 | 104 |
97 // Makes a Mach-based SharedMemoryHandle of the given size. On error, | 105 // Makes a Mach-based SharedMemoryHandle of the given size. On error, |
98 // subsequent calls to IsValid() return false. | 106 // subsequent calls to IsValid() return false. |
| 107 // Passing the wrong |size| has no immediate consequence, but may cause errors |
| 108 // when trying to map the SharedMemoryHandle at a later point in time. |
99 SharedMemoryHandle(mach_vm_size_t size, const base::UnguessableToken& guid); | 109 SharedMemoryHandle(mach_vm_size_t size, const base::UnguessableToken& guid); |
100 | 110 |
101 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry | 111 // Makes a Mach-based SharedMemoryHandle from |memory_object|, a named entry |
102 // in the current task. The memory region has size |size|. | 112 // in the current task. The memory region has size |size|. |
| 113 // Passing the wrong |size| has no immediate consequence, but may cause errors |
| 114 // when trying to map the SharedMemoryHandle at a later point in time. |
103 SharedMemoryHandle(mach_port_t memory_object, | 115 SharedMemoryHandle(mach_port_t memory_object, |
104 mach_vm_size_t size, | 116 mach_vm_size_t size, |
105 const base::UnguessableToken& guid); | 117 const base::UnguessableToken& guid); |
106 | 118 |
107 // Exposed so that the SharedMemoryHandle can be transported between | 119 // Exposed so that the SharedMemoryHandle can be transported between |
108 // processes. | 120 // processes. |
109 mach_port_t GetMemoryObject() const; | 121 mach_port_t GetMemoryObject() const; |
110 | 122 |
111 // Returns false on a failure to determine the size. On success, populates the | |
112 // output variable |size|. | |
113 bool GetSize(size_t* size) const; | |
114 | |
115 // The SharedMemoryHandle must be valid. | 123 // The SharedMemoryHandle must be valid. |
116 // Returns whether the SharedMemoryHandle was successfully mapped into memory. | 124 // Returns whether the SharedMemoryHandle was successfully mapped into memory. |
117 // On success, |memory| is an output variable that contains the start of the | 125 // On success, |memory| is an output variable that contains the start of the |
118 // mapped memory. | 126 // mapped memory. |
119 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); | 127 bool MapAt(off_t offset, size_t bytes, void** memory, bool read_only); |
120 #elif defined(OS_WIN) | 128 #elif defined(OS_WIN) |
121 // Takes implicit ownership of |h|. | 129 // Takes implicit ownership of |h|. |
122 // |guid| uniquely identifies the shared memory region pointed to by the | 130 // |guid| uniquely identifies the shared memory region pointed to by the |
123 // underlying OS resource. If the HANDLE is associated with another | 131 // underlying OS resource. If the HANDLE is associated with another |
124 // SharedMemoryHandle, the caller must pass the |guid| of that | 132 // SharedMemoryHandle, the caller must pass the |guid| of that |
125 // SharedMemoryHandle. Otherwise, the caller should generate a new | 133 // SharedMemoryHandle. Otherwise, the caller should generate a new |
126 // UnguessableToken. | 134 // UnguessableToken. |
127 SharedMemoryHandle(HANDLE h, const base::UnguessableToken& guid); | 135 // Passing the wrong |size| has no immediate consequence, but may cause errors |
| 136 // when trying to map the SharedMemoryHandle at a later point in time. |
| 137 SharedMemoryHandle(HANDLE h, size_t size, const base::UnguessableToken& guid); |
128 HANDLE GetHandle() const; | 138 HANDLE GetHandle() const; |
129 #else | 139 #else |
130 // |guid| uniquely identifies the shared memory region pointed to by the | 140 // |guid| uniquely identifies the shared memory region pointed to by the |
131 // underlying OS resource. If |file_descriptor| is associated with another | 141 // underlying OS resource. If |file_descriptor| is associated with another |
132 // SharedMemoryHandle, the caller must pass the |guid| of that | 142 // SharedMemoryHandle, the caller must pass the |guid| of that |
133 // SharedMemoryHandle. Otherwise, the caller should generate a new | 143 // SharedMemoryHandle. Otherwise, the caller should generate a new |
134 // UnguessableToken. | 144 // UnguessableToken. |
| 145 // Passing the wrong |size| has no immediate consequence, but may cause errors |
| 146 // when trying to map the SharedMemoryHandle at a later point in time. |
135 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, | 147 SharedMemoryHandle(const base::FileDescriptor& file_descriptor, |
| 148 size_t size, |
136 const base::UnguessableToken& guid); | 149 const base::UnguessableToken& guid); |
137 | 150 |
138 // Creates a SharedMemoryHandle from an |fd| supplied from an external | 151 // Creates a SharedMemoryHandle from an |fd| supplied from an external |
139 // service. | 152 // service. |
140 static SharedMemoryHandle ImportHandle(int fd); | 153 // Passing the wrong |size| has no immediate consequence, but may cause errors |
| 154 // when trying to map the SharedMemoryHandle at a later point in time. |
| 155 static SharedMemoryHandle ImportHandle(int fd, size_t size); |
141 | 156 |
142 // Returns the underlying OS resource. | 157 // Returns the underlying OS resource. |
143 int GetHandle() const; | 158 int GetHandle() const; |
144 | 159 |
145 // Takes ownership of the OS resource. | |
146 void SetHandle(int fd); | |
147 | |
148 // Invalidates [but doesn't close] the underlying OS resource. This will leak | 160 // Invalidates [but doesn't close] the underlying OS resource. This will leak |
149 // unless the caller is careful. | 161 // unless the caller is careful. |
150 int Release(); | 162 int Release(); |
151 #endif | 163 #endif |
152 | 164 |
153 private: | 165 private: |
154 #if defined(OS_MACOSX) && !defined(OS_IOS) | 166 #if defined(OS_MACOSX) && !defined(OS_IOS) |
155 friend class SharedMemory; | 167 friend class SharedMemory; |
156 | 168 |
157 Type type_; | 169 Type type_ = MACH; |
158 | 170 |
159 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a | 171 // Each instance of a SharedMemoryHandle is backed either by a POSIX fd or a |
160 // mach port. |type_| determines the backing member. | 172 // mach port. |type_| determines the backing member. |
161 union { | 173 union { |
162 FileDescriptor file_descriptor_; | 174 FileDescriptor file_descriptor_; |
163 | 175 |
164 struct { | 176 struct { |
165 mach_port_t memory_object_; | 177 mach_port_t memory_object_ = MACH_PORT_NULL; |
166 | |
167 // The size of the shared memory region when |type_| is MACH. Only | |
168 // relevant if |memory_object_| is not |MACH_PORT_NULL|. | |
169 mach_vm_size_t size_; | |
170 | 178 |
171 // Whether passing this object as a parameter to an IPC message passes | 179 // Whether passing this object as a parameter to an IPC message passes |
172 // ownership of |memory_object_| to the IPC stack. This is meant to mimic | 180 // ownership of |memory_object_| to the IPC stack. This is meant to mimic |
173 // the behavior of the |auto_close| parameter of FileDescriptor. | 181 // the behavior of the |auto_close| parameter of FileDescriptor. |
174 // Defaults to |false|. | 182 // Defaults to |false|. |
175 bool ownership_passes_to_ipc_; | 183 bool ownership_passes_to_ipc_ = false; |
176 }; | 184 }; |
177 }; | 185 }; |
178 #elif defined(OS_WIN) | 186 #elif defined(OS_WIN) |
179 HANDLE handle_; | 187 HANDLE handle_ = nullptr; |
180 | 188 |
181 // Whether passing this object as a parameter to an IPC message passes | 189 // Whether passing this object as a parameter to an IPC message passes |
182 // ownership of |handle_| to the IPC stack. This is meant to mimic the | 190 // ownership of |handle_| to the IPC stack. This is meant to mimic the |
183 // behavior of the |auto_close| parameter of FileDescriptor. This member only | 191 // behavior of the |auto_close| parameter of FileDescriptor. This member only |
184 // affects attachment-brokered SharedMemoryHandles. | 192 // affects attachment-brokered SharedMemoryHandles. |
185 // Defaults to |false|. | 193 // Defaults to |false|. |
186 bool ownership_passes_to_ipc_; | 194 bool ownership_passes_to_ipc_ = false; |
187 #else | 195 #else |
188 FileDescriptor file_descriptor_; | 196 FileDescriptor file_descriptor_; |
189 #endif | 197 #endif |
190 | 198 |
191 base::UnguessableToken guid_; | 199 base::UnguessableToken guid_; |
| 200 |
| 201 // The size of the region referenced by the SharedMemoryHandle. |
| 202 size_t size_ = 0; |
192 }; | 203 }; |
193 | 204 |
194 } // namespace base | 205 } // namespace base |
195 | 206 |
196 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ | 207 #endif // BASE_MEMORY_SHARED_MEMORY_HANDLE_H_ |
OLD | NEW |