1 /* Debug allocators for the Expat test suite
4 / _ \\ /| '_ \ / _` | __|
5 | __// \| |_) | (_| | |_
6 \___/_/\_\ .__/ \__,_|\__|
9 Copyright (c) 1997-2000 Thai Open Source Software Center Ltd
10 Copyright (c) 2000-2017 Expat development team
11 Licensed under the MIT license:
13 Permission is hereby granted, free of charge, to any person obtaining
14 a copy of this software and associated documentation files (the
15 "Software"), to deal in the Software without restriction, including
16 without limitation the rights to use, copy, modify, merge, publish,
17 distribute, sublicense, and/or sell copies of the Software, and to permit
18 persons to whom the Software is furnished to do so, subject to the
21 The above copyright notice and this permission notice shall be included
22 in all copies or substantial portions of the Software.
24 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
27 NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
28 DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
29 OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
30 USE OR OTHER DEALINGS IN THE SOFTWARE.
37 /* Structures to keep track of what has been allocated. Speed isn't a
38 * big issue for the tests this is required for, so we will use a
39 * doubly-linked list to make deletion easier.
42 typedef struct allocation_entry {
43 struct allocation_entry *next;
44 struct allocation_entry *prev;
49 static AllocationEntry *alloc_head = NULL;
50 static AllocationEntry *alloc_tail = NULL;
52 static AllocationEntry *find_allocation(void *ptr);
54 /* Allocate some memory and keep track of it. */
56 tracking_malloc(size_t size) {
57 AllocationEntry *entry = malloc(sizeof(AllocationEntry));
60 printf("Allocator failure\n");
63 entry->num_bytes = size;
64 entry->allocation = malloc(size);
65 if (entry->allocation == NULL) {
71 /* Add to the list of allocations */
72 if (alloc_head == NULL) {
74 alloc_head = alloc_tail = entry;
76 entry->prev = alloc_tail;
77 alloc_tail->next = entry;
81 return entry->allocation;
84 static AllocationEntry *
85 find_allocation(void *ptr) {
86 AllocationEntry *entry;
88 for (entry = alloc_head; entry != NULL; entry = entry->next) {
89 if (entry->allocation == ptr) {
96 /* Free some memory and remove the tracking for it */
98 tracking_free(void *ptr) {
99 AllocationEntry *entry;
102 /* There won't be an entry for this */
106 entry = find_allocation(ptr);
108 /* This is the relevant allocation. Unlink it */
109 if (entry->prev != NULL)
110 entry->prev->next = entry->next;
112 alloc_head = entry->next;
113 if (entry->next != NULL)
114 entry->next->prev = entry->prev;
116 alloc_tail = entry->next;
119 printf("Attempting to free unallocated memory at %p\n", ptr);
124 /* Reallocate some memory and keep track of it */
126 tracking_realloc(void *ptr, size_t size) {
127 AllocationEntry *entry;
130 /* By definition, this is equivalent to malloc(size) */
131 return tracking_malloc(size);
134 /* By definition, this is equivalent to free(ptr) */
139 /* Find the allocation entry for this memory */
140 entry = find_allocation(ptr);
142 printf("Attempting to realloc unallocated memory at %p\n", ptr);
143 entry = malloc(sizeof(AllocationEntry));
145 printf("Reallocator failure\n");
148 entry->allocation = realloc(ptr, size);
149 if (entry->allocation == NULL) {
154 /* Add to the list of allocations */
156 if (alloc_head == NULL) {
158 alloc_head = alloc_tail = entry;
160 entry->prev = alloc_tail;
161 alloc_tail->next = entry;
165 entry->allocation = realloc(ptr, size);
166 if (entry->allocation == NULL) {
167 /* Realloc semantics say the original is still allocated */
168 entry->allocation = ptr;
173 entry->num_bytes = size;
174 return entry->allocation;
178 tracking_report(void) {
179 AllocationEntry *entry;
181 if (alloc_head == NULL)
184 /* Otherwise we have allocations that haven't been freed */
185 for (entry = alloc_head; entry != NULL; entry = entry->next) {
186 printf("Allocated %lu bytes at %p\n", (long unsigned)entry->num_bytes,