@@ -321,10 +321,22 @@ _vprintf_wa(out_func_t out, void *ctx, const char *fmt, _va_list ap,
321321 return false;
322322}
323323
324+ #ifndef BUILTIN_LIBC_BUFFERED_PRINTF
325+ #define BUILTIN_LIBC_BUFFERED_PRINTF 0
326+ #endif
327+
328+ #ifndef BUILTIN_LIBC_BUFFERED_PRINT_SIZE
329+ #define BUILTIN_LIBC_BUFFERED_PRINT_SIZE 128
330+ #endif
331+
324332struct str_context {
325333 char * str ;
326334 uint32 max ;
327335 uint32 count ;
336+ #if BUILTIN_LIBC_BUFFERED_PRINTF != 0
337+ char print_buf [BUILTIN_LIBC_BUFFERED_PRINT_SIZE ];
338+ uint32 print_buf_size ;
339+ #endif
328340};
329341
330342static int
@@ -345,41 +357,23 @@ sprintf_out(int c, struct str_context *ctx)
345357 return c ;
346358}
347359
348- #ifndef BUILTIN_LIBC_BUFFERED_PRINTF
349- #define BUILTIN_LIBC_BUFFERED_PRINTF 0
350- #endif
351-
352- #ifndef BUILTIN_LIBC_BUFFERED_PRINT_SIZE
353- #define BUILTIN_LIBC_BUFFERED_PRINT_SIZE 128
354- #endif
355- #ifndef BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
356- #define BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
357- #endif
358-
359360#if BUILTIN_LIBC_BUFFERED_PRINTF != 0
360-
361- BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
362- static char print_buf [BUILTIN_LIBC_BUFFERED_PRINT_SIZE ] = { 0 };
363-
364- BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
365- static int print_buf_size = 0 ;
366-
367361static int
368362printf_out (int c , struct str_context * ctx )
369363{
370364 if (c == '\n' ) {
371- print_buf [print_buf_size ] = '\0' ;
372- os_printf ("%s\n" , print_buf );
373- print_buf_size = 0 ;
365+ ctx -> print_buf [ctx -> print_buf_size ] = '\0' ;
366+ os_printf ("%s\n" , ctx -> print_buf );
367+ ctx -> print_buf_size = 0 ;
374368 }
375- else if (print_buf_size >= sizeof (print_buf ) - 2 ) {
376- print_buf [print_buf_size ++ ] = (char )c ;
377- print_buf [print_buf_size ] = '\0' ;
378- os_printf ("%s\n" , print_buf );
379- print_buf_size = 0 ;
369+ else if (ctx -> print_buf_size >= sizeof (ctx -> print_buf ) - 2 ) {
370+ ctx -> print_buf [ctx -> print_buf_size ++ ] = (char )c ;
371+ ctx -> print_buf [ctx -> print_buf_size ] = '\0' ;
372+ os_printf ("%s\n" , ctx -> print_buf );
373+ ctx -> print_buf_size = 0 ;
380374 }
381375 else {
382- print_buf [print_buf_size ++ ] = (char )c ;
376+ ctx -> print_buf [ctx -> print_buf_size ++ ] = (char )c ;
383377 }
384378 ctx -> count ++ ;
385379 return c ;
@@ -398,7 +392,9 @@ static int
398392printf_wrapper (wasm_exec_env_t exec_env , const char * format , _va_list va_args )
399393{
400394 wasm_module_inst_t module_inst = get_module_inst (exec_env );
401- struct str_context ctx = { NULL , 0 , 0 };
395+ struct str_context ctx = { 0 };
396+
397+ memset (& ctx , 0 , sizeof (ctx ));
402398
403399 /* format has been checked by runtime */
404400 if (!validate_native_addr (va_args , (uint64 )sizeof (int32 )))
@@ -408,6 +404,11 @@ printf_wrapper(wasm_exec_env_t exec_env, const char *format, _va_list va_args)
408404 module_inst ))
409405 return 0 ;
410406
407+ #if BUILTIN_LIBC_BUFFERED_PRINTF != 0
408+ if (ctx .print_buf_size > 0 )
409+ os_printf ("%s" , ctx .print_buf );
410+ #endif
411+
411412 return (int )ctx .count ;
412413}
413414
0 commit comments