[PATCH 2/9] malloc: remove malloc_consolidate

Dev Jain dev.jain@arm.com
Fri Oct 17 09:07:00 GMT 2025


In preparation for removal of fastbins, remove the consolidation
infrastructure of fastbins.
---
 malloc/malloc.c | 138 ------------------------------------------------
 1 file changed, 138 deletions(-)

diff --git a/malloc/malloc.c b/malloc/malloc.c
index 18aa546ec5..7a211d45f7 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1868,10 +1868,6 @@ struct malloc_state
   /* Flags (formerly in max_fast).  */
   int flags;
 
-  /* Set if the fastbin chunks contain recently inserted free blocks.  */
-  /* Note this is a bool but not all targets support atomics on booleans.  */
-  int have_fastchunks;
-
   /* Fastbins */
   mfastbinptr fastbinsY[NFASTBINS];
 
@@ -2008,7 +2004,6 @@ malloc_init_state (mstate av)
   set_noncontiguous (av);
   if (av == &main_arena)
     set_max_fast (DEFAULT_MXFAST);
-  atomic_store_relaxed (&av->have_fastchunks, false);
 
   av->top = initial_top (av);
 }
@@ -2019,7 +2014,6 @@ malloc_init_state (mstate av)
 
 static void *sysmalloc (INTERNAL_SIZE_T, mstate);
 static int      systrim (size_t, mstate);
-static void     malloc_consolidate (mstate);
 
 
 /* -------------- Early definitions for debugging hooks ---------------- */
@@ -4118,8 +4112,6 @@ _int_malloc (mstate av, size_t bytes)
   else
     {
       idx = largebin_index (nb);
-      if (atomic_load_relaxed (&av->have_fastchunks))
-        malloc_consolidate (av);
     }
 
   /*
@@ -4545,18 +4537,6 @@ _int_malloc (mstate av, size_t bytes)
           return p;
         }
 
-      /* When we are using atomic ops to free fast chunks we can get
-         here for all block sizes.  */
-      else if (atomic_load_relaxed (&av->have_fastchunks))
-        {
-          malloc_consolidate (av);
-          /* restore original bin index */
-          if (in_smallbin_range (nb))
-            idx = smallbin_index (nb);
-          else
-            idx = largebin_index (nb);
-        }
-
       /*
          Otherwise, relay to handle system-dependent cases
        */
@@ -4620,7 +4600,6 @@ _int_free_chunk (mstate av, mchunkptr p, INTERNAL_SIZE_T size, int have_lock)
 
     free_perturb (chunk2mem(p), size - CHUNK_HDR_SZ);
 
-    atomic_store_relaxed (&av->have_fastchunks, true);
     unsigned int idx = fastbin_index(size);
     fb = &fastbin (av, idx);
 
@@ -4843,9 +4822,6 @@ _int_free_maybe_consolidate (mstate av, INTERNAL_SIZE_T size)
      performed if FASTBIN_CONSOLIDATION_THRESHOLD is reached.  */
   if (size >= FASTBIN_CONSOLIDATION_THRESHOLD)
     {
-      if (atomic_load_relaxed (&av->have_fastchunks))
-	malloc_consolidate(av);
-
       if (av == &main_arena)
 	{
 #ifndef MORECORE_CANNOT_TRIM
@@ -4865,113 +4841,6 @@ _int_free_maybe_consolidate (mstate av, INTERNAL_SIZE_T size)
     }
 }
 
-/*
-  ------------------------- malloc_consolidate -------------------------
-
-  malloc_consolidate is a specialized version of free() that tears
-  down chunks held in fastbins.  Free itself cannot be used for this
-  purpose since, among other things, it might place chunks back onto
-  fastbins.  So, instead, we need to use a minor variant of the same
-  code.
-*/
-
-static void malloc_consolidate(mstate av)
-{
-  mfastbinptr*    fb;                 /* current fastbin being consolidated */
-  mfastbinptr*    maxfb;              /* last fastbin (for loop control) */
-  mchunkptr       p;                  /* current chunk being consolidated */
-  mchunkptr       nextp;              /* next chunk to consolidate */
-  mchunkptr       unsorted_bin;       /* bin header */
-  mchunkptr       first_unsorted;     /* chunk to link to */
-
-  /* These have same use as in free() */
-  mchunkptr       nextchunk;
-  INTERNAL_SIZE_T size;
-  INTERNAL_SIZE_T nextsize;
-  INTERNAL_SIZE_T prevsize;
-  int             nextinuse;
-
-  atomic_store_relaxed (&av->have_fastchunks, false);
-
-  unsorted_bin = unsorted_chunks(av);
-
-  /*
-    Remove each chunk from fast bin and consolidate it, placing it
-    then in unsorted bin. Among other reasons for doing this,
-    placing in unsorted bin avoids needing to calculate actual bins
-    until malloc is sure that chunks aren't immediately going to be
-    reused anyway.
-  */
-
-  maxfb = &fastbin (av, NFASTBINS - 1);
-  fb = &fastbin (av, 0);
-  do {
-    p = atomic_exchange_acquire (fb, NULL);
-    if (p != NULL) {
-      do {
-	{
-	  if (__glibc_unlikely (misaligned_chunk (p)))
-	    malloc_printerr ("malloc_consolidate(): "
-			     "unaligned fastbin chunk detected");
-
-	  unsigned int idx = fastbin_index (chunksize (p));
-	  if ((&fastbin (av, idx)) != fb)
-	    malloc_printerr ("malloc_consolidate(): invalid chunk size");
-	}
-
-	check_inuse_chunk(av, p);
-	nextp = REVEAL_PTR (p->fd);
-
-	/* Slightly streamlined version of consolidation code in free() */
-	size = chunksize (p);
-	nextchunk = chunk_at_offset(p, size);
-	nextsize = chunksize(nextchunk);
-
-	if (!prev_inuse(p)) {
-	  prevsize = prev_size (p);
-	  size += prevsize;
-	  p = chunk_at_offset(p, -((long) prevsize));
-	  if (__glibc_unlikely (chunksize(p) != prevsize))
-	    malloc_printerr ("corrupted size vs. prev_size in fastbins");
-	  unlink_chunk (av, p);
-	}
-
-	if (nextchunk != av->top) {
-	  nextinuse = inuse_bit_at_offset(nextchunk, nextsize);
-
-	  if (!nextinuse) {
-	    size += nextsize;
-	    unlink_chunk (av, nextchunk);
-	  } else
-	    clear_inuse_bit_at_offset(nextchunk, 0);
-
-	  first_unsorted = unsorted_bin->fd;
-	  unsorted_bin->fd = p;
-	  first_unsorted->bk = p;
-
-	  if (!in_smallbin_range (size)) {
-	    p->fd_nextsize = NULL;
-	    p->bk_nextsize = NULL;
-	  }
-
-	  set_head(p, size | PREV_INUSE);
-	  p->bk = unsorted_bin;
-	  p->fd = first_unsorted;
-	  set_foot(p, size);
-	}
-
-	else {
-	  size += nextsize;
-	  set_head(p, size | PREV_INUSE);
-	  av->top = p;
-	}
-
-      } while ( (p = nextp) != NULL);
-
-    }
-  } while (fb++ != maxfb);
-}
-
 /*
   ------------------------------ realloc ------------------------------
 */
@@ -5216,9 +5085,6 @@ _int_memalign (mstate av, size_t alignment, size_t bytes)
 static int
 mtrim (mstate av, size_t pad)
 {
-  /* Ensure all blocks are consolidated.  */
-  malloc_consolidate (av);
-
   const size_t ps = GLRO (dl_pagesize);
   int psindex = bin_index (ps);
   const size_t psm1 = ps - 1;
@@ -5633,10 +5499,6 @@ __libc_mallopt (int param_number, int value)
 
   LIBC_PROBE (memory_mallopt, 2, param_number, value);
 
-  /* We must consolidate main arena before changing max_fast
-     (see definition of set_max_fast).  */
-  malloc_consolidate (av);
-
   /* Many of these helper functions take a size_t.  We do not worry
      about overflow here, because negative int values will wrap to
      very large size_t values and the helpers have sufficient range
-- 
2.43.0



More information about the Libc-alpha mailing list