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.
5 #ifndef MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
6 #define MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_
10 #include "mojo/public/c/system/buffer.h"
11 #include "mojo/public/cpp/system/handle.h"
12 #include "mojo/public/cpp/system/macros.h"
16 // SharedBufferHandle ----------------------------------------------------------
18 class SharedBufferHandle
: public Handle
{
20 SharedBufferHandle() {}
21 explicit SharedBufferHandle(MojoHandle value
) : Handle(value
) {}
23 // Copying and assignment allowed.
26 static_assert(sizeof(SharedBufferHandle
) == sizeof(Handle
),
27 "Bad size for C++ SharedBufferHandle");
29 typedef ScopedHandleBase
<SharedBufferHandle
> ScopedSharedBufferHandle
;
30 static_assert(sizeof(ScopedSharedBufferHandle
) == sizeof(SharedBufferHandle
),
31 "Bad size for C++ ScopedSharedBufferHandle");
33 inline MojoResult
CreateSharedBuffer(
34 const MojoCreateSharedBufferOptions
* options
,
36 ScopedSharedBufferHandle
* shared_buffer
) {
37 assert(shared_buffer
);
38 SharedBufferHandle handle
;
40 MojoCreateSharedBuffer(options
, num_bytes
, handle
.mutable_value());
41 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
43 shared_buffer
->reset(handle
);
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
51 template <class BufferHandleType
>
52 inline MojoResult
DuplicateBuffer(
53 BufferHandleType buffer
,
54 const MojoDuplicateBufferHandleOptions
* options
,
55 ScopedHandleBase
<BufferHandleType
>* 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
62 new_buffer
->reset(handle
);
66 template <class BufferHandleType
>
67 inline MojoResult
MapBuffer(BufferHandleType buffer
,
71 MojoMapBufferFlags flags
) {
72 assert(buffer
.is_valid());
73 return MojoMapBuffer(buffer
.value(), offset
, num_bytes
, pointer
, flags
);
76 inline MojoResult
UnmapBuffer(void* pointer
) {
78 return MojoUnmapBuffer(pointer
);
81 // A wrapper class that automatically creates a shared buffer and owns the
85 explicit SharedBuffer(uint64_t num_bytes
);
86 SharedBuffer(uint64_t num_bytes
,
87 const MojoCreateSharedBufferOptions
& options
);
90 ScopedSharedBufferHandle handle
;
93 inline SharedBuffer::SharedBuffer(uint64_t num_bytes
) {
94 MojoResult result MOJO_ALLOW_UNUSED
=
95 CreateSharedBuffer(nullptr, num_bytes
, &handle
);
96 assert(result
== MOJO_RESULT_OK
);
99 inline SharedBuffer::SharedBuffer(
101 const MojoCreateSharedBufferOptions
& options
) {
102 MojoResult result MOJO_ALLOW_UNUSED
=
103 CreateSharedBuffer(&options
, num_bytes
, &handle
);
104 assert(result
== MOJO_RESULT_OK
);
107 inline SharedBuffer::~SharedBuffer() {
112 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_