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

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

commit bae29d1bafd764726a5b6fb48bc1cf7b8f751705
Author: huojianchao <[email protected]>
AuthorDate: Mon Aug 4 14:47:53 2025 +0800

    delayfree: add kmm_delayfree kumm_delayfree mm_delayfree api
    
    Add mm_delayfree(), kmm_delayfree(), umm_delayfree() and
    kumm_delayfree() for callers that release memory in a context where the
    heap lock cannot be taken.  The block is queued on the heap delay list
    and freed on a later allocation.
    
    Split the immediate free out of the old mm_delayfree(heap, mem, delay)
    into mm_forcefree() (forcefree() in tlsf and the sim heap).  It tries the
    mempool first, so a mempool block taken off the delay list goes back to
    its pool.  mm_free() queues the block when CONFIG_MM_FREE_DELAYCOUNT_MAX
    > 0 and frees it immediately otherwise.
    
    When mm_lock() fails (interrupt context or a context switch in
    progress), mm_free(), mm_forcefree() and forcefree() queue the block on
    the delay list without filling or poisoning it: the heap must not be
    touched without the lock, and the block may still be the stack of the
    running thread.
    The sim heap forcefree() keeps the same interrupt and context switch
    check that its delay_free() had.
    
    Signed-off-by: huojianchao <[email protected]>
    Signed-off-by: v-tangmeng <[email protected]>
    Signed-off-by: zhangyuan29 <[email protected]>
---
 arch/sim/src/sim/sim_ummheap.c | 115 +++++++++++++++------------
 include/nuttx/kmalloc.h        |   2 +
 include/nuttx/mm/mm.h          |   6 ++
 mm/kmm_heap/kmm_free.c         |  20 +++++
 mm/mm_heap/mm.h                |   2 +-
 mm/mm_heap/mm_free.c           | 124 ++++++++++++++++++++---------
 mm/mm_heap/mm_malloc.c         |   2 +-
 mm/tlsf/mm_tlsf.c              | 174 +++++++++++++++++++++++++----------------
 mm/umm_heap/umm_free.c         |  19 +++++
 9 files changed, 308 insertions(+), 156 deletions(-)

diff --git a/arch/sim/src/sim/sim_ummheap.c b/arch/sim/src/sim/sim_ummheap.c
index 65e5e562790..8f826da20ce 100644
--- a/arch/sim/src/sim/sim_ummheap.c
+++ b/arch/sim/src/sim/sim_ummheap.c
@@ -171,12 +171,6 @@ struct mm_heap_s
     }                                              \
   while (0)
 
-/****************************************************************************
- * Private Function Prototypes
- ****************************************************************************/
-
-static void delay_free(struct mm_heap_s *heap, void *mem, bool delay);
-
 /****************************************************************************
  * Private Data
  ****************************************************************************/
@@ -276,6 +270,39 @@ static void add_delaylist(struct mm_heap_s *heap, void 
*mem)
   spin_unlock_irqrestore(&heap->lock, flags);
 }
 
+/****************************************************************************
+ * Name: forcefree
+ *
+ * Description:
+ *   Delay free memory if `delay` is true, otherwise free it immediately.
+ *
+ ****************************************************************************/
+
+static void forcefree(struct mm_heap_s *heap, void *mem)
+{
+  struct mm_allocnode_s *node;
+
+  if (up_interrupt_context() || nxsched_gettid() < 0)
+    {
+      /* In ISR, or nxsched_gettid() returns -ESRCH during context
+       * switching (see nxsched_gettid's comment): the block may be the
+       * running thread stack, add it to the delay list.
+       */
+
+      add_delaylist(heap, mem);
+      return;
+    }
+
+  node = (struct mm_allocnode_s *)((uintptr_t)mem - MM_ALLOCNODE_SIZE);
+#ifdef CONFIG_MM_FILL_ALLOCATIONS
+  memset(mem, MM_FREE_MAGIC,
+         node->size - MM_ALLOCNODE_SIZE - node->padding);
+#endif
+  update_stats(heap, mem, node->size, false);
+  sched_note_heap(NOTE_HEAP_FREE, heap, mem, node->size, 0);
+  host_free(node->allocmem);
+}
+
 static bool free_delaylist(struct mm_heap_s *heap, bool force)
 {
   bool ret = false;
@@ -320,54 +347,12 @@ static bool free_delaylist(struct mm_heap_s *heap, bool 
force)
        * 'while' condition above.
        */
 
-      delay_free(heap, address, false);
+      forcefree(heap, address);
     }
 
   return ret;
 }
 
-/****************************************************************************
- * Name: delay_free
- *
- * Description:
- *   Delay free memory if `delay` is true, otherwise free it immediately.
- *
- ****************************************************************************/
-
-static void delay_free(struct mm_heap_s *heap, void *mem, bool delay)
-{
-  struct mm_allocnode_s *node;
-
-  /* Check current environment */
-
-  if (up_interrupt_context())
-    {
-      /* We are in ISR, add to the delay list */
-
-      add_delaylist(heap, mem);
-    }
-  else if (nxsched_gettid() < 0 || delay)
-    {
-      /* nxsched_gettid() return -ESRCH, means we are in situations
-       * during context switching(See nxsched_gettid's comment).
-       * Then add to the delay list.
-       */
-
-      add_delaylist(heap, mem);
-    }
-  else
-    {
-      node = (struct mm_allocnode_s *)((uintptr_t)mem - MM_ALLOCNODE_SIZE);
-#ifdef CONFIG_MM_FILL_ALLOCATIONS
-      memset(mem, MM_FREE_MAGIC,
-             node->size - MM_ALLOCNODE_SIZE - node->padding);
-#endif
-      update_stats(heap, mem, node->size, false);
-      sched_note_heap(NOTE_HEAP_FREE, heap, mem, node->size, 0);
-      host_free(node->allocmem);
-    }
-}
-
 static void *reallocate(void *oldmem, size_t alignment, size_t size)
 {
   size_t aligned_size;
@@ -549,7 +534,37 @@ void free(void *mem)
       return;
     }
 
-  delay_free(g_mmheap, mem, CONFIG_MM_FREE_DELAYCOUNT_MAX > 0);
+#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
+  add_delaylist(g_mmheap, mem);
+#else
+  forcefree(g_mmheap, mem);
+#endif
+}
+
+/****************************************************************************
+ * Name: umm_delayfree
+ *
+ * Description:
+ *   Add mem to delaylist, mem will be freed delay a while.
+ *
+ * Input Parameters:
+ *   mem: addr of mem to free delay.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void umm_delayfree(void *mem)
+{
+  /* Protect against attempts to free a NULL reference */
+
+  if (mem == NULL)
+    {
+      return;
+    }
+
+  add_delaylist(g_mmheap, mem);
 }
 
 /****************************************************************************
diff --git a/include/nuttx/kmalloc.h b/include/nuttx/kmalloc.h
index 0aca383d4ed..c985d68a51d 100644
--- a/include/nuttx/kmalloc.h
+++ b/include/nuttx/kmalloc.h
@@ -82,6 +82,7 @@ extern "C"
 #define kumm_realloc(p,s)        realloc(p,s)
 #define kumm_memalign(a,s)       memalign(a,s)
 #define kumm_free(p)             free(p)
+#define kumm_delayfree(p)        umm_delayfree(p)
 #define kumm_mallinfo()          mallinfo()
 
 /* This family of allocators is used to manage kernel protected memory */
@@ -102,6 +103,7 @@ extern "C"
 #  define kmm_realloc(p,s)       realloc(p,s)
 #  define kmm_memalign(a,s)      memalign(a,s)
 #  define kmm_free(p)            free(p)
+#  define kmm_delayfree(p)       kumm_delayfree(p)
 #  define kmm_mallinfo()         mallinfo()
 #  define kmm_heapmember(p)      umm_heapmember(p)
 #  define kmm_memdump(p)         umm_memdump(p)
diff --git a/include/nuttx/mm/mm.h b/include/nuttx/mm/mm.h
index ab05a35aff8..f2a2f9c7c96 100644
--- a/include/nuttx/mm/mm.h
+++ b/include/nuttx/mm/mm.h
@@ -351,13 +351,19 @@ size_t kmm_malloc_size(FAR void *mem);
 /* Functions contained in mm_free.c *****************************************/
 
 void mm_free(FAR struct mm_heap_s *heap, FAR void *mem);
+void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem);
 
 /* Functions contained in kmm_free.c ****************************************/
 
 #ifdef CONFIG_MM_KERNEL_HEAP
 void kmm_free(FAR void *mem);
+void kmm_delayfree(FAR void *mem);
 #endif
 
+/* Functions contained in umm_free.c ****************************************/
+
+void umm_delayfree(FAR void *mem);
+
 /* Functions contained in mm_realloc.c **************************************/
 
 FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
diff --git a/mm/kmm_heap/kmm_free.c b/mm/kmm_heap/kmm_free.c
index 275bc3fa1a8..0db38d9ee68 100644
--- a/mm/kmm_heap/kmm_free.c
+++ b/mm/kmm_heap/kmm_free.c
@@ -58,4 +58,24 @@ void kmm_free(FAR void *mem)
   mm_free(KNR_HEAP, mem);
 }
 
+/****************************************************************************
+ * Name: kmm_delayfree
+ *
+ * Description:
+ *   Add mem to delaylist, mem will be freed delay a while.
+ *
+ * Input Parameters:
+ *   mem: addr of mem to free delay.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void kmm_delayfree(FAR void *mem)
+{
+  DEBUGASSERT((mem == NULL) || kmm_heapmember(mem));
+  mm_delayfree(g_kmmheap, mem);
+}
+
 #endif /* CONFIG_MM_KERNEL_HEAP */
diff --git a/mm/mm_heap/mm.h b/mm/mm_heap/mm.h
index 1753da304b4..aca2a783765 100644
--- a/mm/mm_heap/mm.h
+++ b/mm/mm_heap/mm.h
@@ -302,7 +302,7 @@ void mm_foreach(FAR struct mm_heap_s *heap, 
mm_node_handler_t handler,
 
 /* Functions contained in mm_free.c *****************************************/
 
-void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay);
+void mm_forcefree(FAR struct mm_heap_s *heap, FAR void *mem);
 
 /* Functions contained in mm_malloc.c ***************************************/
 
diff --git a/mm/mm_heap/mm_free.c b/mm/mm_heap/mm_free.c
index 3ec17f9dfe2..f4b82ce9a80 100644
--- a/mm/mm_heap/mm_free.c
+++ b/mm/mm_heap/mm_free.c
@@ -41,7 +41,8 @@
  * Private Functions
  ****************************************************************************/
 
-static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
+static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem,
+                          bool asan_check)
 {
 #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
   FAR struct mm_delaynode_s *tmp = mem;
@@ -52,12 +53,28 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR 
void *mem)
   flags = mm_lock_irq(heap);
   kasan_bypass(true);
 
-#  ifdef CONFIG_DEBUG_ASSERTIONS
-  FAR struct mm_freenode_s *node;
-
-  node = (FAR struct mm_freenode_s *)((FAR char *)mem - MM_SIZEOF_ALLOCNODE);
-  DEBUGASSERT(MM_NODE_IS_ALLOC(node));
+#ifdef CONFIG_DEBUG_ASSERTIONS
+#  ifdef CONFIG_MM_HEAP_MEMPOOL
+  if (!heap->mm_mpool || !mempool_multiple_member(heap->mm_mpool, mem))
 #  endif
+    {
+      FAR struct mm_freenode_s *node;
+
+      node = (FAR struct mm_freenode_s *)((FAR char *)mem -
+             MM_SIZEOF_ALLOCNODE);
+      DEBUGASSERT(MM_NODE_IS_ALLOC(node));
+    }
+#endif
+
+  if (asan_check)
+    {
+      size_t size = mm_malloc_size(heap, mem);
+#ifdef CONFIG_MM_FILL_ALLOCATIONS
+      memset(mem, MM_FREE_MAGIC, size);
+#endif
+      UNUSED(size);
+      kasan_poison(mem, size);
+    }
 
   tmp->flink = heap->mm_delaylist[this_cpu()];
   heap->mm_delaylist[this_cpu()] = tmp;
@@ -76,14 +93,14 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR 
void *mem)
  ****************************************************************************/
 
 /****************************************************************************
- * Name: mm_delayfree
+ * Name: mm_forcefree
  *
  * Description:
- *   Delay free memory if `delay` is true, otherwise free it immediately.
+ *   Heap mem relase, add to chunk.
  *
  ****************************************************************************/
 
-void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay)
+void mm_forcefree(FAR struct mm_heap_s *heap, FAR void *mem)
 {
   FAR struct mm_freenode_s *node;
   FAR struct mm_freenode_s *prev;
@@ -91,6 +108,15 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void 
*mem, bool delay)
   size_t nodesize;
   size_t prevsize;
 
+  /* Free mempool blk first */
+
+#ifdef CONFIG_MM_HEAP_MEMPOOL
+  if (heap->mm_mpool && mempool_multiple_free(heap->mm_mpool, mem) >= 0)
+    {
+      return;
+    }
+#endif
+
   if (mm_lock(heap) < 0)
     {
       /* Meet -ESRCH return, which means we are in situations
@@ -98,37 +124,21 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void 
*mem, bool delay)
        * Then add to the delay list.
        */
 
-      add_delaylist(heap, mem);
+      add_delaylist(heap, mem, false);
       return;
     }
 
   kasan_bypass(true);
   nodesize = mm_malloc_size(heap, mem);
-#ifdef CONFIG_MM_FILL_ALLOCATIONS
-#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
-  /* If delay free is enabled, a memory node will be freed twice.
-   * The first time is to add the node to the delay list, and the second
-   * time is to actually free the node. Therefore, we only colorize the
-   * memory node the first time, when `delay` is set to true.
+#if defined(CONFIG_MM_FILL_ALLOCATIONS) && CONFIG_MM_FREE_DELAYCOUNT_MAX == 0
+  /* With the delay count enabled, mm_free() has already filled and
+   * poisoned the block when it was queued on the delay list.
    */
 
-  if (delay)
+  memset(mem, MM_FREE_MAGIC, nodesize);
 #endif
-    {
-      memset(mem, MM_FREE_MAGIC, nodesize);
-    }
-#endif
-
   kasan_poison(mem, nodesize);
 
-  if (delay)
-    {
-      kasan_bypass(false);
-      mm_unlock(heap);
-      add_delaylist(heap, mem);
-      return;
-    }
-
   /* Map the memory chunk into a free node */
 
   node = (FAR struct mm_freenode_s *)
@@ -138,7 +148,6 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void 
*mem, bool delay)
   /* Sanity check against double-frees */
 
   DEBUGASSERT(MM_NODE_IS_ALLOC(node));
-
   node->size &= ~MM_ALLOC_BIT;
 
   /* Update heap statistics */
@@ -226,6 +235,29 @@ void mm_delayfree(FAR struct mm_heap_s *heap, FAR void 
*mem, bool delay)
   mm_unlock(heap);
 }
 
+/****************************************************************************
+ * Name: mm_delayfree
+ *
+ * Description:
+ *   Add mem to delaylist, mem will be freed after a while.
+ *
+ ****************************************************************************/
+
+void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem)
+{
+  minfo("Adding delaylist %p\n", mem);
+
+  /* Protect against attempts to free a NULL reference */
+
+  if (mem == NULL)
+    {
+      return;
+    }
+
+  DEBUGASSERT(mm_heapmember(heap, mem));
+  add_delaylist(heap, mem, false);
+}
+
 /****************************************************************************
  * Name: mm_free
  *
@@ -249,14 +281,32 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
   DEBUGASSERT(mm_heapmember(heap, mem));
 
 #ifdef CONFIG_MM_HEAP_MEMPOOL
-  if (heap->mm_mpool)
+  /* A mempool block is filled and poisoned by the mempool itself, so do
+   * not queue it on the delay list where it would be filled again.
+   */
+
+  if (heap->mm_mpool && mempool_multiple_free(heap->mm_mpool, mem) >= 0)
     {
-      if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
-        {
-          return;
-        }
+      return;
     }
 #endif
 
-  mm_delayfree(heap, mem, CONFIG_MM_FREE_DELAYCOUNT_MAX > 0);
+#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
+  if (mm_lock(heap) < 0)
+    {
+      /* Meet -ESRCH return, which means we are in situations
+       * during context switching(See mm_lock() & gettid()).
+       * The block may be the running thread stack, don't touch it.
+       */
+
+      add_delaylist(heap, mem, false);
+      return;
+    }
+
+  mm_unlock(heap);
+  add_delaylist(heap, mem, true);
+#else
+
+  mm_forcefree(heap, mem);
+#endif
 }
diff --git a/mm/mm_heap/mm_malloc.c b/mm/mm_heap/mm_malloc.c
index a91b4e6bd3c..506a5ef68f1 100644
--- a/mm/mm_heap/mm_malloc.c
+++ b/mm/mm_heap/mm_malloc.c
@@ -104,7 +104,7 @@ static bool free_delaylist(FAR struct mm_heap_s *heap, bool 
force)
        * 'while' condition above.
        */
 
-      mm_delayfree(heap, address, false);
+      mm_forcefree(heap, address);
     }
 
 #endif
diff --git a/mm/tlsf/mm_tlsf.c b/mm/tlsf/mm_tlsf.c
index bf50345a550..610b4b2d927 100644
--- a/mm/tlsf/mm_tlsf.c
+++ b/mm/tlsf/mm_tlsf.c
@@ -180,7 +180,8 @@ void memdump_dump_pool(FAR struct mm_memdump_priv_s *priv,
  * Private Function Prototypes
  ****************************************************************************/
 
-static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay);
+static int mm_lock(FAR struct mm_heap_s *heap);
+static void mm_unlock(FAR struct mm_heap_s *heap);
 
 /****************************************************************************
  * Private Functions
@@ -343,7 +344,8 @@ static void memdump_backtrace(FAR struct mm_heap_s *heap,
  * Name: add_delaylist
  ****************************************************************************/
 
-static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
+static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem,
+                          bool asan_check)
 {
 #if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
   FAR struct mm_delaynode_s *tmp = mem;
@@ -353,6 +355,16 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR 
void *mem)
 
   flags = mm_lock_irq(heap);
 
+  if (asan_check)
+    {
+      size_t size = mm_malloc_size(heap, mem);
+#  ifdef CONFIG_MM_FILL_ALLOCATIONS
+      memset(mem, MM_FREE_MAGIC, size);
+#  endif
+      kasan_poison(mem, size);
+      UNUSED(size);
+    }
+
   tmp->flink = heap->mm_delaylist[this_cpu()];
   heap->mm_delaylist[this_cpu()] = tmp;
 
@@ -364,6 +376,53 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR 
void *mem)
 #endif
 }
 
+/****************************************************************************
+ * Name: forcefree
+ *
+ * Description:
+ *   Free memory immediately.
+ *
+ ****************************************************************************/
+
+static void forcefree(FAR struct mm_heap_s *heap, FAR void *mem)
+{
+  size_t size;
+
+  /* Free mempool blk first */
+
+#ifdef CONFIG_MM_HEAP_MEMPOOL
+  if (heap->mm_mpool && mempool_multiple_free(heap->mm_mpool, mem) >= 0)
+    {
+      return;
+    }
+#endif
+
+  if (mm_lock(heap) < 0)
+    {
+      /* Add to the delay list(see the comment in mm_lock) */
+
+      add_delaylist(heap, mem, false);
+      return;
+    }
+
+  size = mm_malloc_size(heap, mem);
+#if defined(CONFIG_MM_FILL_ALLOCATIONS) && CONFIG_MM_FREE_DELAYCOUNT_MAX == 0
+  /* With the delay count enabled, mm_free() has already filled and
+   * poisoned the block when it was queued on the delay list.
+   */
+
+  memset(mem, MM_FREE_MAGIC, size);
+#endif
+  kasan_poison(mem, size);
+
+  /* Update heap statistics */
+
+  heap->mm_curused -= size;
+  sched_note_heap(NOTE_HEAP_FREE, heap, mem, size, heap->mm_curused);
+  tlsf_free(heap->mm_tlsf, mem);
+  mm_unlock(heap);
+}
+
 /****************************************************************************
  * Name: free_delaylist
  ****************************************************************************/
@@ -414,7 +473,7 @@ static bool free_delaylist(FAR struct mm_heap_s *heap, bool 
force)
        * 'while' condition above.
        */
 
-      mm_delayfree(heap, address, false);
+      forcefree(heap, address);
     }
 
 #endif
@@ -629,64 +688,6 @@ static void memdump_handler(FAR void *ptr, size_t size, 
int used,
     }
 }
 
-/****************************************************************************
- * Name: mm_delayfree
- *
- * Description:
- *   Delay free memory if `delay` is true, otherwise free it immediately.
- *
- ****************************************************************************/
-
-static void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem,
-                         bool delay)
-{
-  if (mm_lock(heap) == 0)
-    {
-      size_t size = mm_malloc_size(heap, mem);
-
-      UNUSED(size);
-#ifdef CONFIG_MM_FILL_ALLOCATIONS
-#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
-      /* If delay free is enabled, a memory node will be freed twice.
-       * The first time is to add the node to the delay list, and the second
-       * time is to actually free the node. Therefore, we only colorize the
-       * memory node the first time, when `delay` is set to true.
-       */
-
-      if (delay)
-#endif
-        {
-          memset(mem, MM_FREE_MAGIC, size);
-        }
-#endif
-
-      kasan_poison(mem, size);
-
-      /* Pass, return to the tlsf pool */
-
-      if (delay)
-        {
-          add_delaylist(heap, mem);
-        }
-      else
-        {
-          /* Update heap statistics */
-
-          heap->mm_curused -= size;
-          sched_note_heap(NOTE_HEAP_FREE, heap, mem, size, heap->mm_curused);
-          tlsf_free(heap->mm_tlsf, mem);
-        }
-
-      mm_unlock(heap);
-    }
-  else
-    {
-      /* Add to the delay list(see the comment in mm_lock) */
-
-      add_delaylist(heap, mem);
-    }
-}
-
 /****************************************************************************
  * Public Functions
  ****************************************************************************/
@@ -933,16 +934,55 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
   DEBUGASSERT(mm_heapmember(heap, mem));
 
 #ifdef CONFIG_MM_HEAP_MEMPOOL
-  if (heap->mm_mpool)
+  /* A mempool block is filled and poisoned by the mempool itself, so do
+   * not queue it on the delay list where it would be filled again.
+   */
+
+  if (heap->mm_mpool && mempool_multiple_free(heap->mm_mpool, mem) >= 0)
     {
-      if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
-        {
-          return;
-        }
+      return;
+    }
+#endif
+
+#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
+  if (mm_lock(heap) < 0)
+    {
+      /* Add to the delay list(see the comment in mm_lock), the block may
+       * be the running thread stack, don't touch it.
+       */
+
+      add_delaylist(heap, mem, false);
+      return;
     }
+
+  mm_unlock(heap);
+  add_delaylist(heap, mem, true);
+#else
+  forcefree(heap, mem);
 #endif
+}
+
+/****************************************************************************
+ * Name: mm_delayfree
+ *
+ * Description:
+ *   Add mem to delaylist, mem will be freed after a while.
+ *
+ ****************************************************************************/
+
+void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem)
+{
+  minfo("Adding delaylist %p\n", mem);
+
+  /* Protect against attempts to free a NULL reference */
 
-  mm_delayfree(heap, mem, CONFIG_MM_FREE_DELAYCOUNT_MAX > 0);
+  if (mem == NULL)
+    {
+      return;
+    }
+
+  DEBUGASSERT(mm_heapmember(heap, mem));
+  add_delaylist(heap, mem, false);
 }
 
 /****************************************************************************
diff --git a/mm/umm_heap/umm_free.c b/mm/umm_heap/umm_free.c
index 72cccdc81c0..a0fd48fb658 100644
--- a/mm/umm_heap/umm_free.c
+++ b/mm/umm_heap/umm_free.c
@@ -50,3 +50,22 @@ void free(FAR void *mem)
 {
   mm_free(USR_HEAP, mem);
 }
+
+/****************************************************************************
+ * Name: umm_delayfree
+ *
+ * Description:
+ *   Add mem to delaylist, mem will be freed delay a while.
+ *
+ * Input Parameters:
+ *   mem: addr of mem to free delay.
+ *
+ * Returned Value:
+ *   None
+ *
+ ****************************************************************************/
+
+void umm_delayfree(FAR void *mem)
+{
+  mm_delayfree(USR_HEAP, mem);
+}

Reply via email to