implemention for create/initialize vectors
This commit is contained in:
Binary file not shown.
@@ -0,0 +1,62 @@
|
|||||||
|
#include "internal_marigold_vector.h"
|
||||||
|
|
||||||
|
vector_struct*
|
||||||
|
vector_initialize(vector_struct* new_vector,
|
||||||
|
size_t item_size,
|
||||||
|
size_t starting_capacity,
|
||||||
|
vector_growth_rate_enum growth_rate,
|
||||||
|
int (*comparator)(const void*, const void*),
|
||||||
|
void (*item_free)(const void *),
|
||||||
|
bool is_multithread_safe)
|
||||||
|
{
|
||||||
|
new_vector->item_size = item_size;
|
||||||
|
new_vector->current_capacity = starting_capacity;
|
||||||
|
new_vector->current_occupancy = 0;
|
||||||
|
new_vector->comparator = comparator;
|
||||||
|
new_vector->item_free = item_free;
|
||||||
|
new_vector->growth_strategy = (uint8_t)growth_rate;
|
||||||
|
new_vector->owner_count = 1;
|
||||||
|
|
||||||
|
if (is_multithread_safe)
|
||||||
|
{
|
||||||
|
new_vector->flags |= vector_multithread_safe;
|
||||||
|
}
|
||||||
|
|
||||||
|
#ifdef MARIGOLD_DEBUG
|
||||||
|
new_vector->debug_flags = 0;
|
||||||
|
new_vector->debug_resize_count = 0;
|
||||||
|
new_vector->debug_function_call_count = 0;
|
||||||
|
new_vector->debug_last_error_code = 0;
|
||||||
|
new_vector->debug_allocation_failures = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
if (starting_capacity > 0)
|
||||||
|
{
|
||||||
|
new_vector->data_pointer = calloc(starting_capacity, item_size);
|
||||||
|
if (!new_vector->data_pointer)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_vector->data_pointer = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (is_multithread_safe)
|
||||||
|
{
|
||||||
|
new_vector->mutex_lock = calloc(1, sizeof(pthread_mutex_t));
|
||||||
|
if (!new_vector->mutex_lock)
|
||||||
|
{
|
||||||
|
free(new_vector->data_pointer);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
pthread_mutex_init(new_vector->mutex_lock, NULL);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
new_vector->mutex_lock = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return new_vector;
|
||||||
|
}
|
||||||
|
|||||||
@@ -58,7 +58,13 @@ vector_struct
|
|||||||
vector_struct;
|
vector_struct;
|
||||||
|
|
||||||
vector_struct*
|
vector_struct*
|
||||||
vector_initialize();
|
vector_initialize(vector_struct* new_vector,
|
||||||
|
size_t item_size,
|
||||||
|
size_t starting_capacity,
|
||||||
|
vector_growth_rate_enum growth_rate,
|
||||||
|
int (*comparator)(const void*, const void*),
|
||||||
|
void (*item_free)(const void *),
|
||||||
|
bool is_multithread_safe);
|
||||||
|
|
||||||
void
|
void
|
||||||
vector_free_members();
|
vector_free_members();
|
||||||
|
|||||||
@@ -11,8 +11,20 @@ vector_create(size_t item_size,
|
|||||||
void (*item_free)(const void *),
|
void (*item_free)(const void *),
|
||||||
bool is_multithread_safe)
|
bool is_multithread_safe)
|
||||||
{
|
{
|
||||||
// allocate memory/mutex etc
|
vector_struct* new_vector = calloc(1, sizeof(vector_struct));
|
||||||
return vector_initialize(); // actually set all internal values accordingly.
|
|
||||||
|
if (!new_vector)
|
||||||
|
{
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return vector_initialize(new_vector,
|
||||||
|
item_size,
|
||||||
|
starting_capacity,
|
||||||
|
growth_rate,
|
||||||
|
comparator,
|
||||||
|
item_free,
|
||||||
|
is_multithread_safe);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@@ -125,6 +137,12 @@ vector_insert_at(vector_struct* vector,
|
|||||||
size_t index,
|
size_t index,
|
||||||
const void* element)
|
const void* element)
|
||||||
{
|
{
|
||||||
|
const bool is_thread_safe = vector_is_thread_safe(vector);
|
||||||
|
if (is_thread_safe)
|
||||||
|
{ /* if thread safe enabled, locking before vector modification. */
|
||||||
|
pthread_mutex_lock(vector->mutex_lock);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user