blob: 4549e19423544a7a77211de59420620b58f087b9 [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 Schuh99f7c6a2024-06-25 22:07:44 -07007#include "absl/log/check.h"
8#include "absl/log/log.h"
Austin Schuh6f3babe2020-01-26 20:34:50 -08009#include "absl/types/span.h"
Philipp Schrader790cb542023-07-05 21:06:52 -070010#include "flatbuffers/flatbuffers.h" // IWYU pragma: export
Philipp Schrader790cb542023-07-05 21:06:52 -070011
Brian Silverman354697a2020-09-22 21:06:32 -070012#include "aos/containers/resizeable_buffer.h"
Austin Schuh3c9f92c2024-04-30 17:56:42 -070013#include "aos/ipc_lib/data_alignment.h"
Tyler Chatow116edf12020-01-26 11:52:39 -080014#include "aos/macros.h"
davidjevans8b9b52f2021-09-17 08:57:30 -070015#include "aos/util/file.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070016
17namespace aos {
18
Austin Schuh40485ed2019-10-26 21:51:44 -070019// This class is a base class for all sizes of array backed allocators.
20class FixedAllocatorBase : public flatbuffers::Allocator {
21 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080022 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
23
Austin Schuh40485ed2019-10-26 21:51:44 -070024 // TODO(austin): Read the contract for these.
25 uint8_t *allocate(size_t) override;
26
Brian Silverman1715d472020-08-12 22:54:15 -070027 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
28 DCHECK_LE(allocated_size, size());
29 DCHECK_EQ(allocated_data, data());
30 CHECK(is_allocated_);
31 is_allocated_ = false;
32 }
Austin Schuh40485ed2019-10-26 21:51:44 -070033
34 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
35 size_t) override;
36
37 virtual const uint8_t *data() const = 0;
38 virtual uint8_t *data() = 0;
39 virtual size_t size() const = 0;
40
Brian Silverman1715d472020-08-12 22:54:15 -070041 void Reset() {
42 CHECK(!is_allocated_);
43 is_allocated_ = false;
44 }
Brian Silvermana1652f32020-01-29 20:41:44 -080045 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070046
Tyler Chatow116edf12020-01-26 11:52:39 -080047 bool allocated() { return is_allocated_; }
48
Austin Schuh40485ed2019-10-26 21:51:44 -070049 private:
50 bool is_allocated_ = false;
51};
52
53// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080054// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070055class FixedAllocator : public FixedAllocatorBase {
56 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080057 FixedAllocator(size_t size) : buffer_(size, 0) {}
58
Austin Schuh40485ed2019-10-26 21:51:44 -070059 uint8_t *data() override { return &buffer_[0]; }
60 const uint8_t *data() const override { return &buffer_[0]; }
61 size_t size() const override { return buffer_.size(); }
62
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 // Releases the data in the buffer.
64 std::vector<uint8_t> release() { return std::move(buffer_); }
65
Austin Schuh40485ed2019-10-26 21:51:44 -070066 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080067 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070068};
69
Alex Perrycb7da4b2019-08-28 19:35:56 -070070// This class adapts a preallocated memory region to an Allocator.
71class PreallocatedAllocator : public FixedAllocatorBase {
72 public:
73 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080074 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080075 PreallocatedAllocator(PreallocatedAllocator &&other)
76 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070077 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080078 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070079 }
Brian Silvermana1652f32020-01-29 20:41:44 -080080
81 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
82 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070083 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080084 CHECK(!other.is_allocated());
85 data_ = other.data_;
86 size_ = other.size_;
87 return *this;
88 }
89
90 uint8_t *data() final {
Austin Schuh6bdcc372024-06-27 14:49:11 -070091 CHECK(data_ != nullptr);
92 return reinterpret_cast<uint8_t *>(data_);
Brian Silvermana1652f32020-01-29 20:41:44 -080093 }
94 const uint8_t *data() const final {
Austin Schuh6bdcc372024-06-27 14:49:11 -070095 CHECK(data_ != nullptr);
96 return reinterpret_cast<const uint8_t *>(data_);
Brian Silvermana1652f32020-01-29 20:41:44 -080097 }
98 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070099
100 private:
Brian Silvermana1652f32020-01-29 20:41:44 -0800101 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700102 size_t size_;
103};
104
Austin Schuh40485ed2019-10-26 21:51:44 -0700105// Base class representing an object which holds the memory representing a root
106// flatbuffer.
107template <typename T>
108class Flatbuffer {
109 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700110 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700111 // Returns the MiniReflectTypeTable for T.
112 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
113 return T::MiniReflectTypeTable();
114 }
115
116 // Returns a message from the buffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800117 virtual const T &message() const = 0;
Austin Schuh40485ed2019-10-26 21:51:44 -0700118 // Returns a mutable message. It can be mutated via the flatbuffer rules.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800119 virtual T *mutable_message() = 0;
Austin Schuh39580f12020-08-01 14:44:08 -0700120
121 // Wipes out the data buffer. This is handy to mark an instance as freed, and
122 // make attempts to use it fail more obviously.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800123 void Wipe() { memset(span().data(), 0, span().size()); }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800124
James Kuszmaul38babac2024-01-25 14:35:08 -0800125 // Returns true if the flatbuffer is valid. Returns false if either:
126 // * The flatbuffer is incorrectly constructed (e.g., it points to memory
127 // locations outside of the current memory buffer).
128 // * The flatbuffer is too complex, and the flatbuffer verifier chosen to bail
129 // when attempting to traverse the tree of tables.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800130 bool Verify() const {
Austin Schuh977a5ed2020-12-02 23:20:04 -0800131 if (span().size() < 4u) {
132 return false;
133 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800134 flatbuffers::Verifier v(span().data(), span().size());
Austin Schuha4fc60f2020-11-01 23:06:47 -0800135 return v.VerifyTable(&message());
136 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800137
138 protected:
139 virtual absl::Span<uint8_t> span() = 0;
140 virtual absl::Span<const uint8_t> span() const = 0;
141};
142
143// Base class for non-size prefixed flatbuffers. span() means different things
144// across the 2 types, so you end up with a different GetRoot.
145template <typename T>
146class NonSizePrefixedFlatbuffer : public Flatbuffer<T> {
147 public:
148 const T &message() const override {
149 return *flatbuffers::GetRoot<T>(
150 reinterpret_cast<const void *>(this->span().data()));
151 }
152 T *mutable_message() override {
153 return flatbuffers::GetMutableRoot<T>(
154 reinterpret_cast<void *>(this->span().data()));
155 }
156
157 absl::Span<uint8_t> span() override = 0;
158 absl::Span<const uint8_t> span() const override = 0;
Austin Schuha4fc60f2020-11-01 23:06:47 -0800159};
160
161// Non-owning Span backed flatbuffer.
162template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800163class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800164 public:
165 // Builds a flatbuffer pointing to the contents of a span.
166 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
167 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800168 FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) {
169 data_ = other.span();
170 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800171
172 // Copies the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800173 FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800174 data_ = other.span();
175 return *this;
176 }
177
178 virtual ~FlatbufferSpan() override {}
179
Austin Schuhadd6eb32020-11-09 21:24:26 -0800180 absl::Span<uint8_t> span() override {
181 LOG(FATAL) << "Unimplemented";
182 return absl::Span<uint8_t>(nullptr, 0);
Austin Schuha4fc60f2020-11-01 23:06:47 -0800183 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800184 absl::Span<const uint8_t> span() const override { return data_; }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800185
186 private:
187 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700188};
189
Austin Schuhadd6eb32020-11-09 21:24:26 -0800190// ResizeableBuffer backed flatbuffer.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800191template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800192class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800193 public:
Austin Schuhadd6eb32020-11-09 21:24:26 -0800194 // Builds a Flatbuffer around a ResizeableBuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700195 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800196
197 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800198 FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) {
199 data_.resize(other.span().size());
Stephan Pleines9dac6772023-06-01 13:21:41 -0700200 CHECK(other.span().data());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800201 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700202 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800203
Austin Schuh5131bd02020-01-08 17:25:59 -0800204 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700205 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800206
Austin Schuh03803bd2019-12-30 18:08:17 -0800207 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800208 FlatbufferVector(FlatbufferVector<T> &&other)
209 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800210
Austin Schuhe309d2a2019-11-29 13:25:21 -0800211 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800212 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700213 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800214 return *this;
215 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800216 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
217 data_ = std::move(other.data_);
218 return *this;
219 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800220
Austin Schuh05b70472020-01-01 17:11:17 -0800221 // Constructs an empty flatbuffer of type T.
222 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700223 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800224 }
225
Austin Schuhe309d2a2019-11-29 13:25:21 -0800226 virtual ~FlatbufferVector() override {}
227
Austin Schuhadd6eb32020-11-09 21:24:26 -0800228 absl::Span<uint8_t> span() override {
229 return absl::Span<uint8_t>(data_.data(), data_.size());
230 }
231 absl::Span<const uint8_t> span() const override {
232 return absl::Span<const uint8_t>(data_.data(), data_.size());
233 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800234
235 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700236 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800237};
238
Austin Schuhe93d8642019-10-13 15:27:07 -0700239// This object associates the message type with the memory storing the
240// flatbuffer. This only stores root tables.
241//
242// From a usage point of view, pointers to the data are very different than
243// pointers to the tables.
244template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800245class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700246 public:
247 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700248 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700249 : buffer_(::std::move(buffer)) {}
250
251 // Builds a flatbuffer by taking ownership of the buffer from the other
252 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700253 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
254 : buffer_(::std::move(fb.buffer_)) {}
255 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700256 ::std::swap(buffer_, fb.buffer_);
257 return *this;
258 }
259
Alex Perrycb7da4b2019-08-28 19:35:56 -0700260 virtual ~FlatbufferDetachedBuffer() override {}
261
Austin Schuhe93d8642019-10-13 15:27:07 -0700262 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700263 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700264 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800265 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700266 const auto end = fbb.EndTable(fbb.StartTable());
267 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700268 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700269 }
270
271 // Returns references to the buffer, and the data.
272 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800273
274 absl::Span<uint8_t> span() override {
275 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
276 }
277 absl::Span<const uint8_t> span() const override {
278 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
279 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700280
281 private:
282 flatbuffers::DetachedBuffer buffer_;
283};
284
Tyler Chatow116edf12020-01-26 11:52:39 -0800285// Array backed flatbuffer which manages building of the flatbuffer.
286template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800287class FlatbufferFixedAllocatorArray final
288 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800289 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700290 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
291
292 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800293 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700294
Austin Schuhadd6eb32020-11-09 21:24:26 -0800295 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700296 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800297 CHECK_LE(other.span().size(), Size)
298 << ": Source flatbuffer is larger than the target.";
299 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700300 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800301 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800302 }
303
Brian Silverman341b57e2020-06-23 16:23:18 -0700304 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700305 CHECK(!allocator_.is_allocated() || data_ != nullptr)
306 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700307 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
308 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700309 data_ = nullptr;
310 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700311 }
312
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700313 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700314 CHECK(!allocator_.allocated())
315 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700316 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
317 fbb_.ForceDefaults(true);
318 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800319 }
320
321 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700322 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700323 fbb_.Finish(root);
324 data_ = fbb_.GetBufferPointer();
325 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700326 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800327 }
328
Austin Schuhadd6eb32020-11-09 21:24:26 -0800329 absl::Span<uint8_t> span() override {
330 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800331 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800332 absl::Span<const uint8_t> span() const override {
333 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800334 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800335
336 private:
337 std::array<uint8_t, Size> buffer_;
338 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700339 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800340 uint8_t *data_ = nullptr;
341 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800342};
343
Austin Schuhadd6eb32020-11-09 21:24:26 -0800344template <typename T>
345class SizePrefixedFlatbuffer : public Flatbuffer<T> {
346 public:
347 const T &message() const override {
348 return *flatbuffers::GetSizePrefixedRoot<T>(
349 reinterpret_cast<const void *>(this->span().data()));
350 }
351
352 T *mutable_message() override {
353 return flatbuffers::GetMutableSizePrefixedRoot<T>(
354 reinterpret_cast<void *>(this->span().data()));
355 }
356
357 absl::Span<uint8_t> span() override = 0;
358 absl::Span<const uint8_t> span() const override = 0;
359};
360
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800361// This object associates the message type with the memory storing the
362// flatbuffer. This only stores root tables.
363//
364// From a usage point of view, pointers to the data are very different than
365// pointers to the tables.
366template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800367class SizePrefixedFlatbufferDetachedBuffer final
368 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800369 public:
370 // Builds a Flatbuffer by taking ownership of the buffer.
371 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
372 : buffer_(::std::move(buffer)) {
373 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
374 }
375
376 // Builds a flatbuffer by taking ownership of the buffer from the other
377 // flatbuffer.
378 SizePrefixedFlatbufferDetachedBuffer(
379 SizePrefixedFlatbufferDetachedBuffer &&fb)
380 : buffer_(::std::move(fb.buffer_)) {}
381 SizePrefixedFlatbufferDetachedBuffer &operator=(
382 SizePrefixedFlatbufferDetachedBuffer &&fb) {
383 ::std::swap(buffer_, fb.buffer_);
384 return *this;
385 }
386
387 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
388
Austin Schuh2f8fd752020-09-01 22:38:28 -0700389 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
390 flatbuffers::FlatBufferBuilder fbb;
391 fbb.ForceDefaults(true);
392 const auto end = fbb.EndTable(fbb.StartTable());
393 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
394 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
395 }
396
Austin Schuh8c399962020-12-25 21:51:45 -0800397 flatbuffers::DetachedBuffer Release() {
398 flatbuffers::FlatBufferBuilder fbb;
399 fbb.ForceDefaults(true);
400 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700401 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800402 flatbuffers::DetachedBuffer result = fbb.Release();
403 std::swap(result, buffer_);
404 return result;
405 }
406
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800407 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800408 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700409 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
410 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800411 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700412 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
413 }
414
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800415 private:
416 flatbuffers::DetachedBuffer buffer_;
417};
Austin Schuhe93d8642019-10-13 15:27:07 -0700418
Austin Schuhadd6eb32020-11-09 21:24:26 -0800419// ResizeableBuffer backed flatbuffer.
420template <typename T>
421class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
422 public:
423 // Builds a Flatbuffer around a ResizeableBuffer.
424 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
425 : data_(std::move(data)) {}
426
427 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700428 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
429 : SizePrefixedFlatbufferVector(other.span()) {}
430
431 // Builds a flatbuffer by copying the data from the provided span.
432 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
433 data_.resize(span.size());
434 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800435 }
436
437 // Copy constructor.
438 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
439 : data_(other.data_) {}
440
441 // Move constructor.
442 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
443 : data_(std::move(other.data_)) {}
444
445 // Copies the data from the other flatbuffer.
446 SizePrefixedFlatbufferVector &operator=(
447 const SizePrefixedFlatbufferVector<T> &other) {
448 data_ = other.data_;
449 return *this;
450 }
451 SizePrefixedFlatbufferVector &operator=(
452 SizePrefixedFlatbufferVector<T> &&other) {
453 data_ = std::move(other.data_);
454 return *this;
455 }
456
457 // Constructs an empty flatbuffer of type T.
458 static SizePrefixedFlatbufferVector<T> Empty() {
459 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
460 }
461
462 virtual ~SizePrefixedFlatbufferVector() override {}
463
464 absl::Span<uint8_t> span() override {
465 return absl::Span<uint8_t>(data_.data(), data_.size());
466 }
467 absl::Span<const uint8_t> span() const override {
468 return absl::Span<const uint8_t>(data_.data(), data_.size());
469 }
470
471 private:
472 ResizeableBuffer data_;
473};
474
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800475// Non-owning Span backed flatbuffer.
476template <typename T>
477class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
478 public:
479 // Builds a flatbuffer pointing to the contents of a span.
480 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
481 : data_(data) {}
482 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
483 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
484 data_ = other.span();
485 }
486
487 // Points to the data in the other flatbuffer.
488 SizePrefixedFlatbufferSpan &operator=(
489 const SizePrefixedFlatbuffer<T> &other) {
490 data_ = other.span();
491 return *this;
492 }
493
494 ~SizePrefixedFlatbufferSpan() override {}
495
496 absl::Span<uint8_t> span() override {
497 LOG(FATAL) << "Unimplemented";
498 return absl::Span<uint8_t>(nullptr, 0);
499 }
500 absl::Span<const uint8_t> span() const override { return data_; }
501
502 private:
503 absl::Span<const uint8_t> data_;
504};
505
Brian Silvermanf51499a2020-09-21 12:49:08 -0700506inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
507 absl::Span<const uint8_t> span) {
508 // Copy the data from the span.
509 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
510 memcpy(buf, span.data(), span.size());
511 // Then give it to a DetachedBuffer to manage.
512 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
513 span.size());
514}
515
davidjevans8b9b52f2021-09-17 08:57:30 -0700516// MMap a flatbuffer on disk.
517template <typename T>
518class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> {
519 public:
520 // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk.
Austin Schuhe4d1a682021-10-01 15:04:50 -0700521 FlatbufferMMap(const std::string &flatbuffer_path,
522 util::FileOptions options = util::FileOptions::kReadable) {
523 span_ = util::MMapFile(flatbuffer_path, options);
davidjevans8b9b52f2021-09-17 08:57:30 -0700524 }
525
526 // Copies the reference to the mapped memory.
527 FlatbufferMMap(const FlatbufferMMap &) = default;
528 FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default;
529
530 // Moves the reference to the mapped memory from one pointer to another.
531 FlatbufferMMap(FlatbufferMMap &&) = default;
532 FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default;
533
Austin Schuhe4d1a682021-10-01 15:04:50 -0700534 absl::Span<uint8_t> span() override { return *span_; }
davidjevans8b9b52f2021-09-17 08:57:30 -0700535 absl::Span<const uint8_t> span() const override { return *span_; }
536
537 private:
538 std::shared_ptr<absl::Span<uint8_t>> span_;
539};
540
James Kuszmaul1d5d9ec2024-01-14 17:53:23 -0800541// The regular flatbuffer API makes it surprisingly irritating to unpack an
542// Object into an ObjectT without a bunch of extra lines.
543template <typename T>
544T::NativeTableType UnpackFlatbuffer(const T *fbs) {
545 typename T::NativeTableType object;
546 fbs->UnPackTo(&object);
547 return object;
548}
549
Austin Schuhe93d8642019-10-13 15:27:07 -0700550} // namespace aos
551
552#endif // AOS_FLATBUFFERS_H_