2 * Copyright (C) 1995-2005, Index Data ApS
5 * $Id: xmalloc.c,v 1.7 2006-04-21 10:28:07 adam Exp $
9 * \brief Implements malloc interface.
21 #include <yaz/xmalloc.h>
24 #define TRACE_XMALLOC 1
27 static int log_level=0;
28 static int log_level_initialized=0;
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};
40 struct dmalloc_info *next;
41 struct dmalloc_info *prev;
44 struct dmalloc_info *dmalloc_list = 0;
47 void *xmalloc_d(size_t nbytes, const char *file, int line)
50 struct dmalloc_info *dinfo;
52 if (!log_level_initialized)
54 log_level=yaz_log_module_level("malloc");
55 log_level_initialized=1;
58 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
60 dinfo = (struct dmalloc_info *) res;
61 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
62 dinfo->file[sizeof(dinfo->file)-1] = '\0';
67 dinfo->next = dmalloc_list;
69 dinfo->next->prev = dinfo;
72 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
73 res += sizeof(*dinfo) + 8*sizeof(char);
74 memcpy(res + nbytes, tail, 8*sizeof(char));
78 void xfree_d(void *ptr, const char *file, int line)
80 struct dmalloc_info *dinfo;
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)))
88 yaz_log(YLOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
91 if (memcmp((char*) ptr + dinfo->len, tail, 8*sizeof(char)))
93 yaz_log(YLOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
97 dinfo->prev->next = dinfo->next;
99 dmalloc_list = dinfo->next;
101 dinfo->next->prev = dinfo->prev;
102 memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
107 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
109 struct dmalloc_info *dinfo;
110 char *ptr = (char*) p;
113 if (!log_level_initialized)
115 log_level=yaz_log_module_level("malloc");
116 log_level_initialized=1;
123 res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char));
127 if (memcmp(head, ptr - 8*sizeof(char), 8*sizeof(char)))
129 yaz_log(YLOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
133 dinfo = (struct dmalloc_info *) (ptr-8*sizeof(char) - sizeof(*dinfo));
134 if (memcmp(ptr + dinfo->len, tail, 8*sizeof(char)))
136 yaz_log(YLOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
141 dinfo->prev->next = dinfo->next;
143 dmalloc_list = dinfo->next;
145 dinfo->next->prev = dinfo->prev;
153 realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char));
157 dinfo = (struct dmalloc_info *) res;
158 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
159 dinfo->file[sizeof(dinfo->file)-1] = '\0';
164 dinfo->next = dmalloc_list;
166 dmalloc_list->prev = dinfo;
167 dmalloc_list = dinfo;
169 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
170 res += sizeof(*dinfo) + 8*sizeof(char);
171 memcpy(res + nbytes, tail, 8*sizeof(char));
175 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
178 struct dmalloc_info *dinfo;
179 size_t nbytes = nmemb * size;
181 if (!log_level_initialized)
183 log_level=yaz_log_module_level("malloc");
184 log_level_initialized=1;
187 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
189 dinfo = (struct dmalloc_info *) res;
190 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
191 dinfo->file[sizeof(dinfo->file)-1] = '\0';
196 dinfo->next = dmalloc_list;
198 dinfo->next->prev = dinfo;
199 dmalloc_list = dinfo;
201 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
202 res += sizeof(*dinfo) + 8*sizeof(char);
203 memcpy(res + nbytes, tail, 8*sizeof(char));
207 void xmalloc_trav_d(const char *file, int line)
210 struct dmalloc_info *dinfo = dmalloc_list;
212 if (!log_level_initialized)
214 log_level=yaz_log_module_level("malloc");
215 log_level_initialized=1;
218 yaz_log (log_level, "malloc_trav %s:%d", file, line);
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);
226 yaz_log (log_level, "total bytes %ld", (long) size);
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)
238 void xmalloc_trav_f(const char *s, const char *file, int line)
240 if (!log_level_initialized)
242 log_level=yaz_log_module_level("malloc");
243 log_level_initialized=1;
246 xmalloc_trav_d(file, line);
249 void *xrealloc_f (void *o, size_t size, const char *file, int line)
251 void *p = xrealloc_d (o, size, file, line);
253 if (!log_level_initialized)
255 log_level=yaz_log_module_level("malloc");
256 log_level_initialized=1;
261 "%s:%d: xrealloc(s=%ld) %p -> %p", file, line, (long) size, o, p);
264 yaz_log (YLOG_FATAL|YLOG_ERRNO, "Out of memory, realloc (%ld bytes)",
271 void *xmalloc_f (size_t size, const char *file, int line)
273 void *p = xmalloc_d (size, file, line);
275 if (!log_level_initialized)
277 log_level=yaz_log_module_level("malloc");
278 log_level_initialized=1;
282 yaz_log (log_level, "%s:%d: xmalloc(s=%ld) %p", file, line,
287 yaz_log (YLOG_FATAL, "Out of memory - malloc (%ld bytes)",
294 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
296 void *p = xcalloc_d (nmemb, size, file, line);
297 if (!log_level_initialized)
299 log_level=yaz_log_module_level("malloc");
300 log_level_initialized=1;
304 yaz_log (log_level, "%s:%d: xcalloc(s=%ld) %p", file, line,
309 yaz_log (YLOG_FATAL, "Out of memory - calloc (%ld, %ld)",
310 (long) nmemb, (long) size);
316 char *xstrdup_f (const char *s, const char *file, int line)
318 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
319 if (!log_level_initialized)
321 log_level=yaz_log_module_level("malloc");
322 log_level_initialized=1;
326 yaz_log (log_level, "%s:%d: xstrdup(s=%ld) %p", file, line,
327 (long) strlen(s)+1, p);
333 void xfree_f(void *p, const char *file, int line)
338 yaz_log (log_level, "%s:%d: xfree %p", file, line, p);
339 xfree_d(p, file, line);
344 * indent-tabs-mode: nil
346 * vim: shiftwidth=4 tabstop=8 expandtab