*polished code.
This commit is contained in:
parent
ee213dc84b
commit
9eae920bfc
@ -5776,46 +5776,49 @@ static void _gc_try_trigger(mb_interpreter_t* s) {
|
|||||||
static void _gc_collect_garbage(mb_interpreter_t* s, int depth) {
|
static void _gc_collect_garbage(mb_interpreter_t* s, int depth) {
|
||||||
/* Collect all garbage */
|
/* Collect all garbage */
|
||||||
_ht_node_t* valid = 0;
|
_ht_node_t* valid = 0;
|
||||||
|
_gc_t* gc = 0;
|
||||||
|
|
||||||
mb_assert(s);
|
mb_assert(s);
|
||||||
|
|
||||||
|
gc = &s->gc;
|
||||||
|
|
||||||
/* Avoid infinity loop */
|
/* Avoid infinity loop */
|
||||||
if(s->gc.collecting) return;
|
if(gc->collecting) return;
|
||||||
s->gc.collecting++;
|
gc->collecting++;
|
||||||
|
|
||||||
/* Get reachable information */
|
/* Get reachable information */
|
||||||
valid = _ht_create(0, _ht_cmp_ref, _ht_hash_ref, _do_nothing_on_object);
|
valid = _ht_create(0, _ht_cmp_ref, _ht_hash_ref, _do_nothing_on_object);
|
||||||
if(depth != -1)
|
if(depth != -1)
|
||||||
s->gc.valid_table = valid;
|
gc->valid_table = valid;
|
||||||
_gc_get_reachable(s, valid);
|
_gc_get_reachable(s, valid);
|
||||||
|
|
||||||
/* Get unreachable information */
|
/* Get unreachable information */
|
||||||
_HT_FOREACH(valid, _do_nothing_on_object, _ht_remove_exist, s->gc.table);
|
_HT_FOREACH(valid, _do_nothing_on_object, _ht_remove_exist, gc->table);
|
||||||
|
|
||||||
/* Collect garbage */
|
/* Collect garbage */
|
||||||
do {
|
do {
|
||||||
#ifdef MB_ENABLE_CLASS
|
#ifdef MB_ENABLE_CLASS
|
||||||
_HT_FOREACH(s->gc.table, _do_nothing_on_object, _gc_destroy_garbage_class, &s->gc);
|
_HT_FOREACH(gc->table, _do_nothing_on_object, _gc_destroy_garbage_class, &s->gc);
|
||||||
#endif /* MB_ENABLE_CLASS */
|
#endif /* MB_ENABLE_CLASS */
|
||||||
_HT_FOREACH(s->gc.table, _do_nothing_on_object, _gc_destroy_garbage, &s->gc);
|
_HT_FOREACH(gc->table, _do_nothing_on_object, _gc_destroy_garbage, &s->gc);
|
||||||
_ht_clear(s->gc.table);
|
_ht_clear(gc->table);
|
||||||
if(s->gc.collecting > 1)
|
if(gc->collecting > 1)
|
||||||
s->gc.collecting--;
|
gc->collecting--;
|
||||||
|
|
||||||
if(!depth || !_ht_count(s->gc.recursive_table))
|
if(!depth || !_ht_count(gc->recursive_table))
|
||||||
break;
|
break;
|
||||||
|
|
||||||
_gc_swap_tables(s);
|
_gc_swap_tables(s);
|
||||||
s->gc.collecting++;
|
gc->collecting++;
|
||||||
} while(1);
|
} while(1);
|
||||||
|
|
||||||
/* Tidy */
|
/* Tidy */
|
||||||
_ht_clear(s->gc.collected_table);
|
_ht_clear(gc->collected_table);
|
||||||
s->gc.valid_table = 0;
|
gc->valid_table = 0;
|
||||||
_ht_clear(valid);
|
_ht_clear(valid);
|
||||||
_ht_destroy(valid);
|
_ht_destroy(valid);
|
||||||
s->gc.collecting--;
|
gc->collecting--;
|
||||||
mb_assert(!s->gc.collecting);
|
mb_assert(!gc->collecting);
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifdef MB_ENABLE_USERTYPE_REF
|
#ifdef MB_ENABLE_USERTYPE_REF
|
||||||
@ -7381,6 +7384,11 @@ static void _unref_routine(_ref_t* ref, void* data) {
|
|||||||
|
|
||||||
static void _destroy_routine(mb_interpreter_t* s, _routine_t* r) {
|
static void _destroy_routine(mb_interpreter_t* s, _routine_t* r) {
|
||||||
/* Destroy a lambda routine */
|
/* Destroy a lambda routine */
|
||||||
|
_gc_t* gc = 0;
|
||||||
|
|
||||||
|
mb_assert(r);
|
||||||
|
|
||||||
|
if(s) gc = &s->gc;
|
||||||
if(r->name) {
|
if(r->name) {
|
||||||
safe_free(r->name);
|
safe_free(r->name);
|
||||||
}
|
}
|
||||||
@ -7409,7 +7417,7 @@ static void _destroy_routine(mb_interpreter_t* s, _routine_t* r) {
|
|||||||
safe_free(r->func.lambda.scope);
|
safe_free(r->func.lambda.scope);
|
||||||
if(r->func.lambda.parameters)
|
if(r->func.lambda.parameters)
|
||||||
_ls_destroy(r->func.lambda.parameters);
|
_ls_destroy(r->func.lambda.parameters);
|
||||||
if(r->func.lambda.outer_scope && !_ht_find(s->gc.collected_table, &r->func.lambda.outer_scope->ref))
|
if(r->func.lambda.outer_scope && !_ht_find(gc->collected_table, &r->func.lambda.outer_scope->ref))
|
||||||
_unref(&r->func.lambda.outer_scope->ref, r->func.lambda.outer_scope);
|
_unref(&r->func.lambda.outer_scope->ref, r->func.lambda.outer_scope);
|
||||||
if(r->func.lambda.upvalues)
|
if(r->func.lambda.upvalues)
|
||||||
_ht_destroy(r->func.lambda.upvalues);
|
_ht_destroy(r->func.lambda.upvalues);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user