| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "mojo/edk/embedder/simple_platform_shared_buffer.h" | 5 #include "mojo/edk/embedder/simple_platform_shared_buffer.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 | 8 |
| 9 #include <utility> | 9 #include <utility> |
| 10 | 10 |
| 11 #include "base/logging.h" | 11 #include "base/logging.h" |
| 12 #include "base/memory/shared_memory.h" |
| 13 #include "base/process/process_handle.h" |
| 14 #include "base/sys_info.h" |
| 12 #include "mojo/edk/embedder/platform_handle_utils.h" | 15 #include "mojo/edk/embedder/platform_handle_utils.h" |
| 13 | 16 |
| 14 namespace mojo { | 17 namespace mojo { |
| 15 namespace edk { | 18 namespace edk { |
| 16 | 19 |
| 20 namespace { |
| 21 |
| 22 // Takes ownership of |memory_handle|. |
| 23 ScopedPlatformHandle SharedMemoryToPlatformHandle( |
| 24 base::SharedMemoryHandle memory_handle) { |
| 25 #if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS)) |
| 26 return ScopedPlatformHandle(PlatformHandle(memory_handle.fd)); |
| 27 #elif defined(OS_WIN) |
| 28 return ScopedPlatformHandle(PlatformHandle(memory_handle.GetHandle())); |
| 29 #else |
| 30 CHECK_EQ(memory_handle.GetType(), base::SharedMemoryHandle::POSIX); |
| 31 return ScopedPlatformHandle(PlatformHandle( |
| 32 memory_handle.GetFileDescriptor().fd)); |
| 33 #endif |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 17 // static | 38 // static |
| 18 SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create( | 39 SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create( |
| 19 size_t num_bytes) { | 40 size_t num_bytes) { |
| 20 DCHECK_GT(num_bytes, 0u); | 41 DCHECK_GT(num_bytes, 0u); |
| 21 | 42 |
| 22 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes); | 43 SimplePlatformSharedBuffer* rv = new SimplePlatformSharedBuffer(num_bytes); |
| 23 if (!rv->Init()) { | 44 if (!rv->Init()) { |
| 24 // We can't just delete it directly, due to the "in destructor" (debug) | 45 // We can't just delete it directly, due to the "in destructor" (debug) |
| 25 // check. | 46 // check. |
| 26 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv); | 47 scoped_refptr<SimplePlatformSharedBuffer> deleter(rv); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 70 if (length > num_bytes_ - offset) | 91 if (length > num_bytes_ - offset) |
| 71 return false; | 92 return false; |
| 72 | 93 |
| 73 return true; | 94 return true; |
| 74 } | 95 } |
| 75 | 96 |
| 76 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck( | 97 scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck( |
| 77 size_t offset, | 98 size_t offset, |
| 78 size_t length) { | 99 size_t length) { |
| 79 DCHECK(IsValidMap(offset, length)); | 100 DCHECK(IsValidMap(offset, length)); |
| 80 return MapImpl(offset, length); | 101 DCHECK(shared_memory_); |
| 102 base::SharedMemoryHandle handle; |
| 103 { |
| 104 base::AutoLock locker(lock_); |
| 105 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle()); |
| 106 } |
| 107 if (handle == base::SharedMemory::NULLHandle()) |
| 108 return nullptr; |
| 109 |
| 110 scoped_ptr<SimplePlatformSharedBufferMapping> mapping( |
| 111 new SimplePlatformSharedBufferMapping(handle, offset, length)); |
| 112 if (mapping->Map()) |
| 113 return make_scoped_ptr(mapping.release()); |
| 114 |
| 115 return nullptr; |
| 81 } | 116 } |
| 82 | 117 |
| 83 ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() { | 118 ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() { |
| 84 return mojo::edk::DuplicatePlatformHandle(handle_.get()); | 119 DCHECK(shared_memory_); |
| 120 base::SharedMemoryHandle handle; |
| 121 { |
| 122 base::AutoLock locker(lock_); |
| 123 handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle()); |
| 124 } |
| 125 if (handle == base::SharedMemory::NULLHandle()) |
| 126 return ScopedPlatformHandle(); |
| 127 |
| 128 return SharedMemoryToPlatformHandle(handle); |
| 85 } | 129 } |
| 86 | 130 |
| 87 ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() { | 131 ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() { |
| 88 DCHECK(HasOneRef()); | 132 DCHECK(HasOneRef()); |
| 89 return std::move(handle_); | 133 |
| 134 // The only way to pass a handle from base::SharedMemory is to duplicate it |
| 135 // and close the original. |
| 136 ScopedPlatformHandle handle = DuplicatePlatformHandle(); |
| 137 |
| 138 base::AutoLock locker(lock_); |
| 139 shared_memory_->Close(); |
| 140 return handle; |
| 90 } | 141 } |
| 91 | 142 |
| 92 SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes) | 143 SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes) |
| 93 : num_bytes_(num_bytes) { | 144 : num_bytes_(num_bytes) { |
| 94 } | 145 } |
| 95 | 146 |
| 96 SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() { | 147 SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() { |
| 97 } | 148 } |
| 98 | 149 |
| 150 bool SimplePlatformSharedBuffer::Init() { |
| 151 DCHECK(!shared_memory_); |
| 152 |
| 153 base::SharedMemoryCreateOptions options; |
| 154 options.size = num_bytes_; |
| 155 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 156 // TODO(crbug.com/582468): Support Mach shared memory. |
| 157 options.type = base::SharedMemoryHandle::POSIX; |
| 158 #endif |
| 159 |
| 160 shared_memory_.reset(new base::SharedMemory); |
| 161 return shared_memory_->Create(options); |
| 162 } |
| 163 |
| 164 bool SimplePlatformSharedBuffer::InitFromPlatformHandle( |
| 165 ScopedPlatformHandle platform_handle) { |
| 166 DCHECK(!shared_memory_); |
| 167 |
| 168 #if defined(OS_WIN) |
| 169 base::SharedMemoryHandle handle(platform_handle.release().handle, |
| 170 base::GetCurrentProcId()); |
| 171 #else |
| 172 base::SharedMemoryHandle handle(platform_handle.release().handle, false); |
| 173 #endif |
| 174 |
| 175 shared_memory_.reset(new base::SharedMemory(handle, false)); |
| 176 return true; |
| 177 } |
| 178 |
| 99 SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() { | 179 SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() { |
| 100 Unmap(); | 180 Unmap(); |
| 101 } | 181 } |
| 102 | 182 |
| 103 void* SimplePlatformSharedBufferMapping::GetBase() const { | 183 void* SimplePlatformSharedBufferMapping::GetBase() const { |
| 104 return base_; | 184 return base_; |
| 105 } | 185 } |
| 106 | 186 |
| 107 size_t SimplePlatformSharedBufferMapping::GetLength() const { | 187 size_t SimplePlatformSharedBufferMapping::GetLength() const { |
| 108 return length_; | 188 return length_; |
| 109 } | 189 } |
| 110 | 190 |
| 191 bool SimplePlatformSharedBufferMapping::Map() { |
| 192 // Mojo shared buffers can be mapped at any offset. However, |
| 193 // base::SharedMemory must be mapped at a page boundary. So calculate what the |
| 194 // nearest whole page offset is, and build a mapping that's offset from that. |
| 195 size_t offset_rounding = offset_ % base::SysInfo::VMAllocationGranularity(); |
| 196 size_t real_offset = offset_ - offset_rounding; |
| 197 size_t real_length = length_ + offset_rounding; |
| 198 |
| 199 if (!shared_memory_.MapAt(static_cast<off_t>(real_offset), real_length)) |
| 200 return false; |
| 201 |
| 202 base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding; |
| 203 return true; |
| 204 } |
| 205 |
| 206 void SimplePlatformSharedBufferMapping::Unmap() { |
| 207 shared_memory_.Unmap(); |
| 208 } |
| 209 |
| 111 } // namespace edk | 210 } // namespace edk |
| 112 } // namespace mojo | 211 } // namespace mojo |
| OLD | NEW |