[PATCH v5 2/7] malloc: optimize initialization

Cupertino Miranda cupertino.miranda@oracle.com
Fri Mar 21 12:00:57 GMT 2025


This patch adds __glibc_unlikely to all conditions related to the call
to ptmalloc_init which check if __malloc_initialized variable is set.
---
 malloc/malloc.c | 26 +++++++++++++-------------
 1 file changed, 13 insertions(+), 13 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 59b22b3455..98d1fda144 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3389,7 +3389,7 @@ __libc_malloc (size_t bytes)
   _Static_assert (PTRDIFF_MAX <= SIZE_MAX / 2,
                   "PTRDIFF_MAX is not more than half of SIZE_MAX");
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 #if USE_TCACHE
   bool err = tcache_try_malloc (bytes, &victim);
@@ -3488,7 +3488,7 @@ __libc_realloc (void *oldmem, size_t bytes)
 
   void *newp;             /* chunk to return */
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
 #if REALLOC_ZERO_BYTES_FREES
@@ -3619,7 +3619,7 @@ libc_hidden_def (__libc_realloc)
 void *
 __libc_memalign (size_t alignment, size_t bytes)
 {
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   void *address = RETURN_ADDRESS (0);
@@ -3632,7 +3632,7 @@ void *
 weak_function
 aligned_alloc (size_t alignment, size_t bytes)
 {
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
 /* Similar to memalign, but starting with ISO C17 the standard
@@ -3742,7 +3742,7 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
 void *
 __libc_valloc (size_t bytes)
 {
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   void *address = RETURN_ADDRESS (0);
@@ -3753,7 +3753,7 @@ __libc_valloc (size_t bytes)
 void *
 __libc_pvalloc (size_t bytes)
 {
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   void *address = RETURN_ADDRESS (0);
@@ -3790,7 +3790,7 @@ __libc_calloc (size_t n, size_t elem_size)
 
   sz = bytes;
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
 #if USE_TCACHE
@@ -5273,7 +5273,7 @@ __malloc_trim (size_t s)
 {
   int result = 0;
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   mstate ar_ptr = &main_arena;
@@ -5392,7 +5392,7 @@ __libc_mallinfo2 (void)
   struct mallinfo2 m;
   mstate ar_ptr;
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   memset (&m, 0, sizeof (m));
@@ -5443,7 +5443,7 @@ __malloc_stats (void)
   mstate ar_ptr;
   unsigned int in_use_b = mp_.mmapped_mem, system_b = in_use_b;
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
   _IO_flockfile (stderr);
   int old_flags2 = stderr->_flags2;
@@ -5625,7 +5625,7 @@ __libc_mallopt (int param_number, int value)
   mstate av = &main_arena;
   int res = 1;
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
   __libc_lock_lock (av->mutex);
 
@@ -5848,7 +5848,7 @@ __posix_memalign (void **memptr, size_t alignment, size_t size)
 {
   void *mem;
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   /* Test whether the SIZE argument is valid.  It must be a power of
@@ -5893,7 +5893,7 @@ __malloc_info (int options, FILE *fp)
 
 
 
-  if (!__malloc_initialized)
+  if (__glibc_unlikely (!__malloc_initialized))
     ptmalloc_init ();
 
   fputs ("<malloc version=\"1\">\n", fp);
-- 
2.39.5



More information about the Libc-alpha mailing list