mallinfo.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. libc_hidden_proto(fprintf)
  15. libc_hidden_proto(stderr)
  16. /* ------------------------------ mallinfo ------------------------------ */
  17. libc_hidden_proto(mallinfo)
  18. struct mallinfo mallinfo(void)
  19. {
  20. mstate av;
  21. struct mallinfo mi;
  22. unsigned int i;
  23. mbinptr b;
  24. mchunkptr p;
  25. size_t avail;
  26. size_t fastavail;
  27. int nblocks;
  28. int nfastblocks;
  29. LOCK;
  30. av = get_malloc_state();
  31. /* Ensure initialization */
  32. if (av->top == 0) {
  33. __malloc_consolidate(av);
  34. }
  35. check_malloc_state();
  36. /* Account for top */
  37. avail = chunksize(av->top);
  38. nblocks = 1; /* top always exists */
  39. /* traverse fastbins */
  40. nfastblocks = 0;
  41. fastavail = 0;
  42. for (i = 0; i < NFASTBINS; ++i) {
  43. for (p = av->fastbins[i]; p != 0; p = p->fd) {
  44. ++nfastblocks;
  45. fastavail += chunksize(p);
  46. }
  47. }
  48. avail += fastavail;
  49. /* traverse regular bins */
  50. for (i = 1; i < NBINS; ++i) {
  51. b = bin_at(av, i);
  52. for (p = last(b); p != b; p = p->bk) {
  53. ++nblocks;
  54. avail += chunksize(p);
  55. }
  56. }
  57. mi.smblks = nfastblocks;
  58. mi.ordblks = nblocks;
  59. mi.fordblks = avail;
  60. mi.uordblks = av->sbrked_mem - avail;
  61. mi.arena = av->sbrked_mem;
  62. mi.hblks = av->n_mmaps;
  63. mi.hblkhd = av->mmapped_mem;
  64. mi.fsmblks = fastavail;
  65. mi.keepcost = chunksize(av->top);
  66. mi.usmblks = av->max_total_mem;
  67. UNLOCK;
  68. return mi;
  69. }
  70. libc_hidden_def(mallinfo)
  71. void malloc_stats(FILE *file)
  72. {
  73. struct mallinfo mi;
  74. if (file==NULL) {
  75. file = stderr;
  76. }
  77. mi = mallinfo();
  78. fprintf(file, "total bytes allocated = %10u\n", (unsigned int)(mi.arena + mi.hblkhd));
  79. fprintf(file, "total bytes in use bytes = %10u\n", (unsigned int)(mi.uordblks + mi.hblkhd));
  80. fprintf(file, "total non-mmapped bytes allocated = %10d\n", mi.arena);
  81. fprintf(file, "number of mmapped regions = %10d\n", mi.hblks);
  82. fprintf(file, "total allocated mmap space = %10d\n", mi.hblkhd);
  83. fprintf(file, "total allocated sbrk space = %10d\n", mi.uordblks);
  84. #if 0
  85. fprintf(file, "number of free chunks = %10d\n", mi.ordblks);
  86. fprintf(file, "number of fastbin blocks = %10d\n", mi.smblks);
  87. fprintf(file, "space in freed fastbin blocks = %10d\n", mi.fsmblks);
  88. #endif
  89. fprintf(file, "maximum total allocated space = %10d\n", mi.usmblks);
  90. fprintf(file, "total free space = %10d\n", mi.fordblks);
  91. fprintf(file, "memory releasable via malloc_trim = %10d\n", mi.keepcost);
  92. }