[Concept,36/37] malloc: Switch to the new malloc() implementation

Message ID 20251201170529.3237986-37-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>

Add CONFIG_SYS_MALLOC_LEGACY to select the current allocator and adjust
the header-file and Makefile rule to use the new dlmalloc
implementation.

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

 Kconfig          | 16 ++++++++++++++++
 common/Makefile  |  4 ++++
 include/malloc.h | 19 +++++++------------
 3 files changed, 27 insertions(+), 12 deletions(-)
  

Patch

diff --git a/Kconfig b/Kconfig
index c4a65597035..378ecfb1867 100644
--- a/Kconfig
+++ b/Kconfig
@@ -511,6 +511,22 @@  config SPL_SYS_MALLOC_SMALL
 
 	  If unsure, say Y to minimize SPL code size.
 
+config SYS_MALLOC_LEGACY
+	bool "Use legacy dlmalloc 2.6.6 instead of dlmalloc 2.8.6"
+	help
+	  Select this option to use the older dlmalloc 2.6.6 implementation
+	  instead of the newer 2.8.6 version. The legacy allocator uses a
+	  simpler bin system, has larger code size in most cases and uses more
+	  static data.
+
+	  The legacy allocator may have slightly worse fragmentation behavior
+	  for some workloads but has been well-tested over many years in U-Boot.
+
+	  This option is provided for compatibility and testing. New boards
+	  should use the default dlmalloc 2.8.6.
+
+	  If unsure, say N to use the modern allocator.
+
 config TOOLS_DEBUG
 	bool "Enable debug information for tools"
 	help
diff --git a/common/Makefile b/common/Makefile
index ffa46ce5e06..fdf4cff94f4 100644
--- a/common/Makefile
+++ b/common/Makefile
@@ -71,7 +71,11 @@  obj-$(CONFIG_BOUNCE_BUFFER) += bouncebuf.o
 obj-$(CONFIG_$(PHASE_)SERIAL) += console.o
 
 obj-$(CONFIG_CROS_EC) += cros_ec.o
+ifdef CONFIG_SYS_MALLOC_LEGACY
 obj-y += dlmalloc_old.o
+else
+obj-y += dlmalloc.o
+endif
 obj-$(CONFIG_$(PHASE_)SYS_MALLOC_F) += malloc_simple.o
 
 obj-$(CONFIG_$(PHASE_)CYCLIC) += cyclic.o
diff --git a/include/malloc.h b/include/malloc.h
index 997651e5c9c..73b2da0c383 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -1,15 +1,4 @@ 
 /* SPDX-License-Identifier: GPL-2.0+ */
-/*
- * Stub header to include the old malloc header
- *
- * This allows the old malloc implementation to be preserved while
- * preparing for a new dlmalloc version.
- */
-
-#include <malloc_old.h>
-
-#if 0 /* not active yet */
-
 /*
   Default header file for malloc-2.8.x, written by Doug Lea
   and released to the public domain, as explained at
@@ -32,6 +21,12 @@ 
   * If MSPACES is defined, declarations for mspace versions are included.
 */
 
+#ifdef CONFIG_SYS_MALLOC_LEGACY
+
+#include <malloc_old.h>
+
+#else
+
 #ifndef MALLOC_280_H
 #define MALLOC_280_H
 
@@ -748,4 +743,4 @@  int initf_malloc(void);
 
 #endif /* MALLOC_280_H */
 
-#endif /* not active yet */
+#endif /* !CONFIG_SYS_MALLOC_LEGACY */