Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(177)

Side by Side Diff: base/memory/shared_memory_handle_mac.cc

Issue 2555483002: Add POSIX shared memory support for Mac (Closed)
Patch Set: Restructure CreateAnonymousSharedMemory Created 4 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/memory/shared_memory_handle.h" 5 #include "base/memory/shared_memory_handle.h"
6 6
7 #include <mach/mach_vm.h> 7 #include <mach/mach_vm.h>
8 #include <stddef.h> 8 #include <stddef.h>
9 #include <sys/mman.h> 9 #include <sys/mman.h>
10 #include <unistd.h> 10 #include <unistd.h>
11 11
12 #include "base/mac/mac_util.h" 12 #include "base/mac/mac_util.h"
13 #include "base/mac/mach_logging.h"
13 #include "base/posix/eintr_wrapper.h" 14 #include "base/posix/eintr_wrapper.h"
14 15
15 namespace base { 16 namespace base {
16 17
17 SharedMemoryHandle::SharedMemoryHandle() {} 18 SharedMemoryHandle::SharedMemoryHandle()
19 : type_(MACH), memory_object_(MACH_PORT_NULL) {}
20
21 SharedMemoryHandle::SharedMemoryHandle(
22 const base::FileDescriptor& file_descriptor)
23 : type_(POSIX), file_descriptor_(file_descriptor) {}
18 24
19 SharedMemoryHandle::SharedMemoryHandle(mach_vm_size_t size) { 25 SharedMemoryHandle::SharedMemoryHandle(mach_vm_size_t size) {
26 type_ = MACH;
20 mach_port_t named_right; 27 mach_port_t named_right;
21 kern_return_t kr = mach_make_memory_entry_64( 28 kern_return_t kr = mach_make_memory_entry_64(
22 mach_task_self(), 29 mach_task_self(),
23 &size, 30 &size,
24 0, // Address. 31 0, // Address.
25 MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE, 32 MAP_MEM_NAMED_CREATE | VM_PROT_READ | VM_PROT_WRITE,
26 &named_right, 33 &named_right,
27 MACH_PORT_NULL); // Parent handle. 34 MACH_PORT_NULL); // Parent handle.
28 if (kr != KERN_SUCCESS) { 35 if (kr != KERN_SUCCESS) {
29 memory_object_ = MACH_PORT_NULL; 36 memory_object_ = MACH_PORT_NULL;
30 return; 37 return;
31 } 38 }
32 39
33 memory_object_ = named_right; 40 memory_object_ = named_right;
34 size_ = size; 41 size_ = size;
35 pid_ = GetCurrentProcId(); 42 pid_ = GetCurrentProcId();
36 ownership_passes_to_ipc_ = false; 43 ownership_passes_to_ipc_ = false;
37 } 44 }
38 45
39 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, 46 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object,
40 mach_vm_size_t size, 47 mach_vm_size_t size,
41 base::ProcessId pid) 48 base::ProcessId pid)
42 : memory_object_(memory_object), 49 : type_(MACH),
50 memory_object_(memory_object),
43 size_(size), 51 size_(size),
44 pid_(pid), 52 pid_(pid),
45 ownership_passes_to_ipc_(false) {} 53 ownership_passes_to_ipc_(false) {}
46 54
47 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) { 55 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) {
48 CopyRelevantData(handle); 56 CopyRelevantData(handle);
49 } 57 }
50 58
51 SharedMemoryHandle& SharedMemoryHandle::operator=( 59 SharedMemoryHandle& SharedMemoryHandle::operator=(
52 const SharedMemoryHandle& handle) { 60 const SharedMemoryHandle& handle) {
53 if (this == &handle) 61 if (this == &handle)
54 return *this; 62 return *this;
55 63
64 type_ = handle.type_;
56 CopyRelevantData(handle); 65 CopyRelevantData(handle);
57 return *this; 66 return *this;
58 } 67 }
59 68
60 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { 69 SharedMemoryHandle SharedMemoryHandle::Duplicate() const {
61 if (!IsValid()) 70 if (!IsValid())
62 return SharedMemoryHandle(MACH_PORT_NULL, 0, 0); 71 return SharedMemoryHandle(MACH_PORT_NULL, 0, 0);
63 72
64 // Increment the ref count. 73 // Increment the ref count.
65 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, 74 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_,
66 MACH_PORT_RIGHT_SEND, 1); 75 MACH_PORT_RIGHT_SEND, 1);
67 DCHECK_EQ(kr, KERN_SUCCESS); 76 DCHECK_EQ(kr, KERN_SUCCESS);
68 SharedMemoryHandle handle(*this); 77 SharedMemoryHandle handle(*this);
69 handle.SetOwnershipPassesToIPC(true); 78 handle.SetOwnershipPassesToIPC(true);
70 return handle; 79 return handle;
71 } 80 }
72 81
73 bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const { 82 bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const {
74 if (!IsValid() && !handle.IsValid()) 83 if (!IsValid() && !handle.IsValid())
75 return true; 84 return true;
76 85
77 return memory_object_ == handle.memory_object_ && size_ == handle.size_ && 86 if (type_ != handle.type_)
78 pid_ == handle.pid_; 87 return false;
88
89 switch (type_) {
90 case POSIX:
91 return file_descriptor_.fd == handle.file_descriptor_.fd;
92 case MACH:
93 return memory_object_ == handle.memory_object_ && size_ == handle.size_ &&
94 pid_ == handle.pid_;
95 }
79 } 96 }
80 97
81 bool SharedMemoryHandle::operator!=(const SharedMemoryHandle& handle) const { 98 bool SharedMemoryHandle::operator!=(const SharedMemoryHandle& handle) const {
82 return !(*this == handle); 99 return !(*this == handle);
83 } 100 }
84 101
85 bool SharedMemoryHandle::IsValid() const { 102 bool SharedMemoryHandle::IsValid() const {
86 return memory_object_ != MACH_PORT_NULL; 103 switch (type_) {
104 case POSIX:
105 return file_descriptor_.fd >= 0;
106 case MACH:
107 return memory_object_ != MACH_PORT_NULL;
108 }
87 } 109 }
88 110
89 mach_port_t SharedMemoryHandle::GetMemoryObject() const { 111 mach_port_t SharedMemoryHandle::GetMemoryObject() const {
112 DCHECK_EQ(type_, MACH);
90 return memory_object_; 113 return memory_object_;
91 } 114 }
92 115
93 bool SharedMemoryHandle::GetSize(size_t* size) const { 116 bool SharedMemoryHandle::GetSize(size_t* size) const {
94 if (!IsValid()) { 117 if (!IsValid()) {
95 *size = 0; 118 *size = 0;
96 return true; 119 return true;
97 } 120 }
98 121
99 *size = size_; 122 switch (type_) {
100 return true; 123 case SharedMemoryHandle::POSIX:
124 struct stat st;
125 if (fstat(file_descriptor_.fd, &st) != 0)
126 return false;
127 if (st.st_size < 0)
128 return false;
129 *size = st.st_size;
130 return true;
131 case SharedMemoryHandle::MACH:
132 *size = size_;
133 return true;
134 }
101 } 135 }
102 136
103 bool SharedMemoryHandle::MapAt(off_t offset, 137 bool SharedMemoryHandle::MapAt(off_t offset,
104 size_t bytes, 138 size_t bytes,
105 void** memory, 139 void** memory,
106 bool read_only) { 140 bool read_only) {
107 DCHECK(IsValid()); 141 DCHECK(IsValid());
108 DCHECK_EQ(pid_, GetCurrentProcId()); 142 switch (type_) {
109 kern_return_t kr = mach_vm_map( 143 case SharedMemoryHandle::POSIX:
110 mach_task_self(), 144 *memory = mmap(nullptr, bytes, PROT_READ | (read_only ? 0 : PROT_WRITE),
111 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter 145 MAP_SHARED, file_descriptor_.fd, offset);
112 bytes, 146 return *memory != MAP_FAILED;
113 0, // Alignment mask 147 case SharedMemoryHandle::MACH:
114 VM_FLAGS_ANYWHERE, memory_object_, offset, 148 DCHECK_EQ(pid_, GetCurrentProcId());
115 FALSE, // Copy 149 kern_return_t kr = mach_vm_map(
116 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection 150 mach_task_self(),
117 VM_PROT_WRITE | VM_PROT_READ | VM_PROT_IS_MASK, // Maximum protection 151 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter
118 VM_INHERIT_NONE); 152 bytes,
119 return kr == KERN_SUCCESS; 153 0, // Alignment mask
154 VM_FLAGS_ANYWHERE,
155 memory_object_,
156 offset,
157 FALSE, // Copy
158 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection
159 VM_PROT_WRITE | VM_PROT_READ | VM_PROT_IS_MASK, // Maximum protection
160 VM_INHERIT_NONE);
161 return kr == KERN_SUCCESS;
162 }
120 } 163 }
121 164
122 void SharedMemoryHandle::Close() const { 165 void SharedMemoryHandle::Close() const {
123 if (!IsValid()) 166 if (!IsValid())
124 return; 167 return;
125 168
126 kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_); 169 switch (type_) {
127 if (kr != KERN_SUCCESS) 170 case POSIX:
128 DPLOG(ERROR) << "Error deallocating mach port: " << kr; 171 if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0)
172 DPLOG(ERROR) << "Error closing fd";
173 break;
174 case MACH:
175 kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_);
176 if (kr != KERN_SUCCESS)
177 MACH_DLOG(ERROR, kr) << "Error deallocating mach port";
178 break;
179 }
129 } 180 }
130 181
131 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) { 182 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) {
183 DCHECK_EQ(type_, MACH);
132 ownership_passes_to_ipc_ = ownership_passes; 184 ownership_passes_to_ipc_ = ownership_passes;
133 } 185 }
134 186
135 bool SharedMemoryHandle::OwnershipPassesToIPC() const { 187 bool SharedMemoryHandle::OwnershipPassesToIPC() const {
188 DCHECK_EQ(type_, MACH);
136 return ownership_passes_to_ipc_; 189 return ownership_passes_to_ipc_;
137 } 190 }
138 191
139 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { 192 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) {
140 memory_object_ = handle.memory_object_; 193 type_ = handle.type_;
141 size_ = handle.size_; 194 switch (type_) {
142 pid_ = handle.pid_; 195 case POSIX:
143 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; 196 file_descriptor_ = handle.file_descriptor_;
197 break;
198 case MACH:
199 memory_object_ = handle.memory_object_;
200 size_ = handle.size_;
201 pid_ = handle.pid_;
202 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_;
203 break;
204 }
144 } 205 }
145 206
146 } // namespace base 207 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698