-
Notifications
You must be signed in to change notification settings - Fork 2.1k
pkg/mplaland-printf: Use buffered output #21805
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
maribu
merged 1 commit into
RIOT-OS:master
from
maribu:pkg/mpaland-printf/buffered-output
Oct 24, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
131 changes: 131 additions & 0 deletions
131
pkg/mpaland-printf/patches/0006-use-buffered-output.patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| From 1070ce82390a7ca9bdaef52f106488ac7ef99645 Mon Sep 17 00:00:00 2001 | ||
| From: Marian Buschsieweke <marian.buschsieweke@posteo.net> | ||
| Date: Sun, 19 Oct 2025 16:58:11 +0200 | ||
| Subject: [PATCH] use buffered output | ||
|
|
||
| --- | ||
| printf.c | 63 +++++++++++++++++++++++++++++++++++++++++--------------- | ||
| 1 file changed, 46 insertions(+), 17 deletions(-) | ||
|
|
||
| diff --git a/printf.c b/printf.c | ||
| index 33fd8a6..756fd12 100644 | ||
| --- a/printf.c | ||
| +++ b/printf.c | ||
| @@ -133,6 +133,27 @@ typedef struct { | ||
| } out_fct_wrap_type; | ||
|
|
||
|
|
||
| +typedef struct { | ||
| + char buf[64]; | ||
| + uint8_t fill; | ||
| +} output_buffer_type; | ||
| + | ||
| + | ||
| +/* RIOT integration: Use stdio_write for output and loop until all output is | ||
| + * flushed. */ | ||
| +static void write_all(const char *buf, size_t len) | ||
| +{ | ||
| + const char *end = buf + len; | ||
| + while (buf < end) { | ||
| + int res = stdio_write(buf, (size_t)end-(size_t)buf); | ||
| + if (res < 0) { | ||
| + return; | ||
| + } | ||
| + buf += res; | ||
| + } | ||
| +} | ||
| + | ||
| + | ||
| // internal buffer output | ||
| static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen) | ||
| { | ||
| @@ -150,15 +171,19 @@ static inline void _out_null(char character, void* buffer, size_t idx, size_t ma | ||
|
|
||
|
|
||
| // internal _putchar wrapper | ||
| -static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen) | ||
| +static inline void _out_char(char character, void* _buffer, size_t idx, size_t maxlen) | ||
| { | ||
| - (void)buffer; (void)idx; (void)maxlen; | ||
| - if (character) { | ||
| - _putchar(character); | ||
| - } | ||
| -} | ||
| - | ||
| - | ||
| + (void)idx; (void)maxlen; | ||
| + output_buffer_type *buffer = _buffer; | ||
| + if (buffer->fill == sizeof(buffer->buf)) { | ||
| + write_all(buffer->buf, sizeof(buffer->buf)); | ||
| + buffer->fill = 0; | ||
| + } | ||
| + if (character) { | ||
| + buffer->buf[buffer->fill++] = character; | ||
| + } | ||
| +} | ||
| + | ||
| // internal output function wrapper | ||
| static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen) | ||
| { | ||
| @@ -578,7 +603,7 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d | ||
|
|
||
|
|
||
| // internal vsnprintf | ||
| -static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va) | ||
| +static int _vsnprintf(out_fct_type out, void* buffer, const size_t maxlen, const char* format, va_list va) | ||
| { | ||
| unsigned int flags, width, precision, n; | ||
| size_t idx = 0U; | ||
| @@ -873,8 +898,10 @@ int printf_(const char* format, ...) | ||
| { | ||
| va_list va; | ||
| va_start(va, format); | ||
| - char buffer[1]; | ||
| - const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va); | ||
| + output_buffer_type buffer; | ||
| + buffer.fill = 0; | ||
| + const int ret = _vsnprintf(_out_char, &buffer, (size_t)-1, format, va); | ||
| + write_all(buffer.buf, buffer.fill); | ||
| va_end(va); | ||
| return ret; | ||
| } | ||
| @@ -902,8 +929,11 @@ int snprintf_(char* buffer, size_t count, const char* format, ...) | ||
|
|
||
| int vprintf_(const char* format, va_list va) | ||
| { | ||
| - char buffer[1]; | ||
| - return _vsnprintf(_out_char, buffer, (size_t)-1, format, va); | ||
| + output_buffer_type buffer; | ||
| + buffer.fill = 0; | ||
| + int result = _vsnprintf(_out_char, &buffer, (size_t)-1, format, va); | ||
| + write_all(buffer.buf, buffer.fill); | ||
| + return result; | ||
| } | ||
|
|
||
|
|
||
| @@ -924,10 +954,9 @@ int fctprintf(void (*out)(char character, void* arg), void* arg, const char* for | ||
| } | ||
|
|
||
|
|
||
| -/* RIOT integration: Use stdio_write for output */ | ||
| static void _putchar(char character) | ||
| { | ||
| - stdio_write(&character, sizeof(character)); | ||
| + write_all(&character, sizeof(character)); | ||
| } | ||
|
|
||
|
|
||
| @@ -962,8 +991,8 @@ int __wrap_putchar(int c) | ||
| int __wrap_puts(const char *s) | ||
| { | ||
| size_t len = strlen(s); | ||
| - stdio_write(s, len); | ||
| - stdio_write("\n", 1); | ||
| + write_all(s, len); | ||
| + write_all("\n", 1); | ||
| return len + 1; | ||
| } | ||
|
|
||
| -- | ||
| 2.43.0 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.