amalloc.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * debugging malloc()/realloc()/calloc()/free() that attempts
  3. * to keep track of just what's been allocated today.
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #define MAGIC 0x1f2e3d4c
  8. struct alist { int magic, size, index; int *end; struct alist *next, *last; };
  9. static struct alist list = { 0, 0, 0, 0 };
  10. static int mallocs=0;
  11. static int reallocs=0;
  12. static int frees=0;
  13. static int index = 0;
  14. static void
  15. die(char *msg, int index)
  16. {
  17. fprintf(stderr, msg, index);
  18. abort();
  19. }
  20. void *
  21. acalloc(int count, int size)
  22. {
  23. struct alist *ret;
  24. if ( size > 1 ) {
  25. count *= size;
  26. size = 1;
  27. }
  28. if ( ret = calloc(count + sizeof(struct alist) + sizeof(int), size) ) {
  29. ret->magic = MAGIC;
  30. ret->size = size * count;
  31. ret->index = index ++;
  32. ret->end = (int*)(count + (char*) (ret + 1));
  33. *(ret->end) = ~MAGIC;
  34. if ( list.next ) {
  35. ret->next = list.next;
  36. ret->last = &list;
  37. ret->next->last = ret;
  38. list.next = ret;
  39. }
  40. else {
  41. ret->last = ret->next = &list;
  42. list.next = list.last = ret;
  43. }
  44. ++mallocs;
  45. return ret+1;
  46. }
  47. return 0;
  48. }
  49. void*
  50. amalloc(int size)
  51. {
  52. return acalloc(size,1);
  53. }
  54. void
  55. afree(void *ptr)
  56. {
  57. struct alist *p2 = ((struct alist*)ptr)-1;
  58. if ( p2->magic == MAGIC ) {
  59. if ( ! (p2->end && *(p2->end) == ~MAGIC) )
  60. die("goddam: corrupted memory block %d in free()!\n", p2->index);
  61. p2->last->next = p2->next;
  62. p2->next->last = p2->last;
  63. ++frees;
  64. free(p2);
  65. }
  66. else
  67. free(ptr);
  68. }
  69. void *
  70. arealloc(void *ptr, int size)
  71. {
  72. struct alist *p2 = ((struct alist*)ptr)-1;
  73. struct alist save;
  74. if ( p2->magic == MAGIC ) {
  75. if ( ! (p2->end && *(p2->end) == ~MAGIC) )
  76. die("goddam: corrupted memory block %d in realloc()!\n", p2->index);
  77. save.next = p2->next;
  78. save.last = p2->last;
  79. p2 = realloc(p2, sizeof(int) + sizeof(*p2) + size);
  80. if ( p2 ) {
  81. p2->size = size;
  82. p2->end = (int*)(size + (char*) (p2 + 1));
  83. *(p2->end) = ~MAGIC;
  84. p2->next->last = p2;
  85. p2->last->next = p2;
  86. ++reallocs;
  87. return p2+1;
  88. }
  89. else {
  90. save.next->last = save.last;
  91. save.last->next = save.next;
  92. return 0;
  93. }
  94. }
  95. return realloc(ptr, size);
  96. }
  97. void
  98. adump()
  99. {
  100. struct alist *p;
  101. for ( p = list.next; p && (p != &list); p = p->next ) {
  102. fprintf(stderr, "allocated: %d byte%s\n", p->size, (p->size==1) ? "" : "s");
  103. fprintf(stderr, " [%.*s]\n", p->size, (char*)(p+1));
  104. }
  105. if ( getenv("AMALLOC_STATISTICS") ) {
  106. fprintf(stderr, "%d malloc%s\n", mallocs, (mallocs==1)?"":"s");
  107. fprintf(stderr, "%d realloc%s\n", reallocs, (reallocs==1)?"":"s");
  108. fprintf(stderr, "%d free%s\n", frees, (frees==1)?"":"s");
  109. }
  110. }