# HG changeset patch # User Mike Becker # Date 1349856241 -7200 # Node ID fb07a0ab9a17f47d57ec78d85e584885e0b90a89 # Parent abae4669fba7d4b455fc5c55f11e8e48e38249b6 added flag field to buffer diff -r abae4669fba7 -r fb07a0ab9a17 test/buffer_tests.c --- a/test/buffer_tests.c Wed Oct 10 09:54:57 2012 +0200 +++ b/test/buffer_tests.c Wed Oct 10 10:04:01 2012 +0200 @@ -9,7 +9,7 @@ memset(buffer, 32, 7); buffer[7] = 0; - UcxBuffer *b = ucx_buffer_new(buffer, 16); + UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); int r; UCX_TEST_BEGIN @@ -51,7 +51,7 @@ char *buffer = malloc(16); memset(buffer, 32, 16); - UcxBuffer *b = ucx_buffer_new(buffer, 16); + UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); int r; UCX_TEST_BEGIN @@ -79,7 +79,7 @@ buffer[i] = 40+i; } - UcxBuffer *b = ucx_buffer_new(buffer, 16); + UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); int r; UCX_TEST_BEGIN @@ -109,7 +109,7 @@ buffer[i] = 40+i; } - UcxBuffer *b = ucx_buffer_new(buffer, 16); + UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); int r; UCX_TEST_BEGIN @@ -151,7 +151,7 @@ buffer[i] = 40+i; } - UcxBuffer *b = ucx_buffer_new(buffer, 16); + UcxBuffer *b = ucx_buffer_new(buffer, 16, UCX_BUFFER_DEFAULT); int r; UCX_TEST_BEGIN diff -r abae4669fba7 -r fb07a0ab9a17 ucx/buffer.c --- a/ucx/buffer.c Wed Oct 10 09:54:57 2012 +0200 +++ b/ucx/buffer.c Wed Oct 10 10:04:01 2012 +0200 @@ -7,12 +7,13 @@ void *space; off_t pos; size_t length; - _Bool autofree; + int flags; }; -UcxBuffer *ucx_buffer_new(void *space, size_t length) { +UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags) { UcxBuffer *buffer = (UcxBuffer*) malloc(sizeof(UcxBuffer)); if (buffer) { + buffer->flags = flags; if (!space) { buffer->space = malloc(length); if (!buffer->space) { @@ -20,10 +21,9 @@ return NULL; } memset(buffer->space, 0, length); - buffer->autofree = 1; + buffer->flags |= UCX_BUFFER_AUTOFREE; } else { buffer->space = space; - buffer->autofree = 0; } buffer->length = length; @@ -34,7 +34,7 @@ } void ucx_buffer_free(UcxBuffer *buffer) { - if (buffer->autofree) { + if ((buffer->flags & UCX_BUFFER_AUTOFREE) > 0) { free(buffer->space); } free(buffer); diff -r abae4669fba7 -r fb07a0ab9a17 ucx/buffer.h --- a/ucx/buffer.h Wed Oct 10 09:54:57 2012 +0200 +++ b/ucx/buffer.h Wed Oct 10 10:04:01 2012 +0200 @@ -7,11 +7,14 @@ extern "C" { #endif +#define UCX_BUFFER_DEFAULT 0x00 +#define UCX_BUFFER_AUTOFREE 0x01 /* the user shall not modify values */ typedef struct UcxBuffer UcxBuffer; -UcxBuffer *ucx_buffer_new(void *space, size_t length); +/* if space is NULL, new space is allocated and the autofree flag is enforced */ +UcxBuffer *ucx_buffer_new(void *space, size_t length, int flags); void ucx_buffer_free(UcxBuffer* buffer); /*