btr: Rename btr_bytes_pending() and make it public.
[paraslash.git] / ringbuffer.c
index 17ea425ad0e6817deb8778ee97412c24a1a54567..eee2ca104e9aa1531f1df4b219bd5e6efb6f6a92 100644 (file)
@@ -1,11 +1,13 @@
 /*
- * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
+ * 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 <regex.h>
+
 #include "para.h"
 #include "ringbuffer.h"
 #include "string.h"
@@ -41,19 +43,19 @@ 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 *));
        rb->size = size;
        return rb;
-};
+}
 
 /**
  * Add one entry to a ringbuffer.
  *
- * \param Cookie the ringbuffer identifier.
- * \param Data the data to be inserted.
+ * \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
@@ -62,9 +64,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 +77,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 +94,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;
 }