diff options
author | Cupertino Miranda <cupertino.miranda@oracle.com> | 2025-04-16 11:22:53 +0000 |
---|---|---|
committer | Wilco Dijkstra <wilco.dijkstra@arm.com> | 2025-04-16 13:09:16 +0000 |
commit | 1c9ac027a5deb6c3e026be0e88d38959529e6102 (patch) | |
tree | 10cb089ea778880bacccfd2eccf0bf187703322c | |
parent | ceeffd970c56893885cbf8382ae34b015f177850 (diff) | |
download | glibc-1c9ac027a5deb6c3e026be0e88d38959529e6102.zip glibc-1c9ac027a5deb6c3e026be0e88d38959529e6102.tar.gz glibc-1c9ac027a5deb6c3e026be0e88d38959529e6102.tar.bz2 |
malloc: move tcache_init out of hot tcache paths
This patch moves any calls of tcache_init away after tcache hot paths.
Since there is no reason to initialize tcaches in the hot path and since
we need to be able to check tcache != NULL in any case, because of
tcache_thread_shutdown function, moving tcache_init away from hot path
can only be beneficial.
The patch also removes the initialization of tcaches within the
__libc_free call. It only makes sense to initialize tcaches for the
thread after it calls one of the allocation functions. Also the patch
removes the save/restore of errno from tcache_init code, as it is no
longer needed.
-rw-r--r-- | malloc/malloc.c | 18 |
1 files changed, 6 insertions, 12 deletions
diff --git a/malloc/malloc.c b/malloc/malloc.c index f30fb4b..23b9306 100644 --- a/malloc/malloc.c +++ b/malloc/malloc.c @@ -3296,9 +3296,6 @@ tcache_init(void) if (MAX_TCACHE_SIZE >= GLRO (dl_pagesize) / 2) malloc_printerr ("max tcache size too large"); - /* Preserve errno when called from free() - _int_malloc may corrupt it. */ - int err = errno; - arena_get (ar_ptr, bytes); victim = _int_malloc (ar_ptr, bytes); if (!victim && ar_ptr != NULL) @@ -3311,8 +3308,6 @@ tcache_init(void) if (ar_ptr != NULL) __libc_lock_unlock (ar_ptr->mutex); - __set_errno (err); - /* In a low memory situation, we may not be able to allocate memory - in which case, we just keep trying later. However, we typically do this very early, so either there is sufficient @@ -3346,13 +3341,15 @@ tcache_try_malloc (size_t bytes, void **memptr) size_t tc_idx = csize2tidx (tbytes); - MAYBE_INIT_TCACHE (); - if (tcache_available (tc_idx)) - *memptr = tcache_get (tc_idx); + { + *memptr = tcache_get (tc_idx); + return false; + } else *memptr = NULL; + MAYBE_INIT_TCACHE (); return false; } @@ -3684,8 +3681,6 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) } size_t tc_idx = csize2tidx (tbytes); - MAYBE_INIT_TCACHE (); - if (tcache_available (tc_idx)) { /* The tcache itself isn't encoded, but the chain is. */ @@ -3702,6 +3697,7 @@ _mid_memalign (size_t alignment, size_t bytes, void *address) return tag_new_usable (victim); } } + MAYBE_INIT_TCACHE (); } #endif @@ -4556,8 +4552,6 @@ _int_free_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size, int have_lock) { mfastbinptr *fb; /* associated fastbin */ - MAYBE_INIT_TCACHE (); - /* If eligible, place chunk on a fastbin so it can be found and used quickly in malloc. |