2 * Copyright (C) 1994-2000, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.11 2000-02-29 13:44:55 adam
8 * Check for config.h (currently not generated).
10 * Revision 1.10 1999/11/30 13:47:12 adam
11 * Improved installation. Moved header files to include/yaz.
13 * Revision 1.9 1999/09/10 08:58:32 adam
14 * Set TRACE_XMALLOC to 1.
16 * Revision 1.8 1999/08/27 09:40:32 adam
17 * Renamed logf function to yaz_log. Removed VC++ project files.
19 * Revision 1.7 1999/07/13 13:24:53 adam
20 * Updated memory debugging memory allocatation routines.
22 * Revision 1.6 1998/02/11 11:53:36 adam
23 * Changed code so that it compiles as C++.
25 * Revision 1.5 1997/10/31 12:20:09 adam
26 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
27 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
28 * Bug fix: missing fclose in data1_read_espec1.
30 * Revision 1.4 1996/07/03 13:21:36 adam
31 * Function xfree_f checks for NULL pointer.
33 * Revision 1.3 1995/12/05 15:08:44 adam
34 * Fixed verbose of xrealloc.
36 * Revision 1.2 1995/12/05 11:08:37 adam
37 * More verbose malloc routines.
39 * Revision 1.1 1995/11/01 11:56:53 quinn
42 * Revision 1.6 1995/10/16 14:03:11 quinn
43 * Changes to support element set names and espec1
45 * Revision 1.5 1995/09/04 12:34:06 adam
46 * Various cleanup. YAZ util used instead.
48 * Revision 1.4 1994/10/05 10:16:16 quinn
49 * Added xrealloc. Fixed bug in log.
51 * Revision 1.3 1994/09/26 16:31:37 adam
54 * Revision 1.2 1994/08/18 08:23:26 adam
55 * Res.c now use handles. xmalloc defines xstrdup.
57 * Revision 1.1 1994/08/17 13:37:54 adam
58 * xmalloc.c added to util.
71 #include <yaz/xmalloc.h>
73 #define TRACE_XMALLOC 1
77 static const unsigned char head[] = {44, 33, 22, 11};
78 static const unsigned char tail[] = {11, 22, 33, 44};
79 static const unsigned char freed[] = {11, 22, 33, 44};
85 struct dmalloc_info *next;
86 struct dmalloc_info *prev;
89 struct dmalloc_info *dmalloc_list = 0;
91 void *xmalloc_d(size_t nbytes, const char *file, int line)
94 struct dmalloc_info *dinfo;
96 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
98 dinfo = (struct dmalloc_info *) res;
99 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
100 dinfo->file[sizeof(dinfo->file)-1] = '\0';
105 dinfo->next = dmalloc_list;
107 dinfo->next->prev = dinfo;
108 dmalloc_list = dinfo;
110 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
111 res += sizeof(*dinfo) + 4*sizeof(char);
112 memcpy(res + nbytes, tail, 4*sizeof(char));
116 void xfree_d(void *ptr, const char *file, int line)
118 struct dmalloc_info *dinfo;
122 dinfo = (struct dmalloc_info *)
123 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
124 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
126 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
129 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
131 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
135 dinfo->prev->next = dinfo->next;
137 dmalloc_list = dinfo->next;
139 dinfo->next->prev = dinfo->prev;
140 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
145 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
147 struct dmalloc_info *dinfo;
148 char *ptr = (char*) p;
155 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
159 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
161 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
165 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
166 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
168 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
173 dinfo->prev->next = dinfo->next;
175 dmalloc_list = dinfo->next;
177 dinfo->next->prev = dinfo->prev;
185 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
189 dinfo = (struct dmalloc_info *) res;
190 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
191 dinfo->file[sizeof(dinfo->file)-1] = '\0';
196 dinfo->next = dmalloc_list;
198 dmalloc_list->prev = dinfo;
199 dmalloc_list = dinfo;
201 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
202 res += sizeof(*dinfo) + 4*sizeof(char);
203 memcpy(res + nbytes, tail, 4*sizeof(char));
207 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
210 struct dmalloc_info *dinfo;
211 size_t nbytes = nmemb * size;
213 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
215 dinfo = (struct dmalloc_info *) res;
216 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
217 dinfo->file[sizeof(dinfo->file)-1] = '\0';
222 dinfo->next = dmalloc_list;
224 dinfo->next->prev = dinfo;
225 dmalloc_list = dinfo;
227 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
228 res += sizeof(*dinfo) + 4*sizeof(char);
229 memcpy(res + nbytes, tail, 4*sizeof(char));
233 void xmalloc_trav_d(const char *file, int line)
236 struct dmalloc_info *dinfo = dmalloc_list;
238 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
241 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
242 dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
246 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
250 /* TRACE_XMALLOC <= 1 */
251 #define xrealloc_d(o, x, f, l) realloc(o, x)
252 #define xmalloc_d(x, f, l) malloc(x)
253 #define xcalloc_d(x,y, f, l) calloc(x,y)
254 #define xfree_d(x, f, l) free(x)
255 #define xmalloc_trav_d(f, l)
258 void xmalloc_trav_f(const char *s, const char *file, int line)
260 xmalloc_trav_d(file, line);
263 void *xrealloc_f (void *o, size_t size, const char *file, int line)
265 void *p = xrealloc_d (o, size, file, line);
269 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
273 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
280 void *xmalloc_f (size_t size, const char *file, int line)
282 void *p = xmalloc_d (size, file, line);
285 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
289 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
295 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
297 void *p = xcalloc_d (nmemb, size, file, line);
299 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
303 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
309 char *xstrdup_f (const char *s, const char *file, int line)
311 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
313 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
319 void xfree_f(void *p, const char *file, int line)
325 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
327 xfree_d(p, file, line);