Skip to content
Newer
Older
100644 228 lines (203 sloc) 5.73 KB
43ccb9c Initial import.
hisham authored Mar 4, 2006
1
2 #define _GNU_SOURCE
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
3 #include <string.h>
43ccb9c Initial import.
hisham authored Mar 4, 2006
4 #include <stdlib.h>
5 #include <stdio.h>
6 #include <stdbool.h>
7 #include <assert.h>
8
9 #undef strdup
10 #undef malloc
11 #undef realloc
12 #undef calloc
13 #undef free
14
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
15 #include "DebugMemory.h"
16
43ccb9c Initial import.
hisham authored Mar 4, 2006
17 /*{
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
18
43ccb9c Initial import.
hisham authored Mar 4, 2006
19 typedef struct DebugMemoryItem_ DebugMemoryItem;
20
21 struct DebugMemoryItem_ {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
22 int magic;
43ccb9c Initial import.
hisham authored Mar 4, 2006
23 void* data;
24 char* file;
25 int line;
26 DebugMemoryItem* next;
27 };
28
29 typedef struct DebugMemory_ {
30 DebugMemoryItem* first;
31 int allocations;
32 int deallocations;
33 int size;
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
34 bool totals;
43ccb9c Initial import.
hisham authored Mar 4, 2006
35 FILE* file;
36 } DebugMemory;
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
37
43ccb9c Initial import.
hisham authored Mar 4, 2006
38 }*/
39
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
40 #if defined(DEBUG)
41
42 static DebugMemory* singleton = NULL;
43ccb9c Initial import.
hisham authored Mar 4, 2006
43
44 void DebugMemory_new() {
45 if (singleton)
46 return;
47 singleton = malloc(sizeof(DebugMemory));
48 singleton->first = NULL;
49 singleton->allocations = 0;
50 singleton->deallocations = 0;
51 singleton->size = 0;
002504b Assign creation of the allocation log file to a separate #define.
loderunner authored Nov 8, 2006
52 #ifdef DEBUG_ALLOC
43ccb9c Initial import.
hisham authored Mar 4, 2006
53 singleton->file = fopen("/tmp/htop-debug-alloc.txt", "w");
002504b Assign creation of the allocation log file to a separate #define.
loderunner authored Nov 8, 2006
54 #else
55 singleton->file = NULL;
56 #endif
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
57 singleton->totals = true;
58 //singleton->file = NULL;
43ccb9c Initial import.
hisham authored Mar 4, 2006
59 }
60
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
61 void* DebugMemory_malloc(int size, char* file, int line, char* str) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
62 void* data = malloc(size);
63 DebugMemory_registerAllocation(data, file, line);
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
64 if (singleton->file) {
65 if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
66 fprintf(singleton->file, "%d\t%s:%d (%s)\n", size, file, line, str);
67 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
68 return data;
69 }
70
71 void* DebugMemory_calloc(int a, int b, char* file, int line) {
72 void* data = calloc(a, b);
73 DebugMemory_registerAllocation(data, file, line);
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
74 if (singleton->file) {
75 if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
76 fprintf(singleton->file, "%d\t%s:%d\n", a*b, file, line);
77 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
78 return data;
79 }
80
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
81 void* DebugMemory_realloc(void* ptr, int size, char* file, int line, char* str) {
43ccb9c Initial import.
hisham authored Mar 4, 2006
82 if (ptr != NULL)
83 DebugMemory_registerDeallocation(ptr, file, line);
84 void* data = realloc(ptr, size);
85 DebugMemory_registerAllocation(data, file, line);
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
86 if (singleton->file) {
87 if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
88 fprintf(singleton->file, "%d\t%s:%d (%s)\n", size, file, line, str);
89 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
90 return data;
91 }
92
93 void* DebugMemory_strdup(char* str, char* file, int line) {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
94 assert(str);
43ccb9c Initial import.
hisham authored Mar 4, 2006
95 char* data = strdup(str);
96 DebugMemory_registerAllocation(data, file, line);
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
97 if (singleton->file) {
98 if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
99 fprintf(singleton->file, "%d\t%s:%d\n", (int) strlen(str), file, line);
100 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
101 return data;
102 }
103
104 void DebugMemory_free(void* data, char* file, int line) {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
105 assert(data);
43ccb9c Initial import.
hisham authored Mar 4, 2006
106 DebugMemory_registerDeallocation(data, file, line);
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
107 if (singleton->file) {
108 if (singleton->totals) fprintf(singleton->file, "%d\t", singleton->size);
109 fprintf(singleton->file, "free\t%s:%d\n", file, line);
110 }
43ccb9c Initial import.
hisham authored Mar 4, 2006
111 free(data);
112 }
113
114 void DebugMemory_assertSize() {
115 if (!singleton->first) {
116 assert (singleton->size == 0);
117 }
118 DebugMemoryItem* walk = singleton->first;
119 int i = 0;
120 while (walk != NULL) {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
121 assert(walk->magic == 11061980);
43ccb9c Initial import.
hisham authored Mar 4, 2006
122 i++;
123 walk = walk->next;
124 }
125 assert (i == singleton->size);
126 }
127
128 int DebugMemory_getBlockCount() {
129 if (!singleton->first) {
130 return 0;
131 }
132 DebugMemoryItem* walk = singleton->first;
133 int i = 0;
134 while (walk != NULL) {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
135 assert(walk->magic == 11061980);
43ccb9c Initial import.
hisham authored Mar 4, 2006
136 i++;
137 walk = walk->next;
138 }
139 return i;
140 }
141
142 void DebugMemory_registerAllocation(void* data, char* file, int line) {
143 if (!singleton)
144 DebugMemory_new();
145 DebugMemory_assertSize();
146 DebugMemoryItem* item = (DebugMemoryItem*) malloc(sizeof(DebugMemoryItem));
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
147 item->magic = 11061980;
43ccb9c Initial import.
hisham authored Mar 4, 2006
148 item->data = data;
149 item->file = file;
150 item->line = line;
151 item->next = NULL;
152 int val = DebugMemory_getBlockCount();
153 if (singleton->first == NULL) {
154 assert (val == 0);
155 singleton->first = item;
156 } else {
157 DebugMemoryItem* walk = singleton->first;
158 while (true) {
159 if (walk->next == NULL) {
160 walk->next = item;
161 break;
162 }
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
163 assert(walk->magic == 11061980);
43ccb9c Initial import.
hisham authored Mar 4, 2006
164 walk = walk->next;
165 }
166 }
167 int nval = DebugMemory_getBlockCount();
168 assert(nval == val + 1);
169 singleton->allocations++;
170 singleton->size++;
171 DebugMemory_assertSize();
172 }
173
174 void DebugMemory_registerDeallocation(void* data, char* file, int line) {
175 assert(singleton);
176 assert(singleton->first);
177 DebugMemoryItem* walk = singleton->first;
178 DebugMemoryItem* prev = NULL;
179 int val = DebugMemory_getBlockCount();
180 while (walk != NULL) {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
181 assert(walk->magic == 11061980);
43ccb9c Initial import.
hisham authored Mar 4, 2006
182 if (walk->data == data) {
183 if (prev == NULL) {
184 singleton->first = walk->next;
185 } else {
186 prev->next = walk->next;
187 }
188 free(walk);
189 assert(DebugMemory_getBlockCount() == val - 1);
190 singleton->deallocations++;
191 singleton->size--;
192 DebugMemory_assertSize();
193 return;
194 }
195 DebugMemoryItem* tmp = walk;
196 walk = walk->next;
197 prev = tmp;
198 }
199 DebugMemory_report();
200 fprintf(stderr, "Couldn't find allocation for memory freed at %s:%d\n", file, line);
201 assert(false);
202 }
203
204 void DebugMemory_report() {
205 assert(singleton);
206 DebugMemoryItem* walk = singleton->first;
207 int i = 0;
208 while (walk != NULL) {
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
209 assert(walk->magic == 11061980);
43ccb9c Initial import.
hisham authored Mar 4, 2006
210 i++;
211 fprintf(stderr, "%p %s:%d\n", walk->data, walk->file, walk->line);
212 walk = walk->next;
213 }
214 fprintf(stderr, "Total:\n");
215 fprintf(stderr, "%d allocations\n", singleton->allocations);
216 fprintf(stderr, "%d deallocations\n", singleton->deallocations);
217 fprintf(stderr, "%d size\n", singleton->size);
218 fprintf(stderr, "%d non-freed blocks\n", i);
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
219 if (singleton->file)
220 fclose(singleton->file);
43ccb9c Initial import.
hisham authored Mar 4, 2006
221 }
3e560c0 More information in debug output.
loderunner authored Jun 6, 2006
222
223 #elif defined(DEBUGLITE)
224
225 //#include "efence.h"
226
227 #endif
Something went wrong with that request. Please try again.