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