summaryrefslogtreecommitdiff
path: root/ringbuffer.c
blob: acfa5b438793eb83fc481cfd09d013b0902594b2 (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
104
105
106
107
108
109
110
111
112
113
/* SPDX-License-Identifier: GPL-2.0 */

/** \file ringbuffer.c Simple ringbuffer implementation */

#include "para.h"
#include "ringbuffer.h"
#include "string.h"

/*
 * Represents an instance of a ringbuffer. Opaque to the users of the
 * ringbuffer API.
 */
struct ringbuffer {
	/* The size of this ringbuffer. */
	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 ringbuffer contains. */
	unsigned filled;
};

/**
 * Initialize a new ringbuffer.
 *
 * \param size The number of entries the ringbuffer holds.
 *
 * This function initializes a circular buffer which can store up to the given
 * number of entries. The entries can be of arbitrary type. Each ringbuffer
 * is identified by an opaque handle which is returned by this function.
 *
 * \return An opaque handle which identifies the new ringbuffer. Other
 * functions of the ringbuffer API such as to \ref ringbuffer_add() and \ref
 * ringbuffer_get() take a handle as their first argument.
 */
struct ringbuffer *ringbuffer_new(unsigned size)
{
	struct ringbuffer *rb = zalloc(sizeof(struct ringbuffer));
	rb->entries = zalloc(size * sizeof(void *));
	rb->size = size;
	return rb;
}

/**
 * Add an entry to a ringbuffer.
 *
 * \param rb The handle which identifies the ringbuffer instance.
 * \param data Pointer to the data to be inserted.
 *
 * If the ringbuffer is full, the data pointer of the oldest entry is replaced
 * by the given pointer.
 *
 * \return The data pointer which is was disregarded, or NULL if the ringbuffer
 * was not full yet.
 */
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 handle which identifies the ringbuffer instance.
 * \param num The number of the entry.
 *
 * \return A pointer to data previously added, or NULL if there is no entry
 * corresponding to the given number. The number counts entries starting
 * at the newest entry so that ringbuffer_get_entry(rb, 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 ringbuffer.
 *
 * \param rb The handle which identifies the ringbuffer instance.
 *
 * \return This function always succeeds and returns a number between zero
 * and one less than the size of the ringbuffer, inclusively.
 */
unsigned ringbuffer_filled(struct ringbuffer *rb)
{
	return rb->filled;
}

/**
 * Remove all entries from a ringbuffer.
 *
 * \param rb The handle which identifies the ringbuffer instance.
 * \param free_func Called on each existing entry.
 */
void ringbuffer_flush(struct ringbuffer *rb, void (*free_func)(void *))
{
	for (int i = 0; i < rb->filled; i++) {
		int pos = (rb->head + rb->size - 1 - i) % rb->size;
		free_func(rb->entries[pos]);
	}
	memset(rb->entries, 0, rb->size * sizeof(void *));
	rb->filled = 0;
	rb->head = 0;
}