Bump version to 0.9.1.
[python/dscho.git] / Include / pyport.h
blob5bd641c9eda34b9d0c458a63b1eeb4b164545909
1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
3 All rights reserved.
5 See the file "Misc/COPYRIGHT" for information on usage and
6 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 ******************************************************************/
9 #ifndef Py_PYPORT_H
10 #define Py_PYPORT_H
12 #include "config.h" /* include for defines */
14 /**************************************************************************
15 Symbols and macros to supply platform-independent interfaces to basic
16 C language & library operations whose spellings vary across platforms.
18 Please try to make documentation here as clear as possible: by definition,
19 the stuff here is trying to illuminate C's darkest corners.
21 Config #defines referenced here:
23 SIGNED_RIGHT_SHIFT_ZERO_FILLS
24 Meaning: To be defined iff i>>j does not extend the sign bit when i is a
25 signed integral type and i < 0.
26 Used in: Py_ARITHMETIC_RIGHT_SHIFT
28 Py_DEBUG
29 Meaning: Extra checks compiled in for debug mode.
30 Used in: Py_SAFE_DOWNCAST
31 **************************************************************************/
34 #define ANY void /* For API compatibility only. Obsolete, do not use. */
36 #ifdef HAVE_STDLIB_H
37 #include <stdlib.h>
38 #endif
40 #include <math.h> /* Moved here from the math section, before extern "C" */
42 /********************************************
43 * WRAPPER FOR <time.h> and/or <sys/time.h> *
44 ********************************************/
46 #ifdef TIME_WITH_SYS_TIME
47 #include <sys/time.h>
48 #include <time.h>
49 #else /* !TIME_WITH_SYS_TIME */
50 #ifdef HAVE_SYS_TIME_H
51 #include <sys/time.h>
52 #else /* !HAVE_SYS_TIME_H */
53 #include <time.h>
54 #endif /* !HAVE_SYS_TIME_H */
55 #endif /* !TIME_WITH_SYS_TIME */
58 /******************************
59 * WRAPPER FOR <sys/select.h> *
60 ******************************/
62 /* NB caller must include <sys/types.h> */
64 #ifdef HAVE_SYS_SELECT_H
66 #include <sys/select.h>
68 #else /* !HAVE_SYS_SELECT_H */
70 #ifdef USE_GUSI1
71 /* If we don't have sys/select the definition may be in unistd.h */
72 #include <GUSI.h>
73 #endif
75 #endif /* !HAVE_SYS_SELECT_H */
78 #ifdef __cplusplus
79 /* Move this down here since some C++ #include's don't like to be included
80 inside an extern "C" */
81 extern "C" {
82 #endif
85 /* Py_ARITHMETIC_RIGHT_SHIFT
86 * C doesn't define whether a right-shift of a signed integer sign-extends
87 * or zero-fills. Here a macro to force sign extension:
88 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
89 * Return I >> J, forcing sign extension.
90 * Requirements:
91 * I is of basic signed type TYPE (char, short, int, long, or long long).
92 * TYPE is one of char, short, int, long, or long long, although long long
93 * must not be used except on platforms that support it.
94 * J is an integer >= 0 and strictly less than the number of bits in TYPE
95 * (because C doesn't define what happens for J outside that range either).
96 * Caution:
97 * I may be evaluated more than once.
99 #ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
100 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
101 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
102 #else
103 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
104 #endif
106 /* Py_FORCE_EXPANSION(X)
107 * "Simply" returns its argument. However, macro expansions within the
108 * argument are evaluated. This unfortunate trickery is needed to get
109 * token-pasting to work as desired in some cases.
111 #define Py_FORCE_EXPANSION(X) X
113 /* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
114 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
115 * assert-fails if any information is lost.
116 * Caution:
117 * VALUE may be evaluated more than once.
119 #ifdef Py_DEBUG
120 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
121 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
122 #else
123 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
124 #endif
126 /* Py_IS_INFINITY(X)
127 * Return 1 if float or double arg is an infinity, else 0.
128 * Caution:
129 * X is evaluated more than once.
130 * This implementation may set the underflow flag if |X| is very small;
131 * it really can't be implemented correctly (& easily) before C99.
133 #define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X))
135 /**************************************************************************
136 Prototypes that are missing from the standard include files on some systems
137 (and possibly only some versions of such systems.)
139 Please be conservative with adding new ones, document them and enclose them
140 in platform-specific #ifdefs.
141 **************************************************************************/
143 #ifdef SOLARIS
144 /* Unchecked */
145 extern int gethostname(char *, int);
146 #endif
148 #ifdef __BEOS__
149 /* Unchecked */
150 /* It's in the libs, but not the headers... - [cjh] */
151 int shutdown( int, int );
152 #endif
154 #ifdef HAVE__GETPTY
155 #include <sys/types.h> /* we need to import mode_t */
156 extern char * _getpty(int *, int, mode_t, int);
157 #endif
159 #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
160 #if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
161 /* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
162 functions, even though they are included in libutil. */
163 #include <termios.h>
164 extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
165 extern int forkpty(int *, char *, struct termios *, struct winsize *);
166 #endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
167 #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
170 /* These are pulled from various places. It isn't obvious on what platforms
171 they are necessary, nor what the exact prototype should look like (which
172 is likely to vary between platforms!) If you find you need one of these
173 declarations, please move them to a platform-specific block and include
174 proper prototypes. */
175 #if 0
177 /* From Modules/resource.c */
178 extern int getrusage();
179 extern int getpagesize();
181 /* From Python/sysmodule.c and Modules/posixmodule.c */
182 extern int fclose(FILE *);
184 /* From Modules/posixmodule.c */
185 extern int fdatasync(int);
186 /* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
187 extern int rename(const char *, const char *);
188 extern int pclose(FILE *);
189 extern int lstat(const char *, struct stat *);
190 extern int symlink(const char *, const char *);
191 extern int fsync(int fd);
193 #endif /* 0 */
196 /************************
197 * WRAPPER FOR <math.h> *
198 ************************/
200 /* On the 68K Mac, when using CFM (Code Fragment Manager),
201 <math.h> requires special treatment -- we need to surround it with
202 #pragma lib_export off / on...
203 This is because MathLib.o is a static library, and exporting its
204 symbols doesn't quite work...
205 XXX Not sure now... Seems to be something else going on as well... */
207 #ifndef HAVE_HYPOT
208 extern double hypot(double, double);
209 #ifdef MWERKS_BEFORE_PRO4
210 #define hypot we_dont_want_faulty_hypot_decl
211 #endif
212 #endif
214 #ifndef HAVE_HYPOT
215 #ifdef __MWERKS__
216 #undef hypot
217 #endif
218 #endif
220 #if defined(USE_MSL) && defined(__MC68K__)
221 /* CodeWarrior MSL 2.1.1 has weird define overrides that don't work
222 ** when you take the address of math functions. If I interpret the
223 ** ANSI C standard correctly this is illegal, but I haven't been able
224 ** to convince the MetroWerks folks of this...
226 #undef acos
227 #undef asin
228 #undef atan
229 #undef atan2
230 #undef ceil
231 #undef cos
232 #undef cosh
233 #undef exp
234 #undef fabs
235 #undef floor
236 #undef fmod
237 #undef log
238 #undef log10
239 #undef pow
240 #undef sin
241 #undef sinh
242 #undef sqrt
243 #undef tan
244 #undef tanh
245 #define acos acosd
246 #define asin asind
247 #define atan atand
248 #define atan2 atan2d
249 #define ceil ceild
250 #define cos cosd
251 #define cosh coshd
252 #define exp expd
253 #define fabs fabsd
254 #define floor floord
255 #define fmod fmodd
256 #define log logd
257 #define log10 log10d
258 #define pow powd
259 #define sin sind
260 #define sinh sinhd
261 #define sqrt sqrtd
262 #define tan tand
263 #define tanh tanhd
264 #endif
267 /************************************
268 * MALLOC COMPATIBILITY FOR pymem.h *
269 ************************************/
271 #ifndef DL_IMPORT /* declarations for DLL import */
272 #define DL_IMPORT(RTYPE) RTYPE
273 #endif
275 #ifndef NULL
276 #define NULL ((void *)0)
277 #endif
279 #ifdef MALLOC_ZERO_RETURNS_NULL
280 /* XXX Always allocate one extra byte, since some malloc's return NULL
281 XXX for malloc(0) or realloc(p, 0). */
282 #define _PyMem_EXTRA 1
283 #else
284 #define _PyMem_EXTRA 0
285 #endif
288 /* If the fd manipulation macros aren't defined,
289 here is a set that should do the job */
291 #if 0 /* disabled and probably obsolete */
293 #ifndef FD_SETSIZE
294 #define FD_SETSIZE 256
295 #endif
297 #ifndef FD_SET
299 typedef long fd_mask;
301 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
302 #ifndef howmany
303 #define howmany(x, y) (((x)+((y)-1))/(y))
304 #endif /* howmany */
306 typedef struct fd_set {
307 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
308 } fd_set;
310 #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
311 #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
312 #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
313 #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
315 #endif /* FD_SET */
317 #endif /* fd manipulation macros */
320 #ifdef __cplusplus
322 #endif
324 #endif /* Py_PYPORT_H */