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