James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 1 | #ifndef AOS_FLATBUFFERS_STATIC_VECTOR_H_ |
| 2 | #define AOS_FLATBUFFERS_STATIC_VECTOR_H_ |
| 3 | #include <span> |
| 4 | |
| 5 | #include "flatbuffers/base.h" |
James Kuszmaul | e65fb40 | 2024-01-13 14:10:51 -0800 | [diff] [blame] | 6 | #include "flatbuffers/vector.h" |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 7 | #include "glog/logging.h" |
| 8 | |
| 9 | #include "aos/containers/inlined_vector.h" |
| 10 | #include "aos/containers/sized_array.h" |
| 11 | #include "aos/flatbuffers/base.h" |
| 12 | |
| 13 | namespace aos::fbs { |
| 14 | |
| 15 | namespace internal { |
| 16 | // Helper class for managing how we specialize the Vector object for different |
| 17 | // contained types. |
| 18 | // Users of the Vector class should never need to care about this. |
| 19 | // Template arguments: |
| 20 | // T: The type that the vector stores. |
| 21 | // kInline: Whether the type in question is stored inline or not. |
| 22 | // Enable: Used for SFINAE around struct values; can be ignored. |
| 23 | // The struct provides the following types: |
| 24 | // Type: The type of the data that will be stored inline in the vector. |
| 25 | // ObjectType: The type of the actual data (only used for non-inline objects). |
| 26 | // FlatbufferType: The type used by flatbuffers::Vector to store this type. |
| 27 | // ConstFlatbufferType: The type used by a const flatbuffers::Vector to store |
| 28 | // this type. |
| 29 | // kDataAlign: Alignment required by the stored type. |
| 30 | // kDataSize: Nominal size required by each non-inline data member. This is |
| 31 | // what will be initially allocated; once created, individual members may |
| 32 | // grow to accommodate dynamically lengthed vectors. |
| 33 | template <typename T, bool kInline, class Enable = void> |
| 34 | struct InlineWrapper; |
| 35 | } // namespace internal |
| 36 | |
| 37 | // This Vector class provides a mutable, resizeable, flatbuffer vector. |
| 38 | // |
| 39 | // Upon creation, the Vector will start with enough space allocated for |
| 40 | // kStaticLength elements, and must be provided with a memory buffer that |
| 41 | // is large enough to serialize all the kStaticLength members (kStaticLength may |
| 42 | // be zero). |
| 43 | // |
| 44 | // Once created, the Vector may be grown using calls to reserve(). |
| 45 | // This will result in the Vector attempting to allocate memory via its |
| 46 | // parent object; such calls may fail if there is no space available in the |
| 47 | // allocator. |
| 48 | // |
| 49 | // Note that if you are using the Vector class in a realtime context (and thus |
| 50 | // must avoid dynamic memory allocations) you must only be using a Vector of |
| 51 | // inline data (i.e., scalars, enums, or structs). Flatbuffer tables and strings |
| 52 | // require overhead to manage and so require some form of dynamic memory |
| 53 | // allocation. If we discover a strong use-case for such things, then we may |
| 54 | // provide some interface that allows managing said metadata on the stack or |
| 55 | // in another realtime-safe manner. |
| 56 | // |
| 57 | // Template arguments: |
| 58 | // T: Type contained by the vector; either a scalar/struct/enum type or a |
| 59 | // static flatbuffer type of some sort (a String or an implementation of |
| 60 | // aos::fbs::Table). |
| 61 | // kStaticLength: Number of elements to statically allocate memory for. |
| 62 | // May be zero. |
| 63 | // kInline: Whether the type T will be stored inline in the vector. |
| 64 | // kForceAlign: Alignment to force for the start of the vector (e.g., for |
| 65 | // byte arrays it may be desirable to have the entire array aligned). |
| 66 | // kNullTerminate: Whether to reserve an extra byte past the end of |
| 67 | // the inline data for null termination. Not included in kStaticLength, |
| 68 | // so if e.g. you want to store the string "abc" then kStaticLength can |
| 69 | // be 3 and kNullTerminate can be true and the vector data will take |
| 70 | // up 4 bytes of memory. |
| 71 | // |
| 72 | // Vector buffer memory layout: |
| 73 | // * Requirements: |
| 74 | // * Minimum alignment of 4 bytes (for element count). |
| 75 | // * The start of the vector data must be aligned to either |
| 76 | // alignof(InlineType) or a user-specified number. |
| 77 | // * The element count for the vector must immediately precede the vector |
| 78 | // data (and so may itself not be aligned to alignof(InlineType)). |
| 79 | // * For non-inlined types, the individual types must be aligned to |
| 80 | // their own alignment. |
| 81 | // * In order to accommodate this, the vector buffer as a whole must |
| 82 | // generally be aligned to the greatest of the above alignments. There |
| 83 | // are two reasonable ways one could do this: |
| 84 | // * Require that the 4th byte of the buffer provided by aligned to |
| 85 | // the maximum alignment of its contents. |
| 86 | // * Require that the buffer itself by aligned, and provide padding |
| 87 | // ourselves. The Vector would then have to expose its own offset |
| 88 | // because it would not start at the start of the buffer. |
| 89 | // The former requires that the wrapping code understand the internals |
| 90 | // of how vectors work; the latter generates extra padding and adds |
| 91 | // extra logic around handling non-zero offsets. |
| 92 | // To maintain general simplicity, we will use the second condition and eat |
| 93 | // the cost of the potential extra few bytes of padding. |
| 94 | // * The layout of the buffer will thus be: |
| 95 | // [padding; element_count; inline_data; padding; offset_data] |
| 96 | // The first padding will be of size max(0, kAlign - 4). |
| 97 | // The element_count is of size 4. |
| 98 | // The inline_data is of size sizeof(InlineType) * kStaticLength. |
| 99 | // The second padding is of size |
| 100 | // (kAlign - ((sizeof(InlineType) * kStaticLength) % kAlign)). |
| 101 | // The remaining data is only present if kInline is false. |
| 102 | // The offset data is of size T::kSize * kStaticLength. T::kSize % T::kAlign |
| 103 | // must be zero. |
| 104 | // Note that no padding is required on the end because T::kAlign will always |
| 105 | // end up being equal to the alignment (this can only be violated if |
| 106 | // kForceAlign is used, but we do not allow that). |
James Kuszmaul | 1c9693f | 2023-12-08 09:45:26 -0800 | [diff] [blame] | 107 | // The Vector class leaves any padding uninitialized. Until and unless we |
| 108 | // determine that it is a performance issue, it is the responsibility of the |
| 109 | // parent of this object to zero-initialize the memory. |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 110 | template <typename T, size_t kStaticLength, bool kInline, |
| 111 | size_t kForceAlign = 0, bool kNullTerminate = false> |
| 112 | class Vector : public ResizeableObject { |
James Kuszmaul | 2244805 | 2023-12-14 15:55:14 -0800 | [diff] [blame] | 113 | template <typename VectorType, typename ValueType> |
| 114 | class generic_iterator { |
| 115 | public: |
| 116 | using iterator_category = std::random_access_iterator_tag; |
| 117 | using value_type = ValueType; |
| 118 | using difference_type = std::ptrdiff_t; |
| 119 | using pointer = value_type *; |
| 120 | using reference = value_type &; |
| 121 | |
| 122 | explicit generic_iterator(VectorType *vector, size_t index) |
| 123 | : vector_(vector), index_(index) {} |
| 124 | generic_iterator(const generic_iterator &) = default; |
| 125 | generic_iterator() : vector_(nullptr), index_(0) {} |
| 126 | generic_iterator &operator=(const generic_iterator &) = default; |
| 127 | |
| 128 | generic_iterator &operator++() { |
| 129 | ++index_; |
| 130 | return *this; |
| 131 | } |
| 132 | generic_iterator operator++(int) { |
| 133 | generic_iterator retval = *this; |
| 134 | ++(*this); |
| 135 | return retval; |
| 136 | } |
| 137 | generic_iterator &operator--() { |
| 138 | --index_; |
| 139 | return *this; |
| 140 | } |
| 141 | generic_iterator operator--(int) { |
| 142 | generic_iterator retval = *this; |
| 143 | --(*this); |
| 144 | return retval; |
| 145 | } |
| 146 | bool operator==(const generic_iterator &other) const { |
| 147 | CHECK_EQ(other.vector_, vector_); |
| 148 | return index_ == other.index_; |
| 149 | } |
| 150 | std::strong_ordering operator<=>(const generic_iterator &other) const { |
| 151 | CHECK_EQ(other.vector_, vector_); |
| 152 | return index_ <=> other.index_; |
| 153 | } |
| 154 | reference operator*() const { return vector_->at(index_); } |
| 155 | difference_type operator-(const generic_iterator &other) const { |
| 156 | CHECK_EQ(other.vector_, vector_); |
| 157 | return index_ - other.index_; |
| 158 | } |
| 159 | generic_iterator operator-(difference_type decrement) const { |
| 160 | return generic_iterator(vector_, index_ - decrement); |
| 161 | } |
| 162 | friend generic_iterator operator-(difference_type decrement, |
| 163 | const generic_iterator &rhs) { |
| 164 | return rhs - decrement; |
| 165 | } |
| 166 | generic_iterator operator+(difference_type increment) const { |
| 167 | return generic_iterator(vector_, index_ + increment); |
| 168 | } |
| 169 | friend generic_iterator operator+(difference_type increment, |
| 170 | const generic_iterator &rhs) { |
| 171 | return rhs + increment; |
| 172 | } |
| 173 | generic_iterator &operator+=(difference_type increment) { |
| 174 | index_ += increment; |
| 175 | return *this; |
| 176 | } |
| 177 | generic_iterator &operator-=(difference_type increment) { |
| 178 | index_ -= increment; |
| 179 | return *this; |
| 180 | } |
| 181 | reference operator[](difference_type index) const { |
| 182 | return *(*this + index); |
| 183 | } |
| 184 | |
| 185 | private: |
| 186 | VectorType *vector_; |
| 187 | size_t index_; |
| 188 | }; |
| 189 | |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 190 | public: |
James Kuszmaul | 2244805 | 2023-12-14 15:55:14 -0800 | [diff] [blame] | 191 | using iterator = generic_iterator<Vector, T>; |
| 192 | using const_iterator = generic_iterator<const Vector, const T>; |
| 193 | |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 194 | static_assert(kInline || !kNullTerminate, |
| 195 | "It does not make sense to null-terminate vectors of objects."); |
| 196 | // Type stored inline in the serialized vector (offsets for tables/strings; T |
| 197 | // otherwise). |
| 198 | using InlineType = typename internal::InlineWrapper<T, kInline>::Type; |
| 199 | // OUt-of-line type for out-of-line T. |
| 200 | using ObjectType = typename internal::InlineWrapper<T, kInline>::ObjectType; |
| 201 | // Type used as the template parameter to flatbuffers::Vector<>. |
| 202 | using FlatbufferType = |
| 203 | typename internal::InlineWrapper<T, kInline>::FlatbufferType; |
| 204 | using ConstFlatbufferType = |
| 205 | typename internal::InlineWrapper<T, kInline>::ConstFlatbufferType; |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 206 | // FlatbufferObjectType corresponds to the type used by the flatbuffer |
| 207 | // "object" API (i.e. the FlatbufferT types). |
| 208 | // This type will be something unintelligble for inline types. |
| 209 | using FlatbufferObjectType = |
| 210 | typename internal::InlineWrapper<T, kInline>::FlatbufferObjectType; |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 211 | // flatbuffers::Vector type that corresponds to this Vector. |
| 212 | typedef flatbuffers::Vector<FlatbufferType> Flatbuffer; |
| 213 | typedef const flatbuffers::Vector<ConstFlatbufferType> ConstFlatbuffer; |
| 214 | // Alignment of the inline data. |
| 215 | static constexpr size_t kInlineAlign = |
| 216 | std::max(kForceAlign, alignof(InlineType)); |
| 217 | // Type used for serializing the length of the vector. |
| 218 | typedef uint32_t LengthType; |
| 219 | // Overall alignment of this type, and required alignment of the buffer that |
| 220 | // must be provided to the Vector. |
| 221 | static constexpr size_t kAlign = |
| 222 | std::max({alignof(LengthType), kInlineAlign, |
| 223 | internal::InlineWrapper<T, kInline>::kDataAlign}); |
| 224 | // Padding inserted prior to the length element of the vector (to manage |
| 225 | // alignment of the data properly; see class comment) |
| 226 | static constexpr size_t kPadding1 = |
| 227 | std::max<size_t>(0, kAlign - sizeof(LengthType)); |
| 228 | // Size of the vector length field. |
| 229 | static constexpr size_t kLengthSize = sizeof(LengthType); |
| 230 | // Size of all the inline vector data, including null termination (prior to |
| 231 | // any dynamic increases in size). |
| 232 | static constexpr size_t kInlineSize = |
| 233 | sizeof(InlineType) * (kStaticLength + (kNullTerminate ? 1 : 0)); |
| 234 | // Per-element size of any out-of-line data. |
| 235 | static constexpr size_t kDataElementSize = |
| 236 | internal::InlineWrapper<T, kInline>::kDataSize; |
| 237 | // Padding between the inline data and any out-of-line data, to manage |
| 238 | // mismatches in alignment between the two. |
| 239 | static constexpr size_t kPadding2 = kAlign - (kInlineSize % kAlign); |
| 240 | // Total statically allocated space for any out-of-line data ("offset data") |
| 241 | // (prior to any dynamic increases in size). |
| 242 | static constexpr size_t kOffsetOffsetDataSize = |
| 243 | kInline ? 0 : (kStaticLength * kDataElementSize); |
| 244 | // Total nominal size of the Vector. |
| 245 | static constexpr size_t kSize = |
| 246 | kPadding1 + kLengthSize + kInlineSize + kPadding2 + kOffsetOffsetDataSize; |
| 247 | // Offset from the start of the provided buffer to where the actual start of |
| 248 | // the vector is. |
| 249 | static constexpr size_t kOffset = kPadding1; |
| 250 | // Constructors; the provided buffer must be aligned to kAlign and be kSize in |
| 251 | // length. parent must be non-null. |
| 252 | Vector(std::span<uint8_t> buffer, ResizeableObject *parent) |
| 253 | : ResizeableObject(buffer, parent) { |
| 254 | CHECK_EQ(0u, reinterpret_cast<size_t>(buffer.data()) % kAlign); |
| 255 | CHECK_EQ(kSize, buffer.size()); |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 256 | SetLength(0u); |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 257 | if (!kInline) { |
| 258 | // Initialize the offsets for any sub-tables. These are used to track |
| 259 | // where each table will get serialized in memory as memory gets |
| 260 | // resized/moved around. |
| 261 | for (size_t index = 0; index < kStaticLength; ++index) { |
| 262 | object_absolute_offsets_.emplace_back(kPadding1 + kLengthSize + |
| 263 | kInlineSize + kPadding2 + |
| 264 | index * kDataElementSize); |
| 265 | } |
| 266 | } |
| 267 | } |
| 268 | Vector(const Vector &) = delete; |
| 269 | Vector &operator=(const Vector &) = delete; |
| 270 | virtual ~Vector() {} |
| 271 | // Current allocated length of this vector. |
| 272 | // Does not include null termination. |
| 273 | size_t capacity() const { return allocated_length_; } |
| 274 | // Current length of the vector. |
| 275 | // Does not include null termination. |
| 276 | size_t size() const { return length_; } |
| 277 | |
| 278 | // Appends an element to the Vector. Used when kInline is false. Returns |
| 279 | // nullptr if the append failed due to insufficient capacity. If you need to |
| 280 | // increase the capacity() of the vector, call reserve(). |
| 281 | [[nodiscard]] T *emplace_back(); |
| 282 | // Appends an element to the Vector. Used when kInline is true. Returns false |
| 283 | // if there is insufficient capacity for a new element. |
| 284 | [[nodiscard]] bool emplace_back(T element) { |
| 285 | static_assert(kInline); |
| 286 | return AddInlineElement(element); |
| 287 | } |
| 288 | |
| 289 | // Adjusts the allocated size of the vector (does not affect the actual |
| 290 | // current length as returned by size()). Returns true on success, and false |
| 291 | // if the allocation failed for some reason. |
| 292 | // Note that reductions in size will not currently result in the allocated |
| 293 | // size actually changing. |
| 294 | [[nodiscard]] bool reserve(size_t new_length) { |
| 295 | if (new_length > allocated_length_) { |
| 296 | const size_t new_elements = new_length - allocated_length_; |
| 297 | // First, we must add space for our new inline elements. |
| 298 | if (!InsertBytes( |
| 299 | inline_data() + allocated_length_ + (kNullTerminate ? 1 : 0), |
| 300 | new_elements * sizeof(InlineType), SetZero::kYes)) { |
| 301 | return false; |
| 302 | } |
| 303 | if (!kInline) { |
| 304 | // For non-inline objects, create the space required for all the new |
| 305 | // object data. |
| 306 | const size_t insertion_point = buffer_.size(); |
| 307 | if (!InsertBytes(buffer_.data() + insertion_point, |
| 308 | new_elements * kDataElementSize, SetZero::kYes)) { |
| 309 | return false; |
| 310 | } |
| 311 | for (size_t index = 0; index < new_elements; ++index) { |
| 312 | // Note that the already-allocated data may be arbitrarily-sized, so |
| 313 | // we cannot use the same static calculation that we do in the |
| 314 | // constructor. |
| 315 | object_absolute_offsets_.emplace_back(insertion_point + |
| 316 | index * kDataElementSize); |
| 317 | } |
| 318 | objects_.reserve(new_length); |
| 319 | } |
| 320 | allocated_length_ = new_length; |
| 321 | } |
| 322 | return true; |
| 323 | } |
| 324 | |
| 325 | // Accessors for using the Vector as a flatbuffers::Vector. |
| 326 | // Note that these pointers will be unstable if any memory allocations occur |
| 327 | // that cause memory to get shifted around. |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 328 | ConstFlatbuffer *AsFlatbufferVector() const { |
| 329 | return reinterpret_cast<const Flatbuffer *>(vector_buffer().data()); |
| 330 | } |
| 331 | |
| 332 | // Copies the contents of the provided vector into this; returns false on |
| 333 | // failure (e.g., if the provided vector is too long for the amount of space |
| 334 | // we can allocate through reserve()). |
James Kuszmaul | 710883b | 2023-12-14 14:34:48 -0800 | [diff] [blame] | 335 | // This is a deep copy, and will call FromFlatbuffer on any constituent |
| 336 | // objects. |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 337 | [[nodiscard]] bool FromFlatbuffer(ConstFlatbuffer *vector) { |
| 338 | return FromFlatbuffer(*CHECK_NOTNULL(vector)); |
| 339 | } |
| 340 | [[nodiscard]] bool FromFlatbuffer(ConstFlatbuffer &vector); |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 341 | // The remaining FromFlatbuffer() overloads are for when using the flatbuffer |
| 342 | // "object" API, which uses std::vector's for representing vectors. |
| 343 | [[nodiscard]] bool FromFlatbuffer(const std::vector<InlineType> &vector) { |
| 344 | static_assert(kInline); |
| 345 | return FromData(vector.data(), vector.size()); |
| 346 | } |
| 347 | // Overload for vectors of bools, since the standard library may not use a |
| 348 | // full byte per vector element. |
| 349 | [[nodiscard]] bool FromFlatbuffer(const std::vector<bool> &vector) { |
| 350 | static_assert(kInline); |
| 351 | // We won't be able to do a clean memcpy because std::vector<bool> may be |
| 352 | // implemented using bit-packing. |
| 353 | return FromIterator(vector.cbegin(), vector.cend()); |
| 354 | } |
| 355 | // Overload for non-inline types. Note that to avoid having this overload get |
| 356 | // resolved with inline types, we make FlatbufferObjectType != InlineType. |
| 357 | [[nodiscard]] bool FromFlatbuffer( |
| 358 | const std::vector<FlatbufferObjectType> &vector) { |
| 359 | static_assert(!kInline); |
| 360 | return FromNotInlineIterable(vector); |
| 361 | } |
| 362 | |
| 363 | // Copies values from the provided data pointer into the vector, resizing the |
| 364 | // vector as needed to match. Returns false on failure (e.g., if the |
| 365 | // underlying allocator has insufficient space to perform the copy). Only |
| 366 | // works for inline data types. |
| 367 | [[nodiscard]] bool FromData(const InlineType *input_data, size_t input_size) { |
| 368 | static_assert(kInline); |
| 369 | if (!reserve(input_size)) { |
| 370 | return false; |
| 371 | } |
| 372 | |
| 373 | // We will be overwriting the whole vector very shortly; there is no need to |
| 374 | // clear the buffer to zero. |
| 375 | resize_inline(input_size, SetZero::kNo); |
| 376 | |
| 377 | memcpy(inline_data(), input_data, size() * sizeof(InlineType)); |
| 378 | return true; |
| 379 | } |
| 380 | |
| 381 | // Copies values from the provided iterators into the vector, resizing the |
| 382 | // vector as needed to match. Returns false on failure (e.g., if the |
| 383 | // underlying allocator has insufficient space to perform the copy). Only |
| 384 | // works for inline data types. |
| 385 | // Does not attempt any optimizations if the iterators meet the |
| 386 | // std::contiguous_iterator concept; instead, it simply copies each element |
| 387 | // out one-by-one. |
| 388 | template <typename Iterator> |
| 389 | [[nodiscard]] bool FromIterator(Iterator begin, Iterator end) { |
| 390 | static_assert(kInline); |
| 391 | resize(0); |
| 392 | for (Iterator it = begin; it != end; ++it) { |
| 393 | if (!reserve(size() + 1)) { |
| 394 | return false; |
| 395 | } |
| 396 | // Should never fail, due to the reserve() above. |
| 397 | CHECK(emplace_back(*it)); |
| 398 | } |
| 399 | return true; |
| 400 | } |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 401 | |
| 402 | // Returns the element at the provided index. index must be less than size(). |
| 403 | const T &at(size_t index) const { |
| 404 | CHECK_LT(index, length_); |
| 405 | return unsafe_at(index); |
| 406 | } |
| 407 | |
| 408 | // Same as at(), except that bounds checks are only performed in non-optimized |
| 409 | // builds. |
| 410 | // TODO(james): The GetInlineElement() call itself does some bounds-checking; |
| 411 | // consider down-grading that. |
| 412 | const T &unsafe_at(size_t index) const { |
| 413 | DCHECK_LT(index, length_); |
| 414 | if (kInline) { |
| 415 | // This reinterpret_cast is extremely wrong if T != InlineType (this is |
| 416 | // fine because we only do this if kInline is true). |
| 417 | // TODO(james): Get the templating improved so that we can get away with |
| 418 | // specializing at() instead of using if statements. Resolving this will |
| 419 | // also allow deduplicating the Resize() calls. |
| 420 | // This specialization is difficult because you cannot partially |
| 421 | // specialize a templated class method (online things seem to suggest e.g. |
| 422 | // using a struct as the template parameter rather than having separate |
| 423 | // parameters). |
| 424 | return reinterpret_cast<const T &>(GetInlineElement(index)); |
| 425 | } else { |
| 426 | return objects_[index].t; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | // Returns a mutable pointer to the element at the provided index. index must |
| 431 | // be less than size(). |
| 432 | T &at(size_t index) { |
| 433 | CHECK_LT(index, length_); |
| 434 | return unsafe_at(index); |
| 435 | } |
| 436 | |
| 437 | // Same as at(), except that bounds checks are only performed in non-optimized |
| 438 | // builds. |
| 439 | // TODO(james): The GetInlineElement() call itself does some bounds-checking; |
| 440 | // consider down-grading that. |
| 441 | T &unsafe_at(size_t index) { |
| 442 | DCHECK_LT(index, length_); |
| 443 | if (kInline) { |
| 444 | // This reinterpret_cast is extremely wrong if T != InlineType (this is |
| 445 | // fine because we only do this if kInline is true). |
| 446 | // TODO(james): Get the templating improved so that we can get away with |
| 447 | // specializing at() instead of using if statements. Resolving this will |
| 448 | // also allow deduplicating the Resize() calls. |
| 449 | // This specialization is difficult because you cannot partially |
| 450 | // specialize a templated class method (online things seem to suggest e.g. |
| 451 | // using a struct as the template parameter rather than having separate |
| 452 | // parameters). |
| 453 | return reinterpret_cast<T &>(GetInlineElement(index)); |
| 454 | } else { |
| 455 | return objects_[index].t; |
| 456 | } |
| 457 | } |
| 458 | |
| 459 | const T &operator[](size_t index) const { return at(index); } |
| 460 | T &operator[](size_t index) { return at(index); } |
| 461 | |
| 462 | // Resizes the vector to the requested size. |
| 463 | // size must be less than or equal to the current capacity() of the vector. |
| 464 | // Does not allocate additional memory (call reserve() to allocate additional |
| 465 | // memory). |
| 466 | // Zero-initializes all inline element; initializes all subtable/string |
| 467 | // elements to extant but empty objects. |
| 468 | void resize(size_t size); |
| 469 | |
| 470 | // Resizes an inline vector to the requested size. |
| 471 | // When changing the size of the vector, the removed/inserted elements will be |
| 472 | // set to zero if requested. Otherwise, they will be left uninitialized. |
| 473 | void resize_inline(size_t size, SetZero set_zero) { |
| 474 | CHECK_LE(size, allocated_length_); |
| 475 | static_assert( |
| 476 | kInline, |
| 477 | "Vector::resize_inline() only works for inline vector types (scalars, " |
| 478 | "enums, structs)."); |
| 479 | if (size == length_) { |
| 480 | return; |
| 481 | } |
| 482 | if (set_zero == SetZero::kYes) { |
| 483 | memset( |
| 484 | reinterpret_cast<void *>(inline_data() + std::min(size, length_)), 0, |
| 485 | std::abs(static_cast<ssize_t>(length_) - static_cast<ssize_t>(size)) * |
| 486 | sizeof(InlineType)); |
| 487 | } |
| 488 | length_ = size; |
| 489 | SetLength(length_); |
| 490 | } |
| 491 | // Resizes a vector of offsets to the requested size. |
| 492 | // If the size is increased, the new elements will be initialized |
| 493 | // to empty but extant objects for non-inlined types (so, zero-length |
| 494 | // vectors/strings; objects that exist but have no fields populated). |
| 495 | // Note that this is always equivalent to resize(). |
| 496 | void resize_not_inline(size_t size) { |
| 497 | CHECK_LE(size, allocated_length_); |
| 498 | static_assert(!kInline, |
| 499 | "Vector::resize_not_inline() only works for offset vector " |
| 500 | "types (objects, strings)."); |
| 501 | if (size == length_) { |
| 502 | return; |
| 503 | } else if (length_ > size) { |
| 504 | // TODO: Remove any excess allocated memory. |
| 505 | length_ = size; |
| 506 | SetLength(length_); |
| 507 | return; |
| 508 | } else { |
| 509 | while (length_ < size) { |
| 510 | CHECK_NOTNULL(emplace_back()); |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | // Accessors directly to the inline data of a vector. |
| 516 | const T *data() const { |
| 517 | static_assert(kInline, |
| 518 | "If you have a use-case for directly accessing the " |
| 519 | "flatbuffer data pointer for vectors of " |
| 520 | "objects/strings, please start a discussion."); |
| 521 | return inline_data(); |
| 522 | } |
| 523 | |
| 524 | T *data() { |
| 525 | static_assert(kInline, |
| 526 | "If you have a use-case for directly accessing the " |
| 527 | "flatbuffer data pointer for vectors of " |
| 528 | "objects/strings, please start a discussion."); |
| 529 | return inline_data(); |
| 530 | } |
| 531 | |
James Kuszmaul | 2244805 | 2023-12-14 15:55:14 -0800 | [diff] [blame] | 532 | // Iterators to allow easy use with standard C++ features. |
| 533 | iterator begin() { return iterator(this, 0); } |
| 534 | iterator end() { return iterator(this, size()); } |
| 535 | const_iterator begin() const { return const_iterator(this, 0); } |
| 536 | const_iterator end() const { return const_iterator(this, size()); } |
| 537 | |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 538 | std::string SerializationDebugString() const { |
| 539 | std::stringstream str; |
| 540 | str << "Raw Size: " << kSize << " alignment: " << kAlign |
| 541 | << " allocated length: " << allocated_length_ << " inline alignment " |
| 542 | << kInlineAlign << " kPadding1 " << kPadding1 << "\n"; |
| 543 | str << "Observed length " << GetLength() << " (expected " << length_ |
| 544 | << ")\n"; |
| 545 | str << "Inline Size " << kInlineSize << " Inline bytes/value:\n"; |
| 546 | // TODO(james): Get pretty-printing for structs so we can provide better |
| 547 | // debug. |
| 548 | internal::DebugBytes( |
| 549 | internal::GetSubSpan(vector_buffer(), kLengthSize, |
| 550 | sizeof(InlineType) * allocated_length_), |
| 551 | str); |
| 552 | str << "kPadding2 " << kPadding2 << " offset data size " |
| 553 | << kOffsetOffsetDataSize << "\n"; |
| 554 | return str.str(); |
| 555 | } |
| 556 | |
| 557 | protected: |
| 558 | friend struct internal::TableMover< |
| 559 | Vector<T, kStaticLength, kInline, kForceAlign, kNullTerminate>>; |
| 560 | // protected so that the String class can access the move constructor. |
| 561 | Vector(Vector &&) = default; |
| 562 | |
| 563 | private: |
| 564 | // See kAlign and kOffset. |
| 565 | size_t Alignment() const final { return kAlign; } |
| 566 | size_t AbsoluteOffsetOffset() const override { return kOffset; } |
| 567 | // Returns a buffer that starts at the start of the vector itself (past any |
| 568 | // padding). |
| 569 | std::span<uint8_t> vector_buffer() { |
| 570 | return internal::GetSubSpan(buffer(), kPadding1); |
| 571 | } |
| 572 | std::span<const uint8_t> vector_buffer() const { |
| 573 | return internal::GetSubSpan(buffer(), kPadding1); |
| 574 | } |
| 575 | |
| 576 | bool AddInlineElement(InlineType e) { |
| 577 | if (length_ == allocated_length_) { |
| 578 | return false; |
| 579 | } |
| 580 | SetInlineElement(length_, e); |
| 581 | ++length_; |
| 582 | SetLength(length_); |
| 583 | return true; |
| 584 | } |
| 585 | |
| 586 | void SetInlineElement(size_t index, InlineType value) { |
| 587 | CHECK_LT(index, allocated_length_); |
| 588 | inline_data()[index] = value; |
| 589 | } |
| 590 | |
| 591 | InlineType &GetInlineElement(size_t index) { |
| 592 | CHECK_LT(index, allocated_length_); |
| 593 | return inline_data()[index]; |
| 594 | } |
| 595 | |
| 596 | const InlineType &GetInlineElement(size_t index) const { |
| 597 | CHECK_LT(index, allocated_length_); |
| 598 | return inline_data()[index]; |
| 599 | } |
| 600 | |
| 601 | // Returns a pointer to the start of the inline data itself. |
| 602 | InlineType *inline_data() { |
| 603 | return reinterpret_cast<InlineType *>(vector_buffer().data() + kLengthSize); |
| 604 | } |
| 605 | const InlineType *inline_data() const { |
| 606 | return reinterpret_cast<const InlineType *>(vector_buffer().data() + |
| 607 | kLengthSize); |
| 608 | } |
| 609 | |
| 610 | // Updates the length of the vector to match the provided length. Does not set |
| 611 | // the length_ member. |
| 612 | void SetLength(LengthType length) { |
| 613 | *reinterpret_cast<LengthType *>(vector_buffer().data()) = length; |
| 614 | if (kNullTerminate) { |
| 615 | memset(reinterpret_cast<void *>(inline_data() + length), 0, |
| 616 | sizeof(InlineType)); |
| 617 | } |
| 618 | } |
| 619 | LengthType GetLength() const { |
| 620 | return *reinterpret_cast<const LengthType *>(vector_buffer().data()); |
| 621 | } |
| 622 | |
| 623 | // Overrides to allow ResizeableObject to manage memory adjustments. |
| 624 | size_t NumberOfSubObjects() const final { |
| 625 | return kInline ? 0 : allocated_length_; |
| 626 | } |
| 627 | using ResizeableObject::SubObject; |
| 628 | SubObject GetSubObject(size_t index) final { |
| 629 | return SubObject{ |
| 630 | reinterpret_cast<uoffset_t *>(&GetInlineElement(index)), |
| 631 | // In order to let this compile regardless of whether type T is an |
| 632 | // object type or not, we just use a reinterpret_cast. |
| 633 | (index < length_) |
| 634 | ? reinterpret_cast<ResizeableObject *>(&objects_[index].t) |
| 635 | : nullptr, |
| 636 | &object_absolute_offsets_[index]}; |
| 637 | } |
| 638 | // Implementation that handles copying from a flatbuffers::Vector of an inline |
| 639 | // data type. |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 640 | [[nodiscard]] bool FromInlineFlatbuffer(ConstFlatbuffer &vector) { |
| 641 | return FromData(reinterpret_cast<const InlineType *>(vector.Data()), |
| 642 | vector.size()); |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 643 | } |
| 644 | |
| 645 | // Implementation that handles copying from a flatbuffers::Vector of a |
| 646 | // not-inline data type. |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 647 | template <typename Iterable> |
| 648 | [[nodiscard]] bool FromNotInlineIterable(const Iterable &vector) { |
| 649 | if (!reserve(vector.size())) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 650 | return false; |
| 651 | } |
| 652 | // "Clear" the vector. |
| 653 | resize_not_inline(0); |
| 654 | |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 655 | for (const auto &entry : vector) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 656 | if (!CHECK_NOTNULL(emplace_back())->FromFlatbuffer(entry)) { |
| 657 | return false; |
| 658 | } |
| 659 | } |
| 660 | return true; |
| 661 | } |
| 662 | |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 663 | [[nodiscard]] bool FromNotInlineFlatbuffer(const Flatbuffer &vector) { |
| 664 | return FromNotInlineIterable(vector); |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 665 | } |
| 666 | |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 667 | // In order to allow for easy partial template specialization, we use a |
| 668 | // non-member class to call FromInline/FromNotInlineFlatbuffer and |
| 669 | // resize_inline/resize_not_inline. There are not actually any great ways to |
| 670 | // do this with just our own class member functions, so instead we make these |
| 671 | // methods members of a friend of the Vector class; we then partially |
| 672 | // specialize the entire InlineWrapper class and use it to isolate anything |
| 673 | // that needs to have a common user interface while still having separate |
| 674 | // actual logic. |
| 675 | template <typename T_, bool kInline_, class Enable_> |
| 676 | friend struct internal::InlineWrapper; |
| 677 | |
| 678 | // Note: The objects here really want to be owned by this object (as opposed |
| 679 | // to e.g. returning a stack-allocated object from the emplace_back() methods |
| 680 | // that the user then owns). There are two main challenges with have the user |
| 681 | // own the object on question: |
| 682 | // 1. We can't have >1 reference floating around, or else one object's state |
| 683 | // can become out of date. This forces us to do ref-counting and could |
| 684 | // make certain types of code obnoxious to write. |
| 685 | // 2. Once the user-created object goes out of scope, we lose all of its |
| 686 | // internal state. In _theory_ it should be possible to reconstruct most |
| 687 | // of the relevant state by examining the contents of the buffer, but |
| 688 | // doing so would be cumbersome. |
| 689 | aos::InlinedVector<internal::TableMover<ObjectType>, |
| 690 | kInline ? 0 : kStaticLength> |
| 691 | objects_; |
| 692 | aos::InlinedVector<size_t, kInline ? 0 : kStaticLength> |
| 693 | object_absolute_offsets_; |
| 694 | // Current actual length of the vector. |
| 695 | size_t length_ = 0; |
| 696 | // Current length that we have allocated space available for. |
| 697 | size_t allocated_length_ = kStaticLength; |
| 698 | }; |
| 699 | |
| 700 | template <typename T, size_t kStaticLength, bool kInline, size_t kForceAlign, |
| 701 | bool kNullTerminate> |
| 702 | T *Vector<T, kStaticLength, kInline, kForceAlign, |
| 703 | kNullTerminate>::emplace_back() { |
| 704 | static_assert(!kInline); |
| 705 | if (length_ >= allocated_length_) { |
| 706 | return nullptr; |
| 707 | } |
| 708 | const size_t object_start = object_absolute_offsets_[length_]; |
| 709 | std::span<uint8_t> object_buffer = |
| 710 | internal::GetSubSpan(buffer(), object_start, T::kSize); |
| 711 | objects_.emplace_back(object_buffer, this); |
| 712 | const uoffset_t offset = |
| 713 | object_start - (reinterpret_cast<size_t>(&GetInlineElement(length_)) - |
| 714 | reinterpret_cast<size_t>(buffer().data())); |
| 715 | CHECK(AddInlineElement(offset)); |
| 716 | return &objects_[objects_.size() - 1].t; |
| 717 | } |
| 718 | |
| 719 | // The String class is a special version of the Vector that is always |
| 720 | // null-terminated, always contains 1-byte character elements, and which has a |
| 721 | // few extra methods for convenient string access. |
| 722 | template <size_t kStaticLength> |
| 723 | class String : public Vector<char, kStaticLength, true, 0, true> { |
| 724 | public: |
| 725 | typedef Vector<char, kStaticLength, true, 0, true> VectorType; |
| 726 | typedef flatbuffers::String Flatbuffer; |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 727 | typedef std::string FlatbufferObjectType; |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 728 | String(std::span<uint8_t> buffer, ResizeableObject *parent) |
| 729 | : VectorType(buffer, parent) {} |
| 730 | virtual ~String() {} |
| 731 | void SetString(std::string_view string) { |
| 732 | CHECK_LT(string.size(), VectorType::capacity()); |
| 733 | VectorType::resize_inline(string.size(), SetZero::kNo); |
| 734 | memcpy(VectorType::data(), string.data(), string.size()); |
| 735 | } |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 736 | using VectorType::FromFlatbuffer; |
| 737 | [[nodiscard]] bool FromFlatbuffer(const std::string &string) { |
| 738 | return VectorType::FromData(string.data(), string.size()); |
| 739 | } |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 740 | std::string_view string_view() const { |
| 741 | return std::string_view(VectorType::data(), VectorType::size()); |
| 742 | } |
| 743 | std::string str() const { |
| 744 | return std::string(VectorType::data(), VectorType::size()); |
| 745 | } |
| 746 | const char *c_str() const { return VectorType::data(); } |
| 747 | |
| 748 | private: |
| 749 | friend struct internal::TableMover<String<kStaticLength>>; |
| 750 | String(String &&) = default; |
| 751 | }; |
| 752 | |
| 753 | namespace internal { |
| 754 | // Specialization for all non-inline vector types. All of these types will just |
| 755 | // use offsets for their inline data and have appropriate member types/constants |
| 756 | // for the remaining fields. |
| 757 | template <typename T> |
| 758 | struct InlineWrapper<T, false, void> { |
| 759 | typedef uoffset_t Type; |
| 760 | typedef T ObjectType; |
| 761 | typedef flatbuffers::Offset<typename T::Flatbuffer> FlatbufferType; |
| 762 | typedef flatbuffers::Offset<typename T::Flatbuffer> ConstFlatbufferType; |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 763 | typedef T::FlatbufferObjectType FlatbufferObjectType; |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 764 | static_assert((T::kSize % T::kAlign) == 0); |
| 765 | static constexpr size_t kDataAlign = T::kAlign; |
| 766 | static constexpr size_t kDataSize = T::kSize; |
| 767 | template <typename StaticVector> |
| 768 | static bool FromFlatbuffer( |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 769 | StaticVector *to, const typename StaticVector::ConstFlatbuffer &from) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 770 | return to->FromNotInlineFlatbuffer(from); |
| 771 | } |
| 772 | template <typename StaticVector> |
| 773 | static void ResizeVector(StaticVector *target, size_t size) { |
| 774 | target->resize_not_inline(size); |
| 775 | } |
| 776 | }; |
| 777 | // Specialization for "normal" scalar inline data (ints, floats, doubles, |
| 778 | // enums). |
| 779 | template <typename T> |
| 780 | struct InlineWrapper<T, true, |
| 781 | typename std::enable_if_t<!std::is_class<T>::value>> { |
| 782 | typedef T Type; |
| 783 | typedef T ObjectType; |
| 784 | typedef T FlatbufferType; |
| 785 | typedef T ConstFlatbufferType; |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 786 | typedef T *FlatbufferObjectType; |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 787 | static constexpr size_t kDataAlign = alignof(T); |
| 788 | static constexpr size_t kDataSize = sizeof(T); |
| 789 | template <typename StaticVector> |
| 790 | static bool FromFlatbuffer( |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 791 | StaticVector *to, const typename StaticVector::ConstFlatbuffer &from) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 792 | return to->FromInlineFlatbuffer(from); |
| 793 | } |
| 794 | template <typename StaticVector> |
| 795 | static void ResizeVector(StaticVector *target, size_t size) { |
| 796 | target->resize_inline(size, SetZero::kYes); |
| 797 | } |
| 798 | }; |
| 799 | // Specialization for booleans, given that flatbuffers uses uint8_t's for bools. |
| 800 | template <> |
| 801 | struct InlineWrapper<bool, true, void> { |
| 802 | typedef uint8_t Type; |
| 803 | typedef uint8_t ObjectType; |
| 804 | typedef uint8_t FlatbufferType; |
| 805 | typedef uint8_t ConstFlatbufferType; |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 806 | typedef uint8_t *FlatbufferObjectType; |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 807 | static constexpr size_t kDataAlign = 1u; |
| 808 | static constexpr size_t kDataSize = 1u; |
| 809 | template <typename StaticVector> |
| 810 | static bool FromFlatbuffer( |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 811 | StaticVector *to, const typename StaticVector::ConstFlatbuffer &from) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 812 | return to->FromInlineFlatbuffer(from); |
| 813 | } |
| 814 | template <typename StaticVector> |
| 815 | static void ResizeVector(StaticVector *target, size_t size) { |
| 816 | target->resize_inline(size, SetZero::kYes); |
| 817 | } |
| 818 | }; |
| 819 | // Specialization for flatbuffer structs. |
| 820 | // The flatbuffers codegen uses struct pointers rather than references or the |
| 821 | // such, so it needs to be treated special. |
| 822 | template <typename T> |
| 823 | struct InlineWrapper<T, true, |
| 824 | typename std::enable_if_t<std::is_class<T>::value>> { |
| 825 | typedef T Type; |
| 826 | typedef T ObjectType; |
| 827 | typedef T *FlatbufferType; |
| 828 | typedef const T *ConstFlatbufferType; |
James Kuszmaul | 6be4102 | 2023-12-20 11:55:28 -0800 | [diff] [blame] | 829 | typedef T *FlatbufferObjectType; |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 830 | static constexpr size_t kDataAlign = alignof(T); |
| 831 | static constexpr size_t kDataSize = sizeof(T); |
| 832 | template <typename StaticVector> |
| 833 | static bool FromFlatbuffer( |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 834 | StaticVector *to, const typename StaticVector::ConstFlatbuffer &from) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 835 | return to->FromInlineFlatbuffer(from); |
| 836 | } |
| 837 | template <typename StaticVector> |
| 838 | static void ResizeVector(StaticVector *target, size_t size) { |
| 839 | target->resize_inline(size, SetZero::kYes); |
| 840 | } |
| 841 | }; |
| 842 | } // namespace internal |
| 843 | // |
| 844 | template <typename T, size_t kStaticLength, bool kInline, size_t kForceAlign, |
| 845 | bool kNullTerminate> |
| 846 | bool Vector<T, kStaticLength, kInline, kForceAlign, |
James Kuszmaul | 692780f | 2023-12-20 14:01:56 -0800 | [diff] [blame] | 847 | kNullTerminate>::FromFlatbuffer(ConstFlatbuffer &vector) { |
James Kuszmaul | f5eb468 | 2023-09-22 17:16:59 -0700 | [diff] [blame] | 848 | return internal::InlineWrapper<T, kInline>::FromFlatbuffer(this, vector); |
| 849 | } |
| 850 | |
| 851 | template <typename T, size_t kStaticLength, bool kInline, size_t kForceAlign, |
| 852 | bool kNullTerminate> |
| 853 | void Vector<T, kStaticLength, kInline, kForceAlign, kNullTerminate>::resize( |
| 854 | size_t size) { |
| 855 | internal::InlineWrapper<T, kInline>::ResizeVector(this, size); |
| 856 | } |
| 857 | |
| 858 | } // namespace aos::fbs |
| 859 | #endif // AOS_FLATBUFFERS_STATIC_VECTOR_H_ |