From: Michael D. Lowis Date: Sat, 19 Jul 2014 04:18:51 +0000 (-0400) Subject: convert vector to reference counting X-Git-Url: https://git.mdlowis.com/?a=commitdiff_plain;h=424e243c1622c51f052493e43811071a0e1cc942;p=projs%2Flibcds.git convert vector to reference counting --- diff --git a/source/vector/vec.c b/source/vector/vec.c index 591d134..60dca5f 100644 --- a/source/vector/vec.c +++ b/source/vector/vec.c @@ -5,18 +5,22 @@ $HeadURL$ */ #include "vec.h" +#include "mem.h" #include #include -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]); + } +} + diff --git a/source/vector/vec.h b/source/vector/vec.h index aa9d2d7..e0473d2 100644 --- a/source/vector/vec.h +++ b/source/vector/vec.h @@ -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.