mallinfo.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. This is a version (aka dlmalloc) of malloc/free/realloc written by
  3. Doug Lea and released to the public domain. Use, modify, and
  4. redistribute this code without permission or acknowledgement in any
  5. way you wish. Send questions, comments, complaints, performance
  6. data, etc to dl@cs.oswego.edu
  7. VERSION 2.7.2 Sat Aug 17 09:07:30 2002 Doug Lea (dl at gee)
  8. Note: There may be an updated version of this malloc obtainable at
  9. ftp://gee.cs.oswego.edu/pub/misc/malloc.c
  10. Check before installing!
  11. Hacked up for uClibc by Erik Andersen <andersen@codepoet.org>
  12. */
  13. #include "malloc.h"
  14. #include <stdio.h> /* fprintf */
  15. /* ------------------------------ mallinfo ------------------------------ */
  16. struct mallinfo mallinfo(void)
  17. {
  18. mstate av;
  19. struct mallinfo mi;
  20. unsigned int i;
  21. mbinptr b;
  22. mchunkptr p;
  23. size_t avail;
  24. size_t fastavail;
  25. int nblocks;
  26. int nfastblocks;
  27. __MALLOC_LOCK;
  28. av = get_malloc_state();
  29. /* Ensure initialization */
  30. if (av->top == 0) {
  31. __malloc_consolidate(av);
  32. }
  33. check_malloc_state();
  34. /* Account for top */
  35. avail = chunksize(av->top);
  36. nblocks = 1; /* top always exists */
  37. /* traverse fastbins */
  38. nfastblocks = 0;
  39. fastavail = 0;
  40. for (i = 0; i < NFASTBINS; ++i) {
  41. for (p = av->fastbins[i]; p != 0; p = REVEAL_PTR(&p->fd, p->fd)) {
  42. CHECK_PTR(p);
  43. ++nfastblocks;
  44. fastavail += chunksize(p);
  45. }
  46. }
  47. avail += fastavail;
  48. /* traverse regular bins */
  49. for (i = 1; i < NBINS; ++i) {
  50. b = bin_at(av, i);
  51. for (p = last(b); p != b; p = p->bk) {
  52. ++nblocks;
  53. avail += chunksize(p);
  54. }
  55. }
  56. mi.smblks = nfastblocks;
  57. mi.ordblks = nblocks;
  58. mi.fordblks = avail;
  59. mi.uordblks = av->sbrked_mem - avail;
  60. mi.arena = av->sbrked_mem;
  61. mi.hblks = av->n_mmaps;
  62. mi.hblkhd = av->mmapped_mem;
  63. mi.fsmblks = fastavail;
  64. mi.keepcost = chunksize(av->top);
  65. mi.usmblks = av->max_total_mem;
  66. __MALLOC_UNLOCK;
  67. return mi;
  68. }
  69. libc_hidden_def(mallinfo)
  70. void malloc_stats(void)
  71. {
  72. struct mallinfo mi;
  73. mi = mallinfo();
  74. fprintf(stderr,
  75. "total bytes allocated = %10u\n"
  76. "total bytes in use bytes = %10u\n"
  77. "total non-mmapped bytes allocated = %10d\n"
  78. "number of mmapped regions = %10d\n"
  79. "total allocated mmap space = %10d\n"
  80. "total allocated sbrk space = %10d\n"
  81. #if 0
  82. "number of free chunks = %10d\n"
  83. "number of fastbin blocks = %10d\n"
  84. "space in freed fastbin blocks = %10d\n"
  85. #endif
  86. "maximum total allocated space = %10d\n"
  87. "total free space = %10d\n"
  88. "memory releasable via malloc_trim = %10d\n",
  89. (unsigned int)(mi.arena + mi.hblkhd),
  90. (unsigned int)(mi.uordblks + mi.hblkhd),
  91. mi.arena,
  92. mi.hblks,
  93. mi.hblkhd,
  94. mi.uordblks,
  95. #if 0
  96. mi.ordblks,
  97. mi.smblks,
  98. mi.fsmblks,
  99. #endif
  100. mi.usmblks,
  101. mi.fordblks,
  102. mi.keepcost
  103. );
  104. }