OLD | NEW |
---|---|
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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_SHARED_MEMORY_H_ | 5 #ifndef BASE_SHARED_MEMORY_H_ |
6 #define BASE_SHARED_MEMORY_H_ | 6 #define BASE_SHARED_MEMORY_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include "build/build_config.h" | 9 #include "build/build_config.h" |
10 | 10 |
(...skipping 20 matching lines...) Expand all Loading... | |
31 #elif defined(OS_POSIX) | 31 #elif defined(OS_POSIX) |
32 // A SharedMemoryId is sufficient to identify a given shared memory segment on a | 32 // A SharedMemoryId is sufficient to identify a given shared memory segment on a |
33 // system, but insufficient to map it. | 33 // system, but insufficient to map it. |
34 typedef FileDescriptor SharedMemoryHandle; | 34 typedef FileDescriptor SharedMemoryHandle; |
35 typedef ino_t SharedMemoryId; | 35 typedef ino_t SharedMemoryId; |
36 // On POSIX, the lock is implemented as a lockf() on the mapped file, | 36 // On POSIX, the lock is implemented as a lockf() on the mapped file, |
37 // so no additional member (or definition of SharedMemoryLock) is | 37 // so no additional member (or definition of SharedMemoryLock) is |
38 // needed. | 38 // needed. |
39 #endif | 39 #endif |
40 | 40 |
41 // Options for creating a shared memory object. | |
42 struct SharedMemoryCreateOptions { | |
43 SharedMemoryCreateOptions() : name(NULL), open_existing(false), | |
44 size(0), executable(false) | |
45 {} | |
Mark Mentovai
2011/12/01 19:31:16
{} belongs on the preceding line, or { on the prec
| |
46 | |
47 explicit SharedMemoryCreateOptions(uint32 isize) : name(NULL), | |
Mark Mentovai
2011/12/01 19:31:16
Why do we need this form of the constructor? Can’t
| |
48 open_existing(false), | |
49 size(isize), | |
50 executable(false) | |
51 {} | |
52 | |
53 // If NULL, the object is anonymous. This pointer is owned by the caller | |
54 // and must live through the call to Create(). | |
55 const std::string* name; | |
56 | |
57 // If true, and the shared memory already exists, Create() will open the | |
58 // existing shared memory and ignore the size parameter. If false, | |
59 // shared memory must not exist. This flag is meaningless unless name is | |
60 // non-NULL. | |
61 bool open_existing; | |
62 | |
63 // Size of the shared memory object to be created. | |
64 // When opening an existing object, this has no effect. | |
65 uint32 size; | |
66 | |
67 // If true, mappings might need to be made executable later. | |
68 bool executable; | |
69 }; | |
70 | |
41 // Platform abstraction for shared memory. Provides a C++ wrapper | 71 // Platform abstraction for shared memory. Provides a C++ wrapper |
42 // around the OS primitive for a memory mapped file. | 72 // around the OS primitive for a memory mapped file. |
43 class BASE_EXPORT SharedMemory { | 73 class BASE_EXPORT SharedMemory { |
44 public: | 74 public: |
45 SharedMemory(); | 75 SharedMemory(); |
46 | 76 |
47 #if defined(OS_WIN) | 77 #if defined(OS_WIN) |
48 // Similar to the default constructor, except that this allows for | 78 // Similar to the default constructor, except that this allows for |
49 // calling Lock() to acquire the named mutex before either Create or Open | 79 // calling Lock() to acquire the named mutex before either Create or Open |
50 // are called on Windows. | 80 // are called on Windows. |
(...skipping 16 matching lines...) Expand all Loading... | |
67 // Return true iff the given handle is valid (i.e. not the distingished | 97 // Return true iff the given handle is valid (i.e. not the distingished |
68 // invalid value; NULL for a HANDLE and -1 for a file descriptor) | 98 // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
69 static bool IsHandleValid(const SharedMemoryHandle& handle); | 99 static bool IsHandleValid(const SharedMemoryHandle& handle); |
70 | 100 |
71 // Returns invalid handle (see comment above for exact definition). | 101 // Returns invalid handle (see comment above for exact definition). |
72 static SharedMemoryHandle NULLHandle(); | 102 static SharedMemoryHandle NULLHandle(); |
73 | 103 |
74 // Closes a shared memory handle. | 104 // Closes a shared memory handle. |
75 static void CloseHandle(const SharedMemoryHandle& handle); | 105 static void CloseHandle(const SharedMemoryHandle& handle); |
76 | 106 |
107 // Creates a shared memory object as described by the options struct. | |
108 // Returns true on success and false on failure. | |
109 bool Create(const SharedMemoryCreateOptions& options); | |
110 | |
77 // Creates and maps an anonymous shared memory segment of size size. | 111 // Creates and maps an anonymous shared memory segment of size size. |
78 // Returns true on success and false on failure. | 112 // Returns true on success and false on failure. |
79 bool CreateAndMapAnonymous(uint32 size); | 113 bool CreateAndMapAnonymous(uint32 size); |
80 | 114 |
81 // Creates an anonymous shared memory segment of size size. | 115 // Creates an anonymous shared memory segment of size size. |
82 // Returns true on success and false on failure. | 116 // Returns true on success and false on failure. |
83 bool CreateAnonymous(uint32 size); | 117 bool CreateAnonymous(uint32 size) { |
118 SharedMemoryCreateOptions options(size); | |
119 return Create(options); | |
120 } | |
84 | 121 |
85 // Creates or opens a shared memory segment based on a name. | 122 // Creates or opens a shared memory segment based on a name. |
86 // If open_existing is true, and the shared memory already exists, | 123 // If open_existing is true, and the shared memory already exists, |
87 // opens the existing shared memory and ignores the size parameter. | 124 // opens the existing shared memory and ignores the size parameter. |
88 // If open_existing is false, shared memory must not exist. | 125 // If open_existing is false, shared memory must not exist. |
89 // size is the size of the block to be created. | 126 // size is the size of the block to be created. |
90 // Returns true on success, false on failure. | 127 // Returns true on success, false on failure. |
91 bool CreateNamed(const std::string& name, bool open_existing, uint32 size); | 128 bool CreateNamed(const std::string& name, bool open_existing, uint32 size) { |
129 SharedMemoryCreateOptions options(size); | |
130 options.name = &name; | |
131 options.open_existing = open_existing; | |
132 return Create(options); | |
133 } | |
92 | 134 |
93 // Deletes resources associated with a shared memory segment based on name. | 135 // Deletes resources associated with a shared memory segment based on name. |
94 // Not all platforms require this call. | 136 // Not all platforms require this call. |
95 bool Delete(const std::string& name); | 137 bool Delete(const std::string& name); |
96 | 138 |
97 // Opens a shared memory segment based on a name. | 139 // Opens a shared memory segment based on a name. |
98 // If read_only is true, opens for read-only access. | 140 // If read_only is true, opens for read-only access. |
99 // Returns true on success, false on failure. | 141 // Returns true on success, false on failure. |
100 bool Open(const std::string& name, bool read_only); | 142 bool Open(const std::string& name, bool read_only); |
101 | 143 |
(...skipping 124 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
226 } | 268 } |
227 | 269 |
228 private: | 270 private: |
229 SharedMemory* shared_memory_; | 271 SharedMemory* shared_memory_; |
230 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); | 272 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); |
231 }; | 273 }; |
232 | 274 |
233 } // namespace base | 275 } // namespace base |
234 | 276 |
235 #endif // BASE_SHARED_MEMORY_H_ | 277 #endif // BASE_SHARED_MEMORY_H_ |
OLD | NEW |