Bump year
[yaz-moved-to-github.git] / src / xmalloc.c
1 /*
2  * Copyright (C) 1995-2005, Index Data ApS
3  * All rights reserved.
4  *
5  * $Id: xmalloc.c,v 1.5 2005-01-15 19:47:14 adam Exp $
6  */
7 /**
8  * \file xmalloc.c
9  * \brief Implements malloc interface.
10  */
11
12 #if HAVE_CONFIG_H
13 #include <config.h>
14 #endif
15
16 #include <stdio.h>
17 #include <stdlib.h>
18 #include <string.h>
19
20 #include <yaz/log.h>
21 #include <yaz/xmalloc.h>
22
23 #ifndef TRACE_XMALLOC
24 #define TRACE_XMALLOC 1
25 #endif
26
27 static int log_level=0;
28 static int log_level_initialized=0;
29
30 #if TRACE_XMALLOC > 1
31
32 static const unsigned char head[] = {88, 77, 66, 55, 44, 33, 22, 11};
33 static const unsigned char tail[] = {11, 22, 33, 44, 55, 66, 77, 88};
34 static const unsigned char freed[] = {11, 22, 33, 44, 55, 66, 77, 88};
35
36 struct dmalloc_info {
37     int len;
38     char file[16];
39     int line;
40     struct dmalloc_info *next;
41     struct dmalloc_info *prev;
42 };
43
44 struct dmalloc_info *dmalloc_list = 0;
45
46
47 void *xmalloc_d(size_t nbytes, const char *file, int line)
48 {
49     char *res;
50     struct dmalloc_info *dinfo;
51     
52     if (!log_level_initialized)
53     {
54         log_level=yaz_log_module_level("malloc");
55         log_level_initialized=1;
56     }
57
58     if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
59         return 0;
60     dinfo = (struct dmalloc_info *) res;
61     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
62     dinfo->file[sizeof(dinfo->file)-1] = '\0';
63     dinfo->line = line;
64     dinfo->len = nbytes;
65     
66     dinfo->prev = 0;
67     dinfo->next = dmalloc_list;
68     if (dinfo->next)
69         dinfo->next->prev = dinfo;
70     dmalloc_list = dinfo;
71     
72     memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
73     res += sizeof(*dinfo) + 8*sizeof(char);
74     memcpy(res + nbytes, tail, 8*sizeof(char));
75     return res;
76 }
77
78 void xfree_d(void *ptr, const char *file, int line)
79 {
80     struct dmalloc_info *dinfo;
81
82     if (!ptr)
83         return;
84     dinfo = (struct dmalloc_info *)
85         ((char*)ptr - 8*sizeof(char) - sizeof(*dinfo));
86     if (memcmp(head, (char*) ptr - 8*sizeof(char), 8*sizeof(char)))
87     {
88         yaz_log(YLOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
89         abort();
90     }
91     if (memcmp((char*) ptr + dinfo->len, tail, 8*sizeof(char)))
92     {
93         yaz_log(YLOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
94         abort();
95     }
96     if (dinfo->prev)
97         dinfo->prev->next = dinfo->next;
98     else
99         dmalloc_list = dinfo->next;
100     if (dinfo->next)
101         dinfo->next->prev = dinfo->prev;
102     memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
103     free(dinfo);
104     return;
105 }
106
107 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
108 {
109     struct dmalloc_info *dinfo;
110     char *ptr = (char*) p;
111     char *res;
112     
113     if (!log_level_initialized)
114     {
115         log_level=yaz_log_module_level("malloc");
116         log_level_initialized=1;
117     }
118
119     if (!ptr)
120     {
121         if (!nbytes)
122             return 0;
123         res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char));
124     }
125     else
126     {
127         if (memcmp(head, ptr - 8*sizeof(char), 8*sizeof(char)))
128         {
129             yaz_log(YLOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
130                     file, line, ptr);
131             abort();
132         }
133         dinfo = (struct dmalloc_info *) (ptr-8*sizeof(char) - sizeof(*dinfo));
134         if (memcmp(ptr + dinfo->len, tail, 8*sizeof(char)))
135         {
136             yaz_log(YLOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
137                     file, line, ptr);
138             abort();
139         }
140         if (dinfo->prev)
141             dinfo->prev->next = dinfo->next;
142         else
143             dmalloc_list = dinfo->next;
144         if (dinfo->next)
145             dinfo->next->prev = dinfo->prev;
146         
147         if (!nbytes)
148         {
149             free (dinfo);
150             return 0;
151         }
152         res = (char *)
153             realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char));
154     }
155     if (!res)
156         return 0;
157     dinfo = (struct dmalloc_info *) res;
158     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
159     dinfo->file[sizeof(dinfo->file)-1] = '\0';
160     dinfo->line = line;
161     dinfo->len = nbytes;
162
163     dinfo->prev = 0;
164     dinfo->next = dmalloc_list;
165     if (dmalloc_list)
166         dmalloc_list->prev = dinfo;
167     dmalloc_list = dinfo;
168     
169     memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
170     res += sizeof(*dinfo) + 8*sizeof(char);
171     memcpy(res + nbytes, tail, 8*sizeof(char));
172     return res;
173 }
174
175 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
176 {
177     char *res;
178     struct dmalloc_info *dinfo;
179     size_t nbytes = nmemb * size;
180     
181     if (!log_level_initialized)
182     {
183         log_level=yaz_log_module_level("malloc");
184         log_level_initialized=1;
185     }
186
187     if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
188         return 0;
189     dinfo = (struct dmalloc_info *) res;
190     strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
191     dinfo->file[sizeof(dinfo->file)-1] = '\0';
192     dinfo->line = line;
193     dinfo->len = nbytes;
194     
195     dinfo->prev = 0;
196     dinfo->next = dmalloc_list;
197     if (dinfo->next)
198         dinfo->next->prev = dinfo;
199     dmalloc_list = dinfo;
200     
201     memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
202     res += sizeof(*dinfo) + 8*sizeof(char);
203     memcpy(res + nbytes, tail, 8*sizeof(char));
204     return res;
205 }
206
207 void xmalloc_trav_d(const char *file, int line)
208 {
209     size_t size = 0;
210     struct dmalloc_info *dinfo = dmalloc_list;
211     
212     if (!log_level_initialized)
213     {
214         log_level=yaz_log_module_level("malloc");
215         log_level_initialized=1;
216     }
217
218     yaz_log (log_level, "malloc_trav %s:%d", file, line);
219     while (dinfo)
220     {
221         yaz_log (log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
222               ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
223         size += dinfo->len;
224         dinfo = dinfo->next;
225     }
226     yaz_log (log_level, "total bytes %ld", (long) size);
227 }
228
229 #else
230 /* TRACE_XMALLOC <= 1 */
231 #define xrealloc_d(o, x, f, l) realloc(o, x)
232 #define xmalloc_d(x, f, l) malloc(x)
233 #define xcalloc_d(x,y, f, l) calloc(x,y)
234 #define xfree_d(x, f, l) free(x)
235 #define xmalloc_trav_d(f, l) 
236 #endif
237
238 void xmalloc_trav_f(const char *s, const char *file, int line)
239 {
240     if (!log_level_initialized)
241     {
242         log_level=yaz_log_module_level("malloc");
243         log_level_initialized=1;
244     }
245
246     xmalloc_trav_d(file, line);
247 }
248
249 void *xrealloc_f (void *o, size_t size, const char *file, int line)
250 {
251     void *p = xrealloc_d (o, size, file, line);
252
253     if (!log_level_initialized)
254     {
255         log_level=yaz_log_module_level("malloc");
256         log_level_initialized=1;
257     }
258
259     if(log_level)
260         yaz_log (log_level,
261             "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
262     if (!p)
263     {
264         yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%d bytes)",
265                  size);
266         exit(1);
267     }
268     return p;
269 }
270
271 void *xmalloc_f (size_t size, const char *file, int line)
272 {
273     void *p = xmalloc_d (size, file, line);
274     
275     if (!log_level_initialized)
276     {
277         log_level=yaz_log_module_level("malloc");
278         log_level_initialized=1;
279     }
280
281     if (log_level)
282         yaz_log (log_level, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
283
284     if (!p)
285     {
286         yaz_log (YLOG_FATAL, "Out of memory - malloc (%d bytes)", size);
287         exit (1);
288     }
289     return p;
290 }
291
292 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
293 {
294     void *p = xcalloc_d (nmemb, size, file, line);
295     if (!log_level_initialized)
296     {
297         log_level=yaz_log_module_level("malloc");
298         log_level_initialized=1;
299     }
300
301     if (log_level)
302         yaz_log (log_level, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
303
304     if (!p)
305     {
306         yaz_log (YLOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
307         exit (1);
308     }
309     return p;
310 }
311
312 char *xstrdup_f (const char *s, const char *file, int line)
313 {
314     char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
315     if (!log_level_initialized)
316     {
317         log_level=yaz_log_module_level("malloc");
318         log_level_initialized=1;
319     }
320
321     if (log_level)
322         yaz_log (log_level, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
323
324     strcpy (p, s);
325     return p;
326 }
327
328 void xfree_f(void *p, const char *file, int line)
329 {
330     if (!p)
331         return ;
332     if (log_level)
333         yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
334     xfree_d(p, file, line);
335 }