blob: 4febf55593b33c078faed094186fab67cd708c26 [file] [log] [blame]
Brian Silvermand844a572019-01-26 15:59:00 -08001#ifndef AOS_CONTAINERS_SIZED_ARRAY_H_
2#define AOS_CONTAINERS_SIZED_ARRAY_H_
3
Stephan Pleines5fc35072024-05-22 17:33:18 -07004#include <cstddef>
5
Tyler Chatowd0a49742022-02-25 22:06:19 -08006#include "absl/container/inlined_vector.h"
Brian Silvermand844a572019-01-26 15:59:00 -08007
8namespace aos {
9
Tyler Chatowd0a49742022-02-25 22:06:19 -080010// Minimal compliant allocator whose allocating operations are all fatal.
11template <typename T>
12class FatalAllocator {
Brian Silvermand844a572019-01-26 15:59:00 -080013 public:
Tyler Chatowd0a49742022-02-25 22:06:19 -080014 using value_type = T;
Brian Silvermand844a572019-01-26 15:59:00 -080015
Tyler Chatowd0a49742022-02-25 22:06:19 -080016 [[nodiscard, noreturn]] T *allocate(std::size_t) { __builtin_trap(); }
Brian Silvermand844a572019-01-26 15:59:00 -080017
Tyler Chatowd0a49742022-02-25 22:06:19 -080018 [[noreturn]] void deallocate(T *, std::size_t) { __builtin_trap(); }
Brian Silvermand844a572019-01-26 15:59:00 -080019};
20
Tyler Chatowd0a49742022-02-25 22:06:19 -080021// Reuse the logic from absl::InlinedVector for a statically allocated,
22// dynamically sized list of values. InlinedVector's default behavior is to
23// allocate from the heap when growing beyond the static capacity, make this
24// fatal instead to enforce RT guarantees.
25template <typename T, size_t N>
26using SizedArray = absl::InlinedVector<T, N, FatalAllocator<T>>;
27
Brian Silvermand844a572019-01-26 15:59:00 -080028} // namespace aos
29
30#endif // AOS_CONTAINERS_SIZED_ARRAY_H_