From 2effa0f98cac2a2845aba5e303e977170f72a8d8 Mon Sep 17 00:00:00 2001 From: Peter McGoron Date: Sun, 14 Jul 2024 01:20:50 -0400 Subject: [PATCH] cheney_c89: valgrind --- Makefile | 6 +++++- cheney_c89.c | 21 ++++++++++++++++++--- gen_tests.sh | 6 +++--- 3 files changed, 26 insertions(+), 7 deletions(-) diff --git a/Makefile b/Makefile index 7440a9d..49fb35c 100644 --- a/Makefile +++ b/Makefile @@ -3,11 +3,15 @@ .SUFFIXES: .SUFFIXES: .c .o .test CC=cc +TEST_WRAPPER=valgrind --exit-on-first-error=yes -CFLAGS=-Wall -std=c89 -Werror -pedantic -fPIC -g -Iinclude -I. +CFLAGS=-Wall -std=c89 -Werror -pedantic -fPIC -g -Iinclude -I. -Wno-unused-function -Wno-unused-variable ${EXTRACFLAGS} tests: string_tests htable_tests +with_valgrind: + make EXTRACFLAGS=-DUNS_VALGRIND + COMMON_OBJECTS=uns.o uns.o: uns.c include/uns.h diff --git a/cheney_c89.c b/cheney_c89.c index d1b499d..b94fd55 100644 --- a/cheney_c89.c +++ b/cheney_c89.c @@ -16,6 +16,16 @@ */ #include +#ifdef UNS_VALGRIND +# include +# define REDZONE 16 +#else +# define REDZONE 0 +# define VALGRIND_CREATE_MEMPOOL(pool, rzB, is_zeroed) (void)0 +# define VALGRIND_DESTROY_MEMPOOL(pool) (void)0 +# define VALGRIND_MEMPOOL_ALLOC(pool, ptr, siz) (void)0 +#endif + #include #include #include @@ -186,7 +196,7 @@ static void hdr_write_direct(struct hdr *hdr, unsigned char *p) */ static int enough_space(struct ctx *ctx, uns_sword bytes) { - return ctx->tospace_end - ctx->tospace_alloc >= bytes + HDR_LEN; + return ctx->tospace_end - ctx->tospace_alloc >= bytes + HDR_LEN + REDZONE; } /** Allocate region without bounds checking. @@ -215,8 +225,10 @@ static void *raw_alloc(struct ctx *ctx, uns_sword len, int is_record) assert(enough_space(ctx, len)); p = ctx->tospace_alloc; + VALGRIND_MEMPOOL_ALLOC(ctx->tospace, p, len); + hdr_write_direct(&hdr, p); - ctx->tospace_alloc += len; + ctx->tospace_alloc += len + REDZONE; return p + HDR_LEN; } @@ -342,7 +354,7 @@ static void relocate_everything(Uns_GC gc) if (hdr.typ == RECORD) scan_record(ctx, scanptr, (size_t)hdr.len/sizeof(void*)); - scanptr += hdr.len; + scanptr += hdr.len + REDZONE; } } @@ -363,6 +375,7 @@ int uns_cheney_c89_collect(Uns_GC gc) */ assert(newlen >= fromspace_lim - fromspace); ctx->tospace = malloc(newlen); + VALGRIND_CREATE_MEMPOOL(ctx->tospace, REDZONE, 0); if (!ctx->tospace) { ctx->tospace = fromspace; return 1; @@ -374,6 +387,7 @@ int uns_cheney_c89_collect(Uns_GC gc) relocate_everything(gc); + VALGRIND_DESTROY_MEMPOOL(fromspace); free(fromspace); ctx->stats.usage_after = ctx->tospace_alloc - ctx->tospace; @@ -446,6 +460,7 @@ int uns_cheney_c89_init(Uns_GC gc, size_t heap_size) uns_deinit(gc); ctx->tospace_alloc = ctx->tospace = malloc(heap_size); + VALGRIND_CREATE_MEMPOOL(ctx->tospace, REDZONE, 0); if (!ctx->tospace) { free(ctx); return 0; diff --git a/gen_tests.sh b/gen_tests.sh index 16f1419..114dbf2 100755 --- a/gen_tests.sh +++ b/gen_tests.sh @@ -21,8 +21,8 @@ gen_test() { # test_name, collector_name, exec_deps, obj_file_deps echo " $TARGET: $DEPS - \${CC} \${LDFLAGS} $DEPS $TARGET - ./valgrind ./$TARGET + \${CC} \${TEST_LDFLAGS} $DEPS -o $TARGET + \${TEST_WRAPPER} ./$TARGET " TEST_TARGETS="$TEST_TARGETS $TARGET" @@ -72,7 +72,7 @@ gen_lisp_test() { #collector_name, exec_deps $SHIM_OBJ: $OBJDEPS $TARGET: $DEPS - \${CC} \${LDFLAGS} $DEPS -o $TARGET + \${CC} \${TEST_LDFLAGS} $DEPS -o $TARGET " TEST_TARGETS="$TEST_TARGETS $TARGET"