2 * Copyright (C) 1994, Index Data I/S
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.2 1995-04-10 10:23:50 quinn
10 * Revision 1.1 1995/03/27 08:35:17 quinn
11 * Created util library
12 * Added memory debugging module. Imported options-manager
21 static const unsigned long head = 0xaabbccdd;
22 static const unsigned long tail = 0x11223344;
23 static const unsigned long freed = 0xffeeffee;
25 void *d_malloc(char *file, int line, int nbytes)
30 if (!(res = malloc(nbytes + 3 * sizeof(long))))
32 fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
33 file, line, nbytes, res + 2 * sizeof(long));
35 memcpy(res, &head, sizeof(long));
36 memcpy(res + sizeof(long), &len, sizeof(long));
37 res += 2 * sizeof(long);
38 memcpy(res + nbytes, &tail, sizeof(long));
42 void d_free(char *file, int line, char *ptr)
46 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
48 memcpy(ptr, &freed, sizeof(long));
49 memcpy(&len, ptr - sizeof(long), sizeof(long));
50 if (memcmp(ptr + len, &tail, sizeof(long)))
52 fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
53 file, line, ptr, len);
54 free(ptr - 2 * sizeof(long));
58 void *d_realloc(char *file, int line, char *ptr, int nbytes)
60 long len, nlen = nbytes;
64 if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
66 memcpy(&len, ptr - sizeof(long), sizeof(long));
67 if (memcmp(ptr + len, &tail, sizeof(long)))
69 if (!(r = realloc(ptr, nbytes + 3 * sizeof(long))))
71 fprintf(stderr, "---d_realloc, '%s':%d, %d->%d, %p->%p\n",
72 file, line, len, nbytes, p, r + 2 * sizeof(long));
73 memcpy(r, &head, sizeof(long));
74 memcpy(r + sizeof(long), &nlen, sizeof(long));
75 if (r != ptr - 2 * sizeof(long))
77 memcpy(r + 2 * sizeof(long), ptr, len);
78 memcpy(ptr - 2 * sizeof(long), &freed, sizeof(long));
80 r += 2 * sizeof(long);
81 memcpy(r + nbytes, &tail, sizeof(long));