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
;
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.
46 * @buffer: the buffer object
49 * Optionally attach a name to the buffer, to make it easier
50 * to identify in debug traces.
52 void buffer_init(Buffer
*buffer
, const char *name
, ...)
57 * @buffer: the buffer object
59 * Try to shrink the buffer. Checks current buffer capacity and size
60 * and reduces capacity in case only a fraction of the buffer is
63 void buffer_shrink(Buffer
*buffer
);
67 * @buffer: the buffer object
68 * @len: the minimum required free space
70 * Ensure that the buffer has space allocated for at least
71 * @len bytes. If the current buffer is too small, it will
72 * be reallocated, possibly to a larger size than requested.
74 void buffer_reserve(Buffer
*buffer
, size_t len
);
78 * @buffer: the buffer object
80 * Reset the length of the stored data to zero, but do
81 * not free / reallocate the memory buffer
83 void buffer_reset(Buffer
*buffer
);
87 * @buffer: the buffer object
89 * Reset the length of the stored data to zero and also
90 * free the internal memory buffer
92 void buffer_free(Buffer
*buffer
);
96 * @buffer: the buffer object
97 * @data: the data block to append
98 * @len: the length of @data in bytes
100 * Append the contents of @data to the end of the buffer.
101 * The caller must ensure that the buffer has sufficient
102 * free space for @len bytes, typically by calling the
103 * buffer_reserve() method prior to appending.
105 void buffer_append(Buffer
*buffer
, const void *data
, size_t len
);
109 * @buffer: the buffer object
110 * @len: the number of bytes to skip
112 * Remove @len bytes of data from the head of the buffer.
113 * The internal buffer will not be reallocated, so will
114 * have at least @len bytes of free space after this
117 void buffer_advance(Buffer
*buffer
, size_t len
);
121 * @buffer: the buffer object
123 * Get a pointer to the tail end of the internal buffer
124 * The returned pointer is only valid until the next
125 * call to buffer_reserve().
127 * Returns: the tail of the buffer
129 uint8_t *buffer_end(Buffer
*buffer
);
133 * @buffer: the buffer object
135 * Determine if the buffer contains any current data
137 * Returns: true if the buffer holds data, false otherwise
139 gboolean
buffer_empty(Buffer
*buffer
);
143 * @to: destination buffer object
144 * @from: source buffer object
146 * Moves buffer, without copying data. 'to' buffer must be empty.
147 * 'from' buffer is empty and zero-sized on return.
149 void buffer_move_empty(Buffer
*to
, Buffer
*from
);
153 * @to: destination buffer object
154 * @from: source buffer object
156 * Moves buffer, copying data (unless 'to' buffer happens to be empty).
157 * 'from' buffer is empty and zero-sized on return.
159 void buffer_move(Buffer
*to
, Buffer
*from
);
161 #endif /* QEMU_BUFFER_H__ */