util: pull Buffer code out of VNC module
[qemu/ar7.git] / include / qemu / buffer.h
blobb380cec6fae7542fa2f1735562b73be39ac90570
1 /*
2 * QEMU generic buffers
4 * Copyright (c) 2015 Red Hat, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 #ifndef QEMU_BUFFER_H__
22 #define QEMU_BUFFER_H__
24 #include "qemu-common.h"
26 typedef struct Buffer Buffer;
28 /**
29 * Buffer:
31 * The Buffer object provides a simple dynamically resizing
32 * array, with separate tracking of capacity and usage. This
33 * is typically useful when buffering I/O or processing data.
36 struct Buffer {
37 size_t capacity;
38 size_t offset;
39 uint8_t *buffer;
42 /**
43 * buffer_reserve:
44 * @buffer: the buffer object
45 * @len: the minimum required free space
47 * Ensure that the buffer has space allocated for at least
48 * @len bytes. If the current buffer is too small, it will
49 * be reallocated, possibly to a larger size than requested.
51 void buffer_reserve(Buffer *buffer, size_t len);
53 /**
54 * buffer_reset:
55 * @buffer: the buffer object
57 * Reset the length of the stored data to zero, but do
58 * not free / reallocate the memory buffer
60 void buffer_reset(Buffer *buffer);
62 /**
63 * buffer_free:
64 * @buffer: the buffer object
66 * Reset the length of the stored data to zero and also
67 * free the internal memory buffer
69 void buffer_free(Buffer *buffer);
71 /**
72 * buffer_append:
73 * @buffer: the buffer object
74 * @data: the data block to append
75 * @len: the length of @data in bytes
77 * Append the contents of @data to the end of the buffer.
78 * The caller must ensure that the buffer has sufficient
79 * free space for @len bytes, typically by calling the
80 * buffer_reserve() method prior to appending.
82 void buffer_append(Buffer *buffer, const void *data, size_t len);
84 /**
85 * buffer_advance:
86 * @buffer: the buffer object
87 * @len: the number of bytes to skip
89 * Remove @len bytes of data from the head of the buffer.
90 * The internal buffer will not be reallocated, so will
91 * have at least @len bytes of free space after this
92 * call completes
94 void buffer_advance(Buffer *buffer, size_t len);
96 /**
97 * buffer_end:
98 * @buffer: the buffer object
100 * Get a pointer to the tail end of the internal buffer
101 * The returned pointer is only valid until the next
102 * call to buffer_reserve().
104 * Returns: the tail of the buffer
106 uint8_t *buffer_end(Buffer *buffer);
109 * buffer_empty:
110 * @buffer: the buffer object
112 * Determine if the buffer contains any current data
114 * Returns: true if the buffer holds data, false otherwise
116 gboolean buffer_empty(Buffer *buffer);
118 #endif /* QEMU_BUFFER_H__ */