12 #ifndef ROC_CORE_RING_QUEUE_H_
13 #define ROC_CORE_RING_QUEUE_H_
49 roc_panic(
"ring queue: the length must be greater than 0");
52 buff_ = allocate_(max_len);
73 return (end_ - begin_ + buff_len_) % buff_len_;
78 return begin_ == end_;
91 roc_panic(
"ring queue: front() called on empty buffer");
101 roc_panic(
"ring queue: front() called on empty buffer");
103 return buff_[begin_];
111 roc_panic(
"ring queue: back() called on empty buffer");
113 return buff_[(end_ - 1 + buff_len_) % buff_len_];
121 roc_panic(
"ring queue: back() called on empty buffer");
123 return buff_[(end_ - 1 + buff_len_) % buff_len_];
131 roc_panic(
"ring queue: buffer overflow");
133 begin_ = (begin_ - 1 + buff_len_) % buff_len_;
134 new (&buff_[begin_]) T(x);
142 roc_panic(
"ring queue: pop_front() called on empty buffer");
145 begin_ = (begin_ + 1) % buff_len_;
153 roc_panic(
"ring queue: buffer overflow");
155 new (&buff_[end_]) T(x);
156 end_ = (end_ + 1) % buff_len_;
164 roc_panic(
"ring queue: pop_back() called on empty buffer");
167 end_ = (end_ - 1 + buff_len_) % buff_len_;
171 T* allocate_(
size_t n_elems) {
174 if (n_elems <= EmbeddedCapacity) {
175 data = (T*)embedded_data_.memory();
177 data = (T*)arena_.
allocate(n_elems *
sizeof(T));
182 "ring queue: can't allocate memory:"
183 " requested_cap=%lu embedded_cap=%lu",
184 (
unsigned long)n_elems, (
unsigned long)EmbeddedCapacity);
190 void deallocate_(T* data) {
191 if ((
void*)data != (
void*)embedded_data_.memory()) {
201 AlignedStorage<EmbeddedCapacity *
sizeof(T)> embedded_data_;
virtual void deallocate(void *ptr)=0
Deallocate previously allocated memory.
virtual void * allocate(size_t size)=0
Allocate memory.
Base class for non-copyable objects.
Queue on continuous memory buffer.
bool is_valid() const
Check that initial allocation succeeded.
const T & back() const
Get reference of the back element.
RingQueue(core::IArena &arena, size_t max_len)
Initialize.
void push_front(const T &x)
Push an element to the front of the queue.
T & front()
Get reference of the front element.
bool is_full()
Is the queue full.
void pop_back()
Remove the first element from the back.
bool is_empty()
Is the queue empty.
size_t capacity() const
Get maximum number of elements in queue/.
const T & front() const
Get reference of the front element.
T & back()
Get reference of the back element.
void pop_front()
Remove the first element from the front.
size_t size() const
Get current number of elements in the queue.
void push_back(const T &x)
Push an element to the backside of the queue.
#define roc_log(level,...)
Print message to log.
#define roc_panic(...)
Print error message and terminate program gracefully.