diff --git a/src/opt/gc.c b/src/opt/gc.c index e605ed11..32236292 100644 --- a/src/opt/gc.c +++ b/src/opt/gc.c @@ -41,7 +41,9 @@ void gc_tryFree(Value* v) { gc_freedBytes+= mm_size(v); gc_freedCount++; #endif v->type = t_freed; + ptr_inc(v); // required as otherwise the object may free itself while not done reading its own fields ti[t].free(v); + ptr_dec(v); // Object may not be immediately freed if it's not a part of a cycle, but instead a descendant of one. // It will be freed when the cycle is freed, and the t_freed type ensures it doesn't double-free itself } diff --git a/src/opt/mm_buddyTemplate.c b/src/opt/mm_buddyTemplate.c index 269958b9..da1c5a16 100644 --- a/src/opt/mm_buddyTemplate.c +++ b/src/opt/mm_buddyTemplate.c @@ -25,6 +25,9 @@ NOINLINE EmptyValue* BN(makeEmpty)(u8 bucket) { // result->next is garbage mm_heapAlloc+= sz; // gc_maybeGC(); c = mmap(NULL, sz, PROT_READ|PROT_WRITE, MAP_NORESERVE|MAP_PRIVATE|MAP_ANON, -1, 0); + #ifdef USE_VALGRIND + VALGRIND_MAKE_MEM_UNDEFINED(c, sz); + #endif if (alSize+1>=alCap) { alCap = alCap? alCap*2 : 1024; al = realloc(al, sizeof(AllocInfo)*alCap); diff --git a/src/opt/mm_buddyTemplate.h b/src/opt/mm_buddyTemplate.h index 908ce15d..b30ee725 100644 --- a/src/opt/mm_buddyTemplate.h +++ b/src/opt/mm_buddyTemplate.h @@ -45,6 +45,12 @@ static void* BN(allocL)(u8 bucket, u8 type) { x->flags = x->extra = x->type = 0; x->refc = 1; x->type = type; + #ifdef DEBUG + u64* p = (u64*)x; + u64* s = p + sizeof(Value)/8; + u64* e = p + BSZ(bucket)/8; + while(sext->vars[(u32)s.u] = inc(x); } else { VTY(s, t_harr);