00001 /* 00002 * *************************************************************************** 00003 * MALOC = < Minimal Abstraction Layer for Object-oriented C > 00004 * Copyright (C) 1994--2000 Michael Holst 00005 * 00006 * This program is free software; you can redistribute it and/or modify it 00007 * under the terms of the GNU General Public License as published by the 00008 * Free Software Foundation; either version 2 of the License, or (at your 00009 * option) any later version. 00010 * 00011 * This program is distributed in the hope that it will be useful, 00012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00014 * See the GNU General Public License for more details. 00015 * 00016 * You should have received a copy of the GNU General Public License along 00017 * with this program; if not, write to the Free Software Foundation, Inc., 00018 * 675 Mass Ave, Cambridge, MA 02139, USA. 00019 * 00020 * rcsid="$Id: maloc_base.h,v 1.23 2003/07/06 01:54:09 mholst Exp $" 00021 * *************************************************************************** 00022 */ 00023 00024 /* 00025 * *************************************************************************** 00026 * File: maloc_base.h 00027 * 00028 * Purpose: The base (or foundation) header for MALOC. 00029 * 00030 * Notes: This header sets things up correctly for using ISO/ANSI-C. 00031 * The following macros affect the behavior of the header: 00032 * 00033 * Inlining for speed: (Normal C functions if VINLINE_XXX not defined.) 00034 * ------------------- 00035 * -DVINLINE_VNM : Enables macro replacement of time-critical funcs in VNM. 00036 * 00037 * Author: Michael Holst 00038 * *************************************************************************** 00039 */ 00040 00041 #ifndef _MALOC_BASE_H_ 00042 #define _MALOC_BASE_H_ 00043 00044 /* 00045 * *************************************************************************** 00046 * Proper ISO-C header setup (with a slight "signals" tweek for setjmp) 00047 * *************************************************************************** 00048 */ 00049 00050 /* Get the fifteen ISO-C headers (CAREFUL: POSIX/BSD flags delicate...) */ 00051 00052 /* Some compilers don't set this for you; GCC does with -ansi */ 00053 /* 00054 * if !defined(__STDC__) 00055 * define __STDC__ 1 00056 * endif 00057 */ 00058 00059 /* Old Sparc compilers need this to give you prototypes */ 00060 /* 00061 * if !defined(__USE_FIXED_PROTOTYPES__) 00062 * define __USE_FIXED_PROTOTYPES__ 00063 * endif 00064 */ 00065 00066 /* Include 14 of the 15 ISO-C headers (postponing setjmp.h) */ 00067 #include <assert.h> 00068 #include <ctype.h> 00069 #include <errno.h> 00070 #include <float.h> 00071 #include <limits.h> 00072 #include <locale.h> 00073 #include <math.h> 00074 #include <signal.h> 00075 #include <stdarg.h> 00076 #include <stddef.h> 00077 #include <stdio.h> 00078 #include <stdlib.h> 00079 #include <string.h> 00080 #include <time.h> 00081 00082 /* Fix to broken <time.h> on old SunOS */ 00083 #if !defined(CLOCKS_PER_SEC) 00084 # define CLOCKS_PER_SEC 60 00085 #endif 00086 00087 /* 00088 * Problems using setjmp/longjmp for use in the MC-shell. 00089 * 00090 * Problem: Some implementations of ISO-C "setjmp/longjmp" do not return 00091 * the interrupt mask to its pre-jump state after returning. 00092 * The behavior this produces is for example you can capture a 00093 * single CTRL-C, but that is it; the mask for CTRL-C is wiped 00094 * after the first interrupt is handled. 00095 * 00096 * Solution: Use the "sigsetjmp/siglongjmp" extensions provided by most 00097 * UNIX variants. You just have to set an appropriate macro 00098 * before including <setjmp.h> to get sigsetjmp rather than 00099 * setjmp behavior. 00100 * 00101 * Notes: You can run into trouble (e.g. some versions of Linux) if 00102 * you set some of these special signal macros before some of 00103 * the other ISO-C headers. Therefore, we only set the macros 00104 * just before including <setjmp.h> as the final ISO-C header. 00105 */ 00106 #define __FAVOR_BSD /* Linux: uses sigsetjmp as the setjmp function */ 00107 #define _BSD_SIGNALS /* IRIX: uses sigsetjmp as the setjmp function */ 00108 00109 /* Now finally include the 15th header, setjmp.h */ 00110 #include <setjmp.h> 00111 00112 /* 00113 * *************************************************************************** 00114 * Setup so this include file (and subsequent) will work for both C and C++ 00115 * *************************************************************************** 00116 */ 00117 00118 #if defined(__cplusplus) 00119 # define VCXX 00120 # define extern "C" 00121 #else 00122 # define VCC 00123 # define extern 00124 #endif 00125 00126 /* 00127 * *************************************************************************** 00128 * Private and Public type modifier simulation 00129 * *************************************************************************** 00130 */ 00131 00132 #define VPRIVATE static /* Mimic C++ "Private" type modifier */ 00133 #define VPUBLIC /*empty*/ /* Mimic C++ "Public" type modifier */ 00134 00135 /* 00136 * *************************************************************************** 00137 * Slick assertion macros 00138 * *************************************************************************** 00139 */ 00140 00141 #define VWARN1(file, lineno) (fprintf(stderr,"VWARN: ASSERTION FAILURE! filename %s, line %u\n", (file), (lineno)), 0) 00142 #define VASSERT1(file, lineno) (fprintf(stderr,"VASSERT: ASSERTION FAILURE! filename %s, line %u\n", (file), (lineno)), exit(1), 0) 00143 #define VASSERT2(file, lineno) (fprintf(stderr,"VASSERT: ASSERTION FAILURE! filename %s, line %u\n", (file), (lineno)), abort(), 0) 00144 #define VASSERT3(file, lineno, ex) (fprintf(stderr,"VASSERT: ASSERTION FAILURE! filename %s, line %u, (%s)\n", (file), (lineno), (#ex)), abort(), 0) 00145 00146 #define VWARN(ex) ((void) ((ex) ? 0 : VWARN1(__FILE__, __LINE__))) 00147 #define VASSERT(ex) ((void) ((ex) ? 0 : VASSERT3(__FILE__, __LINE__, ex))) 00148 00149 /* 00150 * *************************************************************************** 00151 * A useful error handling macro 00152 * *************************************************************************** 00153 */ 00154 00155 #define VJMPERR0(x) if (!(x)) goto VERROR0 00156 #define VJMPERR1(x) if (!(x)) goto VERROR1 00157 #define VJMPERR2(x) if (!(x)) goto VERROR2 00158 #define VJMPERR3(x) if (!(x)) goto VERROR3 00159 #define VJMPERR4(x) if (!(x)) goto VERROR4 00160 #define VJMPERR5(x) if (!(x)) goto VERROR5 00161 #define VJMPERR6(x) if (!(x)) goto VERROR6 00162 #define VJMPERR7(x) if (!(x)) goto VERROR7 00163 #define VJMPERR8(x) if (!(x)) goto VERROR8 00164 #define VJMPERR9(x) if (!(x)) goto VERROR9 00165 00166 /* 00167 * *************************************************************************** 00168 * Global constants 00169 * *************************************************************************** 00170 */ 00171 00172 #define VPI 3.14159265358979323846 00173 #define VLARGE 1.0e+9 /* 1e9 just fits into 32-bit signed int */ 00174 #define VSMALL 1.0e-9 00175 #define VPRTKEY 10000 00176 00177 #define VPTRSIZE 4 00178 #define VMAX_ARGNUM 50 00179 #define VMAX_ARGLEN 1024 00180 #define VMAX_BUFSIZE 8192 00181 #define VMAX_OBJECTS 16777216 /* (1<<24) = 2^24 */ 00182 00183 #define VNULL NULL 00184 #define VINULL -1 00185 #define VTRUE 1 00186 #define VFALSE 0 00187 #define VSTDMODE 0600 00188 00189 #define VNULL_STRING "\0" 00190 #define VBLANK_STRING " " 00191 #define VNEWLINE_STRING "\n" 00192 00193 #define VNULL_SYMBOL '\0' 00194 #define VBLANK_SYMBOL ' ' 00195 #define VNEWLINE_SYMBOL '\n' 00196 #define VRDIN_SYMBOL '<' 00197 #define VRDOUT_SYMBOL '>' 00198 #define VPIPE_SYMBOL '|' 00199 #define VDELIM_SET " ><|&" 00200 00201 /* 00202 * *************************************************************************** 00203 * Mathematical macros 00204 * *************************************************************************** 00205 */ 00206 00207 #define VABS(x) ((x) >= 0 ? (x) : -(x)) 00208 #define VMIN2(x,y) ((x) <= (y) ? (x) : (y)) 00209 #define VMAX2(x,y) ((x) >= (y) ? (x) : (y)) 00210 #define VSIGN(x,y) ((y) >= 0 ? (VABS(x)) : (-VABS(x))) 00211 00212 #define VODD(x) ((x)&1) 00213 #define VEVEN(x) (!((x)&1)) 00214 #define VZERO(x) ((x)==0) 00215 #define VPOS(x) ((x)>0) 00216 #define VNEG(x) ((x)<0) 00217 #define VEVENP(x) (VEVEN(x) && VPOS(x)) 00218 #define VEVENN(x) (VEVEN(x) && VNEG(x)) 00219 00220 #define VSQRT(x) (sqrt(x)) 00221 #define VSQR(x) ((x)*(x)) 00222 #define VSIN(x) (sin(x)) 00223 #define VCOS(x) (cos(x)) 00224 #define VTAN(x) (tan(x)) 00225 #define VASIN(x) (asin(x)) 00226 #define VACOS(x) (acos(x)) 00227 #define VATAN(x) (atan(x)) 00228 #define VSINH(x) (sinh(x)) 00229 #define VCOSH(x) (cosh(x)) 00230 #define VTANH(x) (tanh(x)) 00231 #define VEXP(x) (exp(x)) 00232 #define VPOW(x,y) (pow(x,y)) 00233 #define VRINT(x) ((int)(floor((x)+0.5))) 00234 00235 #define VRAND (rand()) 00236 #define VRANDMAX (RAND_MAX) 00237 00238 /* 00239 * *************************************************************************** 00240 * Inlining via macros for speed 00241 * *************************************************************************** 00242 */ 00243 00244 #if 1 00245 # define VINLINE_MALOC 00246 #endif 00247 00248 #endif /* _MALOC_BASE_H_ */ 00249 00250