diff --git a/arch/sim/src/sim/up_heap.c b/arch/sim/src/sim/up_heap.c index acc8f17ac55..0baf3ca4e5f 100644 --- a/arch/sim/src/sim/up_heap.c +++ b/arch/sim/src/sim/up_heap.c @@ -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; } diff --git a/arch/sim/src/sim/up_hostmemory.c b/arch/sim/src/sim/up_hostmemory.c index 82a38dbebf5..6d69cedb150 100644 --- a/arch/sim/src/sim/up_hostmemory.c +++ b/arch/sim/src/sim/up_hostmemory.c @@ -28,6 +28,7 @@ #include #include #include +#include #include #include @@ -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; } diff --git a/arch/sim/src/sim/up_internal.h b/arch/sim/src/sim/up_internal.h index e9e21f585d4..1fc0743818e 100644 --- a/arch/sim/src/sim/up_internal.h +++ b/arch/sim/src/sim/up_internal.h @@ -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 ************************************************************/