Sanitising debug-printf formats, mostly.

This commit is contained in:
Simon Brooke 2021-09-12 13:41:27 +01:00
parent be5cc4e528
commit 2c96e7c30d
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987
9 changed files with 26 additions and 5 deletions

4
.gitignore vendored
View file

@ -42,3 +42,7 @@ hi\.*
.vscode/ .vscode/
core core
.kdev4/
post-scarcity.kdev4

View file

@ -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 ) ? __int128_t result = ( __int128_t ) integerp( c ) ?
( val == 0 ) ? carry : val : op == '*' ? 1 : 0; ( val == 0 ) ? carry : val : op == '*' ? 1 : 0;
debug_printf( DEBUG_ARITH, 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", val, is_first_cell ? "true" : "false",
pointer2cell( c ).tag.bytes ); pointer2cell( c ).tag.bytes );
debug_print_128bit( result, DEBUG_ARITH ); debug_print_128bit( result, DEBUG_ARITH );

View file

@ -288,6 +288,7 @@ int main( int argc, char *argv[] ) {
dump_pages( file_to_url_file( stdout ) ); dump_pages( file_to_url_file( stdout ) );
} }
summarise_allocation();
curl_global_cleanup( ); curl_global_cleanup( );
return ( 0 ); return ( 0 );
} }

View file

@ -166,6 +166,7 @@ wint_t url_fgetwc( URL_FILE * input ) {
debug_print( L"url_fgetwc: back from url_fgets\n", debug_print( L"url_fgetwc: back from url_fgets\n",
DEBUG_IO ); DEBUG_IO );
int c = ( int ) cbuff[0]; int c = ( int ) cbuff[0];
// TODO: risk of reading off cbuff?
debug_printf( DEBUG_IO, debug_printf( DEBUG_IO,
L"url_fgetwc: cbuff is '%s'; (first) character = %d (%c)\n", L"url_fgetwc: cbuff is '%s'; (first) character = %d (%c)\n",
cbuff, c, c & 0xf7 ); cbuff, c, c & 0xf7 );

View file

@ -28,6 +28,12 @@
*/ */
bool conspageinitihasbeencalled = false; 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. * 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.car = NIL;
cell->payload.free.cdr = freelist; cell->payload.free.cdr = freelist;
freelist = pointer; freelist = pointer;
total_cells_freed ++;
} else { } else {
debug_printf( DEBUG_ALLOC, debug_printf( DEBUG_ALLOC,
L"ERROR: Attempt to free cell with %d dangling references at page %d, offset %d\n", 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.car = NIL;
cell->payload.cons.cdr = NIL; cell->payload.cons.cdr = NIL;
total_cells_allocated ++;
debug_printf( DEBUG_ALLOC, 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 ); result.page, result.offset );
} else { } else {
debug_printf( DEBUG_ALLOC, L"WARNING: Allocating non-free cell!" ); 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" ); 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 );
}

View file

@ -61,4 +61,6 @@ void initialise_cons_pages( );
void dump_pages( URL_FILE * output ); void dump_pages( URL_FILE * output );
void summarise_allocation();
#endif #endif

View file

@ -324,7 +324,7 @@ make_string_like_thing( wint_t c, struct cons_pointer tail, uint32_t tag ) {
} else { } else {
// \todo should throw an exception! // \todo should throw an exception!
debug_printf( DEBUG_ALLOC, 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 ); tag, tag );
} }

View file

@ -85,7 +85,7 @@ struct cons_pointer make_vso( uint32_t tag, uint64_t payload_size ) {
if ( vso != NULL ) { if ( vso != NULL ) {
memset( vso, 0, padded ); memset( vso, 0, padded );
debug_printf( DEBUG_ALLOC, 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 ); tag, vso );
vso->header.tag.value = tag; vso->header.tag.value = tag;
result = make_vec_pointer( vso, tag ); result = make_vec_pointer( vso, tag );