Fix style nits in gdata directory.
[chromium-blink-merge.git] / base / shared_memory_win.cc
blob042eb8bdac065c5b1d1911faeee66c5a1e2f1569
1 // Copyright (c) 2011 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.
5 #include "base/shared_memory.h"
7 #include "base/logging.h"
8 #include "base/utf_string_conversions.h"
10 namespace base {
12 SharedMemory::SharedMemory()
13 : mapped_file_(NULL),
14 memory_(NULL),
15 read_only_(false),
16 created_size_(0),
17 lock_(NULL) {
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) {
29 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
30 : mapped_file_(handle),
31 memory_(NULL),
32 read_only_(read_only),
33 created_size_(0),
34 lock_(NULL) {
37 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
38 ProcessHandle process)
39 : mapped_file_(NULL),
40 memory_(NULL),
41 read_only_(read_only),
42 created_size_(0),
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);
51 SharedMemory::~SharedMemory() {
52 Close();
53 if (lock_ != NULL)
54 CloseHandle(lock_);
57 // static
58 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
59 return handle != NULL;
62 // static
63 SharedMemoryHandle SharedMemory::NULLHandle() {
64 return NULL;
67 // static
68 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
69 DCHECK(handle != NULL);
70 ::CloseHandle(handle);
73 bool SharedMemory::CreateAndMapAnonymous(uint32 size) {
74 return CreateAnonymous(size) && Map(size);
77 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
78 DCHECK(!options.executable);
79 DCHECK(!mapped_file_);
80 if (options.size == 0)
81 return false;
83 // 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.
88 uint32 rounded_size = (options.size + 0xffff) & ~0xffff;
89 name_ = ASCIIToWide(options.name == NULL ? "" : *options.name);
90 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
91 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
92 name_.empty() ? NULL : name_.c_str());
93 if (!mapped_file_)
94 return false;
96 created_size_ = options.size;
98 // Check if the shared memory pre-exists.
99 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;
103 if (!options.open_existing) {
104 Close();
105 return false;
109 return true;
112 bool SharedMemory::Delete(const std::string& name) {
113 // intentionally empty -- there is nothing for us to do on Windows.
114 return true;
117 bool SharedMemory::Open(const std::string& name, bool read_only) {
118 DCHECK(!mapped_file_);
120 name_ = ASCIIToWide(name);
121 read_only_ = read_only;
122 mapped_file_ = OpenFileMapping(
123 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, false,
124 name_.empty() ? NULL : name_.c_str());
125 if (mapped_file_ != NULL) {
126 // Note: size_ is not set in this case.
127 return true;
129 return false;
132 bool SharedMemory::Map(uint32 bytes) {
133 if (mapped_file_ == NULL)
134 return false;
136 memory_ = MapViewOfFile(mapped_file_,
137 read_only_ ? FILE_MAP_READ : FILE_MAP_ALL_ACCESS, 0, 0, bytes);
138 if (memory_ != NULL) {
139 return true;
141 return false;
144 bool SharedMemory::Unmap() {
145 if (memory_ == NULL)
146 return false;
148 UnmapViewOfFile(memory_);
149 memory_ = NULL;
150 return true;
153 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
154 SharedMemoryHandle *new_handle,
155 bool close_self) {
156 *new_handle = 0;
157 DWORD access = STANDARD_RIGHTS_REQUIRED | FILE_MAP_READ;
158 DWORD options = 0;
159 HANDLE mapped_file = mapped_file_;
160 HANDLE result;
161 if (!read_only_)
162 access |= FILE_MAP_WRITE;
163 if (close_self) {
164 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
165 options = DUPLICATE_CLOSE_SOURCE;
166 mapped_file_ = NULL;
167 Unmap();
170 if (process == GetCurrentProcess() && close_self) {
171 *new_handle = mapped_file;
172 return true;
175 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
176 &result, access, FALSE, options))
177 return false;
178 *new_handle = result;
179 return true;
183 void SharedMemory::Close() {
184 if (memory_ != NULL) {
185 UnmapViewOfFile(memory_);
186 memory_ = NULL;
189 if (mapped_file_ != NULL) {
190 CloseHandle(mapped_file_);
191 mapped_file_ = NULL;
195 void SharedMemory::Lock() {
196 Lock(INFINITE, NULL);
199 bool SharedMemory::Lock(uint32 timeout_ms, SECURITY_ATTRIBUTES* sec_attr) {
200 if (lock_ == NULL) {
201 std::wstring name = name_;
202 name.append(L"lock");
203 lock_ = CreateMutex(sec_attr, FALSE, name.c_str());
204 if (lock_ == NULL) {
205 DPLOG(ERROR) << "Could not create mutex.";
206 return false; // there is nothing good we can do here.
209 DWORD result = WaitForSingleObject(lock_, timeout_ms);
211 // Return false for WAIT_ABANDONED, WAIT_TIMEOUT or WAIT_FAILED.
212 return (result == WAIT_OBJECT_0);
215 void SharedMemory::Unlock() {
216 DCHECK(lock_ != NULL);
217 ReleaseMutex(lock_);
220 SharedMemoryHandle SharedMemory::handle() const {
221 return mapped_file_;
224 } // namespace base