2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1995-09-29 17:12:33 quinn
10 * Revision 1.7 1995/09/29 17:01:50 quinn
13 * Revision 1.6 1995/09/27 15:03:02 quinn
14 * Modified function heads & prototypes.
16 * Revision 1.5 1995/05/16 08:51:10 quinn
17 * License, documentation, and memory fixes
19 * Revision 1.4 1995/05/15 13:25:13 quinn
22 * Revision 1.3 1995/05/15 11:56:55 quinn
23 * Debuggng & adjustments.
25 * Revision 1.2 1995/04/10 10:23:50 quinn
28 * Revision 1.1 1995/03/27 08:35:17 quinn
29 * Created util library
30 * Added memory debugging module. Imported options-manager
40 static const unsigned char head[] = {44, 33, 22, 11};
41 static const unsigned char tail[] = {11, 22, 33, 44};
42 static const unsigned char freed[] = {99, 99, 99, 99};
44 void *d_malloc(char *file, int line, int nbytes)
49 if (!(res = malloc(nbytes + 3 * sizeof(long))))
51 fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
52 file, line, nbytes, res + 2 * sizeof(long));
54 memcpy(res, &head, sizeof(long));
55 memcpy(res + sizeof(long), &len, sizeof(long));
56 res += 2 * sizeof(long);
57 memcpy(res + nbytes, &tail, sizeof(long));
61 void d_free(char *file, int line, char *ptr)
65 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
67 memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
68 memcpy(&len, ptr - sizeof(long), sizeof(long));
69 if (memcmp(ptr + len, &tail, sizeof(long)))
71 fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
72 file, line, ptr, len);
73 free(ptr - 2 * sizeof(long));
77 void *d_realloc(char *file, int line, char *ptr, int nbytes)
79 long len, nlen = nbytes;
83 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
85 memcpy(&len, ptr - sizeof(long), sizeof(long));
86 if (memcmp(ptr + len, &tail, sizeof(long)))
88 if (!(r = realloc(ptr - 2 * sizeof(long), nbytes + 3 * sizeof(long))))
90 fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, %p->%p\n",
91 file, line, len, nbytes, p, r + 2 * sizeof(long));
92 memcpy(r, &head, sizeof(long));
93 memcpy(r + sizeof(long), &nlen, sizeof(long));
94 r += 2 * sizeof(long);
95 memcpy(r + nbytes, &tail, sizeof(long));