mallinfo.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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. }