summaryrefslogtreecommitdiff
path: root/bytequeue.h
blob: c789a9a120aebd5fe042c61387449da7a67eb886 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
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