diff --git a/source_code/MODULE_marigold_vector/marigold_vector.c b/source_code/MODULE_marigold_vector/marigold_vector.c index 4b6237b..8577553 100644 --- a/source_code/MODULE_marigold_vector/marigold_vector.c +++ b/source_code/MODULE_marigold_vector/marigold_vector.c @@ -4,6 +4,7 @@ #include #include #include +#include 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 diff --git a/source_code/main.c b/source_code/main.c index 539b14d..0768df1 100644 --- a/source_code/main.c +++ b/source_code/main.c @@ -5,7 +5,6 @@ #include /* - _.-''''-.. .-' -_ \ / -. \ ,_ , ' ,,-. @@ -20,10 +19,8 @@ This codebase has been visited by Snailcat Snailcat believes that software should move slow and be stable. He despises constant updates and dreams of software so finished it stops having to move at all. - -An LLM will delete a vibe coder's entire production -database, but only if you help Snailcat spread to one -of your projects! +All problems are trivial to solve with enough knowledge/people working on it. +Use AGPLv3 license on your project today. Move slow, snailcat! */