blob: 877ccd76e3310370b0394f196cbb6bee9290c581 [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
5#include "base/shared_memory.h"
6
7#include "base/logging.h"
[email protected]b6413b49b2010-09-29 20:32:228#include "base/utf_string_conversions.h"
initial.commitd7cae122008-07-26 21:49:389
[email protected]176aa482008-11-14 03:25:1510namespace base {
11
initial.commitd7cae122008-07-26 21:49:3812SharedMemory::SharedMemory()
13 : mapped_file_(NULL),
14 memory_(NULL),
15 read_only_(false),
[email protected]54e3dfa22010-10-27 18:16:0616 created_size_(0),
initial.commitd7cae122008-07-26 21:49:3817 lock_(NULL) {
18}
19
[email protected]8cc41942010-11-05 19:16:0720SharedMemory::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.commitd7cae122008-07-26 21:49:3829SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
30 : mapped_file_(handle),
31 memory_(NULL),
32 read_only_(read_only),
[email protected]54e3dfa22010-10-27 18:16:0633 created_size_(0),
initial.commitd7cae122008-07-26 21:49:3834 lock_(NULL) {
35}
36
37SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
38 ProcessHandle process)
39 : mapped_file_(NULL),
40 memory_(NULL),
41 read_only_(read_only),
[email protected]54e3dfa22010-10-27 18:16:0642 created_size_(0),
initial.commitd7cae122008-07-26 21:49:3843 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
51SharedMemory::~SharedMemory() {
52 Close();
53 if (lock_ != NULL)
54 CloseHandle(lock_);
55}
56
[email protected]5fe733de2009-02-11 18:59:2057// static
58bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
59 return handle != NULL;
60}
61
[email protected]76aac1e2009-03-16 16:45:3662// static
63SharedMemoryHandle SharedMemory::NULLHandle() {
64 return NULL;
65}
66
[email protected]b0af04c2009-05-18 17:46:3167// static
68void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
69 DCHECK(handle != NULL);
70 ::CloseHandle(handle);
71}
72
[email protected]374f1a82013-01-10 02:16:2473bool SharedMemory::CreateAndMapAnonymous(size_t size) {
[email protected]54e3dfa22010-10-27 18:16:0674 return CreateAnonymous(size) && Map(size);
75}
76
[email protected]b05df6b2011-12-01 23:19:3177bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
78 DCHECK(!options.executable);
[email protected]5d2b4492011-03-01 02:48:0579 DCHECK(!mapped_file_);
[email protected]b05df6b2011-12-01 23:19:3180 if (options.size == 0)
[email protected]54e3dfa22010-10-27 18:16:0681 return false;
initial.commitd7cae122008-07-26 21:49:3882
[email protected]374f1a82013-01-10 02:16:2483 if (options.size > static_cast<size_t>(std::numeric_limits<int>::max()))
84 return false;
85
[email protected]97684352010-03-25 15:40:2486 // NaCl's memory allocator requires 0mod64K alignment and size for
87 // shared memory objects. To allow passing shared memory to NaCl,
88 // therefore we round the size actually created to the nearest 64K unit.
89 // To avoid client impact, we continue to retain the size as the
90 // actual requested size.
[email protected]b05df6b2011-12-01 23:19:3191 uint32 rounded_size = (options.size + 0xffff) & ~0xffff;
[email protected]935cb0de2012-12-06 00:49:0892 if (rounded_size < options.size)
93 return false;
[email protected]b05df6b2011-12-01 23:19:3194 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name);
initial.commitd7cae122008-07-26 21:49:3895 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
[email protected]54e3dfa22010-10-27 18:16:0696 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
[email protected]b6413b49b2010-09-29 20:32:2297 name_.empty() ? NULL : name_.c_str());
initial.commitd7cae122008-07-26 21:49:3898 if (!mapped_file_)
99 return false;
100
[email protected]b05df6b2011-12-01 23:19:31101 created_size_ = options.size;
[email protected]54e3dfa22010-10-27 18:16:06102
initial.commitd7cae122008-07-26 21:49:38103 // Check if the shared memory pre-exists.
[email protected]54e3dfa22010-10-27 18:16:06104 if (GetLastError() == ERROR_ALREADY_EXISTS) {
105 // If the file already existed, set created_size_ to 0 to show that
106 // we don't know the size.
107 created_size_ = 0;
[email protected]b05df6b2011-12-01 23:19:31108 if (!options.open_existing) {
[email protected]54e3dfa22010-10-27 18:16:06109 Close();
110 return false;
111 }
initial.commitd7cae122008-07-26 21:49:38112 }
[email protected]54e3dfa22010-10-27 18:16:06113
initial.commitd7cae122008-07-26 21:49:38114 return true;
115}
116
[email protected]b6413b49b2010-09-29 20:32:22117bool SharedMemory::Delete(const std::string& name) {
[email protected]9e51af92009-02-04 00:58:39118 // intentionally empty -- there is nothing for us to do on Windows.
119 return true;
120}
121
[email protected]b6413b49b2010-09-29 20:32:22122bool SharedMemory::Open(const std::string& name, bool read_only) {
[email protected]5d2b4492011-03-01 02:48:05123 DCHECK(!mapped_file_);
initial.commitd7cae122008-07-26 21:49:38124
[email protected]b6413b49b2010-09-29 20:32:22125 name_ = ASCIIToWide(name);
initial.commitd7cae122008-07-26 21:49:38126 read_only_ = read_only;
127 mapped_file_ = OpenFileMapping(
128 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false,
[email protected]b6413b49b2010-09-29 20:32:22129 name_.empty() ? NULL : name_.c_str());
initial.commitd7cae122008-07-26 21:49:38130 if (mapped_file_ != NULL) {
131 // Note: size_ is not set in this case.
132 return true;
133 }
134 return false;
135}
136
[email protected]374f1a82013-01-10 02:16:24137bool SharedMemory::Map(size_t bytes) {
initial.commitd7cae122008-07-26 21:49:38138 if (mapped_file_ == NULL)
139 return false;
140
[email protected]374f1a82013-01-10 02:16:24141 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
142 return false;
143
initial.commitd7cae122008-07-26 21:49:38144 memory_ = MapViewOfFile(mapped_file_,
145 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes);
146 if (memory_ != NULL) {
[email protected]404a0582012-08-18 02:17:26147 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
148 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
initial.commitd7cae122008-07-26 21:49:38149 return true;
150 }
151 return false;
152}
153
154bool SharedMemory::Unmap() {
155 if (memory_ == NULL)
156 return false;
157
158 UnmapViewOfFile(memory_);
159 memory_ = NULL;
160 return true;
161}
162
163bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
164 SharedMemoryHandle *new_handle,
165 bool close_self) {
166 *new_handle = 0;
167 DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ;
168 DWORD options = 0;
169 HANDLE mapped_file = mapped_file_;
170 HANDLE result;
171 if (!read_only_)
172 access |= FILE_MAP_WRITE;
173 if (close_self) {
174 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
175 options = DUPLICATE_CLOSE_SOURCE;
176 mapped_file_ = NULL;
177 Unmap();
178 }
179
180 if (process == GetCurrentProcess() && close_self) {
181 *new_handle = mapped_file;
182 return true;
183 }
184
185 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
186 &result, access, FALSE, options))
187 return false;
188 *new_handle = result;
189 return true;
190}
191
192
193void SharedMemory::Close() {
194 if (memory_ != NULL) {
195 UnmapViewOfFile(memory_);
196 memory_ = NULL;
197 }
198
199 if (mapped_file_ != NULL) {
200 CloseHandle(mapped_file_);
201 mapped_file_ = NULL;
202 }
203}
204
205void SharedMemory::Lock() {
[email protected]42155162010-11-16 14:25:03206 Lock(INFINITE, NULL);
[email protected]8cc41942010-11-05 19:16:07207}
208
[email protected]42155162010-11-16 14:25:03209bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) {
initial.commitd7cae122008-07-26 21:49:38210 if (lock_ == NULL) {
211 std::wstring name = name_;
212 name.append(L"lock");
[email protected]42155162010-11-16 14:25:03213 lock_ = CreateMutex(sec_attr, FALSE, name.c_str());
initial.commitd7cae122008-07-26 21:49:38214 if (lock_ == NULL) {
[email protected]a42d4632011-10-26 21:48:00215 DPLOG(ERROR) << "Could not create mutex.";
[email protected]8cc41942010-11-05 19:16:07216 return false; // there is nothing good we can do here.
initial.commitd7cae122008-07-26 21:49:38217 }
218 }
[email protected]8cc41942010-11-05 19:16:07219 DWORD result = WaitForSingleObject(lock_, timeout_ms);
220
221 // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED.
222 return (result == WAIT_OBJECT_0);
initial.commitd7cae122008-07-26 21:49:38223}
224
225void SharedMemory::Unlock() {
226 DCHECK(lock_ != NULL);
227 ReleaseMutex(lock_);
228}
license.botbf09a502008-08-24 00:55:55229
[email protected]5fe733de2009-02-11 18:59:20230SharedMemoryHandle SharedMemory::handle() const {
231 return mapped_file_;
232}
233
[email protected]176aa482008-11-14 03:25:15234} // namespace base