void initRingbuffer(ringbuffer *buf, int size)
{
+ buf->count = 0;
buf->tail = 0;
buf->head = 0;
buf->max = size;
void addValue(ringbuffer *buf, int value)
{
+ if (buf->count >= buf->max)
+ buf->count = buf->max;
+
if (buf->head >= buf->max)
buf->head = 0;
buf->position[buf->head] = value;
buf->head++;
-
+ buf->count++;
}
int getValue(ringbuffer *buf)
{
+ if (buf->count < 0)
+ return -1;
+
if (buf->tail >= buf->max)
buf->tail = 0;
int value = buf->position[buf->tail];
buf->tail++;
+ buf->count--;
return value;
}