MALOC  0.1
maloc_base.h
1 /*
2  * ***************************************************************************
3  * MALOC = < Minimal Abstraction Layer for Object-oriented C >
4  * Copyright (C) 1994--2000 Michael Holst
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; either version 2 of the License, or (at your
9  * option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 675 Mass Ave, Cambridge, MA 02139, USA.
19  *
20  * rcsid="$Id: maloc_base.h,v 1.23 2003/07/06 01:54:09 mholst Exp $"
21  * ***************************************************************************
22  */
23 
24 /*
25  * ***************************************************************************
26  * File: maloc_base.h
27  *
28  * Purpose: The base (or foundation) header for MALOC.
29  *
30  * Notes: This header sets things up correctly for using ISO/ANSI-C.
31  * The following macros affect the behavior of the header:
32  *
33  * Inlining for speed: (Normal C functions if VINLINE_XXX not defined.)
34  * -------------------
35  * -DVINLINE_VNM : Enables macro replacement of time-critical funcs in VNM.
36  *
37  * Author: Michael Holst
38  * ***************************************************************************
39  */
40 
41 #ifndef _MALOC_BASE_H_
42 #define _MALOC_BASE_H_
43 
44 /*
45  * ***************************************************************************
46  * Proper ISO-C header setup (with a slight "signals" tweek for setjmp)
47  * ***************************************************************************
48  */
49 
50 /* Get the fifteen ISO-C headers (CAREFUL: POSIX/BSD flags delicate...) */
51 
52 /* Some compilers don't set this for you; GCC does with -ansi */
53 /*
54  * if !defined(__STDC__)
55  * define __STDC__ 1
56  * endif
57  */
58 
59 /* Old Sparc compilers need this to give you prototypes */
60 /*
61  * if !defined(__USE_FIXED_PROTOTYPES__)
62  * define __USE_FIXED_PROTOTYPES__
63  * endif
64  */
65 
66 /* Include 14 of the 15 ISO-C headers (postponing setjmp.h) */
67 #include <assert.h>
68 #include <ctype.h>
69 #include <errno.h>
70 #include <float.h>
71 #include <limits.h>
72 #include <locale.h>
73 #include <math.h>
74 #include <signal.h>
75 #include <stdarg.h>
76 #include <stddef.h>
77 #include <stdio.h>
78 #include <stdlib.h>
79 #include <string.h>
80 #include <time.h>
81 
82 /* Fix to broken <time.h> on old SunOS */
83 #if !defined(CLOCKS_PER_SEC)
84 # define CLOCKS_PER_SEC 60
85 #endif
86 
87 /*
88  * Problems using setjmp/longjmp for use in the MC-shell.
89  *
90  * Problem: Some implementations of ISO-C "setjmp/longjmp" do not return
91  * the interrupt mask to its pre-jump state after returning.
92  * The behavior this produces is for example you can capture a
93  * single CTRL-C, but that is it; the mask for CTRL-C is wiped
94  * after the first interrupt is handled.
95  *
96  * Solution: Use the "sigsetjmp/siglongjmp" extensions provided by most
97  * UNIX variants. You just have to set an appropriate macro
98  * before including <setjmp.h> to get sigsetjmp rather than
99  * setjmp behavior.
100  *
101  * Notes: You can run into trouble (e.g. some versions of Linux) if
102  * you set some of these special signal macros before some of
103  * the other ISO-C headers. Therefore, we only set the macros
104  * just before including <setjmp.h> as the final ISO-C header.
105  */
106 #define __FAVOR_BSD /* Linux: uses sigsetjmp as the setjmp function */
107 #define _BSD_SIGNALS /* IRIX: uses sigsetjmp as the setjmp function */
108 
109 /* Now finally include the 15th header, setjmp.h */
110 #include <setjmp.h>
111 
112 /*
113  * ***************************************************************************
114  * Setup so this include file (and subsequent) will work for both C and C++
115  * ***************************************************************************
116  */
117 
118 #if defined(__cplusplus)
119 # define VCXX
120 # define extern "C"
121 #else
122 # define VCC
123 # define extern
124 #endif
125 
126 /*
127  * ***************************************************************************
128  * Private and Public type modifier simulation
129  * ***************************************************************************
130  */
131 
132 #define VPRIVATE static /* Mimic C++ "Private" type modifier */
133 #define VPUBLIC /*empty*/ /* Mimic C++ "Public" type modifier */
134 
135 /*
136  * ***************************************************************************
137  * Slick assertion macros
138  * ***************************************************************************
139  */
140 
141 #define VWARN1(file, lineno) (fprintf(stderr,"VWARN: ASSERTION FAILURE! filename %s, line %u\n", (file), (lineno)), 0)
142 #define VASSERT1(file, lineno) (fprintf(stderr,"VASSERT: ASSERTION FAILURE! filename %s, line %u\n", (file), (lineno)), exit(1), 0)
143 #define VASSERT2(file, lineno) (fprintf(stderr,"VASSERT: ASSERTION FAILURE! filename %s, line %u\n", (file), (lineno)), abort(), 0)
144 #define VASSERT3(file, lineno, ex) (fprintf(stderr,"VASSERT: ASSERTION FAILURE! filename %s, line %u, (%s)\n", (file), (lineno), (#ex)), abort(), 0)
145 
146 #define VWARN(ex) ((void) ((ex) ? 0 : VWARN1(__FILE__, __LINE__)))
147 #define VASSERT(ex) ((void) ((ex) ? 0 : VASSERT3(__FILE__, __LINE__, ex)))
148 
149 /*
150  * ***************************************************************************
151  * A useful error handling macro
152  * ***************************************************************************
153  */
154 
155 #define VJMPERR0(x) if (!(x)) goto VERROR0
156 #define VJMPERR1(x) if (!(x)) goto VERROR1
157 #define VJMPERR2(x) if (!(x)) goto VERROR2
158 #define VJMPERR3(x) if (!(x)) goto VERROR3
159 #define VJMPERR4(x) if (!(x)) goto VERROR4
160 #define VJMPERR5(x) if (!(x)) goto VERROR5
161 #define VJMPERR6(x) if (!(x)) goto VERROR6
162 #define VJMPERR7(x) if (!(x)) goto VERROR7
163 #define VJMPERR8(x) if (!(x)) goto VERROR8
164 #define VJMPERR9(x) if (!(x)) goto VERROR9
165 
166 /*
167  * ***************************************************************************
168  * Global constants
169  * ***************************************************************************
170  */
171 
172 #define VPI 3.14159265358979323846
173 #define VLARGE 1.0e+9 /* 1e9 just fits into 32-bit signed int */
174 #define VSMALL 1.0e-9
175 #define VPRTKEY 10000
176 
177 #define VPTRSIZE 4
178 #define VMAX_ARGNUM 50
179 #define VMAX_ARGLEN 1024
180 #define VMAX_BUFSIZE 8192
181 #define VMAX_OBJECTS 16777216 /* (1<<24) = 2^24 */
182 
183 #define VNULL NULL
184 #define VINULL -1
185 #define VTRUE 1
186 #define VFALSE 0
187 #define VSTDMODE 0600
188 
189 #define VNULL_STRING "\0"
190 #define VBLANK_STRING " "
191 #define VNEWLINE_STRING "\n"
192 
193 #define VNULL_SYMBOL '\0'
194 #define VBLANK_SYMBOL ' '
195 #define VNEWLINE_SYMBOL '\n'
196 #define VRDIN_SYMBOL '<'
197 #define VRDOUT_SYMBOL '>'
198 #define VPIPE_SYMBOL '|'
199 #define VDELIM_SET " ><|&"
200 
201 /*
202  * ***************************************************************************
203  * Mathematical macros
204  * ***************************************************************************
205  */
206 
207 #define VABS(x) ((x) >= 0 ? (x) : -(x))
208 #define VMIN2(x,y) ((x) <= (y) ? (x) : (y))
209 #define VMAX2(x,y) ((x) >= (y) ? (x) : (y))
210 #define VSIGN(x,y) ((y) >= 0 ? (VABS(x)) : (-VABS(x)))
211 
212 #define VODD(x) ((x)&1)
213 #define VEVEN(x) (!((x)&1))
214 #define VZERO(x) ((x)==0)
215 #define VPOS(x) ((x)>0)
216 #define VNEG(x) ((x)<0)
217 #define VEVENP(x) (VEVEN(x) && VPOS(x))
218 #define VEVENN(x) (VEVEN(x) && VNEG(x))
219 
220 #define VSQRT(x) (sqrt(x))
221 #define VSQR(x) ((x)*(x))
222 #define VSIN(x) (sin(x))
223 #define VCOS(x) (cos(x))
224 #define VTAN(x) (tan(x))
225 #define VASIN(x) (asin(x))
226 #define VACOS(x) (acos(x))
227 #define VATAN(x) (atan(x))
228 #define VSINH(x) (sinh(x))
229 #define VCOSH(x) (cosh(x))
230 #define VTANH(x) (tanh(x))
231 #define VEXP(x) (exp(x))
232 #define VPOW(x,y) (pow(x,y))
233 #define VRINT(x) ((int)(floor((x)+0.5)))
234 
235 #define VRAND (rand())
236 #define VRANDMAX (RAND_MAX)
237 
238 /*
239  * ***************************************************************************
240  * Inlining via macros for speed
241  * ***************************************************************************
242  */
243 
244 #if 1
245 # define VINLINE_MALOC
246 #endif
247 
248 #endif /* _MALOC_BASE_H_ */
249