diff options
author | Marc-André Lureau <marcandre.lureau@redhat.com> | 2011-02-09 15:11:22 +0100 |
---|---|---|
committer | Marc-André Lureau <marcandre.lureau@redhat.com> | 2011-02-28 16:36:35 +0100 |
commit | 2bbb4fca0c97043b183fb65126b7198bc0750a78 (patch) | |
tree | 61935a52920577690082c4b7238afe3b98830412 /common/mem.c | |
parent | 2ced8996c86605bad91c7d2f73ea5460252b4517 (diff) | |
download | spice-2bbb4fca0c97043b183fb65126b7198bc0750a78.tar.gz spice-2bbb4fca0c97043b183fb65126b7198bc0750a78.tar.xz spice-2bbb4fca0c97043b183fb65126b7198bc0750a78.zip |
common: add SpiceBuffer - based on qemu-vnc Buffer
https://bugs.freedesktop.org/show_bug.cgi?id=34795
Diffstat (limited to 'common/mem.c')
-rw-r--r-- | common/mem.c | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/common/mem.c b/common/mem.c index b0a7eb16..a9bd6cc2 100644 --- a/common/mem.c +++ b/common/mem.c @@ -237,3 +237,56 @@ void spice_chunks_linearize(SpiceChunks *chunks) chunks->chunk[0].len = chunks->data_size; } } + +void spice_buffer_reserve(SpiceBuffer *buffer, size_t len) +{ + if ((buffer->capacity - buffer->offset) < len) { + buffer->capacity += (len + 1024); + buffer->buffer = (uint8_t*)spice_realloc(buffer->buffer, buffer->capacity); + } +} + +int spice_buffer_empty(SpiceBuffer *buffer) +{ + return buffer->offset == 0; +} + +uint8_t *spice_buffer_end(SpiceBuffer *buffer) +{ + return buffer->buffer + buffer->offset; +} + +void spice_buffer_reset(SpiceBuffer *buffer) +{ + buffer->offset = 0; +} + +void spice_buffer_free(SpiceBuffer *buffer) +{ + free(buffer->buffer); + buffer->offset = 0; + buffer->capacity = 0; + buffer->buffer = NULL; +} + +void spice_buffer_append(SpiceBuffer *buffer, const void *data, size_t len) +{ + spice_buffer_reserve(buffer, len); + memcpy(buffer->buffer + buffer->offset, data, len); + buffer->offset += len; +} + +size_t spice_buffer_copy(SpiceBuffer *buffer, void *dest, size_t len) +{ + len = MIN(buffer->offset, len); + memcpy(dest, buffer->buffer, len); + return len; +} + +size_t spice_buffer_remove(SpiceBuffer *buffer, size_t len) +{ + len = MIN(buffer->offset, len); + memmove(buffer->buffer, buffer->buffer + len, buffer->offset - len); + buffer->offset -= len; + return len; +} |