2 * Copyright (C) 1994-2003, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: xmalloc.c,v 1.18 2003-01-06 08:20:28 adam Exp $
18 #include <yaz/xmalloc.h>
21 #define TRACE_XMALLOC 1
26 static const unsigned char head[] = {44, 33, 22, 11};
27 static const unsigned char tail[] = {11, 22, 33, 44};
28 static const unsigned char freed[] = {11, 22, 33, 44};
34 struct dmalloc_info *next;
35 struct dmalloc_info *prev;
38 struct dmalloc_info *dmalloc_list = 0;
40 void *xmalloc_d(size_t nbytes, const char *file, int line)
43 struct dmalloc_info *dinfo;
45 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
47 dinfo = (struct dmalloc_info *) res;
48 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
49 dinfo->file[sizeof(dinfo->file)-1] = '\0';
54 dinfo->next = dmalloc_list;
56 dinfo->next->prev = dinfo;
59 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
60 res += sizeof(*dinfo) + 4*sizeof(char);
61 memcpy(res + nbytes, tail, 4*sizeof(char));
65 void xfree_d(void *ptr, const char *file, int line)
67 struct dmalloc_info *dinfo;
71 dinfo = (struct dmalloc_info *)
72 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
73 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
75 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
78 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
80 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
84 dinfo->prev->next = dinfo->next;
86 dmalloc_list = dinfo->next;
88 dinfo->next->prev = dinfo->prev;
89 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
94 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
96 struct dmalloc_info *dinfo;
97 char *ptr = (char*) p;
104 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
108 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
110 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
114 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
115 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
117 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
122 dinfo->prev->next = dinfo->next;
124 dmalloc_list = dinfo->next;
126 dinfo->next->prev = dinfo->prev;
134 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
138 dinfo = (struct dmalloc_info *) res;
139 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
140 dinfo->file[sizeof(dinfo->file)-1] = '\0';
145 dinfo->next = dmalloc_list;
147 dmalloc_list->prev = dinfo;
148 dmalloc_list = dinfo;
150 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
151 res += sizeof(*dinfo) + 4*sizeof(char);
152 memcpy(res + nbytes, tail, 4*sizeof(char));
156 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
159 struct dmalloc_info *dinfo;
160 size_t nbytes = nmemb * size;
162 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
164 dinfo = (struct dmalloc_info *) res;
165 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
166 dinfo->file[sizeof(dinfo->file)-1] = '\0';
171 dinfo->next = dmalloc_list;
173 dinfo->next->prev = dinfo;
174 dmalloc_list = dinfo;
176 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
177 res += sizeof(*dinfo) + 4*sizeof(char);
178 memcpy(res + nbytes, tail, 4*sizeof(char));
182 void xmalloc_trav_d(const char *file, int line)
185 struct dmalloc_info *dinfo = dmalloc_list;
187 yaz_log (LOG_MALLOC, "malloc_trav %s:%d", file, line);
190 yaz_log (LOG_MALLOC, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
191 ((char*) dinfo)+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
195 yaz_log (LOG_MALLOC, "total bytes %ld", (long) size);
199 /* TRACE_XMALLOC <= 1 */
200 #define xrealloc_d(o, x, f, l) realloc(o, x)
201 #define xmalloc_d(x, f, l) malloc(x)
202 #define xcalloc_d(x,y, f, l) calloc(x,y)
203 #define xfree_d(x, f, l) free(x)
204 #define xmalloc_trav_d(f, l)
207 void xmalloc_trav_f(const char *s, const char *file, int line)
209 xmalloc_trav_d(file, line);
212 void *xrealloc_f (void *o, size_t size, const char *file, int line)
214 void *p = xrealloc_d (o, size, file, line);
218 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
222 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
229 void *xmalloc_f (size_t size, const char *file, int line)
231 void *p = xmalloc_d (size, file, line);
234 yaz_log (LOG_MALLOC, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
238 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
244 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
246 void *p = xcalloc_d (nmemb, size, file, line);
248 yaz_log (LOG_MALLOC, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
252 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
258 char *xstrdup_f (const char *s, const char *file, int line)
260 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
262 yaz_log (LOG_MALLOC, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
268 void xfree_f(void *p, const char *file, int line)
274 yaz_log (LOG_MALLOC, "%s:%d: xfree %p", file, line, p);
276 xfree_d(p, file, line);