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 24b09771789ad8a5a8d8c25ee86e968d3fa51466
Author: zhanghongyu <[email protected]>
AuthorDate: Tue Nov 11 21:14:55 2025 +0800

    net/tcp: Use MEMPOOL_DEFINE for the TCP connections.
    
    Replace the NET_BUFPOOL wrapper with the generic mempool for the TCP
    connection pool, so that the network code uses one allocator instead of
    its own layer on top of it.
    
    tcp_conn_list_lock() and tcp_conn_list_unlock() were built on the pool
    lock, which mempool does not expose.  Give the connection list its own
    rmutex, g_tcp_connections_lock, and make both functions inline wrappers
    around it in tcp.h.
    
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/tcp/tcp.h      | 53 +++++++++++++++++++++++++++++++++--------------------
 net/tcp/tcp_conn.c | 47 +++++++++++++++--------------------------------
 2 files changed, 48 insertions(+), 52 deletions(-)

diff --git a/net/tcp/tcp.h b/net/tcp/tcp.h
index fae1aa5007a..ce5ed9587be 100644
--- a/net/tcp/tcp.h
+++ b/net/tcp/tcp.h
@@ -35,6 +35,7 @@
 #include <nuttx/queue.h>
 #include <nuttx/semaphore.h>
 #include <nuttx/mm/iob.h>
+#include <nuttx/mutex.h>
 #include <nuttx/net/ip.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/tcp.h>
@@ -492,6 +493,38 @@ extern "C"
 {
 #endif
 
+extern rmutex_t g_tcp_connections_lock;
+
+/****************************************************************************
+ * Inline Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: tcp_conn_list_lock
+ *
+ * Description:
+ *   Lock the TCP connection list.
+ *
+ ****************************************************************************/
+
+static inline_function void tcp_conn_list_lock(void)
+{
+  nxrmutex_lock(&g_tcp_connections_lock);
+}
+
+/****************************************************************************
+ * Name: tcp_conn_list_unlock
+ *
+ * Description:
+ *   Unlock the TCP connection list.
+ *
+ ****************************************************************************/
+
+static inline_function void tcp_conn_list_unlock(void)
+{
+  nxrmutex_unlock(&g_tcp_connections_lock);
+}
+
 /****************************************************************************
  * Public Function Prototypes
  ****************************************************************************/
@@ -792,26 +825,6 @@ void tcp_removeconn(FAR struct tcp_conn_s *conn);
 
 void tcp_remove_syn_backlog(FAR struct tcp_conn_s *listener);
 
-/****************************************************************************
- * Name: tcp_conn_list_lock
- *
- * Description:
- *   Lock the TCP connection list.
- *
- ****************************************************************************/
-
-void tcp_conn_list_lock(void);
-
-/****************************************************************************
- * Name: tcp_conn_list_unlock
- *
- * Description:
- *   Unlock the TCP connection list.
- *
- ****************************************************************************/
-
-void tcp_conn_list_unlock(void);
-
 /****************************************************************************
  * Name: psock_tcp_connect
  *
diff --git a/net/tcp/tcp_conn.c b/net/tcp/tcp_conn.c
index e25bbdc80c9..3939e67871a 100644
--- a/net/tcp/tcp_conn.c
+++ b/net/tcp/tcp_conn.c
@@ -58,6 +58,7 @@
 
 #include <nuttx/clock.h>
 #include <nuttx/kmalloc.h>
+#include <nuttx/mm/mempool.h>
 #include <nuttx/net/netconfig.h>
 #include <nuttx/net/net.h>
 #include <nuttx/net/netdev.h>
@@ -82,15 +83,23 @@
 #  define CONFIG_NET_TCP_MAX_CONNS 0
 #endif
 
+/****************************************************************************
+ * Public Data
+ ****************************************************************************/
+
+/* The TCP connections rmutex */
+
+rmutex_t g_tcp_connections_lock = NXRMUTEX_INITIALIZER;
+
 /****************************************************************************
  * Private Data
  ****************************************************************************/
 
 /* The array containing all TCP connections. */
 
-NET_BUFPOOL_DECLARE(g_tcp_connections, sizeof(struct tcp_conn_s),
-                    CONFIG_NET_TCP_PREALLOC_CONNS,
-                    CONFIG_NET_TCP_ALLOC_CONNS, CONFIG_NET_TCP_MAX_CONNS);
+MEMPOOL_DEFINE(g_tcp_connections, sizeof(struct tcp_conn_s),
+               CONFIG_NET_TCP_PREALLOC_CONNS, CONFIG_NET_TCP_MAX_CONNS,
+               CONFIG_NET_TCP_ALLOC_CONNS);
 
 /* A list of all connected TCP connections */
 
@@ -803,7 +812,7 @@ static FAR struct tcp_conn_s *tcp_alloc_conn(uint8_t domain,
 
   /* Return the entry from the head of the free list */
 
-  conn = NET_BUFPOOL_TRYALLOC(g_tcp_connections);
+  conn = mempool_allocate(&g_tcp_connections, 0);
   tcp_conn_list_unlock();
 
   /* Is the free list empty?  As a fall-back, free the oldest connection
@@ -820,7 +829,7 @@ static FAR struct tcp_conn_s *tcp_alloc_conn(uint8_t domain,
        */
 
       tcp_conn_list_lock();
-      conn = NET_BUFPOOL_TRYALLOC(g_tcp_connections);
+      conn = mempool_allocate(&g_tcp_connections, 0);
       tcp_conn_list_unlock();
     }
 
@@ -1034,7 +1043,7 @@ void tcp_free(FAR struct tcp_conn_s *conn)
 
   /* Free the connection structure */
 
-  NET_BUFPOOL_FREE(g_tcp_connections, conn);
+  mempool_release(&g_tcp_connections, conn);
 }
 
 /****************************************************************************
@@ -1664,32 +1673,6 @@ int tcp_connect(FAR struct tcp_conn_s *conn, FAR const 
struct sockaddr *addr)
   return OK;
 }
 
-/****************************************************************************
- * Name: tcp_conn_list_lock
- *
- * Description:
- *   Lock the TCP connection list.
- *
- ****************************************************************************/
-
-void tcp_conn_list_lock(void)
-{
-  NET_BUFPOOL_LOCK(g_tcp_connections);
-}
-
-/****************************************************************************
- * Name: tcp_conn_list_unlock
- *
- * Description:
- *   Unlock the TCP connection list.
- *
- ****************************************************************************/
-
-void tcp_conn_list_unlock(void)
-{
-  NET_BUFPOOL_UNLOCK(g_tcp_connections);
-}
-
 /****************************************************************************
  * Name: tcp_add_active_conn
  *

Reply via email to