gcrypt: Remove dead stores.
[paraslash.git] / ringbuffer.c
1 /*
2  * Copyright (C) 2006-2012 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 <regex.h>
10
11 #include "para.h"
12 #include "ringbuffer.h"
13 #include "string.h"
14
15 /**
16  * Holds all information about one ring buffer
17  *
18  * It is intentionally not exported via ringbuffer.h. Think abstract.
19  */
20 struct ringbuffer
21 {
22         /** The size of this ring buffer. */
23         unsigned size;
24         /** The actual entries of the ringbuffer. */
25         void **entries;
26         /** The next entry will be added at this position. */
27         int head;
28         /** How many entries the ring buffer contains. */
29         unsigned filled;
30 };
31
32 /**
33  * Initialize a new ringbuffer.
34  *
35  * \param size The number of entries the ringbuffer holds.
36  *
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'.
42  *
43  * \return  A 'cookie' which identifies the ringbuffer just created and
44  * which must be passed to ringbuffer_add() and ringbuffer_get().
45  */
46 struct ringbuffer *ringbuffer_new(unsigned size)
47 {
48         struct ringbuffer *rb = para_calloc(sizeof(struct ringbuffer));
49         rb->entries = para_calloc(size * sizeof(void *));
50         rb->size = size;
51         return rb;
52 }
53
54 /**
55  * Add one entry to a ringbuffer.
56  *
57  * \param rb The ringbuffer identifier.
58  * \param data The data to be inserted.
59  *
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
62  * data.
63  *
64  * \return The old \a data pointer which is going to be disregarded, or
65  * NULL if the ringbuffer is not yet full.
66  */
67 void *ringbuffer_add(struct ringbuffer *rb, void *data)
68 {
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)
73                 rb->filled++;
74         return ret;
75 }
76
77 /**
78  * Get one entry from a ringbuffer.
79  *
80  * \param rb The ringbuffer identifier.
81  * \param num The number of the entry.
82  *
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.
86  */
87 void *ringbuffer_get(struct ringbuffer *rb, int num)
88 {
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 rb The ringbuffer identifier
98  *
99  * \return This function always succeeds. It returns a number less than the
100  * size of the ring buffer.
101  */
102 unsigned ringbuffer_filled(struct ringbuffer *rb)
103 {
104         return rb->filled;
105 }