FOSSology  4.4.0
Open Source License Compliance by Open Source Software
DMalloc.c
Go to the documentation of this file.
1 /*
2  SPDX-FileCopyrightText: © 2007-2011 Hewlett-Packard Development Company, L.P.
3 
4  SPDX-License-Identifier: GPL-2.0-only
5 */
27 #include <stdio.h>
28 #include <stdlib.h>
29 
30 
31 /* GLOBALS */
32 int DMverbose = 1;
33 char *DMtriggeraddr = NULL;
34 
35 #define TRIGGER(p) if( (p) == DMtriggeraddr ) DMtrigger();
36 #define GUARD 0x73
37 #define MC68000
38 
39 #define TABSIZE (16*1024)
40 static char *__memtab[TABSIZE];
41 
42 #define HDRSIZE (2 * sizeof (unsigned long))
43 
44 #undef BRAINDEADABORT
45 #ifdef BRAINDEADABORT
46 static void abort(void *s)
47 {
48  exit(6);
49 }
50 #endif
51 
52 static malloced(char *ptr);
53 static freed(char *ptr, char *fname, int line);
54 
62 static char *guardit(char *ptr, int size)
63 {
64  unsigned long *lptr = (unsigned long *) ptr;
65 
66  /* add a guard on the beginning */
67  lptr[0] = lptr[1] = size;
68 
69  /* and a guard byte on the end */
70  ptr += HDRSIZE;
71  *(ptr + size) = GUARD;
72 
73  return ptr;
74 }
75 
85 static char *memorycheck(char *ptr, char *fname, int line)
86 {
87  unsigned long size, *lptr = (unsigned long *) ptr;
88 
89  /* check guard word on start */
90  ptr -= HDRSIZE;
91  lptr = (unsigned long *) ptr;
92  if (lptr[0] != lptr[1]) {
93  if (lptr[0] == (lptr[1] ^ 0x00ff)) {
94  fprintf(stderr, "%s[%d]: memcheck(0x%x) already freed - exit\n",
95  fname, line, ptr + HDRSIZE);
96  } else {
97  fprintf(stderr,
98  "%s[%d]: memcheck(0x%x) start pointer corrupt - exit\n",
99  fname, line, ptr + HDRSIZE);
100  }
101  abort();
102  }
103  size = lptr[0];
104  if (*(ptr + HDRSIZE + size) != GUARD) {
105  fprintf(stderr,
106  "%s[%d]: memcheck(0x%x) end overwritten - exit\n",
107  fname, line, ptr + HDRSIZE);
108  abort();
109  }
110  return(ptr);
111 }
112 
116 char *DMmemcheck(char *ptr, char *fname, int line)
117 {
118  int i;
119 
120  if (ptr != NULL) {
121  ptr = memorycheck(ptr, fname, line);
122  }
123 
124  for (i = 0; i < TABSIZE; i++) {
125  if (__memtab[i] != NULL) {
126  memorycheck(__memtab[i], fname, line);
127  }
128  }
129  return(ptr);
130 }
131 
138 DMfree(char *ptr, char *fname, int line)
139 {
140  unsigned long size;
141 
142  if (ptr == NULL)
143  return;
144 
145  if (DMverbose || (ptr == DMtriggeraddr)) {
146  size = ((unsigned long *)ptr)[-2];
147  fprintf(stderr, "%s[%d]: free(0x%x) (%ld bytes)\n",
148  fname, line, ptr, size);
149  TRIGGER(ptr);
150  }
151  ptr = DMmemcheck(ptr, fname, line);
152 
153  /* Negate the last byte of the header guard to signify freed */
154  ((unsigned long *)ptr)[1] ^= 0x00ff;
155 
156  /* all's well so free it */
157  freed(ptr + HDRSIZE, fname, line);
158  free(ptr);
159 }
160 
169 char *DMmalloc(int size, char *fname, int line)
170 {
171  char *ptr;
172 
173  DMmemcheck(NULL, fname, line);
174 
175  if ((ptr = (char *) malloc(size + HDRSIZE + 1)) == NULL) {
176  fprintf(stderr, "%s[%d]: malloc(%d) OUT OF MEMORY\n", fname, line,
177  size);
178  abort();
179  }
180 
181  ptr = guardit(ptr, size);
182 
183 
184  if (DMverbose || (DMtriggeraddr == ptr)) {
185  fprintf(stderr, "%s[%d]: malloc(%d) = 0x%x\n",
186  fname, line, size, ptr);
187  TRIGGER(ptr);
188  }
189  malloced(ptr);
190  return(ptr);
191 }
192 
203 char *DMcalloc(int size, int nitems, char *fname, int line)
204 {
205  char *ptr;
206  int totalsize;
207  int i;
208  char *tempptr;
209 
210  DMmemcheck(NULL, fname, line);
211 
212  totalsize = size * nitems;
213  if ((ptr = (char *) malloc(totalsize + HDRSIZE + 1)) == NULL) {
214  fprintf(stderr, "%s[%d]: calloc(%d,%d) OUT OF MEMORY\n",
215  fname, line, size, nitems);
216  abort();
217  }
218  ptr = guardit(ptr, totalsize);
219 
220  /* initialize to zeros */
221  tempptr = ptr;
222  for (i = 0; i < totalsize; i++) {
223  *tempptr++ = 0;
224  }
225 
226  if (DMverbose || (ptr == DMtriggeraddr)) {
227  fprintf(stderr, "%s[%d]: calloc(%d,%d) = 0x%x\n", fname, line,
228  size, nitems, ptr);
229  TRIGGER(ptr);
230  }
231  malloced(ptr);
232  return(ptr);
233 }
234 
235 
236 
240 static malloced(char *ptr)
241  {
242  int i;
243 
244  for (i = 0; i < TABSIZE; i++) {
245  if (__memtab[i] == NULL) {
246  __memtab[i] = ptr;
247  break;
248  }
249  }
250 
251  if (i >= TABSIZE) {
252  /* table overflow */
253  fprintf(stderr, "Memory table record overflow\n");
254  }
255  }
256 
257 
262 static freed(char *ptr, char *fname, int line)
263 {
264  int i;
265 
266  for (i = 0; i < TABSIZE; i++) {
267  if (__memtab[i] == ptr) {
268  __memtab[i] = NULL;
269  break;
270  }
271  }
272 
273  if (i >= TABSIZE) {
274  /* not found */
275  fprintf(stderr, "%s[%d]: freed(0x%x) NOT MALLOCED\n", fname, line,
276  ptr);
277  abort();
278  }
279 }
280 
291 char *DMrealloc(char *ptr, int size, char *fname, int line)
292 {
293  char *saveptr;
294 
295  saveptr = ptr;
296  ptr = DMmemcheck(ptr, fname, line);
297 
298  if ((ptr = (char *) realloc(ptr, size + HDRSIZE + 1)) == NULL) {
299  fprintf(stderr, "%s[%d]: realloc(0x%x,%d) OUT OF MEMORY\n",
300  fname, line,
301  saveptr,
302  size);
303  abort();
304  }
305  ptr = guardit(ptr, size);
306  if (DMverbose || (DMtriggeraddr == ptr) || (DMtriggeraddr == saveptr)) {
307  fprintf(stderr, "%s[%d]: realloc(0x%x,%d) = 0x%x\n",
308  fname, line, saveptr, size, ptr);
309  TRIGGER(saveptr);
310  TRIGGER(ptr);
311  }
312  freed(saveptr, fname, line);
313  malloced(ptr);
314  return(ptr);
315 }
316 
317 
322 {
323  int i;
324 
325  for (i = 0; i < TABSIZE; i++) {
326  if (__memtab[i] != NULL) {
327  printf("0x%x\n", __memtab[i]);
328  }
329  }
330 }
331 
337 {
338  int i = 0;
339  i++;
340 }
341 
static malloced(char *ptr)
record 'ptr's value in a list of malloc-ed memory
Definition: DMalloc.c:240
char * DMcalloc(int size, int nitems, char *fname, int line)
Allocate memory safely using calloc()
Definition: DMalloc.c:203
char * DMmemcheck(char *ptr, char *fname, int line)
Check the validity of allocated memory areas and report any problems.
Definition: DMalloc.c:116
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:62
static char * memorycheck(char *ptr, char *fname, int line)
Check the validity of allocated memory areas and report any problems.
Definition: DMalloc.c:85
char * DMrealloc(char *ptr, int size, char *fname, int line)
Reallocate memory safely using realloc()
Definition: DMalloc.c:291
DMnotfreed()
Print a list of memory pointers not freed - one per line.
Definition: DMalloc.c:321
DMfree(char *ptr, char *fname, int line)
Free a pointer allocated by DMmalloc()
Definition: DMalloc.c:138
DMtrigger()
Dummy routine with the sole purpose of being available for setting breakpoints from a debugger.
Definition: DMalloc.c:336
int DMverbose
Verbosity level.
Definition: DMalloc.c:32
char * DMmalloc(int size, char *fname, int line)
Allocate memory safely using malloc()
Definition: DMalloc.c:169
static freed(char *ptr, char *fname, int line)
remove 'ptr's value from a list of malloc-ed memory - print error and die if it's not in the list at ...
Definition: DMalloc.c:262
int s
The socket that the CLI will use to communicate.
Definition: fo_cli.c:37