From 11c2ad914c599774ea167c83f67db060ff63c0bc Mon Sep 17 00:00:00 2001 From: epochryphon Date: Sat, 16 May 2026 15:25:58 -0500 Subject: [PATCH] initial commit --- LICENSE | 2 +- intermediate_code/main.o | Bin 0 -> 1560 bytes makefile | 52 +++++ .../internal_marigold_dynamic_array.c | 0 .../internal_marigold_dynamic_array.h | 10 + .../marigold_dynamic_array.c | 0 .../marigold_dynamic_array.h | 180 ++++++++++++++++++ .../tests/test_marigold_dynamic_array.c | 0 .../tests/test_marigold_dynamic_array.h | 0 source_code/main.c | 12 ++ source_code/test_main.c | 0 11 files changed, 255 insertions(+), 1 deletion(-) create mode 100644 intermediate_code/main.o create mode 100644 makefile create mode 100644 source_code/MODULE_marigold_dynamic_array/internal/internal_marigold_dynamic_array.c create mode 100644 source_code/MODULE_marigold_dynamic_array/internal/internal_marigold_dynamic_array.h create mode 100644 source_code/MODULE_marigold_dynamic_array/marigold_dynamic_array.c create mode 100644 source_code/MODULE_marigold_dynamic_array/marigold_dynamic_array.h create mode 100644 source_code/MODULE_marigold_dynamic_array/tests/test_marigold_dynamic_array.c create mode 100644 source_code/MODULE_marigold_dynamic_array/tests/test_marigold_dynamic_array.h create mode 100644 source_code/main.c create mode 100644 source_code/test_main.c diff --git a/LICENSE b/LICENSE index 96a8af6..f66c8bf 100644 --- a/LICENSE +++ b/LICENSE @@ -220,7 +220,7 @@ If you develop a new program, and you want it to be of the greatest possible use To do so, attach the following notices to the program. It is safest to attach them to the start of each source file to most effectively state the exclusion of warranty; and each file should have at least the "copyright" line and a pointer to where the full notice is found. marigold_dynamic_array - Copyright (C) 2026 epochryphon + Copyright (C) 2026 Emilia Marigold This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. diff --git a/intermediate_code/main.o b/intermediate_code/main.o new file mode 100644 index 0000000000000000000000000000000000000000..e3260cf08c7a2a8c7cb611202f799938adb3df8a GIT binary patch literal 1560 zcmbtUzi-n}5I!fRh0>t#V?ZhbGZcYnA8|p5RtSX#NCw1EslbAwm&BzOv0d378WuKC z2c-TfjO>+wKY<+~b!A5?clKTK@?ByC6ySSh zVmEWM2{l-Nt&c_pkboRfDt*@U6Im+R`M)SN^hoMe_*YR-B9gHF!2K}y15seZo74lq zg+hVzb`zYvy0f@2ck%#V^b+ED)aX;Py?N5P`xw-mBfY9 zkHgT9Fc!s`uiDSHR?~n=4!Q?o-~>dV{_%Ehj~hA3f; zB0PQ`vgMAE!Vtdb4TQxuQ-7F_VcWScm~Z!g#GHEumrOZ-fj7}8o8L>`Q07GQBd+iT wr+)0(2!YFu%KT3wMs;ldDa7pAAq1b8FaI;bNbL8g@4k$`%T4K=HF? +#include +#include + +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 */ diff --git a/source_code/MODULE_marigold_dynamic_array/tests/test_marigold_dynamic_array.c b/source_code/MODULE_marigold_dynamic_array/tests/test_marigold_dynamic_array.c new file mode 100644 index 0000000..e69de29 diff --git a/source_code/MODULE_marigold_dynamic_array/tests/test_marigold_dynamic_array.h b/source_code/MODULE_marigold_dynamic_array/tests/test_marigold_dynamic_array.h new file mode 100644 index 0000000..e69de29 diff --git a/source_code/main.c b/source_code/main.c new file mode 100644 index 0000000..fb15d6e --- /dev/null +++ b/source_code/main.c @@ -0,0 +1,12 @@ +#include +#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; +} diff --git a/source_code/test_main.c b/source_code/test_main.c new file mode 100644 index 0000000..e69de29