From 7d7bedc2bbf9c95aa6cf9b3a7628125e769b9f51 Mon Sep 17 00:00:00 2001 From: "hyunghwan.chung" Date: Sat, 28 Nov 2020 15:56:24 +0000 Subject: [PATCH] fixed a bug regarding lazy sweeping in moo_allocbytes --- moo/lib/exec.c | 4 +++- moo/lib/gc.c | 12 ++++++------ moo/lib/obj.c | 8 ++++++++ moo/lib/xma.c | 6 ++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/moo/lib/exec.c b/moo/lib/exec.c index bf2f2c1..3c8bf60 100644 --- a/moo/lib/exec.c +++ b/moo/lib/exec.c @@ -6355,10 +6355,12 @@ int moo_execute (moo_t* moo) moo->proc_switched = 0; moo->abort_req = 0; + moo->gci.lazy_sweep = 1; /* TODO: make it configurable?? */ -//moo->gci.lazy_sweep = 1; n = __execute (moo); + moo->gci.lazy_sweep = 0; + vm_cleanup (moo); moo->log.default_type_mask = log_default_type_mask; diff --git a/moo/lib/gc.c b/moo/lib/gc.c index 4fff5fc..c0eace3 100644 --- a/moo/lib/gc.c +++ b/moo/lib/gc.c @@ -1003,7 +1003,6 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize) freed_size = 0; -//printf ("starting lazy sweep...gci.ls.curr [%p]\n", moo->gci.ls.curr); prev = moo->gci.ls.prev; curr = moo->gci.ls.curr; @@ -1031,10 +1030,8 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize) if (freed_size > allocsize) /* TODO: can it secure large enough space? */ { -//printf ("stopping lazy sweeping after %lu free, allocsize %lu\n", (unsigned long int)freed_size, (unsigned long int)allocsize); - moo->gci.ls.curr = next; /* let the next lazy sweeping begin at this point */ moo->gci.ls.prev = prev; -//printf ("finished lazy sweep...\n"); + moo->gci.ls.curr = next; /* let the next lazy sweeping begin at this point */ return; } } @@ -1043,7 +1040,6 @@ void moo_gc_ms_sweep_lazy (moo_t* moo, moo_oow_t allocsize) } moo->gci.ls.curr = MOO_NULL; -//printf ("finished lazy sweep...\n"); } static MOO_INLINE void gc_ms_sweep (moo_t* moo) @@ -1075,7 +1071,6 @@ static MOO_INLINE void gc_ms_sweep (moo_t* moo) curr = next; } -printf ("finised full sweep...\n"); moo->gci.ls.curr = MOO_NULL; } #endif @@ -1397,6 +1392,8 @@ void moo_gc (moo_t* moo, int full) #if defined(MOO_ENABLE_GC_MARK_SWEEP) if (moo->gc_type == MOO_GC_TYPE_MARK_SWEEP) { +if (moo->gci.lazy_sweep) moo_gc_ms_sweep_lazy (moo, MOO_TYPE_MAX(moo_oow_t)); + MOO_LOG1 (moo, MOO_LOG_GC | MOO_LOG_INFO, "Starting GC (mark-sweep) - gci.bsz = %zu\n", moo->gci.bsz); moo->gci.stack.len = 0; @@ -1405,6 +1402,9 @@ void moo_gc (moo_t* moo, int full) if (!full && moo->gci.lazy_sweep) { + /* set the lazy sweeping point to the head of the allocated blocks. + * hawk_allocbytes() updates moo->gci.ls.prev if it is called while + * moo->gci.ls.curr stays at moo->gci.b */ moo->gci.ls.prev = MOO_NULL; moo->gci.ls.curr = moo->gci.b; } diff --git a/moo/lib/obj.c b/moo/lib/obj.c index ea47319..b2ccf3c 100644 --- a/moo/lib/obj.c +++ b/moo/lib/obj.c @@ -79,6 +79,14 @@ void* moo_allocbytes (moo_t* moo, moo_oow_t size) } } + if (moo->gci.lazy_sweep && moo->gci.ls.curr == moo->gci.b) + { + /* if the lazy sweeping point is at the beginning of the allocation block, + * moo->gc.ls.prev must get updated */ + MOO_ASSERT (moo, moo->gci.ls.prev == MOO_NULL); + moo->gci.ls.prev = gch; + } + gch->next = moo->gci.b; moo->gci.b = gch; moo->gci.bsz += size; diff --git a/moo/lib/xma.c b/moo/lib/xma.c index 34c37c0..1a9237e 100644 --- a/moo/lib/xma.c +++ b/moo/lib/xma.c @@ -236,6 +236,12 @@ int moo_xma_init (moo_xma_t* xma, moo_mmgr_t* mmgr, void* zoneptr, moo_oow_t zon internal = 1; /* internally created. must be freed upon moo_xma_fini() */ } + else if (zonesize < FBLKMINSIZE) + { + /* the zone size is too small for an externally allocated zone. */ +/* TODO: difference error code from memory allocation failure.. this is not really memory shortage */ + return -1; + } first = (moo_xma_fblk_t*)zoneptr;