OLD | NEW |
1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 | 7 |
8 #include "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #if defined(OS_POSIX) | 10 #if defined(OS_POSIX) |
| 11 #include <sys/types.h> |
11 #include <semaphore.h> | 12 #include <semaphore.h> |
12 #include "base/file_descriptor_posix.h" | 13 #include "base/file_descriptor_posix.h" |
13 #endif | 14 #endif |
14 #include <string> | 15 #include <string> |
15 | 16 |
16 #include "base/basictypes.h" | 17 #include "base/basictypes.h" |
17 #include "base/process.h" | 18 #include "base/process.h" |
18 | 19 |
19 namespace base { | 20 namespace base { |
20 | 21 |
21 // SharedMemoryHandle is a platform specific type which represents | 22 // SharedMemoryHandle is a platform specific type which represents |
22 // the underlying OS handle to a shared memory segment. | 23 // the underlying OS handle to a shared memory segment. |
23 #if defined(OS_WIN) | 24 #if defined(OS_WIN) |
24 typedef HANDLE SharedMemoryHandle; | 25 typedef HANDLE SharedMemoryHandle; |
25 typedef HANDLE SharedMemoryLock; | 26 typedef HANDLE SharedMemoryLock; |
26 #elif defined(OS_POSIX) | 27 #elif defined(OS_POSIX) |
| 28 // A SharedMemoryId is sufficient to identify a given shared memory segment on a |
| 29 // system, but insufficient to map it. |
27 typedef FileDescriptor SharedMemoryHandle; | 30 typedef FileDescriptor SharedMemoryHandle; |
| 31 typedef ino_t SharedMemoryId; |
28 // On POSIX, the lock is implemented as a lockf() on the mapped file, | 32 // On POSIX, the lock is implemented as a lockf() on the mapped file, |
29 // so no additional member (or definition of SharedMemoryLock) is | 33 // so no additional member (or definition of SharedMemoryLock) is |
30 // needed. | 34 // needed. |
31 #endif | 35 #endif |
32 | 36 |
33 // Platform abstraction for shared memory. Provides a C++ wrapper | 37 // Platform abstraction for shared memory. Provides a C++ wrapper |
34 // around the OS primitive for a memory mapped file. | 38 // around the OS primitive for a memory mapped file. |
35 class SharedMemory { | 39 class SharedMemory { |
36 public: | 40 public: |
37 // Create a new SharedMemory object. | 41 // Create a new SharedMemory object. |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
92 | 96 |
93 // Gets a pointer to the opened memory space if it has been | 97 // Gets a pointer to the opened memory space if it has been |
94 // Mapped via Map(). Returns NULL if it is not mapped. | 98 // Mapped via Map(). Returns NULL if it is not mapped. |
95 void *memory() const { return memory_; } | 99 void *memory() const { return memory_; } |
96 | 100 |
97 // Get access to the underlying OS handle for this segment. | 101 // Get access to the underlying OS handle for this segment. |
98 // Use of this handle for anything other than an opaque | 102 // Use of this handle for anything other than an opaque |
99 // identifier is not portable. | 103 // identifier is not portable. |
100 SharedMemoryHandle handle() const; | 104 SharedMemoryHandle handle() const; |
101 | 105 |
| 106 #if defined(OS_POSIX) |
| 107 // Return a unique identifier for this shared memory segment. Inode numbers |
| 108 // are technically only unique to a single filesystem. However, we always |
| 109 // allocate shared memory backing files from the same directory, so will end |
| 110 // up on the same filesystem. |
| 111 SharedMemoryId id() const { return inode_; } |
| 112 #endif |
| 113 |
102 // Closes the open shared memory segment. | 114 // Closes the open shared memory segment. |
103 // It is safe to call Close repeatedly. | 115 // It is safe to call Close repeatedly. |
104 void Close(); | 116 void Close(); |
105 | 117 |
106 // Share the shared memory to another process. Attempts | 118 // Share the shared memory to another process. Attempts |
107 // to create a platform-specific new_handle which can be | 119 // to create a platform-specific new_handle which can be |
108 // used in a remote process to access the shared memory | 120 // used in a remote process to access the shared memory |
109 // file. new_handle is an ouput parameter to receive | 121 // file. new_handle is an ouput parameter to receive |
110 // the handle for use in the remote process. | 122 // the handle for use in the remote process. |
111 // Returns true on success, false otherwise. | 123 // Returns true on success, false otherwise. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
146 std::wstring *filename); | 158 std::wstring *filename); |
147 void LockOrUnlockCommon(int function); | 159 void LockOrUnlockCommon(int function); |
148 | 160 |
149 #endif | 161 #endif |
150 bool ShareToProcessCommon(ProcessHandle process, | 162 bool ShareToProcessCommon(ProcessHandle process, |
151 SharedMemoryHandle* new_handle, | 163 SharedMemoryHandle* new_handle, |
152 bool close_self); | 164 bool close_self); |
153 | 165 |
154 std::wstring name_; | 166 std::wstring name_; |
155 #if defined(OS_WIN) | 167 #if defined(OS_WIN) |
156 HANDLE mapped_file_; | 168 HANDLE mapped_file_; |
157 #elif defined(OS_POSIX) | 169 #elif defined(OS_POSIX) |
158 int mapped_file_; | 170 int mapped_file_; |
| 171 ino_t inode_; |
159 #endif | 172 #endif |
160 void* memory_; | 173 void* memory_; |
161 bool read_only_; | 174 bool read_only_; |
162 size_t max_size_; | 175 size_t max_size_; |
163 #if !defined(OS_POSIX) | 176 #if !defined(OS_POSIX) |
164 SharedMemoryLock lock_; | 177 SharedMemoryLock lock_; |
165 #endif | 178 #endif |
166 | 179 |
167 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); | 180 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); |
168 }; | 181 }; |
(...skipping 12 matching lines...) Expand all Loading... |
181 } | 194 } |
182 | 195 |
183 private: | 196 private: |
184 SharedMemory* shared_memory_; | 197 SharedMemory* shared_memory_; |
185 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); | 198 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); |
186 }; | 199 }; |
187 | 200 |
188 } // namespace base | 201 } // namespace base |
189 | 202 |
190 #endif // BASE_SHARED_MEMORY_H_ | 203 #endif // BASE_SHARED_MEMORY_H_ |
OLD | NEW |