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 7c703c8e20dd37e8d0b22b8087959c89cf86fd90
Author: anjiahao <[email protected]>
AuthorDate: Mon Nov 16 14:34:29 2026 +0800

    mm/mempool: wake waiters of static pools
    
    mempool_init(), mempool_allocate(), mempool_release(), mempool_info() and
    mempool_deinit() test expandsize == 0 to decide whether a pool can block.
    MEMPOOL_DEFINE() always leaves expandsize at least MEMPOOL_HEADER_SIZE,
    so a statically defined pool that cannot expand returned NULL instead of
    waiting, and its waitsem was never initialized or posted.
    
    Decide all cases with mempool_can_wait(): the pool waits only when it
    cannot expand by at least one block.
    
    Signed-off-by: anjiahao <[email protected]>
    Signed-off-by: Bowen Wang <[email protected]>
---
 mm/mempool/mempool.c | 22 +++++++++++++++++-----
 1 file changed, 17 insertions(+), 5 deletions(-)

diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index d39ca4dc0c6..91061c5bbe2 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -65,6 +65,18 @@ static mutex_t g_mempool_init_lock = NXMUTEX_INITIALIZER;
  * Private Functions
  ****************************************************************************/
 
+/* A pool can wait for a free block only when it cannot expand on demand.
+ * MEMPOOL_DEFINE() leaves expandsize at MEMPOOL_HEADER_SIZE, so testing for
+ * zero misses every statically defined pool and leaves its waitsem unused.
+ */
+
+static inline bool mempool_can_wait(FAR struct mempool_s *pool)
+{
+  size_t blocksize = MEMPOOL_REALBLOCKSIZE(pool->blocksize);
+
+  return pool->wait && pool->expandsize < blocksize + MEMPOOL_HEADER_SIZE;
+}
+
 static inline FAR sq_entry_t *
 mempool_remove_queue(FAR struct mempool_s *pool, FAR sq_queue_t *queue)
 {
@@ -408,7 +420,7 @@ int mempool_init(FAR struct mempool_s *pool)
     }
 
   spin_lock_init(&pool->lock);
-  if (pool->wait && pool->expandsize == 0)
+  if (mempool_can_wait(pool))
     {
       nxsem_init(&pool->waitsem, 0, 0);
     }
@@ -499,7 +511,7 @@ retry:
                          &pool->equeue);
               blk = mempool_remove_queue(pool, &pool->queue);
             }
-          else if (!pool->wait || pool->expandsize > 0 || timeout == 0)
+          else if (!mempool_can_wait(pool) || timeout == 0)
             {
               return NULL;
             }
@@ -593,7 +605,7 @@ void mempool_release(FAR struct mempool_s *pool, FAR void 
*blk)
 
   kasan_poison(blk, pool->blocksize);
   spin_unlock_irqrestore(&pool->lock, flags);
-  if (pool->wait && pool->expandsize == 0)
+  if (mempool_can_wait(pool))
     {
       int semcount;
 
@@ -671,7 +683,7 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct 
mempoolinfo_s *info)
   info->maxalloc = pool->maxalloc;
   spin_unlock_irqrestore(&pool->lock, flags);
   info->sizeblks = blocksize;
-  if (pool->wait && pool->expandsize == 0)
+  if (mempool_can_wait(pool))
     {
       int semcount;
 
@@ -828,7 +840,7 @@ int mempool_deinit(FAR struct mempool_s *pool)
       pool->free(pool, pool->ibase);
     }
 
-  if (pool->wait && pool->expandsize == 0)
+  if (mempool_can_wait(pool))
     {
       nxsem_destroy(&pool->waitsem);
     }

Reply via email to