Implemented garbage collection

As a whole, this is still tentative.
This commit is contained in:
2026-05-08 16:28:12 +10:00
parent be84a8dfe2
commit 6c055a0435
15 changed files with 267 additions and 80 deletions
+66 -6
View File
@@ -26,21 +26,29 @@ Toy_Bucket* Toy_allocateBucket(unsigned int capacity) {
unsigned char* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount) {
//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; //NOTE: this also leaves the lowest two bits as zero
assert((*bucketHandle) != NULL && "Expected a 'Toy_Bucket', received NULL");
assert((*bucketHandle)->capacity >= amount && "ERROR: Failed to partition a 'Toy_Bucket', requested amount is too high");
assert((*bucketHandle)->capacity >= (amount + 4) && "ERROR: Failed to partition a 'Toy_Bucket', requested amount is too high");
//if you're out of space in this bucket, allocate another one
if ((*bucketHandle)->capacity < (*bucketHandle)->count + amount) {
if ((*bucketHandle)->capacity < (*bucketHandle)->count + amount + 4) { //+4 for the metadata header
Toy_Bucket* tmp = Toy_allocateBucket((*bucketHandle)->capacity);
tmp->next = (*bucketHandle); //it's buckets all the way down
(*bucketHandle) = tmp;
}
//track the new count, and return the specified memory space
(*bucketHandle)->count += amount;
return ((*bucketHandle)->data + (*bucketHandle)->count - amount);
//use a 4-byte metadata header to hold the size of this partition, for GC
*((unsigned int*)((*bucketHandle)->data + (*bucketHandle)->count)) = amount;
//track the new metadata, and return the requested memory space
(*bucketHandle)->count += amount + 4;
return ((*bucketHandle)->data + (*bucketHandle)->count - amount); //metadata is before the pointer's address
}
void Toy_releaseBucketPartition(unsigned char* ptr) {
*((int*)(ptr-4)) |= 1; //flips the low-bit within the header
//no checks here, for technical reasons
}
void Toy_freeBucket(Toy_Bucket** bucketHandle) {
@@ -58,3 +66,55 @@ void Toy_freeBucket(Toy_Bucket** bucketHandle) {
//for safety
(*bucketHandle) = NULL;
}
TOY_API void Toy_collectBucketGarbage(Toy_Bucket** bucketHandle) {
//clear whatever this handle is pointing to
if ((*bucketHandle) == NULL) {
return;
}
Toy_Bucket* link = *bucketHandle;
while (link) {
//find non-free partitions
unsigned char* ptr = link->data;
bool gc = true;
while (ptr - link->data < link->count) { //for each partition
if ( (*((int*)ptr) & 1) == 0) { //is this partition still in use?
gc = false;
break;
}
ptr += (*((int*)(ptr)) ^ 1) + 4; //XOR to remove the 'free' flag from the size
}
//free this link, if its been entirely released
if (gc) {
//if link is the head
if (link == (*bucketHandle)) {
//if there's nowhere to go, don't delete the whole bucket
if ((*bucketHandle)->next == NULL) {
return;
}
else {
(*bucketHandle) = (*bucketHandle)->next;
free(link);
link = (*bucketHandle);
}
}
else {
//find the prev and free this link before continuing
Toy_Bucket* it = (*bucketHandle);
while (it->next != link) {
it = it->next;
}
it->next = link->next;
free(link);
link = it->next;
}
}
else {
link = link->next;
}
}
}
+3
View File
@@ -18,8 +18,11 @@ typedef struct Toy_Bucket { //32 | 64 BITNESS
TOY_API Toy_Bucket* Toy_allocateBucket(unsigned int capacity);
TOY_API unsigned char* Toy_partitionBucket(Toy_Bucket** bucketHandle, unsigned int amount);
TOY_API void Toy_releaseBucketPartition(unsigned char* ptr);
TOY_API void Toy_freeBucket(Toy_Bucket** bucketHandle);
TOY_API void Toy_collectBucketGarbage(Toy_Bucket** bucketHandle);
//standard capacity sizes
#ifndef TOY_BUCKET_1KB
#define TOY_BUCKET_1KB (1 << 10)
+2
View File
@@ -44,4 +44,6 @@ TOY_API void Toy_freeFunction(Toy_Function* fn) {
else if (fn->type == TOY_FUNCTION_NATIVE) {
fn->native.callback = NULL;
}
Toy_releaseBucketPartition((void*)fn);
}
+19 -7
View File
@@ -227,17 +227,29 @@ void Toy_private_incrementScopeRefCount(Toy_Scope* scope) {
}
void Toy_private_decrementScopeRefCount(Toy_Scope* scope) {
for (Toy_Scope* iter = scope; iter; iter = iter->next) {
Toy_Scope* iter = scope;
while (iter) {
iter->refCount--;
if (iter->refCount == 0 && iter->data != NULL) {
if (iter->refCount == 0) {
//free the scope entries when this scope is no longer needed
for (unsigned int i = 0; i < iter->capacity; i++) {
if (iter->data[i].psl > 0) {
Toy_freeString(&(iter->data[i].key));
Toy_freeValue(iter->data[i].value);
if (iter->data != NULL) {
for (unsigned int i = 0; i < iter->capacity; i++) {
if (iter->data[i].psl > 0) {
Toy_freeString(&(iter->data[i].key));
Toy_freeValue(iter->data[i].value);
}
}
free(iter->data);
}
free(iter->data);
//free the scope itself, fixing the iterator for the next loop
Toy_Scope* empty = iter;
iter = iter->next;
Toy_releaseBucketPartition((void*)empty);
}
else {
iter = iter->next;
}
}
}
+5 -2
View File
@@ -23,6 +23,9 @@ static void decrementRefCount(Toy_String* str) {
decrementRefCount(str->node.left);
decrementRefCount(str->node.right);
}
if (str->info.refCount == 0) {
Toy_releaseBucketPartition((void*)str);
}
}
//exposed functions
@@ -43,10 +46,10 @@ Toy_String* Toy_toStringLength(Toy_Bucket** bucketHandle, const char* cstring, u
}
Toy_String* Toy_createStringLength(Toy_Bucket** bucketHandle, const char* cstring, unsigned int length) {
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String));
Toy_String* ret = (Toy_String*)Toy_partitionBucket(bucketHandle, sizeof(Toy_String) + length + 1);
if (length > 0) {
ret->leaf.data = (char*)Toy_partitionBucket(bucketHandle, length + 1);
ret->leaf.data = (char*)(ret + 1); //increments by 1 'string', to the length +1
strncpy((char*)(ret->leaf.data), cstring, length);
((char*)(ret->leaf.data))[length] = '\0'; //don't forget the null
ret->info.length = length;
+4 -1
View File
@@ -1097,7 +1097,10 @@ void Toy_resetVM(Toy_VM* vm, bool preserveScope, bool preserveStack) {
Toy_resetStack(&vm->stack); //NOTE: has a realloc()
}
//NOTE: buckets are not altered during resets
//not sure how often to call teh GC
if (vm->memoryBucket) {
Toy_collectBucketGarbage(&vm->memoryBucket);
}
}
void Toy_initVM(Toy_VM* vm) {