blob: 6026a2bcf238507593afe4eb58dc20f1cd9a8e50 [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
12/**************************************************************************
13Symbols and macros to supply platform-independent interfaces to basic
Tim Peters1be46842000-07-23 18:10:18 +000014C language & library operations whose spellings vary across platforms.
Tim Peters7d3a5112000-07-08 04:17:21 +000015
16Please try to make documentation here as clear as possible: by definition,
17the stuff here is trying to illuminate C's darkest corners.
18
19Config #defines referenced here:
20
21SIGNED_RIGHT_SHIFT_ZERO_FILLS
22Meaning: To be defined iff i>>j does not extend the sign bit when i is a
23 signed integral type and i < 0.
24Used in: Py_ARITHMETIC_RIGHT_SHIFT
Tim Peters1be46842000-07-23 18:10:18 +000025
Tim Peters8315ea52000-07-23 19:28:35 +000026Py_DEBUG
27Meaning: Extra checks compiled in for debug mode.
28Used in: Py_SAFE_DOWNCAST
Tim Peters7d3a5112000-07-08 04:17:21 +000029**************************************************************************/
30
31
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000032#define ANY void /* For API compatibility only. Obsolete, do not use. */
33
34#ifdef HAVE_STDLIB_H
35#include <stdlib.h>
36#endif
37
Tim Peters7d3a5112000-07-08 04:17:21 +000038#ifdef __cplusplus
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +000039/* Move this down here since some C++ #include's don't like to be included
40 inside an extern "C" */
Tim Peters7d3a5112000-07-08 04:17:21 +000041extern "C" {
42#endif
43
44/* Py_ARITHMETIC_RIGHT_SHIFT
45 * C doesn't define whether a right-shift of a signed integer sign-extends
46 * or zero-fills. Here a macro to force sign extension:
47 * Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J)
48 * Return I >> J, forcing sign extension.
49 * Requirements:
50 * I is of basic signed type TYPE (char, short, int, long, or long long).
51 * TYPE is one of char, short, int, long, or long long, although long long
52 * must not be used except on platforms that support it.
53 * J is an integer >= 0 and strictly less than the number of bits in TYPE
54 * (because C doesn't define what happens for J outside that range either).
55 * Caution:
56 * I may be evaluated more than once.
57 */
58#ifdef SIGNED_RIGHT_SHIFT_ZERO_FILLS
59#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) \
60 ((I) < 0 ? ~((~(unsigned TYPE)(I)) >> (J)) : (I) >> (J))
61#else
62#define Py_ARITHMETIC_RIGHT_SHIFT(TYPE, I, J) ((I) >> (J))
63#endif
64
Tim Peters1be46842000-07-23 18:10:18 +000065/* Py_FORCE_EXPANSION
66 * "Simply" returns its argument. However, macro expansions within the
67 * argument are evaluated. This unfortunate trickery is needed to get
68 * token-pasting to work as desired in some cases.
69 */
70#define Py_FORCE_EXPANSION(X) X
71
Tim Peters8315ea52000-07-23 19:28:35 +000072/* Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW)
73 * Cast VALUE to type NARROW from type WIDE. In Py_DEBUG mode, this
74 * assert-fails if any information is lost.
75 * Caution:
76 * VALUE may be evaluated more than once.
77 */
78#ifdef Py_DEBUG
79#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) \
80 (assert((WIDE)(NARROW)(VALUE) == (VALUE)), (NARROW)(VALUE))
81#else
82#define Py_SAFE_DOWNCAST(VALUE, WIDE, NARROW) (NARROW)(VALUE)
83#endif
84
Thomas Wouters1e0c2f42000-07-24 16:06:23 +000085
86
87/**************************************************************************
88Prototypes that are missing from the standard include files on some systems
89(and possibly only some versions of such systems.)
90
91Please be conservative with adding new ones, document them and enclose them
92in platform-specific #ifdefs.
93**************************************************************************/
94
95#ifdef SOLARIS
96/* Unchecked */
97extern int gethostname(char *, int);
98#endif
99
100#ifdef __BEOS__
101/* Unchecked */
102/* It's in the libs, but not the headers... - [cjh] */
103int shutdown( int, int );
104#endif
105
106#ifdef HAVE__GETPTY
Sjoerd Mullender0765fe32000-07-26 15:46:29 +0000107#include <sys/types.h> /* we need to import mode_t */
Thomas Wouters1e0c2f42000-07-24 16:06:23 +0000108extern char * _getpty(int *, int, mode_t, int);
109#endif
110
111#if defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY)
112#if !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H)
113/* BSDI does not supply a prototype for the 'openpty' and 'forkpty'
114 functions, even though they are included in libutil. */
115#include <termios.h>
116extern int openpty(int *, int *, char *, struct termios *, struct winsize *);
117extern int forkpty(int *, char *, struct termios *, struct winsize *);
118#endif /* !defined(HAVE_PTY_H) && !defined(HAVE_LIBUTIL_H) */
119#endif /* defined(HAVE_OPENPTY) || defined(HAVE_FORKPTY) */
120
121
122/* These are pulled from various places. It isn't obvious on what platforms
123 they are necessary, nor what the exact prototype should look like (which
124 is likely to vary between platforms!) If you find you need one of these
125 declarations, please move them to a platform-specific block and include
126 proper prototypes. */
127#if 0
128
129/* From Modules/resource.c */
130extern int getrusage();
131extern int getpagesize();
132
133/* From Python/sysmodule.c and Modules/posixmodule.c */
134extern int fclose(FILE *);
135
136/* From Modules/posixmodule.c */
137extern int fdatasync(int);
138/* XXX These are supposedly for SunOS4.1.3 but "shouldn't hurt elsewhere" */
139extern int rename(const char *, const char *);
140extern int pclose(FILE *);
141extern int lstat(const char *, struct stat *);
142extern int symlink(const char *, const char *);
143extern int fsync(int fd);
144
145#endif /* 0 */
146
Peter Schneider-Kamp7e018902000-07-31 15:28:04 +0000147
148/************************
149 * WRAPPER FOR <math.h> *
150 ************************/
151
152/* On the 68K Mac, when using CFM (Code Fragment Manager),
153 <math.h> requires special treatment -- we need to surround it with
154 #pragma lib_export off / on...
155 This is because MathLib.o is a static library, and exporting its
156 symbols doesn't quite work...
157 XXX Not sure now... Seems to be something else going on as well... */
158
159#ifndef HAVE_HYPOT
160extern double hypot(double, double);
161#ifdef MWERKS_BEFORE_PRO4
162#define hypot we_dont_want_faulty_hypot_decl
163#endif
164#endif
165
166#include <math.h>
167
168#ifndef HAVE_HYPOT
169#ifdef __MWERKS__
170#undef hypot
171#endif
172#endif
173
174#if defined(USE_MSL) && defined(__MC68K__)
175/* CodeWarrior MSL 2.1.1 has weird define overrides that don't work
176** when you take the address of math functions. If I interpret the
177** ANSI C standard correctly this is illegal, but I haven't been able
178** to convince the MetroWerks folks of this...
179*/
180#undef acos
181#undef asin
182#undef atan
183#undef atan2
184#undef ceil
185#undef cos
186#undef cosh
187#undef exp
188#undef fabs
189#undef floor
190#undef fmod
191#undef log
192#undef log10
193#undef pow
194#undef rint
195#undef sin
196#undef sinh
197#undef sqrt
198#undef tan
199#undef tanh
200#define acos acosd
201#define asin asind
202#define atan atand
203#define atan2 atan2d
204#define ceil ceild
205#define cos cosd
206#define cosh coshd
207#define exp expd
208#define fabs fabsd
209#define floor floord
210#define fmod fmodd
211#define log logd
212#define log10 log10d
213#define pow powd
214#define rint rintd
215#define sin sind
216#define sinh sinhd
217#define sqrt sqrtd
218#define tan tand
219#define tanh tanhd
220#endif
221
222
223/***********************************
224 * WRAPPER FOR malloc/realloc/free *
225 ***********************************/
226
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
243/*
244 * Core memory allocator
245 * =====================
246 */
247
248/* To make sure the interpreter is user-malloc friendly, all memory
249 APIs are implemented on top of this one.
250
251 The PyCore_* macros can be defined to make the interpreter use a
252 custom allocator. Note that they are for internal use only. Both
253 the core and extension modules should use the PyMem_* API.
254
255 See the comment block at the end of this file for two scenarios
256 showing how to use this to use a different allocator. */
257
258#ifndef PyCore_MALLOC_FUNC
259#undef PyCore_REALLOC_FUNC
260#undef PyCore_FREE_FUNC
261#define PyCore_MALLOC_FUNC malloc
262#define PyCore_REALLOC_FUNC realloc
263#define PyCore_FREE_FUNC free
264#endif
265
266#ifndef PyCore_MALLOC_PROTO
267#undef PyCore_REALLOC_PROTO
268#undef PyCore_FREE_PROTO
269#define PyCore_MALLOC_PROTO (size_t)
270#define PyCore_REALLOC_PROTO (void *, size_t)
271#define PyCore_FREE_PROTO (void *)
272#endif
273
274#ifdef NEED_TO_DECLARE_MALLOC_AND_FRIEND
275extern void *PyCore_MALLOC_FUNC PyCore_MALLOC_PROTO;
276extern void *PyCore_REALLOC_FUNC PyCore_REALLOC_PROTO;
277extern void PyCore_FREE_FUNC PyCore_FREE_PROTO;
278#endif
279
280#ifndef PyCore_MALLOC
281#undef PyCore_REALLOC
282#undef PyCore_FREE
283#define PyCore_MALLOC(n) PyCore_MALLOC_FUNC(n)
284#define PyCore_REALLOC(p, n) PyCore_REALLOC_FUNC((p), (n))
285#define PyCore_FREE(p) PyCore_FREE_FUNC(p)
286#endif
287
288/* BEWARE:
289
290 Each interface exports both functions and macros. Extension modules
291 should normally use the functions for ensuring binary compatibility
292 of the user's code across Python versions. Subsequently, if the
293 Python runtime switches to its own malloc (different from standard
294 malloc), no recompilation is required for the extensions.
295
296 The macro versions trade compatibility for speed. They can be used
297 whenever there is a performance problem, but their use implies
298 recompilation of the code for each new Python release. The Python
299 core uses the macros because it *is* compiled on every upgrade.
300 This might not be the case with 3rd party extensions in a custom
301 setup (for example, a customer does not always have access to the
302 source of 3rd party deliverables). You have been warned! */
303
304/*
305 * Raw memory interface
306 * ====================
307 */
308
309/* Functions */
310
311/* Function wrappers around PyCore_MALLOC and friends; useful if you
312 need to be sure that you are using the same memory allocator as
313 Python. Note that the wrappers make sure that allocating 0 bytes
314 returns a non-NULL pointer, even if the underlying malloc
315 doesn't. Returned pointers must be checked for NULL explicitly.
316 No action is performed on failure. */
317extern DL_IMPORT(void *) PyMem_Malloc(size_t);
318extern DL_IMPORT(void *) PyMem_Realloc(void *, size_t);
319extern DL_IMPORT(void) PyMem_Free(void *);
320
321/* Starting from Python 1.6, the wrappers Py_{Malloc,Realloc,Free} are
322 no longer supported. They used to call PyErr_NoMemory() on failure. */
323
324/* Macros */
325#define PyMem_MALLOC(n) PyCore_MALLOC(n)
326#define PyMem_REALLOC(p, n) PyCore_REALLOC((void *)(p), (n))
327#define PyMem_FREE(p) PyCore_FREE((void *)(p))
328
329/*
330 * Type-oriented memory interface
331 * ==============================
332 */
333
334/* Functions */
335#define PyMem_New(type, n) \
336 ( (type *) PyMem_Malloc((n) * sizeof(type)) )
337#define PyMem_Resize(p, type, n) \
338 ( (p) = (type *) PyMem_Realloc((n) * sizeof(type)) )
339#define PyMem_Del(p) PyMem_Free(p)
340
341/* Macros */
342#define PyMem_NEW(type, n) \
343 ( (type *) PyMem_MALLOC(_PyMem_EXTRA + (n) * sizeof(type)) )
344#define PyMem_RESIZE(p, type, n) \
345 if ((p) == NULL) \
346 (p) = (type *)(PyMem_MALLOC( \
347 _PyMem_EXTRA + (n) * sizeof(type))); \
348 else \
349 (p) = (type *)(PyMem_REALLOC((p), \
350 _PyMem_EXTRA + (n) * sizeof(type)))
351#define PyMem_DEL(p) PyMem_FREE(p)
352
353/* PyMem_XDEL is deprecated. To avoid the call when p is NULL,
354 it is recommended to write the test explicitly in the code.
355 Note that according to ANSI C, free(NULL) has no effect. */
356
357/* SCENARIOS
358
359 Here are two scenarios by Vladimir Marangozov (the author of the
360 memory allocation redesign).
361
362 1) Scenario A
363
364 Suppose you want to use a debugging malloc library that collects info on
365 where the malloc calls originate from. Assume the interface is:
366
367 d_malloc(size_t n, char* src_file, unsigned long src_line) c.s.
368
369 In this case, you would define (for example in config.h) :
370
371 #define PyCore_MALLOC_FUNC d_malloc
372 ...
373 #define PyCore_MALLOC_PROTO (size_t, char *, unsigned long)
374 ...
375 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
376
377 #define PyCore_MALLOC(n) PyCore_MALLOC_FUNC((n), __FILE__, __LINE__)
378 ...
379
380 2) Scenario B
381
382 Suppose you want to use malloc hooks (defined & initialized in a 3rd party
383 malloc library) instead of malloc functions. In this case, you would
384 define:
385
386 #define PyCore_MALLOC_FUNC (*malloc_hook)
387 ...
388 #define NEED_TO_DECLARE_MALLOC_AND_FRIEND
389
390 and ignore the previous definitions about PyCore_MALLOC_FUNC, etc.
391
392
393*/
394
395/********************************************
396 * WRAPPER FOR <time.h> and/or <sys/time.h> *
397 ********************************************/
398
399#ifdef TIME_WITH_SYS_TIME
400#include <sys/time.h>
401#include <time.h>
402#else /* !TIME_WITH_SYS_TIME */
403#ifdef HAVE_SYS_TIME_H
404#include <sys/time.h>
405#else /* !HAVE_SYS_TIME_H */
406#include <time.h>
407#endif /* !HAVE_SYS_TIME_H */
408#endif /* !TIME_WITH_SYS_TIME */
409
410
411/******************************
412 * WRAPPER FOR <sys/select.h> *
413 ******************************/
414
415/* NB caller must include <sys/types.h> */
416
417#ifdef HAVE_SYS_SELECT_H
418
419#include <sys/select.h>
420
421#else /* !HAVE_SYS_SELECT_H */
422
423#ifdef USE_GUSI1
424/* If we don't have sys/select the definition may be in unistd.h */
425#include <GUSI.h>
426#endif
427
428#endif /* !HAVE_SYS_SELECT_H */
429
430/* If the fd manipulation macros aren't defined,
431 here is a set that should do the job */
432
433#ifndef FD_SETSIZE
434#define FD_SETSIZE 256
435#endif
436
437#ifndef FD_SET
438
439typedef long fd_mask;
440
441#define NFDBITS (sizeof(fd_mask) * NBBY) /* bits per mask */
442#ifndef howmany
443#define howmany(x, y) (((x)+((y)-1))/(y))
444#endif /* howmany */
445
446typedef struct fd_set {
447 fd_mask fds_bits[howmany(FD_SETSIZE, NFDBITS)];
448} fd_set;
449
450#define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1 << ((n) % NFDBITS)))
451#define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1 << ((n) % NFDBITS)))
452#define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1 << ((n) % NFDBITS)))
453#define FD_ZERO(p) memset((char *)(p), '\0', sizeof(*(p)))
454
455#endif /* FD_SET */
Tim Peters7d3a5112000-07-08 04:17:21 +0000456#ifdef __cplusplus
457}
458#endif
459
460#endif /* Py_PYPORT_H */