more implementations added
This commit is contained in:
@@ -4,6 +4,7 @@
|
||||
#include <pthread.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <limits.h>
|
||||
|
||||
vector_struct*
|
||||
vector_create(size_t item_size,
|
||||
@@ -378,25 +379,156 @@ bool
|
||||
vector_increase_capacity(vector_struct* vector,
|
||||
size_t new_capacity)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (new_capacity <= vector->current_capacity)
|
||||
{ /* new capacity not larger than current, nothing to do. */
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex post vector modification if it exists/is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
return true; /* returns true to show success (no-op). */
|
||||
}
|
||||
|
||||
void* new_data = calloc(new_capacity, vector->item_size);
|
||||
if (!new_data)
|
||||
{ /* failed to allocate new memory. */
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex during fail case if it is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
return false; /* returns false to show capacity increase failed. */
|
||||
}
|
||||
|
||||
if (vector->current_occupancy > 0)
|
||||
{
|
||||
memcpy(new_data,
|
||||
vector->data_pointer,
|
||||
vector->current_occupancy * vector->item_size);
|
||||
}
|
||||
|
||||
free(vector->data_pointer);
|
||||
vector->data_pointer = new_data;
|
||||
vector->current_capacity = new_capacity;
|
||||
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex post vector modification if it exists/is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
|
||||
return true; /* returns true to show capacity increase success. */
|
||||
}
|
||||
|
||||
bool
|
||||
vector_shrink_to_fit(vector_struct* vector)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (vector->current_capacity <= vector->current_occupancy)
|
||||
{ /* capacity already matches or is smaller than occupancy, nothing to do. */
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex post vector modification if it exists/is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
return true; /* returns true to show success (no-op). */
|
||||
}
|
||||
|
||||
void* new_data = calloc(vector->current_occupancy, vector->item_size);
|
||||
if (!new_data)
|
||||
{ /* failed to allocate new memory. */
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex during fail case if it is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
return false; /* returns false to show shrink failed. */
|
||||
}
|
||||
|
||||
if (vector->current_occupancy > 0)
|
||||
{
|
||||
memcpy(new_data,
|
||||
vector->data_pointer,
|
||||
vector->current_occupancy * vector->item_size);
|
||||
}
|
||||
|
||||
free(vector->data_pointer);
|
||||
vector->data_pointer = new_data;
|
||||
vector->current_capacity = vector->current_occupancy;
|
||||
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex post vector modification if it exists/is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
|
||||
return true; /* returns true to show shrink success. */
|
||||
}
|
||||
|
||||
bool
|
||||
vector_acquire(vector_struct* vector)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (vector->owner_count >= USHRT_MAX)
|
||||
{ /* owner_count would overflow. */
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex during fail case if it is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
return false; /* returns false to show acquire failed. */
|
||||
}
|
||||
|
||||
vector->owner_count++;
|
||||
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex post vector modification if it exists/is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
|
||||
return true; /* returns true to show acquire success. */
|
||||
}
|
||||
|
||||
bool
|
||||
vector_release(vector_struct* vector)
|
||||
{
|
||||
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);
|
||||
}
|
||||
|
||||
if (vector->owner_count == 0)
|
||||
{ /* owner_count already zero, nothing to release. */
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex during fail case if it is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
return false; /* returns false to show release failed (already released). */
|
||||
}
|
||||
|
||||
vector->owner_count--;
|
||||
|
||||
if (vector->owner_count == 0)
|
||||
{ /* owner_count reached zero, destroy the vector. */
|
||||
return vector_destroy(vector);
|
||||
}
|
||||
|
||||
if (is_thread_safe)
|
||||
{ /* unlocking mutex post vector modification if it exists/is locked. */
|
||||
pthread_mutex_unlock(vector->mutex_lock);
|
||||
}
|
||||
|
||||
return vector->owner_count > 0; /* returns true if owner_count > 0, false if destroyed. */
|
||||
}
|
||||
|
||||
unsigned short
|
||||
|
||||
Reference in New Issue
Block a user