blob: bea1225318483afc0a7819078dd740c625ad1910 [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
Austin Schuhadd6eb32020-11-09 21:24:26 -0800121 bool Verify() const {
Austin Schuh977a5ed2020-12-02 23:20:04 -0800122 if (span().size() < 4u) {
123 return false;
124 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800125 flatbuffers::Verifier v(span().data(), span().size());
Austin Schuha4fc60f2020-11-01 23:06:47 -0800126 return v.VerifyTable(&message());
127 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800128
129 protected:
130 virtual absl::Span<uint8_t> span() = 0;
131 virtual absl::Span<const uint8_t> span() const = 0;
132};
133
134// Base class for non-size prefixed flatbuffers. span() means different things
135// across the 2 types, so you end up with a different GetRoot.
136template <typename T>
137class NonSizePrefixedFlatbuffer : public Flatbuffer<T> {
138 public:
139 const T &message() const override {
140 return *flatbuffers::GetRoot<T>(
141 reinterpret_cast<const void *>(this->span().data()));
142 }
143 T *mutable_message() override {
144 return flatbuffers::GetMutableRoot<T>(
145 reinterpret_cast<void *>(this->span().data()));
146 }
147
148 absl::Span<uint8_t> span() override = 0;
149 absl::Span<const uint8_t> span() const override = 0;
Austin Schuha4fc60f2020-11-01 23:06:47 -0800150};
151
152// Non-owning Span backed flatbuffer.
153template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800154class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800155 public:
156 // Builds a flatbuffer pointing to the contents of a span.
157 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
158 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800159 FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) {
160 data_ = other.span();
161 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800162
163 // Copies the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800164 FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800165 data_ = other.span();
166 return *this;
167 }
168
169 virtual ~FlatbufferSpan() override {}
170
Austin Schuhadd6eb32020-11-09 21:24:26 -0800171 absl::Span<uint8_t> span() override {
172 LOG(FATAL) << "Unimplemented";
173 return absl::Span<uint8_t>(nullptr, 0);
Austin Schuha4fc60f2020-11-01 23:06:47 -0800174 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800175 absl::Span<const uint8_t> span() const override { return data_; }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800176
177 private:
178 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700179};
180
Austin Schuhadd6eb32020-11-09 21:24:26 -0800181// ResizeableBuffer backed flatbuffer.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800182template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800183class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800184 public:
Austin Schuhadd6eb32020-11-09 21:24:26 -0800185 // Builds a Flatbuffer around a ResizeableBuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700186 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800187
188 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800189 FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) {
190 data_.resize(other.span().size());
191 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700192 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800193
Austin Schuh5131bd02020-01-08 17:25:59 -0800194 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700195 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800196
Austin Schuh03803bd2019-12-30 18:08:17 -0800197 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800198 FlatbufferVector(FlatbufferVector<T> &&other)
199 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800200
Austin Schuhe309d2a2019-11-29 13:25:21 -0800201 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800202 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700203 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800204 return *this;
205 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800206 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
207 data_ = std::move(other.data_);
208 return *this;
209 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800210
Austin Schuh05b70472020-01-01 17:11:17 -0800211 // Constructs an empty flatbuffer of type T.
212 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700213 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800214 }
215
Austin Schuhe309d2a2019-11-29 13:25:21 -0800216 virtual ~FlatbufferVector() override {}
217
Austin Schuhadd6eb32020-11-09 21:24:26 -0800218 absl::Span<uint8_t> span() override {
219 return absl::Span<uint8_t>(data_.data(), data_.size());
220 }
221 absl::Span<const uint8_t> span() const override {
222 return absl::Span<const uint8_t>(data_.data(), data_.size());
223 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800224
225 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700226 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800227};
228
Austin Schuhe93d8642019-10-13 15:27:07 -0700229// This object associates the message type with the memory storing the
230// flatbuffer. This only stores root tables.
231//
232// From a usage point of view, pointers to the data are very different than
233// pointers to the tables.
234template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800235class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700236 public:
237 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700238 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700239 : buffer_(::std::move(buffer)) {}
240
241 // Builds a flatbuffer by taking ownership of the buffer from the other
242 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700243 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
244 : buffer_(::std::move(fb.buffer_)) {}
245 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700246 ::std::swap(buffer_, fb.buffer_);
247 return *this;
248 }
249
Alex Perrycb7da4b2019-08-28 19:35:56 -0700250 virtual ~FlatbufferDetachedBuffer() override {}
251
Austin Schuhe93d8642019-10-13 15:27:07 -0700252 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700253 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700254 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800255 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700256 const auto end = fbb.EndTable(fbb.StartTable());
257 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700258 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700259 }
260
261 // Returns references to the buffer, and the data.
262 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800263
264 absl::Span<uint8_t> span() override {
265 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
266 }
267 absl::Span<const uint8_t> span() const override {
268 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
269 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700270
271 private:
272 flatbuffers::DetachedBuffer buffer_;
273};
274
Tyler Chatow116edf12020-01-26 11:52:39 -0800275// Array backed flatbuffer which manages building of the flatbuffer.
276template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800277class FlatbufferFixedAllocatorArray final
278 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800279 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700280 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
281
282 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800283 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700284
Austin Schuhadd6eb32020-11-09 21:24:26 -0800285 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700286 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800287 CHECK_LE(other.span().size(), Size)
288 << ": Source flatbuffer is larger than the target.";
289 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700290 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800291 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800292 }
293
Brian Silverman341b57e2020-06-23 16:23:18 -0700294 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700295 CHECK(!allocator_.is_allocated() || data_ != nullptr)
296 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700297 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
298 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700299 data_ = nullptr;
300 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700301 }
302
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700303 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700304 CHECK(!allocator_.allocated())
305 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700306 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
307 fbb_.ForceDefaults(true);
308 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800309 }
310
311 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700312 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700313 fbb_.Finish(root);
314 data_ = fbb_.GetBufferPointer();
315 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700316 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800317 }
318
Austin Schuhadd6eb32020-11-09 21:24:26 -0800319 absl::Span<uint8_t> span() override {
320 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800321 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800322 absl::Span<const uint8_t> span() const override {
323 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800324 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800325
326 private:
327 std::array<uint8_t, Size> buffer_;
328 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700329 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800330 uint8_t *data_ = nullptr;
331 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800332};
333
Austin Schuhadd6eb32020-11-09 21:24:26 -0800334template <typename T>
335class SizePrefixedFlatbuffer : public Flatbuffer<T> {
336 public:
337 const T &message() const override {
338 return *flatbuffers::GetSizePrefixedRoot<T>(
339 reinterpret_cast<const void *>(this->span().data()));
340 }
341
342 T *mutable_message() override {
343 return flatbuffers::GetMutableSizePrefixedRoot<T>(
344 reinterpret_cast<void *>(this->span().data()));
345 }
346
347 absl::Span<uint8_t> span() override = 0;
348 absl::Span<const uint8_t> span() const override = 0;
349};
350
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800351// This object associates the message type with the memory storing the
352// flatbuffer. This only stores root tables.
353//
354// From a usage point of view, pointers to the data are very different than
355// pointers to the tables.
356template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800357class SizePrefixedFlatbufferDetachedBuffer final
358 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800359 public:
360 // Builds a Flatbuffer by taking ownership of the buffer.
361 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
362 : buffer_(::std::move(buffer)) {
363 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
364 }
365
366 // Builds a flatbuffer by taking ownership of the buffer from the other
367 // flatbuffer.
368 SizePrefixedFlatbufferDetachedBuffer(
369 SizePrefixedFlatbufferDetachedBuffer &&fb)
370 : buffer_(::std::move(fb.buffer_)) {}
371 SizePrefixedFlatbufferDetachedBuffer &operator=(
372 SizePrefixedFlatbufferDetachedBuffer &&fb) {
373 ::std::swap(buffer_, fb.buffer_);
374 return *this;
375 }
376
377 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
378
Austin Schuh2f8fd752020-09-01 22:38:28 -0700379 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
380 flatbuffers::FlatBufferBuilder fbb;
381 fbb.ForceDefaults(true);
382 const auto end = fbb.EndTable(fbb.StartTable());
383 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
384 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
385 }
386
Austin Schuh8c399962020-12-25 21:51:45 -0800387 flatbuffers::DetachedBuffer Release() {
388 flatbuffers::FlatBufferBuilder fbb;
389 fbb.ForceDefaults(true);
390 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700391 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800392 flatbuffers::DetachedBuffer result = fbb.Release();
393 std::swap(result, buffer_);
394 return result;
395 }
396
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800397 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800398 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700399 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
400 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800401 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700402 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
403 }
404
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800405 private:
406 flatbuffers::DetachedBuffer buffer_;
407};
Austin Schuhe93d8642019-10-13 15:27:07 -0700408
Austin Schuhadd6eb32020-11-09 21:24:26 -0800409// ResizeableBuffer backed flatbuffer.
410template <typename T>
411class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
412 public:
413 // Builds a Flatbuffer around a ResizeableBuffer.
414 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
415 : data_(std::move(data)) {}
416
417 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700418 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
419 : SizePrefixedFlatbufferVector(other.span()) {}
420
421 // Builds a flatbuffer by copying the data from the provided span.
422 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
423 data_.resize(span.size());
424 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800425 }
426
427 // Copy constructor.
428 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
429 : data_(other.data_) {}
430
431 // Move constructor.
432 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
433 : data_(std::move(other.data_)) {}
434
435 // Copies the data from the other flatbuffer.
436 SizePrefixedFlatbufferVector &operator=(
437 const SizePrefixedFlatbufferVector<T> &other) {
438 data_ = other.data_;
439 return *this;
440 }
441 SizePrefixedFlatbufferVector &operator=(
442 SizePrefixedFlatbufferVector<T> &&other) {
443 data_ = std::move(other.data_);
444 return *this;
445 }
446
447 // Constructs an empty flatbuffer of type T.
448 static SizePrefixedFlatbufferVector<T> Empty() {
449 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
450 }
451
452 virtual ~SizePrefixedFlatbufferVector() override {}
453
454 absl::Span<uint8_t> span() override {
455 return absl::Span<uint8_t>(data_.data(), data_.size());
456 }
457 absl::Span<const uint8_t> span() const override {
458 return absl::Span<const uint8_t>(data_.data(), data_.size());
459 }
460
461 private:
462 ResizeableBuffer data_;
463};
464
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800465// Non-owning Span backed flatbuffer.
466template <typename T>
467class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
468 public:
469 // Builds a flatbuffer pointing to the contents of a span.
470 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
471 : data_(data) {}
472 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
473 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
474 data_ = other.span();
475 }
476
477 // Points to the data in the other flatbuffer.
478 SizePrefixedFlatbufferSpan &operator=(
479 const SizePrefixedFlatbuffer<T> &other) {
480 data_ = other.span();
481 return *this;
482 }
483
484 ~SizePrefixedFlatbufferSpan() override {}
485
486 absl::Span<uint8_t> span() override {
487 LOG(FATAL) << "Unimplemented";
488 return absl::Span<uint8_t>(nullptr, 0);
489 }
490 absl::Span<const uint8_t> span() const override { return data_; }
491
492 private:
493 absl::Span<const uint8_t> data_;
494};
495
Brian Silvermanf51499a2020-09-21 12:49:08 -0700496inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
497 absl::Span<const uint8_t> span) {
498 // Copy the data from the span.
499 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
500 memcpy(buf, span.data(), span.size());
501 // Then give it to a DetachedBuffer to manage.
502 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
503 span.size());
504}
505
davidjevans8b9b52f2021-09-17 08:57:30 -0700506// MMap a flatbuffer on disk.
507template <typename T>
508class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> {
509 public:
510 // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk.
Austin Schuhe4d1a682021-10-01 15:04:50 -0700511 FlatbufferMMap(const std::string &flatbuffer_path,
512 util::FileOptions options = util::FileOptions::kReadable) {
513 span_ = util::MMapFile(flatbuffer_path, options);
davidjevans8b9b52f2021-09-17 08:57:30 -0700514 }
515
516 // Copies the reference to the mapped memory.
517 FlatbufferMMap(const FlatbufferMMap &) = default;
518 FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default;
519
520 // Moves the reference to the mapped memory from one pointer to another.
521 FlatbufferMMap(FlatbufferMMap &&) = default;
522 FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default;
523
Austin Schuhe4d1a682021-10-01 15:04:50 -0700524 absl::Span<uint8_t> span() override { return *span_; }
davidjevans8b9b52f2021-09-17 08:57:30 -0700525 absl::Span<const uint8_t> span() const override { return *span_; }
526
527 private:
528 std::shared_ptr<absl::Span<uint8_t>> span_;
529};
530
Austin Schuhe93d8642019-10-13 15:27:07 -0700531} // namespace aos
532
533#endif // AOS_FLATBUFFERS_H_