-
Notifications
You must be signed in to change notification settings - Fork 0
/
DMalloc.c
353 lines (300 loc) · 8.27 KB
/
DMalloc.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
/***************************************************************
Copyright (C) 2007-2011 Hewlett-Packard Development Company, L.P.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
***************************************************************/
/**
* \file
* - Several global variables control actions taken by the memory check
* routines. These are provided also as a convenient interface to
* run-time debuggers.
* - DMverbose (default = 1) \n
* If != 0, each memory allocation/free is printed
* - DMtriggeraddr (default = 0) \n
* If != 0, then any allocation/free operation with \n
* a pointer argument or return == DMtrigger causes \n
* a message to be printed.
* - DMtrigger() \n
* Function is called whenever a trigger occurs for \n
* setting breakpoints in a debugger.
* .
* - Possible combinations:
* - DMverbose = 0; DMtriggeraddr = 0x12345 \n
* Print messages only when 0x12345 is involved
* - DMverbose = 0; DMtriggeraddr = 0 \n
* Print messages only when an error occurs
*/
#include <stdio.h>
#include <stdlib.h>
/* GLOBALS */
int DMverbose = 1; ///< Verbosity level
char *DMtriggeraddr = NULL;
#define TRIGGER(p) if( (p) == DMtriggeraddr ) DMtrigger();
#define GUARD 0x73
#define MC68000
#define TABSIZE (16*1024)
static char *__memtab[TABSIZE];
#define HDRSIZE (2 * sizeof (unsigned long))
#undef BRAINDEADABORT
#ifdef BRAINDEADABORT
static void abort(void *s)
{
exit(6);
}
#endif
static malloced(char *ptr);
static freed(char *ptr, char *fname, int line);
/**
* \brief Add guard word encoding size on start of memory area and a guard byte
* just past the end of the area.
* \param prt Pointer to guard
* \param size
* \return pointer to user's area
*/
static char *guardit(char *ptr, int size)
{
unsigned long *lptr = (unsigned long *) ptr;
/* add a guard on the beginning */
lptr[0] = lptr[1] = size;
/* and a guard byte on the end */
ptr += HDRSIZE;
*(ptr + size) = GUARD;
return ptr;
}
/**
* \brief Check the validity of allocated memory areas and report any
* problems.
*
* Called by DMmemcheck().
* \param ptr Pointer to check
* \param fname Source file name for logging
* \param line Source line number for logging
*/
static char *memorycheck(char *ptr, char *fname, int line)
{
unsigned long size, *lptr = (unsigned long *) ptr;
/* check guard word on start */
ptr -= HDRSIZE;
lptr = (unsigned long *) ptr;
if (lptr[0] != lptr[1]) {
if (lptr[0] == (lptr[1] ^ 0x00ff)) {
fprintf(stderr, "%s[%d]: memcheck(0x%x) already freed - exit\n",
fname, line, ptr + HDRSIZE);
} else {
fprintf(stderr,
"%s[%d]: memcheck(0x%x) start pointer corrupt - exit\n",
fname, line, ptr + HDRSIZE);
}
abort();
}
size = lptr[0];
if (*(ptr + HDRSIZE + size) != GUARD) {
fprintf(stderr,
"%s[%d]: memcheck(0x%x) end overwritten - exit\n",
fname, line, ptr + HDRSIZE);
abort();
}
return(ptr);
}
/**
* \copybrief memorycheck()
*/
char *DMmemcheck(char *ptr, char *fname, int line)
{
int i;
if (ptr != NULL) {
ptr = memorycheck(ptr, fname, line);
}
for (i = 0; i < TABSIZE; i++) {
if (__memtab[i] != NULL) {
memorycheck(__memtab[i], fname, line);
}
}
return(ptr);
}
/**
* \brief Free a pointer allocated by DMmalloc()
* \param ptr Pointer to be freed
* \param fname Source file name
* \param line Source line number
*/
DMfree(char *ptr, char *fname, int line)
{
unsigned long size;
if (ptr == NULL)
return;
if (DMverbose || (ptr == DMtriggeraddr)) {
size = ((unsigned long *)ptr)[-2];
fprintf(stderr, "%s[%d]: free(0x%x) (%ld bytes)\n",
fname, line, ptr, size);
TRIGGER(ptr);
}
ptr = DMmemcheck(ptr, fname, line);
/* Negate the last byte of the header guard to signify freed */
((unsigned long *)ptr)[1] ^= 0x00ff;
/* all's well so free it */
freed(ptr + HDRSIZE, fname, line);
free(ptr);
}
/**
* \brief Allocate memory safely using malloc()
* \param size Size to be allocated
* \param fname Source file name
* \param line Source line number
* \return Pointer to new memory
* \note Allocated pointer must be freed using DMfree()
*/
char *DMmalloc(int size, char *fname, int line)
{
char *ptr;
DMmemcheck(NULL, fname, line);
if ((ptr = (char *) malloc(size + HDRSIZE + 1)) == NULL) {
fprintf(stderr, "%s[%d]: malloc(%d) OUT OF MEMORY\n", fname, line,
size);
abort();
}
ptr = guardit(ptr, size);
if (DMverbose || (DMtriggeraddr == ptr)) {
fprintf(stderr, "%s[%d]: malloc(%d) = 0x%x\n",
fname, line, size, ptr);
TRIGGER(ptr);
}
malloced(ptr);
return(ptr);
}
/**
* \brief Allocate memory safely using calloc()
* \param size Size of single element
* \param nitems Total number of elements
* \param fname Source file name
* \param line Source line number
* \return Pointer to new memory
* \note Allocated pointer must be freed using DMfree()
* \sa DMmalloc()
*/
char *DMcalloc(int size, int nitems, char *fname, int line)
{
char *ptr;
int totalsize;
int i;
char *tempptr;
DMmemcheck(NULL, fname, line);
totalsize = size * nitems;
if ((ptr = (char *) malloc(totalsize + HDRSIZE + 1)) == NULL) {
fprintf(stderr, "%s[%d]: calloc(%d,%d) OUT OF MEMORY\n",
fname, line, size, nitems);
abort();
}
ptr = guardit(ptr, totalsize);
/* initialize to zeros */
tempptr = ptr;
for (i = 0; i < totalsize; i++) {
*tempptr++ = 0;
}
if (DMverbose || (ptr == DMtriggeraddr)) {
fprintf(stderr, "%s[%d]: calloc(%d,%d) = 0x%x\n", fname, line,
size, nitems, ptr);
TRIGGER(ptr);
}
malloced(ptr);
return(ptr);
}
/**
* \brief record 'ptr's value in a list of malloc-ed memory
*/
static malloced(char *ptr)
{
int i;
for (i = 0; i < TABSIZE; i++) {
if (__memtab[i] == NULL) {
__memtab[i] = ptr;
break;
}
}
if (i >= TABSIZE) {
/* table overflow */
fprintf(stderr, "Memory table record overflow\n");
}
}
/**
* \brief remove 'ptr's value from a list of malloc-ed memory - print
* error and die if it's not in the list at all.
*/
static freed(char *ptr, char *fname, int line)
{
int i;
for (i = 0; i < TABSIZE; i++) {
if (__memtab[i] == ptr) {
__memtab[i] = NULL;
break;
}
}
if (i >= TABSIZE) {
/* not found */
fprintf(stderr, "%s[%d]: freed(0x%x) NOT MALLOCED\n", fname, line,
ptr);
abort();
}
}
/**
* \brief Reallocate memory safely using realloc()
* \param ptr The pointer to be reallocated
* \param size New size
* \param fname Source file name
* \param line Source line number
* \return Pointer to new memory
* \sa DMmemcheck()
* \sa guardit()
*/
char *DMrealloc(char *ptr, int size, char *fname, int line)
{
char *saveptr;
saveptr = ptr;
ptr = DMmemcheck(ptr, fname, line);
if ((ptr = (char *) realloc(ptr, size + HDRSIZE + 1)) == NULL) {
fprintf(stderr, "%s[%d]: realloc(0x%x,%d) OUT OF MEMORY\n",
fname, line,
saveptr,
size);
abort();
}
ptr = guardit(ptr, size);
if (DMverbose || (DMtriggeraddr == ptr) || (DMtriggeraddr == saveptr)) {
fprintf(stderr, "%s[%d]: realloc(0x%x,%d) = 0x%x\n",
fname, line, saveptr, size, ptr);
TRIGGER(saveptr);
TRIGGER(ptr);
}
freed(saveptr, fname, line);
malloced(ptr);
return(ptr);
}
/**
* \brief Print a list of memory pointers not freed - one per line
*/
DMnotfreed()
{
int i;
for (i = 0; i < TABSIZE; i++) {
if (__memtab[i] != NULL) {
printf("0x%x\n", __memtab[i]);
}
}
}
/**
* \brief Dummy routine with the sole purpose of being available for setting
* breakpoints from a debugger.
*/
DMtrigger()
{
int i = 0;
i++;
}