• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/logging.h"
8 
9 #include <lib/zx/vmar.h>
10 #include "base/fuchsia/fuchsia_logging.h"
11 
12 namespace base {
13 
Map(subtle::PlatformSharedMemoryHandle handle,bool write_allowed,uint64_t offset,size_t size)14 absl::optional<span<uint8_t>> PlatformSharedMemoryMapper::Map(
15     subtle::PlatformSharedMemoryHandle handle,
16     bool write_allowed,
17     uint64_t offset,
18     size_t size) {
19   uintptr_t addr;
20   zx_vm_option_t options = ZX_VM_REQUIRE_NON_RESIZABLE | ZX_VM_PERM_READ;
21   if (write_allowed)
22     options |= ZX_VM_PERM_WRITE;
23   zx_status_t status = zx::vmar::root_self()->map(options, /*vmar_offset=*/0,
24                                                   *handle, offset, size, &addr);
25   if (status != ZX_OK) {
26     ZX_DLOG(ERROR, status) << "zx_vmar_map";
27     return absl::nullopt;
28   }
29 
30   return make_span(reinterpret_cast<uint8_t*>(addr), size);
31 }
32 
Unmap(span<uint8_t> mapping)33 void PlatformSharedMemoryMapper::Unmap(span<uint8_t> mapping) {
34   uintptr_t addr = reinterpret_cast<uintptr_t>(mapping.data());
35   zx_status_t status = zx::vmar::root_self()->unmap(addr, mapping.size());
36   if (status != ZX_OK)
37     ZX_DLOG(ERROR, status) << "zx_vmar_unmap";
38 }
39 
40 }  // namespace base
41