source: trunk/gsdl/packages/yaz/util/xmalloc.c@ 1343

Last change on this file since 1343 was 1343, checked in by johnmcp, 24 years ago

Added the YAZ toolkit source to the packages directory (for z39.50 stuff)

  • Property svn:keywords set to Author Date Id Revision
File size: 8.2 KB
Line 
1/*
2 * Copyright (C) 1994-2000, Index Data
3 * All rights reserved.
4 * Sebastian Hammer, Adam Dickmeiss
5 *
6 * $Log$
7 * Revision 1.1 2000/08/03 03:12:11 johnmcp
8 * Added the YAZ toolkit source to the packages directory (for z39.50 stuff)
9 *
10 * Revision 1.11 2000/02/29 13:44:55 adam
11 * Check for config.h (currently not generated).
12 *
13 * Revision 1.10 1999/11/30 13:47:12 adam
14 * Improved installation. Moved header files to include/yaz.
15 *
16 * Revision 1.9 1999/09/10 08:58:32 adam
17 * Set TRACE_XMALLOC to 1.
18 *
19 * Revision 1.8 1999/08/27 09:40:32 adam
20 * Renamed logf function to yaz_log. Removed VC++ project files.
21 *
22 * Revision 1.7 1999/07/13 13:24:53 adam
23 * Updated memory debugging memory allocatation routines.
24 *
25 * Revision 1.6 1998/02/11 11:53:36 adam
26 * Changed code so that it compiles as C++.
27 *
28 * Revision 1.5 1997/10/31 12:20:09 adam
29 * Improved memory debugging for xmalloc/nmem.c. References to NMEM
30 * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
31 * Bug fix: missing fclose in data1_read_espec1.
32 *
33 * Revision 1.4 1996/07/03 13:21:36 adam
34 * Function xfree_f checks for NULL pointer.
35 *
36 * Revision 1.3 1995/12/05 15:08:44 adam
37 * Fixed verbose of xrealloc.
38 *
39 * Revision 1.2 1995/12/05 11:08:37 adam
40 * More verbose malloc routines.
41 *
42 * Revision 1.1 1995/11/01 11:56:53 quinn
43 * Added Xmalloc.
44 *
45 * Revision 1.6 1995/10/16 14:03:11 quinn
46 * Changes to support element set names and espec1
47 *
48 * Revision 1.5 1995/09/04 12:34:06 adam
49 * Various cleanup. YAZ util used instead.
50 *
51 * Revision 1.4 1994/10/05 10:16:16 quinn
52 * Added xrealloc. Fixed bug in log.
53 *
54 * Revision 1.3 1994/09/26 16:31:37 adam
55 * Added xcalloc_f.
56 *
57 * Revision 1.2 1994/08/18 08:23:26 adam
58 * Res.c now use handles. xmalloc defines xstrdup.
59 *
60 * Revision 1.1 1994/08/17 13:37:54 adam
61 * xmalloc.c added to util.
62 *
63 */
64
65#if HAVE_CONFIG_H
66#include <config.h>
67#endif
68
69#include <stdio.h>
70#include <stdlib.h>
71#include <string.h>
72
73#include <yaz/log.h>
74#include <yaz/xmalloc.h>
75
76#define TRACE_XMALLOC 1
77
78#if TRACE_XMALLOC > 1
79
80static const unsigned char head[] = {44, 33, 22, 11};
81static const unsigned char tail[] = {11, 22, 33, 44};
82static const unsigned char freed[] = {11, 22, 33, 44};
83
84struct dmalloc_info {
85 int len;
86 char file[16];
87 int line;
88 struct dmalloc_info *next;
89 struct dmalloc_info *prev;
90};
91
92struct dmalloc_info *dmalloc_list = 0;
93
94void *xmalloc_d(size_t nbytes, const char *file, int line)
95{
96 char *res;
97 struct dmalloc_info *dinfo;
98
99 if (!(res = (char*) malloc(nbytes + sizeof(*dinfo)+8*sizeof(char))))
100 return 0;
101 dinfo = (struct dmalloc_info *) res;
102 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
103 dinfo->file[sizeof(dinfo->file)-1] = '\0';
104 dinfo->line = line;
105 dinfo->len = nbytes;
106
107 dinfo->prev = 0;
108 dinfo->next = dmalloc_list;
109 if (dinfo->next)
110 dinfo->next->prev = dinfo;
111 dmalloc_list = dinfo;
112
113 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
114 res += sizeof(*dinfo) + 4*sizeof(char);
115 memcpy(res + nbytes, tail, 4*sizeof(char));
116 return res;
117}
118
119void xfree_d(void *ptr, const char *file, int line)
120{
121 struct dmalloc_info *dinfo;
122
123 if (!ptr)
124 return;
125 dinfo = (struct dmalloc_info *)
126 ((char*)ptr - 4*sizeof(char) - sizeof(*dinfo));
127 if (memcmp(head, (char*) ptr - 4*sizeof(char), 4*sizeof(char)))
128 {
129 yaz_log(LOG_FATAL, "xfree_d bad head, %s:%d, %p", file, line, ptr);
130 abort();
131 }
132 if (memcmp((char*) ptr + dinfo->len, tail, 4*sizeof(char)))
133 {
134 yaz_log(LOG_FATAL, "xfree_d bad tail, %s:%d, %p", file, line, ptr);
135 abort();
136 }
137 if (dinfo->prev)
138 dinfo->prev->next = dinfo->next;
139 else
140 dmalloc_list = dinfo->next;
141 if (dinfo->next)
142 dinfo->next->prev = dinfo->prev;
143 memcpy ((char*) ptr - 4*sizeof(char), freed, 4*sizeof(char));
144 free(dinfo);
145 return;
146}
147
148void *xrealloc_d(void *p, size_t nbytes, const char *file, int line)
149{
150 struct dmalloc_info *dinfo;
151 char *ptr = (char*) p;
152 char *res;
153
154 if (!ptr)
155 {
156 if (!nbytes)
157 return 0;
158 res = (char *) malloc(nbytes + sizeof(*dinfo) + 8*sizeof(char));
159 }
160 else
161 {
162 if (memcmp(head, ptr - 4*sizeof(char), 4*sizeof(char)))
163 {
164 yaz_log(LOG_FATAL, "xrealloc_d bad head, %s:%d, %p",
165 file, line, ptr);
166 abort();
167 }
168 dinfo = (struct dmalloc_info *) (ptr-4*sizeof(char) - sizeof(*dinfo));
169 if (memcmp(ptr + dinfo->len, tail, 4*sizeof(char)))
170 {
171 yaz_log(LOG_FATAL, "xrealloc_d bad tail, %s:%d, %p",
172 file, line, ptr);
173 abort();
174 }
175 if (dinfo->prev)
176 dinfo->prev->next = dinfo->next;
177 else
178 dmalloc_list = dinfo->next;
179 if (dinfo->next)
180 dinfo->next->prev = dinfo->prev;
181
182 if (!nbytes)
183 {
184 free (dinfo);
185 return 0;
186 }
187 res = (char *)
188 realloc(dinfo, nbytes + sizeof(*dinfo) + 8*sizeof(char));
189 }
190 if (!res)
191 return 0;
192 dinfo = (struct dmalloc_info *) res;
193 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
194 dinfo->file[sizeof(dinfo->file)-1] = '\0';
195 dinfo->line = line;
196 dinfo->len = nbytes;
197
198 dinfo->prev = 0;
199 dinfo->next = dmalloc_list;
200 if (dmalloc_list)
201 dmalloc_list->prev = dinfo;
202 dmalloc_list = dinfo;
203
204 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
205 res += sizeof(*dinfo) + 4*sizeof(char);
206 memcpy(res + nbytes, tail, 4*sizeof(char));
207 return res;
208}
209
210void *xcalloc_d(size_t nmemb, size_t size, const char *file, int line)
211{
212 char *res;
213 struct dmalloc_info *dinfo;
214 size_t nbytes = nmemb * size;
215
216 if (!(res = (char*) calloc(1, nbytes+sizeof(*dinfo)+8*sizeof(char))))
217 return 0;
218 dinfo = (struct dmalloc_info *) res;
219 strncpy (dinfo->file, file, sizeof(dinfo->file)-1);
220 dinfo->file[sizeof(dinfo->file)-1] = '\0';
221 dinfo->line = line;
222 dinfo->len = nbytes;
223
224 dinfo->prev = 0;
225 dinfo->next = dmalloc_list;
226 if (dinfo->next)
227 dinfo->next->prev = dinfo;
228 dmalloc_list = dinfo;
229
230 memcpy(res + sizeof(*dinfo), head, 4*sizeof(char));
231 res += sizeof(*dinfo) + 4*sizeof(char);
232 memcpy(res + nbytes, tail, 4*sizeof(char));
233 return res;
234}
235
236void xmalloc_trav_d(const char *file, int line)
237{
238 size_t size = 0;
239 struct dmalloc_info *dinfo = dmalloc_list;
240
241 yaz_log (LOG_LOG, "malloc_trav %s:%d", file, line);
242 while (dinfo)
243 {
244 yaz_log (LOG_LOG, " %20s:%d p=%p size=%d", dinfo->file, dinfo->line,
245 dinfo+sizeof(*dinfo)+4*sizeof(char), dinfo->len);
246 size += dinfo->len;
247 dinfo = dinfo->next;
248 }
249 yaz_log (LOG_LOG, "total bytes %ld", (long) size);
250}
251
252#else
253/* TRACE_XMALLOC <= 1 */
254#define xrealloc_d(o, x, f, l) realloc(o, x)
255#define xmalloc_d(x, f, l) malloc(x)
256#define xcalloc_d(x,y, f, l) calloc(x,y)
257#define xfree_d(x, f, l) free(x)
258#define xmalloc_trav_d(f, l)
259#endif
260
261void xmalloc_trav_f(const char *s, const char *file, int line)
262{
263 xmalloc_trav_d(file, line);
264}
265
266void *xrealloc_f (void *o, size_t size, const char *file, int line)
267{
268 void *p = xrealloc_d (o, size, file, line);
269
270#if TRACE_XMALLOC
271 yaz_log (LOG_DEBUG,
272 "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
273#endif
274 if (!p)
275 {
276 yaz_log (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)",
277 size);
278 exit(1);
279 }
280 return p;
281}
282
283void *xmalloc_f (size_t size, const char *file, int line)
284{
285 void *p = xmalloc_d (size, file, line);
286
287#if TRACE_XMALLOC
288 yaz_log (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
289#endif
290 if (!p)
291 {
292 yaz_log (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
293 exit (1);
294 }
295 return p;
296}
297
298void *xcalloc_f (size_t nmemb, size_t size, const char *file, int line)
299{
300 void *p = xcalloc_d (nmemb, size, file, line);
301#if TRACE_XMALLOC
302 yaz_log (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
303#endif
304 if (!p)
305 {
306 yaz_log (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
307 exit (1);
308 }
309 return p;
310}
311
312char *xstrdup_f (const char *s, const char *file, int line)
313{
314 char *p = (char *)xmalloc_d (strlen(s)+1, file, line);
315#if TRACE_XMALLOC
316 yaz_log (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
317#endif
318 strcpy (p, s);
319 return p;
320}
321
322void xfree_f(void *p, const char *file, int line)
323{
324 if (!p)
325 return ;
326#if TRACE_XMALLOC
327 if (p)
328 yaz_log (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
329#endif
330 xfree_d(p, file, line);
331}
Note: See TracBrowser for help on using the repository browser.