blob: f1ed62691add57a3d6df271d02525115ea8f60d0 [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"
Austin Schuh3c9f92c2024-04-30 17:56:42 -070012#include "aos/ipc_lib/data_alignment.h"
Tyler Chatow116edf12020-01-26 11:52:39 -080013#include "aos/macros.h"
davidjevans8b9b52f2021-09-17 08:57:30 -070014#include "aos/util/file.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070015
16namespace aos {
17
Austin Schuh40485ed2019-10-26 21:51:44 -070018// This class is a base class for all sizes of array backed allocators.
19class FixedAllocatorBase : public flatbuffers::Allocator {
20 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080021 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
22
Austin Schuh40485ed2019-10-26 21:51:44 -070023 // TODO(austin): Read the contract for these.
24 uint8_t *allocate(size_t) override;
25
Brian Silverman1715d472020-08-12 22:54:15 -070026 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
27 DCHECK_LE(allocated_size, size());
28 DCHECK_EQ(allocated_data, data());
29 CHECK(is_allocated_);
30 is_allocated_ = false;
31 }
Austin Schuh40485ed2019-10-26 21:51:44 -070032
33 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
34 size_t) override;
35
36 virtual const uint8_t *data() const = 0;
37 virtual uint8_t *data() = 0;
38 virtual size_t size() const = 0;
39
Brian Silverman1715d472020-08-12 22:54:15 -070040 void Reset() {
41 CHECK(!is_allocated_);
42 is_allocated_ = false;
43 }
Brian Silvermana1652f32020-01-29 20:41:44 -080044 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070045
Tyler Chatow116edf12020-01-26 11:52:39 -080046 bool allocated() { return is_allocated_; }
47
Austin Schuh40485ed2019-10-26 21:51:44 -070048 private:
49 bool is_allocated_ = false;
50};
51
52// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080053// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070054class FixedAllocator : public FixedAllocatorBase {
55 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080056 FixedAllocator(size_t size) : buffer_(size, 0) {}
57
Austin Schuh40485ed2019-10-26 21:51:44 -070058 uint8_t *data() override { return &buffer_[0]; }
59 const uint8_t *data() const override { return &buffer_[0]; }
60 size_t size() const override { return buffer_.size(); }
61
Austin Schuhe84c3ed2019-12-14 15:29:48 -080062 // Releases the data in the buffer.
63 std::vector<uint8_t> release() { return std::move(buffer_); }
64
Austin Schuh40485ed2019-10-26 21:51:44 -070065 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080066 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070067};
68
Alex Perrycb7da4b2019-08-28 19:35:56 -070069// This class adapts a preallocated memory region to an Allocator.
70class PreallocatedAllocator : public FixedAllocatorBase {
71 public:
72 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080073 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080074 PreallocatedAllocator(PreallocatedAllocator &&other)
75 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070076 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080077 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070078 }
Brian Silvermana1652f32020-01-29 20:41:44 -080079
80 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
81 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070082 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080083 CHECK(!other.is_allocated());
84 data_ = other.data_;
85 size_ = other.size_;
86 return *this;
87 }
88
89 uint8_t *data() final {
Austin Schuh6bdcc372024-06-27 14:49:11 -070090 CHECK(data_ != nullptr);
91 return reinterpret_cast<uint8_t *>(data_);
Brian Silvermana1652f32020-01-29 20:41:44 -080092 }
93 const uint8_t *data() const final {
Austin Schuh6bdcc372024-06-27 14:49:11 -070094 CHECK(data_ != nullptr);
95 return reinterpret_cast<const uint8_t *>(data_);
Brian Silvermana1652f32020-01-29 20:41:44 -080096 }
97 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070098
99 private:
Brian Silvermana1652f32020-01-29 20:41:44 -0800100 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -0700101 size_t size_;
102};
103
Austin Schuh40485ed2019-10-26 21:51:44 -0700104// Base class representing an object which holds the memory representing a root
105// flatbuffer.
106template <typename T>
107class Flatbuffer {
108 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700109 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700110 // Returns the MiniReflectTypeTable for T.
111 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
112 return T::MiniReflectTypeTable();
113 }
114
115 // Returns a message from the buffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800116 virtual const T &message() const = 0;
Austin Schuh40485ed2019-10-26 21:51:44 -0700117 // Returns a mutable message. It can be mutated via the flatbuffer rules.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800118 virtual T *mutable_message() = 0;
Austin Schuh39580f12020-08-01 14:44:08 -0700119
120 // Wipes out the data buffer. This is handy to mark an instance as freed, and
121 // make attempts to use it fail more obviously.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800122 void Wipe() { memset(span().data(), 0, span().size()); }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800123
James Kuszmaul38babac2024-01-25 14:35:08 -0800124 // Returns true if the flatbuffer is valid. Returns false if either:
125 // * The flatbuffer is incorrectly constructed (e.g., it points to memory
126 // locations outside of the current memory buffer).
127 // * The flatbuffer is too complex, and the flatbuffer verifier chosen to bail
128 // when attempting to traverse the tree of tables.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800129 bool Verify() const {
Austin Schuh977a5ed2020-12-02 23:20:04 -0800130 if (span().size() < 4u) {
131 return false;
132 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800133 flatbuffers::Verifier v(span().data(), span().size());
Austin Schuha4fc60f2020-11-01 23:06:47 -0800134 return v.VerifyTable(&message());
135 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800136
137 protected:
138 virtual absl::Span<uint8_t> span() = 0;
139 virtual absl::Span<const uint8_t> span() const = 0;
140};
141
142// Base class for non-size prefixed flatbuffers. span() means different things
143// across the 2 types, so you end up with a different GetRoot.
144template <typename T>
145class NonSizePrefixedFlatbuffer : public Flatbuffer<T> {
146 public:
147 const T &message() const override {
148 return *flatbuffers::GetRoot<T>(
149 reinterpret_cast<const void *>(this->span().data()));
150 }
151 T *mutable_message() override {
152 return flatbuffers::GetMutableRoot<T>(
153 reinterpret_cast<void *>(this->span().data()));
154 }
155
156 absl::Span<uint8_t> span() override = 0;
157 absl::Span<const uint8_t> span() const override = 0;
Austin Schuha4fc60f2020-11-01 23:06:47 -0800158};
159
160// Non-owning Span backed flatbuffer.
161template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800162class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800163 public:
164 // Builds a flatbuffer pointing to the contents of a span.
165 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
166 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800167 FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) {
168 data_ = other.span();
169 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800170
171 // Copies the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800172 FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800173 data_ = other.span();
174 return *this;
175 }
176
177 virtual ~FlatbufferSpan() override {}
178
Austin Schuhadd6eb32020-11-09 21:24:26 -0800179 absl::Span<uint8_t> span() override {
180 LOG(FATAL) << "Unimplemented";
181 return absl::Span<uint8_t>(nullptr, 0);
Austin Schuha4fc60f2020-11-01 23:06:47 -0800182 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800183 absl::Span<const uint8_t> span() const override { return data_; }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800184
185 private:
186 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700187};
188
Austin Schuhadd6eb32020-11-09 21:24:26 -0800189// ResizeableBuffer backed flatbuffer.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800190template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800191class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800192 public:
Austin Schuhadd6eb32020-11-09 21:24:26 -0800193 // Builds a Flatbuffer around a ResizeableBuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700194 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800195
196 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800197 FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) {
198 data_.resize(other.span().size());
Stephan Pleines9dac6772023-06-01 13:21:41 -0700199 CHECK(other.span().data());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800200 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700201 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800202
Austin Schuh5131bd02020-01-08 17:25:59 -0800203 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700204 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800205
Austin Schuh03803bd2019-12-30 18:08:17 -0800206 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800207 FlatbufferVector(FlatbufferVector<T> &&other)
208 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800209
Austin Schuhe309d2a2019-11-29 13:25:21 -0800210 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800211 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700212 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800213 return *this;
214 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800215 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
216 data_ = std::move(other.data_);
217 return *this;
218 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800219
Austin Schuh05b70472020-01-01 17:11:17 -0800220 // Constructs an empty flatbuffer of type T.
221 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700222 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800223 }
224
Austin Schuhe309d2a2019-11-29 13:25:21 -0800225 virtual ~FlatbufferVector() override {}
226
Austin Schuhadd6eb32020-11-09 21:24:26 -0800227 absl::Span<uint8_t> span() override {
228 return absl::Span<uint8_t>(data_.data(), data_.size());
229 }
230 absl::Span<const uint8_t> span() const override {
231 return absl::Span<const uint8_t>(data_.data(), data_.size());
232 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800233
234 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700235 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800236};
237
Austin Schuhe93d8642019-10-13 15:27:07 -0700238// This object associates the message type with the memory storing the
239// flatbuffer. This only stores root tables.
240//
241// From a usage point of view, pointers to the data are very different than
242// pointers to the tables.
243template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800244class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700245 public:
246 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700247 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700248 : buffer_(::std::move(buffer)) {}
249
250 // Builds a flatbuffer by taking ownership of the buffer from the other
251 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700252 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
253 : buffer_(::std::move(fb.buffer_)) {}
254 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700255 ::std::swap(buffer_, fb.buffer_);
256 return *this;
257 }
258
Alex Perrycb7da4b2019-08-28 19:35:56 -0700259 virtual ~FlatbufferDetachedBuffer() override {}
260
Austin Schuhe93d8642019-10-13 15:27:07 -0700261 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700262 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700263 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800264 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700265 const auto end = fbb.EndTable(fbb.StartTable());
266 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700267 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700268 }
269
270 // Returns references to the buffer, and the data.
271 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800272
273 absl::Span<uint8_t> span() override {
274 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
275 }
276 absl::Span<const uint8_t> span() const override {
277 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
278 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700279
280 private:
281 flatbuffers::DetachedBuffer buffer_;
282};
283
Tyler Chatow116edf12020-01-26 11:52:39 -0800284// Array backed flatbuffer which manages building of the flatbuffer.
285template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800286class FlatbufferFixedAllocatorArray final
287 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800288 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700289 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
290
291 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800292 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700293
Austin Schuhadd6eb32020-11-09 21:24:26 -0800294 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700295 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800296 CHECK_LE(other.span().size(), Size)
297 << ": Source flatbuffer is larger than the target.";
298 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700299 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800300 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800301 }
302
Brian Silverman341b57e2020-06-23 16:23:18 -0700303 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700304 CHECK(!allocator_.is_allocated() || data_ != nullptr)
305 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700306 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
307 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700308 data_ = nullptr;
309 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700310 }
311
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700312 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700313 CHECK(!allocator_.allocated())
314 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700315 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
316 fbb_.ForceDefaults(true);
317 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800318 }
319
320 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700321 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700322 fbb_.Finish(root);
323 data_ = fbb_.GetBufferPointer();
324 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700325 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800326 }
327
Austin Schuhadd6eb32020-11-09 21:24:26 -0800328 absl::Span<uint8_t> span() override {
329 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800330 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800331 absl::Span<const uint8_t> span() const override {
332 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800333 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800334
335 private:
336 std::array<uint8_t, Size> buffer_;
337 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700338 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800339 uint8_t *data_ = nullptr;
340 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800341};
342
Austin Schuhadd6eb32020-11-09 21:24:26 -0800343template <typename T>
344class SizePrefixedFlatbuffer : public Flatbuffer<T> {
345 public:
346 const T &message() const override {
347 return *flatbuffers::GetSizePrefixedRoot<T>(
348 reinterpret_cast<const void *>(this->span().data()));
349 }
350
351 T *mutable_message() override {
352 return flatbuffers::GetMutableSizePrefixedRoot<T>(
353 reinterpret_cast<void *>(this->span().data()));
354 }
355
356 absl::Span<uint8_t> span() override = 0;
357 absl::Span<const uint8_t> span() const override = 0;
358};
359
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800360// This object associates the message type with the memory storing the
361// flatbuffer. This only stores root tables.
362//
363// From a usage point of view, pointers to the data are very different than
364// pointers to the tables.
365template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800366class SizePrefixedFlatbufferDetachedBuffer final
367 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800368 public:
369 // Builds a Flatbuffer by taking ownership of the buffer.
370 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
371 : buffer_(::std::move(buffer)) {
372 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
373 }
374
375 // Builds a flatbuffer by taking ownership of the buffer from the other
376 // flatbuffer.
377 SizePrefixedFlatbufferDetachedBuffer(
378 SizePrefixedFlatbufferDetachedBuffer &&fb)
379 : buffer_(::std::move(fb.buffer_)) {}
380 SizePrefixedFlatbufferDetachedBuffer &operator=(
381 SizePrefixedFlatbufferDetachedBuffer &&fb) {
382 ::std::swap(buffer_, fb.buffer_);
383 return *this;
384 }
385
386 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
387
Austin Schuh2f8fd752020-09-01 22:38:28 -0700388 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
389 flatbuffers::FlatBufferBuilder fbb;
390 fbb.ForceDefaults(true);
391 const auto end = fbb.EndTable(fbb.StartTable());
392 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
393 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
394 }
395
Austin Schuh8c399962020-12-25 21:51:45 -0800396 flatbuffers::DetachedBuffer Release() {
397 flatbuffers::FlatBufferBuilder fbb;
398 fbb.ForceDefaults(true);
399 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700400 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800401 flatbuffers::DetachedBuffer result = fbb.Release();
402 std::swap(result, buffer_);
403 return result;
404 }
405
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800406 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800407 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700408 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
409 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800410 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700411 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
412 }
413
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800414 private:
415 flatbuffers::DetachedBuffer buffer_;
416};
Austin Schuhe93d8642019-10-13 15:27:07 -0700417
Austin Schuhadd6eb32020-11-09 21:24:26 -0800418// ResizeableBuffer backed flatbuffer.
419template <typename T>
420class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
421 public:
422 // Builds a Flatbuffer around a ResizeableBuffer.
423 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
424 : data_(std::move(data)) {}
425
426 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700427 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
428 : SizePrefixedFlatbufferVector(other.span()) {}
429
430 // Builds a flatbuffer by copying the data from the provided span.
431 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
432 data_.resize(span.size());
433 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800434 }
435
436 // Copy constructor.
437 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
438 : data_(other.data_) {}
439
440 // Move constructor.
441 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
442 : data_(std::move(other.data_)) {}
443
444 // Copies the data from the other flatbuffer.
445 SizePrefixedFlatbufferVector &operator=(
446 const SizePrefixedFlatbufferVector<T> &other) {
447 data_ = other.data_;
448 return *this;
449 }
450 SizePrefixedFlatbufferVector &operator=(
451 SizePrefixedFlatbufferVector<T> &&other) {
452 data_ = std::move(other.data_);
453 return *this;
454 }
455
456 // Constructs an empty flatbuffer of type T.
457 static SizePrefixedFlatbufferVector<T> Empty() {
458 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
459 }
460
461 virtual ~SizePrefixedFlatbufferVector() override {}
462
463 absl::Span<uint8_t> span() override {
464 return absl::Span<uint8_t>(data_.data(), data_.size());
465 }
466 absl::Span<const uint8_t> span() const override {
467 return absl::Span<const uint8_t>(data_.data(), data_.size());
468 }
469
470 private:
471 ResizeableBuffer data_;
472};
473
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800474// Non-owning Span backed flatbuffer.
475template <typename T>
476class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
477 public:
478 // Builds a flatbuffer pointing to the contents of a span.
479 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
480 : data_(data) {}
481 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
482 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
483 data_ = other.span();
484 }
485
486 // Points to the data in the other flatbuffer.
487 SizePrefixedFlatbufferSpan &operator=(
488 const SizePrefixedFlatbuffer<T> &other) {
489 data_ = other.span();
490 return *this;
491 }
492
493 ~SizePrefixedFlatbufferSpan() override {}
494
495 absl::Span<uint8_t> span() override {
496 LOG(FATAL) << "Unimplemented";
497 return absl::Span<uint8_t>(nullptr, 0);
498 }
499 absl::Span<const uint8_t> span() const override { return data_; }
500
501 private:
502 absl::Span<const uint8_t> data_;
503};
504
Brian Silvermanf51499a2020-09-21 12:49:08 -0700505inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
506 absl::Span<const uint8_t> span) {
507 // Copy the data from the span.
508 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
509 memcpy(buf, span.data(), span.size());
510 // Then give it to a DetachedBuffer to manage.
511 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
512 span.size());
513}
514
davidjevans8b9b52f2021-09-17 08:57:30 -0700515// MMap a flatbuffer on disk.
516template <typename T>
517class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> {
518 public:
519 // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk.
Austin Schuhe4d1a682021-10-01 15:04:50 -0700520 FlatbufferMMap(const std::string &flatbuffer_path,
521 util::FileOptions options = util::FileOptions::kReadable) {
522 span_ = util::MMapFile(flatbuffer_path, options);
davidjevans8b9b52f2021-09-17 08:57:30 -0700523 }
524
525 // Copies the reference to the mapped memory.
526 FlatbufferMMap(const FlatbufferMMap &) = default;
527 FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default;
528
529 // Moves the reference to the mapped memory from one pointer to another.
530 FlatbufferMMap(FlatbufferMMap &&) = default;
531 FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default;
532
Austin Schuhe4d1a682021-10-01 15:04:50 -0700533 absl::Span<uint8_t> span() override { return *span_; }
davidjevans8b9b52f2021-09-17 08:57:30 -0700534 absl::Span<const uint8_t> span() const override { return *span_; }
535
536 private:
537 std::shared_ptr<absl::Span<uint8_t>> span_;
538};
539
James Kuszmaul1d5d9ec2024-01-14 17:53:23 -0800540// The regular flatbuffer API makes it surprisingly irritating to unpack an
541// Object into an ObjectT without a bunch of extra lines.
542template <typename T>
543T::NativeTableType UnpackFlatbuffer(const T *fbs) {
544 typename T::NativeTableType object;
545 fbs->UnPackTo(&object);
546 return object;
547}
548
Austin Schuhe93d8642019-10-13 15:27:07 -0700549} // namespace aos
550
551#endif // AOS_FLATBUFFERS_H_