]> git.ipfire.org Git - thirdparty/glibc.git/commitdiff
malloc: Inline tcache_try_malloc
authorWilco Dijkstra <wilco.dijkstra@arm.com>
Wed, 16 Apr 2025 12:21:56 +0000 (12:21 +0000)
committerWilco Dijkstra <wilco.dijkstra@arm.com>
Thu, 1 May 2025 20:01:53 +0000 (20:01 +0000)
Inline tcache_try_malloc into calloc since it is the only caller.  Also fix
usize2tidx and use it in __libc_malloc, __libc_calloc and _mid_memalign.
The result is simpler, cleaner code.

Reviewed-by: DJ Delorie <dj@redhat.com>
malloc/malloc.c

index 23b9306a1926d8541df9efdaa4844215506c09bc..9d860eac9cc923ef8f20218eb37f35b926f3ef82 100644 (file)
 /* When "x" is from chunksize().  */
 # define csize2tidx(x) (((x) - MINSIZE) / MALLOC_ALIGNMENT)
 /* When "x" is a user-provided size.  */
-# define usize2tidx(x) csize2tidx (request2size (x))
+# define usize2tidx(x) csize2tidx (checked_request2size (x))
 
 /* With rounding and alignment, the bins are...
    idx 0   bytes 0..24 (64-bit) or 0..12 (32-bit)
@@ -3325,34 +3325,6 @@ tcache_init(void)
   if (__glibc_unlikely (tcache == NULL)) \
     tcache_init();
 
-/* Trying to alloc BYTES from tcache. If tcache is available, chunk
-   is allocated and stored to MEMPTR, otherwise, MEMPTR is NULL.
-   It returns true if error occurs, else false. */
-static __always_inline bool
-tcache_try_malloc (size_t bytes, void **memptr)
-{
-  /* int_free also calls request2size, be careful to not pad twice.  */
-  size_t tbytes = checked_request2size (bytes);
-  if (tbytes == 0)
-    {
-      __set_errno (ENOMEM);
-      return true;
-    }
-
-  size_t tc_idx = csize2tidx (tbytes);
-
-  if (tcache_available (tc_idx))
-    {
-      *memptr = tcache_get (tc_idx);
-      return false;
-    }
-  else
-    *memptr = NULL;
-
-  MAYBE_INIT_TCACHE ();
-  return false;
-}
-
 #else  /* !USE_TCACHE */
 # define MAYBE_INIT_TCACHE()
 
@@ -3411,7 +3383,7 @@ void *
 __libc_malloc (size_t bytes)
 {
 #if USE_TCACHE
-  size_t tc_idx = csize2tidx (checked_request2size (bytes));
+  size_t tc_idx = usize2tidx (bytes);
 
   if (tcache_available (tc_idx))
     return tag_new_usable (tcache_get (tc_idx));
@@ -3672,14 +3644,7 @@ _mid_memalign (size_t alignment, size_t bytes, void *address)
 
 #if USE_TCACHE
   {
-    size_t tbytes;
-    tbytes = checked_request2size (bytes);
-    if (tbytes == 0)
-      {
-       __set_errno (ENOMEM);
-       return NULL;
-      }
-    size_t tc_idx = csize2tidx (tbytes);
+    size_t tc_idx = usize2tidx (bytes);
 
     if (tcache_available (tc_idx))
       {
@@ -3782,13 +3747,10 @@ __libc_calloc (size_t n, size_t elem_size)
     ptmalloc_init ();
 
 #if USE_TCACHE
-  bool err = tcache_try_malloc (bytes, &mem);
-
-  if (err)
-    return NULL;
-
-  if (mem)
+  size_t tc_idx = usize2tidx (bytes);
+  if (tcache_available (tc_idx))
     {
+      mem = tcache_get (tc_idx);
       p = mem2chunk (mem);
       if (__glibc_unlikely (mtag_enabled))
        return tag_new_zero_region (mem, memsize (p));
@@ -3797,6 +3759,7 @@ __libc_calloc (size_t n, size_t elem_size)
       clearsize = csz - SIZE_SZ;
       return clear_memory ((INTERNAL_SIZE_T *) mem, clearsize);
     }
+  MAYBE_INIT_TCACHE ();
 #endif
 
   if (SINGLE_THREAD_P)