3 #include "../../config.h"
4 #if defined(HAVE_MALLINFO)
8 #define BIGINCREASE 32000
13 #if defined(HAVE_MALLINFO)
14 struct mallinfo mallinfo_result
;
15 mallinfo_result
= mallinfo();
18 /* from /usr/include/malloc.h */
21 #if defined(HAVE_MALLINFO)
22 printf("%10d int arena; /* non-mmapped space allocated from system */\n", mallinfo_result
.arena
);
23 printf("%10d int ordblks; /* number of free chunks */\n", mallinfo_result
.ordblks
);
24 printf("%10d int smblks; /* number of fastbin blocks */\n", mallinfo_result
.smblks
);
25 printf("%10d int hblks; /* number of mmapped regions */\n", mallinfo_result
.hblks
);
26 printf("%10d int hblkhd; /* space in mmapped regions */\n", mallinfo_result
.hblkhd
);
27 printf("%10d int usmblks; /* maximum total allocated space */\n", mallinfo_result
.usmblks
);
28 printf("%10d int fsmblks; /* space available in freed fastbin blocks */\n", mallinfo_result
.fsmblks
);
29 printf("%10d int uordblks; /* total allocated space */\n", mallinfo_result
.uordblks
);
30 printf("%10d int fordblks; /* total free space */\n", mallinfo_result
.fordblks
);
31 printf("%10d int keepcost; /* top-most, releasable (via malloc_trim) space */\n", mallinfo_result
.keepcost
);
36 int main(int argc
, char *argv
[])
42 int malloc_failure
= 0;
43 unsigned long bigsize
= 8; // current size of the (reallocated) big block.
47 // two optional arguments: [nr of loop] [debug]
56 bigsize
+= BIGINCREASE
;
57 big
= malloc (bigsize
);
59 printf ("failure %d could not allocate size %lu\n",
60 ++malloc_failure
, bigsize
);
62 printf("big 0x%p\n", big
);
64 for (i
= 0; i
< loop
; i
++)
66 bigsize
+= BIGINCREASE
;
67 newbig
= malloc(bigsize
);
69 printf ("failure %d could not allocate size %lu\n",
70 ++malloc_failure
, bigsize
);
74 printf("big 0x%p\n", big
);
77 printf ("after %d loops, last size block requested %lu\n", loop
, bigsize
);
78 // verify if superblock fragmentation occurred
79 // We consider that an arena of up to 3 times more than bigsize is ok.
81 #if defined(HAVE_MALLINFO)
82 struct mallinfo mallinfo_result
;
83 mallinfo_result
= mallinfo();
84 // Under valgrind, hblkhd is 0 : all the space is in arena.
85 // Under native linux, some space is counted hblkhd.
86 if (malloc_failure
> 0)
87 printf ("%d mallocs failed, below output is doubful\n", malloc_failure
);
88 if (mallinfo_result
.arena
+ mallinfo_result
.hblkhd
> 3 * bigsize
)
89 printf("unexpected heap fragmentation %lu\n",
90 (unsigned long) mallinfo_result
.arena
91 + (unsigned long) mallinfo_result
.hblkhd
);
94 printf("reasonable heap usage\n");
98 stats ("before freeing last block");
101 stats ("after freeing last block");