malloc: Add malloc_get_info() to retrieve memory statistics

Add struct malloc_info and malloc_get_info() function to
programmatically access the memory-allocation stats that malloc_stats()
prints.

The struct contains the size of the malloc() poll and the number of
bytes in use.

Add a static inline stub to return an error when DEBUG is not defined.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
This commit is contained in:
Simon Glass
2025-11-28 08:32:36 -07:00
parent a91d1a5541
commit bd73f8182a
2 changed files with 39 additions and 0 deletions

View File

@@ -3689,6 +3689,16 @@ static struct mallinfo internal_mallinfo(mstate m) {
}
return nm;
}
int malloc_get_info(struct malloc_info *info)
{
struct mallinfo mi = internal_mallinfo(gm);
info->total_bytes = mem_malloc_end - mem_malloc_start;
info->in_use_bytes = mi.uordblks;
return 0;
}
#endif /* !NO_MALLINFO */
#if !NO_MALLOC_STATS

View File

@@ -665,8 +665,22 @@ void mspace_inspect_all(mspace msp,
/* --------------------- U-Boot additions --------------------- */
#ifdef __UBOOT__
#include <linux/errno.h>
#include <linux/types.h>
/**
* struct malloc_info - Memory allocation statistics
*
* This is filled in by malloc_get_info().
*
* @total_bytes: Total bytes available in the heap
* @in_use_bytes: Current bytes allocated (in use by application)
*/
struct malloc_info {
ulong total_bytes;
ulong in_use_bytes;
};
/* Memory pool boundaries */
extern ulong mem_malloc_start;
extern ulong mem_malloc_end;
@@ -735,6 +749,21 @@ void *memalign_simple(size_t alignment, size_t bytes);
*/
int initf_malloc(void);
/**
* malloc_get_info() - Get memory allocation statistics
*
* @info: Place to put the statistics
* Return: 0 on success, -ENOSYS if not available (DEBUG not defined)
*/
#ifdef DEBUG
int malloc_get_info(struct malloc_info *info);
#else
static inline int malloc_get_info(struct malloc_info *info)
{
return -ENOSYS;
}
#endif
#endif /* __UBOOT__ */
#ifdef __cplusplus