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