Merge commit 'meins/next' into next
[paraslash.git] / ringbuffer.c
index b2ff0c7d449a0de3b739ee9f4f05ea5f749218cf..eee2ca104e9aa1531f1df4b219bd5e6efb6f6a92 100644 (file)
@@ -1,64 +1,38 @@
 /*
- * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
+ * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
  *
- *     This program is free software; you can redistribute it and/or modify
- *     it under the terms of the GNU General Public License as published by
- *     the Free Software Foundation; either version 2 of the License, or
- *     (at your option) any later version.
- *
- *     This program is distributed in the hope that it will be useful,
- *     but WITHOUT ANY WARRANTY; without even the implied warranty of
- *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- *     GNU General Public License for more details.
- *
- *     You should have received a copy of the GNU General Public License
- *     along with this program; if not, write to the Free Software
- *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ * Licensed under the GPL v2. For licencing details see COPYING.
  */
 
-/** \file ringbuffer.c simple ringbuffer implementation */
+/** \file ringbuffer.c Simple ringbuffer implementation */
+
+#include <regex.h>
 
-#include "gcc-compat.h"
 #include "para.h"
 #include "ringbuffer.h"
 #include "string.h"
 
 /**
- * holds all information about one ring buffer
+ * 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;
+       /** 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
+ * Initialize a new ringbuffer.
  *
- * @param size the number of entries the ringbuffer holds
+ * \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
@@ -66,33 +40,32 @@ unsigned filled;
  * many ringbuffers may be initialized via this function. Each ringbuffer is
  * identified by a 'cookie'.
  *
- * Return value: A 'cookie' which identifies the ringbuffer just created and
+ * \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
+ * 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
+ * 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(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;
@@ -102,33 +75,31 @@ void *ringbuffer_add(void *cookie, void *data)
 }
 
 /**
- * get one entry from a ringbuffer
+ * Get one entry from a ringbuffer.
  *
- * @param cookie the ringbuffer identifier
- * @param num the number of the entry
+ * \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];
 }
 
 /**
- * get the number of entries in the ring buffer
+ * 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;
 }