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 "base/basictypes.h" | 9 #include "base/basictypes.h" |
9 #include "base/process_util.h" | 10 #include "base/process.h" |
10 | 11 |
11 // SharedMemoryHandle is a platform specific type which represents | 12 // SharedMemoryHandle is a platform specific type which represents |
12 // the underlying OS handle to a shared memory segment. | 13 // the underlying OS handle to a shared memory segment. |
13 #if defined(OS_WIN) | 14 #if defined(OS_WIN) |
14 typedef HANDLE SharedMemoryHandle; | 15 typedef HANDLE SharedMemoryHandle; |
15 typedef HANDLE SharedMemoryLock; | 16 typedef HANDLE SharedMemoryLock; |
16 #elif defined(OS_POSIX) | 17 #elif defined(OS_POSIX) |
17 #include <semaphore.h> | 18 #include <semaphore.h> |
18 typedef int SharedMemoryHandle; | 19 typedef int SharedMemoryHandle; |
19 typedef sem_t* SharedMemoryLock; | 20 typedef sem_t* SharedMemoryLock; |
(...skipping 17 matching lines...) Expand all Loading... |
37 ProcessHandle process); | 38 ProcessHandle process); |
38 | 39 |
39 // Destructor. Will close any open files. | 40 // Destructor. Will close any open files. |
40 ~SharedMemory(); | 41 ~SharedMemory(); |
41 | 42 |
42 // Creates or opens a shared memory segment based on a name. | 43 // Creates or opens a shared memory segment based on a name. |
43 // If read_only is true, opens the memory as read-only. | 44 // If read_only is true, opens the memory as read-only. |
44 // If open_existing is true, and the shared memory already exists, | 45 // If open_existing is true, and the shared memory already exists, |
45 // opens the existing shared memory and ignores the size parameter. | 46 // opens the existing shared memory and ignores the size parameter. |
46 // Returns true on success, false on failure. | 47 // Returns true on success, false on failure. |
47 bool Create(const std::wstring &name, bool read_only, bool open_existing, | 48 bool Create(const std::wstring& name, bool read_only, bool open_existing, |
48 size_t size); | 49 size_t size); |
49 | 50 |
50 // Opens a shared memory segment based on a name. | 51 // Opens a shared memory segment based on a name. |
51 // If read_only is true, opens for read-only access. | 52 // If read_only is true, opens for read-only access. |
52 // Returns true on success, false on failure. | 53 // Returns true on success, false on failure. |
53 bool Open(const std::wstring &name, bool read_only); | 54 bool Open(const std::wstring& name, bool read_only); |
54 | 55 |
55 // Maps the shared memory into the caller's address space. | 56 // Maps the shared memory into the caller's address space. |
56 // Returns true on success, false otherwise. The memory address | 57 // Returns true on success, false otherwise. The memory address |
57 // is accessed via the memory() accessor. | 58 // is accessed via the memory() accessor. |
58 bool Map(size_t bytes); | 59 bool Map(size_t bytes); |
59 | 60 |
60 // Unmaps the shared memory from the caller's address space. | 61 // Unmaps the shared memory from the caller's address space. |
61 // Returns true if successful; returns false on error or if the | 62 // Returns true if successful; returns false on error or if the |
62 // memory is not mapped. | 63 // memory is not mapped. |
63 bool Unmap(); | 64 bool Unmap(); |
(...skipping 18 matching lines...) Expand all Loading... |
82 // It is safe to call Close repeatedly. | 83 // It is safe to call Close repeatedly. |
83 void Close(); | 84 void Close(); |
84 | 85 |
85 // Share the shared memory to another process. Attempts | 86 // Share the shared memory to another process. Attempts |
86 // to create a platform-specific new_handle which can be | 87 // to create a platform-specific new_handle which can be |
87 // used in a remote process to access the shared memory | 88 // used in a remote process to access the shared memory |
88 // file. new_handle is an ouput parameter to receive | 89 // file. new_handle is an ouput parameter to receive |
89 // the handle for use in the remote process. | 90 // the handle for use in the remote process. |
90 // Returns true on success, false otherwise. | 91 // Returns true on success, false otherwise. |
91 bool ShareToProcess(ProcessHandle process, | 92 bool ShareToProcess(ProcessHandle process, |
92 SharedMemoryHandle *new_handle) { | 93 SharedMemoryHandle* new_handle) { |
93 return ShareToProcessCommon(process, new_handle, false); | 94 return ShareToProcessCommon(process, new_handle, false); |
94 } | 95 } |
95 | 96 |
96 // Logically equivalent to: | 97 // Logically equivalent to: |
97 // bool ok = ShareToProcess(process, new_handle); | 98 // bool ok = ShareToProcess(process, new_handle); |
98 // Close(); | 99 // Close(); |
99 // return ok; | 100 // return ok; |
100 bool GiveToProcess(ProcessHandle process, | 101 bool GiveToProcess(ProcessHandle process, |
101 SharedMemoryHandle *new_handle) { | 102 SharedMemoryHandle* new_handle) { |
102 return ShareToProcessCommon(process, new_handle, true); | 103 return ShareToProcessCommon(process, new_handle, true); |
103 } | 104 } |
104 | 105 |
105 // Lock the shared memory. | 106 // Lock the shared memory. |
106 // This is a cross-process lock which may be recursively | 107 // This is a cross-process lock which may be recursively |
107 // locked by the same thread. | 108 // locked by the same thread. |
108 void Lock(); | 109 void Lock(); |
109 | 110 |
110 // Release the shared memory lock. | 111 // Release the shared memory lock. |
111 void Unlock(); | 112 void Unlock(); |
112 | 113 |
113 private: | 114 private: |
114 #if defined(OS_POSIX) | 115 #if defined(OS_POSIX) |
115 bool CreateOrOpen(const std::wstring &name, int posix_flags); | 116 bool CreateOrOpen(const std::wstring &name, int posix_flags); |
116 #endif | 117 #endif |
117 bool ShareToProcessCommon(ProcessHandle process, | 118 bool ShareToProcessCommon(ProcessHandle process, |
118 SharedMemoryHandle *new_handle, bool close_self); | 119 SharedMemoryHandle* new_handle, |
| 120 bool close_self); |
119 | 121 |
120 std::wstring name_; | 122 std::wstring name_; |
121 SharedMemoryHandle mapped_file_; | 123 SharedMemoryHandle mapped_file_; |
122 void* memory_; | 124 void* memory_; |
123 bool read_only_; | 125 bool read_only_; |
124 size_t max_size_; | 126 size_t max_size_; |
125 SharedMemoryLock lock_; | 127 SharedMemoryLock lock_; |
126 | 128 |
127 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); | 129 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); |
128 }; | 130 }; |
(...skipping 11 matching lines...) Expand all Loading... |
140 shared_memory_->Unlock(); | 142 shared_memory_->Unlock(); |
141 } | 143 } |
142 | 144 |
143 private: | 145 private: |
144 SharedMemory* shared_memory_; | 146 SharedMemory* shared_memory_; |
145 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); | 147 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); |
146 }; | 148 }; |
147 | 149 |
148 | 150 |
149 #endif // BASE_SHARED_MEMORY_H_ | 151 #endif // BASE_SHARED_MEMORY_H_ |
150 | |
OLD | NEW |