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

index 591d134d1d07ebb6e3dfd8932c72c4dbf885b5e8..60dca5f6cd820b9d71d8c0083c9a177a008d363d 100644 (file)
@@ -5,18 +5,22 @@
   $HeadURL$
 */
 #include "vec.h"
+#include "mem.h"
 #include <stdlib.h>
 #include <string.h>
 
-vec_t* vec_new(bool own_contents, size_t num_elements, ...)
+static void vec_free(void* p_vec);
+
+static void vec_free_range(void** p_buffer, size_t start_idx, size_t end_idx);
+
+vec_t* vec_new(size_t num_elements, ...)
 {
     vec_t* p_vec;
     va_list elements;
     size_t index;
 
     /* Allocate and construct the vector object */
-    p_vec = (vec_t*)malloc( sizeof(vec_t) );
-    p_vec->own_contents = own_contents;
+    p_vec = (vec_t*)mem_allocate(sizeof(vec_t), vec_free);
     p_vec->size = num_elements;
     p_vec->capacity = (0 == num_elements) ? DEFAULT_VEC_CAPACITY : num_elements;
     p_vec->p_buffer = (void**)malloc( sizeof(void*) * p_vec->capacity );
@@ -32,25 +36,6 @@ vec_t* vec_new(bool own_contents, size_t num_elements, ...)
     return p_vec;
 }
 
-void vec_free(vec_t* p_vec)
-{
-    if (p_vec->own_contents)
-    {
-        vec_clear(p_vec);
-    }
-    free(p_vec->p_buffer);
-    free(p_vec);
-}
-
-void vec_free_range(void** p_buffer, size_t start_idx, size_t end_idx)
-{
-    size_t i;
-    for(i = start_idx; i < end_idx; i++)
-    {
-        free(p_buffer[i]);
-    }
-}
-
 size_t vec_size(vec_t* p_vec)
 {
     return p_vec->size;
@@ -78,8 +63,7 @@ void vec_resize(vec_t* p_vec, size_t size, void* data)
     }
     else if (size < p_vec->size)
     {
-        if (p_vec->own_contents)
-            vec_free_range(p_vec->p_buffer, size-1, p_vec->size);
+        vec_free_range(p_vec->p_buffer, size-1, p_vec->size);
         p_vec->size = size;
     }
 }
@@ -173,10 +157,7 @@ bool vec_erase(vec_t* p_vec, size_t start_idx, size_t end_idx)
     if ((start_idx < p_vec->size) && (end_idx < p_vec->size) && (start_idx <= end_idx))
     {
         /* Free the range of data */
-        if (p_vec->own_contents)
-        {
-            vec_free_range(p_vec->p_buffer, start_idx, end_idx + 1);
-        }
+        vec_free_range(p_vec->p_buffer, start_idx, end_idx + 1);
         /* Compact the remaining data */
         memcpy( &(p_vec->p_buffer[start_idx]), /* Destination is beginning of erased range */
             &(p_vec->p_buffer[end_idx+1]), /* Source is end of erased range */
@@ -210,3 +191,19 @@ void vec_clear(vec_t* p_vec)
     p_vec->size = 0;
 }
 
+
+static void vec_free(void* p_vec)
+{
+    vec_clear((vec_t*)p_vec);
+    free(((vec_t*)p_vec)->p_buffer);
+}
+
+static void vec_free_range(void** p_buffer, size_t start_idx, size_t end_idx)
+{
+    size_t i;
+    for(i = start_idx; i < end_idx; i++)
+    {
+        mem_release(p_buffer[i]);
+    }
+}
+
index aa9d2d7478f3cb48e395f7ce7919da2d59c7ac09..e0473d243573cca9b8fd0b98d9d068f39e3baa57 100644 (file)
@@ -13,7 +13,6 @@
 
 /** A vector implementation */
 typedef struct {
-    bool own_contents; /*< Whether the vector is responsible for freeing it's contents */
     size_t size;       /*< The number of elements currently in the array */
     size_t capacity;   /*< The size of the internal array */
     void** p_buffer;   /*< Pointer to the array */
@@ -32,30 +31,12 @@ extern "C" {
 /**
  * @brief Creates a new vector initialized with the given elements.
  *
- * @param own_contents Whether the contents will be freed when the vector is freed.
  * @param num_elements The number of elements to be put into the vector.
  * @param ... The list of elements (length must be equal to num_elements).
  *
  * @return Pointer to newly created vector.
  */
-vec_t* vec_new(bool own_contents, size_t num_elements, ...);
-
-/**
- * @brief Reclaims the memory of the provided vector.
- *
- * @param p_vec The vector to free.
- * @param free_contents Whether the contents of the vector should also be freed.
- */
-void vec_free(vec_t* p_vec);
-
-/**
- * @brief Calls free on a range of pointers in an array.
- *
- * @param p_buffer Pointer to the array.
- * @param start_idx The index at which the process will start.
- * @param end_idx The index at which the process will end.
- */
-void vec_free_range(void** p_buffer, size_t start_idx, size_t end_idx);
+vec_t* vec_new(size_t num_elements, ...);
 
 /**
  * @brief Returns the number of items in the vector.