2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.8 1999-08-27 09:40:32 adam
8 * Renamed logf function to yaz_log. Removed VC++ project files.
10 * Revision 1.7 1999/07/13 13:24:53 adam
11 * Updated memory debugging memory allocatation routines.
13 * Revision 1.6 1998/02/11 11:53:36 adam
14 * Changed code so that it compiles as C++.
16 * Revision 1.5 1997/10/31 12:20:09 adam
17 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
18 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
19 * Bug fix: missing fclose in data1_read_espec1.
21 * Revision 1.4 1996/07/03 13:21:36 adam
22 * Function xfree_f checks for NULL pointer.
24 * Revision 1.3 1995/12/05 15:08:44 adam
25 * Fixed verbose of xrealloc.
27 * Revision 1.2 1995/12/05 11:08:37 adam
28 * More verbose malloc routines.
30 * Revision 1.1 1995/11/01 11:56:53 quinn
33 * Revision 1.6 1995/10/16 14:03:11 quinn
34 * Changes to support element set names and espec1
36 * Revision 1.5 1995/09/04 12:34:06 adam
37 * Various cleanup. YAZ util used instead.
39 * Revision 1.4 1994/10/05 10:16:16 quinn
40 * Added xrealloc. Fixed bug in log.
42 * Revision 1.3 1994/09/26 16:31:37 adam
45 * Revision 1.2 1994/08/18 08:23:26 adam
46 * Res.c now use handles. xmalloc defines xstrdup.
48 * Revision 1.1 1994/08/17 13:37:54 adam
49 * xmalloc.c added to util.
60 #define TRACE_XMALLOC 2
64 static const unsigned char head[] = {44, 33, 22, 11};
65 static const unsigned char tail[] = {11, 22, 33, 44};
66 static const unsigned char freed[] = {11, 22, 33, 44};
72 struct dmalloc_info *next;
73 struct dmalloc_info *prev;
76 struct dmalloc_info *dmalloc_list = 0;
78 void *xmalloc_d(size_t nbytes, const char *file, int line)
81 struct dmalloc_info *dinfo;
83 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
85 dinfo = (struct dmalloc_info *) res;
86 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
87 dinfo->file[sizeof(dinfo->file)-1] = '\0';
92 dinfo->next = dmalloc_list;
94 dinfo->next->prev = dinfo;
97 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
98 res += sizeof(*dinfo) + 4*sizeof(char);
99 memcpy(res + nbytes, tail, 4*sizeof(char));
103 void xfree_d(void *ptr, const char *file, int line)
105 struct dmalloc_info *dinfo;
109 dinfo = (struct dmalloc_info *)
110 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
111 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
113 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
116 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
118 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
122 dinfo->prev->next = dinfo->next;
124 dmalloc_list = dinfo->next;
126 dinfo->next->prev = dinfo->prev;
127 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
132 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
134 struct dmalloc_info *dinfo;
135 char *ptr = (char*) p;
142 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
146 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
148 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
152 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
153 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
155 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
160 dinfo->prev->next = dinfo->next;
162 dmalloc_list = dinfo->next;
164 dinfo->next->prev = dinfo->prev;
172 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
176 dinfo = (struct dmalloc_info *) res;
177 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
178 dinfo->file[sizeof(dinfo->file)-1] = '\0';
183 dinfo->next = dmalloc_list;
185 dmalloc_list->prev = dinfo;
186 dmalloc_list = dinfo;
188 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
189 res += sizeof(*dinfo) + 4*sizeof(char);
190 memcpy(res + nbytes, tail, 4*sizeof(char));
194 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
197 struct dmalloc_info *dinfo;
198 size_t nbytes = nmemb * size;
200 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
202 dinfo = (struct dmalloc_info *) res;
203 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
204 dinfo->file[sizeof(dinfo->file)-1] = '\0';
209 dinfo->next = dmalloc_list;
211 dinfo->next->prev = dinfo;
212 dmalloc_list = dinfo;
214 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
215 res += sizeof(*dinfo) + 4*sizeof(char);
216 memcpy(res + nbytes, tail, 4*sizeof(char));
220 void xmalloc_trav_d(const char *file, int line)
223 struct dmalloc_info *dinfo = dmalloc_list;
225 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
228 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
229 dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
233 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
237 /* TRACE_XMALLOC <= 1 */
238 #define xrealloc_d(o, x, f, l) realloc(o, x)
239 #define xmalloc_d(x, f, l) malloc(x)
240 #define xcalloc_d(x,y, f, l) calloc(x,y)
241 #define xfree_d(x, f, l) free(x)
242 #define xmalloc_trav_d(f, l)
245 void xmalloc_trav_f(const char *s, const char *file, int line)
247 xmalloc_trav_d(file, line);
250 void *xrealloc_f (void *o, size_t size, const char *file, int line)
252 void *p = xrealloc_d (o, size, file, line);
256 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
260 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
267 void *xmalloc_f (size_t size, const char *file, int line)
269 void *p = xmalloc_d (size, file, line);
272 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
276 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
282 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
284 void *p = xcalloc_d (nmemb, size, file, line);
286 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
290 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
296 char *xstrdup_f (const char *s, const char *file, int line)
298 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
300 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
306 void xfree_f(void *p, const char *file, int line)
312 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
314 xfree_d(p, file, line);