diff options
Diffstat (limited to 'bytequeue.h')
-rw-r--r-- | bytequeue.h | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/bytequeue.h b/bytequeue.h new file mode 100644 index 0000000..c789a9a --- /dev/null +++ b/bytequeue.h @@ -0,0 +1,63 @@ +#ifndef _BYTEQUEUE_H +#define _BYTEQUEUE_H + +#include <glib.h> + +typedef struct ByteQueue ByteQueue; + +ByteQueue *byte_queue_new (void); +guint8 *byte_queue_free (ByteQueue *queue, + gboolean free_data); +gsize byte_queue_get_length (ByteQueue *queue); + +/* The data returned is owned by the byte queue and becomes invalid + * as soon as any method is called on the queue. It is explicitly + * allowed to call byte_queue_append() with the returned data, and + * in that case the queue will avoid copying if it can. The append + * must be the first method called on the queue after the read. + */ +const guint8 *byte_queue_get_data (ByteQueue *queue, + gsize *n_bytes); +void byte_queue_put_back (ByteQueue *queue, + const guint8 *bytes, + gsize n_bytes); +void byte_queue_append (ByteQueue *queue, + const guint8 *bytes, + gsize n_bytes); +/* This function appends uninitialized data of length @size + * to the queue, then returns a pointer to the added data. + * The intention is that the app can read() into this + * memory, then use byte_queue_pop_tail() to delete the + * area that wasn't read into. Example + * + * guint8 *area = byte_queue_alloc_tail (queue, 8192); + * + * n_read = read (fd, area, 8192); + * + * byte_queue_delete_tail (queue, 8192 - (n_read < 0)? 0 : n_read); + * + * if (n_read < 0) + * { + * byte_queue_delete_tail (queue, 8192); + * handle_error(); + * n_read = 0; + * } + * else + * { + * byte_queue_delete_tail (8192 - n_read); + * + * // enjoy the new data in the queue + * } + */ + +guint8 *byte_queue_alloc_tail (ByteQueue *queue, + gsize size); +void byte_queue_delete_tail (ByteQueue *queue, + gsize size); + +/* Transfer data from @src to @dest without copying + */ +void byte_queue_steal_data (ByteQueue *dest, + ByteQueue *src); + +#endif |