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 "build/build_config.h" | 8 #include "build/build_config.h" |
9 | 9 |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #if defined(OS_POSIX) | 12 #if defined(OS_POSIX) |
13 #include <stdio.h> | 13 #include <stdio.h> |
14 #include <sys/types.h> | 14 #include <sys/types.h> |
15 #include <semaphore.h> | 15 #include <semaphore.h> |
16 #endif | 16 #endif |
17 | 17 |
18 #include "base/base_export.h" | 18 #include "base/base_export.h" |
19 #include "base/basictypes.h" | 19 #include "base/basictypes.h" |
| 20 #include "base/memory/shared_memory_handle.h" |
20 #include "base/process/process_handle.h" | 21 #include "base/process/process_handle.h" |
21 | 22 |
22 #if defined(OS_POSIX) | 23 #if defined(OS_POSIX) |
23 #include "base/file_descriptor_posix.h" | 24 #include "base/file_descriptor_posix.h" |
24 #include "base/files/file_util.h" | 25 #include "base/files/file_util.h" |
25 #include "base/files/scoped_file.h" | 26 #include "base/files/scoped_file.h" |
26 #endif | 27 #endif |
27 | 28 |
28 namespace base { | 29 namespace base { |
29 | 30 |
30 class FilePath; | 31 class FilePath; |
31 | 32 |
32 // SharedMemoryHandle is a platform specific type which represents | |
33 // the underlying OS handle to a shared memory segment. | |
34 #if defined(OS_WIN) | |
35 typedef HANDLE SharedMemoryHandle; | |
36 #elif defined(OS_POSIX) | |
37 typedef FileDescriptor SharedMemoryHandle; | |
38 #endif | |
39 | |
40 // Options for creating a shared memory object. | 33 // Options for creating a shared memory object. |
41 struct SharedMemoryCreateOptions { | 34 struct SharedMemoryCreateOptions { |
42 SharedMemoryCreateOptions() | 35 SharedMemoryCreateOptions() |
43 : name_deprecated(NULL), | 36 : name_deprecated(NULL), |
44 size(0), | 37 size(0), |
45 open_existing_deprecated(false), | 38 open_existing_deprecated(false), |
46 executable(false), | 39 executable(false), |
47 share_read_only(false) {} | 40 share_read_only(false) {} |
48 | 41 |
49 // DEPRECATED (crbug.com/345734): | 42 // DEPRECATED (crbug.com/345734): |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 explicit SharedMemory(const std::wstring& name); | 75 explicit SharedMemory(const std::wstring& name); |
83 #endif | 76 #endif |
84 | 77 |
85 // Create a new SharedMemory object from an existing, open | 78 // Create a new SharedMemory object from an existing, open |
86 // shared memory file. | 79 // shared memory file. |
87 // | 80 // |
88 // WARNING: This does not reduce the OS-level permissions on the handle; it | 81 // WARNING: This does not reduce the OS-level permissions on the handle; it |
89 // only affects how the SharedMemory will be mmapped. Use | 82 // only affects how the SharedMemory will be mmapped. Use |
90 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK | 83 // ShareReadOnlyToProcess to drop permissions. TODO(jln,jyasskin): DCHECK |
91 // that |read_only| matches the permissions of the handle. | 84 // that |read_only| matches the permissions of the handle. |
92 SharedMemory(SharedMemoryHandle handle, bool read_only); | 85 SharedMemory(const SharedMemoryHandle& handle, bool read_only); |
93 | 86 |
94 // Create a new SharedMemory object from an existing, open | 87 // Create a new SharedMemory object from an existing, open |
95 // shared memory file that was created by a remote process and not shared | 88 // shared memory file that was created by a remote process and not shared |
96 // to the current process. | 89 // to the current process. |
97 SharedMemory(SharedMemoryHandle handle, bool read_only, | 90 SharedMemory(const SharedMemoryHandle& handle, |
| 91 bool read_only, |
98 ProcessHandle process); | 92 ProcessHandle process); |
99 | 93 |
100 // Closes any open files. | 94 // Closes any open files. |
101 ~SharedMemory(); | 95 ~SharedMemory(); |
102 | 96 |
103 // 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 |
104 // 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) |
105 static bool IsHandleValid(const SharedMemoryHandle& handle); | 99 static bool IsHandleValid(const SharedMemoryHandle& handle); |
106 | 100 |
107 // Returns invalid handle (see comment above for exact definition). | 101 // Returns invalid handle (see comment above for exact definition). |
(...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
250 // bool ok = ShareToProcess(process, new_handle); | 244 // bool ok = ShareToProcess(process, new_handle); |
251 // Close(); | 245 // Close(); |
252 // return ok; | 246 // return ok; |
253 // Note that the memory is unmapped by calling this method, regardless of the | 247 // Note that the memory is unmapped by calling this method, regardless of the |
254 // return value. | 248 // return value. |
255 bool GiveToProcess(ProcessHandle process, | 249 bool GiveToProcess(ProcessHandle process, |
256 SharedMemoryHandle* new_handle) { | 250 SharedMemoryHandle* new_handle) { |
257 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); | 251 return ShareToProcessCommon(process, new_handle, true, SHARE_CURRENT_MODE); |
258 } | 252 } |
259 | 253 |
260 // DEPRECATED (crbug.com/345734): | |
261 // Locks the shared memory. | |
262 // | |
263 // WARNING: on POSIX the memory locking primitive only works across | |
264 // processes, not across threads. The LockDeprecated method is not currently | |
265 // used in inner loops, so we protect against multiple threads in a | |
266 // critical section using a class global lock. | |
267 void LockDeprecated(); | |
268 | |
269 // DEPRECATED (crbug.com/345734): | |
270 // Releases the shared memory lock. | |
271 void UnlockDeprecated(); | |
272 | |
273 private: | 254 private: |
274 #if defined(OS_POSIX) && !defined(OS_NACL) | 255 #if defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
275 #if !defined(OS_ANDROID) | |
276 bool PrepareMapFile(ScopedFILE fp, ScopedFD readonly); | 256 bool PrepareMapFile(ScopedFILE fp, ScopedFD readonly); |
277 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); | 257 bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); |
278 #endif | 258 #endif // defined(OS_POSIX) && !defined(OS_NACL) && !defined(OS_ANDROID) |
279 void LockOrUnlockCommon(int function); | |
280 #endif // defined(OS_POSIX) && !defined(OS_NACL) | |
281 enum ShareMode { | 259 enum ShareMode { |
282 SHARE_READONLY, | 260 SHARE_READONLY, |
283 SHARE_CURRENT_MODE, | 261 SHARE_CURRENT_MODE, |
284 }; | 262 }; |
285 bool ShareToProcessCommon(ProcessHandle process, | 263 bool ShareToProcessCommon(ProcessHandle process, |
286 SharedMemoryHandle* new_handle, | 264 SharedMemoryHandle* new_handle, |
287 bool close_self, | 265 bool close_self, |
288 ShareMode); | 266 ShareMode); |
289 | 267 |
290 #if defined(OS_WIN) | 268 #if defined(OS_WIN) |
291 std::wstring name_; | 269 std::wstring name_; |
292 HANDLE mapped_file_; | 270 HANDLE mapped_file_; |
293 #elif defined(OS_POSIX) | 271 #elif defined(OS_POSIX) |
294 int mapped_file_; | 272 int mapped_file_; |
295 int readonly_mapped_file_; | 273 int readonly_mapped_file_; |
296 #endif | 274 #endif |
297 size_t mapped_size_; | 275 size_t mapped_size_; |
298 void* memory_; | 276 void* memory_; |
299 bool read_only_; | 277 bool read_only_; |
300 size_t requested_size_; | 278 size_t requested_size_; |
301 #if !defined(OS_POSIX) | |
302 HANDLE lock_; | |
303 #endif | |
304 | 279 |
305 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 280 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
306 }; | 281 }; |
307 | |
308 // DEPRECATED (crbug.com/345734): | |
309 // A helper class that acquires the shared memory lock while | |
310 // the SharedMemoryAutoLockDeprecated is in scope. | |
311 class SharedMemoryAutoLockDeprecated { | |
312 public: | |
313 explicit SharedMemoryAutoLockDeprecated(SharedMemory* shared_memory) | |
314 : shared_memory_(shared_memory) { | |
315 shared_memory_->LockDeprecated(); | |
316 } | |
317 | |
318 ~SharedMemoryAutoLockDeprecated() { | |
319 shared_memory_->UnlockDeprecated(); | |
320 } | |
321 | |
322 private: | |
323 SharedMemory* shared_memory_; | |
324 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLockDeprecated); | |
325 }; | |
326 | |
327 } // namespace base | 282 } // namespace base |
328 | 283 |
329 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 284 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
OLD | NEW |