Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(217)

Side by Side Diff: base/memory/shared_memory.h

Issue 2555483002: Add POSIX shared memory support for Mac (Closed)
Patch Set: Restructure CreateAnonymousSharedMemory Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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
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);
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after
248 // Note that the memory is unmapped by calling this method, regardless of the 251 // Note that the memory is unmapped by calling this method, regardless of the
249 // return value. 252 // return value.
250 bool GiveToProcess(ProcessHandle process, 253 bool GiveToProcess(ProcessHandle process,
251 SharedMemoryHandle* new_handle) { 254 SharedMemoryHandle* new_handle) {
252 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); 255 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE);
253 } 256 }
254 257
255 private: 258 private:
256 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \ 259 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) && \
257 !(defined(OS_MACOSX) && !defined(OS_IOS)) 260 !(defined(OS_MACOSX) && !defined(OS_IOS))
258 bool PrepareMapFile(ScopedFILE fp, ScopedFD readonly);
259 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); 261 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path);
260 #endif 262 #endif
263
261 enum ShareMode { 264 enum ShareMode {
262 SHARE_READONLY, 265 SHARE_READONLY,
263 SHARE_CURRENT_MODE, 266 SHARE_CURRENT_MODE,
264 }; 267 };
268
269 bool Share(SharedMemoryHandle* new_handle, ShareMode share_mode);
270
265 bool ShareToProcessCommon(ProcessHandle process, 271 bool ShareToProcessCommon(ProcessHandle process,
266 SharedMemoryHandle* new_handle, 272 SharedMemoryHandle* new_handle,
267 bool close_self, 273 bool close_self,
268 ShareMode); 274 ShareMode);
269 275
270 #if defined(OS_WIN) 276 #if defined(OS_WIN)
271 // If true indicates this came from an external source so needs extra checks 277 // If true indicates this came from an external source so needs extra checks
272 // before being mapped. 278 // before being mapped.
273 bool external_section_; 279 bool external_section_;
274 std::wstring name_; 280 std::wstring name_;
275 win::ScopedHandle mapped_file_; 281 win::ScopedHandle mapped_file_;
276 #elif defined(OS_MACOSX) && !defined(OS_IOS) 282 #elif defined(OS_MACOSX) && !defined(OS_IOS)
277 // The OS primitive that backs the shared memory region. 283 // The OS primitive that backs the shared memory region.
278 SharedMemoryHandle shm_; 284 SharedMemoryHandle shm_;
285
286 // The mechanism by which the memory is mapped. Only valid if |memory_| is not
287 // |nullptr|.
288 SharedMemoryHandle::Type mapped_memory_mechanism_;
289
290 int readonly_mapped_file_;
279 #elif defined(OS_POSIX) 291 #elif defined(OS_POSIX)
280 int mapped_file_; 292 int mapped_file_;
281 int readonly_mapped_file_; 293 int readonly_mapped_file_;
282 #endif 294 #endif
283 size_t mapped_size_; 295 size_t mapped_size_;
284 void* memory_; 296 void* memory_;
285 bool read_only_; 297 bool read_only_;
286 size_t requested_size_; 298 size_t requested_size_;
287 299
288 DISALLOW_COPY_AND_ASSIGN(SharedMemory); 300 DISALLOW_COPY_AND_ASSIGN(SharedMemory);
289 }; 301 };
290 } // namespace base 302 } // namespace base
291 303
292 #endif // BASE_MEMORY_SHARED_MEMORY_H_ 304 #endif // BASE_MEMORY_SHARED_MEMORY_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698