12 #ifndef ROC_CORE_ARRAY_H_
13 #define ROC_CORE_ARRAY_H_
109 if (index >= size_) {
110 roc_panic(
"array: subscript out of range: index=%lu size=%lu",
111 (
unsigned long)index, (
unsigned long)size_);
118 if (index >= size_) {
119 roc_panic(
"array: subscript out of range: index=%lu size=%lu",
120 (
unsigned long)index, (
unsigned long)size_);
133 new (data_ + size_) T(value);
151 for (
size_t n = size_; n < sz; n++) {
156 for (
size_t n = size_; n > sz; n--) {
179 if (max_sz <= max_size_) {
183 T* new_data = allocate_(max_sz);
185 roc_log(
LogError,
"array: can't allocate memory: old_size=%lu new_size=%lu",
186 (
unsigned long)max_size_, (
unsigned long)max_sz);
190 if (new_data != data_) {
192 for (
size_t n = 0; n < size_; n++) {
193 new (new_data + n) T(data_[n]);
197 for (
size_t n = size_; n > 0; n--) {
222 if (min_size <= max_size_) {
226 size_t new_max_size_ = max_size_;
228 if (max_size_ < 1024) {
229 while (min_size > new_max_size_) {
230 new_max_size_ = (new_max_size_ == 0) ? 2 : new_max_size_ * 2;
233 while (min_size > new_max_size_) {
234 new_max_size_ += new_max_size_ / 4;
238 return grow(new_max_size_);
242 T* allocate_(
size_t n_elems) {
243 if (n_elems <= EmbeddedCapacity) {
244 return (T*)embedded_data_.memory();
246 return (T*)arena_->
allocate(n_elems *
sizeof(T));
252 void deallocate_(T*
data) {
253 if ((
void*)
data != (
void*)embedded_data_.memory()) {
265 AlignedStorage<EmbeddedCapacity *
sizeof(T)> embedded_data_;
#define ROC_ATTR_NODISCARD
Emit warning if function result is not checked.
ROC_ATTR_NODISCARD bool grow_exp(size_t min_size)
Increase array capacity exponentially.
Array()
Initialize empty array without arena.
const T * data() const
Get pointer to first element.
void clear()
Set array size to zero.
ROC_ATTR_NODISCARD bool push_back(const T &value)
Append element to array.
T * data()
Get pointer to first element.
Array(IArena &arena)
Initialize empty array with arena.
T & operator[](size_t index)
Get element at given position.
ROC_ATTR_NODISCARD bool resize(size_t sz)
Set array size.
ROC_ATTR_NODISCARD bool grow(size_t max_sz)
Increase array capacity.
bool is_empty() const
Check if size is zero.
size_t capacity() const
Get maximum number of elements. If array has arena, capacity can be grown.
const T & operator[](size_t index) const
Get element at given position.
size_t size() const
Get number of elements.
virtual void deallocate(void *)=0
Deallocate previously allocated memory.
virtual void * allocate(size_t size)=0
Allocate memory.
Base class for non-copyable objects.
#define roc_log(level,...)
Print message to log.
#define roc_panic_if(x)
Panic if condition is true.
#define roc_panic(...)
Print error message and terminate program gracefully.
Commonly used types and functions.