[Concept,18/37] sandbox: Use a prefix for all allocation functions

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

Commit Message

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

In order to allow use of both U-Boot's malloc() and the C library's
version, set a prefix for the allocation functions so that they can
co-exist.

This is only done for sandbox. For other archs everything remains the
same.

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 cfda60f99ae237494e9341aad9676152d3bac3c9)
---

 include/malloc.h | 23 +++++++++++++++++++++++
 1 file changed, 23 insertions(+)
  

Patch

diff --git a/include/malloc.h b/include/malloc.h
index d5cccc96e50..76068032da7 100644
--- a/include/malloc.h
+++ b/include/malloc.h
@@ -60,6 +60,14 @@  extern "C" {
 
 #if !ONLY_MSPACES
 
+/*
+ * Rename the U-Boot alloc functions so that sandbox can still use the system
+ * ones
+ */
+#ifdef CONFIG_SANDBOX
+#define USE_DL_PREFIX
+#endif
+
 #ifndef USE_DL_PREFIX
 #define dlcalloc               calloc
 #define dlfree                 free
@@ -82,6 +90,21 @@  extern "C" {
 #define dlindependent_calloc   independent_calloc
 #define dlindependent_comalloc independent_comalloc
 #define dlbulk_free            bulk_free
+#else /* USE_DL_PREFIX */
+/* Ensure that U-Boot actually uses dlmalloc versions */
+#define calloc(n, s)           dlcalloc(n, s)
+#define free(p)                dlfree(p)
+#define malloc(s)              dlmalloc(s)
+#define memalign(a, s)         dlmemalign(a, s)
+#define posix_memalign(p, a, s) dlposix_memalign(p, a, s)
+#define realloc(p, s)          dlrealloc(p, s)
+#define valloc(s)              dlvalloc(s)
+#define pvalloc(s)             dlpvalloc(s)
+#define mallinfo()             dlmallinfo()
+#define mallopt(p, v)          dlmallopt(p, v)
+#define malloc_trim(s)         dlmalloc_trim(s)
+#define malloc_stats()         dlmalloc_stats()
+#define malloc_usable_size(p)  dlmalloc_usable_size(p)
 #endif /* USE_DL_PREFIX */
 
 #if !NO_MALLINFO