mirror of
https://github.com/krgamestudios/Toy.git
synced 2026-05-06 00:40:11 +10:00
Corrected usage of 'Toy_allocateTable'
This commit is contained in:
+1
-1
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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++) {
|
||||
|
||||
Reference in New Issue
Block a user