From: Simon Glass <simon.glass@canonical.com> The C runtime calls malloc() before starting main(), e.g. to get some memory to use for dynamic linking. If CONFIG_TPL_SYS_MALLOC_SIMPLE is enabled, the calloc() symbol is defined within U-Boot. The C runtime may call that too. Add the SYS_MALLOC_SIMPLE section to the new malloc.h header to redirect malloc, realloc, calloc, and memalign to their simple implementations when SYS_MALLOC_SIMPLE is enabled. Add a COMPILING_DLMALLOC guard so that dlmalloc.c can include malloc.h without hitting the SYS_MALLOC_SIMPLE redirects, which would otherwise cause conflicts with the dlfree/free macro definitions. Changes from original commit: - Applied to new dlmalloc 2.8.6 malloc.h header structure Signed-off-by: Simon Glass <sjg@chromium.org> Co-developed-by: Claude <noreply@anthropic.com> Signed-off-by: Simon Glass <simon.glass@canonical.com> (cherry picked from commit 22f87ef53045c19df9a770c4101ed3ba744c1b35) --- include/malloc.h | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/include/malloc.h b/include/malloc.h index 72db7fdb507..f8f0dbb9b70 100644 --- a/include/malloc.h +++ b/include/malloc.h @@ -68,6 +68,24 @@ extern "C" { #define USE_DL_PREFIX #endif +/* + * When using simple malloc (SPL/TPL), redirect to simple implementations. + * Skip this when compiling dlmalloc.c itself to avoid conflicts. + */ +#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) +#define malloc malloc_simple +#define realloc realloc_simple +#define calloc calloc_simple +#define memalign memalign_simple +#if IS_ENABLED(CONFIG_VALGRIND) +#define free free_simple +#else +static inline void free(void *ptr) {} +#endif +void *calloc(size_t nmemb, size_t size); +void *realloc_simple(void *ptr, size_t size); +#else /* !SYS_MALLOC_SIMPLE || COMPILING_DLMALLOC */ + #ifndef USE_DL_PREFIX #define dlcalloc calloc #define dlfree free @@ -106,6 +124,7 @@ extern "C" { #define malloc_stats() dlmalloc_stats() #define malloc_usable_size(p) dlmalloc_usable_size(p) #endif /* USE_DL_PREFIX */ +#endif /* !SYS_MALLOC_SIMPLE || COMPILING_DLMALLOC */ #if !NO_MALLINFO #ifndef HAVE_USR_INCLUDE_MALLOC_H -- 2.43.0