mirror of
https://github.com/CPunch/Cosmo.git
synced 2024-11-22 15:30:06 +00:00
minor refactoring
This commit is contained in:
parent
d00b803e6f
commit
aff011a8d1
19
src/cmem.c
19
src/cmem.c
@ -149,19 +149,10 @@ void markObject(CState *state, CObj *obj) {
|
|||||||
if (obj->type == COBJ_CFUNCTION || obj->type == COBJ_STRING)
|
if (obj->type == COBJ_CFUNCTION || obj->type == COBJ_STRING)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
// we don't use cosmoM_growarray because we don't want to trigger another GC event while in the GC!
|
// we can use cosmoM_growaraay because we lock the GC when we entered in cosmoM_collectGarbage
|
||||||
if (state->grayCount >= state->grayCapacity || state->grayStack == NULL) {
|
cosmoM_growarray(state, CObj*, state->grayStack.array, state->grayStack.count, state->grayStack.capacity);
|
||||||
int old = state->grayCapacity;
|
|
||||||
state->grayCapacity = old * GROW_FACTOR;
|
|
||||||
state->grayStack = (CObj**)realloc(state->grayStack, sizeof(CObj*) * state->grayCapacity);
|
|
||||||
|
|
||||||
if (state->grayStack == NULL) {
|
state->grayStack.array[state->grayStack.count++] = obj;
|
||||||
CERROR("failed to allocate memory for grayStack!");
|
|
||||||
exit(1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
state->grayStack[state->grayCount++] = obj;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void markValue(CState *state, CValue val) {
|
void markValue(CState *state, CValue val) {
|
||||||
@ -171,8 +162,8 @@ void markValue(CState *state, CValue val) {
|
|||||||
|
|
||||||
// trace our gray references
|
// trace our gray references
|
||||||
void traceGrays(CState *state) {
|
void traceGrays(CState *state) {
|
||||||
while (state->grayCount > 0) {
|
while (state->grayStack.count > 0) {
|
||||||
CObj* obj = state->grayStack[--state->grayCount];
|
CObj* obj = state->grayStack.array[--state->grayStack.count];
|
||||||
blackenObject(state, obj);
|
blackenObject(state, obj);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -20,9 +20,9 @@ CState *cosmoV_newState() {
|
|||||||
|
|
||||||
// GC
|
// GC
|
||||||
state->objects = NULL;
|
state->objects = NULL;
|
||||||
state->grayCount = 0;
|
state->grayStack.count = 0;
|
||||||
state->grayCapacity = 2;
|
state->grayStack.capacity = 2;
|
||||||
state->grayStack = NULL;
|
state->grayStack.array = NULL;
|
||||||
state->allocatedBytes = 0;
|
state->allocatedBytes = 0;
|
||||||
state->nextGC = 1024 * 8; // threshhold starts at 8kb
|
state->nextGC = 1024 * 8; // threshhold starts at 8kb
|
||||||
|
|
||||||
@ -72,7 +72,7 @@ void cosmoV_freeState(CState *state) {
|
|||||||
cosmoT_clearTable(state, &state->globals);
|
cosmoT_clearTable(state, &state->globals);
|
||||||
|
|
||||||
// free our gray stack & finally free the state structure
|
// free our gray stack & finally free the state structure
|
||||||
free(state->grayStack);
|
free(state->grayStack.array);
|
||||||
free(state);
|
free(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
10
src/cstate.h
10
src/cstate.h
@ -22,13 +22,17 @@ typedef enum IStringEnum {
|
|||||||
ISTRING_MAX
|
ISTRING_MAX
|
||||||
} IStringEnum;
|
} IStringEnum;
|
||||||
|
|
||||||
|
typedef struct ArrayCObj {
|
||||||
|
CObj **array;
|
||||||
|
int count;
|
||||||
|
int capacity;
|
||||||
|
} ArrayCObj;
|
||||||
|
|
||||||
typedef struct CState {
|
typedef struct CState {
|
||||||
bool panic;
|
bool panic;
|
||||||
int freezeGC; // when > 0, GC events will be ignored (for internal use)
|
int freezeGC; // when > 0, GC events will be ignored (for internal use)
|
||||||
CObj *objects; // tracks all of our allocated objects
|
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
|
ArrayCObj grayStack; // keeps track of which objects *haven't yet* been traversed in our GC, but *have been* found
|
||||||
int grayCount;
|
|
||||||
int grayCapacity;
|
|
||||||
size_t allocatedBytes;
|
size_t allocatedBytes;
|
||||||
size_t nextGC; // when allocatedBytes reaches this threshhold, trigger a GC event
|
size_t nextGC; // when allocatedBytes reaches this threshhold, trigger a GC event
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user