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; 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 //extract the directory from src, and store it in dest
#if defined(_WIN32) || defined(_WIN64) #if defined(_WIN32) || defined(_WIN64)
@@ -55,6 +55,22 @@ int dir(char* dest, const char* src) {
return len; 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) \ #define APPEND(dest, src) \
strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1); strncpy((dest) + (strlen(dest)), (src), strlen((src)) + 1);
@@ -143,7 +159,7 @@ CmdLine parseCmdLine(int argc, const char* argv[]) {
exit(-1); exit(-1);
} }
dir(cmd.infile, argv[0]); getDirPath(cmd.infile, argv[0]);
APPEND(cmd.infile, argv[i]); APPEND(cmd.infile, argv[i]);
FLIPSLASH(cmd.infile); FLIPSLASH(cmd.infile);
} }
@@ -162,11 +178,13 @@ static void errorAndContinueCallback(const char* msg) {
fprintf(stderr, "%s\n", msg); fprintf(stderr, "%s\n", msg);
} }
int repl(const char* name) { int repl(const char* filepath) {
Toy_setErrorCallback(errorAndContinueCallback); Toy_setErrorCallback(errorAndContinueCallback);
Toy_setAssertFailureCallback(errorAndContinueCallback); Toy_setAssertFailureCallback(errorAndContinueCallback);
//vars to use //vars to use
char prompt[256];
getFileName(prompt, filepath);
unsigned int INPUT_BUFFER_SIZE = 4096; unsigned int INPUT_BUFFER_SIZE = 4096;
char inputBuffer[INPUT_BUFFER_SIZE]; char inputBuffer[INPUT_BUFFER_SIZE];
memset(inputBuffer, 0, INPUT_BUFFER_SIZE); memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
@@ -176,7 +194,7 @@ int repl(const char* name) {
Toy_VM vm; Toy_VM vm;
Toy_initVM(&vm); Toy_initVM(&vm);
printf("%s> ", name); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt
//read from the terminal //read from the terminal
while(fgets(inputBuffer, INPUT_BUFFER_SIZE, stdin)) { while(fgets(inputBuffer, INPUT_BUFFER_SIZE, stdin)) {
@@ -187,7 +205,7 @@ int repl(const char* name) {
} }
if (length == 0) { if (length == 0) {
printf("%s> ", name); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt
continue; continue;
} }
@@ -205,7 +223,7 @@ int repl(const char* name) {
//parsing error, retry //parsing error, retry
if (parser.error) { if (parser.error) {
printf("%s> ", name); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt
continue; continue;
} }
@@ -223,14 +241,14 @@ int repl(const char* name) {
int depth = 0; int depth = 0;
while (iter->next) { while (iter->next) {
iter = iter->next; iter = iter->next;
if (++depth >= 7) { //8 buckets in the chain total, about 8kb allocated if (++depth >= 7) {
Toy_freeBucket(&bucket); Toy_freeBucket(&bucket);
bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
break; break;
} }
} }
printf("%s> ", name); //shows the terminal prompt printf("%s> ", prompt); //shows the terminal prompt
} }
//cleanp all memory //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_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount);
TOY_API void Toy_freeBucket(Toy_Bucket** bucketHandle); TOY_API void Toy_freeBucket(Toy_Bucket** bucketHandle);
//some useful bucket sizes //some useful bucket sizes, could be swapped or ifdef'd as needed
#define TOY_BUCKET_SMALL 256 #define TOY_BUCKET_TINY (1024 * 2)
#define TOY_BUCKET_MEDIUM 512 #define TOY_BUCKET_SMALL (1024 * 4)
#define TOY_BUCKET_LARGE 1024 #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 //allocate a single string from a c-string
{ {
//setup //setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "Hello world"; const char* cstring = "Hello world";
Toy_String* str = Toy_createString(&bucket, cstring); Toy_String* str = Toy_createString(&bucket, cstring);
@@ -77,7 +77,7 @@ int test_string_allocation() {
//copy and deep copy a string //copy and deep copy a string
{ {
//setup //setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "Hello world"; const char* cstring = "Hello world";
Toy_String* str = Toy_createString(&bucket, cstring); Toy_String* str = Toy_createString(&bucket, cstring);
@@ -103,7 +103,7 @@ int test_string_allocation() {
//copy and deep copy a name string //copy and deep copy a name string
{ {
//setup //setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = "Hello world"; const char* cstring = "Hello world";
Toy_String* str = Toy_createNameString(&bucket, cstring, TOY_VALUE_NULL); Toy_String* str = Toy_createNameString(&bucket, cstring, TOY_VALUE_NULL);
@@ -129,7 +129,7 @@ int test_string_allocation() {
//allocate a zero-length string //allocate a zero-length string
{ {
//setup //setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(1024);
const char* cstring = ""; const char* cstring = "";
Toy_String* str = Toy_createString(&bucket, cstring); Toy_String* str = Toy_createString(&bucket, cstring);
@@ -156,7 +156,7 @@ int test_string_allocation() {
int test_string_concatenation() { int test_string_concatenation() {
//one big bucket o' fun //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 //concatenate two strings, and check the refcounts
{ {
@@ -297,7 +297,7 @@ int test_string_equality() {
//simple string equality (no concats) //simple string equality (no concats)
{ {
//setup //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* helloWorldOne = Toy_createString(&bucket, "Hello world");
Toy_String* helloWorldTwo = Toy_createString(&bucket, "Hello world"); Toy_String* helloWorldTwo = Toy_createString(&bucket, "Hello world");
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone"); Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -347,7 +347,7 @@ int test_string_equality() {
//string equality (with concat left) //string equality (with concat left)
{ {
//setup //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* helloWorldOne = Toy_concatStrings(&bucket, Toy_createString(&bucket, "Hello "), Toy_createString(&bucket, "world"));
Toy_String* helloWorldTwo = Toy_createString(&bucket, "Hello world"); Toy_String* helloWorldTwo = Toy_createString(&bucket, "Hello world");
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone"); Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -397,7 +397,7 @@ int test_string_equality() {
//string equality (with concat right) //string equality (with concat right)
{ {
//setup //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* helloWorldOne = Toy_createString(&bucket, "Hello world");
Toy_String* helloWorldTwo = 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_createString(&bucket, "Hello everyone"); Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -423,7 +423,7 @@ int test_string_equality() {
//string equality (with concat both) //string equality (with concat both)
{ {
//setup - these concat points are deliberately different //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* 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* 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")); 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) //string equality (with concat arbitrary)
{ {
//setup - The quick brown fox jumps over the lazy dog. //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_String* helloWorldOne = Toy_concatStrings(&bucket,
Toy_createString(&bucket, "The quick brown "), Toy_createString(&bucket, "The quick brown "),
Toy_concatStrings(&bucket, Toy_concatStrings(&bucket,
@@ -525,7 +525,7 @@ int test_string_equality() {
//string equality (empty strings, no concats) //string equality (empty strings, no concats)
{ {
//setup //setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_createString(&bucket, ""); Toy_String* helloWorldOne = Toy_createString(&bucket, "");
Toy_String* helloWorldTwo = Toy_createString(&bucket, ""); Toy_String* helloWorldTwo = Toy_createString(&bucket, "");
Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone"); Toy_String* helloEveryone = Toy_createString(&bucket, "Hello everyone");
@@ -575,7 +575,7 @@ int test_string_equality() {
//string equality (empty strings, deep concats) //string equality (empty strings, deep concats)
{ {
//setup //setup
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); Toy_Bucket* bucket = Toy_allocateBucket(1024);
Toy_String* helloWorldOne = Toy_concatStrings(&bucket, Toy_String* helloWorldOne = Toy_concatStrings(&bucket,
Toy_createString(&bucket, ""), Toy_createString(&bucket, ""),
Toy_concatStrings(&bucket, Toy_concatStrings(&bucket,
@@ -651,7 +651,7 @@ int test_string_equality() {
//simple name equality //simple name equality
{ {
//setup //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* helloWorldOne = Toy_createNameString(&bucket, "Hello world", TOY_VALUE_NULL);
Toy_String* helloWorldTwo = 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? 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) //simple string diffs (no concats)
{ {
//setup //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* helloEveryone = Toy_createString(&bucket, "Hello everyone");
Toy_String* helloUniverse = Toy_createString(&bucket, "Hello universe"); Toy_String* helloUniverse = Toy_createString(&bucket, "Hello universe");
@@ -742,7 +742,7 @@ int test_string_diffs() {
//string diffs (with concat arbitrary) //string diffs (with concat arbitrary)
{ {
//setup - The quick brown fox jumps over the lazy dog. //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_String* pangram = Toy_concatStrings(&bucket,
Toy_createString(&bucket, "The quick brown "), Toy_createString(&bucket, "The quick brown "),
Toy_concatStrings(&bucket, Toy_concatStrings(&bucket,