Use expanded heuristics for GPU rasterization on future OS versions
[chromium-blink-merge.git] / base / memory / shared_memory_win.cc
blob3c1054fd5d3c5693981eeb979ece4c6999c12b06
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 "base/logging.h"
8 #include "base/rand_util.h"
9 #include "base/strings/stringprintf.h"
10 #include "base/strings/utf_string_conversions.h"
12 namespace {
14 // Returns the length of the memory section starting at the supplied address.
15 size_t GetMemorySectionSize(void* address) {
16 MEMORY_BASIC_INFORMATION memory_info;
17 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info)))
18 return 0;
19 return memory_info.RegionSize - (static_cast<char*>(address) -
20 static_cast<char*>(memory_info.AllocationBase));
23 } // namespace.
25 namespace base {
27 SharedMemory::SharedMemory()
28 : mapped_file_(NULL),
29 memory_(NULL),
30 read_only_(false),
31 mapped_size_(0),
32 requested_size_(0),
33 lock_(NULL) {
36 SharedMemory::SharedMemory(const std::wstring& name)
37 : mapped_file_(NULL),
38 memory_(NULL),
39 read_only_(false),
40 requested_size_(0),
41 mapped_size_(0),
42 lock_(NULL),
43 name_(name) {
46 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
47 : mapped_file_(handle),
48 memory_(NULL),
49 read_only_(read_only),
50 requested_size_(0),
51 mapped_size_(0),
52 lock_(NULL) {
55 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
56 ProcessHandle process)
57 : mapped_file_(NULL),
58 memory_(NULL),
59 read_only_(read_only),
60 requested_size_(0),
61 mapped_size_(0),
62 lock_(NULL) {
63 ::DuplicateHandle(process, handle,
64 GetCurrentProcess(), &mapped_file_,
65 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
66 FILE_MAP_WRITE,
67 FALSE, 0);
70 SharedMemory::~SharedMemory() {
71 Close();
72 if (lock_ != NULL)
73 CloseHandle(lock_);
76 // static
77 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
78 return handle != NULL;
81 // static
82 SharedMemoryHandle SharedMemory::NULLHandle() {
83 return NULL;
86 // static
87 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
88 DCHECK(handle != NULL);
89 ::CloseHandle(handle);
92 // static
93 size_t SharedMemory::GetHandleLimit() {
94 // Rounded down from value reported here:
95 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx
96 return static_cast<size_t>(1 << 23);
99 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
100 return CreateAnonymous(size) && Map(size);
103 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
104 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here,
105 // wasting 32k per mapping on average.
106 static const size_t kSectionMask = 65536 - 1;
107 DCHECK(!options.executable);
108 DCHECK(!mapped_file_);
109 if (options.size == 0)
110 return false;
112 // Check maximum accounting for overflow.
113 if (options.size >
114 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask)
115 return false;
117 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask;
118 name_ = ASCIIToWide(options.name_deprecated == NULL ? "" :
119 *options.name_deprecated);
120 if (options.share_read_only && name_.empty()) {
121 // Windows ignores DACLs on certain unnamed objects (like shared sections).
122 // So, we generate a random name when we need to enforce read-only.
123 uint64_t rand_values[4];
124 base::RandBytes(&rand_values, sizeof(rand_values));
125 name_ = base::StringPrintf(L"CrSharedMem_%016x%016x%016x%016x",
126 rand_values[0], rand_values[1],
127 rand_values[2], rand_values[3]);
129 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
130 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size), name_.c_str());
131 if (!mapped_file_)
132 return false;
134 requested_size_ = options.size;
136 // Check if the shared memory pre-exists.
137 if (GetLastError() == ERROR_ALREADY_EXISTS) {
138 // If the file already existed, set requested_size_ to 0 to show that
139 // we don't know the size.
140 requested_size_ = 0;
141 if (!options.open_existing_deprecated) {
142 Close();
143 return false;
147 return true;
150 bool SharedMemory::Delete(const std::string& name) {
151 // intentionally empty -- there is nothing for us to do on Windows.
152 return true;
155 bool SharedMemory::Open(const std::string& name, bool read_only) {
156 DCHECK(!mapped_file_);
158 name_ = ASCIIToWide(name);
159 read_only_ = read_only;
160 mapped_file_ = OpenFileMapping(
161 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE,
162 false, name_.empty() ? NULL : name_.c_str());
163 if (mapped_file_ != NULL) {
164 // Note: size_ is not set in this case.
165 return true;
167 return false;
170 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
171 if (mapped_file_ == NULL)
172 return false;
174 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
175 return false;
177 if (memory_)
178 return false;
180 memory_ = MapViewOfFile(mapped_file_,
181 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
182 FILE_MAP_WRITE,
183 static_cast<uint64>(offset) >> 32,
184 static_cast<DWORD>(offset),
185 bytes);
186 if (memory_ != NULL) {
187 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
188 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
189 mapped_size_ = GetMemorySectionSize(memory_);
190 return true;
192 return false;
195 bool SharedMemory::Unmap() {
196 if (memory_ == NULL)
197 return false;
199 UnmapViewOfFile(memory_);
200 memory_ = NULL;
201 return true;
204 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
205 SharedMemoryHandle *new_handle,
206 bool close_self,
207 ShareMode share_mode) {
208 *new_handle = 0;
209 DWORD access = FILE_MAP_READ;
210 DWORD options = 0;
211 HANDLE mapped_file = mapped_file_;
212 HANDLE result;
213 if (share_mode == SHARE_CURRENT_MODE && !read_only_)
214 access |= FILE_MAP_WRITE;
215 if (close_self) {
216 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
217 options = DUPLICATE_CLOSE_SOURCE;
218 mapped_file_ = NULL;
219 Unmap();
222 if (process == GetCurrentProcess() && close_self) {
223 *new_handle = mapped_file;
224 return true;
227 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
228 &result, access, FALSE, options))
229 return false;
230 *new_handle = result;
231 return true;
235 void SharedMemory::Close() {
236 if (memory_ != NULL) {
237 UnmapViewOfFile(memory_);
238 memory_ = NULL;
241 if (mapped_file_ != NULL) {
242 CloseHandle(mapped_file_);
243 mapped_file_ = NULL;
247 void SharedMemory::LockDeprecated() {
248 if (lock_ == NULL) {
249 std::wstring name = name_;
250 name.append(L"lock");
251 lock_ = CreateMutex(NULL, FALSE, name.c_str());
252 if (lock_ == NULL) {
253 DPLOG(ERROR) << "Could not create mutex.";
254 NOTREACHED();
255 return; // There is nothing good we can do here.
258 DWORD result = WaitForSingleObject(lock_, INFINITE);
259 DCHECK_EQ(result, WAIT_OBJECT_0);
262 void SharedMemory::UnlockDeprecated() {
263 DCHECK(lock_ != NULL);
264 ReleaseMutex(lock_);
267 SharedMemoryHandle SharedMemory::handle() const {
268 return mapped_file_;
271 } // namespace base