Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
michael@0 | 2 | // Use of this source code is governed by a BSD-style license that can be |
michael@0 | 3 | // found in the LICENSE file. |
michael@0 | 4 | |
michael@0 | 5 | #ifndef BASE_SHARED_MEMORY_H_ |
michael@0 | 6 | #define BASE_SHARED_MEMORY_H_ |
michael@0 | 7 | |
michael@0 | 8 | #include "build/build_config.h" |
michael@0 | 9 | |
michael@0 | 10 | #if defined(OS_POSIX) |
michael@0 | 11 | #include <sys/types.h> |
michael@0 | 12 | #include <semaphore.h> |
michael@0 | 13 | #include "base/file_descriptor_posix.h" |
michael@0 | 14 | #endif |
michael@0 | 15 | #include <string> |
michael@0 | 16 | |
michael@0 | 17 | #include "base/basictypes.h" |
michael@0 | 18 | #include "base/process.h" |
michael@0 | 19 | |
michael@0 | 20 | namespace base { |
michael@0 | 21 | |
michael@0 | 22 | // SharedMemoryHandle is a platform specific type which represents |
michael@0 | 23 | // the underlying OS handle to a shared memory segment. |
michael@0 | 24 | #if defined(OS_WIN) |
michael@0 | 25 | typedef HANDLE SharedMemoryHandle; |
michael@0 | 26 | typedef HANDLE SharedMemoryLock; |
michael@0 | 27 | #elif defined(OS_POSIX) |
michael@0 | 28 | // A SharedMemoryId is sufficient to identify a given shared memory segment on a |
michael@0 | 29 | // system, but insufficient to map it. |
michael@0 | 30 | typedef FileDescriptor SharedMemoryHandle; |
michael@0 | 31 | typedef ino_t SharedMemoryId; |
michael@0 | 32 | // On POSIX, the lock is implemented as a lockf() on the mapped file, |
michael@0 | 33 | // so no additional member (or definition of SharedMemoryLock) is |
michael@0 | 34 | // needed. |
michael@0 | 35 | #endif |
michael@0 | 36 | |
michael@0 | 37 | // Platform abstraction for shared memory. Provides a C++ wrapper |
michael@0 | 38 | // around the OS primitive for a memory mapped file. |
michael@0 | 39 | class SharedMemory { |
michael@0 | 40 | public: |
michael@0 | 41 | // Create a new SharedMemory object. |
michael@0 | 42 | SharedMemory(); |
michael@0 | 43 | |
michael@0 | 44 | // Create a new SharedMemory object from an existing, open |
michael@0 | 45 | // shared memory file. |
michael@0 | 46 | SharedMemory(SharedMemoryHandle handle, bool read_only); |
michael@0 | 47 | |
michael@0 | 48 | // Create a new SharedMemory object from an existing, open |
michael@0 | 49 | // shared memory file that was created by a remote process and not shared |
michael@0 | 50 | // to the current process. |
michael@0 | 51 | SharedMemory(SharedMemoryHandle handle, bool read_only, |
michael@0 | 52 | base::ProcessHandle process); |
michael@0 | 53 | |
michael@0 | 54 | // Destructor. Will close any open files. |
michael@0 | 55 | ~SharedMemory(); |
michael@0 | 56 | |
michael@0 | 57 | // Return true iff the given handle is valid (i.e. not the distingished |
michael@0 | 58 | // invalid value; NULL for a HANDLE and -1 for a file descriptor) |
michael@0 | 59 | static bool IsHandleValid(const SharedMemoryHandle& handle); |
michael@0 | 60 | |
michael@0 | 61 | // Return invalid handle (see comment above for exact definition). |
michael@0 | 62 | static SharedMemoryHandle NULLHandle(); |
michael@0 | 63 | |
michael@0 | 64 | // Creates or opens a shared memory segment based on a name. |
michael@0 | 65 | // If read_only is true, opens the memory as read-only. |
michael@0 | 66 | // If open_existing is true, and the shared memory already exists, |
michael@0 | 67 | // opens the existing shared memory and ignores the size parameter. |
michael@0 | 68 | // If name is the empty string, use a unique name. |
michael@0 | 69 | // Returns true on success, false on failure. |
michael@0 | 70 | bool Create(const std::string& name, bool read_only, bool open_existing, |
michael@0 | 71 | size_t size); |
michael@0 | 72 | |
michael@0 | 73 | // Deletes resources associated with a shared memory segment based on name. |
michael@0 | 74 | // Not all platforms require this call. |
michael@0 | 75 | bool Delete(const std::wstring& name); |
michael@0 | 76 | |
michael@0 | 77 | // Opens a shared memory segment based on a name. |
michael@0 | 78 | // If read_only is true, opens for read-only access. |
michael@0 | 79 | // If name is the empty string, use a unique name. |
michael@0 | 80 | // Returns true on success, false on failure. |
michael@0 | 81 | bool Open(const std::wstring& name, bool read_only); |
michael@0 | 82 | |
michael@0 | 83 | // Maps the shared memory into the caller's address space. |
michael@0 | 84 | // Returns true on success, false otherwise. The memory address |
michael@0 | 85 | // is accessed via the memory() accessor. |
michael@0 | 86 | bool Map(size_t bytes); |
michael@0 | 87 | |
michael@0 | 88 | // Unmaps the shared memory from the caller's address space. |
michael@0 | 89 | // Returns true if successful; returns false on error or if the |
michael@0 | 90 | // memory is not mapped. |
michael@0 | 91 | bool Unmap(); |
michael@0 | 92 | |
michael@0 | 93 | // Get the size of the opened shared memory backing file. |
michael@0 | 94 | // Note: This size is only available to the creator of the |
michael@0 | 95 | // shared memory, and not to those that opened shared memory |
michael@0 | 96 | // created externally. |
michael@0 | 97 | // Returns 0 if not opened or unknown. |
michael@0 | 98 | size_t max_size() const { return max_size_; } |
michael@0 | 99 | |
michael@0 | 100 | // Gets a pointer to the opened memory space if it has been |
michael@0 | 101 | // Mapped via Map(). Returns NULL if it is not mapped. |
michael@0 | 102 | void *memory() const { return memory_; } |
michael@0 | 103 | |
michael@0 | 104 | // Get access to the underlying OS handle for this segment. |
michael@0 | 105 | // Use of this handle for anything other than an opaque |
michael@0 | 106 | // identifier is not portable. |
michael@0 | 107 | SharedMemoryHandle handle() const; |
michael@0 | 108 | |
michael@0 | 109 | #if defined(OS_POSIX) |
michael@0 | 110 | // Return a unique identifier for this shared memory segment. Inode numbers |
michael@0 | 111 | // are technically only unique to a single filesystem. However, we always |
michael@0 | 112 | // allocate shared memory backing files from the same directory, so will end |
michael@0 | 113 | // up on the same filesystem. |
michael@0 | 114 | SharedMemoryId id() const { return inode_; } |
michael@0 | 115 | #endif |
michael@0 | 116 | |
michael@0 | 117 | // Closes the open shared memory segment. |
michael@0 | 118 | // It is safe to call Close repeatedly. |
michael@0 | 119 | void Close(); |
michael@0 | 120 | |
michael@0 | 121 | // Share the shared memory to another process. Attempts |
michael@0 | 122 | // to create a platform-specific new_handle which can be |
michael@0 | 123 | // used in a remote process to access the shared memory |
michael@0 | 124 | // file. new_handle is an ouput parameter to receive |
michael@0 | 125 | // the handle for use in the remote process. |
michael@0 | 126 | // Returns true on success, false otherwise. |
michael@0 | 127 | bool ShareToProcess(base::ProcessHandle process, |
michael@0 | 128 | SharedMemoryHandle* new_handle) { |
michael@0 | 129 | return ShareToProcessCommon(process, new_handle, false); |
michael@0 | 130 | } |
michael@0 | 131 | |
michael@0 | 132 | // Logically equivalent to: |
michael@0 | 133 | // bool ok = ShareToProcess(process, new_handle); |
michael@0 | 134 | // Close(); |
michael@0 | 135 | // return ok; |
michael@0 | 136 | // Note that the memory is unmapped by calling this method, regardless of the |
michael@0 | 137 | // return value. |
michael@0 | 138 | bool GiveToProcess(ProcessHandle process, |
michael@0 | 139 | SharedMemoryHandle* new_handle) { |
michael@0 | 140 | return ShareToProcessCommon(process, new_handle, true); |
michael@0 | 141 | } |
michael@0 | 142 | |
michael@0 | 143 | // Lock the shared memory. |
michael@0 | 144 | // This is a cross-process lock which may be recursively |
michael@0 | 145 | // locked by the same thread. |
michael@0 | 146 | // TODO(port): |
michael@0 | 147 | // WARNING: on POSIX the lock only works across processes, not |
michael@0 | 148 | // across threads. 2 threads in the same process can both grab the |
michael@0 | 149 | // lock at the same time. There are several solutions for this |
michael@0 | 150 | // (futex, lockf+anon_semaphore) but none are both clean and common |
michael@0 | 151 | // across Mac and Linux. |
michael@0 | 152 | void Lock(); |
michael@0 | 153 | |
michael@0 | 154 | // Release the shared memory lock. |
michael@0 | 155 | void Unlock(); |
michael@0 | 156 | |
michael@0 | 157 | private: |
michael@0 | 158 | #if defined(OS_POSIX) |
michael@0 | 159 | bool CreateOrOpen(const std::wstring &name, int posix_flags, size_t size); |
michael@0 | 160 | bool FilenameForMemoryName(const std::wstring &memname, |
michael@0 | 161 | std::wstring *filename); |
michael@0 | 162 | void LockOrUnlockCommon(int function); |
michael@0 | 163 | |
michael@0 | 164 | #endif |
michael@0 | 165 | bool ShareToProcessCommon(ProcessHandle process, |
michael@0 | 166 | SharedMemoryHandle* new_handle, |
michael@0 | 167 | bool close_self); |
michael@0 | 168 | |
michael@0 | 169 | #if defined(OS_WIN) |
michael@0 | 170 | std::wstring name_; |
michael@0 | 171 | HANDLE mapped_file_; |
michael@0 | 172 | #elif defined(OS_POSIX) |
michael@0 | 173 | int mapped_file_; |
michael@0 | 174 | ino_t inode_; |
michael@0 | 175 | #endif |
michael@0 | 176 | void* memory_; |
michael@0 | 177 | bool read_only_; |
michael@0 | 178 | size_t max_size_; |
michael@0 | 179 | #if !defined(OS_POSIX) |
michael@0 | 180 | SharedMemoryLock lock_; |
michael@0 | 181 | #endif |
michael@0 | 182 | |
michael@0 | 183 | DISALLOW_EVIL_CONSTRUCTORS(SharedMemory); |
michael@0 | 184 | }; |
michael@0 | 185 | |
michael@0 | 186 | // A helper class that acquires the shared memory lock while |
michael@0 | 187 | // the SharedMemoryAutoLock is in scope. |
michael@0 | 188 | class SharedMemoryAutoLock { |
michael@0 | 189 | public: |
michael@0 | 190 | explicit SharedMemoryAutoLock(SharedMemory* shared_memory) |
michael@0 | 191 | : shared_memory_(shared_memory) { |
michael@0 | 192 | shared_memory_->Lock(); |
michael@0 | 193 | } |
michael@0 | 194 | |
michael@0 | 195 | ~SharedMemoryAutoLock() { |
michael@0 | 196 | shared_memory_->Unlock(); |
michael@0 | 197 | } |
michael@0 | 198 | |
michael@0 | 199 | private: |
michael@0 | 200 | SharedMemory* shared_memory_; |
michael@0 | 201 | DISALLOW_EVIL_CONSTRUCTORS(SharedMemoryAutoLock); |
michael@0 | 202 | }; |
michael@0 | 203 | |
michael@0 | 204 | } // namespace base |
michael@0 | 205 | |
michael@0 | 206 | #endif // BASE_SHARED_MEMORY_H_ |