You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by xi...@apache.org on 2023/01/18 06:12:51 UTC

[nuttx] branch master updated (0a88799bab -> 632ed0d3a4)

This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a change to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git


    from 0a88799bab sched: fix task_delete crash in SMP case
     new f00d56337f mempool:remove unnecessary alignment
     new e7d02ffac2 mempool:remove multiple fixed api
     new f24fb2b10a mempool:add a private pointer to stroe private data
     new 49ffd99eaf mempool:use single queue insdie of list
     new 6b530fceae mempool:change mempool_multiple way of initialization
     new 632ed0d3a4 Optimize multipe mempool memory space usage

The 6 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


Summary of changes:
 include/nuttx/mm/mempool.h    | 138 ++++--------
 mm/Kconfig                    |   2 +-
 mm/mempool/mempool.c          | 287 ++++++++++++------------
 mm/mempool/mempool_multiple.c | 501 ++++++++++++++++++++++++++----------------
 mm/mm_heap/mm.h               |   9 +-
 mm/mm_heap/mm_free.c          |   3 +-
 mm/mm_heap/mm_initialize.c    |  43 ++--
 mm/mm_heap/mm_mallinfo.c      |   2 +-
 mm/mm_heap/mm_malloc.c        |   2 +-
 mm/mm_heap/mm_malloc_size.c   |  15 +-
 mm/mm_heap/mm_memalign.c      |   2 +-
 mm/mm_heap/mm_memdump.c       |   2 +-
 mm/mm_heap/mm_realloc.c       |  24 +-
 mm/tlsf/mm_tlsf.c             |  80 +++----
 14 files changed, 581 insertions(+), 529 deletions(-)


[nuttx] 06/06: Optimize multipe mempool memory space usage

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 632ed0d3a42241fdc7b7a01fa8cd60bf01b3560d
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Wed Nov 23 22:08:13 2022 +0800

    Optimize multipe mempool memory space usage
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h    |  15 ++-
 mm/Kconfig                    |   2 +-
 mm/mempool/mempool.c          |   5 +-
 mm/mempool/mempool_multiple.c | 301 +++++++++++++++++++++++++++++++-----------
 mm/mm_heap/mm.h               |   5 -
 mm/mm_heap/mm_free.c          |   3 +-
 mm/mm_heap/mm_malloc_size.c   |  15 ++-
 mm/mm_heap/mm_realloc.c       |  24 +---
 mm/tlsf/mm_tlsf.c             |  39 ++----
 9 files changed, 264 insertions(+), 145 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 7b991292f1..7af6558728 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -354,10 +354,14 @@ FAR void *mempool_multiple_realloc(FAR struct mempool_multiple_s *mpool,
  * Input Parameters:
  *   mpool - The handle of multiple memory pool to be used.
  *   blk  - The pointer of memory block.
+ *
+ * Returned Value:
+ *   Zero on success; Negative number mean the block doesn't come from pool.
+ *
  ****************************************************************************/
 
-void mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
-                           FAR void *blk);
+int mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
+                          FAR void *blk);
 
 /****************************************************************************
  * Name: mempool_multiple_alloc_size
@@ -370,12 +374,13 @@ void mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
  *   blk  - The pointer of memory block.
  *
  * Returned Value:
- *   The size of memory block.
+ *   The size of memory block on success. Negative number mean the block
+ *   doesn't come from pool.
  *
  ****************************************************************************/
 
-size_t mempool_multiple_alloc_size(FAR struct mempool_multiple_s *mpool,
-                                   FAR void *blk);
+ssize_t mempool_multiple_alloc_size(FAR struct mempool_multiple_s *mpool,
+                                    FAR void *blk);
 
 /****************************************************************************
  * Name: mempool_multiple_memalign
diff --git a/mm/Kconfig b/mm/Kconfig
index e47eda7d9c..8b572f142b 100644
--- a/mm/Kconfig
+++ b/mm/Kconfig
@@ -190,7 +190,7 @@ config MM_HEAP_MEMPOOL_THRESHOLD
 
 config MM_HEAP_MEMPOOL_EXPAND
 	int "The expand size for each mempool in multiple mempool"
-	default 1024
+	default 4096
 	depends on MM_HEAP_MEMPOOL_THRESHOLD != 0
 	---help---
 		This size describes the size of each expansion of each memory
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index c64ab8dc2f..d1ee35caf2 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -38,9 +38,8 @@
 #  define MM_PTR_FMT_WIDTH 19
 #endif
 
-#ifndef ALIGN_UP
-#  define ALIGN_UP(x, a) (((x) + ((a) - 1)) & (~((a) - 1)))
-#endif
+#undef  ALIGN_UP
+#define ALIGN_UP(x, a) (((x) + ((a) - 1)) & (~((a) - 1)))
 
 /****************************************************************************
  * Private Types
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index 843964d0c8..d03b541f25 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -29,21 +29,36 @@
  * Pre-processor Definitions
  ****************************************************************************/
 
-#define SIZEOF_HEAD sizeof(FAR struct mempool_s *)
-#define MAX(a, b)   ((a) > (b) ? (a) : (b))
-#define MIN(a, b)   ((a) < (b) ? (a) : (b))
-#define ALIGN_BIT   (1 << 1)
+#define MIN(a, b)             ((a) < (b) ? (a) : (b))
+#undef  ALIGN_UP
+#define ALIGN_UP(x, a)        ((((size_t)x) + ((a) - 1)) & (~((a) - 1)))
+#undef  ALIGN_DOWN
+#define ALIGN_DOWN(x, a)      ((size_t)(x) & (~((a) - 1)))
 
-struct mempool_multiple_s
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct mpool_dict_s
 {
-  FAR struct mempool_s *   pools;      /* The memory pool array */
-  size_t                   npools;     /* The number of memory pool array elements */
+  FAR struct mempool_s *pool; /* Record pool when expanding */
+  FAR void             *addr; /* Record expand memary address */
+  size_t                size; /* Record expand memary size */
+};
 
-  FAR void                *arg;     /* This pointer is used to store the user's
-                                     * private data
-                                     */
-  mempool_multiple_alloc_t alloc;   /* The alloc function for mempool */
-  mempool_multiple_free_t  free;    /* The free function for mempool */
+struct mempool_multiple_s
+{
+  FAR struct mempool_s *   pools;       /* The memory pool array */
+  size_t                   npools;      /* The number of memory pool array elements */
+  size_t                   expandsize;  /* The number not will use it to init erery
+                                         * pool expandsize
+                                         */
+  size_t                   minpoolsize; /* The number is align for each memory pool */
+  FAR void                *arg;         /* This pointer is used to store the user's
+                                         * private data
+                                         */
+  mempool_multiple_alloc_t alloc;       /* The alloc function for mempool */
+  mempool_multiple_free_t  free;        /* The free function for mempool */
 
   /* This delta describes the relationship between the block size of each
    * mempool in multiple mempool by user initialized. It is automatically
@@ -53,6 +68,14 @@ struct mempool_multiple_s
    */
 
   size_t                   delta;
+
+  /* It is used to record the information recorded by the mempool during
+   * expansion, and find the mempool by adding an index
+   */
+
+  size_t                   dict_alloc;
+  size_t                   dict_used;
+  FAR struct mpool_dict_s *dict;
 };
 
 /****************************************************************************
@@ -75,6 +98,11 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
   if (mpool->delta != 0)
     {
       left = mpool->pools[0].blocksize;
+      if (left >= size)
+        {
+          return &mpool->pools[0];
+        }
+
       mid = (size - left + mpool->delta - 1) / mpool->delta;
       return mid < right ? &mpool->pools[mid] : NULL;
     }
@@ -100,6 +128,95 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
   return &mpool->pools[left];
 }
 
+static FAR void *mempool_multiple_alloc_callback(FAR struct mempool_s *pool,
+                                                 size_t size)
+{
+  FAR struct mempool_multiple_s *mpool = pool->priv;
+  FAR void *ret;
+
+  if (mpool->dict_used >= mpool->dict_alloc)
+    {
+      ret = mpool->alloc(mpool->arg, sizeof(uintptr_t),
+                         mpool->dict_alloc *
+                         sizeof(struct mpool_dict_s) * 2);
+      if (ret == NULL)
+        {
+          return NULL;
+        }
+
+      memcpy(ret, mpool->dict,
+             mpool->dict_alloc * sizeof(struct mpool_dict_s));
+      mpool->free(mpool->arg, mpool->dict);
+      mpool->dict = ret;
+      mpool->dict_alloc *= 2;
+    }
+
+  ret = mpool->alloc(mpool->arg, mpool->expandsize,
+                     mpool->minpoolsize + size);
+  if (ret == NULL)
+    {
+      return NULL;
+    }
+
+  mpool->dict[mpool->dict_used].pool = pool;
+  mpool->dict[mpool->dict_used].addr = ret;
+  mpool->dict[mpool->dict_used].size = mpool->minpoolsize + size;
+  *(FAR size_t *)ret = mpool->dict_used++;
+  return (FAR char *)ret + mpool->minpoolsize;
+}
+
+static void mempool_multiple_free_callback(FAR struct mempool_s *pool,
+                                           FAR void *addr)
+{
+  FAR struct mempool_multiple_s *mpool = pool->priv;
+
+  return mpool->free(mpool->arg, (FAR char *)addr - mpool->minpoolsize);
+}
+
+/****************************************************************************
+ * Name: mempool_multiple_get_dict
+ *
+ * Description:
+ *   Obtain the dict through mpool and blk
+ *
+ * Input Parameters:
+ *   mpool - The handle of the multiple memory pool to be used.
+ *   blk   - The pointer of memory block.
+ *
+ * Returned Value:
+ *   Address of the dict to be used or NULL is not find.
+ *
+ ****************************************************************************/
+
+static FAR struct mpool_dict_s *
+mempool_multiple_get_dict(FAR struct mempool_multiple_s *mpool,
+                          FAR void *blk)
+{
+  FAR void *addr;
+  size_t index;
+
+  if (mpool == NULL || blk == NULL)
+    {
+      return NULL;
+    }
+
+  addr = (FAR void *)ALIGN_DOWN(blk, mpool->expandsize);
+
+  index = *(FAR size_t *)addr;
+  if (index >= mpool->dict_used)
+    {
+      return NULL;
+    }
+
+  if (mpool->dict[index].addr != addr ||
+      blk - addr >= mpool->dict[index].size)
+    {
+      return NULL;
+    }
+
+  return &mpool->dict[index];
+}
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -141,9 +258,31 @@ mempool_multiple_init(FAR const char *name,
 {
   FAR struct mempool_multiple_s *mpool;
   FAR struct mempool_s *pools;
+  size_t maxpoolszie;
+  size_t minpoolsize;
   int ret;
   int i;
 
+  if (expandsize & (expandsize - 1))
+    {
+      return NULL;
+    }
+
+  maxpoolszie = poolsize[0];
+  minpoolsize = poolsize[0];
+  for (i = 0; i < npools; i++)
+    {
+      if (maxpoolszie < poolsize[i])
+        {
+          maxpoolszie = poolsize[i];
+        }
+
+      if (minpoolsize > poolsize[i])
+        {
+          minpoolsize = poolsize[i];
+        }
+    }
+
   mpool = alloc(arg, sizeof(uintptr_t), sizeof(struct mempool_multiple_s));
   if (mpool == NULL)
     {
@@ -159,6 +298,8 @@ mempool_multiple_init(FAR const char *name,
 
   mpool->pools = pools;
   mpool->npools = npools;
+  mpool->expandsize = expandsize;
+  mpool->minpoolsize = minpoolsize;
   mpool->alloc = alloc;
   mpool->free = free;
   mpool->arg = arg;
@@ -166,17 +307,16 @@ mempool_multiple_init(FAR const char *name,
   for (i = 0; i < npools; i++)
     {
       pools[i].blocksize = poolsize[i];
-      pools[i].expandsize = expandsize;
+      pools[i].expandsize = expandsize - mpool->minpoolsize;
       pools[i].initialsize = 0;
       pools[i].interruptsize = 0;
+      pools[i].priv = mpool;
+      pools[i].alloc = mempool_multiple_alloc_callback;
+      pools[i].free = mempool_multiple_free_callback;
+
       ret = mempool_init(pools + i, name);
       if (ret < 0)
         {
-          while (--i >= 0)
-            {
-              mempool_deinit(pools + i);
-            }
-
           goto err_with_pools;
         }
 
@@ -195,9 +335,23 @@ mempool_multiple_init(FAR const char *name,
         }
     }
 
+  mpool->dict_alloc = maxpoolszie / sizeof(struct mpool_dict_s) + 1;
+  mpool->dict_used = 0;
+  mpool->dict = alloc(arg, sizeof(uintptr_t),
+                      mpool->dict_alloc * sizeof(struct mpool_dict_s));
+  if (mpool->dict == NULL)
+    {
+      goto err_with_pools;
+    }
+
   return mpool;
 
 err_with_pools:
+  while (--i >= 0)
+    {
+      mempool_deinit(pools + i);
+    }
+
   free(arg, pools);
 err_with_mpool:
   free(arg, mpool);
@@ -225,22 +379,28 @@ err_with_mpool:
 FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool,
                                  size_t size)
 {
-  FAR struct mempool_s *end = mpool->pools + mpool->npools;
+  FAR struct mempool_s *end;
   FAR struct mempool_s *pool;
 
-  pool = mempool_multiple_find(mpool, size + SIZEOF_HEAD);
+  if (size < 1)
+    {
+      return NULL;
+    }
+
+  pool = mempool_multiple_find(mpool, size);
   if (pool == NULL)
     {
       return NULL;
     }
 
+  end = mpool->pools + mpool->npools;
   do
     {
       FAR void *blk = mempool_alloc(pool);
-      if (blk != NULL)
+
+      if (blk)
         {
-          *(FAR struct mempool_s **)blk = pool;
-          return (FAR char *)blk + SIZEOF_HEAD;
+          return blk;
         }
     }
   while (++pool < end);
@@ -267,33 +427,30 @@ FAR void *mempool_multiple_alloc(FAR struct mempool_multiple_s *mpool,
 FAR void *mempool_multiple_realloc(FAR struct mempool_multiple_s *mpool,
                                    FAR void *oldblk, size_t size)
 {
+  FAR struct mpool_dict_s *dict;
   FAR void *blk;
 
+  if (oldblk == NULL)
+    {
+      return mempool_multiple_alloc(mpool, size);
+    }
+
   if (size < 1)
     {
       mempool_multiple_free(mpool, oldblk);
       return NULL;
     }
 
+  dict = mempool_multiple_get_dict(mpool, oldblk);
+  if (dict == NULL)
+    {
+      return NULL;
+    }
+
   blk = mempool_multiple_alloc(mpool, size);
   if (blk != NULL && oldblk != NULL)
     {
-      FAR struct mempool_s *oldpool;
-
-      oldpool = *(FAR struct mempool_s **)
-                ((FAR char *)oldblk - SIZEOF_HEAD);
-      if ((uintptr_t)oldpool & ALIGN_BIT)
-        {
-          oldpool = (FAR struct mempool_s *)
-                    ((uintptr_t)oldpool & ~ALIGN_BIT);
-          size = MIN(size, oldpool->blocksize -
-                     *(FAR size_t *)((FAR char *)oldblk - 2 * SIZEOF_HEAD));
-        }
-      else
-        {
-          size = MIN(size, oldpool->blocksize - SIZEOF_HEAD);
-        }
-
+      size = MIN(size, dict->pool->blocksize);
       memcpy(blk, oldblk, size);
       mempool_multiple_free(mpool, oldblk);
     }
@@ -311,25 +468,28 @@ FAR void *mempool_multiple_realloc(FAR struct mempool_multiple_s *mpool,
  * Input Parameters:
  *   mpool - The handle of multiple memory pool to be used.
  *   blk  - The pointer of memory block.
+ *
+ * Returned Value:
+ *   Zero on success; Negative number on any failure.
+ *
  ****************************************************************************/
 
-void mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
-                           FAR void *blk)
+int mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
+                          FAR void *blk)
 {
-  FAR struct mempool_s *pool;
-  FAR char *mem;
-
-  DEBUGASSERT(mpool != NULL && blk != NULL);
+  FAR struct mpool_dict_s *dict;
 
-  mem = (FAR char *)blk - SIZEOF_HEAD;
-  pool = *(FAR struct mempool_s **)mem;
-  if ((uintptr_t)pool & ALIGN_BIT)
+  dict = mempool_multiple_get_dict(mpool, blk);
+  if (dict == NULL)
     {
-      pool = (FAR struct mempool_s *)((uintptr_t)pool & ~ALIGN_BIT);
-      mem = (FAR char *)blk - *(FAR size_t *)(mem - SIZEOF_HEAD);
+      return -EINVAL;
     }
 
-  mempool_free(pool, mem);
+  blk = (FAR char *)blk - (((FAR char *)blk -
+                           ((FAR char *)dict->addr + mpool->minpoolsize)) %
+                           dict->pool->blocksize);
+  mempool_free(dict->pool, blk);
+  return 0;
 }
 
 /****************************************************************************
@@ -343,29 +503,24 @@ void mempool_multiple_free(FAR struct mempool_multiple_s *mpool,
  *   blk  - The pointer of memory block.
  *
  * Returned Value:
- *   The size of memory block.
+ *   The size of memory block on success. Negative number on any failure.
  *
  ****************************************************************************/
 
-size_t mempool_multiple_alloc_size(FAR struct mempool_multiple_s *mpool,
-                                   FAR void *blk)
+ssize_t mempool_multiple_alloc_size(FAR struct mempool_multiple_s *mpool,
+                                    FAR void *blk)
 {
-  FAR struct mempool_s *pool;
-  FAR char *mem;
+  FAR struct mpool_dict_s *dict;
 
   DEBUGASSERT(blk != NULL);
 
-  mem = (FAR char *)blk - SIZEOF_HEAD;
-  pool = *(FAR struct mempool_s **)mem;
-  if ((uintptr_t)pool & ALIGN_BIT)
+  dict = mempool_multiple_get_dict(mpool, blk);
+  if (dict == NULL)
     {
-      pool = (FAR struct mempool_s *)((uintptr_t)pool & ~ALIGN_BIT);
-      return pool->blocksize - *(FAR size_t *)(mem - SIZEOF_HEAD);
-    }
-  else
-    {
-      return pool->blocksize - SIZEOF_HEAD;
+      return -EINVAL;
     }
+
+  return dict->pool->blocksize;
 }
 
 /****************************************************************************
@@ -398,31 +553,24 @@ size_t mempool_multiple_alloc_size(FAR struct mempool_multiple_s *mpool,
 FAR void *mempool_multiple_memalign(FAR struct mempool_multiple_s *mpool,
                                     size_t alignment, size_t size)
 {
-  FAR struct mempool_s *end = mpool->pools + mpool->npools;
+  FAR struct mempool_s *end;
   FAR struct mempool_s *pool;
 
   DEBUGASSERT((alignment & (alignment - 1)) == 0);
 
-  pool = mempool_multiple_find(mpool, size + alignment + 2 * SIZEOF_HEAD);
+  pool = mempool_multiple_find(mpool, size + alignment);
   if (pool == NULL)
     {
       return NULL;
     }
 
+  end = mpool->pools + mpool->npools;
   do
     {
       FAR char *blk = mempool_alloc(pool);
       if (blk != NULL)
         {
-          FAR char *mem;
-
-          mem = blk + 2 * SIZEOF_HEAD;
-          mem = (FAR char *)(((uintptr_t)mem + alignment - 1) &
-                             ~(alignment - 1));
-          *(FAR uintptr_t *)(mem - SIZEOF_HEAD) =
-                                                 (uintptr_t)pool | ALIGN_BIT;
-          *(FAR size_t *)(mem - 2 * SIZEOF_HEAD) = mem - blk;
-          return mem;
+          return (FAR void *)ALIGN_UP(blk, alignment);
         }
     }
   while (++pool < end);
@@ -495,6 +643,7 @@ void mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
       DEBUGVERIFY(mempool_deinit(mpool->pools + i));
     }
 
+  mpool->free(mpool->arg, mpool->dict);
   mpool->free(mpool->arg, mpool->pools);
   mpool->free(mpool->arg, mpool);
 }
diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h
index 3b1d60d3a5..958b3f3008 100644
--- a/mm/mm_heap/mm.h
+++ b/mm/mm_heap/mm.h
@@ -134,11 +134,6 @@
 
 #define SIZEOF_MM_FREENODE sizeof(struct mm_freenode_s)
 
-#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-#  define MM_IS_FROM_MEMPOOL(mem) \
-  ((*((FAR mmsize_t *)mem - 1) & MM_ALLOC_BIT) == 0)
-#endif
-
 /****************************************************************************
  * Public Types
  ****************************************************************************/
diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c
index ba6d06269b..cef44775a2 100644
--- a/mm/mm_heap/mm_free.c
+++ b/mm/mm_heap/mm_free.c
@@ -83,9 +83,8 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  if (MM_IS_FROM_MEMPOOL(mem))
+  if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
     {
-      mempool_multiple_free(heap->mm_mpool, mem);
       return;
     }
 #endif
diff --git a/mm/mm_heap/mm_malloc_size.c b/mm/mm_heap/mm_malloc_size.c
index 987635780e..96eb27c4e6 100644
--- a/mm/mm_heap/mm_malloc_size.c
+++ b/mm/mm_heap/mm_malloc_size.c
@@ -38,6 +38,14 @@
 size_t mm_malloc_size(FAR struct mm_heap_s *heap, FAR void *mem)
 {
   FAR struct mm_freenode_s *node;
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  ssize_t size = mempool_multiple_alloc_size(heap->mm_mpool, mem);
+
+  if (size >= 0)
+    {
+      return size;
+    }
+#endif
 
   /* Protect against attempts to query a NULL reference */
 
@@ -46,13 +54,6 @@ size_t mm_malloc_size(FAR struct mm_heap_s *heap, FAR void *mem)
       return 0;
     }
 
-#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  if (MM_IS_FROM_MEMPOOL(mem))
-    {
-      return mempool_multiple_alloc_size(heap->mm_mpool, mem);
-    }
-#endif
-
   /* Map the memory chunk into a free node */
 
   node = (FAR struct mm_freenode_s *)((FAR char *)mem - SIZEOF_MM_ALLOCNODE);
diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c
index b423c190df..85239783b4 100644
--- a/mm/mm_heap/mm_realloc.c
+++ b/mm/mm_heap/mm_realloc.c
@@ -95,30 +95,18 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  if (MM_IS_FROM_MEMPOOL(oldmem))
+  newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
+  if (newmem != NULL)
     {
-      newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
-      if (newmem != NULL)
-        {
-          return newmem;
-        }
-
-      newmem = mm_malloc(heap, size);
-      if (newmem != NULL)
-        {
-          memcpy(newmem, oldmem,
-                 mempool_multiple_alloc_size(heap->mm_mpool, oldmem));
-          mempool_multiple_free(heap->mm_mpool, oldmem);
-        }
-
       return newmem;
     }
-  else
+  else if (size <= CONFIG_MM_HEAP_MEMPOOL_THRESHOLD ||
+           mempool_multiple_alloc_size(heap->mm_mpool, oldmem) >= 0)
     {
-      newmem = mempool_multiple_alloc(heap->mm_mpool, size);
+      newmem = mm_malloc(heap, size);
       if (newmem != NULL)
         {
-          memcpy(newmem, oldmem, MIN(size, mm_malloc_size(heap, oldmem)));
+          memcpy(newmem, oldmem, size);
           mm_free(heap, oldmem);
           return newmem;
         }
diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c
index 6b90456148..0bd36e20dc 100644
--- a/mm/tlsf/mm_tlsf.c
+++ b/mm/tlsf/mm_tlsf.c
@@ -56,12 +56,6 @@
 
 #define MIN(x, y) ((x) < (y) ? (x) : (y))
 
-#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-#  define MM_MPOOL_BIT (1 << 0)
-#  define MM_IS_FROM_MEMPOOL(mem) \
-          ((*((FAR size_t *)(mem) - 1)) & MM_MPOOL_BIT) == 0
-#endif
-
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
 #  define MEMPOOL_NPOOLS (CONFIG_MM_HEAP_MEMPOOL_THRESHOLD / tlsf_align_size())
 #endif
@@ -654,9 +648,8 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  if (MM_IS_FROM_MEMPOOL(mem))
+  if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
     {
-      mempool_multiple_free(heap->mm_mpool, mem);
       return;
     }
 #endif
@@ -954,9 +947,10 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid)
 size_t mm_malloc_size(FAR struct mm_heap_s *heap, FAR void *mem)
 {
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  if (MM_IS_FROM_MEMPOOL(mem))
+  ssize_t size = mempool_multiple_alloc_size(heap->mm_mpool, mem);
+  if (size >= 0)
     {
-      return mempool_multiple_alloc_size(heap->mm_mpool, mem);
+      return size;
     }
 #endif
 
@@ -1113,29 +1107,18 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  if (MM_IS_FROM_MEMPOOL(oldmem))
+  newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
+  if (newmem != NULL)
     {
-      newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
-      if (newmem != NULL)
-        {
-          return newmem;
-        }
-
-      newmem = mm_malloc(heap, size);
-      if (newmem != NULL)
-        {
-          memcpy(newmem, oldmem, size);
-          mempool_multiple_free(heap->mm_mpool, oldmem);
-        }
-
       return newmem;
     }
-  else
+  else if (size <= CONFIG_MM_HEAP_MEMPOOL_THRESHOLD ||
+           mempool_multiple_alloc_size(heap->mm_mpool, oldmem) >= 0)
     {
-      newmem = mempool_multiple_alloc(heap->mm_mpool, size);
-      if (newmem != NULL)
+      newmem = mm_malloc(heap, size);
+      if (newmem != 0)
         {
-          memcpy(newmem, oldmem, MIN(size, mm_malloc_size(heap, oldmem)));
+          memcpy(newmem, oldmem, size);
           mm_free(heap, oldmem);
           return newmem;
         }


[nuttx] 04/06: mempool:use single queue insdie of list

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 49ffd99eafadfba2122958642d2c9ca9c187e491
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Sat Nov 26 00:22:12 2022 +0800

    mempool:use single queue insdie of list
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
    Signed-off-by: dongjiuzhu1 <do...@xiaomi.com>
---
 include/nuttx/mm/mempool.h    |  16 +--
 mm/mempool/mempool.c          | 260 +++++++++++++++++++++++++-----------------
 mm/mempool/mempool_multiple.c |  17 +--
 3 files changed, 166 insertions(+), 127 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 0309c4224b..ca9fb7b71a 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -29,6 +29,7 @@
 #include <sys/types.h>
 
 #include <nuttx/list.h>
+#include <nuttx/queue.h>
 #include <nuttx/fs/procfs.h>
 #include <nuttx/spinlock.h>
 #include <nuttx/semaphore.h>
@@ -74,13 +75,15 @@ struct mempool_s
 
   /* Private data for memory pool */
 
-  struct list_node list;    /* The free block list in normal mempool */
-  struct list_node ilist;   /* The free block list in interrupt mempool */
-  struct list_node elist;   /* The expand block list for normal mempool */
+  FAR char  *ibase;   /* The inerrupt mempool base pointer */
+  sq_queue_t queue;   /* The free block queue in normal mempool */
+  sq_queue_t iqueue;  /* The free block queue in interrupt mempool */
+  sq_queue_t equeue;  /* The expand block queue for normal mempool */
 #if CONFIG_MM_BACKTRACE >= 0
   struct list_node alist;   /* The used block list in mempool */
+#else
+  size_t           nalloc;  /* The number of used block in mempool */
 #endif
-  size_t           nused;   /* 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 */
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
@@ -433,12 +436,9 @@ void mempool_multiple_memdump(FAR struct mempool_multiple_s *mpool,
  * Input Parameters:
  *   mpool - The handle of multiple memory pool to be used.
  *
- * Returned Value:
- *   Zero on success; A negated errno value is returned on any failure.
- *
  ****************************************************************************/
 
-int mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool);
+void mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool);
 
 /****************************************************************************
  * Name: mempool_multiple_info_task
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index 86add5fc07..4cd9c77184 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -61,14 +61,40 @@ struct mempool_backtrace_s
  * Private Functions
  ****************************************************************************/
 
-static inline void mempool_add_list(FAR struct list_node *list,
-                                    FAR void *base, size_t nblks,
-                                    size_t blocksize)
+static inline FAR sq_entry_t *mempool_remove_queue(FAR sq_queue_t *queue)
+{
+  if (!sq_empty(queue))
+    {
+      FAR sq_entry_t *entry = queue->head;
+
+      queue->head = entry->flink;
+      return entry;
+    }
+  else
+    {
+      return NULL;
+    }
+}
+
+static inline size_t mempool_queue_lenth(FAR sq_queue_t *queue)
+{
+  FAR sq_entry_t *node;
+  size_t count;
+
+  for (node = queue->head, count = 0;
+       node != NULL;
+       node = node->flink, count++);
+
+  return count;
+}
+
+static inline void mempool_add_queue(FAR sq_queue_t *queue,
+                                     FAR char *base, size_t nblks,
+                                     size_t blocksize)
 {
   while (nblks-- > 0)
     {
-      list_add_head(list, ((FAR struct list_node *)((FAR char *)base +
-                                                    blocksize * nblks)));
+      sq_addfirst((FAR sq_entry_t *)(base + blocksize * nblks), queue);
     }
 }
 
@@ -144,48 +170,63 @@ static inline void mempool_add_backtrace(FAR struct mempool_s *pool,
 int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
 {
 #if CONFIG_MM_BACKTRACE >= 0
-  size_t blocksize = pool->blocksize + sizeof(struct mempool_backtrace_s);
+  size_t blocksize = ALIGN_UP(pool->blocksize +
+                              sizeof(struct mempool_backtrace_s),
+                              pool->blocksize);
 #else
   size_t blocksize = pool->blocksize;
 #endif
-  size_t ninterrupt;
-  size_t ninitial;
-  size_t count;
 
-  DEBUGASSERT(pool->blocksize != 0);
-
-  pool->nused = 0;
-  list_initialize(&pool->list);
-  list_initialize(&pool->ilist);
-  list_initialize(&pool->elist);
+  sq_init(&pool->queue);
+  sq_init(&pool->iqueue);
+  sq_init(&pool->equeue);
 
 #if CONFIG_MM_BACKTRACE >= 0
   list_initialize(&pool->alist);
+#else
+  pool->nalloc = 0;
 #endif
 
-  blocksize = ALIGN_UP(blocksize, pool->blocksize);
-  ninitial = pool->initialsize / blocksize;
-  ninterrupt = pool->interruptsize / blocksize;
-  count = ninitial + ninterrupt;
-  if (count != 0)
+  if (pool->interruptsize > sizeof(sq_entry_t))
     {
+      size_t ninterrupt = (pool->interruptsize - sizeof(sq_entry_t)) /
+                          blocksize;
+      size_t size = ninterrupt * blocksize + sizeof(sq_entry_t);
+
+      pool->ibase = mempool_malloc(pool, size);
+      if (pool->ibase == NULL)
+        {
+          return -ENOMEM;
+        }
+
+      mempool_add_queue(&pool->iqueue, pool->ibase, ninterrupt, blocksize);
+      kasan_poison(pool->ibase, size);
+    }
+  else
+    {
+      pool->ibase = NULL;
+    }
+
+  if (pool->initialsize > sizeof(sq_entry_t))
+    {
+      size_t ninitial = (pool->initialsize - sizeof(sq_entry_t)) / blocksize;
+      size_t size = ninitial * blocksize + sizeof(sq_entry_t);
       FAR char *base;
 
-      base = mempool_malloc(pool, blocksize * count +
-                            sizeof(struct list_node));
+      base = mempool_malloc(pool, size);
       if (base == NULL)
         {
+          mempool_free(pool, pool->ibase);
           return -ENOMEM;
         }
 
-      mempool_add_list(&pool->ilist, base, ninterrupt, blocksize);
-      mempool_add_list(&pool->list, base + ninterrupt * blocksize,
-                       ninitial, blocksize);
-      list_add_head(&pool->elist, (FAR struct list_node *)
-                                  (base + count * blocksize));
-      kasan_poison(base, blocksize * count);
+      mempool_add_queue(&pool->queue, base, ninitial, blocksize);
+      sq_addlast((FAR sq_entry_t *)(base + ninitial * blocksize),
+                  &pool->equeue);
+      kasan_poison(base, size);
     }
 
+  pool->lock = 0;
   if (pool->wait && pool->expandsize == 0)
     {
       nxsem_init(&pool->waitsem, 0, 0);
@@ -220,17 +261,17 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
 
 FAR void *mempool_alloc(FAR struct mempool_s *pool)
 {
-  FAR struct list_node *blk;
+  FAR sq_entry_t *blk;
   irqstate_t flags;
 
 retry:
   flags = spin_lock_irqsave(&pool->lock);
-  blk = list_remove_head(&pool->list);
+  blk = mempool_remove_queue(&pool->queue);
   if (blk == NULL)
     {
       if (up_interrupt_context())
         {
-          blk = list_remove_head(&pool->ilist);
+          blk = mempool_remove_queue(&pool->iqueue);
           if (blk == NULL)
             {
               goto out_with_lock;
@@ -239,30 +280,31 @@ retry:
       else
         {
           spin_unlock_irqrestore(&pool->lock, flags);
-          if (pool->expandsize != 0)
+          if (pool->expandsize > sizeof(sq_entry_t))
             {
 #if CONFIG_MM_BACKTRACE >= 0
-              size_t blocksize = pool->blocksize +
-                                 sizeof(struct mempool_backtrace_s);
+              size_t blocksize = ALIGN_UP(pool->blocksize +
+                                 sizeof(struct mempool_backtrace_s),
+                                 pool->blocksize);
 #else
               size_t blocksize = pool->blocksize;
 #endif
-              size_t nexpand;
+              size_t nexpand = (pool->expandsize - sizeof(sq_entry_t)) /
+                               blocksize;
+              size_t size = nexpand * blocksize + sizeof(sq_entry_t);
+              FAR char *base = mempool_malloc(pool, size);
 
-              blocksize = ALIGN_UP(blocksize, pool->blocksize);
-              nexpand = pool->expandsize / blocksize;
-              blk = mempool_malloc(pool, blocksize * nexpand + sizeof(*blk));
-              if (blk == NULL)
+              if (base == NULL)
                 {
                   return NULL;
                 }
 
-              kasan_poison(blk, blocksize * nexpand);
+              kasan_poison(base, size);
               flags = spin_lock_irqsave(&pool->lock);
-              mempool_add_list(&pool->list, blk, nexpand, blocksize);
-              list_add_head(&pool->elist, (FAR struct list_node *)
-                            ((FAR char *)blk + nexpand * blocksize));
-              blk = list_remove_head(&pool->list);
+              mempool_add_queue(&pool->queue, base, nexpand, blocksize);
+              sq_addlast((FAR sq_entry_t *)(base + nexpand * blocksize),
+                         &pool->equeue);
+              blk = mempool_remove_queue(&pool->queue);
             }
           else if (!pool->wait ||
                    nxsem_wait_uninterruptible(&pool->waitsem) < 0)
@@ -276,10 +318,11 @@ retry:
         }
     }
 
-  pool->nused++;
 #if CONFIG_MM_BACKTRACE >= 0
   mempool_add_backtrace(pool, (FAR struct mempool_backtrace_s *)
                               ((FAR char *)blk + pool->blocksize));
+#else
+  pool->nalloc++;
 #endif
   kasan_unpoison(blk, pool->blocksize);
 out_with_lock:
@@ -302,44 +345,38 @@ void mempool_free(FAR struct mempool_s *pool, FAR void *blk)
 {
   irqstate_t flags;
 #if CONFIG_MM_BACKTRACE >= 0
-  size_t blocksize = pool->blocksize + sizeof(struct mempool_backtrace_s);
+  size_t blocksize =  ALIGN_UP(pool->blocksize +
+                               sizeof(struct mempool_backtrace_s),
+                               pool->blocksize);
   FAR struct mempool_backtrace_s *buf =
-  (FAR struct mempool_backtrace_s *)((FAR char *)blk + pool->blocksize);
+    (FAR struct mempool_backtrace_s *)((FAR char *)blk + pool->blocksize);
 
   list_delete(&buf->node);
 #else
   size_t blocksize = pool->blocksize;
+
+  pool->nalloc--;
 #endif
 
   flags = spin_lock_irqsave(&pool->lock);
-  if ((pool->blocksize & (pool->blocksize - 1)) == 0)
-    {
-      blocksize = ALIGN_UP(blocksize, pool->blocksize);
-    }
 
-  if (pool->interruptsize != 0)
+  if (pool->interruptsize > blocksize)
     {
-      FAR char *base;
-      size_t ninterrupt;
-
-      base = (FAR char *)(list_peek_head(&pool->elist) + 1);
-      ninterrupt = pool->interruptsize / blocksize;
-      if ((FAR char *)blk >= base &&
-          (FAR char *)blk < base + ninterrupt * blocksize)
+      if ((FAR char *)blk >= pool->ibase &&
+          (FAR char *)blk < pool->ibase + pool->interruptsize - blocksize)
         {
-          list_add_head(&pool->ilist, blk);
+          sq_addfirst(blk, &pool->iqueue);
         }
       else
         {
-          list_add_head(&pool->list, blk);
+          sq_addfirst(blk, &pool->queue);
         }
     }
   else
     {
-      list_add_head(&pool->list, blk);
+      sq_addfirst(blk, &pool->queue);
     }
 
-  pool->nused--;
   kasan_poison(blk, pool->blocksize);
   spin_unlock_irqrestore(&pool->lock, flags);
   if (pool->wait && pool->expandsize == 0)
@@ -375,10 +412,14 @@ int mempool_info(FAR struct mempool_s *pool, FAR struct mempoolinfo_s *info)
   DEBUGASSERT(pool != NULL && info != NULL);
 
   flags = spin_lock_irqsave(&pool->lock);
-  info->ordblks = list_length(&pool->list);
-  info->iordblks = list_length(&pool->ilist);
-  info->aordblks = pool->nused;
-  info->arena = (pool->nused + info->ordblks + info->iordblks) *
+  info->ordblks = mempool_queue_lenth(&pool->queue);
+  info->iordblks = mempool_queue_lenth(&pool->iqueue);
+#if CONFIG_MM_BACKTRACE >= 0
+  info->aordblks = list_length(&pool->alist);
+#else
+  info->aordblks = pool->nalloc;
+#endif
+  info->arena = (info->aordblks + info->ordblks + info->iordblks) *
                 pool->blocksize;
   spin_unlock_irqrestore(&pool->lock, flags);
   info->sizeblks = pool->blocksize;
@@ -407,7 +448,8 @@ int mempool_info_task(FAR struct mempool_s *pool,
   DEBUGASSERT(info);
   if (info->pid == -2)
     {
-      size_t count = list_length(&pool->list);
+      size_t count = mempool_queue_lenth(&pool->queue) +
+                     mempool_queue_lenth(&pool->iqueue);
 
       info->aordblks += count;
       info->uordblks += count * pool->blocksize;
@@ -415,20 +457,10 @@ int mempool_info_task(FAR struct mempool_s *pool,
   else if (info->pid == -1)
     {
 #if CONFIG_MM_BACKTRACE >= 0
-      size_t blocksize = pool->blocksize +
-             sizeof(struct mempool_backtrace_s);
+      size_t count = list_length(&pool->alist);
 #else
-      size_t blocksize = pool->blocksize;
+      size_t count = pool->nalloc;
 #endif
-      size_t count;
-
-      if ((pool->blocksize & (pool->blocksize - 1)) == 0)
-        {
-          blocksize = ALIGN_UP(blocksize, pool->blocksize);
-        }
-
-      count = (pool->initialsize + pool->interruptsize) / blocksize +
-              (list_length(&pool->elist) - 1) - list_length(&pool->list);
 
       info->aordblks += count;
       info->uordblks += count * pool->blocksize;
@@ -437,6 +469,7 @@ int mempool_info_task(FAR struct mempool_s *pool,
   else
     {
       FAR struct mempool_backtrace_s *buf;
+
       list_for_every_entry(&pool->alist, buf, struct mempool_backtrace_s,
                            node)
         {
@@ -475,18 +508,27 @@ void mempool_memdump(FAR struct mempool_s *pool, pid_t pid)
 {
   if (pid == -2)
     {
-      FAR struct list_node *node;
-      list_for_every(&pool->list, node)
+      FAR sq_entry_t *entry;
+
+      sq_for_every(&pool->queue, entry)
         {
           syslog(LOG_INFO, "%12zu%*p\n",
                  pool->blocksize, MM_PTR_FMT_WIDTH,
-                 (FAR char *)node);
+                 (FAR char *)entry);
+        }
+
+      sq_for_every(&pool->iqueue, entry)
+        {
+          syslog(LOG_INFO, "%12zu%*p\n",
+                 pool->blocksize, MM_PTR_FMT_WIDTH,
+                 (FAR char *)entry);
         }
     }
 #if CONFIG_MM_BACKTRACE >= 0
   else
     {
       FAR struct mempool_backtrace_s *buf;
+
       list_for_every_entry(&pool->alist, buf, struct mempool_backtrace_s,
                            node)
         {
@@ -529,44 +571,54 @@ void mempool_memdump(FAR struct mempool_s *pool, pid_t pid)
 int mempool_deinit(FAR struct mempool_s *pool)
 {
 #if CONFIG_MM_BACKTRACE >= 0
-  size_t blocksize = pool->blocksize + sizeof(struct mempool_backtrace_s);
+  size_t blocksize = ALIGN_UP(pool->blocksize +
+                              sizeof(struct mempool_backtrace_s),
+                              pool->blocksize);
 #else
   size_t blocksize = pool->blocksize;
 #endif
-  FAR struct list_node *blk;
-  size_t ninterrupt;
-  size_t ninitial;
-  size_t count;
+  FAR sq_entry_t *blk;
+  size_t count = 0;
 
-  if (pool->nused != 0)
+#if CONFIG_MM_BACKTRACE >= 0
+  if (!list_is_empty(&pool->alist))
+#else
+  if (pool->nalloc != 0)
+#endif
     {
       return -EBUSY;
     }
 
-#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
-  mempool_procfs_unregister(&pool->procfs);
-#endif
-
-  if ((pool->blocksize & (pool->blocksize - 1)) == 0)
+  if (pool->initialsize > sizeof(sq_entry_t))
     {
-      blocksize = ALIGN_UP(blocksize, pool->blocksize);
+      count = (pool->initialsize - sizeof(sq_entry_t)) / blocksize;
     }
 
-  ninitial = pool->initialsize / blocksize;
-  ninterrupt = pool->interruptsize / blocksize;
-  count = ninitial + ninterrupt;
   if (count == 0)
     {
-      count = pool->expandsize / blocksize;
+      if (pool->expandsize > sizeof(sq_entry_t))
+        {
+          count = (pool->expandsize - sizeof(sq_entry_t)) / blocksize;
+        }
     }
 
-  while ((blk = list_remove_head(&pool->elist)) != NULL)
+#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
+  mempool_procfs_unregister(&pool->procfs);
+#endif
+
+  while ((blk = mempool_remove_queue(&pool->equeue)) != NULL)
     {
-      blk = (FAR struct list_node *)((FAR char *)blk -
-                                     count * blocksize);
-      kasan_unpoison(blk, blocksize);
+      blk = (FAR sq_entry_t *)((FAR char *)blk - count * blocksize);
       mempool_mfree(pool, blk);
-      count = pool->expandsize / blocksize;
+      if (pool->expandsize > sizeof(sq_entry_t))
+        {
+          count = (pool->expandsize - sizeof(sq_entry_t)) / blocksize;
+        }
+    }
+
+  if (pool->ibase)
+    {
+      mempool_mfree(pool, pool->ibase);
     }
 
   if (pool->wait && pool->expandsize == 0)
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index b2233b3e24..c38c6e68ec 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -415,12 +415,9 @@ void mempool_multiple_memdump(FAR struct mempool_multiple_s *mpool,
  * Input Parameters:
  *   mpool - The handle of multiple memory pool to be used.
  *
- * Returned Value:
- *   Zero on success; A negated errno value is returned on any failure.
- *
  ****************************************************************************/
 
-int mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
+void mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
 {
   size_t i;
 
@@ -428,16 +425,6 @@ int mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
 
   for (i = 0; i < mpool->npools; i++)
     {
-      if (mpool->pools[i].nused != 0)
-        {
-          return -EBUSY;
-        }
+      DEBUGVERIFY(mempool_deinit(mpool->pools + i));
     }
-
-  for (i = 0; i < mpool->npools; i++)
-    {
-      mempool_deinit(mpool->pools + i);
-    }
-
-  return 0;
 }


[nuttx] 02/06: mempool:remove multiple fixed api

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit e7d02ffac21d5f6a2c59c73f0eac313af7b89043
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Thu Nov 24 12:14:52 2022 +0800

    mempool:remove multiple fixed api
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h    | 58 --------------------------
 mm/mempool/mempool_multiple.c | 95 -------------------------------------------
 2 files changed, 153 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 22366157c3..b85795207d 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -423,64 +423,6 @@ FAR void *mempool_multiple_memalign(FAR struct mempool_multiple_s *mpool,
 void mempool_multiple_memdump(FAR struct mempool_multiple_s *mpool,
                               pid_t pid);
 
-/****************************************************************************
- * Name: mempool_multiple_fixed_alloc
- *
- * Description:
- *   Allocate an block from specific multiple memory pool.
- *   If the mempool of the corresponding size doesn't have free block,
- *   then wait until free happened or return NULL.
- *
- * Input Parameters:
- *   mpool - The handle of multiple memory pool to be used.
- *   size  - The size of alloc blk.
- *
- * Returned Value:
- *   The pointer to the allocated block on success; NULL on any failure.
- *
- ****************************************************************************/
-
-FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool,
-                                       size_t size);
-
-/****************************************************************************
- * Name: mempool_multiple_fixed_realloc
- *
- * Description:
- *   Change the size of the block memory pointed to by oldblk to size bytes.
- *
- * Input Parameters:
- *   mpool   - The handle of multiple memory pool to be used.
- *   oldblk  - The pointer to change the size of the block memory.
- *   oldsize - The size of block memory to oldblk.
- *   size    - The size of alloc blk.
- *
- * Returned Value:
- *   The pointer to the allocated block on success; NULL on any failure.
- *
- ****************************************************************************/
-
-FAR void *
-mempool_multiple_fixed_realloc(FAR struct mempool_multiple_s *mpool,
-                               FAR void *oldblk, size_t oldsize,
-                               size_t size);
-
-/****************************************************************************
- * Name: mempool_multiple_fixed_free
- *
- * Description:
- *   Release an memory block to the multiple mempry pool. The blk must have
- *   been returned by a previous call to mempool_multiple_fixed_alloc.
- *
- * Input Parameters:
- *   mpool - The handle of multiple memory pool to be used.
- *   blk   - The pointer of memory block.
- *   size  - The size of alloc blk.
- ****************************************************************************/
-
-void mempool_multiple_fixed_free(FAR struct mempool_multiple_s *mpool,
-                                 FAR void *blk, size_t size);
-
 /****************************************************************************
  * Name: mempool_multiple_deinit
  *
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index ef799b7da3..b2233b3e24 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -406,101 +406,6 @@ void mempool_multiple_memdump(FAR struct mempool_multiple_s *mpool,
     }
 }
 
-/****************************************************************************
- * Name: mempool_multiple_fixed_alloc
- *
- * Description:
- *   Allocate an block from specific multiple memory pool.
- *   If the mempool of the corresponding size doesn't have free block,
- *   then wait until free happened or return NULL.
- *
- * Input Parameters:
- *   mpool - The handle of multiple memory pool to be used.
- *   size  - The size of alloc blk.
- *
- * Returned Value:
- *   The pointer to the allocated block on success; NULL on any failure.
- *
- ****************************************************************************/
-
-FAR void *mempool_multiple_fixed_alloc(FAR struct mempool_multiple_s *mpool,
-                                       size_t size)
-{
-  FAR struct mempool_s *pool;
-
-  pool = mempool_multiple_find(mpool, size);
-  if (pool == NULL)
-    {
-      return NULL;
-    }
-
-  return mempool_alloc(pool);
-}
-
-/****************************************************************************
- * Name: mempool_multiple_fixed_realloc
- *
- * Description:
- *   Change the size of the block memory pointed to by oldblk to size bytes.
- *
- * Input Parameters:
- *   mpool   - The handle of multiple memory pool to be used.
- *   oldblk  - The pointer to change the size of the block memory.
- *   oldsize - The size of block memory to oldblk.
- *   size    - The size of alloc blk.
- *
- * Returned Value:
- *   The pointer to the allocated block on success; NULL on any failure.
- *
- ****************************************************************************/
-
-FAR void *
-mempool_multiple_fixed_realloc(FAR struct mempool_multiple_s *mpool,
-                               FAR void *oldblk, size_t oldsize, size_t size)
-{
-  FAR void *blk;
-
-  if (size < 1)
-    {
-      mempool_multiple_fixed_free(mpool, oldblk, oldsize);
-      return NULL;
-    }
-
-  blk = mempool_multiple_fixed_alloc(mpool, size);
-  if (blk != NULL && oldblk != NULL)
-    {
-      memcpy(blk, oldblk, MIN(oldsize, size));
-      mempool_multiple_fixed_free(mpool, oldblk, oldsize);
-    }
-
-  return blk;
-}
-
-/****************************************************************************
- * Name: mempool_multiple_fixed_free
- *
- * Description:
- *   Release an memory block to the multiple mempry pool. The blk must have
- *   been returned by a previous call to mempool_multiple_fixed_alloc.
- *
- * Input Parameters:
- *   mpool - The handle of multiple memory pool to be used.
- *   blk   - The pointer of memory block.
- *   size  - The size of alloc blk.
- ****************************************************************************/
-
-void mempool_multiple_fixed_free(FAR struct mempool_multiple_s *mpool,
-                                 FAR void *blk, size_t size)
-{
-  FAR struct mempool_s *pool;
-
-  DEBUGASSERT(mpool != NULL && blk != NULL);
-
-  pool = mempool_multiple_find(mpool, size);
-  DEBUGASSERT(pool != NULL);
-  mempool_free(pool, blk);
-}
-
 /****************************************************************************
  * Name: mempool_multiple_deinit
  *


[nuttx] 05/06: mempool:change mempool_multiple way of initialization

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit 6b530fceae178276fc7356b6b1df4d8438955984
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Sat Nov 26 23:44:38 2022 +0800

    mempool:change mempool_multiple way of initialization
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h    |  48 ++++++++---------
 mm/mempool/mempool.c          |  35 ++-----------
 mm/mempool/mempool_multiple.c | 118 +++++++++++++++++++++++++++++++++---------
 mm/mm_heap/mm.h               |   4 +-
 mm/mm_heap/mm_free.c          |   2 +-
 mm/mm_heap/mm_initialize.c    |  43 ++++++++++-----
 mm/mm_heap/mm_mallinfo.c      |   2 +-
 mm/mm_heap/mm_malloc.c        |   2 +-
 mm/mm_heap/mm_malloc_size.c   |   2 +-
 mm/mm_heap/mm_memalign.c      |   2 +-
 mm/mm_heap/mm_memdump.c       |   2 +-
 mm/mm_heap/mm_realloc.c       |   8 +--
 mm/tlsf/mm_tlsf.c             |  57 ++++++++++----------
 13 files changed, 194 insertions(+), 131 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index ca9fb7b71a..7b991292f1 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -39,11 +39,16 @@
  ****************************************************************************/
 
 struct mempool_s;
-typedef CODE void *(*mempool_alloc_t)(FAR struct mempool_s *pool,
-                                      size_t size);
+typedef CODE FAR void *(*mempool_alloc_t)(FAR struct mempool_s *pool,
+                                          size_t size);
 typedef CODE void (*mempool_free_t)(FAR struct mempool_s *pool,
                                     FAR void *addr);
 
+typedef CODE FAR void *(*mempool_multiple_alloc_t)(FAR void *args,
+                                                   size_t alignment,
+                                                   size_t size);
+typedef CODE void (*mempool_multiple_free_t)(FAR void *args, FAR void *addr);
+
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMPOOL)
 struct mempool_procfs_entry_s
 {
@@ -91,23 +96,6 @@ struct mempool_s
 #endif
 };
 
-struct mempool_multiple_s
-{
-  FAR struct mempool_s *pools;  /* The memory pool array */
-  size_t                npools; /* The number of memory pool array elements */
-
-  /* Private data for multiple memory pool */
-
-  /* This delta describes the relationship between the block size of each
-   * mempool in multiple mempool by user initialized. It is automatically
-   * detected by the mempool_multiple_init function. If the delta is not
-   * equal to 0, the block size of the pool in the multiple mempool is an
-   * arithmetic progressions, otherwise it is an increasing progressions.
-   */
-
-  size_t                delta;
-};
-
 struct mempoolinfo_s
 {
   unsigned long arena;    /* This is the total size of mempool */
@@ -293,16 +281,28 @@ void mempool_procfs_unregister(FAR struct mempool_procfs_entry_s *entry);
  *   relationship between the each block size of mempool in multiple mempool.
  *
  * Input Parameters:
- *   name  - The name of memory pool.
- *   mpool - The handle of the multiple memory pool to be used.
+ *   name        - The name of memory pool.
+ *   poolsize    - The block size array for pools in multiples pool.
+ *   npools      - How many pools in multiples pool.
+ *   alloc       - The alloc memory function for multiples pool.
+ *   free        - The free memory function for multiples pool.
+ *   arg         - The alloc & free memory fuctions used arg.
+ *   expandsize  - The expend mempry for all pools in multiples pool.
  *
  * Returned Value:
- *   Zero on success; A negated errno value is returned on any failure.
+ *   Return an initialized multiple pool pointer on success,
+ *   otherwise NULL is returned.
  *
  ****************************************************************************/
 
-int mempool_multiple_init(FAR struct mempool_multiple_s *mpool,
-                          FAR const char *name);
+struct mempool_multiple_s;
+
+FAR struct mempool_multiple_s *
+mempool_multiple_init(FAR const char *name,
+                      FAR size_t *poolsize, size_t npools,
+                      mempool_multiple_alloc_t alloc,
+                      mempool_multiple_free_t free,
+                      FAR void *arg, size_t expandsize);
 
 /****************************************************************************
  * Name: mempool_multiple_alloc
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index 4cd9c77184..c64ab8dc2f 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -98,31 +98,6 @@ static inline void mempool_add_queue(FAR sq_queue_t *queue,
     }
 }
 
-static inline FAR void *mempool_malloc(FAR struct mempool_s *pool,
-                                       size_t size)
-{
-  if (pool->alloc != NULL)
-    {
-      return pool->alloc(pool, size);
-    }
-  else
-    {
-      return kmm_malloc(size);
-    }
-}
-
-static inline void mempool_mfree(FAR struct mempool_s *pool, FAR void *addr)
-{
-  if (pool->free != NULL)
-    {
-      return pool->free(pool, addr);
-    }
-  else
-    {
-      return kmm_free(addr);
-    }
-}
-
 #if CONFIG_MM_BACKTRACE >= 0
 static inline void mempool_add_backtrace(FAR struct mempool_s *pool,
                                          FAR struct mempool_backtrace_s *buf)
@@ -193,7 +168,7 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
                           blocksize;
       size_t size = ninterrupt * blocksize + sizeof(sq_entry_t);
 
-      pool->ibase = mempool_malloc(pool, size);
+      pool->ibase = pool->alloc(pool, size);
       if (pool->ibase == NULL)
         {
           return -ENOMEM;
@@ -213,7 +188,7 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
       size_t size = ninitial * blocksize + sizeof(sq_entry_t);
       FAR char *base;
 
-      base = mempool_malloc(pool, size);
+      base = pool->alloc(pool, size);
       if (base == NULL)
         {
           mempool_free(pool, pool->ibase);
@@ -292,7 +267,7 @@ retry:
               size_t nexpand = (pool->expandsize - sizeof(sq_entry_t)) /
                                blocksize;
               size_t size = nexpand * blocksize + sizeof(sq_entry_t);
-              FAR char *base = mempool_malloc(pool, size);
+              FAR char *base = pool->alloc(pool, size);
 
               if (base == NULL)
                 {
@@ -609,7 +584,7 @@ int mempool_deinit(FAR struct mempool_s *pool)
   while ((blk = mempool_remove_queue(&pool->equeue)) != NULL)
     {
       blk = (FAR sq_entry_t *)((FAR char *)blk - count * blocksize);
-      mempool_mfree(pool, blk);
+      pool->free(pool, blk);
       if (pool->expandsize > sizeof(sq_entry_t))
         {
           count = (pool->expandsize - sizeof(sq_entry_t)) / blocksize;
@@ -618,7 +593,7 @@ int mempool_deinit(FAR struct mempool_s *pool)
 
   if (pool->ibase)
     {
-      mempool_mfree(pool, pool->ibase);
+      pool->free(pool, pool->ibase);
     }
 
   if (pool->wait && pool->expandsize == 0)
diff --git a/mm/mempool/mempool_multiple.c b/mm/mempool/mempool_multiple.c
index c38c6e68ec..843964d0c8 100644
--- a/mm/mempool/mempool_multiple.c
+++ b/mm/mempool/mempool_multiple.c
@@ -34,6 +34,27 @@
 #define MIN(a, b)   ((a) < (b) ? (a) : (b))
 #define ALIGN_BIT   (1 << 1)
 
+struct mempool_multiple_s
+{
+  FAR struct mempool_s *   pools;      /* The memory pool array */
+  size_t                   npools;     /* The number of memory pool array elements */
+
+  FAR void                *arg;     /* This pointer is used to store the user's
+                                     * private data
+                                     */
+  mempool_multiple_alloc_t alloc;   /* The alloc function for mempool */
+  mempool_multiple_free_t  free;    /* The free function for mempool */
+
+  /* This delta describes the relationship between the block size of each
+   * mempool in multiple mempool by user initialized. It is automatically
+   * detected by the mempool_multiple_init function. If the delta is not
+   * equal to 0, the block size of the pool in the multiple mempool is an
+   * arithmetic progressions, otherwise it is an increasing progressions.
+   */
+
+  size_t                   delta;
+};
+
 /****************************************************************************
  * Private Functions
  ****************************************************************************/
@@ -41,10 +62,16 @@
 static inline struct mempool_s *
 mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
 {
-  size_t right = mpool->npools;
+  size_t right;
   size_t left = 0;
   size_t mid;
 
+  if (mpool == NULL)
+    {
+      return NULL;
+    }
+
+  right = mpool->npools;
   if (mpool->delta != 0)
     {
       left = mpool->pools[0].blocksize;
@@ -91,50 +118,90 @@ mempool_multiple_find(FAR struct mempool_multiple_s *mpool, size_t size)
  *   relationship between the each block size of mempool in multiple mempool.
  *
  * Input Parameters:
- *   name  - The name of memory pool.
- *   mpool - The handle of the multiple memory pool to be used.
+ *   name        - The name of memory pool.
+ *   poolsize    - The block size array for pools in multiples pool.
+ *   npools      - How many pools in multiples pool.
+ *   alloc       - The alloc memory function for multiples pool.
+ *   free        - The free memory function for multiples pool.
+ *   arg         - The alloc & free memory fuctions used arg.
+ *   expandsize  - The expend mempry for all pools in multiples pool.
  *
  * Returned Value:
- *   Zero on success; A negated errno value is returned on any failure.
+ *   Return an initialized multiple pool pointer on success,
+ *   otherwise NULL is returned.
  *
  ****************************************************************************/
 
-int mempool_multiple_init(FAR struct mempool_multiple_s *mpool,
-                          FAR const char *name)
+FAR struct mempool_multiple_s *
+mempool_multiple_init(FAR const char *name,
+                      FAR size_t *poolsize, size_t npools,
+                      mempool_multiple_alloc_t alloc,
+                      mempool_multiple_free_t free,
+                      FAR void *arg, size_t expandsize)
 {
-  size_t i;
+  FAR struct mempool_multiple_s *mpool;
+  FAR struct mempool_s *pools;
+  int ret;
+  int i;
 
-  DEBUGASSERT(mpool != NULL && mpool->pools != NULL);
-
-  mpool->delta = 0;
-  for (i = 1; i < mpool->npools; i++)
+  mpool = alloc(arg, sizeof(uintptr_t), sizeof(struct mempool_multiple_s));
+  if (mpool == NULL)
     {
-      size_t delta = mpool->pools[i].blocksize -
-                     mpool->pools[i - 1].blocksize;
-      if (mpool->delta != 0 && delta != mpool->delta)
-        {
-          mpool->delta = 0;
-          break;
-        }
+      return NULL;
+    }
 
-      mpool->delta = delta;
+  pools = alloc(arg, sizeof(uintptr_t),
+                npools * sizeof(FAR struct mempool_s));
+  if (pools == NULL)
+    {
+      goto err_with_mpool;
     }
 
-  for (i = 0; i < mpool->npools; i++)
+  mpool->pools = pools;
+  mpool->npools = npools;
+  mpool->alloc = alloc;
+  mpool->free = free;
+  mpool->arg = arg;
+
+  for (i = 0; i < npools; i++)
     {
-      int ret = mempool_init(mpool->pools + i, name);
+      pools[i].blocksize = poolsize[i];
+      pools[i].expandsize = expandsize;
+      pools[i].initialsize = 0;
+      pools[i].interruptsize = 0;
+      ret = mempool_init(pools + i, name);
       if (ret < 0)
         {
           while (--i >= 0)
             {
-              mempool_deinit(mpool->pools + i);
+              mempool_deinit(pools + i);
             }
 
-          return ret;
+          goto err_with_pools;
+        }
+
+      if (i + 1 != npools)
+        {
+          size_t delta = pools[i + 1].blocksize - pools[i].blocksize;
+
+          if (i == 0)
+            {
+              mpool->delta = delta;
+            }
+          else if (delta != mpool->delta)
+            {
+              mpool->delta = 0;
+            }
         }
     }
 
-  return 0;
+  return mpool;
+
+err_with_pools:
+  free(arg, pools);
+err_with_mpool:
+  free(arg, mpool);
+  return NULL;
 }
 
 /****************************************************************************
@@ -427,4 +494,7 @@ void mempool_multiple_deinit(FAR struct mempool_multiple_s *mpool)
     {
       DEBUGVERIFY(mempool_deinit(mpool->pools + i));
     }
+
+  mpool->free(mpool->arg, mpool->pools);
+  mpool->free(mpool->arg, mpool);
 }
diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h
index 952f2ed459..3b1d60d3a5 100644
--- a/mm/mm_heap/mm.h
+++ b/mm/mm_heap/mm.h
@@ -234,9 +234,7 @@ struct mm_heap_s
   /* The is a multiple mempool of the heap */
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  struct mempool_multiple_s mm_mpool;
-  struct mempool_s mm_pools[CONFIG_MM_HEAP_MEMPOOL_THRESHOLD /
-                            sizeof(uintptr_t)];
+  FAR struct mempool_multiple_s *mm_mpool;
 #endif
 
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c
index 6380afc2d6..ba6d06269b 100644
--- a/mm/mm_heap/mm_free.c
+++ b/mm/mm_heap/mm_free.c
@@ -85,7 +85,7 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
   if (MM_IS_FROM_MEMPOOL(mem))
     {
-      mempool_multiple_free(&heap->mm_mpool, mem);
+      mempool_multiple_free(heap->mm_mpool, mem);
       return;
     }
 #endif
diff --git a/mm/mm_heap/mm_initialize.c b/mm/mm_heap/mm_initialize.c
index c0fe89d27b..533b5d8231 100644
--- a/mm/mm_heap/mm_initialize.c
+++ b/mm/mm_heap/mm_initialize.c
@@ -33,6 +33,14 @@
 #include "mm_heap/mm.h"
 #include "kasan/kasan.h"
 
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+#  define MEMPOOL_NPOOLS (CONFIG_MM_HEAP_MEMPOOL_THRESHOLD / MM_MIN_CHUNK)
+#endif
+
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -177,6 +185,9 @@ void mm_addregion(FAR struct mm_heap_s *heap, FAR void *heapstart,
 FAR struct mm_heap_s *mm_initialize(FAR const char *name,
                                     FAR void *heapstart, size_t heapsize)
 {
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  size_t poolsize[MEMPOOL_NPOOLS];
+#endif
   FAR struct mm_heap_s *heap;
   uintptr_t             heap_adj;
   int                   i;
@@ -226,20 +237,6 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
 #  endif
 #endif
 
-  /* Initialize the multiple mempool in heap */
-
-#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  heap->mm_mpool.pools = heap->mm_pools;
-  heap->mm_mpool.npools = sizeof(heap->mm_pools) / sizeof(heap->mm_pools[0]);
-  for (i = 0; i < heap->mm_mpool.npools; i++)
-    {
-      heap->mm_pools[i].blocksize = (i + 1) * sizeof(uintptr_t);
-      heap->mm_pools[i].expandsize = CONFIG_MM_HEAP_MEMPOOL_EXPAND;
-    }
-
-  mempool_multiple_init(&heap->mm_mpool, name);
-#endif
-
   /* Add the initial region of memory to the heap */
 
   mm_addregion(heap, heapstart, heapsize);
@@ -250,6 +247,20 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
 #  endif
 #endif
 
+  /* Initialize the multiple mempool in heap */
+
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  for (i = 0; i < MEMPOOL_NPOOLS; i++)
+    {
+      poolsize[i] = (i + 1) * MM_MIN_CHUNK;
+    }
+
+  heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS,
+                                    (mempool_multiple_alloc_t)mm_memalign,
+                                    (mempool_multiple_free_t)mm_free, heap,
+                                    CONFIG_MM_HEAP_MEMPOOL_EXPAND);
+#endif
+
   return heap;
 }
 
@@ -269,6 +280,10 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
 
 void mm_uninitialize(FAR struct mm_heap_s *heap)
 {
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  mempool_multiple_deinit(heap->mm_mpool);
+#endif
+
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
 #  if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
   procfs_unregister_meminfo(&heap->mm_procfs);
diff --git a/mm/mm_heap/mm_mallinfo.c b/mm/mm_heap/mm_mallinfo.c
index 616983fb9e..0c51df59f3 100644
--- a/mm/mm_heap/mm_mallinfo.c
+++ b/mm/mm_heap/mm_mallinfo.c
@@ -153,7 +153,7 @@ int mm_mallinfo_task(FAR struct mm_heap_s *heap,
   info->aordblks = 0;
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  mempool_multiple_info_task(&heap->mm_mpool, info);
+  mempool_multiple_info_task(heap->mm_mpool, info);
 #endif
 
   mm_foreach(heap, mallinfo_task_handler, info);
diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c
index 6893cbf160..f840f629ea 100644
--- a/mm/mm_heap/mm_malloc.c
+++ b/mm/mm_heap/mm_malloc.c
@@ -121,7 +121,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  ret = mempool_multiple_alloc(&heap->mm_mpool, size);
+  ret = mempool_multiple_alloc(heap->mm_mpool, size);
   if (ret != NULL)
     {
       return ret;
diff --git a/mm/mm_heap/mm_malloc_size.c b/mm/mm_heap/mm_malloc_size.c
index ab6d7f6012..987635780e 100644
--- a/mm/mm_heap/mm_malloc_size.c
+++ b/mm/mm_heap/mm_malloc_size.c
@@ -49,7 +49,7 @@ size_t mm_malloc_size(FAR struct mm_heap_s *heap, FAR void *mem)
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
   if (MM_IS_FROM_MEMPOOL(mem))
     {
-      return mempool_multiple_alloc_size(&heap->mm_mpool, mem);
+      return mempool_multiple_alloc_size(heap->mm_mpool, mem);
     }
 #endif
 
diff --git a/mm/mm_heap/mm_memalign.c b/mm/mm_heap/mm_memalign.c
index 5de5ecd198..a5ae28ce33 100644
--- a/mm/mm_heap/mm_memalign.c
+++ b/mm/mm_heap/mm_memalign.c
@@ -73,7 +73,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  node = mempool_multiple_memalign(&heap->mm_mpool, alignment, size);
+  node = mempool_multiple_memalign(heap->mm_mpool, alignment, size);
   if (node != NULL)
     {
       return node;
diff --git a/mm/mm_heap/mm_memdump.c b/mm/mm_heap/mm_memdump.c
index b9fa2342fc..f6774bdc50 100644
--- a/mm/mm_heap/mm_memdump.c
+++ b/mm/mm_heap/mm_memdump.c
@@ -148,7 +148,7 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid)
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  mempool_multiple_memdump(&heap->mm_mpool, pid);
+  mempool_multiple_memdump(heap->mm_mpool, pid);
 #endif
   mm_foreach(heap, memdump_handler, &pid);
 
diff --git a/mm/mm_heap/mm_realloc.c b/mm/mm_heap/mm_realloc.c
index cb4734dbdd..b423c190df 100644
--- a/mm/mm_heap/mm_realloc.c
+++ b/mm/mm_heap/mm_realloc.c
@@ -97,7 +97,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
   if (MM_IS_FROM_MEMPOOL(oldmem))
     {
-      newmem = mempool_multiple_realloc(&heap->mm_mpool, oldmem, size);
+      newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
       if (newmem != NULL)
         {
           return newmem;
@@ -107,15 +107,15 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
       if (newmem != NULL)
         {
           memcpy(newmem, oldmem,
-                 mempool_multiple_alloc_size(&heap->mm_mpool, oldmem));
-          mempool_multiple_free(&heap->mm_mpool, oldmem);
+                 mempool_multiple_alloc_size(heap->mm_mpool, oldmem));
+          mempool_multiple_free(heap->mm_mpool, oldmem);
         }
 
       return newmem;
     }
   else
     {
-      newmem = mempool_multiple_alloc(&heap->mm_mpool, size);
+      newmem = mempool_multiple_alloc(heap->mm_mpool, size);
       if (newmem != NULL)
         {
           memcpy(newmem, oldmem, MIN(size, mm_malloc_size(heap, oldmem)));
diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c
index 8a59c3333d..6b90456148 100644
--- a/mm/tlsf/mm_tlsf.c
+++ b/mm/tlsf/mm_tlsf.c
@@ -62,6 +62,10 @@
           ((*((FAR size_t *)(mem) - 1)) & MM_MPOOL_BIT) == 0
 #endif
 
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+#  define MEMPOOL_NPOOLS (CONFIG_MM_HEAP_MEMPOOL_THRESHOLD / tlsf_align_size())
+#endif
+
 /****************************************************************************
  * Private Types
  ****************************************************************************/
@@ -97,9 +101,7 @@ struct mm_heap_s
   /* The is a multiple mempool of the heap */
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  struct mempool_multiple_s mm_mpool;
-  struct mempool_s mm_pools[CONFIG_MM_HEAP_MEMPOOL_THRESHOLD /
-                            sizeof(uintptr_t)];
+  FAR struct mempool_multiple_s *mm_mpool;
 #endif
 
   /* Free delay list, for some situation can't do free immdiately */
@@ -654,7 +656,7 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
   if (MM_IS_FROM_MEMPOOL(mem))
     {
-      mempool_multiple_free(&heap->mm_mpool, mem);
+      mempool_multiple_free(heap->mm_mpool, mem);
       return;
     }
 #endif
@@ -757,6 +759,7 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
 {
   FAR struct mm_heap_s *heap;
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  size_t poolsize[MEMPOOL_NPOOLS];
   int i;
 #endif
 
@@ -770,20 +773,6 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
   heapstart += sizeof(struct mm_heap_s);
   heapsize -= sizeof(struct mm_heap_s);
 
-  /* Initialize the multiple mempool in heap */
-
-#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  heap->mm_mpool.pools = heap->mm_pools;
-  heap->mm_mpool.npools = sizeof(heap->mm_pools) / sizeof(heap->mm_pools[0]);
-  for (i = 0; i < heap->mm_mpool.npools; i++)
-    {
-      heap->mm_pools[i].blocksize = (i + 1) * sizeof(uintptr_t);
-      heap->mm_pools[i].expandsize = CONFIG_MM_HEAP_MEMPOOL_EXPAND;
-    }
-
-  mempool_multiple_init(&heap->mm_mpool, name);
-#endif
-
   /* Allocate and create TLSF context */
 
   DEBUGASSERT(heapsize > tlsf_size());
@@ -812,6 +801,18 @@ FAR struct mm_heap_s *mm_initialize(FAR const char *name,
 #endif
 #endif
 
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  for (i = 0; i < MEMPOOL_NPOOLS; i++)
+    {
+      poolsize[i] = (i + 1) * tlsf_align_size();
+    }
+
+  heap->mm_mpool = mempool_multiple_init(name, poolsize, MEMPOOL_NPOOLS,
+                                    (mempool_multiple_alloc_t)mm_memalign,
+                                    (mempool_multiple_free_t)mm_free, heap,
+                                    CONFIG_MM_HEAP_MEMPOOL_EXPAND);
+#endif
+
   return heap;
 }
 
@@ -870,7 +871,7 @@ int mm_mallinfo_task(FAR struct mm_heap_s *heap,
   info->aordblks = 0;
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  mempool_multiple_info_task(&heap->mm_mpool, info);
+  mempool_multiple_info_task(heap->mm_mpool, info);
 #endif
 
 #if CONFIG_MM_REGIONS > 1
@@ -926,7 +927,7 @@ void mm_memdump(FAR struct mm_heap_s *heap, pid_t pid)
     }
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  mempool_multiple_memdump(&heap->mm_mpool, pid);
+  mempool_multiple_memdump(heap->mm_mpool, pid);
 #endif
 
 #if CONFIG_MM_REGIONS > 1
@@ -955,7 +956,7 @@ size_t mm_malloc_size(FAR struct mm_heap_s *heap, FAR void *mem)
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
   if (MM_IS_FROM_MEMPOOL(mem))
     {
-      return mempool_multiple_alloc_size(&heap->mm_mpool, mem);
+      return mempool_multiple_alloc_size(heap->mm_mpool, mem);
     }
 #endif
 
@@ -982,7 +983,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
   FAR void *ret;
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  ret = mempool_multiple_alloc(&heap->mm_mpool, size);
+  ret = mempool_multiple_alloc(heap->mm_mpool, size);
   if (ret != NULL)
     {
       return ret;
@@ -1037,7 +1038,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
   FAR void *ret;
 
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
-  ret = mempool_multiple_memalign(&heap->mm_mpool, alignment, size);
+  ret = mempool_multiple_memalign(heap->mm_mpool, alignment, size);
   if (ret != NULL)
     {
       return ret;
@@ -1114,7 +1115,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
 #if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
   if (MM_IS_FROM_MEMPOOL(oldmem))
     {
-      newmem = mempool_multiple_realloc(&heap->mm_mpool, oldmem, size);
+      newmem = mempool_multiple_realloc(heap->mm_mpool, oldmem, size);
       if (newmem != NULL)
         {
           return newmem;
@@ -1124,14 +1125,14 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
       if (newmem != NULL)
         {
           memcpy(newmem, oldmem, size);
-          mempool_multiple_free(&heap->mm_mpool, oldmem);
+          mempool_multiple_free(heap->mm_mpool, oldmem);
         }
 
       return newmem;
     }
   else
     {
-      newmem = mempool_multiple_alloc(&heap->mm_mpool, size);
+      newmem = mempool_multiple_alloc(heap->mm_mpool, size);
       if (newmem != NULL)
         {
           memcpy(newmem, oldmem, MIN(size, mm_malloc_size(heap, oldmem)));
@@ -1201,6 +1202,10 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
 
 void mm_uninitialize(FAR struct mm_heap_s *heap)
 {
+#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
+  mempool_multiple_deinit(heap->mm_mpool);
+#endif
+
 #if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
 #  if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
   procfs_unregister_meminfo(&heap->mm_procfs);


[nuttx] 03/06: mempool:add a private pointer to stroe private data

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit f24fb2b10a3647cb25bd2eebcb81ec39d1a7539b
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Wed Nov 23 22:19:21 2022 +0800

    mempool:add a private pointer to stroe private data
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h | 1 +
 1 file changed, 1 insertion(+)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index b85795207d..0309c4224b 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -68,6 +68,7 @@ struct mempool_s
   size_t     interruptsize; /* The initialize size in interrupt mempool */
   size_t     expandsize;    /* The size of expand block every time for mempool */
   bool       wait;          /* The flag of need to wait when mempool is empty */
+  FAR void  *priv;          /* This pointer is used to store the user's private data */
   mempool_alloc_t alloc;    /* The alloc function for mempool */
   mempool_free_t  free;     /* The free function for mempool */
 


[nuttx] 01/06: mempool:remove unnecessary alignment

Posted by xi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

xiaoxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/nuttx.git

commit f00d56337fe246ae32b39524e32fd5e076ee7f0f
Author: anjiahao <an...@xiaomi.com>
AuthorDate: Thu Nov 24 11:28:09 2022 +0800

    mempool:remove unnecessary alignment
    
    Signed-off-by: anjiahao <an...@xiaomi.com>
---
 include/nuttx/mm/mempool.h |  2 +-
 mm/mempool/mempool.c       | 33 ++++++++-------------------------
 2 files changed, 9 insertions(+), 26 deletions(-)

diff --git a/include/nuttx/mm/mempool.h b/include/nuttx/mm/mempool.h
index 1d23d88b7b..22366157c3 100644
--- a/include/nuttx/mm/mempool.h
+++ b/include/nuttx/mm/mempool.h
@@ -39,7 +39,7 @@
 
 struct mempool_s;
 typedef CODE void *(*mempool_alloc_t)(FAR struct mempool_s *pool,
-                                      size_t alignment, size_t size);
+                                      size_t size);
 typedef CODE void (*mempool_free_t)(FAR struct mempool_s *pool,
                                     FAR void *addr);
 
diff --git a/mm/mempool/mempool.c b/mm/mempool/mempool.c
index ee72c2b313..86add5fc07 100644
--- a/mm/mempool/mempool.c
+++ b/mm/mempool/mempool.c
@@ -73,19 +73,15 @@ static inline void mempool_add_list(FAR struct list_node *list,
 }
 
 static inline FAR void *mempool_malloc(FAR struct mempool_s *pool,
-                                       size_t alignment, size_t size)
+                                       size_t size)
 {
   if (pool->alloc != NULL)
     {
-      return pool->alloc(pool, alignment, size);
-    }
-  else if (alignment == 0)
-    {
-      return kmm_malloc(size);
+      return pool->alloc(pool, size);
     }
   else
     {
-      return kmm_memalign(alignment, size);
+      return kmm_malloc(size);
     }
 }
 
@@ -152,7 +148,6 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
 #else
   size_t blocksize = pool->blocksize;
 #endif
-  size_t alignment = 0;
   size_t ninterrupt;
   size_t ninitial;
   size_t count;
@@ -168,12 +163,7 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
   list_initialize(&pool->alist);
 #endif
 
-  if ((pool->blocksize & (pool->blocksize - 1)) == 0)
-    {
-      alignment = pool->blocksize;
-      blocksize = ALIGN_UP(blocksize, alignment);
-    }
-
+  blocksize = ALIGN_UP(blocksize, pool->blocksize);
   ninitial = pool->initialsize / blocksize;
   ninterrupt = pool->interruptsize / blocksize;
   count = ninitial + ninterrupt;
@@ -181,8 +171,8 @@ int mempool_init(FAR struct mempool_s *pool, FAR const char *name)
     {
       FAR char *base;
 
-      base = mempool_malloc(pool, alignment,
-                            blocksize * count + sizeof(struct list_node));
+      base = mempool_malloc(pool, blocksize * count +
+                            sizeof(struct list_node));
       if (base == NULL)
         {
           return -ENOMEM;
@@ -257,18 +247,11 @@ retry:
 #else
               size_t blocksize = pool->blocksize;
 #endif
-              size_t alignment = 0;
               size_t nexpand;
 
-              if ((pool->blocksize & (pool->blocksize - 1)) == 0)
-                {
-                  alignment = pool->blocksize;
-                  blocksize = ALIGN_UP(blocksize, alignment);
-                }
-
+              blocksize = ALIGN_UP(blocksize, pool->blocksize);
               nexpand = pool->expandsize / blocksize;
-              blk = mempool_malloc(pool, alignment,
-                                   blocksize * nexpand + sizeof(*blk));
+              blk = mempool_malloc(pool, blocksize * nexpand + sizeof(*blk));
               if (blk == NULL)
                 {
                   return NULL;