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 af7bd948b0228a3809565e75a588b71a43689e56
Author: Bowen Wang <[email protected]>
AuthorDate: Wed Jul 30 10:32:56 2025 +0800

    !mm/mempool: support use static buffer as mempool initial buffer
    
    Now user can use pool->initialbase to pass the static buffer as
    the initial mempool buffer.
    
    BREAKING CHANGE: struct mempool_s gains initialbase as its first member
    and blocksize moves after initialsize.  Quick fix: code that fills
    struct mempool_s with a positional initializer switches to designated
    initializers (.blocksize = ..., .initialsize = ...).
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/mm/mempool.h |  3 ++-
 mm/mempool/mempool.c       | 36 +++++++++++++++++++++++-------------
 2 files changed, 25 insertions(+), 14 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 80cef518cec..267f728f6f3 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -92,8 +92,9 @@ struct mempool_procfs_entry_s
 
 struct mempool_s
 {
-  size_t     blocksize;     /* The size for every block in mempool */
+  FAR void  *initialbase;   /* The initial base pointer of normal mempool */
   size_t     initialsize;   /* The initialize size in normal mempool */
+  size_t     blocksize;     /* The size for every block in mempool */
   size_t     interruptsize; /* The initialize size in interrupt mempool */
   size_t     expandsize;    /* The size of expand block every time for mempool 
*/
   size_t     maxalloc;      /* The maximum allowed alloc number, 0 means not 
limit */
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index e9e201f11d4..5ce11c0b7f3 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -64,6 +64,7 @@ static inline FAR sq_entry_t *
 mempool_remove_queue(FAR struct mempool_s *pool, FAR sq_queue_t *queue)
 {
   FAR sq_entry_t *ret = queue->head;
+  FAR void *addr = ret;
 
   if (ret)
     {
@@ -72,9 +73,10 @@ mempool_remove_queue(FAR struct mempool_s *pool, FAR 
sq_queue_t *queue)
         {
           queue->tail = NULL;
         }
-      else
+      else if (pool->initialbase == NULL || addr < pool->initialbase ||
+               addr >= pool->initialbase + pool->initialsize)
         {
-          pool->check(pool, queue->head);
+          pool->check(pool, addr);
         }
 
       ret->flink = NULL;
@@ -298,22 +300,30 @@ int mempool_init(FAR struct mempool_s *pool, FAR const 
char *name)
       size_t size = ninitial * blocksize + MEMPOOL_HEADER_SIZE;
       FAR char *base;
 
-      base = pool->alloc(pool, size);
-      if (base == NULL)
+      if (pool->initialbase == NULL)
         {
-          if (pool->ibase)
+          base = pool->alloc(pool, size);
+          if (base == NULL)
             {
-              pool->free(pool, pool->ibase);
+              if (pool->ibase)
+                {
+                  pool->free(pool, pool->ibase);
+                }
+
+              return -ENOMEM;
             }
 
-          return -ENOMEM;
+          mempool_add_queue(pool, &pool->queue, base, ninitial, blocksize);
+          sq_addlast((FAR sq_entry_t *)(base + ninitial * blocksize),
+                      &pool->equeue);
+          kasan_poison(base, size);
+        }
+      else
+        {
+          DEBUGASSERT(pool->initialsize >= size);
+          base = pool->initialbase;
+          mempool_add_queue(pool, &pool->queue, base, ninitial, blocksize);
         }
-
-      mempool_add_queue(pool, &pool->queue,
-                        base, ninitial, blocksize);
-      sq_addlast((FAR sq_entry_t *)(base + ninitial * blocksize),
-                  &pool->equeue);
-      kasan_poison(base, size);
     }
 
   spin_lock_init(&pool->lock);

Reply via email to