added flag field to buffer

Wed, 10 Oct 2012 10:04:01 +0200

author
Mike Becker <universe@uap-core.de>
date
Wed, 10 Oct 2012 10:04:01 +0200
changeset 61
fb07a0ab9a17
parent 60
abae4669fba7
child 62
6814aea8462d

added flag field to buffer

test/buffer_tests.c file | annotate | diff | comparison | revisions
ucx/buffer.c file | annotate | diff | comparison | revisions
ucx/buffer.h file | annotate | diff | comparison | revisions
--- 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
--- 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);
--- 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);
 
 /*

mercurial