$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 );
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;
}
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;
}
}
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 */
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]);
+ }
+}
+
/** 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 */
/**
* @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.