mm/mm_mallinfo: using mm_foreach to do mm_mallinfo

Signed-off-by: Jiuzhu Dong <dongjiuzhu1@xiaomi.com>
This commit is contained in:
Jiuzhu Dong
2022-01-24 14:44:38 +08:00
committed by Xiang Xiao
parent b1a51a5b30
commit 12b256f860
+37 -68
View File
@@ -33,55 +33,15 @@
#include "mm_heap/mm.h" #include "mm_heap/mm.h"
/**************************************************************************** /****************************************************************************
* Public Functions * Private Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** static void mallinfo_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
* Name: mm_mallinfo
*
* Description:
* mallinfo returns a copy of updated current heap information.
*
****************************************************************************/
int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
{ {
FAR struct mm_allocnode_s *node; FAR struct mallinfo *info = arg;
FAR struct mm_allocnode_s *prev;
size_t mxordblk = 0;
int ordblks = 0; /* Number of non-inuse chunks */
int aordblks = 0; /* Number of inuse chunks */
size_t uordblks = 0; /* Total allocated space */
size_t fordblks = 0; /* Total non-inuse space */
#if CONFIG_MM_REGIONS > 1
int region;
#else
# define region 0
#endif
DEBUGASSERT(info); minfo("node=%p size=%u preceding=%u (%c)\n",
node, (unsigned int)node->size,
/* Visit each region */
#if CONFIG_MM_REGIONS > 1
for (region = 0; region < heap->mm_nregions; region++)
#endif
{
prev = NULL;
/* Visit each node in the region
* Retake the semaphore for each region to reduce latencies
*/
DEBUGVERIFY(mm_takesemaphore(heap));
for (node = heap->mm_heapstart[region];
node < heap->mm_heapend[region];
node = (FAR struct mm_allocnode_s *)
((FAR char *)node + node->size))
{
minfo("region=%d node=%p size=%u preceding=%u (%c)\n",
region, node, (unsigned int)node->size,
(unsigned int)(node->preceding & ~MM_ALLOC_BIT), (unsigned int)(node->preceding & ~MM_ALLOC_BIT),
(node->preceding & MM_ALLOC_BIT) ? 'A' : 'F'); (node->preceding & MM_ALLOC_BIT) ? 'A' : 'F');
@@ -90,8 +50,8 @@ int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
if ((node->preceding & MM_ALLOC_BIT) != 0) if ((node->preceding & MM_ALLOC_BIT) != 0)
{ {
DEBUGASSERT(node->size >= SIZEOF_MM_ALLOCNODE); DEBUGASSERT(node->size >= SIZEOF_MM_ALLOCNODE);
aordblks++; info->aordblks++;
uordblks += node->size; info->uordblks += node->size;
} }
else else
{ {
@@ -105,36 +65,45 @@ int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
DEBUGASSERT(fnode->flink == NULL || DEBUGASSERT(fnode->flink == NULL ||
fnode->flink->size == 0 || fnode->flink->size == 0 ||
fnode->flink->size >= fnode->size); fnode->flink->size >= fnode->size);
ordblks++;
fordblks += node->size; info->ordblks++;
if (node->size > mxordblk) info->fordblks += node->size;
if (node->size > info->mxordblk)
{ {
mxordblk = node->size; info->mxordblk = node->size;
} }
} }
}
DEBUGASSERT(prev == NULL || /****************************************************************************
prev->size == (node->preceding & ~MM_ALLOC_BIT)); * Public Functions
prev = node; ****************************************************************************/
}
minfo("region=%d node=%p heapend=%p\n", /****************************************************************************
region, node, heap->mm_heapend[region]); * Name: mm_mallinfo
DEBUGASSERT(node == heap->mm_heapend[region]); *
* Description:
* mallinfo returns a copy of updated current heap information.
*
****************************************************************************/
mm_givesemaphore(heap); int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
{
#if CONFIG_MM_REGIONS > 1
int region = heap->mm_nregions;
#else
# define region 1
#endif
uordblks += SIZEOF_MM_ALLOCNODE; /* account for the tail node */ DEBUGASSERT(info);
}
#undef region
DEBUGASSERT(uordblks + fordblks == heap->mm_heapsize); memset(info, 0, sizeof(*info));
mm_foreach(heap, mallinfo_handler, info);
info->arena = heap->mm_heapsize; info->arena = heap->mm_heapsize;
info->ordblks = ordblks; info->uordblks += region * SIZEOF_MM_ALLOCNODE; /* account for the tail node */
info->aordblks = aordblks;
info->mxordblk = mxordblk; DEBUGASSERT(info->uordblks + info->fordblks == heap->mm_heapsize);
info->uordblks = uordblks;
info->fordblks = fordblks;
return OK; return OK;
} }