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