more debug related refactoring

This commit is contained in:
CPunch 2023-08-25 20:44:24 -05:00
parent a1c58647ba
commit 5fc9af5564
3 changed files with 26 additions and 14 deletions

View File

@ -11,17 +11,20 @@
void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize) void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize)
{ {
#ifdef GC_DEBUG #ifdef GC_DEBUG
printf("old allocated bytes: %ld\n", state->allocatedBytes);
if (buf) { if (buf) {
if (newSize == 0) { if (newSize == 0) {
printf("freeing %p, reclaiming %ld bytes...\n", buf, oldSize); printf("freeing %p, reclaiming %ld bytes...\n", buf, oldSize);
} else { } else {
printf("realloc %p, byte difference: %ld\n", buf, newSize - oldSize); printf("realloc %p, byte difference: %ld\n", buf, newSize - oldSize);
} }
} else {
printf("allocating new buffer of size %ld\n", newSize - oldSize);
} }
#endif #endif
state->allocatedBytes += newSize - oldSize; state->allocatedBytes += newSize - oldSize;
#ifdef GC_DEBUG
printf("new allocated bytes: %ld\n", state->allocatedBytes);
fflush(stdout);
#endif
if (newSize == 0) { // it needs to be freed if (newSize == 0) { // it needs to be freed
free(buf); free(buf);
@ -44,6 +47,11 @@ void *cosmoM_reallocate(CState *state, void *buf, size_t oldSize, size_t newSize
// if NULL is passed, realloc() acts like malloc() // if NULL is passed, realloc() acts like malloc()
void *newBuf = realloc(buf, newSize); void *newBuf = realloc(buf, newSize);
#ifdef GC_DEBUG
printf("allocating new buffer of size %ld at %p\n", newSize - oldSize, newBuf);
fflush(stdout);
#endif
if (newBuf == NULL) { if (newBuf == NULL) {
CERROR("failed to allocate memory!"); CERROR("failed to allocate memory!");
exit(1); exit(1);

View File

@ -33,7 +33,7 @@ CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type)
obj->nextRoot = NULL; obj->nextRoot = NULL;
#ifdef GC_DEBUG #ifdef GC_DEBUG
printf("allocated %p with OBJ_TYPE %d\n", obj, type); printf("allocated %s %p\n", cosmoO_typeStr(obj), obj);
#endif #endif
return obj; return obj;
} }
@ -41,9 +41,7 @@ CObj *cosmoO_allocateBase(CState *state, size_t sz, CObjType type)
void cosmoO_free(CState *state, CObj *obj) void cosmoO_free(CState *state, CObj *obj)
{ {
#ifdef GC_DEBUG #ifdef GC_DEBUG
printf("freeing %p [", obj); printf("freeing %s %p\n", cosmoO_typeStr(obj), obj);
printObject(obj);
printf("]\n");
#endif #endif
switch (obj->type) { switch (obj->type) {
case COBJ_STRING: { case COBJ_STRING: {

View File

@ -26,7 +26,7 @@ CState *cosmoV_newState()
state->grayStack.count = 0; state->grayStack.count = 0;
state->grayStack.capacity = 2; state->grayStack.capacity = 2;
state->grayStack.array = NULL; state->grayStack.array = NULL;
state->allocatedBytes = sizeof(CState); state->allocatedBytes = 0;
state->nextGC = 1024 * 8; // threshhold starts at 8kb state->nextGC = 1024 * 8; // threshhold starts at 8kb
// init stack // init stack
@ -86,6 +86,12 @@ void cosmoV_freeState(CState *state)
CObj *objs = state->objects; CObj *objs = state->objects;
while (objs != NULL) { while (objs != NULL) {
CObj *next = objs->next; CObj *next = objs->next;
#ifdef GC_DEBUG
printf("STATE FREEING %p\n", objs);
fflush(stdout);
#endif
cosmoO_free(state, objs); cosmoO_free(state, objs);
objs = next; objs = next;
} }
@ -100,13 +106,13 @@ void cosmoV_freeState(CState *state)
// free our gray stack & finally free the state structure // free our gray stack & finally free the state structure
cosmoM_freearray(state, CObj *, state->grayStack.array, state->grayStack.capacity); cosmoM_freearray(state, CObj *, state->grayStack.array, state->grayStack.capacity);
// TODO: yeah idk, it looks like im missing 520 bytes somewhere? i'll look into it later // TODO: yeah idk, it looks like im missing 688 bytes somewhere? i'll look into it later
/*#ifdef GC_DEBUG #ifdef GC_DEBUG
if (state->allocatedBytes != sizeof(CState)) { if (state->allocatedBytes != 0) {
printf("state->allocatedBytes doesn't match expected value (%lu), got %lu!", printf("state->allocatedBytes doesn't match, got %lu\n", state->allocatedBytes);
sizeof(CState), state->allocatedBytes); exit(0);
} }
#endif*/ #endif
free(state); free(state);
} }