1 /* 2 * Copyright (C) 2002 2003 2005, Magnus Hjorth 3 * 4 * This file is part of mhWaveEdit. 5 * 6 * mhWaveEdit is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * 11 * mhWaveEdit is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with mhWaveEdit; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA 19 */ 20 21 22 /* Ring-buffer functions */ 23 24 #ifndef RINGBUF_H_INCLUDED 25 #define RINGBUF_H_INCLUDED 26 27 #include <glib.h> 28 29 typedef struct { 30 31 volatile guint32 start; /* First position in the buffer. */ 32 volatile guint32 end; /* First unused position. Equal to start if 33 * buffer is empty. */ 34 35 guint32 size; /* Number of bytes that the buffer can hold. */ 36 gchar data[1]; /* The data buffer. */ 37 } Ringbuf; 38 39 /* Creates a new ringbuffer with desired size */ 40 Ringbuf *ringbuf_new(guint32 size); 41 42 /* Destroys a ringbuffer */ 43 void ringbuf_free(Ringbuf *bufptr); 44 45 /* Empties a ringbuffer */ 46 void ringbuf_drain(Ringbuf *buf); 47 48 /* Returns the amount of data currently in the queue. */ 49 guint32 ringbuf_available(Ringbuf *buf); 50 51 /* Returns the amount of data that can be enqueued onto the queue 52 (buf->size - ringbuf_available(buf)). */ 53 guint32 ringbuf_freespace(Ringbuf *buf); 54 55 /* Tells whether the ringbuffer is empty or not. */ 56 gboolean ringbuf_isempty(Ringbuf *buf); 57 58 /* Tells whether the ringbuffer is full or not. */ 59 gboolean ringbuf_isfull(Ringbuf *buf); 60 61 /* Adds data to the end of the buffer. Returns the number of bytes added. */ 62 guint32 ringbuf_enqueue(Ringbuf *buf, gpointer data, guint32 datasize); 63 64 /* Adds null bytes to the end of the buffer. Returns the number of bytes added*/ 65 guint32 ringbuf_enqueue_zeroes(Ringbuf *buf, guint32 count); 66 67 /* Copies and removes data from the end of the buffer. Returns the number of 68 bytes removed. */ 69 guint32 ringbuf_dequeue(Ringbuf *buf, gpointer data, guint32 datasize); 70 71 /* Transfers data from one ringbuffer to another. Returns when source is empty 72 or dest is full. Returns number of bytes transferred. */ 73 guint32 ringbuf_transfer(Ringbuf *source, Ringbuf *dest); 74 75 #endif 76