36#ifndef RUNTIME_QUE_H_INCLUDED
37#define RUNTIME_QUE_H_INCLUDED
41#include "memutils/common_utils/common_assert.h"
42#include "memutils/os_utils/cpp_util.h"
44#ifdef RUNTIME_QUE_DEBUG
47#define DBG_P(...) printf(__VA_ARGS__)
56 typename NumT =
size_t>
58 void post_push() { ++m_count;
if (++m_put == capacity()) m_put = 0; }
59 void post_pop() { --m_count;
if (++m_get == capacity()) m_get = 0; }
61 NumT get_index(NumT n)
const {
62 return static_cast<NumT
>((m_get + n < capacity()) ? m_get + n : m_get + n - capacity());
67 m_data(
static_cast<T*
>(area)),
69 m_put(0), m_get(0), m_count(0) {
74 const void* que_area()
const {
return m_data; }
75 size_t elem_size()
const {
return sizeof(T); }
76 NumT capacity()
const {
return m_capacity; }
77 NumT size()
const {
return m_count; }
78 NumT rest()
const {
return static_cast<NumT
>(capacity() - size()); }
79 bool empty()
const {
return size() == 0; }
80 bool full()
const {
return size() == capacity(); }
83 printf(
"dump: put=%u, get=%u, capa=%u, size=%u, rest=%u, empty=%d, full=%d\n",
84 m_put, m_get, capacity(), size(), rest(), empty(), full());
85 for (NumT i = 0; i < size(); ++i) {
86 printf(
"%u: data=%08x\n",
87 i, *
reinterpret_cast<const int*
>(&m_data[get_index(i)]));
100 bool push(
const T& data) {
101 if (full())
return false;
102 ::new(&m_data[m_put]) T(data);
103 DBG_P(
"push: pos=%u, cnt=%u\n", m_put, m_count);
111 if (empty())
return false;
112 DBG_P(
"pop : pos=%u, cnt=%u\n", m_get, m_count);
120 const T& top()
const {
return at(0); }
124 const T& at(NumT n)
const {
125 D_ASSERT(n < m_count);
128 DBG_P(
"at(%u): pos=%u\n", n, m_get);
129 return m_data[get_index(n)];
132 T& writable_at(NumT n) {
return const_cast<T&
>(at(n)); }
134 const T& front()
const {
return at(0); }
135 T& front() {
return writable_at(0); }
137 const T& back()
const {
return at(
static_cast<NumT
>(m_count - 1)); }
138 T& back() {
return writable_at(
static_cast<NumT
>(m_count - 1)); }
Definition: cpp_util.h:45
Definition: RuntimeQue.h:57