From a928277e8ce9cdba0c1393ca2c054037b911febd Mon Sep 17 00:00:00 2001 From: dzaima Date: Sat, 25 Feb 2023 00:20:51 +0200 Subject: [PATCH] make heapverify run on GC_VISIT_V2 also make it able to track shape object leaks --- src/core/harr.c | 1 + src/core/heap.c | 13 ++++++++++--- src/core/stuff.c | 5 ++++- src/core/stuff.h | 7 +++++++ src/core/tyarr.c | 2 +- src/core/tyarrTemplate.c | 2 +- src/opt/gc.c | 12 +++++++++++- 7 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/core/harr.c b/src/core/harr.c index 846e65b9..6cf2383c 100644 --- a/src/core/harr.c +++ b/src/core/harr.c @@ -93,6 +93,7 @@ DEF_FREE(harr) { for (usz i = 0; i < ia; i++) dec(p[i]); } static void harr_visit(Value* x) { + VISIT_SHAPE(x); usz ia = PIA((Arr*)x); B* p = ((HArr*)x)->a; for (usz i = 0; i < ia; i++) mm_visit(p[i]); } diff --git a/src/core/heap.c b/src/core/heap.c index 435e8dac..36b7094c 100644 --- a/src/core/heap.c +++ b/src/core/heap.c @@ -30,11 +30,18 @@ void heap_getReferents(Value* v) { TIv(v,visit)(v); } void gc_visitRoots(void); +void gcv2_runHeapverify(i32); void heapVerify() { heap_observed = 0; - heapVerify_mode=0; mm_forHeap(heapVerify_callVisit); gc_visitRoots(); - mm_forHeap(heapVerify_checkFn); - heapVerify_mode=1; mm_forHeap(heapVerify_callVisit); gc_visitRoots(); + #if GC_VISIT_V2 + gcv2_runHeapverify(0); + mm_forHeap(heapVerify_checkFn); + gcv2_runHeapverify(1); + #else + heapVerify_mode=0; mm_forHeap(heapVerify_callVisit); gc_visitRoots(); + mm_forHeap(heapVerify_checkFn); + heapVerify_mode=1; mm_forHeap(heapVerify_callVisit); gc_visitRoots(); + #endif if (heap_observed) { printf("refc of last: %d\n", heap_observed->refc); // heapVerify_mode=2; mm_forHeap(heap_getReferents); diff --git a/src/core/stuff.c b/src/core/stuff.c index 06c9d3e1..c948b1ad 100644 --- a/src/core/stuff.c +++ b/src/core/stuff.c @@ -44,6 +44,9 @@ NOINLINE B c2F(B f, B w, B x) { dec(w); dec(x); NOINLINE void value_freeF(Value* x) { value_free(x); } NOINLINE void decA_F(B x) { dec(x); } void noop_visit(Value* x) { } +#if HEAP_VERIFY && GC_VISIT_V2 + void arr_visit(Value* x) { VISIT_SHAPE(x); } +#endif NOINLINE B c1_bad(B f, B x) { thrM("This function can't be called monadically"); } NOINLINE B c2_bad(B f, B w, B x) { thrM("This function can't be called dyadically"); } NOINLINE B m1c1_bad(Md1D* d, B x) { thrM("This 1-modifier can't be called monadically"); } @@ -583,7 +586,7 @@ void tyarr_freeO(Value* x) { decSh(x); } void slice_freeO(Value* x) { ptr_dec(((Slice*)x)->p); decSh(x); } void tyarr_freeF(Value* x) { tyarr_freeO(x); mm_free(x); } void slice_freeF(Value* x) { slice_freeO(x); mm_free(x); } -void slice_visit(Value* x) { mm_visitP(((Slice*)x)->p); } +void slice_visit(Value* x) { mm_visitP(((Slice*)x)->p); VISIT_SHAPE(x); } void slice_print(B x) { arr_print(x); } char* type_repr(u8 u) { diff --git a/src/core/stuff.h b/src/core/stuff.h index b944c540..319d3206 100644 --- a/src/core/stuff.h +++ b/src/core/stuff.h @@ -222,6 +222,13 @@ B def_fn_ix(B t, B w, B x); B def_decompose(B x); void noop_visit(Value* x); +#if HEAP_VERIFY && GC_VISIT_V2 + void arr_visit(Value* x); + #define VISIT_SHAPE(X) ({ if (PRNK(X)>1) mm_visitP(shObjP(X)); }) +#else + #define arr_visit noop_visit + #define VISIT_SHAPE(X) +#endif #define CMP(W,X) ({ AUTO wt = (W); AUTO xt = (X); (wt>xt?1:0)-(wt