Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright (c) 2000, BeOpen.com. |
| 3 | All rights reserved. |
| 4 | |
| 5 | See the file "Misc/COPYRIGHT" for information on usage and |
| 6 | redistribution of this file, and for a DISCLAIMER OF ALL WARRANTIES. |
| 7 | ******************************************************************/ |
| 8 | |
| 9 | #ifndef Py_PYPORT_H |
Vladimir Marangozov | 14a4d88 | 2000-07-10 04:59:49 +0000 | [diff] [blame] | 10 | #define Py_PYPORT_H |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 11 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 12 | #include "config.h" /* include for defines */ |
| 13 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 14 | /************************************************************************** |
| 15 | Symbols and macros to supply platform-independent interfaces to basic |
Tim Peters | 1be4684 | 2000-07-23 18:10:18 +0000 | [diff] [blame] | 16 | C language & library operations whose spellings vary across platforms. |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 17 | |
| 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. |
| 20 | |
| 21 | Config #defines referenced here: |
| 22 | |
| 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 |
Tim Peters | 1be4684 | 2000-07-23 18:10:18 +0000 | [diff] [blame] | 27 | |
Tim Peters | 8315ea5 | 2000-07-23 19:28:35 +0000 | [diff] [blame] | 28 | Py_DEBUG |
| 29 | Meaning: Extra checks compiled in for debug mode. |
| 30 | Used in: Py_SAFE_DOWNCAST |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 31 | **************************************************************************/ |
| 32 | |
| 33 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 34 | #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 Marangozov | 2c57e07 | 2000-08-11 11:48:33 +0000 | [diff] [blame] | 40 | #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 Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 78 | #ifdef __cplusplus |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 79 | /* Move this down here since some C++ #include's don't like to be included |
| 80 | inside an extern "C" */ |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 81 | extern "C" { |
| 82 | #endif |
| 83 | |
Vladimir Marangozov | 2c57e07 | 2000-08-11 11:48:33 +0000 | [diff] [blame] | 84 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 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. |
| 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 Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 106 | /* Py_FORCE_EXPANSION(X) |
Tim Peters | 1be4684 | 2000-07-23 18:10:18 +0000 | [diff] [blame] | 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 Peters | 8315ea5 | 2000-07-23 19:28:35 +0000 | [diff] [blame] | 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. |
| 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 | |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 126 | /* Py_IS_INFINITY(X) |
| 127 | * Return 1 if float or double arg is an infinity, else 0. |
| 128 | * Caution: |
| 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. |
| 132 | */ |
| 133 | #define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X)) |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 134 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 135 | /************************************************************************** |
| 136 | Prototypes that are missing from the standard include files on some systems |
| 137 | (and possibly only some versions of such systems.) |
| 138 | |
| 139 | Please be conservative with adding new ones, document them and enclose them |
| 140 | in platform-specific #ifdefs. |
| 141 | **************************************************************************/ |
| 142 | |
| 143 | #ifdef SOLARIS |
| 144 | /* Unchecked */ |
| 145 | extern int gethostname(char *, int); |
| 146 | #endif |
| 147 | |
| 148 | #ifdef __BEOS__ |
| 149 | /* Unchecked */ |
| 150 | /* It's in the libs, but not the headers... - [cjh] */ |
| 151 | int shutdown( int, int ); |
| 152 | #endif |
| 153 | |
| 154 | #ifdef HAVE__GETPTY |
Sjoerd Mullender | 0765fe3 | 2000-07-26 15:46:29 +0000 | [diff] [blame] | 155 | #include <sys/types.h> /* we need to import mode_t */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 156 | extern char * _getpty(int *, int, mode_t, int); |
| 157 | #endif |
| 158 | |
| 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. */ |
| 163 | #include <termios.h> |
| 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) */ |
| 168 | |
| 169 | |
| 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. */ |
| 175 | #if 0 |
| 176 | |
| 177 | /* From Modules/resource.c */ |
| 178 | extern int getrusage(); |
| 179 | extern int getpagesize(); |
| 180 | |
| 181 | /* From Python/sysmodule.c and Modules/posixmodule.c */ |
| 182 | extern int fclose(FILE *); |
| 183 | |
| 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); |
| 192 | |
| 193 | #endif /* 0 */ |
| 194 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 195 | |
| 196 | /************************ |
| 197 | * WRAPPER FOR <math.h> * |
| 198 | ************************/ |
| 199 | |
| 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... */ |
| 206 | |
| 207 | #ifndef HAVE_HYPOT |
| 208 | extern double hypot(double, double); |
| 209 | #ifdef MWERKS_BEFORE_PRO4 |
| 210 | #define hypot we_dont_want_faulty_hypot_decl |
| 211 | #endif |
| 212 | #endif |
| 213 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 214 | #ifndef HAVE_HYPOT |
| 215 | #ifdef __MWERKS__ |
| 216 | #undef hypot |
| 217 | #endif |
| 218 | #endif |
| 219 | |
| 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... |
| 225 | */ |
| 226 | #undef acos |
| 227 | #undef asin |
| 228 | #undef atan |
| 229 | #undef atan2 |
| 230 | #undef ceil |
| 231 | #undef cos |
| 232 | #undef cosh |
| 233 | #undef exp |
| 234 | #undef fabs |
| 235 | #undef floor |
| 236 | #undef fmod |
| 237 | #undef log |
| 238 | #undef log10 |
| 239 | #undef pow |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 240 | #undef sin |
| 241 | #undef sinh |
| 242 | #undef sqrt |
| 243 | #undef tan |
| 244 | #undef tanh |
| 245 | #define acos acosd |
| 246 | #define asin asind |
| 247 | #define atan atand |
| 248 | #define atan2 atan2d |
| 249 | #define ceil ceild |
| 250 | #define cos cosd |
| 251 | #define cosh coshd |
| 252 | #define exp expd |
| 253 | #define fabs fabsd |
| 254 | #define floor floord |
| 255 | #define fmod fmodd |
| 256 | #define log logd |
| 257 | #define log10 log10d |
| 258 | #define pow powd |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 259 | #define sin sind |
| 260 | #define sinh sinhd |
| 261 | #define sqrt sqrtd |
| 262 | #define tan tand |
| 263 | #define tanh tanhd |
| 264 | #endif |
| 265 | |
| 266 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 267 | /************************************ |
| 268 | * MALLOC COMPATIBILITY FOR pymem.h * |
| 269 | ************************************/ |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 270 | |
| 271 | #ifndef DL_IMPORT /* declarations for DLL import */ |
| 272 | #define DL_IMPORT(RTYPE) RTYPE |
| 273 | #endif |
| 274 | |
| 275 | #ifndef NULL |
| 276 | #define NULL ((void *)0) |
| 277 | #endif |
| 278 | |
| 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 |
| 283 | #else |
| 284 | #define _PyMem_EXTRA 0 |
| 285 | #endif |
| 286 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 287 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 288 | /* If the fd manipulation macros aren't defined, |
| 289 | here is a set that should do the job */ |
| 290 | |
Guido van Rossum | 367e46a | 2000-08-01 18:28:44 +0000 | [diff] [blame] | 291 | #if 0 /* disabled and probably obsolete */ |
Peter Schneider-Kamp | 1c2b178 | 2000-08-01 16:53:44 +0000 | [diff] [blame] | 292 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 293 | #ifndef FD_SETSIZE |
| 294 | #define FD_SETSIZE 256 |
| 295 | #endif |
| 296 | |
| 297 | #ifndef FD_SET |
| 298 | |
| 299 | typedef long fd_mask; |
| 300 | |
| 301 | #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */ |
| 302 | #ifndef howmany |
| 303 | #define howmany(x, y) (((x)+((y)-1))/(y)) |
| 304 | #endif /* howmany */ |
| 305 | |
| 306 | typedef struct fd_set { |
| 307 | fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)]; |
| 308 | } fd_set; |
| 309 | |
| 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))) |
| 314 | |
| 315 | #endif /* FD_SET */ |
Peter Schneider-Kamp | 1c2b178 | 2000-08-01 16:53:44 +0000 | [diff] [blame] | 316 | |
| 317 | #endif /* fd manipulation macros */ |
| 318 | |
Vladimir Marangozov | 2c57e07 | 2000-08-11 11:48:33 +0000 | [diff] [blame] | 319 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 320 | #ifdef __cplusplus |
| 321 | } |
| 322 | #endif |
| 323 | |
| 324 | #endif /* Py_PYPORT_H */ |