1 // Copyright 2016 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "mojo/public/cpp/system/buffer.h" 6 7 namespace mojo { 8 9 // static 10 ScopedSharedBufferHandle SharedBufferHandle::Create(uint64_t num_bytes) { 11 MojoCreateSharedBufferOptions options = {sizeof(options), 12 MOJO_CREATE_SHARED_BUFFER_FLAG_NONE}; 13 SharedBufferHandle handle; 14 MojoCreateSharedBuffer(num_bytes, &options, handle.mutable_value()); 15 return MakeScopedHandle(handle); 16 } 17 18 ScopedSharedBufferHandle SharedBufferHandle::Clone( 19 SharedBufferHandle::AccessMode access_mode) const { 20 ScopedSharedBufferHandle result; 21 if (!is_valid()) 22 return result; 23 24 MojoDuplicateBufferHandleOptions options = { 25 sizeof(options), MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_NONE}; 26 if (access_mode == AccessMode::READ_ONLY) 27 options.flags |= MOJO_DUPLICATE_BUFFER_HANDLE_FLAG_READ_ONLY; 28 SharedBufferHandle result_handle; 29 MojoDuplicateBufferHandle(value(), &options, result_handle.mutable_value()); 30 result.reset(result_handle); 31 return result; 32 } 33 34 ScopedSharedBufferMapping SharedBufferHandle::Map(uint64_t size) const { 35 return MapAtOffset(size, 0); 36 } 37 38 ScopedSharedBufferMapping SharedBufferHandle::MapAtOffset( 39 uint64_t size, 40 uint64_t offset) const { 41 void* buffer = nullptr; 42 MojoMapBuffer(value(), offset, size, nullptr, &buffer); 43 return ScopedSharedBufferMapping(buffer); 44 } 45 46 uint64_t SharedBufferHandle::GetSize() const { 47 MojoSharedBufferInfo buffer_info; 48 buffer_info.struct_size = sizeof(buffer_info); 49 return MojoGetBufferInfo(value(), nullptr, &buffer_info) == MOJO_RESULT_OK 50 ? buffer_info.size 51 : 0; 52 } 53 54 } // namespace mojo 55