2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.4 1995-05-15 13:25:13 quinn
10 * Revision 1.3 1995/05/15 11:56:55 quinn
11 * Debuggng & adjustments.
13 * Revision 1.2 1995/04/10 10:23:50 quinn
16 * Revision 1.1 1995/03/27 08:35:17 quinn
17 * Created util library
18 * Added memory debugging module. Imported options-manager
27 static const unsigned char head[] = {44, 33, 22, 11};
28 static const unsigned char tail[] = {11, 22, 33, 44};
29 static const unsigned char freed[] = {99, 99, 99, 99};
31 void *d_malloc(char *file, int line, int nbytes)
36 if (!(res = malloc(nbytes + 3 * sizeof(long))))
38 fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
39 file, line, nbytes, res + 2 * sizeof(long));
41 memcpy(res, &head, sizeof(long));
42 memcpy(res + sizeof(long), &len, sizeof(long));
43 res += 2 * sizeof(long);
44 memcpy(res + nbytes, &tail, sizeof(long));
48 void d_free(char *file, int line, char *ptr)
52 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
54 memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
55 memcpy(&len, ptr - sizeof(long), sizeof(long));
56 if (memcmp(ptr + len, &tail, sizeof(long)))
58 fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
59 file, line, ptr, len);
60 free(ptr - 2 * sizeof(long));
64 void *d_realloc(char *file, int line, char *ptr, int nbytes)
66 long len, nlen = nbytes;
70 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
72 memcpy(&len, ptr - sizeof(long), sizeof(long));
73 if (memcmp(ptr + len, &tail, sizeof(long)))
75 if (!(r = realloc(ptr - 2 * sizeof(long), nbytes + 3 * sizeof(long))))
77 fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, %p->%p\n",
78 file, line, len, nbytes, p, r + 2 * sizeof(long));
79 memcpy(r, &head, sizeof(long));
80 memcpy(r + sizeof(long), &nlen, sizeof(long));
81 r += 2 * sizeof(long);
82 memcpy(r + nbytes, &tail, sizeof(long));