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 b1d4e52ef67975e4809874294d0aa78255afde3b
Author: Bowen Wang <[email protected]>
AuthorDate: Wed Jul 30 16:19:03 2025 +0800

    mm/mempool: add mempool_navail() support for mempool
    
    User can use this api to get the available buffer number in mempool
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/mm/mempool.h | 16 ++++++++++++++++
 mm/mempool/mempool.c       | 34 ++++++++++++++++++++++++++++++++++
 2 files changed, 50 insertions(+)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 267f728f6f3..60cb8129a1d 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -207,6 +207,22 @@ FAR void *mempool_allocate(FAR struct mempool_s *pool, 
unsigned int timeout);
 
 void mempool_release(FAR struct mempool_s *pool, FAR void *blk);
 
+/****************************************************************************
+ * Name: mempool_navail
+ *
+ * Description:
+ *   Return the number of available buffers in the mempool.
+ *
+ * Input Parameters:
+ *   pool - Address of the memory pool to be used.
+ *
+ * Returned Value:
+ *   Return the number of available buffers in the mempool, 0 means no room.
+ *
+ ****************************************************************************/
+
+size_t mempool_navail(FAR struct mempool_s *pool);
+
 /****************************************************************************
  * Name: mempool_info
  *
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index 5ce11c0b7f3..096a49ddbe6 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -520,6 +520,40 @@ void mempool_release(FAR struct mempool_s *pool, FAR void 
*blk)
     }
 }
 
+/****************************************************************************
+ * Name: mempool_navail
+ *
+ * Description:
+ *   Return the number of available buffers in the mempool.
+ *
+ * Input Parameters:
+ *   pool - Address of the memory pool to be used.
+ *
+ * Returned Value:
+ *   Return the number of available buffers in the mempool, 0 means no room.
+ *
+ ****************************************************************************/
+
+size_t mempool_navail(FAR struct mempool_s *pool)
+{
+  irqstate_t flags;
+  size_t ret;
+
+  DEBUGASSERT(pool != NULL);
+
+  flags = spin_lock_irqsave(&pool->lock);
+  if (pool->maxalloc == 0)
+    {
+      spin_unlock_irqrestore(&pool->lock, flags);
+      return SIZE_MAX;
+    }
+
+  ret = pool->maxalloc > pool->nalloc ? pool->maxalloc - pool->nalloc : 0;
+  spin_unlock_irqrestore(&pool->lock, flags);
+
+  return ret;
+}
+
 /****************************************************************************
  * Name: mempool_info
  *

Reply via email to