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