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