harec

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 8780240eb382fd7e4b9c94975150053ddfcc267b
parent b643af3c085156975b1e21b599d9f0f67485eba2
Author: Drew DeVault <sir@cmpwn.com>
Date:   Sat, 30 Jan 2021 14:23:52 -0500

rt: check for memory leaks on exit

Diffstat:
Mrt/malloc.ha | 7+++++++
1 file changed, 7 insertions(+), 0 deletions(-)

diff --git a/rt/malloc.ha b/rt/malloc.ha @@ -45,6 +45,11 @@ fn size2bin(s: size) size = { return (s + (WORD * (ALIGN - 1z) - 1z)) / (WORD * ALIGN); }; +let nalloc: int = 0; +let nfree: int = 0; + +@fini fn fini() void = assert(nalloc == nfree); + // Allocates n bytes of memory and returns a pointer to them, or null if there // is insufficient memory. export fn malloc(n: size) nullable *void = { @@ -52,6 +57,7 @@ export fn malloc(n: size) nullable *void = { let r: nullable *void = if (n > bin2size(len(bins) - 1z)) malloc_large(n) else malloc_small(n); + nalloc += 1; return r; }; @@ -111,6 +117,7 @@ fn list_from_block(s: size, p: uintptr) nullable *void = { // Frees a pointer previously allocated with [malloc]. export @symbol("rt.free") fn free_(_p: nullable *void) void = { + nfree += 1; if (_p != null: nullable *void) { let p = _p: *void; let bsize = (p: uintptr - size(size): uintptr): *[1]size;