blob: dceb98de27f99dd6be3fc33d5bc6a285f7b23050 [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
196#undef rint
197#undef sin
198#undef sinh
199#undef sqrt
200#undef tan
201#undef tanh
202#define acos acosd
203#define asin asind
204#define atan atand
205#define atan2 atan2d
206#define ceil ceild
207#define cos cosd
208#define cosh coshd
209#define exp expd
210#define fabs fabsd
211#define floor floord
212#define fmod fmodd
213#define log logd
214#define log10 log10d
215#define pow powd
216#define rint rintd
217#define sin sind
218#define sinh sinhd
219#define sqrt sqrtd
220#define tan tand
221#define tanh tanhd
222#endif
223
224
Peter Schneider-Kamp25f68942000-07-31 22:19:30 +0000225/************************************
226 * MALLOC COMPATIBILITY FOR pymem.h *
227 ************************************/
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000228
229#ifndef DL_IMPORT /* declarations for DLL import */
230#define DL_IMPORT(RTYPE) RTYPE
231#endif
232
233#ifndef NULL
234#define NULL ((void *)0)
235#endif
236
237#ifdef MALLOC_ZERO_RETURNS_NULL
238/* XXX Always allocate one extra byte, since some malloc's return NULL
239 XXX for malloc(0) or realloc(p, 0). */
240#define _PyMem_EXTRA 1
241#else
242#define _PyMem_EXTRA 0
243#endif
244
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000245
246/********************************************
247 * WRAPPER FOR <time.h> and/or <sys/time.h> *
248 ********************************************/
249
250#ifdef TIME_WITH_SYS_TIME
251#include <sys/time.h>
252#include <time.h>
253#else /* !TIME_WITH_SYS_TIME */
254#ifdef HAVE_SYS_TIME_H
255#include <sys/time.h>
256#else /* !HAVE_SYS_TIME_H */
257#include <time.h>
258#endif /* !HAVE_SYS_TIME_H */
259#endif /* !TIME_WITH_SYS_TIME */
260
261
262/******************************
263 * WRAPPER FOR <sys/select.h> *
264 ******************************/
265
266/* NB caller must include <sys/types.h> */
267
268#ifdef HAVE_SYS_SELECT_H
269
270#include <sys/select.h>
271
272#else /* !HAVE_SYS_SELECT_H */
273
274#ifdef USE_GUSI1
275/* If we don't have sys/select the definition may be in unistd.h */
276#include <GUSI.h>
277#endif
278
279#endif /* !HAVE_SYS_SELECT_H */
280
281/* If the fd manipulation macros aren't defined,
282 here is a set that should do the job */
283
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000284#ifdef 0 /* disabled and probably obsolete */
285
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000286#ifndef FD_SETSIZE
287#define FD_SETSIZE 256
288#endif
289
290#ifndef FD_SET
291
292typedef long fd_mask;
293
294#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
295#ifndef howmany
296#define howmany(x, y) (((x)+((y)-1))/(y))
297#endif /* howmany */
298
299typedef struct fd_set {
300 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
301} fd_set;
302
303#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
304#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
305#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
306#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
307
308#endif /* FD_SET */
Peter Schneider-Kamp1c2b1782000-08-01 16:53:44 +0000309
310#endif /* fd manipulation macros */
311
Tim Peters7d3a5112000-07-08 04:17:21 +0000312#ifdef __cplusplus
313}
314#endif
315
316#endif /* Py_PYPORT_H */