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: |
| 46 enum class AccessMode { |
| 47 READ_WRITE, |
| 48 READ_ONLY, |
| 49 }; |
| 50 |
28 SharedBufferHandle() {} | 51 SharedBufferHandle() {} |
29 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} | 52 explicit SharedBufferHandle(MojoHandle value) : Handle(value) {} |
30 | 53 |
31 // Copying and assignment allowed. | 54 // Copying and assignment allowed. |
| 55 |
| 56 // Clones this shared buffer handle. If |access_mode| is READ_ONLY or this is |
| 57 // a read-only handle, the new handle will be read-only. On failure, this will |
| 58 // return an empty result. |
| 59 ScopedSharedBufferHandle Clone(AccessMode = AccessMode::READ_WRITE) const; |
| 60 |
| 61 // Maps |size| bytes of this shared buffer. On failure, this will return a |
| 62 // null mapping. |
| 63 ScopedSharedBufferMapping Map(uint64_t size) const; |
| 64 |
| 65 // Maps |size| bytes of this shared buffer, starting |offset| bytes into the |
| 66 // buffer. On failure, this will return a null mapping. |
| 67 ScopedSharedBufferMapping MapAtOffset(uint64_t size, uint64_t offset) const; |
32 }; | 68 }; |
33 | 69 |
34 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), | 70 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), |
35 "Bad size for C++ SharedBufferHandle"); | 71 "Bad size for C++ SharedBufferHandle"); |
36 | |
37 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; | |
38 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), | 72 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), |
39 "Bad size for C++ ScopedSharedBufferHandle"); | 73 "Bad size for C++ ScopedSharedBufferHandle"); |
40 | 74 |
41 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete | 75 // Creates a shared buffer. See |MojoCreateSharedBuffer()| for complete |
42 // documentation. | 76 // documentation. |
43 inline MojoResult CreateSharedBuffer( | 77 inline MojoResult CreateSharedBuffer( |
44 const MojoCreateSharedBufferOptions* options, | 78 const MojoCreateSharedBufferOptions* options, |
45 uint64_t num_bytes, | 79 uint64_t num_bytes, |
46 ScopedSharedBufferHandle* shared_buffer) { | 80 ScopedSharedBufferHandle* shared_buffer) { |
47 DCHECK(shared_buffer); | 81 DCHECK(shared_buffer); |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 ALLOW_UNUSED_LOCAL(result); | 158 ALLOW_UNUSED_LOCAL(result); |
125 DCHECK_EQ(MOJO_RESULT_OK, result); | 159 DCHECK_EQ(MOJO_RESULT_OK, result); |
126 } | 160 } |
127 | 161 |
128 inline SharedBuffer::~SharedBuffer() { | 162 inline SharedBuffer::~SharedBuffer() { |
129 } | 163 } |
130 | 164 |
131 } // namespace mojo | 165 } // namespace mojo |
132 | 166 |
133 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | 167 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ |
OLD | NEW |