blob: 607303c29d4335f84cf82841ec58f8667b13dd29 [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"
Philipp Schrader790cb542023-07-05 21:06:52 -07008#include "flatbuffers/flatbuffers.h" // IWYU pragma: export
9#include "glog/logging.h"
10
Brian Silverman354697a2020-09-22 21:06:32 -070011#include "aos/containers/resizeable_buffer.h"
Tyler Chatow116edf12020-01-26 11:52:39 -080012#include "aos/macros.h"
davidjevans8b9b52f2021-09-17 08:57:30 -070013#include "aos/util/file.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070014
15namespace aos {
16
Austin Schuh40485ed2019-10-26 21:51:44 -070017// This class is a base class for all sizes of array backed allocators.
18class FixedAllocatorBase : public flatbuffers::Allocator {
19 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080020 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
21
Austin Schuh40485ed2019-10-26 21:51:44 -070022 // TODO(austin): Read the contract for these.
23 uint8_t *allocate(size_t) override;
24
Brian Silverman1715d472020-08-12 22:54:15 -070025 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
26 DCHECK_LE(allocated_size, size());
27 DCHECK_EQ(allocated_data, data());
28 CHECK(is_allocated_);
29 is_allocated_ = false;
30 }
Austin Schuh40485ed2019-10-26 21:51:44 -070031
32 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
33 size_t) override;
34
35 virtual const uint8_t *data() const = 0;
36 virtual uint8_t *data() = 0;
37 virtual size_t size() const = 0;
38
Brian Silverman1715d472020-08-12 22:54:15 -070039 void Reset() {
40 CHECK(!is_allocated_);
41 is_allocated_ = false;
42 }
Brian Silvermana1652f32020-01-29 20:41:44 -080043 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070044
Tyler Chatow116edf12020-01-26 11:52:39 -080045 bool allocated() { return is_allocated_; }
46
Austin Schuh40485ed2019-10-26 21:51:44 -070047 private:
48 bool is_allocated_ = false;
49};
50
51// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080052// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070053class FixedAllocator : public FixedAllocatorBase {
54 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080055 FixedAllocator(size_t size) : buffer_(size, 0) {}
56
Austin Schuh40485ed2019-10-26 21:51:44 -070057 uint8_t *data() override { return &buffer_[0]; }
58 const uint8_t *data() const override { return &buffer_[0]; }
59 size_t size() const override { return buffer_.size(); }
60
Austin Schuhe84c3ed2019-12-14 15:29:48 -080061 // Releases the data in the buffer.
62 std::vector<uint8_t> release() { return std::move(buffer_); }
63
Austin Schuh40485ed2019-10-26 21:51:44 -070064 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080065 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070066};
67
Alex Perrycb7da4b2019-08-28 19:35:56 -070068// This class adapts a preallocated memory region to an Allocator.
69class PreallocatedAllocator : public FixedAllocatorBase {
70 public:
71 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080072 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080073 PreallocatedAllocator(PreallocatedAllocator &&other)
74 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070075 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080076 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070077 }
Brian Silvermana1652f32020-01-29 20:41:44 -080078
79 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
80 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070081 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080082 CHECK(!other.is_allocated());
83 data_ = other.data_;
84 size_ = other.size_;
85 return *this;
86 }
87
88 uint8_t *data() final {
89 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
90 }
91 const uint8_t *data() const final {
92 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
93 }
94 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070095
96 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080097 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070098 size_t size_;
99};
100
Austin Schuh40485ed2019-10-26 21:51:44 -0700101// Base class representing an object which holds the memory representing a root
102// flatbuffer.
103template <typename T>
104class Flatbuffer {
105 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700106 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700107 // Returns the MiniReflectTypeTable for T.
108 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
109 return T::MiniReflectTypeTable();
110 }
111
112 // Returns a message from the buffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800113 virtual const T &message() const = 0;
Austin Schuh40485ed2019-10-26 21:51:44 -0700114 // Returns a mutable message. It can be mutated via the flatbuffer rules.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800115 virtual T *mutable_message() = 0;
Austin Schuh39580f12020-08-01 14:44:08 -0700116
117 // Wipes out the data buffer. This is handy to mark an instance as freed, and
118 // make attempts to use it fail more obviously.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800119 void Wipe() { memset(span().data(), 0, span().size()); }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800120
James Kuszmaul38babac2024-01-25 14:35:08 -0800121 // Returns true if the flatbuffer is valid. Returns false if either:
122 // * The flatbuffer is incorrectly constructed (e.g., it points to memory
123 // locations outside of the current memory buffer).
124 // * The flatbuffer is too complex, and the flatbuffer verifier chosen to bail
125 // when attempting to traverse the tree of tables.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800126 bool Verify() const {
Austin Schuh977a5ed2020-12-02 23:20:04 -0800127 if (span().size() < 4u) {
128 return false;
129 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800130 flatbuffers::Verifier v(span().data(), span().size());
Austin Schuha4fc60f2020-11-01 23:06:47 -0800131 return v.VerifyTable(&message());
132 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800133
134 protected:
135 virtual absl::Span<uint8_t> span() = 0;
136 virtual absl::Span<const uint8_t> span() const = 0;
137};
138
139// Base class for non-size prefixed flatbuffers. span() means different things
140// across the 2 types, so you end up with a different GetRoot.
141template <typename T>
142class NonSizePrefixedFlatbuffer : public Flatbuffer<T> {
143 public:
144 const T &message() const override {
145 return *flatbuffers::GetRoot<T>(
146 reinterpret_cast<const void *>(this->span().data()));
147 }
148 T *mutable_message() override {
149 return flatbuffers::GetMutableRoot<T>(
150 reinterpret_cast<void *>(this->span().data()));
151 }
152
153 absl::Span<uint8_t> span() override = 0;
154 absl::Span<const uint8_t> span() const override = 0;
Austin Schuha4fc60f2020-11-01 23:06:47 -0800155};
156
157// Non-owning Span backed flatbuffer.
158template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800159class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800160 public:
161 // Builds a flatbuffer pointing to the contents of a span.
162 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
163 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800164 FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) {
165 data_ = other.span();
166 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800167
168 // Copies the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800169 FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800170 data_ = other.span();
171 return *this;
172 }
173
174 virtual ~FlatbufferSpan() override {}
175
Austin Schuhadd6eb32020-11-09 21:24:26 -0800176 absl::Span<uint8_t> span() override {
177 LOG(FATAL) << "Unimplemented";
178 return absl::Span<uint8_t>(nullptr, 0);
Austin Schuha4fc60f2020-11-01 23:06:47 -0800179 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800180 absl::Span<const uint8_t> span() const override { return data_; }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800181
182 private:
183 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700184};
185
Austin Schuhadd6eb32020-11-09 21:24:26 -0800186// ResizeableBuffer backed flatbuffer.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800187template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800188class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800189 public:
Austin Schuhadd6eb32020-11-09 21:24:26 -0800190 // Builds a Flatbuffer around a ResizeableBuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700191 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800192
193 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800194 FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) {
195 data_.resize(other.span().size());
Stephan Pleines9dac6772023-06-01 13:21:41 -0700196 CHECK(other.span().data());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800197 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700198 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800199
Austin Schuh5131bd02020-01-08 17:25:59 -0800200 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700201 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800202
Austin Schuh03803bd2019-12-30 18:08:17 -0800203 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800204 FlatbufferVector(FlatbufferVector<T> &&other)
205 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800206
Austin Schuhe309d2a2019-11-29 13:25:21 -0800207 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800208 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700209 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800210 return *this;
211 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800212 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
213 data_ = std::move(other.data_);
214 return *this;
215 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800216
Austin Schuh05b70472020-01-01 17:11:17 -0800217 // Constructs an empty flatbuffer of type T.
218 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700219 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800220 }
221
Austin Schuhe309d2a2019-11-29 13:25:21 -0800222 virtual ~FlatbufferVector() override {}
223
Austin Schuhadd6eb32020-11-09 21:24:26 -0800224 absl::Span<uint8_t> span() override {
225 return absl::Span<uint8_t>(data_.data(), data_.size());
226 }
227 absl::Span<const uint8_t> span() const override {
228 return absl::Span<const uint8_t>(data_.data(), data_.size());
229 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800230
231 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700232 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800233};
234
Austin Schuhe93d8642019-10-13 15:27:07 -0700235// This object associates the message type with the memory storing the
236// flatbuffer. This only stores root tables.
237//
238// From a usage point of view, pointers to the data are very different than
239// pointers to the tables.
240template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800241class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700242 public:
243 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700244 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700245 : buffer_(::std::move(buffer)) {}
246
247 // Builds a flatbuffer by taking ownership of the buffer from the other
248 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700249 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
250 : buffer_(::std::move(fb.buffer_)) {}
251 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700252 ::std::swap(buffer_, fb.buffer_);
253 return *this;
254 }
255
Alex Perrycb7da4b2019-08-28 19:35:56 -0700256 virtual ~FlatbufferDetachedBuffer() override {}
257
Austin Schuhe93d8642019-10-13 15:27:07 -0700258 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700259 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700260 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800261 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700262 const auto end = fbb.EndTable(fbb.StartTable());
263 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700264 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700265 }
266
267 // Returns references to the buffer, and the data.
268 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800269
270 absl::Span<uint8_t> span() override {
271 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
272 }
273 absl::Span<const uint8_t> span() const override {
274 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
275 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700276
277 private:
278 flatbuffers::DetachedBuffer buffer_;
279};
280
Tyler Chatow116edf12020-01-26 11:52:39 -0800281// Array backed flatbuffer which manages building of the flatbuffer.
282template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800283class FlatbufferFixedAllocatorArray final
284 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800285 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700286 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
287
288 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800289 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700290
Austin Schuhadd6eb32020-11-09 21:24:26 -0800291 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700292 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800293 CHECK_LE(other.span().size(), Size)
294 << ": Source flatbuffer is larger than the target.";
295 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700296 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800297 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800298 }
299
Brian Silverman341b57e2020-06-23 16:23:18 -0700300 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700301 CHECK(!allocator_.is_allocated() || data_ != nullptr)
302 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700303 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
304 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700305 data_ = nullptr;
306 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700307 }
308
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700309 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700310 CHECK(!allocator_.allocated())
311 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700312 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
313 fbb_.ForceDefaults(true);
314 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800315 }
316
317 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700318 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700319 fbb_.Finish(root);
320 data_ = fbb_.GetBufferPointer();
321 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700322 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800323 }
324
Austin Schuhadd6eb32020-11-09 21:24:26 -0800325 absl::Span<uint8_t> span() override {
326 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800327 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800328 absl::Span<const uint8_t> span() const override {
329 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800330 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800331
332 private:
333 std::array<uint8_t, Size> buffer_;
334 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700335 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800336 uint8_t *data_ = nullptr;
337 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800338};
339
Austin Schuhadd6eb32020-11-09 21:24:26 -0800340template <typename T>
341class SizePrefixedFlatbuffer : public Flatbuffer<T> {
342 public:
343 const T &message() const override {
344 return *flatbuffers::GetSizePrefixedRoot<T>(
345 reinterpret_cast<const void *>(this->span().data()));
346 }
347
348 T *mutable_message() override {
349 return flatbuffers::GetMutableSizePrefixedRoot<T>(
350 reinterpret_cast<void *>(this->span().data()));
351 }
352
353 absl::Span<uint8_t> span() override = 0;
354 absl::Span<const uint8_t> span() const override = 0;
355};
356
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800357// This object associates the message type with the memory storing the
358// flatbuffer. This only stores root tables.
359//
360// From a usage point of view, pointers to the data are very different than
361// pointers to the tables.
362template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800363class SizePrefixedFlatbufferDetachedBuffer final
364 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800365 public:
366 // Builds a Flatbuffer by taking ownership of the buffer.
367 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
368 : buffer_(::std::move(buffer)) {
369 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
370 }
371
372 // Builds a flatbuffer by taking ownership of the buffer from the other
373 // flatbuffer.
374 SizePrefixedFlatbufferDetachedBuffer(
375 SizePrefixedFlatbufferDetachedBuffer &&fb)
376 : buffer_(::std::move(fb.buffer_)) {}
377 SizePrefixedFlatbufferDetachedBuffer &operator=(
378 SizePrefixedFlatbufferDetachedBuffer &&fb) {
379 ::std::swap(buffer_, fb.buffer_);
380 return *this;
381 }
382
383 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
384
Austin Schuh2f8fd752020-09-01 22:38:28 -0700385 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
386 flatbuffers::FlatBufferBuilder fbb;
387 fbb.ForceDefaults(true);
388 const auto end = fbb.EndTable(fbb.StartTable());
389 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
390 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
391 }
392
Austin Schuh8c399962020-12-25 21:51:45 -0800393 flatbuffers::DetachedBuffer Release() {
394 flatbuffers::FlatBufferBuilder fbb;
395 fbb.ForceDefaults(true);
396 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700397 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800398 flatbuffers::DetachedBuffer result = fbb.Release();
399 std::swap(result, buffer_);
400 return result;
401 }
402
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800403 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800404 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700405 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
406 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800407 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700408 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
409 }
410
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800411 private:
412 flatbuffers::DetachedBuffer buffer_;
413};
Austin Schuhe93d8642019-10-13 15:27:07 -0700414
Austin Schuhadd6eb32020-11-09 21:24:26 -0800415// ResizeableBuffer backed flatbuffer.
416template <typename T>
417class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
418 public:
419 // Builds a Flatbuffer around a ResizeableBuffer.
420 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
421 : data_(std::move(data)) {}
422
423 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700424 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
425 : SizePrefixedFlatbufferVector(other.span()) {}
426
427 // Builds a flatbuffer by copying the data from the provided span.
428 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
429 data_.resize(span.size());
430 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800431 }
432
433 // Copy constructor.
434 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
435 : data_(other.data_) {}
436
437 // Move constructor.
438 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
439 : data_(std::move(other.data_)) {}
440
441 // Copies the data from the other flatbuffer.
442 SizePrefixedFlatbufferVector &operator=(
443 const SizePrefixedFlatbufferVector<T> &other) {
444 data_ = other.data_;
445 return *this;
446 }
447 SizePrefixedFlatbufferVector &operator=(
448 SizePrefixedFlatbufferVector<T> &&other) {
449 data_ = std::move(other.data_);
450 return *this;
451 }
452
453 // Constructs an empty flatbuffer of type T.
454 static SizePrefixedFlatbufferVector<T> Empty() {
455 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
456 }
457
458 virtual ~SizePrefixedFlatbufferVector() override {}
459
460 absl::Span<uint8_t> span() override {
461 return absl::Span<uint8_t>(data_.data(), data_.size());
462 }
463 absl::Span<const uint8_t> span() const override {
464 return absl::Span<const uint8_t>(data_.data(), data_.size());
465 }
466
467 private:
468 ResizeableBuffer data_;
469};
470
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800471// Non-owning Span backed flatbuffer.
472template <typename T>
473class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
474 public:
475 // Builds a flatbuffer pointing to the contents of a span.
476 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
477 : data_(data) {}
478 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
479 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
480 data_ = other.span();
481 }
482
483 // Points to the data in the other flatbuffer.
484 SizePrefixedFlatbufferSpan &operator=(
485 const SizePrefixedFlatbuffer<T> &other) {
486 data_ = other.span();
487 return *this;
488 }
489
490 ~SizePrefixedFlatbufferSpan() override {}
491
492 absl::Span<uint8_t> span() override {
493 LOG(FATAL) << "Unimplemented";
494 return absl::Span<uint8_t>(nullptr, 0);
495 }
496 absl::Span<const uint8_t> span() const override { return data_; }
497
498 private:
499 absl::Span<const uint8_t> data_;
500};
501
Brian Silvermanf51499a2020-09-21 12:49:08 -0700502inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
503 absl::Span<const uint8_t> span) {
504 // Copy the data from the span.
505 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
506 memcpy(buf, span.data(), span.size());
507 // Then give it to a DetachedBuffer to manage.
508 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
509 span.size());
510}
511
davidjevans8b9b52f2021-09-17 08:57:30 -0700512// MMap a flatbuffer on disk.
513template <typename T>
514class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> {
515 public:
516 // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk.
Austin Schuhe4d1a682021-10-01 15:04:50 -0700517 FlatbufferMMap(const std::string &flatbuffer_path,
518 util::FileOptions options = util::FileOptions::kReadable) {
519 span_ = util::MMapFile(flatbuffer_path, options);
davidjevans8b9b52f2021-09-17 08:57:30 -0700520 }
521
522 // Copies the reference to the mapped memory.
523 FlatbufferMMap(const FlatbufferMMap &) = default;
524 FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default;
525
526 // Moves the reference to the mapped memory from one pointer to another.
527 FlatbufferMMap(FlatbufferMMap &&) = default;
528 FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default;
529
Austin Schuhe4d1a682021-10-01 15:04:50 -0700530 absl::Span<uint8_t> span() override { return *span_; }
davidjevans8b9b52f2021-09-17 08:57:30 -0700531 absl::Span<const uint8_t> span() const override { return *span_; }
532
533 private:
534 std::shared_ptr<absl::Span<uint8_t>> span_;
535};
536
James Kuszmaul1d5d9ec2024-01-14 17:53:23 -0800537// The regular flatbuffer API makes it surprisingly irritating to unpack an
538// Object into an ObjectT without a bunch of extra lines.
539template <typename T>
540T::NativeTableType UnpackFlatbuffer(const T *fbs) {
541 typename T::NativeTableType object;
542 fbs->UnPackTo(&object);
543 return object;
544}
545
Austin Schuhe93d8642019-10-13 15:27:07 -0700546} // namespace aos
547
548#endif // AOS_FLATBUFFERS_H_