Skip to content

Commit

Permalink
libc-builtin: Enhance buffered print for printf_wrapper (#3460)
Browse files Browse the repository at this point in the history
Move print_buf and print_buf to str_context struct to support multi-threading
when BUILTIN_LIBC_BUFFERED_PRINTF macro is enabled, and fix the issue of
losing some text.

ps. #3430.
  • Loading branch information
wenyongh committed May 22, 2024
1 parent c5ab862 commit f143344
Showing 1 changed file with 29 additions and 28 deletions.
57 changes: 29 additions & 28 deletions core/iwasm/libraries/libc-builtin/libc_builtin_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -321,10 +321,22 @@ _vprintf_wa(out_func_t out, void *ctx, const char *fmt, _va_list ap,
return false;
}

#ifndef BUILTIN_LIBC_BUFFERED_PRINTF
#define BUILTIN_LIBC_BUFFERED_PRINTF 0
#endif

#ifndef BUILTIN_LIBC_BUFFERED_PRINT_SIZE
#define BUILTIN_LIBC_BUFFERED_PRINT_SIZE 128
#endif

struct str_context {
char *str;
uint32 max;
uint32 count;
#if BUILTIN_LIBC_BUFFERED_PRINTF != 0
char print_buf[BUILTIN_LIBC_BUFFERED_PRINT_SIZE];
uint32 print_buf_size;
#endif
};

static int
Expand All @@ -345,41 +357,23 @@ sprintf_out(int c, struct str_context *ctx)
return c;
}

#ifndef BUILTIN_LIBC_BUFFERED_PRINTF
#define BUILTIN_LIBC_BUFFERED_PRINTF 0
#endif

#ifndef BUILTIN_LIBC_BUFFERED_PRINT_SIZE
#define BUILTIN_LIBC_BUFFERED_PRINT_SIZE 128
#endif
#ifndef BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
#define BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
#endif

#if BUILTIN_LIBC_BUFFERED_PRINTF != 0

BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
static char print_buf[BUILTIN_LIBC_BUFFERED_PRINT_SIZE] = { 0 };

BUILTIN_LIBC_BUFFERED_PRINT_PREFIX
static int print_buf_size = 0;

static int
printf_out(int c, struct str_context *ctx)
{
if (c == '\n') {
print_buf[print_buf_size] = '\0';
os_printf("%s\n", print_buf);
print_buf_size = 0;
ctx->print_buf[ctx->print_buf_size] = '\0';
os_printf("%s\n", ctx->print_buf);
ctx->print_buf_size = 0;
}
else if (print_buf_size >= sizeof(print_buf) - 2) {
print_buf[print_buf_size++] = (char)c;
print_buf[print_buf_size] = '\0';
os_printf("%s\n", print_buf);
print_buf_size = 0;
else if (ctx->print_buf_size >= sizeof(ctx->print_buf) - 2) {
ctx->print_buf[ctx->print_buf_size++] = (char)c;
ctx->print_buf[ctx->print_buf_size] = '\0';
os_printf("%s\n", ctx->print_buf);
ctx->print_buf_size = 0;
}
else {
print_buf[print_buf_size++] = (char)c;
ctx->print_buf[ctx->print_buf_size++] = (char)c;
}
ctx->count++;
return c;
Expand All @@ -398,7 +392,9 @@ static int
printf_wrapper(wasm_exec_env_t exec_env, const char *format, _va_list va_args)
{
wasm_module_inst_t module_inst = get_module_inst(exec_env);
struct str_context ctx = { NULL, 0, 0 };
struct str_context ctx = { 0 };

memset(&ctx, 0, sizeof(ctx));

/* format has been checked by runtime */
if (!validate_native_addr(va_args, (uint64)sizeof(int32)))
Expand All @@ -408,6 +404,11 @@ printf_wrapper(wasm_exec_env_t exec_env, const char *format, _va_list va_args)
module_inst))
return 0;

#if BUILTIN_LIBC_BUFFERED_PRINTF != 0
if (ctx.print_buf_size > 0)
os_printf("%s", ctx.print_buf);
#endif

return (int)ctx.count;
}

Expand Down

0 comments on commit f143344

Please sign in to comment.