From 09bdca815b92c29178c3a2aeb8118b6acdddb237 Mon Sep 17 00:00:00 2001 From: hgn Date: Sat, 20 Sep 2025 16:28:58 +0000 Subject: [PATCH] there be macros --- build/foundation.txt | 13 ++++++ include/common_api.h | 62 ++++++++++++++++++-------- source/foundation/allocator_heap.c | 13 ++++-- source/foundation/allocator_pool.c | 2 - source/foundation/allocator_queue.c | 1 - source/foundation/allocator_stack.c | 18 +++----- source/foundation/allocator_stretchy.c | 8 ---- source/foundation/buffer_operations.c | 2 - source/foundation/exit.c | 18 ++++---- source/foundation/io.c | 1 - source/foundation/keyvalues.c | 1 - source/foundation/logging.c | 23 ++++------ source/foundation/options.c | 5 +-- source/foundation/stream.c | 43 +++++------------- source/foundation/string.c | 31 ++++++++++++- 15 files changed, 133 insertions(+), 108 deletions(-) create mode 100644 build/foundation.txt diff --git a/build/foundation.txt b/build/foundation.txt new file mode 100644 index 0000000..6844480 --- /dev/null +++ b/build/foundation.txt @@ -0,0 +1,13 @@ +vg/source/foundation/options.c +vg/source/foundation/logging.c +vg/source/foundation/allocator_heap.c +vg/source/foundation/allocator_stack.c +vg/source/foundation/allocator_pool.c +vg/source/foundation/allocator_queue.c +vg/source/foundation/allocator_stretchy.c +vg/source/foundation/stream.c +vg/source/foundation/string.c +vg/source/foundation/keyvalues.c +vg/source/foundation/exit.c +vg/source/foundation/io.c +vg/source/foundation/buffer_operations.c diff --git a/include/common_api.h b/include/common_api.h index a032458..5ab73c8 100644 --- a/include/common_api.h +++ b/include/common_api.h @@ -37,11 +37,10 @@ static inline f32 f32_clamp( f32 a, f32 min, f32 max ) { return f32_min( max, f3 static inline f32 f32_sign( f32 a ) { return a < 0.0f? -1.0f: 1.0f; } void _exit_init(void); -void _fatal_exit( const c8 *reason ); -void _normal_exit( const c8 *reason ); +void _fatal_exit(void); +void _normal_exit(void); #define ASSERT_CRITICAL( CONDITION ) \ - if( !(CONDITION) ) \ - _fatal_exit( "("__BECOME_STRING__(CONDITION) ") evaluated to 0\nLocation: " __LINE_STRING__ "\n" ); + if( !(CONDITION) ) { $log( $fatal, {$TO_STRING(CONDITION) "== 0"} ); _fatal_exit(); } /* Command line options * ------------------------------------------------------------------------------------------------------------------ */ @@ -193,6 +192,35 @@ void string_append_i64r( struct stream *string, i64 value, u64 base, u32 width, void string_append_u64( struct stream *string, u64 value, u64 base ); void string_append_f64( struct stream *string, f64 value, u64 base, u32 decimal_places ); +struct v_string_arg +{ + const c8 *_string; + union + { + u64 _u64; + i64 _i64; + f64 _f64; + }; + + u32 type; + u32 base; + u32 decimals; +}; +void v_string( struct stream *string, struct v_string_arg *argument_list ); + +#define k_$end 99 +#define k_$string 0 +#define k_$unsigned 2 +#define k_$signed 3 +#define k_$float 4 +#define k_$errno 5 + +#define $unsigned( X, ... ) { .type=k_$unsigned, ._u64=X, __VA_ARGS__ } +#define $signed( X, ... ) { .type=k_$signed, ._i64=X, __VA_ARGS__ } +#define $float( X, ... ) { .type=k_$float, ._f64=X, __VA_ARGS__ } +#define $errno( ... ) { .type=k_$errno, __VA_ARGS__ } +#define $v_string( STRING, ... ) v_string( STRING, (struct v_string_arg[]){ __VA_ARGS__, {.type=k_$end} } ) + enum string_parse_result { k_string_parse_ok, @@ -207,23 +235,21 @@ enum string_parse_result string_parse_f64( struct stream *string, f64 *value ); /* Logging * ------------------------------------------------------------------------------------------------------------------ */ -enum log_event -{ - k_log_low = 0x1, - k_log_info = 0x2, - k_log_ok = 0x4, - k_log_warning = 0x8, - k_log_error = 0x10 -}; + +#define $low 0x1 +#define $info 0x2 +#define $ok 0x4 +#define $warning 0x8 +#define $error 0x10 +#define $fatal 0x20 /* One day we will replace this shit with a good pre-processor. */ -#define __BECOME_STRING__(S) __BECOME_STRING_REALLY__(S) -#define __BECOME_STRING_REALLY__(S) #S -#define __LINE_STRING__ __FILE__ ":" __BECOME_STRING__(__LINE__) -#define _log( TYPE, TEXT ) _log_event( TYPE, TEXT, __LINE_STRING__ ) +#define $TO_STRING( S ) $TO_STRING_1( S ) +#define $TO_STRING_1( S ) #S +#define $line __FILE__ ":" $TO_STRING(__LINE__) +#define $log( C, ... ) $v_string( _log_event( C, $line ), __VA_ARGS__ ) -struct stream *_log_event( enum log_event type, const c8 *text, const c8 *code_location ); -void _log_append_errno( struct stream *stream ); +struct stream *_log_event( u32 type, const c8 *code_location ); struct stream *_get_console_stream(); /* Keyvalues diff --git a/source/foundation/allocator_heap.c b/source/foundation/allocator_heap.c index f969ce0..ad8a194 100644 --- a/source/foundation/allocator_heap.c +++ b/source/foundation/allocator_heap.c @@ -1,17 +1,24 @@ -#include "common_api.h" #include void *_heap_allocate( u64 size ) { void *buffer = malloc( size ); - if( !buffer ) _fatal_exit( "Out of heap memory (malloc)" ); + if( !buffer ) + { + $log( $fatal, {"Out of heap memory (malloc)"} ); + _fatal_exit(); + } return buffer; } void *_heap_reallocate( void *buf, u64 size ) { void *new_buffer = realloc( buf, size ); - if( !new_buffer ) _fatal_exit( "Out of heap memory (realloc)" ); + if( !new_buffer ) + { + $log( $fatal, {"Out of heap memory (malloc)"} ); + _fatal_exit(); + } return new_buffer; } diff --git a/source/foundation/allocator_pool.c b/source/foundation/allocator_pool.c index 6301acf..a35885a 100644 --- a/source/foundation/allocator_pool.c +++ b/source/foundation/allocator_pool.c @@ -1,5 +1,3 @@ -#include "common_api.h" - void pool_init( struct pool_allocator *pool, struct pool_node *nodes, u16 node_count, struct pool_chain *full_chain ) { pool->nodes = nodes; diff --git a/source/foundation/allocator_queue.c b/source/foundation/allocator_queue.c index 9fa8228..e69de29 100644 --- a/source/foundation/allocator_queue.c +++ b/source/foundation/allocator_queue.c @@ -1 +0,0 @@ -#include "common_api.h" diff --git a/source/foundation/allocator_stack.c b/source/foundation/allocator_stack.c index c1bfa12..fd310b0 100644 --- a/source/foundation/allocator_stack.c +++ b/source/foundation/allocator_stack.c @@ -1,5 +1,3 @@ -#include "common_api.h" - void stack_init( struct stack_allocator *stack, void *buffer, u32 capacity, const c8 *debug_name ) { zero_buffer( stack, sizeof(struct stack_allocator) ); @@ -18,17 +16,11 @@ void *stack_allocate( struct stack_allocator *stack, u32 size, u32 alignment, co u32 new_usage = stack->offset + size + alignment; if( new_usage > stack->capacity ) { - struct stream *log = _log_event( k_log_error, "Stack @", __LINE_STRING__ ); - string_append_u64( log, (u64)stack, 16 ); - string_append( log, ", capacity: " ); - string_append_u64( log, stack->capacity, 10 ); - string_append( log, ", used: " ); - string_append_u64( log, stack->offset, 10 ); - string_append( log, ", new_usage: " ); - string_append_u64( log, new_usage, 10 ); - string_append( log, "\n" ); - _fatal_exit( "Stack allocator overflow" ); - // log( F"Stack @{here}, capacity: {stack->capacity}, used: {stack->used}, new_usage: {new_usage}" ); + $log( $fatal, {"stack @0x"}, $unsigned( (u64)stack, .base=16 ), + {", capacity: "}, $unsigned( stack->capacity ), + {", used: "}, $unsigned( stack->offset ), + {", new: "}, $unsigned( new_usage ) ); + _fatal_exit(); } while( ((u64)stack->data + stack->offset) & (alignment-1) ) stack->offset ++; diff --git a/source/foundation/allocator_stretchy.c b/source/foundation/allocator_stretchy.c index d6857a6..6d913d0 100644 --- a/source/foundation/allocator_stretchy.c +++ b/source/foundation/allocator_stretchy.c @@ -1,6 +1,4 @@ -#include "common_api.h" #define SMALL_SEGMENTS 1 - void stretchy_init( struct stretchy_allocator *stretchy, u32 element_size ) { stretchy->count = 0; @@ -39,12 +37,6 @@ void stretchy_free( struct stretchy_allocator *stretchy ) u32 c = 1; for( u32 i=0; stretchy->count >= c; i ++ ) { - struct stream *log = _log( k_log_info, "" ); - string_append_u64( log, i, 10 ); - string_append( log, " freed: " ); - string_append_u64( log, (u64)stretchy->segments[i], 16 ); - string_append( log, "\n" ); - _heap_free( stretchy->segments[i] ); c += 1 << (SMALL_SEGMENTS + i); stretchy->segments[i] = NULL; diff --git a/source/foundation/buffer_operations.c b/source/foundation/buffer_operations.c index 99bd8cd..b9ad005 100644 --- a/source/foundation/buffer_operations.c +++ b/source/foundation/buffer_operations.c @@ -1,5 +1,3 @@ -#include "common_api.h" - void zero_buffer( void *buffer, u32 length ) { for( u32 i=0; i #include #include @@ -9,11 +8,12 @@ static void sync_signal_handler( i32 signum ) { raise( SIGTRAP ); - if( signum == SIGSEGV ) _fatal_exit( "SIGSEGV" ); - if( signum == SIGBUS ) _fatal_exit( "SIGBUS" ); - if( signum == SIGFPE ) _fatal_exit( "SIGFPE" ); - if( signum == SIGILL ) _fatal_exit( "SIGILL" ); - _fatal_exit( "UNKNOWN SIGNAL" ); + if( signum == SIGSEGV ) $log( $fatal, { "SIGSEGV" } ); + if( signum == SIGBUS ) $log( $fatal, { "SIGBUS" } ); + if( signum == SIGFPE ) $log( $fatal, { "SIGFPE" } ); + if( signum == SIGILL ) $log( $fatal, { "SIGILL" } ); + $log( $fatal, { "UNKNOWN SIGNAL" } ); + _fatal_exit(); } void _exit_init(void) @@ -24,11 +24,9 @@ void _exit_init(void) signal( SIGILL, sync_signal_handler ); } -void _fatal_exit( const c8 *reason ) +void _fatal_exit(void) { fflush( stdout ); - write( STDOUT_FILENO, reason, strlen(reason) ); - void *functions[20]; int count = backtrace( functions, 20 ); backtrace_symbols_fd( functions, count, STDOUT_FILENO ); @@ -36,7 +34,7 @@ void _fatal_exit( const c8 *reason ) exit(-1); } -void _normal_exit( const c8 *reason ) +void _normal_exit(void) { exit(0); } diff --git a/source/foundation/io.c b/source/foundation/io.c index 27ae615..9c421d8 100644 --- a/source/foundation/io.c +++ b/source/foundation/io.c @@ -1,4 +1,3 @@ -#include "common_api.h" #define _DEFAULT_SOURCE #include #include diff --git a/source/foundation/keyvalues.c b/source/foundation/keyvalues.c index c4af334..68fc0f5 100644 --- a/source/foundation/keyvalues.c +++ b/source/foundation/keyvalues.c @@ -1,4 +1,3 @@ -#include "common_api.h" #include #define KV_PAGE_COUNT 32 diff --git a/source/foundation/logging.c b/source/foundation/logging.c index f1fddc7..e13fabc 100644 --- a/source/foundation/logging.c +++ b/source/foundation/logging.c @@ -1,6 +1,4 @@ -#include "common_api.h" #include -#include #include static struct stream _stdout_stream; @@ -18,20 +16,17 @@ struct stream *_get_console_stream() return &_stdout_stream; } -struct stream *_log_event( enum log_event type, const c8 *text, const c8 *code_location ) +struct stream *_log_event( u32 type, const c8 *code_location ) { struct stream *output = _get_console_stream(); string_append( output, code_location ); - string_append( output, "|LOGGY| " ); - string_append( output, text ); - return output; -} + while( output->offset < 20 ) string_append_c8( output, ' ' ); -void _log_append_errno( struct stream *stream ) -{ - string_append( stream, " Errno: " ); - string_append_u64( stream, errno, 10 ); - string_append( stream, " (" ); - string_append( stream, strerror(errno) ); - string_append( stream, ")\n" ); + if( type == $error ) string_append( output, "ERR|" ); + else if( type == $warning ) string_append( output, "WRN|" ); + else if( type == $ok ) string_append( output, "OK |" ); + else if( type == $info ) string_append( output, "LOG|" ); + else if( type == $low ) string_append( output, "LOW|" ); + else if( type == $fatal ) string_append( output, "!!!|" ); + return output; } diff --git a/source/foundation/options.c b/source/foundation/options.c index c4aace3..3eca315 100644 --- a/source/foundation/options.c +++ b/source/foundation/options.c @@ -1,4 +1,3 @@ -#include "common_api.h" #define MAX_OPTIONS 32 #define MAX_ARGUMENTS 32 @@ -132,7 +131,7 @@ void _options_check_end(void) string_append( console, desc ); string_append_c8( console, '\n' ); } - _normal_exit( "Help page" ); + _normal_exit(); } bool errors = 0; @@ -164,7 +163,7 @@ void _options_check_end(void) } if( errors ) - _normal_exit( "Invalid argument" ); + _normal_exit(); } bool _option_flag( c8 c, const c8 *desc ) diff --git a/source/foundation/stream.c b/source/foundation/stream.c index 8d3b743..3c0e8ab 100644 --- a/source/foundation/stream.c +++ b/source/foundation/stream.c @@ -1,4 +1,3 @@ -#include "common_api.h" #include #include #include @@ -16,9 +15,7 @@ bool stream_open_file( struct stream *stream, const c8 *path, u32 flags ) #if defined( VG_ENGINE ) if( !_thread_has_flags( ENGINE_THREAD_BACKGROUND ) ) { - _log_event( k_log_warning, - "Performance: I/O file stream opened in main thread. This will cause frame stalls!\n", - __LINE_STRING__ ); + $log( $warning, {"Performance: I/O file stream opened in main thread. This will cause frame stalls!"} ) } #endif stream->posix_stream = fopen( path, (flags & k_stream_write)? "wb": "rb" ); @@ -29,11 +26,7 @@ bool stream_open_file( struct stream *stream, const c8 *path, u32 flags ) return 1; else { - struct stream *log = _log_event( k_log_error, "Failure to open file stream ( ", __LINE_STRING__ ); - string_append_u64( log, errno, 10 ); - string_append( log, ": " ); - string_append( log, strerror(errno) ); - string_append( log, " )\n" ); + $log( $error, {"Failure to open file stream "}, $errno() ); return 0; } } @@ -70,19 +63,15 @@ u32 stream_read( struct stream *stream, void *buffer, u32 length ) { if( ferror( stream->posix_stream ) ) { - struct stream *log = _log_event( k_log_error, "FILE read error ( ", __LINE_STRING__ ); - string_append_u64( log, errno, 10 ); - string_append( log, ": " ); - string_append( log, strerror(errno) ); - string_append( log, " )\n" ); - + $log( $fatal, {"FILE read error "}, $errno() ); fclose( stream->posix_stream ); - _fatal_exit( "FILE stream read error" ); + _fatal_exit(); } else { + $log( $fatal, {"FILE stream read error (no reason was given)"} ); fclose( stream->posix_stream ); - _fatal_exit( "FILE stream read error (no reason was given)" ); + _fatal_exit(); } } } @@ -111,18 +100,14 @@ u32 stream_write( struct stream *stream, const void *buffer, u32 length ) { if( ferror( stream->posix_stream ) ) { - struct stream *log = _log_event( k_log_error, "FILE write error ( ", __LINE_STRING__ ); - string_append_u64( log, errno, 10 ); - string_append( log, ": " ); - string_append( log, strerror(errno) ); - string_append( log, " )\n" ); - fclose( stream->posix_stream ); - _fatal_exit( "FILE stream write error" ); + $log( $fatal, {"FILE write error "}, $errno() ); + _fatal_exit(); } else { fclose( stream->posix_stream ); - _fatal_exit( "FILE stream write error (no reason was given)" ); + $log( $fatal, {"FILE stream write error (no reason was given)"} ); + _fatal_exit(); } } } @@ -145,13 +130,9 @@ void stream_seek( struct stream *stream, u32 offset ) { if( fseek( stream->posix_stream, offset, SEEK_SET ) == -1 ) { - struct stream *log = _log_event( k_log_error, "FILE seek error ( ", __LINE_STRING__ ); - string_append_u64( log, errno, 10 ); - string_append( log, ": " ); - string_append( log, strerror(errno) ); - string_append( log, " )\n" ); + $log( $fatal, {"File seek error "}, $errno() ); fclose( stream->posix_stream ); - _fatal_exit( "FILE stream seek error" ); + _fatal_exit(); } } stream->offset = offset; diff --git a/source/foundation/string.c b/source/foundation/string.c index 820a350..9a03fba 100644 --- a/source/foundation/string.c +++ b/source/foundation/string.c @@ -1,4 +1,5 @@ -#include "common_api.h" +#include +#include void string_init( struct stream *string, c8 *buffer, u32 buffer_length ) { @@ -259,3 +260,31 @@ enum string_parse_result string_parse_f64( struct stream *string, f64 *value ) *value = (f64)sign * (int_part + decimal_part); return (got||got_decimal)? info: k_string_parse_eof; } + +void v_string( struct stream *string, struct v_string_arg *argument_list ) +{ + for( u32 i=0; i<1000; i ++ ) + { + struct v_string_arg *arg = &argument_list[i]; + if( arg->type == k_$end ) + break; + else if( arg->type == k_$unsigned ) + string_append_u64( string, arg->_u64, arg->base? arg->base: 10 ); + else if( arg->type == k_$signed ) + string_append_i64( string, arg->_i64, arg->base? arg->base: 10 ); + else if( arg->type == k_$float ) + string_append_f64( string, arg->_f64, arg->base? arg->base: 10, arg->decimals? arg->decimals: 2 ); + else if( arg->type == k_$string ) + string_append( string, arg->_string ); + else if( arg->type == k_$errno ) + { + string_append( string, "" ); + } + } + string_append_c8( string, '\n' ); +} + -- 2.25.1