stdlib: Undo post review change to 16adc58e73f3 [BZ #27749]
[glibc.git] / malloc / tst-mallinfo2.c
blob86d08b209a30c1e02c720886a19c7afa829aa6ed
1 /* Smoke test for mallinfo2
2 Copyright (C) 2020-2023 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
5 The GNU C Library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
10 The GNU C Library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
15 You should have received a copy of the GNU Lesser General Public
16 License along with the GNU C Library; if not, see
17 <https://www.gnu.org/licenses/>. */
19 /* Test that mallinfo2 is properly exported and basically works. */
21 #include <array_length.h>
22 #include <malloc.h>
23 #include <stdlib.h>
24 #include <support/check.h>
26 /* This is not specifically needed for the test, but (1) does
27 something to the data so gcc doesn't optimize it away, and (2) may
28 help when developing future tests. */
29 static void
30 print_mi (const char *msg, struct mallinfo2 *m)
32 printf("\n%s...\n", msg);
33 #define P(f) printf("%s: %zu\n", #f, m->f)
34 P(arena);
35 P(ordblks);
36 P(smblks);
37 P(hblks);
38 P(hblkhd);
39 P(usmblks);
40 P(fsmblks);
41 P(uordblks);
42 P(fordblks);
43 P(keepcost);
46 /* We do this to force the call to malloc to not be optimized
47 away. */
48 volatile void *ptr;
50 static int
51 do_test (void)
53 struct mallinfo2 mi1, mi2;
54 int i;
55 size_t total = 0;
57 /* This is the key difference between mallinfo() and mallinfo2().
58 It may be a false positive if int and size_t are the same
59 size. */
60 TEST_COMPARE (sizeof (mi1.arena), sizeof (size_t));
62 mi1 = mallinfo2 ();
63 print_mi ("before", &mi1);
65 /* Allocations that are meaningful-sized but not so large as to be
66 mmapped, so that they're all accounted for in the field we test
67 below. */
68 for (i = 1; i < 20; ++i)
70 ptr = malloc (160 * i);
71 total += 160 * i;
74 mi2 = mallinfo2 ();
75 print_mi ("after", &mi2);
77 /* Check at least something changed. */
78 TEST_VERIFY (mi2.uordblks >= mi1.uordblks + total);
80 return 0;
83 #include <support/test-driver.c>