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