drd: Add a consistency check
[valgrind.git] / memcheck / tests / mallinfo.c
blobd8e853be79c30e2dfea1bd0f07249b027d6a3e1b
1 #include "tests/malloc.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <unistd.h> // getopt()
5 #include "../config.h"
8 static int s_quiet = 0;
11 #if defined(HAVE_MALLINFO)
12 static size_t check(size_t min, size_t max)
14 struct mallinfo mi;
15 size_t used;
17 mi = mallinfo();
19 if (! s_quiet)
21 printf("arena = %d\n", mi.arena); /* non-mmapped space allocated from system */
22 printf("ordblks = %d\n", mi.ordblks); /* number of free chunks */
23 printf("smblks = %d\n", mi.smblks); /* number of fastbin blocks */
24 printf("hblks = %d\n", mi.hblks); /* number of mmapped regions */
25 printf("hblkhd = %d\n", mi.hblkhd); /* space in mmapped regions */
26 printf("usmblks = %d\n", mi.usmblks); /* maximum total allocated space */
27 printf("fsmblks = %d\n", mi.fsmblks); /* space available in freed fastbin blocks */
28 printf("uordblks = %d\n", mi.uordblks); /* total allocated space */
29 printf("fordblks = %d\n", mi.fordblks); /* total free space */
30 printf("keepcost = %d\n", mi.keepcost); /* top-most, releasable (via malloc_trim) space */
31 printf("(min = %zu, max = %zu)\n", min, max);
32 printf("\n");
35 // size checks
36 used = mi.uordblks + mi.hblkhd;
37 if (used < min)
38 exit(1);
40 if (used > max)
41 exit(2);
43 // used should be reasonably close to min
44 // define "reasonably" as within 20%
45 if (used/5*4 > min)
46 exit(3);
48 // sanity checks
49 if ((mi.ordblks == 0) != (mi.fordblks == 0))
50 exit(10);
52 if ((mi.smblks == 0) != (mi.fsmblks == 0))
53 exit(11);
55 if ((mi.hblks == 0) != (mi.hblkhd == 0))
56 exit(12);
58 if (mi.keepcost > mi.fordblks)
59 exit(13);
61 if (mi.fsmblks > mi.fordblks)
62 exit(14);
64 // arena should be reasonably close to fordblks + uordblks
65 if (mi.arena < mi.fordblks + mi.uordblks)
66 exit(15);
68 if (mi.arena/5*4 > mi.fordblks + mi.uordblks)
69 exit(16);
71 return used;
73 #else
74 static size_t check(size_t min, size_t max)
76 if (! s_quiet)
78 printf("mallinfo() is not supported on this platform.\n");
79 printf("\n");
81 return 0;
83 #endif
85 int main(int argc, char** argv)
87 void* ptr[40];
88 int i;
89 size_t min, max;
90 int optchar;
92 while ((optchar = getopt(argc, argv, "q")) != EOF)
94 switch (optchar)
96 case 'q':
97 s_quiet = 1;
98 break;
99 default:
100 fprintf(stderr, "Usage: %s [-q].\n", argv[0]);
101 return 1;
105 min = 0;
106 for (i = 1; i <= 40; i++)
108 int size = i * i * 8;
109 min += size;
110 ptr[i - 1] = malloc(size);
113 max = check(min, (size_t)-1);
115 for (i = 1; i <= 20; i++)
117 int size = i * i * 8;
118 min -= size;
119 max -= size;
120 free(ptr[i - 1]);
123 check(min, max);
125 for ( ; i <= 40; i++)
127 free(ptr[i - 1]);
130 fprintf(stderr, "Success.\n");
132 return 0;