mirror of
https://github.com/apache/nuttx.git
synced 2026-06-06 16:50:55 +08:00
arch/sim: Implement mm_mallinfo for the custom heap
Signed-off-by: Xiang Xiao <xiaoxiang@xiaomi.com> Change-Id: Idf07081869a4659490109aacf8d7905bdb485835
This commit is contained in:
@@ -410,6 +410,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;
|
||||
}
|
||||
|
||||
|
||||
@@ -28,6 +28,7 @@
|
||||
#include <string.h>
|
||||
#include <malloc.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdatomic.h>
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <sys/stat.h>
|
||||
@@ -40,6 +41,13 @@
|
||||
|
||||
#include "up_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static atomic_int g_aordblks;
|
||||
static atomic_int g_uordblks;
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
@@ -145,28 +153,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(struct host_mallinfo *info)
|
||||
void host_mallinfo(int *aordblks, int *uordblks)
|
||||
{
|
||||
struct mallinfo tmp;
|
||||
|
||||
tmp = mallinfo();
|
||||
info->arena = tmp.arena;
|
||||
info->ordblks = tmp.ordblks;
|
||||
info->aordblks = tmp.hblks;
|
||||
info->mxordblk = tmp.usmblks;
|
||||
info->uordblks = tmp.uordblks;
|
||||
info->fordblks = tmp.fordblks;
|
||||
*aordblks = g_aordblks;
|
||||
*uordblks = g_uordblks;
|
||||
}
|
||||
|
||||
@@ -94,21 +94,6 @@ struct qspi_dev_s;
|
||||
struct ioexpander_dev_s;
|
||||
struct i2c_master_s;
|
||||
|
||||
/* This describes the information about memory allocations */
|
||||
|
||||
struct host_mallinfo
|
||||
{
|
||||
int arena; /* This is the total size of memory allocated
|
||||
* for use by malloc in bytes. */
|
||||
int ordblks; /* This is the number of free (not in use) chunks */
|
||||
int aordblks; /* This is the number of allocated (in use) chunks */
|
||||
int mxordblk; /* Size of the largest free (not in use) chunk */
|
||||
int uordblks; /* This is the total size of memory occupied by
|
||||
* chunks handed out by malloc. */
|
||||
int fordblks; /* This is the total size of memory occupied
|
||||
* by free (not in use) chunks. */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Public Data
|
||||
****************************************************************************/
|
||||
@@ -163,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