[PATCH] malloc: refactored aligned_OK and misaligned_chunk

Wilco Dijkstra Wilco.Dijkstra@arm.com
Wed Jun 25 12:53:37 GMT 2025


Hi William,
 
> Renamed aligned_OK to misaligned_mem as to be similar
> to misaligned_chunk, and reversed any assertions using
> the macro. Made misaligned_chunk call misaligned_mem after
> chunk2mem rather than bitmasking with the malloc alignment
> itself, since misaligned_chunk is meant to test the data
> chunk itself rather than the header, and the compiler
> will optimise the addition so the ternary operator is not
> needed.

Nice cleanup! It results in some minor differences in the malloc.os object,
which slightly improves performance of bench-malloc-tcache. LGTM.

Reviewed-by: Wilco Dijkstra  <Wilco.Dijkstra@arm.com>

I'll commit it for you later.

Cheers,
Wilco


-  if (!aligned_OK (mem))
+  if (misaligned_mem (mem))
     return NULL;
 
OK

-#define aligned_OK(m)  (((unsigned long)(m) & MALLOC_ALIGN_MASK) == 0)
+#define misaligned_mem(m)  ((uintptr_t)(m) & MALLOC_ALIGN_MASK)
 
OK (unsigned long is strictly incorrect on LLP64 targets)

-#define misaligned_chunk(p) \
-  ((uintptr_t)(MALLOC_ALIGNMENT == CHUNK_HDR_SZ ? (p) : chunk2mem (p)) \
-   & MALLOC_ALIGN_MASK)
+#define misaligned_chunk(p) (misaligned_mem( chunk2mem (p)))
 
OK

-      assert (aligned_OK (chunk2mem (p)));
+      assert (!misaligned_chunk (p));

OK
 
-      assert (aligned_OK (chunk2mem (p)));
+      assert (!misaligned_chunk (p));

OK

-  assert (aligned_OK (chunk2mem (p)));
+  assert (!misaligned_chunk (p));

OK

-  assert (aligned_OK (chunk2mem (p)));
+  assert (!misaligned_chunk (p));

OK
 
-  if (__glibc_unlikely (!aligned_OK (e)))
+  if (__glibc_unlikely (misaligned_mem (e)))

OK

-         if (__glibc_unlikely (!aligned_OK (tmp)))
+         if (__glibc_unlikely (misaligned_mem (tmp)))

OK

-         if (__glibc_unlikely (!aligned_OK (e)))
+         if (__glibc_unlikely (misaligned_mem (e)))

OK


More information about the Libc-alpha mailing list