]> git.proxmox.com Git - mirror_qemu.git/blob - util/buffer.c
Merge remote-tracking branch 'remotes/berrange/tags/io-channel-3-for-upstream' into...
[mirror_qemu.git] / util / buffer.c
1 /*
2 * QEMU generic buffers
3 *
4 * Copyright (c) 2015 Red Hat, Inc.
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 */
20
21 #include "qemu/buffer.h"
22
23 void buffer_reserve(Buffer *buffer, size_t len)
24 {
25 if ((buffer->capacity - buffer->offset) < len) {
26 buffer->capacity += (len + 1024);
27 buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
28 }
29 }
30
31 gboolean buffer_empty(Buffer *buffer)
32 {
33 return buffer->offset == 0;
34 }
35
36 uint8_t *buffer_end(Buffer *buffer)
37 {
38 return buffer->buffer + buffer->offset;
39 }
40
41 void buffer_reset(Buffer *buffer)
42 {
43 buffer->offset = 0;
44 }
45
46 void buffer_free(Buffer *buffer)
47 {
48 g_free(buffer->buffer);
49 buffer->offset = 0;
50 buffer->capacity = 0;
51 buffer->buffer = NULL;
52 }
53
54 void buffer_append(Buffer *buffer, const void *data, size_t len)
55 {
56 memcpy(buffer->buffer + buffer->offset, data, len);
57 buffer->offset += len;
58 }
59
60 void buffer_advance(Buffer *buffer, size_t len)
61 {
62 memmove(buffer->buffer, buffer->buffer + len,
63 (buffer->offset - len));
64 buffer->offset -= len;
65 }