From: Ivan Maidanski Date: Thu, 19 Jul 2012 09:48:07 +0000 (+0400) Subject: Code refactoring of GC_x_printf (move shared code to macro) X-Git-Tag: gc7_4_0~257 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=035af2671bf789a69b4a63d52bbaa38305fe60d4;p=gc Code refactoring of GC_x_printf (move shared code to macro) * misc.c (GC_PRINTF_IMPL): New macro (used by GC_[err/log_]printf only); copy code from GC_err_printf parametrizing output stream. * misc.c (GC_printf, GC_err_printf, GC_log_printf): Replace shared code with GC_PRINTF_IMPL macro usage. --- diff --git a/misc.c b/misc.c index e5dbdf79..94f90560 100644 --- a/misc.c +++ b/misc.c @@ -1369,52 +1369,40 @@ GC_API void GC_CALL GC_enable_incremental(void) # define vsnprintf _vsnprintf # endif #endif + /* A version of printf that is unlikely to call malloc, and is thus safer */ /* to call from the collector in case malloc has been bound to GC_malloc. */ -/* Floating point arguments and formats should be avoided, since fp */ -/* conversion is more likely to allocate. */ +/* 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) { \ + va_list args; \ + char buf[BUFSZ + 1]; \ + va_start(args, format); \ + buf[BUFSZ] = 0x15; \ + (void)vsnprintf(buf, BUFSZ, format, args); \ + va_end(args); \ + if (buf[BUFSZ] != 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, ...) { - va_list args; - char buf[BUFSZ+1]; - if (GC_quiet) return; - va_start(args, format); - buf[BUFSZ] = 0x15; - (void) vsnprintf(buf, BUFSZ, format, args); - va_end(args); - if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack"); - if (WRITE(GC_stdout, buf, strlen(buf)) < 0) - ABORT("write to stdout failed"); + + GC_PRINTF_IMPL(GC_stdout, "stdout", format); } void GC_err_printf(const char *format, ...) { - va_list args; - char buf[BUFSZ+1]; - - va_start(args, format); - buf[BUFSZ] = 0x15; - (void) vsnprintf(buf, BUFSZ, format, args); - va_end(args); - if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack"); - if (WRITE(GC_stderr, buf, strlen(buf)) < 0) - ABORT("write to stderr failed"); + GC_PRINTF_IMPL(GC_stderr, "stderr", format); } void GC_log_printf(const char *format, ...) { - va_list args; - char buf[BUFSZ+1]; - - va_start(args, format); - buf[BUFSZ] = 0x15; - (void) vsnprintf(buf, BUFSZ, format, args); - va_end(args); - if (buf[BUFSZ] != 0x15) ABORT("GC_printf clobbered stack"); - if (WRITE(GC_log, buf, strlen(buf)) < 0) - ABORT("write to log failed"); + GC_PRINTF_IMPL(GC_log, "log", format); } /* This is equivalent to GC_err_printf("%s",s). */