(root)/
glibc-2.38/
malloc/
tst-mallinfo2.c
       1  /* Smoke test for mallinfo2
       2     Copyright (C) 2020-2023 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  /* 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)
      31  {
      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);
      44  }
      45  
      46  /* We do this to force the call to malloc to not be optimized
      47     away.  */
      48  volatile void *ptr;
      49  
      50  static int
      51  do_test (void)
      52  {
      53    struct mallinfo2 mi1, mi2;
      54    int i;
      55    size_t total = 0;
      56  
      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));
      61  
      62    mi1 = mallinfo2 ();
      63    print_mi ("before", &mi1);
      64  
      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)
      69      {
      70        ptr = malloc (160 * i);
      71        total += 160 * i;
      72      }
      73  
      74    mi2 = mallinfo2 ();
      75    print_mi ("after", &mi2);
      76  
      77    /* Check at least something changed.  */
      78    TEST_VERIFY (mi2.uordblks >= mi1.uordblks + total);
      79  
      80    return 0;
      81  }
      82  
      83  #include <support/test-driver.c>