blob: cdf5339c493431bcd112c4d2ab92afd7b3e26c23 [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"
Brian Silverman354697a2020-09-22 21:06:32 -07008#include "aos/containers/resizeable_buffer.h"
Tyler Chatow116edf12020-01-26 11:52:39 -08009#include "aos/macros.h"
davidjevans8b9b52f2021-09-17 08:57:30 -070010#include "aos/util/file.h"
Austin Schuhb8075812020-10-19 09:36:49 -070011#include "flatbuffers/flatbuffers.h" // IWYU pragma: export
Austin Schuhe84c3ed2019-12-14 15:29:48 -080012#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070013
14namespace aos {
15
Austin Schuh40485ed2019-10-26 21:51:44 -070016// This class is a base class for all sizes of array backed allocators.
17class FixedAllocatorBase : public flatbuffers::Allocator {
18 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080019 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
20
Austin Schuh40485ed2019-10-26 21:51:44 -070021 // TODO(austin): Read the contract for these.
22 uint8_t *allocate(size_t) override;
23
Brian Silverman1715d472020-08-12 22:54:15 -070024 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
25 DCHECK_LE(allocated_size, size());
26 DCHECK_EQ(allocated_data, data());
27 CHECK(is_allocated_);
28 is_allocated_ = false;
29 }
Austin Schuh40485ed2019-10-26 21:51:44 -070030
31 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
32 size_t) override;
33
34 virtual const uint8_t *data() const = 0;
35 virtual uint8_t *data() = 0;
36 virtual size_t size() const = 0;
37
Brian Silverman1715d472020-08-12 22:54:15 -070038 void Reset() {
39 CHECK(!is_allocated_);
40 is_allocated_ = false;
41 }
Brian Silvermana1652f32020-01-29 20:41:44 -080042 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070043
Tyler Chatow116edf12020-01-26 11:52:39 -080044 bool allocated() { return is_allocated_; }
45
Austin Schuh40485ed2019-10-26 21:51:44 -070046 private:
47 bool is_allocated_ = false;
48};
49
50// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080051// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070052class FixedAllocator : public FixedAllocatorBase {
53 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080054 FixedAllocator(size_t size) : buffer_(size, 0) {}
55
Austin Schuh40485ed2019-10-26 21:51:44 -070056 uint8_t *data() override { return &buffer_[0]; }
57 const uint8_t *data() const override { return &buffer_[0]; }
58 size_t size() const override { return buffer_.size(); }
59
Austin Schuhe84c3ed2019-12-14 15:29:48 -080060 // Releases the data in the buffer.
61 std::vector<uint8_t> release() { return std::move(buffer_); }
62
Austin Schuh40485ed2019-10-26 21:51:44 -070063 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080064 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070065};
66
Alex Perrycb7da4b2019-08-28 19:35:56 -070067// This class adapts a preallocated memory region to an Allocator.
68class PreallocatedAllocator : public FixedAllocatorBase {
69 public:
70 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080071 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080072 PreallocatedAllocator(PreallocatedAllocator &&other)
73 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070074 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080075 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070076 }
Brian Silvermana1652f32020-01-29 20:41:44 -080077
78 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
79 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070080 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080081 CHECK(!other.is_allocated());
82 data_ = other.data_;
83 size_ = other.size_;
84 return *this;
85 }
86
87 uint8_t *data() final {
88 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
89 }
90 const uint8_t *data() const final {
91 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
92 }
93 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070094
95 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080096 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070097 size_t size_;
98};
99
Austin Schuh40485ed2019-10-26 21:51:44 -0700100// Base class representing an object which holds the memory representing a root
101// flatbuffer.
102template <typename T>
103class Flatbuffer {
104 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700105 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700106 // Returns the MiniReflectTypeTable for T.
107 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
108 return T::MiniReflectTypeTable();
109 }
110
111 // Returns a message from the buffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800112 virtual const T &message() const = 0;
Austin Schuh40485ed2019-10-26 21:51:44 -0700113 // Returns a mutable message. It can be mutated via the flatbuffer rules.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800114 virtual T *mutable_message() = 0;
Austin Schuh39580f12020-08-01 14:44:08 -0700115
116 // Wipes out the data buffer. This is handy to mark an instance as freed, and
117 // make attempts to use it fail more obviously.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800118 void Wipe() { memset(span().data(), 0, span().size()); }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800119
Austin Schuhadd6eb32020-11-09 21:24:26 -0800120 bool Verify() const {
Austin Schuh977a5ed2020-12-02 23:20:04 -0800121 if (span().size() < 4u) {
122 return false;
123 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800124 flatbuffers::Verifier v(span().data(), span().size());
Austin Schuha4fc60f2020-11-01 23:06:47 -0800125 return v.VerifyTable(&message());
126 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800127
128 protected:
129 virtual absl::Span<uint8_t> span() = 0;
130 virtual absl::Span<const uint8_t> span() const = 0;
131};
132
133// Base class for non-size prefixed flatbuffers. span() means different things
134// across the 2 types, so you end up with a different GetRoot.
135template <typename T>
136class NonSizePrefixedFlatbuffer : public Flatbuffer<T> {
137 public:
138 const T &message() const override {
139 return *flatbuffers::GetRoot<T>(
140 reinterpret_cast<const void *>(this->span().data()));
141 }
142 T *mutable_message() override {
143 return flatbuffers::GetMutableRoot<T>(
144 reinterpret_cast<void *>(this->span().data()));
145 }
146
147 absl::Span<uint8_t> span() override = 0;
148 absl::Span<const uint8_t> span() const override = 0;
Austin Schuha4fc60f2020-11-01 23:06:47 -0800149};
150
151// Non-owning Span backed flatbuffer.
152template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800153class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800154 public:
155 // Builds a flatbuffer pointing to the contents of a span.
156 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
157 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800158 FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) {
159 data_ = other.span();
160 }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800161
162 // Copies the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800163 FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) {
Austin Schuha4fc60f2020-11-01 23:06:47 -0800164 data_ = other.span();
165 return *this;
166 }
167
168 virtual ~FlatbufferSpan() override {}
169
Austin Schuhadd6eb32020-11-09 21:24:26 -0800170 absl::Span<uint8_t> span() override {
171 LOG(FATAL) << "Unimplemented";
172 return absl::Span<uint8_t>(nullptr, 0);
Austin Schuha4fc60f2020-11-01 23:06:47 -0800173 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800174 absl::Span<const uint8_t> span() const override { return data_; }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800175
176 private:
177 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700178};
179
Austin Schuhadd6eb32020-11-09 21:24:26 -0800180// ResizeableBuffer backed flatbuffer.
Austin Schuhe309d2a2019-11-29 13:25:21 -0800181template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800182class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800183 public:
Austin Schuhadd6eb32020-11-09 21:24:26 -0800184 // Builds a Flatbuffer around a ResizeableBuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700185 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800186
187 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800188 FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) {
189 data_.resize(other.span().size());
190 memcpy(data_.data(), other.span().data(), data_.size());
Brian Silverman354697a2020-09-22 21:06:32 -0700191 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800192
Austin Schuh5131bd02020-01-08 17:25:59 -0800193 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700194 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800195
Austin Schuh03803bd2019-12-30 18:08:17 -0800196 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800197 FlatbufferVector(FlatbufferVector<T> &&other)
198 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800199
Austin Schuhe309d2a2019-11-29 13:25:21 -0800200 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800201 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700202 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800203 return *this;
204 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800205 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
206 data_ = std::move(other.data_);
207 return *this;
208 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800209
Austin Schuh05b70472020-01-01 17:11:17 -0800210 // Constructs an empty flatbuffer of type T.
211 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700212 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800213 }
214
Austin Schuhe309d2a2019-11-29 13:25:21 -0800215 virtual ~FlatbufferVector() override {}
216
Austin Schuhadd6eb32020-11-09 21:24:26 -0800217 absl::Span<uint8_t> span() override {
218 return absl::Span<uint8_t>(data_.data(), data_.size());
219 }
220 absl::Span<const uint8_t> span() const override {
221 return absl::Span<const uint8_t>(data_.data(), data_.size());
222 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800223
224 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700225 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800226};
227
Austin Schuhe93d8642019-10-13 15:27:07 -0700228// This object associates the message type with the memory storing the
229// flatbuffer. This only stores root tables.
230//
231// From a usage point of view, pointers to the data are very different than
232// pointers to the tables.
233template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800234class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700235 public:
236 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700237 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700238 : buffer_(::std::move(buffer)) {}
239
240 // Builds a flatbuffer by taking ownership of the buffer from the other
241 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700242 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
243 : buffer_(::std::move(fb.buffer_)) {}
244 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700245 ::std::swap(buffer_, fb.buffer_);
246 return *this;
247 }
248
Alex Perrycb7da4b2019-08-28 19:35:56 -0700249 virtual ~FlatbufferDetachedBuffer() override {}
250
Austin Schuhe93d8642019-10-13 15:27:07 -0700251 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700252 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700253 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800254 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700255 const auto end = fbb.EndTable(fbb.StartTable());
256 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700257 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700258 }
259
260 // Returns references to the buffer, and the data.
261 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800262
263 absl::Span<uint8_t> span() override {
264 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
265 }
266 absl::Span<const uint8_t> span() const override {
267 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
268 }
Austin Schuhe93d8642019-10-13 15:27:07 -0700269
270 private:
271 flatbuffers::DetachedBuffer buffer_;
272};
273
Tyler Chatow116edf12020-01-26 11:52:39 -0800274// Array backed flatbuffer which manages building of the flatbuffer.
275template <typename T, size_t Size>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800276class FlatbufferFixedAllocatorArray final
277 : public NonSizePrefixedFlatbuffer<T> {
Tyler Chatow116edf12020-01-26 11:52:39 -0800278 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700279 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
280
281 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
Austin Schuhadd6eb32020-11-09 21:24:26 -0800282 void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete;
Brian Silverman1715d472020-08-12 22:54:15 -0700283
Austin Schuhadd6eb32020-11-09 21:24:26 -0800284 void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) {
Brian Silverman1715d472020-08-12 22:54:15 -0700285 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
Austin Schuh12e77842020-11-11 20:02:55 -0800286 CHECK_LE(other.span().size(), Size)
287 << ": Source flatbuffer is larger than the target.";
288 memcpy(buffer_.begin(), other.span().data(), other.span().size());
Brian Silverman1715d472020-08-12 22:54:15 -0700289 data_ = buffer_.begin();
Austin Schuh12e77842020-11-11 20:02:55 -0800290 size_ = other.span().size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800291 }
292
Brian Silverman341b57e2020-06-23 16:23:18 -0700293 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700294 CHECK(!allocator_.is_allocated() || data_ != nullptr)
295 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700296 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
297 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700298 data_ = nullptr;
299 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700300 }
301
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700302 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700303 CHECK(!allocator_.allocated())
304 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700305 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
306 fbb_.ForceDefaults(true);
307 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800308 }
309
310 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700311 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700312 fbb_.Finish(root);
313 data_ = fbb_.GetBufferPointer();
314 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700315 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800316 }
317
Austin Schuhadd6eb32020-11-09 21:24:26 -0800318 absl::Span<uint8_t> span() override {
319 return absl::Span<uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800320 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800321 absl::Span<const uint8_t> span() const override {
322 return absl::Span<const uint8_t>(data_, size_);
Tyler Chatow116edf12020-01-26 11:52:39 -0800323 }
Tyler Chatow116edf12020-01-26 11:52:39 -0800324
325 private:
326 std::array<uint8_t, Size> buffer_;
327 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700328 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800329 uint8_t *data_ = nullptr;
330 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800331};
332
Austin Schuhadd6eb32020-11-09 21:24:26 -0800333template <typename T>
334class SizePrefixedFlatbuffer : public Flatbuffer<T> {
335 public:
336 const T &message() const override {
337 return *flatbuffers::GetSizePrefixedRoot<T>(
338 reinterpret_cast<const void *>(this->span().data()));
339 }
340
341 T *mutable_message() override {
342 return flatbuffers::GetMutableSizePrefixedRoot<T>(
343 reinterpret_cast<void *>(this->span().data()));
344 }
345
346 absl::Span<uint8_t> span() override = 0;
347 absl::Span<const uint8_t> span() const override = 0;
348};
349
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800350// This object associates the message type with the memory storing the
351// flatbuffer. This only stores root tables.
352//
353// From a usage point of view, pointers to the data are very different than
354// pointers to the tables.
355template <typename T>
Austin Schuhadd6eb32020-11-09 21:24:26 -0800356class SizePrefixedFlatbufferDetachedBuffer final
357 : public SizePrefixedFlatbuffer<T> {
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800358 public:
359 // Builds a Flatbuffer by taking ownership of the buffer.
360 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
361 : buffer_(::std::move(buffer)) {
362 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
363 }
364
365 // Builds a flatbuffer by taking ownership of the buffer from the other
366 // flatbuffer.
367 SizePrefixedFlatbufferDetachedBuffer(
368 SizePrefixedFlatbufferDetachedBuffer &&fb)
369 : buffer_(::std::move(fb.buffer_)) {}
370 SizePrefixedFlatbufferDetachedBuffer &operator=(
371 SizePrefixedFlatbufferDetachedBuffer &&fb) {
372 ::std::swap(buffer_, fb.buffer_);
373 return *this;
374 }
375
376 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
377
Austin Schuh2f8fd752020-09-01 22:38:28 -0700378 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
379 flatbuffers::FlatBufferBuilder fbb;
380 fbb.ForceDefaults(true);
381 const auto end = fbb.EndTable(fbb.StartTable());
382 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
383 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
384 }
385
Austin Schuh8c399962020-12-25 21:51:45 -0800386 flatbuffers::DetachedBuffer Release() {
387 flatbuffers::FlatBufferBuilder fbb;
388 fbb.ForceDefaults(true);
389 const auto end = fbb.EndTable(fbb.StartTable());
Austin Schuhfd7b6312021-07-30 18:26:51 -0700390 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh8c399962020-12-25 21:51:45 -0800391 flatbuffers::DetachedBuffer result = fbb.Release();
392 std::swap(result, buffer_);
393 return result;
394 }
395
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800396 // Returns references to the buffer, and the data.
Austin Schuhadd6eb32020-11-09 21:24:26 -0800397 absl::Span<uint8_t> span() override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700398 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
399 }
Austin Schuhadd6eb32020-11-09 21:24:26 -0800400 absl::Span<const uint8_t> span() const override {
Austin Schuh2f8fd752020-09-01 22:38:28 -0700401 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
402 }
403
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800404 private:
405 flatbuffers::DetachedBuffer buffer_;
406};
Austin Schuhe93d8642019-10-13 15:27:07 -0700407
Austin Schuhadd6eb32020-11-09 21:24:26 -0800408// ResizeableBuffer backed flatbuffer.
409template <typename T>
410class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> {
411 public:
412 // Builds a Flatbuffer around a ResizeableBuffer.
413 SizePrefixedFlatbufferVector(ResizeableBuffer &&data)
414 : data_(std::move(data)) {}
415
416 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Austin Schuhb929c4e2021-07-12 15:32:53 -0700417 SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other)
418 : SizePrefixedFlatbufferVector(other.span()) {}
419
420 // Builds a flatbuffer by copying the data from the provided span.
421 SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) {
422 data_.resize(span.size());
423 memcpy(data_.data(), span.data(), data_.size());
Austin Schuhadd6eb32020-11-09 21:24:26 -0800424 }
425
426 // Copy constructor.
427 SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other)
428 : data_(other.data_) {}
429
430 // Move constructor.
431 SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other)
432 : data_(std::move(other.data_)) {}
433
434 // Copies the data from the other flatbuffer.
435 SizePrefixedFlatbufferVector &operator=(
436 const SizePrefixedFlatbufferVector<T> &other) {
437 data_ = other.data_;
438 return *this;
439 }
440 SizePrefixedFlatbufferVector &operator=(
441 SizePrefixedFlatbufferVector<T> &&other) {
442 data_ = std::move(other.data_);
443 return *this;
444 }
445
446 // Constructs an empty flatbuffer of type T.
447 static SizePrefixedFlatbufferVector<T> Empty() {
448 return SizePrefixedFlatbufferVector<T>(ResizeableBuffer());
449 }
450
451 virtual ~SizePrefixedFlatbufferVector() override {}
452
453 absl::Span<uint8_t> span() override {
454 return absl::Span<uint8_t>(data_.data(), data_.size());
455 }
456 absl::Span<const uint8_t> span() const override {
457 return absl::Span<const uint8_t>(data_.data(), data_.size());
458 }
459
460 private:
461 ResizeableBuffer data_;
462};
463
Austin Schuh4b5c22a2020-11-30 22:58:43 -0800464// Non-owning Span backed flatbuffer.
465template <typename T>
466class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> {
467 public:
468 // Builds a flatbuffer pointing to the contents of a span.
469 SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data)
470 : data_(data) {}
471 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
472 SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) {
473 data_ = other.span();
474 }
475
476 // Points to the data in the other flatbuffer.
477 SizePrefixedFlatbufferSpan &operator=(
478 const SizePrefixedFlatbuffer<T> &other) {
479 data_ = other.span();
480 return *this;
481 }
482
483 ~SizePrefixedFlatbufferSpan() override {}
484
485 absl::Span<uint8_t> span() override {
486 LOG(FATAL) << "Unimplemented";
487 return absl::Span<uint8_t>(nullptr, 0);
488 }
489 absl::Span<const uint8_t> span() const override { return data_; }
490
491 private:
492 absl::Span<const uint8_t> data_;
493};
494
Brian Silvermanf51499a2020-09-21 12:49:08 -0700495inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
496 absl::Span<const uint8_t> span) {
497 // Copy the data from the span.
498 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
499 memcpy(buf, span.data(), span.size());
500 // Then give it to a DetachedBuffer to manage.
501 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
502 span.size());
503}
504
davidjevans8b9b52f2021-09-17 08:57:30 -0700505// MMap a flatbuffer on disk.
506template <typename T>
507class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> {
508 public:
509 // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk.
Austin Schuhe4d1a682021-10-01 15:04:50 -0700510 FlatbufferMMap(const std::string &flatbuffer_path,
511 util::FileOptions options = util::FileOptions::kReadable) {
512 span_ = util::MMapFile(flatbuffer_path, options);
davidjevans8b9b52f2021-09-17 08:57:30 -0700513 }
514
515 // Copies the reference to the mapped memory.
516 FlatbufferMMap(const FlatbufferMMap &) = default;
517 FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default;
518
519 // Moves the reference to the mapped memory from one pointer to another.
520 FlatbufferMMap(FlatbufferMMap &&) = default;
521 FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default;
522
Austin Schuhe4d1a682021-10-01 15:04:50 -0700523 absl::Span<uint8_t> span() override { return *span_; }
davidjevans8b9b52f2021-09-17 08:57:30 -0700524 absl::Span<const uint8_t> span() const override { return *span_; }
525
526 private:
527 std::shared_ptr<absl::Span<uint8_t>> span_;
528};
529
Austin Schuhe93d8642019-10-13 15:27:07 -0700530} // namespace aos
531
532#endif // AOS_FLATBUFFERS_H_