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