[PATCH] malloc: Turn unlink_chunk into a function

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[PATCH] malloc: Turn unlink_chunk into a function

Florian Weimer-5
Also clean up uses of __builtin_expect.

2018-11-08  Florian Weimer  <[hidden email]>

        * malloc/malloc.c (unlink_chunk): Turn macro into function.  Move
        after the definition of in_smallbin_range.  Do not use
        __builtin_expect for paths that lead to a noreturn function.  Drop
        remaining __builtin_expect (p->fd_nextsize != NULL, 0) because it
        is unclear whether this is in fact an unlikely condition.

diff --git a/malloc/malloc.c b/malloc/malloc.c
index e2546870e7..fcfe0811f3 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -1384,39 +1384,6 @@ typedef struct malloc_chunk *mbinptr;
 #define first(b)     ((b)->fd)
 #define last(b)      ((b)->bk)
 
-/* Take a chunk off a bin list */
-#define unlink_chunk(AV, P) ({ \
-    if (__builtin_expect (chunksize(P) != prev_size (next_chunk(P)), 0))      \
-      malloc_printerr ("corrupted size vs. prev_size");      \
-    mchunkptr FD = P->fd;      \
-    mchunkptr BK = P->bk;      \
-    if (__builtin_expect (FD->bk != P || BK->fd != P, 0))      \
-      malloc_printerr ("corrupted double-linked list");      \
-    else {      \
-        FD->bk = BK;      \
-        BK->fd = FD;      \
-        if (!in_smallbin_range (chunksize_nomask (P))      \
-            && __builtin_expect (P->fd_nextsize != NULL, 0)) {      \
-    if (__builtin_expect (P->fd_nextsize->bk_nextsize != P, 0)      \
- || __builtin_expect (P->bk_nextsize->fd_nextsize != P, 0))    \
-      malloc_printerr ("corrupted double-linked list (not small)");   \
-            if (FD->fd_nextsize == NULL) {      \
-                if (P->fd_nextsize == P)      \
-                  FD->fd_nextsize = FD->bk_nextsize = FD;      \
-                else {      \
-                    FD->fd_nextsize = P->fd_nextsize;      \
-                    FD->bk_nextsize = P->bk_nextsize;      \
-                    P->fd_nextsize->bk_nextsize = FD;      \
-                    P->bk_nextsize->fd_nextsize = FD;      \
-                  }      \
-              } else {      \
-                P->fd_nextsize->bk_nextsize = P->bk_nextsize;      \
-                P->bk_nextsize->fd_nextsize = P->fd_nextsize;      \
-              }      \
-          }      \
-      }      \
-})
-
 /*
    Indexing
 
@@ -1489,6 +1456,46 @@ typedef struct malloc_chunk *mbinptr;
 #define bin_index(sz) \
   ((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz))
 
+/* Take a chunk off a bin list.  */
+static void
+unlink_chunk (mstate av, mchunkptr p)
+{
+    if (chunksize (p) != prev_size (next_chunk (p)))
+      malloc_printerr ("corrupted size vs. prev_size");
+
+    mchunkptr fd = p->fd;
+    mchunkptr bk = p->bk;
+
+    if (__builtin_expect (fd->bk != p || bk->fd != p, 0))
+      malloc_printerr ("corrupted double-linked list");
+
+    fd->bk = bk;
+    bk->fd = fd;
+    if (!in_smallbin_range (chunksize_nomask (p)) && p->fd_nextsize != NULL)
+      {
+ if (p->fd_nextsize->bk_nextsize != p
+    || p->bk_nextsize->fd_nextsize != p)
+  malloc_printerr ("corrupted double-linked list (not small)");
+
+ if (fd->fd_nextsize == NULL)
+  {
+    if (p->fd_nextsize == p)
+      fd->fd_nextsize = fd->bk_nextsize = fd;
+    else
+      {
+ fd->fd_nextsize = p->fd_nextsize;
+ fd->bk_nextsize = p->bk_nextsize;
+ p->fd_nextsize->bk_nextsize = fd;
+ p->bk_nextsize->fd_nextsize = fd;
+      }
+  }
+ else
+  {
+    p->fd_nextsize->bk_nextsize = p->bk_nextsize;
+    p->bk_nextsize->fd_nextsize = p->fd_nextsize;
+  }
+      }
+}
 
 /*
    Unsorted chunks
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] malloc: Turn unlink_chunk into a function

Andreas Schwab
On Nov 08 2018, Florian Weimer <[hidden email]> wrote:

> @@ -1489,6 +1456,46 @@ typedef struct malloc_chunk *mbinptr;
>  #define bin_index(sz) \
>    ((in_smallbin_range (sz)) ? smallbin_index (sz) : largebin_index (sz))
>  
> +/* Take a chunk off a bin list.  */
> +static void
> +unlink_chunk (mstate av, mchunkptr p)
> +{
> +    if (chunksize (p) != prev_size (next_chunk (p)))

Too much indent.

Ok with that change, and please fold with the previous patch.

Andreas.

--
Andreas Schwab, SUSE Labs, [hidden email]
GPG Key fingerprint = 0196 BAD8 1CE9 1970 F4BE  1748 E4D4 88E3 0EEA B9D7
"And now for something completely different."