Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 1 | #ifndef AOS_CONTAINERS_SIZED_ARRAY_H_ |
| 2 | #define AOS_CONTAINERS_SIZED_ARRAY_H_ |
| 3 | |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame^] | 4 | #include "absl/container/inlined_vector.h" |
Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 5 | |
| 6 | namespace aos { |
| 7 | |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame^] | 8 | // Minimal compliant allocator whose allocating operations are all fatal. |
| 9 | template <typename T> |
| 10 | class FatalAllocator { |
Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 11 | public: |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame^] | 12 | using value_type = T; |
Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 13 | |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame^] | 14 | [[nodiscard, noreturn]] T *allocate(std::size_t) { __builtin_trap(); } |
Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 15 | |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame^] | 16 | [[noreturn]] void deallocate(T *, std::size_t) { __builtin_trap(); } |
Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 17 | }; |
| 18 | |
Tyler Chatow | d0a4974 | 2022-02-25 22:06:19 -0800 | [diff] [blame^] | 19 | // 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. |
| 23 | template <typename T, size_t N> |
| 24 | using SizedArray = absl::InlinedVector<T, N, FatalAllocator<T>>; |
| 25 | |
Brian Silverman | d844a57 | 2019-01-26 15:59:00 -0800 | [diff] [blame] | 26 | } // namespace aos |
| 27 | |
| 28 | #endif // AOS_CONTAINERS_SIZED_ARRAY_H_ |