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" |
Philipp Schrader | 790cb54 | 2023-07-05 21:06:52 -0700 | [diff] [blame] | 8 | #include "flatbuffers/flatbuffers.h" // IWYU pragma: export |
| 9 | #include "glog/logging.h" |
| 10 | |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 11 | #include "aos/containers/resizeable_buffer.h" |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 12 | #include "aos/macros.h" |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 13 | #include "aos/util/file.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 14 | |
| 15 | namespace aos { |
| 16 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 17 | // This class is a base class for all sizes of array backed allocators. |
| 18 | class FixedAllocatorBase : public flatbuffers::Allocator { |
| 19 | public: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 20 | ~FixedAllocatorBase() override { CHECK(!is_allocated_); } |
| 21 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 22 | // TODO(austin): Read the contract for these. |
| 23 | uint8_t *allocate(size_t) override; |
| 24 | |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 25 | void deallocate(uint8_t *allocated_data, size_t allocated_size) override { |
| 26 | DCHECK_LE(allocated_size, size()); |
| 27 | DCHECK_EQ(allocated_data, data()); |
| 28 | CHECK(is_allocated_); |
| 29 | is_allocated_ = false; |
| 30 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 31 | |
| 32 | uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t, |
| 33 | size_t) override; |
| 34 | |
| 35 | virtual const uint8_t *data() const = 0; |
| 36 | virtual uint8_t *data() = 0; |
| 37 | virtual size_t size() const = 0; |
| 38 | |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 39 | void Reset() { |
| 40 | CHECK(!is_allocated_); |
| 41 | is_allocated_ = false; |
| 42 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 43 | bool is_allocated() const { return is_allocated_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 44 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 45 | bool allocated() { return is_allocated_; } |
| 46 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 47 | private: |
| 48 | bool is_allocated_ = false; |
| 49 | }; |
| 50 | |
| 51 | // 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] | 52 | // in a vector. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 53 | class FixedAllocator : public FixedAllocatorBase { |
| 54 | public: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 55 | FixedAllocator(size_t size) : buffer_(size, 0) {} |
| 56 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 57 | uint8_t *data() override { return &buffer_[0]; } |
| 58 | const uint8_t *data() const override { return &buffer_[0]; } |
| 59 | size_t size() const override { return buffer_.size(); } |
| 60 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 61 | // Releases the data in the buffer. |
| 62 | std::vector<uint8_t> release() { return std::move(buffer_); } |
| 63 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 64 | private: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 65 | std::vector<uint8_t> buffer_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 66 | }; |
| 67 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 68 | // This class adapts a preallocated memory region to an Allocator. |
| 69 | class PreallocatedAllocator : public FixedAllocatorBase { |
| 70 | public: |
| 71 | PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {} |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 72 | PreallocatedAllocator(const PreallocatedAllocator &) = delete; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 73 | PreallocatedAllocator(PreallocatedAllocator &&other) |
| 74 | : data_(other.data_), size_(other.size_) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 75 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 76 | CHECK(!other.is_allocated()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 77 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 78 | |
| 79 | PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete; |
| 80 | PreallocatedAllocator &operator=(PreallocatedAllocator &&other) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 81 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 82 | CHECK(!other.is_allocated()); |
| 83 | data_ = other.data_; |
| 84 | size_ = other.size_; |
| 85 | return *this; |
| 86 | } |
| 87 | |
| 88 | uint8_t *data() final { |
| 89 | return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_)); |
| 90 | } |
| 91 | const uint8_t *data() const final { |
| 92 | return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_)); |
| 93 | } |
| 94 | size_t size() const final { return size_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | |
| 96 | private: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 97 | void *data_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 98 | size_t size_; |
| 99 | }; |
| 100 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 101 | // Base class representing an object which holds the memory representing a root |
| 102 | // flatbuffer. |
| 103 | template <typename T> |
| 104 | class Flatbuffer { |
| 105 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 106 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 107 | // Returns the MiniReflectTypeTable for T. |
| 108 | static const flatbuffers::TypeTable *MiniReflectTypeTable() { |
| 109 | return T::MiniReflectTypeTable(); |
| 110 | } |
| 111 | |
| 112 | // Returns a message from the buffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 113 | virtual const T &message() const = 0; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 114 | // Returns a mutable message. It can be mutated via the flatbuffer rules. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 115 | virtual T *mutable_message() = 0; |
Austin Schuh | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 116 | |
| 117 | // Wipes out the data buffer. This is handy to mark an instance as freed, and |
| 118 | // make attempts to use it fail more obviously. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 119 | void Wipe() { memset(span().data(), 0, span().size()); } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 120 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 121 | bool Verify() const { |
Austin Schuh | 977a5ed | 2020-12-02 23:20:04 -0800 | [diff] [blame] | 122 | if (span().size() < 4u) { |
| 123 | return false; |
| 124 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 125 | flatbuffers::Verifier v(span().data(), span().size()); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 126 | return v.VerifyTable(&message()); |
| 127 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 128 | |
| 129 | protected: |
| 130 | virtual absl::Span<uint8_t> span() = 0; |
| 131 | virtual absl::Span<const uint8_t> span() const = 0; |
| 132 | }; |
| 133 | |
| 134 | // Base class for non-size prefixed flatbuffers. span() means different things |
| 135 | // across the 2 types, so you end up with a different GetRoot. |
| 136 | template <typename T> |
| 137 | class NonSizePrefixedFlatbuffer : public Flatbuffer<T> { |
| 138 | public: |
| 139 | const T &message() const override { |
| 140 | return *flatbuffers::GetRoot<T>( |
| 141 | reinterpret_cast<const void *>(this->span().data())); |
| 142 | } |
| 143 | T *mutable_message() override { |
| 144 | return flatbuffers::GetMutableRoot<T>( |
| 145 | reinterpret_cast<void *>(this->span().data())); |
| 146 | } |
| 147 | |
| 148 | absl::Span<uint8_t> span() override = 0; |
| 149 | absl::Span<const uint8_t> span() const override = 0; |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 150 | }; |
| 151 | |
| 152 | // Non-owning Span backed flatbuffer. |
| 153 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 154 | class FlatbufferSpan : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 155 | public: |
| 156 | // Builds a flatbuffer pointing to the contents of a span. |
| 157 | FlatbufferSpan(const absl::Span<const uint8_t> data) : data_(data) {} |
| 158 | // Builds a Flatbuffer pointing to the contents of another flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 159 | FlatbufferSpan(const NonSizePrefixedFlatbuffer<T> &other) { |
| 160 | data_ = other.span(); |
| 161 | } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 162 | |
| 163 | // Copies the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 164 | FlatbufferSpan &operator=(const NonSizePrefixedFlatbuffer<T> &other) { |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 165 | data_ = other.span(); |
| 166 | return *this; |
| 167 | } |
| 168 | |
| 169 | virtual ~FlatbufferSpan() override {} |
| 170 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 171 | absl::Span<uint8_t> span() override { |
| 172 | LOG(FATAL) << "Unimplemented"; |
| 173 | return absl::Span<uint8_t>(nullptr, 0); |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 174 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 175 | absl::Span<const uint8_t> span() const override { return data_; } |
Austin Schuh | a4fc60f | 2020-11-01 23:06:47 -0800 | [diff] [blame] | 176 | |
| 177 | private: |
| 178 | absl::Span<const uint8_t> data_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 181 | // ResizeableBuffer backed flatbuffer. |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 182 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 183 | class FlatbufferVector : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 184 | public: |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 185 | // Builds a Flatbuffer around a ResizeableBuffer. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 186 | FlatbufferVector(ResizeableBuffer &&data) : data_(std::move(data)) {} |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 187 | |
| 188 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 189 | FlatbufferVector(const NonSizePrefixedFlatbuffer<T> &other) { |
| 190 | data_.resize(other.span().size()); |
Stephan Pleines | 9dac677 | 2023-06-01 13:21:41 -0700 | [diff] [blame] | 191 | CHECK(other.span().data()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 192 | memcpy(data_.data(), other.span().data(), data_.size()); |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 193 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 194 | |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 195 | // Copy constructor. |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 196 | FlatbufferVector(const FlatbufferVector<T> &other) : data_(other.data_) {} |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 197 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 198 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 199 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 200 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 201 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 202 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 203 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 204 | data_ = other.data_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 205 | return *this; |
| 206 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 207 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 208 | data_ = std::move(other.data_); |
| 209 | return *this; |
| 210 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 211 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 212 | // Constructs an empty flatbuffer of type T. |
| 213 | static FlatbufferVector<T> Empty() { |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 214 | return FlatbufferVector<T>(ResizeableBuffer()); |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 215 | } |
| 216 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 217 | virtual ~FlatbufferVector() override {} |
| 218 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 219 | absl::Span<uint8_t> span() override { |
| 220 | return absl::Span<uint8_t>(data_.data(), data_.size()); |
| 221 | } |
| 222 | absl::Span<const uint8_t> span() const override { |
| 223 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
| 224 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 225 | |
| 226 | private: |
Brian Silverman | 354697a | 2020-09-22 21:06:32 -0700 | [diff] [blame] | 227 | ResizeableBuffer data_; |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 228 | }; |
| 229 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 230 | // This object associates the message type with the memory storing the |
| 231 | // flatbuffer. This only stores root tables. |
| 232 | // |
| 233 | // From a usage point of view, pointers to the data are very different than |
| 234 | // pointers to the tables. |
| 235 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 236 | class FlatbufferDetachedBuffer final : public NonSizePrefixedFlatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 237 | public: |
| 238 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 239 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 240 | : buffer_(::std::move(buffer)) {} |
| 241 | |
| 242 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 243 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 244 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 245 | : buffer_(::std::move(fb.buffer_)) {} |
| 246 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 247 | ::std::swap(buffer_, fb.buffer_); |
| 248 | return *this; |
| 249 | } |
| 250 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 251 | virtual ~FlatbufferDetachedBuffer() override {} |
| 252 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 253 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 254 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 255 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 256 | fbb.ForceDefaults(true); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 257 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 258 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 259 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 260 | } |
| 261 | |
| 262 | // Returns references to the buffer, and the data. |
| 263 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 264 | |
| 265 | absl::Span<uint8_t> span() override { |
| 266 | return absl::Span<uint8_t>(buffer_.data(), buffer_.size()); |
| 267 | } |
| 268 | absl::Span<const uint8_t> span() const override { |
| 269 | return absl::Span<const uint8_t>(buffer_.data(), buffer_.size()); |
| 270 | } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 271 | |
| 272 | private: |
| 273 | flatbuffers::DetachedBuffer buffer_; |
| 274 | }; |
| 275 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 276 | // Array backed flatbuffer which manages building of the flatbuffer. |
| 277 | template <typename T, size_t Size> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 278 | class FlatbufferFixedAllocatorArray final |
| 279 | : public NonSizePrefixedFlatbuffer<T> { |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 280 | public: |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 281 | FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) {} |
| 282 | |
| 283 | FlatbufferFixedAllocatorArray(const FlatbufferFixedAllocatorArray &) = delete; |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 284 | void operator=(const NonSizePrefixedFlatbuffer<T> &) = delete; |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 285 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 286 | void CopyFrom(const NonSizePrefixedFlatbuffer<T> &other) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 287 | CHECK(!allocator_.is_allocated()) << ": May not overwrite while building"; |
Austin Schuh | 12e7784 | 2020-11-11 20:02:55 -0800 | [diff] [blame] | 288 | CHECK_LE(other.span().size(), Size) |
| 289 | << ": Source flatbuffer is larger than the target."; |
| 290 | memcpy(buffer_.begin(), other.span().data(), other.span().size()); |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 291 | data_ = buffer_.begin(); |
Austin Schuh | 12e7784 | 2020-11-11 20:02:55 -0800 | [diff] [blame] | 292 | size_ = other.span().size(); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 293 | } |
| 294 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 295 | void Reset() { |
Austin Schuh | 9dac21b | 2020-10-19 10:02:48 -0700 | [diff] [blame] | 296 | CHECK(!allocator_.is_allocated() || data_ != nullptr) |
| 297 | << ": May not reset while building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 298 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 299 | fbb_.ForceDefaults(true); |
Austin Schuh | 9dac21b | 2020-10-19 10:02:48 -0700 | [diff] [blame] | 300 | data_ = nullptr; |
| 301 | size_ = 0; |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 302 | } |
| 303 | |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 304 | flatbuffers::FlatBufferBuilder *fbb() { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 305 | CHECK(!allocator_.allocated()) |
| 306 | << ": Array backed flatbuffer can only be built once"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 307 | fbb_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 308 | fbb_.ForceDefaults(true); |
| 309 | return &fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 310 | } |
| 311 | |
| 312 | void Finish(flatbuffers::Offset<T> root) { |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 313 | CHECK(allocator_.allocated()) << ": Cannot finish if not building"; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 314 | fbb_.Finish(root); |
| 315 | data_ = fbb_.GetBufferPointer(); |
| 316 | size_ = fbb_.GetSize(); |
Brian Silverman | 1715d47 | 2020-08-12 22:54:15 -0700 | [diff] [blame] | 317 | DCHECK_LE(size_, Size); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 318 | } |
| 319 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 320 | absl::Span<uint8_t> span() override { |
| 321 | return absl::Span<uint8_t>(data_, size_); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 322 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 323 | absl::Span<const uint8_t> span() const override { |
| 324 | return absl::Span<const uint8_t>(data_, size_); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 325 | } |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 326 | |
| 327 | private: |
| 328 | std::array<uint8_t, Size> buffer_; |
| 329 | PreallocatedAllocator allocator_; |
Austin Schuh | 1bcd0e7 | 2020-08-26 21:50:45 -0700 | [diff] [blame] | 330 | flatbuffers::FlatBufferBuilder fbb_; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 331 | uint8_t *data_ = nullptr; |
| 332 | size_t size_ = 0; |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 333 | }; |
| 334 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 335 | template <typename T> |
| 336 | class SizePrefixedFlatbuffer : public Flatbuffer<T> { |
| 337 | public: |
| 338 | const T &message() const override { |
| 339 | return *flatbuffers::GetSizePrefixedRoot<T>( |
| 340 | reinterpret_cast<const void *>(this->span().data())); |
| 341 | } |
| 342 | |
| 343 | T *mutable_message() override { |
| 344 | return flatbuffers::GetMutableSizePrefixedRoot<T>( |
| 345 | reinterpret_cast<void *>(this->span().data())); |
| 346 | } |
| 347 | |
| 348 | absl::Span<uint8_t> span() override = 0; |
| 349 | absl::Span<const uint8_t> span() const override = 0; |
| 350 | }; |
| 351 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 352 | // This object associates the message type with the memory storing the |
| 353 | // flatbuffer. This only stores root tables. |
| 354 | // |
| 355 | // From a usage point of view, pointers to the data are very different than |
| 356 | // pointers to the tables. |
| 357 | template <typename T> |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 358 | class SizePrefixedFlatbufferDetachedBuffer final |
| 359 | : public SizePrefixedFlatbuffer<T> { |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 360 | public: |
| 361 | // Builds a Flatbuffer by taking ownership of the buffer. |
| 362 | SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
| 363 | : buffer_(::std::move(buffer)) { |
| 364 | CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t)); |
| 365 | } |
| 366 | |
| 367 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 368 | // flatbuffer. |
| 369 | SizePrefixedFlatbufferDetachedBuffer( |
| 370 | SizePrefixedFlatbufferDetachedBuffer &&fb) |
| 371 | : buffer_(::std::move(fb.buffer_)) {} |
| 372 | SizePrefixedFlatbufferDetachedBuffer &operator=( |
| 373 | SizePrefixedFlatbufferDetachedBuffer &&fb) { |
| 374 | ::std::swap(buffer_, fb.buffer_); |
| 375 | return *this; |
| 376 | } |
| 377 | |
| 378 | virtual ~SizePrefixedFlatbufferDetachedBuffer() override {} |
| 379 | |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 380 | static SizePrefixedFlatbufferDetachedBuffer<T> Empty() { |
| 381 | flatbuffers::FlatBufferBuilder fbb; |
| 382 | fbb.ForceDefaults(true); |
| 383 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 384 | fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end)); |
| 385 | return SizePrefixedFlatbufferDetachedBuffer<T>(fbb.Release()); |
| 386 | } |
| 387 | |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 388 | flatbuffers::DetachedBuffer Release() { |
| 389 | flatbuffers::FlatBufferBuilder fbb; |
| 390 | fbb.ForceDefaults(true); |
| 391 | const auto end = fbb.EndTable(fbb.StartTable()); |
Austin Schuh | fd7b631 | 2021-07-30 18:26:51 -0700 | [diff] [blame] | 392 | fbb.FinishSizePrefixed(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 8c39996 | 2020-12-25 21:51:45 -0800 | [diff] [blame] | 393 | flatbuffers::DetachedBuffer result = fbb.Release(); |
| 394 | std::swap(result, buffer_); |
| 395 | return result; |
| 396 | } |
| 397 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 398 | // Returns references to the buffer, and the data. |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 399 | absl::Span<uint8_t> span() override { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 400 | return absl::Span<uint8_t>(buffer_.data(), buffer_.size()); |
| 401 | } |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 402 | absl::Span<const uint8_t> span() const override { |
Austin Schuh | 2f8fd75 | 2020-09-01 22:38:28 -0700 | [diff] [blame] | 403 | return absl::Span<const uint8_t>(buffer_.data(), buffer_.size()); |
| 404 | } |
| 405 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 406 | private: |
| 407 | flatbuffers::DetachedBuffer buffer_; |
| 408 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 409 | |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 410 | // ResizeableBuffer backed flatbuffer. |
| 411 | template <typename T> |
| 412 | class SizePrefixedFlatbufferVector : public SizePrefixedFlatbuffer<T> { |
| 413 | public: |
| 414 | // Builds a Flatbuffer around a ResizeableBuffer. |
| 415 | SizePrefixedFlatbufferVector(ResizeableBuffer &&data) |
| 416 | : data_(std::move(data)) {} |
| 417 | |
| 418 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
Austin Schuh | b929c4e | 2021-07-12 15:32:53 -0700 | [diff] [blame] | 419 | SizePrefixedFlatbufferVector(const SizePrefixedFlatbuffer<T> &other) |
| 420 | : SizePrefixedFlatbufferVector(other.span()) {} |
| 421 | |
| 422 | // Builds a flatbuffer by copying the data from the provided span. |
| 423 | SizePrefixedFlatbufferVector(const absl::Span<const uint8_t> span) { |
| 424 | data_.resize(span.size()); |
| 425 | memcpy(data_.data(), span.data(), data_.size()); |
Austin Schuh | add6eb3 | 2020-11-09 21:24:26 -0800 | [diff] [blame] | 426 | } |
| 427 | |
| 428 | // Copy constructor. |
| 429 | SizePrefixedFlatbufferVector(const SizePrefixedFlatbufferVector<T> &other) |
| 430 | : data_(other.data_) {} |
| 431 | |
| 432 | // Move constructor. |
| 433 | SizePrefixedFlatbufferVector(SizePrefixedFlatbufferVector<T> &&other) |
| 434 | : data_(std::move(other.data_)) {} |
| 435 | |
| 436 | // Copies the data from the other flatbuffer. |
| 437 | SizePrefixedFlatbufferVector &operator=( |
| 438 | const SizePrefixedFlatbufferVector<T> &other) { |
| 439 | data_ = other.data_; |
| 440 | return *this; |
| 441 | } |
| 442 | SizePrefixedFlatbufferVector &operator=( |
| 443 | SizePrefixedFlatbufferVector<T> &&other) { |
| 444 | data_ = std::move(other.data_); |
| 445 | return *this; |
| 446 | } |
| 447 | |
| 448 | // Constructs an empty flatbuffer of type T. |
| 449 | static SizePrefixedFlatbufferVector<T> Empty() { |
| 450 | return SizePrefixedFlatbufferVector<T>(ResizeableBuffer()); |
| 451 | } |
| 452 | |
| 453 | virtual ~SizePrefixedFlatbufferVector() override {} |
| 454 | |
| 455 | absl::Span<uint8_t> span() override { |
| 456 | return absl::Span<uint8_t>(data_.data(), data_.size()); |
| 457 | } |
| 458 | absl::Span<const uint8_t> span() const override { |
| 459 | return absl::Span<const uint8_t>(data_.data(), data_.size()); |
| 460 | } |
| 461 | |
| 462 | private: |
| 463 | ResizeableBuffer data_; |
| 464 | }; |
| 465 | |
Austin Schuh | 4b5c22a | 2020-11-30 22:58:43 -0800 | [diff] [blame] | 466 | // Non-owning Span backed flatbuffer. |
| 467 | template <typename T> |
| 468 | class SizePrefixedFlatbufferSpan : public SizePrefixedFlatbuffer<T> { |
| 469 | public: |
| 470 | // Builds a flatbuffer pointing to the contents of a span. |
| 471 | SizePrefixedFlatbufferSpan(const absl::Span<const uint8_t> data) |
| 472 | : data_(data) {} |
| 473 | // Builds a Flatbuffer pointing to the contents of another flatbuffer. |
| 474 | SizePrefixedFlatbufferSpan(const SizePrefixedFlatbuffer<T> &other) { |
| 475 | data_ = other.span(); |
| 476 | } |
| 477 | |
| 478 | // Points to the data in the other flatbuffer. |
| 479 | SizePrefixedFlatbufferSpan &operator=( |
| 480 | const SizePrefixedFlatbuffer<T> &other) { |
| 481 | data_ = other.span(); |
| 482 | return *this; |
| 483 | } |
| 484 | |
| 485 | ~SizePrefixedFlatbufferSpan() override {} |
| 486 | |
| 487 | absl::Span<uint8_t> span() override { |
| 488 | LOG(FATAL) << "Unimplemented"; |
| 489 | return absl::Span<uint8_t>(nullptr, 0); |
| 490 | } |
| 491 | absl::Span<const uint8_t> span() const override { return data_; } |
| 492 | |
| 493 | private: |
| 494 | absl::Span<const uint8_t> data_; |
| 495 | }; |
| 496 | |
Brian Silverman | f51499a | 2020-09-21 12:49:08 -0700 | [diff] [blame] | 497 | inline flatbuffers::DetachedBuffer CopySpanAsDetachedBuffer( |
| 498 | absl::Span<const uint8_t> span) { |
| 499 | // Copy the data from the span. |
| 500 | uint8_t *buf = flatbuffers::DefaultAllocator().allocate(span.size()); |
| 501 | memcpy(buf, span.data(), span.size()); |
| 502 | // Then give it to a DetachedBuffer to manage. |
| 503 | return flatbuffers::DetachedBuffer(nullptr, false, buf, span.size(), buf, |
| 504 | span.size()); |
| 505 | } |
| 506 | |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 507 | // MMap a flatbuffer on disk. |
| 508 | template <typename T> |
| 509 | class FlatbufferMMap : public NonSizePrefixedFlatbuffer<T> { |
| 510 | public: |
| 511 | // Builds a Flatbuffer by mmaping the data from a flatbuffer saved on disk. |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 512 | FlatbufferMMap(const std::string &flatbuffer_path, |
| 513 | util::FileOptions options = util::FileOptions::kReadable) { |
| 514 | span_ = util::MMapFile(flatbuffer_path, options); |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | // Copies the reference to the mapped memory. |
| 518 | FlatbufferMMap(const FlatbufferMMap &) = default; |
| 519 | FlatbufferMMap &operator=(const FlatbufferMMap<T> &other) = default; |
| 520 | |
| 521 | // Moves the reference to the mapped memory from one pointer to another. |
| 522 | FlatbufferMMap(FlatbufferMMap &&) = default; |
| 523 | FlatbufferMMap &operator=(FlatbufferMMap<T> &&other) = default; |
| 524 | |
Austin Schuh | e4d1a68 | 2021-10-01 15:04:50 -0700 | [diff] [blame] | 525 | absl::Span<uint8_t> span() override { return *span_; } |
davidjevans | 8b9b52f | 2021-09-17 08:57:30 -0700 | [diff] [blame] | 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 | |
James Kuszmaul | 1d5d9ec | 2024-01-14 17:53:23 -0800 | [diff] [blame^] | 532 | // The regular flatbuffer API makes it surprisingly irritating to unpack an |
| 533 | // Object into an ObjectT without a bunch of extra lines. |
| 534 | template <typename T> |
| 535 | T::NativeTableType UnpackFlatbuffer(const T *fbs) { |
| 536 | typename T::NativeTableType object; |
| 537 | fbs->UnPackTo(&object); |
| 538 | return object; |
| 539 | } |
| 540 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 541 | } // namespace aos |
| 542 | |
| 543 | #endif // AOS_FLATBUFFERS_H_ |