diff --git a/source/toy_array.c b/source/toy_array.c index 64c7f1f..a103b1f 100644 --- a/source/toy_array.c +++ b/source/toy_array.c @@ -5,15 +5,19 @@ #include Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) { + //TODO: slip in a call to free the complex values here + if (capacity == 0) { free(paramArray); return NULL; } - Toy_Array* array = realloc(paramArray, capacity + sizeof(Toy_Array)); + unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity; + + Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array)); if (array == NULL) { - fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Array' of %d capacity\n" TOY_CC_RESET, (int)capacity); + fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to resize a 'Toy_Array' from %d to %d capacity\n" TOY_CC_RESET, (int)originalCapacity, (int)capacity); exit(-1); } diff --git a/source/toy_array.h b/source/toy_array.h index f681762..3deef1e 100644 --- a/source/toy_array.h +++ b/source/toy_array.h @@ -1,25 +1,13 @@ #pragma once #include "toy_common.h" +#include "toy_value.h" //standard generic array typedef struct Toy_Array { //32 | 64 BITNESS unsigned int capacity; //4 | 4 unsigned int count; //4 | 4 - char data[]; //- | - + Toy_Value data[]; //- | - } Toy_Array; //8 | 8 TOY_API Toy_Array* Toy_resizeArray(Toy_Array* array, unsigned int capacity); - -#define TOY_ALLOCATE_ARRAY(type, count) \ - Toy_resizeArray(NULL, sizeof(type)*(count)) - -#define TOY_FREE_ARRAY(type, array) \ - Toy_resizeArray(array, 0) - -#define TOY_ADJUST_ARRAY(type, array, newCapacity) \ - Toy_resizeArray(array, sizeof(type) * newCapacity) - -#define TOY_DOUBLE_ARRAY_CAPACITY(type, array) \ - Toy_resizeArray(array, sizeof(type) * array->capacity < 8 ? sizeof(type) * 8 : sizeof(type) * array->capacity * 2) - diff --git a/source/toy_table.h b/source/toy_table.h index 1459ea4..4f8ec8e 100644 --- a/source/toy_table.h +++ b/source/toy_table.h @@ -1,7 +1,6 @@ #pragma once #include "toy_common.h" - #include "toy_value.h" //key-value entry, and probe sequence length - https://programming.guide/robin-hood-hashing.html diff --git a/tests/cases/test_array.c b/tests/cases/test_array.c index a47abc2..7d9398d 100644 --- a/tests/cases/test_array.c +++ b/tests/cases/test_array.c @@ -3,33 +3,33 @@ #include -int test_resizeArray() { - //test single pointer +int test_array() { + //test allocation and free { - Toy_Array* array = TOY_ALLOCATE_ARRAY(int, 1); - TOY_FREE_ARRAY(int, array); + Toy_Array* array = Toy_resizeArray(NULL, 1); + array = Toy_resizeArray(array, 0); } - //test single pointer array + //test initial data { - Toy_Array* array = TOY_ALLOCATE_ARRAY(int, 10); + Toy_Array* array = Toy_resizeArray(NULL, 10); //check you can access the memory - array->data[1] = 42; + array->data[1] = TOY_VALUE_FROM_INTEGER(42); - TOY_FREE_ARRAY(int, array); + Toy_resizeArray(array, 0); } - //test multiple pointer arrays + //test multiple arrays (no overlaps or conflicts) { - Toy_Array* array1 = TOY_ALLOCATE_ARRAY(int, 10); - Toy_Array* array2 = TOY_ALLOCATE_ARRAY(int, 10); + Toy_Array* array1 = Toy_resizeArray(NULL, 10); + Toy_Array* array2 = Toy_resizeArray(NULL, 10); - array1->data[1] = 42; //access the given memory - array2->data[1] = 42; //access the given memory + array1->data[1] = TOY_VALUE_FROM_INTEGER(42); + array2->data[1] = TOY_VALUE_FROM_INTEGER(42); - TOY_FREE_ARRAY(int, array1); - TOY_FREE_ARRAY(int, array2); + Toy_resizeArray(array1, 0); + Toy_resizeArray(array2, 0); } return 0; @@ -40,7 +40,7 @@ int main() { int total = 0, res = 0; { - res = test_resizeArray(); + res = test_array(); total += res; if (res == 0) {