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


The following commit(s) were added to refs/heads/master by this push:
     new d96cceb4462 net: replace net_bufpool with mempool
d96cceb4462 is described below

commit d96cceb4462d6428f60ba5e95226ecacd6d1eed4
Author: zhanghongyu <[email protected]>
AuthorDate: Wed Nov 25 21:17:57 2026 +0800

    net: replace net_bufpool with mempool
    
    net_bufpool duplicates what mm/mempool already provides: a pool of
    fixed-size blocks with a free list, a preallocated array, optional
    dynamic expansion and a blocking allocation with a timeout.  mempool
    has since grown mempool_zallocate(), the last piece that net_bufpool
    users needed, so the duplicate can go.
    
    Replace the remaining users and delete net_bufpool:
    
      NET_BUFPOOL_DECLARE(p, size, prealloc, dynalloc, maxalloc)
        -> MEMPOOL_DEFINE(p, size, prealloc, maxalloc, dynalloc)
      NET_BUFPOOL_TIMEDALLOC(p, t) -> mempool_allocate(&p, t)
      NET_BUFPOOL_TRYALLOC(p)      -> mempool_zallocate(&p, 0)
      NET_BUFPOOL_FREE(p, n)       -> mempool_release(&p, n)
      NET_BUFPOOL_NAVAIL(p)        -> mempool_navail(&p)
    
    Note that MEMPOOL_DEFINE takes maxalloc before dynalloc, the reverse of
    NET_BUFPOOL_DECLARE.
    
    The connection pools use mempool_zallocate() because
    net_bufpool_timedalloc() returned zeroed memory and the callers rely on
    it: can_raw_alloc() and isotp_setup() set only a few fields and leave
    the rest at zero.  can_bufpool keeps mempool_allocate(): it hands out
    raw IOB buffers, and iob_init_with_data() clears the header itself while
    the payload is overwritten, so clearing the whole block would be waste.
    
    The NET_BUFPOOL_LOCK()/UNLOCK() pair around the CAN connection pools is
    dropped.  mempool takes its own lock, and the active connection lists
    that the same critical sections touch are already protected by
    can_conn_list_lock().
    
    Assisted-by: claude-opus-5
    Signed-off-by: zhanghongyu <[email protected]>
---
 net/can/can_bufpool.c    |  12 +-
 net/can/can_isotp.c      |  18 ++-
 net/can/can_raw.c        |  24 ++--
 net/utils/CMakeLists.txt |   3 +-
 net/utils/Make.defs      |   1 -
 net/utils/net_bufpool.c  | 279 -----------------------------------------------
 net/utils/utils.h        | 136 -----------------------
 7 files changed, 22 insertions(+), 451 deletions(-)

diff --git a/net/can/can_bufpool.c b/net/can/can_bufpool.c
index cdf22a3fc1d..996207c9510 100644
--- a/net/can/can_bufpool.c
+++ b/net/can/can_bufpool.c
@@ -27,8 +27,8 @@
 #include <nuttx/config.h>
 
 #include <nuttx/debug.h>
-
 #include <nuttx/net/can.h>
+#include <nuttx/mm/mempool.h>
 
 #include "utils/utils.h"
 
@@ -46,8 +46,8 @@
 
 /* The array containing all CAN buffers */
 
-NET_BUFPOOL_DECLARE(g_can_buffer, CAN_BUFFER_SIZE,
-                    CONFIG_NET_CAN_NBUFFERS, 0, 0);
+MEMPOOL_DEFINE(g_can_buffer, CAN_BUFFER_SIZE,
+               CONFIG_NET_CAN_NBUFFERS, 0, 0);
 
 /****************************************************************************
  * Private Functions
@@ -66,7 +66,7 @@ NET_BUFPOOL_DECLARE(g_can_buffer, CAN_BUFFER_SIZE,
 
 static void can_buf_free(FAR void *buf)
 {
-  NET_BUFPOOL_FREE(g_can_buffer, buf);
+  mempool_release(&g_can_buffer, buf);
 }
 
 /****************************************************************************
@@ -103,7 +103,7 @@ FAR struct iob_s *can_iob_timedalloc(bool throttled, 
unsigned int timeout,
       blresult2 = nxrmutex_breaklock(&dev->d_lock, &count2);
     }
 
-  buf = NET_BUFPOOL_TIMEDALLOC(g_can_buffer, timeout);
+  buf = mempool_allocate(&g_can_buffer, timeout);
   if (buf != NULL)
     {
       buf = iob_init_with_data(buf, CAN_BUFFER_SIZE, can_buf_free);
@@ -165,5 +165,5 @@ FAR struct iob_s *can_iob_clone(FAR struct net_driver_s 
*dev)
 
 int can_iob_navail(void)
 {
-  return NET_BUFPOOL_NAVAIL(g_can_buffer);
+  return (int)mempool_navail(&g_can_buffer);
 }
diff --git a/net/can/can_isotp.c b/net/can/can_isotp.c
index 3a9682992de..4005ec97720 100644
--- a/net/can/can_isotp.c
+++ b/net/can/can_isotp.c
@@ -53,6 +53,8 @@
 #include "netdev/netdev.h"
 #include "devif/devif.h"
 #include "socket/socket.h"
+#include <nuttx/mm/mempool.h>
+
 #include "utils/utils.h"
 #include "can/can.h"
 
@@ -162,10 +164,10 @@ static void isotp_tx_finish(FAR struct isotp_conn_s 
*isotp, int result);
  * Private Data
  ****************************************************************************/
 
-NET_BUFPOOL_DECLARE(g_isotp_conns, sizeof(struct isotp_conn_s),
-                    CONFIG_NET_CAN_ISOTP_PREALLOC_CONNS,
-                    CONFIG_NET_CAN_ISOTP_ALLOC_CONNS,
-                    CONFIG_NET_CAN_ISOTP_MAX_CONNS);
+MEMPOOL_DEFINE(g_isotp_conns, sizeof(struct isotp_conn_s),
+               CONFIG_NET_CAN_ISOTP_PREALLOC_CONNS,
+               CONFIG_NET_CAN_ISOTP_MAX_CONNS,
+               CONFIG_NET_CAN_ISOTP_ALLOC_CONNS);
 
 /****************************************************************************
  * Public Data
@@ -1144,9 +1146,7 @@ static int isotp_setup(FAR struct socket *psock)
       return -EPROTONOSUPPORT;
     }
 
-  NET_BUFPOOL_LOCK(g_isotp_conns);
-  conn = NET_BUFPOOL_TRYALLOC(g_isotp_conns);
-  NET_BUFPOOL_UNLOCK(g_isotp_conns);
+  conn = mempool_zallocate(&g_isotp_conns, 0);
   if (conn == NULL)
     {
       return -ENOMEM;
@@ -1737,9 +1737,7 @@ static int isotp_close(FAR struct socket *psock)
       nxmutex_destroy(&conn->rx_lock);
       can->crefs = 0;
       can_conn_uninit(can);
-      NET_BUFPOOL_LOCK(g_isotp_conns);
-      NET_BUFPOOL_FREE(g_isotp_conns, conn);
-      NET_BUFPOOL_UNLOCK(g_isotp_conns);
+      mempool_release(&g_isotp_conns, conn);
     }
   else
     {
diff --git a/net/can/can_raw.c b/net/can/can_raw.c
index 5d866cfc1d6..c33984c2c69 100644
--- a/net/can/can_raw.c
+++ b/net/can/can_raw.c
@@ -61,6 +61,8 @@
 #include "devif/devif.h"
 #include "netdev/netdev.h"
 #include "socket/socket.h"
+#include <nuttx/mm/mempool.h>
+
 #include "utils/utils.h"
 
 /****************************************************************************
@@ -154,10 +156,9 @@ static int can_raw_setsockopt(FAR struct socket *psock, 
int level,
 
 /* The array containing all CAN_RAW connections. */
 
-NET_BUFPOOL_DECLARE(g_can_raw_connections, sizeof(struct can_raw_conn_s),
-                    CONFIG_NET_CAN_PREALLOC_CONNS,
-                    CONFIG_NET_CAN_ALLOC_CONNS,
-                    CONFIG_NET_CAN_MAX_CONNS);
+MEMPOOL_DEFINE(g_can_raw_connections, sizeof(struct can_raw_conn_s),
+               CONFIG_NET_CAN_PREALLOC_CONNS, CONFIG_NET_CAN_MAX_CONNS,
+               CONFIG_NET_CAN_ALLOC_CONNS);
 
 /****************************************************************************
  * Public Data
@@ -325,11 +326,7 @@ static FAR struct can_conn_s *can_raw_alloc(void)
 {
   FAR struct can_raw_conn_s *raw;
 
-  /* The free list is protected by a a mutex. */
-
-  NET_BUFPOOL_LOCK(g_can_raw_connections);
-
-  raw = NET_BUFPOOL_TRYALLOC(g_can_raw_connections);
+  raw = mempool_zallocate(&g_can_raw_connections, 0);
   if (raw != NULL)
     {
       /* FIXME SocketCAN default behavior enables loopback */
@@ -359,7 +356,6 @@ static FAR struct can_conn_s *can_raw_alloc(void)
       can_conn_init(&raw->conn);
     }
 
-  NET_BUFPOOL_UNLOCK(g_can_raw_connections);
   return raw != NULL ? &raw->conn : NULL;
 }
 
@@ -374,19 +370,13 @@ static FAR struct can_conn_s *can_raw_alloc(void)
 
 static void can_raw_free(FAR struct can_conn_s *conn)
 {
-  /* The free list is protected by a mutex. */
-
   DEBUGASSERT(conn->crefs == 0);
 
-  NET_BUFPOOL_LOCK(g_can_raw_connections);
-
   can_conn_uninit(conn);
 
   /* Free the connection. */
 
-  NET_BUFPOOL_FREE(g_can_raw_connections, conn);
-
-  NET_BUFPOOL_UNLOCK(g_can_raw_connections);
+  mempool_release(&g_can_raw_connections, conn);
 }
 
 /****************************************************************************
diff --git a/net/utils/CMakeLists.txt b/net/utils/CMakeLists.txt
index edde35684c9..ff6cc52ef40 100644
--- a/net/utils/CMakeLists.txt
+++ b/net/utils/CMakeLists.txt
@@ -33,8 +33,7 @@ set(SRCS
     net_snoop.c
     net_cmsg.c
     net_iob_concat.c
-    net_mask2pref.c
-    net_bufpool.c)
+    net_mask2pref.c)
 
 # IPv6 utilities
 
diff --git a/net/utils/Make.defs b/net/utils/Make.defs
index 2a7ce45940e..a75b9b231c5 100644
--- a/net/utils/Make.defs
+++ b/net/utils/Make.defs
@@ -25,7 +25,6 @@
 NET_CSRCS += net_dsec2tick.c net_dsec2timeval.c net_timeval2dsec.c
 NET_CSRCS += net_chksum.c net_ipchksum.c net_incr32.c net_lock.c
 NET_CSRCS += net_snoop.c net_cmsg.c net_iob_concat.c net_mask2pref.c
-NET_CSRCS += net_bufpool.c
 
 # IPv6 utilities
 
diff --git a/net/utils/net_bufpool.c b/net/utils/net_bufpool.c
deleted file mode 100644
index 3631cf47967..00000000000
--- a/net/utils/net_bufpool.c
+++ /dev/null
@@ -1,279 +0,0 @@
-/****************************************************************************
- * net/utils/net_bufpool.c
- *
- * SPDX-License-Identifier: Apache-2.0
- *
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements.  See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.  The
- * ASF licenses this file to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance with the
- * License.  You may obtain a copy of the License at
- *
- *   http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
- * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
- * License for the specific language governing permissions and limitations
- * under the License.
- *
- ****************************************************************************/
-
-/****************************************************************************
- * Included Files
- ****************************************************************************/
-
-#include <nuttx/config.h>
-
-#include <assert.h>
-
-#include <nuttx/kmalloc.h>
-#include <nuttx/net/net.h>
-#include <nuttx/semaphore.h>
-
-#include "utils/utils.h"
-
-/****************************************************************************
- * Private Types
- ****************************************************************************/
-
-/* The node to store in the pool */
-
-struct net_bufnode_s
-{
-  sq_entry_t node;
-};
-
-/****************************************************************************
- * Private Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: net_bufpool_init
- *
- * Description:
- *   Initialize a network buffer pool.
- *
- * Input Parameters:
- *   pool - The pool to be initialized
- *
- ****************************************************************************/
-
-static void net_bufpool_init(FAR struct net_bufpool_s *pool)
-{
-  int i;
-
-  DEBUGASSERT(pool->nodesize < 0);
-  pool->nodesize = -pool->nodesize;
-
-  for (i = 0; i < pool->prealloc; i++)
-    {
-      FAR struct net_bufnode_s *node = (FAR struct net_bufnode_s *)
-                                      (pool->pool + i * pool->nodesize);
-
-      sq_addlast(&node->node, &pool->freebuffers);
-    }
-}
-
-/****************************************************************************
- * Public Functions
- ****************************************************************************/
-
-/****************************************************************************
- * Name: net_bufpool_timedalloc
- *
- * Description:
- *   Allocate a buffer from the pool.  If no buffer is available, then wait
- *   for the specified timeout.
- *
- * Input Parameters:
- *   pool    - The pool from which to allocate the buffer
- *   timeout - The maximum time to wait for a buffer to become available.
- *
- * Returned Value:
- *   A reference to the allocated buffer, which is guaranteed to be zeroed.
- *   NULL is returned on a timeout.
- *
- ****************************************************************************/
-
-FAR void *net_bufpool_timedalloc(FAR struct net_bufpool_s *pool,
-                                 unsigned int timeout)
-{
-  FAR struct net_bufnode_s *node;
-  FAR void *buf;
-  int ret;
-  int i;
-
-  if (pool->nodesize < 0)
-    {
-      net_bufpool_init(pool);
-      DEBUGASSERT(pool->nodesize > 0);
-    }
-
-  if (timeout == 0)
-    {
-      ret = nxsem_trywait(&pool->sem);
-    }
-  else
-    {
-      ret = net_sem_timedwait_uninterruptible(&pool->sem, timeout);
-    }
-
-  if (ret != OK)
-    {
-      return NULL;
-    }
-
-  nxrmutex_lock(&pool->lock);
-
-  /* If we get here, then we didn't exceed maxalloc. */
-
-  if (pool->dynalloc > 0 && sq_peek(&pool->freebuffers) == NULL)
-    {
-      node = kmm_zalloc(pool->nodesize * pool->dynalloc);
-      if (node == NULL)
-        {
-          buf = NULL;
-          goto out;
-        }
-
-      /* Now initialize each connection structure */
-
-      for (i = 0; i < pool->dynalloc; i++)
-        {
-          sq_addlast(&node->node, &pool->freebuffers);
-          node = (FAR struct net_bufnode_s *)
-                                      ((FAR char *)node + pool->nodesize);
-        }
-    }
-
-  buf = sq_remfirst(&pool->freebuffers);
-
-out:
-  nxrmutex_unlock(&pool->lock);
-  return buf;
-}
-
-/****************************************************************************
- * Name: net_bufpool_free
- *
- * Description:
- *   Free a buffer from the pool.
- *
- * Input Parameters:
- *   pool - The pool from which to allocate the buffer
- *   node - The buffer to be freed
- *
- ****************************************************************************/
-
-void net_bufpool_free(FAR struct net_bufpool_s *pool, FAR void *node)
-{
-  DEBUGASSERT(pool->nodesize > 0);
-
-  if (pool->dynalloc == 1 &&
-      ((FAR char *)node < pool->pool ||
-       (FAR char *)node >= pool->pool + pool->prealloc * pool->nodesize))
-    {
-      kmm_free(node);
-    }
-  else
-    {
-      FAR struct net_bufnode_s *net_bufnode = node;
-
-      net_bufpool_lock(pool);
-
-      /* Set the buffer to zero, to make sure all nodes in the free buffer
-       * pool are zeroed.
-       */
-
-      memset(net_bufnode, 0, pool->nodesize);
-      sq_addlast(&net_bufnode->node, &pool->freebuffers);
-
-      net_bufpool_unlock(pool);
-    }
-
-  nxsem_post(&pool->sem);
-}
-
-/****************************************************************************
- * Name: net_bufpool_test
- *
- * Description:
- *   Check if there is room in the buffer pool.  Does not reserve any space.
- *
- * Assumptions:
- *   None.
- *
- ****************************************************************************/
-
-int net_bufpool_test(FAR struct net_bufpool_s *pool)
-{
-  int val = 0;
-  int ret;
-
-  ret = nxsem_get_value(&pool->sem, &val);
-  if (ret >= 0)
-    {
-      ret = val > 0 ? OK : -ENOSPC;
-    }
-
-  return ret;
-}
-
-/****************************************************************************
- * Name: net_bufpool_navail
- *
- * Description:
- *   Return the number of available buffers in the buffer pool.
- *
- * Assumptions:
- *   None.
- *
- ****************************************************************************/
-
-int net_bufpool_navail(FAR struct net_bufpool_s *pool)
-{
-  int val = 0;
-  int ret;
-
-  ret = nxsem_get_value(&pool->sem, &val);
-  if (ret >= 0)
-    {
-      ret = val;
-    }
-
-  return ret;
-}
-
-/****************************************************************************
- * Name: net_bufpool_lock
- *
- * Description:
- *   Use the bufpool lock to protect the node of the buffer pool.
- *
- * Input Parameters:
- *   pool - The lock of pool to be locked.
- *
- ****************************************************************************/
-
-void net_bufpool_lock(FAR struct net_bufpool_s *pool)
-{
-  nxrmutex_lock(&pool->lock);
-}
-
-/****************************************************************************
- * Name: net_bufpool_unlock
- *
- * Description:
- *   Finish using the bufpool lock to protect the node of the buffer pool.
- *
- * Input Parameters:
- *   pool - The lock of pool to be unlocked.
- *
- ****************************************************************************/
-
-void net_bufpool_unlock(FAR struct net_bufpool_s *pool)
-{
-  nxrmutex_unlock(&pool->lock);
-}
diff --git a/net/utils/utils.h b/net/utils/utils.h
index 0ed741b2930..20389aefdbd 100644
--- a/net/utils/utils.h
+++ b/net/utils/utils.h
@@ -78,39 +78,6 @@
       (nport) = HTONS(hport); \
     } while (0)
 
-/* Network buffer pool related macros, in which:
- *   pool:     The name of the buffer pool
- *   nodesize: The size of each node in the pool
- *   prealloc: The number of pre-allocated buffers
- *   dynalloc: The number per dynamic allocations
- *   maxalloc: The number of max allocations, 0 means no limit
- */
-
-#define NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc) \
-  (dynalloc) <= 0 ? (prealloc) : ((maxalloc) > 0 ? (maxalloc) : INT16_MAX)
-
-#define NET_BUFPOOL_DECLARE(pool, nodesize, prealloc, dynalloc, maxalloc) \
-  static char pool##_buffer[prealloc][nodesize] 
aligned_data(sizeof(uintptr_t)); \
-  static struct net_bufpool_s pool = \
-    { \
-      pool##_buffer[0], \
-      prealloc, \
-      dynalloc, \
-      -(int)(nodesize), \
-      SEM_INITIALIZER(NET_BUFPOOL_MAX(prealloc, dynalloc, maxalloc)), \
-      NXRMUTEX_INITIALIZER, \
-      { NULL, NULL } \
-    };
-
-#define NET_BUFPOOL_TIMEDALLOC(p,t) net_bufpool_timedalloc(&p, t)
-#define NET_BUFPOOL_TRYALLOC(p)     net_bufpool_timedalloc(&p, 0)
-#define NET_BUFPOOL_ALLOC(p)        net_bufpool_timedalloc(&p, UINT_MAX)
-#define NET_BUFPOOL_FREE(p,n)       net_bufpool_free(&p, n)
-#define NET_BUFPOOL_TEST(p)         net_bufpool_test(&p)
-#define NET_BUFPOOL_NAVAIL(p)       net_bufpool_navail(&p)
-#define NET_BUFPOOL_LOCK(p)         net_bufpool_lock(&p)
-#define NET_BUFPOOL_UNLOCK(p)       net_bufpool_unlock(&p)
-
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -124,23 +91,6 @@ enum tv2ds_remainder_e
   TV2DS_CEIL       /* Force to next larger full decisecond */
 };
 
-/* This structure is used to manage a pool of network buffers */
-
-struct net_bufpool_s
-{
-  /* Allocation configuration */
-
-  FAR char  *pool;     /* The beginning of the pre-allocated buffer pool */
-  int        prealloc; /* The number of pre-allocated buffers */
-  int        dynalloc; /* The number per dynamic allocations */
-  int        nodesize; /* The size of each node in the pool */
-
-  sem_t      sem;      /* The semaphore for waiting for free buffers */
-
-  rmutex_t   lock;     /* The lock for the pool */
-  sq_queue_t freebuffers;
-};
-
 /****************************************************************************
  * Public Data
  ****************************************************************************/
@@ -484,92 +434,6 @@ FAR void *net_ipv6_payload(FAR struct ipv6_hdr_s *ipv6, 
FAR uint8_t *proto);
 uint16_t net_iob_concat(FAR struct iob_s **iob1, FAR struct iob_s **iob2);
 #endif
 
-/****************************************************************************
- * Name: net_bufpool_timedalloc
- *
- * Description:
- *   Allocate a buffer from the pool.  If no buffer is available, then wait
- *   for the specified timeout.
- *
- * Input Parameters:
- *   pool    - The pool from which to allocate the buffer
- *   timeout - The maximum time to wait for a buffer to become available.
- *
- * Returned Value:
- *   A reference to the allocated buffer, which is guaranteed to be zeroed.
- *   NULL is returned on a timeout.
- *
- ****************************************************************************/
-
-FAR void *net_bufpool_timedalloc(FAR struct net_bufpool_s *pool,
-                                 unsigned int timeout);
-
-/****************************************************************************
- * Name: net_bufpool_free
- *
- * Description:
- *   Free a buffer from the pool.
- *
- * Input Parameters:
- *   pool - The pool from which to allocate the buffer
- *   node - The buffer to be freed
- *
- ****************************************************************************/
-
-void net_bufpool_free(FAR struct net_bufpool_s *pool, FAR void *node);
-
-/****************************************************************************
- * Name: net_bufpool_test
- *
- * Description:
- *   Check if there is room in the buffer pool.  Does not reserve any space.
- *
- * Assumptions:
- *   None.
- *
- ****************************************************************************/
-
-int net_bufpool_test(FAR struct net_bufpool_s *pool);
-
-/****************************************************************************
- * Name: net_bufpool_navail
- *
- * Description:
- *   Return the number of available buffers in the buffer pool.
- *
- * Assumptions:
- *   None.
- *
- ****************************************************************************/
-
-int net_bufpool_navail(FAR struct net_bufpool_s *pool);
-
-/****************************************************************************
- * Name: net_bufpool_lock
- *
- * Description:
- *   Use the bufpool lock to protect the node of the buffer pool.
- *
- * Input Parameters:
- *   pool - The lock of pool to be locked.
- *
- ****************************************************************************/
-
-void net_bufpool_lock(FAR struct net_bufpool_s *pool);
-
-/****************************************************************************
- * Name: net_bufpool_unlock
- *
- * Description:
- *   Finish using the bufpool lock to protect the node of the buffer pool.
- *
- * Input Parameters:
- *   pool - The lock of pool to be unlocked.
- *
- ****************************************************************************/
-
-void net_bufpool_unlock(FAR struct net_bufpool_s *pool);
-
 /****************************************************************************
  * Name: net_chksum_adjust
  *

Reply via email to