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_) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 64 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 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) { |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 70 | CHECK(!is_allocated()) << ": May not overwrite in-use allocator"; |
Brian Silverman | a1652f3 | 2020-01-29 20:41:44 -0800 | [diff] [blame] | 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 | 39580f1 | 2020-08-01 14:44:08 -0700 | [diff] [blame] | 118 | |
| 119 | // Wipes out the data buffer. This is handy to mark an instance as freed, and |
| 120 | // make attempts to use it fail more obviously. |
| 121 | void Wipe() { memset(data(), 0, size()); } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 122 | }; |
| 123 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 124 | // String backed flatbuffer. |
| 125 | template <typename T> |
| 126 | class FlatbufferString : public Flatbuffer<T> { |
| 127 | public: |
| 128 | // Builds a flatbuffer using the contents of the string. |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 129 | FlatbufferString(const std::string_view data) : data_(data) {} |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 130 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 131 | FlatbufferString(const Flatbuffer<T> &other) { |
Brian Silverman | c8a5b55 | 2020-04-28 16:43:59 -0700 | [diff] [blame] | 132 | data_ = |
| 133 | std::string(reinterpret_cast<const char *>(other.data()), other.size()); |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Coppies the data from the other flatbuffer. |
| 137 | FlatbufferString &operator=(const Flatbuffer<T> &other) { |
| 138 | data_ = std::string(other.data(), other.size()); |
| 139 | return *this; |
| 140 | } |
| 141 | |
| 142 | virtual ~FlatbufferString() override {} |
| 143 | |
| 144 | const uint8_t *data() const override { |
| 145 | return reinterpret_cast<const uint8_t *>(data_.data()); |
| 146 | } |
James Kuszmaul | 872efd2 | 2019-12-03 20:59:09 -0800 | [diff] [blame] | 147 | uint8_t *data() override { return reinterpret_cast<uint8_t *>(data_.data()); } |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 148 | size_t size() const override { return data_.size(); } |
| 149 | |
| 150 | private: |
| 151 | std::string data_; |
| 152 | }; |
| 153 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 154 | // Vector backed flatbuffer. |
| 155 | template <typename T> |
| 156 | class FlatbufferVector : public Flatbuffer<T> { |
| 157 | public: |
| 158 | // Builds a Flatbuffer around a vector. |
| 159 | FlatbufferVector(std::vector<uint8_t> &&data) : data_(std::move(data)) {} |
| 160 | |
| 161 | // Builds a Flatbuffer by copying the data from the other flatbuffer. |
| 162 | FlatbufferVector(const Flatbuffer<T> &other) |
| 163 | : data_(other.data(), other.data() + other.size()) {} |
| 164 | |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 165 | // Copy constructor. |
| 166 | FlatbufferVector(const FlatbufferVector<T> &other) |
| 167 | : data_(other.data(), other.data() + other.size()) {} |
| 168 | |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 169 | // Move constructor. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 170 | FlatbufferVector(FlatbufferVector<T> &&other) |
| 171 | : data_(std::move(other.data_)) {} |
Austin Schuh | 03803bd | 2019-12-30 18:08:17 -0800 | [diff] [blame] | 172 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 173 | // Copies the data from the other flatbuffer. |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 174 | FlatbufferVector &operator=(const FlatbufferVector<T> &other) { |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 175 | data_ = std::vector<uint8_t>(other.data(), other.data() + other.size()); |
| 176 | return *this; |
| 177 | } |
Austin Schuh | 5131bd0 | 2020-01-08 17:25:59 -0800 | [diff] [blame] | 178 | FlatbufferVector &operator=(FlatbufferVector<T> &&other) { |
| 179 | data_ = std::move(other.data_); |
| 180 | return *this; |
| 181 | } |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 182 | |
Austin Schuh | 05b7047 | 2020-01-01 17:11:17 -0800 | [diff] [blame] | 183 | // Constructs an empty flatbuffer of type T. |
| 184 | static FlatbufferVector<T> Empty() { |
| 185 | return FlatbufferVector<T>(std::vector<uint8_t>{}); |
| 186 | } |
| 187 | |
Austin Schuh | e309d2a | 2019-11-29 13:25:21 -0800 | [diff] [blame] | 188 | virtual ~FlatbufferVector() override {} |
| 189 | |
| 190 | const uint8_t *data() const override { return data_.data(); } |
| 191 | uint8_t *data() override { return data_.data(); } |
| 192 | size_t size() const override { return data_.size(); } |
| 193 | |
| 194 | private: |
| 195 | std::vector<uint8_t> data_; |
| 196 | }; |
| 197 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 198 | // This object associates the message type with the memory storing the |
| 199 | // flatbuffer. This only stores root tables. |
| 200 | // |
| 201 | // From a usage point of view, pointers to the data are very different than |
| 202 | // pointers to the tables. |
| 203 | template <typename T> |
James Kuszmaul | 3ae4226 | 2019-11-08 12:33:41 -0800 | [diff] [blame] | 204 | class FlatbufferDetachedBuffer final : public Flatbuffer<T> { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 205 | public: |
| 206 | // Builds a Flatbuffer by taking ownership of the buffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 207 | FlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 208 | : buffer_(::std::move(buffer)) {} |
| 209 | |
| 210 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 211 | // flatbuffer. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 212 | FlatbufferDetachedBuffer(FlatbufferDetachedBuffer &&fb) |
| 213 | : buffer_(::std::move(fb.buffer_)) {} |
| 214 | FlatbufferDetachedBuffer &operator=(FlatbufferDetachedBuffer &&fb) { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 215 | ::std::swap(buffer_, fb.buffer_); |
| 216 | return *this; |
| 217 | } |
| 218 | |
Alex Perry | cb7da4b | 2019-08-28 19:35:56 -0700 | [diff] [blame] | 219 | virtual ~FlatbufferDetachedBuffer() override {} |
| 220 | |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 221 | // Constructs an empty flatbuffer of type T. |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 222 | static FlatbufferDetachedBuffer<T> Empty() { |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 223 | flatbuffers::FlatBufferBuilder fbb; |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 224 | fbb.ForceDefaults(true); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 225 | const auto end = fbb.EndTable(fbb.StartTable()); |
| 226 | fbb.Finish(flatbuffers::Offset<flatbuffers::Table>(end)); |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 227 | return FlatbufferDetachedBuffer<T>(fbb.Release()); |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Returns references to the buffer, and the data. |
| 231 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
Austin Schuh | 40485ed | 2019-10-26 21:51:44 -0700 | [diff] [blame] | 232 | const uint8_t *data() const override { return buffer_.data(); } |
| 233 | uint8_t *data() override { return buffer_.data(); } |
| 234 | size_t size() const override { return buffer_.size(); } |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 235 | |
| 236 | private: |
| 237 | flatbuffers::DetachedBuffer buffer_; |
| 238 | }; |
| 239 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 240 | // Array backed flatbuffer which manages building of the flatbuffer. |
| 241 | template <typename T, size_t Size> |
| 242 | class FlatbufferFixedAllocatorArray final : public Flatbuffer<T> { |
| 243 | public: |
| 244 | FlatbufferFixedAllocatorArray() : buffer_(), allocator_(&buffer_[0], Size) { |
| 245 | builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
Austin Schuh | d7b15da | 2020-02-17 15:06:11 -0800 | [diff] [blame] | 246 | builder_.ForceDefaults(true); |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 247 | } |
| 248 | |
Brian Silverman | 341b57e | 2020-06-23 16:23:18 -0700 | [diff] [blame] | 249 | void Reset() { |
| 250 | allocator_.Reset(); |
| 251 | builder_ = flatbuffers::FlatBufferBuilder(Size, &allocator_); |
| 252 | builder_.ForceDefaults(true); |
| 253 | } |
| 254 | |
Tyler Chatow | 116edf1 | 2020-01-26 11:52:39 -0800 | [diff] [blame] | 255 | flatbuffers::FlatBufferBuilder *Builder() { |
| 256 | if (allocator_.allocated()) { |
| 257 | LOG(FATAL) << "Array backed flatbuffer can only be built once"; |
| 258 | } |
| 259 | return &builder_; |
| 260 | } |
| 261 | |
| 262 | void Finish(flatbuffers::Offset<T> root) { |
| 263 | if (!allocator_.allocated()) { |
| 264 | LOG(FATAL) << "Cannot finish if never building"; |
| 265 | } |
| 266 | builder_.Finish(root); |
| 267 | data_ = builder_.GetBufferPointer(); |
| 268 | size_ = builder_.GetSize(); |
| 269 | } |
| 270 | |
| 271 | const uint8_t *data() const override { |
| 272 | CHECK_NOTNULL(data_); |
| 273 | return data_; |
| 274 | } |
| 275 | uint8_t *data() override { |
| 276 | CHECK_NOTNULL(data_); |
| 277 | return data_; |
| 278 | } |
| 279 | size_t size() const override { return size_; } |
| 280 | |
| 281 | private: |
| 282 | std::array<uint8_t, Size> buffer_; |
| 283 | PreallocatedAllocator allocator_; |
| 284 | flatbuffers::FlatBufferBuilder builder_; |
| 285 | uint8_t *data_ = nullptr; |
| 286 | size_t size_ = 0; |
| 287 | |
| 288 | DISALLOW_COPY_AND_ASSIGN(FlatbufferFixedAllocatorArray); |
| 289 | }; |
| 290 | |
Austin Schuh | e84c3ed | 2019-12-14 15:29:48 -0800 | [diff] [blame] | 291 | // This object associates the message type with the memory storing the |
| 292 | // flatbuffer. This only stores root tables. |
| 293 | // |
| 294 | // From a usage point of view, pointers to the data are very different than |
| 295 | // pointers to the tables. |
| 296 | template <typename T> |
| 297 | class SizePrefixedFlatbufferDetachedBuffer final : public Flatbuffer<T> { |
| 298 | public: |
| 299 | // Builds a Flatbuffer by taking ownership of the buffer. |
| 300 | SizePrefixedFlatbufferDetachedBuffer(flatbuffers::DetachedBuffer &&buffer) |
| 301 | : buffer_(::std::move(buffer)) { |
| 302 | CHECK_GE(buffer_.size(), sizeof(flatbuffers::uoffset_t)); |
| 303 | } |
| 304 | |
| 305 | // Builds a flatbuffer by taking ownership of the buffer from the other |
| 306 | // flatbuffer. |
| 307 | SizePrefixedFlatbufferDetachedBuffer( |
| 308 | SizePrefixedFlatbufferDetachedBuffer &&fb) |
| 309 | : buffer_(::std::move(fb.buffer_)) {} |
| 310 | SizePrefixedFlatbufferDetachedBuffer &operator=( |
| 311 | SizePrefixedFlatbufferDetachedBuffer &&fb) { |
| 312 | ::std::swap(buffer_, fb.buffer_); |
| 313 | return *this; |
| 314 | } |
| 315 | |
| 316 | virtual ~SizePrefixedFlatbufferDetachedBuffer() override {} |
| 317 | |
| 318 | // Returns references to the buffer, and the data. |
| 319 | const flatbuffers::DetachedBuffer &buffer() const { return buffer_; } |
| 320 | const uint8_t *data() const override { |
| 321 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 322 | } |
| 323 | uint8_t *data() override { |
| 324 | return buffer_.data() + sizeof(flatbuffers::uoffset_t); |
| 325 | } |
| 326 | size_t size() const override { |
| 327 | return buffer_.size() - sizeof(flatbuffers::uoffset_t); |
| 328 | } |
| 329 | |
| 330 | private: |
| 331 | flatbuffers::DetachedBuffer buffer_; |
| 332 | }; |
Austin Schuh | e93d864 | 2019-10-13 15:27:07 -0700 | [diff] [blame] | 333 | // TODO(austin): Need a way to get our hands on the max size. Can start with |
| 334 | // "large" for now. |
| 335 | |
| 336 | } // namespace aos |
| 337 | |
| 338 | #endif // AOS_FLATBUFFERS_H_ |