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

@@ -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. 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 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. 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.

BIN
intermediate_code/main.o Normal file

Binary file not shown.

52
makefile Normal file
View File

@@ -0,0 +1,52 @@
.POSIX:
CC = gcc
AR = ar
ARFLAGS = rcs
CFLAGS = -std=c99 -Wall -Wextra -Werror -Wpedantic -Wconversion -Wuninitialized -Wunused -Wshadow -Wstrict-prototypes -Wpointer-arith -Wformat=2 -fanalyzer -Wstrict-aliasing=3 -Wlogical-op -Wduplicated-cond
LDFLAGS =
LDLIBS =
PREFIX = /usr/local
DESTDIR =
MODULE_DIR = source_code/MODULE_marigold_dynamic_array
INTERNAL_DIR = $(MODULE_DIR)/internal
TEST_DIR = $(MODULE_DIR)/tests
BUILD_DIR = build_output
OBJ_DIR = intermediate_code
.PHONY: all clean install test example lib
all: example test lib
clean:
rm -f $(OBJ_DIR)/*.o $(BUILD_DIR)/libmarigold_dynamic_array.a $(BUILD_DIR)/example $(BUILD_DIR)/test
lib: $(BUILD_DIR)/libmarigold_dynamic_array.a
example: $(BUILD_DIR)/example
test: $(BUILD_DIR)/test
$(BUILD_DIR)/libmarigold_dynamic_array.a: $(OBJ_DIR)/marigold_dynamic_array.o $(OBJ_DIR)/internal_marigold_dynamic_array.o
$(AR) $(ARFLAGS) $@ $(OBJ_DIR)/marigold_dynamic_array.o $(OBJ_DIR)/internal_marigold_dynamic_array.o
$(BUILD_DIR)/example: $(OBJ_DIR)/main.o $(OBJ_DIR)/marigold_dynamic_array.o $(OBJ_DIR)/internal_marigold_dynamic_array.o
$(CC) $(LDFLAGS) -o $@ $(OBJ_DIR)/main.o $(OBJ_DIR)/marigold_dynamic_array.o $(OBJ_DIR)/internal_marigold_dynamic_array.o
$(BUILD_DIR)/test: $(OBJ_DIR)/test_main.o $(OBJ_DIR)/marigold_dynamic_array.o $(OBJ_DIR)/internal_marigold_dynamic_array.o $(OBJ_DIR)/test_marigold_dynamic_array.o
$(CC) $(LDFLAGS) -o $@ $(OBJ_DIR)/test_main.o $(OBJ_DIR)/marigold_dynamic_array.o $(OBJ_DIR)/internal_marigold_dynamic_array.o $(OBJ_DIR)/test_marigold_dynamic_array.o
$(OBJ_DIR)/main.o: source_code/main.c $(MODULE_DIR)/marigold_dynamic_array.h
$(CC) $(CFLAGS) -c source_code/main.c -o $@
$(OBJ_DIR)/marigold_dynamic_array.o: $(MODULE_DIR)/marigold_dynamic_array.c $(MODULE_DIR)/marigold_dynamic_array.h $(INTERNAL_DIR)/internal_marigold_dynamic_array.h
$(CC) $(CFLAGS) -c $(MODULE_DIR)/marigold_dynamic_array.c -o $@
$(OBJ_DIR)/internal_marigold_dynamic_array.o: $(INTERNAL_DIR)/internal_marigold_dynamic_array.c $(INTERNAL_DIR)/internal_marigold_dynamic_array.h
$(CC) $(CFLAGS) -c $(INTERNAL_DIR)/internal_marigold_dynamic_array.c -o $@
$(OBJ_DIR)/test_main.o: source_code/test_main.c $(MODULE_DIR)/marigold_dynamic_array.h $(TEST_DIR)/test_marigold_dynamic_array.h
$(CC) $(CFLAGS) -c source_code/test_main.c -o $@
$(OBJ_DIR)/test_marigold_dynamic_array.o: $(TEST_DIR)/test_marigold_dynamic_array.c $(TEST_DIR)/test_marigold_dynamic_array.h $(MODULE_DIR)/marigold_dynamic_array.h
$(CC) $(CFLAGS) -c $(TEST_DIR)/test_marigold_dynamic_array.c -o $@

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