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