2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.10 1999-11-30 13:47:12 adam
8 * Improved installation. Moved header files to include/yaz.
10 * Revision 1.9 1999/09/10 08:58:32 adam
11 * Set TRACE_XMALLOC to 1.
13 * Revision 1.8 1999/08/27 09:40:32 adam
14 * Renamed logf function to yaz_log. Removed VC++ project files.
16 * Revision 1.7 1999/07/13 13:24:53 adam
17 * Updated memory debugging memory allocatation routines.
19 * Revision 1.6 1998/02/11 11:53:36 adam
20 * Changed code so that it compiles as C++.
22 * Revision 1.5 1997/10/31 12:20:09 adam
23 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
24 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
25 * Bug fix: missing fclose in data1_read_espec1.
27 * Revision 1.4 1996/07/03 13:21:36 adam
28 * Function xfree_f checks for NULL pointer.
30 * Revision 1.3 1995/12/05 15:08:44 adam
31 * Fixed verbose of xrealloc.
33 * Revision 1.2 1995/12/05 11:08:37 adam
34 * More verbose malloc routines.
36 * Revision 1.1 1995/11/01 11:56:53 quinn
39 * Revision 1.6 1995/10/16 14:03:11 quinn
40 * Changes to support element set names and espec1
42 * Revision 1.5 1995/09/04 12:34:06 adam
43 * Various cleanup. YAZ util used instead.
45 * Revision 1.4 1994/10/05 10:16:16 quinn
46 * Added xrealloc. Fixed bug in log.
48 * Revision 1.3 1994/09/26 16:31:37 adam
51 * Revision 1.2 1994/08/18 08:23:26 adam
52 * Res.c now use handles. xmalloc defines xstrdup.
54 * Revision 1.1 1994/08/17 13:37:54 adam
55 * xmalloc.c added to util.
64 #include <yaz/xmalloc.h>
66 #define TRACE_XMALLOC 1
70 static const unsigned char head[] = {44, 33, 22, 11};
71 static const unsigned char tail[] = {11, 22, 33, 44};
72 static const unsigned char freed[] = {11, 22, 33, 44};
78 struct dmalloc_info *next;
79 struct dmalloc_info *prev;
82 struct dmalloc_info *dmalloc_list = 0;
84 void *xmalloc_d(size_t nbytes, const char *file, int line)
87 struct dmalloc_info *dinfo;
89 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
91 dinfo = (struct dmalloc_info *) res;
92 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
93 dinfo->file[sizeof(dinfo->file)-1] = '\0';
98 dinfo->next = dmalloc_list;
100 dinfo->next->prev = dinfo;
101 dmalloc_list = dinfo;
103 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
104 res += sizeof(*dinfo) + 4*sizeof(char);
105 memcpy(res + nbytes, tail, 4*sizeof(char));
109 void xfree_d(void *ptr, const char *file, int line)
111 struct dmalloc_info *dinfo;
115 dinfo = (struct dmalloc_info *)
116 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
117 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
119 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
122 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
124 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
128 dinfo->prev->next = dinfo->next;
130 dmalloc_list = dinfo->next;
132 dinfo->next->prev = dinfo->prev;
133 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
138 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
140 struct dmalloc_info *dinfo;
141 char *ptr = (char*) p;
148 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
152 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
154 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
158 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
159 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
161 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
166 dinfo->prev->next = dinfo->next;
168 dmalloc_list = dinfo->next;
170 dinfo->next->prev = dinfo->prev;
178 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
182 dinfo = (struct dmalloc_info *) res;
183 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
184 dinfo->file[sizeof(dinfo->file)-1] = '\0';
189 dinfo->next = dmalloc_list;
191 dmalloc_list->prev = dinfo;
192 dmalloc_list = dinfo;
194 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
195 res += sizeof(*dinfo) + 4*sizeof(char);
196 memcpy(res + nbytes, tail, 4*sizeof(char));
200 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
203 struct dmalloc_info *dinfo;
204 size_t nbytes = nmemb * size;
206 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
208 dinfo = (struct dmalloc_info *) res;
209 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
210 dinfo->file[sizeof(dinfo->file)-1] = '\0';
215 dinfo->next = dmalloc_list;
217 dinfo->next->prev = dinfo;
218 dmalloc_list = dinfo;
220 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
221 res += sizeof(*dinfo) + 4*sizeof(char);
222 memcpy(res + nbytes, tail, 4*sizeof(char));
226 void xmalloc_trav_d(const char *file, int line)
229 struct dmalloc_info *dinfo = dmalloc_list;
231 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
234 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
235 dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
239 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
243 /* TRACE_XMALLOC <= 1 */
244 #define xrealloc_d(o, x, f, l) realloc(o, x)
245 #define xmalloc_d(x, f, l) malloc(x)
246 #define xcalloc_d(x,y, f, l) calloc(x,y)
247 #define xfree_d(x, f, l) free(x)
248 #define xmalloc_trav_d(f, l)
251 void xmalloc_trav_f(const char *s, const char *file, int line)
253 xmalloc_trav_d(file, line);
256 void *xrealloc_f (void *o, size_t size, const char *file, int line)
258 void *p = xrealloc_d (o, size, file, line);
262 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
266 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
273 void *xmalloc_f (size_t size, const char *file, int line)
275 void *p = xmalloc_d (size, file, line);
278 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
282 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
288 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
290 void *p = xcalloc_d (nmemb, size, file, line);
292 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
296 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
302 char *xstrdup_f (const char *s, const char *file, int line)
304 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
306 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
312 void xfree_f(void *p, const char *file, int line)
318 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
320 xfree_d(p, file, line);