8fd0132fe68bcbe220df6943f140fd384e2cc218
[yaz-moved-to-github.git] / util / xmalloc.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: xmalloc.c,v $
7  * Revision 1.5  1997-10-31 12:20:09  adam
8  * Improved memory debugging for xmalloc/nmem.c. References to NMEM
9  * instead of ODR in n ESPEC-1 handling in source d1_espec.c.
10  * Bug fix: missing fclose in data1_read_espec1.
11  *
12  * Revision 1.4  1996/07/03 13:21:36  adam
13  * Function xfree_f checks for NULL pointer.
14  *
15  * Revision 1.3  1995/12/05  15:08:44  adam
16  * Fixed verbose of xrealloc.
17  *
18  * Revision 1.2  1995/12/05  11:08:37  adam
19  * More verbose malloc routines.
20  *
21  * Revision 1.1  1995/11/01  11:56:53  quinn
22  * Added Xmalloc.
23  *
24  * Revision 1.6  1995/10/16  14:03:11  quinn
25  * Changes to support element set names and espec1
26  *
27  * Revision 1.5  1995/09/04  12:34:06  adam
28  * Various cleanup. YAZ util used instead.
29  *
30  * Revision 1.4  1994/10/05  10:16:16  quinn
31  * Added xrealloc. Fixed bug in log.
32  *
33  * Revision 1.3  1994/09/26  16:31:37  adam
34  * Added xcalloc_f.
35  *
36  * Revision 1.2  1994/08/18  08:23:26  adam
37  * Res.c now use handles. xmalloc defines xstrdup.
38  *
39  * Revision 1.1  1994/08/17  13:37:54  adam
40  * xmalloc.c added to util.
41  *
42  */
43
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include <log.h>
49 #include <xmalloc.h>
50 #include <dmalloc.h>
51
52 void *xrealloc_f (void *o, size_t size, char *file, int line)
53 {
54     void *p = realloc (o, size);
55
56 #if TRACE_XMALLOC
57     logf (LOG_DEBUG,
58             "%s:%d: xrealloc(s=%d) %p -> %p", file, line, size, o, p);
59 #endif
60     if (!p)
61     {
62         logf (LOG_FATAL|LOG_ERRNO, "Out of memory, realloc (%d bytes)", size);
63         exit(1);
64     }
65     return p;
66 }
67
68 void *xmalloc_f (size_t size, char *file, int line)
69 {
70     void *p = malloc (size);
71
72 #if TRACE_XMALLOC
73     logf (LOG_DEBUG, "%s:%d: xmalloc(s=%d) %p", file, line, size, p);
74 #endif
75     if (!p)
76     {
77         logf (LOG_FATAL, "Out of memory - malloc (%d bytes)", size);
78         exit (1);
79     }
80     return p;
81 }
82
83 void *xcalloc_f (size_t nmemb, size_t size, char *file, int line)
84 {
85     void *p = calloc (nmemb, size);
86 #if TRACE_XMALLOC
87     logf (LOG_DEBUG, "%s:%d: xcalloc(s=%d) %p", file, line, size, p);
88 #endif
89     if (!p)
90     {
91         logf (LOG_FATAL, "Out of memory - calloc (%d, %d)", nmemb, size);
92         exit (1);
93     }
94     return p;
95 }
96
97 char *xstrdup_f (const char *s, char *file, int line)
98 {
99     char *p = xmalloc (strlen(s)+1);
100 #if TRACE_XMALLOC
101     logf (LOG_DEBUG, "%s:%d: xstrdup(s=%d) %p", file, line, strlen(s)+1, p);
102 #endif
103     strcpy (p, s);
104     return p;
105 }
106
107
108 void xfree_f(void *p, char *file, int line)
109 {
110     if (!p)
111         return ;
112 #if TRACE_XMALLOC
113     if (p)
114         logf (LOG_DEBUG, "%s:%d: xfree %p", file, line, p);
115 #endif
116     free(p);
117 }