| 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_MEMORY_SHARED_MEMORY_H_ | 5 #ifndef BASE_MEMORY_SHARED_MEMORY_H_ |
| 6 #define BASE_MEMORY_SHARED_MEMORY_H_ | 6 #define BASE_MEMORY_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 15 matching lines...) Expand all Loading... |
| 26 #include "base/files/scoped_file.h" | 26 #include "base/files/scoped_file.h" |
| 27 #endif | 27 #endif |
| 28 | 28 |
| 29 namespace base { | 29 namespace base { |
| 30 | 30 |
| 31 class FilePath; | 31 class FilePath; |
| 32 | 32 |
| 33 // Options for creating a shared memory object. | 33 // Options for creating a shared memory object. |
| 34 struct SharedMemoryCreateOptions { | 34 struct SharedMemoryCreateOptions { |
| 35 SharedMemoryCreateOptions() | 35 SharedMemoryCreateOptions() |
| 36 : name_deprecated(NULL), | 36 : size(0), |
| 37 size(0), | |
| 38 open_existing_deprecated(false), | |
| 39 executable(false), | 37 executable(false), |
| 40 share_read_only(false) {} | 38 share_read_only(false) { |
| 39 #if !defined(OS_MACOSX) |
| 40 name_deprecated = nullptr; |
| 41 open_existing_deprecated = false; |
| 42 #endif |
| 43 } |
| 41 | 44 |
| 45 #if !defined(OS_MACOSX) |
| 42 // DEPRECATED (crbug.com/345734): | 46 // DEPRECATED (crbug.com/345734): |
| 43 // If NULL, the object is anonymous. This pointer is owned by the caller | 47 // If NULL, the object is anonymous. This pointer is owned by the caller |
| 44 // and must live through the call to Create(). | 48 // and must live through the call to Create(). |
| 45 const std::string* name_deprecated; | 49 const std::string* name_deprecated; |
| 50 #endif |
| 46 | 51 |
| 47 // Size of the shared memory object to be created. | 52 // Size of the shared memory object to be created. |
| 48 // When opening an existing object, this has no effect. | 53 // When opening an existing object, this has no effect. |
| 49 size_t size; | 54 size_t size; |
| 50 | 55 |
| 56 #if !defined(OS_MACOSX) |
| 51 // DEPRECATED (crbug.com/345734): | 57 // DEPRECATED (crbug.com/345734): |
| 52 // 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 |
| 53 // existing shared memory and ignore the size parameter. If false, | 59 // existing shared memory and ignore the size parameter. If false, |
| 54 // shared memory must not exist. This flag is meaningless unless | 60 // shared memory must not exist. This flag is meaningless unless |
| 55 // name_deprecated is non-NULL. | 61 // name_deprecated is non-NULL. |
| 56 bool open_existing_deprecated; | 62 bool open_existing_deprecated; |
| 63 #endif |
| 57 | 64 |
| 58 // If true, mappings might need to be made executable later. | 65 // If true, mappings might need to be made executable later. |
| 59 bool executable; | 66 bool executable; |
| 60 | 67 |
| 61 // If true, the file can be shared read-only to a process. | 68 // If true, the file can be shared read-only to a process. |
| 62 bool share_read_only; | 69 bool share_read_only; |
| 63 }; | 70 }; |
| 64 | 71 |
| 65 // Platform abstraction for shared memory. Provides a C++ wrapper | 72 // Platform abstraction for shared memory. Provides a C++ wrapper |
| 66 // around the OS primitive for a memory mapped file. | 73 // around the OS primitive for a memory mapped file. |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 131 bool CreateAndMapAnonymous(size_t size); | 138 bool CreateAndMapAnonymous(size_t size); |
| 132 | 139 |
| 133 // Creates an anonymous shared memory segment of size size. | 140 // Creates an anonymous shared memory segment of size size. |
| 134 // Returns true on success and false on failure. | 141 // Returns true on success and false on failure. |
| 135 bool CreateAnonymous(size_t size) { | 142 bool CreateAnonymous(size_t size) { |
| 136 SharedMemoryCreateOptions options; | 143 SharedMemoryCreateOptions options; |
| 137 options.size = size; | 144 options.size = size; |
| 138 return Create(options); | 145 return Create(options); |
| 139 } | 146 } |
| 140 | 147 |
| 148 #if !defined(OS_MACOSX) |
| 141 // DEPRECATED (crbug.com/345734): | 149 // DEPRECATED (crbug.com/345734): |
| 142 // Creates or opens a shared memory segment based on a name. | 150 // Creates or opens a shared memory segment based on a name. |
| 143 // If open_existing is true, and the shared memory already exists, | 151 // If open_existing is true, and the shared memory already exists, |
| 144 // opens the existing shared memory and ignores the size parameter. | 152 // opens the existing shared memory and ignores the size parameter. |
| 145 // If open_existing is false, shared memory must not exist. | 153 // If open_existing is false, shared memory must not exist. |
| 146 // size is the size of the block to be created. | 154 // size is the size of the block to be created. |
| 147 // Returns true on success, false on failure. | 155 // Returns true on success, false on failure. |
| 148 bool CreateNamedDeprecated( | 156 bool CreateNamedDeprecated( |
| 149 const std::string& name, bool open_existing, size_t size) { | 157 const std::string& name, bool open_existing, size_t size) { |
| 150 SharedMemoryCreateOptions options; | 158 SharedMemoryCreateOptions options; |
| 151 options.name_deprecated = &name; | 159 options.name_deprecated = &name; |
| 152 options.open_existing_deprecated = open_existing; | 160 options.open_existing_deprecated = open_existing; |
| 153 options.size = size; | 161 options.size = size; |
| 154 return Create(options); | 162 return Create(options); |
| 155 } | 163 } |
| 156 | 164 |
| 157 // Deletes resources associated with a shared memory segment based on name. | 165 // Deletes resources associated with a shared memory segment based on name. |
| 158 // Not all platforms require this call. | 166 // Not all platforms require this call. |
| 159 bool Delete(const std::string& name); | 167 bool Delete(const std::string& name); |
| 160 | 168 |
| 161 // Opens a shared memory segment based on a name. | 169 // Opens a shared memory segment based on a name. |
| 162 // If read_only is true, opens for read-only access. | 170 // If read_only is true, opens for read-only access. |
| 163 // Returns true on success, false on failure. | 171 // Returns true on success, false on failure. |
| 164 bool Open(const std::string& name, bool read_only); | 172 bool Open(const std::string& name, bool read_only); |
| 173 #endif // !defined(OS_MACOSX) |
| 165 | 174 |
| 166 // Maps the shared memory into the caller's address space. | 175 // Maps the shared memory into the caller's address space. |
| 167 // Returns true on success, false otherwise. The memory address | 176 // Returns true on success, false otherwise. The memory address |
| 168 // is accessed via the memory() accessor. The mapped address is guaranteed to | 177 // is accessed via the memory() accessor. The mapped address is guaranteed to |
| 169 // have an alignment of at least MAP_MINIMUM_ALIGNMENT. This method will fail | 178 // have an alignment of at least MAP_MINIMUM_ALIGNMENT. This method will fail |
| 170 // if this object is currently mapped. | 179 // if this object is currently mapped. |
| 171 bool Map(size_t bytes) { | 180 bool Map(size_t bytes) { |
| 172 return MapAt(0, bytes); | 181 return MapAt(0, bytes); |
| 173 } | 182 } |
| 174 | 183 |
| 175 // Same as above, but with |offset| to specify from begining of the shared | 184 // Same as above, but with |offset| to specify from begining of the shared |
| 176 // memory block to map. | 185 // memory block to map. |
| 177 // |offset| must be alignent to value of |SysInfo::VMAllocationGranularity()|. | 186 // |offset| must be alignent to value of |SysInfo::VMAllocationGranularity()|. |
| 178 bool MapAt(off_t offset, size_t bytes); | 187 bool MapAt(off_t offset, size_t bytes); |
| 179 enum { MAP_MINIMUM_ALIGNMENT = 32 }; | 188 enum { MAP_MINIMUM_ALIGNMENT = 32 }; |
| 180 | 189 |
| 181 // Unmaps the shared memory from the caller's address space. | 190 // Unmaps the shared memory from the caller's address space. |
| 182 // Returns true if successful; returns false on error or if the | 191 // Returns true if successful; returns false on error or if the |
| 183 // memory is not mapped. | 192 // memory is not mapped. |
| 184 bool Unmap(); | 193 bool Unmap(); |
| 185 | 194 |
| 186 // The size requested when the map is first created. | 195 // The size requested when the map is first created. |
| 187 size_t requested_size() const { return requested_size_; } | 196 size_t requested_size() const { return requested_size_; } |
| 188 | 197 |
| 189 // The actual size of the mapped memory (may be larger than requested). | 198 // The actual size of the mapped memory (may be larger than requested). |
| 190 size_t mapped_size() const { return mapped_size_; } | 199 size_t mapped_size() const { return mapped_size_; } |
| 191 | 200 |
| 192 // Gets a pointer to the opened memory space if it has been | 201 // Gets a pointer to the opened memory space if it has been |
| 193 // Mapped via Map(). Returns NULL if it is not mapped. | 202 // Mapped via Map(). Returns NULL if it is not mapped. |
| 194 void *memory() const { return memory_; } | 203 void* memory() const { return memory_; } |
| 195 | 204 |
| 196 // Returns the underlying OS handle for this segment. | 205 // Returns the underlying OS handle for this segment. |
| 197 // Use of this handle for anything other than an opaque | 206 // Use of this handle for anything other than an opaque |
| 198 // identifier is not portable. | 207 // identifier is not portable. |
| 199 SharedMemoryHandle handle() const; | 208 SharedMemoryHandle handle() const; |
| 200 | 209 |
| 201 // Closes the open shared memory segment. The memory will remain mapped if | 210 // Closes the open shared memory segment. The memory will remain mapped if |
| 202 // it was previously mapped. | 211 // it was previously mapped. |
| 203 // It is safe to call Close repeatedly. | 212 // It is safe to call Close repeatedly. |
| 204 void Close(); | 213 void Close(); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 275 size_t mapped_size_; | 284 size_t mapped_size_; |
| 276 void* memory_; | 285 void* memory_; |
| 277 bool read_only_; | 286 bool read_only_; |
| 278 size_t requested_size_; | 287 size_t requested_size_; |
| 279 | 288 |
| 280 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 289 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
| 281 }; | 290 }; |
| 282 } // namespace base | 291 } // namespace base |
| 283 | 292 |
| 284 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 293 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
| OLD | NEW |