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 <semaphore.h> | 11 #include <semaphore.h> |
| 12 #include "base/file_descriptor_posix.h" |
12 #endif | 13 #endif |
13 #include <string> | 14 #include <string> |
14 | 15 |
15 #include "base/basictypes.h" | 16 #include "base/basictypes.h" |
16 #include "base/process.h" | 17 #include "base/process.h" |
17 | 18 |
18 namespace base { | 19 namespace base { |
19 | 20 |
20 // SharedMemoryHandle is a platform specific type which represents | 21 // SharedMemoryHandle is a platform specific type which represents |
21 // the underlying OS handle to a shared memory segment. | 22 // the underlying OS handle to a shared memory segment. |
22 #if defined(OS_WIN) | 23 #if defined(OS_WIN) |
23 typedef HANDLE SharedMemoryHandle; | 24 typedef HANDLE SharedMemoryHandle; |
24 typedef HANDLE SharedMemoryLock; | 25 typedef HANDLE SharedMemoryLock; |
25 #elif defined(OS_POSIX) | 26 #elif defined(OS_POSIX) |
26 typedef int SharedMemoryHandle; | 27 typedef FileDescriptor SharedMemoryHandle; |
27 // On POSIX, the lock is implemented as a lockf() on the mapped file, | 28 // On POSIX, the lock is implemented as a lockf() on the mapped file, |
28 // so no additional member (or definition of SharedMemoryLock) is | 29 // so no additional member (or definition of SharedMemoryLock) is |
29 // needed. | 30 // needed. |
30 #endif | 31 #endif |
31 | 32 |
32 // Platform abstraction for shared memory. Provides a C++ wrapper | 33 // Platform abstraction for shared memory. Provides a C++ wrapper |
33 // around the OS primitive for a memory mapped file. | 34 // around the OS primitive for a memory mapped file. |
34 class SharedMemory { | 35 class SharedMemory { |
35 public: | 36 public: |
36 // Create a new SharedMemory object. | 37 // Create a new SharedMemory object. |
37 SharedMemory(); | 38 SharedMemory(); |
38 | 39 |
39 // Create a new SharedMemory object from an existing, open | 40 // Create a new SharedMemory object from an existing, open |
40 // shared memory file. | 41 // shared memory file. |
41 SharedMemory(SharedMemoryHandle handle, bool read_only); | 42 SharedMemory(SharedMemoryHandle handle, bool read_only); |
42 | 43 |
43 // Create a new SharedMemory object from an existing, open | 44 // Create a new SharedMemory object from an existing, open |
44 // shared memory file that was created by a remote process and not shared | 45 // shared memory file that was created by a remote process and not shared |
45 // to the current process. | 46 // to the current process. |
46 SharedMemory(SharedMemoryHandle handle, bool read_only, | 47 SharedMemory(SharedMemoryHandle handle, bool read_only, |
47 base::ProcessHandle process); | 48 base::ProcessHandle process); |
48 | 49 |
49 // Destructor. Will close any open files. | 50 // Destructor. Will close any open files. |
50 ~SharedMemory(); | 51 ~SharedMemory(); |
51 | 52 |
| 53 // Return true iff the given handle is valid (i.e. not the distingished |
| 54 // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
| 55 static bool IsHandleValid(const SharedMemoryHandle& handle); |
| 56 |
52 // Creates or opens a shared memory segment based on a name. | 57 // Creates or opens a shared memory segment based on a name. |
53 // If read_only is true, opens the memory as read-only. | 58 // If read_only is true, opens the memory as read-only. |
54 // If open_existing is true, and the shared memory already exists, | 59 // If open_existing is true, and the shared memory already exists, |
55 // opens the existing shared memory and ignores the size parameter. | 60 // opens the existing shared memory and ignores the size parameter. |
56 // If name is the empty string, use a unique name. | 61 // If name is the empty string, use a unique name. |
57 // Returns true on success, false on failure. | 62 // Returns true on success, false on failure. |
58 bool Create(const std::wstring& name, bool read_only, bool open_existing, | 63 bool Create(const std::wstring& name, bool read_only, bool open_existing, |
59 size_t size); | 64 size_t size); |
60 | 65 |
61 // Deletes resources associated with a shared memory segment based on name. | 66 // Deletes resources associated with a shared memory segment based on name. |
(...skipping 23 matching lines...) Expand all Loading... |
85 // Returns 0 if not opened or unknown. | 90 // Returns 0 if not opened or unknown. |
86 size_t max_size() const { return max_size_; } | 91 size_t max_size() const { return max_size_; } |
87 | 92 |
88 // Gets a pointer to the opened memory space if it has been | 93 // Gets a pointer to the opened memory space if it has been |
89 // Mapped via Map(). Returns NULL if it is not mapped. | 94 // Mapped via Map(). Returns NULL if it is not mapped. |
90 void *memory() const { return memory_; } | 95 void *memory() const { return memory_; } |
91 | 96 |
92 // Get access to the underlying OS handle for this segment. | 97 // Get access to the underlying OS handle for this segment. |
93 // Use of this handle for anything other than an opaque | 98 // Use of this handle for anything other than an opaque |
94 // identifier is not portable. | 99 // identifier is not portable. |
95 SharedMemoryHandle handle() const { return mapped_file_; } | 100 SharedMemoryHandle handle() const; |
96 | 101 |
97 // Closes the open shared memory segment. | 102 // Closes the open shared memory segment. |
98 // It is safe to call Close repeatedly. | 103 // It is safe to call Close repeatedly. |
99 void Close(); | 104 void Close(); |
100 | 105 |
101 // Share the shared memory to another process. Attempts | 106 // Share the shared memory to another process. Attempts |
102 // to create a platform-specific new_handle which can be | 107 // to create a platform-specific new_handle which can be |
103 // used in a remote process to access the shared memory | 108 // used in a remote process to access the shared memory |
104 // file. new_handle is an ouput parameter to receive | 109 // file. new_handle is an ouput parameter to receive |
105 // the handle for use in the remote process. | 110 // the handle for use in the remote process. |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
140 bool FilenameForMemoryName(const std::wstring &memname, | 145 bool FilenameForMemoryName(const std::wstring &memname, |
141 std::wstring *filename); | 146 std::wstring *filename); |
142 void LockOrUnlockCommon(int function); | 147 void LockOrUnlockCommon(int function); |
143 | 148 |
144 #endif | 149 #endif |
145 bool ShareToProcessCommon(ProcessHandle process, | 150 bool ShareToProcessCommon(ProcessHandle process, |
146 SharedMemoryHandle* new_handle, | 151 SharedMemoryHandle* new_handle, |
147 bool close_self); | 152 bool close_self); |
148 | 153 |
149 std::wstring name_; | 154 std::wstring name_; |
150 SharedMemoryHandle mapped_file_; | 155 #if defined(OS_WIN) |
| 156 HANDLE mapped_file_; |
| 157 #elif defined(OS_POSIX) |
| 158 int mapped_file_; |
| 159 #endif |
151 void* memory_; | 160 void* memory_; |
152 bool read_only_; | 161 bool read_only_; |
153 size_t max_size_; | 162 size_t max_size_; |
154 #if !defined(OS_POSIX) | 163 #if !defined(OS_POSIX) |
155 SharedMemoryLock lock_; | 164 SharedMemoryLock lock_; |
156 #endif | 165 #endif |
157 | 166 |
158 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); | 167 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); |
159 }; | 168 }; |
160 | 169 |
(...skipping 11 matching lines...) Expand all Loading... |
172 } | 181 } |
173 | 182 |
174 private: | 183 private: |
175 SharedMemory* shared_memory_; | 184 SharedMemory* shared_memory_; |
176 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); | 185 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); |
177 }; | 186 }; |
178 | 187 |
179 } // namespace base | 188 } // namespace base |
180 | 189 |
181 #endif // BASE_SHARED_MEMORY_H_ | 190 #endif // BASE_SHARED_MEMORY_H_ |
OLD | NEW |