blob: 39b072be23cb0a31d21f1796ec0aed0bf6647633 [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"
Brian Silverman354697a2020-09-22 21:06:32 -07008#include "aos/containers/resizeable_buffer.h"
Tyler Chatow116edf12020-01-26 11:52:39 -08009#include "aos/macros.h"
Austin Schuhb8075812020-10-19 09:36:49 -070010#include "flatbuffers/flatbuffers.h" // IWYU pragma: export
Austin Schuhe84c3ed2019-12-14 15:29:48 -080011#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070012
13namespace aos {
14
Austin Schuh40485ed2019-10-26 21:51:44 -070015// This class is a base class for all sizes of array backed allocators.
16class FixedAllocatorBase : public flatbuffers::Allocator {
17 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080018 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
19
Austin Schuh40485ed2019-10-26 21:51:44 -070020 // TODO(austin): Read the contract for these.
21 uint8_t *allocate(size_t) override;
22
Brian Silverman1715d472020-08-12 22:54:15 -070023 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
24 DCHECK_LE(allocated_size, size());
25 DCHECK_EQ(allocated_data, data());
26 CHECK(is_allocated_);
27 is_allocated_ = false;
28 }
Austin Schuh40485ed2019-10-26 21:51:44 -070029
30 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
31 size_t) override;
32
33 virtual const uint8_t *data() const = 0;
34 virtual uint8_t *data() = 0;
35 virtual size_t size() const = 0;
36
Brian Silverman1715d472020-08-12 22:54:15 -070037 void Reset() {
38 CHECK(!is_allocated_);
39 is_allocated_ = false;
40 }
Brian Silvermana1652f32020-01-29 20:41:44 -080041 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070042
Tyler Chatow116edf12020-01-26 11:52:39 -080043 bool allocated() { return is_allocated_; }
44
Austin Schuh40485ed2019-10-26 21:51:44 -070045 private:
46 bool is_allocated_ = false;
47};
48
49// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080050// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070051class FixedAllocator : public FixedAllocatorBase {
52 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080053 FixedAllocator(size_t size) : buffer_(size, 0) {}
54
Austin Schuh40485ed2019-10-26 21:51:44 -070055 uint8_t *data() override { return &buffer_[0]; }
56 const uint8_t *data() const override { return &buffer_[0]; }
57 size_t size() const override { return buffer_.size(); }
58
Austin Schuhe84c3ed2019-12-14 15:29:48 -080059 // Releases the data in the buffer.
60 std::vector<uint8_t> release() { return std::move(buffer_); }
61
Austin Schuh40485ed2019-10-26 21:51:44 -070062 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070064};
65
Alex Perrycb7da4b2019-08-28 19:35:56 -070066// This class adapts a preallocated memory region to an Allocator.
67class PreallocatedAllocator : public FixedAllocatorBase {
68 public:
69 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080070 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080071 PreallocatedAllocator(PreallocatedAllocator &&other)
72 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070073 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080074 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070075 }
Brian Silvermana1652f32020-01-29 20:41:44 -080076
77 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
78 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070079 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080080 CHECK(!other.is_allocated());
81 data_ = other.data_;
82 size_ = other.size_;
83 return *this;
84 }
85
86 uint8_t *data() final {
87 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
88 }
89 const uint8_t *data() const final {
90 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
91 }
92 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070093
94 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080095 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 size_t size_;
97};
98
Austin Schuh40485ed2019-10-26 21:51:44 -070099// Base class representing an object which holds the memory representing a root
100// flatbuffer.
101template <typename T>
102class Flatbuffer {
103 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700105 // Returns the MiniReflectTypeTable for T.
106 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
107 return T::MiniReflectTypeTable();
108 }
109
110 // Returns a message from the buffer.
111 const T &message() const {
112 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
113 }
114 // Returns a mutable message. It can be mutated via the flatbuffer rules.
115 T *mutable_message() {
116 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
117 }
118
119 virtual const uint8_t *data() const = 0;
120 virtual uint8_t *data() = 0;
121 virtual size_t size() const = 0;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800122
123 absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); }
124 absl::Span<const uint8_t> span() const {
125 return absl::Span<const uint8_t>(data(), size());
126 }
Austin Schuh39580f12020-08-01 14:44:08 -0700127
128 // Wipes out the data buffer. This is handy to mark an instance as freed, and
129 // make attempts to use it fail more obviously.
130 void Wipe() { memset(data(), 0, size()); }
Austin Schuh40485ed2019-10-26 21:51:44 -0700131};
132
Alex Perrycb7da4b2019-08-28 19:35:56 -0700133// String backed flatbuffer.
134template <typename T>
135class FlatbufferString : public Flatbuffer<T> {
136 public:
137 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800138 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700139 // Builds a Flatbuffer by copying the data from the other flatbuffer.
140 FlatbufferString(const Flatbuffer<T> &other) {
Brian Silvermanc8a5b552020-04-28 16:43:59 -0700141 data_ =
142 std::string(reinterpret_cast<const char *>(other.data()), other.size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700143 }
144
Jim Ostrowski46d25232020-09-07 18:44:17 -0700145 // Copies the data from the other flatbuffer.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700146 FlatbufferString &operator=(const Flatbuffer<T> &other) {
147 data_ = std::string(other.data(), other.size());
148 return *this;
149 }
150
151 virtual ~FlatbufferString() override {}
152
153 const uint8_t *data() const override {
154 return reinterpret_cast<const uint8_t *>(data_.data());
155 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800156 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700157 size_t size() const override { return data_.size(); }
158
159 private:
160 std::string data_;
161};
162
Austin Schuhe309d2a2019-11-29 13:25:21 -0800163// Vector backed flatbuffer.
164template <typename T>
165class FlatbufferVector : public Flatbuffer<T> {
166 public:
167 // Builds a Flatbuffer around a vector.
Brian Silverman354697a2020-09-22 21:06:32 -0700168 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800169
170 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700171 FlatbufferVector(const Flatbuffer<T> &other) {
172 data_.resize(other.size());
173 memcpy(data_.data(), other.data(), data_.size());
174 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800175
Austin Schuh5131bd02020-01-08 17:25:59 -0800176 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700177 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800178
Austin Schuh03803bd2019-12-30 18:08:17 -0800179 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800180 FlatbufferVector(FlatbufferVector<T> &&other)
181 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800182
Austin Schuhe309d2a2019-11-29 13:25:21 -0800183 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800184 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700185 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800186 return *this;
187 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800188 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
189 data_ = std::move(other.data_);
190 return *this;
191 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800192
Austin Schuh05b70472020-01-01 17:11:17 -0800193 // Constructs an empty flatbuffer of type T.
194 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700195 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800196 }
197
Austin Schuhe309d2a2019-11-29 13:25:21 -0800198 virtual ~FlatbufferVector() override {}
199
200 const uint8_t *data() const override { return data_.data(); }
201 uint8_t *data() override { return data_.data(); }
202 size_t size() const override { return data_.size(); }
203
204 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700205 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800206};
207
Austin Schuhe93d8642019-10-13 15:27:07 -0700208// This object associates the message type with the memory storing the
209// flatbuffer. This only stores root tables.
210//
211// From a usage point of view, pointers to the data are very different than
212// pointers to the tables.
213template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800214class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700215 public:
216 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700217 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700218 : buffer_(::std::move(buffer)) {}
219
220 // Builds a flatbuffer by taking ownership of the buffer from the other
221 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700222 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
223 : buffer_(::std::move(fb.buffer_)) {}
224 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700225 ::std::swap(buffer_, fb.buffer_);
226 return *this;
227 }
228
Alex Perrycb7da4b2019-08-28 19:35:56 -0700229 virtual ~FlatbufferDetachedBuffer() override {}
230
Austin Schuhe93d8642019-10-13 15:27:07 -0700231 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700232 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700233 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800234 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700235 const auto end = fbb.EndTable(fbb.StartTable());
236 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700237 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700238 }
239
240 // Returns references to the buffer, and the data.
241 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700242 const uint8_t *data() const override { return buffer_.data(); }
243 uint8_t *data() override { return buffer_.data(); }
244 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700245
246 private:
247 flatbuffers::DetachedBuffer buffer_;
248};
249
Tyler Chatow116edf12020-01-26 11:52:39 -0800250// Array backed flatbuffer which manages building of the flatbuffer.
251template <typename T, size_t Size>
252class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> {
253 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700254 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
255
256 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
257 void operator=(const Flatbuffer<T> &) = delete;
258
259 void CopyFrom(const Flatbuffer<T> &other) {
260 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
261 memcpy(buffer_.begin(), other.data(), other.size());
262 data_ = buffer_.begin();
263 size_ = other.size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800264 }
265
Brian Silverman341b57e2020-06-23 16:23:18 -0700266 void Reset() {
Brian Silverman1715d472020-08-12 22:54:15 -0700267 CHECK(!allocator_.is_allocated()) << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700268 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
269 fbb_.ForceDefaults(true);
Brian Silverman341b57e2020-06-23 16:23:18 -0700270 }
271
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700272 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700273 CHECK(!allocator_.allocated())
274 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700275 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
276 fbb_.ForceDefaults(true);
277 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800278 }
279
280 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700281 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700282 fbb_.Finish(root);
283 data_ = fbb_.GetBufferPointer();
284 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700285 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800286 }
287
288 const uint8_t *data() const override {
289 CHECK_NOTNULL(data_);
290 return data_;
291 }
292 uint8_t *data() override {
293 CHECK_NOTNULL(data_);
294 return data_;
295 }
296 size_t size() const override { return size_; }
297
298 private:
299 std::array<uint8_t, Size> buffer_;
300 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700301 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800302 uint8_t *data_ = nullptr;
303 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800304};
305
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800306// This object associates the message type with the memory storing the
307// flatbuffer. This only stores root tables.
308//
309// From a usage point of view, pointers to the data are very different than
310// pointers to the tables.
311template <typename T>
312class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
313 public:
314 // Builds a Flatbuffer by taking ownership of the buffer.
315 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
316 : buffer_(::std::move(buffer)) {
317 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
318 }
319
320 // Builds a flatbuffer by taking ownership of the buffer from the other
321 // flatbuffer.
322 SizePrefixedFlatbufferDetachedBuffer(
323 SizePrefixedFlatbufferDetachedBuffer &&fb)
324 : buffer_(::std::move(fb.buffer_)) {}
325 SizePrefixedFlatbufferDetachedBuffer &operator=(
326 SizePrefixedFlatbufferDetachedBuffer &&fb) {
327 ::std::swap(buffer_, fb.buffer_);
328 return *this;
329 }
330
331 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
332
Austin Schuh2f8fd752020-09-01 22:38:28 -0700333 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
334 flatbuffers::FlatBufferBuilder fbb;
335 fbb.ForceDefaults(true);
336 const auto end = fbb.EndTable(fbb.StartTable());
337 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
338 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
339 }
340
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800341 // Returns references to the buffer, and the data.
342 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
343 const uint8_t *data() const override {
344 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
345 }
346 uint8_t *data() override {
347 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
348 }
349 size_t size() const override {
350 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
351 }
352
Austin Schuh2f8fd752020-09-01 22:38:28 -0700353 absl::Span<uint8_t> full_span() {
354 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
355 }
356 absl::Span<const uint8_t> full_span() const {
357 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
358 }
359
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800360 private:
361 flatbuffers::DetachedBuffer buffer_;
362};
Austin Schuhe93d8642019-10-13 15:27:07 -0700363
Brian Silvermanf51499a2020-09-21 12:49:08 -0700364inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
365 absl::Span<const uint8_t> span) {
366 // Copy the data from the span.
367 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
368 memcpy(buf, span.data(), span.size());
369 // Then give it to a DetachedBuffer to manage.
370 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
371 span.size());
372}
373
Austin Schuhe93d8642019-10-13 15:27:07 -0700374} // namespace aos
375
376#endif // AOS_FLATBUFFERS_H_