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