[email protected] | 90509cb | 2011-03-25 18:46:38 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 5 | #ifndef BASE_SHARED_MEMORY_H_ |
| 6 | #define BASE_SHARED_MEMORY_H_ |
[email protected] | 32b76ef | 2010-07-26 23:08:24 | [diff] [blame] | 7 | #pragma once |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | |
[email protected] | 5425c68 | 2008-11-14 20:08:02 | [diff] [blame] | 9 | #include "build/build_config.h" |
| 10 | |
| 11 | #if defined(OS_POSIX) |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 12 | #include <sys/types.h> |
[email protected] | 5425c68 | 2008-11-14 20:08:02 | [diff] [blame] | 13 | #include <semaphore.h> |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 14 | #include "base/file_descriptor_posix.h" |
[email protected] | 5425c68 | 2008-11-14 20:08:02 | [diff] [blame] | 15 | #endif |
[email protected] | de5abb9 | 2008-10-22 21:33:27 | [diff] [blame] | 16 | #include <string> |
[email protected] | 5425c68 | 2008-11-14 20:08:02 | [diff] [blame] | 17 | |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 18 | #include "base/base_export.h" |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 19 | #include "base/basictypes.h" |
[email protected] | de5abb9 | 2008-10-22 21:33:27 | [diff] [blame] | 20 | #include "base/process.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 21 | |
[email protected] | abd97e42 | 2009-09-16 17:32:11 | [diff] [blame] | 22 | class FilePath; |
| 23 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 24 | namespace base { |
| 25 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 26 | // SharedMemoryHandle is a platform specific type which represents |
| 27 | // the underlying OS handle to a shared memory segment. |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 28 | #if defined(OS_WIN) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | typedef HANDLE SharedMemoryHandle; |
| 30 | typedef HANDLE SharedMemoryLock; |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 31 | #elif defined(OS_POSIX) |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 32 | // A SharedMemoryId is sufficient to identify a given shared memory segment on a |
| 33 | // system, but insufficient to map it. |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 34 | typedef FileDescriptor SharedMemoryHandle; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 35 | typedef ino_t SharedMemoryId; |
[email protected] | ec031eb | 2009-02-05 19:04:34 | [diff] [blame] | 36 | // On POSIX, the lock is implemented as a lockf() on the mapped file, |
| 37 | // so no additional member (or definition of SharedMemoryLock) is |
| 38 | // needed. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 39 | #endif |
| 40 | |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 41 | // Options for creating a shared memory object. |
| 42 | struct SharedMemoryCreateOptions { |
| 43 | SharedMemoryCreateOptions() : name(NULL), size(0), open_existing(false), |
| 44 | executable(false) {} |
| 45 | |
| 46 | // If NULL, the object is anonymous. This pointer is owned by the caller |
| 47 | // and must live through the call to Create(). |
| 48 | const std::string* name; |
| 49 | |
| 50 | // Size of the shared memory object to be created. |
| 51 | // When opening an existing object, this has no effect. |
| 52 | uint32 size; |
| 53 | |
| 54 | // If true, and the shared memory already exists, Create() will open the |
| 55 | // existing shared memory and ignore the size parameter. If false, |
| 56 | // shared memory must not exist. This flag is meaningless unless name is |
| 57 | // non-NULL. |
| 58 | bool open_existing; |
| 59 | |
| 60 | // If true, mappings might need to be made executable later. |
| 61 | bool executable; |
| 62 | }; |
| 63 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 64 | // Platform abstraction for shared memory. Provides a C++ wrapper |
| 65 | // around the OS primitive for a memory mapped file. |
[email protected] | 0bea725 | 2011-08-05 15:34:00 | [diff] [blame] | 66 | class BASE_EXPORT SharedMemory { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 67 | public: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 68 | SharedMemory(); |
| 69 | |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 70 | #if defined(OS_WIN) |
| 71 | // Similar to the default constructor, except that this allows for |
| 72 | // calling Lock() to acquire the named mutex before either Create or Open |
| 73 | // are called on Windows. |
| 74 | explicit SharedMemory(const std::wstring& name); |
| 75 | #endif |
| 76 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 77 | // Create a new SharedMemory object from an existing, open |
| 78 | // shared memory file. |
| 79 | SharedMemory(SharedMemoryHandle handle, bool read_only); |
| 80 | |
| 81 | // Create a new SharedMemory object from an existing, open |
| 82 | // shared memory file that was created by a remote process and not shared |
| 83 | // to the current process. |
| 84 | SharedMemory(SharedMemoryHandle handle, bool read_only, |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 85 | ProcessHandle process); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 87 | // Closes any open files. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 88 | ~SharedMemory(); |
| 89 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 90 | // Return true iff the given handle is valid (i.e. not the distingished |
| 91 | // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
| 92 | static bool IsHandleValid(const SharedMemoryHandle& handle); |
| 93 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 94 | // Returns invalid handle (see comment above for exact definition). |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 95 | static SharedMemoryHandle NULLHandle(); |
| 96 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 97 | // Closes a shared memory handle. |
[email protected] | b0af04c | 2009-05-18 17:46:31 | [diff] [blame] | 98 | static void CloseHandle(const SharedMemoryHandle& handle); |
| 99 | |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 100 | // Creates a shared memory object as described by the options struct. |
| 101 | // Returns true on success and false on failure. |
| 102 | bool Create(const SharedMemoryCreateOptions& options); |
| 103 | |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 104 | // Creates and maps an anonymous shared memory segment of size size. |
| 105 | // Returns true on success and false on failure. |
| 106 | bool CreateAndMapAnonymous(uint32 size); |
| 107 | |
| 108 | // Creates an anonymous shared memory segment of size size. |
| 109 | // Returns true on success and false on failure. |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 110 | bool CreateAnonymous(uint32 size) { |
| 111 | SharedMemoryCreateOptions options; |
| 112 | options.size = size; |
| 113 | return Create(options); |
| 114 | } |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 115 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 116 | // Creates or opens a shared memory segment based on a name. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 117 | // If open_existing is true, and the shared memory already exists, |
| 118 | // opens the existing shared memory and ignores the size parameter. |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 119 | // If open_existing is false, shared memory must not exist. |
| 120 | // size is the size of the block to be created. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 121 | // Returns true on success, false on failure. |
[email protected] | b05df6b | 2011-12-01 23:19:31 | [diff] [blame] | 122 | bool CreateNamed(const std::string& name, bool open_existing, uint32 size) { |
| 123 | SharedMemoryCreateOptions options; |
| 124 | options.name = &name; |
| 125 | options.open_existing = open_existing; |
| 126 | options.size = size; |
| 127 | return Create(options); |
| 128 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 130 | // Deletes resources associated with a shared memory segment based on name. |
| 131 | // Not all platforms require this call. |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 132 | bool Delete(const std::string& name); |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 133 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 134 | // Opens a shared memory segment based on a name. |
| 135 | // If read_only is true, opens for read-only access. |
| 136 | // Returns true on success, false on failure. |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 137 | bool Open(const std::string& name, bool read_only); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 138 | |
| 139 | // Maps the shared memory into the caller's address space. |
| 140 | // Returns true on success, false otherwise. The memory address |
| 141 | // is accessed via the memory() accessor. |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 142 | bool Map(uint32 bytes); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 143 | |
| 144 | // Unmaps the shared memory from the caller's address space. |
| 145 | // Returns true if successful; returns false on error or if the |
| 146 | // memory is not mapped. |
| 147 | bool Unmap(); |
| 148 | |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 149 | // Get the size of the shared memory backing file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 150 | // Note: This size is only available to the creator of the |
| 151 | // shared memory, and not to those that opened shared memory |
| 152 | // created externally. |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 153 | // Returns 0 if not created or unknown. |
| 154 | // Deprecated method, please keep track of the size yourself if you created |
| 155 | // it. |
| 156 | // http://crbug.com/60821 |
| 157 | uint32 created_size() const { return created_size_; } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 158 | |
| 159 | // Gets a pointer to the opened memory space if it has been |
| 160 | // Mapped via Map(). Returns NULL if it is not mapped. |
| 161 | void *memory() const { return memory_; } |
| 162 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 163 | // Returns the underlying OS handle for this segment. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 164 | // Use of this handle for anything other than an opaque |
| 165 | // identifier is not portable. |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 166 | SharedMemoryHandle handle() const; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 167 | |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 168 | #if defined(OS_POSIX) |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 169 | // Returns a unique identifier for this shared memory segment. Inode numbers |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 170 | // are technically only unique to a single filesystem. However, we always |
| 171 | // allocate shared memory backing files from the same directory, so will end |
| 172 | // up on the same filesystem. |
| 173 | SharedMemoryId id() const { return inode_; } |
| 174 | #endif |
| 175 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 176 | // Closes the open shared memory segment. |
| 177 | // It is safe to call Close repeatedly. |
| 178 | void Close(); |
| 179 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 180 | // Shares the shared memory to another process. Attempts |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | // to create a platform-specific new_handle which can be |
| 182 | // used in a remote process to access the shared memory |
| 183 | // file. new_handle is an ouput parameter to receive |
| 184 | // the handle for use in the remote process. |
| 185 | // Returns true on success, false otherwise. |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 186 | bool ShareToProcess(ProcessHandle process, |
[email protected] | de5abb9 | 2008-10-22 21:33:27 | [diff] [blame] | 187 | SharedMemoryHandle* new_handle) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | return ShareToProcessCommon(process, new_handle, false); |
| 189 | } |
| 190 | |
| 191 | // Logically equivalent to: |
| 192 | // bool ok = ShareToProcess(process, new_handle); |
| 193 | // Close(); |
| 194 | // return ok; |
[email protected] | ed1f53e | 2009-02-11 01:32:58 | [diff] [blame] | 195 | // Note that the memory is unmapped by calling this method, regardless of the |
| 196 | // return value. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 197 | bool GiveToProcess(ProcessHandle process, |
[email protected] | de5abb9 | 2008-10-22 21:33:27 | [diff] [blame] | 198 | SharedMemoryHandle* new_handle) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | return ShareToProcessCommon(process, new_handle, true); |
| 200 | } |
| 201 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 202 | // Locks the shared memory. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 203 | // This is a cross-process lock which may be recursively |
| 204 | // locked by the same thread. |
[email protected] | ec031eb | 2009-02-05 19:04:34 | [diff] [blame] | 205 | // TODO(port): |
| 206 | // WARNING: on POSIX the lock only works across processes, not |
| 207 | // across threads. 2 threads in the same process can both grab the |
| 208 | // lock at the same time. There are several solutions for this |
| 209 | // (futex, lockf+anon_semaphore) but none are both clean and common |
| 210 | // across Mac and Linux. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | void Lock(); |
| 212 | |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 213 | #if defined(OS_WIN) |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 214 | // A Lock() implementation with a timeout that also allows setting |
| 215 | // security attributes on the mutex. sec_attr may be NULL. |
| 216 | // Returns true if the Lock() has been acquired, false if the timeout was |
| 217 | // reached. |
| 218 | bool Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr); |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 219 | #endif |
| 220 | |
[email protected] | b6128aa | 2010-04-29 17:44:42 | [diff] [blame] | 221 | // Releases the shared memory lock. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 222 | void Unlock(); |
| 223 | |
| 224 | private: |
[email protected] | cba5d98 | 2008-08-13 19:59:07 | [diff] [blame] | 225 | #if defined(OS_POSIX) |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 226 | bool PrepareMapFile(FILE *fp); |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 227 | bool FilePathForMemoryName(const std::string& mem_name, FilePath* path); |
[email protected] | ec031eb | 2009-02-05 19:04:34 | [diff] [blame] | 228 | void LockOrUnlockCommon(int function); |
[email protected] | cba5d98 | 2008-08-13 19:59:07 | [diff] [blame] | 229 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 230 | bool ShareToProcessCommon(ProcessHandle process, |
[email protected] | de5abb9 | 2008-10-22 21:33:27 | [diff] [blame] | 231 | SharedMemoryHandle* new_handle, |
| 232 | bool close_self); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 233 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 234 | #if defined(OS_WIN) |
[email protected] | f25810a | 2009-04-04 00:44:22 | [diff] [blame] | 235 | std::wstring name_; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 236 | HANDLE mapped_file_; |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 237 | #elif defined(OS_POSIX) |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 238 | int mapped_file_; |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 239 | uint32 mapped_size_; |
[email protected] | e68e62fa | 2009-02-20 02:00:04 | [diff] [blame] | 240 | ino_t inode_; |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 241 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 242 | void* memory_; |
| 243 | bool read_only_; |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 244 | uint32 created_size_; |
[email protected] | ec031eb | 2009-02-05 19:04:34 | [diff] [blame] | 245 | #if !defined(OS_POSIX) |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 246 | SharedMemoryLock lock_; |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 247 | #endif |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 248 | |
[email protected] | fc29bc70 | 2010-06-04 16:13:51 | [diff] [blame] | 249 | DISALLOW_COPY_AND_ASSIGN(SharedMemory); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | // A helper class that acquires the shared memory lock while |
| 253 | // the SharedMemoryAutoLock is in scope. |
| 254 | class SharedMemoryAutoLock { |
| 255 | public: |
| 256 | explicit SharedMemoryAutoLock(SharedMemory* shared_memory) |
| 257 | : shared_memory_(shared_memory) { |
| 258 | shared_memory_->Lock(); |
| 259 | } |
| 260 | |
| 261 | ~SharedMemoryAutoLock() { |
| 262 | shared_memory_->Unlock(); |
| 263 | } |
| 264 | |
| 265 | private: |
| 266 | SharedMemory* shared_memory_; |
[email protected] | fc29bc70 | 2010-06-04 16:13:51 | [diff] [blame] | 267 | DISALLOW_COPY_AND_ASSIGN(SharedMemoryAutoLock); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 268 | }; |
| 269 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 270 | } // namespace base |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 271 | |
[email protected] | 39be424 | 2008-08-07 18:31:40 | [diff] [blame] | 272 | #endif // BASE_SHARED_MEMORY_H_ |