[Concept,21/35] malloc: Move mcheck block after includes

Message ID 20251210000737.180797-22-sjg@u-boot.org
State New
Headers
Series malloc: Add heap debugging commands and mcheck caller tracking |

Commit Message

Simon Glass Dec. 10, 2025, 12:07 a.m. UTC
  From: Simon Glass <simon.glass@canonical.com>

Move the CONFIG_MCHECK_HEAP_PROTECTION block after the standard
includes so that size_t and string functions are available for
the inline MALLOC_ZERO and MALLOC_COPY functions.

Add the <string.h> and <linux/types.h> includes needed for mcheck.

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

 common/dlmalloc.c | 32 +++++++++++++++++---------------
 1 file changed, 17 insertions(+), 15 deletions(-)
  

Patch

diff --git a/common/dlmalloc.c b/common/dlmalloc.c
index 7258a7dda84..f59b1151606 100644
--- a/common/dlmalloc.c
+++ b/common/dlmalloc.c
@@ -572,21 +572,6 @@  MAX_RELEASE_CHECK_RATE   default: 4095 unless not HAVE_MMAP
 #define DEBUG 1
 #endif
 
-#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION)
-#define STATIC_IF_MCHECK static
-#undef MALLOC_COPY
-#undef MALLOC_ZERO
-static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); }
-static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); }
-#else
-#define STATIC_IF_MCHECK
-#define dlmalloc_impl dlmalloc
-#define dlfree_impl dlfree
-#define dlrealloc_impl dlrealloc
-#define dlmemalign_impl dlmemalign
-#define dlcalloc_impl dlcalloc
-#endif
-
 #define LACKS_FCNTL_H
 #define LACKS_UNISTD_H
 #define LACKS_SYS_PARAM_H
@@ -633,12 +618,29 @@  static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(
 #include <log.h>
 #include <malloc.h>
 #include <mapmem.h>
+#include <string.h>
 #include <vsprintf.h>
 #include <asm/global_data.h>
+#include <linux/types.h>
 #include <valgrind/memcheck.h>
 
 DECLARE_GLOBAL_DATA_PTR;
 
+#if CONFIG_IS_ENABLED(MCHECK_HEAP_PROTECTION)
+#define STATIC_IF_MCHECK static
+#undef MALLOC_COPY
+#undef MALLOC_ZERO
+static inline void MALLOC_ZERO(void *p, size_t sz) { memset(p, 0, sz); }
+static inline void MALLOC_COPY(void *dest, const void *src, size_t sz) { memcpy(dest, src, sz); }
+#else
+#define STATIC_IF_MCHECK
+#define dlmalloc_impl dlmalloc
+#define dlfree_impl dlfree
+#define dlrealloc_impl dlrealloc
+#define dlmemalign_impl dlmemalign
+#define dlcalloc_impl dlcalloc
+#endif
+
 static bool malloc_testing;	/* enable test mode */
 static int malloc_max_allocs;	/* return NULL after this many calls to malloc() */