[Concept,33/37] malloc: Add SIMPLE_MEMALIGN to simplify memalign for code size

Message ID 20251201170529.3237986-34-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 a new SIMPLE_MEMALIGN to remove the fallback-retry logic from
memalign(), to reduce code sizein SPL.

The fallback mechanism attempts multiple allocation strategies:
1. Over-allocate to guarantee finding aligned space
2. If that fails, allocate exact size and check if aligned
3. If not aligned, free and retry with calculated extra space

While this fallback is useful in low-memory situations, SPL typically
has predictable memory usage and sufficient heap space for the initial
over-allocation to succeed. The fallback adds code complexity without
obvious practical benefit.

This reduces code size on imx8mp_venice (for example) SPL by 74 bytes.

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

 common/dlmalloc.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
  

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 13ae0e10918..65bfb97e1db 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -597,9 +597,11 @@  static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(
 
 #if CONFIG_IS_ENABLED(SYS_MALLOC_SMALL)
 #define NO_REALLOC_IN_PLACE 1
+#define SIMPLE_MEMALIGN 1
 #define NO_TREE_BINS 1
 #else
 #define NO_TREE_BINS 0
+#define SIMPLE_MEMALIGN 0
 #endif
 
 /* Use simplified sys_alloc for non-sandbox builds */
@@ -5260,7 +5262,7 @@  static void* internal_memalign(mstate m, size_t alignment, size_t bytes) {
     size_t nb = request2size(bytes);
     size_t req = nb + alignment + MIN_CHUNK_SIZE - CHUNK_OVERHEAD;
     mem = internal_malloc(m, req);
-#ifdef __UBOOT__
+#if defined(__UBOOT__) && !SIMPLE_MEMALIGN
     /*
      * The attempt to over-allocate (with a size large enough to guarantee the
      * ability to find an aligned region within allocated memory) failed.