2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.1 1995-03-27 08:35:17 quinn
9 * Added memory debugging module. Imported options-manager
18 static const unsigned long head = 0xaabbccdd;
19 static const unsigned long tail = 0x11223344;
20 static const unsigned long freed = 0xffeeffee;
22 void *d_malloc(char *file, int line, int nbytes)
27 if (!(res = malloc(nbytes + 3 * sizeof(long))))
29 fprintf(stderr, "---d_malloc, '%s':%d, %d->0x%p\n",
30 file, line, nbytes, res + 2 * sizeof(long));
32 memcpy(res, &head, sizeof(long));
33 memcpy(res + sizeof(long), &len, sizeof(long));
34 res += 2 * sizeof(long);
35 memcpy(res + nbytes, &tail, sizeof(long));
39 void d_free(char *file, int line, char *ptr)
43 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
45 memcpy(ptr, &freed, sizeof(long));
46 memcpy(&len, ptr - sizeof(long), sizeof(long));
47 if (memcmp(ptr + len, &tail, sizeof(long)))
49 fprintf(stderr, "---d_free, '%s':%d, 0x%p (%d)\n",
50 file, line, ptr, len);
51 free(ptr - 2 * sizeof(long));
55 void *d_realloc(char *file, int line, char *ptr, int nbytes)
57 long len, nlen = nbytes;
61 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
63 memcpy(&len, ptr - sizeof(long), sizeof(long));
64 if (memcmp(ptr + len, &tail, sizeof(long)))
66 if (!(r = realloc(ptr, nbytes + 3 * sizeof(long))))
68 fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, 0x%p->0x%p\n",
69 file, line, len, nbytes, p, r + 2 * sizeof(long));
70 memcpy(r, &head, sizeof(long));
71 memcpy(r + sizeof(long), &nlen, sizeof(long));
72 if (r != ptr - 2 * sizeof(long))
74 memcpy(r + 2 * sizeof(long), ptr, len);
75 memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
77 r += 2 * sizeof(long);
78 memcpy(r + nbytes, &tail, sizeof(long));