12 #include <type_traits> 14 #include "quill/core/Attributes.h" 15 #include "quill/core/Common.h" 16 #include "quill/core/QuillError.h" 18 #if defined(__GNUC__) && !defined(__clang__) 19 #pragma GCC diagnostic push 20 #pragma GCC diagnostic ignored "-Warray-bounds" 21 #pragma GCC diagnostic ignored "-Wstringop-overflow" 22 #elif defined(__clang__) 23 #pragma GCC diagnostic push 24 #pragma GCC diagnostic ignored "-Warray-bounds" 25 #elif defined(_WIN32) && defined(_MSC_VER) 27 #pragma warning(disable : 4789) 35 template <
typename T,
size_t N>
40 static_assert(std::is_trivially_copyable_v<value_type>,
"value_type must be trivially copyable");
44 for (
size_t i = 0; i < _capacity; ++i)
46 _storage.inline_buffer[i] = value_type{};
54 delete[] _storage.heap_buffer;
69 if (_size == _capacity)
71 if (QUILL_UNLIKELY(_capacity > (SIZE_MAX / 2)))
73 QUILL_THROW(
QuillError{
"InlinedVector capacity overflow"});
76 size_t const new_capacity = _capacity * 2;
77 auto* new_data =
new value_type[new_capacity];
83 std::memcpy(new_data, _storage.inline_buffer, _size *
sizeof(value_type));
88 std::memcpy(new_data, _storage.heap_buffer, _size *
sizeof(value_type));
89 delete[] _storage.heap_buffer;
92 _storage.heap_buffer = new_data;
93 _capacity = new_capacity;
98 _storage.inline_buffer[_size] = value;
102 _storage.heap_buffer[_size] = value;
113 QUILL_ATTRIBUTE_HOT value_type
operator[](
size_t index)
const 115 if (QUILL_UNLIKELY(index >= _size))
117 QUILL_THROW(
QuillError{
"index out of bounds"});
122 return _storage.inline_buffer[index];
126 return _storage.heap_buffer[index];
135 if (QUILL_UNLIKELY(index >= _size))
137 QUILL_THROW(
QuillError{
"index out of bounds"});
142 _storage.inline_buffer[index] = value;
146 _storage.heap_buffer[index] = value;
150 QUILL_NODISCARD QUILL_ATTRIBUTE_HOT
size_t size()
const noexcept {
return _size; }
151 QUILL_NODISCARD
size_t capacity()
const noexcept {
return _capacity; }
152 QUILL_ATTRIBUTE_HOT
void clear() noexcept { _size = 0; }
157 value_type inline_buffer[N];
158 value_type* heap_buffer;
171 "SizeCacheVector should not exceed a cache line");
176 #if defined(__GNUC__) && !defined(__clang__) 177 #pragma GCC diagnostic pop 178 #elif defined(__clang__) 179 #pragma GCC diagnostic pop 180 #elif defined(_WIN32) && defined(_MSC_VER) QUILL_ATTRIBUTE_HOT value_type push_back(value_type value)
Push back a new element.
Definition: InlinedVector.h:67
QUILL_ATTRIBUTE_HOT value_type operator[](size_t index) const
Access element.
Definition: InlinedVector.h:113
QUILL_ATTRIBUTE_HOT void assign(size_t index, value_type value)
Assign value at index.
Definition: InlinedVector.h:133
Definition: InlinedVector.h:36
Setups a signal handler to handle fatal signals.
Definition: BackendManager.h:28
constexpr size_t QUILL_CACHE_LINE_SIZE
Cache line constants.
Definition: Common.h:67
custom exception
Definition: QuillError.h:47