memcheck.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Debug allocators for the Expat test suite
  2. __ __ _
  3. ___\ \/ /_ __ __ _| |_
  4. / _ \\ /| '_ \ / _` | __|
  5. | __// \| |_) | (_| | |_
  6. \___/_/\_\ .__/ \__,_|\__|
  7. |_| XML parser
  8. Copyright (c) 2017 Rhodri James <[email protected]>
  9. Copyright (c) 2017 Sebastian Pipping <[email protected]>
  10. Licensed under the MIT license:
  11. Permission is hereby granted, free of charge, to any person obtaining
  12. a copy of this software and associated documentation files (the
  13. "Software"), to deal in the Software without restriction, including
  14. without limitation the rights to use, copy, modify, merge, publish,
  15. distribute, sublicense, and/or sell copies of the Software, and to permit
  16. persons to whom the Software is furnished to do so, subject to the
  17. following conditions:
  18. The above copyright notice and this permission notice shall be included
  19. in all copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
  23. NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
  24. DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  25. OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
  26. USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. */
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "memcheck.h"
  31. /* Structures to keep track of what has been allocated. Speed isn't a
  32. * big issue for the tests this is required for, so we will use a
  33. * doubly-linked list to make deletion easier.
  34. */
  35. typedef struct allocation_entry {
  36. struct allocation_entry *next;
  37. struct allocation_entry *prev;
  38. void *allocation;
  39. size_t num_bytes;
  40. } AllocationEntry;
  41. static AllocationEntry *alloc_head = NULL;
  42. static AllocationEntry *alloc_tail = NULL;
  43. static AllocationEntry *find_allocation(void *ptr);
  44. /* Allocate some memory and keep track of it. */
  45. void *
  46. tracking_malloc(size_t size) {
  47. AllocationEntry *entry = malloc(sizeof(AllocationEntry));
  48. if (entry == NULL) {
  49. printf("Allocator failure\n");
  50. return NULL;
  51. }
  52. entry->num_bytes = size;
  53. entry->allocation = malloc(size);
  54. if (entry->allocation == NULL) {
  55. free(entry);
  56. return NULL;
  57. }
  58. entry->next = NULL;
  59. /* Add to the list of allocations */
  60. if (alloc_head == NULL) {
  61. entry->prev = NULL;
  62. alloc_head = alloc_tail = entry;
  63. } else {
  64. entry->prev = alloc_tail;
  65. alloc_tail->next = entry;
  66. alloc_tail = entry;
  67. }
  68. return entry->allocation;
  69. }
  70. static AllocationEntry *
  71. find_allocation(void *ptr) {
  72. AllocationEntry *entry;
  73. for (entry = alloc_head; entry != NULL; entry = entry->next) {
  74. if (entry->allocation == ptr) {
  75. return entry;
  76. }
  77. }
  78. return NULL;
  79. }
  80. /* Free some memory and remove the tracking for it */
  81. void
  82. tracking_free(void *ptr) {
  83. AllocationEntry *entry;
  84. if (ptr == NULL) {
  85. /* There won't be an entry for this */
  86. return;
  87. }
  88. entry = find_allocation(ptr);
  89. if (entry != NULL) {
  90. /* This is the relevant allocation. Unlink it */
  91. if (entry->prev != NULL)
  92. entry->prev->next = entry->next;
  93. else
  94. alloc_head = entry->next;
  95. if (entry->next != NULL)
  96. entry->next->prev = entry->prev;
  97. else
  98. alloc_tail = entry->next;
  99. free(entry);
  100. } else {
  101. printf("Attempting to free unallocated memory at %p\n", ptr);
  102. }
  103. free(ptr);
  104. }
  105. /* Reallocate some memory and keep track of it */
  106. void *
  107. tracking_realloc(void *ptr, size_t size) {
  108. AllocationEntry *entry;
  109. if (ptr == NULL) {
  110. /* By definition, this is equivalent to malloc(size) */
  111. return tracking_malloc(size);
  112. }
  113. if (size == 0) {
  114. /* By definition, this is equivalent to free(ptr) */
  115. tracking_free(ptr);
  116. return NULL;
  117. }
  118. /* Find the allocation entry for this memory */
  119. entry = find_allocation(ptr);
  120. if (entry == NULL) {
  121. printf("Attempting to realloc unallocated memory at %p\n", ptr);
  122. entry = malloc(sizeof(AllocationEntry));
  123. if (entry == NULL) {
  124. printf("Reallocator failure\n");
  125. return NULL;
  126. }
  127. entry->allocation = realloc(ptr, size);
  128. if (entry->allocation == NULL) {
  129. free(entry);
  130. return NULL;
  131. }
  132. /* Add to the list of allocations */
  133. entry->next = NULL;
  134. if (alloc_head == NULL) {
  135. entry->prev = NULL;
  136. alloc_head = alloc_tail = entry;
  137. } else {
  138. entry->prev = alloc_tail;
  139. alloc_tail->next = entry;
  140. alloc_tail = entry;
  141. }
  142. } else {
  143. entry->allocation = realloc(ptr, size);
  144. if (entry->allocation == NULL) {
  145. /* Realloc semantics say the original is still allocated */
  146. entry->allocation = ptr;
  147. return NULL;
  148. }
  149. }
  150. entry->num_bytes = size;
  151. return entry->allocation;
  152. }
  153. int
  154. tracking_report(void) {
  155. AllocationEntry *entry;
  156. if (alloc_head == NULL)
  157. return 1;
  158. /* Otherwise we have allocations that haven't been freed */
  159. for (entry = alloc_head; entry != NULL; entry = entry->next) {
  160. printf("Allocated %lu bytes at %p\n", (long unsigned)entry->num_bytes,
  161. entry->allocation);
  162. }
  163. return 0;
  164. }