Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 1 | #ifndef Py_PYPORT_H |
Vladimir Marangozov | 14a4d88 | 2000-07-10 04:59:49 +0000 | [diff] [blame] | 2 | #define Py_PYPORT_H |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 3 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 4 | #include "config.h" /* include for defines */ |
| 5 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 6 | /************************************************************************** |
| 7 | Symbols and macros to supply platform-independent interfaces to basic |
Tim Peters | 1be4684 | 2000-07-23 18:10:18 +0000 | [diff] [blame] | 8 | C language & library operations whose spellings vary across platforms. |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 9 | |
| 10 | Please try to make documentation here as clear as possible: by definition, |
| 11 | the stuff here is trying to illuminate C's darkest corners. |
| 12 | |
| 13 | Config #defines referenced here: |
| 14 | |
| 15 | SIGNED_RIGHT_SHIFT_ZERO_FILLS |
| 16 | Meaning: To be defined iff i>>j does not extend the sign bit when i is a |
| 17 | signed integral type and i < 0. |
| 18 | Used in: Py_ARITHMETIC_RIGHT_SHIFT |
Tim Peters | 1be4684 | 2000-07-23 18:10:18 +0000 | [diff] [blame] | 19 | |
Tim Peters | 8315ea5 | 2000-07-23 19:28:35 +0000 | [diff] [blame] | 20 | Py_DEBUG |
| 21 | Meaning: Extra checks compiled in for debug mode. |
| 22 | Used in: Py_SAFE_DOWNCAST |
Barry Warsaw | fd847b2 | 2000-08-18 04:48:18 +0000 | [diff] [blame] | 23 | |
| 24 | HAVE_UINTPTR_T |
| 25 | Meaning: The C9X type uintptr_t is supported by the compiler |
| 26 | Used in: Py_uintptr_t |
| 27 | |
| 28 | HAVE_LONG_LONG |
| 29 | Meaning: The compiler supports the C type "long long" |
| 30 | Used in: LONG_LONG |
| 31 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 32 | **************************************************************************/ |
| 33 | |
| 34 | |
Vladimir Marangozov | e2bf7e6 | 2000-09-08 12:55:35 +0000 | [diff] [blame] | 35 | /* For backward compatibility only. Obsolete, do not use. */ |
| 36 | #define ANY void |
| 37 | #ifdef HAVE_PROTOTYPES |
| 38 | #define Py_PROTO(x) x |
| 39 | #else |
| 40 | #define Py_PROTO(x) () |
| 41 | #endif |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 42 | |
Barry Warsaw | fd847b2 | 2000-08-18 04:48:18 +0000 | [diff] [blame] | 43 | /* typedefs for some C9X-defined synonyms for integral types. |
| 44 | * |
| 45 | * The names in Python are exactly the same as the C9X names, except with a |
| 46 | * Py_ prefix. Until C9X is universally implemented, this is the only way |
| 47 | * to ensure that Python gets reliable names that don't conflict with names |
| 48 | * in non-Python code that are playing their own tricks to define the C9X |
| 49 | * names. |
| 50 | * |
| 51 | * NOTE: don't go nuts here! Python has no use for *most* of the C9X |
| 52 | * integral synonyms. Only define the ones we actually need. |
| 53 | */ |
| 54 | |
| 55 | #ifdef HAVE_LONG_LONG |
| 56 | #ifndef LONG_LONG |
| 57 | #define LONG_LONG long long |
| 58 | #endif |
| 59 | #endif /* HAVE_LONG_LONG */ |
| 60 | |
| 61 | /* uintptr_t is the C9X name for an unsigned integral type such that a |
| 62 | * legitimate void* can be cast to uintptr_t and then back to void* again |
| 63 | * without loss of information. |
| 64 | */ |
| 65 | #ifdef HAVE_UINTPTR_T |
| 66 | typedef uintptr_t Py_uintptr_t; |
| 67 | #elif SIZEOF_VOID_P <= SIZEOF_INT |
| 68 | typedef unsigned int Py_uintptr_t; |
| 69 | #elif SIZEOF_VOID_P <= SIZEOF_LONG |
| 70 | typedef unsigned long Py_uintptr_t; |
| 71 | #elif defined(HAVE_LONG_LONG) && (SIZEOF_VOID_P <= SIZEOF_LONG_LONG) |
| 72 | typedef unsigned LONG_LONG Py_uintptr_t; |
| 73 | #else |
| 74 | # error "Python needs a typedef for Py_uintptr_t in pyport.h." |
| 75 | #endif /* HAVE_UINTPTR_T */ |
| 76 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 77 | #ifdef HAVE_STDLIB_H |
| 78 | #include <stdlib.h> |
| 79 | #endif |
| 80 | |
Vladimir Marangozov | 2c57e07 | 2000-08-11 11:48:33 +0000 | [diff] [blame] | 81 | #include <math.h> /* Moved here from the math section, before extern "C" */ |
| 82 | |
| 83 | /******************************************** |
| 84 | * WRAPPER FOR <time.h> and/or <sys/time.h> * |
| 85 | ********************************************/ |
| 86 | |
| 87 | #ifdef TIME_WITH_SYS_TIME |
| 88 | #include <sys/time.h> |
| 89 | #include <time.h> |
| 90 | #else /* !TIME_WITH_SYS_TIME */ |
| 91 | #ifdef HAVE_SYS_TIME_H |
| 92 | #include <sys/time.h> |
| 93 | #else /* !HAVE_SYS_TIME_H */ |
| 94 | #include <time.h> |
| 95 | #endif /* !HAVE_SYS_TIME_H */ |
| 96 | #endif /* !TIME_WITH_SYS_TIME */ |
| 97 | |
| 98 | |
| 99 | /****************************** |
| 100 | * WRAPPER FOR <sys/select.h> * |
| 101 | ******************************/ |
| 102 | |
| 103 | /* NB caller must include <sys/types.h> */ |
| 104 | |
| 105 | #ifdef HAVE_SYS_SELECT_H |
| 106 | |
| 107 | #include <sys/select.h> |
| 108 | |
| 109 | #else /* !HAVE_SYS_SELECT_H */ |
| 110 | |
| 111 | #ifdef USE_GUSI1 |
| 112 | /* If we don't have sys/select the definition may be in unistd.h */ |
| 113 | #include <GUSI.h> |
| 114 | #endif |
| 115 | |
| 116 | #endif /* !HAVE_SYS_SELECT_H */ |
| 117 | |
| 118 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 119 | #ifdef __cplusplus |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 120 | /* Move this down here since some C++ #include's don't like to be included |
| 121 | inside an extern "C" */ |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 122 | extern "C" { |
| 123 | #endif |
| 124 | |
Vladimir Marangozov | 2c57e07 | 2000-08-11 11:48:33 +0000 | [diff] [blame] | 125 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 126 | /* Py_ARITHMETIC_RIGHT_SHIFT |
| 127 | * C doesn't define whether a right-shift of a signed integer sign-extends |
| 128 | * or zero-fills. Here a macro to force sign extension: |
| 129 | * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) |
| 130 | * Return I >> J, forcing sign extension. |
| 131 | * Requirements: |
| 132 | * I is of basic signed type TYPE (char, short, int, long, or long long). |
| 133 | * TYPE is one of char, short, int, long, or long long, although long long |
| 134 | * must not be used except on platforms that support it. |
| 135 | * J is an integer >= 0 and strictly less than the number of bits in TYPE |
| 136 | * (because C doesn't define what happens for J outside that range either). |
| 137 | * Caution: |
| 138 | * I may be evaluated more than once. |
| 139 | */ |
| 140 | #ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS |
| 141 | #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \ |
| 142 | ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J)) |
| 143 | #else |
| 144 | #define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J)) |
| 145 | #endif |
| 146 | |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 147 | /* Py_FORCE_EXPANSION(X) |
Tim Peters | 1be4684 | 2000-07-23 18:10:18 +0000 | [diff] [blame] | 148 | * "Simply" returns its argument. However, macro expansions within the |
| 149 | * argument are evaluated. This unfortunate trickery is needed to get |
| 150 | * token-pasting to work as desired in some cases. |
| 151 | */ |
| 152 | #define Py_FORCE_EXPANSION(X) X |
| 153 | |
Tim Peters | 8315ea5 | 2000-07-23 19:28:35 +0000 | [diff] [blame] | 154 | /* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) |
| 155 | * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this |
| 156 | * assert-fails if any information is lost. |
| 157 | * Caution: |
| 158 | * VALUE may be evaluated more than once. |
| 159 | */ |
| 160 | #ifdef Py_DEBUG |
| 161 | #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \ |
| 162 | (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE)) |
| 163 | #else |
| 164 | #define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE) |
| 165 | #endif |
| 166 | |
Tim Peters | 39dce29 | 2000-08-15 03:34:48 +0000 | [diff] [blame] | 167 | /* Py_IS_INFINITY(X) |
| 168 | * Return 1 if float or double arg is an infinity, else 0. |
| 169 | * Caution: |
| 170 | * X is evaluated more than once. |
| 171 | * This implementation may set the underflow flag if |X| is very small; |
| 172 | * it really can't be implemented correctly (& easily) before C99. |
| 173 | */ |
Tim Peters | 1a2eefd | 2000-09-08 15:45:34 +0000 | [diff] [blame] | 174 | #define Py_IS_INFINITY(X) ((X) && (X)*0.5 == (X)) |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 175 | |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 176 | /************************************************************************** |
| 177 | Prototypes that are missing from the standard include files on some systems |
| 178 | (and possibly only some versions of such systems.) |
| 179 | |
| 180 | Please be conservative with adding new ones, document them and enclose them |
| 181 | in platform-specific #ifdefs. |
| 182 | **************************************************************************/ |
| 183 | |
| 184 | #ifdef SOLARIS |
| 185 | /* Unchecked */ |
| 186 | extern int gethostname(char *, int); |
| 187 | #endif |
| 188 | |
| 189 | #ifdef __BEOS__ |
| 190 | /* Unchecked */ |
| 191 | /* It's in the libs, but not the headers... - [cjh] */ |
| 192 | int shutdown( int, int ); |
| 193 | #endif |
| 194 | |
| 195 | #ifdef HAVE__GETPTY |
Sjoerd Mullender | 0765fe3 | 2000-07-26 15:46:29 +0000 | [diff] [blame] | 196 | #include <sys/types.h> /* we need to import mode_t */ |
Thomas Wouters | 1e0c2f4 | 2000-07-24 16:06:23 +0000 | [diff] [blame] | 197 | extern char * _getpty(int *, int, mode_t, int); |
| 198 | #endif |
| 199 | |
| 200 | #if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) |
| 201 | #if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) |
| 202 | /* BSDI does not supply a prototype for the 'openpty' and 'forkpty' |
| 203 | functions, even though they are included in libutil. */ |
| 204 | #include <termios.h> |
| 205 | extern int openpty(int *, int *, char *, struct termios *, struct winsize *); |
| 206 | extern int forkpty(int *, char *, struct termios *, struct winsize *); |
| 207 | #endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */ |
| 208 | #endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */ |
| 209 | |
| 210 | |
| 211 | /* These are pulled from various places. It isn't obvious on what platforms |
| 212 | they are necessary, nor what the exact prototype should look like (which |
| 213 | is likely to vary between platforms!) If you find you need one of these |
| 214 | declarations, please move them to a platform-specific block and include |
| 215 | proper prototypes. */ |
| 216 | #if 0 |
| 217 | |
| 218 | /* From Modules/resource.c */ |
| 219 | extern int getrusage(); |
| 220 | extern int getpagesize(); |
| 221 | |
| 222 | /* From Python/sysmodule.c and Modules/posixmodule.c */ |
| 223 | extern int fclose(FILE *); |
| 224 | |
| 225 | /* From Modules/posixmodule.c */ |
| 226 | extern int fdatasync(int); |
| 227 | /* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */ |
| 228 | extern int rename(const char *, const char *); |
| 229 | extern int pclose(FILE *); |
| 230 | extern int lstat(const char *, struct stat *); |
| 231 | extern int symlink(const char *, const char *); |
| 232 | extern int fsync(int fd); |
| 233 | |
| 234 | #endif /* 0 */ |
| 235 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 236 | |
| 237 | /************************ |
| 238 | * WRAPPER FOR <math.h> * |
| 239 | ************************/ |
| 240 | |
| 241 | /* On the 68K Mac, when using CFM (Code Fragment Manager), |
| 242 | <math.h> requires special treatment -- we need to surround it with |
| 243 | #pragma lib_export off / on... |
| 244 | This is because MathLib.o is a static library, and exporting its |
| 245 | symbols doesn't quite work... |
| 246 | XXX Not sure now... Seems to be something else going on as well... */ |
| 247 | |
| 248 | #ifndef HAVE_HYPOT |
| 249 | extern double hypot(double, double); |
| 250 | #ifdef MWERKS_BEFORE_PRO4 |
| 251 | #define hypot we_dont_want_faulty_hypot_decl |
| 252 | #endif |
| 253 | #endif |
| 254 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 255 | #ifndef HAVE_HYPOT |
| 256 | #ifdef __MWERKS__ |
| 257 | #undef hypot |
| 258 | #endif |
| 259 | #endif |
| 260 | |
| 261 | #if defined(USE_MSL) && defined(__MC68K__) |
| 262 | /* CodeWarrior MSL 2.1.1 has weird define overrides that don't work |
| 263 | ** when you take the address of math functions. If I interpret the |
| 264 | ** ANSI C standard correctly this is illegal, but I haven't been able |
| 265 | ** to convince the MetroWerks folks of this... |
| 266 | */ |
| 267 | #undef acos |
| 268 | #undef asin |
| 269 | #undef atan |
| 270 | #undef atan2 |
| 271 | #undef ceil |
| 272 | #undef cos |
| 273 | #undef cosh |
| 274 | #undef exp |
| 275 | #undef fabs |
| 276 | #undef floor |
| 277 | #undef fmod |
| 278 | #undef log |
| 279 | #undef log10 |
| 280 | #undef pow |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 281 | #undef sin |
| 282 | #undef sinh |
| 283 | #undef sqrt |
| 284 | #undef tan |
| 285 | #undef tanh |
| 286 | #define acos acosd |
| 287 | #define asin asind |
| 288 | #define atan atand |
| 289 | #define atan2 atan2d |
| 290 | #define ceil ceild |
| 291 | #define cos cosd |
| 292 | #define cosh coshd |
| 293 | #define exp expd |
| 294 | #define fabs fabsd |
| 295 | #define floor floord |
| 296 | #define fmod fmodd |
| 297 | #define log logd |
| 298 | #define log10 log10d |
| 299 | #define pow powd |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 300 | #define sin sind |
| 301 | #define sinh sinhd |
| 302 | #define sqrt sqrtd |
| 303 | #define tan tand |
| 304 | #define tanh tanhd |
| 305 | #endif |
| 306 | |
| 307 | |
Peter Schneider-Kamp | 25f6894 | 2000-07-31 22:19:30 +0000 | [diff] [blame] | 308 | /************************************ |
| 309 | * MALLOC COMPATIBILITY FOR pymem.h * |
| 310 | ************************************/ |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 311 | |
| 312 | #ifndef DL_IMPORT /* declarations for DLL import */ |
| 313 | #define DL_IMPORT(RTYPE) RTYPE |
| 314 | #endif |
| 315 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 316 | #ifdef MALLOC_ZERO_RETURNS_NULL |
| 317 | /* XXX Always allocate one extra byte, since some malloc's return NULL |
| 318 | XXX for malloc(0) or realloc(p, 0). */ |
| 319 | #define _PyMem_EXTRA 1 |
| 320 | #else |
| 321 | #define _PyMem_EXTRA 0 |
| 322 | #endif |
| 323 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 324 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 325 | /* If the fd manipulation macros aren't defined, |
| 326 | here is a set that should do the job */ |
| 327 | |
Guido van Rossum | 367e46a | 2000-08-01 18:28:44 +0000 | [diff] [blame] | 328 | #if 0 /* disabled and probably obsolete */ |
Peter Schneider-Kamp | 1c2b178 | 2000-08-01 16:53:44 +0000 | [diff] [blame] | 329 | |
Peter Schneider-Kamp | 7e01890 | 2000-07-31 15:28:04 +0000 | [diff] [blame] | 330 | #ifndef FD_SETSIZE |
| 331 | #define FD_SETSIZE 256 |
| 332 | #endif |
| 333 | |
| 334 | #ifndef FD_SET |
| 335 | |
| 336 | typedef long fd_mask; |
| 337 | |
| 338 | #define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */ |
| 339 | #ifndef howmany |
| 340 | #define howmany(x, y) (((x)+((y)-1))/(y)) |
| 341 | #endif /* howmany */ |
| 342 | |
| 343 | typedef struct fd_set { |
| 344 | fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)]; |
| 345 | } fd_set; |
| 346 | |
| 347 | #define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS))) |
| 348 | #define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS))) |
| 349 | #define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS))) |
| 350 | #define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p))) |
| 351 | |
| 352 | #endif /* FD_SET */ |
Peter Schneider-Kamp | 1c2b178 | 2000-08-01 16:53:44 +0000 | [diff] [blame] | 353 | |
| 354 | #endif /* fd manipulation macros */ |
| 355 | |
Vladimir Marangozov | 2c57e07 | 2000-08-11 11:48:33 +0000 | [diff] [blame] | 356 | |
Tim Peters | 7d3a511 | 2000-07-08 04:17:21 +0000 | [diff] [blame] | 357 | #ifdef __cplusplus |
| 358 | } |
| 359 | #endif |
| 360 | |
| 361 | #endif /* Py_PYPORT_H */ |