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 MOJO_COMPILE_ASSERT(sizeof(SharedBufferHandle
) == sizeof(Handle
),
27 bad_size_for_cpp_SharedBufferHandle
);
29 typedef ScopedHandleBase
<SharedBufferHandle
> ScopedSharedBufferHandle
;
30 MOJO_COMPILE_ASSERT(sizeof(ScopedSharedBufferHandle
) ==
31 sizeof(SharedBufferHandle
),
32 bad_size_for_cpp_ScopedSharedBufferHandle
);
34 inline MojoResult
CreateSharedBuffer(
35 const MojoCreateSharedBufferOptions
* options
,
37 ScopedSharedBufferHandle
* shared_buffer
) {
38 assert(shared_buffer
);
39 SharedBufferHandle handle
;
40 MojoResult rv
= MojoCreateSharedBuffer(options
, num_bytes
,
41 handle
.mutable_value());
42 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
44 shared_buffer
->reset(handle
);
48 // TODO(vtl): This (and also the functions below) are templatized to allow for
49 // future/other buffer types. A bit "safer" would be to overload this function
50 // manually. (The template enforces that the in and out handles to be of the
52 template <class BufferHandleType
>
53 inline MojoResult
DuplicateBuffer(
54 BufferHandleType buffer
,
55 const MojoDuplicateBufferHandleOptions
* options
,
56 ScopedHandleBase
<BufferHandleType
>* new_buffer
) {
58 BufferHandleType handle
;
59 MojoResult rv
= MojoDuplicateBufferHandle(
60 buffer
.value(), options
, handle
.mutable_value());
61 // Reset even on failure (reduces the chances that a "stale"/incorrect handle
63 new_buffer
->reset(handle
);
67 template <class BufferHandleType
>
68 inline MojoResult
MapBuffer(BufferHandleType buffer
,
72 MojoMapBufferFlags flags
) {
73 assert(buffer
.is_valid());
74 return MojoMapBuffer(buffer
.value(), offset
, num_bytes
, pointer
, flags
);
77 inline MojoResult
UnmapBuffer(void* pointer
) {
79 return MojoUnmapBuffer(pointer
);
82 // A wrapper class that automatically creates a shared buffer and owns the
86 explicit SharedBuffer(uint64_t num_bytes
);
87 SharedBuffer(uint64_t num_bytes
,
88 const MojoCreateSharedBufferOptions
& options
);
91 ScopedSharedBufferHandle handle
;
94 inline SharedBuffer::SharedBuffer(uint64_t num_bytes
) {
95 MojoResult result MOJO_ALLOW_UNUSED
=
96 CreateSharedBuffer(NULL
, num_bytes
, &handle
);
97 assert(result
== MOJO_RESULT_OK
);
100 inline SharedBuffer::SharedBuffer(
102 const MojoCreateSharedBufferOptions
& options
) {
103 MojoResult result MOJO_ALLOW_UNUSED
=
104 CreateSharedBuffer(&options
, num_bytes
, &handle
);
105 assert(result
== MOJO_RESULT_OK
);
108 inline SharedBuffer::~SharedBuffer() {
113 #endif // MOJO_PUBLIC_CPP_SYSTEM_BUFFER_H_