blob: 48cd45b9442d358a87c7c292faded17323846ac6 [file] [log] [blame]
Tim Peters7d3a5112000-07-08 04:17:21 +00001#ifndef Py_PYPORT_H
Vladimir Marangozov14a4d882000-07-10 04:59:49 +00002#define Py_PYPORT_H
Tim Peters7d3a5112000-07-08 04:17:21 +00003
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +00004#include "config.h" /* include for defines */
5
Tim Peters7d3a5112000-07-08 04:17:21 +00006/**************************************************************************
7Symbols and macros to supply platform-independent interfaces to basic
Tim Peters1be46842000-07-23 18:10:18 +00008C language & library operations whose spellings vary across platforms.
Tim Peters7d3a5112000-07-08 04:17:21 +00009
10Please try to make documentation here as clear as possible: by definition,
11the stuff here is trying to illuminate C's darkest corners.
12
13Config #defines referenced here:
14
15SIGNED_RIGHT_SHIFT_ZERO_FILLS
16Meaning: To be defined iff i>>j does not extend the sign bit when i is a
17 signed integral type and i < 0.
18Used in: Py_ARITHMETIC_RIGHT_SHIFT
Tim Peters1be46842000-07-23 18:10:18 +000019
Tim Peters8315ea52000-07-23 19:28:35 +000020Py_DEBUG
21Meaning: Extra checks compiled in for debug mode.
22Used in: Py_SAFE_DOWNCAST
Barry Warsawfd847b22000-08-18 04:48:18 +000023
24HAVE_UINTPTR_T
25Meaning: The C9X type uintptr_t is supported by the compiler
26Used in: Py_uintptr_t
27
28HAVE_LONG_LONG
29Meaning: The compiler supports the C type "long long"
30Used in: LONG_LONG
31
Tim Peters7d3a5112000-07-08 04:17:21 +000032**************************************************************************/
33
34
Vladimir Marangozove2bf7e62000-09-08 12:55:35 +000035/* For backward compatibility only. Obsolete, do not use. */
36#define ANY void
37#ifdef HAVE_PROTOTYPES
38#define Py_PROTO(x) x
39#else
40#define Py_PROTO(x) ()
41#endif
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000042
Barry Warsawfd847b22000-08-18 04:48:18 +000043/* typedefs for some C9X-defined synonyms for integral types.
44 *
45 * The names in Python are exactly the same as the C9X names, except with a
46 * Py_ prefix. Until C9X is universally implemented, this is the only way
47 * to ensure that Python gets reliable names that don't conflict with names
48 * in non-Python code that are playing their own tricks to define the C9X
49 * names.
50 *
51 * NOTE: don't go nuts here! Python has no use for *most* of the C9X
52 * integral synonyms. Only define the ones we actually need.
53 */
54
55#ifdef HAVE_LONG_LONG
56#ifndef LONG_LONG
57#define LONG_LONG long long
58#endif
59#endif /* HAVE_LONG_LONG */
60
61/* uintptr_t is the C9X name for an unsigned integral type such that a
62 * legitimate void* can be cast to uintptr_t and then back to void* again
63 * without loss of information.
64 */
65#ifdef HAVE_UINTPTR_T
66typedef uintptr_t Py_uintptr_t;
67#elif SIZEOF_VOID_P <= SIZEOF_INT
68typedef unsigned int Py_uintptr_t;
69#elif SIZEOF_VOID_P <= SIZEOF_LONG
70typedef unsigned long Py_uintptr_t;
71#elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG)
72typedef unsigned LONG_LONG Py_uintptr_t;
73#else
74# error "Python needs a typedef for Py_uintptr_t in pyport.h."
75#endif /* HAVE_UINTPTR_T */
76
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000077#ifdef HAVE_STDLIB_H
78#include <stdlib.h>
79#endif
80
Vladimir Marangozov2c57e072000-08-11 11:48:33 +000081#include <math.h> /* Moved here from the math section, before extern "C" */
82
83/********************************************
84 * WRAPPER FOR <time.h> and/or <sys/time.h> *
85 ********************************************/
86
87#ifdef TIME_WITH_SYS_TIME
88#include <sys/time.h>
89#include <time.h>
90#else /* !TIME_WITH_SYS_TIME */
91#ifdef HAVE_SYS_TIME_H
92#include <sys/time.h>
93#else /* !HAVE_SYS_TIME_H */
94#include <time.h>
95#endif /* !HAVE_SYS_TIME_H */
96#endif /* !TIME_WITH_SYS_TIME */
97
98
99/******************************
100 * WRAPPER FOR <sys/select.h> *
101 ******************************/
102
103/* NB caller must include <sys/types.h> */
104
105#ifdef HAVE_SYS_SELECT_H
106
107#include <sys/select.h>
108
109#else /* !HAVE_SYS_SELECT_H */
110
111#ifdef USE_GUSI1
112/* If we don't have sys/select the definition may be in unistd.h */
113#include <GUSI.h>
114#endif
115
116#endif /* !HAVE_SYS_SELECT_H */
117
118
Tim Peters7d3a5112000-07-08 04:17:21 +0000119#ifdef __cplusplus
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000120/* Move this down here since some C++ #include's don't like to be included
121 inside an extern "C" */
Tim Peters7d3a5112000-07-08 04:17:21 +0000122extern "C" {
123#endif
124
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000125
Tim Peters7d3a5112000-07-08 04:17:21 +0000126/* Py_ARITHMETIC_RIGHT_SHIFT
127 * C doesn't define whether a right-shift of a signed integer sign-extends
128 * or zero-fills. Here a macro to force sign extension:
129 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
130 * Return I >> J, forcing sign extension.
131 * Requirements:
132 * I is of basic signed type TYPE (char, short, int, long, or long long).
133 * TYPE is one of char, short, int, long, or long long, although long long
134 * must not be used except on platforms that support it.
135 * J is an integer >= 0 and strictly less than the number of bits in TYPE
136 * (because C doesn't define what happens for J outside that range either).
137 * Caution:
138 * I may be evaluated more than once.
139 */
140#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
141#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
142 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
143#else
144#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
145#endif
146
Tim Peters39dce292000-08-15 03:34:48 +0000147/* Py_FORCE_EXPANSION(X)
Tim Peters1be46842000-07-23 18:10:18 +0000148 * "Simply" returns its argument. However, macro expansions within the
149 * argument are evaluated. This unfortunate trickery is needed to get
150 * token-pasting to work as desired in some cases.
151 */
152#define Py_FORCE_EXPANSION(X) X
153
Tim Peters8315ea52000-07-23 19:28:35 +0000154/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
155 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
156 * assert-fails if any information is lost.
157 * Caution:
158 * VALUE may be evaluated more than once.
159 */
160#ifdef Py_DEBUG
161#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
162 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
163#else
164#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
165#endif
166
Tim Peters39dce292000-08-15 03:34:48 +0000167/* Py_IS_INFINITY(X)
168 * Return 1 if float or double arg is an infinity, else 0.
169 * Caution:
170 * X is evaluated more than once.
171 * This implementation may set the underflow flag if |X| is very small;
172 * it really can't be implemented correctly (& easily) before C99.
173 */
Tim Peters1a2eefd2000-09-08 15:45:34 +0000174#define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X))
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000175
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000176/**************************************************************************
177Prototypes that are missing from the standard include files on some systems
178(and possibly only some versions of such systems.)
179
180Please be conservative with adding new ones, document them and enclose them
181in platform-specific #ifdefs.
182**************************************************************************/
183
184#ifdef SOLARIS
185/* Unchecked */
186extern int gethostname(char *, int);
187#endif
188
189#ifdef __BEOS__
190/* Unchecked */
191/* It's in the libs, but not the headers... - [cjh] */
192int shutdown( int, int );
193#endif
194
195#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000196#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000197extern char * _getpty(int *, int, mode_t, int);
198#endif
199
200#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
201#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
202/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
203 functions, even though they are included in libutil. */
204#include <termios.h>
205extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
206extern int forkpty(int *, char *, struct termios *, struct winsize *);
207#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
208#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
209
210
211/* These are pulled from various places. It isn't obvious on what platforms
212 they are necessary, nor what the exact prototype should look like (which
213 is likely to vary between platforms!) If you find you need one of these
214 declarations, please move them to a platform-specific block and include
215 proper prototypes. */
216#if 0
217
218/* From Modules/resource.c */
219extern int getrusage();
220extern int getpagesize();
221
222/* From Python/sysmodule.c and Modules/posixmodule.c */
223extern int fclose(FILE *);
224
225/* From Modules/posixmodule.c */
226extern int fdatasync(int);
227/* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
228extern int rename(const char *, const char *);
229extern int pclose(FILE *);
230extern int lstat(const char *, struct stat *);
231extern int symlink(const char *, const char *);
232extern int fsync(int fd);
233
234#endif /* 0 */
235
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000236
237/************************
238 * WRAPPER FOR <math.h> *
239 ************************/
240
241/* On the 68K Mac, when using CFM (Code Fragment Manager),
242 <math.h> requires special treatment -- we need to surround it with
243 #pragma lib_export off / on...
244 This is because MathLib.o is a static library, and exporting its
245 symbols doesn't quite work...
246 XXX Not sure now... Seems to be something else going on as well... */
247
248#ifndef HAVE_HYPOT
249extern double hypot(double, double);
250#ifdef MWERKS_BEFORE_PRO4
251#define hypot we_dont_want_faulty_hypot_decl
252#endif
253#endif
254
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000255#ifndef HAVE_HYPOT
256#ifdef __MWERKS__
257#undef hypot
258#endif
259#endif
260
261#if defined(USE_MSL) && defined(__MC68K__)
262/* CodeWarrior MSL 2.1.1 has weird define overrides that don't work
263** when you take the address of math functions. If I interpret the
264** ANSI C standard correctly this is illegal, but I haven't been able
265** to convince the MetroWerks folks of this...
266*/
267#undef acos
268#undef asin
269#undef atan
270#undef atan2
271#undef ceil
272#undef cos
273#undef cosh
274#undef exp
275#undef fabs
276#undef floor
277#undef fmod
278#undef log
279#undef log10
280#undef pow
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000281#undef sin
282#undef sinh
283#undef sqrt
284#undef tan
285#undef tanh
286#define acos acosd
287#define asin asind
288#define atan atand
289#define atan2 atan2d
290#define ceil ceild
291#define cos cosd
292#define cosh coshd
293#define exp expd
294#define fabs fabsd
295#define floor floord
296#define fmod fmodd
297#define log logd
298#define log10 log10d
299#define pow powd
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000300#define sin sind
301#define sinh sinhd
302#define sqrt sqrtd
303#define tan tand
304#define tanh tanhd
305#endif
306
307
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000308/************************************
309 * MALLOC COMPATIBILITY FOR pymem.h *
310 ************************************/
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000311
312#ifndef DL_IMPORT /* declarations for DLL import */
313#define DL_IMPORT(RTYPE) RTYPE
314#endif
315
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000316#ifdef MALLOC_ZERO_RETURNS_NULL
317/* XXX Always allocate one extra byte, since some malloc's return NULL
318 XXX for malloc(0) or realloc(p, 0). */
319#define _PyMem_EXTRA 1
320#else
321#define _PyMem_EXTRA 0
322#endif
323
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000324
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000325/* If the fd manipulation macros aren't defined,
326 here is a set that should do the job */
327
Guido van Rossum367e46a2000-08-01 18:28:44 +0000328#if 0 /* disabled and probably obsolete */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000329
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000330#ifndef FD_SETSIZE
331#define FD_SETSIZE 256
332#endif
333
334#ifndef FD_SET
335
336typedef long fd_mask;
337
338#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
339#ifndef howmany
340#define howmany(x, y) (((x)+((y)-1))/(y))
341#endif /* howmany */
342
343typedef struct fd_set {
344 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
345} fd_set;
346
347#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
348#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
349#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
350#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
351
352#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000353
354#endif /* fd manipulation macros */
355
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000356
Tim Peters7d3a5112000-07-08 04:17:21 +0000357#ifdef __cplusplus
358}
359#endif
360
361#endif /* Py_PYPORT_H */