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_) {
185 roc_log(
LogError,
"array: can't allocate memory: old_size=%lu new_size=%lu",
186 (
unsigned long)max_size_, (
unsigned long)
max_sz);
192 for (
size_t n = 0; n < size_; n++) {
197 for (
size_t n = size_; n > 0; n--) {
228 if (max_size_ < 1024) {
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.
T * data()
Get pointer to first element.
Array()
Initialize empty array without arena.
void clear()
Set array size to zero.
const T * data() const
Get pointer to first element.
ROC_ATTR_NODISCARD bool push_back(const T &value)
Append element to array.
Array(IArena &arena)
Initialize empty array with arena.
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.
T & operator[](size_t index)
Get element at given position.
size_t size() const
Get number of elements.
virtual void * allocate(size_t size)=0
Allocate memory.
virtual void deallocate(void *)=0
Deallocate previously allocated memory.
Base class for non-copyable objects.
Shared ownership intrusive pointer.
#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.