| OLD | NEW |
| (Empty) |
| 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 // This file provides a C++ wrapping around the Mojo C API for shared buffers, | |
| 6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more | |
| 7 // strongly-typed representations of |MojoHandle|s. | |
| 8 // | |
| 9 // Please see "mojo/public/c/include/mojo/system/buffer.h" for complete | |
| 10 // documentation of the API. | |
| 11 | |
| 12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | |
| 13 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | |
| 14 | |
| 15 #include <assert.h> | |
| 16 #include <mojo/system/buffer.h> | |
| 17 | |
| 18 #include "mojo/public/cpp/system/handle.h" | |
| 19 #include "mojo/public/cpp/system/macros.h" | |
| 20 | |
| 21 namespace mojo { | |
| 22 | |
| 23 // A strongly-typed representation of a |MojoHandle| referring to a shared | |
| 24 // buffer. | |
| 25 class SharedBufferHandle : public Handle { | |
| 26 public: | |
| 27 SharedBufferHandle() {} | |
| 28 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} | |
| 29 | |
| 30 // Copying and assignment allowed. | |
| 31 }; | |
| 32 | |
| 33 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), | |
| 34 "Bad size for C++ SharedBufferHandle"); | |
| 35 | |
| 36 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; | |
| 37 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), | |
| 38 "Bad size for C++ ScopedSharedBufferHandle"); | |
| 39 | |
| 40 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete | |
| 41 // documentation. | |
| 42 inline MojoResult CreateSharedBuffer( | |
| 43 const MojoCreateSharedBufferOptions* options, | |
| 44 uint64_t num_bytes, | |
| 45 ScopedSharedBufferHandle* shared_buffer) { | |
| 46 assert(shared_buffer); | |
| 47 SharedBufferHandle handle; | |
| 48 MojoResult rv = | |
| 49 MojoCreateSharedBuffer(options, num_bytes, handle.mutable_value()); | |
| 50 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
| 51 // will be used). | |
| 52 shared_buffer->reset(handle); | |
| 53 return rv; | |
| 54 } | |
| 55 | |
| 56 // Maps a part of a buffer (specified by |buffer|, |offset|, and |num_bytes|) | |
| 57 // into memory. See |MojoMapBuffer()| for complete documentation. | |
| 58 template <class BufferHandleType> | |
| 59 inline MojoResult MapBuffer(BufferHandleType buffer, | |
| 60 uint64_t offset, | |
| 61 uint64_t num_bytes, | |
| 62 void** pointer, | |
| 63 MojoMapBufferFlags flags) { | |
| 64 assert(buffer.is_valid()); | |
| 65 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags); | |
| 66 } | |
| 67 | |
| 68 // Unmaps a part of a buffer that was previously mapped with |MapBuffer()|. | |
| 69 // See |MojoUnmapBuffer()| for complete documentation. | |
| 70 inline MojoResult UnmapBuffer(void* pointer) { | |
| 71 assert(pointer); | |
| 72 return MojoUnmapBuffer(pointer); | |
| 73 } | |
| 74 | |
| 75 // A wrapper class that automatically creates a shared buffer and owns the | |
| 76 // handle. | |
| 77 class SharedBuffer { | |
| 78 public: | |
| 79 explicit SharedBuffer(uint64_t num_bytes); | |
| 80 SharedBuffer(uint64_t num_bytes, | |
| 81 const MojoCreateSharedBufferOptions& options); | |
| 82 ~SharedBuffer(); | |
| 83 | |
| 84 ScopedSharedBufferHandle handle; | |
| 85 }; | |
| 86 | |
| 87 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) { | |
| 88 MojoResult result = CreateSharedBuffer(nullptr, num_bytes, &handle); | |
| 89 MOJO_ALLOW_UNUSED_LOCAL(result); | |
| 90 assert(result == MOJO_RESULT_OK); | |
| 91 } | |
| 92 | |
| 93 inline SharedBuffer::SharedBuffer( | |
| 94 uint64_t num_bytes, | |
| 95 const MojoCreateSharedBufferOptions& options) { | |
| 96 MojoResult result = CreateSharedBuffer(&options, num_bytes, &handle); | |
| 97 MOJO_ALLOW_UNUSED_LOCAL(result); | |
| 98 assert(result == MOJO_RESULT_OK); | |
| 99 } | |
| 100 | |
| 101 inline SharedBuffer::~SharedBuffer() { | |
| 102 } | |
| 103 | |
| 104 } // namespace mojo | |
| 105 | |
| 106 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | |
| OLD | NEW |