36#ifndef BUFFER_H_INCLUDED
37#define BUFFER_H_INCLUDED
39#include "memutils/s_stl/s_stl_config.h"
50 unsigned char data[
sizeof(T)];
53 SEG() : index(EMPTY){}
60 Buffer(): search_top(buffer) {}
63 for(SEG* area_p = search_top;area_p<&buffer[N];area_p++){
64 if(area_p->index == EMPTY){
66 search_top = area_p+1;
68 return reinterpret_cast<T*
>(&(area_p->data));
75 SEG* area_p =
reinterpret_cast<SEG*
>(data_p);
76 if((area_p < &buffer[0]) || (area_p >= &buffer[N]))
return FALSE;
77 if(area_p->index == EMPTY)
return FALSE;
79 area_p->index = EMPTY;
80 if(search_top > area_p){ search_top = area_p;}
86 T* area_p =
reinterpret_cast<T*
>(buffer[i].data);
87 if(buffer[i].index == FULL) area_p->~T();
88 buffer[i].index = EMPTY;
99 unsigned char buffer[
sizeof(T)*N];
106 RingBuffer(): head_p(
static_cast<T*
>(
static_cast<void*
>(buffer))), tail_p(head_p), cnt(0){}
108 bool alloc_front(
const T& new_data){
109 if(cnt>=N){
return FALSE; }
else { cnt++; }
111 if(head_p+1 >
static_cast<T*
>(
static_cast<void*
>(&buffer[
sizeof(T)*(N-1)])) ){
112 head_p =
static_cast<T*
>(
static_cast<void*
>(&buffer[0]));
117 new(head_p) T(new_data);
121 bool alloc_back(
const T& new_data){
122 if(cnt>=N){
return FALSE; }
else { cnt++; }
124 if(tail_p-1 <
static_cast<T*
>(
static_cast<void*
>(&buffer[0]))){
125 tail_p =
static_cast<T*
>(
static_cast<void*
>(&buffer[
sizeof(T)*(N-1)]));
130 new(tail_p) T(new_data);
134 bool free_front(
void){
135 if(cnt<=0){
return FALSE; }
else { cnt--; }
138 if(head_p-1 <
static_cast<T*
>(
static_cast<void*
>(&buffer[0]))){
139 head_p =
static_cast<T*
>(
static_cast<void*
>(&buffer[
sizeof(T)*(N-1)]));
147 bool free_back(
void){
148 if(cnt<=0){
return FALSE; }
else {cnt--;}
151 if(tail_p+1 >
static_cast<T*
>(
static_cast<void*
>(&buffer[
sizeof(T)*(N-1)]))){
152 tail_p =
static_cast<T*
>(
static_cast<void*
>(&buffer[0]));
160 const T& get_front(
void)
const {
165 const T& get_back(
void)
const {
171 const T& at_front(
const int n)
const {
173 if(head_p-n+N >
reinterpret_cast<const T*
>(&buffer[
sizeof(T)*(N-1)])){
176 return *(head_p-n+N);
180 const T& at_back(
const int n)
const {
182 if(tail_p+n >
reinterpret_cast<const T*
>(&buffer[
sizeof(T)*(N-1)])){
183 return *(tail_p+n-N);
190 bool empty(
void)
const {
return (cnt==0); }
191 bool full(
void)
const {
return (cnt==N); }