platform_shared_memory_mapper_posix.cc 1014 B

123456789101112131415161718192021222324252627282930313233343536
  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 "base/numerics/safe_conversions.h"
  7. #include <sys/mman.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. void* address =
  15. mmap(nullptr, size, PROT_READ | (write_allowed ? PROT_WRITE : 0),
  16. MAP_SHARED, handle.fd, checked_cast<off_t>(offset));
  17. if (address == MAP_FAILED) {
  18. DPLOG(ERROR) << "mmap " << handle.fd << " failed";
  19. return absl::nullopt;
  20. }
  21. return make_span(reinterpret_cast<uint8_t*>(address), size);
  22. }
  23. void PlatformSharedMemoryMapper::Unmap(span<uint8_t> mapping) {
  24. if (munmap(mapping.data(), mapping.size()) < 0)
  25. DPLOG(ERROR) << "munmap";
  26. }
  27. } // namespace base