1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/memory/platform_shared_memory_mapper.h"
6
7 #include "base/allocator/partition_allocator/src/partition_alloc/page_allocator.h"
8 #include "base/logging.h"
9
10 #include <aclapi.h>
11
12 namespace base {
13
14 namespace {
15 // Returns the length of the memory section starting at the supplied address.
GetMemorySectionSize(void * address)16 size_t GetMemorySectionSize(void* address) {
17 MEMORY_BASIC_INFORMATION memory_info;
18 if (!::VirtualQuery(address, &memory_info, sizeof(memory_info)))
19 return 0;
20 return memory_info.RegionSize -
21 static_cast<size_t>(static_cast<char*>(address) -
22 static_cast<char*>(memory_info.AllocationBase));
23 }
24 } // namespace
25
Map(subtle::PlatformSharedMemoryHandle handle,bool write_allowed,uint64_t offset,size_t size)26 absl::optional<span<uint8_t>> PlatformSharedMemoryMapper::Map(
27 subtle::PlatformSharedMemoryHandle handle,
28 bool write_allowed,
29 uint64_t offset,
30 size_t size) {
31 // Try to map the shared memory. On the first failure, release any reserved
32 // address space for a single retry.
33 void* address;
34 for (int i = 0; i < 2; ++i) {
35 address = MapViewOfFile(
36 handle, FILE_MAP_READ | (write_allowed ? FILE_MAP_WRITE : 0),
37 static_cast<DWORD>(offset >> 32), static_cast<DWORD>(offset), size);
38 if (address)
39 break;
40 partition_alloc::ReleaseReservation();
41 }
42 if (!address) {
43 DPLOG(ERROR) << "Failed executing MapViewOfFile";
44 return absl::nullopt;
45 }
46
47 return make_span(reinterpret_cast<uint8_t*>(address),
48 GetMemorySectionSize(address));
49 }
50
Unmap(span<uint8_t> mapping)51 void PlatformSharedMemoryMapper::Unmap(span<uint8_t> mapping) {
52 if (!UnmapViewOfFile(mapping.data()))
53 DPLOG(ERROR) << "UnmapViewOfFile";
54 }
55
56 } // namespace base
57