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 aa519e050111afd8c5a1a3d4459b0a057fab0b79
Author: Bowen Wang <[email protected]>
AuthorDate: Mon Aug 4 14:18:04 2025 +0800

    mm/mempool: support static define the mempool and auto init feature
    
    Support use MEMPOOL_DEFINE to define a mempool and this mempool will
    auto initialize at the first time to access the mempool.
    And mempool that not use MEMPOOL_DEFINE defined can this feature too.
    
    Signed-off-by: Bowen Wang <[email protected]>
---
 include/nuttx/mm/mempool.h | 33 ++++++++++++++++
 mm/mempool/mempool.c       | 95 +++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 126 insertions(+), 2 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 071d14722a0..d4ae0029f30 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -35,6 +35,7 @@
 #include <nuttx/mm/mm.h>
 #include <nuttx/nuttx.h>
 #include <nuttx/fs/procfs.h>
+#include <nuttx/macro.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/semaphore.h>
 
@@ -42,6 +43,9 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
+#define MEMPOOL_HEADER_SIZE        (sizeof(sq_entry_t) + \
+                                    CONFIG_MM_NODE_GUARDSIZE)
+
 #if CONFIG_MM_BACKTRACE >= 0
 #  define MEMPOOL_REALBLOCKSIZE(s) (ALIGN_UP((s) + \
                                     sizeof(struct mempool_backtrace_s), \
@@ -50,6 +54,34 @@
 #  define MEMPOOL_REALBLOCKSIZE(s) (s)
 #endif
 
+/* Static mempool definition related macros, in which:
+ *   name:     The name of the static mempool
+ *   blksize:  The size of each node in the mempool
+ *   prealloc: The number of pre-allocated buffers
+ *   maxalloc: The number of max allocations, 0 means no limit
+ *   dynalloc: The number per dynamic allocations
+ */
+
+#define MEMPOOL_DEFINE(name, blksize, prealloc, maxalloc, dynalloc) \
+  static char CONCATENATE(name, _buffer) \
+  [(prealloc) * MEMPOOL_REALBLOCKSIZE(blksize) + MEMPOOL_HEADER_SIZE] \
+  aligned_data(sizeof(uintptr_t)); \
+  static struct mempool_s name = \
+    { \
+      CONCATENATE(name, _buffer), \
+      MEMPOOL_REALBLOCKSIZE(blksize) * (prealloc), \
+      (blksize), \
+      0, \
+      MEMPOOL_REALBLOCKSIZE(blksize) * (dynalloc), \
+      (maxalloc), \
+      true, \
+      NULL, \
+      STRINGIFY(name), \
+      NULL, \
+      NULL, \
+      NULL, \
+    }
+
 /****************************************************************************
  * Public Types
  ****************************************************************************/
@@ -114,6 +146,7 @@ struct mempool_s
   size_t     nalloc;  /* The number of used block in mempool */
   spinlock_t lock;    /* The protect lock to mempool */
   sem_t      waitsem; /* The semaphore of waiter get free block */
+  bool       init;    /* The flag indicates whether the mempool initialize 
finished */
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
   struct mempool_procfs_entry_s procfs; /* The entry of procfs */
 #endif
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index c4c548a8651..d473853db21 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -34,6 +34,7 @@
 #include <nuttx/kmalloc.h>
 #include <nuttx/mm/kasan.h>
 #include <nuttx/mm/mempool.h>
+#include <nuttx/mutex.h>
 #include <nuttx/nuttx.h>
 #include <nuttx/sched.h>
 
@@ -41,8 +42,6 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define MEMPOOL_HEADER_SIZE (sizeof(sq_entry_t) + CONFIG_MM_NODE_GUARDSIZE)
-
 #if CONFIG_MM_BACKTRACE >= 0
 #define MEMPOOL_MAGIC_FREE  0x55555555
 #define MEMPOOL_MAGIC_ALLOC 0xAAAAAAAA
@@ -56,6 +55,12 @@ typedef void (*mempool_callback_t)(FAR struct mempool_s 
*pool,
                                    FAR const void *input, FAR void *output);
 #endif
 
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static mutex_t g_mempool_init_lock = NXMUTEX_INITIALIZER;
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -244,6 +249,47 @@ mempool_memdump_free_callback(FAR struct mempool_s *pool,
 }
 #endif
 
+/****************************************************************************
+ * Name: mempool_alloc_callback
+ *
+ * Description:
+ *   The static mempool alloc callback function.
+ *
+ ****************************************************************************/
+
+static FAR void *mempool_alloc_callback(FAR struct mempool_s *pool,
+                                        size_t size)
+{
+  return mm_malloc(pool->priv, size);
+}
+
+/****************************************************************************
+ * Name: mempool_free_callback
+ *
+ * Description:
+ *   The static mempool free callback function.
+ *
+ ****************************************************************************/
+
+static void mempool_free_callback(FAR struct mempool_s *pool, FAR void *addr)
+{
+  mm_free(pool->priv, addr);
+}
+
+/****************************************************************************
+ * Name: mempool_check_callback
+ *
+ * Description:
+ *   The static mempool check callback function.
+ *
+ ****************************************************************************/
+
+static void mempool_check_callback(FAR struct mempool_s *pool,
+                                   FAR void *addr)
+{
+  DEBUGASSERT(mm_heapmember(pool->priv, addr));
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -255,6 +301,12 @@ mempool_memdump_free_callback(FAR struct mempool_s *pool,
  *   Initialize a memory pool.
  *   The user needs to specify the initialization information of mempool
  *   including blocksize, initialsize, expandsize, interruptsize.
+ *   This function is also used to auto init the mempool if user do not
+ *   call the mempool_init() explicitly.
+ *   And if user do not specified the `priv`, `alloc`, `free` and `check`
+ *   elements, mempool_init() will use a default dynamic expand method
+ *   (expand memory from USER_HEAP/KNR_HEAP) and MEMPOOL_DEFINE defined
+ *   mempool will use this method too.
  *
  * Input Parameters:
  *   pool - Address of the memory pool to be used.
@@ -268,6 +320,33 @@ int mempool_init(FAR struct mempool_s *pool)
 {
   size_t blocksize = MEMPOOL_REALBLOCKSIZE(pool->blocksize);
 
+  if (pool->init)
+    {
+      return OK;
+    }
+
+  nxmutex_lock(&g_mempool_init_lock);
+  if (pool->init)
+    {
+      nxmutex_unlock(&g_mempool_init_lock);
+      return OK;
+    }
+
+  if (pool->priv == NULL && pool->alloc == NULL &&
+      pool->free == NULL && pool->check == NULL)
+    {
+      /* Default dynamic expand feature */
+
+#ifdef __KERNEL__
+      pool->priv = KNR_HEAP;
+#else
+      pool->priv = USR_HEAP;
+#endif
+      pool->alloc = mempool_alloc_callback;
+      pool->free  = mempool_free_callback;
+      pool->check = mempool_check_callback;
+    }
+
   sq_init(&pool->queue);
   sq_init(&pool->iqueue);
   sq_init(&pool->equeue);
@@ -280,6 +359,7 @@ int mempool_init(FAR struct mempool_s *pool)
       pool->ibase = pool->alloc(pool, size);
       if (pool->ibase == NULL)
         {
+          nxmutex_unlock(&g_mempool_init_lock);
           return -ENOMEM;
         }
 
@@ -309,6 +389,7 @@ int mempool_init(FAR struct mempool_s *pool)
                   pool->free(pool, pool->ibase);
                 }
 
+              nxmutex_unlock(&g_mempool_init_lock);
               return -ENOMEM;
             }
 
@@ -340,6 +421,8 @@ int mempool_init(FAR struct mempool_s *pool)
 #  endif
 #endif
 
+  pool->init = true;
+  nxmutex_unlock(&g_mempool_init_lock);
   return 0;
 }
 
@@ -367,6 +450,8 @@ FAR void *mempool_allocate(FAR struct mempool_s *pool, 
unsigned int timeout)
   FAR sq_entry_t *blk;
   irqstate_t flags;
 
+  mempool_init(pool);
+
 retry:
   flags = spin_lock_irqsave(&pool->lock);
   if (pool->maxalloc > 0 && pool->nalloc >= pool->maxalloc)
@@ -539,6 +624,7 @@ size_t mempool_navail(FAR struct mempool_s *pool)
   size_t ret;
 
   DEBUGASSERT(pool != NULL);
+  mempool_init(pool);
 
   flags = spin_lock_irqsave(&pool->lock);
   if (pool->maxalloc == 0)
@@ -573,6 +659,7 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct 
mempoolinfo_s *info)
   irqstate_t flags;
 
   DEBUGASSERT(pool != NULL && info != NULL);
+  mempool_init(pool);
 
   flags = spin_lock_irqsave(&pool->lock);
   info->ordblks = sq_count(&pool->queue);
@@ -612,6 +699,8 @@ mempool_info_task(FAR struct mempool_s *pool,
       0, 0
     };
 
+  mempool_init(pool);
+
   if (task->pid == PID_MM_FREE)
     {
       irqstate_t flags = spin_lock_irqsave(&pool->lock);
@@ -659,6 +748,8 @@ mempool_info_task(FAR struct mempool_s *pool,
 void mempool_memdump(FAR struct mempool_s *pool,
                      FAR const struct mm_memdump_s *dump)
 {
+  mempool_init(pool);
+
 #if CONFIG_MM_BACKTRACE >= 0
   if (dump->pid == PID_MM_FREE)
     {

Reply via email to