mm: free delay list when exceeding specified count
Signed-off-by: Xu Xingliang <xuxingliang@xiaomi.com>
This commit is contained in:
parent
6dabcd567e
commit
6e7115ca09
@ -25,6 +25,7 @@
|
|||||||
#include <nuttx/config.h>
|
#include <nuttx/config.h>
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
|
#include <debug.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <stdatomic.h>
|
#include <stdatomic.h>
|
||||||
#include <stdbool.h>
|
#include <stdbool.h>
|
||||||
@ -51,6 +52,11 @@ struct mm_delaynode_s
|
|||||||
struct mm_heap_s
|
struct mm_heap_s
|
||||||
{
|
{
|
||||||
struct mm_delaynode_s *mm_delaylist[CONFIG_SMP_NCPUS];
|
struct mm_delaynode_s *mm_delaylist[CONFIG_SMP_NCPUS];
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
size_t mm_delaycount[CONFIG_SMP_NCPUS];
|
||||||
|
#endif
|
||||||
|
|
||||||
atomic_int aordblks;
|
atomic_int aordblks;
|
||||||
atomic_int uordblks;
|
atomic_int uordblks;
|
||||||
atomic_int usmblks;
|
atomic_int usmblks;
|
||||||
@ -60,6 +66,12 @@ struct mm_heap_s
|
|||||||
#endif
|
#endif
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -77,12 +89,17 @@ static void mm_add_delaylist(struct mm_heap_s *heap, void *mem)
|
|||||||
tmp->flink = heap->mm_delaylist[up_cpu_index()];
|
tmp->flink = heap->mm_delaylist[up_cpu_index()];
|
||||||
heap->mm_delaylist[up_cpu_index()] = tmp;
|
heap->mm_delaylist[up_cpu_index()] = tmp;
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
heap->mm_delaycount[up_cpu_index()]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
static void mm_free_delaylist(struct mm_heap_s *heap)
|
static bool mm_free_delaylist(struct mm_heap_s *heap, bool force)
|
||||||
{
|
{
|
||||||
|
bool ret = false;
|
||||||
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
||||||
struct mm_delaynode_s *tmp;
|
struct mm_delaynode_s *tmp;
|
||||||
irqstate_t flags;
|
irqstate_t flags;
|
||||||
@ -92,12 +109,26 @@ static void mm_free_delaylist(struct mm_heap_s *heap)
|
|||||||
flags = up_irq_save();
|
flags = up_irq_save();
|
||||||
|
|
||||||
tmp = heap->mm_delaylist[up_cpu_index()];
|
tmp = heap->mm_delaylist[up_cpu_index()];
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
if (tmp == NULL ||
|
||||||
|
(!force &&
|
||||||
|
heap->mm_delaycount[up_cpu_index()] < CONFIG_MM_FREE_DELAYCOUNT_MAX))
|
||||||
|
{
|
||||||
|
up_irq_restore(flags);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->mm_delaycount[up_cpu_index()] = 0;
|
||||||
|
#endif
|
||||||
heap->mm_delaylist[up_cpu_index()] = NULL;
|
heap->mm_delaylist[up_cpu_index()] = NULL;
|
||||||
|
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
|
|
||||||
/* Test if the delayed is empty */
|
/* Test if the delayed is empty */
|
||||||
|
|
||||||
|
ret = tmp != NULL;
|
||||||
|
|
||||||
while (tmp)
|
while (tmp)
|
||||||
{
|
{
|
||||||
void *address;
|
void *address;
|
||||||
@ -111,9 +142,51 @@ static void mm_free_delaylist(struct mm_heap_s *heap)
|
|||||||
* 'while' condition above.
|
* 'while' condition above.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mm_free(heap, address);
|
mm_delayfree(heap, address, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: mm_delayfree
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Delay free memory if `delay` is true, otherwise free it immediately.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay)
|
||||||
|
{
|
||||||
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
||||||
|
/* Check current environment */
|
||||||
|
|
||||||
|
if (up_interrupt_context())
|
||||||
|
{
|
||||||
|
/* We are in ISR, add to the delay list */
|
||||||
|
|
||||||
|
mm_add_delaylist(heap, mem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
#endif
|
||||||
|
|
||||||
|
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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
mm_add_delaylist(heap, mem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
int size = host_mallocsize(mem);
|
||||||
|
atomic_fetch_sub(&heap->aordblks, 1);
|
||||||
|
atomic_fetch_sub(&heap->uordblks, size);
|
||||||
|
host_free(mem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -208,34 +281,16 @@ void *mm_malloc(struct mm_heap_s *heap, size_t size)
|
|||||||
|
|
||||||
void mm_free(struct mm_heap_s *heap, void *mem)
|
void mm_free(struct mm_heap_s *heap, void *mem)
|
||||||
{
|
{
|
||||||
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
minfo("Freeing %p\n", mem);
|
||||||
/* Check current environment */
|
|
||||||
|
|
||||||
if (up_interrupt_context())
|
/* Protect against attempts to free a NULL reference */
|
||||||
|
|
||||||
|
if (mem == NULL)
|
||||||
{
|
{
|
||||||
/* We are in ISR, add to the delay list */
|
return;
|
||||||
|
|
||||||
mm_add_delaylist(heap, mem);
|
|
||||||
}
|
}
|
||||||
else
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (nxsched_gettid() < 0)
|
mm_delayfree(heap, mem, CONFIG_MM_FREE_DELAYCOUNT_MAX > 0);
|
||||||
{
|
|
||||||
/* nxsched_gettid() return -ESRCH, means we are in situations
|
|
||||||
* during context switching(See nxsched_gettid's comment).
|
|
||||||
* Then add to the delay list.
|
|
||||||
*/
|
|
||||||
|
|
||||||
mm_add_delaylist(heap, mem);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
int size = host_mallocsize(mem);
|
|
||||||
atomic_fetch_sub(&heap->aordblks, 1);
|
|
||||||
atomic_fetch_sub(&heap->uordblks, size);
|
|
||||||
host_free(mem);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -269,7 +324,7 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem,
|
|||||||
int usmblks;
|
int usmblks;
|
||||||
int newsize;
|
int newsize;
|
||||||
|
|
||||||
mm_free_delaylist(heap);
|
mm_free_delaylist(heap, false);
|
||||||
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
{
|
{
|
||||||
@ -295,6 +350,13 @@ void *mm_realloc(struct mm_heap_s *heap, void *oldmem,
|
|||||||
}
|
}
|
||||||
while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks));
|
while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks));
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
if (mem == NULL && mm_free_delaylist(heap, true))
|
||||||
|
{
|
||||||
|
return mm_realloc(heap, oldmem, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -358,7 +420,7 @@ void *mm_memalign(struct mm_heap_s *heap, size_t alignment, size_t size)
|
|||||||
int uordblks;
|
int uordblks;
|
||||||
int usmblks;
|
int usmblks;
|
||||||
|
|
||||||
mm_free_delaylist(heap);
|
mm_free_delaylist(heap, false);
|
||||||
mem = host_memalign(alignment, size);
|
mem = host_memalign(alignment, size);
|
||||||
|
|
||||||
if (mem == NULL)
|
if (mem == NULL)
|
||||||
@ -381,6 +443,13 @@ void *mm_memalign(struct mm_heap_s *heap, size_t alignment, size_t size)
|
|||||||
}
|
}
|
||||||
while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks));
|
while (atomic_compare_exchange_weak(&heap->usmblks, &usmblks, uordblks));
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
if (mem == NULL && mm_free_delaylist(heap, true))
|
||||||
|
{
|
||||||
|
return mm_memalign(heap, alignment, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return mem;
|
return mem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -358,4 +358,12 @@ config MM_PANIC_ON_FAILURE
|
|||||||
default n
|
default n
|
||||||
depends on DEBUG_MM
|
depends on DEBUG_MM
|
||||||
|
|
||||||
|
config MM_FREE_DELAYCOUNT_MAX
|
||||||
|
int "Maximum memory nodes can be delayed to free"
|
||||||
|
default 0
|
||||||
|
---help---
|
||||||
|
Set to 0 to disable the delayed free mechanism. Otherwise,
|
||||||
|
the value decides the maximum number of memory nodes that
|
||||||
|
will be delayed to free.
|
||||||
|
|
||||||
source "mm/iob/Kconfig"
|
source "mm/iob/Kconfig"
|
||||||
|
@ -260,6 +260,10 @@ struct mm_heap_s
|
|||||||
|
|
||||||
FAR struct mm_delaynode_s *mm_delaylist[CONFIG_SMP_NCPUS];
|
FAR struct mm_delaynode_s *mm_delaylist[CONFIG_SMP_NCPUS];
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
size_t mm_delaycount[CONFIG_SMP_NCPUS];
|
||||||
|
#endif
|
||||||
|
|
||||||
/* The is a multiple mempool of the heap */
|
/* The is a multiple mempool of the heap */
|
||||||
|
|
||||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
||||||
@ -304,4 +308,8 @@ int mm_size2ndx(size_t size);
|
|||||||
void mm_foreach(FAR struct mm_heap_s *heap, mm_node_handler_t handler,
|
void mm_foreach(FAR struct mm_heap_s *heap, mm_node_handler_t handler,
|
||||||
FAR void *arg);
|
FAR void *arg);
|
||||||
|
|
||||||
|
/* Functions contained in mm_free.c *****************************************/
|
||||||
|
|
||||||
|
void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay);
|
||||||
|
|
||||||
#endif /* __MM_MM_HEAP_MM_H */
|
#endif /* __MM_MM_HEAP_MM_H */
|
||||||
|
@ -50,6 +50,10 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
tmp->flink = heap->mm_delaylist[up_cpu_index()];
|
tmp->flink = heap->mm_delaylist[up_cpu_index()];
|
||||||
heap->mm_delaylist[up_cpu_index()] = tmp;
|
heap->mm_delaylist[up_cpu_index()] = tmp;
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
heap->mm_delaycount[up_cpu_index()]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -59,15 +63,14 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Name: mm_free
|
* Name: mm_delayfree
|
||||||
*
|
*
|
||||||
* Description:
|
* Description:
|
||||||
* Returns a chunk of memory to the list of free nodes, merging with
|
* Delay free memory if `delay` is true, otherwise free it immediately.
|
||||||
* adjacent free chunks if possible.
|
|
||||||
*
|
*
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
void mm_delayfree(FAR struct mm_heap_s *heap, FAR void *mem, bool delay)
|
||||||
{
|
{
|
||||||
FAR struct mm_freenode_s *node;
|
FAR struct mm_freenode_s *node;
|
||||||
FAR struct mm_freenode_s *prev;
|
FAR struct mm_freenode_s *prev;
|
||||||
@ -75,24 +78,6 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
size_t nodesize;
|
size_t nodesize;
|
||||||
size_t prevsize;
|
size_t prevsize;
|
||||||
|
|
||||||
minfo("Freeing %p\n", mem);
|
|
||||||
|
|
||||||
/* Protect against attempts to free a NULL reference */
|
|
||||||
|
|
||||||
if (!mem)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
DEBUGASSERT(mm_heapmember(heap, mem));
|
|
||||||
|
|
||||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
|
||||||
if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
|
|
||||||
{
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
if (mm_lock(heap) < 0)
|
if (mm_lock(heap) < 0)
|
||||||
{
|
{
|
||||||
/* Meet -ESRCH return, which means we are in situations
|
/* Meet -ESRCH return, which means we are in situations
|
||||||
@ -110,6 +95,13 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
|
|
||||||
kasan_poison(mem, mm_malloc_size(heap, mem));
|
kasan_poison(mem, mm_malloc_size(heap, mem));
|
||||||
|
|
||||||
|
if (delay)
|
||||||
|
{
|
||||||
|
mm_unlock(heap);
|
||||||
|
add_delaylist(heap, mem);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
/* Map the memory chunk into a free node */
|
/* Map the memory chunk into a free node */
|
||||||
|
|
||||||
node = (FAR struct mm_freenode_s *)((FAR char *)mem - MM_SIZEOF_ALLOCNODE);
|
node = (FAR struct mm_freenode_s *)((FAR char *)mem - MM_SIZEOF_ALLOCNODE);
|
||||||
@ -202,3 +194,35 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
mm_addfreechunk(heap, node);
|
mm_addfreechunk(heap, node);
|
||||||
mm_unlock(heap);
|
mm_unlock(heap);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Name: mm_free
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Returns a chunk of memory to the list of free nodes, merging with
|
||||||
|
* adjacent free chunks if possible.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
||||||
|
{
|
||||||
|
minfo("Freeing %p\n", mem);
|
||||||
|
|
||||||
|
/* Protect against attempts to free a NULL reference */
|
||||||
|
|
||||||
|
if (mem == NULL)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
DEBUGASSERT(mm_heapmember(heap, mem));
|
||||||
|
|
||||||
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
||||||
|
if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
mm_delayfree(heap, mem, CONFIG_MM_FREE_DELAYCOUNT_MAX > 0);
|
||||||
|
}
|
||||||
|
@ -39,8 +39,23 @@
|
|||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static void free_delaylist(FAR struct mm_heap_s *heap)
|
/****************************************************************************
|
||||||
|
* Name: free_delaylist
|
||||||
|
*
|
||||||
|
* Description:
|
||||||
|
* Free the memory in delay list either added because of mm_lock failed or
|
||||||
|
* added because of CONFIG_MM_FREE_DELAYCOUNT_MAX.
|
||||||
|
* Set force to true to free all the memory in delay list immediately, set
|
||||||
|
* to false will only free delaylist when time is up if
|
||||||
|
* CONFIG_MM_FREE_DELAYCOUNT_MAX is enabled.
|
||||||
|
*
|
||||||
|
* Return true if there is memory freed.
|
||||||
|
*
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static bool free_delaylist(FAR struct mm_heap_s *heap, bool force)
|
||||||
{
|
{
|
||||||
|
bool ret = false;
|
||||||
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
||||||
FAR struct mm_delaynode_s *tmp;
|
FAR struct mm_delaynode_s *tmp;
|
||||||
irqstate_t flags;
|
irqstate_t flags;
|
||||||
@ -50,12 +65,27 @@ static void free_delaylist(FAR struct mm_heap_s *heap)
|
|||||||
flags = up_irq_save();
|
flags = up_irq_save();
|
||||||
|
|
||||||
tmp = heap->mm_delaylist[up_cpu_index()];
|
tmp = heap->mm_delaylist[up_cpu_index()];
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
if (tmp == NULL ||
|
||||||
|
(!force &&
|
||||||
|
heap->mm_delaycount[up_cpu_index()] < CONFIG_MM_FREE_DELAYCOUNT_MAX))
|
||||||
|
{
|
||||||
|
up_irq_restore(flags);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->mm_delaycount[up_cpu_index()] = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
heap->mm_delaylist[up_cpu_index()] = NULL;
|
heap->mm_delaylist[up_cpu_index()] = NULL;
|
||||||
|
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
|
|
||||||
/* Test if the delayed is empty */
|
/* Test if the delayed is empty */
|
||||||
|
|
||||||
|
ret = tmp != NULL;
|
||||||
|
|
||||||
while (tmp)
|
while (tmp)
|
||||||
{
|
{
|
||||||
FAR void *address;
|
FAR void *address;
|
||||||
@ -69,9 +99,11 @@ static void free_delaylist(FAR struct mm_heap_s *heap)
|
|||||||
* 'while' condition above.
|
* 'while' condition above.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mm_free(heap, address);
|
mm_delayfree(heap, address, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CONFIG_MM_BACKTRACE >= 0
|
#if CONFIG_MM_BACKTRACE >= 0
|
||||||
@ -126,7 +158,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
|||||||
|
|
||||||
/* Free the delay list first */
|
/* Free the delay list first */
|
||||||
|
|
||||||
free_delaylist(heap);
|
free_delaylist(heap, false);
|
||||||
|
|
||||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
||||||
ret = mempool_multiple_alloc(heap->mm_mpool, size);
|
ret = mempool_multiple_alloc(heap->mm_mpool, size);
|
||||||
@ -279,6 +311,16 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
|||||||
minfo("Allocated %p, size %zu\n", ret, alignsize);
|
minfo("Allocated %p, size %zu\n", ret, alignsize);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
/* Try again after free delay list */
|
||||||
|
|
||||||
|
else if (free_delaylist(heap, true))
|
||||||
|
{
|
||||||
|
return mm_malloc(heap, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
#ifdef CONFIG_DEBUG_MM
|
#ifdef CONFIG_DEBUG_MM
|
||||||
else if (MM_INTERNAL_HEAP(heap))
|
else if (MM_INTERNAL_HEAP(heap))
|
||||||
{
|
{
|
||||||
|
@ -103,10 +103,14 @@ struct mm_heap_s
|
|||||||
FAR struct mempool_multiple_s *mm_mpool;
|
FAR struct mempool_multiple_s *mm_mpool;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* Free delay list, for some situation can't do free immdiately */
|
/* Free delay list, for some situation can't do free immediately */
|
||||||
|
|
||||||
struct mm_delaynode_s *mm_delaylist[CONFIG_SMP_NCPUS];
|
struct mm_delaynode_s *mm_delaylist[CONFIG_SMP_NCPUS];
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
size_t mm_delaycount[CONFIG_SMP_NCPUS];
|
||||||
|
#endif
|
||||||
|
|
||||||
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
|
#if defined(CONFIG_FS_PROCFS) && !defined(CONFIG_FS_PROCFS_EXCLUDE_MEMINFO)
|
||||||
struct procfs_meminfo_entry_s mm_procfs;
|
struct procfs_meminfo_entry_s mm_procfs;
|
||||||
#endif
|
#endif
|
||||||
@ -129,6 +133,12 @@ struct mm_mallinfo_handler_s
|
|||||||
FAR struct mallinfo_task *info;
|
FAR struct mallinfo_task *info;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/****************************************************************************
|
||||||
|
* Private Function Prototypes
|
||||||
|
****************************************************************************/
|
||||||
|
|
||||||
|
static void mm_delayfree(struct mm_heap_s *heap, void *mem, bool delay);
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
* Private Functions
|
* Private Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -182,6 +192,10 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
tmp->flink = heap->mm_delaylist[up_cpu_index()];
|
tmp->flink = heap->mm_delaylist[up_cpu_index()];
|
||||||
heap->mm_delaylist[up_cpu_index()] = tmp;
|
heap->mm_delaylist[up_cpu_index()] = tmp;
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
heap->mm_delaycount[up_cpu_index()]++;
|
||||||
|
#endif
|
||||||
|
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
@ -190,8 +204,9 @@ static void add_delaylist(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
* Name: free_delaylist
|
* Name: free_delaylist
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
|
|
||||||
static void free_delaylist(FAR struct mm_heap_s *heap)
|
static bool free_delaylist(FAR struct mm_heap_s *heap, bool force)
|
||||||
{
|
{
|
||||||
|
bool ret = false;
|
||||||
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
#if defined(CONFIG_BUILD_FLAT) || defined(__KERNEL__)
|
||||||
FAR struct mm_delaynode_s *tmp;
|
FAR struct mm_delaynode_s *tmp;
|
||||||
irqstate_t flags;
|
irqstate_t flags;
|
||||||
@ -201,12 +216,27 @@ static void free_delaylist(FAR struct mm_heap_s *heap)
|
|||||||
flags = up_irq_save();
|
flags = up_irq_save();
|
||||||
|
|
||||||
tmp = heap->mm_delaylist[up_cpu_index()];
|
tmp = heap->mm_delaylist[up_cpu_index()];
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
if (tmp == NULL ||
|
||||||
|
(!force &&
|
||||||
|
heap->mm_delaycount[up_cpu_index()] < CONFIG_MM_FREE_DELAYCOUNT_MAX))
|
||||||
|
{
|
||||||
|
up_irq_restore(flags);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
heap->mm_delaycount[up_cpu_index()] = 0;
|
||||||
|
#endif
|
||||||
|
|
||||||
heap->mm_delaylist[up_cpu_index()] = NULL;
|
heap->mm_delaylist[up_cpu_index()] = NULL;
|
||||||
|
|
||||||
up_irq_restore(flags);
|
up_irq_restore(flags);
|
||||||
|
|
||||||
/* Test if the delayed is empty */
|
/* Test if the delayed is empty */
|
||||||
|
|
||||||
|
ret = tmp != NULL;
|
||||||
|
|
||||||
while (tmp)
|
while (tmp)
|
||||||
{
|
{
|
||||||
FAR void *address;
|
FAR void *address;
|
||||||
@ -220,9 +250,11 @@ static void free_delaylist(FAR struct mm_heap_s *heap)
|
|||||||
* 'while' condition above.
|
* 'while' condition above.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
mm_free(heap, address);
|
mm_delayfree(heap, address, false);
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0 && CONFIG_MM_BACKTRACE >= 0
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0 && CONFIG_MM_BACKTRACE >= 0
|
||||||
@ -457,6 +489,50 @@ 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)
|
||||||
|
{
|
||||||
|
#ifdef CONFIG_MM_FILL_ALLOCATIONS
|
||||||
|
memset(mem, 0x55, mm_malloc_size(heap, mem));
|
||||||
|
#endif
|
||||||
|
|
||||||
|
kasan_poison(mem, mm_malloc_size(heap, mem));
|
||||||
|
|
||||||
|
/* Update heap statistics */
|
||||||
|
|
||||||
|
heap->mm_curused -= mm_malloc_size(heap, mem);
|
||||||
|
|
||||||
|
/* Pass, return to the tlsf pool */
|
||||||
|
|
||||||
|
if (delay)
|
||||||
|
{
|
||||||
|
add_delaylist(heap, mem);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
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
|
* Public Functions
|
||||||
****************************************************************************/
|
****************************************************************************/
|
||||||
@ -678,18 +754,17 @@ void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size,
|
|||||||
|
|
||||||
void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
||||||
{
|
{
|
||||||
int ret;
|
|
||||||
|
|
||||||
UNUSED(ret);
|
|
||||||
minfo("Freeing %p\n", mem);
|
minfo("Freeing %p\n", mem);
|
||||||
|
|
||||||
/* Protect against attempts to free a NULL reference */
|
/* Protect against attempts to free a NULL reference */
|
||||||
|
|
||||||
if (!mem)
|
if (mem == NULL)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
DEBUGASSERT(mm_heapmember(heap, mem));
|
||||||
|
|
||||||
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
#if CONFIG_MM_HEAP_MEMPOOL_THRESHOLD != 0
|
||||||
if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
|
if (mempool_multiple_free(heap->mm_mpool, mem) >= 0)
|
||||||
{
|
{
|
||||||
@ -697,29 +772,7 @@ void mm_free(FAR struct mm_heap_s *heap, FAR void *mem)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
if (mm_lock(heap) == 0)
|
mm_delayfree(heap, mem, CONFIG_MM_FREE_DELAYCOUNT_MAX > 0);
|
||||||
{
|
|
||||||
#ifdef CONFIG_MM_FILL_ALLOCATIONS
|
|
||||||
memset(mem, 0x55, mm_malloc_size(heap, mem));
|
|
||||||
#endif
|
|
||||||
|
|
||||||
kasan_poison(mem, mm_malloc_size(heap, mem));
|
|
||||||
|
|
||||||
/* Update heap statistics */
|
|
||||||
|
|
||||||
heap->mm_curused -= mm_malloc_size(heap, mem);
|
|
||||||
|
|
||||||
/* Pass, return to the tlsf pool */
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/****************************************************************************
|
/****************************************************************************
|
||||||
@ -1061,7 +1114,7 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
|||||||
|
|
||||||
/* Free the delay list first */
|
/* Free the delay list first */
|
||||||
|
|
||||||
free_delaylist(heap);
|
free_delaylist(heap, false);
|
||||||
|
|
||||||
/* Allocate from the tlsf pool */
|
/* Allocate from the tlsf pool */
|
||||||
|
|
||||||
@ -1095,6 +1148,15 @@ FAR void *mm_malloc(FAR struct mm_heap_s *heap, size_t size)
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
/* Try again after free delay list */
|
||||||
|
|
||||||
|
else if (free_delaylist(heap, true))
|
||||||
|
{
|
||||||
|
return mm_malloc(heap, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1126,7 +1188,7 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
|
|||||||
|
|
||||||
/* Free the delay list first */
|
/* Free the delay list first */
|
||||||
|
|
||||||
free_delaylist(heap);
|
free_delaylist(heap, false);
|
||||||
|
|
||||||
/* Allocate from the tlsf pool */
|
/* Allocate from the tlsf pool */
|
||||||
|
|
||||||
@ -1156,6 +1218,15 @@ FAR void *mm_memalign(FAR struct mm_heap_s *heap, size_t alignment,
|
|||||||
kasan_unpoison(ret, mm_malloc_size(heap, ret));
|
kasan_unpoison(ret, mm_malloc_size(heap, ret));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
/* Try again after free delay list */
|
||||||
|
|
||||||
|
else if (free_delaylist(heap, true))
|
||||||
|
{
|
||||||
|
return mm_memalign(heap, alignment, size);
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1237,7 +1308,7 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
|
|||||||
#else
|
#else
|
||||||
/* Free the delay list first */
|
/* Free the delay list first */
|
||||||
|
|
||||||
free_delaylist(heap);
|
free_delaylist(heap, false);
|
||||||
|
|
||||||
/* Allocate from the tlsf pool */
|
/* Allocate from the tlsf pool */
|
||||||
|
|
||||||
@ -1268,8 +1339,16 @@ FAR void *mm_realloc(FAR struct mm_heap_s *heap, FAR void *oldmem,
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#if CONFIG_MM_FREE_DELAYCOUNT_MAX > 0
|
||||||
|
/* Try again after free delay list */
|
||||||
|
|
||||||
|
if (newmem == NULL && free_delaylist(heap, true))
|
||||||
|
{
|
||||||
|
return mm_realloc(heap, oldmem, size);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#endif
|
||||||
return newmem;
|
return newmem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user