summaryrefslogtreecommitdiffstats
path: root/common
diff options
context:
space:
mode:
authorMarc-André Lureau <marcandre.lureau@redhat.com>2011-02-09 15:11:22 +0100
committerMarc-André Lureau <marcandre.lureau@redhat.com>2011-02-28 16:36:35 +0100
commit2bbb4fca0c97043b183fb65126b7198bc0750a78 (patch)
tree61935a52920577690082c4b7238afe3b98830412 /common
parent2ced8996c86605bad91c7d2f73ea5460252b4517 (diff)
downloadspice-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')
-rw-r--r--common/mem.c53
-rw-r--r--common/mem.h17
2 files changed, 70 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;
+}
diff --git a/common/mem.h b/common/mem.h
index 5f0eb25e..797bba0a 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