[Concept,29/37] malloc: Allow building dlmalloc with SPL_SYS_MALLOC_SIMPLE

Message ID 20251201170529.3237986-30-sjg@u-boot.org
State New
Headers
Series malloc: Import dlmalloc 2.8.6 |

Commit Message

Simon Glass Dec. 1, 2025, 5:05 p.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

When building boards that use CONFIG_SPL_SYS_MALLOC_SIMPLE (like
qemu-x86_64), we need to avoid a conflict between the stub free()
function defined by malloc and the real free() defined by dlmalloc.c

Fix this by define COMPILING_DLMALLOC in dlmalloc.c before including
malloc.h and adding a guard to the latter.

Co-developed-by: Claude <noreply@anthropic.com>
Signed-off-by: Simon Glass <simon.glass@canonical.com>
---

 common/dlmalloc.c | 2 ++
 include/malloc.h  | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
  

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 9330848d059..869473b2bd1 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -603,6 +603,8 @@  static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(
 #define MALLOC_FAILURE_ACTION
 #define ABORT do {} while (1)
 
+#define COMPILING_DLMALLOC
+
 #include <log.h>
 #include <malloc.h>
 #include <mapmem.h>
diff --git a/include/malloc.h b/include/malloc.h
index f8f0dbb9b70..997651e5c9c 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -72,7 +72,7 @@  extern "C" {
  * 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)
+#if CONFIG_IS_ENABLED(SYS_MALLOC_SIMPLE) && !defined(COMPILING_DLMALLOC)
 #define malloc malloc_simple
 #define realloc realloc_simple
 #define calloc calloc_simple