From 2c96e7c30d8d152b4bc5fb88770c0a8e37348975 Mon Sep 17 00:00:00 2001 From: Simon Brooke Date: Sun, 12 Sep 2021 13:41:27 +0100 Subject: [PATCH] Sanitising debug-printf formats, mostly. --- .gitignore | 4 ++++ Makefile | 2 +- src/arith/integer.c | 2 +- src/init.c | 1 + src/io/io.c | 1 + src/memory/conspage.c | 15 ++++++++++++++- src/memory/conspage.h | 2 ++ src/memory/consspaceobject.c | 2 +- src/memory/vectorspace.c | 2 +- 9 files changed, 26 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index a85ac01..b07b2a6 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,7 @@ hi\.* .vscode/ core + +.kdev4/ + +post-scarcity.kdev4 diff --git a/Makefile b/Makefile index d8e6e81..7e5efb4 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SRC_DIRS ?= ./src SRCS := $(shell find $(SRC_DIRS) -name *.cpp -or -name *.c -or -name *.s) HDRS := $(shell find $(SRC_DIRS) -name *.h) -OBJS := $(addsuffix .o,$(basename $(SRCS))) +OBJS := $(addsuffix .o,$(basename $(SRCS))) DEPS := $(OBJS:.o=.d) TESTS := $(shell find unit-tests -name *.sh) diff --git a/src/arith/integer.c b/src/arith/integer.c index db486d2..06ef8a3 100644 --- a/src/arith/integer.c +++ b/src/arith/integer.c @@ -76,7 +76,7 @@ __int128_t cell_value( struct cons_pointer c, char op, bool is_first_cell ) { __int128_t result = ( __int128_t ) integerp( c ) ? ( val == 0 ) ? carry : val : op == '*' ? 1 : 0; debug_printf( DEBUG_ARITH, - L"cell_value: raw value is %ld, is_first_cell = %s; %4.4s; returning ", + L"cell_value: raw value is %ld, is_first_cell = %s; '%4.4s'; returning ", val, is_first_cell ? "true" : "false", pointer2cell( c ).tag.bytes ); debug_print_128bit( result, DEBUG_ARITH ); diff --git a/src/init.c b/src/init.c index 031d0ba..a47f008 100644 --- a/src/init.c +++ b/src/init.c @@ -288,6 +288,7 @@ int main( int argc, char *argv[] ) { dump_pages( file_to_url_file( stdout ) ); } + summarise_allocation(); curl_global_cleanup( ); return ( 0 ); } diff --git a/src/io/io.c b/src/io/io.c index 72830a4..fe08a77 100644 --- a/src/io/io.c +++ b/src/io/io.c @@ -166,6 +166,7 @@ wint_t url_fgetwc( URL_FILE * input ) { debug_print( L"url_fgetwc: back from url_fgets\n", DEBUG_IO ); int c = ( int ) cbuff[0]; + // TODO: risk of reading off cbuff? debug_printf( DEBUG_IO, L"url_fgetwc: cbuff is '%s'; (first) character = %d (%c)\n", cbuff, c, c & 0xf7 ); diff --git a/src/memory/conspage.c b/src/memory/conspage.c index d8d54f9..f967c74 100644 --- a/src/memory/conspage.c +++ b/src/memory/conspage.c @@ -28,6 +28,12 @@ */ bool conspageinitihasbeencalled = false; +/** + * keep track of total cells allocated and freed to check for leakage. + */ +uint64_t total_cells_allocated = 0; +uint64_t total_cells_freed = 0; + /** * the number of cons pages which have thus far been initialised. */ @@ -187,6 +193,7 @@ void free_cell( struct cons_pointer pointer ) { cell->payload.free.car = NIL; cell->payload.free.cdr = freelist; freelist = pointer; + total_cells_freed ++; } else { debug_printf( DEBUG_ALLOC, L"ERROR: Attempt to free cell with %d dangling references at page %d, offset %d\n", @@ -228,8 +235,10 @@ struct cons_pointer allocate_cell( uint32_t tag ) { cell->payload.cons.car = NIL; cell->payload.cons.cdr = NIL; + total_cells_allocated ++; + debug_printf( DEBUG_ALLOC, - L"Allocated cell of type '%s' at %d, %d \n", tag, + L"Allocated cell of type '%4.4s' at %d, %d \n", tag, result.page, result.offset ); } else { debug_printf( DEBUG_ALLOC, L"WARNING: Allocating non-free cell!" ); @@ -255,3 +264,7 @@ void initialise_cons_pages( ) { L"WARNING: initialise_cons_pages() called a second or subsequent time\n" ); } } + +void summarise_allocation() { + fwprintf(stderr, L"Allocation summary: allocated %lld; deallocated %lld.\n", total_cells_allocated, total_cells_freed ); +} \ No newline at end of file diff --git a/src/memory/conspage.h b/src/memory/conspage.h index 260794e..18eda3b 100644 --- a/src/memory/conspage.h +++ b/src/memory/conspage.h @@ -61,4 +61,6 @@ void initialise_cons_pages( ); void dump_pages( URL_FILE * output ); +void summarise_allocation(); + #endif diff --git a/src/memory/consspaceobject.c b/src/memory/consspaceobject.c index 5b04699..0eef3d5 100644 --- a/src/memory/consspaceobject.c +++ b/src/memory/consspaceobject.c @@ -324,7 +324,7 @@ make_string_like_thing( wint_t c, struct cons_pointer tail, uint32_t tag ) { } else { // \todo should throw an exception! debug_printf( DEBUG_ALLOC, - L"Warning: only NIL and %s can be prepended to %s\n", + L"Warning: only NIL and %4.4s can be prepended to %4.4s\n", tag, tag ); } diff --git a/src/memory/vectorspace.c b/src/memory/vectorspace.c index 4709482..0c1b159 100644 --- a/src/memory/vectorspace.c +++ b/src/memory/vectorspace.c @@ -85,7 +85,7 @@ struct cons_pointer make_vso( uint32_t tag, uint64_t payload_size ) { if ( vso != NULL ) { memset( vso, 0, padded ); debug_printf( DEBUG_ALLOC, - L"make_vso: about to write tag '%s' into vso at %p\n", + L"make_vso: about to write tag '%4.4s' into vso at %p\n", tag, vso ); vso->header.tag.value = tag; result = make_vec_pointer( vso, tag );