blob: a4f86cbc15de70e51389caa21ad0458f9cff9b7e [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]54e3dfa22010-10-27 18:16:0673bool SharedMemory::CreateAndMapAnonymous(uint32 size) {
74 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]97684352010-03-25 15:40:2483 // NaCl's memory allocator requires 0mod64K alignment and size for
84 // shared memory objects. To allow passing shared memory to NaCl,
85 // therefore we round the size actually created to the nearest 64K unit.
86 // To avoid client impact, we continue to retain the size as the
87 // actual requested size.
[email protected]b05df6b2011-12-01 23:19:3188 uint32 rounded_size = (options.size + 0xffff) & ~0xffff;
89 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name);
initial.commitd7cae122008-07-26 21:49:3890 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
[email protected]54e3dfa22010-10-27 18:16:0691 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
[email protected]b6413b49b2010-09-29 20:32:2292 name_.empty() ? NULL : name_.c_str());
initial.commitd7cae122008-07-26 21:49:3893 if (!mapped_file_)
94 return false;
95
[email protected]b05df6b2011-12-01 23:19:3196 created_size_ = options.size;
[email protected]54e3dfa22010-10-27 18:16:0697
initial.commitd7cae122008-07-26 21:49:3898 // Check if the shared memory pre-exists.
[email protected]54e3dfa22010-10-27 18:16:0699 if (GetLastError() == ERROR_ALREADY_EXISTS) {
100 // If the file already existed, set created_size_ to 0 to show that
101 // we don't know the size.
102 created_size_ = 0;
[email protected]b05df6b2011-12-01 23:19:31103 if (!options.open_existing) {
[email protected]54e3dfa22010-10-27 18:16:06104 Close();
105 return false;
106 }
initial.commitd7cae122008-07-26 21:49:38107 }
[email protected]54e3dfa22010-10-27 18:16:06108
initial.commitd7cae122008-07-26 21:49:38109 return true;
110}
111
[email protected]b6413b49b2010-09-29 20:32:22112bool SharedMemory::Delete(const std::string& name) {
[email protected]9e51af92009-02-04 00:58:39113 // intentionally empty -- there is nothing for us to do on Windows.
114 return true;
115}
116
[email protected]b6413b49b2010-09-29 20:32:22117bool SharedMemory::Open(const std::string& name, bool read_only) {
[email protected]5d2b4492011-03-01 02:48:05118 DCHECK(!mapped_file_);
initial.commitd7cae122008-07-26 21:49:38119
[email protected]b6413b49b2010-09-29 20:32:22120 name_ = ASCIIToWide(name);
initial.commitd7cae122008-07-26 21:49:38121 read_only_ = read_only;
122 mapped_file_ = OpenFileMapping(
123 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false,
[email protected]b6413b49b2010-09-29 20:32:22124 name_.empty() ? NULL : name_.c_str());
initial.commitd7cae122008-07-26 21:49:38125 if (mapped_file_ != NULL) {
126 // Note: size_ is not set in this case.
127 return true;
128 }
129 return false;
130}
131
[email protected]b5ab3982010-02-16 23:58:27132bool SharedMemory::Map(uint32 bytes) {
initial.commitd7cae122008-07-26 21:49:38133 if (mapped_file_ == NULL)
134 return false;
135
136 memory_ = MapViewOfFile(mapped_file_,
137 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes);
138 if (memory_ != NULL) {
[email protected]404a0582012-08-18 02:17:26139 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
140 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
initial.commitd7cae122008-07-26 21:49:38141 return true;
142 }
143 return false;
144}
145
146bool SharedMemory::Unmap() {
147 if (memory_ == NULL)
148 return false;
149
150 UnmapViewOfFile(memory_);
151 memory_ = NULL;
152 return true;
153}
154
155bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
156 SharedMemoryHandle *new_handle,
157 bool close_self) {
158 *new_handle = 0;
159 DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ;
160 DWORD options = 0;
161 HANDLE mapped_file = mapped_file_;
162 HANDLE result;
163 if (!read_only_)
164 access |= FILE_MAP_WRITE;
165 if (close_self) {
166 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
167 options = DUPLICATE_CLOSE_SOURCE;
168 mapped_file_ = NULL;
169 Unmap();
170 }
171
172 if (process == GetCurrentProcess() && close_self) {
173 *new_handle = mapped_file;
174 return true;
175 }
176
177 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
178 &result, access, FALSE, options))
179 return false;
180 *new_handle = result;
181 return true;
182}
183
184
185void SharedMemory::Close() {
186 if (memory_ != NULL) {
187 UnmapViewOfFile(memory_);
188 memory_ = NULL;
189 }
190
191 if (mapped_file_ != NULL) {
192 CloseHandle(mapped_file_);
193 mapped_file_ = NULL;
194 }
195}
196
197void SharedMemory::Lock() {
[email protected]42155162010-11-16 14:25:03198 Lock(INFINITE, NULL);
[email protected]8cc41942010-11-05 19:16:07199}
200
[email protected]42155162010-11-16 14:25:03201bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) {
initial.commitd7cae122008-07-26 21:49:38202 if (lock_ == NULL) {
203 std::wstring name = name_;
204 name.append(L"lock");
[email protected]42155162010-11-16 14:25:03205 lock_ = CreateMutex(sec_attr, FALSE, name.c_str());
initial.commitd7cae122008-07-26 21:49:38206 if (lock_ == NULL) {
[email protected]a42d4632011-10-26 21:48:00207 DPLOG(ERROR) << "Could not create mutex.";
[email protected]8cc41942010-11-05 19:16:07208 return false; // there is nothing good we can do here.
initial.commitd7cae122008-07-26 21:49:38209 }
210 }
[email protected]8cc41942010-11-05 19:16:07211 DWORD result = WaitForSingleObject(lock_, timeout_ms);
212
213 // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED.
214 return (result == WAIT_OBJECT_0);
initial.commitd7cae122008-07-26 21:49:38215}
216
217void SharedMemory::Unlock() {
218 DCHECK(lock_ != NULL);
219 ReleaseMutex(lock_);
220}
license.botbf09a502008-08-24 00:55:55221
[email protected]5fe733de2009-02-11 18:59:20222SharedMemoryHandle SharedMemory::handle() const {
223 return mapped_file_;
224}
225
[email protected]176aa482008-11-14 03:25:15226} // namespace base