2 * Copyright (C) 1994-1999, Index Data
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.7 1999-07-13 13:24:53 adam
8 * Updated memory debugging memory allocatation routines.
10 * Revision 1.6 1998/02/11 11:53:36 adam
11 * Changed code so that it compiles as C++.
13 * Revision 1.5 1997/10/31 12:20:09 adam
14 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
15 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
16 * Bug fix: missing fclose in data1_read_espec1.
18 * Revision 1.4 1996/07/03 13:21:36 adam
19 * Function xfree_f checks for NULL pointer.
21 * Revision 1.3 1995/12/05 15:08:44 adam
22 * Fixed verbose of xrealloc.
24 * Revision 1.2 1995/12/05 11:08:37 adam
25 * More verbose malloc routines.
27 * Revision 1.1 1995/11/01 11:56:53 quinn
30 * Revision 1.6 1995/10/16 14:03:11 quinn
31 * Changes to support element set names and espec1
33 * Revision 1.5 1995/09/04 12:34:06 adam
34 * Various cleanup. YAZ util used instead.
36 * Revision 1.4 1994/10/05 10:16:16 quinn
37 * Added xrealloc. Fixed bug in log.
39 * Revision 1.3 1994/09/26 16:31:37 adam
42 * Revision 1.2 1994/08/18 08:23:26 adam
43 * Res.c now use handles. xmalloc defines xstrdup.
45 * Revision 1.1 1994/08/17 13:37:54 adam
46 * xmalloc.c added to util.
57 #define TRACE_XMALLOC 0
61 static const unsigned char head[] = {44, 33, 22, 11};
62 static const unsigned char tail[] = {11, 22, 33, 44};
63 static const unsigned char freed[] = {11, 22, 33, 44};
69 struct dmalloc_info *next;
70 struct dmalloc_info *prev;
73 struct dmalloc_info *dmalloc_list = 0;
75 void *xmalloc_d(size_t nbytes, const char *file, int line)
78 struct dmalloc_info *dinfo;
80 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
82 dinfo = (struct dmalloc_info *) res;
83 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
84 dinfo->file[sizeof(dinfo->file)-1] = '\0';
89 dinfo->next = dmalloc_list;
91 dinfo->next->prev = dinfo;
94 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
95 res += sizeof(*dinfo) + 4*sizeof(char);
96 memcpy(res + nbytes, tail, 4*sizeof(char));
100 void xfree_d(void *ptr, const char *file, int line)
102 struct dmalloc_info *dinfo;
106 dinfo = (struct dmalloc_info *)
107 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
108 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
110 logf(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
113 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
115 logf(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
119 dinfo->prev->next = dinfo->next;
121 dmalloc_list = dinfo->next;
123 dinfo->next->prev = dinfo->prev;
124 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
129 void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
131 struct dmalloc_info *dinfo;
132 char *ptr = (char*) p;
139 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
143 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
145 logf(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p", file, line, ptr);
148 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
149 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
151 logf(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p", file, line, ptr);
155 dinfo->prev->next = dinfo->next;
157 dmalloc_list = dinfo->next;
159 dinfo->next->prev = dinfo->prev;
167 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
171 dinfo = (struct dmalloc_info *) res;
172 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
173 dinfo->file[sizeof(dinfo->file)-1] = '\0';
178 dinfo->next = dmalloc_list;
180 dmalloc_list->prev = dinfo;
181 dmalloc_list = dinfo;
183 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
184 res += sizeof(*dinfo) + 4*sizeof(char);
185 memcpy(res + nbytes, tail, 4*sizeof(char));
189 void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
192 struct dmalloc_info *dinfo;
193 size_t nbytes = nmemb * size;
195 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
197 dinfo = (struct dmalloc_info *) res;
198 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
199 dinfo->file[sizeof(dinfo->file)-1] = '\0';
204 dinfo->next = dmalloc_list;
206 dinfo->next->prev = dinfo;
207 dmalloc_list = dinfo;
209 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
210 res += sizeof(*dinfo) + 4*sizeof(char);
211 memcpy(res + nbytes, tail, 4*sizeof(char));
215 void xmalloc_trav_d(const char *file, int line)
218 struct dmalloc_info *dinfo = dmalloc_list;
220 logf (LOG_LOG, "malloc_trav %s:%d", file, line);
223 logf (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
224 dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
228 logf (LOG_LOG, "total bytes %ld", (long) size);
232 /* ! TRACE_XMALLOC */
233 #define xrealloc_d(o, x, f, l) realloc(o, x)
234 #define xmalloc_d(x, f, l) malloc(x)
235 #define xcalloc_d(x,y, f, l) calloc(x,y)
236 #define xfree_d(x, f, l) free(x)
237 #define xmalloc_trav_d(f, l)
240 void xmalloc_trav_f(const char *s, const char *file, int line)
242 xmalloc_trav_d(file, line);
245 void *xrealloc_f (void *o, size_t size, const char *file, int line)
247 void *p = xrealloc_d (o, size, file, line);
251 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
255 logf (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)", size);
261 void *xmalloc_f (size_t size, const char *file, int line)
263 void *p = xmalloc_d (size, file, line);
266 logf (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
270 logf (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
276 void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
278 void *p = xcalloc_d (nmemb, size, file, line);
280 logf (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
284 logf (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
290 char *xstrdup_f (const char *s, const char *file, int line)
292 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
294 logf (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
300 void xfree_f(void *p, const char *file, int line)
306 logf (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
308 xfree_d(p, file, line);