| Index: mojo/edk/embedder/simple_platform_shared_buffer.cc
|
| diff --git a/mojo/edk/embedder/simple_platform_shared_buffer.cc b/mojo/edk/embedder/simple_platform_shared_buffer.cc
|
| index 7467c793a54aae832869c8cce45fec4b6667bf15..caa926c9044ec25f2ad6cdc2feeafd42106866be 100644
|
| --- a/mojo/edk/embedder/simple_platform_shared_buffer.cc
|
| +++ b/mojo/edk/embedder/simple_platform_shared_buffer.cc
|
| @@ -9,11 +9,32 @@
|
| #include <utility>
|
|
|
| #include "base/logging.h"
|
| +#include "base/memory/shared_memory.h"
|
| +#include "base/process/process_handle.h"
|
| +#include "base/sys_info.h"
|
| #include "mojo/edk/embedder/platform_handle_utils.h"
|
|
|
| namespace mojo {
|
| namespace edk {
|
|
|
| +namespace {
|
| +
|
| +// Takes ownership of |memory_handle|.
|
| +ScopedPlatformHandle SharedMemoryToPlatformHandle(
|
| + base::SharedMemoryHandle memory_handle) {
|
| +#if defined(OS_POSIX) && !(defined(OS_MACOSX) && !defined(OS_IOS))
|
| + return ScopedPlatformHandle(PlatformHandle(memory_handle.fd));
|
| +#elif defined(OS_WIN)
|
| + return ScopedPlatformHandle(PlatformHandle(memory_handle.GetHandle()));
|
| +#else
|
| + CHECK_EQ(memory_handle.GetType(), base::SharedMemoryHandle::POSIX);
|
| + return ScopedPlatformHandle(PlatformHandle(
|
| + memory_handle.GetFileDescriptor().fd));
|
| +#endif
|
| +}
|
| +
|
| +} // namespace
|
| +
|
| // static
|
| SimplePlatformSharedBuffer* SimplePlatformSharedBuffer::Create(
|
| size_t num_bytes) {
|
| @@ -77,16 +98,46 @@ scoped_ptr<PlatformSharedBufferMapping> SimplePlatformSharedBuffer::MapNoCheck(
|
| size_t offset,
|
| size_t length) {
|
| DCHECK(IsValidMap(offset, length));
|
| - return MapImpl(offset, length);
|
| + DCHECK(shared_memory_);
|
| + base::SharedMemoryHandle handle;
|
| + {
|
| + base::AutoLock locker(lock_);
|
| + handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
|
| + }
|
| + if (handle == base::SharedMemory::NULLHandle())
|
| + return nullptr;
|
| +
|
| + scoped_ptr<SimplePlatformSharedBufferMapping> mapping(
|
| + new SimplePlatformSharedBufferMapping(handle, offset, length));
|
| + if (mapping->Map())
|
| + return make_scoped_ptr(mapping.release());
|
| +
|
| + return nullptr;
|
| }
|
|
|
| ScopedPlatformHandle SimplePlatformSharedBuffer::DuplicatePlatformHandle() {
|
| - return mojo::edk::DuplicatePlatformHandle(handle_.get());
|
| + DCHECK(shared_memory_);
|
| + base::SharedMemoryHandle handle;
|
| + {
|
| + base::AutoLock locker(lock_);
|
| + handle = base::SharedMemory::DuplicateHandle(shared_memory_->handle());
|
| + }
|
| + if (handle == base::SharedMemory::NULLHandle())
|
| + return ScopedPlatformHandle();
|
| +
|
| + return SharedMemoryToPlatformHandle(handle);
|
| }
|
|
|
| ScopedPlatformHandle SimplePlatformSharedBuffer::PassPlatformHandle() {
|
| DCHECK(HasOneRef());
|
| - return std::move(handle_);
|
| +
|
| + // The only way to pass a handle from base::SharedMemory is to duplicate it
|
| + // and close the original.
|
| + ScopedPlatformHandle handle = DuplicatePlatformHandle();
|
| +
|
| + base::AutoLock locker(lock_);
|
| + shared_memory_->Close();
|
| + return handle;
|
| }
|
|
|
| SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes)
|
| @@ -96,6 +147,35 @@ SimplePlatformSharedBuffer::SimplePlatformSharedBuffer(size_t num_bytes)
|
| SimplePlatformSharedBuffer::~SimplePlatformSharedBuffer() {
|
| }
|
|
|
| +bool SimplePlatformSharedBuffer::Init() {
|
| + DCHECK(!shared_memory_);
|
| +
|
| + base::SharedMemoryCreateOptions options;
|
| + options.size = num_bytes_;
|
| +#if defined(OS_MACOSX) && !defined(OS_IOS)
|
| + // TODO(crbug.com/582468): Support Mach shared memory.
|
| + options.type = base::SharedMemoryHandle::POSIX;
|
| +#endif
|
| +
|
| + shared_memory_.reset(new base::SharedMemory);
|
| + return shared_memory_->Create(options);
|
| +}
|
| +
|
| +bool SimplePlatformSharedBuffer::InitFromPlatformHandle(
|
| + ScopedPlatformHandle platform_handle) {
|
| + DCHECK(!shared_memory_);
|
| +
|
| +#if defined(OS_WIN)
|
| + base::SharedMemoryHandle handle(platform_handle.release().handle,
|
| + base::GetCurrentProcId());
|
| +#else
|
| + base::SharedMemoryHandle handle(platform_handle.release().handle, false);
|
| +#endif
|
| +
|
| + shared_memory_.reset(new base::SharedMemory(handle, false));
|
| + return true;
|
| +}
|
| +
|
| SimplePlatformSharedBufferMapping::~SimplePlatformSharedBufferMapping() {
|
| Unmap();
|
| }
|
| @@ -108,5 +188,24 @@ size_t SimplePlatformSharedBufferMapping::GetLength() const {
|
| return length_;
|
| }
|
|
|
| +bool SimplePlatformSharedBufferMapping::Map() {
|
| + // Mojo shared buffers can be mapped at any offset. However,
|
| + // base::SharedMemory must be mapped at a page boundary. So calculate what the
|
| + // nearest whole page offset is, and build a mapping that's offset from that.
|
| + size_t offset_rounding = offset_ % base::SysInfo::VMAllocationGranularity();
|
| + size_t real_offset = offset_ - offset_rounding;
|
| + size_t real_length = length_ + offset_rounding;
|
| +
|
| + if (!shared_memory_.MapAt(static_cast<off_t>(real_offset), real_length))
|
| + return false;
|
| +
|
| + base_ = static_cast<char*>(shared_memory_.memory()) + offset_rounding;
|
| + return true;
|
| +}
|
| +
|
| +void SimplePlatformSharedBufferMapping::Unmap() {
|
| + shared_memory_.Unmap();
|
| +}
|
| +
|
| } // namespace edk
|
| } // namespace mojo
|
|
|