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/strings/utf_string_conversions.h"
12 // Returns the length of the memory section starting at the supplied address.
13 size_t GetMemorySectionSize(void* address) {
14 MEMORY_BASIC_INFORMATION memory_info;
15 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info)))
17 return memory_info.RegionSize - (static_cast<char*>(address) -
18 static_cast<char*>(memory_info.AllocationBase));
25 SharedMemory::SharedMemory()
34 SharedMemory::SharedMemory(const std::wstring& name)
44 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only)
45 : mapped_file_(handle),
47 read_only_(read_only),
53 SharedMemory::SharedMemory(SharedMemoryHandle handle, bool read_only,
54 ProcessHandle process)
57 read_only_(read_only),
61 ::DuplicateHandle(process, handle,
62 GetCurrentProcess(), &mapped_file_,
63 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
68 SharedMemory::~SharedMemory() {
75 bool SharedMemory::IsHandleValid(const SharedMemoryHandle& handle) {
76 return handle != NULL;
80 SharedMemoryHandle SharedMemory::NULLHandle() {
85 void SharedMemory::CloseHandle(const SharedMemoryHandle& handle) {
86 DCHECK(handle != NULL);
87 ::CloseHandle(handle);
91 size_t SharedMemory::GetHandleLimit() {
92 // Rounded down from value reported here:
93 // http://blogs.technet.com/b/markrussinovich/archive/2009/09/29/3283844.aspx
94 return static_cast<size_t>(1 << 23);
97 bool SharedMemory::CreateAndMapAnonymous(size_t size) {
98 return CreateAnonymous(size) && Map(size);
101 bool SharedMemory::Create(const SharedMemoryCreateOptions& options) {
102 // TODO(bsy,sehr): crbug.com/210609 NaCl forces us to round up 64k here,
103 // wasting 32k per mapping on average.
104 static const size_t kSectionMask = 65536 - 1;
105 DCHECK(!options.executable);
106 DCHECK(!mapped_file_);
107 if (options.size == 0)
110 // Check maximum accounting for overflow.
112 static_cast<size_t>(std::numeric_limits<int>::max()) - kSectionMask)
115 size_t rounded_size = (options.size + kSectionMask) & ~kSectionMask;
116 name_ = ASCIIToWide(options.name_deprecated == NULL ? "" :
117 *options.name_deprecated);
118 mapped_file_ = CreateFileMapping(INVALID_HANDLE_VALUE, NULL,
119 PAGE_READWRITE, 0, static_cast<DWORD>(rounded_size),
120 name_.empty() ? NULL : name_.c_str());
124 requested_size_ = options.size;
126 // Check if the shared memory pre-exists.
127 if (GetLastError() == ERROR_ALREADY_EXISTS) {
128 // If the file already existed, set requested_size_ to 0 to show that
129 // we don't know the size.
131 if (!options.open_existing_deprecated) {
140 bool SharedMemory::Delete(const std::string& name) {
141 // intentionally empty -- there is nothing for us to do on Windows.
145 bool SharedMemory::Open(const std::string& name, bool read_only) {
146 DCHECK(!mapped_file_);
148 name_ = ASCIIToWide(name);
149 read_only_ = read_only;
150 mapped_file_ = OpenFileMapping(
151 read_only_ ? FILE_MAP_READ : FILE_MAP_READ | FILE_MAP_WRITE,
152 false, name_.empty() ? NULL : name_.c_str());
153 if (mapped_file_ != NULL) {
154 // Note: size_ is not set in this case.
160 bool SharedMemory::MapAt(off_t offset, size_t bytes) {
161 if (mapped_file_ == NULL)
164 if (bytes > static_cast<size_t>(std::numeric_limits<int>::max()))
170 memory_ = MapViewOfFile(mapped_file_,
171 read_only_ ? FILE_MAP_READ : FILE_MAP_READ |
173 static_cast<uint64>(offset) >> 32,
174 static_cast<DWORD>(offset),
176 if (memory_ != NULL) {
177 DCHECK_EQ(0U, reinterpret_cast<uintptr_t>(memory_) &
178 (SharedMemory::MAP_MINIMUM_ALIGNMENT - 1));
179 mapped_size_ = GetMemorySectionSize(memory_);
185 bool SharedMemory::Unmap() {
189 UnmapViewOfFile(memory_);
194 bool SharedMemory::ShareToProcessCommon(ProcessHandle process,
195 SharedMemoryHandle *new_handle,
197 ShareMode share_mode) {
199 DWORD access = FILE_MAP_READ;
201 HANDLE mapped_file = mapped_file_;
203 if (share_mode == SHARE_CURRENT_MODE && !read_only_)
204 access |= FILE_MAP_WRITE;
206 // DUPLICATE_CLOSE_SOURCE causes DuplicateHandle to close mapped_file.
207 options = DUPLICATE_CLOSE_SOURCE;
212 if (process == GetCurrentProcess() && close_self) {
213 *new_handle = mapped_file;
217 if (!DuplicateHandle(GetCurrentProcess(), mapped_file, process,
218 &result, access, FALSE, options))
220 *new_handle = result;
225 void SharedMemory::Close() {
226 if (memory_ != NULL) {
227 UnmapViewOfFile(memory_);
231 if (mapped_file_ != NULL) {
232 CloseHandle(mapped_file_);
237 void SharedMemory::LockDeprecated() {
239 std::wstring name = name_;
240 name.append(L"lock");
241 lock_ = CreateMutex(NULL, FALSE, name.c_str());
243 DPLOG(ERROR) << "Could not create mutex.";
245 return; // There is nothing good we can do here.
248 DWORD result = WaitForSingleObject(lock_, INFINITE);
249 DCHECK_EQ(result, WAIT_OBJECT_0);
252 void SharedMemory::UnlockDeprecated() {
253 DCHECK(lock_ != NULL);
257 SharedMemoryHandle SharedMemory::handle() const {