2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file ringbuffer.c Simple ringbuffer implementation */
12 #include "ringbuffer.h"
16 * Holds all information about one ring buffer
18 * It is intentionally not exported via ringbuffer.h. Think abstract.
22 /** The size of this ring buffer. */
24 /** The actual entries of the ringbuffer. */
26 /** The next entry will be added at this position. */
28 /** How many entries the ring buffer contains. */
33 * Initialize a new ringbuffer.
35 * \param size The number of entries the ringbuffer holds.
37 * This function initializes a circular ring buffer which can hold up to \a
38 * size entries of arbitrary type. If performance is an issue, \a size should
39 * be a power of two to make the underlying modulo operations cheap. Arbitrary
40 * many ringbuffers may be initialized via this function. Each ringbuffer is
41 * identified by a 'cookie'.
43 * \return A 'cookie' which identifies the ringbuffer just created and
44 * which must be passed to ringbuffer_add() and ringbuffer_get().
46 struct ringbuffer *ringbuffer_new(unsigned size)
48 struct ringbuffer *rb = para_calloc(sizeof(struct ringbuffer));
49 rb->entries = para_calloc(size * sizeof(void *));
55 * Add one entry to a ringbuffer.
57 * \param rb The ringbuffer identifier.
58 * \param data The data to be inserted.
60 * Insert \a data into the ringbuffer associated with \a cookie. As soon as
61 * the ringbuffer fills up, its oldest entry is disregarded and replaced by \a
64 * \return The old \a data pointer which is going to be disregarded, or
65 * NULL if the ringbuffer is not yet full.
67 void *ringbuffer_add(struct ringbuffer *rb, void *data)
69 void *ret = rb->entries[rb->head];
70 rb->entries[rb->head] = data;
71 rb->head = (rb->head + 1) % rb->size;
72 if (rb->filled < rb->size)
78 * Get one entry from a ringbuffer.
80 * \param rb The ringbuffer identifier.
81 * \param num The number of the entry.
83 * \return A pointer to data previously added, or NULL if entry number
84 * \a num is not available. \a num counts backwards from zero, i.e.
85 * ringbuffer_get_entry(0) gets the entry which was added most recently.
87 void *ringbuffer_get(struct ringbuffer *rb, int num)
89 int pos = (rb->head + rb->size - 1 - num) % rb->size;
90 // fprintf(stderr, "pos = %d\n", pos);
91 return rb->entries[pos];
95 * Get the number of entries in the ring buffer.
97 * \param rb The ringbuffer identifier
99 * \return This function always succeeds. It returns a number less than the
100 * size of the ring buffer.
102 unsigned ringbuffer_filled(struct ringbuffer *rb)