blob: 4aa0ff85b2586bd0c9b28f49e83825f866b0d4a1 [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 Schuh39580f12020-08-01 14:44:08 -0700118
119 // Wipes out the data buffer. This is handy to mark an instance as freed, and
120 // make attempts to use it fail more obviously.
121 void Wipe() { memset(data(), 0, size()); }
Austin Schuh40485ed2019-10-26 21:51:44 -0700122};
123
Alex Perrycb7da4b2019-08-28 19:35:56 -0700124// String backed flatbuffer.
125template <typename T>
126class FlatbufferString : public Flatbuffer<T> {
127 public:
128 // Builds a flatbuffer using the contents of the string.
James Kuszmaul3ae42262019-11-08 12:33:41 -0800129 FlatbufferString(const std::string_view data) : data_(data) {}
Alex Perrycb7da4b2019-08-28 19:35:56 -0700130 // Builds a Flatbuffer by copying the data from the other flatbuffer.
131 FlatbufferString(const Flatbuffer<T> &other) {
Brian Silvermanc8a5b552020-04-28 16:43:59 -0700132 data_ =
133 std::string(reinterpret_cast<const char *>(other.data()), other.size());
Alex Perrycb7da4b2019-08-28 19:35:56 -0700134 }
135
136 // Coppies the data from the other flatbuffer.
137 FlatbufferString &operator=(const Flatbuffer<T> &other) {
138 data_ = std::string(other.data(), other.size());
139 return *this;
140 }
141
142 virtual ~FlatbufferString() override {}
143
144 const uint8_t *data() const override {
145 return reinterpret_cast<const uint8_t *>(data_.data());
146 }
James Kuszmaul872efd22019-12-03 20:59:09 -0800147 uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); }
Alex Perrycb7da4b2019-08-28 19:35:56 -0700148 size_t size() const override { return data_.size(); }
149
150 private:
151 std::string data_;
152};
153
Austin Schuhe309d2a2019-11-29 13:25:21 -0800154// Vector backed flatbuffer.
155template <typename T>
156class FlatbufferVector : public Flatbuffer<T> {
157 public:
158 // Builds a Flatbuffer around a vector.
159 FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {}
160
161 // Builds a Flatbuffer by copying the data from the other flatbuffer.
162 FlatbufferVector(const Flatbuffer<T> &other)
163 : data_(other.data(), other.data() + other.size()) {}
164
Austin Schuh5131bd02020-01-08 17:25:59 -0800165 // Copy constructor.
166 FlatbufferVector(const FlatbufferVector<T> &other)
167 : data_(other.data(), other.data() + other.size()) {}
168
Austin Schuh03803bd2019-12-30 18:08:17 -0800169 // Move constructor.
Austin Schuh5131bd02020-01-08 17:25:59 -0800170 FlatbufferVector(FlatbufferVector<T> &&other)
171 : data_(std::move(other.data_)) {}
Austin Schuh03803bd2019-12-30 18:08:17 -0800172
Austin Schuhe309d2a2019-11-29 13:25:21 -0800173 // Copies the data from the other flatbuffer.
Austin Schuh5131bd02020-01-08 17:25:59 -0800174 FlatbufferVector &operator=(const FlatbufferVector<T> &other) {
Austin Schuhe309d2a2019-11-29 13:25:21 -0800175 data_ = std::vector<uint8_t>(other.data(), other.data() + other.size());
176 return *this;
177 }
Austin Schuh5131bd02020-01-08 17:25:59 -0800178 FlatbufferVector &operator=(FlatbufferVector<T> &&other) {
179 data_ = std::move(other.data_);
180 return *this;
181 }
Austin Schuhe309d2a2019-11-29 13:25:21 -0800182
Austin Schuh05b70472020-01-01 17:11:17 -0800183 // Constructs an empty flatbuffer of type T.
184 static FlatbufferVector<T> Empty() {
185 return FlatbufferVector<T>(std::vector<uint8_t>{});
186 }
187
Austin Schuhe309d2a2019-11-29 13:25:21 -0800188 virtual ~FlatbufferVector() override {}
189
190 const uint8_t *data() const override { return data_.data(); }
191 uint8_t *data() override { return data_.data(); }
192 size_t size() const override { return data_.size(); }
193
194 private:
195 std::vector<uint8_t> data_;
196};
197
Austin Schuhe93d8642019-10-13 15:27:07 -0700198// This object associates the message type with the memory storing the
199// flatbuffer. This only stores root tables.
200//
201// From a usage point of view, pointers to the data are very different than
202// pointers to the tables.
203template <typename T>
James Kuszmaul3ae42262019-11-08 12:33:41 -0800204class FlatbufferDetachedBuffer final : public Flatbuffer<T> {
Austin Schuhe93d8642019-10-13 15:27:07 -0700205 public:
206 // Builds a Flatbuffer by taking ownership of the buffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700207 FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
Austin Schuhe93d8642019-10-13 15:27:07 -0700208 : buffer_(::std::move(buffer)) {}
209
210 // Builds a flatbuffer by taking ownership of the buffer from the other
211 // flatbuffer.
Austin Schuh40485ed2019-10-26 21:51:44 -0700212 FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb)
213 : buffer_(::std::move(fb.buffer_)) {}
214 FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) {
Austin Schuhe93d8642019-10-13 15:27:07 -0700215 ::std::swap(buffer_, fb.buffer_);
216 return *this;
217 }
218
Alex Perrycb7da4b2019-08-28 19:35:56 -0700219 virtual ~FlatbufferDetachedBuffer() override {}
220
Austin Schuhe93d8642019-10-13 15:27:07 -0700221 // Constructs an empty flatbuffer of type T.
Austin Schuh40485ed2019-10-26 21:51:44 -0700222 static FlatbufferDetachedBuffer<T> Empty() {
Austin Schuhe93d8642019-10-13 15:27:07 -0700223 flatbuffers::FlatBufferBuilder fbb;
Austin Schuhd7b15da2020-02-17 15:06:11 -0800224 fbb.ForceDefaults(true);
Austin Schuhe93d8642019-10-13 15:27:07 -0700225 const auto end = fbb.EndTable(fbb.StartTable());
226 fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end));
Austin Schuh40485ed2019-10-26 21:51:44 -0700227 return FlatbufferDetachedBuffer<T>(fbb.Release());
Austin Schuhe93d8642019-10-13 15:27:07 -0700228 }
229
230 // Returns references to the buffer, and the data.
231 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
Austin Schuh40485ed2019-10-26 21:51:44 -0700232 const uint8_t *data() const override { return buffer_.data(); }
233 uint8_t *data() override { return buffer_.data(); }
234 size_t size() const override { return buffer_.size(); }
Austin Schuhe93d8642019-10-13 15:27:07 -0700235
236 private:
237 flatbuffers::DetachedBuffer buffer_;
238};
239
Tyler Chatow116edf12020-01-26 11:52:39 -0800240// Array backed flatbuffer which manages building of the flatbuffer.
241template <typename T, size_t Size>
242class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> {
243 public:
244 FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {
245 builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
Austin Schuhd7b15da2020-02-17 15:06:11 -0800246 builder_.ForceDefaults(true);
Tyler Chatow116edf12020-01-26 11:52:39 -0800247 }
248
Brian Silverman341b57e2020-06-23 16:23:18 -0700249 void Reset() {
250 allocator_.Reset();
251 builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_);
252 builder_.ForceDefaults(true);
253 }
254
Tyler Chatow116edf12020-01-26 11:52:39 -0800255 flatbuffers::FlatBufferBuilder *Builder() {
256 if (allocator_.allocated()) {
257 LOG(FATAL) << "Array backed flatbuffer can only be built once";
258 }
259 return &builder_;
260 }
261
262 void Finish(flatbuffers::Offset<T> root) {
263 if (!allocator_.allocated()) {
264 LOG(FATAL) << "Cannot finish if never building";
265 }
266 builder_.Finish(root);
267 data_ = builder_.GetBufferPointer();
268 size_ = builder_.GetSize();
269 }
270
271 const uint8_t *data() const override {
272 CHECK_NOTNULL(data_);
273 return data_;
274 }
275 uint8_t *data() override {
276 CHECK_NOTNULL(data_);
277 return data_;
278 }
279 size_t size() const override { return size_; }
280
281 private:
282 std::array<uint8_t, Size> buffer_;
283 PreallocatedAllocator allocator_;
284 flatbuffers::FlatBufferBuilder builder_;
285 uint8_t *data_ = nullptr;
286 size_t size_ = 0;
287
288 DISALLOW_COPY_AND_ASSIGN(FlatbufferFixedAllocatorArray);
289};
290
Austin Schuhe84c3ed2019-12-14 15:29:48 -0800291// This object associates the message type with the memory storing the
292// flatbuffer. This only stores root tables.
293//
294// From a usage point of view, pointers to the data are very different than
295// pointers to the tables.
296template <typename T>
297class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> {
298 public:
299 // Builds a Flatbuffer by taking ownership of the buffer.
300 SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer)
301 : buffer_(::std::move(buffer)) {
302 CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t));
303 }
304
305 // Builds a flatbuffer by taking ownership of the buffer from the other
306 // flatbuffer.
307 SizePrefixedFlatbufferDetachedBuffer(
308 SizePrefixedFlatbufferDetachedBuffer &&fb)
309 : buffer_(::std::move(fb.buffer_)) {}
310 SizePrefixedFlatbufferDetachedBuffer &operator=(
311 SizePrefixedFlatbufferDetachedBuffer &&fb) {
312 ::std::swap(buffer_, fb.buffer_);
313 return *this;
314 }
315
316 virtual ~SizePrefixedFlatbufferDetachedBuffer() override {}
317
318 // Returns references to the buffer, and the data.
319 const flatbuffers::DetachedBuffer &buffer() const { return buffer_; }
320 const uint8_t *data() const override {
321 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
322 }
323 uint8_t *data() override {
324 return buffer_.data() + sizeof(flatbuffers::uoffset_t);
325 }
326 size_t size() const override {
327 return buffer_.size() - sizeof(flatbuffers::uoffset_t);
328 }
329
330 private:
331 flatbuffers::DetachedBuffer buffer_;
332};
Austin Schuhe93d8642019-10-13 15:27:07 -0700333// TODO(austin): Need a way to get our hands on the max size. Can start with
334// "large" for now.
335
336} // namespace aos
337
338#endif // AOS_FLATBUFFERS_H_