malloc: Minor cleanups

Message ID PAWPR08MB89824CB0A8E3BE463CAD192A83122@PAWPR08MB8982.eurprd08.prod.outlook.com (mailing list archive)
State Committed
Headers
Series malloc: Minor cleanups |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Build passed
redhat-pt-bot/TryBot-32bit success Build for i686
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_glibc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm fail Patch failed to apply

Commit Message

Wilco Dijkstra June 2, 2026, 4:01 p.m. UTC
  Merge request2size into checked_request2size.  Improve interface of
clear_memory.  Fix incorrect use of __glibc_unlikely in __libc_calloc. 
Fix missing tabs.

Passes regress, OK for commit?

---
  

Comments

Yury Khrustalev June 3, 2026, 10:51 a.m. UTC | #1
On Tue, Jun 02, 2026 at 04:01:49PM +0000, Wilco Dijkstra wrote:
> 
> Merge request2size into checked_request2size.  Improve interface of
> clear_memory.  Fix incorrect use of __glibc_unlikely in __libc_calloc. 
> Fix missing tabs.
> 
> Passes regress, OK for commit?

Checked on aarch64-linux-gnu, x86-64-pc-linux-gnu and i686-pc-linux-gnu.

LGTM.

Reviewed-by: Yury Khrustalev <yury.khrustalev@arm.com>
  

Patch

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 18fe48e0016adc83a9c2f1db4ad872067f343112..b5b09fb8b0ccd7155ef798a6ece789b2ca3f1e22 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1126,14 +1126,6 @@  nextchunk-> +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
 
 #define misaligned_chunk(p) (misaligned_mem( chunk2mem (p)))
 
-/* pad request bytes into a usable size -- internal version */
-/* Note: This must be a macro that evaluates to a compile time constant
-   if passed a literal constant.  */
-#define request2size(req)                                         \
-  (((req) + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE)  ?             \
-   MINSIZE :                                                      \
-   ((req) + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK)
-
 /* Check if REQ overflows when padded and aligned and if the resulting
    value is less than PTRDIFF_T.  Returns the requested size or
    MINSIZE in case the value is less than MINSIZE, or SIZE_MAX if any
@@ -1143,7 +1135,10 @@  checked_request2size (size_t req) __nonnull (1)
 {
   if (__glibc_unlikely (req > PTRDIFF_MAX))
     return SIZE_MAX;
-  return request2size (req);
+
+  return (req + SIZE_SZ + MALLOC_ALIGN_MASK < MINSIZE
+	  ? MINSIZE
+	  : (req + SIZE_SZ + MALLOC_ALIGN_MASK) & ~MALLOC_ALIGN_MASK);
 }
 
 /*
@@ -3035,7 +3030,7 @@  tcache_init (mstate av)
   size_t bytes = sizeof (tcache_perthread_struct);
   if (av)
     tcache =
-      (tcache_perthread_struct *) _int_malloc (av, request2size (bytes));
+      (tcache_perthread_struct *) _int_malloc (av, bytes);
   else
     tcache = (tcache_perthread_struct *) __libc_malloc2 (bytes);
 
@@ -3433,7 +3428,6 @@  __libc_calloc2 (size_t sz)
   mchunkptr oldtop, p;
   INTERNAL_SIZE_T oldtopsize, csz;
   void *mem;
-  unsigned long clearsize;
 
   if (SINGLE_THREAD_P)
     av = &main_arena;
@@ -3510,8 +3504,7 @@  __libc_calloc2 (size_t sz)
     }
 #endif
 
-  clearsize = csz - SIZE_SZ;
-  return clear_memory ((INTERNAL_SIZE_T *) mem, clearsize);
+  return clear_memory (mem, csz - SIZE_SZ);
 }
 
 void *
@@ -3532,16 +3525,13 @@  __libc_calloc (size_t n, size_t elem_size)
     {
       size_t tc_idx = csize2tidx (nb);
 
-      if (__glibc_unlikely (tc_idx < TCACHE_SMALL_BINS))
-        {
+      if (__glibc_likely (tc_idx < TCACHE_SMALL_BINS))
+	{
 	  if (tcache->entries[tc_idx] != NULL)
-	    {
-	      void *mem = tcache_get (tc_idx);
-	      return clear_memory ((INTERNAL_SIZE_T *) mem, tidx2usize (tc_idx));
-	    }
+	    return clear_memory (tcache_get (tc_idx), tidx2usize (tc_idx));
 	}
       else
-        {
+	{
 	  tc_idx = large_csize2tidx (nb);
 	  void *mem = tcache_get_large (tc_idx, nb);
 	  if (mem != NULL)
@@ -4754,7 +4744,7 @@  do_set_tcache_max (size_t value)
   if (value > PTRDIFF_MAX)
     return 0;
 
-  size_t nb = request2size (value);
+  size_t nb = checked_request2size (value);
   size_t tc_idx = csize2tidx (nb);
 
   if (tc_idx >= TCACHE_SMALL_BINS)
diff --git a/sysdeps/generic/calloc-clear-memory.h b/sysdeps/generic/calloc-clear-memory.h
index bce9979e66bb850c05b068792b5f00a9afcb9290..b121d6b363a0eb865c421ae34885bf371c750dac 100644
--- a/sysdeps/generic/calloc-clear-memory.h
+++ b/sysdeps/generic/calloc-clear-memory.h
@@ -19,12 +19,13 @@ 
 #include <string.h>
 
 static __always_inline void *
-clear_memory (INTERNAL_SIZE_T *d, unsigned long clearsize)
+clear_memory (void *p, unsigned long clearsize)
 {
+  INTERNAL_SIZE_T *d = (INTERNAL_SIZE_T *) p;
   /* Unroll clear memory size up to 9 * INTERNAL_SIZE_T bytes.  We know
      that contents have an odd number of INTERNAL_SIZE_T-sized words;
      minimally 3 words.  */
-  unsigned long nclears = clearsize / sizeof (INTERNAL_SIZE_T);
+  unsigned long nclears = clearsize / SIZE_SZ;
 
   if (nclears > 9)
     return memset (d, 0, clearsize);