platform_shared_memory_mapper_fuchsia.cc 1.2 KB

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