2 * Copyright (C) 1994-2004, Index Data
5 * $Id: xmalloc.c,v 1.2 2004-10-15 00:19:01 adam Exp $
9 * \brief Implements malloc interface.
21 #include <yaz/xmalloc.h>
24 #define TRACE_XMALLOC 1
29 static const unsigned char head[] = {88, 77, 66, 55, 44, 33, 22, 11};
30 static const unsigned char tail[] = {11, 22, 33, 44, 55, 66, 77, 88};
31 static const unsigned char freed[] = {11, 22, 33, 44, 55, 66, 77, 88};
37 struct dmalloc_info *next;
38 struct dmalloc_info *prev;
41 struct dmalloc_info *dmalloc_list = 0;
43 void *xmalloc_d(size_t nbytes, const char *file, int line)
46 struct dmalloc_info *dinfo;
48 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+16*sizeof(char))))
50 dinfo = (struct dmalloc_info *) res;
51 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
52 dinfo->file[sizeof(dinfo->file)-1] = '\0';
57 dinfo->next = dmalloc_list;
59 dinfo->next->prev = dinfo;
62 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
63 res += sizeof(*dinfo) + 8*sizeof(char);
64 memcpy(res + nbytes, tail, 8*sizeof(char));
68 void xfree_d(void *ptr, const char *file, int line)
70 struct dmalloc_info *dinfo;
74 dinfo = (struct dmalloc_info *)
75 ((char*)ptr - 8*sizeof(char) - sizeof(*dinfo));
76 if (memcmp(head, (char*) ptr - 8*sizeof(char), 8*sizeof(char)))
78 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
81 if (memcmp((char*) ptr + dinfo->len, tail, 8*sizeof(char)))
83 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
87 dinfo->prev->next = dinfo->next;
89 dmalloc_list = dinfo->next;
91 dinfo->next->prev = dinfo->prev;
92 memcpy ((char*) ptr - 8*sizeof(char), freed, 8*sizeof(char));
97 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
99 struct dmalloc_info *dinfo;
100 char *ptr = (char*) p;
107 res = (char *) malloc(nbytes + sizeof(*dinfo) + 16*sizeof(char));
111 if (memcmp(head, ptr - 8*sizeof(char), 8*sizeof(char)))
113 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
117 dinfo = (struct dmalloc_info *) (ptr-8*sizeof(char) - sizeof(*dinfo));
118 if (memcmp(ptr + dinfo->len, tail, 8*sizeof(char)))
120 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
125 dinfo->prev->next = dinfo->next;
127 dmalloc_list = dinfo->next;
129 dinfo->next->prev = dinfo->prev;
137 realloc(dinfo, nbytes + sizeof(*dinfo) + 16*sizeof(char));
141 dinfo = (struct dmalloc_info *) res;
142 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
143 dinfo->file[sizeof(dinfo->file)-1] = '\0';
148 dinfo->next = dmalloc_list;
150 dmalloc_list->prev = dinfo;
151 dmalloc_list = dinfo;
153 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
154 res += sizeof(*dinfo) + 8*sizeof(char);
155 memcpy(res + nbytes, tail, 8*sizeof(char));
159 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
162 struct dmalloc_info *dinfo;
163 size_t nbytes = nmemb * size;
165 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+16*sizeof(char))))
167 dinfo = (struct dmalloc_info *) res;
168 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
169 dinfo->file[sizeof(dinfo->file)-1] = '\0';
174 dinfo->next = dmalloc_list;
176 dinfo->next->prev = dinfo;
177 dmalloc_list = dinfo;
179 memcpy(res + sizeof(*dinfo), head, 8*sizeof(char));
180 res += sizeof(*dinfo) + 8*sizeof(char);
181 memcpy(res + nbytes, tail, 8*sizeof(char));
185 void xmalloc_trav_d(const char *file, int line)
188 struct dmalloc_info *dinfo = dmalloc_list;
190 yaz_log (LOG_MALLOC, "malloc_trav %s:%d", file, line);
193 yaz_log (LOG_MALLOC, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
194 ((char*) dinfo)+sizeof(*dinfo)+8*sizeof(char), dinfo->len);
198 yaz_log (LOG_MALLOC, "total bytes %ld", (long) size);
202 /* TRACE_XMALLOC <= 1 */
203 #define xrealloc_d(o, x, f, l) realloc(o, x)
204 #define xmalloc_d(x, f, l) malloc(x)
205 #define xcalloc_d(x,y, f, l) calloc(x,y)
206 #define xfree_d(x, f, l) free(x)
207 #define xmalloc_trav_d(f, l)
210 void xmalloc_trav_f(const char *s, const char *file, int line)
212 xmalloc_trav_d(file, line);
215 void *xrealloc_f (void *o, size_t size, const char *file, int line)
217 void *p = xrealloc_d (o, size, file, line);
221 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
225 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
232 void *xmalloc_f (size_t size, const char *file, int line)
234 void *p = xmalloc_d (size, file, line);
237 yaz_log (LOG_MALLOC, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
241 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
247 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
249 void *p = xcalloc_d (nmemb, size, file, line);
251 yaz_log (LOG_MALLOC, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
255 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
261 char *xstrdup_f (const char *s, const char *file, int line)
263 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
265 yaz_log (LOG_MALLOC, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
271 void xfree_f(void *p, const char *file, int line)
277 yaz_log (LOG_MALLOC, "%s:%d: xfree %p", file, line, p);
279 xfree_d(p, file, line);