8a8cfb
commit 91d5989356325759503311df67e750b358ef4148
8a8cfb
Author: Niklas Hambüchen <mail@nh2.me>
8a8cfb
Date:   Thu Aug 8 22:02:27 2019 +0200
8a8cfb
8a8cfb
    malloc: Fix missing accounting of top chunk in malloc_info [BZ #24026]
8a8cfb
    
8a8cfb
    Fixes `<total type="rest" size="..."> incorrectly showing as 0 most
8a8cfb
    of the time.
8a8cfb
    
8a8cfb
    The rest value being wrong is significant because to compute the
8a8cfb
    actual amount of memory handed out via malloc, the user must subtract
8a8cfb
    it from <system type="current" size="...">. That result being wrong
8a8cfb
    makes investigating memory fragmentation issues like
8a8cfb
    <https://bugzilla.redhat.com/show_bug.cgi?id=843478> close to
8a8cfb
    impossible.
8a8cfb
    
8a8cfb
    (cherry picked from commit b6d2c4475d5abc05dd009575b90556bdd3c78ad0)
8a8cfb
8a8cfb
diff --git a/malloc/malloc.c b/malloc/malloc.c
8a8cfb
index 4fc7f175fe42d6c6..fcf480acdaea1b86 100644
8a8cfb
--- a/malloc/malloc.c
8a8cfb
+++ b/malloc/malloc.c
8a8cfb
@@ -5433,6 +5433,12 @@ __malloc_info (int options, FILE *fp)
8a8cfb
 
8a8cfb
       __libc_lock_lock (ar_ptr->mutex);
8a8cfb
 
8a8cfb
+      /* Account for top chunk.  The top-most available chunk is
8a8cfb
+	 treated specially and is never in any bin. See "initial_top"
8a8cfb
+	 comments.  */
8a8cfb
+      avail = chunksize (ar_ptr->top);
8a8cfb
+      nblocks = 1;  /* Top always exists.  */
8a8cfb
+
8a8cfb
       for (size_t i = 0; i < NFASTBINS; ++i)
8a8cfb
 	{
8a8cfb
 	  mchunkptr p = fastbin (ar_ptr, i);