2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.13 2001-09-30 20:10:28 adam
10 * Revision 1.12 2001/09/24 21:51:56 adam
11 * New Z39.50 OID utilities: yaz_oidval_to_z3950oid, yaz_str_to_z3950oid
12 * and yaz_z3950oid_to_str.
14 * Revision 1.11 2000/02/29 13:44:55 adam
15 * Check for config.h (currently not generated).
17 * Revision 1.10 1999/11/30 13:47:12 adam
18 * Improved installation. Moved header files to include/yaz.
20 * Revision 1.9 1999/09/10 08:58:32 adam
21 * Set TRACE_XMALLOC to 1.
23 * Revision 1.8 1999/08/27 09:40:32 adam
24 * Renamed logf function to yaz_log. Removed VC++ project files.
26 * Revision 1.7 1999/07/13 13:24:53 adam
27 * Updated memory debugging memory allocatation routines.
29 * Revision 1.6 1998/02/11 11:53:36 adam
30 * Changed code so that it compiles as C++.
32 * Revision 1.5 1997/10/31 12:20:09 adam
33 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
34 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
35 * Bug fix: missing fclose in data1_read_espec1.
37 * Revision 1.4 1996/07/03 13:21:36 adam
38 * Function xfree_f checks for NULL pointer.
40 * Revision 1.3 1995/12/05 15:08:44 adam
41 * Fixed verbose of xrealloc.
43 * Revision 1.2 1995/12/05 11:08:37 adam
44 * More verbose malloc routines.
46 * Revision 1.1 1995/11/01 11:56:53 quinn
49 * Revision 1.6 1995/10/16 14:03:11 quinn
50 * Changes to support element set names and espec1
52 * Revision 1.5 1995/09/04 12:34:06 adam
53 * Various cleanup. YAZ util used instead.
55 * Revision 1.4 1994/10/05 10:16:16 quinn
56 * Added xrealloc. Fixed bug in log.
58 * Revision 1.3 1994/09/26 16:31:37 adam
61 * Revision 1.2 1994/08/18 08:23:26 adam
62 * Res.c now use handles. xmalloc defines xstrdup.
64 * Revision 1.1 1994/08/17 13:37:54 adam
65 * xmalloc.c added to util.
78 #include <yaz/xmalloc.h>
80 #define TRACE_XMALLOC 1
84 static const unsigned char head[] = {44, 33, 22, 11};
85 static const unsigned char tail[] = {11, 22, 33, 44};
86 static const unsigned char freed[] = {11, 22, 33, 44};
92 struct dmalloc_info *next;
93 struct dmalloc_info *prev;
96 struct dmalloc_info *dmalloc_list = 0;
98 void *xmalloc_d(size_t nbytes, const char *file, int line)
101 struct dmalloc_info *dinfo;
103 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
105 dinfo = (struct dmalloc_info *) res;
106 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
107 dinfo->file[sizeof(dinfo->file)-1] = '\0';
112 dinfo->next = dmalloc_list;
114 dinfo->next->prev = dinfo;
115 dmalloc_list = dinfo;
117 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
118 res += sizeof(*dinfo) + 4*sizeof(char);
119 memcpy(res + nbytes, tail, 4*sizeof(char));
123 void xfree_d(void *ptr, const char *file, int line)
125 struct dmalloc_info *dinfo;
129 dinfo = (struct dmalloc_info *)
130 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
131 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
133 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
136 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
138 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
142 dinfo->prev->next = dinfo->next;
144 dmalloc_list = dinfo->next;
146 dinfo->next->prev = dinfo->prev;
147 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
152 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
154 struct dmalloc_info *dinfo;
155 char *ptr = (char*) p;
162 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
166 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
168 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
172 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
173 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
175 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
180 dinfo->prev->next = dinfo->next;
182 dmalloc_list = dinfo->next;
184 dinfo->next->prev = dinfo->prev;
192 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
196 dinfo = (struct dmalloc_info *) res;
197 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
198 dinfo->file[sizeof(dinfo->file)-1] = '\0';
203 dinfo->next = dmalloc_list;
205 dmalloc_list->prev = dinfo;
206 dmalloc_list = dinfo;
208 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
209 res += sizeof(*dinfo) + 4*sizeof(char);
210 memcpy(res + nbytes, tail, 4*sizeof(char));
214 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
217 struct dmalloc_info *dinfo;
218 size_t nbytes = nmemb * size;
220 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
222 dinfo = (struct dmalloc_info *) res;
223 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
224 dinfo->file[sizeof(dinfo->file)-1] = '\0';
229 dinfo->next = dmalloc_list;
231 dinfo->next->prev = dinfo;
232 dmalloc_list = dinfo;
234 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
235 res += sizeof(*dinfo) + 4*sizeof(char);
236 memcpy(res + nbytes, tail, 4*sizeof(char));
240 void xmalloc_trav_d(const char *file, int line)
243 struct dmalloc_info *dinfo = dmalloc_list;
245 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
248 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
249 ((char*) dinfo)+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
253 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
257 /* TRACE_XMALLOC <= 1 */
258 #define xrealloc_d(o, x, f, l) realloc(o, x)
259 #define xmalloc_d(x, f, l) malloc(x)
260 #define xcalloc_d(x,y, f, l) calloc(x,y)
261 #define xfree_d(x, f, l) free(x)
262 #define xmalloc_trav_d(f, l)
265 void xmalloc_trav_f(const char *s, const char *file, int line)
267 xmalloc_trav_d(file, line);
270 void *xrealloc_f (void *o, size_t size, const char *file, int line)
272 void *p = xrealloc_d (o, size, file, line);
276 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
280 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
287 void *xmalloc_f (size_t size, const char *file, int line)
289 void *p = xmalloc_d (size, file, line);
292 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
296 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
302 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
304 void *p = xcalloc_d (nmemb, size, file, line);
306 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
310 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
316 char *xstrdup_f (const char *s, const char *file, int line)
318 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
320 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
326 void xfree_f(void *p, const char *file, int line)
332 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
334 xfree_d(p, file, line);