Add R_8 GPU memory buffers format.
[chromium-blink-merge.git] / base / memory / shared_memory_win.cc
blob7e0cf0bf23e67f0d13d22e56e0924cecda7456ce
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/memory/shared_memory.h"
7 #include <aclapi.h>
9 #include "base/logging.h"
10 #include "base/memory/scoped_ptr.h"
11 #include "base/rand_util.h"
12 #include "base/strings/stringprintf.h"
13 #include "base/strings/utf_string_conversions.h"
15 namespace {
17 // Returns the length of the memory section starting at the supplied address.
18 size_t GetMemorySectionSize(void* address) {
19 MEMORY_BASIC_INFORMATION memory_info;
20 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info)))
21 return 0;
22 return memory_info.RegionSize - (static_cast<char*>(address) -
23 static_cast<char*>(memory_info.AllocationBase));
26 } // namespace.
28 namespace base {
30 SharedMemory::SharedMemory()
31 : mapped_file_(NULL),
32 memory_(NULL),
33 read_only_(false),
34 mapped_size_(0),
35 requested_size_(0),
36 lock_(NULL) {
39 SharedMemory::SharedMemory(const std::wstring& name)
40 : mapped_file_(NULL),
41 memory_(NULL),
42 read_only_(false),
43 requested_size_(0),
44 mapped_size_(0),
45 lock_(NULL),
46 name_(name) {
49 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
50 : mapped_file_(handle),
51 memory_(NULL),
52 read_only_(read_only),
53 requested_size_(0),
54 mapped_size_(0),
55 lock_(NULL) {
58 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
59 ProcessHandle process)
60 : mapped_file_(NULL),
61 memory_(NULL),
62 read_only_(read_only),
63 requested_size_(0),
64 mapped_size_(0),
65 lock_(NULL) {
66 ::DuplicateHandle(process, handle,
67 GetCurrentProcess(), &mapped_file_,
68 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
69 FILE_MAP_WRITE,
70 FALSE, 0);
73 SharedMemory::~SharedMemory() {
74 Unmap();
75 Close();
76 if (lock_ != NULL)
77 CloseHandle(lock_);
80 // static
81 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
82 return handle != NULL;
85 // static
86 SharedMemoryHandle SharedMemory::NULLHandle() {
87 return NULL;
90 // static
91 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
92 DCHECK(handle != NULL);
93 ::CloseHandle(handle);
96 // static
97 size_t SharedMemory::GetHandleLimit() {
98 // Rounded down from value reported here:
99 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx
100 return static_cast<size_t>(1 << 23);
103 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
104 return CreateAnonymous(size) && Map(size);
107 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
108 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here,
109 // wasting 32k per mapping on average.
110 static const size_t kSectionMask = 65536 - 1;
111 DCHECK(!options.executable);
112 DCHECK(!mapped_file_);
113 if (options.size == 0)
114 return false;
116 // Check maximum accounting for overflow.
117 if (options.size >
118 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask)
119 return false;
121 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask;
122 name_ = options.name_deprecated ?
123 ASCIIToUTF16(*options.name_deprecated) : L"";
124 SECURITY_ATTRIBUTES sa = { sizeof(sa), NULL, FALSE };
125 SECURITY_DESCRIPTOR sd;
126 ACL dacl;
128 if (options.share_read_only && name_.empty()) {
129 // Add an empty DACL to enforce anonymous read-only sections.
130 sa.lpSecurityDescriptor = &sd;
131 if (!InitializeAcl(&dacl, sizeof(dacl), ACL_REVISION))
132 return false;
133 if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
134 return false;
135 if (!SetSecurityDescriptorDacl(&sd, TRUE, &dacl, FALSE))
136 return false;
138 // Windows ignores DACLs on certain unnamed objects (like shared sections).
139 // So, we generate a random name when we need to enforce read-only.
140 uint64_t rand_values[4];
141 RandBytes(&rand_values, sizeof(rand_values));
142 name_ = StringPrintf(L"CrSharedMem_%016x%016x%016x%016x",
143 rand_values[0], rand_values[1],
144 rand_values[2], rand_values[3]);
146 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, &sa,
147 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
148 name_.empty() ? nullptr : name_.c_str());
149 if (!mapped_file_)
150 return false;
152 requested_size_ = options.size;
154 // Check if the shared memory pre-exists.
155 if (GetLastError() == ERROR_ALREADY_EXISTS) {
156 // If the file already existed, set requested_size_ to 0 to show that
157 // we don't know the size.
158 requested_size_ = 0;
159 if (!options.open_existing_deprecated) {
160 Close();
161 return false;
165 return true;
168 bool SharedMemory::Delete(const std::string& name) {
169 // intentionally empty -- there is nothing for us to do on Windows.
170 return true;
173 bool SharedMemory::Open(const std::string& name, bool read_only) {
174 DCHECK(!mapped_file_);
176 name_ = ASCIIToUTF16(name);
177 read_only_ = read_only;
178 mapped_file_ = OpenFileMapping(
179 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE,
180 false, name_.empty() ? NULL : name_.c_str());
181 if (mapped_file_ != NULL) {
182 // Note: size_ is not set in this case.
183 return true;
185 return false;
188 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
189 if (mapped_file_ == NULL)
190 return false;
192 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
193 return false;
195 if (memory_)
196 return false;
198 memory_ = MapViewOfFile(mapped_file_,
199 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
200 FILE_MAP_WRITE,
201 static_cast<uint64>(offset) >> 32,
202 static_cast<DWORD>(offset),
203 bytes);
204 if (memory_ != NULL) {
205 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
206 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
207 mapped_size_ = GetMemorySectionSize(memory_);
208 return true;
210 return false;
213 bool SharedMemory::Unmap() {
214 if (memory_ == NULL)
215 return false;
217 UnmapViewOfFile(memory_);
218 memory_ = NULL;
219 return true;
222 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
223 SharedMemoryHandle* new_handle,
224 bool close_self,
225 ShareMode share_mode) {
226 *new_handle = 0;
227 DWORD access = FILE_MAP_READ;
228 DWORD options = 0;
229 HANDLE mapped_file = mapped_file_;
230 HANDLE result;
231 if (share_mode == SHARE_CURRENT_MODE && !read_only_)
232 access |= FILE_MAP_WRITE;
233 if (close_self) {
234 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
235 options = DUPLICATE_CLOSE_SOURCE;
236 mapped_file_ = NULL;
237 Unmap();
240 if (process == GetCurrentProcess() && close_self) {
241 *new_handle = mapped_file;
242 return true;
245 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
246 &result, access, FALSE, options))
247 return false;
248 *new_handle = result;
249 return true;
253 void SharedMemory::Close() {
254 if (mapped_file_ != NULL) {
255 CloseHandle(mapped_file_);
256 mapped_file_ = NULL;
260 void SharedMemory::LockDeprecated() {
261 if (lock_ == NULL) {
262 std::wstring name = name_;
263 name.append(L"lock");
264 lock_ = CreateMutex(NULL, FALSE, name.c_str());
265 if (lock_ == NULL) {
266 DPLOG(ERROR) << "Could not create mutex.";
267 NOTREACHED();
268 return; // There is nothing good we can do here.
271 DWORD result = WaitForSingleObject(lock_, INFINITE);
272 DCHECK_EQ(result, WAIT_OBJECT_0);
275 void SharedMemory::UnlockDeprecated() {
276 DCHECK(lock_ != NULL);
277 ReleaseMutex(lock_);
280 SharedMemoryHandle SharedMemory::handle() const {
281 return mapped_file_;
284 } // namespace base