Found and fixed some obscure leaks in 'Toy_Scope'

This commit is contained in:
2026-05-10 17:35:07 +10:00
parent 83fb5222a2
commit 53d3606c7e
7 changed files with 26 additions and 71 deletions
+1 -36
View File
@@ -1,5 +1,5 @@
#include "bucket_inspector.h"
// #include <toy_string.h>
#include <toy_string.h>
#include <stdio.h>
@@ -13,40 +13,6 @@ int inspect_bucket(Toy_Bucket** bucketHandle) {
unsigned char* ptr = iter->data;
while ((ptr - iter->data < iter->count) && *((int*)ptr) != 0) { //for each partition
if ( ( *((int*)ptr) & 1) == 0) { //is this partition still in use?
occupied++;
}
else {
released++;
}
//jump distance: ((*((int*)ptr) | 1) ^ 1) + 4
// printf(" jump %d, ", ((*((int*)ptr) | 1) ^ 1) + 4);
ptr += ((*((int*)ptr) | 1) ^ 1) + 4; //OR + XOR to remove the 'free' flag from the size
}
printf("Bucket link %d: count %u, %d occupied, %d released\n", depth, iter->count, occupied, released);
depth++;
}
printf("\n");
return depth;
}
/*
int inspect_bucket_for_strings(Toy_Bucket** bucketHandle) {
int depth = 0;
//for each bucket
for (Toy_Bucket* iter = (*bucketHandle); iter != NULL; iter = iter->next) {
int occupied = 0;
int released = 0;
unsigned char* ptr = iter->data;
while ((ptr - iter->data < iter->count) && *((int*)ptr) != 0) { //for each partition
if ( ( *((int*)ptr) & 1) == 0) { //is this partition still in use?
occupied++;
@@ -79,4 +45,3 @@ int inspect_bucket_for_strings(Toy_Bucket** bucketHandle) {
return depth;
}
*/
+1 -2
View File
@@ -2,5 +2,4 @@
#include "toy_bucket.h"
int inspect_bucket(Toy_Bucket** bucketHandle);
// int inspect_bucket_for_strings(Toy_Bucket** bucketHandle);
int inspect_bucket(Toy_Bucket** bucketHandle);
+4 -4
View File
@@ -280,14 +280,14 @@ static void debugScopePrint(Toy_Scope* scope, int depth) {
printf("\n" TOY_CC_NOTICE "Scope Dump [%d]" TOY_CC_RESET "\n" TOY_CC_NOTICE "%-20s%-20s%-20s" TOY_CC_RESET "\n", depth, "type", "name", "value");
for (unsigned int i = 0; i < scope->capacity; i++) {
if (scope->data[i].key.info.length == 0) {
if (scope->data[i].key == NULL || scope->data[i].key->info.length == 0) {
continue;
}
Toy_String k = scope->data[i].key;
Toy_String* k = scope->data[i].key;
Toy_Value v = scope->data[i].value;
printf("%-10s%-10s%-20s", Toy_getValueTypeAsCString(scope->data[i].type), scope->data[i].constant ? "const" : "", k.leaf.data);
printf("%-10s%-10s%-20s", Toy_getValueTypeAsCString(scope->data[i].type), scope->data[i].constant ? "const" : "", k != NULL ? k->leaf.data : "");
//print value
Toy_String* string = Toy_stringifyValue(&stringBucket, Toy_unwrapValue(v));
@@ -321,7 +321,7 @@ int repl(const char* filepath, bool verbose) {
char inputBuffer[INPUT_BUFFER_SIZE];
memset(inputBuffer, 0, INPUT_BUFFER_SIZE);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL);
Toy_Bucket* bucket = Toy_allocateBucket(TOY_BUCKET_IDEAL); //TODO: gc this
Toy_VM vm;
Toy_initVM(&vm);
+2 -6
View File
@@ -56,13 +56,9 @@ void initStandardLibrary(Toy_VM* vm) {
//declare each pair
for (int i = 0; callbackPairs[i].name; i++) {
//cheat
Toy_String key = (Toy_String){
.leaf = { ._padding = { .type = TOY_STRING_LEAF, .length = strlen(callbackPairs[i].name), .refCount = 1, .cachedHash = 0 }, .data = callbackPairs[i].name }
};
Toy_String* key = Toy_createStringLength(&vm->memoryBucket, callbackPairs[i].name, strlen(callbackPairs[i].name));
Toy_Function* fn = Toy_createFunctionFromCallback(&(vm->memoryBucket), callbackPairs[i].callback);
Toy_declareScope(vm->scope, &key, TOY_VALUE_FUNCTION, TOY_VALUE_FROM_FUNCTION(fn), true);
Toy_declareScope(vm->scope, key, TOY_VALUE_FUNCTION, TOY_VALUE_FROM_FUNCTION(fn), true);
}
}