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 <string> | 8 #include <string> |
9 #include "base/basictypes.h" | 9 #include "base/basictypes.h" |
10 #include "base/process.h" | 10 #include "base/process.h" |
11 | 11 |
| 12 namespace base { |
| 13 |
12 // SharedMemoryHandle is a platform specific type which represents | 14 // SharedMemoryHandle is a platform specific type which represents |
13 // the underlying OS handle to a shared memory segment. | 15 // the underlying OS handle to a shared memory segment. |
14 #if defined(OS_WIN) | 16 #if defined(OS_WIN) |
15 typedef HANDLE SharedMemoryHandle; | 17 typedef HANDLE SharedMemoryHandle; |
16 typedef HANDLE SharedMemoryLock; | 18 typedef HANDLE SharedMemoryLock; |
17 #elif defined(OS_POSIX) | 19 #elif defined(OS_POSIX) |
18 #include <semaphore.h> | 20 #include <semaphore.h> |
19 typedef int SharedMemoryHandle; | 21 typedef int SharedMemoryHandle; |
20 typedef sem_t* SharedMemoryLock; | 22 typedef sem_t* SharedMemoryLock; |
21 #endif | 23 #endif |
22 | 24 |
23 // Platform abstraction for shared memory. Provides a C++ wrapper | 25 // Platform abstraction for shared memory. Provides a C++ wrapper |
24 // around the OS primitive for a memory mapped file. | 26 // around the OS primitive for a memory mapped file. |
25 class SharedMemory { | 27 class SharedMemory { |
26 public: | 28 public: |
27 // Create a new SharedMemory object. | 29 // Create a new SharedMemory object. |
28 SharedMemory(); | 30 SharedMemory(); |
29 | 31 |
30 // Create a new SharedMemory object from an existing, open | 32 // Create a new SharedMemory object from an existing, open |
31 // shared memory file. | 33 // shared memory file. |
32 SharedMemory(SharedMemoryHandle handle, bool read_only); | 34 SharedMemory(SharedMemoryHandle handle, bool read_only); |
33 | 35 |
34 // Create a new SharedMemory object from an existing, open | 36 // Create a new SharedMemory object from an existing, open |
35 // shared memory file that was created by a remote process and not shared | 37 // shared memory file that was created by a remote process and not shared |
36 // to the current process. | 38 // to the current process. |
37 SharedMemory(SharedMemoryHandle handle, bool read_only, | 39 SharedMemory(SharedMemoryHandle handle, bool read_only, |
38 ProcessHandle process); | 40 base::ProcessHandle process); |
39 | 41 |
40 // Destructor. Will close any open files. | 42 // Destructor. Will close any open files. |
41 ~SharedMemory(); | 43 ~SharedMemory(); |
42 | 44 |
43 // Creates or opens a shared memory segment based on a name. | 45 // Creates or opens a shared memory segment based on a name. |
44 // If read_only is true, opens the memory as read-only. | 46 // If read_only is true, opens the memory as read-only. |
45 // If open_existing is true, and the shared memory already exists, | 47 // If open_existing is true, and the shared memory already exists, |
46 // opens the existing shared memory and ignores the size parameter. | 48 // opens the existing shared memory and ignores the size parameter. |
47 // Returns true on success, false on failure. | 49 // Returns true on success, false on failure. |
48 bool Create(const std::wstring& name, bool read_only, bool open_existing, | 50 bool Create(const std::wstring& name, bool read_only, bool open_existing, |
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 // Closes the open shared memory segment. | 84 // Closes the open shared memory segment. |
83 // It is safe to call Close repeatedly. | 85 // It is safe to call Close repeatedly. |
84 void Close(); | 86 void Close(); |
85 | 87 |
86 // Share the shared memory to another process. Attempts | 88 // Share the shared memory to another process. Attempts |
87 // to create a platform-specific new_handle which can be | 89 // to create a platform-specific new_handle which can be |
88 // used in a remote process to access the shared memory | 90 // used in a remote process to access the shared memory |
89 // file. new_handle is an ouput parameter to receive | 91 // file. new_handle is an ouput parameter to receive |
90 // the handle for use in the remote process. | 92 // the handle for use in the remote process. |
91 // Returns true on success, false otherwise. | 93 // Returns true on success, false otherwise. |
92 bool ShareToProcess(ProcessHandle process, | 94 bool ShareToProcess(base::ProcessHandle process, |
93 SharedMemoryHandle* new_handle) { | 95 SharedMemoryHandle* new_handle) { |
94 return ShareToProcessCommon(process, new_handle, false); | 96 return ShareToProcessCommon(process, new_handle, false); |
95 } | 97 } |
96 | 98 |
97 // Logically equivalent to: | 99 // Logically equivalent to: |
98 // bool ok = ShareToProcess(process, new_handle); | 100 // bool ok = ShareToProcess(process, new_handle); |
99 // Close(); | 101 // Close(); |
100 // return ok; | 102 // return ok; |
101 bool GiveToProcess(ProcessHandle process, | 103 bool GiveToProcess(ProcessHandle process, |
102 SharedMemoryHandle* new_handle) { | 104 SharedMemoryHandle* new_handle) { |
(...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 | 142 |
141 ~SharedMemoryAutoLock() { | 143 ~SharedMemoryAutoLock() { |
142 shared_memory_->Unlock(); | 144 shared_memory_->Unlock(); |
143 } | 145 } |
144 | 146 |
145 private: | 147 private: |
146 SharedMemory* shared_memory_; | 148 SharedMemory* shared_memory_; |
147 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); | 149 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); |
148 }; | 150 }; |
149 | 151 |
| 152 } // namespace base |
150 | 153 |
151 #endif // BASE_SHARED_MEMORY_H_ | 154 #endif // BASE_SHARED_MEMORY_H_ |
OLD | NEW |