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