• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2018 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 
5 #include "base/memory/shared_memory_mapping.h"
6 
7 #include <utility>
8 
9 #include "base/logging.h"
10 // Unsupported in libchrome
11 // #include "base/memory/shared_memory_tracker.h"
12 #include "base/unguessable_token.h"
13 #include "build/build_config.h"
14 
15 #if defined(OS_POSIX)
16 #include <sys/mman.h>
17 #endif
18 
19 #if defined(OS_WIN)
20 #include <aclapi.h>
21 #endif
22 
23 #if defined(OS_MACOSX) && !defined(OS_IOS)
24 #include <mach/mach_vm.h>
25 #include "base/mac/mach_logging.h"
26 #endif
27 
28 #if defined(OS_FUCHSIA)
29 #include <zircon/process.h>
30 #include <zircon/status.h>
31 #include <zircon/syscalls.h>
32 #endif
33 
34 namespace base {
35 
36 SharedMemoryMapping::SharedMemoryMapping() = default;
37 
SharedMemoryMapping(SharedMemoryMapping && mapping)38 SharedMemoryMapping::SharedMemoryMapping(SharedMemoryMapping&& mapping)
39     : memory_(mapping.memory_),
40       size_(mapping.size_),
41       mapped_size_(mapping.mapped_size_),
42       guid_(mapping.guid_) {
43   mapping.memory_ = nullptr;
44 }
45 
operator =(SharedMemoryMapping && mapping)46 SharedMemoryMapping& SharedMemoryMapping::operator=(
47     SharedMemoryMapping&& mapping) {
48   Unmap();
49   memory_ = mapping.memory_;
50   size_ = mapping.size_;
51   mapped_size_ = mapping.mapped_size_;
52   guid_ = mapping.guid_;
53   mapping.memory_ = nullptr;
54   return *this;
55 }
56 
~SharedMemoryMapping()57 SharedMemoryMapping::~SharedMemoryMapping() {
58   Unmap();
59 }
60 
SharedMemoryMapping(void * memory,size_t size,size_t mapped_size,const UnguessableToken & guid)61 SharedMemoryMapping::SharedMemoryMapping(void* memory,
62                                          size_t size,
63                                          size_t mapped_size,
64                                          const UnguessableToken& guid)
65     : memory_(memory), size_(size), mapped_size_(mapped_size), guid_(guid) {
66   // Unsupported in libchrome.
67   // SharedMemoryTracker::GetInstance()->IncrementMemoryUsage(*this);
68 }
69 
Unmap()70 void SharedMemoryMapping::Unmap() {
71   if (!IsValid())
72     return;
73   // Unsupported in libchrome.
74   // SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
75 #if defined(OS_WIN)
76   if (!UnmapViewOfFile(memory_))
77     DPLOG(ERROR) << "UnmapViewOfFile";
78 #elif defined(OS_FUCHSIA)
79   uintptr_t addr = reinterpret_cast<uintptr_t>(memory_);
80   zx_status_t status = zx_vmar_unmap(zx_vmar_root_self(), addr, size_);
81   DLOG_IF(ERROR, status != ZX_OK)
82       << "zx_vmar_unmap failed: " << zx_status_get_string(status);
83 #elif defined(OS_MACOSX) && !defined(OS_IOS)
84   kern_return_t kr = mach_vm_deallocate(
85       mach_task_self(), reinterpret_cast<mach_vm_address_t>(memory_), size_);
86   MACH_DLOG_IF(ERROR, kr != KERN_SUCCESS, kr) << "mach_vm_deallocate";
87 #else
88   if (munmap(memory_, size_) < 0)
89     DPLOG(ERROR) << "munmap";
90 #endif
91 }
92 
93 ReadOnlySharedMemoryMapping::ReadOnlySharedMemoryMapping() = default;
94 ReadOnlySharedMemoryMapping::ReadOnlySharedMemoryMapping(
95     ReadOnlySharedMemoryMapping&&) = default;
96 ReadOnlySharedMemoryMapping& ReadOnlySharedMemoryMapping::operator=(
97     ReadOnlySharedMemoryMapping&&) = default;
ReadOnlySharedMemoryMapping(void * address,size_t size,size_t mapped_size,const UnguessableToken & guid)98 ReadOnlySharedMemoryMapping::ReadOnlySharedMemoryMapping(
99     void* address,
100     size_t size,
101     size_t mapped_size,
102     const UnguessableToken& guid)
103     : SharedMemoryMapping(address, size, mapped_size, guid) {}
104 
105 WritableSharedMemoryMapping::WritableSharedMemoryMapping() = default;
106 WritableSharedMemoryMapping::WritableSharedMemoryMapping(
107     WritableSharedMemoryMapping&&) = default;
108 WritableSharedMemoryMapping& WritableSharedMemoryMapping::operator=(
109     WritableSharedMemoryMapping&&) = default;
WritableSharedMemoryMapping(void * address,size_t size,size_t mapped_size,const UnguessableToken & guid)110 WritableSharedMemoryMapping::WritableSharedMemoryMapping(
111     void* address,
112     size_t size,
113     size_t mapped_size,
114     const UnguessableToken& guid)
115     : SharedMemoryMapping(address, size, mapped_size, guid) {}
116 
117 }  // namespace base
118