]> git.mdlowis.com Git - projs/libcds.git/commitdiff
Convert circular buffer over to reference counting
authorMichael D. Lowis <mike@mdlowis.com>
Sat, 19 Jul 2014 04:29:52 +0000 (00:29 -0400)
committerMichael D. Lowis <mike@mdlowis.com>
Sat, 19 Jul 2014 04:29:52 +0000 (00:29 -0400)
source/buffer/buf.c
source/buffer/buf.h
source/vector/vec.c

index 76b64ec5d0c0d02d469d94101041c2b00e1939a3..d93f0aacc549a140cec38c0f606d48073c00deed 100644 (file)
@@ -6,13 +6,16 @@
   */
 #include <stdlib.h>
 #include "buf.h"
+#include "mem.h"
+
+static void buf_free(void* p_buf);
 
 buf_t* buf_new(size_t size)
 {
     buf_t* buf = NULL;
     if (size > 0)
     {
-        buf         = (buf_t*) malloc( sizeof(buf_t) );
+        buf         = (buf_t*) mem_allocate(sizeof(buf_t), &buf_free);
         buf->buffer = (void**) malloc( sizeof(void*) * size );
         buf->size   = size;
         buf->reads  = 0;
@@ -21,13 +24,6 @@ buf_t* buf_new(size_t size)
     return buf;
 }
 
-void buf_free(buf_t* buf, bool free_contents)
-{
-    buf_clear(buf,free_contents);
-    free( buf->buffer );
-    free( buf );
-}
-
 size_t buf_size(buf_t* buf)
 {
     return (size_t)buf->size;
@@ -45,14 +41,11 @@ bool buf_full(buf_t* buf)
     return full;
 }
 
-void buf_clear(buf_t* buf, bool free_contents)
+void buf_clear(buf_t* buf)
 {
-    if (free_contents)
+    while ( !buf_empty(buf) )
     {
-        while ( !buf_empty(buf) )
-        {
-            free( buf_read(buf) );
-        }
+        mem_release( buf_read(buf) );
     }
     buf->reads  = 0;
     buf->writes = 0;
@@ -81,3 +74,8 @@ bool buf_write(buf_t* buf, void* data)
     return success;
 }
 
+static void buf_free(void* p_buf)
+{
+    buf_clear((buf_t*)p_buf);
+    free( ((buf_t*)p_buf)->buffer );
+}
index 773d34b9f1bfe77da788aac58071bb922868fa24..14bf04ff6c468c26c15d88cd0eff1b7d76142aa3 100644 (file)
@@ -30,14 +30,6 @@ typedef struct {
  */
 buf_t* buf_new(size_t size);
 
-/**
- * @brief Frees the provided buffer and optionally any unread contents.
- *
- * @param buf           The buffer to free.
- * @param free_contents Whether unread contents should also be freed.
- */
-void buf_free(buf_t* buf, bool free_contents);
-
 /**
  * @brief Returns the size of the provided buffer.
  *
@@ -69,9 +61,8 @@ bool buf_full(buf_t* buf);
  * @brief Clears all unread data from the provided buffer.
  *
  * @param buf           The buffer to clear.
- * @param free_contents Whether the unread contents should also be freed.
  */
-void buf_clear(buf_t* buf, bool free_contents);
+void buf_clear(buf_t* buf);
 
 /**
  * @brief Reads an item from the provided buffer.
index 60dca5f6cd820b9d71d8c0083c9a177a008d363d..e645e9e513c5936d56127aa0bd054f06a1cb236a 100644 (file)
@@ -191,7 +191,6 @@ void vec_clear(vec_t* p_vec)
     p_vec->size = 0;
 }
 
-
 static void vec_free(void* p_vec)
 {
     vec_clear((vec_t*)p_vec);