From 7b6aa9f70998b7efd5d597fa466326c8c0e0b957 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= Date: Wed, 9 Feb 2011 15:11:22 +0100 Subject: [PATCH] common: add SpiceBuffer - based on qemu-vnc Buffer https://bugs.freedesktop.org/show_bug.cgi?id=34795 --- common/mem.c | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ common/mem.h | 17 +++++++++++++++++ 2 files changed, 70 insertions(+), 0 deletions(-) diff --git a/common/mem.c b/common/mem.c index b0a7eb1..a9bd6cc 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; +} diff --git a/common/mem.h b/common/mem.h index 5f0eb25..797bba0 100644 --- a/common/mem.h +++ b/common/mem.h @@ -39,6 +39,13 @@ typedef struct SpiceChunks { SpiceChunk chunk[0]; } SpiceChunks; +typedef struct SpiceBuffer +{ + size_t capacity; + size_t offset; + uint8_t *buffer; +} SpiceBuffer; + char *spice_strdup(const char *str) SPICE_GNUC_MALLOC; char *spice_strndup(const char *str, size_t n_bytes) SPICE_GNUC_MALLOC; void *spice_memdup(const void *mem, size_t n_bytes) SPICE_GNUC_MALLOC; @@ -103,4 +110,14 @@ size_t spice_strnlen(const char *str, size_t max_len); #define spice_new0(struct_type, n_structs) _SPICE_NEW(struct_type, n_structs, malloc0) #define spice_renew(struct_type, mem, n_structs) _SPICE_RENEW(struct_type, mem, n_structs, realloc) +/* Buffer management */ +void spice_buffer_reserve(SpiceBuffer *buffer, size_t len); +int spice_buffer_empty(SpiceBuffer *buffer); +uint8_t *spice_buffer_end(SpiceBuffer *buffer); +void spice_buffer_reset(SpiceBuffer *buffer); +void spice_buffer_free(SpiceBuffer *buffer); +void spice_buffer_append(SpiceBuffer *buffer, const void *data, size_t len); +size_t spice_buffer_copy(SpiceBuffer *buffer, void *dest, size_t len); +size_t spice_buffer_remove(SpiceBuffer *buffer, size_t len); + #endif -- 1.7.4