blob: 21dd30c450157a00aa798af98866ec262fe15e8e [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"
Austin Schuhb8075812020-10-19 09:36:49 -070010#include "flatbuffers/flatbuffers.h" // IWYU pragma: export
Austin Schuhe84c3ed2019-12-14 15:29:48 -080011#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070012
13namespace aos {
14
Austin Schuh40485ed2019-10-26 21:51:44 -070015// This class is a base class for all sizes of array backed allocators.
16class FixedAllocatorBase : public flatbuffers::Allocator {
17 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080018 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
19
Austin Schuh40485ed2019-10-26 21:51:44 -070020 // TODO(austin): Read the contract for these.
21 uint8_t *allocate(size_t) override;
22
Brian Silverman1715d472020-08-12 22:54:15 -070023 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
24 DCHECK_LE(allocated_size, size());
25 DCHECK_EQ(allocated_data, data());
26 CHECK(is_allocated_);
27 is_allocated_ = false;
28 }
Austin Schuh40485ed2019-10-26 21:51:44 -070029
30 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
31 size_t) override;
32
33 virtual const uint8_t *data() const = 0;
34 virtual uint8_t *data() = 0;
35 virtual size_t size() const = 0;
36
Brian Silverman1715d472020-08-12 22:54:15 -070037 void Reset() {
38 CHECK(!is_allocated_);
39 is_allocated_ = false;
40 }
Brian Silvermana1652f32020-01-29 20:41:44 -080041 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070042
Tyler Chatow116edf12020-01-26 11:52:39 -080043 bool allocated() { return is_allocated_; }
44
Austin Schuh40485ed2019-10-26 21:51:44 -070045 private:
46 bool is_allocated_ = false;
47};
48
49// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080050// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070051class FixedAllocator : public FixedAllocatorBase {
52 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080053 FixedAllocator(size_t size) : buffer_(size, 0) {}
54
Austin Schuh40485ed2019-10-26 21:51:44 -070055 uint8_t *data() override { return &buffer_[0]; }
56 const uint8_t *data() const override { return &buffer_[0]; }
57 size_t size() const override { return buffer_.size(); }
58
Austin Schuhe84c3ed2019-12-14 15:29:48 -080059 // Releases the data in the buffer.
60 std::vector<uint8_t> release() { return std::move(buffer_); }
61
Austin Schuh40485ed2019-10-26 21:51:44 -070062 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080063 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070064};
65
Alex Perrycb7da4b2019-08-28 19:35:56 -070066// This class adapts a preallocated memory region to an Allocator.
67class PreallocatedAllocator : public FixedAllocatorBase {
68 public:
69 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080070 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080071 PreallocatedAllocator(PreallocatedAllocator &&other)
72 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070073 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080074 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070075 }
Brian Silvermana1652f32020-01-29 20:41:44 -080076
77 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
78 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070079 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080080 CHECK(!other.is_allocated());
81 data_ = other.data_;
82 size_ = other.size_;
83 return *this;
84 }
85
86 uint8_t *data() final {
87 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
88 }
89 const uint8_t *data() const final {
90 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
91 }
92 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070093
94 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080095 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070096 size_t size_;
97};
98
Austin Schuh40485ed2019-10-26 21:51:44 -070099// Base class representing an object which holds the memory representing a root
100// flatbuffer.
101template <typename T>
102class Flatbuffer {
103 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700104 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700105 // Returns the MiniReflectTypeTable for T.
106 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
107 return T::MiniReflectTypeTable();
108 }
109
110 // Returns a message from the buffer.
111 const T &message() const {
112 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
113 }
114 // Returns a mutable message. It can be mutated via the flatbuffer rules.
115 T *mutable_message() {
116 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
117 }
118
119 virtual const uint8_t *data() const = 0;
120 virtual uint8_t *data() = 0;
121 virtual size_t size() const = 0;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800122
123 absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); }
124 absl::Span<const uint8_t> span() const {
125 return absl::Span<const uint8_t>(data(), size());
126 }
Austin Schuh39580f12020-08-01 14:44:08 -0700127
128 // Wipes out the data buffer. This is handy to mark an instance as freed, and
129 // make attempts to use it fail more obviously.
130 void Wipe() { memset(data(), 0, size()); }
Austin Schuha4fc60f2020-11-01 23:06:47 -0800131
132 virtual bool Verify() const {
133 flatbuffers::Verifier v(data(), size());
134 return v.VerifyTable(&message());
135 }
136};
137
138// Non-owning Span backed flatbuffer.
139template <typename T>
140class FlatbufferSpan : public Flatbuffer<T> {
141 public:
142 // Builds a flatbuffer pointing to the contents of a span.
143 FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {}
144 // Builds a Flatbuffer pointing to the contents of another flatbuffer.
145 FlatbufferSpan(const Flatbuffer<T> &other) { data_ = other.span(); }
146
147 // Copies the data from the other flatbuffer.
148 FlatbufferSpan &operator=(const Flatbuffer<T> &other) {
149 data_ = other.span();
150 return *this;
151 }
152
153 virtual ~FlatbufferSpan() override {}
154
155 const uint8_t *data() const override {
156 return data_.data();
157 }
158 uint8_t *data() override { return CHECK_NOTNULL(nullptr); }
159 size_t size() const override { return data_.size(); }
160
161 private:
162 absl::Span<const uint8_t> data_;
Austin Schuh40485ed2019-10-26 21:51:44 -0700163};
164
Alex Perrycb7da4b2019-08-28 19:35:56 -0700165// String backed flatbuffer.
166template <typename T>
167class FlatbufferString : public Flatbuffer<T> {
168 public:
169 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800170 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700171 // Builds a Flatbuffer by copying the data from the other flatbuffer.
172 FlatbufferString(const Flatbuffer<T> &other) {
Brian Silvermanc8a5b552020-04-28 16:43:59 -0700173 data_ =
174 std::string(reinterpret_cast<const char *>(other.data()), other.size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700175 }
176
Jim Ostrowski46d25232020-09-07 18:44:17 -0700177 // Copies the data from the other flatbuffer.
Alex Perrycb7da4b2019-08-28 19:35:56 -0700178 FlatbufferString &operator=(const Flatbuffer<T> &other) {
179 data_ = std::string(other.data(), other.size());
180 return *this;
181 }
182
183 virtual ~FlatbufferString() override {}
184
185 const uint8_t *data() const override {
186 return reinterpret_cast<const uint8_t *>(data_.data());
187 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800188 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700189 size_t size() const override { return data_.size(); }
190
191 private:
192 std::string data_;
193};
194
Austin Schuhe309d2a2019-11-29 13:25:21 -0800195// Vector backed flatbuffer.
196template <typename T>
197class FlatbufferVector : public Flatbuffer<T> {
198 public:
199 // Builds a Flatbuffer around a vector.
Brian Silverman354697a2020-09-22 21:06:32 -0700200 FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {}
Austin Schuhe309d2a2019-11-29 13:25:21 -0800201
202 // Builds a Flatbuffer by copying the data from the other flatbuffer.
Brian Silverman354697a2020-09-22 21:06:32 -0700203 FlatbufferVector(const Flatbuffer<T> &other) {
204 data_.resize(other.size());
205 memcpy(data_.data(), other.data(), data_.size());
206 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800207
Austin Schuh5131bd02020-01-08 17:25:59 -0800208 // Copy constructor.
Brian Silverman354697a2020-09-22 21:06:32 -0700209 FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {}
Austin Schuh5131bd02020-01-08 17:25:59 -0800210
Austin Schuh03803bd2019-12-30 18:08:17 -0800211 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800212 FlatbufferVector(FlatbufferVector<T> &&other)
213 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800214
Austin Schuhe309d2a2019-11-29 13:25:21 -0800215 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800216 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Brian Silverman354697a2020-09-22 21:06:32 -0700217 data_ = other.data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800218 return *this;
219 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800220 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
221 data_ = std::move(other.data_);
222 return *this;
223 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800224
Austin Schuh05b70472020-01-01 17:11:17 -0800225 // Constructs an empty flatbuffer of type T.
226 static FlatbufferVector<T> Empty() {
Brian Silverman354697a2020-09-22 21:06:32 -0700227 return FlatbufferVector<T>(ResizeableBuffer());
Austin Schuh05b70472020-01-01 17:11:17 -0800228 }
229
Austin Schuhe309d2a2019-11-29 13:25:21 -0800230 virtual ~FlatbufferVector() override {}
231
232 const uint8_t *data() const override { return data_.data(); }
233 uint8_t *data() override { return data_.data(); }
234 size_t size() const override { return data_.size(); }
235
236 private:
Brian Silverman354697a2020-09-22 21:06:32 -0700237 ResizeableBuffer data_;
Austin Schuhe309d2a2019-11-29 13:25:21 -0800238};
239
Austin Schuhe93d8642019-10-13 15:27:07 -0700240// This object associates the message type with the memory storing the
241// flatbuffer. This only stores root tables.
242//
243// From a usage point of view, pointers to the data are very different than
244// pointers to the tables.
245template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800246class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700247 public:
248 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700249 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700250 : buffer_(::std::move(buffer)) {}
251
252 // Builds a flatbuffer by taking ownership of the buffer from the other
253 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700254 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
255 : buffer_(::std::move(fb.buffer_)) {}
256 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700257 ::std::swap(buffer_, fb.buffer_);
258 return *this;
259 }
260
Alex Perrycb7da4b2019-08-28 19:35:56 -0700261 virtual ~FlatbufferDetachedBuffer() override {}
262
Austin Schuhe93d8642019-10-13 15:27:07 -0700263 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700264 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700265 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800266 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700267 const auto end = fbb.EndTable(fbb.StartTable());
268 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700269 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700270 }
271
272 // Returns references to the buffer, and the data.
273 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700274 const uint8_t *data() const override { return buffer_.data(); }
275 uint8_t *data() override { return buffer_.data(); }
276 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700277
278 private:
279 flatbuffers::DetachedBuffer buffer_;
280};
281
Tyler Chatow116edf12020-01-26 11:52:39 -0800282// Array backed flatbuffer which manages building of the flatbuffer.
283template <typename T, size_t Size>
284class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> {
285 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700286 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
287
288 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
289 void operator=(const Flatbuffer<T> &) = delete;
290
291 void CopyFrom(const Flatbuffer<T> &other) {
292 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
293 memcpy(buffer_.begin(), other.data(), other.size());
294 data_ = buffer_.begin();
295 size_ = other.size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800296 }
297
Brian Silverman341b57e2020-06-23 16:23:18 -0700298 void Reset() {
Austin Schuh9dac21b2020-10-19 10:02:48 -0700299 CHECK(!allocator_.is_allocated() || data_ != nullptr)
300 << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700301 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
302 fbb_.ForceDefaults(true);
Austin Schuh9dac21b2020-10-19 10:02:48 -0700303 data_ = nullptr;
304 size_ = 0;
Brian Silverman341b57e2020-06-23 16:23:18 -0700305 }
306
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700307 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700308 CHECK(!allocator_.allocated())
309 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700310 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
311 fbb_.ForceDefaults(true);
312 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800313 }
314
315 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700316 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700317 fbb_.Finish(root);
318 data_ = fbb_.GetBufferPointer();
319 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700320 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800321 }
322
323 const uint8_t *data() const override {
324 CHECK_NOTNULL(data_);
325 return data_;
326 }
327 uint8_t *data() override {
328 CHECK_NOTNULL(data_);
329 return data_;
330 }
331 size_t size() const override { return size_; }
332
333 private:
334 std::array<uint8_t, Size> buffer_;
335 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700336 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800337 uint8_t *data_ = nullptr;
338 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800339};
340
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800341// This object associates the message type with the memory storing the
342// flatbuffer. This only stores root tables.
343//
344// From a usage point of view, pointers to the data are very different than
345// pointers to the tables.
346template <typename T>
347class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
348 public:
349 // Builds a Flatbuffer by taking ownership of the buffer.
350 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
351 : buffer_(::std::move(buffer)) {
352 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
353 }
354
355 // Builds a flatbuffer by taking ownership of the buffer from the other
356 // flatbuffer.
357 SizePrefixedFlatbufferDetachedBuffer(
358 SizePrefixedFlatbufferDetachedBuffer &&fb)
359 : buffer_(::std::move(fb.buffer_)) {}
360 SizePrefixedFlatbufferDetachedBuffer &operator=(
361 SizePrefixedFlatbufferDetachedBuffer &&fb) {
362 ::std::swap(buffer_, fb.buffer_);
363 return *this;
364 }
365
366 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
367
Austin Schuh2f8fd752020-09-01 22:38:28 -0700368 static SizePrefixedFlatbufferDetachedBuffer<T> Empty() {
369 flatbuffers::FlatBufferBuilder fbb;
370 fbb.ForceDefaults(true);
371 const auto end = fbb.EndTable(fbb.StartTable());
372 fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end));
373 return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release());
374 }
375
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800376 // Returns references to the buffer, and the data.
377 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
378 const uint8_t *data() const override {
379 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
380 }
381 uint8_t *data() override {
382 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
383 }
384 size_t size() const override {
385 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
386 }
387
Austin Schuh2f8fd752020-09-01 22:38:28 -0700388 absl::Span<uint8_t> full_span() {
389 return absl::Span<uint8_t>(buffer_.data(), buffer_.size());
390 }
391 absl::Span<const uint8_t> full_span() const {
392 return absl::Span<const uint8_t>(buffer_.data(), buffer_.size());
393 }
394
Austin Schuha4fc60f2020-11-01 23:06:47 -0800395 bool Verify() const override {
396 // TODO(austin): Should we push full_span up to Flatbuffer<> class?
397 // The base pointer has the wrong alignment if we strip off the size.
398 flatbuffers::Verifier v(full_span().data(), full_span().size());
399 return v.VerifyTable(&this->message());
400 }
401
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800402 private:
403 flatbuffers::DetachedBuffer buffer_;
404};
Austin Schuhe93d8642019-10-13 15:27:07 -0700405
Brian Silvermanf51499a2020-09-21 12:49:08 -0700406inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer(
407 absl::Span<const uint8_t> span) {
408 // Copy the data from the span.
409 uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size());
410 memcpy(buf, span.data(), span.size());
411 // Then give it to a DetachedBuffer to manage.
412 return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf,
413 span.size());
414}
415
Austin Schuhe93d8642019-10-13 15:27:07 -0700416} // namespace aos
417
418#endif // AOS_FLATBUFFERS_H_