*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
/** \file ringbuffer.c Simple ringbuffer implementation */
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
/** \file ringbuffer.c Simple ringbuffer implementation */
* \return A 'cookie' which identifies the ringbuffer just created and
* which must be passed to ringbuffer_add() and ringbuffer_get().
*/
* \return A 'cookie' which identifies the ringbuffer just created and
* which must be passed to ringbuffer_add() and ringbuffer_get().
*/
{
struct ringbuffer *rb = para_calloc(sizeof(struct ringbuffer));
rb->entries = para_calloc(size * sizeof(void *));
rb->size = size;
return rb;
{
struct ringbuffer *rb = para_calloc(sizeof(struct ringbuffer));
rb->entries = para_calloc(size * sizeof(void *));
rb->size = size;
return rb;
* \param data The data to be inserted.
*
* Insert \a data into the ringbuffer associated with \a cookie. As soon as
* \param data The data to be inserted.
*
* Insert \a data into the ringbuffer associated with \a cookie. As soon as
* \return The old \a data pointer which is going to be disregarded, or
* NULL if the ringbuffer is not yet full.
*/
* \return The old \a data pointer which is going to be disregarded, or
* NULL if the ringbuffer is not yet full.
*/
void *ret = rb->entries[rb->head];
rb->entries[rb->head] = data;
rb->head = (rb->head + 1) % rb->size;
void *ret = rb->entries[rb->head];
rb->entries[rb->head] = data;
rb->head = (rb->head + 1) % rb->size;
* \param num The number of the entry.
*
* \return A pointer to data previously added, or NULL if entry number
* \a num is not available. \a num counts backwards from zero, i.e.
* ringbuffer_get_entry(0) gets the entry which was added most recently.
*/
* \param num The number of the entry.
*
* \return A pointer to data previously added, or NULL if entry number
* \a num is not available. \a num counts backwards from zero, i.e.
* ringbuffer_get_entry(0) gets the entry which was added most recently.
*/
int pos = (rb->head + rb->size - 1 - num) % rb->size;
// fprintf(stderr, "pos = %d\n", pos);
return rb->entries[pos];
int pos = (rb->head + rb->size - 1 - num) % rb->size;
// fprintf(stderr, "pos = %d\n", pos);
return rb->entries[pos];