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" |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 8 | #include "aos/macros.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 9 | #include "flatbuffers/flatbuffers.h" |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 10 | #include "glog/logging.h" |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 11 | |
| 12 | namespace aos { |
| 13 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 14 | // This class is a base class for all sizes of array backed allocators. |
| 15 | class FixedAllocatorBase : public flatbuffers::Allocator { |
| 16 | public: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 17 | ~FixedAllocatorBase() override { CHECK(!is_allocated_); } |
| 18 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 19 | // TODO(austin): Read the contract for these. |
| 20 | uint8_t *allocate(size_t) override; |
| 21 | |
| 22 | void deallocate(uint8_t *, size_t) override { is_allocated_ = false; } |
| 23 | |
| 24 | uint8_t *reallocate_downward(uint8_t *, size_t, size_t, size_t, |
| 25 | size_t) override; |
| 26 | |
| 27 | virtual const uint8_t *data() const = 0; |
| 28 | virtual uint8_t *data() = 0; |
| 29 | virtual size_t size() const = 0; |
| 30 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 31 | void Reset() { is_allocated_ = false; } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 32 | bool is_allocated() const { return is_allocated_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 33 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 34 | bool allocated() { return is_allocated_; } |
| 35 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 36 | private: |
| 37 | bool is_allocated_ = false; |
| 38 | }; |
| 39 | |
| 40 | // 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] | 41 | // in a vector. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 42 | class FixedAllocator : public FixedAllocatorBase { |
| 43 | public: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 44 | FixedAllocator(size_t size) : buffer_(size, 0) {} |
| 45 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 46 | uint8_t *data() override { return &buffer_[0]; } |
| 47 | const uint8_t *data() const override { return &buffer_[0]; } |
| 48 | size_t size() const override { return buffer_.size(); } |
| 49 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 50 | // Releases the data in the buffer. |
| 51 | std::vector<uint8_t> release() { return std::move(buffer_); } |
| 52 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 53 | private: |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 54 | std::vector<uint8_t> buffer_; |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 55 | }; |
| 56 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 57 | // This class adapts a preallocated memory region to an Allocator. |
| 58 | class PreallocatedAllocator : public FixedAllocatorBase { |
| 59 | public: |
| 60 | PreallocatedAllocator(void *data, size_t size) : data_(data), size_(size) {} |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 61 | PreallocatedAllocator(const PreallocatedAllocator &) = delete; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 62 | PreallocatedAllocator(PreallocatedAllocator &&other) |
| 63 | : data_(other.data_), size_(other.size_) { |
| 64 | CHECK(!is_allocated()); |
| 65 | CHECK(!other.is_allocated()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 66 | } |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 67 | |
| 68 | PreallocatedAllocator &operator=(const PreallocatedAllocator &) = delete; |
| 69 | PreallocatedAllocator &operator=(PreallocatedAllocator &&other) { |
| 70 | CHECK(!is_allocated()); |
| 71 | CHECK(!other.is_allocated()); |
| 72 | data_ = other.data_; |
| 73 | size_ = other.size_; |
| 74 | return *this; |
| 75 | } |
| 76 | |
| 77 | uint8_t *data() final { |
| 78 | return reinterpret_cast<uint8_t *>(CHECK_NOTNULL(data_)); |
| 79 | } |
| 80 | const uint8_t *data() const final { |
| 81 | return reinterpret_cast<const uint8_t *>(CHECK_NOTNULL(data_)); |
| 82 | } |
| 83 | size_t size() const final { return size_; } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 84 | |
| 85 | private: |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 86 | void *data_; |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 87 | size_t size_; |
| 88 | }; |
| 89 | |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 90 | // Base class representing an object which holds the memory representing a root |
| 91 | // flatbuffer. |
| 92 | template <typename T> |
| 93 | class Flatbuffer { |
| 94 | public: |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 95 | virtual ~Flatbuffer() {} |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 96 | // Returns the MiniReflectTypeTable for T. |
| 97 | static const flatbuffers::TypeTable *MiniReflectTypeTable() { |
| 98 | return T::MiniReflectTypeTable(); |
| 99 | } |
| 100 | |
| 101 | // Returns a message from the buffer. |
| 102 | const T &message() const { |
| 103 | return *flatbuffers::GetRoot<T>(reinterpret_cast<const void *>(data())); |
| 104 | } |
| 105 | // Returns a mutable message. It can be mutated via the flatbuffer rules. |
| 106 | T *mutable_message() { |
| 107 | return flatbuffers::GetMutableRoot<T>(reinterpret_cast<void *>(data())); |
| 108 | } |
| 109 | |
| 110 | virtual const uint8_t *data() const = 0; |
| 111 | virtual uint8_t *data() = 0; |
| 112 | virtual size_t size() const = 0; |
Austin Schuh | 6f3babe | 2020-01-26 20:34:50 -0800 | [diff] [blame] | 113 | |
| 114 | absl::Span<uint8_t> span() { return absl::Span<uint8_t>(data(), size()); } |
| 115 | absl::Span<const uint8_t> span() const { |
| 116 | return absl::Span<const uint8_t>(data(), size()); |
| 117 | } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 120 | // String backed flatbuffer. |
| 121 | template <typename T> |
| 122 | class FlatbufferString : public Flatbuffer<T> { |
| 123 | public: |
| 124 | // Builds a flatbuffer using the contents of the string. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 125 | FlatbufferString(const std::string_view data) : data_(data) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 126 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 127 | FlatbufferString(const Flatbuffer<T> &other) { |
| 128 | data_ = std::string(other.data(), other.size()); |
| 129 | } |
| 130 | |
| 131 | // Coppies the data from the other flatbuffer. |
| 132 | FlatbufferString &operator=(const Flatbuffer<T> &other) { |
| 133 | data_ = std::string(other.data(), other.size()); |
| 134 | return *this; |
| 135 | } |
| 136 | |
| 137 | virtual ~FlatbufferString() override {} |
| 138 | |
| 139 | const uint8_t *data() const override { |
| 140 | return reinterpret_cast<const uint8_t *>(data_.data()); |
| 141 | } |
James Kuszmaul | 872efd2 | 2019-12-03 20:59:09 -0800 | [diff] [blame] | 142 | uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 143 | size_t size() const override { return data_.size(); } |
| 144 | |
| 145 | private: |
| 146 | std::string data_; |
| 147 | }; |
| 148 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 149 | // Vector backed flatbuffer. |
| 150 | template <typename T> |
| 151 | class FlatbufferVector : public Flatbuffer<T> { |
| 152 | public: |
| 153 | // Builds a Flatbuffer around a vector. |
| 154 | FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {} |
| 155 | |
| 156 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 157 | FlatbufferVector(const Flatbuffer<T> &other) |
| 158 | : data_(other.data(), other.data() + other.size()) {} |
| 159 | |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 160 | // Copy constructor. |
| 161 | FlatbufferVector(const FlatbufferVector<T> &other) |
| 162 | : data_(other.data(), other.data() + other.size()) {} |
| 163 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 164 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 165 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 166 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 167 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 168 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 169 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 170 | data_ = std::vector<uint8_t>(other.data(), other.data() + other.size()); |
| 171 | return *this; |
| 172 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 173 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 174 | data_ = std::move(other.data_); |
| 175 | return *this; |
| 176 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 177 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 178 | // Constructs an empty flatbuffer of type T. |
| 179 | static FlatbufferVector<T> Empty() { |
| 180 | return FlatbufferVector<T>(std::vector<uint8_t>{}); |
| 181 | } |
| 182 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 183 | virtual ~FlatbufferVector() override {} |
| 184 | |
| 185 | const uint8_t *data() const override { return data_.data(); } |
| 186 | uint8_t *data() override { return data_.data(); } |
| 187 | size_t size() const override { return data_.size(); } |
| 188 | |
| 189 | private: |
| 190 | std::vector<uint8_t> data_; |
| 191 | }; |
| 192 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 193 | // This object associates the message type with the memory storing the |
| 194 | // flatbuffer. This only stores root tables. |
| 195 | // |
| 196 | // From a usage point of view, pointers to the data are very different than |
| 197 | // pointers to the tables. |
| 198 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 199 | class FlatbufferDetachedBuffer final : public Flatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 200 | public: |
| 201 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 202 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 203 | : buffer_(::std::move(buffer)) {} |
| 204 | |
| 205 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 206 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 207 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 208 | : buffer_(::std::move(fb.buffer_)) {} |
| 209 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 210 | ::std::swap(buffer_, fb.buffer_); |
| 211 | return *this; |
| 212 | } |
| 213 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 214 | virtual ~FlatbufferDetachedBuffer() override {} |
| 215 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 216 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 217 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 218 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 219 | fbb.ForceDefaults(true); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 220 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 221 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 222 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | // Returns references to the buffer, and the data. |
| 226 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 227 | const uint8_t *data() const override { return buffer_.data(); } |
| 228 | uint8_t *data() override { return buffer_.data(); } |
| 229 | size_t size() const override { return buffer_.size(); } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 230 | |
| 231 | private: |
| 232 | flatbuffers::DetachedBuffer buffer_; |
| 233 | }; |
| 234 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 235 | // Array backed flatbuffer which manages building of the flatbuffer. |
| 236 | template <typename T, size_t Size> |
| 237 | class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> { |
| 238 | public: |
| 239 | FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) { |
| 240 | builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 241 | builder_.ForceDefaults(true); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | flatbuffers::FlatBufferBuilder *Builder() { |
| 245 | if (allocator_.allocated()) { |
| 246 | LOG(FATAL) << "Array backed flatbuffer can only be built once"; |
| 247 | } |
| 248 | return &builder_; |
| 249 | } |
| 250 | |
| 251 | void Finish(flatbuffers::Offset<T> root) { |
| 252 | if (!allocator_.allocated()) { |
| 253 | LOG(FATAL) << "Cannot finish if never building"; |
| 254 | } |
| 255 | builder_.Finish(root); |
| 256 | data_ = builder_.GetBufferPointer(); |
| 257 | size_ = builder_.GetSize(); |
| 258 | } |
| 259 | |
| 260 | const uint8_t *data() const override { |
| 261 | CHECK_NOTNULL(data_); |
| 262 | return data_; |
| 263 | } |
| 264 | uint8_t *data() override { |
| 265 | CHECK_NOTNULL(data_); |
| 266 | return data_; |
| 267 | } |
| 268 | size_t size() const override { return size_; } |
| 269 | |
| 270 | private: |
| 271 | std::array<uint8_t, Size> buffer_; |
| 272 | PreallocatedAllocator allocator_; |
| 273 | flatbuffers::FlatBufferBuilder builder_; |
| 274 | uint8_t *data_ = nullptr; |
| 275 | size_t size_ = 0; |
| 276 | |
| 277 | DISALLOW_COPY_AND_ASSIGN(FlatbufferFixedAllocatorArray); |
| 278 | }; |
| 279 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 280 | // This object associates the message type with the memory storing the |
| 281 | // flatbuffer. This only stores root tables. |
| 282 | // |
| 283 | // From a usage point of view, pointers to the data are very different than |
| 284 | // pointers to the tables. |
| 285 | template <typename T> |
| 286 | class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> { |
| 287 | public: |
| 288 | // Builds a Flatbuffer by taking ownership of the buffer. |
| 289 | SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
| 290 | : buffer_(::std::move(buffer)) { |
| 291 | CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t)); |
| 292 | } |
| 293 | |
| 294 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 295 | // flatbuffer. |
| 296 | SizePrefixedFlatbufferDetachedBuffer( |
| 297 | SizePrefixedFlatbufferDetachedBuffer &&fb) |
| 298 | : buffer_(::std::move(fb.buffer_)) {} |
| 299 | SizePrefixedFlatbufferDetachedBuffer &operator=( |
| 300 | SizePrefixedFlatbufferDetachedBuffer &&fb) { |
| 301 | ::std::swap(buffer_, fb.buffer_); |
| 302 | return *this; |
| 303 | } |
| 304 | |
| 305 | virtual ~SizePrefixedFlatbufferDetachedBuffer() override {} |
| 306 | |
| 307 | // Returns references to the buffer, and the data. |
| 308 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
| 309 | const uint8_t *data() const override { |
| 310 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 311 | } |
| 312 | uint8_t *data() override { |
| 313 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 314 | } |
| 315 | size_t size() const override { |
| 316 | return buffer_.size() - sizeof(flatbuffers::uoffset_t); |
| 317 | } |
| 318 | |
| 319 | private: |
| 320 | flatbuffers::DetachedBuffer buffer_; |
| 321 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 322 | // TODO(austin): Need a way to get our hands on the max size. Can start with |
| 323 | // "large" for now. |
| 324 | |
| 325 | } // namespace aos |
| 326 | |
| 327 | #endif // AOS_FLATBUFFERS_H_ |