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