diff options
author | Peter Maydell <peter.maydell@linaro.org> | 2015-10-20 16:51:43 +0100 |
---|---|---|
committer | Peter Maydell <peter.maydell@linaro.org> | 2015-10-20 16:51:43 +0100 |
commit | 426c0df9e3e6e64c7ea489092c57088ca4d227d0 (patch) | |
tree | e4880d0411a419ef368c030762e6b54bc8276be9 /util/buffer.c | |
parent | ee9dfed242610ecb91418270fd46b875ed56e201 (diff) | |
parent | 88c5f205fa4c095db4c50eb7ad72816140206819 (diff) |
Merge remote-tracking branch 'remotes/berrange/tags/io-channel-3-for-upstream' into staging
Merge io-channels-3 partial branch
# gpg: Signature made Tue 20 Oct 2015 16:36:10 BST using RSA key ID 15104FDF
# gpg: Good signature from "Daniel P. Berrange <dan@berrange.com>"
# gpg: aka "Daniel P. Berrange <berrange@redhat.com>"
* remotes/berrange/tags/io-channel-3-for-upstream:
util: pull Buffer code out of VNC module
coroutine: move into libqemuutil.a library
osdep: add qemu_fork() wrapper for safely handling signals
ui: convert VNC startup code to use SocketAddress
sockets: allow port to be NULL when listening on IP address
sockets: move qapi_copy_SocketAddress into qemu-sockets.c
sockets: add helpers for creating SocketAddress from a socket
Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
Diffstat (limited to 'util/buffer.c')
-rw-r--r-- | util/buffer.c | 65 |
1 files changed, 65 insertions, 0 deletions
diff --git a/util/buffer.c b/util/buffer.c new file mode 100644 index 0000000000..cedd055680 --- /dev/null +++ b/util/buffer.c @@ -0,0 +1,65 @@ +/* + * QEMU generic buffers + * + * Copyright (c) 2015 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, see <http://www.gnu.org/licenses/>. + * + */ + +#include "qemu/buffer.h" + +void buffer_reserve(Buffer *buffer, size_t len) +{ + if ((buffer->capacity - buffer->offset) < len) { + buffer->capacity += (len + 1024); + buffer->buffer = g_realloc(buffer->buffer, buffer->capacity); + } +} + +gboolean buffer_empty(Buffer *buffer) +{ + return buffer->offset == 0; +} + +uint8_t *buffer_end(Buffer *buffer) +{ + return buffer->buffer + buffer->offset; +} + +void buffer_reset(Buffer *buffer) +{ + buffer->offset = 0; +} + +void buffer_free(Buffer *buffer) +{ + g_free(buffer->buffer); + buffer->offset = 0; + buffer->capacity = 0; + buffer->buffer = NULL; +} + +void buffer_append(Buffer *buffer, const void *data, size_t len) +{ + memcpy(buffer->buffer + buffer->offset, data, len); + buffer->offset += len; +} + +void buffer_advance(Buffer *buffer, size_t len) +{ + memmove(buffer->buffer, buffer->buffer + len, + (buffer->offset - len)); + buffer->offset -= len; +} |