initial commit

This commit is contained in:
2026-05-16 15:25:58 -05:00
parent d781ef9501
commit 11c2ad914c
11 changed files with 255 additions and 1 deletions

View File

@@ -0,0 +1,10 @@
/**
* @brief Destroy a dynamic array and free its memory.
*
* Frees the memory block and the array structure itself.
* Sets owner_count to 0 before freeing.
*
* @param array Pointer to the dynamic_array to destroy.
*/
void dynamic_array_destroy(dynamic_array* array);

View File

@@ -0,0 +1,180 @@
#ifndef DYNAMIC_ARRAY_H
#define DYNAMIC_ARRAY_H
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
typedef struct dynamic_array
{
size_t item_size;
void* memory_block_pointer;
unsigned int starting_capacity;
unsigned int current_capacity;
unsigned int current_size;
unsigned short owner_count;
unsigned char growth_factor;
bool is_multithread_safe;
} dynamic_array;
/**
* @brief Create and initialize a new dynamic array.
*
* Creates a dynamic array with the specified item size and starting capacity.
* The growth_factor determines how the array expands when capacity is exceeded.
*
* @param item_size Size of each element in bytes.
* @param starting_capacity Initial number of elements to allocate space for.
* @param growth_factor Growth strategy: 0 = linear (+starting_capacity),
* 1 = 1.5x, 2+ = multiplier (2 = 2x, 3 = 3x, etc.)
* @param is_multithread_safe If true, enables thread-safe operations.
* @return Pointer to the initialized dynamic_array, or NULL on failure.
*/
dynamic_array* dynamic_array_create(size_t item_size,
unsigned int starting_capacity,
unsigned char growth_factor,
bool is_multithread_safe);
/**
* @brief Get the number of elements currently in the array.
*
* @param array Pointer to the dynamic_array.
* @return Number of elements (current_size).
*/
unsigned int dynamic_array_get_occupancy(const dynamic_array* array);
/**
* @brief Get the total capacity of the array.
*
* @param array Pointer to the dynamic_array.
* @return Total capacity (current_capacity).
*/
unsigned int dynamic_array_get_capacity(const dynamic_array* array);
/**
* @brief Get a pointer to the element at the specified index.
*
* @param array Pointer to the dynamic_array.
* @param index Index of the element to access.
* @return Pointer to the element, or NULL if index is out of bounds.
*/
void* dynamic_array_get_pointer_to_index(dynamic_array* array,
unsigned int index);
/**
* @brief Get a const pointer to the element at the specified index.
*
* @param array Pointer to the dynamic_array.
* @param index Index of the element to access.
* @return Const pointer to the element, or NULL if index is out of bounds.
*/
const void* dynamic_array_get_const_pointer_to_index(const dynamic_array* array,
unsigned int index);
/**
* @brief Append an element to the end of the array.
*
* Automatically resizes if current_size equals current_capacity.
*
* @param array Pointer to the dynamic_array.
* @param element Pointer to the element to append.
* @return true on success, false on allocation failure.
*/
bool dynamic_array_append(dynamic_array* array,
const void* element);
/**
* @brief Remove the last element from the array.
*
* Decrements current_size. Does not free the memory block.
*
* @param array Pointer to the dynamic_array.
* @return true on success, false if array is empty.
*/
bool dynamic_array_pop(dynamic_array* array);
/**
* @brief Remove an element at the specified index.
*
* Shifts subsequent elements down to fill the gap.
*
* @param array Pointer to the dynamic_array.
* @param index Index of the element to remove.
* @return true on success, false if index is out of bounds.
*/
bool dynamic_array_remove(dynamic_array* array,
unsigned int index);
/**
* @brief Clear all elements from the array.
*
* Sets current_size to 0. Does not free the memory block.
*
* @param array Pointer to the dynamic_array.
*/
void dynamic_array_clear(dynamic_array* array);
/**
* @brief Check if the array is empty.
*
* @param array Pointer to the dynamic_array.
* @return true if current_size is 0, false otherwise.
*/
bool dynamic_array_is_empty(const dynamic_array* array);
/**
* @brief Reserve additional capacity without changing size.
*
* @param array Pointer to the dynamic_array.
* @param new_capacity Desired capacity.
* @return true on success, false on allocation failure.
*/
bool dynamic_array_increase_capacity(dynamic_array* array,
unsigned int new_capacity);
/**
* @brief Increment the owner count for shared ownership.
*
* @param array Pointer to the dynamic_array.
* @return true on success, false if owner_count would overflow.
*/
bool dynamic_array_acquire(dynamic_array* array);
/**
* @brief Decrement the owner count for shared ownership.
*
* If owner_count reaches 0, the array is automatically destroyed.
*
* @param array Pointer to the dynamic_array.
* @return true if owner_count > 0 after decrement, false if destroyed.
*/
bool dynamic_array_release(dynamic_array* array);
/**
* @brief Get the current owner count.
*
* @param array Pointer to the dynamic_array.
* @return Current owner_count value.
*/
unsigned short dynamic_array_get_owner_count(const dynamic_array* array);
/**
* @brief Check if the array is thread-safe.
*
* @param array Pointer to the dynamic_array.
* @return true if is_multithread_safe is set, false otherwise.
*/
bool dynamic_array_is_thread_safe(const dynamic_array* array);
/**
* @brief Creates a deep copy of the array.
*
* @param array Pointer to the copy being copied, and one to copy it onto.
* @return true if creation is successful. False if it fails for any reason.
*/
bool dynamic_array_clone(const dynamic_array* original_array, dynamic_array* new_array);
#endif /* DYNAMIC_ARRAY_H */

12
source_code/main.c Normal file
View File

@@ -0,0 +1,12 @@
#include <stdio.h>
#include "MODULE_marigold_dynamic_array/marigold_dynamic_array.h"
int main(void)
{
printf("Hello, %s!\n", "marigold_dynamic_array");
printf("Version: %s\n", "26.136.1101");
dynamic_array array = init_dynamic_array();
return 0;
}

0
source_code/test_main.c Normal file
View File