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


The following commit(s) were added to refs/heads/master by this push:
     new 0cd31f8c016 mm/mempool: Count the free blocks of a pool that cannot 
expand.
0cd31f8c016 is described below

commit 0cd31f8c016208933bbf83720f39ebc481182407
Author: Marco Casaroli <[email protected]>
AuthorDate: Thu Nov 26 15:39:41 2026 +0100

    mm/mempool: Count the free blocks of a pool that cannot expand.
    
    mempool_navail() returned SIZE_MAX for every pool without maxalloc, also
    for a pool that cannot expand (MEMPOOL_DEFINE() with dynalloc 0).  The
    callers that moved from net_bufpool to mempool then saw a free buffer
    when all were in use:
    
    - tcp_wrbuffer_test() and udp_wrbuffer_test() with
      CONFIG_NET_TCP_ALLOC_WRBCHAINS=0 or CONFIG_NET_UDP_ALLOC_WRBCHAINS=0:
      poll() reports POLLOUT, and the non-blocking send() that follows fails
      with EAGAIN, so an event loop spins.
    - can_iob_navail() returns (int)SIZE_MAX, that is -1: a CAN socket with
      write buffers never reports POLLOUT.
    
    A pool that cannot expand now gives the number of blocks in its free
    queue (and in its interrupt queue in an interrupt, as
    mempool_allocate() takes them), limited by maxalloc.  A pool that
    expands on demand gives SIZE_MAX or the rest of maxalloc, as before.
    
    Assisted-by: Claude Code:claude-opus-5-5
    Signed-off-by: Marco Casaroli <[email protected]>
---
 include/nuttx/mm/mempool.h |  2 ++
 mm/mempool/mempool.c       | 30 ++++++++++++++++++++++++------
 2 files changed, 26 insertions(+), 6 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 015dcdb6e83..bcd84f1196c 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -273,6 +273,8 @@ void mempool_release(FAR struct mempool_s *pool, FAR void 
*blk);
  *
  * Returned Value:
  *   Return the number of available buffers in the mempool, 0 means no room.
+ *   SIZE_MAX means no limit: the pool expands on demand and has no
+ *   maxalloc.
  *
  ****************************************************************************/
 
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index 66ff7f9b96a..aa76f85fe08 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -30,6 +30,7 @@
 #include <stdbool.h>
 #include <stdio.h>
 #include <syslog.h>
+#include <sys/param.h>
 
 #include <nuttx/kmalloc.h>
 #include <nuttx/mm/kasan.h>
@@ -659,27 +660,44 @@ void mempool_release(FAR struct mempool_s *pool, FAR void 
*blk)
  *
  * Returned Value:
  *   Return the number of available buffers in the mempool, 0 means no room.
+ *   SIZE_MAX means no limit: the pool expands on demand and has no
+ *   maxalloc.
  *
  ****************************************************************************/
 
 size_t mempool_navail(FAR struct mempool_s *pool)
 {
+  size_t blocksize;
   irqstate_t flags;
-  size_t ret;
+  size_t ret = SIZE_MAX;
 
   DEBUGASSERT(pool != NULL);
   mempool_init(pool);
 
+  blocksize = MEMPOOL_REALBLOCKSIZE(pool->blocksize);
   flags = spin_lock_irqsave(&pool->lock);
-  if (pool->maxalloc == 0)
+
+  /* A pool that cannot expand has only the free blocks of its queues, as
+   * mempool_allocate() takes them: the interrupt queue only in an
+   * interrupt.
+   */
+
+  if (pool->expandsize < blocksize + MEMPOOL_HEADER_SIZE)
     {
-      spin_unlock_irqrestore(&pool->lock, flags);
-      return SIZE_MAX;
+      ret = sq_count(&pool->queue);
+      if (up_interrupt_context())
+        {
+          ret += sq_count(&pool->iqueue);
+        }
     }
 
-  ret = pool->maxalloc > pool->nalloc ? pool->maxalloc - pool->nalloc : 0;
-  spin_unlock_irqrestore(&pool->lock, flags);
+  if (pool->maxalloc > 0)
+    {
+      ret = MIN(ret, pool->maxalloc > pool->nalloc ?
+                     pool->maxalloc - pool->nalloc : 0);
+    }
 
+  spin_unlock_irqrestore(&pool->lock, flags);
   return ret;
 }
 

Reply via email to