| 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_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 #include <string> | 10 #include <string> |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 46 struct SharedMemoryCreateOptions { | 46 struct SharedMemoryCreateOptions { |
| 47 SharedMemoryCreateOptions() : name(NULL), size(0), open_existing(false), | 47 SharedMemoryCreateOptions() : name(NULL), size(0), open_existing(false), |
| 48 executable(false) {} | 48 executable(false) {} |
| 49 | 49 |
| 50 // If NULL, the object is anonymous. This pointer is owned by the caller | 50 // If NULL, the object is anonymous. This pointer is owned by the caller |
| 51 // and must live through the call to Create(). | 51 // and must live through the call to Create(). |
| 52 const std::string* name; | 52 const std::string* name; |
| 53 | 53 |
| 54 // Size of the shared memory object to be created. | 54 // Size of the shared memory object to be created. |
| 55 // When opening an existing object, this has no effect. | 55 // When opening an existing object, this has no effect. |
| 56 uint32 size; | 56 size_t size; |
| 57 | 57 |
| 58 // If true, and the shared memory already exists, Create() will open the | 58 // If true, and the shared memory already exists, Create() will open the |
| 59 // existing shared memory and ignore the size parameter. If false, | 59 // existing shared memory and ignore the size parameter. If false, |
| 60 // shared memory must not exist. This flag is meaningless unless name is | 60 // shared memory must not exist. This flag is meaningless unless name is |
| 61 // non-NULL. | 61 // non-NULL. |
| 62 bool open_existing; | 62 bool open_existing; |
| 63 | 63 |
| 64 // If true, mappings might need to be made executable later. | 64 // If true, mappings might need to be made executable later. |
| 65 bool executable; | 65 bool executable; |
| 66 }; | 66 }; |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 100 | 100 |
| 101 // Closes a shared memory handle. | 101 // Closes a shared memory handle. |
| 102 static void CloseHandle(const SharedMemoryHandle& handle); | 102 static void CloseHandle(const SharedMemoryHandle& handle); |
| 103 | 103 |
| 104 // Creates a shared memory object as described by the options struct. | 104 // Creates a shared memory object as described by the options struct. |
| 105 // Returns true on success and false on failure. | 105 // Returns true on success and false on failure. |
| 106 bool Create(const SharedMemoryCreateOptions& options); | 106 bool Create(const SharedMemoryCreateOptions& options); |
| 107 | 107 |
| 108 // Creates and maps an anonymous shared memory segment of size size. | 108 // Creates and maps an anonymous shared memory segment of size size. |
| 109 // Returns true on success and false on failure. | 109 // Returns true on success and false on failure. |
| 110 bool CreateAndMapAnonymous(uint32 size); | 110 bool CreateAndMapAnonymous(size_t size); |
| 111 | 111 |
| 112 // Creates an anonymous shared memory segment of size size. | 112 // Creates an anonymous shared memory segment of size size. |
| 113 // Returns true on success and false on failure. | 113 // Returns true on success and false on failure. |
| 114 bool CreateAnonymous(uint32 size) { | 114 bool CreateAnonymous(size_t size) { |
| 115 SharedMemoryCreateOptions options; | 115 SharedMemoryCreateOptions options; |
| 116 options.size = size; | 116 options.size = size; |
| 117 return Create(options); | 117 return Create(options); |
| 118 } | 118 } |
| 119 | 119 |
| 120 // Creates or opens a shared memory segment based on a name. | 120 // Creates or opens a shared memory segment based on a name. |
| 121 // If open_existing is true, and the shared memory already exists, | 121 // If open_existing is true, and the shared memory already exists, |
| 122 // opens the existing shared memory and ignores the size parameter. | 122 // opens the existing shared memory and ignores the size parameter. |
| 123 // If open_existing is false, shared memory must not exist. | 123 // If open_existing is false, shared memory must not exist. |
| 124 // size is the size of the block to be created. | 124 // size is the size of the block to be created. |
| 125 // Returns true on success, false on failure. | 125 // Returns true on success, false on failure. |
| 126 bool CreateNamed(const std::string& name, bool open_existing, uint32 size) { | 126 bool CreateNamed(const std::string& name, bool open_existing, size_t size) { |
| 127 SharedMemoryCreateOptions options; | 127 SharedMemoryCreateOptions options; |
| 128 options.name = &name; | 128 options.name = &name; |
| 129 options.open_existing = open_existing; | 129 options.open_existing = open_existing; |
| 130 options.size = size; | 130 options.size = size; |
| 131 return Create(options); | 131 return Create(options); |
| 132 } | 132 } |
| 133 | 133 |
| 134 // Deletes resources associated with a shared memory segment based on name. | 134 // Deletes resources associated with a shared memory segment based on name. |
| 135 // Not all platforms require this call. | 135 // Not all platforms require this call. |
| 136 bool Delete(const std::string& name); | 136 bool Delete(const std::string& name); |
| 137 | 137 |
| 138 // Opens a shared memory segment based on a name. | 138 // Opens a shared memory segment based on a name. |
| 139 // If read_only is true, opens for read-only access. | 139 // If read_only is true, opens for read-only access. |
| 140 // Returns true on success, false on failure. | 140 // Returns true on success, false on failure. |
| 141 bool Open(const std::string& name, bool read_only); | 141 bool Open(const std::string& name, bool read_only); |
| 142 | 142 |
| 143 // Maps the shared memory into the caller's address space. | 143 // Maps the shared memory into the caller's address space. |
| 144 // Returns true on success, false otherwise. The memory address | 144 // Returns true on success, false otherwise. The memory address |
| 145 // is accessed via the memory() accessor. The mapped address is guaranteed to | 145 // is accessed via the memory() accessor. The mapped address is guaranteed to |
| 146 // have an alignment of at least MAP_MINIMUM_ALIGNMENT. | 146 // have an alignment of at least MAP_MINIMUM_ALIGNMENT. |
| 147 bool Map(uint32 bytes); | 147 bool Map(size_t bytes); |
| 148 enum { MAP_MINIMUM_ALIGNMENT = 32 }; | 148 enum { MAP_MINIMUM_ALIGNMENT = 32 }; |
| 149 | 149 |
| 150 // Unmaps the shared memory from the caller's address space. | 150 // Unmaps the shared memory from the caller's address space. |
| 151 // Returns true if successful; returns false on error or if the | 151 // Returns true if successful; returns false on error or if the |
| 152 // memory is not mapped. | 152 // memory is not mapped. |
| 153 bool Unmap(); | 153 bool Unmap(); |
| 154 | 154 |
| 155 // Get the size of the shared memory backing file. | 155 // Get the size of the shared memory backing file. |
| 156 // Note: This size is only available to the creator of the | 156 // Note: This size is only available to the creator of the |
| 157 // shared memory, and not to those that opened shared memory | 157 // shared memory, and not to those that opened shared memory |
| 158 // created externally. | 158 // created externally. |
| 159 // Returns 0 if not created or unknown. | 159 // Returns 0 if not created or unknown. |
| 160 // Deprecated method, please keep track of the size yourself if you created | 160 // Deprecated method, please keep track of the size yourself if you created |
| 161 // it. | 161 // it. |
| 162 // http://crbug.com/60821 | 162 // http://crbug.com/60821 |
| 163 uint32 created_size() const { return created_size_; } | 163 size_t created_size() const { return created_size_; } |
| 164 | 164 |
| 165 // Gets a pointer to the opened memory space if it has been | 165 // Gets a pointer to the opened memory space if it has been |
| 166 // Mapped via Map(). Returns NULL if it is not mapped. | 166 // Mapped via Map(). Returns NULL if it is not mapped. |
| 167 void *memory() const { return memory_; } | 167 void *memory() const { return memory_; } |
| 168 | 168 |
| 169 // Returns the underlying OS handle for this segment. | 169 // Returns the underlying OS handle for this segment. |
| 170 // Use of this handle for anything other than an opaque | 170 // Use of this handle for anything other than an opaque |
| 171 // identifier is not portable. | 171 // identifier is not portable. |
| 172 SharedMemoryHandle handle() const; | 172 SharedMemoryHandle handle() const; |
| 173 | 173 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 #endif | 232 #endif |
| 233 bool ShareToProcessCommon(ProcessHandle process, | 233 bool ShareToProcessCommon(ProcessHandle process, |
| 234 SharedMemoryHandle* new_handle, | 234 SharedMemoryHandle* new_handle, |
| 235 bool close_self); | 235 bool close_self); |
| 236 | 236 |
| 237 #if defined(OS_WIN) | 237 #if defined(OS_WIN) |
| 238 std::wstring name_; | 238 std::wstring name_; |
| 239 HANDLE mapped_file_; | 239 HANDLE mapped_file_; |
| 240 #elif defined(OS_POSIX) | 240 #elif defined(OS_POSIX) |
| 241 int mapped_file_; | 241 int mapped_file_; |
| 242 uint32 mapped_size_; | 242 size_t mapped_size_; |
| 243 ino_t inode_; | 243 ino_t inode_; |
| 244 #endif | 244 #endif |
| 245 void* memory_; | 245 void* memory_; |
| 246 bool read_only_; | 246 bool read_only_; |
| 247 uint32 created_size_; | 247 size_t created_size_; |
| 248 #if !defined(OS_POSIX) | 248 #if !defined(OS_POSIX) |
| 249 SharedMemoryLock lock_; | 249 SharedMemoryLock lock_; |
| 250 #endif | 250 #endif |
| 251 | 251 |
| 252 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 252 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
| 253 }; | 253 }; |
| 254 | 254 |
| 255 // A helper class that acquires the shared memory lock while | 255 // A helper class that acquires the shared memory lock while |
| 256 // the SharedMemoryAutoLock is in scope. | 256 // the SharedMemoryAutoLock is in scope. |
| 257 class SharedMemoryAutoLock { | 257 class SharedMemoryAutoLock { |
| 258 public: | 258 public: |
| 259 explicit SharedMemoryAutoLock(SharedMemory* shared_memory) | 259 explicit SharedMemoryAutoLock(SharedMemory* shared_memory) |
| 260 : shared_memory_(shared_memory) { | 260 : shared_memory_(shared_memory) { |
| 261 shared_memory_->Lock(); | 261 shared_memory_->Lock(); |
| 262 } | 262 } |
| 263 | 263 |
| 264 ~SharedMemoryAutoLock() { | 264 ~SharedMemoryAutoLock() { |
| 265 shared_memory_->Unlock(); | 265 shared_memory_->Unlock(); |
| 266 } | 266 } |
| 267 | 267 |
| 268 private: | 268 private: |
| 269 SharedMemory* shared_memory_; | 269 SharedMemory* shared_memory_; |
| 270 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); | 270 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); |
| 271 }; | 271 }; |
| 272 | 272 |
| 273 } // namespace base | 273 } // namespace base |
| 274 | 274 |
| 275 #endif // BASE_SHARED_MEMORY_H_ | 275 #endif // BASE_SHARED_MEMORY_H_ |
| OLD | NEW |