| OLD | NEW |
| 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> |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 60 const SharedMemoryHandle& handle) { | 60 const SharedMemoryHandle& handle) { |
| 61 if (this == &handle) | 61 if (this == &handle) |
| 62 return *this; | 62 return *this; |
| 63 | 63 |
| 64 type_ = handle.type_; | 64 type_ = handle.type_; |
| 65 CopyRelevantData(handle); | 65 CopyRelevantData(handle); |
| 66 return *this; | 66 return *this; |
| 67 } | 67 } |
| 68 | 68 |
| 69 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { | 69 SharedMemoryHandle SharedMemoryHandle::Duplicate() const { |
| 70 if (!IsValid()) | 70 switch (type_) { |
| 71 return SharedMemoryHandle(MACH_PORT_NULL, 0, 0); | 71 case POSIX: { |
| 72 if (!IsValid()) |
| 73 return SharedMemoryHandle(); |
| 74 int duped_fd = HANDLE_EINTR(dup(file_descriptor_.fd)); |
| 75 if (duped_fd < 0) |
| 76 return SharedMemoryHandle(); |
| 77 return SharedMemoryHandle(FileDescriptor(duped_fd, true)); |
| 78 } |
| 79 case MACH: { |
| 80 if (!IsValid()) |
| 81 return SharedMemoryHandle(MACH_PORT_NULL, 0, 0); |
| 72 | 82 |
| 73 // Increment the ref count. | 83 // Increment the ref count. |
| 74 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, | 84 kern_return_t kr = mach_port_mod_refs(mach_task_self(), memory_object_, |
| 75 MACH_PORT_RIGHT_SEND, 1); | 85 MACH_PORT_RIGHT_SEND, 1); |
| 76 DCHECK_EQ(kr, KERN_SUCCESS); | 86 DCHECK_EQ(kr, KERN_SUCCESS); |
| 77 SharedMemoryHandle handle(*this); | 87 SharedMemoryHandle handle(*this); |
| 78 handle.SetOwnershipPassesToIPC(true); | 88 handle.SetOwnershipPassesToIPC(true); |
| 79 return handle; | 89 return handle; |
| 90 } |
| 91 } |
| 80 } | 92 } |
| 81 | 93 |
| 82 bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const { | 94 bool SharedMemoryHandle::operator==(const SharedMemoryHandle& handle) const { |
| 83 if (!IsValid() && !handle.IsValid()) | 95 if (!IsValid() && !handle.IsValid()) |
| 84 return true; | 96 return true; |
| 85 | 97 |
| 86 if (type_ != handle.type_) | 98 if (type_ != handle.type_) |
| 87 return false; | 99 return false; |
| 88 | 100 |
| 89 switch (type_) { | 101 switch (type_) { |
| (...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 198 case MACH: | 210 case MACH: |
| 199 memory_object_ = handle.memory_object_; | 211 memory_object_ = handle.memory_object_; |
| 200 size_ = handle.size_; | 212 size_ = handle.size_; |
| 201 pid_ = handle.pid_; | 213 pid_ = handle.pid_; |
| 202 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; | 214 ownership_passes_to_ipc_ = handle.ownership_passes_to_ipc_; |
| 203 break; | 215 break; |
| 204 } | 216 } |
| 205 } | 217 } |
| 206 | 218 |
| 207 } // namespace base | 219 } // namespace base |
| OLD | NEW |