blob: 0a06e0075d204936dd88f0eaad2dc25d83dfc923 [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
22 void deallocate(uint8_t *, size_t) override { is_allocated_ = false; }
23
24 uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t,
25 size_t) override;
26
27 virtual const uint8_t *data() const = 0;
28 virtual uint8_t *data() = 0;
29 virtual size_t size() const = 0;
30
Alex Perrycb7da4b2019-08-28 19:35:56 -070031 void Reset() { is_allocated_ = false; }
Brian Silvermana1652f32020-01-29 20:41:44 -080032 bool is_allocated() const { return is_allocated_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070033
Tyler Chatow116edf12020-01-26 11:52:39 -080034 bool allocated() { return is_allocated_; }
35
Austin Schuh40485ed2019-10-26 21:51:44 -070036 private:
37 bool is_allocated_ = false;
38};
39
40// This class is a fixed memory allocator which holds the data for a flatbuffer
Tyler Chatow116edf12020-01-26 11:52:39 -080041// in a vector.
Austin Schuh40485ed2019-10-26 21:51:44 -070042class FixedAllocator : public FixedAllocatorBase {
43 public:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080044 FixedAllocator(size_t size) : buffer_(size, 0) {}
45
Austin Schuh40485ed2019-10-26 21:51:44 -070046 uint8_t *data() override { return &buffer_[0]; }
47 const uint8_t *data() const override { return &buffer_[0]; }
48 size_t size() const override { return buffer_.size(); }
49
Austin Schuhe84c3ed2019-12-14 15:29:48 -080050 // Releases the data in the buffer.
51 std::vector<uint8_t> release() { return std::move(buffer_); }
52
Austin Schuh40485ed2019-10-26 21:51:44 -070053 private:
Austin Schuhe84c3ed2019-12-14 15:29:48 -080054 std::vector<uint8_t> buffer_;
Austin Schuh40485ed2019-10-26 21:51:44 -070055};
56
Alex Perrycb7da4b2019-08-28 19:35:56 -070057// This class adapts a preallocated memory region to an Allocator.
58class PreallocatedAllocator : public FixedAllocatorBase {
59 public:
60 PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {}
Tyler Chatow116edf12020-01-26 11:52:39 -080061 PreallocatedAllocator(const PreallocatedAllocator &) = delete;
Brian Silvermana1652f32020-01-29 20:41:44 -080062 PreallocatedAllocator(PreallocatedAllocator &&other)
63 : data_(other.data_), size_(other.size_) {
Brian Silverman341b57e2020-06-23 16:23:18 -070064 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080065 CHECK(!other.is_allocated());
Alex Perrycb7da4b2019-08-28 19:35:56 -070066 }
Brian Silvermana1652f32020-01-29 20:41:44 -080067
68 PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete;
69 PreallocatedAllocator &operator=(PreallocatedAllocator &&other) {
Brian Silverman341b57e2020-06-23 16:23:18 -070070 CHECK(!is_allocated()) << ": May not overwrite in-use allocator";
Brian Silvermana1652f32020-01-29 20:41:44 -080071 CHECK(!other.is_allocated());
72 data_ = other.data_;
73 size_ = other.size_;
74 return *this;
75 }
76
77 uint8_t *data() final {
78 return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_));
79 }
80 const uint8_t *data() const final {
81 return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_));
82 }
83 size_t size() const final { return size_; }
Alex Perrycb7da4b2019-08-28 19:35:56 -070084
85 private:
Brian Silvermana1652f32020-01-29 20:41:44 -080086 void *data_;
Alex Perrycb7da4b2019-08-28 19:35:56 -070087 size_t size_;
88};
89
Austin Schuh40485ed2019-10-26 21:51:44 -070090// Base class representing an object which holds the memory representing a root
91// flatbuffer.
92template <typename T>
93class Flatbuffer {
94 public:
Alex Perrycb7da4b2019-08-28 19:35:56 -070095 virtual ~Flatbuffer() {}
Austin Schuh40485ed2019-10-26 21:51:44 -070096 // Returns the MiniReflectTypeTable for T.
97 static const flatbuffers::TypeTable *MiniReflectTypeTable() {
98 return T::MiniReflectTypeTable();
99 }
100
101 // Returns a message from the buffer.
102 const T &message() const {
103 return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data()));
104 }
105 // Returns a mutable message. It can be mutated via the flatbuffer rules.
106 T *mutable_message() {
107 return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data()));
108 }
109
110 virtual const uint8_t *data() const = 0;
111 virtual uint8_t *data() = 0;
112 virtual size_t size() const = 0;
Austin Schuh6f3babe2020-01-26 20:34:50 -0800113
114 absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); }
115 absl::Span<const uint8_t> span() const {
116 return absl::Span<const uint8_t>(data(), size());
117 }
Austin Schuh40485ed2019-10-26 21:51:44 -0700118};
119
Alex Perrycb7da4b2019-08-28 19:35:56 -0700120// String backed flatbuffer.
121template <typename T>
122class FlatbufferString : public Flatbuffer<T> {
123 public:
124 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800125 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700126 // Builds a Flatbuffer by copying the data from the other flatbuffer.
127 FlatbufferString(const Flatbuffer<T> &other) {
Brian Silvermanc8a5b552020-04-28 16:43:59 -0700128 data_ =
129 std::string(reinterpret_cast<const char *>(other.data()), other.size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 }
131
132 // Coppies the data from the other flatbuffer.
133 FlatbufferString &operator=(const Flatbuffer<T> &other) {
134 data_ = std::string(other.data(), other.size());
135 return *this;
136 }
137
138 virtual ~FlatbufferString() override {}
139
140 const uint8_t *data() const override {
141 return reinterpret_cast<const uint8_t *>(data_.data());
142 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800143 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700144 size_t size() const override { return data_.size(); }
145
146 private:
147 std::string data_;
148};
149
Austin Schuhe309d2a2019-11-29 13:25:21 -0800150// Vector backed flatbuffer.
151template <typename T>
152class FlatbufferVector : public Flatbuffer<T> {
153 public:
154 // Builds a Flatbuffer around a vector.
155 FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
156
157 // Builds a Flatbuffer by copying the data from the other flatbuffer.
158 FlatbufferVector(const Flatbuffer<T> &other)
159 : data_(other.data(), other.data() + other.size()) {}
160
Austin Schuh5131bd02020-01-08 17:25:59 -0800161 // Copy constructor.
162 FlatbufferVector(const FlatbufferVector<T> &other)
163 : data_(other.data(), other.data() + other.size()) {}
164
Austin Schuh03803bd2019-12-30 18:08:17 -0800165 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800166 FlatbufferVector(FlatbufferVector<T> &&other)
167 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800168
Austin Schuhe309d2a2019-11-29 13:25:21 -0800169 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800170 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800171 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
172 return *this;
173 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800174 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
175 data_ = std::move(other.data_);
176 return *this;
177 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800178
Austin Schuh05b70472020-01-01 17:11:17 -0800179 // Constructs an empty flatbuffer of type T.
180 static FlatbufferVector<T> Empty() {
181 return FlatbufferVector<T>(std::vector<uint8_t>{});
182 }
183
Austin Schuhe309d2a2019-11-29 13:25:21 -0800184 virtual ~FlatbufferVector() override {}
185
186 const uint8_t *data() const override { return data_.data(); }
187 uint8_t *data() override { return data_.data(); }
188 size_t size() const override { return data_.size(); }
189
190 private:
191 std::vector<uint8_t> data_;
192};
193
Austin Schuhe93d8642019-10-13 15:27:07 -0700194// This object associates the message type with the memory storing the
195// flatbuffer. This only stores root tables.
196//
197// From a usage point of view, pointers to the data are very different than
198// pointers to the tables.
199template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800200class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700201 public:
202 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700203 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700204 : buffer_(::std::move(buffer)) {}
205
206 // Builds a flatbuffer by taking ownership of the buffer from the other
207 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700208 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
209 : buffer_(::std::move(fb.buffer_)) {}
210 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700211 ::std::swap(buffer_, fb.buffer_);
212 return *this;
213 }
214
Alex Perrycb7da4b2019-08-28 19:35:56 -0700215 virtual ~FlatbufferDetachedBuffer() override {}
216
Austin Schuhe93d8642019-10-13 15:27:07 -0700217 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700218 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700219 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800220 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700221 const auto end = fbb.EndTable(fbb.StartTable());
222 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700223 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700224 }
225
226 // Returns references to the buffer, and the data.
227 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700228 const uint8_t *data() const override { return buffer_.data(); }
229 uint8_t *data() override { return buffer_.data(); }
230 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700231
232 private:
233 flatbuffers::DetachedBuffer buffer_;
234};
235
Tyler Chatow116edf12020-01-26 11:52:39 -0800236// Array backed flatbuffer which manages building of the flatbuffer.
237template <typename T, size_t Size>
238class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> {
239 public:
240 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {
241 builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
Austin Schuhd7b15da2020-02-17 15:06:11 -0800242 builder_.ForceDefaults(true);
Tyler Chatow116edf12020-01-26 11:52:39 -0800243 }
244
Brian Silverman341b57e2020-06-23 16:23:18 -0700245 void Reset() {
246 allocator_.Reset();
247 builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
248 builder_.ForceDefaults(true);
249 }
250
Tyler Chatow116edf12020-01-26 11:52:39 -0800251 flatbuffers::FlatBufferBuilder *Builder() {
252 if (allocator_.allocated()) {
253 LOG(FATAL) << "Array backed flatbuffer can only be built once";
254 }
255 return &builder_;
256 }
257
258 void Finish(flatbuffers::Offset<T> root) {
259 if (!allocator_.allocated()) {
260 LOG(FATAL) << "Cannot finish if never building";
261 }
262 builder_.Finish(root);
263 data_ = builder_.GetBufferPointer();
264 size_ = builder_.GetSize();
265 }
266
267 const uint8_t *data() const override {
268 CHECK_NOTNULL(data_);
269 return data_;
270 }
271 uint8_t *data() override {
272 CHECK_NOTNULL(data_);
273 return data_;
274 }
275 size_t size() const override { return size_; }
276
277 private:
278 std::array<uint8_t, Size> buffer_;
279 PreallocatedAllocator allocator_;
280 flatbuffers::FlatBufferBuilder builder_;
281 uint8_t *data_ = nullptr;
282 size_t size_ = 0;
283
284 DISALLOW_COPY_AND_ASSIGN(FlatbufferFixedAllocatorArray);
285};
286
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800287// This object associates the message type with the memory storing the
288// flatbuffer. This only stores root tables.
289//
290// From a usage point of view, pointers to the data are very different than
291// pointers to the tables.
292template <typename T>
293class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
294 public:
295 // Builds a Flatbuffer by taking ownership of the buffer.
296 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
297 : buffer_(::std::move(buffer)) {
298 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
299 }
300
301 // Builds a flatbuffer by taking ownership of the buffer from the other
302 // flatbuffer.
303 SizePrefixedFlatbufferDetachedBuffer(
304 SizePrefixedFlatbufferDetachedBuffer &&fb)
305 : buffer_(::std::move(fb.buffer_)) {}
306 SizePrefixedFlatbufferDetachedBuffer &operator=(
307 SizePrefixedFlatbufferDetachedBuffer &&fb) {
308 ::std::swap(buffer_, fb.buffer_);
309 return *this;
310 }
311
312 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
313
314 // Returns references to the buffer, and the data.
315 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
316 const uint8_t *data() const override {
317 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
318 }
319 uint8_t *data() override {
320 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
321 }
322 size_t size() const override {
323 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
324 }
325
326 private:
327 flatbuffers::DetachedBuffer buffer_;
328};
Austin Schuhe93d8642019-10-13 15:27:07 -0700329// TODO(austin): Need a way to get our hands on the max size. Can start with
330// "large" for now.
331
332} // namespace aos
333
334#endif // AOS_FLATBUFFERS_H_