2 * Copyright (C) 1994-2001, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: xmalloc.c,v 1.14 2001-11-06 17:05:19 adam Exp $
18 #include <yaz/xmalloc.h>
20 #define TRACE_XMALLOC 2
24 static const unsigned char head[] = {44, 33, 22, 11};
25 static const unsigned char tail[] = {11, 22, 33, 44};
26 static const unsigned char freed[] = {11, 22, 33, 44};
32 struct dmalloc_info *next;
33 struct dmalloc_info *prev;
36 struct dmalloc_info *dmalloc_list = 0;
38 void *xmalloc_d(size_t nbytes, const char *file, int line)
41 struct dmalloc_info *dinfo;
43 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
45 dinfo = (struct dmalloc_info *) res;
46 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
47 dinfo->file[sizeof(dinfo->file)-1] = '\0';
52 dinfo->next = dmalloc_list;
54 dinfo->next->prev = dinfo;
57 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
58 res += sizeof(*dinfo) + 4*sizeof(char);
59 memcpy(res + nbytes, tail, 4*sizeof(char));
63 void xfree_d(void *ptr, const char *file, int line)
65 struct dmalloc_info *dinfo;
69 dinfo = (struct dmalloc_info *)
70 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
71 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
73 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
76 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
78 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
82 dinfo->prev->next = dinfo->next;
84 dmalloc_list = dinfo->next;
86 dinfo->next->prev = dinfo->prev;
87 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
92 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
94 struct dmalloc_info *dinfo;
95 char *ptr = (char*) p;
102 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
106 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
108 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
112 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
113 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
115 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
120 dinfo->prev->next = dinfo->next;
122 dmalloc_list = dinfo->next;
124 dinfo->next->prev = dinfo->prev;
132 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
136 dinfo = (struct dmalloc_info *) res;
137 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
138 dinfo->file[sizeof(dinfo->file)-1] = '\0';
143 dinfo->next = dmalloc_list;
145 dmalloc_list->prev = dinfo;
146 dmalloc_list = dinfo;
148 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
149 res += sizeof(*dinfo) + 4*sizeof(char);
150 memcpy(res + nbytes, tail, 4*sizeof(char));
154 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
157 struct dmalloc_info *dinfo;
158 size_t nbytes = nmemb * size;
160 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
162 dinfo = (struct dmalloc_info *) res;
163 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
164 dinfo->file[sizeof(dinfo->file)-1] = '\0';
169 dinfo->next = dmalloc_list;
171 dinfo->next->prev = dinfo;
172 dmalloc_list = dinfo;
174 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
175 res += sizeof(*dinfo) + 4*sizeof(char);
176 memcpy(res + nbytes, tail, 4*sizeof(char));
180 void xmalloc_trav_d(const char *file, int line)
183 struct dmalloc_info *dinfo = dmalloc_list;
185 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
188 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
189 ((char*) dinfo)+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
193 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
197 /* TRACE_XMALLOC <= 1 */
198 #define xrealloc_d(o, x, f, l) realloc(o, x)
199 #define xmalloc_d(x, f, l) malloc(x)
200 #define xcalloc_d(x,y, f, l) calloc(x,y)
201 #define xfree_d(x, f, l) free(x)
202 #define xmalloc_trav_d(f, l)
205 void xmalloc_trav_f(const char *s, const char *file, int line)
207 xmalloc_trav_d(file, line);
210 void *xrealloc_f (void *o, size_t size, const char *file, int line)
212 void *p = xrealloc_d (o, size, file, line);
216 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
220 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
227 void *xmalloc_f (size_t size, const char *file, int line)
229 void *p = xmalloc_d (size, file, line);
232 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
236 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
242 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
244 void *p = xcalloc_d (nmemb, size, file, line);
246 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
250 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
256 char *xstrdup_f (const char *s, const char *file, int line)
258 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
260 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
266 void xfree_f(void *p, const char *file, int line)
272 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
274 xfree_d(p, file, line);