Improved installation. Moved header files to include/yaz.
[yaz-moved-to-github.git] / util / xmalloc.c
1 /*
2  * Copyright (C) 1994-1999, Index Data
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: xmalloc.c,v $
7  * Revision 1.10  1999-11-30 13:47:12  adam
8  * Improved installation. Moved header files to include/yaz.
9  *
10  * Revision 1.9  1999/09/10 08:58:32  adam
11  * Set TRACE_XMALLOC to 1.
12  *
13  * Revision 1.8  1999/08/27 09:40:32  adam
14  * Renamed logf function to yaz_log. Removed VC++ project files.
15  *
16  * Revision 1.7  1999/07/13 13:24:53  adam
17  * Updated memory debugging memory allocatation routines.
18  *
19  * Revision 1.6  1998/02/11 11:53:36  adam
20  * Changed code so that it compiles as C++.
21  *
22  * Revision 1.5  1997/10/31 12:20:09  adam
23  * Improved memory debugging for xmalloc/nmem.c. References to NMEM
24  * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
25  * Bug fix: missing fclose in data1_read_espec1.
26  *
27  * Revision 1.4  1996/07/03 13:21:36  adam
28  * Function xfree_f checks for NULL pointer.
29  *
30  * Revision 1.3  1995/12/05  15:08:44  adam
31  * Fixed verbose of xrealloc.
32  *
33  * Revision 1.2  1995/12/05  11:08:37  adam
34  * More verbose malloc routines.
35  *
36  * Revision 1.1  1995/11/01  11:56:53  quinn
37  * Added Xmalloc.
38  *
39  * Revision 1.6  1995/10/16  14:03:11  quinn
40  * Changes to support element set names and espec1
41  *
42  * Revision 1.5  1995/09/04  12:34:06  adam
43  * Various cleanup. YAZ util used instead.
44  *
45  * Revision 1.4  1994/10/05  10:16:16  quinn
46  * Added xrealloc. Fixed bug in log.
47  *
48  * Revision 1.3  1994/09/26  16:31:37  adam
49  * Added xcalloc_f.
50  *
51  * Revision 1.2  1994/08/18  08:23:26  adam
52  * Res.c now use handles. xmalloc defines xstrdup.
53  *
54  * Revision 1.1  1994/08/17  13:37:54  adam
55  * xmalloc.c added to util.
56  *
57  */
58
59 #include <stdio.h>
60 #include <stdlib.h>
61 #include <string.h>
62
63 #include <yaz/log.h>
64 #include <yaz/xmalloc.h>
65
66 #define TRACE_XMALLOC 1
67
68 #if TRACE_XMALLOC > 1
69
70 static const unsigned char head[] = {44, 33, 22, 11};
71 static const unsigned char tail[] = {11, 22, 33, 44};
72 static const unsigned char freed[] = {11, 22, 33, 44};
73
74 struct dmalloc_info {
75     int len;
76     char file[16];
77     int line;
78     struct dmalloc_info *next;
79     struct dmalloc_info *prev;
80 };
81
82 struct dmalloc_info *dmalloc_list = 0;
83
84 void *xmalloc_d(size_t nbytes, const char *file, int line)
85 {
86     char *res;
87     struct dmalloc_info *dinfo;
88     
89     if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
90         return 0;
91     dinfo = (struct dmalloc_info *) res;
92     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
93     dinfo->file[sizeof(dinfo->file)-1] = '\0';
94     dinfo->line = line;
95     dinfo->len = nbytes;
96     
97     dinfo->prev = 0;
98     dinfo->next = dmalloc_list;
99     if (dinfo->next)
100         dinfo->next->prev = dinfo;
101     dmalloc_list = dinfo;
102     
103     memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
104     res += sizeof(*dinfo) + 4*sizeof(char);
105     memcpy(res + nbytes, tail, 4*sizeof(char));
106     return res;
107 }
108
109 void xfree_d(void *ptr, const char *file, int line)
110 {
111     struct dmalloc_info *dinfo;
112
113     if (!ptr)
114         return;
115     dinfo = (struct dmalloc_info *)
116         ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
117     if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
118     {
119         yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
120         abort();
121     }
122     if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
123     {
124         yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
125         abort();
126     }
127     if (dinfo->prev)
128         dinfo->prev->next = dinfo->next;
129     else
130         dmalloc_list = dinfo->next;
131     if (dinfo->next)
132         dinfo->next->prev = dinfo->prev;
133     memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
134     free(dinfo);
135     return;
136 }
137
138 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
139 {
140     struct dmalloc_info *dinfo;
141     char *ptr = (char*) p;
142     char *res;
143     
144     if (!ptr)
145     {
146         if (!nbytes)
147             return 0;
148         res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
149     }
150     else
151     {
152         if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
153         {
154             yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
155                     file, line, ptr);
156             abort();
157         }
158         dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
159         if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
160         {
161             yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
162                     file, line, ptr);
163             abort();
164         }
165         if (dinfo->prev)
166             dinfo->prev->next = dinfo->next;
167         else
168             dmalloc_list = dinfo->next;
169         if (dinfo->next)
170             dinfo->next->prev = dinfo->prev;
171         
172         if (!nbytes)
173         {
174             free (dinfo);
175             return 0;
176         }
177         res = (char *)
178             realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
179     }
180     if (!res)
181         return 0;
182     dinfo = (struct dmalloc_info *) res;
183     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
184     dinfo->file[sizeof(dinfo->file)-1] = '\0';
185     dinfo->line = line;
186     dinfo->len = nbytes;
187
188     dinfo->prev = 0;
189     dinfo->next = dmalloc_list;
190     if (dmalloc_list)
191         dmalloc_list->prev = dinfo;
192     dmalloc_list = dinfo;
193     
194     memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
195     res += sizeof(*dinfo) + 4*sizeof(char);
196     memcpy(res + nbytes, tail, 4*sizeof(char));
197     return res;
198 }
199
200 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
201 {
202     char *res;
203     struct dmalloc_info *dinfo;
204     size_t nbytes = nmemb * size;
205     
206     if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
207         return 0;
208     dinfo = (struct dmalloc_info *) res;
209     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
210     dinfo->file[sizeof(dinfo->file)-1] = '\0';
211     dinfo->line = line;
212     dinfo->len = nbytes;
213     
214     dinfo->prev = 0;
215     dinfo->next = dmalloc_list;
216     if (dinfo->next)
217         dinfo->next->prev = dinfo;
218     dmalloc_list = dinfo;
219     
220     memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
221     res += sizeof(*dinfo) + 4*sizeof(char);
222     memcpy(res + nbytes, tail, 4*sizeof(char));
223     return res;
224 }
225
226 void xmalloc_trav_d(const char *file, int line)
227 {
228     size_t size = 0;
229     struct dmalloc_info *dinfo = dmalloc_list;
230     
231     yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
232     while (dinfo)
233     {
234         yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
235               dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
236         size += dinfo->len;
237         dinfo = dinfo->next;
238     }
239     yaz_log (LOG_LOG, "total bytes %ld", (long) size);
240 }
241
242 #else
243 /* TRACE_XMALLOC <= 1 */
244 #define xrealloc_d(o, x, f, l) realloc(o, x)
245 #define xmalloc_d(x, f, l) malloc(x)
246 #define xcalloc_d(x,y, f, l) calloc(x,y)
247 #define xfree_d(x, f, l) free(x)
248 #define xmalloc_trav_d(f, l) 
249 #endif
250
251 void xmalloc_trav_f(const char *s, const char *file, int line)
252 {
253     xmalloc_trav_d(file, line);
254 }
255
256 void *xrealloc_f (void *o, size_t size, const char *file, int line)
257 {
258     void *p = xrealloc_d (o, size, file, line);
259
260 #if TRACE_XMALLOC
261     yaz_log (LOG_DEBUG,
262             "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
263 #endif
264     if (!p)
265     {
266         yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
267                  size);
268         exit(1);
269     }
270     return p;
271 }
272
273 void *xmalloc_f (size_t size, const char *file, int line)
274 {
275     void *p = xmalloc_d (size, file, line);
276     
277 #if TRACE_XMALLOC
278     yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
279 #endif
280     if (!p)
281     {
282         yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
283         exit (1);
284     }
285     return p;
286 }
287
288 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
289 {
290     void *p = xcalloc_d (nmemb, size, file, line);
291 #if TRACE_XMALLOC
292     yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
293 #endif
294     if (!p)
295     {
296         yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
297         exit (1);
298     }
299     return p;
300 }
301
302 char *xstrdup_f (const char *s, const char *file, int line)
303 {
304     char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
305 #if TRACE_XMALLOC
306     yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
307 #endif
308     strcpy (p, s);
309     return p;
310 }
311
312 void xfree_f(void *p, const char *file, int line)
313 {
314     if (!p)
315         return ;
316 #if TRACE_XMALLOC
317     if (p)
318         yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
319 #endif
320     xfree_d(p, file, line);
321 }