Swapped some if-checks for asserts, closes #161

Thanks 'devast8a' on discord
This commit is contained in:
2024-12-26 13:09:21 +11:00
parent 9cb138a7d6
commit 3ca816439e
4 changed files with 24 additions and 74 deletions

View File

@@ -4,6 +4,8 @@
#LIBS+=-lm #LIBS+=-lm
#LDFLAGS+= #LDFLAGS+=
#TODO: release builds should define the NDEBUG flag; double check it works
#directories #directories
export TOY_SOURCEDIR=source export TOY_SOURCEDIR=source
export TOY_REPLDIR=repl export TOY_REPLDIR=repl

View File

@@ -1,15 +1,13 @@
#include "toy_bucket.h" #include "toy_bucket.h"
#include "toy_console_colors.h" #include "toy_console_colors.h"
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
//buckets of fun //buckets of fun
Toy_Bucket* Toy_allocateBucket(unsigned int capacity) { Toy_Bucket* Toy_allocateBucket(unsigned int capacity) {
if (capacity == 0) { assert(capacity != 0 && "Cannot allocate a 'Toy_Bucket' with zero capacity");
fprintf(stderr, TOY_CC_ERROR "ERROR: Cannot allocate a 'Toy_Bucket' with zero capacity\n" TOY_CC_RESET);
exit(1);
}
Toy_Bucket* bucket = malloc(sizeof(Toy_Bucket) + capacity); Toy_Bucket* bucket = malloc(sizeof(Toy_Bucket) + capacity);
@@ -27,23 +25,15 @@ Toy_Bucket* Toy_allocateBucket(unsigned int capacity) {
} }
void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount) { void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount) {
if ((*bucketHandle) == NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Expected a 'Toy_Bucket', received NULL\n" TOY_CC_RESET);
exit(1);
}
//BUGFIX: the endpoint must be aligned to the word size, otherwise you'll get a bus error from moving pointers //BUGFIX: the endpoint must be aligned to the word size, otherwise you'll get a bus error from moving pointers
amount = (amount + 3) & ~3; amount = (amount + 3) & ~3;
//if you try to allocate too much space assert((*bucketHandle) != NULL && "Expected a 'Toy_Bucket', received NULL");
if ((*bucketHandle)->capacity < amount) { assert((*bucketHandle)->capacity >= amount && "ERROR: Failed to partition a 'Toy_Bucket', requested amount is too high");
fprintf(stderr, TOY_CC_ERROR "ERROR: Failed to partition a 'Toy_Bucket': requested %d from a bucket of %d capacity\n" TOY_CC_RESET, (int)amount, (int)((*bucketHandle)->capacity));
exit(1);
}
//if you're out of space in this bucket //if you're out of space in this bucket, allocate another one
if ((*bucketHandle)->capacity < (*bucketHandle)->count + amount) { if ((*bucketHandle)->capacity < (*bucketHandle)->count + amount) {
//move to the next bucket
Toy_Bucket* tmp = Toy_allocateBucket((*bucketHandle)->capacity); Toy_Bucket* tmp = Toy_allocateBucket((*bucketHandle)->capacity);
tmp->next = (*bucketHandle); //it's buckets all the way down tmp->next = (*bucketHandle); //it's buckets all the way down
(*bucketHandle) = tmp; (*bucketHandle) = tmp;

View File

@@ -1,6 +1,7 @@
#include "toy_string.h" #include "toy_string.h"
#include "toy_console_colors.h" #include "toy_console_colors.h"
#include <assert.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -48,11 +49,6 @@ static unsigned int hashCString(const char* string) {
} }
static Toy_String* partitionStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length) { static Toy_String* partitionStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length) {
if (sizeof(Toy_String) + length + 1 > (*bucketHandle)->capacity) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't partition enough space for a string, requested %d length (%d total) but buckets have a capacity of %d\n" TOY_CC_RESET, (int)length, (int)(sizeof(Toy_String) + length + 1), (int)((*bucketHandle)->capacity));
exit(-1);
}
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1); Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1);
ret->info.type = TOY_STRING_LEAF; ret->info.type = TOY_STRING_LEAF;
@@ -92,16 +88,7 @@ Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstrin
} }
Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cname, unsigned int length, Toy_ValueType varType, bool constant) { Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cname, unsigned int length, Toy_ValueType varType, bool constant) {
//name strings can't be broken up assert(varType != TOY_VALUE_NULL && "Can't declare a name string with variable type 'null'");
if (sizeof(Toy_String) + length + 1 > (*bucketHandle)->capacity) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't partition enough space for a name string, requested %d length (%d total) but buckets have a capacity of %d\n" TOY_CC_RESET, (int)length, (int)(sizeof(Toy_String) + length + 1), (int)((*bucketHandle)->capacity));
exit(-1);
}
if (varType == TOY_VALUE_NULL) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't declare a name string with type 'null'\n" TOY_CC_RESET);
exit(-1);
}
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1); Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1);
@@ -118,21 +105,15 @@ Toy_String* Toy_createNameStringLength(Toy_Bucket** bucketHandle, const char* cn
} }
Toy_String* Toy_copyString(Toy_String* str) { Toy_String* Toy_copyString(Toy_String* str) {
if (str->info.refCount == 0) { assert(str->info.refCount != 0 && "Can't copy a string with refcount of zero");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't copy a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
incrementRefCount(str); incrementRefCount(str);
return str; return str;
} }
Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) { Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) {
if (str->info.refCount == 0) { assert(str->info.refCount != 0 && "Can't deep copy a string with refcount of zero");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't deep copy a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
//handle deep copies of strings that are too long for the bucket capacity NOTE: slow, could replace this at some point //handle deep copies of strings that are too long for the bucket capacity
if (sizeof(Toy_String) + str->info.length + 1 > (*bucketHandle)->capacity) { if (sizeof(Toy_String) + str->info.length + 1 > (*bucketHandle)->capacity) {
char* buffer = Toy_getStringRawBuffer(str); char* buffer = Toy_getStringRawBuffer(str);
Toy_String* result = Toy_createStringLength(bucketHandle, buffer, str->info.length); //handles the fragmenting Toy_String* result = Toy_createStringLength(bucketHandle, buffer, str->info.length); //handles the fragmenting
@@ -163,15 +144,8 @@ Toy_String* Toy_deepCopyString(Toy_Bucket** bucketHandle, Toy_String* str) {
} }
Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_String* right) { Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_String* right) {
if (left->info.type == TOY_STRING_NAME || right->info.type == TOY_STRING_NAME) { assert(left->info.refCount != 0 && right->info.refCount != 0 && "Can't concatenate a string with a refcount of zero");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a name string\n" TOY_CC_RESET); assert(left->info.type != TOY_STRING_NAME && right->info.type != TOY_STRING_NAME && "Can't concatenate a name string");
exit(-1);
}
if (left->info.refCount == 0 || right->info.refCount == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't concatenate a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String)); Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String));
@@ -189,7 +163,8 @@ Toy_String* Toy_concatStrings(Toy_Bucket** bucketHandle, Toy_String* left, Toy_S
} }
void Toy_freeString(Toy_String* str) { void Toy_freeString(Toy_String* str) {
decrementRefCount(str); //TODO: tool for checking the bucket is empty, and freeing it? //memory is freed when the bucket is
decrementRefCount(str);
} }
unsigned int Toy_getStringLength(Toy_String* str) { unsigned int Toy_getStringLength(Toy_String* str) {
@@ -201,37 +176,24 @@ unsigned int Toy_getStringRefCount(Toy_String* str) {
} }
Toy_ValueType Toy_getNameStringVarType(Toy_String* str) { Toy_ValueType Toy_getNameStringVarType(Toy_String* str) {
if (str->info.type != TOY_STRING_NAME) { assert(str->info.type == TOY_STRING_NAME && "Can't get the variable type of a non-name string");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get the variable type of a non-name string\n" TOY_CC_RESET);
exit(-1);
}
return str->name.varType; return str->name.varType;
} }
Toy_ValueType Toy_getNameStringVarConstant(Toy_String* str) { bool Toy_getNameStringVarConstant(Toy_String* str) {
if (str->info.type != TOY_STRING_NAME) { assert(str->info.type == TOY_STRING_NAME && "Can't get the variable constness of a non-name string");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get the variable constness of a non-name string\n" TOY_CC_RESET);
exit(-1);
}
return str->name.varConstant; return str->name.varConstant;
} }
char* Toy_getStringRawBuffer(Toy_String* str) { char* Toy_getStringRawBuffer(Toy_String* str) {
if (str->info.type == TOY_STRING_NAME) { assert(str->info.type != TOY_STRING_NAME && "Can't get raw string buffer of a name string");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a name string\n" TOY_CC_RESET); assert(str->info.refCount != 0 && "Can't get raw string buffer of a string with refcount of zero");
exit(-1);
}
if (str->info.refCount == 0) {
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't get raw string buffer of a string with refcount of zero\n" TOY_CC_RESET);
exit(-1);
}
//BUGFIX: Make sure it's aligned, and there's space for the null //BUGFIX: Make sure it's aligned, and there's space for the null
unsigned int len = (str->info.length + 3) & ~3; unsigned int len = (str->info.length + 3) & ~3;
if (len == str->info.length) { //nulls aren't counted if (len == str->info.length) { //nulls aren't counted in a string's length
len += 4; len += 4;
} }
@@ -323,11 +285,7 @@ int Toy_compareStrings(Toy_String* left, Toy_String* right) {
} }
if (left->info.type == TOY_STRING_NAME || right->info.type == TOY_STRING_NAME) { if (left->info.type == TOY_STRING_NAME || right->info.type == TOY_STRING_NAME) {
if (left->info.type != right->info.type) { assert (left->info.type == right->info.type && "Can't compare a name string to a non-name string");
fprintf(stderr, TOY_CC_ERROR "ERROR: Can't compare a name string to a non-name string\n" TOY_CC_RESET);
exit(-1);
}
return strncmp(left->name.data, right->name.data, left->info.length); return strncmp(left->name.data, right->name.data, left->info.length);
} }

View File

@@ -63,7 +63,7 @@ TOY_API void Toy_freeString(Toy_String* str);
TOY_API unsigned int Toy_getStringLength(Toy_String* str); TOY_API unsigned int Toy_getStringLength(Toy_String* str);
TOY_API unsigned int Toy_getStringRefCount(Toy_String* str); TOY_API unsigned int Toy_getStringRefCount(Toy_String* str);
TOY_API Toy_ValueType Toy_getNameStringVarType(Toy_String* str); TOY_API Toy_ValueType Toy_getNameStringVarType(Toy_String* str);
TOY_API Toy_ValueType Toy_getNameStringVarConstant(Toy_String* str); TOY_API bool Toy_getNameStringVarConstant(Toy_String* str);
TOY_API char* Toy_getStringRawBuffer(Toy_String* str); //allocates the buffer on the heap, needs to be freed TOY_API char* Toy_getStringRawBuffer(Toy_String* str); //allocates the buffer on the heap, needs to be freed