cheney_c89: valgrind

This commit is contained in:
Peter McGoron 2024-07-14 01:20:50 -04:00
parent b41e2100c2
commit 2effa0f98c
3 changed files with 26 additions and 7 deletions

View File

@ -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

View File

@ -16,6 +16,16 @@
*/
#include <stdio.h>
#ifdef UNS_VALGRIND
# include <valgrind/valgrind.h>
# 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 <stdlib.h>
#include <limits.h>
#include <assert.h>
@ -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;

View File

@ -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"