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 bc371c666966be900248dc4ed172d83c969e2ecc
Author: wangshaoxin <[email protected]>
AuthorDate: Wed Jul 29 10:16:56 2026 +0800

    drivers/rpmsg: take ns-bind records from the rpmsg pool when possible
    
    A name service announcement that no registered callback matches is kept
    in a struct rpmsg_bind_s, allocated with kmm_malloc(), until its service
    registers. Take the record from the rpmsg pool instead when the pool is
    enabled (RPMSG_POOL_COUNT > 0) and its blocks are large enough, so these
    records do not need the heap.
    
    The choice is made at compile time, so a record is always freed to the
    allocator it came from. With RPMSG_POOL_COUNT == 0, rpmsg_pool_alloc() is
    alloca(), which cannot hold a list node beyond the caller, so the heap is
    used then.
    
    Signed-off-by: wangshaoxin <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 drivers/rpmsg/rpmsg.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++----
 1 file changed, 52 insertions(+), 4 deletions(-)

diff --git a/drivers/rpmsg/rpmsg.c b/drivers/rpmsg/rpmsg.c
index 75d0db384dc..e8d9cb07c03 100644
--- a/drivers/rpmsg/rpmsg.c
+++ b/drivers/rpmsg/rpmsg.c
@@ -322,6 +322,54 @@ static int rpmsg_create_wqueues(FAR struct rpmsg_wqueue_s 
*wqueues,
   return ret;
 }
 
+/****************************************************************************
+ * Name: rpmsg_bind_alloc / rpmsg_bind_free
+ *
+ * Description:
+ *   Allocate/free a name-service bind record.  When the rpmsg pool is
+ *   enabled (RPMSG_POOL_COUNT > 0) and its block size is large enough to
+ *   hold a struct rpmsg_bind_s, the record is taken from the rpmsg pool to
+ *   avoid heap allocation.  Otherwise it falls back to the heap.
+ *
+ *   The choice is a compile-time constant, so alloc and free always use the
+ *   same allocator within a build.  When RPMSG_POOL_COUNT <= 0 the rpmsg
+ *   pool degenerates into alloca() and must not be used for these long-lived
+ *   list nodes, so the heap is used unconditionally.
+ *
+ ****************************************************************************/
+
+static inline_function FAR void *rpmsg_bind_alloc(void)
+{
+  FAR void *bind;
+
+#if CONFIG_RPMSG_POOL_COUNT > 0
+  if (CONFIG_RPMSG_POOL_SIZE >= sizeof(struct rpmsg_bind_s))
+    {
+      bind = rpmsg_pool_alloc(sizeof(struct rpmsg_bind_s));
+    }
+  else
+#endif
+    {
+      bind = kmm_malloc(sizeof(struct rpmsg_bind_s));
+    }
+
+  return bind;
+}
+
+static inline_function void rpmsg_bind_free(FAR void *bind)
+{
+#if CONFIG_RPMSG_POOL_COUNT > 0
+  if (CONFIG_RPMSG_POOL_SIZE >= sizeof(struct rpmsg_bind_s))
+    {
+      rpmsg_pool_free(bind);
+    }
+  else
+#endif
+    {
+      kmm_free(bind);
+    }
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -589,7 +637,7 @@ int rpmsg_register_callback(FAR void *priv,
                       nxrmutex_unlock(&rpmsg->lock);
                       ns_bind(rdev, priv, bind->name, bind->dest);
 
-                      kmm_free(bind);
+                      rpmsg_bind_free(bind);
                       again = true;
 
                       nxrmutex_lock(&rpmsg->lock);
@@ -675,7 +723,7 @@ void rpmsg_ns_bind(FAR struct rpmsg_device *rdev,
 
   if (!matched)
     {
-      bind = kmm_malloc(sizeof(struct rpmsg_bind_s));
+      bind = rpmsg_bind_alloc();
       if (bind != NULL)
         {
           bind->dest = dest;
@@ -702,7 +750,7 @@ void rpmsg_ns_unbind(FAR struct rpmsg_device *rdev,
       if (bind->dest == dest && !strncmp(bind->name, name, RPMSG_NAME_SIZE))
         {
           list_delete(&bind->node);
-          kmm_free(bind);
+          rpmsg_bind_free(bind);
           break;
         }
     }
@@ -774,7 +822,7 @@ void rpmsg_device_destroy(FAR struct rpmsg_s *rpmsg)
                             struct rpmsg_bind_s, node)
     {
       list_delete(&bind->node);
-      kmm_free(bind);
+      rpmsg_bind_free(bind);
     }
 
   nxrmutex_unlock(&rpmsg->lock);

Reply via email to