blob: 77741bc0d9d62184b231f724c23d9abd78d138a5 [file] [log] [blame]
[email protected]b05df6b2011-12-01 23:19:311// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commitd7cae122008-07-26 21:49:384
[email protected]99873aa2013-03-29 17:46:235#include "base/memory/shared_memory.h"
initial.commitd7cae122008-07-26 21:49:386
7#include "base/logging.h"
[email protected]a4ea1f12013-06-07 18:37:078#include "base/strings/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:389
[email protected]67ea5072013-03-28 02:02:1810namespace {
11
12// Returns the length of the memory section starting at the supplied address.
13size_t GetMemorySectionSize(void* address) {
14 MEMORY_BASIC_INFORMATION memory_info;
15 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info)))
16 return 0;
17 return memory_info.RegionSize - (static_cast<char*>(address) -
18 static_cast<char*>(memory_info.AllocationBase));
19}
20
21} // namespace.
22
[email protected]176aa482008-11-14 03:25:1523namespace base {
24
initial.commitd7cae122008-07-26 21:49:3825SharedMemory::SharedMemory()
26 : mapped_file_(NULL),
27 memory_(NULL),
28 read_only_(false),
[email protected]67ea5072013-03-28 02:02:1829 mapped_size_(0),
30 requested_size_(0),
initial.commitd7cae122008-07-26 21:49:3831 lock_(NULL) {
32}
33
[email protected]8cc41942010-11-05 19:16:0734SharedMemory::SharedMemory(const std::wstring& name)
35 : mapped_file_(NULL),
36 memory_(NULL),
37 read_only_(false),
[email protected]67ea5072013-03-28 02:02:1838 requested_size_(0),
39 mapped_size_(0),
[email protected]8cc41942010-11-05 19:16:0740 lock_(NULL),
41 name_(name) {
42}
43
initial.commitd7cae122008-07-26 21:49:3844SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
45 : mapped_file_(handle),
46 memory_(NULL),
47 read_only_(read_only),
[email protected]67ea5072013-03-28 02:02:1848 requested_size_(0),
49 mapped_size_(0),
initial.commitd7cae122008-07-26 21:49:3850 lock_(NULL) {
51}
52
53SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
54 ProcessHandle process)
55 : mapped_file_(NULL),
56 memory_(NULL),
57 read_only_(read_only),
[email protected]67ea5072013-03-28 02:02:1858 requested_size_(0),
59 mapped_size_(0),
initial.commitd7cae122008-07-26 21:49:3860 lock_(NULL) {
61 ::DuplicateHandle(process, handle,
62 GetCurrentProcess(), &mapped_file_,
[email protected]5c744612013-10-30 15:17:0563 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
64 FILE_MAP_WRITE,
initial.commitd7cae122008-07-26 21:49:3865 FALSE, 0);
66}
67
68SharedMemory::~SharedMemory() {
69 Close();
70 if (lock_ != NULL)
71 CloseHandle(lock_);
72}
73
[email protected]5fe733de2009-02-11 18:59:2074// static
75bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
76 return handle != NULL;
77}
78
[email protected]76aac1e2009-03-16 16:45:3679// static
80SharedMemoryHandle SharedMemory::NULLHandle() {
81 return NULL;
82}
83
[email protected]b0af04c2009-05-18 17:46:3184// static
85void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
86 DCHECK(handle != NULL);
87 ::CloseHandle(handle);
88}
89
[email protected]c14eda92013-05-09 23:15:4090// static
91size_t SharedMemory::GetHandleLimit() {
92 // Rounded down from value reported here:
93 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx
94 return static_cast<size_t>(1 << 23);
95}
96
[email protected]374f1a82013-01-10 02:16:2497bool SharedMemory::CreateAndMapAnonymous(size_t size) {
[email protected]54e3dfa22010-10-27 18:16:0698 return CreateAnonymous(size) && Map(size);
99}
100
[email protected]b05df6b2011-12-01 23:19:31101bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
[email protected]67ea5072013-03-28 02:02:18102 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here,
103 // wasting 32k per mapping on average.
104 static const size_t kSectionMask = 65536 - 1;
[email protected]b05df6b2011-12-01 23:19:31105 DCHECK(!options.executable);
[email protected]5d2b4492011-03-01 02:48:05106 DCHECK(!mapped_file_);
[email protected]b05df6b2011-12-01 23:19:31107 if (options.size == 0)
[email protected]54e3dfa22010-10-27 18:16:06108 return false;
initial.commitd7cae122008-07-26 21:49:38109
[email protected]67ea5072013-03-28 02:02:18110 // Check maximum accounting for overflow.
111 if (options.size >
112 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask)
[email protected]374f1a82013-01-10 02:16:24113 return false;
114
[email protected]67ea5072013-03-28 02:02:18115 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask;
[email protected]b05df6b2011-12-01 23:19:31116 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name);
initial.commitd7cae122008-07-26 21:49:38117 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
[email protected]54e3dfa22010-10-27 18:16:06118 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
[email protected]b6413b49b2010-09-29 20:32:22119 name_.empty() ? NULL : name_.c_str());
initial.commitd7cae122008-07-26 21:49:38120 if (!mapped_file_)
121 return false;
122
[email protected]67ea5072013-03-28 02:02:18123 requested_size_ = options.size;
[email protected]54e3dfa22010-10-27 18:16:06124
initial.commitd7cae122008-07-26 21:49:38125 // Check if the shared memory pre-exists.
[email protected]54e3dfa22010-10-27 18:16:06126 if (GetLastError() == ERROR_ALREADY_EXISTS) {
[email protected]67ea5072013-03-28 02:02:18127 // If the file already existed, set requested_size_ to 0 to show that
[email protected]54e3dfa22010-10-27 18:16:06128 // we don't know the size.
[email protected]67ea5072013-03-28 02:02:18129 requested_size_ = 0;
[email protected]b05df6b2011-12-01 23:19:31130 if (!options.open_existing) {
[email protected]54e3dfa22010-10-27 18:16:06131 Close();
132 return false;
133 }
initial.commitd7cae122008-07-26 21:49:38134 }
[email protected]54e3dfa22010-10-27 18:16:06135
initial.commitd7cae122008-07-26 21:49:38136 return true;
137}
138
[email protected]b6413b49b2010-09-29 20:32:22139bool SharedMemory::Delete(const std::string& name) {
[email protected]9e51af92009-02-04 00:58:39140 // intentionally empty -- there is nothing for us to do on Windows.
141 return true;
142}
143
[email protected]b6413b49b2010-09-29 20:32:22144bool SharedMemory::Open(const std::string& name, bool read_only) {
[email protected]5d2b4492011-03-01 02:48:05145 DCHECK(!mapped_file_);
initial.commitd7cae122008-07-26 21:49:38146
[email protected]b6413b49b2010-09-29 20:32:22147 name_ = ASCIIToWide(name);
initial.commitd7cae122008-07-26 21:49:38148 read_only_ = read_only;
149 mapped_file_ = OpenFileMapping(
[email protected]5c744612013-10-30 15:17:05150 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE,
151 false, name_.empty() ? NULL : name_.c_str());
initial.commitd7cae122008-07-26 21:49:38152 if (mapped_file_ != NULL) {
153 // Note: size_ is not set in this case.
154 return true;
155 }
156 return false;
157}
158
[email protected]e29e3f552013-01-16 09:02:34159bool SharedMemory::MapAt(off_t offset, size_t bytes) {
initial.commitd7cae122008-07-26 21:49:38160 if (mapped_file_ == NULL)
161 return false;
162
[email protected]374f1a82013-01-10 02:16:24163 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
164 return false;
165
initial.commitd7cae122008-07-26 21:49:38166 memory_ = MapViewOfFile(mapped_file_,
[email protected]5c744612013-10-30 15:17:05167 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
168 FILE_MAP_WRITE,
[email protected]e29e3f552013-01-16 09:02:34169 static_cast<uint64>(offset) >> 32,
170 static_cast<DWORD>(offset),
171 bytes);
initial.commitd7cae122008-07-26 21:49:38172 if (memory_ != NULL) {
[email protected]404a0582012-08-18 02:17:26173 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
174 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
[email protected]67ea5072013-03-28 02:02:18175 mapped_size_ = GetMemorySectionSize(memory_);
initial.commitd7cae122008-07-26 21:49:38176 return true;
177 }
178 return false;
179}
180
181bool SharedMemory::Unmap() {
182 if (memory_ == NULL)
183 return false;
184
185 UnmapViewOfFile(memory_);
186 memory_ = NULL;
187 return true;
188}
189
190bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
191 SharedMemoryHandle *new_handle,
[email protected]5f58adab2013-11-20 23:41:25192 bool close_self,
193 ShareMode share_mode) {
initial.commitd7cae122008-07-26 21:49:38194 *new_handle = 0;
[email protected]5c744612013-10-30 15:17:05195 DWORD access = FILE_MAP_READ;
initial.commitd7cae122008-07-26 21:49:38196 DWORD options = 0;
197 HANDLE mapped_file = mapped_file_;
198 HANDLE result;
[email protected]5f58adab2013-11-20 23:41:25199 if (share_mode == SHARE_CURRENT_MODE && !read_only_)
initial.commitd7cae122008-07-26 21:49:38200 access |= FILE_MAP_WRITE;
201 if (close_self) {
202 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
203 options = DUPLICATE_CLOSE_SOURCE;
204 mapped_file_ = NULL;
205 Unmap();
206 }
207
208 if (process == GetCurrentProcess() && close_self) {
209 *new_handle = mapped_file;
210 return true;
211 }
212
213 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
214 &result, access, FALSE, options))
215 return false;
216 *new_handle = result;
217 return true;
218}
219
220
221void SharedMemory::Close() {
222 if (memory_ != NULL) {
223 UnmapViewOfFile(memory_);
224 memory_ = NULL;
225 }
226
227 if (mapped_file_ != NULL) {
228 CloseHandle(mapped_file_);
229 mapped_file_ = NULL;
230 }
231}
232
233void SharedMemory::Lock() {
[email protected]42155162010-11-16 14:25:03234 Lock(INFINITE, NULL);
[email protected]8cc41942010-11-05 19:16:07235}
236
[email protected]42155162010-11-16 14:25:03237bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) {
initial.commitd7cae122008-07-26 21:49:38238 if (lock_ == NULL) {
239 std::wstring name = name_;
240 name.append(L"lock");
[email protected]42155162010-11-16 14:25:03241 lock_ = CreateMutex(sec_attr, FALSE, name.c_str());
initial.commitd7cae122008-07-26 21:49:38242 if (lock_ == NULL) {
[email protected]a42d4632011-10-26 21:48:00243 DPLOG(ERROR) << "Could not create mutex.";
[email protected]8cc41942010-11-05 19:16:07244 return false; // there is nothing good we can do here.
initial.commitd7cae122008-07-26 21:49:38245 }
246 }
[email protected]8cc41942010-11-05 19:16:07247 DWORD result = WaitForSingleObject(lock_, timeout_ms);
248
249 // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED.
250 return (result == WAIT_OBJECT_0);
initial.commitd7cae122008-07-26 21:49:38251}
252
253void SharedMemory::Unlock() {
254 DCHECK(lock_ != NULL);
255 ReleaseMutex(lock_);
256}
license.botbf09a502008-08-24 00:55:55257
[email protected]5fe733de2009-02-11 18:59:20258SharedMemoryHandle SharedMemory::handle() const {
259 return mapped_file_;
260}
261
[email protected]176aa482008-11-14 03:25:15262} // namespace base