blob: 6e86d35cec1e9f957ea6e40028365ea9de6f0ff6 [file] [log] [blame]
Austin Schuhe93d8642019-10-13 15:27:07 -07001#ifndef AOS_FLATBUFFERS_H_
2#define AOS_FLATBUFFERS_H_
3
Alex Perrycb7da4b2019-08-28 19:35:56 -07004#include <array>
James Kuszmaul3ae42262019-11-08 12:33:41 -08005#include <string_view>
Alex Perrycb7da4b2019-08-28 19:35:56 -07006
Austin Schuh6f3babe2020-01-26 20:34:50 -08007#include "absl/types/span.h"
Tyler Chatow116edf12020-01-26 11:52:39 -08008#include "aos/macros.h"
Austin Schuhe93d8642019-10-13 15:27:07 -07009#include "flatbuffers/flatbuffers.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070011
12namespace aos {
13
Austin Schuh40485ed2019-10-26 21:51:44 -070014// This class is a base class for all sizes of array backed allocators.
15class FixedAllocatorBase : public flatbuffers::Allocator {
16 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080017 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
18
Austin Schuh40485ed2019-10-26 21:51:44 -070019 // TODO(austin): Read the contract for these.
20 uint8_t *allocate(size_t) override;
21
22 void deallocate(uint8_t *, size_t) override { is_allocated_ = false; }
23
24 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
25 size_t) override;
26
27 virtual const uint8_t *data() const = 0;
28 virtual uint8_t *data() = 0;
29 virtual size_t size() const = 0;
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 void Reset() { is_allocated_ = false; }
Brian Silvermana1652f32020-01-29 20:41:44 -080032 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070033
Tyler Chatow116edf12020-01-26 11:52:39 -080034 bool allocated() { return is_allocated_; }
35
Austin Schuh40485ed2019-10-26 21:51:44 -070036 private:
37 bool is_allocated_ = false;
38};
39
40// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080041// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070042class FixedAllocator : public FixedAllocatorBase {
43 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080044 FixedAllocator(size_t size) : buffer_(size, 0) {}
45
Austin Schuh40485ed2019-10-26 21:51:44 -070046 uint8_t *data() override { return &buffer_[0]; }
47 const uint8_t *data() const override { return &buffer_[0]; }
48 size_t size() const override { return buffer_.size(); }
49
Austin Schuhe84c3ed2019-12-14 15:29:48 -080050 // Releases the data in the buffer.
51 std::vector<uint8_t> release() { return std::move(buffer_); }
52
Austin Schuh40485ed2019-10-26 21:51:44 -070053 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080054 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070055};
56
Alex Perrycb7da4b2019-08-28 19:35:56 -070057// This class adapts a preallocated memory region to an Allocator.
58class PreallocatedAllocator : public FixedAllocatorBase {
59 public:
60 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080061 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080062 PreallocatedAllocator(PreallocatedAllocator &&other)
63 : data_(other.data_), size_(other.size_) {
64 CHECK(!is_allocated());
65 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 }
Brian Silvermana1652f32020-01-29 20:41:44 -080067
68 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
69 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
70 CHECK(!is_allocated());
71 CHECK(!other.is_allocated());
72 data_ = other.data_;
73 size_ = other.size_;
74 return *this;
75 }
76
77 uint8_t *data() final {
78 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
79 }
80 const uint8_t *data() const final {
81 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
82 }
83 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070084
85 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080086 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 size_t size_;
88};
89
Austin Schuh40485ed2019-10-26 21:51:44 -070090// Base class representing an object which holds the memory representing a root
91// flatbuffer.
92template <typename T>
93class Flatbuffer {
94 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -070096 // Returns the MiniReflectTypeTable for T.
97 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
98 return T::MiniReflectTypeTable();
99 }
100
101 // Returns a message from the buffer.
102 const T &message() const {
103 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
104 }
105 // Returns a mutable message. It can be mutated via the flatbuffer rules.
106 T *mutable_message() {
107 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
108 }
109
110 virtual const uint8_t *data() const = 0;
111 virtual uint8_t *data() = 0;
112 virtual size_t size() const = 0;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800113
114 absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); }
115 absl::Span<const uint8_t> span() const {
116 return absl::Span<const uint8_t>(data(), size());
117 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700118};
119
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120// String backed flatbuffer.
121template <typename T>
122class FlatbufferString : public Flatbuffer<T> {
123 public:
124 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800125 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 // Builds a Flatbuffer by copying the data from the other flatbuffer.
127 FlatbufferString(const Flatbuffer<T> &other) {
128 data_ = std::string(other.data(), other.size());
129 }
130
131 // Coppies the data from the other flatbuffer.
132 FlatbufferString &operator=(const Flatbuffer<T> &other) {
133 data_ = std::string(other.data(), other.size());
134 return *this;
135 }
136
137 virtual ~FlatbufferString() override {}
138
139 const uint8_t *data() const override {
140 return reinterpret_cast<const uint8_t *>(data_.data());
141 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800142 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 size_t size() const override { return data_.size(); }
144
145 private:
146 std::string data_;
147};
148
Austin Schuhe309d2a2019-11-29 13:25:21 -0800149// Vector backed flatbuffer.
150template <typename T>
151class FlatbufferVector : public Flatbuffer<T> {
152 public:
153 // Builds a Flatbuffer around a vector.
154 FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
155
156 // Builds a Flatbuffer by copying the data from the other flatbuffer.
157 FlatbufferVector(const Flatbuffer<T> &other)
158 : data_(other.data(), other.data() + other.size()) {}
159
Austin Schuh5131bd02020-01-08 17:25:59 -0800160 // Copy constructor.
161 FlatbufferVector(const FlatbufferVector<T> &other)
162 : data_(other.data(), other.data() + other.size()) {}
163
Austin Schuh03803bd2019-12-30 18:08:17 -0800164 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800165 FlatbufferVector(FlatbufferVector<T> &&other)
166 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800167
Austin Schuhe309d2a2019-11-29 13:25:21 -0800168 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800169 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800170 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
171 return *this;
172 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800173 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
174 data_ = std::move(other.data_);
175 return *this;
176 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800177
Austin Schuh05b70472020-01-01 17:11:17 -0800178 // Constructs an empty flatbuffer of type T.
179 static FlatbufferVector<T> Empty() {
180 return FlatbufferVector<T>(std::vector<uint8_t>{});
181 }
182
Austin Schuhe309d2a2019-11-29 13:25:21 -0800183 virtual ~FlatbufferVector() override {}
184
185 const uint8_t *data() const override { return data_.data(); }
186 uint8_t *data() override { return data_.data(); }
187 size_t size() const override { return data_.size(); }
188
189 private:
190 std::vector<uint8_t> data_;
191};
192
Austin Schuhe93d8642019-10-13 15:27:07 -0700193// This object associates the message type with the memory storing the
194// flatbuffer. This only stores root tables.
195//
196// From a usage point of view, pointers to the data are very different than
197// pointers to the tables.
198template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800199class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700200 public:
201 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700202 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700203 : buffer_(::std::move(buffer)) {}
204
205 // Builds a flatbuffer by taking ownership of the buffer from the other
206 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700207 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
208 : buffer_(::std::move(fb.buffer_)) {}
209 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700210 ::std::swap(buffer_, fb.buffer_);
211 return *this;
212 }
213
Alex Perrycb7da4b2019-08-28 19:35:56 -0700214 virtual ~FlatbufferDetachedBuffer() override {}
215
Austin Schuhe93d8642019-10-13 15:27:07 -0700216 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700217 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700218 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800219 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700220 const auto end = fbb.EndTable(fbb.StartTable());
221 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700222 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700223 }
224
225 // Returns references to the buffer, and the data.
226 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700227 const uint8_t *data() const override { return buffer_.data(); }
228 uint8_t *data() override { return buffer_.data(); }
229 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700230
231 private:
232 flatbuffers::DetachedBuffer buffer_;
233};
234
Tyler Chatow116edf12020-01-26 11:52:39 -0800235// Array backed flatbuffer which manages building of the flatbuffer.
236template <typename T, size_t Size>
237class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> {
238 public:
239 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {
240 builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
Austin Schuhd7b15da2020-02-17 15:06:11 -0800241 builder_.ForceDefaults(true);
Tyler Chatow116edf12020-01-26 11:52:39 -0800242 }
243
244 flatbuffers::FlatBufferBuilder *Builder() {
245 if (allocator_.allocated()) {
246 LOG(FATAL) << "Array backed flatbuffer can only be built once";
247 }
248 return &builder_;
249 }
250
251 void Finish(flatbuffers::Offset<T> root) {
252 if (!allocator_.allocated()) {
253 LOG(FATAL) << "Cannot finish if never building";
254 }
255 builder_.Finish(root);
256 data_ = builder_.GetBufferPointer();
257 size_ = builder_.GetSize();
258 }
259
260 const uint8_t *data() const override {
261 CHECK_NOTNULL(data_);
262 return data_;
263 }
264 uint8_t *data() override {
265 CHECK_NOTNULL(data_);
266 return data_;
267 }
268 size_t size() const override { return size_; }
269
270 private:
271 std::array<uint8_t, Size> buffer_;
272 PreallocatedAllocator allocator_;
273 flatbuffers::FlatBufferBuilder builder_;
274 uint8_t *data_ = nullptr;
275 size_t size_ = 0;
276
277 DISALLOW_COPY_AND_ASSIGN(FlatbufferFixedAllocatorArray);
278};
279
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800280// This object associates the message type with the memory storing the
281// flatbuffer. This only stores root tables.
282//
283// From a usage point of view, pointers to the data are very different than
284// pointers to the tables.
285template <typename T>
286class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
287 public:
288 // Builds a Flatbuffer by taking ownership of the buffer.
289 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
290 : buffer_(::std::move(buffer)) {
291 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
292 }
293
294 // Builds a flatbuffer by taking ownership of the buffer from the other
295 // flatbuffer.
296 SizePrefixedFlatbufferDetachedBuffer(
297 SizePrefixedFlatbufferDetachedBuffer &&fb)
298 : buffer_(::std::move(fb.buffer_)) {}
299 SizePrefixedFlatbufferDetachedBuffer &operator=(
300 SizePrefixedFlatbufferDetachedBuffer &&fb) {
301 ::std::swap(buffer_, fb.buffer_);
302 return *this;
303 }
304
305 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
306
307 // Returns references to the buffer, and the data.
308 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
309 const uint8_t *data() const override {
310 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
311 }
312 uint8_t *data() override {
313 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
314 }
315 size_t size() const override {
316 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
317 }
318
319 private:
320 flatbuffers::DetachedBuffer buffer_;
321};
Austin Schuhe93d8642019-10-13 15:27:07 -0700322// TODO(austin): Need a way to get our hands on the max size. Can start with
323// "large" for now.
324
325} // namespace aos
326
327#endif // AOS_FLATBUFFERS_H_