1 /* xstd.h: Wrappers for functions in C standard library
2  Was: xmem, xfile */
3 
4 /* These call the corresponding function in the standard library, and
5    abort if those routines fail. */
6 
7 #ifndef XSTD_H
8 #define XSTD_H
9 
10 #include "types.h"
11 #include "message.h"
12 #include <assert.h>
13 #include <stdio.h>
14 #include <string.h>
15 
16 /*
17  * XMEM
18  */
19 #ifndef __cplusplus
20 #define XMALLOC(new_mem, size)			\
21 do						\
22   {						\
23     new_mem = (at_address) malloc (size);	\
24     assert(new_mem);				\
25   } while (0)
26 
27 
28 #define XCALLOC(new_mem, size)			\
29 do						\
30   {						\
31     new_mem = (at_address) calloc (size, 1);	\
32     assert(new_mem);				\
33   } while (0)
34 
35 
36 #define XREALLOC(old_ptr, size)				\
37 do							\
38   {							\
39     at_address new_mem;					\
40 							\
41     if (old_ptr == NULL)				\
42       XMALLOC (new_mem, size);				\
43     else						\
44       {							\
45         new_mem = (at_address) realloc (old_ptr, size);	\
46         assert(new_mem);				\
47       }							\
48 							\
49     old_ptr = new_mem;					\
50 } while (0)
51 
52 
53 #else
54 /* Use templates if Cplusplus... */
55 #define XMALLOC(new_mem, size)					\
56 do								\
57   {								\
58     (at_address&)(new_mem) = (at_address) malloc (size);	\
59      assert(new_mem);						\
60   } while (0)
61 
62 
63 #define XCALLOC(new_mem, sizex)					\
64 do								\
65   {								\
66     (at_address&)(new_mem) = (void *) calloc (sizex, 1);	\
67     assert(new_mem);						\
68   } while (0)
69 
70 #define XREALLOC(old_ptr, size)						  \
71 do									  \
72   {									  \
73     at_address new_mem;							  \
74 									  \
75     if (old_ptr == NULL)						  \
76       XMALLOC (new_mem, (size));					  \
77     else								  \
78       {									  \
79         (at_address&) new_mem = (at_address) realloc ((old_ptr), (size)); \
80         assert(new_mem);						  \
81       }									  \
82 									  \
83     (at_address&)old_ptr = new_mem;					  \
84   } while (0)
85 #endif
86 
87 /*
88  * XFILE
89  */
90 /* Like their stdio counterparts, but abort on error, after calling
91    perror(3) with FILENAME as its argument.  */
92 extern FILE *xfopen (at_string filename, at_string mode);
93 extern void xfclose (FILE *, at_string filename);
94 extern void xfseek (FILE *, long, int, at_string filename);
95 
96 #endif /* Not XSTD_H */
97