Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 1 | #ifndef AOS_FLATBUFFERS_H_ |
| 2 | #define AOS_FLATBUFFERS_H_ |
| 3 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 4 | #include <array> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 5 | #include <string_view> |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 6 | |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 7 | #include "absl/types/span.h" |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 8 | #include "aos/containers/resizeable_buffer.h" |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 9 | #include "aos/macros.h" |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame^] | 10 | #include "aos/util/file.h" |
Austin Schuh | b807581 | 2020-10-19 09:36:49 -0700 | [diff] [blame] | 11 | #include "flatbuffers/flatbuffers.h" // IWYU pragma: export |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 12 | #include "glog/logging.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 13 | |
| 14 | namespace aos { |
| 15 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 16 | // This class is a base class for all sizes of array backed allocators. |
| 17 | class FixedAllocatorBase : public flatbuffers::Allocator { |
| 18 | public: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 19 | ~FixedAllocatorBase() override { CHECK(!is_allocated_); } |
| 20 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 21 | // TODO(austin): Read the contract for these. |
| 22 | uint8_t *allocate(size_t) override; |
| 23 | |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 24 | 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 Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 30 | |
| 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 Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 38 | void Reset() { |
| 39 | CHECK(!is_allocated_); |
| 40 | is_allocated_ = false; |
| 41 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 42 | bool is_allocated() const { return is_allocated_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 43 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 44 | bool allocated() { return is_allocated_; } |
| 45 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 46 | private: |
| 47 | bool is_allocated_ = false; |
| 48 | }; |
| 49 | |
| 50 | // This class is a fixed memory allocator which holds the data for a flatbuffer |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 51 | // in a vector. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 52 | class FixedAllocator : public FixedAllocatorBase { |
| 53 | public: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 54 | FixedAllocator(size_t size) : buffer_(size, 0) {} |
| 55 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 56 | 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 60 | // Releases the data in the buffer. |
| 61 | std::vector<uint8_t> release() { return std::move(buffer_); } |
| 62 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 63 | private: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 64 | std::vector<uint8_t> buffer_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 65 | }; |
| 66 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 67 | // This class adapts a preallocated memory region to an Allocator. |
| 68 | class PreallocatedAllocator : public FixedAllocatorBase { |
| 69 | public: |
| 70 | PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {} |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 71 | PreallocatedAllocator(const PreallocatedAllocator &) = delete; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 72 | PreallocatedAllocator(PreallocatedAllocator &&other) |
| 73 | : data_(other.data_), size_(other.size_) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 74 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 75 | CHECK(!other.is_allocated()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 76 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 77 | |
| 78 | PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete; |
| 79 | PreallocatedAllocator &operator=(PreallocatedAllocator &&other) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 80 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 81 | 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 Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 94 | |
| 95 | private: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 96 | void *data_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 97 | size_t size_; |
| 98 | }; |
| 99 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 100 | // Base class representing an object which holds the memory representing a root |
| 101 | // flatbuffer. |
| 102 | template <typename T> |
| 103 | class Flatbuffer { |
| 104 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 105 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 106 | // 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 112 | virtual const T &message() const = 0; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 113 | // Returns a mutable message. It can be mutated via the flatbuffer rules. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 114 | virtual T *mutable_message() = 0; |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 115 | |
| 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 118 | void Wipe() { memset(span().data(), 0, span().size()); } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 119 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 120 | bool Verify() const { |
Austin Schuh | 977a5ed | 2020-12-02 23:20:04 -0800 | [diff] [blame] | 121 | if (span().size() < 4u) { |
| 122 | return false; |
| 123 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 124 | flatbuffers::Verifier v(span().data(), span().size()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 125 | return v.VerifyTable(&message()); |
| 126 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 127 | |
| 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. |
| 135 | template <typename T> |
| 136 | class 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 Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 149 | }; |
| 150 | |
| 151 | // Non-owning Span backed flatbuffer. |
| 152 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 153 | class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 154 | 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 158 | FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) { |
| 159 | data_ = other.span(); |
| 160 | } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 161 | |
| 162 | // Copies the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 163 | FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 164 | data_ = other.span(); |
| 165 | return *this; |
| 166 | } |
| 167 | |
| 168 | virtual ~FlatbufferSpan() override {} |
| 169 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 170 | absl::Span<uint8_t> span() override { |
| 171 | LOG(FATAL) << "Unimplemented"; |
| 172 | return absl::Span<uint8_t>(nullptr, 0); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 173 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 174 | absl::Span<const uint8_t> span() const override { return data_; } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 175 | |
| 176 | private: |
| 177 | absl::Span<const uint8_t> data_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 178 | }; |
| 179 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 180 | // ResizeableBuffer backed flatbuffer. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 181 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 182 | class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 183 | public: |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 184 | // Builds a Flatbuffer around a ResizeableBuffer. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 185 | FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {} |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 186 | |
| 187 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 188 | FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) { |
| 189 | data_.resize(other.span().size()); |
| 190 | memcpy(data_.data(), other.span().data(), data_.size()); |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 191 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 192 | |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 193 | // Copy constructor. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 194 | FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {} |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 195 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 196 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 197 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 198 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 199 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 200 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 201 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 202 | data_ = other.data_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 203 | return *this; |
| 204 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 205 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 206 | data_ = std::move(other.data_); |
| 207 | return *this; |
| 208 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 209 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 210 | // Constructs an empty flatbuffer of type T. |
| 211 | static FlatbufferVector<T> Empty() { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 212 | return FlatbufferVector<T>(ResizeableBuffer()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 213 | } |
| 214 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 215 | virtual ~FlatbufferVector() override {} |
| 216 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 217 | 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 Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 223 | |
| 224 | private: |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 225 | ResizeableBuffer data_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 226 | }; |
| 227 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 228 | // 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. |
| 233 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 234 | class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 235 | public: |
| 236 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 237 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 238 | : buffer_(::std::move(buffer)) {} |
| 239 | |
| 240 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 241 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 242 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 243 | : buffer_(::std::move(fb.buffer_)) {} |
| 244 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 245 | ::std::swap(buffer_, fb.buffer_); |
| 246 | return *this; |
| 247 | } |
| 248 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 249 | virtual ~FlatbufferDetachedBuffer() override {} |
| 250 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 251 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 252 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 253 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 254 | fbb.ForceDefaults(true); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 255 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 256 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 257 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 258 | } |
| 259 | |
| 260 | // Returns references to the buffer, and the data. |
| 261 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 262 | |
| 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 Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 269 | |
| 270 | private: |
| 271 | flatbuffers::DetachedBuffer buffer_; |
| 272 | }; |
| 273 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 274 | // Array backed flatbuffer which manages building of the flatbuffer. |
| 275 | template <typename T, size_t Size> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 276 | class FlatbufferFixedAllocatorArray final |
| 277 | : public NonSizePrefixedFlatbuffer<T> { |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 278 | public: |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 279 | FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {} |
| 280 | |
| 281 | FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 282 | void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete; |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 283 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 284 | void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 285 | CHECK(!allocator_.is_allocated()) << ": May not overwrite while building"; |
Austin Schuh | 12e7784 | 2020-11-11 20:02:55 -0800 | [diff] [blame] | 286 | 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 Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 289 | data_ = buffer_.begin(); |
Austin Schuh | 12e7784 | 2020-11-11 20:02:55 -0800 | [diff] [blame] | 290 | size_ = other.span().size(); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 291 | } |
| 292 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 293 | void Reset() { |
Austin Schuh | 9dac21b | 2020-10-19 10:02:48 -0700 | [diff] [blame] | 294 | CHECK(!allocator_.is_allocated() || data_ != nullptr) |
| 295 | << ": May not reset while building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 296 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 297 | fbb_.ForceDefaults(true); |
Austin Schuh | 9dac21b | 2020-10-19 10:02:48 -0700 | [diff] [blame] | 298 | data_ = nullptr; |
| 299 | size_ = 0; |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 300 | } |
| 301 | |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 302 | flatbuffers::FlatBufferBuilder *fbb() { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 303 | CHECK(!allocator_.allocated()) |
| 304 | << ": Array backed flatbuffer can only be built once"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 305 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 306 | fbb_.ForceDefaults(true); |
| 307 | return &fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 308 | } |
| 309 | |
| 310 | void Finish(flatbuffers::Offset<T> root) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 311 | CHECK(allocator_.allocated()) << ": Cannot finish if not building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 312 | fbb_.Finish(root); |
| 313 | data_ = fbb_.GetBufferPointer(); |
| 314 | size_ = fbb_.GetSize(); |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 315 | DCHECK_LE(size_, Size); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 316 | } |
| 317 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 318 | absl::Span<uint8_t> span() override { |
| 319 | return absl::Span<uint8_t>(data_, size_); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 320 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 321 | absl::Span<const uint8_t> span() const override { |
| 322 | return absl::Span<const uint8_t>(data_, size_); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 323 | } |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 324 | |
| 325 | private: |
| 326 | std::array<uint8_t, Size> buffer_; |
| 327 | PreallocatedAllocator allocator_; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 328 | flatbuffers::FlatBufferBuilder fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 329 | uint8_t *data_ = nullptr; |
| 330 | size_t size_ = 0; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 331 | }; |
| 332 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 333 | template <typename T> |
| 334 | class 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 Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 350 | // 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. |
| 355 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 356 | class SizePrefixedFlatbufferDetachedBuffer final |
| 357 | : public SizePrefixedFlatbuffer<T> { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 358 | 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 Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 378 | 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 Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 386 | flatbuffers::DetachedBuffer Release() { |
| 387 | flatbuffers::FlatBufferBuilder fbb; |
| 388 | fbb.ForceDefaults(true); |
| 389 | const auto end = fbb.EndTable(fbb.StartTable()); |
Austin Schuh | fd7b631 | 2021-07-30 18:26:51 -0700 | [diff] [blame] | 390 | fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 391 | flatbuffers::DetachedBuffer result = fbb.Release(); |
| 392 | std::swap(result, buffer_); |
| 393 | return result; |
| 394 | } |
| 395 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 396 | // Returns references to the buffer, and the data. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 397 | absl::Span<uint8_t> span() override { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 398 | return absl::Span<uint8_t>(buffer_.data(), buffer_.size()); |
| 399 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 400 | absl::Span<const uint8_t> span() const override { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 401 | return absl::Span<const uint8_t>(buffer_.data(), buffer_.size()); |
| 402 | } |
| 403 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 404 | private: |
| 405 | flatbuffers::DetachedBuffer buffer_; |
| 406 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 407 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 408 | // ResizeableBuffer backed flatbuffer. |
| 409 | template <typename T> |
| 410 | class 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 Schuh | b929c4e | 2021-07-12 15:32:53 -0700 | [diff] [blame] | 417 | 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 Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 424 | } |
| 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 Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 464 | // Non-owning Span backed flatbuffer. |
| 465 | template <typename T> |
| 466 | class 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 Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 495 | inline 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 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame^] | 505 | // MMap a flatbuffer on disk. |
| 506 | template <typename T> |
| 507 | class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> { |
| 508 | public: |
| 509 | // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk. |
| 510 | FlatbufferMMap(const std::string &flatbuffer_path) { |
| 511 | span_ = util::MMapFile(flatbuffer_path); |
| 512 | } |
| 513 | |
| 514 | // Copies the reference to the mapped memory. |
| 515 | FlatbufferMMap(const FlatbufferMMap &) = default; |
| 516 | FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default; |
| 517 | |
| 518 | // Moves the reference to the mapped memory from one pointer to another. |
| 519 | FlatbufferMMap(FlatbufferMMap &&) = default; |
| 520 | FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default; |
| 521 | |
| 522 | absl::Span<uint8_t> span() override { |
| 523 | LOG(FATAL) << "Unimplemented. A flatbuffer is immutable."; |
| 524 | return *span_; |
| 525 | } |
| 526 | absl::Span<const uint8_t> span() const override { return *span_; } |
| 527 | |
| 528 | private: |
| 529 | std::shared_ptr<absl::Span<uint8_t>> span_; |
| 530 | }; |
| 531 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 532 | } // namespace aos |
| 533 | |
| 534 | #endif // AOS_FLATBUFFERS_H_ |