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