mallinfo.3: Update example program to use mallinfo2()

Signed-off-by: Michael Kerrisk <mtk.manpages@gmail.com>
This commit is contained in:
Michael Kerrisk 2020-10-17 19:04:58 +02:00
parent 5bf7268349
commit e9e534537d
1 changed files with 17 additions and 17 deletions

View File

@ -197,7 +197,7 @@ However, because some internal bookkeeping values may be of type
the reported values may wrap around zero and thus be inaccurate. the reported values may wrap around zero and thus be inaccurate.
.SH EXAMPLES .SH EXAMPLES
The program below employs The program below employs
.BR mallinfo () .BR mallinfo2 ()
to retrieve memory allocation statistics before and after to retrieve memory allocation statistics before and after
allocating and freeing some blocks of memory. allocating and freeing some blocks of memory.
The statistics are displayed on standard output. The statistics are displayed on standard output.
@ -272,22 +272,22 @@ Topmost releasable block (keepcost): 31168
#include <string.h> #include <string.h>
static void static void
display_mallinfo(void) display_mallinfo2(void)
{ {
struct mallinfo mi; struct mallinfo2 mi;
mi = mallinfo(); mi = mallinfo2();
printf("Total non\-mmapped bytes (arena): %d\en", mi.arena); printf("Total non\-mmapped bytes (arena): %zu\en", mi.arena);
printf("# of free chunks (ordblks): %d\en", mi.ordblks); printf("# of free chunks (ordblks): %zu\en", mi.ordblks);
printf("# of free fastbin blocks (smblks): %d\en", mi.smblks); printf("# of free fastbin blocks (smblks): %zu\en", mi.smblks);
printf("# of mapped regions (hblks): %d\en", mi.hblks); printf("# of mapped regions (hblks): %zu\en", mi.hblks);
printf("Bytes in mapped regions (hblkhd): %d\en", mi.hblkhd); printf("Bytes in mapped regions (hblkhd): %zu\en", mi.hblkhd);
printf("Max. total allocated space (usmblks): %d\en", mi.usmblks); printf("Max. total allocated space (usmblks): %zu\en", mi.usmblks);
printf("Free bytes held in fastbins (fsmblks): %d\en", mi.fsmblks); printf("Free bytes held in fastbins (fsmblks): %zu\en", mi.fsmblks);
printf("Total allocated space (uordblks): %d\en", mi.uordblks); printf("Total allocated space (uordblks): %zu\en", mi.uordblks);
printf("Total free space (fordblks): %d\en", mi.fordblks); printf("Total free space (fordblks): %zu\en", mi.fordblks);
printf("Topmost releasable block (keepcost): %d\en", mi.keepcost); printf("Topmost releasable block (keepcost): %zu\en", mi.keepcost);
} }
int int
@ -311,7 +311,7 @@ main(int argc, char *argv[])
freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks; freeEnd = (argc > 5) ? atoi(argv[5]) : numBlocks;
printf("============== Before allocating blocks ==============\en"); printf("============== Before allocating blocks ==============\en");
display_mallinfo(); display_mallinfo2();
for (int j = 0; j < numBlocks; j++) { for (int j = 0; j < numBlocks; j++) {
if (numBlocks >= MAX_ALLOCS) { if (numBlocks >= MAX_ALLOCS) {
@ -327,13 +327,13 @@ main(int argc, char *argv[])
} }
printf("\en============== After allocating blocks ==============\en"); printf("\en============== After allocating blocks ==============\en");
display_mallinfo(); display_mallinfo2();
for (int j = freeBegin; j < freeEnd; j += freeStep) for (int j = freeBegin; j < freeEnd; j += freeStep)
free(alloc[j]); free(alloc[j]);
printf("\en============== After freeing blocks ==============\en"); printf("\en============== After freeing blocks ==============\en");
display_mallinfo(); display_mallinfo2();
exit(EXIT_SUCCESS); exit(EXIT_SUCCESS);
} }