malloc: Add mem_malloc_init() and sbrk()

Add mem_malloc_init() to initialise the malloc heap and sbrk() for
heap extension. These are U-Boot-specific functions that manage the
memory pool used by dlmalloc.

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 09:55:23 -07:00
parent 2ab06fd175
commit 1b2200b47b
2 changed files with 78 additions and 0 deletions

View File

@@ -542,6 +542,18 @@ MAX_RELEASE_CHECK_RATE default: 4095 unless not HAVE_MMAP
#define DLMALLOC_EXPORT extern
#endif
#ifdef __UBOOT__
#include <mapmem.h>
#include <asm/global_data.h>
DECLARE_GLOBAL_DATA_PTR;
ulong mem_malloc_start;
ulong mem_malloc_end;
ulong mem_malloc_brk;
#endif /* __UBOOT__ */
#ifndef WIN32
#ifdef _WIN32
#define WIN32 1
@@ -6290,3 +6302,42 @@ History:
structure of old version, but most details differ.)
*/
/* --------------------- U-Boot additions --------------------- */
#ifdef __UBOOT__
void *sbrk(ptrdiff_t increment)
{
ulong old = mem_malloc_brk;
ulong new = old + increment;
/* mem_malloc_end points one byte past the end, so >= is correct */
if ((new < mem_malloc_start) || (new >= mem_malloc_end))
return (void *)MORECORE_FAILURE;
/*
* if we are giving memory back make sure we clear it out since
* we set MORECORE_CLEARS to 1
*/
if (increment < 0)
memset((void *)new, '\0', -increment);
mem_malloc_brk = new;
return (void *)old;
}
void mem_malloc_init(ulong start, ulong size)
{
mem_malloc_start = (ulong)map_sysmem(start, size);
mem_malloc_end = mem_malloc_start + size;
mem_malloc_brk = mem_malloc_end;
debug("using memory %#lx-%#lx for malloc()\n", mem_malloc_start,
mem_malloc_end);
#if CONFIG_IS_ENABLED(SYS_MALLOC_CLEAR_ON_INIT)
memset((void *)mem_malloc_start, '\0', size);
#endif
}
#endif /* __UBOOT__ */

View File

@@ -625,6 +625,33 @@ void mspace_inspect_all(mspace msp,
void* arg);
#endif /* MSPACES */
/* --------------------- U-Boot additions --------------------- */
#ifdef __UBOOT__
#include <linux/types.h>
/* Memory pool boundaries */
extern ulong mem_malloc_start;
extern ulong mem_malloc_end;
extern ulong mem_malloc_brk;
/**
* mem_malloc_init() - Initialize the malloc() heap
*
* @start: Start address of heap memory region
* @size: Size of heap memory region in bytes
*/
void mem_malloc_init(ulong start, ulong size);
/**
* sbrk() - Extend the heap
*
* @increment: Number of bytes to add (or remove if negative)
* Return: Previous break value on success, MORECORE_FAILURE on error
*/
void *sbrk(ptrdiff_t increment);
#endif /* __UBOOT__ */
#ifdef __cplusplus
}; /* end of extern "C" */
#endif