/* 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; }