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

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

Issue 1418113003: mac: Add auto-close and share-read-only functionality to SharedMemory. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 2 months 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 <sys/mman.h> 8 #include <sys/mman.h>
9 #include <unistd.h> 9 #include <unistd.h>
10 10
(...skipping 26 matching lines...) Expand all
37 &named_right, 37 &named_right,
38 MACH_PORT_NULL); // Parent handle. 38 MACH_PORT_NULL); // Parent handle.
39 if (kr != KERN_SUCCESS) { 39 if (kr != KERN_SUCCESS) {
40 memory_object_ = MACH_PORT_NULL; 40 memory_object_ = MACH_PORT_NULL;
41 return; 41 return;
42 } 42 }
43 43
44 memory_object_ = named_right; 44 memory_object_ = named_right;
45 size_ = size; 45 size_ = size;
46 pid_ = GetCurrentProcId(); 46 pid_ = GetCurrentProcId();
47 ownership_passes_to_ipc_ = false;
47 } 48 }
48 49
49 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object, 50 SharedMemoryHandle::SharedMemoryHandle(mach_port_t memory_object,
50 mach_vm_size_t size, 51 mach_vm_size_t size,
51 base::ProcessId pid) 52 base::ProcessId pid)
52 : type_(MACH), memory_object_(memory_object), size_(size), pid_(pid) {} 53 : type_(MACH),
54 memory_object_(memory_object),
55 size_(size),
56 pid_(pid),
57 ownership_passes_to_ipc_(false) {}
53 58
54 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle) 59 SharedMemoryHandle::SharedMemoryHandle(const SharedMemoryHandle& handle)
55 : type_(handle.type_) { 60 : type_(handle.type_) {
56 CopyRelevantData(handle); 61 CopyRelevantData(handle);
57 } 62 }
58 63
59 SharedMemoryHandle& SharedMemoryHandle::operator=( 64 SharedMemoryHandle& SharedMemoryHandle::operator=(
60 const SharedMemoryHandle& handle) { 65 const SharedMemoryHandle& handle) {
61 if (this == &handle) 66 if (this == &handle)
62 return *this; 67 return *this;
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 kern_return_t kr = mach_vm_map( 180 kern_return_t kr = mach_vm_map(
176 mach_task_self(), 181 mach_task_self(),
177 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter 182 reinterpret_cast<mach_vm_address_t*>(memory), // Output parameter
178 bytes, 183 bytes,
179 0, // Alignment mask 184 0, // Alignment mask
180 VM_FLAGS_ANYWHERE, 185 VM_FLAGS_ANYWHERE,
181 memory_object_, 186 memory_object_,
182 offset, 187 offset,
183 FALSE, // Copy 188 FALSE, // Copy
184 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection 189 VM_PROT_READ | (read_only ? 0 : VM_PROT_WRITE), // Current protection
185 VM_PROT_READ | VM_PROT_WRITE, // Maximum protection 190 VM_PROT_WRITE | VM_PROT_READ | VM_PROT_IS_MASK, // Maximum protection
Mark Mentovai 2015/10/23 14:24:03 This bit wasn’t defined in 10.6. Does its kernel m
erikchen 2015/10/23 17:13:31 mark: 10.6.8 doesn't have VM_PROT_IS_MASK and will
186 VM_INHERIT_NONE); 191 VM_INHERIT_NONE);
187 return kr == KERN_SUCCESS; 192 return kr == KERN_SUCCESS;
188 } 193 }
189 } 194 }
190 195
191 void SharedMemoryHandle::Close() const { 196 void SharedMemoryHandle::Close() const {
192 if (!IsValid()) 197 if (!IsValid())
193 return; 198 return;
194 199
195 switch (type_) { 200 switch (type_) {
196 case POSIX: 201 case POSIX:
197 if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0) 202 if (IGNORE_EINTR(close(file_descriptor_.fd)) < 0)
198 DPLOG(ERROR) << "Error closing fd."; 203 DPLOG(ERROR) << "Error closing fd.";
199 break; 204 break;
200 case MACH: 205 case MACH:
201 kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_); 206 kern_return_t kr = mach_port_deallocate(mach_task_self(), memory_object_);
202 if (kr != KERN_SUCCESS) 207 if (kr != KERN_SUCCESS)
203 DPLOG(ERROR) << "Error deallocating mach port: " << kr; 208 DPLOG(ERROR) << "Error deallocating mach port: " << kr;
204 break; 209 break;
205 } 210 }
206 } 211 }
207 212
213 void SharedMemoryHandle::SetOwnershipPassesToIPC(bool ownership_passes) {
214 DCHECK_EQ(type_, MACH);
215 ownership_passes_to_ipc_ = ownership_passes;
216 }
217
218 bool SharedMemoryHandle::OwnershipPassesToIPC() const {
219 DCHECK_EQ(type_, MACH);
220 return ownership_passes_to_ipc_;
221 }
222
208 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) { 223 void SharedMemoryHandle::CopyRelevantData(const SharedMemoryHandle& handle) {
209 switch (type_) { 224 switch (type_) {
210 case POSIX: 225 case POSIX:
211 file_descriptor_ = handle.file_descriptor_; 226 file_descriptor_ = handle.file_descriptor_;
212 break; 227 break;
213 case MACH: 228 case MACH:
214 memory_object_ = handle.memory_object_; 229 memory_object_ = handle.memory_object_;
215 size_ = handle.size_; 230 size_ = handle.size_;
216 pid_ = handle.pid_; 231 pid_ = handle.pid_;
232 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_;
217 break; 233 break;
218 } 234 }
219 } 235 }
220 236
221 } // namespace base 237 } // namespace base
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698