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