2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.12 2001-09-24 21:51:56 adam
8 * New Z39.50 OID utilities: yaz_oidval_to_z3950oid, yaz_str_to_z3950oid
9 * and yaz_z3950oid_to_str.
11 * Revision 1.11 2000/02/29 13:44:55 adam
12 * Check for config.h (currently not generated).
14 * Revision 1.10 1999/11/30 13:47:12 adam
15 * Improved installation. Moved header files to include/yaz.
17 * Revision 1.9 1999/09/10 08:58:32 adam
18 * Set TRACE_XMALLOC to 1.
20 * Revision 1.8 1999/08/27 09:40:32 adam
21 * Renamed logf function to yaz_log. Removed VC++ project files.
23 * Revision 1.7 1999/07/13 13:24:53 adam
24 * Updated memory debugging memory allocatation routines.
26 * Revision 1.6 1998/02/11 11:53:36 adam
27 * Changed code so that it compiles as C++.
29 * Revision 1.5 1997/10/31 12:20:09 adam
30 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
31 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
32 * Bug fix: missing fclose in data1_read_espec1.
34 * Revision 1.4 1996/07/03 13:21:36 adam
35 * Function xfree_f checks for NULL pointer.
37 * Revision 1.3 1995/12/05 15:08:44 adam
38 * Fixed verbose of xrealloc.
40 * Revision 1.2 1995/12/05 11:08:37 adam
41 * More verbose malloc routines.
43 * Revision 1.1 1995/11/01 11:56:53 quinn
46 * Revision 1.6 1995/10/16 14:03:11 quinn
47 * Changes to support element set names and espec1
49 * Revision 1.5 1995/09/04 12:34:06 adam
50 * Various cleanup. YAZ util used instead.
52 * Revision 1.4 1994/10/05 10:16:16 quinn
53 * Added xrealloc. Fixed bug in log.
55 * Revision 1.3 1994/09/26 16:31:37 adam
58 * Revision 1.2 1994/08/18 08:23:26 adam
59 * Res.c now use handles. xmalloc defines xstrdup.
61 * Revision 1.1 1994/08/17 13:37:54 adam
62 * xmalloc.c added to util.
75 #include <yaz/xmalloc.h>
77 #define TRACE_XMALLOC 2
81 static const unsigned char head[] = {44, 33, 22, 11};
82 static const unsigned char tail[] = {11, 22, 33, 44};
83 static const unsigned char freed[] = {11, 22, 33, 44};
89 struct dmalloc_info *next;
90 struct dmalloc_info *prev;
93 struct dmalloc_info *dmalloc_list = 0;
95 void *xmalloc_d(size_t nbytes, const char *file, int line)
98 struct dmalloc_info *dinfo;
100 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
102 dinfo = (struct dmalloc_info *) res;
103 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
104 dinfo->file[sizeof(dinfo->file)-1] = '\0';
109 dinfo->next = dmalloc_list;
111 dinfo->next->prev = dinfo;
112 dmalloc_list = dinfo;
114 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
115 res += sizeof(*dinfo) + 4*sizeof(char);
116 memcpy(res + nbytes, tail, 4*sizeof(char));
120 void xfree_d(void *ptr, const char *file, int line)
122 struct dmalloc_info *dinfo;
126 dinfo = (struct dmalloc_info *)
127 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
128 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
130 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
133 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
135 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
139 dinfo->prev->next = dinfo->next;
141 dmalloc_list = dinfo->next;
143 dinfo->next->prev = dinfo->prev;
144 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
149 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
151 struct dmalloc_info *dinfo;
152 char *ptr = (char*) p;
159 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
163 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
165 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
169 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
170 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
172 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
177 dinfo->prev->next = dinfo->next;
179 dmalloc_list = dinfo->next;
181 dinfo->next->prev = dinfo->prev;
189 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
193 dinfo = (struct dmalloc_info *) res;
194 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
195 dinfo->file[sizeof(dinfo->file)-1] = '\0';
200 dinfo->next = dmalloc_list;
202 dmalloc_list->prev = dinfo;
203 dmalloc_list = dinfo;
205 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
206 res += sizeof(*dinfo) + 4*sizeof(char);
207 memcpy(res + nbytes, tail, 4*sizeof(char));
211 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
214 struct dmalloc_info *dinfo;
215 size_t nbytes = nmemb * size;
217 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
219 dinfo = (struct dmalloc_info *) res;
220 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
221 dinfo->file[sizeof(dinfo->file)-1] = '\0';
226 dinfo->next = dmalloc_list;
228 dinfo->next->prev = dinfo;
229 dmalloc_list = dinfo;
231 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
232 res += sizeof(*dinfo) + 4*sizeof(char);
233 memcpy(res + nbytes, tail, 4*sizeof(char));
237 void xmalloc_trav_d(const char *file, int line)
240 struct dmalloc_info *dinfo = dmalloc_list;
242 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
245 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
246 ((char*) dinfo)+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
250 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
254 /* TRACE_XMALLOC <= 1 */
255 #define xrealloc_d(o, x, f, l) realloc(o, x)
256 #define xmalloc_d(x, f, l) malloc(x)
257 #define xcalloc_d(x,y, f, l) calloc(x,y)
258 #define xfree_d(x, f, l) free(x)
259 #define xmalloc_trav_d(f, l)
262 void xmalloc_trav_f(const char *s, const char *file, int line)
264 xmalloc_trav_d(file, line);
267 void *xrealloc_f (void *o, size_t size, const char *file, int line)
269 void *p = xrealloc_d (o, size, file, line);
273 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
277 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
284 void *xmalloc_f (size_t size, const char *file, int line)
286 void *p = xmalloc_d (size, file, line);
289 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
293 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
299 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
301 void *p = xcalloc_d (nmemb, size, file, line);
303 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
307 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
313 char *xstrdup_f (const char *s, const char *file, int line)
315 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
317 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
323 void xfree_f(void *p, const char *file, int line)
329 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
331 xfree_d(p, file, line);