diff --git a/Include/Fledasty/Core/Stack.h b/Include/Fledasty/Core/Stack.h index bcf0a97..fc0d237 100644 --- a/Include/Fledasty/Core/Stack.h +++ b/Include/Fledasty/Core/Stack.h @@ -34,7 +34,7 @@ typedef struct { unsigned char *buffer; } FledastyStack; -FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size); +FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, const size_t values_size, const size_t element_byte_size); FledastyError fledasty_stack_destroy(FledastyStack *current_stack); FledastyError fledasty_stack_push(FledastyStack *current_stack, void *value); diff --git a/Src/Core/Stack.c b/Src/Core/Stack.c index 4a377c0..f59775c 100644 --- a/Src/Core/Stack.c +++ b/Src/Core/Stack.c @@ -26,18 +26,30 @@ #include #include -FledastyError fledasty_stack_initialize(FledastyStack *new_stack, const size_t element_byte_size) { +FledastyError fledasty_stack_initialize(FledastyStack *new_stack, void *values, const size_t values_size, const size_t element_byte_size) { if (new_stack == NULL) { return FLEDASTY_ERROR_INVALID_POINTER; } - new_stack->size = 0; - new_stack->capacity = 10; new_stack->element_byte_size = element_byte_size; + if (values == NULL || values_size == 0) { + new_stack->size = 0; + new_stack->capacity = 10; - new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size); - if (new_stack->buffer == NULL) { - return FLEDASTY_ERROR_FAILED_ALLOCATION; + new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size); + if (new_stack->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + } else { + new_stack->size = values_size; + new_stack->capacity = values_size + (values_size / 2); + + new_stack->buffer = (unsigned char*)hallocy_malloc(new_stack->capacity * element_byte_size); + if (new_stack->buffer == NULL) { + return FLEDASTY_ERROR_FAILED_ALLOCATION; + } + + hallocy_copy_memory(new_stack->buffer, values, values_size * element_byte_size); } return FLEDASTY_ERROR_NONE; diff --git a/Tests/Main.c b/Tests/Main.c index 933fd43..8a4b193 100644 --- a/Tests/Main.c +++ b/Tests/Main.c @@ -46,7 +46,7 @@ int main() { fledasty_queue_destroy(&test_queue); FledastyStack test_stack; - fledasty_stack_initialize(&test_stack, sizeof(int)); + fledasty_stack_initialize(&test_stack, (int[]){ 11, 12, 13, 14, 15 }, 5, sizeof(int)); for (int i = 0; i < 10; i += 1) { fledasty_stack_push(&test_stack, &i);