blob: 392a7ebea5860e10b8b4319718f5245f92b292ee [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"
Tyler Chatow116edf12020-01-26 11:52:39 -08008#include "aos/macros.h"
Austin Schuhe93d8642019-10-13 15:27:07 -07009#include "flatbuffers/flatbuffers.h"
Austin Schuhe84c3ed2019-12-14 15:29:48 -080010#include "glog/logging.h"
Austin Schuhe93d8642019-10-13 15:27:07 -070011
12namespace aos {
13
Austin Schuh40485ed2019-10-26 21:51:44 -070014// This class is a base class for all sizes of array backed allocators.
15class FixedAllocatorBase : public flatbuffers::Allocator {
16 public:
Brian Silvermana1652f32020-01-29 20:41:44 -080017 ~FixedAllocatorBase() override { CHECK(!is_allocated_); }
18
Austin Schuh40485ed2019-10-26 21:51:44 -070019 // TODO(austin): Read the contract for these.
20 uint8_t *allocate(size_t) override;
21
Brian Silverman1715d472020-08-12 22:54:15 -070022 void deallocate(uint8_t *allocated_data, size_t allocated_size) override {
23 DCHECK_LE(allocated_size, size());
24 DCHECK_EQ(allocated_data, data());
25 CHECK(is_allocated_);
26 is_allocated_ = false;
27 }
Austin Schuh40485ed2019-10-26 21:51:44 -070028
29 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
30 size_t) override;
31
32 virtual const uint8_t *data() const = 0;
33 virtual uint8_t *data() = 0;
34 virtual size_t size() const = 0;
35
Brian Silverman1715d472020-08-12 22:54:15 -070036 void Reset() {
37 CHECK(!is_allocated_);
38 is_allocated_ = false;
39 }
Brian Silvermana1652f32020-01-29 20:41:44 -080040 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070041
Tyler Chatow116edf12020-01-26 11:52:39 -080042 bool allocated() { return is_allocated_; }
43
Austin Schuh40485ed2019-10-26 21:51:44 -070044 private:
45 bool is_allocated_ = false;
46};
47
48// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080049// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070050class FixedAllocator : public FixedAllocatorBase {
51 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080052 FixedAllocator(size_t size) : buffer_(size, 0) {}
53
Austin Schuh40485ed2019-10-26 21:51:44 -070054 uint8_t *data() override { return &buffer_[0]; }
55 const uint8_t *data() const override { return &buffer_[0]; }
56 size_t size() const override { return buffer_.size(); }
57
Austin Schuhe84c3ed2019-12-14 15:29:48 -080058 // Releases the data in the buffer.
59 std::vector<uint8_t> release() { return std::move(buffer_); }
60
Austin Schuh40485ed2019-10-26 21:51:44 -070061 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080062 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070063};
64
Alex Perrycb7da4b2019-08-28 19:35:56 -070065// This class adapts a preallocated memory region to an Allocator.
66class PreallocatedAllocator : public FixedAllocatorBase {
67 public:
68 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080069 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080070 PreallocatedAllocator(PreallocatedAllocator &&other)
71 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070072 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080073 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070074 }
Brian Silvermana1652f32020-01-29 20:41:44 -080075
76 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
77 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070078 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080079 CHECK(!other.is_allocated());
80 data_ = other.data_;
81 size_ = other.size_;
82 return *this;
83 }
84
85 uint8_t *data() final {
86 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
87 }
88 const uint8_t *data() const final {
89 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
90 }
91 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070092
93 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080094 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 size_t size_;
96};
97
Austin Schuh40485ed2019-10-26 21:51:44 -070098// Base class representing an object which holds the memory representing a root
99// flatbuffer.
100template <typename T>
101class Flatbuffer {
102 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -0700103 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -0700104 // Returns the MiniReflectTypeTable for T.
105 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
106 return T::MiniReflectTypeTable();
107 }
108
109 // Returns a message from the buffer.
110 const T &message() const {
111 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
112 }
113 // Returns a mutable message. It can be mutated via the flatbuffer rules.
114 T *mutable_message() {
115 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
116 }
117
118 virtual const uint8_t *data() const = 0;
119 virtual uint8_t *data() = 0;
120 virtual size_t size() const = 0;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800121
122 absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); }
123 absl::Span<const uint8_t> span() const {
124 return absl::Span<const uint8_t>(data(), size());
125 }
Austin Schuh39580f12020-08-01 14:44:08 -0700126
127 // Wipes out the data buffer. This is handy to mark an instance as freed, and
128 // make attempts to use it fail more obviously.
129 void Wipe() { memset(data(), 0, size()); }
Austin Schuh40485ed2019-10-26 21:51:44 -0700130};
131
Alex Perrycb7da4b2019-08-28 19:35:56 -0700132// String backed flatbuffer.
133template <typename T>
134class FlatbufferString : public Flatbuffer<T> {
135 public:
136 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800137 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700138 // Builds a Flatbuffer by copying the data from the other flatbuffer.
139 FlatbufferString(const Flatbuffer<T> &other) {
Brian Silvermanc8a5b552020-04-28 16:43:59 -0700140 data_ =
141 std::string(reinterpret_cast<const char *>(other.data()), other.size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700142 }
143
144 // Coppies the data from the other flatbuffer.
145 FlatbufferString &operator=(const Flatbuffer<T> &other) {
146 data_ = std::string(other.data(), other.size());
147 return *this;
148 }
149
150 virtual ~FlatbufferString() override {}
151
152 const uint8_t *data() const override {
153 return reinterpret_cast<const uint8_t *>(data_.data());
154 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800155 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700156 size_t size() const override { return data_.size(); }
157
158 private:
159 std::string data_;
160};
161
Austin Schuhe309d2a2019-11-29 13:25:21 -0800162// Vector backed flatbuffer.
163template <typename T>
164class FlatbufferVector : public Flatbuffer<T> {
165 public:
166 // Builds a Flatbuffer around a vector.
167 FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
168
169 // Builds a Flatbuffer by copying the data from the other flatbuffer.
170 FlatbufferVector(const Flatbuffer<T> &other)
171 : data_(other.data(), other.data() + other.size()) {}
172
Austin Schuh5131bd02020-01-08 17:25:59 -0800173 // Copy constructor.
174 FlatbufferVector(const FlatbufferVector<T> &other)
175 : data_(other.data(), other.data() + other.size()) {}
176
Austin Schuh03803bd2019-12-30 18:08:17 -0800177 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800178 FlatbufferVector(FlatbufferVector<T> &&other)
179 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800180
Austin Schuhe309d2a2019-11-29 13:25:21 -0800181 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800182 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800183 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
184 return *this;
185 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800186 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
187 data_ = std::move(other.data_);
188 return *this;
189 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800190
Austin Schuh05b70472020-01-01 17:11:17 -0800191 // Constructs an empty flatbuffer of type T.
192 static FlatbufferVector<T> Empty() {
193 return FlatbufferVector<T>(std::vector<uint8_t>{});
194 }
195
Austin Schuhe309d2a2019-11-29 13:25:21 -0800196 virtual ~FlatbufferVector() override {}
197
198 const uint8_t *data() const override { return data_.data(); }
199 uint8_t *data() override { return data_.data(); }
200 size_t size() const override { return data_.size(); }
201
202 private:
203 std::vector<uint8_t> data_;
204};
205
Austin Schuhe93d8642019-10-13 15:27:07 -0700206// This object associates the message type with the memory storing the
207// flatbuffer. This only stores root tables.
208//
209// From a usage point of view, pointers to the data are very different than
210// pointers to the tables.
211template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800212class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700213 public:
214 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700215 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700216 : buffer_(::std::move(buffer)) {}
217
218 // Builds a flatbuffer by taking ownership of the buffer from the other
219 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700220 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
221 : buffer_(::std::move(fb.buffer_)) {}
222 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700223 ::std::swap(buffer_, fb.buffer_);
224 return *this;
225 }
226
Alex Perrycb7da4b2019-08-28 19:35:56 -0700227 virtual ~FlatbufferDetachedBuffer() override {}
228
Austin Schuhe93d8642019-10-13 15:27:07 -0700229 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700230 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700231 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800232 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700233 const auto end = fbb.EndTable(fbb.StartTable());
234 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700235 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700236 }
237
238 // Returns references to the buffer, and the data.
239 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700240 const uint8_t *data() const override { return buffer_.data(); }
241 uint8_t *data() override { return buffer_.data(); }
242 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700243
244 private:
245 flatbuffers::DetachedBuffer buffer_;
246};
247
Tyler Chatow116edf12020-01-26 11:52:39 -0800248// Array backed flatbuffer which manages building of the flatbuffer.
249template <typename T, size_t Size>
250class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> {
251 public:
Brian Silverman1715d472020-08-12 22:54:15 -0700252 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {}
253
254 FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete;
255 void operator=(const Flatbuffer<T> &) = delete;
256
257 void CopyFrom(const Flatbuffer<T> &other) {
258 CHECK(!allocator_.is_allocated()) << ": May not overwrite while building";
259 memcpy(buffer_.begin(), other.data(), other.size());
260 data_ = buffer_.begin();
261 size_ = other.size();
Tyler Chatow116edf12020-01-26 11:52:39 -0800262 }
263
Brian Silverman341b57e2020-06-23 16:23:18 -0700264 void Reset() {
Brian Silverman1715d472020-08-12 22:54:15 -0700265 CHECK(!allocator_.is_allocated()) << ": May not reset while building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700266 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
267 fbb_.ForceDefaults(true);
Brian Silverman341b57e2020-06-23 16:23:18 -0700268 }
269
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700270 flatbuffers::FlatBufferBuilder *fbb() {
Brian Silverman1715d472020-08-12 22:54:15 -0700271 CHECK(!allocator_.allocated())
272 << ": Array backed flatbuffer can only be built once";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700273 fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
274 fbb_.ForceDefaults(true);
275 return &fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800276 }
277
278 void Finish(flatbuffers::Offset<T> root) {
Brian Silverman1715d472020-08-12 22:54:15 -0700279 CHECK(allocator_.allocated()) << ": Cannot finish if not building";
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700280 fbb_.Finish(root);
281 data_ = fbb_.GetBufferPointer();
282 size_ = fbb_.GetSize();
Brian Silverman1715d472020-08-12 22:54:15 -0700283 DCHECK_LE(size_, Size);
Tyler Chatow116edf12020-01-26 11:52:39 -0800284 }
285
286 const uint8_t *data() const override {
287 CHECK_NOTNULL(data_);
288 return data_;
289 }
290 uint8_t *data() override {
291 CHECK_NOTNULL(data_);
292 return data_;
293 }
294 size_t size() const override { return size_; }
295
296 private:
297 std::array<uint8_t, Size> buffer_;
298 PreallocatedAllocator allocator_;
Austin Schuh1bcd0e72020-08-26 21:50:45 -0700299 flatbuffers::FlatBufferBuilder fbb_;
Tyler Chatow116edf12020-01-26 11:52:39 -0800300 uint8_t *data_ = nullptr;
301 size_t size_ = 0;
Tyler Chatow116edf12020-01-26 11:52:39 -0800302};
303
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800304// This object associates the message type with the memory storing the
305// flatbuffer. This only stores root tables.
306//
307// From a usage point of view, pointers to the data are very different than
308// pointers to the tables.
309template <typename T>
310class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
311 public:
312 // Builds a Flatbuffer by taking ownership of the buffer.
313 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
314 : buffer_(::std::move(buffer)) {
315 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
316 }
317
318 // Builds a flatbuffer by taking ownership of the buffer from the other
319 // flatbuffer.
320 SizePrefixedFlatbufferDetachedBuffer(
321 SizePrefixedFlatbufferDetachedBuffer &&fb)
322 : buffer_(::std::move(fb.buffer_)) {}
323 SizePrefixedFlatbufferDetachedBuffer &operator=(
324 SizePrefixedFlatbufferDetachedBuffer &&fb) {
325 ::std::swap(buffer_, fb.buffer_);
326 return *this;
327 }
328
329 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
330
331 // Returns references to the buffer, and the data.
332 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
333 const uint8_t *data() const override {
334 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
335 }
336 uint8_t *data() override {
337 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
338 }
339 size_t size() const override {
340 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
341 }
342
343 private:
344 flatbuffers::DetachedBuffer buffer_;
345};
Austin Schuhe93d8642019-10-13 15:27:07 -0700346
347} // namespace aos
348
349#endif // AOS_FLATBUFFERS_H_