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 3 // found in the LICENSE file. 4 5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ 6 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ 7 8 #include <assert.h> 9 10 #include "mojo/public/c/system/buffer.h" 11 #include "mojo/public/cpp/system/handle.h" 12 #include "mojo/public/cpp/system/macros.h" 13 14 namespace mojo { 15 16 // SharedBufferHandle ---------------------------------------------------------- 17 18 class SharedBufferHandle : public Handle { 19 public: 20 SharedBufferHandle() {} 21 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} 22 23 // Copying and assignment allowed. 24 }; 25 26 MOJO_COMPILE_ASSERT(sizeof(SharedBufferHandle) == sizeof(Handle), 27 bad_size_for_cpp_SharedBufferHandle); 28 29 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; 30 MOJO_COMPILE_ASSERT(sizeof(ScopedSharedBufferHandle) == 31 sizeof(SharedBufferHandle), 32 bad_size_for_cpp_ScopedSharedBufferHandle); 33 34 inline MojoResult CreateSharedBuffer( 35 const MojoCreateSharedBufferOptions* options, 36 uint64_t num_bytes, 37 ScopedSharedBufferHandle* shared_buffer) { 38 assert(shared_buffer); 39 SharedBufferHandle handle; 40 MojoResult rv = MojoCreateSharedBuffer(options, num_bytes, 41 handle.mutable_value()); 42 // Reset even on failure (reduces the chances that a "stale"/incorrect handle 43 // will be used). 44 shared_buffer->reset(handle); 45 return rv; 46 } 47 48 // TODO(vtl): This (and also the functions below) are templatized to allow for 49 // future/other buffer types. A bit "safer" would be to overload this function 50 // manually. (The template enforces that the in and out handles to be of the 51 // same type.) 52 template <class BufferHandleType> 53 inline MojoResult DuplicateBuffer( 54 BufferHandleType buffer, 55 const MojoDuplicateBufferHandleOptions* options, 56 ScopedHandleBase<BufferHandleType>* new_buffer) { 57 assert(new_buffer); 58 BufferHandleType handle; 59 MojoResult rv = MojoDuplicateBufferHandle( 60 buffer.value(), options, handle.mutable_value()); 61 // Reset even on failure (reduces the chances that a "stale"/incorrect handle 62 // will be used). 63 new_buffer->reset(handle); 64 return rv; 65 } 66 67 template <class BufferHandleType> 68 inline MojoResult MapBuffer(BufferHandleType buffer, 69 uint64_t offset, 70 uint64_t num_bytes, 71 void** pointer, 72 MojoMapBufferFlags flags) { 73 assert(buffer.is_valid()); 74 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags); 75 } 76 77 inline MojoResult UnmapBuffer(void* pointer) { 78 assert(pointer); 79 return MojoUnmapBuffer(pointer); 80 } 81 82 // A wrapper class that automatically creates a shared buffer and owns the 83 // handle. 84 class SharedBuffer { 85 public: 86 explicit SharedBuffer(uint64_t num_bytes); 87 SharedBuffer(uint64_t num_bytes, 88 const MojoCreateSharedBufferOptions& options); 89 ~SharedBuffer(); 90 91 ScopedSharedBufferHandle handle; 92 }; 93 94 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) { 95 MojoResult result MOJO_ALLOW_UNUSED = 96 CreateSharedBuffer(NULL, num_bytes, &handle); 97 assert(result == MOJO_RESULT_OK); 98 } 99 100 inline SharedBuffer::SharedBuffer( 101 uint64_t num_bytes, 102 const MojoCreateSharedBufferOptions& options) { 103 MojoResult result MOJO_ALLOW_UNUSED = 104 CreateSharedBuffer(&options, num_bytes, &handle); 105 assert(result == MOJO_RESULT_OK); 106 } 107 108 inline SharedBuffer::~SharedBuffer() { 109 } 110 111 } // namespace mojo 112 113 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ 114