about summary refs log tree commit diff
path: root/malloc
diff options
context:
space:
mode:
authorDJ Delorie <dj@redhat.com>2020-09-01 16:17:25 -0400
committerDJ Delorie <dj@redhat.com>2020-09-17 18:49:30 -0400
commitcdf645427d176197b82f44308a5e131d69fb53ad (patch)
treed591da3901136905771fdf4273f4560868ab9a4d /malloc
parentd38e1bbda0be2d184f0496001cfeab00216fe01c (diff)
downloadglibc-cdf645427d176197b82f44308a5e131d69fb53ad.tar.gz
glibc-cdf645427d176197b82f44308a5e131d69fb53ad.tar.xz
glibc-cdf645427d176197b82f44308a5e131d69fb53ad.zip
Update mallinfo2 ABI, and test
This patch adds the ABI-related bits to reflect the new mallinfo2
function, and adds a test case to verify basic functionality.

Reviewed-by: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Diffstat (limited to 'malloc')
-rw-r--r--malloc/Makefile2
-rw-r--r--malloc/Versions3
-rw-r--r--malloc/malloc.c2
-rw-r--r--malloc/tst-mallinfo2.c83
4 files changed, 89 insertions, 1 deletions
diff --git a/malloc/Makefile b/malloc/Makefile
index e22cbde22d..09ae63bee5 100644
--- a/malloc/Makefile
+++ b/malloc/Makefile
@@ -35,7 +35,7 @@ tests := mallocbug tst-malloc tst-valloc tst-calloc tst-obstack \
 	 tst-interpose-thread \
 	 tst-alloc_buffer \
 	 tst-malloc-tcache-leak \
-	 tst-malloc_info \
+	 tst-malloc_info tst-mallinfo2 \
 	 tst-malloc-too-large \
 	 tst-malloc-stats-cancellation \
 	 tst-tcfree1 tst-tcfree2 tst-tcfree3 \
diff --git a/malloc/Versions b/malloc/Versions
index 2357cff3da..94c8ba8040 100644
--- a/malloc/Versions
+++ b/malloc/Versions
@@ -64,6 +64,9 @@ libc {
   GLIBC_2.26 {
     reallocarray;
   }
+  GLIBC_2.33 {
+    mallinfo2;
+  }
   GLIBC_PRIVATE {
     # Internal startup hook for libpthread.
     __libc_malloc_pthread_startup;
diff --git a/malloc/malloc.c b/malloc/malloc.c
index 560fee2c31..cd9933b4e5 100644
--- a/malloc/malloc.c
+++ b/malloc/malloc.c
@@ -639,6 +639,7 @@ libc_hidden_proto (__libc_mallopt)
   thus be inaccurate.
 */
 struct mallinfo2 __libc_mallinfo2(void);
+libc_hidden_proto (__libc_mallinfo2)
 
 struct mallinfo __libc_mallinfo(void);
 
@@ -4999,6 +5000,7 @@ __libc_mallinfo2 (void)
 
   return m;
 }
+libc_hidden_def (__libc_mallinfo2)
 
 struct mallinfo
 __libc_mallinfo (void)
diff --git a/malloc/tst-mallinfo2.c b/malloc/tst-mallinfo2.c
new file mode 100644
index 0000000000..39a70d7df2
--- /dev/null
+++ b/malloc/tst-mallinfo2.c
@@ -0,0 +1,83 @@
+/* Smoke test for mallinfo2
+   Copyright (C) 2020 Free Software Foundation, Inc.
+   This file is part of the GNU C Library.
+
+   The GNU C Library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2.1 of the License, or (at your option) any later version.
+
+   The GNU C Library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with the GNU C Library; if not, see
+   <https://www.gnu.org/licenses/>.  */
+
+/* Test that mallinfo2 is properly exported and basically works.  */
+
+#include <array_length.h>
+#include <malloc.h>
+#include <stdlib.h>
+#include <support/check.h>
+
+/* This is not specifically needed for the test, but (1) does
+   something to the data so gcc doesn't optimize it away, and (2) may
+   help when developing future tests.  */
+static void
+print_mi (const char *msg, struct mallinfo2 *m)
+{
+  printf("\n%s...\n", msg);
+#define P(f) printf("%s: %zu\n", #f, m->f);
+  P(arena);
+  P(ordblks);
+  P(smblks);
+  P(hblks);
+  P(hblkhd);
+  P(usmblks);
+  P(fsmblks);
+  P(uordblks);
+  P(fordblks);
+  P(keepcost);
+}
+
+/* We do this to force the call to malloc to not be optimized
+   away.  */
+volatile void *ptr;
+
+static int
+do_test (void)
+{
+  struct mallinfo2 mi1, mi2;
+  int i;
+  size_t total = 0;
+
+  /* This is the key difference between mallinfo() and mallinfo2().
+     It may be a false positive if int and size_t are the same
+     size.  */
+  TEST_COMPARE (sizeof (mi1.arena), sizeof (size_t));
+
+  mi1 = mallinfo2 ();
+  print_mi ("before", &mi1);
+
+  /* Allocations that are meaningful-sized but not so large as to be
+     mmapped, so that they're all accounted for in the field we test
+     below.  */
+  for (i = 1; i < 20; ++i)
+    {
+      ptr = malloc (160 * i);
+      total += 16 * i;
+    }
+
+  mi2 = mallinfo2 ();
+  print_mi ("after", &mi2);
+
+  /* Check at least something changed.  */
+  TEST_VERIFY (mi2.uordblks > mi1.uordblks + total);
+
+  return 0;
+}
+
+#include <support/test-driver.c>