| 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 16 matching lines...) Expand all Loading... |
| 27 | 27 |
| 28 namespace base { | 28 namespace base { |
| 29 | 29 |
| 30 class FilePath; | 30 class FilePath; |
| 31 | 31 |
| 32 // SharedMemoryHandle is a platform specific type which represents | 32 // SharedMemoryHandle is a platform specific type which represents |
| 33 // the underlying OS handle to a shared memory segment. | 33 // the underlying OS handle to a shared memory segment. |
| 34 #if defined(OS_WIN) | 34 #if defined(OS_WIN) |
| 35 typedef HANDLE SharedMemoryHandle; | 35 typedef HANDLE SharedMemoryHandle; |
| 36 #elif defined(OS_POSIX) | 36 #elif defined(OS_POSIX) |
| 37 // A SharedMemoryId is sufficient to identify a given shared memory segment on a | |
| 38 // system, but insufficient to map it. | |
| 39 typedef FileDescriptor SharedMemoryHandle; | 37 typedef FileDescriptor SharedMemoryHandle; |
| 40 typedef ino_t SharedMemoryId; | |
| 41 #endif | 38 #endif |
| 42 | 39 |
| 43 // Options for creating a shared memory object. | 40 // Options for creating a shared memory object. |
| 44 struct SharedMemoryCreateOptions { | 41 struct SharedMemoryCreateOptions { |
| 45 SharedMemoryCreateOptions() | 42 SharedMemoryCreateOptions() |
| 46 : name_deprecated(NULL), | 43 : name_deprecated(NULL), |
| 47 size(0), | 44 size(0), |
| 48 open_existing_deprecated(false), | 45 open_existing_deprecated(false), |
| 49 executable(false), | 46 executable(false), |
| 50 share_read_only(false) {} | 47 share_read_only(false) {} |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 | 106 |
| 110 // Returns invalid handle (see comment above for exact definition). | 107 // Returns invalid handle (see comment above for exact definition). |
| 111 static SharedMemoryHandle NULLHandle(); | 108 static SharedMemoryHandle NULLHandle(); |
| 112 | 109 |
| 113 // Closes a shared memory handle. | 110 // Closes a shared memory handle. |
| 114 static void CloseHandle(const SharedMemoryHandle& handle); | 111 static void CloseHandle(const SharedMemoryHandle& handle); |
| 115 | 112 |
| 116 // Returns the maximum number of handles that can be open at once per process. | 113 // Returns the maximum number of handles that can be open at once per process. |
| 117 static size_t GetHandleLimit(); | 114 static size_t GetHandleLimit(); |
| 118 | 115 |
| 116 // Duplicates The underlying OS primitive. Returns NULLHandle() on failure. |
| 117 // The caller is responsible for destroying the duplicated OS primitive. |
| 118 static SharedMemoryHandle DuplicateHandle(const SharedMemoryHandle& handle); |
| 119 |
| 120 #if defined(OS_POSIX) |
| 121 // This method requires that the SharedMemoryHandle is backed by a POSIX fd. |
| 122 static int GetFdFromSharedMemoryHandle(const SharedMemoryHandle& handle); |
| 123 #endif |
| 124 |
| 125 #if defined(OS_POSIX) && !defined(OS_ANDROID) |
| 126 // Returns the size of the shared memory region referred to by |handle|. |
| 127 // Returns '-1' on a failure to determine the size. |
| 128 static int GetSizeFromSharedMemoryHandle(const SharedMemoryHandle& handle); |
| 129 #endif // defined(OS_POSIX) && !defined(OS_ANDROID) |
| 130 |
| 119 // Creates a shared memory object as described by the options struct. | 131 // Creates a shared memory object as described by the options struct. |
| 120 // Returns true on success and false on failure. | 132 // Returns true on success and false on failure. |
| 121 bool Create(const SharedMemoryCreateOptions& options); | 133 bool Create(const SharedMemoryCreateOptions& options); |
| 122 | 134 |
| 123 // Creates and maps an anonymous shared memory segment of size size. | 135 // Creates and maps an anonymous shared memory segment of size size. |
| 124 // Returns true on success and false on failure. | 136 // Returns true on success and false on failure. |
| 125 bool CreateAndMapAnonymous(size_t size); | 137 bool CreateAndMapAnonymous(size_t size); |
| 126 | 138 |
| 127 // Creates an anonymous shared memory segment of size size. | 139 // Creates an anonymous shared memory segment of size size. |
| 128 // Returns true on success and false on failure. | 140 // Returns true on success and false on failure. |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 185 | 197 |
| 186 // Gets a pointer to the opened memory space if it has been | 198 // Gets a pointer to the opened memory space if it has been |
| 187 // Mapped via Map(). Returns NULL if it is not mapped. | 199 // Mapped via Map(). Returns NULL if it is not mapped. |
| 188 void *memory() const { return memory_; } | 200 void *memory() const { return memory_; } |
| 189 | 201 |
| 190 // Returns the underlying OS handle for this segment. | 202 // Returns the underlying OS handle for this segment. |
| 191 // Use of this handle for anything other than an opaque | 203 // Use of this handle for anything other than an opaque |
| 192 // identifier is not portable. | 204 // identifier is not portable. |
| 193 SharedMemoryHandle handle() const; | 205 SharedMemoryHandle handle() const; |
| 194 | 206 |
| 195 #if defined(OS_POSIX) && !defined(OS_NACL) | |
| 196 // Returns a unique identifier for this shared memory segment. Inode numbers | |
| 197 // are technically only unique to a single filesystem. However, we always | |
| 198 // allocate shared memory backing files from the same directory, so will end | |
| 199 // up on the same filesystem. | |
| 200 SharedMemoryId id() const { return inode_; } | |
| 201 #endif | |
| 202 | |
| 203 // Closes the open shared memory segment. The memory will remain mapped if | 207 // Closes the open shared memory segment. The memory will remain mapped if |
| 204 // it was previously mapped. | 208 // it was previously mapped. |
| 205 // It is safe to call Close repeatedly. | 209 // It is safe to call Close repeatedly. |
| 206 void Close(); | 210 void Close(); |
| 207 | 211 |
| 208 // Shares the shared memory to another process. Attempts to create a | 212 // Shares the shared memory to another process. Attempts to create a |
| 209 // platform-specific new_handle which can be used in a remote process to read | 213 // platform-specific new_handle which can be used in a remote process to read |
| 210 // the shared memory file. new_handle is an output parameter to receive the | 214 // the shared memory file. new_handle is an output parameter to receive the |
| 211 // handle for use in the remote process. | 215 // handle for use in the remote process. |
| 212 // | 216 // |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 282 SharedMemoryHandle* new_handle, | 286 SharedMemoryHandle* new_handle, |
| 283 bool close_self, | 287 bool close_self, |
| 284 ShareMode); | 288 ShareMode); |
| 285 | 289 |
| 286 #if defined(OS_WIN) | 290 #if defined(OS_WIN) |
| 287 std::wstring name_; | 291 std::wstring name_; |
| 288 HANDLE mapped_file_; | 292 HANDLE mapped_file_; |
| 289 #elif defined(OS_POSIX) | 293 #elif defined(OS_POSIX) |
| 290 int mapped_file_; | 294 int mapped_file_; |
| 291 int readonly_mapped_file_; | 295 int readonly_mapped_file_; |
| 292 ino_t inode_; | |
| 293 #endif | 296 #endif |
| 294 size_t mapped_size_; | 297 size_t mapped_size_; |
| 295 void* memory_; | 298 void* memory_; |
| 296 bool read_only_; | 299 bool read_only_; |
| 297 size_t requested_size_; | 300 size_t requested_size_; |
| 298 #if !defined(OS_POSIX) | 301 #if !defined(OS_POSIX) |
| 299 HANDLE lock_; | 302 HANDLE lock_; |
| 300 #endif | 303 #endif |
| 301 | 304 |
| 302 DISALLOW_COPY_AND_ASSIGN(SharedMemory); | 305 DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 317 } | 320 } |
| 318 | 321 |
| 319 private: | 322 private: |
| 320 SharedMemory* shared_memory_; | 323 SharedMemory* shared_memory_; |
| 321 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLockDeprecated); | 324 DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLockDeprecated); |
| 322 }; | 325 }; |
| 323 | 326 |
| 324 } // namespace base | 327 } // namespace base |
| 325 | 328 |
| 326 #endif // BASE_MEMORY_SHARED_MEMORY_H_ | 329 #endif // BASE_MEMORY_SHARED_MEMORY_H_ |
| OLD | NEW |