blob: 8cbc821a0184f35bbc1ec0202566a120ea5e8e12 [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
Tim Peters7d3a5112000-07-08 04:17:21 +000040#ifdef __cplusplus
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000041/* Move this down here since some C++ #include's don't like to be included
42 inside an extern "C" */
Tim Peters7d3a5112000-07-08 04:17:21 +000043extern "C" {
44#endif
45
46/* Py_ARITHMETIC_RIGHT_SHIFT
47 * C doesn't define whether a right-shift of a signed integer sign-extends
48 * or zero-fills. Here a macro to force sign extension:
49 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
50 * Return I >> J, forcing sign extension.
51 * Requirements:
52 * I is of basic signed type TYPE (char, short, int, long, or long long).
53 * TYPE is one of char, short, int, long, or long long, although long long
54 * must not be used except on platforms that support it.
55 * J is an integer >= 0 and strictly less than the number of bits in TYPE
56 * (because C doesn't define what happens for J outside that range either).
57 * Caution:
58 * I may be evaluated more than once.
59 */
60#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
61#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
62 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
63#else
64#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
65#endif
66
Tim Peters1be46842000-07-23 18:10:18 +000067/* Py_FORCE_EXPANSION
68 * "Simply" returns its argument. However, macro expansions within the
69 * argument are evaluated. This unfortunate trickery is needed to get
70 * token-pasting to work as desired in some cases.
71 */
72#define Py_FORCE_EXPANSION(X) X
73
Tim Peters8315ea52000-07-23 19:28:35 +000074/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
75 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
76 * assert-fails if any information is lost.
77 * Caution:
78 * VALUE may be evaluated more than once.
79 */
80#ifdef Py_DEBUG
81#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
82 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
83#else
84#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
85#endif
86
Thomas Wouters1e0c2f42000-07-24 16:06:23 +000087
88
89/**************************************************************************
90Prototypes that are missing from the standard include files on some systems
91(and possibly only some versions of such systems.)
92
93Please be conservative with adding new ones, document them and enclose them
94in platform-specific #ifdefs.
95**************************************************************************/
96
97#ifdef SOLARIS
98/* Unchecked */
99extern int gethostname(char *, int);
100#endif
101
102#ifdef __BEOS__
103/* Unchecked */
104/* It's in the libs, but not the headers... - [cjh] */
105int shutdown( int, int );
106#endif
107
108#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000109#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000110extern char * _getpty(int *, int, mode_t, int);
111#endif
112
113#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
114#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
115/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
116 functions, even though they are included in libutil. */
117#include <termios.h>
118extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
119extern int forkpty(int *, char *, struct termios *, struct winsize *);
120#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
121#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
122
123
124/* These are pulled from various places. It isn't obvious on what platforms
125 they are necessary, nor what the exact prototype should look like (which
126 is likely to vary between platforms!) If you find you need one of these
127 declarations, please move them to a platform-specific block and include
128 proper prototypes. */
129#if 0
130
131/* From Modules/resource.c */
132extern int getrusage();
133extern int getpagesize();
134
135/* From Python/sysmodule.c and Modules/posixmodule.c */
136extern int fclose(FILE *);
137
138/* From Modules/posixmodule.c */
139extern int fdatasync(int);
140/* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
141extern int rename(const char *, const char *);
142extern int pclose(FILE *);
143extern int lstat(const char *, struct stat *);
144extern int symlink(const char *, const char *);
145extern int fsync(int fd);
146
147#endif /* 0 */
148
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000149
150/************************
151 * WRAPPER FOR <math.h> *
152 ************************/
153
154/* On the 68K Mac, when using CFM (Code Fragment Manager),
155 <math.h> requires special treatment -- we need to surround it with
156 #pragma lib_export off / on...
157 This is because MathLib.o is a static library, and exporting its
158 symbols doesn't quite work...
159 XXX Not sure now... Seems to be something else going on as well... */
160
161#ifndef HAVE_HYPOT
162extern double hypot(double, double);
163#ifdef MWERKS_BEFORE_PRO4
164#define hypot we_dont_want_faulty_hypot_decl
165#endif
166#endif
167
168#include <math.h>
169
170#ifndef HAVE_HYPOT
171#ifdef __MWERKS__
172#undef hypot
173#endif
174#endif
175
176#if defined(USE_MSL) && defined(__MC68K__)
177/* CodeWarrior MSL 2.1.1 has weird define overrides that don't work
178** when you take the address of math functions. If I interpret the
179** ANSI C standard correctly this is illegal, but I haven't been able
180** to convince the MetroWerks folks of this...
181*/
182#undef acos
183#undef asin
184#undef atan
185#undef atan2
186#undef ceil
187#undef cos
188#undef cosh
189#undef exp
190#undef fabs
191#undef floor
192#undef fmod
193#undef log
194#undef log10
195#undef pow
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000196#undef sin
197#undef sinh
198#undef sqrt
199#undef tan
200#undef tanh
201#define acos acosd
202#define asin asind
203#define atan atand
204#define atan2 atan2d
205#define ceil ceild
206#define cos cosd
207#define cosh coshd
208#define exp expd
209#define fabs fabsd
210#define floor floord
211#define fmod fmodd
212#define log logd
213#define log10 log10d
214#define pow powd
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000215#define sin sind
216#define sinh sinhd
217#define sqrt sqrtd
218#define tan tand
219#define tanh tanhd
220#endif
221
222
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000223/************************************
224 * MALLOC COMPATIBILITY FOR pymem.h *
225 ************************************/
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000226
227#ifndef DL_IMPORT /* declarations for DLL import */
228#define DL_IMPORT(RTYPE) RTYPE
229#endif
230
231#ifndef NULL
232#define NULL ((void *)0)
233#endif
234
235#ifdef MALLOC_ZERO_RETURNS_NULL
236/* XXX Always allocate one extra byte, since some malloc's return NULL
237 XXX for malloc(0) or realloc(p, 0). */
238#define _PyMem_EXTRA 1
239#else
240#define _PyMem_EXTRA 0
241#endif
242
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000243
244/********************************************
245 * WRAPPER FOR <time.h> and/or <sys/time.h> *
246 ********************************************/
247
248#ifdef TIME_WITH_SYS_TIME
249#include <sys/time.h>
250#include <time.h>
251#else /* !TIME_WITH_SYS_TIME */
252#ifdef HAVE_SYS_TIME_H
253#include <sys/time.h>
254#else /* !HAVE_SYS_TIME_H */
255#include <time.h>
256#endif /* !HAVE_SYS_TIME_H */
257#endif /* !TIME_WITH_SYS_TIME */
258
259
260/******************************
261 * WRAPPER FOR <sys/select.h> *
262 ******************************/
263
264/* NB caller must include <sys/types.h> */
265
266#ifdef HAVE_SYS_SELECT_H
267
268#include <sys/select.h>
269
270#else /* !HAVE_SYS_SELECT_H */
271
272#ifdef USE_GUSI1
273/* If we don't have sys/select the definition may be in unistd.h */
274#include <GUSI.h>
275#endif
276
277#endif /* !HAVE_SYS_SELECT_H */
278
279/* If the fd manipulation macros aren't defined,
280 here is a set that should do the job */
281
Guido van Rossum367e46a2000-08-01 18:28:44 +0000282#if 0 /* disabled and probably obsolete */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000283
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000284#ifndef FD_SETSIZE
285#define FD_SETSIZE 256
286#endif
287
288#ifndef FD_SET
289
290typedef long fd_mask;
291
292#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
293#ifndef howmany
294#define howmany(x, y) (((x)+((y)-1))/(y))
295#endif /* howmany */
296
297typedef struct fd_set {
298 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
299} fd_set;
300
301#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
302#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
303#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
304#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
305
306#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000307
308#endif /* fd manipulation macros */
309
Tim Peters7d3a5112000-07-08 04:17:21 +0000310#ifdef __cplusplus
311}
312#endif
313
314#endif /* Py_PYPORT_H */