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

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

commit 93865442d23de5ea03462c37a26dd13ca24696cc
Author: buxiasen <[email protected]>
AuthorDate: Mon Feb 2 18:26:39 2026 +0800

    libc/misc/circbuf: add circbuf_reinit handle malloc/free not direct use.
    
    In case the buffer is special or lock confict with memory manager,
    should isolate the malloc/free API, only configure & copy.
    
    Signed-off-by: buxiasen <[email protected]>
---
 include/nuttx/circbuf.h      | 24 ++++++++++++++++
 libs/libc/misc/lib_circbuf.c | 66 ++++++++++++++++++++++++++++++++------------
 2 files changed, 73 insertions(+), 17 deletions(-)

diff --git a/include/nuttx/circbuf.h b/include/nuttx/circbuf.h
index 691ab4530c2..9d9de1bd67f 100644
--- a/include/nuttx/circbuf.h
+++ b/include/nuttx/circbuf.h
@@ -89,6 +89,30 @@ extern "C"
 int circbuf_init(FAR struct circbuf_s *circ,
                   FAR void *base, size_t bytes);
 
+/****************************************************************************
+ * Name: circbuf_reinit
+ *
+ * Description:
+ *   Reinitialize a circular buffer with a new buffer, and new size.
+ *   In case malloc & free is not reliable here, we handle buffer outside.
+ *   Will copy the old data to new buffer.
+ *
+ * Input Parameters:
+ *   circ  - Address of the circular buffer to be used.
+ *   base  - A pointer to circular buffer's internal buffer. It can be
+ *           provided by caller because sometimes the creation of buffer
+ *           is special or needs to preallocated, eg: DMA buffer.
+ *           If NULL, a buffer of the given size will be allocated.
+ *   bytes - The size of the internal buffer.
+ *
+ * Returned Value:
+ *   The previous internal buffer pointer is returned.
+ *
+ ****************************************************************************/
+
+FAR void *circbuf_reinit(FAR struct circbuf_s *circ, FAR void *base,
+                         size_t bytes);
+
 /****************************************************************************
  * Name: circbuf_resize
  *
diff --git a/libs/libc/misc/lib_circbuf.c b/libs/libc/misc/lib_circbuf.c
index faeebc83ef4..c575ac37066 100644
--- a/libs/libc/misc/lib_circbuf.c
+++ b/libs/libc/misc/lib_circbuf.c
@@ -89,6 +89,54 @@ int circbuf_init(FAR struct circbuf_s *circ, FAR void *base, 
size_t bytes)
   return 0;
 }
 
+/****************************************************************************
+ * Name: circbuf_reinit
+ *
+ * Description:
+ *   Reinitialize a circular buffer with a new buffer, and new size.
+ *   In case malloc & free is not reliable here, we handle buffer outside.
+ *   Will copy the old data to new buffer.
+ *
+ * Input Parameters:
+ *   circ  - Address of the circular buffer to be used.
+ *   base  - A pointer to circular buffer's internal buffer. It can be
+ *           provided by caller because sometimes the creation of buffer
+ *           is special or needs to preallocated, eg: DMA buffer.
+ *           If NULL, a buffer of the given size will be allocated.
+ *   bytes - The size of the internal buffer.
+ *
+ * Returned Value:
+ *   The previous internal buffer pointer is returned.
+ *
+ ****************************************************************************/
+
+FAR void *circbuf_reinit(FAR struct circbuf_s *circ, FAR void *base,
+                         size_t bytes)
+{
+  size_t len = 0;
+  FAR void *old;
+
+  if (bytes && base)
+    {
+      len = circbuf_used(circ);
+      if (bytes < len)
+        {
+          circbuf_skip(circ, len - bytes);
+          len = bytes;
+        }
+
+      circbuf_read(circ, base, len);
+    }
+
+  old = circ->base;
+  circ->base = base;
+  circ->size = bytes;
+  circ->head = len;
+  circ->tail = 0;
+
+  return old;
+}
+
 /****************************************************************************
  * Name: circbuf_resize
  *
@@ -107,7 +155,6 @@ int circbuf_init(FAR struct circbuf_s *circ, FAR void 
*base, size_t bytes)
 int circbuf_resize(FAR struct circbuf_s *circ, size_t bytes)
 {
   FAR void *tmp = NULL;
-  size_t len = 0;
 
   DEBUGASSERT(circ);
   DEBUGASSERT(!circ->external);
@@ -123,24 +170,9 @@ int circbuf_resize(FAR struct circbuf_s *circ, size_t 
bytes)
         {
           return -ENOMEM;
         }
-
-      len = circbuf_used(circ);
-      if (bytes < len)
-        {
-          circbuf_skip(circ, len - bytes);
-          len = bytes;
-        }
-
-      circbuf_read(circ, tmp, len);
     }
 
-  lib_free(circ->base);
-
-  circ->base = tmp;
-  circ->size = bytes;
-  circ->head = len;
-  circ->tail = 0;
-
+  lib_free(circbuf_reinit(circ, tmp, bytes));
   return 0;
 }
 

Reply via email to