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