blob: 8a8d2dea1932fe87722b84231e64c3d13267ec45 [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
Tyler Chatowd0a49742022-02-25 22:06:19 -08004#include "absl/container/inlined_vector.h"
Brian Silvermand844a572019-01-26 15:59:00 -08005
6namespace aos {
7
Tyler Chatowd0a49742022-02-25 22:06:19 -08008// Minimal compliant allocator whose allocating operations are all fatal.
9template <typename T>
10class FatalAllocator {
Brian Silvermand844a572019-01-26 15:59:00 -080011 public:
Tyler Chatowd0a49742022-02-25 22:06:19 -080012 using value_type = T;
Brian Silvermand844a572019-01-26 15:59:00 -080013
Tyler Chatowd0a49742022-02-25 22:06:19 -080014 [[nodiscard, noreturn]] T *allocate(std::size_t) { __builtin_trap(); }
Brian Silvermand844a572019-01-26 15:59:00 -080015
Tyler Chatowd0a49742022-02-25 22:06:19 -080016 [[noreturn]] void deallocate(T *, std::size_t) { __builtin_trap(); }
Brian Silvermand844a572019-01-26 15:59:00 -080017};
18
Tyler Chatowd0a49742022-02-25 22:06:19 -080019// Reuse the logic from absl::InlinedVector for a statically allocated,
20// dynamically sized list of values. InlinedVector's default behavior is to
21// allocate from the heap when growing beyond the static capacity, make this
22// fatal instead to enforce RT guarantees.
23template <typename T, size_t N>
24using SizedArray = absl::InlinedVector<T, N, FatalAllocator<T>>;
25
Brian Silvermand844a572019-01-26 15:59:00 -080026} // namespace aos
27
28#endif // AOS_CONTAINERS_SIZED_ARRAY_H_