blob: 3d9b305d914a2f48fab0117bf7b0606e915186e8 [file] [log] [blame]
Tim Peters7d3a5112000-07-08 04:17:21 +00001/***********************************************************
2Copyright (c) 2000, BeOpen.com.
3All rights reserved.
4
5See the file "Misc/COPYRIGHT" for information on usage and
6redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7******************************************************************/
8
9#ifndef Py_PYPORT_H
Vladimir Marangozov14a4d882000-07-10 04:59:49 +000010#define Py_PYPORT_H
Tim Peters7d3a5112000-07-08 04:17:21 +000011
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +000012#include "config.h" /* include for defines */
13
Tim Peters7d3a5112000-07-08 04:17:21 +000014/**************************************************************************
15Symbols and macros to supply platform-independent interfaces to basic
Tim Peters1be46842000-07-23 18:10:18 +000016C language & library operations whose spellings vary across platforms.
Tim Peters7d3a5112000-07-08 04:17:21 +000017
18Please try to make documentation here as clear as possible: by definition,
19the stuff here is trying to illuminate C's darkest corners.
20
21Config #defines referenced here:
22
23SIGNED_RIGHT_SHIFT_ZERO_FILLS
24Meaning: To be defined iff i>>j does not extend the sign bit when i is a
25 signed integral type and i < 0.
26Used in: Py_ARITHMETIC_RIGHT_SHIFT
Tim Peters1be46842000-07-23 18:10:18 +000027
Tim Peters8315ea52000-07-23 19:28:35 +000028Py_DEBUG
29Meaning: Extra checks compiled in for debug mode.
30Used in: Py_SAFE_DOWNCAST
Tim Peters7d3a5112000-07-08 04:17:21 +000031**************************************************************************/
32
33
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000034#define ANY void /* For API compatibility only. Obsolete, do not use. */
35
36#ifdef HAVE_STDLIB_H
37#include <stdlib.h>
38#endif
39
Vladimir Marangozov2c57e072000-08-11 11:48:33 +000040#include <math.h> /* Moved here from the math section, before extern "C" */
41
42/********************************************
43 * WRAPPER FOR <time.h> and/or <sys/time.h> *
44 ********************************************/
45
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 */
56
57
58/******************************
59 * WRAPPER FOR <sys/select.h> *
60 ******************************/
61
62/* NB caller must include <sys/types.h> */
63
64#ifdef HAVE_SYS_SELECT_H
65
66#include <sys/select.h>
67
68#else /* !HAVE_SYS_SELECT_H */
69
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
74
75#endif /* !HAVE_SYS_SELECT_H */
76
77
Tim Peters7d3a5112000-07-08 04:17:21 +000078#ifdef __cplusplus
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000079/* Move this down here since some C++ #include's don't like to be included
80 inside an extern "C" */
Tim Peters7d3a5112000-07-08 04:17:21 +000081extern "C" {
82#endif
83
Vladimir Marangozov2c57e072000-08-11 11:48:33 +000084
Tim Peters7d3a5112000-07-08 04:17:21 +000085/* 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.
98 */
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
105
Tim Peters1be46842000-07-23 18:10:18 +0000106/* Py_FORCE_EXPANSION
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.
110 */
111#define Py_FORCE_EXPANSION(X) X
112
Tim Peters8315ea52000-07-23 19:28:35 +0000113/* 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.
118 */
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
125
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000126
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000127/**************************************************************************
128Prototypes that are missing from the standard include files on some systems
129(and possibly only some versions of such systems.)
130
131Please be conservative with adding new ones, document them and enclose them
132in platform-specific #ifdefs.
133**************************************************************************/
134
135#ifdef SOLARIS
136/* Unchecked */
137extern int gethostname(char *, int);
138#endif
139
140#ifdef __BEOS__
141/* Unchecked */
142/* It's in the libs, but not the headers... - [cjh] */
143int shutdown( int, int );
144#endif
145
146#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000147#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000148extern char * _getpty(int *, int, mode_t, int);
149#endif
150
151#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
152#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
153/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
154 functions, even though they are included in libutil. */
155#include <termios.h>
156extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
157extern int forkpty(int *, char *, struct termios *, struct winsize *);
158#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
159#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
160
161
162/* These are pulled from various places. It isn't obvious on what platforms
163 they are necessary, nor what the exact prototype should look like (which
164 is likely to vary between platforms!) If you find you need one of these
165 declarations, please move them to a platform-specific block and include
166 proper prototypes. */
167#if 0
168
169/* From Modules/resource.c */
170extern int getrusage();
171extern int getpagesize();
172
173/* From Python/sysmodule.c and Modules/posixmodule.c */
174extern int fclose(FILE *);
175
176/* From Modules/posixmodule.c */
177extern int fdatasync(int);
178/* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
179extern int rename(const char *, const char *);
180extern int pclose(FILE *);
181extern int lstat(const char *, struct stat *);
182extern int symlink(const char *, const char *);
183extern int fsync(int fd);
184
185#endif /* 0 */
186
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000187
188/************************
189 * WRAPPER FOR <math.h> *
190 ************************/
191
192/* On the 68K Mac, when using CFM (Code Fragment Manager),
193 <math.h> requires special treatment -- we need to surround it with
194 #pragma lib_export off / on...
195 This is because MathLib.o is a static library, and exporting its
196 symbols doesn't quite work...
197 XXX Not sure now... Seems to be something else going on as well... */
198
199#ifndef HAVE_HYPOT
200extern double hypot(double, double);
201#ifdef MWERKS_BEFORE_PRO4
202#define hypot we_dont_want_faulty_hypot_decl
203#endif
204#endif
205
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000206#ifndef HAVE_HYPOT
207#ifdef __MWERKS__
208#undef hypot
209#endif
210#endif
211
212#if defined(USE_MSL) && defined(__MC68K__)
213/* CodeWarrior MSL 2.1.1 has weird define overrides that don't work
214** when you take the address of math functions. If I interpret the
215** ANSI C standard correctly this is illegal, but I haven't been able
216** to convince the MetroWerks folks of this...
217*/
218#undef acos
219#undef asin
220#undef atan
221#undef atan2
222#undef ceil
223#undef cos
224#undef cosh
225#undef exp
226#undef fabs
227#undef floor
228#undef fmod
229#undef log
230#undef log10
231#undef pow
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000232#undef sin
233#undef sinh
234#undef sqrt
235#undef tan
236#undef tanh
237#define acos acosd
238#define asin asind
239#define atan atand
240#define atan2 atan2d
241#define ceil ceild
242#define cos cosd
243#define cosh coshd
244#define exp expd
245#define fabs fabsd
246#define floor floord
247#define fmod fmodd
248#define log logd
249#define log10 log10d
250#define pow powd
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000251#define sin sind
252#define sinh sinhd
253#define sqrt sqrtd
254#define tan tand
255#define tanh tanhd
256#endif
257
258
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000259/************************************
260 * MALLOC COMPATIBILITY FOR pymem.h *
261 ************************************/
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000262
263#ifndef DL_IMPORT /* declarations for DLL import */
264#define DL_IMPORT(RTYPE) RTYPE
265#endif
266
267#ifndef NULL
268#define NULL ((void *)0)
269#endif
270
271#ifdef MALLOC_ZERO_RETURNS_NULL
272/* XXX Always allocate one extra byte, since some malloc's return NULL
273 XXX for malloc(0) or realloc(p, 0). */
274#define _PyMem_EXTRA 1
275#else
276#define _PyMem_EXTRA 0
277#endif
278
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000279
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000280/* If the fd manipulation macros aren't defined,
281 here is a set that should do the job */
282
Guido van Rossum367e46a2000-08-01 18:28:44 +0000283#if 0 /* disabled and probably obsolete */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000284
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000285#ifndef FD_SETSIZE
286#define FD_SETSIZE 256
287#endif
288
289#ifndef FD_SET
290
291typedef long fd_mask;
292
293#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
294#ifndef howmany
295#define howmany(x, y) (((x)+((y)-1))/(y))
296#endif /* howmany */
297
298typedef struct fd_set {
299 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
300} fd_set;
301
302#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
303#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
304#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
305#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
306
307#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000308
309#endif /* fd manipulation macros */
310
Vladimir Marangozov2c57e072000-08-11 11:48:33 +0000311
Tim Peters7d3a5112000-07-08 04:17:21 +0000312#ifdef __cplusplus
313}
314#endif
315
316#endif /* Py_PYPORT_H */