This is an automated email from the ASF dual-hosted git repository.

acassis pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit f9411a225e6e9763a7dc0908ea3084c5dfaa72bb
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 5 12:06:49 2026 +0100

    libs/libc/stdio: Let vdprintf() work without a buffer.
    
    With STREAM_OUT_BUFFER_SIZE at 0, vdprintf() writes through the raw stream,
    and lib_bufferedoutstream.c is not built.  This removes the code and the
    buffer on the stack, and each write of printf() becomes one write().
    
    The size is 0 by default in the Minimal profile only.
    
    Part of https://github.com/apache/nuttx/issues/21467
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/streams.h         | 4 ++++
 libs/libc/stdio/lib_vdprintf.c  | 6 ++++++
 libs/libc/stream/CMakeLists.txt | 5 ++++-
 libs/libc/stream/Kconfig        | 5 +++++
 libs/libc/stream/Make.defs      | 6 +++++-
 5 files changed, 24 insertions(+), 2 deletions(-)

diff --git a/include/nuttx/streams.h b/include/nuttx/streams.h
index 1ee192af17a..f5bb3b9df2e 100644
--- a/include/nuttx/streams.h
+++ b/include/nuttx/streams.h
@@ -235,6 +235,7 @@ struct lib_rawsostream_s
   int                    fd;
 };
 
+#if CONFIG_STREAM_OUT_BUFFER_SIZE > 0
 struct lib_bufferedoutstream_s
 {
   struct lib_outstream_s      common;
@@ -242,6 +243,7 @@ struct lib_bufferedoutstream_s
   size_t                      pending;
   char                        buffer[CONFIG_STREAM_OUT_BUFFER_SIZE];
 };
+#endif
 
 struct lib_hexdumpstream_s
 {
@@ -477,8 +479,10 @@ void lib_rawsostream(FAR struct lib_rawsostream_s *stream, 
int fd);
  *
  ****************************************************************************/
 
+#if CONFIG_STREAM_OUT_BUFFER_SIZE > 0
 void lib_bufferedoutstream(FAR struct lib_bufferedoutstream_s *stream,
                            FAR struct lib_outstream_s *backend);
+#endif
 
 /****************************************************************************
  * Name: lib_hexdumpstream
diff --git a/libs/libc/stdio/lib_vdprintf.c b/libs/libc/stdio/lib_vdprintf.c
index d7c7349eb43..f79953ec33b 100644
--- a/libs/libc/stdio/lib_vdprintf.c
+++ b/libs/libc/stdio/lib_vdprintf.c
@@ -49,13 +49,19 @@ int vdprintf(int fd, FAR const IPTR char *fmt, va_list ap)
 {
   int ret;
   struct lib_rawoutstream_s rawoutstream;
+#if CONFIG_STREAM_OUT_BUFFER_SIZE > 0
   struct lib_bufferedoutstream_s outstream;
+#endif
 
   /* Wrap the fd in a stream object and let lib_vsprintf do the work. */
 
   lib_rawoutstream(&rawoutstream, fd);
+#if CONFIG_STREAM_OUT_BUFFER_SIZE > 0
   lib_bufferedoutstream(&outstream, &rawoutstream.common);
   ret = lib_vsprintf(&outstream.common, fmt, ap);
   lib_stream_flush(&outstream.common);
+#else
+  ret = lib_vsprintf(&rawoutstream.common, fmt, ap);
+#endif
   return ret;
 }
diff --git a/libs/libc/stream/CMakeLists.txt b/libs/libc/stream/CMakeLists.txt
index 0fc7478834f..5ac8d833995 100644
--- a/libs/libc/stream/CMakeLists.txt
+++ b/libs/libc/stream/CMakeLists.txt
@@ -40,7 +40,6 @@ list(
   lib_libsnoflush.c
   lib_syslogstream.c
   lib_syslograwstream.c
-  lib_bufferedoutstream.c
   lib_hexdumpstream.c
   lib_base64outstream.c
   lib_fileinstream.c
@@ -50,6 +49,10 @@ list(
   lib_libvsprintf.c
   lib_ultoa_invert.c)
 
+if(NOT CONFIG_STREAM_OUT_BUFFER_SIZE EQUAL 0)
+  list(APPEND SRCS lib_bufferedoutstream.c)
+endif()
+
 if(CONFIG_LIBC_FLOATINGPOINT)
   list(APPEND SRCS lib_dtoa_engine.c lib_dtoa_data.c)
 endif()
diff --git a/libs/libc/stream/Kconfig b/libs/libc/stream/Kconfig
index a0792475f6a..32bf9b0c713 100644
--- a/libs/libc/stream/Kconfig
+++ b/libs/libc/stream/Kconfig
@@ -23,7 +23,12 @@ endif
 
 config STREAM_OUT_BUFFER_SIZE
        int "Output stream buffer size"
+       default 0 if LIBC_PRINTF_MINIMAL
        default 64
+       ---help---
+               The size of the buffer that vdprintf() and dprintf() keep on the
+               stack.  With 0 they have no buffer: each write of printf() is 
one
+               write() to the file descriptor.
 
 config STREAM_HEXDUMP_BUFFER_SIZE
        int "Output hexdump stream buffer size"
diff --git a/libs/libc/stream/Make.defs b/libs/libc/stream/Make.defs
index 43166d52c8c..3f821272c08 100644
--- a/libs/libc/stream/Make.defs
+++ b/libs/libc/stream/Make.defs
@@ -28,11 +28,15 @@ CSRCS += lib_memsostream.c lib_lowoutstream.c 
lib_rawinstream.c
 CSRCS += lib_rawoutstream.c lib_rawsistream.c lib_rawsostream.c
 CSRCS += lib_zeroinstream.c lib_nullinstream.c lib_nulloutstream.c
 CSRCS += lib_mtdoutstream.c lib_libnoflush.c lib_libsnoflush.c
-CSRCS += lib_syslogstream.c lib_syslograwstream.c lib_bufferedoutstream.c
+CSRCS += lib_syslogstream.c lib_syslograwstream.c
 CSRCS += lib_hexdumpstream.c lib_base64outstream.c lib_mtdsostream.c
 CSRCS += lib_fileinstream.c lib_fileoutstream.c lib_libbsprintf.c
 CSRCS += lib_libvscanf.c lib_libvsprintf.c lib_ultoa_invert.c
 
+ifneq ($(CONFIG_STREAM_OUT_BUFFER_SIZE),0)
+CSRCS += lib_bufferedoutstream.c
+endif
+
 ifeq ($(CONFIG_LIBC_FLOATINGPOINT),y)
 CSRCS += lib_dtoa_engine.c lib_dtoa_data.c
 endif

Reply via email to