[PATCH] malloc: fix large tcache code to check for exact size match

Dev Jain dev.jain@arm.com
Mon Sep 29 16:02:21 GMT 2025


The tcache is used for allocation only if an exact match is found. In the
large tcache code added in commit cbfd7988107b, we currently extract a
chunk of size greater than or equal to the size we need, but don't check
strict equality. This patch fixes that behaviour.

Built on Aarch64, all malloc tests pass.

Signed-off-by: Dev Jain <dev.jain@arm.com>
---
This patch is based on commit e7eadbb29fc54d8f450d3b0bc51814e9948a0e0d.

I couldn't get a visible perf improvement on running the largetcache
versions of the tests in the malloc testsuite, for example,
./tst-aligned-alloc-random-malloc-largetcache.

 malloc/malloc.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 802318d143..81d94312fb 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -3264,10 +3264,16 @@ tcache_get_large (size_t tc_idx, size_t nb)
 {
   tcache_entry **entry;
   bool mangled = false;
+  tcache_entry *e;
+  INTERNAL_SIZE_T e_size;
+
   entry = tcache_location_large (nb, tc_idx, &mangled);
+  e = mangled ? REVEAL_PTR (*entry) : *entry;
+  if (e == NULL)
+    return NULL;
 
-  if ((mangled && REVEAL_PTR (*entry) == NULL)
-      || (!mangled && *entry == NULL))
+  e_size = chunksize (mem2chunk (e));
+  if (e_size != nb)
     return NULL;
 
   return tcache_get_n (tc_idx, entry, mangled);
-- 
2.43.0



More information about the Libc-alpha mailing list