Corrected usage of 'Toy_allocateTable'

This commit is contained in:
2026-04-26 09:55:11 +10:00
parent 0c24a7609e
commit c9a34e2259
9 changed files with 37 additions and 39 deletions
+1 -1
View File
@@ -206,7 +206,7 @@ CmdLine parseCmdLine(int argc, const char* argv[]) {
//total space to reserve
cmd.infileLength = strlen(argv[i]) + 1;
cmd.infileLength = (cmd.infileLength + 3) & ~3; //BUGFIX: align to word size for malloc()
cmd.infileLength = (cmd.infileLength + 3) & ~3; //BUGFIX: align to word size
cmd.infile = malloc(cmd.infileLength);
if (cmd.infile == NULL) {
+1 -1
View File
@@ -20,7 +20,7 @@ Toy_Array* Toy_resizeArray(Toy_Array* paramArray, unsigned int capacity) {
unsigned int originalCapacity = paramArray == NULL ? 0 : paramArray->capacity;
Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array));
Toy_Array* array = realloc(paramArray, capacity * sizeof(Toy_Value) + sizeof(Toy_Array)); //URGENT: Swap to a bucket
if (array == NULL) {
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);
+1 -1
View File
@@ -71,7 +71,7 @@ static void probeAndInsert(Toy_Scope* scope, Toy_String* key, Toy_Value value, T
static Toy_ScopeEntry* adjustScopeEntries(Toy_Scope* scope, unsigned int newCapacity) {
//allocate and zero a new Toy_ScopeEntry array in memory
Toy_ScopeEntry* newEntries = malloc(newCapacity * sizeof(Toy_ScopeEntry)); //URGENT: could use a bucket instead?
Toy_ScopeEntry* newEntries = malloc(newCapacity * sizeof(Toy_ScopeEntry)); //URGENT: Swap to a bucket
if (newEntries == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate space for 'Toy_Scope' entries\n" TOY_CC_RESET);
+1 -1
View File
@@ -5,7 +5,7 @@
#include <stdlib.h>
Toy_Stack* Toy_allocateStack(void) {
Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack));
Toy_Stack* stack = malloc(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)); //URGENT: Swap to a bucket (4 instances)
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, TOY_STACK_INITIAL_CAPACITY, (int)(TOY_STACK_INITIAL_CAPACITY * sizeof(Toy_Value) + sizeof(Toy_Stack)));
+14 -3
View File
@@ -46,7 +46,7 @@ static void probeAndInsert(Toy_Table** tableHandle, Toy_Value key, Toy_Value val
//exposed functions
Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned int newCapacity) {
//allocate and zero a new table in memory
Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table));
Toy_Table* newTable = malloc(newCapacity * sizeof(Toy_TableEntry) + sizeof(Toy_Table)); //URGENT: Swap to a bucket
if (newTable == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to allocate a 'Toy_Table'\n" TOY_CC_RESET);
@@ -76,8 +76,19 @@ Toy_Table* Toy_private_adjustTableCapacity(Toy_Table* oldTable, unsigned int new
return newTable;
}
Toy_Table* Toy_allocateTable(void) {
return Toy_private_adjustTableCapacity(NULL, TOY_TABLE_INITIAL_CAPACITY);
Toy_Table* Toy_allocateTable(unsigned int minCapacity) {
minCapacity = minCapacity > TOY_TABLE_INITIAL_CAPACITY ? minCapacity : TOY_TABLE_INITIAL_CAPACITY;
//neat trick to find the next power of two, inclusive
minCapacity--;
minCapacity |= minCapacity >> 1;
minCapacity |= minCapacity >> 2;
minCapacity |= minCapacity >> 4;
minCapacity |= minCapacity >> 8;
minCapacity |= minCapacity >> 16;
minCapacity++;
return Toy_private_adjustTableCapacity(NULL, minCapacity);
}
void Toy_freeTable(Toy_Table* table) {
+1 -1
View File
@@ -18,7 +18,7 @@ typedef struct Toy_Table { //32 | 64 BITNESS
Toy_TableEntry data[]; //- | -
} Toy_Table; //12 | 12
TOY_API Toy_Table* Toy_allocateTable(void);
TOY_API Toy_Table* Toy_allocateTable(unsigned int minCapacity); //minCapacity of 0 uses the default
TOY_API void Toy_freeTable(Toy_Table* table);
TOY_API void Toy_insertTable(Toy_Table** tableHandle, Toy_Value key, Toy_Value value);
TOY_API Toy_Value Toy_lookupTable(Toy_Table** tableHandle, Toy_Value key);
+1 -1
View File
@@ -122,7 +122,7 @@ Toy_Value Toy_copyValue(Toy_Bucket** bucketHandle, Toy_Value value) {
case TOY_VALUE_TABLE: { //TODO: switch to buckets
//tables probably won't get copied much
Toy_Table* ptr = value.as.table;
Toy_Table* result = Toy_private_adjustTableCapacity(NULL, ptr->capacity);
Toy_Table* result = Toy_allocateTable(ptr->capacity);
for (unsigned int i = 0; i < ptr->capacity; i++) {
if (TOY_VALUE_IS_NULL(ptr->data[i].key) != true) {
+2 -15
View File
@@ -121,21 +121,8 @@ static void processRead(Toy_VM* vm) {
//the number of values to read from the stack
unsigned int count = (unsigned int)READ_INT(vm);
//capacity covers keys AND values
unsigned int capacity = count / 2;
capacity = capacity > TOY_TABLE_INITIAL_CAPACITY ? capacity : TOY_TABLE_INITIAL_CAPACITY;
//neat trick to find the next power of two, inclusive (restriction of the table system)
capacity--;
capacity |= capacity >> 1;
capacity |= capacity >> 2;
capacity |= capacity >> 4;
capacity |= capacity >> 8;
capacity |= capacity >> 16;
capacity++;
//create the table and read in the key-values
Toy_Table* table = Toy_private_adjustTableCapacity(NULL, capacity);
//create the table (count covers keys AND values)
Toy_Table* table = Toy_allocateTable(count / 2);
//read in backwards from the stack
for (unsigned int i = 0; i < count / 2; i++) {
+15 -15
View File
@@ -7,7 +7,7 @@ int test_table_allocation(void) {
//allocate and free a table
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//check
if (table == NULL)
@@ -28,7 +28,7 @@ int test_table_simple_insert_lookup_and_remove(void) {
//simple insert
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
Toy_Value key = TOY_VALUE_FROM_INTEGER(1);
Toy_Value value = TOY_VALUE_FROM_INTEGER(42);
@@ -90,7 +90,7 @@ int test_table_contents_no_expansion(void) {
//single insert
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//insert a key and value
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(1), TOY_VALUE_FROM_INTEGER(42));
@@ -115,7 +115,7 @@ int test_table_contents_no_expansion(void) {
//multiple inserts, no collisions
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(1), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
@@ -144,7 +144,7 @@ int test_table_contents_no_expansion(void) {
//multiple inserts, with collisions
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(5), TOY_VALUE_FROM_INTEGER(42)); //hash: 2
@@ -175,7 +175,7 @@ int test_table_contents_no_expansion(void) {
//multiple inserts, with collisions, modulo wrap
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(1), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
@@ -207,7 +207,7 @@ int test_table_contents_no_expansion(void) {
//lookup, with collisions, modulo wrap
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
@@ -238,7 +238,7 @@ int test_table_contents_no_expansion(void) {
//multiple inserts, with collisions, modulo wrap, psl overlap
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
@@ -269,7 +269,7 @@ int test_table_contents_no_expansion(void) {
//multiple inserts, with collisions, modulo wrap, psl overlap, psl shift
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(17), TOY_VALUE_FROM_INTEGER(42)); //hash: 7
@@ -306,7 +306,7 @@ int test_table_contents_with_expansions(void) {
//simple expansion
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//insert a key and value
for (int i = 0; i < 20; i++) {
@@ -331,7 +331,7 @@ int test_table_contents_with_expansions(void) {
//expansion, multiple inserts, no collisions
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(0), TOY_VALUE_FROM_INTEGER(42)); //hash: 0
@@ -395,7 +395,7 @@ int test_table_contents_with_expansions(void) {
//multiple inserts, with collisions
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(0), TOY_VALUE_FROM_INTEGER(42)); //hash: 0
@@ -464,7 +464,7 @@ int test_table_contents_with_expansions(void) {
//multiple inserts, with collisions, modulo wrap
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
Toy_insertTable(&table, TOY_VALUE_FROM_INTEGER(123), TOY_VALUE_FROM_INTEGER(42)); //hash: 20
@@ -533,7 +533,7 @@ int test_table_contents_with_expansions(void) {
//lookup, with collisions, modulo wrap
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
//inserts
for (int i = 0; i < 20; i++) { //enough to expand
@@ -573,7 +573,7 @@ int test_table_expansions_under_stress(void) {
//multiple expansions, find one value
{
//setup
Toy_Table* table = Toy_allocateTable();
Toy_Table* table = Toy_allocateTable(0);
int top = 300;