From 7066e3f0a8ff4b8c226dc1f606999816721e3b86 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 14 Jan 2008 20:05:24 +0100 Subject: [PATCH] ringbuffer improvements. Introduce struct ringbuffer. This allows to get rid of the void* pointers of the ringbuffer implementation. --- gui.c | 2 +- ringbuffer.c | 17 +++++++---------- ringbuffer.h | 10 ++++++---- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/gui.c b/gui.c index f2115832..cf25cd45 100644 --- a/gui.c +++ b/gui.c @@ -45,7 +45,7 @@ struct rb_entry { size_t len; int color; }; -void *bot_win_rb; +struct ringbuffer *bot_win_rb; #define NUM_LINES(len) (1 + (len) / bot.cols) static unsigned scroll_position; diff --git a/ringbuffer.c b/ringbuffer.c index 1c62e7f3..efe4fd05 100644 --- a/ringbuffer.c +++ b/ringbuffer.c @@ -41,7 +41,7 @@ struct ringbuffer * \return A 'cookie' which identifies the ringbuffer just created and * which must be passed to ringbuffer_add() and ringbuffer_get(). */ -void *ringbuffer_new(unsigned size) +struct ringbuffer *ringbuffer_new(unsigned size) { struct ringbuffer *rb = para_calloc(sizeof(struct ringbuffer)); rb->entries = para_calloc(size * sizeof(void *)); @@ -52,7 +52,7 @@ void *ringbuffer_new(unsigned size) /** * Add one entry to a ringbuffer. * - * \param cookie The ringbuffer identifier. + * \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 @@ -62,9 +62,8 @@ void *ringbuffer_new(unsigned size) * \return The old \a data pointer which is going to be disregarded, or * NULL if the ringbuffer is not yet full. */ -void *ringbuffer_add(void *cookie, void *data) +void *ringbuffer_add(struct ringbuffer *rb, void *data) { - struct ringbuffer *rb = cookie; void *ret = rb->entries[rb->head]; rb->entries[rb->head] = data; rb->head = (rb->head + 1) % rb->size; @@ -76,16 +75,15 @@ void *ringbuffer_add(void *cookie, void *data) /** * Get one entry from a ringbuffer. * - * \param cookie The ringbuffer identifier. + * \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(void *cookie, int num) +void *ringbuffer_get(struct ringbuffer *rb, int num) { - struct ringbuffer *rb = cookie; int pos = (rb->head + rb->size - 1 - num) % rb->size; // fprintf(stderr, "pos = %d\n", pos); return rb->entries[pos]; @@ -94,13 +92,12 @@ void *ringbuffer_get(void *cookie, int num) /** * Get the number of entries in the ring buffer. * - * \param cookie The ringbuffer identifier + * \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(void *cookie) +unsigned ringbuffer_filled(struct ringbuffer *rb) { - struct ringbuffer *rb = cookie; return rb->filled; } diff --git a/ringbuffer.h b/ringbuffer.h index 20724b17..2bea374e 100644 --- a/ringbuffer.h +++ b/ringbuffer.h @@ -5,8 +5,10 @@ */ /** \file ringbuffer.h Exported symbols from ringbuffer.c. */ -void *ringbuffer_new(unsigned size); -void *ringbuffer_add(void *cookie, void *data); -void *ringbuffer_get(void *cookie, int num); -unsigned ringbuffer_filled(void *cookie); +struct ringbuffer; + +struct ringbuffer *ringbuffer_new(unsigned size); +void *ringbuffer_add(struct ringbuffer *rb, void *data); +void *ringbuffer_get(struct ringbuffer *rb, int num); +unsigned ringbuffer_filled(struct ringbuffer *rb); -- 2.39.5