2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.9 1999-09-10 08:58:32 adam
8 * Set TRACE_XMALLOC to 1.
10 * Revision 1.8 1999/08/27 09:40:32 adam
11 * Renamed logf function to yaz_log. Removed VC++ project files.
13 * Revision 1.7 1999/07/13 13:24:53 adam
14 * Updated memory debugging memory allocatation routines.
16 * Revision 1.6 1998/02/11 11:53:36 adam
17 * Changed code so that it compiles as C++.
19 * Revision 1.5 1997/10/31 12:20:09 adam
20 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
21 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
22 * Bug fix: missing fclose in data1_read_espec1.
24 * Revision 1.4 1996/07/03 13:21:36 adam
25 * Function xfree_f checks for NULL pointer.
27 * Revision 1.3 1995/12/05 15:08:44 adam
28 * Fixed verbose of xrealloc.
30 * Revision 1.2 1995/12/05 11:08:37 adam
31 * More verbose malloc routines.
33 * Revision 1.1 1995/11/01 11:56:53 quinn
36 * Revision 1.6 1995/10/16 14:03:11 quinn
37 * Changes to support element set names and espec1
39 * Revision 1.5 1995/09/04 12:34:06 adam
40 * Various cleanup. YAZ util used instead.
42 * Revision 1.4 1994/10/05 10:16:16 quinn
43 * Added xrealloc. Fixed bug in log.
45 * Revision 1.3 1994/09/26 16:31:37 adam
48 * Revision 1.2 1994/08/18 08:23:26 adam
49 * Res.c now use handles. xmalloc defines xstrdup.
51 * Revision 1.1 1994/08/17 13:37:54 adam
52 * xmalloc.c added to util.
63 #define TRACE_XMALLOC 1
67 static const unsigned char head[] = {44, 33, 22, 11};
68 static const unsigned char tail[] = {11, 22, 33, 44};
69 static const unsigned char freed[] = {11, 22, 33, 44};
75 struct dmalloc_info *next;
76 struct dmalloc_info *prev;
79 struct dmalloc_info *dmalloc_list = 0;
81 void *xmalloc_d(size_t nbytes, const char *file, int line)
84 struct dmalloc_info *dinfo;
86 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
88 dinfo = (struct dmalloc_info *) res;
89 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
90 dinfo->file[sizeof(dinfo->file)-1] = '\0';
95 dinfo->next = dmalloc_list;
97 dinfo->next->prev = dinfo;
100 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
101 res += sizeof(*dinfo) + 4*sizeof(char);
102 memcpy(res + nbytes, tail, 4*sizeof(char));
106 void xfree_d(void *ptr, const char *file, int line)
108 struct dmalloc_info *dinfo;
112 dinfo = (struct dmalloc_info *)
113 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
114 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
116 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
119 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
121 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
125 dinfo->prev->next = dinfo->next;
127 dmalloc_list = dinfo->next;
129 dinfo->next->prev = dinfo->prev;
130 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
135 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
137 struct dmalloc_info *dinfo;
138 char *ptr = (char*) p;
145 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
149 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
151 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
155 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
156 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
158 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
163 dinfo->prev->next = dinfo->next;
165 dmalloc_list = dinfo->next;
167 dinfo->next->prev = dinfo->prev;
175 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
179 dinfo = (struct dmalloc_info *) res;
180 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
181 dinfo->file[sizeof(dinfo->file)-1] = '\0';
186 dinfo->next = dmalloc_list;
188 dmalloc_list->prev = dinfo;
189 dmalloc_list = dinfo;
191 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
192 res += sizeof(*dinfo) + 4*sizeof(char);
193 memcpy(res + nbytes, tail, 4*sizeof(char));
197 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
200 struct dmalloc_info *dinfo;
201 size_t nbytes = nmemb * size;
203 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
205 dinfo = (struct dmalloc_info *) res;
206 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
207 dinfo->file[sizeof(dinfo->file)-1] = '\0';
212 dinfo->next = dmalloc_list;
214 dinfo->next->prev = dinfo;
215 dmalloc_list = dinfo;
217 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
218 res += sizeof(*dinfo) + 4*sizeof(char);
219 memcpy(res + nbytes, tail, 4*sizeof(char));
223 void xmalloc_trav_d(const char *file, int line)
226 struct dmalloc_info *dinfo = dmalloc_list;
228 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
231 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
232 dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
236 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
240 /* TRACE_XMALLOC <= 1 */
241 #define xrealloc_d(o, x, f, l) realloc(o, x)
242 #define xmalloc_d(x, f, l) malloc(x)
243 #define xcalloc_d(x,y, f, l) calloc(x,y)
244 #define xfree_d(x, f, l) free(x)
245 #define xmalloc_trav_d(f, l)
248 void xmalloc_trav_f(const char *s, const char *file, int line)
250 xmalloc_trav_d(file, line);
253 void *xrealloc_f (void *o, size_t size, const char *file, int line)
255 void *p = xrealloc_d (o, size, file, line);
259 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
263 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
270 void *xmalloc_f (size_t size, const char *file, int line)
272 void *p = xmalloc_d (size, file, line);
275 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
279 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
285 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
287 void *p = xcalloc_d (nmemb, size, file, line);
289 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
293 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
299 char *xstrdup_f (const char *s, const char *file, int line)
301 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
303 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
309 void xfree_f(void *p, const char *file, int line)
315 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
317 xfree_d(p, file, line);