[patch v4] malloc: avoid need for tcache == NULL checks
Florian Weimer
fweimer@redhat.com
Fri Sep 5 10:20:48 GMT 2025
* DJ Delorie:
> /* Initialize tcache. In the rare case there isn't any memory available,
> later calls will retry initialization. */
> static void
> -tcache_init (void)
> +tcache_init (mstate av)
> {
> - if (tcache_shutting_down)
> + /* Set this unconditionally to avoid infinite loops. */
> + tcache_set_disabled ();
> + if (mp_.tcache_count == 0)
> return;
>
> size_t bytes = sizeof (tcache_perthread_struct);
> - tcache = (tcache_perthread_struct *) __libc_malloc2 (bytes);
> + if (av)
> + tcache = (tcache_perthread_struct *) _int_malloc (av, request2size (bytes));
I think this is too long.
> + else
> + tcache = (tcache_perthread_struct *) __libc_malloc2 (bytes);
>
> - if (tcache != NULL)
> + if (tcache == NULL)
> + {
> + /* If the allocation failed, don't try again. */
> + tcache_set_disabled ();
> + }
> + else
Already set above.
> @@ -4030,10 +4073,14 @@ _int_malloc (mstate av, size_t bytes)
> /* While we're here, if we see other chunks of the same size,
> stash them in the tcache. */
> size_t tc_idx = csize2tidx (nb);
> - if (tcache != NULL && tc_idx < mp_.tcache_small_bins)
> + if (tc_idx < mp_.tcache_small_bins)
> {
> mchunkptr tc_victim;
>
> +#if USE_TCACHE
> + if (__glibc_unlikely (tcache_inactive ()))
> + tcache_init (av);
> +#endif
> /* While bin not empty and tcache not full, copy chunks. */
> while (tcache->num_slots[tc_idx] != 0 && (tc_victim = *fb) != NULL)
> {
I'm still concerned that tcache_init invalidates *fb because it triggers
consolidation.
Wilco, what do you thinK?
Thanks,
Florian
More information about the Libc-alpha
mailing list