From: Simon Glass <simon.glass@canonical.com> 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> --- common/dlmalloc.c | 51 +++++++++++++++++++++++++++++++++++++++++++++++ include/malloc.h | 27 +++++++++++++++++++++++++ 2 files changed, 78 insertions(+) diff --git a/common/dlmalloc.c b/common/dlmalloc.c index ba8ca5bd0ae..2be2262ebeb 100644 --- a/common/dlmalloc.c +++ b/common/dlmalloc.c @@ -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__ */ diff --git a/include/malloc.h b/include/malloc.h index 4608082d2e0..e0a5b732203 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -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 -- 2.43.0