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

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

commit 72de1535d5bf5edd5e30651ce4409472fbf90910
Author: zhangshoukui <[email protected]>
AuthorDate: Thu Dec 12 17:10:01 2024 +0800

    nxplayer/nxrecorder/nxlooper: allocate the command line from temp pool
    
    The three command line applications keep a LINE_MAX byte command line
    buffer on the stack of main() for their whole lifetime.  Take it from the
    temporary buffer pool instead and release it before returning, which
    removes that much stack from every instance of the applications.
    
    readline_stream() is passed the buffer size explicitly now that the
    buffer is no longer an array.
    
    Signed-off-by: zhangshoukui <[email protected]>
---
 system/nxlooper/nxlooper_main.c     | 14 ++++++++++++--
 system/nxplayer/nxplayer_main.c     | 14 ++++++++++++--
 system/nxrecorder/nxrecorder_main.c | 14 ++++++++++++--
 3 files changed, 36 insertions(+), 6 deletions(-)

diff --git a/system/nxlooper/nxlooper_main.c b/system/nxlooper/nxlooper_main.c
index e90ed5784..5c23eabf3 100644
--- a/system/nxlooper/nxlooper_main.c
+++ b/system/nxlooper/nxlooper_main.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 #include <nuttx/audio/audio.h>
+#include <nuttx/lib/lib.h>
 
 #include <sys/types.h>
 #include <stdio.h>
@@ -502,7 +503,7 @@ static int nxlooper_cmd_help(FAR struct nxlooper_s 
*plooper, char *parg)
 
 int main(int argc, FAR char *argv[])
 {
-  char                  buffer[LINE_MAX];
+  FAR char             *buffer;
   int                   len;
   int                   x;
   int                   running;
@@ -523,6 +524,14 @@ int main(int argc, FAR char *argv[])
       return -ENOMEM;
     }
 
+  buffer = lib_get_tempbuffer(LINE_MAX);
+  if (buffer == NULL)
+    {
+      nxlooper_release(plooper);
+      printf("Error: lib_get_tempbuffer failed\n");
+      return -ENOMEM;
+    }
+
   /* Loop until the user exits */
 
   running = TRUE;
@@ -535,7 +544,7 @@ int main(int argc, FAR char *argv[])
 
       /* Read a line from the terminal */
 
-      len = readline_stream(buffer, sizeof(buffer),
+      len = readline_stream(buffer, LINE_MAX,
                             stdin, stdout);
       if (len > 0)
         {
@@ -597,6 +606,7 @@ int main(int argc, FAR char *argv[])
   /* Release the NxLooper context */
 
   nxlooper_release(plooper);
+  lib_put_tempbuffer(buffer);
 
   return OK;
 }
diff --git a/system/nxplayer/nxplayer_main.c b/system/nxplayer/nxplayer_main.c
index 58c5be95a..c46946ad4 100644
--- a/system/nxplayer/nxplayer_main.c
+++ b/system/nxplayer/nxplayer_main.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 #include <nuttx/audio/audio.h>
+#include <nuttx/lib/lib.h>
 
 #include <inttypes.h>
 #include <sys/types.h>
@@ -802,7 +803,7 @@ static int nxplayer_cmd_help(FAR struct nxplayer_s 
*pplayer, char *parg)
 
 int main(int argc, FAR char *argv[])
 {
-  char                    buffer[LINE_MAX];
+  FAR char               *buffer;
   int                     len;
   int                     x;
   int                     running;
@@ -823,6 +824,14 @@ int main(int argc, FAR char *argv[])
       return -ENOMEM;
     }
 
+  buffer = lib_get_tempbuffer(LINE_MAX);
+  if (buffer == NULL)
+    {
+      nxplayer_release(pplayer);
+      printf("Error: lib_get_tempbuffer failed\n");
+      return -ENOMEM;
+    }
+
   /* Loop until the user exits */
 
   running = TRUE;
@@ -835,7 +844,7 @@ int main(int argc, FAR char *argv[])
 
       /* Read a line from the terminal */
 
-      len = readline_stream(buffer, sizeof(buffer),
+      len = readline_stream(buffer, LINE_MAX,
                             stdin, stdout);
       if (len > 0)
         {
@@ -904,6 +913,7 @@ int main(int argc, FAR char *argv[])
   /* Release the NxPlayer context */
 
   nxplayer_release(pplayer);
+  lib_put_tempbuffer(buffer);
 
   return OK;
 }
diff --git a/system/nxrecorder/nxrecorder_main.c 
b/system/nxrecorder/nxrecorder_main.c
index 485784a03..ac8682e3c 100644
--- a/system/nxrecorder/nxrecorder_main.c
+++ b/system/nxrecorder/nxrecorder_main.c
@@ -26,6 +26,7 @@
 
 #include <nuttx/config.h>
 #include <nuttx/audio/audio.h>
+#include <nuttx/lib/lib.h>
 
 #include <sys/types.h>
 #include <stdio.h>
@@ -553,7 +554,7 @@ static int nxrecorder_cmd_help(FAR struct nxrecorder_s 
*precorder,
 
 int main(int argc, FAR char *argv[])
 {
-  char                    buffer[LINE_MAX];
+  FAR char               *buffer;
   int                     len;
   int                     x;
   int                     running;
@@ -574,6 +575,14 @@ int main(int argc, FAR char *argv[])
       return -ENOMEM;
     }
 
+  buffer = lib_get_tempbuffer(LINE_MAX);
+  if (buffer == NULL)
+    {
+      nxrecorder_release(precorder);
+      printf("Error: lib_get_tempbuffer failed\n");
+      return -ENOMEM;
+    }
+
   /* Loop until the user exits */
 
   running = TRUE;
@@ -586,7 +595,7 @@ int main(int argc, FAR char *argv[])
 
       /* Read a line from the terminal */
 
-      len = readline_stream(buffer, sizeof(buffer),
+      len = readline_stream(buffer, LINE_MAX,
                             stdin, stdout);
       if (len > 0)
         {
@@ -655,6 +664,7 @@ int main(int argc, FAR char *argv[])
   /* Release the NxRecorder context */
 
   nxrecorder_release(precorder);
+  lib_put_tempbuffer(buffer);
 
   return OK;
 }

Reply via email to