FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
DMalloc.c
Go to the documentation of this file.
1 /***************************************************************
2  Copyright (C) 2007-2011 Hewlett-Packard Development Company, L.P.
3 
4  This program is free software; you can redistribute it and/or
5  modify it under the terms of the GNU General Public License
6  version 2 as published by the Free Software Foundation.
7 
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12 
13  You should have received a copy of the GNU General Public License along
14  with this program; if not, write to the Free Software Foundation, Inc.,
15  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 
17  ***************************************************************/
39 #include <stdio.h>
40 #include <stdlib.h>
41 
42 
43 /* GLOBALS */
44 int DMverbose = 1;
45 char *DMtriggeraddr = NULL;
46 
47 #define TRIGGER(p) if( (p) == DMtriggeraddr ) DMtrigger();
48 #define GUARD 0x73
49 #define MC68000
50 
51 #define TABSIZE (16*1024)
52 static char *__memtab[TABSIZE];
53 
54 #define HDRSIZE (2 * sizeof (unsigned long))
55 
56 #undef BRAINDEADABORT
57 #ifdef BRAINDEADABORT
58 static void abort(void *s)
59 {
60  exit(6);
61 }
62 #endif
63 
64 static malloced(char *ptr);
65 static freed(char *ptr, char *fname, int line);
66 
74 static char *guardit(char *ptr, int size)
75 {
76  unsigned long *lptr = (unsigned long *) ptr;
77 
78  /* add a guard on the beginning */
79  lptr[0] = lptr[1] = size;
80 
81  /* and a guard byte on the end */
82  ptr += HDRSIZE;
83  *(ptr + size) = GUARD;
84 
85  return ptr;
86 }
87 
97 static char *memorycheck(char *ptr, char *fname, int line)
98 {
99  unsigned long size, *lptr = (unsigned long *) ptr;
100 
101  /* check guard word on start */
102  ptr -= HDRSIZE;
103  lptr = (unsigned long *) ptr;
104  if (lptr[0] != lptr[1]) {
105  if (lptr[0] == (lptr[1] ^ 0x00ff)) {
106  fprintf(stderr, "%s[%d]: memcheck(0x%x) already freed - exit\n",
107  fname, line, ptr + HDRSIZE);
108  } else {
109  fprintf(stderr,
110  "%s[%d]: memcheck(0x%x) start pointer corrupt - exit\n",
111  fname, line, ptr + HDRSIZE);
112  }
113  abort();
114  }
115  size = lptr[0];
116  if (*(ptr + HDRSIZE + size) != GUARD) {
117  fprintf(stderr,
118  "%s[%d]: memcheck(0x%x) end overwritten - exit\n",
119  fname, line, ptr + HDRSIZE);
120  abort();
121  }
122  return(ptr);
123 }
124 
128 char *DMmemcheck(char *ptr, char *fname, int line)
129 {
130  int i;
131 
132  if (ptr != NULL) {
133  ptr = memorycheck(ptr, fname, line);
134  }
135 
136  for (i = 0; i < TABSIZE; i++) {
137  if (__memtab[i] != NULL) {
138  memorycheck(__memtab[i], fname, line);
139  }
140  }
141  return(ptr);
142 }
143 
150 DMfree(char *ptr, char *fname, int line)
151 {
152  unsigned long size;
153 
154  if (ptr == NULL)
155  return;
156 
157  if (DMverbose || (ptr == DMtriggeraddr)) {
158  size = ((unsigned long *)ptr)[-2];
159  fprintf(stderr, "%s[%d]: free(0x%x) (%ld bytes)\n",
160  fname, line, ptr, size);
161  TRIGGER(ptr);
162  }
163  ptr = DMmemcheck(ptr, fname, line);
164 
165  /* Negate the last byte of the header guard to signify freed */
166  ((unsigned long *)ptr)[1] ^= 0x00ff;
167 
168  /* all's well so free it */
169  freed(ptr + HDRSIZE, fname, line);
170  free(ptr);
171 }
172 
181 char *DMmalloc(int size, char *fname, int line)
182 {
183  char *ptr;
184 
185  DMmemcheck(NULL, fname, line);
186 
187  if ((ptr = (char *) malloc(size + HDRSIZE + 1)) == NULL) {
188  fprintf(stderr, "%s[%d]: malloc(%d) OUT OF MEMORY\n", fname, line,
189  size);
190  abort();
191  }
192 
193  ptr = guardit(ptr, size);
194 
195 
196  if (DMverbose || (DMtriggeraddr == ptr)) {
197  fprintf(stderr, "%s[%d]: malloc(%d) = 0x%x\n",
198  fname, line, size, ptr);
199  TRIGGER(ptr);
200  }
201  malloced(ptr);
202  return(ptr);
203 }
204 
215 char *DMcalloc(int size, int nitems, char *fname, int line)
216 {
217  char *ptr;
218  int totalsize;
219  int i;
220  char *tempptr;
221 
222  DMmemcheck(NULL, fname, line);
223 
224  totalsize = size * nitems;
225  if ((ptr = (char *) malloc(totalsize + HDRSIZE + 1)) == NULL) {
226  fprintf(stderr, "%s[%d]: calloc(%d,%d) OUT OF MEMORY\n",
227  fname, line, size, nitems);
228  abort();
229  }
230  ptr = guardit(ptr, totalsize);
231 
232  /* initialize to zeros */
233  tempptr = ptr;
234  for (i = 0; i < totalsize; i++) {
235  *tempptr++ = 0;
236  }
237 
238  if (DMverbose || (ptr == DMtriggeraddr)) {
239  fprintf(stderr, "%s[%d]: calloc(%d,%d) = 0x%x\n", fname, line,
240  size, nitems, ptr);
241  TRIGGER(ptr);
242  }
243  malloced(ptr);
244  return(ptr);
245 }
246 
247 
248 
252 static malloced(char *ptr)
253  {
254  int i;
255 
256  for (i = 0; i < TABSIZE; i++) {
257  if (__memtab[i] == NULL) {
258  __memtab[i] = ptr;
259  break;
260  }
261  }
262 
263  if (i >= TABSIZE) {
264  /* table overflow */
265  fprintf(stderr, "Memory table record overflow\n");
266  }
267  }
268 
269 
274 static freed(char *ptr, char *fname, int line)
275 {
276  int i;
277 
278  for (i = 0; i < TABSIZE; i++) {
279  if (__memtab[i] == ptr) {
280  __memtab[i] = NULL;
281  break;
282  }
283  }
284 
285  if (i >= TABSIZE) {
286  /* not found */
287  fprintf(stderr, "%s[%d]: freed(0x%x) NOT MALLOCED\n", fname, line,
288  ptr);
289  abort();
290  }
291 }
292 
303 char *DMrealloc(char *ptr, int size, char *fname, int line)
304 {
305  char *saveptr;
306 
307  saveptr = ptr;
308  ptr = DMmemcheck(ptr, fname, line);
309 
310  if ((ptr = (char *) realloc(ptr, size + HDRSIZE + 1)) == NULL) {
311  fprintf(stderr, "%s[%d]: realloc(0x%x,%d) OUT OF MEMORY\n",
312  fname, line,
313  saveptr,
314  size);
315  abort();
316  }
317  ptr = guardit(ptr, size);
318  if (DMverbose || (DMtriggeraddr == ptr) || (DMtriggeraddr == saveptr)) {
319  fprintf(stderr, "%s[%d]: realloc(0x%x,%d) = 0x%x\n",
320  fname, line, saveptr, size, ptr);
321  TRIGGER(saveptr);
322  TRIGGER(ptr);
323  }
324  freed(saveptr, fname, line);
325  malloced(ptr);
326  return(ptr);
327 }
328 
329 
334 {
335  int i;
336 
337  for (i = 0; i < TABSIZE; i++) {
338  if (__memtab[i] != NULL) {
339  printf("0x%x\n", __memtab[i]);
340  }
341  }
342 }
343 
349 {
350  int i = 0;
351  i++;
352 }
353 
static char * guardit(char *ptr, int size)
Add guard word encoding size on start of memory area and a guard byte just past the end of the area...
Definition: DMalloc.c:74
static char * memorycheck(char *ptr, char *fname, int line)
Check the validity of allocated memory areas and report any problems.
Definition: DMalloc.c:97
DMtrigger()
Dummy routine with the sole purpose of being available for setting breakpoints from a debugger...
Definition: DMalloc.c:348
char * DMrealloc(char *ptr, int size, char *fname, int line)
Reallocate memory safely using realloc()
Definition: DMalloc.c:303
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:48
DMnotfreed()
Print a list of memory pointers not freed - one per line.
Definition: DMalloc.c:333
static malloced(char *ptr)
record &#39;ptr&#39;s value in a list of malloc-ed memory
Definition: DMalloc.c:252
DMfree(char *ptr, char *fname, int line)
Free a pointer allocated by DMmalloc()
Definition: DMalloc.c:150
char * DMcalloc(int size, int nitems, char *fname, int line)
Allocate memory safely using calloc()
Definition: DMalloc.c:215
int DMverbose
Verbosity level.
Definition: DMalloc.c:44
char * DMmalloc(int size, char *fname, int line)
Allocate memory safely using malloc()
Definition: DMalloc.c:181
static freed(char *ptr, char *fname, int line)
remove &#39;ptr&#39;s value from a list of malloc-ed memory - print error and die if it&#39;s not in the list at ...
Definition: DMalloc.c:274
char * DMmemcheck(char *ptr, char *fname, int line)
Definition: DMalloc.c:128