minor refactoring

This commit is contained in:
CPunch 2020-12-07 14:35:14 -06:00
parent d00b803e6f
commit aff011a8d1
3 changed files with 16 additions and 21 deletions

View File

@ -149,19 +149,10 @@ void markObject(CState *state, CObj *obj) {
if (obj->type == COBJ_CFUNCTION || obj->type == COBJ_STRING)
return;
// we don't use cosmoM_growarray because we don't want to trigger another GC event while in the GC!
if (state->grayCount >= state->grayCapacity || state->grayStack == NULL) {
int old = state->grayCapacity;
state->grayCapacity = old * GROW_FACTOR;
state->grayStack = (CObj**)realloc(state->grayStack, sizeof(CObj*) * state->grayCapacity);
// we can use cosmoM_growaraay because we lock the GC when we entered in cosmoM_collectGarbage
cosmoM_growarray(state, CObj*, state->grayStack.array, state->grayStack.count, state->grayStack.capacity);
if (state->grayStack == NULL) {
CERROR("failed to allocate memory for grayStack!");
exit(1);
}
}
state->grayStack[state->grayCount++] = obj;
state->grayStack.array[state->grayStack.count++] = obj;
}
void markValue(CState *state, CValue val) {
@ -171,8 +162,8 @@ void markValue(CState *state, CValue val) {
// trace our gray references
void traceGrays(CState *state) {
while (state->grayCount > 0) {
CObj* obj = state->grayStack[--state->grayCount];
while (state->grayStack.count > 0) {
CObj* obj = state->grayStack.array[--state->grayStack.count];
blackenObject(state, obj);
}
}

View File

@ -20,9 +20,9 @@ CState *cosmoV_newState() {
// GC
state->objects = NULL;
state->grayCount = 0;
state->grayCapacity = 2;
state->grayStack = NULL;
state->grayStack.count = 0;
state->grayStack.capacity = 2;
state->grayStack.array = NULL;
state->allocatedBytes = 0;
state->nextGC = 1024 * 8; // threshhold starts at 8kb
@ -72,7 +72,7 @@ void cosmoV_freeState(CState *state) {
cosmoT_clearTable(state, &state->globals);
// free our gray stack & finally free the state structure
free(state->grayStack);
free(state->grayStack.array);
free(state);
}

View File

@ -22,13 +22,17 @@ typedef enum IStringEnum {
ISTRING_MAX
} IStringEnum;
typedef struct ArrayCObj {
CObj **array;
int count;
int capacity;
} ArrayCObj;
typedef struct CState {
bool panic;
int freezeGC; // when > 0, GC events will be ignored (for internal use)
CObj *objects; // tracks all of our allocated objects
CObj **grayStack; // keeps track of which objects *haven't yet* been traversed in our GC, but *have been* found
int grayCount;
int grayCapacity;
ArrayCObj grayStack; // keeps track of which objects *haven't yet* been traversed in our GC, but *have been* found
size_t allocatedBytes;
size_t nextGC; // when allocatedBytes reaches this threshhold, trigger a GC event