mirror of
https://github.com/apache/nuttx.git
synced 2026-09-23 07:14:28 +08:00
arch/sim: Implement mm_mallinfo for the custom heap
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com>
This commit is contained in:
committed by
Alan Carvalho de Assis
parent
8ebf9c92cf
commit
4589c369be
@@ -427,6 +427,7 @@ void mm_extend(FAR struct mm_heap_s *heap, FAR void *mem, size_t size,
|
||||
int mm_mallinfo(FAR struct mm_heap_s *heap, FAR struct mallinfo *info)
|
||||
{
|
||||
memset(info, 0, sizeof(struct mallinfo));
|
||||
host_mallinfo(&info->aordblks, &info->uordblks);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@
|
||||
#include <unistd.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -39,6 +40,13 @@
|
||||
|
||||
#include "up_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static atomic_int g_aordblks;
|
||||
static atomic_int g_uordblks;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
@@ -144,15 +152,59 @@ void *host_memalign(size_t alignment, size_t size)
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size = host_malloc_size(p);
|
||||
g_aordblks += 1;
|
||||
g_uordblks += size;
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
void host_free(void *mem)
|
||||
{
|
||||
size_t size;
|
||||
|
||||
if (mem == NULL)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
size = host_malloc_size(mem);
|
||||
g_aordblks -= 1;
|
||||
g_uordblks -= size;
|
||||
free(mem);
|
||||
}
|
||||
|
||||
void *host_realloc(void *oldmem, size_t size)
|
||||
{
|
||||
return realloc(oldmem, size);
|
||||
size_t oldsize;
|
||||
void *mem;
|
||||
|
||||
if (size == 0)
|
||||
{
|
||||
host_free(oldmem);
|
||||
return NULL;
|
||||
}
|
||||
else if (oldmem == NULL)
|
||||
{
|
||||
return host_memalign(sizeof(void *), size);
|
||||
}
|
||||
|
||||
oldsize = host_malloc_size(oldmem);
|
||||
mem = realloc(oldmem, size);
|
||||
if (mem == NULL)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
size = host_malloc_size(mem);
|
||||
g_uordblks -= oldsize;
|
||||
g_uordblks += size;
|
||||
|
||||
return mem;
|
||||
}
|
||||
|
||||
void host_mallinfo(int *aordblks, int *uordblks)
|
||||
{
|
||||
*aordblks = g_aordblks;
|
||||
*uordblks = g_uordblks;
|
||||
}
|
||||
|
||||
@@ -148,6 +148,7 @@ size_t host_malloc_size(void *mem);
|
||||
void *host_memalign(size_t alignment, size_t size);
|
||||
void host_free(void *mem);
|
||||
void *host_realloc(void *oldmem, size_t size);
|
||||
void host_mallinfo(int *aordblks, int *uordblks);
|
||||
|
||||
/* up_hosttime.c ************************************************************/
|
||||
|
||||
|
||||
Reference in New Issue
Block a user