mirror of
https://github.com/janet-lang/janet
synced 2025-11-07 19:13:02 +00:00
Merge branch 'correctgc'
This commit is contained in:
@@ -50,26 +50,14 @@ static void janet_mark_string(const uint8_t *str);
|
|||||||
static void janet_mark_fiber(JanetFiber *fiber);
|
static void janet_mark_fiber(JanetFiber *fiber);
|
||||||
static void janet_mark_abstract(void *adata);
|
static void janet_mark_abstract(void *adata);
|
||||||
|
|
||||||
static JANET_THREAD_LOCAL int recursion_depth;
|
/* Local state that is only temporary */
|
||||||
|
static JANET_THREAD_LOCAL uint32_t depth = JANET_RECURSION_GUARD;
|
||||||
|
static JANET_THREAD_LOCAL uint32_t orig_rootcount;
|
||||||
|
|
||||||
/* Add a janet to the list for later processing */
|
/* Mark a value */
|
||||||
static void janet_addtolist(Janet x) {
|
|
||||||
if (janet_vm_gc_marklist_count >= janet_vm_gc_marklist_capacity) {
|
|
||||||
janet_vm_gc_marklist_capacity = (2 * janet_vm_gc_marklist_count) + 1;
|
|
||||||
janet_vm_gc_marklist = realloc(janet_vm_gc_marklist,
|
|
||||||
janet_vm_gc_marklist_capacity * sizeof(Janet));
|
|
||||||
if (NULL == janet_vm_gc_marklist) {
|
|
||||||
JANET_OUT_OF_MEMORY;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
janet_vm_gc_marklist[janet_vm_gc_marklist_count++] = x;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Mark a value Recurses on x, or pushes it to the black list
|
|
||||||
* if recursed too deeply (ran out of C stack).*/
|
|
||||||
void janet_mark(Janet x) {
|
void janet_mark(Janet x) {
|
||||||
if (recursion_depth) {
|
if (depth) {
|
||||||
recursion_depth--;
|
depth--;
|
||||||
switch (janet_type(x)) {
|
switch (janet_type(x)) {
|
||||||
default: break;
|
default: break;
|
||||||
case JANET_STRING:
|
case JANET_STRING:
|
||||||
@@ -83,9 +71,9 @@ void janet_mark(Janet x) {
|
|||||||
case JANET_FIBER: janet_mark_fiber(janet_unwrap_fiber(x)); break;
|
case JANET_FIBER: janet_mark_fiber(janet_unwrap_fiber(x)); break;
|
||||||
case JANET_ABSTRACT: janet_mark_abstract(janet_unwrap_abstract(x)); break;
|
case JANET_ABSTRACT: janet_mark_abstract(janet_unwrap_abstract(x)); break;
|
||||||
}
|
}
|
||||||
recursion_depth++;
|
depth++;
|
||||||
} else {
|
} else {
|
||||||
janet_addtolist(x);
|
janet_gcroot(x);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -338,13 +326,12 @@ void *janet_gcalloc(enum JanetMemoryType type, size_t size) {
|
|||||||
void janet_collect(void) {
|
void janet_collect(void) {
|
||||||
size_t i;
|
size_t i;
|
||||||
if (janet_vm_gc_suspend) return;
|
if (janet_vm_gc_suspend) return;
|
||||||
recursion_depth = JANET_RECURSION_GUARD;
|
depth = JANET_RECURSION_GUARD;
|
||||||
/* Mark the roots */
|
orig_rootcount = janet_vm_root_count;
|
||||||
for (i = 0; i < janet_vm_gc_marklist_rootcount; i++)
|
for (i = 0; i < orig_rootcount; i++)
|
||||||
janet_mark(janet_vm_gc_marklist[i]);
|
janet_mark(janet_vm_roots[i]);
|
||||||
/* While list not empty */
|
while (orig_rootcount < janet_vm_root_count) {
|
||||||
while (janet_vm_gc_marklist_count > janet_vm_gc_marklist_rootcount) {
|
Janet x = janet_vm_roots[--janet_vm_root_count];
|
||||||
Janet x = janet_vm_gc_marklist[--janet_vm_gc_marklist_count];
|
|
||||||
janet_mark(x);
|
janet_mark(x);
|
||||||
}
|
}
|
||||||
janet_sweep();
|
janet_sweep();
|
||||||
|
|||||||
Reference in New Issue
Block a user