1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2008 Index Data
3 * See the file LICENSE for details.
7 * \brief Implements malloc interface.
19 #include <yaz/xmalloc.h>
22 #define TRACE_XMALLOC 1
25 static int log_level=0;
26 static int log_level_initialized=0;
30 static const unsigned char head[] = {88, 77, 66, 55, 44, 33, 22, 11};
31 static const unsigned char tail[] = {11, 22, 33, 44, 55, 66, 77, 88};
32 static const unsigned char freed[] = {11, 22, 33, 44, 55, 66, 77, 88};
38 struct dmalloc_info *next;
39 struct dmalloc_info *prev;
42 struct dmalloc_info *dmalloc_list = 0;
45 void *xmalloc_d(size_t nbytes, const char *file, int line)
48 struct dmalloc_info *dinfo;
50 if (!log_level_initialized)
52 log_level=yaz_log_module_level("malloc");
53 log_level_initialized=1;
56 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
58 dinfo = (struct dmalloc_info *) res;
59 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
60 dinfo->file[sizeof(dinfo->file)-1] = '\0';
65 dinfo->next = dmalloc_list;
67 dinfo->next->prev = dinfo;
70 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
71 res += sizeof(*dinfo) + 8*sizeof(char);
72 memcpy(res + nbytes, tail, 8*sizeof(char));
76 void xfree_d(void *ptr, const char *file, int line)
78 struct dmalloc_info *dinfo;
82 dinfo = (struct dmalloc_info *)
83 ((char*)ptr - 8*sizeof(char) - sizeof(*dinfo));
84 if (memcmp(head, (char*) ptr - 8*sizeof(char), 8*sizeof(char)))
86 yaz_log(YLOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
89 if (memcmp((char*) ptr + dinfo->len, tail, 8*sizeof(char)))
91 yaz_log(YLOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
95 dinfo->prev->next = dinfo->next;
97 dmalloc_list = dinfo->next;
99 dinfo->next->prev = dinfo->prev;
100 memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
105 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
107 struct dmalloc_info *dinfo;
108 char *ptr = (char*) p;
111 if (!log_level_initialized)
113 log_level=yaz_log_module_level("malloc");
114 log_level_initialized=1;
121 res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char));
125 if (memcmp(head, ptr - 8*sizeof(char), 8*sizeof(char)))
127 yaz_log(YLOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
131 dinfo = (struct dmalloc_info *) (ptr-8*sizeof(char) - sizeof(*dinfo));
132 if (memcmp(ptr + dinfo->len, tail, 8*sizeof(char)))
134 yaz_log(YLOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
139 dinfo->prev->next = dinfo->next;
141 dmalloc_list = dinfo->next;
143 dinfo->next->prev = dinfo->prev;
151 realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char));
155 dinfo = (struct dmalloc_info *) res;
156 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
157 dinfo->file[sizeof(dinfo->file)-1] = '\0';
162 dinfo->next = dmalloc_list;
164 dmalloc_list->prev = dinfo;
165 dmalloc_list = dinfo;
167 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
168 res += sizeof(*dinfo) + 8*sizeof(char);
169 memcpy(res + nbytes, tail, 8*sizeof(char));
173 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
176 struct dmalloc_info *dinfo;
177 size_t nbytes = nmemb * size;
179 if (!log_level_initialized)
181 log_level=yaz_log_module_level("malloc");
182 log_level_initialized=1;
185 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
187 dinfo = (struct dmalloc_info *) res;
188 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
189 dinfo->file[sizeof(dinfo->file)-1] = '\0';
194 dinfo->next = dmalloc_list;
196 dinfo->next->prev = dinfo;
197 dmalloc_list = dinfo;
199 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
200 res += sizeof(*dinfo) + 8*sizeof(char);
201 memcpy(res + nbytes, tail, 8*sizeof(char));
205 void xmalloc_trav_d(const char *file, int line)
208 struct dmalloc_info *dinfo = dmalloc_list;
210 if (!log_level_initialized)
212 log_level=yaz_log_module_level("malloc");
213 log_level_initialized=1;
216 yaz_log (log_level, "malloc_trav %s:%d", file, line);
219 yaz_log (log_level, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
220 ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
224 yaz_log (log_level, "total bytes %ld", (long) size);
228 /* TRACE_XMALLOC <= 1 */
229 #define xrealloc_d(o, x, f, l) realloc(o, x)
230 #define xmalloc_d(x, f, l) malloc(x)
231 #define xcalloc_d(x,y, f, l) calloc(x,y)
232 #define xfree_d(x, f, l) free(x)
233 #define xmalloc_trav_d(f, l)
236 void xmalloc_trav_f(const char *s, const char *file, int line)
238 if (!log_level_initialized)
240 log_level=yaz_log_module_level("malloc");
241 log_level_initialized=1;
244 xmalloc_trav_d(file, line);
247 void xmalloc_fatal(void)
252 void *xrealloc_f (void *o, size_t size, const char *file, int line)
254 void *p = xrealloc_d (o, size, file, line);
256 if (!log_level_initialized)
258 log_level=yaz_log_module_level("malloc");
259 log_level_initialized=1;
264 "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p);
267 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)",
274 void *xmalloc_f (size_t size, const char *file, int line)
276 void *p = xmalloc_d (size, file, line);
278 if (!log_level_initialized)
280 log_level=yaz_log_module_level("malloc");
281 log_level_initialized=1;
285 yaz_log (log_level, "%s:%d: xmalloc(s=%ld) %p", file, line,
290 yaz_log (YLOG_FATAL, "Out of memory - malloc (%ld bytes)",
297 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
299 void *p = xcalloc_d (nmemb, size, file, line);
300 if (!log_level_initialized)
302 log_level=yaz_log_module_level("malloc");
303 log_level_initialized=1;
307 yaz_log (log_level, "%s:%d: xcalloc(s=%ld) %p", file, line,
312 yaz_log (YLOG_FATAL, "Out of memory - calloc (%ld, %ld)",
313 (long) nmemb, (long) size);
319 char *xstrdup_f (const char *s, const char *file, int line)
321 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
322 if (!log_level_initialized)
324 log_level=yaz_log_module_level("malloc");
325 log_level_initialized=1;
329 yaz_log (log_level, "%s:%d: xstrdup(s=%ld) %p", file, line,
330 (long) strlen(s)+1, p);
336 void xfree_f(void *p, const char *file, int line)
341 yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
342 xfree_d(p, file, line);
347 * indent-tabs-mode: nil
349 * vim: shiftwidth=4 tabstop=8 expandtab