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"
/****************************************************************************
* Public Functions
* Private Functions
****************************************************************************/
/****************************************************************************
* 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)
static void mallinfo_handler(FAR struct mm_allocnode_s *node, FAR void *arg)
{
FAR struct mm_allocnode_s *node;
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
FAR struct mallinfo *info = arg;
DEBUGASSERT(info);
/* 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,
minfo("node=%p size=%u preceding=%u (%c)\n",
node, (unsigned int)node->size,
(unsigned int)(node->preceding & ~MM_ALLOC_BIT),
(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)
{
DEBUGASSERT(node->size >= SIZEOF_MM_ALLOCNODE);
aordblks++;
uordblks += node->size;
info->aordblks++;
info->uordblks += node->size;
}
else
{
@@ -105,36 +65,45 @@ int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
DEBUGASSERT(fnode->flink == NULL ||
fnode->flink->size == 0 ||
fnode->flink->size >= fnode->size);
ordblks++;
fordblks += node->size;
if (node->size > mxordblk)
info->ordblks++;
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));
prev = node;
}
/****************************************************************************
* Public Functions
****************************************************************************/
minfo("region=%d node=%p heapend=%p\n",
region, node, heap->mm_heapend[region]);
DEBUGASSERT(node == heap->mm_heapend[region]);
/****************************************************************************
* Name: mm_mallinfo
*
* 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 */
}
#undef region
DEBUGASSERT(info);
DEBUGASSERT(uordblks + fordblks == heap->mm_heapsize);
memset(info, 0, sizeof(*info));
mm_foreach(heap, mallinfo_handler, info);
info->arena = heap->mm_heapsize;
info->ordblks = ordblks;
info->aordblks = aordblks;
info->mxordblk = mxordblk;
info->uordblks = uordblks;
info->fordblks = fordblks;
info->uordblks += region * SIZEOF_MM_ALLOCNODE; /* account for the tail node */
DEBUGASSERT(info->uordblks + info->fordblks == heap->mm_heapsize);
return OK;
}