Initial import from FreeBSD RELENG_4:
[dragonfly.git] / tools / test / malloc / main.c
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <unistd.h>
4
5 u_long NBUCKETS         = 2000;
6 u_long NOPS             = 200000;
7 u_long NSIZE            = (16*1024);
8
9 char **foo;
10
11 int
12 main(int argc, char **argv) 
13 {
14     int i,j,k;
15     
16     if (argc > 1) NOPS     = strtoul(argv[1],0,0);
17     if (argc > 2) NBUCKETS = strtoul(argv[2],0,0);
18     if (argc > 3) NSIZE    = strtoul(argv[3],0,0);
19     printf("BRK(0)=%x ",sbrk(0));
20     foo = malloc (sizeof *foo * NBUCKETS);
21     memset(foo,0,sizeof *foo * NBUCKETS);
22     for (i = 1; i <= 4096; i+=i) {
23         for (j = 0 ; j < 40960/i && j < NBUCKETS; j++) {
24             foo[j] = malloc(i);
25         }
26         for (j = 0 ; j < 40960/i && j < NBUCKETS; j++) {
27             free(foo[j]);
28             foo[j] = 0;
29         }
30     }
31
32     for (i = 0 ; i < NOPS ; i++) {
33         j = random() % NBUCKETS;
34         k = random() % NSIZE;
35         foo[j] = realloc(foo[j], k & 1 ? 0 : k);
36         if (foo[j])
37             foo[j][0] = 1;
38     }
39     printf("BRK(1)=%x ",sbrk(0));
40     for (j = 0 ; j < NBUCKETS ; j++) {
41         if (foo[j]) {
42             free(foo[j]);
43             foo[j] = 0;
44         }
45     }
46     printf("BRK(2)=%x NOPS=%lu NBUCKETS=%lu NSIZE=%lu\n",
47         sbrk(0),NOPS,NBUCKETS,NSIZE);
48     return 0;
49 }