Added body-of-text to BIB-1 ANY and the WAIS profile
[yaz-moved-to-github.git] / util / dmalloc.c
1 /*
2  * Copyright (c) 1995, Index Data
3  * See the file LICENSE for details.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: dmalloc.c,v $
7  * Revision 1.8  1995-09-29 17:12:33  quinn
8  * Smallish
9  *
10  * Revision 1.7  1995/09/29  17:01:50  quinn
11  * More Windows work
12  *
13  * Revision 1.6  1995/09/27  15:03:02  quinn
14  * Modified function heads & prototypes.
15  *
16  * Revision 1.5  1995/05/16  08:51:10  quinn
17  * License, documentation, and memory fixes
18  *
19  * Revision 1.4  1995/05/15  13:25:13  quinn
20  * Fixed memory bug.
21  *
22  * Revision 1.3  1995/05/15  11:56:55  quinn
23  * Debuggng & adjustments.
24  *
25  * Revision 1.2  1995/04/10  10:23:50  quinn
26  * Fixes.
27  *
28  * Revision 1.1  1995/03/27  08:35:17  quinn
29  * Created util library
30  * Added memory debugging module. Imported options-manager
31  *
32  *
33  */
34
35 #include <stdlib.h>
36 #include <stdio.h>
37 #include <string.h>
38 #include <yconfig.h>
39
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};
43
44 void *d_malloc(char *file, int line, int nbytes)
45 {
46     char *res;
47     int long len;
48
49     if (!(res = malloc(nbytes + 3 * sizeof(long))))
50         return 0;
51     fprintf(stderr, "---d_malloc, '%s':%d, %d->%p\n",
52         file, line, nbytes, res + 2 * sizeof(long));
53     len = nbytes;
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));
58     return res;
59 }
60
61 void d_free(char *file, int line, char *ptr)
62 {
63     long len;
64
65     if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
66         abort();
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)))
70         abort();
71     fprintf(stderr, "---d_free, '%s':%d, %p (%d)\n",
72         file, line, ptr, len);
73     free(ptr - 2 * sizeof(long));
74     return;
75 }
76
77 void *d_realloc(char *file, int line, char *ptr, int nbytes)
78 {
79     long len, nlen = nbytes;
80     char *p = ptr;
81     char *r;
82
83     if (memcmp(&head, ptr - 2 * sizeof(long), sizeof(long)))
84         abort();
85     memcpy(&len, ptr - sizeof(long), sizeof(long));
86     if (memcmp(ptr + len, &tail, sizeof(long)))
87         abort();
88     if (!(r = realloc(ptr - 2 * sizeof(long), nbytes + 3 * sizeof(long))))
89         return 0;
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));
96     return r;
97 }