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