2 * Copyright (c) 1995, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.5 1995-05-16 08:51:10 quinn
8 * License, documentation, and memory fixes
10 * Revision 1.4 1995/05/15 13:25:13 quinn
13 * Revision 1.3 1995/05/15 11:56:55 quinn
14 * Debuggng & adjustments.
16 * Revision 1.2 1995/04/10 10:23:50 quinn
19 * Revision 1.1 1995/03/27 08:35:17 quinn
20 * Created util library
21 * Added memory debugging module. Imported options-manager
30 static const unsigned char head[] = {44, 33, 22, 11};
31 static const unsigned char tail[] = {11, 22, 33, 44};
32 static const unsigned char freed[] = {99, 99, 99, 99};
34 void *d_malloc(char *file, int line, int nbytes)
39 if (!(res = malloc(nbytes + 3 * sizeof(long))))
41 fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
42 file, line, nbytes, res + 2 * sizeof(long));
44 memcpy(res, &head, sizeof(long));
45 memcpy(res + sizeof(long), &len, sizeof(long));
46 res += 2 * sizeof(long);
47 memcpy(res + nbytes, &tail, sizeof(long));
51 void d_free(char *file, int line, char *ptr)
55 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
57 memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
58 memcpy(&len, ptr - sizeof(long), sizeof(long));
59 if (memcmp(ptr + len, &tail, sizeof(long)))
61 fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
62 file, line, ptr, len);
63 free(ptr - 2 * sizeof(long));
67 void *d_realloc(char *file, int line, char *ptr, int nbytes)
69 long len, nlen = nbytes;
73 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
75 memcpy(&len, ptr - sizeof(long), sizeof(long));
76 if (memcmp(ptr + len, &tail, sizeof(long)))
78 if (!(r = realloc(ptr - 2 * sizeof(long), nbytes + 3 * sizeof(long))))
80 fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, %p->%p\n",
81 file, line, len, nbytes, p, r + 2 * sizeof(long));
82 memcpy(r, &head, sizeof(long));
83 memcpy(r + sizeof(long), &nlen, sizeof(long));
84 r += 2 * sizeof(long);
85 memcpy(r + nbytes, &tail, sizeof(long));