d8307d
commit 1ecba1fafc160ca70f81211b23f688df8676e612
d8307d
Author: Florian Weimer <fweimer@redhat.com>
d8307d
Date:   Mon Nov 12 14:15:14 2018 +0100
d8307d
d8307d
    malloc: Convert the unlink macro to the unlink_chunk function
d8307d
    
d8307d
    This commit is in preparation of turning the macro into a proper
d8307d
    function.  The output arguments of the macro were in fact unused.
d8307d
    
d8307d
    Also clean up uses of __builtin_expect.
d8307d
d8307d
diff --git a/malloc/arena.c b/malloc/arena.c
d8307d
index 497ae475e7a85902..ff8fd5d2a7e51ac8 100644
d8307d
--- a/malloc/arena.c
d8307d
+++ b/malloc/arena.c
d8307d
@@ -596,7 +596,7 @@ heap_trim (heap_info *heap, size_t pad)
d8307d
 {
d8307d
   mstate ar_ptr = heap->ar_ptr;
d8307d
   unsigned long pagesz = GLRO (dl_pagesize);
d8307d
-  mchunkptr top_chunk = top (ar_ptr), p, bck, fwd;
d8307d
+  mchunkptr top_chunk = top (ar_ptr), p;
d8307d
   heap_info *prev_heap;
d8307d
   long new_size, top_size, top_area, extra, prev_size, misalign;
d8307d
 
d8307d
@@ -625,7 +625,7 @@ heap_trim (heap_info *heap, size_t pad)
d8307d
       if (!prev_inuse (p)) /* consolidate backward */
d8307d
         {
d8307d
           p = prev_chunk (p);
d8307d
-          unlink (ar_ptr, p, bck, fwd);
d8307d
+          unlink_chunk (ar_ptr, p);
d8307d
         }
d8307d
       assert (((unsigned long) ((char *) p + new_size) & (pagesz - 1)) == 0);
d8307d
       assert (((char *) p + new_size) == ((char *) heap + heap->size));
d8307d
diff --git a/malloc/malloc.c b/malloc/malloc.c
d8307d
index e450597e2e527fb7..7bfa66a56786d110 100644
d8307d
--- a/malloc/malloc.c
d8307d
+++ b/malloc/malloc.c
d8307d
@@ -1384,39 +1384,6 @@ typedef struct malloc_chunk *mbinptr;
d8307d
 #define first(b)     ((b)->fd)
d8307d
 #define last(b)      ((b)->bk)
d8307d
 
d8307d
-/* Take a chunk off a bin list */
d8307d
-#define unlink(AV, P, BK, FD) {                                            \
d8307d
-    if (__builtin_expect (chunksize(P) != prev_size (next_chunk(P)), 0))      \
d8307d
-      malloc_printerr ("corrupted size vs. prev_size");			      \
d8307d
-    FD = P->fd;								      \
d8307d
-    BK = P->bk;								      \
d8307d
-    if (__builtin_expect (FD->bk != P || BK->fd != P, 0))		      \
d8307d
-      malloc_printerr ("corrupted double-linked list");			      \
d8307d
-    else {								      \
d8307d
-        FD->bk = BK;							      \
d8307d
-        BK->fd = FD;							      \
d8307d
-        if (!in_smallbin_range (chunksize_nomask (P))			      \
d8307d
-            && __builtin_expect (P->fd_nextsize != NULL, 0)) {		      \
d8307d
-	    if (__builtin_expect (P->fd_nextsize->bk_nextsize != P, 0)	      \
d8307d
-		|| __builtin_expect (P->bk_nextsize->fd_nextsize != P, 0))    \
d8307d
-	      malloc_printerr ("corrupted double-linked list (not small)");   \
d8307d
-            if (FD->fd_nextsize == NULL) {				      \
d8307d
-                if (P->fd_nextsize == P)				      \
d8307d
-                  FD->fd_nextsize = FD->bk_nextsize = FD;		      \
d8307d
-                else {							      \
d8307d
-                    FD->fd_nextsize = P->fd_nextsize;			      \
d8307d
-                    FD->bk_nextsize = P->bk_nextsize;			      \
d8307d
-                    P->fd_nextsize->bk_nextsize = FD;			      \
d8307d
-                    P->bk_nextsize->fd_nextsize = FD;			      \
d8307d
-                  }							      \
d8307d
-              } else {							      \
d8307d
-                P->fd_nextsize->bk_nextsize = P->bk_nextsize;		      \
d8307d
-                P->bk_nextsize->fd_nextsize = P->fd_nextsize;		      \
d8307d
-              }								      \
d8307d
-          }								      \
d8307d
-      }									      \
d8307d
-}
d8307d
-
d8307d
 /*
d8307d
    Indexing
d8307d
 
d8307d
@@ -1489,6 +1456,46 @@ typedef struct malloc_chunk *mbinptr;
d8307d
 #define bin_index(sz) \
d8307d
   ((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz))
d8307d
 
d8307d
+/* Take a chunk off a bin list.  */
d8307d
+static void
d8307d
+unlink_chunk (mstate av, mchunkptr p)
d8307d
+{
d8307d
+  if (chunksize (p) != prev_size (next_chunk (p)))
d8307d
+    malloc_printerr ("corrupted size vs. prev_size");
d8307d
+
d8307d
+  mchunkptr fd = p->fd;
d8307d
+  mchunkptr bk = p->bk;
d8307d
+
d8307d
+  if (__builtin_expect (fd->bk != p || bk->fd != p, 0))
d8307d
+    malloc_printerr ("corrupted double-linked list");
d8307d
+
d8307d
+  fd->bk = bk;
d8307d
+  bk->fd = fd;
d8307d
+  if (!in_smallbin_range (chunksize_nomask (p)) && p->fd_nextsize != NULL)
d8307d
+    {
d8307d
+      if (p->fd_nextsize->bk_nextsize != p
d8307d
+	  || p->bk_nextsize->fd_nextsize != p)
d8307d
+	malloc_printerr ("corrupted double-linked list (not small)");
d8307d
+
d8307d
+      if (fd->fd_nextsize == NULL)
d8307d
+	{
d8307d
+	  if (p->fd_nextsize == p)
d8307d
+	    fd->fd_nextsize = fd->bk_nextsize = fd;
d8307d
+	  else
d8307d
+	    {
d8307d
+	      fd->fd_nextsize = p->fd_nextsize;
d8307d
+	      fd->bk_nextsize = p->bk_nextsize;
d8307d
+	      p->fd_nextsize->bk_nextsize = fd;
d8307d
+	      p->bk_nextsize->fd_nextsize = fd;
d8307d
+	    }
d8307d
+	}
d8307d
+      else
d8307d
+	{
d8307d
+	  p->fd_nextsize->bk_nextsize = p->bk_nextsize;
d8307d
+	  p->bk_nextsize->fd_nextsize = p->fd_nextsize;
d8307d
+	}
d8307d
+    }
d8307d
+}
d8307d
 
d8307d
 /*
d8307d
    Unsorted chunks
d8307d
@@ -3917,7 +3924,7 @@ _int_malloc (mstate av, size_t bytes)
d8307d
                 victim = victim->fd;
d8307d
 
d8307d
               remainder_size = size - nb;
d8307d
-              unlink (av, victim, bck, fwd);
d8307d
+              unlink_chunk (av, victim);
d8307d
 
d8307d
               /* Exhaust */
d8307d
               if (remainder_size < MINSIZE)
d8307d
@@ -4019,7 +4026,7 @@ _int_malloc (mstate av, size_t bytes)
d8307d
               remainder_size = size - nb;
d8307d
 
d8307d
               /* unlink */
d8307d
-              unlink (av, victim, bck, fwd);
d8307d
+              unlink_chunk (av, victim);
d8307d
 
d8307d
               /* Exhaust */
d8307d
               if (remainder_size < MINSIZE)
d8307d
@@ -4308,7 +4315,7 @@ _int_free (mstate av, mchunkptr p, int have_lock)
d8307d
       p = chunk_at_offset(p, -((long) prevsize));
d8307d
       if (__glibc_unlikely (chunksize(p) != prevsize))
d8307d
         malloc_printerr ("corrupted size vs. prev_size while consolidating");
d8307d
-      unlink(av, p, bck, fwd);
d8307d
+      unlink_chunk (av, p);
d8307d
     }
d8307d
 
d8307d
     if (nextchunk != av->top) {
d8307d
@@ -4317,7 +4324,7 @@ _int_free (mstate av, mchunkptr p, int have_lock)
d8307d
 
d8307d
       /* consolidate forward */
d8307d
       if (!nextinuse) {
d8307d
-	unlink(av, nextchunk, bck, fwd);
d8307d
+	unlink_chunk (av, nextchunk);
d8307d
 	size += nextsize;
d8307d
       } else
d8307d
 	clear_inuse_bit_at_offset(nextchunk, 0);
d8307d
@@ -4430,8 +4437,6 @@ static void malloc_consolidate(mstate av)
d8307d
   INTERNAL_SIZE_T nextsize;
d8307d
   INTERNAL_SIZE_T prevsize;
d8307d
   int             nextinuse;
d8307d
-  mchunkptr       bck;
d8307d
-  mchunkptr       fwd;
d8307d
 
d8307d
   atomic_store_relaxed (&av->have_fastchunks, false);
d8307d
 
d8307d
@@ -4471,7 +4476,7 @@ static void malloc_consolidate(mstate av)
d8307d
 	  p = chunk_at_offset(p, -((long) prevsize));
d8307d
 	  if (__glibc_unlikely (chunksize(p) != prevsize))
d8307d
 	    malloc_printerr ("corrupted size vs. prev_size in fastbins");
d8307d
-	  unlink(av, p, bck, fwd);
d8307d
+	  unlink_chunk (av, p);
d8307d
 	}
d8307d
 
d8307d
 	if (nextchunk != av->top) {
d8307d
@@ -4479,7 +4484,7 @@ static void malloc_consolidate(mstate av)
d8307d
 
d8307d
 	  if (!nextinuse) {
d8307d
 	    size += nextsize;
d8307d
-	    unlink(av, nextchunk, bck, fwd);
d8307d
+	    unlink_chunk (av, nextchunk);
d8307d
 	  } else
d8307d
 	    clear_inuse_bit_at_offset(nextchunk, 0);
d8307d
 
d8307d
@@ -4527,9 +4532,6 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
d8307d
   mchunkptr        remainder;       /* extra space at end of newp */
d8307d
   unsigned long    remainder_size;  /* its size */
d8307d
 
d8307d
-  mchunkptr        bck;             /* misc temp for linking */
d8307d
-  mchunkptr        fwd;             /* misc temp for linking */
d8307d
-
d8307d
   unsigned long    copysize;        /* bytes to copy */
d8307d
   unsigned int     ncopies;         /* INTERNAL_SIZE_T words to copy */
d8307d
   INTERNAL_SIZE_T* s;               /* copy source */
d8307d
@@ -4579,7 +4581,7 @@ _int_realloc(mstate av, mchunkptr oldp, INTERNAL_SIZE_T oldsize,
d8307d
                (unsigned long) (nb))
d8307d
         {
d8307d
           newp = oldp;
d8307d
-          unlink (av, next, bck, fwd);
d8307d
+          unlink_chunk (av, next);
d8307d
         }
d8307d
 
d8307d
       /* allocate, copy, free */