Reworked generic structures, read more

The following structures are now more independant:

- Toy_Array
- Toy_Stack
- Toy_Bucket
- Toy_String

I reworked a lot of the memory allocation, so now there are more direct
calls to malloc() or realloc(), rather than relying on the macros from
toy_memory.h.

I've also split toy_memory into proper array and bucket files, because
it makes more sense this way, rather than having them both jammed into
one file. This means the eventual hashtable structure can also stand on
its own.

Toy_Array is a new wrapper around raw array pointers, and all of the
structures have their metadata embedded into their allocated memory now,
using variable length array members.

A lot of 'capacity' and 'count' variables were changed to 'size_t'
types, but this doesn't seem to be a problem anywhere.

If the workflow fails, then I'll leave it for tonight - I'm too tired,
and I don't want to overdo myself.
This commit is contained in:
2024-10-01 20:24:52 +10:00
parent 53b0fc158c
commit 7b453bc35f
34 changed files with 566 additions and 576 deletions

View File

@@ -1,72 +1,86 @@
#include "toy_stack.h"
#include "toy_console_colors.h"
#include "toy_memory.h"
#include <stdio.h>
#include <stdlib.h>
//a good chunk of space
#define MIN_SIZE 64
//a good chunk of space - 'count' actually tracks the number of values
#define MIN_CAPACITY 64
TOY_API void Toy_initStack(Toy_Stack* stack) {
stack->ptr = NULL;
stack->capacity = 0;
stack->count = 0;
}
Toy_Stack* Toy_allocateStack() {
Toy_Stack* stack = malloc(MIN_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack));
void Toy_preallocateStack(Toy_Stack* stack) {
stack->capacity = MIN_SIZE;
if (stack == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, MIN_CAPACITY, (int)(MIN_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)));
exit(1);
}
stack->capacity = MIN_CAPACITY;
stack->count = 0;
stack->ptr = TOY_ALLOCATE(Toy_Value, stack->capacity);
return stack;
}
void Toy_freeStack(Toy_Stack* stack) {
//TODO: slip in a call to free the complex values here
TOY_FREE_ARRAY(Toy_Value, stack->ptr, stack->capacity);
Toy_initStack(stack);
free(stack);
}
void Toy_pushStack(Toy_Stack* stack, Toy_Value value) {
//don't go overboard - limit to 1mb
if (stack->count >= 1024 * 1024 / sizeof(Toy_Value)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack overflow, exiting\n" TOY_CC_RESET);
void Toy_pushStack(Toy_Stack** stack, Toy_Value value) {
//don't go overboard - limit to 1mb of capacity used
if ((*stack)->count >= 1024 * 1024 / sizeof(Toy_Value)) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack overflow\n" TOY_CC_RESET);
exit(-1);
}
//expand the capacity if needed
if (stack->count + 1 > stack->capacity) {
int oldCapacity = stack->capacity;
stack->capacity = stack->capacity < MIN_SIZE ? MIN_SIZE : stack->capacity * 2; //similar to TOY_GROW_CAPACITY, with a bigger initial size
stack->ptr = TOY_GROW_ARRAY(Toy_Value, stack->ptr, oldCapacity, stack->capacity);
if ((*stack)->count + 1 > (*stack)->capacity) {
while ((*stack)->count + 1 > (*stack)->capacity) {
(*stack)->capacity = (*stack)->capacity < MIN_CAPACITY ? MIN_CAPACITY : (*stack)->capacity * 2;
}
size_t newCapacity = (*stack)->capacity;
(*stack) = realloc((*stack), newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack));
if ((*stack) == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to reallocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, (int)newCapacity, (int)(newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack)));
exit(1);
}
}
stack->ptr[stack->count++] = value;
//Note: "pointer arithmetic in C/C++ is type-relative"
((Toy_Value*)((*stack) + 1))[(*stack)->count++] = value;
}
Toy_Value Toy_peekStack(Toy_Stack* stack) {
if (stack->count <= 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack underflow, exiting\n" TOY_CC_RESET);
Toy_Value Toy_peekStack(Toy_Stack** stack) {
if ((*stack)->count == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack underflow\n" TOY_CC_RESET);
exit(-1);
}
return stack->ptr[stack->count - 1];
return ((Toy_Value*)((*stack) + 1))[(*stack)->count - 1];
}
Toy_Value Toy_popStack(Toy_Stack* stack) {
if (stack->count <= 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack underflow, exiting\n" TOY_CC_RESET);
Toy_Value Toy_popStack(Toy_Stack** stack) {
if ((*stack)->count == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Stack underflow\n" TOY_CC_RESET);
exit(-1);
}
//shrink if possible
if (stack->count > MIN_SIZE && stack->count < stack->capacity / 4) {
stack->ptr = TOY_SHRINK_ARRAY(Toy_Value, stack->ptr, stack->capacity, stack->capacity / 2);
stack->capacity /= 2;
if ((*stack)->count > MIN_CAPACITY && (*stack)->count < (*stack)->capacity / 4) {
(*stack)->capacity /= 2;
size_t newCapacity = (*stack)->capacity;
(*stack) = realloc((*stack), (*stack)->capacity * sizeof(Toy_Value) + sizeof(Toy_Stack));
if ((*stack) == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to reallocate a 'Toy_Stack' of %d capacity (%d space in memory)\n" TOY_CC_RESET, (int)newCapacity, (int)(newCapacity * sizeof(Toy_Value) + sizeof(Toy_Stack)));
exit(1);
}
}
return stack->ptr[--stack->count];
return ((Toy_Value*)((*stack) + 1))[--(*stack)->count];
}