license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
| 5 | #include "base/shared_memory.h" |
| 6 | |
| 7 | #include "base/logging.h" |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 8 | #include "base/utf_string_conversions.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 9 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 10 | namespace base { |
| 11 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 12 | SharedMemory::SharedMemory() |
| 13 | : mapped_file_(NULL), |
| 14 | memory_(NULL), |
| 15 | read_only_(false), |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 16 | created_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 17 | lock_(NULL) { |
| 18 | } |
| 19 | |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 20 | SharedMemory::SharedMemory(const std::wstring& name) |
| 21 | : mapped_file_(NULL), |
| 22 | memory_(NULL), |
| 23 | read_only_(false), |
| 24 | created_size_(0), |
| 25 | lock_(NULL), |
| 26 | name_(name) { |
| 27 | } |
| 28 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 29 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only) |
| 30 | : mapped_file_(handle), |
| 31 | memory_(NULL), |
| 32 | read_only_(read_only), |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 33 | created_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 34 | lock_(NULL) { |
| 35 | } |
| 36 | |
| 37 | SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only, |
| 38 | ProcessHandle process) |
| 39 | : mapped_file_(NULL), |
| 40 | memory_(NULL), |
| 41 | read_only_(read_only), |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 42 | created_size_(0), |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 43 | lock_(NULL) { |
| 44 | ::DuplicateHandle(process, handle, |
| 45 | GetCurrentProcess(), &mapped_file_, |
| 46 | STANDARD_RIGHTS_REQUIRED | |
| 47 | (read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS), |
| 48 | FALSE, 0); |
| 49 | } |
| 50 | |
| 51 | SharedMemory::~SharedMemory() { |
| 52 | Close(); |
| 53 | if (lock_ != NULL) |
| 54 | CloseHandle(lock_); |
| 55 | } |
| 56 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 57 | // static |
| 58 | bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) { |
| 59 | return handle != NULL; |
| 60 | } |
| 61 | |
[email protected] | 76aac1e | 2009-03-16 16:45:36 | [diff] [blame] | 62 | // static |
| 63 | SharedMemoryHandle SharedMemory::NULLHandle() { |
| 64 | return NULL; |
| 65 | } |
| 66 | |
[email protected] | b0af04c | 2009-05-18 17:46:31 | [diff] [blame] | 67 | // static |
| 68 | void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) { |
| 69 | DCHECK(handle != NULL); |
| 70 | ::CloseHandle(handle); |
| 71 | } |
| 72 | |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 73 | bool SharedMemory::CreateAndMapAnonymous(uint32 size) { |
| 74 | return CreateAnonymous(size) && Map(size); |
| 75 | } |
| 76 | |
| 77 | bool SharedMemory::CreateAnonymous(uint32 size) { |
| 78 | return CreateNamed("", false, size); |
| 79 | } |
| 80 | |
| 81 | bool SharedMemory::CreateNamed(const std::string& name, |
| 82 | bool open_existing, uint32 size) { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame^] | 83 | DCHECK(!mapped_file_); |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 84 | if (size == 0) |
| 85 | return false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 86 | |
[email protected] | 9768435 | 2010-03-25 15:40:24 | [diff] [blame] | 87 | // NaCl's memory allocator requires 0mod64K alignment and size for |
| 88 | // shared memory objects. To allow passing shared memory to NaCl, |
| 89 | // therefore we round the size actually created to the nearest 64K unit. |
| 90 | // To avoid client impact, we continue to retain the size as the |
| 91 | // actual requested size. |
| 92 | uint32 rounded_size = (size + 0xffff) & ~0xffff; |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 93 | name_ = ASCIIToWide(name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 94 | mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 95 | PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 96 | name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 97 | if (!mapped_file_) |
| 98 | return false; |
| 99 | |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 100 | created_size_ = size; |
| 101 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 102 | // Check if the shared memory pre-exists. |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 103 | if (GetLastError() == ERROR_ALREADY_EXISTS) { |
| 104 | // If the file already existed, set created_size_ to 0 to show that |
| 105 | // we don't know the size. |
| 106 | created_size_ = 0; |
| 107 | if (!open_existing) { |
| 108 | Close(); |
| 109 | return false; |
| 110 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 111 | } |
[email protected] | 54e3dfa2 | 2010-10-27 18:16:06 | [diff] [blame] | 112 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 113 | return true; |
| 114 | } |
| 115 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 116 | bool SharedMemory::Delete(const std::string& name) { |
[email protected] | 9e51af9 | 2009-02-04 00:58:39 | [diff] [blame] | 117 | // intentionally empty -- there is nothing for us to do on Windows. |
| 118 | return true; |
| 119 | } |
| 120 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 121 | bool SharedMemory::Open(const std::string& name, bool read_only) { |
[email protected] | 5d2b449 | 2011-03-01 02:48:05 | [diff] [blame^] | 122 | DCHECK(!mapped_file_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 123 | |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 124 | name_ = ASCIIToWide(name); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 125 | read_only_ = read_only; |
| 126 | mapped_file_ = OpenFileMapping( |
| 127 | read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false, |
[email protected] | b6413b49b | 2010-09-29 20:32:22 | [diff] [blame] | 128 | name_.empty() ? NULL : name_.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 129 | if (mapped_file_ != NULL) { |
| 130 | // Note: size_ is not set in this case. |
| 131 | return true; |
| 132 | } |
| 133 | return false; |
| 134 | } |
| 135 | |
[email protected] | b5ab398 | 2010-02-16 23:58:27 | [diff] [blame] | 136 | bool SharedMemory::Map(uint32 bytes) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 137 | if (mapped_file_ == NULL) |
| 138 | return false; |
| 139 | |
| 140 | memory_ = MapViewOfFile(mapped_file_, |
| 141 | read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes); |
| 142 | if (memory_ != NULL) { |
| 143 | return true; |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool SharedMemory::Unmap() { |
| 149 | if (memory_ == NULL) |
| 150 | return false; |
| 151 | |
| 152 | UnmapViewOfFile(memory_); |
| 153 | memory_ = NULL; |
| 154 | return true; |
| 155 | } |
| 156 | |
| 157 | bool SharedMemory::ShareToProcessCommon(ProcessHandle process, |
| 158 | SharedMemoryHandle *new_handle, |
| 159 | bool close_self) { |
| 160 | *new_handle = 0; |
| 161 | DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ; |
| 162 | DWORD options = 0; |
| 163 | HANDLE mapped_file = mapped_file_; |
| 164 | HANDLE result; |
| 165 | if (!read_only_) |
| 166 | access |= FILE_MAP_WRITE; |
| 167 | if (close_self) { |
| 168 | // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file. |
| 169 | options = DUPLICATE_CLOSE_SOURCE; |
| 170 | mapped_file_ = NULL; |
| 171 | Unmap(); |
| 172 | } |
| 173 | |
| 174 | if (process == GetCurrentProcess() && close_self) { |
| 175 | *new_handle = mapped_file; |
| 176 | return true; |
| 177 | } |
| 178 | |
| 179 | if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process, |
| 180 | &result, access, FALSE, options)) |
| 181 | return false; |
| 182 | *new_handle = result; |
| 183 | return true; |
| 184 | } |
| 185 | |
| 186 | |
| 187 | void SharedMemory::Close() { |
| 188 | if (memory_ != NULL) { |
| 189 | UnmapViewOfFile(memory_); |
| 190 | memory_ = NULL; |
| 191 | } |
| 192 | |
| 193 | if (mapped_file_ != NULL) { |
| 194 | CloseHandle(mapped_file_); |
| 195 | mapped_file_ = NULL; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | void SharedMemory::Lock() { |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 200 | Lock(INFINITE, NULL); |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 201 | } |
| 202 | |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 203 | bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | if (lock_ == NULL) { |
| 205 | std::wstring name = name_; |
| 206 | name.append(L"lock"); |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 207 | lock_ = CreateMutex(sec_attr, FALSE, name.c_str()); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 208 | if (lock_ == NULL) { |
[email protected] | 4215516 | 2010-11-16 14:25:03 | [diff] [blame] | 209 | PLOG(ERROR) << "Could not create mutex."; |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 210 | return false; // there is nothing good we can do here. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | } |
| 212 | } |
[email protected] | 8cc4194 | 2010-11-05 19:16:07 | [diff] [blame] | 213 | DWORD result = WaitForSingleObject(lock_, timeout_ms); |
| 214 | |
| 215 | // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED. |
| 216 | return (result == WAIT_OBJECT_0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | void SharedMemory::Unlock() { |
| 220 | DCHECK(lock_ != NULL); |
| 221 | ReleaseMutex(lock_); |
| 222 | } |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 223 | |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 224 | SharedMemoryHandle SharedMemory::handle() const { |
| 225 | return mapped_file_; |
| 226 | } |
| 227 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 228 | } // namespace base |