1 // Copyright 2018 The Fuchsia 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 LIB_FIDL_CPP_MESSAGE_BUFFER_H_
6 #define LIB_FIDL_CPP_MESSAGE_BUFFER_H_
7 
8 #include <stdint.h>
9 
10 #include <lib/fidl/cpp/builder.h>
11 #include <lib/fidl/cpp/message.h>
12 #include <zircon/fidl.h>
13 #include <zircon/types.h>
14 
15 namespace fidl {
16 
17 class MessageBuffer {
18 public:
19     // Creates a |MessageBuffer| that allocates buffers for message of the
20     // given capacities.
21     //
22     // The buffers are freed when the |MessageBuffer| is destructed.
23     explicit MessageBuffer(
24         uint32_t bytes_capacity = ZX_CHANNEL_MAX_MSG_BYTES,
25         uint32_t handles_capacity = ZX_CHANNEL_MAX_MSG_HANDLES);
26 
27     // The memory that backs the message is freed by this destructor.
28     ~MessageBuffer();
29 
30     // The memory in which bytes can be stored in this buffer.
bytes()31     uint8_t* bytes() const { return buffer_; }
32 
33     // The total number of bytes that can be stored in this buffer.
bytes_capacity()34     uint32_t bytes_capacity() const { return bytes_capacity_; }
35 
36     // The memory in which handles can be stored in this buffer.
37     zx_handle_t* handles() const;
38 
39     // The total number of handles that can be stored in this buffer.
handles_capacity()40     uint32_t handles_capacity() const { return handles_capacity_; }
41 
42     // Creates a |Message| that is backed by the memory in this buffer.
43     //
44     // The returned |Message| contains no bytes or handles.
45     Message CreateEmptyMessage();
46 
47     // Creates a |Builder| that is backed by the memory in this buffer.
48     Builder CreateBuilder();
49 
50 private:
51     uint8_t* const buffer_;
52     const uint32_t bytes_capacity_;
53     const uint32_t handles_capacity_;
54 };
55 
56 } // namespace fidl
57 
58 #endif // LIB_FIDL_CPP_MESSAGE_BUFFER_H_
59