mallinfo.c 3.0 KB

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