From dc374ed7e2e772850db2e32f536280506ee459ca Mon Sep 17 00:00:00 2001 From: Ivan Maidanski Date: Tue, 13 Nov 2012 08:04:41 +0400 Subject: [PATCH] Code refactoring of GC_X_printf to improve extensibility * misc.c (GC_DEFAULT_STDOUT_FD, GC_DEFAULT_STDERR_FD): New macro. * misc.c (GC_stdout, GC_stderr, GC_log): Use GC_DEFAULT_STDOUT_FD and GC_DEFAULT_STDERR_FD (only if not OS2, MACOS, Win32). * misc.c (GC_PRINTF_IMPL): Replace with GC_PRINTF_FILLBUF (move out "buf" array definition, move out WRITE call, remove "f" and "f_name" arguments, add "buf" argument, use sizeof(buf)-1 instead of BUFSZ. * misc.c (GC_printf, GC_err_printf, GC_log_printf): Declare and use "buf" array; replace GC_PRINTF_IMPL with GC_PRINTF_FILLBUF. * misc.c (GC_printf, GC_log_printf): Call WRITE to output "buf" content. * misc.c (GC_err_printf): Call GC_err_puts to output "buf" content. * misc.c (GC_default_warn_proc): Add TODO item. --- misc.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/misc.c b/misc.c index fb56bf39..c5117967 100644 --- a/misc.c +++ b/misc.c @@ -746,10 +746,13 @@ STATIC void GC_exit_check(void) # define maybe_install_looping_handler() #endif +#define GC_DEFAULT_STDOUT_FD 1 +#define GC_DEFAULT_STDERR_FD 2 + #if !defined(OS2) && !defined(MACOS) && !defined(MSWIN32) && !defined(MSWINCE) - STATIC int GC_stdout = 1; - STATIC int GC_stderr = 2; - STATIC int GC_log = 2; /* stderr */ + STATIC int GC_stdout = GC_DEFAULT_STDOUT_FD; + STATIC int GC_stderr = GC_DEFAULT_STDERR_FD; + STATIC int GC_log = GC_DEFAULT_STDERR_FD; #endif STATIC word GC_parse_mem_size_arg(const char *str) @@ -1455,37 +1458,44 @@ GC_API void GC_CALL GC_enable_incremental(void) /* Floating point arguments and formats should be avoided, since FP */ /* conversion is more likely to allocate memory. */ /* Assumes that no more than BUFSZ-1 characters are written at once. */ -#define GC_PRINTF_IMPL(f, f_name, format) { \ +#define GC_PRINTF_FILLBUF(buf, format) { \ va_list args; \ - char buf[BUFSZ + 1]; \ va_start(args, format); \ - buf[BUFSZ] = 0x15; \ - (void)vsnprintf(buf, BUFSZ, format, args); \ + (buf)[sizeof(buf) - 1] = 0x15; /* guard */ \ + (void)vsnprintf(buf, sizeof(buf) - 1, format, args); \ va_end(args); \ - if (buf[BUFSZ] != 0x15) \ + if ((buf)[sizeof(buf) - 1] != 0x15) \ ABORT("GC_printf clobbered stack"); \ - if (WRITE(f, buf, strlen(buf)) < 0) \ - ABORT("write to " f_name " failed"); \ } void GC_printf(const char *format, ...) { - if (GC_quiet) return; + char buf[BUFSZ + 1]; - GC_PRINTF_IMPL(GC_stdout, "stdout", format); + if (!GC_quiet) { + GC_PRINTF_FILLBUF(buf, format); + if (WRITE(GC_stdout, buf, strlen(buf)) < 0) + ABORT("write to stdout failed"); + } } void GC_err_printf(const char *format, ...) { - GC_PRINTF_IMPL(GC_stderr, "stderr", format); + char buf[BUFSZ + 1]; + + GC_PRINTF_FILLBUF(buf, format); + GC_err_puts(buf); } void GC_log_printf(const char *format, ...) { - GC_PRINTF_IMPL(GC_log, "log", format); + char buf[BUFSZ + 1]; + + GC_PRINTF_FILLBUF(buf, format); + if (WRITE(GC_log, buf, strlen(buf)) < 0) + ABORT("write to GC log failed"); } -/* This is equivalent to GC_err_printf("%s",s). */ void GC_err_puts(const char *s) { if (WRITE(GC_stderr, s, strlen(s)) < 0) ABORT("write to stderr failed"); @@ -1493,6 +1503,7 @@ void GC_err_puts(const char *s) STATIC void GC_CALLBACK GC_default_warn_proc(char *msg, GC_word arg) { + /* TODO: Add assertion on arg comply with msg (format). */ GC_err_printf(msg, arg); } -- 2.40.0