1 /***********************************************************
2 Copyright (c) 2000, BeOpen.com.
5 See the file "Misc/COPYRIGHT" for information on usage and
6 redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES.
7 ******************************************************************/
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
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. */
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
49 #else /* !TIME_WITH_SYS_TIME */
50 #ifdef HAVE_SYS_TIME_H
52 #else /* !HAVE_SYS_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 */
71 /* If we don't have sys/select the definition may be in unistd.h */
75 #endif /* !HAVE_SYS_SELECT_H */
79 /* Move this down here since some C++ #include's don't like to be included
80 inside an extern "C" */
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.
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).
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))
103 #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
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.
117 * VALUE may be evaluated more than once.
120 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
121 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
123 #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
127 * Return 1 if float or double arg is an infinity, else 0.
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 **************************************************************************/
145 extern int gethostname(char *, int);
150 /* It's in the libs, but not the headers... - [cjh] */
151 int shutdown( int, int );
155 #include <sys/types.h> /* we need to import mode_t */
156 extern char * _getpty(int *, int, mode_t
, int);
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. */
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. */
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
);
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... */
208 extern double hypot(double, double);
209 #ifdef MWERKS_BEFORE_PRO4
210 #define hypot we_dont_want_faulty_hypot_decl
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...
267 /************************************
268 * MALLOC COMPATIBILITY FOR pymem.h *
269 ************************************/
271 #ifndef DL_IMPORT /* declarations for DLL import */
272 #define DL_IMPORT(RTYPE) RTYPE
276 #define NULL ((void *)0)
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
284 #define _PyMem_EXTRA 0
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 */
294 #define FD_SETSIZE 256
299 typedef long fd_mask
;
301 #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
303 #define howmany(x, y) (((x)+((y)-1))/(y))
306 typedef struct fd_set
{
307 fd_mask fds_bits
[howmany(FD_SETSIZE
, NFDBITS
)];
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)))
317 #endif /* fd manipulation macros */
324 #endif /* Py_PYPORT_H */