circbuf.txt 1.33 KB
Newer Older
Ercan Ersoy's avatar
Ercan Ersoy committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
CIRCBUF.TXT

License CC0 PUBLIC DOMAIN

To the extent possible under law, Mark J. Olesen has waived all copyright 
and related or neighboring rights to TUI Library. This work is published 
from: United States.

source: http://www.tophatstuff.co.uk/index.html@p=116.html
retrieved: 08/10/2106
no author was mentioned to credit

A circular buffer or ring buffer is a fixed-size data structure that wraps from end to end. New data overwrites older data, but it can be written to infinitely. This makes it ideal for buffering keyboard input or streaming data, like real-time sound or video, where old data less important.

It looks simple, but it can be unintuitive to implement when you are used to linear memory.

Here is a simple C implementation which I release into the public domain (you are free to use it without credit):

#define BUFFER_SIZE 256

item *buffer[BUFFER_SIZE];
int start = 0;
int end = 0
int active = 0;

void PushToQueue(item *p)
{
    buffer[end] = p;
    end = (end + 1) % BUFFER_SIZE;

    if (active < BUFFER_SIZE)
    {
        active++;
    } else {
        /* Overwriting the oldest. Move start to next-oldest */
        start = (start + 1) % BUFFER_SIZE;
    }
}

item *RetrieveFromQueue(void)
{
    item *p;

    if (!active) { return NULL; }

    p = buffer[start];
    start = (start + 1) % BUFFER_SIZE;

    active--;
    return p;
}