block: Add "drained begin/end" for internal snapshot
[qemu/ar7.git] / util / buffer.c
blobcedd05568020843b537b30f7867760cb8db42d2b
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 #include "qemu/buffer.h"
23 void buffer_reserve(Buffer *buffer, size_t len)
25 if ((buffer->capacity - buffer->offset) < len) {
26 buffer->capacity += (len + 1024);
27 buffer->buffer = g_realloc(buffer->buffer, buffer->capacity);
31 gboolean buffer_empty(Buffer *buffer)
33 return buffer->offset == 0;
36 uint8_t *buffer_end(Buffer *buffer)
38 return buffer->buffer + buffer->offset;
41 void buffer_reset(Buffer *buffer)
43 buffer->offset = 0;
46 void buffer_free(Buffer *buffer)
48 g_free(buffer->buffer);
49 buffer->offset = 0;
50 buffer->capacity = 0;
51 buffer->buffer = NULL;
54 void buffer_append(Buffer *buffer, const void *data, size_t len)
56 memcpy(buffer->buffer + buffer->offset, data, len);
57 buffer->offset += len;
60 void buffer_advance(Buffer *buffer, size_t len)
62 memmove(buffer->buffer, buffer->buffer + len,
63 (buffer->offset - len));
64 buffer->offset -= len;