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 #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 static_assert(sizeof(SharedBufferHandle) == sizeof(Handle), | |
27 "Bad size for C++ SharedBufferHandle"); | |
28 | |
29 typedef ScopedHandleBase<SharedBufferHandle> ScopedSharedBufferHandle; | |
30 static_assert(sizeof(ScopedSharedBufferHandle) == sizeof(SharedBufferHandle), | |
31 "Bad size for C++ ScopedSharedBufferHandle"); | |
32 | |
33 inline MojoResult CreateSharedBuffer( | |
34 const MojoCreateSharedBufferOptions* options, | |
35 uint64_t num_bytes, | |
36 ScopedSharedBufferHandle* shared_buffer) { | |
37 assert(shared_buffer); | |
38 SharedBufferHandle handle; | |
39 MojoResult rv = | |
40 MojoCreateSharedBuffer(options, num_bytes, handle.mutable_value()); | |
41 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
42 // will be used). | |
43 shared_buffer->reset(handle); | |
44 return rv; | |
45 } | |
46 | |
47 // TODO(vtl): This (and also the functions below) are templatized to allow for | |
48 // future/other buffer types. A bit "safer" would be to overload this function | |
49 // manually. (The template enforces that the in and out handles to be of the | |
50 // same type.) | |
51 template <class BufferHandleType> | |
52 inline MojoResult DuplicateBuffer( | |
53 BufferHandleType buffer, | |
54 const MojoDuplicateBufferHandleOptions* options, | |
55 ScopedHandleBase<BufferHandleType>* new_buffer) { | |
56 assert(new_buffer); | |
57 BufferHandleType handle; | |
58 MojoResult rv = MojoDuplicateBufferHandle( | |
59 buffer.value(), options, handle.mutable_value()); | |
60 // Reset even on failure (reduces the chances that a "stale"/incorrect handle | |
61 // will be used). | |
62 new_buffer->reset(handle); | |
63 return rv; | |
64 } | |
65 | |
66 template <class BufferHandleType> | |
67 inline MojoResult MapBuffer(BufferHandleType buffer, | |
68 uint64_t offset, | |
69 uint64_t num_bytes, | |
70 void** pointer, | |
71 MojoMapBufferFlags flags) { | |
72 assert(buffer.is_valid()); | |
73 return MojoMapBuffer(buffer.value(), offset, num_bytes, pointer, flags); | |
74 } | |
75 | |
76 inline MojoResult UnmapBuffer(void* pointer) { | |
77 assert(pointer); | |
78 return MojoUnmapBuffer(pointer); | |
79 } | |
80 | |
81 // A wrapper class that automatically creates a shared buffer and owns the | |
82 // handle. | |
83 class SharedBuffer { | |
84 public: | |
85 explicit SharedBuffer(uint64_t num_bytes); | |
86 SharedBuffer(uint64_t num_bytes, | |
87 const MojoCreateSharedBufferOptions& options); | |
88 ~SharedBuffer(); | |
89 | |
90 ScopedSharedBufferHandle handle; | |
91 }; | |
92 | |
93 inline SharedBuffer::SharedBuffer(uint64_t num_bytes) { | |
94 MojoResult result = CreateSharedBuffer(nullptr, num_bytes, &handle); | |
95 MOJO_ALLOW_UNUSED_LOCAL(result); | |
96 assert(result == MOJO_RESULT_OK); | |
97 } | |
98 | |
99 inline SharedBuffer::SharedBuffer( | |
100 uint64_t num_bytes, | |
101 const MojoCreateSharedBufferOptions& options) { | |
102 MojoResult result = CreateSharedBuffer(&options, num_bytes, &handle); | |
103 MOJO_ALLOW_UNUSED_LOCAL(result); | |
104 assert(result == MOJO_RESULT_OK); | |
105 } | |
106 | |
107 inline SharedBuffer::~SharedBuffer() { | |
108 } | |
109 | |
110 } // namespace mojo | |
111 | |
112 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_ | |
OLD | NEW |