1/* Smoke test for mallinfo2
2 Copyright (C) 2020-2024 Free Software Foundation, Inc.
3 This file is part of the GNU C Library.
4
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.
9
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.
14
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/>. */
18
19/* Test that mallinfo2 is properly exported and basically works. */
20
21#include <array_length.h>
22#include <malloc.h>
23#include <stdlib.h>
24#include <support/check.h>
25
26#include "tst-malloc-aux.h"
27
28/* This is not specifically needed for the test, but (1) does
29 something to the data so gcc doesn't optimize it away, and (2) may
30 help when developing future tests. */
31static void
32print_mi (const char *msg, struct mallinfo2 *m)
33{
34 printf(format: "\n%s...\n", msg);
35#define P(f) printf("%s: %zu\n", #f, m->f)
36 P(arena);
37 P(ordblks);
38 P(smblks);
39 P(hblks);
40 P(hblkhd);
41 P(usmblks);
42 P(fsmblks);
43 P(uordblks);
44 P(fordblks);
45 P(keepcost);
46}
47
48/* We do this to force the call to malloc to not be optimized
49 away. */
50volatile void *ptr;
51
52static int
53do_test (void)
54{
55 struct mallinfo2 mi1, mi2;
56 int i;
57 size_t total = 0;
58
59 /* This is the key difference between mallinfo() and mallinfo2().
60 It may be a false positive if int and size_t are the same
61 size. */
62 TEST_COMPARE (sizeof (mi1.arena), sizeof (size_t));
63
64 mi1 = mallinfo2 ();
65 print_mi (msg: "before", m: &mi1);
66
67 /* Allocations that are meaningful-sized but not so large as to be
68 mmapped, so that they're all accounted for in the field we test
69 below. */
70 for (i = 1; i < 20; ++i)
71 {
72 ptr = malloc (160 * i);
73 total += 160 * i;
74 }
75
76 mi2 = mallinfo2 ();
77 print_mi (msg: "after", m: &mi2);
78
79 /* Check at least something changed. */
80 TEST_VERIFY (mi2.uordblks >= mi1.uordblks + total);
81
82 return 0;
83}
84
85#include <support/test-driver.c>
86

source code of glibc/malloc/tst-mallinfo2.c