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