OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_H_ |
6 #define BASE_MEMORY_SHARED_MEMORY_H_ | 6 #define BASE_MEMORY_SHARED_MEMORY_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 | 9 |
10 #include <string> | 10 #include <string> |
(...skipping 14 matching lines...) Expand all Loading... |
25 #endif | 25 #endif |
26 | 26 |
27 namespace base { | 27 namespace base { |
28 | 28 |
29 class FilePath; | 29 class FilePath; |
30 | 30 |
31 // Options for creating a shared memory object. | 31 // Options for creating a shared memory object. |
32 struct BASE_EXPORT SharedMemoryCreateOptions { | 32 struct BASE_EXPORT SharedMemoryCreateOptions { |
33 SharedMemoryCreateOptions(); | 33 SharedMemoryCreateOptions(); |
34 | 34 |
35 #if defined(OS_MACOSX) && !defined(OS_IOS) | 35 #if !(defined(OS_MACOSX) && !defined(OS_IOS)) |
36 // The type of OS primitive that should back the SharedMemory object. | |
37 SharedMemoryHandle::Type type; | |
38 #else | |
39 // DEPRECATED (crbug.com/345734): | 36 // DEPRECATED (crbug.com/345734): |
40 // If NULL, the object is anonymous. This pointer is owned by the caller | 37 // If NULL, the object is anonymous. This pointer is owned by the caller |
41 // and must live through the call to Create(). | 38 // and must live through the call to Create(). |
42 const std::string* name_deprecated; | 39 const std::string* name_deprecated; |
43 | 40 |
44 // DEPRECATED (crbug.com/345734): | 41 // DEPRECATED (crbug.com/345734): |
45 // If true, and the shared memory already exists, Create() will open the | 42 // If true, and the shared memory already exists, Create() will open the |
46 // existing shared memory and ignore the size parameter. If false, | 43 // existing shared memory and ignore the size parameter. If false, |
47 // shared memory must not exist. This flag is meaningless unless | 44 // shared memory must not exist. This flag is meaningless unless |
48 // name_deprecated is non-NULL. | 45 // name_deprecated is non-NULL. |
49 bool open_existing_deprecated; | 46 bool open_existing_deprecated; |
50 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | 47 #endif // !(defined(OS_MACOSX) && !defined(OS_IOS)) |
51 | 48 |
52 // Size of the shared memory object to be created. | 49 // Size of the shared memory object to be created. |
53 // When opening an existing object, this has no effect. | 50 // When opening an existing object, this has no effect. |
54 size_t size; | 51 size_t size; |
55 | 52 |
56 // If true, mappings might need to be made executable later. | 53 // If true, mappings might need to be made executable later. |
57 bool executable; | 54 bool executable; |
58 | 55 |
59 // If true, the file can be shared read-only to a process. | 56 // If true, the file can be shared read-only to a process. |
60 bool share_read_only; | 57 bool share_read_only; |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
95 // Closes a shared memory handle. | 92 // Closes a shared memory handle. |
96 static void CloseHandle(const SharedMemoryHandle& handle); | 93 static void CloseHandle(const SharedMemoryHandle& handle); |
97 | 94 |
98 // Returns the maximum number of handles that can be open at once per process. | 95 // Returns the maximum number of handles that can be open at once per process. |
99 static size_t GetHandleLimit(); | 96 static size_t GetHandleLimit(); |
100 | 97 |
101 // Duplicates The underlying OS primitive. Returns NULLHandle() on failure. | 98 // Duplicates The underlying OS primitive. Returns NULLHandle() on failure. |
102 // The caller is responsible for destroying the duplicated OS primitive. | 99 // The caller is responsible for destroying the duplicated OS primitive. |
103 static SharedMemoryHandle DuplicateHandle(const SharedMemoryHandle& handle); | 100 static SharedMemoryHandle DuplicateHandle(const SharedMemoryHandle& handle); |
104 | 101 |
105 #if defined(OS_POSIX) | 102 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) |
106 // This method requires that the SharedMemoryHandle is backed by a POSIX fd. | 103 // This method requires that the SharedMemoryHandle is backed by a POSIX fd. |
107 static int GetFdFromSharedMemoryHandle(const SharedMemoryHandle& handle); | 104 static int GetFdFromSharedMemoryHandle(const SharedMemoryHandle& handle); |
108 #endif | 105 #endif |
109 | 106 |
110 #if defined(OS_POSIX) && !defined(OS_ANDROID) | 107 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
111 // Gets the size of the shared memory region referred to by |handle|. | 108 // Gets the size of the shared memory region referred to by |handle|. |
112 // Returns false on a failure to determine the size. On success, populates the | 109 // Returns false on a failure to determine the size. On success, populates the |
113 // output variable |size|. | 110 // output variable |size|. |
114 static bool GetSizeFromSharedMemoryHandle(const SharedMemoryHandle& handle, | 111 static bool GetSizeFromSharedMemoryHandle(const SharedMemoryHandle& handle, |
115 size_t* size); | 112 size_t* size); |
116 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) | 113 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
117 | 114 |
118 // Creates a shared memory object as described by the options struct. | 115 // Creates a shared memory object as described by the options struct. |
119 // Returns true on success and false on failure. | 116 // Returns true on success and false on failure. |
120 bool Create(const SharedMemoryCreateOptions& options); | 117 bool Create(const SharedMemoryCreateOptions& options); |
121 | 118 |
122 // Creates and maps an anonymous shared memory segment of size size. | 119 // Creates and maps an anonymous shared memory segment of size size. |
123 // Returns true on success and false on failure. | 120 // Returns true on success and false on failure. |
124 bool CreateAndMapAnonymous(size_t size); | 121 bool CreateAndMapAnonymous(size_t size); |
125 | 122 |
126 #if defined(OS_MACOSX) && !defined(OS_IOS) | |
127 // These two methods are analogs of CreateAndMapAnonymous and CreateAnonymous | |
128 // that force the underlying OS primitive to be a POSIX fd. Do not add new | |
129 // uses of these methods unless absolutely necessary, since constructing a | |
130 // fd-backed SharedMemory object frequently takes 100ms+. | |
131 // http://crbug.com/466437. | |
132 bool CreateAndMapAnonymousPosix(size_t size); | |
133 bool CreateAnonymousPosix(size_t size); | |
134 #endif // defined(OS_MACOSX) && !defined(OS_IOS) | |
135 | |
136 // Creates an anonymous shared memory segment of size size. | 123 // Creates an anonymous shared memory segment of size size. |
137 // Returns true on success and false on failure. | 124 // Returns true on success and false on failure. |
138 bool CreateAnonymous(size_t size) { | 125 bool CreateAnonymous(size_t size) { |
139 SharedMemoryCreateOptions options; | 126 SharedMemoryCreateOptions options; |
140 options.size = size; | 127 options.size = size; |
141 return Create(options); | 128 return Create(options); |
142 } | 129 } |
143 | 130 |
144 #if !defined(OS_MACOSX) || defined(OS_IOS) | 131 #if !defined(OS_MACOSX) || defined(OS_IOS) |
145 // DEPRECATED (crbug.com/345734): | 132 // DEPRECATED (crbug.com/345734): |
(...skipping 104 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // Close(); | 237 // Close(); |
251 // return ok; | 238 // return ok; |
252 // Note that the memory is unmapped by calling this method, regardless of the | 239 // Note that the memory is unmapped by calling this method, regardless of the |
253 // return value. | 240 // return value. |
254 bool GiveToProcess(ProcessHandle process, | 241 bool GiveToProcess(ProcessHandle process, |
255 SharedMemoryHandle* new_handle) { | 242 SharedMemoryHandle* new_handle) { |
256 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); | 243 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); |
257 } | 244 } |
258 | 245 |
259 private: | 246 private: |
260 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) | 247 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \ |
| 248 !(defined(OS_MACOSX) && !defined(OS_IOS)) |
261 bool PrepareMapFile(ScopedFILE fp, ScopedFD readonly); | 249 bool PrepareMapFile(ScopedFILE fp, ScopedFD readonly); |
262 #if !(defined(OS_MACOSX) && !defined(OS_IOS)) | |
263 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); | 250 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); |
264 #endif | 251 #endif |
265 #endif // defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) | |
266 enum ShareMode { | 252 enum ShareMode { |
267 SHARE_READONLY, | 253 SHARE_READONLY, |
268 SHARE_CURRENT_MODE, | 254 SHARE_CURRENT_MODE, |
269 }; | 255 }; |
270 bool ShareToProcessCommon(ProcessHandle process, | 256 bool ShareToProcessCommon(ProcessHandle process, |
271 SharedMemoryHandle* new_handle, | 257 SharedMemoryHandle* new_handle, |
272 bool close_self, | 258 bool close_self, |
273 ShareMode); | 259 ShareMode); |
274 | 260 |
275 #if defined(OS_WIN) | 261 #if defined(OS_WIN) |
276 // If true indicates this came from an external source so needs extra checks | 262 // If true indicates this came from an external source so needs extra checks |
277 // before being mapped. | 263 // before being mapped. |
278 bool external_section_; | 264 bool external_section_; |
279 std::wstring name_; | 265 std::wstring name_; |
280 HANDLE mapped_file_; | 266 HANDLE mapped_file_; |
281 #elif defined(OS_MACOSX) && !defined(OS_IOS) | 267 #elif defined(OS_MACOSX) && !defined(OS_IOS) |
282 // The OS primitive that backs the shared memory region. | 268 // The OS primitive that backs the shared memory region. |
283 SharedMemoryHandle shm_; | 269 SharedMemoryHandle shm_; |
284 | 270 |
285 // The mechanism by which the memory is mapped. Only valid if |memory_| is not | |
286 // |nullptr|. | |
287 SharedMemoryHandle::Type mapped_memory_mechanism_; | |
288 | |
289 int readonly_mapped_file_; | 271 int readonly_mapped_file_; |
290 #elif defined(OS_POSIX) | 272 #elif defined(OS_POSIX) |
291 int mapped_file_; | 273 int mapped_file_; |
292 int readonly_mapped_file_; | 274 int readonly_mapped_file_; |
293 #endif | 275 #endif |
294 size_t mapped_size_; | 276 size_t mapped_size_; |
295 void* memory_; | 277 void* memory_; |
296 bool read_only_; | 278 bool read_only_; |
297 size_t requested_size_; | 279 size_t requested_size_; |
298 | 280 |
299 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 281 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
300 }; | 282 }; |
301 } // namespace base | 283 } // namespace base |
302 | 284 |
303 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 285 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
OLD | NEW |