Expanded defaults for bucket sizes

This commit is contained in:
2024-10-12 23:02:15 +11:00
parent bfc8fe3717
commit 1ad6bdff70
3 changed files with 50 additions and 28 deletions

View File

@@ -39,7 +39,7 @@ unsigned char* readFile(char* path, int* size) {
return buffer;
}
int dir(char* dest, const char* src) {
int getDirPath(char* dest, const char* src) {
//extract the directory from src, and store it in dest
#if defined(_WIN32) || defined(_WIN64)
@@ -55,6 +55,22 @@ int dir(char* dest, const char* src) {
return len;
}
int getFileName(char* dest, const char* src) {
//extract the directory from src, and store it in dest
#if defined(_WIN32) || defined(_WIN64)
char* p = strrchr(src, '\\') + 1;
#else
char* p = strrchr(src, '/') + 1;
#endif
int len = strlen(p);
strncpy(dest, p, len);
dest[len] = '\0';
return len;
}
#define APPEND(dest, src) \
strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1);
@@ -143,7 +159,7 @@ CmdLine parseCmdLine(int argc, const char* argv[]) {
exit(-1);
}
dir(cmd.infile, argv[0]);
getDirPath(cmd.infile, argv[0]);
APPEND(cmd.infile, argv[i]);
FLIPSLASH(cmd.infile);
}
@@ -162,11 +178,13 @@ static void errorAndContinueCallback(const char* msg) {
fprintf(stderr, "%s\n", msg);
}
int repl(const char* name) {
int repl(const char* filepath) {
Toy_setErrorCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(errorAndContinueCallback);
//vars to use
char prompt[256];
getFileName(prompt, filepath);
unsigned int INPUT_BUFFER_SIZE = 4096;
char inputBuffer[INPUT_BUFFER_SIZE];
memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
@@ -176,7 +194,7 @@ int repl(const char* name) {
Toy_VM vm;
Toy_initVM(&vm);
printf("%s> ", name); //shows the terminal prompt
printf("%s> ", prompt); //shows the terminal prompt
//read from the terminal
while(fgets(inputBuffer, INPUT_BUFFER_SIZE, stdin)) {
@@ -187,7 +205,7 @@ int repl(const char* name) {
}
if (length == 0) {
printf("%s> ", name); //shows the terminal prompt
printf("%s> ", prompt); //shows the terminal prompt
continue;
}
@@ -205,7 +223,7 @@ int repl(const char* name) {
//parsing error, retry
if (parser.error) {
printf("%s> ", name); //shows the terminal prompt
printf("%s> ", prompt); //shows the terminal prompt
continue;
}
@@ -223,14 +241,14 @@ int repl(const char* name) {
int depth = 0;
while (iter->next) {
iter = iter->next;
if (++depth >= 7) { //8 buckets in the chain total, about 8kb allocated
if (++depth >= 7) {
Toy_freeBucket(&bucket);
bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
break;
}
}
printf("%s> ", name); //shows the terminal prompt
printf("%s> ", prompt); //shows the terminal prompt
}
//cleanp all memory

View File

@@ -20,9 +20,13 @@ TOY_API Toy_Bucket* Toy_allocateBucket(unsigned int capacity);
TOY_API void* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount);
TOY_API void Toy_freeBucket(Toy_Bucket** bucketHandle);
//some useful bucket sizes
#define TOY_BUCKET_SMALL 256
#define TOY_BUCKET_MEDIUM 512
#define TOY_BUCKET_LARGE 1024
//some useful bucket sizes, could be swapped or ifdef'd as needed
#define TOY_BUCKET_TINY (1024 * 2)
#define TOY_BUCKET_SMALL (1024 * 4)
#define TOY_BUCKET_MEDIUM (1024 * 8)
#define TOY_BUCKET_LARGE (1024 * 16)
#define TOY_BUCKET_HUGE (1024 * 32)
//sizeof(Toy_Bucket) is added internally, so reverse it here
#define TOY_BUCKET_IDEAL (TOY_BUCKET_HUGE - sizeof(Toy_Bucket))
#define TOY_BUCKET_IDEAL 1024

View File

@@ -35,7 +35,7 @@ int test_string_allocation() {
//allocate a single string from a c-string
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "Hello world";
Toy_String* str = Toy_createString(&bucket, cstring);
@@ -77,7 +77,7 @@ int test_string_allocation() {
//copy and deep copy a string
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "Hello world";
Toy_String* str = Toy_createString(&bucket, cstring);
@@ -103,7 +103,7 @@ int test_string_allocation() {
//copy and deep copy a name string
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "Hello world";
Toy_String* str = Toy_createNameString(&bucket, cstring, TOY_VALUE_NULL);
@@ -129,7 +129,7 @@ int test_string_allocation() {
//allocate a zero-length string
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "";
Toy_String* str = Toy_createString(&bucket, cstring);
@@ -156,7 +156,7 @@ int test_string_allocation() {
int test_string_concatenation() {
//one big bucket o' fun
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
//concatenate two strings, and check the refcounts
{
@@ -297,7 +297,7 @@ int test_string_equality() {
//simple string equality (no concats)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_createString(&bucket, "Hello world");
Toy_String* helloWorldTwo = Toy_createString(&bucket, "Hello world");
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -347,7 +347,7 @@ int test_string_equality() {
//string equality (with concat left)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_concatStrings(&bucket, Toy_createString(&bucket, "Hello "), Toy_createString(&bucket, "world"));
Toy_String* helloWorldTwo = Toy_createString(&bucket, "Hello world");
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -397,7 +397,7 @@ int test_string_equality() {
//string equality (with concat right)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_createString(&bucket, "Hello world");
Toy_String* helloWorldTwo = Toy_concatStrings(&bucket, Toy_createString(&bucket, "Hello "), Toy_createString(&bucket, "world"));
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -423,7 +423,7 @@ int test_string_equality() {
//string equality (with concat both)
{
//setup - these concat points are deliberately different
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_concatStrings(&bucket, Toy_createString(&bucket, "Hello "), Toy_createString(&bucket, "world"));
Toy_String* helloWorldTwo = Toy_concatStrings(&bucket, Toy_createString(&bucket, "Hello"), Toy_createString(&bucket, " world"));
Toy_String* helloEveryone = Toy_concatStrings(&bucket, Toy_createString(&bucket, "Hell"), Toy_createString(&bucket, "world"));
@@ -461,7 +461,7 @@ int test_string_equality() {
//string equality (with concat arbitrary)
{
//setup - The quick brown fox jumps over the lazy dog.
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_concatStrings(&bucket,
Toy_createString(&bucket, "The quick brown "),
Toy_concatStrings(&bucket,
@@ -525,7 +525,7 @@ int test_string_equality() {
//string equality (empty strings, no concats)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_createString(&bucket, "");
Toy_String* helloWorldTwo = Toy_createString(&bucket, "");
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -575,7 +575,7 @@ int test_string_equality() {
//string equality (empty strings, deep concats)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_concatStrings(&bucket,
Toy_createString(&bucket, ""),
Toy_concatStrings(&bucket,
@@ -651,7 +651,7 @@ int test_string_equality() {
//simple name equality
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_createNameString(&bucket, "Hello world", TOY_VALUE_NULL);
Toy_String* helloWorldTwo = Toy_createNameString(&bucket, "Hello world", TOY_VALUE_NULL);
Toy_String* helloEveryone = Toy_createNameString(&bucket, "Hello everyone", TOY_VALUE_NULL); //TODO: compare types?
@@ -705,7 +705,7 @@ int test_string_diffs() {
//simple string diffs (no concats)
{
//setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
Toy_String* helloUniverse = Toy_createString(&bucket, "Hello universe");
@@ -742,7 +742,7 @@ int test_string_diffs() {
//string diffs (with concat arbitrary)
{
//setup - The quick brown fox jumps over the lazy dog.
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* pangram = Toy_concatStrings(&bucket,
Toy_createString(&bucket, "The quick brown "),
Toy_concatStrings(&bucket,