| 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 #endif | 12 #endif |
| 13 #include <string> | 13 #include <string> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/process.h" | 16 #include "base/process.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 | 19 |
| 20 // SharedMemoryHandle is a platform specific type which represents | 20 // SharedMemoryHandle is a platform specific type which represents |
| 21 // the underlying OS handle to a shared memory segment. | 21 // the underlying OS handle to a shared memory segment. |
| 22 #if defined(OS_WIN) | 22 #if defined(OS_WIN) |
| 23 typedef HANDLE SharedMemoryHandle; | 23 typedef HANDLE SharedMemoryHandle; |
| 24 typedef HANDLE SharedMemoryLock; | 24 typedef HANDLE SharedMemoryLock; |
| 25 #elif defined(OS_POSIX) | 25 #elif defined(OS_POSIX) |
| 26 typedef int SharedMemoryHandle; | 26 typedef int SharedMemoryHandle; |
| 27 // TODO(port): these semaphores can leak if we crash, causing |
| 28 // autobuilder problems. Transition to something easier to clean up |
| 29 // (e.g. lockf/flock). |
| 30 // TODO(port): make sure what we transition to is fast enough. |
| 27 typedef sem_t* SharedMemoryLock; | 31 typedef sem_t* SharedMemoryLock; |
| 28 #endif | 32 #endif |
| 29 | 33 |
| 30 // Platform abstraction for shared memory. Provides a C++ wrapper | 34 // Platform abstraction for shared memory. Provides a C++ wrapper |
| 31 // around the OS primitive for a memory mapped file. | 35 // around the OS primitive for a memory mapped file. |
| 32 class SharedMemory { | 36 class SharedMemory { |
| 33 public: | 37 public: |
| 34 // Create a new SharedMemory object. | 38 // Create a new SharedMemory object. |
| 35 SharedMemory(); | 39 SharedMemory(); |
| 36 | 40 |
| 37 // Create a new SharedMemory object from an existing, open | 41 // Create a new SharedMemory object from an existing, open |
| 38 // shared memory file. | 42 // shared memory file. |
| 39 SharedMemory(SharedMemoryHandle handle, bool read_only); | 43 SharedMemory(SharedMemoryHandle handle, bool read_only); |
| 40 | 44 |
| 41 // Create a new SharedMemory object from an existing, open | 45 // Create a new SharedMemory object from an existing, open |
| 42 // shared memory file that was created by a remote process and not shared | 46 // shared memory file that was created by a remote process and not shared |
| 43 // to the current process. | 47 // to the current process. |
| 44 SharedMemory(SharedMemoryHandle handle, bool read_only, | 48 SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 45 base::ProcessHandle process); | 49 base::ProcessHandle process); |
| 46 | 50 |
| 47 // Destructor. Will close any open files. | 51 // Destructor. Will close any open files. |
| 48 ~SharedMemory(); | 52 ~SharedMemory(); |
| 49 | 53 |
| 50 // Creates or opens a shared memory segment based on a name. | 54 // Creates or opens a shared memory segment based on a name. |
| 51 // If read_only is true, opens the memory as read-only. | 55 // If read_only is true, opens the memory as read-only. |
| 52 // If open_existing is true, and the shared memory already exists, | 56 // If open_existing is true, and the shared memory already exists, |
| 53 // opens the existing shared memory and ignores the size parameter. | 57 // opens the existing shared memory and ignores the size parameter. |
| 58 // If name is the empty string, use a unique name. |
| 54 // Returns true on success, false on failure. | 59 // Returns true on success, false on failure. |
| 55 bool Create(const std::wstring& name, bool read_only, bool open_existing, | 60 bool Create(const std::wstring& name, bool read_only, bool open_existing, |
| 56 size_t size); | 61 size_t size); |
| 57 | 62 |
| 63 // Deletes resources associated with a shared memory segment based on name. |
| 64 // Not all platforms require this call. |
| 65 bool Delete(const std::wstring& name); |
| 66 |
| 58 // Opens a shared memory segment based on a name. | 67 // Opens a shared memory segment based on a name. |
| 59 // If read_only is true, opens for read-only access. | 68 // If read_only is true, opens for read-only access. |
| 69 // If name is the empty string, use a unique name. |
| 60 // Returns true on success, false on failure. | 70 // Returns true on success, false on failure. |
| 61 bool Open(const std::wstring& name, bool read_only); | 71 bool Open(const std::wstring& name, bool read_only); |
| 62 | 72 |
| 63 // Maps the shared memory into the caller's address space. | 73 // Maps the shared memory into the caller's address space. |
| 64 // Returns true on success, false otherwise. The memory address | 74 // Returns true on success, false otherwise. The memory address |
| 65 // is accessed via the memory() accessor. | 75 // is accessed via the memory() accessor. |
| 66 bool Map(size_t bytes); | 76 bool Map(size_t bytes); |
| 67 | 77 |
| 68 // Unmaps the shared memory from the caller's address space. | 78 // Unmaps the shared memory from the caller's address space. |
| 69 // Returns true if successful; returns false on error or if the | 79 // Returns true if successful; returns false on error or if the |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 113 // Lock the shared memory. | 123 // Lock the shared memory. |
| 114 // This is a cross-process lock which may be recursively | 124 // This is a cross-process lock which may be recursively |
| 115 // locked by the same thread. | 125 // locked by the same thread. |
| 116 void Lock(); | 126 void Lock(); |
| 117 | 127 |
| 118 // Release the shared memory lock. | 128 // Release the shared memory lock. |
| 119 void Unlock(); | 129 void Unlock(); |
| 120 | 130 |
| 121 private: | 131 private: |
| 122 #if defined(OS_POSIX) | 132 #if defined(OS_POSIX) |
| 123 bool CreateOrOpen(const std::wstring &name, int posix_flags); | 133 bool CreateOrOpen(const std::wstring &name, int posix_flags, size_t size); |
| 134 bool FilenameForMemoryName(const std::wstring &memname, |
| 135 std::wstring *filename); |
| 124 #endif | 136 #endif |
| 125 bool ShareToProcessCommon(ProcessHandle process, | 137 bool ShareToProcessCommon(ProcessHandle process, |
| 126 SharedMemoryHandle* new_handle, | 138 SharedMemoryHandle* new_handle, |
| 127 bool close_self); | 139 bool close_self); |
| 128 | 140 |
| 129 std::wstring name_; | 141 std::wstring name_; |
| 130 SharedMemoryHandle mapped_file_; | 142 SharedMemoryHandle mapped_file_; |
| 131 void* memory_; | 143 void* memory_; |
| 132 bool read_only_; | 144 bool read_only_; |
| 133 size_t max_size_; | 145 size_t max_size_; |
| 134 SharedMemoryLock lock_; | 146 SharedMemoryLock lock_; |
| 147 #if defined(OS_POSIX) |
| 148 std::string sem_name_; |
| 149 #endif |
| 135 | 150 |
| 136 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); | 151 DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); |
| 137 }; | 152 }; |
| 138 | 153 |
| 139 // A helper class that acquires the shared memory lock while | 154 // A helper class that acquires the shared memory lock while |
| 140 // the SharedMemoryAutoLock is in scope. | 155 // the SharedMemoryAutoLock is in scope. |
| 141 class SharedMemoryAutoLock { | 156 class SharedMemoryAutoLock { |
| 142 public: | 157 public: |
| 143 explicit SharedMemoryAutoLock(SharedMemory* shared_memory) | 158 explicit SharedMemoryAutoLock(SharedMemory* shared_memory) |
| 144 : shared_memory_(shared_memory) { | 159 : shared_memory_(shared_memory) { |
| 145 shared_memory_->Lock(); | 160 shared_memory_->Lock(); |
| 146 } | 161 } |
| 147 | 162 |
| 148 ~SharedMemoryAutoLock() { | 163 ~SharedMemoryAutoLock() { |
| 149 shared_memory_->Unlock(); | 164 shared_memory_->Unlock(); |
| 150 } | 165 } |
| 151 | 166 |
| 152 private: | 167 private: |
| 153 SharedMemory* shared_memory_; | 168 SharedMemory* shared_memory_; |
| 154 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); | 169 DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); |
| 155 }; | 170 }; |
| 156 | 171 |
| 157 } // namespace base | 172 } // namespace base |
| 158 | 173 |
| 159 #endif // BASE_SHARED_MEMORY_H_ | 174 #endif // BASE_SHARED_MEMORY_H_ |
| OLD | NEW |