blob: ed9c8ac674f3a15e2f6dc2fe4e02687db07fbd70 [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());
Stephan Pleines9dac6772023-06-01 13:21:41 -0700191 CHECK(other.span().data());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800192 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700193 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800194
Austin Schuh5131bd02020-01-08 17:25:59 -0800195 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700196 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800197
Austin Schuh03803bd2019-12-30 18:08:17 -0800198 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800199 FlatbufferVector(FlatbufferVector<T> &&other)
200 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800201
Austin Schuhe309d2a2019-11-29 13:25:21 -0800202 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800203 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700204 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800205 return *this;
206 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800207 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
208 data_ = std::move(other.data_);
209 return *this;
210 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800211
Austin Schuh05b70472020-01-01 17:11:17 -0800212 // Constructs an empty flatbuffer of type T.
213 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700214 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800215 }
216
Austin Schuhe309d2a2019-11-29 13:25:21 -0800217 virtual ~FlatbufferVector() override {}
218
Austin Schuhadd6eb32020-11-09 21:24:26 -0800219 absl::Span<uint8_t> span() override {
220 return absl::Span<uint8_t>(data_.data(), data_.size());
221 }
222 absl::Span<const uint8_t> span() const override {
223 return absl::Span<const uint8_t>(data_.data(), data_.size());
224 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800225
226 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700227 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800228};
229
Austin Schuhe93d8642019-10-13 15:27:07 -0700230// This object associates the message type with the memory storing the
231// flatbuffer. This only stores root tables.
232//
233// From a usage point of view, pointers to the data are very different than
234// pointers to the tables.
235template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800236class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700237 public:
238 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700239 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700240 : buffer_(::std::move(buffer)) {}
241
242 // Builds a flatbuffer by taking ownership of the buffer from the other
243 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700244 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
245 : buffer_(::std::move(fb.buffer_)) {}
246 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700247 ::std::swap(buffer_, fb.buffer_);
248 return *this;
249 }
250
Alex Perrycb7da4b2019-08-28 19:35:56 -0700251 virtual ~FlatbufferDetachedBuffer() override {}
252
Austin Schuhe93d8642019-10-13 15:27:07 -0700253 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700254 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700255 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800256 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700257 const auto end = fbb.EndTable(fbb.StartTable());
258 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700259 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700260 }
261
262 // Returns references to the buffer, and the data.
263 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800264
265 absl::Span<uint8_t> span() override {
266 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
267 }
268 absl::Span<const uint8_t> span() const override {
269 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
270 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700271
272 private:
273 flatbuffers::DetachedBuffer buffer_;
274};
275
Tyler Chatow116edf12020-01-26 11:52:39 -0800276// Array backed flatbuffer which manages building of the flatbuffer.
277template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800278class FlatbufferFixedAllocatorArray final
279 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800280 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700281 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
282
283 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800284 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700285
Austin Schuhadd6eb32020-11-09 21:24:26 -0800286 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700287 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800288 CHECK_LE(other.span().size(), Size)
289 << ": Source flatbuffer is larger than the target.";
290 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700291 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800292 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800293 }
294
Brian Silverman341b57e2020-06-23 16:23:18 -0700295 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700296 CHECK(!allocator_.is_allocated() || data_ != nullptr)
297 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700298 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
299 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700300 data_ = nullptr;
301 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700302 }
303
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700304 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700305 CHECK(!allocator_.allocated())
306 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700307 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
308 fbb_.ForceDefaults(true);
309 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800310 }
311
312 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700313 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700314 fbb_.Finish(root);
315 data_ = fbb_.GetBufferPointer();
316 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700317 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800318 }
319
Austin Schuhadd6eb32020-11-09 21:24:26 -0800320 absl::Span<uint8_t> span() override {
321 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800322 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800323 absl::Span<const uint8_t> span() const override {
324 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800325 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800326
327 private:
328 std::array<uint8_t, Size> buffer_;
329 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700330 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800331 uint8_t *data_ = nullptr;
332 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800333};
334
Austin Schuhadd6eb32020-11-09 21:24:26 -0800335template <typename T>
336class SizePrefixedFlatbuffer : public Flatbuffer<T> {
337 public:
338 const T &message() const override {
339 return *flatbuffers::GetSizePrefixedRoot<T>(
340 reinterpret_cast<const void *>(this->span().data()));
341 }
342
343 T *mutable_message() override {
344 return flatbuffers::GetMutableSizePrefixedRoot<T>(
345 reinterpret_cast<void *>(this->span().data()));
346 }
347
348 absl::Span<uint8_t> span() override = 0;
349 absl::Span<const uint8_t> span() const override = 0;
350};
351
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800352// This object associates the message type with the memory storing the
353// flatbuffer. This only stores root tables.
354//
355// From a usage point of view, pointers to the data are very different than
356// pointers to the tables.
357template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800358class SizePrefixedFlatbufferDetachedBuffer final
359 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800360 public:
361 // Builds a Flatbuffer by taking ownership of the buffer.
362 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
363 : buffer_(::std::move(buffer)) {
364 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
365 }
366
367 // Builds a flatbuffer by taking ownership of the buffer from the other
368 // flatbuffer.
369 SizePrefixedFlatbufferDetachedBuffer(
370 SizePrefixedFlatbufferDetachedBuffer &&fb)
371 : buffer_(::std::move(fb.buffer_)) {}
372 SizePrefixedFlatbufferDetachedBuffer &operator=(
373 SizePrefixedFlatbufferDetachedBuffer &&fb) {
374 ::std::swap(buffer_, fb.buffer_);
375 return *this;
376 }
377
378 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
379
Austin Schuh2f8fd752020-09-01 22:38:28 -0700380 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
381 flatbuffers::FlatBufferBuilder fbb;
382 fbb.ForceDefaults(true);
383 const auto end = fbb.EndTable(fbb.StartTable());
384 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
385 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
386 }
387
Austin Schuh8c399962020-12-25 21:51:45 -0800388 flatbuffers::DetachedBuffer Release() {
389 flatbuffers::FlatBufferBuilder fbb;
390 fbb.ForceDefaults(true);
391 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700392 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800393 flatbuffers::DetachedBuffer result = fbb.Release();
394 std::swap(result, buffer_);
395 return result;
396 }
397
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800398 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800399 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700400 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
401 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800402 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700403 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
404 }
405
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800406 private:
407 flatbuffers::DetachedBuffer buffer_;
408};
Austin Schuhe93d8642019-10-13 15:27:07 -0700409
Austin Schuhadd6eb32020-11-09 21:24:26 -0800410// ResizeableBuffer backed flatbuffer.
411template <typename T>
412class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
413 public:
414 // Builds a Flatbuffer around a ResizeableBuffer.
415 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
416 : data_(std::move(data)) {}
417
418 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700419 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
420 : SizePrefixedFlatbufferVector(other.span()) {}
421
422 // Builds a flatbuffer by copying the data from the provided span.
423 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
424 data_.resize(span.size());
425 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800426 }
427
428 // Copy constructor.
429 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
430 : data_(other.data_) {}
431
432 // Move constructor.
433 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
434 : data_(std::move(other.data_)) {}
435
436 // Copies the data from the other flatbuffer.
437 SizePrefixedFlatbufferVector &operator=(
438 const SizePrefixedFlatbufferVector<T> &other) {
439 data_ = other.data_;
440 return *this;
441 }
442 SizePrefixedFlatbufferVector &operator=(
443 SizePrefixedFlatbufferVector<T> &&other) {
444 data_ = std::move(other.data_);
445 return *this;
446 }
447
448 // Constructs an empty flatbuffer of type T.
449 static SizePrefixedFlatbufferVector<T> Empty() {
450 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
451 }
452
453 virtual ~SizePrefixedFlatbufferVector() override {}
454
455 absl::Span<uint8_t> span() override {
456 return absl::Span<uint8_t>(data_.data(), data_.size());
457 }
458 absl::Span<const uint8_t> span() const override {
459 return absl::Span<const uint8_t>(data_.data(), data_.size());
460 }
461
462 private:
463 ResizeableBuffer data_;
464};
465
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800466// Non-owning Span backed flatbuffer.
467template <typename T>
468class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
469 public:
470 // Builds a flatbuffer pointing to the contents of a span.
471 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
472 : data_(data) {}
473 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
474 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
475 data_ = other.span();
476 }
477
478 // Points to the data in the other flatbuffer.
479 SizePrefixedFlatbufferSpan &operator=(
480 const SizePrefixedFlatbuffer<T> &other) {
481 data_ = other.span();
482 return *this;
483 }
484
485 ~SizePrefixedFlatbufferSpan() override {}
486
487 absl::Span<uint8_t> span() override {
488 LOG(FATAL) << "Unimplemented";
489 return absl::Span<uint8_t>(nullptr, 0);
490 }
491 absl::Span<const uint8_t> span() const override { return data_; }
492
493 private:
494 absl::Span<const uint8_t> data_;
495};
496
Brian Silvermanf51499a2020-09-21 12:49:08 -0700497inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
498 absl::Span<const uint8_t> span) {
499 // Copy the data from the span.
500 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
501 memcpy(buf, span.data(), span.size());
502 // Then give it to a DetachedBuffer to manage.
503 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
504 span.size());
505}
506
davidjevans8b9b52f2021-09-17 08:57:30 -0700507// MMap a flatbuffer on disk.
508template <typename T>
509class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> {
510 public:
511 // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk.
Austin Schuhe4d1a682021-10-01 15:04:50 -0700512 FlatbufferMMap(const std::string &flatbuffer_path,
513 util::FileOptions options = util::FileOptions::kReadable) {
514 span_ = util::MMapFile(flatbuffer_path, options);
davidjevans8b9b52f2021-09-17 08:57:30 -0700515 }
516
517 // Copies the reference to the mapped memory.
518 FlatbufferMMap(const FlatbufferMMap &) = default;
519 FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default;
520
521 // Moves the reference to the mapped memory from one pointer to another.
522 FlatbufferMMap(FlatbufferMMap &&) = default;
523 FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default;
524
Austin Schuhe4d1a682021-10-01 15:04:50 -0700525 absl::Span<uint8_t> span() override { return *span_; }
davidjevans8b9b52f2021-09-17 08:57:30 -0700526 absl::Span<const uint8_t> span() const override { return *span_; }
527
528 private:
529 std::shared_ptr<absl::Span<uint8_t>> span_;
530};
531
Austin Schuhe93d8642019-10-13 15:27:07 -0700532} // namespace aos
533
534#endif // AOS_FLATBUFFERS_H_