OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // This file provides a C++ wrapping around the Mojo C API for shared buffers, | 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 | 6 // replacing the prefix of "Mojo" with a "mojo" namespace, and using more |
7 // strongly-typed representations of |MojoHandle|s. | 7 // strongly-typed representations of |MojoHandle|s. |
8 // | 8 // |
9 // Please see "mojo/public/c/system/buffer.h" for complete documentation of the | 9 // Please see "mojo/public/c/system/buffer.h" for complete documentation of the |
10 // API. | 10 // API. |
11 | 11 |
12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | 12 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ |
13 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | 13 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ |
14 | 14 |
15 #include <stdint.h> | 15 #include <stdint.h> |
16 | 16 |
| 17 #include <memory> |
| 18 |
17 #include "base/compiler_specific.h" | 19 #include "base/compiler_specific.h" |
18 #include "base/logging.h" | 20 #include "base/logging.h" |
19 #include "mojo/public/c/system/buffer.h" | 21 #include "mojo/public/c/system/buffer.h" |
20 #include "mojo/public/cpp/system/handle.h" | 22 #include "mojo/public/cpp/system/handle.h" |
21 | 23 |
22 namespace mojo { | 24 namespace mojo { |
| 25 namespace internal { |
| 26 |
| 27 struct Unmapper { |
| 28 void operator()(void* buffer) { |
| 29 MojoResult result = MojoUnmapBuffer(buffer); |
| 30 DCHECK_EQ(MOJO_RESULT_OK, result); |
| 31 } |
| 32 }; |
| 33 |
| 34 } // namespace internal |
| 35 |
| 36 using ScopedSharedBufferMapping = std::unique_ptr<void, internal::Unmapper>; |
| 37 |
| 38 class SharedBufferHandle; |
| 39 |
| 40 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; |
23 | 41 |
24 // A strongly-typed representation of a |MojoHandle| referring to a shared | 42 // A strongly-typed representation of a |MojoHandle| referring to a shared |
25 // buffer. | 43 // buffer. |
26 class SharedBufferHandle : public Handle { | 44 class SharedBufferHandle : public Handle { |
27 public: | 45 public: |
28 SharedBufferHandle() {} | 46 SharedBufferHandle() {} |
29 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} | 47 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} |
30 | 48 |
31 // Copying and assignment allowed. | 49 // Copying and assignment allowed. |
| 50 |
| 51 // Clones this shared buffer handle. If |read_only| is true or this is a |
| 52 // read-only handle, the new handle will be read-only. On failure, this will |
| 53 // return an empty result. |
| 54 ScopedSharedBufferHandle Clone(bool read_only = false) const; |
| 55 |
| 56 // Maps |size| bytes of this shared buffer. On failure, this will return a |
| 57 // null mapping. |
| 58 ScopedSharedBufferMapping Map(uint64_t size) const; |
| 59 |
| 60 // Maps |size| bytes of this shared buffer, starting |offset| bytes into the |
| 61 // buffer. On failure, this will return a null mapping. |
| 62 ScopedSharedBufferMapping MapAtOffset(uint64_t size, uint64_t offset) const; |
32 }; | 63 }; |
33 | 64 |
34 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), | 65 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), |
35 "Bad size for C++ SharedBufferHandle"); | 66 "Bad size for C++ SharedBufferHandle"); |
36 | |
37 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; | |
38 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), | 67 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), |
39 "Bad size for C++ ScopedSharedBufferHandle"); | 68 "Bad size for C++ ScopedSharedBufferHandle"); |
40 | 69 |
41 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete | 70 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete |
42 // documentation. | 71 // documentation. |
43 inline MojoResult CreateSharedBuffer( | 72 inline MojoResult CreateSharedBuffer( |
44 const MojoCreateSharedBufferOptions* options, | 73 const MojoCreateSharedBufferOptions* options, |
45 uint64_t num_bytes, | 74 uint64_t num_bytes, |
46 ScopedSharedBufferHandle* shared_buffer) { | 75 ScopedSharedBufferHandle* shared_buffer) { |
47 DCHECK(shared_buffer); | 76 DCHECK(shared_buffer); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 ALLOW_UNUSED_LOCAL(result); | 153 ALLOW_UNUSED_LOCAL(result); |
125 DCHECK_EQ(MOJO_RESULT_OK, result); | 154 DCHECK_EQ(MOJO_RESULT_OK, result); |
126 } | 155 } |
127 | 156 |
128 inline SharedBuffer::~SharedBuffer() { | 157 inline SharedBuffer::~SharedBuffer() { |
129 } | 158 } |
130 | 159 |
131 } // namespace mojo | 160 } // namespace mojo |
132 | 161 |
133 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | 162 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ |
OLD | NEW |