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