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.git

commit fd914b19db1a404dd9425caaf490ca0345d06455
Author: zhanghongyu <[email protected]>
AuthorDate: Thu Nov 20 20:45:54 2025 +0800

    net/bluetooth, net/ieee802154, net/usrsock: Use MEMPOOL_DEFINE for 
connections.
    
    Replace the NET_BUFPOOL wrapper with the generic mempool for the
    bluetooth, ieee802154 and usrsock connection pools, so that the network
    code uses one allocator instead of its own layer on top of it.
    
    NET_BUFPOOL_LOCK() protected both the pool and the active connection
    list.  mempool has its own lock, so the list is now protected by the
    existing per-protocol lock instead.
    
    NET_BUFPOOL_TRYALLOC() returned zeroed memory only for blocks it had
    just grown with kmm_zalloc(), and each *_alloc() sets only part of the
    connection.  Clear the block explicitly after mempool_allocate() so a
    recycled connection does not start with stale fields.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/bluetooth/bluetooth_conn.c   | 15 +++++++++------
 net/ieee802154/ieee802154_conn.c | 16 +++++++++-------
 net/usrsock/usrsock_conn.c       | 22 ++++++++++++----------
 3 files changed, 30 insertions(+), 23 deletions(-)

diff --git a/net/bluetooth/bluetooth_conn.c b/net/bluetooth/bluetooth_conn.c
index 08d943a4b10..bc2c914e7ec 100644
--- a/net/bluetooth/bluetooth_conn.c
+++ b/net/bluetooth/bluetooth_conn.c
@@ -36,6 +36,7 @@
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/mm/iob.h>
+#include <nuttx/mm/mempool.h>
 #include <nuttx/net/netconfig.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/netdev.h>
@@ -71,10 +72,10 @@ rmutex_t g_bluetooth_connections_lock = 
NXRMUTEX_INITIALIZER;
  * network lock.
  */
 
-NET_BUFPOOL_DECLARE(g_bluetooth_connections, sizeof(struct bluetooth_conn_s),
-                    CONFIG_NET_BLUETOOTH_PREALLOC_CONNS,
-                    CONFIG_NET_BLUETOOTH_ALLOC_CONNS,
-                    CONFIG_NET_BLUETOOTH_MAX_CONNS);
+MEMPOOL_DEFINE(g_bluetooth_connections, sizeof(struct bluetooth_conn_s),
+               CONFIG_NET_BLUETOOTH_PREALLOC_CONNS,
+               CONFIG_NET_BLUETOOTH_MAX_CONNS,
+               CONFIG_NET_BLUETOOTH_ALLOC_CONNS);
 
 /* A list of all allocated packet socket connections */
 
@@ -106,9 +107,11 @@ FAR struct bluetooth_conn_s *bluetooth_conn_alloc(void)
 
   bluetooth_conn_list_lock();
 
-  conn = NET_BUFPOOL_TRYALLOC(g_bluetooth_connections);
+  conn = mempool_allocate(&g_bluetooth_connections, 0);
   if (conn)
     {
+      memset(conn, 0, sizeof(*conn));
+
       /* Mark as unbound */
 
       conn->bc_proto = BTPROTO_NONE;
@@ -170,7 +173,7 @@ void bluetooth_conn_free(FAR struct bluetooth_conn_s *conn)
 
   /* Free the connection structure */
 
-  NET_BUFPOOL_FREE(g_bluetooth_connections, conn);
+  mempool_release(&g_bluetooth_connections, conn);
 
   bluetooth_conn_list_unlock();
 }
diff --git a/net/ieee802154/ieee802154_conn.c b/net/ieee802154/ieee802154_conn.c
index 44fa1046c5f..fa7dfe18924 100644
--- a/net/ieee802154/ieee802154_conn.c
+++ b/net/ieee802154/ieee802154_conn.c
@@ -35,6 +35,7 @@
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/mm/iob.h>
+#include <nuttx/mm/mempool.h>
 #include <nuttx/net/netconfig.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/netdev.h>
@@ -70,11 +71,10 @@ rmutex_t g_ieee802154_connections_lock = 
NXRMUTEX_INITIALIZER;
  * network lock.
  */
 
-NET_BUFPOOL_DECLARE(g_ieee802154_connections,
-                    sizeof(struct ieee802154_conn_s),
-                    CONFIG_NET_IEEE802154_PREALLOC_CONNS,
-                    CONFIG_NET_IEEE802154_ALLOC_CONNS,
-                    CONFIG_NET_IEEE802154_MAX_CONNS);
+MEMPOOL_DEFINE(g_ieee802154_connections, sizeof(struct ieee802154_conn_s),
+               CONFIG_NET_IEEE802154_PREALLOC_CONNS,
+               CONFIG_NET_IEEE802154_MAX_CONNS,
+               CONFIG_NET_IEEE802154_ALLOC_CONNS);
 
 /* A list of all allocated packet socket connections */
 
@@ -101,9 +101,11 @@ FAR struct ieee802154_conn_s *ieee802154_conn_alloc(void)
 
   ieee802154_conn_list_lock();
 
-  conn = NET_BUFPOOL_TRYALLOC(g_ieee802154_connections);
+  conn = mempool_allocate(&g_ieee802154_connections, 0);
   if (conn)
     {
+      memset(conn, 0, sizeof(*conn));
+
       dq_addlast(&conn->sconn.s_node, &g_active_ieee802154_connections);
     }
 
@@ -157,7 +159,7 @@ void ieee802154_conn_free(FAR struct ieee802154_conn_s 
*conn)
 
   /* Free the connection. */
 
-  NET_BUFPOOL_FREE(g_ieee802154_connections, conn);
+  mempool_release(&g_ieee802154_connections, conn);
 
   ieee802154_conn_list_unlock();
 }
diff --git a/net/usrsock/usrsock_conn.c b/net/usrsock/usrsock_conn.c
index 0b76e6996b8..3b9ce631547 100644
--- a/net/usrsock/usrsock_conn.c
+++ b/net/usrsock/usrsock_conn.c
@@ -38,6 +38,7 @@
 #include <nuttx/kmalloc.h>
 #include <nuttx/mutex.h>
 #include <nuttx/semaphore.h>
+#include <nuttx/mm/mempool.h>
 #include <nuttx/net/netconfig.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/usrsock.h>
@@ -67,10 +68,9 @@ rmutex_t g_usrsock_lock = NXRMUTEX_INITIALIZER;
 
 /* The array containing all usrsock connections. */
 
-NET_BUFPOOL_DECLARE(g_usrsock_connections, sizeof(struct usrsock_conn_s),
-                    CONFIG_NET_USRSOCK_PREALLOC_CONNS,
-                    CONFIG_NET_USRSOCK_ALLOC_CONNS,
-                    CONFIG_NET_USRSOCK_MAX_CONNS);
+MEMPOOL_DEFINE(g_usrsock_connections, sizeof(struct usrsock_conn_s),
+               CONFIG_NET_USRSOCK_PREALLOC_CONNS,
+               CONFIG_NET_USRSOCK_MAX_CONNS, CONFIG_NET_USRSOCK_ALLOC_CONNS);
 
 /* A list of all allocated usrsock connections */
 
@@ -95,11 +95,13 @@ FAR struct usrsock_conn_s *usrsock_alloc(void)
 
   /* The free list is protected by a a mutex. */
 
-  NET_BUFPOOL_LOCK(g_usrsock_connections);
+  usrsock_lock();
 
-  conn = NET_BUFPOOL_TRYALLOC(g_usrsock_connections);
+  conn = mempool_allocate(&g_usrsock_connections, 0);
   if (conn)
     {
+      memset(conn, 0, sizeof(*conn));
+
       /* Make sure that the connection is marked as uninitialized */
 
       nxsem_init(&conn->resp.sem, 0, 1);
@@ -115,7 +117,7 @@ FAR struct usrsock_conn_s *usrsock_alloc(void)
       dq_addlast(&conn->sconn.s_node, &g_active_usrsock_connections);
     }
 
-  NET_BUFPOOL_UNLOCK(g_usrsock_connections);
+  usrsock_unlock();
   return conn;
 }
 
@@ -134,7 +136,7 @@ void usrsock_free(FAR struct usrsock_conn_s *conn)
 
   DEBUGASSERT(conn->crefs == 0);
 
-  NET_BUFPOOL_LOCK(g_usrsock_connections);
+  usrsock_lock();
 
   /* Remove the connection from the active list */
 
@@ -150,9 +152,9 @@ void usrsock_free(FAR struct usrsock_conn_s *conn)
 
   /* Free the connection. */
 
-  NET_BUFPOOL_FREE(g_usrsock_connections, conn);
+  mempool_release(&g_usrsock_connections, conn);
 
-  NET_BUFPOOL_UNLOCK(g_usrsock_connections);
+  usrsock_unlock();
 }
 
 /****************************************************************************

Reply via email to