Remove ->init() of struct receiver.
[paraslash.git] / ringbuffer.c
1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file ringbuffer.c Simple ringbuffer implementation */
4
5 #include <regex.h>
6
7 #include "para.h"
8 #include "ringbuffer.h"
9 #include "string.h"
10
11 /**
12 * Holds all information about one ring buffer
13 *
14 * It is intentionally not exported via ringbuffer.h. Think abstract.
15 */
16 struct ringbuffer
17 {
18 /** The size of this ring buffer. */
19 unsigned size;
20 /** The actual entries of the ringbuffer. */
21 void **entries;
22 /** The next entry will be added at this position. */
23 int head;
24 /** How many entries the ring buffer contains. */
25 unsigned filled;
26 };
27
28 /**
29 * Initialize a new ringbuffer.
30 *
31 * \param size The number of entries the ringbuffer holds.
32 *
33 * This function initializes a circular ring buffer which can hold up to \a
34 * size entries of arbitrary type. If performance is an issue, \a size should
35 * be a power of two to make the underlying modulo operations cheap. Arbitrary
36 * many ringbuffers may be initialized via this function. Each ringbuffer is
37 * identified by a 'cookie'.
38 *
39 * \return A 'cookie' which identifies the ringbuffer just created and
40 * which must be passed to ringbuffer_add() and ringbuffer_get().
41 */
42 struct ringbuffer *ringbuffer_new(unsigned size)
43 {
44 struct ringbuffer *rb = para_calloc(sizeof(struct ringbuffer));
45 rb->entries = para_calloc(size * sizeof(void *));
46 rb->size = size;
47 return rb;
48 }
49
50 /**
51 * Add one entry to a ringbuffer.
52 *
53 * \param rb The ringbuffer identifier.
54 * \param data The data to be inserted.
55 *
56 * Insert \a data into the ringbuffer associated with \a cookie. As soon as
57 * the ringbuffer fills up, its oldest entry is disregarded and replaced by \a
58 * data.
59 *
60 * \return The old \a data pointer which is going to be disregarded, or
61 * NULL if the ringbuffer is not yet full.
62 */
63 void *ringbuffer_add(struct ringbuffer *rb, void *data)
64 {
65 void *ret = rb->entries[rb->head];
66 rb->entries[rb->head] = data;
67 rb->head = (rb->head + 1) % rb->size;
68 if (rb->filled < rb->size)
69 rb->filled++;
70 return ret;
71 }
72
73 /**
74 * Get one entry from a ringbuffer.
75 *
76 * \param rb The ringbuffer identifier.
77 * \param num The number of the entry.
78 *
79 * \return A pointer to data previously added, or NULL if entry number
80 * \a num is not available. \a num counts backwards from zero, i.e.
81 * ringbuffer_get_entry(0) gets the entry which was added most recently.
82 */
83 void *ringbuffer_get(struct ringbuffer *rb, int num)
84 {
85 int pos = (rb->head + rb->size - 1 - num) % rb->size;
86 // fprintf(stderr, "pos = %d\n", pos);
87 return rb->entries[pos];
88 }
89
90 /**
91 * Get the number of entries in the ring buffer.
92 *
93 * \param rb The ringbuffer identifier
94 *
95 * \return This function always succeeds. It returns a number less than the
96 * size of the ring buffer.
97 */
98 unsigned ringbuffer_filled(struct ringbuffer *rb)
99 {
100 return rb->filled;
101 }