Skip to content

Commit 30a4647

Browse files
committed
pkg/mplaland-printf: Use buffered output
With this patch a small (64 byte) on-stack is buffer is used. On stdio transports with extremely high overhead per call of `stdio_write()` (like e.g. telnet or semihosting), this will make the stdio usable again. Compared to e.g. the stdio from newlib, the increased stack requirements are still modest. In addition, the RIOT integration now also checks the return value of `stdio_write()` and loops until all output has been written, fixing loss of output if a transport does not write all data in one go.
1 parent 89cd3db commit 30a4647

File tree

1 file changed

+131
-0
lines changed

1 file changed

+131
-0
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
From 1070ce82390a7ca9bdaef52f106488ac7ef99645 Mon Sep 17 00:00:00 2001
2+
From: Marian Buschsieweke <[email protected]>
3+
Date: Sun, 19 Oct 2025 16:58:11 +0200
4+
Subject: [PATCH] use buffered output
5+
6+
---
7+
printf.c | 63 +++++++++++++++++++++++++++++++++++++++++---------------
8+
1 file changed, 46 insertions(+), 17 deletions(-)
9+
10+
diff --git a/printf.c b/printf.c
11+
index 33fd8a6..756fd12 100644
12+
--- a/printf.c
13+
+++ b/printf.c
14+
@@ -133,6 +133,27 @@ typedef struct {
15+
} out_fct_wrap_type;
16+
17+
18+
+typedef struct {
19+
+ char buf[64];
20+
+ uint8_t fill;
21+
+} output_buffer_type;
22+
+
23+
+
24+
+/* RIOT integration: Use stdio_write for output and loop until all output is
25+
+ * flushed. */
26+
+static void write_all(const char *buf, size_t len)
27+
+{
28+
+ const char *end = buf + len;
29+
+ while (buf < end) {
30+
+ int res = stdio_write(buf, (size_t)end-(size_t)buf);
31+
+ if (res < 0) {
32+
+ return;
33+
+ }
34+
+ buf += res;
35+
+ }
36+
+}
37+
+
38+
+
39+
// internal buffer output
40+
static inline void _out_buffer(char character, void* buffer, size_t idx, size_t maxlen)
41+
{
42+
@@ -150,15 +171,19 @@ static inline void _out_null(char character, void* buffer, size_t idx, size_t ma
43+
44+
45+
// internal _putchar wrapper
46+
-static inline void _out_char(char character, void* buffer, size_t idx, size_t maxlen)
47+
+static inline void _out_char(char character, void* _buffer, size_t idx, size_t maxlen)
48+
{
49+
- (void)buffer; (void)idx; (void)maxlen;
50+
- if (character) {
51+
- _putchar(character);
52+
- }
53+
-}
54+
-
55+
-
56+
+ (void)idx; (void)maxlen;
57+
+ output_buffer_type *buffer = _buffer;
58+
+ if (buffer->fill == sizeof(buffer->buf)) {
59+
+ write_all(buffer->buf, sizeof(buffer->buf));
60+
+ buffer->fill = 0;
61+
+ }
62+
+ if (character) {
63+
+ buffer->buf[buffer->fill++] = character;
64+
+ }
65+
+}
66+
+
67+
// internal output function wrapper
68+
static inline void _out_fct(char character, void* buffer, size_t idx, size_t maxlen)
69+
{
70+
@@ -578,7 +603,7 @@ static size_t _etoa(out_fct_type out, char* buffer, size_t idx, size_t maxlen, d
71+
72+
73+
// internal vsnprintf
74+
-static int _vsnprintf(out_fct_type out, char* buffer, const size_t maxlen, const char* format, va_list va)
75+
+static int _vsnprintf(out_fct_type out, void* buffer, const size_t maxlen, const char* format, va_list va)
76+
{
77+
unsigned int flags, width, precision, n;
78+
size_t idx = 0U;
79+
@@ -873,8 +898,10 @@ int printf_(const char* format, ...)
80+
{
81+
va_list va;
82+
va_start(va, format);
83+
- char buffer[1];
84+
- const int ret = _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
85+
+ output_buffer_type buffer;
86+
+ buffer.fill = 0;
87+
+ const int ret = _vsnprintf(_out_char, &buffer, (size_t)-1, format, va);
88+
+ write_all(buffer.buf, buffer.fill);
89+
va_end(va);
90+
return ret;
91+
}
92+
@@ -902,8 +929,11 @@ int snprintf_(char* buffer, size_t count, const char* format, ...)
93+
94+
int vprintf_(const char* format, va_list va)
95+
{
96+
- char buffer[1];
97+
- return _vsnprintf(_out_char, buffer, (size_t)-1, format, va);
98+
+ output_buffer_type buffer;
99+
+ buffer.fill = 0;
100+
+ int result = _vsnprintf(_out_char, &buffer, (size_t)-1, format, va);
101+
+ write_all(buffer.buf, buffer.fill);
102+
+ return result;
103+
}
104+
105+
106+
@@ -924,10 +954,9 @@ int fctprintf(void (*out)(char character, void* arg), void* arg, const char* for
107+
}
108+
109+
110+
-/* RIOT integration: Use stdio_write for output */
111+
static void _putchar(char character)
112+
{
113+
- stdio_write(&character, sizeof(character));
114+
+ write_all(&character, sizeof(character));
115+
}
116+
117+
118+
@@ -962,8 +991,8 @@ int __wrap_putchar(int c)
119+
int __wrap_puts(const char *s)
120+
{
121+
size_t len = strlen(s);
122+
- stdio_write(s, len);
123+
- stdio_write("\n", 1);
124+
+ write_all(s, len);
125+
+ write_all("\n", 1);
126+
return len + 1;
127+
}
128+
129+
--
130+
2.43.0
131+

0 commit comments

Comments
 (0)