This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Restrict features in wildcards
[perl5.git] / perl.h
CommitLineData
a0d0e21e 1/* perl.h
a687059c 2 *
1129b882 3 * Copyright (C) 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001
5cd3f21a 4 * 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others
a687059c 5 *
352d5a3a
LW
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
8d063cd8 8 *
8d063cd8 9 */
d6376244 10
85e6fe83
LW
11#ifndef H_PERL
12#define H_PERL 1
8d063cd8 13
760ac839
LW
14#ifdef PERL_FOR_X2P
15/*
d460ef45 16 * This file is being used for x2p stuff.
760ac839 17 * Above symbol is defined via -D in 'x2p/Makefile.SH'
d460ef45 18 * Decouple x2p stuff from some of perls more extreme eccentricities.
760ac839 19 */
55497cff 20#undef MULTIPLICITY
760ac839
LW
21#undef USE_STDIO
22#define USE_STDIO
23#endif /* PERL_FOR_X2P */
24
d460ef45 25#ifdef PERL_MICRO
12ae5dfc
JH
26# include "uconfig.h"
27#else
611cf957 28# include "config.h"
12ae5dfc 29#endif
0cb96387 30
70febf4f
CB
31/* this is used for functions which take a depth trailing
32 * argument under debugging */
33#ifdef DEBUGGING
34#define _pDEPTH ,U32 depth
35#define _aDEPTH ,depth
36#else
37#define _pDEPTH
38#define _aDEPTH
39#endif
40
7dc3eb73 41/* NOTE 1: that with gcc -std=c89 the __STDC_VERSION__ is *not* defined
9267464d 42 * because the __STDC_VERSION__ became a thing only with C90. Therefore,
7dc3eb73
JH
43 * with gcc, HAS_C99 will never become true as long as we use -std=c89.
44
45 * NOTE 2: headers lie. Do not expect that if HAS_C99 gets to be true,
46 * all the C99 features are there and are correct. */
9267464d 47#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || \
996959af 48 defined(_STDC_C99) || defined(__c99)
86a3cabf 49# define HAS_C99 1
86a3cabf
JH
50#endif
51
2c2d71f5
JH
52/* See L<perlguts/"The Perl API"> for detailed notes on
53 * PERL_IMPLICIT_CONTEXT and PERL_IMPLICIT_SYS */
54
7ee02ac1 55/* XXX NOTE that from here --> to <-- the same logic is
ac6bedea
JH
56 * repeated in makedef.pl, so be certain to update
57 * both places when editing. */
58
6f4183fe 59#ifdef USE_ITHREADS
acfe0abc 60# if !defined(MULTIPLICITY)
6f4183fe
GS
61# define MULTIPLICITY
62# endif
63#endif
64
27da23d5
JH
65#ifdef PERL_GLOBAL_STRUCT_PRIVATE
66# ifndef PERL_GLOBAL_STRUCT
67# define PERL_GLOBAL_STRUCT
68# endif
69#endif
97aff369 70
27da23d5
JH
71#ifdef PERL_GLOBAL_STRUCT
72# ifndef MULTIPLICITY
73# define MULTIPLICITY
74# endif
75#endif
76
97aff369
JH
77#ifdef MULTIPLICITY
78# ifndef PERL_IMPLICIT_CONTEXT
79# define PERL_IMPLICIT_CONTEXT
80# endif
81#endif
82
026fd48b
GH
83/* undef WIN32 when building on Cygwin (for libwin32) - gph */
84#ifdef __CYGWIN__
85# undef WIN32
86# undef _WIN32
87#endif
88
27da23d5
JH
89#if defined(__SYMBIAN32__) || (defined(__VC32__) && defined(WINS))
90# ifndef SYMBIAN
91# define SYMBIAN
92# endif
93#endif
94
a0fd4948 95#ifdef __SYMBIAN32__
27da23d5
JH
96# include "symbian/symbian_proto.h"
97#endif
98
99/* Any stack-challenged places. The limit varies (and often
100 * is configurable), but using more than a kilobyte of stack
101 * is usually dubious in these systems. */
739a0b84
NC
102#if defined(__SYMBIAN32__)
103/* Symbian: need to work around the SDK features. *
27da23d5
JH
104 * On WINS: MS VC5 generates calls to _chkstk, *
105 * if a "large" stack frame is allocated. *
106 * gcc on MARM does not generate calls like these. */
107# define USE_HEAP_INSTEAD_OF_STACK
108#endif
109
dd134d2c 110/* Use the reentrant APIs like localtime_r and getpwent_r */
7ee02ac1
KW
111/* Win32 has naturally threadsafe libraries, no need to use any _r variants.
112 * XXX KEEP makedef.pl copy of this code in sync */
14795193 113#if defined(USE_ITHREADS) && !defined(USE_REENTRANT_API) && !defined(NETWARE) && !defined(WIN32)
10bc17b6
JH
114# define USE_REENTRANT_API
115#endif
116
ac6bedea
JH
117/* <--- here ends the logic shared by perl.h and makedef.pl */
118
cf5a8da6
MHM
119#undef START_EXTERN_C
120#undef END_EXTERN_C
121#undef EXTERN_C
122#ifdef __cplusplus
123# define START_EXTERN_C extern "C" {
124# define END_EXTERN_C }
125# define EXTERN_C extern "C"
126#else
127# define START_EXTERN_C
128# define END_EXTERN_C
129# define EXTERN_C extern
130#endif
131
17a6c8e3
AD
132/* Fallback definitions in case we don't have definitions from config.h.
133 This should only matter for systems that don't use Configure and
134 haven't been modified to define PERL_STATIC_INLINE yet.
135*/
136#if !defined(PERL_STATIC_INLINE)
137# ifdef HAS_STATIC_INLINE
138# define PERL_STATIC_INLINE static inline
139# else
140# define PERL_STATIC_INLINE static
141# endif
142#endif
143
da5fdda8 144#if defined(PERL_GLOBAL_STRUCT) && !defined(PERL_GET_VARS)
27da23d5 145# ifdef PERL_GLOBAL_STRUCT_PRIVATE
cf5a8da6 146 EXTERN_C struct perl_vars* Perl_GetVarsPrivate();
27da23d5 147# define PERL_GET_VARS() Perl_GetVarsPrivate() /* see miniperlmain.c */
27da23d5
JH
148# else
149# define PERL_GET_VARS() PL_VarsPtr
150# endif
27da23d5
JH
151#endif
152
f0e5c859
DD
153/* this used to be off by default, now its on, see perlio.h */
154#define PERLIO_FUNCS_CONST
155
5aaab254 156#define pVAR struct perl_vars* my_vars PERL_UNUSED_DECL
27da23d5
JH
157
158#ifdef PERL_GLOBAL_STRUCT
159# define dVAR pVAR = (struct perl_vars*)PERL_GET_VARS()
160#else
161# define dVAR dNOOP
162#endif
163
c5be433b 164#ifdef PERL_IMPLICIT_CONTEXT
3db8f154
MB
165# ifndef MULTIPLICITY
166# define MULTIPLICITY
c5be433b 167# endif
e8dda941 168# define tTHX PerlInterpreter*
5aaab254 169# define pTHX tTHX my_perl PERL_UNUSED_DECL
3db8f154 170# define aTHX my_perl
9399a70c 171# define aTHXa(a) aTHX = (tTHX)a
27da23d5 172# ifdef PERL_GLOBAL_STRUCT
e8dda941 173# define dTHXa(a) dVAR; pTHX = (tTHX)a
27da23d5 174# else
e8dda941 175# define dTHXa(a) pTHX = (tTHX)a
27da23d5
JH
176# endif
177# ifdef PERL_GLOBAL_STRUCT
178# define dTHX dVAR; pTHX = PERL_GET_THX
179# else
180# define dTHX pTHX = PERL_GET_THX
181# endif
c5be433b 182# define pTHX_ pTHX,
c5be433b 183# define aTHX_ aTHX,
4373e329 184# define pTHX_1 2
894356b3
GS
185# define pTHX_2 3
186# define pTHX_3 4
187# define pTHX_4 5
4373e329
AL
188# define pTHX_5 6
189# define pTHX_6 7
a3b680e6
AL
190# define pTHX_7 8
191# define pTHX_8 9
192# define pTHX_9 10
a6fc70e5 193# define pTHX_12 13
e8dda941
JD
194# if defined(DEBUGGING) && !defined(PERL_TRACK_MEMPOOL)
195# define PERL_TRACK_MEMPOOL
196# endif
197#else
198# undef PERL_TRACK_MEMPOOL
c5be433b
GS
199#endif
200
2d17fb74
KW
201#ifdef DEBUGGING
202# define dTHX_DEBUGGING dTHX
203#else
204# define dTHX_DEBUGGING dNOOP
205#endif
206
76e3520e 207#define STATIC static
16c91539
BM
208
209#ifndef PERL_CORE
210/* Do not use these macros. They were part of PERL_OBJECT, which was an
211 * implementation of multiplicity using C++ objects. They have been left
212 * here solely for the sake of XS code which has incorrectly
213 * cargo-culted them.
214 */
76e3520e 215#define CPERLscope(x) x
565764a8 216#define CPERLarg void
76e3520e 217#define CPERLarg_
e3b8966e 218#define _CPERLarg
1d583055
GS
219#define PERL_OBJECT_THIS
220#define _PERL_OBJECT_THIS
221#define PERL_OBJECT_THIS_
312caa8e 222#define CALL_FPTR(fptr) (*fptr)
ef69c8fc 223#define MEMBER_TO_FPTR(name) name
16c91539 224#endif /* !PERL_CORE */
76e3520e 225
16c91539 226#define CALLRUNOPS PL_runops
f9f4320a 227
3ab4a224 228#define CALLREGCOMP(sv, flags) Perl_pregcomp(aTHX_ (sv),(flags))
f9f4320a 229
16c91539 230#define CALLREGCOMP_ENG(prog, sv, flags) (prog)->comp(aTHX_ sv, flags)
a340edde 231#define CALLREGEXEC(prog,stringarg,strend,strbeg,minend,sv,data,flags) \
16c91539 232 RX_ENGINE(prog)->exec(aTHX_ (prog),(stringarg),(strend), \
a340edde 233 (strbeg),(minend),(sv),(data),(flags))
52a21eb3
DM
234#define CALLREG_INTUIT_START(prog,sv,strbeg,strpos,strend,flags,data) \
235 RX_ENGINE(prog)->intuit(aTHX_ (prog), (sv), (strbeg), (strpos), \
f9f4320a
YO
236 (strend),(flags),(data))
237#define CALLREG_INTUIT_STRING(prog) \
16c91539 238 RX_ENGINE(prog)->checkstr(aTHX_ (prog))
f8149455 239
f8149455
YO
240#define CALLREGFREE(prog) \
241 Perl_pregfree(aTHX_ (prog))
242
243#define CALLREGFREE_PVT(prog) \
4c8117d3 244 if(prog && RX_ENGINE(prog)) RX_ENGINE(prog)->rxfree(aTHX_ (prog))
f8149455 245
2fdbfb4d 246#define CALLREG_NUMBUF_FETCH(rx,paren,usesv) \
16c91539 247 RX_ENGINE(rx)->numbered_buff_FETCH(aTHX_ (rx),(paren),(usesv))
93b32b6d 248
2fdbfb4d 249#define CALLREG_NUMBUF_STORE(rx,paren,value) \
16c91539 250 RX_ENGINE(rx)->numbered_buff_STORE(aTHX_ (rx),(paren),(value))
2fdbfb4d
AB
251
252#define CALLREG_NUMBUF_LENGTH(rx,sv,paren) \
16c91539 253 RX_ENGINE(rx)->numbered_buff_LENGTH(aTHX_ (rx),(sv),(paren))
2fdbfb4d 254
192b9cd1 255#define CALLREG_NAMED_BUFF_FETCH(rx, key, flags) \
16c91539 256 RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), NULL, ((flags) | RXapif_FETCH))
192b9cd1
AB
257
258#define CALLREG_NAMED_BUFF_STORE(rx, key, value, flags) \
16c91539 259 RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), (value), ((flags) | RXapif_STORE))
192b9cd1
AB
260
261#define CALLREG_NAMED_BUFF_DELETE(rx, key, flags) \
16c91539 262 RX_ENGINE(rx)->named_buff(aTHX_ (rx),(key), NULL, ((flags) | RXapif_DELETE))
192b9cd1
AB
263
264#define CALLREG_NAMED_BUFF_CLEAR(rx, flags) \
16c91539 265 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, ((flags) | RXapif_CLEAR))
192b9cd1
AB
266
267#define CALLREG_NAMED_BUFF_EXISTS(rx, key, flags) \
16c91539 268 RX_ENGINE(rx)->named_buff(aTHX_ (rx), (key), NULL, ((flags) | RXapif_EXISTS))
192b9cd1
AB
269
270#define CALLREG_NAMED_BUFF_FIRSTKEY(rx, flags) \
16c91539 271 RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx), NULL, ((flags) | RXapif_FIRSTKEY))
192b9cd1
AB
272
273#define CALLREG_NAMED_BUFF_NEXTKEY(rx, lastkey, flags) \
16c91539 274 RX_ENGINE(rx)->named_buff_iter(aTHX_ (rx), (lastkey), ((flags) | RXapif_NEXTKEY))
192b9cd1
AB
275
276#define CALLREG_NAMED_BUFF_SCALAR(rx, flags) \
16c91539 277 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, ((flags) | RXapif_SCALAR))
192b9cd1
AB
278
279#define CALLREG_NAMED_BUFF_COUNT(rx) \
16c91539 280 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, RXapif_REGNAMES_COUNT)
192b9cd1
AB
281
282#define CALLREG_NAMED_BUFF_ALL(rx, flags) \
16c91539 283 RX_ENGINE(rx)->named_buff(aTHX_ (rx), NULL, NULL, flags)
93b32b6d 284
49d7dfbc 285#define CALLREG_PACKAGE(rx) \
16c91539 286 RX_ENGINE(rx)->qr_package(aTHX_ (rx))
93b32b6d 287
00f254e2 288#if defined(USE_ITHREADS)
f9f4320a 289#define CALLREGDUPE(prog,param) \
f8149455
YO
290 Perl_re_dup(aTHX_ (prog),(param))
291
292#define CALLREGDUPE_PVT(prog,param) \
16c91539 293 (prog ? RX_ENGINE(prog)->dupe(aTHX_ (prog),(param)) \
00f254e2 294 : (REGEXP *)NULL)
f9f4320a 295#endif
14dd3ad8 296
09f0a348
TK
297/* In case Configure was not used (we are using a "canned config"
298 * such as Win32, or a cross-compilation setup, for example) try going
299 * by the gcc major and minor versions. One useful URL is
300 * http://www.ohse.de/uwe/articles/gcc-attributes.html,
301 * but contrary to this information warn_unused_result seems
302 * not to be in gcc 3.3.5, at least. --jhi
303 * Also, when building extensions with an installed perl, this allows
304 * the user to upgrade gcc and get the right attributes, rather than
305 * relying on the list generated at Configure time. --AD
306 * Set these up now otherwise we get confused when some of the <*thread.h>
307 * includes below indirectly pull in <perlio.h> (which needs to know if we
308 * have HASATTRIBUTE_FORMAT).
309 */
310
311#ifndef PERL_MICRO
312#if defined __GNUC__ && !defined(__INTEL_COMPILER)
313# if __GNUC__ == 3 && __GNUC_MINOR__ >= 1 || __GNUC__ > 3 /* 3.1 -> */
314# define HASATTRIBUTE_DEPRECATED
315# endif
316# if __GNUC__ >= 3 /* 3.0 -> */ /* XXX Verify this version */
317# define HASATTRIBUTE_FORMAT
318# if defined __MINGW32__
319# define PRINTF_FORMAT_NULL_OK
320# endif
321# endif
322# if __GNUC__ >= 3 /* 3.0 -> */
323# define HASATTRIBUTE_MALLOC
324# endif
325# if __GNUC__ == 3 && __GNUC_MINOR__ >= 3 || __GNUC__ > 3 /* 3.3 -> */
326# define HASATTRIBUTE_NONNULL
327# endif
328# if __GNUC__ == 2 && __GNUC_MINOR__ >= 5 || __GNUC__ > 2 /* 2.5 -> */
329# define HASATTRIBUTE_NORETURN
330# endif
331# if __GNUC__ >= 3 /* gcc 3.0 -> */
332# define HASATTRIBUTE_PURE
333# endif
334# if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 || __GNUC__ > 3 /* 3.4 -> */
335# define HASATTRIBUTE_UNUSED
336# endif
337# if __GNUC__ == 3 && __GNUC_MINOR__ == 3 && !defined(__cplusplus)
338# define HASATTRIBUTE_UNUSED /* gcc-3.3, but not g++-3.3. */
339# endif
340# if __GNUC__ == 3 && __GNUC_MINOR__ >= 4 || __GNUC__ > 3 /* 3.4 -> */
341# define HASATTRIBUTE_WARN_UNUSED_RESULT
342# endif
343#endif
344#endif /* #ifndef PERL_MICRO */
a20207d7 345
09f0a348
TK
346#ifdef HASATTRIBUTE_DEPRECATED
347# define __attribute__deprecated__ __attribute__((deprecated))
348#endif
349#ifdef HASATTRIBUTE_FORMAT
350# define __attribute__format__(x,y,z) __attribute__((format(x,y,z)))
351#endif
352#ifdef HASATTRIBUTE_MALLOC
353# define __attribute__malloc__ __attribute__((__malloc__))
354#endif
355#ifdef HASATTRIBUTE_NONNULL
356# define __attribute__nonnull__(a) __attribute__((nonnull(a)))
357#endif
358#ifdef HASATTRIBUTE_NORETURN
359# define __attribute__noreturn__ __attribute__((noreturn))
360#endif
361#ifdef HASATTRIBUTE_PURE
362# define __attribute__pure__ __attribute__((pure))
363#endif
364#ifdef HASATTRIBUTE_UNUSED
365# define __attribute__unused__ __attribute__((unused))
366#endif
367#ifdef HASATTRIBUTE_WARN_UNUSED_RESULT
368# define __attribute__warn_unused_result__ __attribute__((warn_unused_result))
369#endif
bcdf7404 370
09f0a348
TK
371/* If we haven't defined the attributes yet, define them to blank. */
372#ifndef __attribute__deprecated__
373# define __attribute__deprecated__
374#endif
375#ifndef __attribute__format__
376# define __attribute__format__(x,y,z)
377#endif
378#ifndef __attribute__malloc__
379# define __attribute__malloc__
380#endif
381#ifndef __attribute__nonnull__
382# define __attribute__nonnull__(a)
383#endif
384#ifndef __attribute__noreturn__
385# define __attribute__noreturn__
386#endif
387#ifndef __attribute__pure__
388# define __attribute__pure__
389#endif
390#ifndef __attribute__unused__
391# define __attribute__unused__
392#endif
393#ifndef __attribute__warn_unused_result__
394# define __attribute__warn_unused_result__
395#endif
a20207d7 396
09f0a348
TK
397/* Some OS warn on NULL format to printf */
398#ifdef PRINTF_FORMAT_NULL_OK
399# define __attribute__format__null_ok__(x,y,z) __attribute__format__(x,y,z)
400#else
401# define __attribute__format__null_ok__(x,y,z)
402#endif
a20207d7 403
5ba4cab2
SH
404/*
405 * Because of backward compatibility reasons the PERL_UNUSED_DECL
406 * cannot be changed from postfix to PERL_UNUSED_DECL(x). Sigh.
407 *
408 * Note that there are C compilers such as MetroWerks CodeWarrior
1266ad8c 409 * which do not have an "inlined" way (like the gcc __attribute__) of
5ba4cab2
SH
410 * marking unused variables (they need e.g. a #pragma) and therefore
411 * cpp macros like PERL_UNUSED_DECL cannot work for this purpose, even
412 * if it were PERL_UNUSED_DECL(x), which it cannot be (see above).
413 *
414 */
1266ad8c 415
5b2bd0a5 416#ifndef PERL_UNUSED_DECL
09f0a348 417# define PERL_UNUSED_DECL __attribute__unused__
5b2bd0a5 418#endif
00f254e2 419
349b520e
DM
420/* gcc -Wall:
421 * for silencing unused variables that are actually used most of the time,
a730e3f2
JH
422 * but we cannot quite get rid of, such as "ax" in PPCODE+noargs xsubs,
423 * or variables/arguments that are used only in certain configurations.
349b520e 424 */
53c1dcc0 425#ifndef PERL_UNUSED_ARG
c707756e 426# define PERL_UNUSED_ARG(x) ((void)sizeof(x))
ad73156c 427#endif
53c1dcc0 428#ifndef PERL_UNUSED_VAR
a730e3f2 429# define PERL_UNUSED_VAR(x) ((void)sizeof(x))
53c1dcc0 430#endif
ad73156c 431
23491f1d 432#if defined(USE_ITHREADS) || defined(PERL_GLOBAL_STRUCT)
96a5add6
AL
433# define PERL_UNUSED_CONTEXT PERL_UNUSED_ARG(my_perl)
434#else
435# define PERL_UNUSED_CONTEXT
436#endif
437
24e7ff4e
JH
438/* gcc (-ansi) -pedantic doesn't allow gcc statement expressions,
439 * g++ allows them but seems to have problems with them
440 * (insane errors ensue).
441 * g++ does not give insane errors now (RMB 2008-01-30, gcc 4.2.2).
442 */
443#if defined(PERL_GCC_PEDANTIC) || \
444 (defined(__GNUC__) && defined(__cplusplus) && \
445 ((__GNUC__ < 4) || ((__GNUC__ == 4) && (__GNUC_MINOR__ < 2))))
446# ifndef PERL_GCC_BRACE_GROUPS_FORBIDDEN
447# define PERL_GCC_BRACE_GROUPS_FORBIDDEN
448# endif
449#endif
450
ffea512f 451/* Use PERL_UNUSED_RESULT() to suppress the warnings about unused results
b469f1e0
JH
452 * of function calls, e.g. PERL_UNUSED_RESULT(foo(a, b)).
453 *
454 * The main reason for this is that the combination of gcc -Wunused-result
455 * (part of -Wall) and the __attribute__((warn_unused_result)) cannot
456 * be silenced with casting to void. This causes trouble when the system
457 * header files use the attribute.
458 *
459 * Use PERL_UNUSED_RESULT sparingly, though, since usually the warning
460 * is there for a good reason: you might lose success/failure information,
461 * or leak resources, or changes in resources.
ffea512f
JH
462 *
463 * But sometimes you just want to ignore the return value, e.g. on
b469f1e0
JH
464 * codepaths soon ending up in abort, or in "best effort" attempts,
465 * or in situations where there is no good way to handle failures.
ffea512f 466 *
b469f1e0
JH
467 * Sometimes PERL_UNUSED_RESULT might not be the most natural way:
468 * another possibility is that you can capture the return value
469 * and use PERL_UNUSED_VAR on that.
ffea512f 470 *
b469f1e0
JH
471 * The __typeof__() is used instead of typeof() since typeof() is not
472 * available under strict C89, and because of compilers masquerading
473 * as gcc (clang and icc), we want exactly the gcc extension
474 * __typeof__ and nothing else.
ffea512f
JH
475 */
476#ifndef PERL_UNUSED_RESULT
b469f1e0
JH
477# if defined(__GNUC__) && defined(HASATTRIBUTE_WARN_UNUSED_RESULT)
478# define PERL_UNUSED_RESULT(v) STMT_START { __typeof__(v) z = (v); (void)sizeof(z); } STMT_END
ffea512f
JH
479# else
480# define PERL_UNUSED_RESULT(v) ((void)(v))
481# endif
482#endif
483
e7ae132e
KW
484#if defined(_MSC_VER)
485/* XXX older MSVC versions have a smallish macro buffer */
486#define PERL_SMALL_MACRO_BUFFER
487#endif
488
b56aac20
DM
489/* on gcc (and clang), specify that a warning should be temporarily
490 * ignored; e.g.
491 *
7347ee54 492 * GCC_DIAG_IGNORE_DECL(-Wmultichar);
b56aac20 493 * char b = 'ab';
7347ee54 494 * GCC_DIAG_RESTORE_DECL;
b56aac20
DM
495 *
496 * based on http://dbp-consulting.com/tutorials/SuppressingGCCWarnings.html
497 *
498 * Note that "pragma GCC diagnostic push/pop" was added in GCC 4.6, Mar 2011;
499 * clang only pretends to be GCC 4.2, but still supports push/pop.
c1d6452f 500 *
7347ee54
Z
501 * Note on usage: all macros must be used at a place where a declaration
502 * or statement can occur, i.e., not in the middle of an expression.
503 * *_DIAG_IGNORE() and *_DIAG_RESTORE can be used in any such place, but
504 * must be used without a following semicolon. *_DIAG_IGNORE_DECL() and
505 * *_DIAG_RESTORE_DECL must be used with a following semicolon, and behave
506 * syntactically as declarations (like dNOOP). *_DIAG_IGNORE_STMT()
507 * and *_DIAG_RESTORE_STMT must be used with a following semicolon,
508 * and behave syntactically as statements (like NOOP).
c1d6452f 509 *
b56aac20
DM
510 */
511
57d4b8c5 512#if defined(__clang__) || defined(__clang) || \
d28ce8b4 513 (defined( __GNUC__) && ((__GNUC__ * 100) + __GNUC_MINOR__) >= 406)
c1d6452f
JH
514# define GCC_DIAG_PRAGMA(x) _Pragma (#x)
515/* clang has "clang diagnostic" pragmas, but also understands gcc. */
b56aac20 516# define GCC_DIAG_IGNORE(x) _Pragma("GCC diagnostic push") \
c1d6452f 517 GCC_DIAG_PRAGMA(GCC diagnostic ignored #x)
b56aac20
DM
518# define GCC_DIAG_RESTORE _Pragma("GCC diagnostic pop")
519#else
8ce6f80a
JH
520# define GCC_DIAG_IGNORE(w)
521# define GCC_DIAG_RESTORE
b56aac20 522#endif
7347ee54
Z
523#define GCC_DIAG_IGNORE_DECL(x) GCC_DIAG_IGNORE(x) dNOOP
524#define GCC_DIAG_RESTORE_DECL GCC_DIAG_RESTORE dNOOP
525#define GCC_DIAG_IGNORE_STMT(x) GCC_DIAG_IGNORE(x) NOOP
526#define GCC_DIAG_RESTORE_STMT GCC_DIAG_RESTORE NOOP
337458b1
JH
527/* for clang specific pragmas */
528#if defined(__clang__) || defined(__clang)
529# define CLANG_DIAG_PRAGMA(x) _Pragma (#x)
530# define CLANG_DIAG_IGNORE(x) _Pragma("clang diagnostic push") \
531 CLANG_DIAG_PRAGMA(clang diagnostic ignored #x)
532# define CLANG_DIAG_RESTORE _Pragma("clang diagnostic pop")
533#else
534# define CLANG_DIAG_IGNORE(w)
535# define CLANG_DIAG_RESTORE
536#endif
7347ee54
Z
537#define CLANG_DIAG_IGNORE_DECL(x) CLANG_DIAG_IGNORE(x) dNOOP
538#define CLANG_DIAG_RESTORE_DECL CLANG_DIAG_RESTORE dNOOP
539#define CLANG_DIAG_IGNORE_STMT(x) CLANG_DIAG_IGNORE(x) NOOP
540#define CLANG_DIAG_RESTORE_STMT CLANG_DIAG_RESTORE NOOP
b56aac20 541
6879a07b
TK
542#if defined(_MSC_VER) && (_MSC_VER >= 1300)
543# define MSVC_DIAG_IGNORE(x) __pragma(warning(push)) \
544 __pragma(warning(disable : x))
545# define MSVC_DIAG_RESTORE __pragma(warning(pop))
546#else
547# define MSVC_DIAG_IGNORE(x)
548# define MSVC_DIAG_RESTORE
549#endif
550#define MSVC_DIAG_IGNORE_DECL(x) MSVC_DIAG_IGNORE(x) dNOOP
551#define MSVC_DIAG_RESTORE_DECL MSVC_DIAG_RESTORE dNOOP
552#define MSVC_DIAG_IGNORE_STMT(x) MSVC_DIAG_IGNORE(x) NOOP
553#define MSVC_DIAG_RESTORE_STMT MSVC_DIAG_RESTORE NOOP
554
6f207bd3 555#define NOOP /*EMPTY*/(void)0
91ca80c3 556#define dNOOP struct Perl___notused_struct
71be2cbc 557
0cb96387 558#ifndef pTHX
a78adc84 559/* Don't bother defining tTHX ; using it outside
e8dda941
JD
560 * code guarded by PERL_IMPLICIT_CONTEXT is an error.
561 */
0cb96387
GS
562# define pTHX void
563# define pTHX_
0cb96387
GS
564# define aTHX
565# define aTHX_
9399a70c 566# define aTHXa(a) NOOP
0cb96387
GS
567# define dTHXa(a) dNOOP
568# define dTHX dNOOP
894356b3
GS
569# define pTHX_1 1
570# define pTHX_2 2
571# define pTHX_3 3
572# define pTHX_4 4
3d42dc86
RGS
573# define pTHX_5 5
574# define pTHX_6 6
a3b680e6
AL
575# define pTHX_7 7
576# define pTHX_8 8
577# define pTHX_9 9
a6fc70e5 578# define pTHX_12 12
0cb96387
GS
579#endif
580
27da23d5
JH
581#ifndef dVAR
582# define dVAR dNOOP
583#endif
584
b4f7f263
GS
585/* these are only defined for compatibility; should not be used internally */
586#if !defined(pTHXo) && !defined(PERL_CORE)
0cb96387
GS
587# define pTHXo pTHX
588# define pTHXo_ pTHX_
0cb96387
GS
589# define aTHXo aTHX
590# define aTHXo_ aTHX_
c5be433b 591# define dTHXo dTHX
71d280e3 592# define dTHXoa(x) dTHXa(x)
0cb96387
GS
593#endif
594
595#ifndef pTHXx
5aaab254 596# define pTHXx PerlInterpreter *my_perl
0cb96387 597# define pTHXx_ pTHXx,
0cb96387
GS
598# define aTHXx my_perl
599# define aTHXx_ aTHXx,
c5be433b 600# define dTHXx dTHX
22c35a8c 601#endif
71be2cbc 602
1de7c2ac
GS
603/* Under PERL_IMPLICIT_SYS (used in Windows for fork emulation)
604 * PerlIO_foo() expands to PL_StdIO->pFOO(PL_StdIO, ...).
605 * dTHXs is therefore needed for all functions using PerlIO_foo(). */
606#ifdef PERL_IMPLICIT_SYS
27da23d5
JH
607# ifdef PERL_GLOBAL_STRUCT_PRIVATE
608# define dTHXs dVAR; dTHX
609# else
610# define dTHXs dTHX
611# endif
1de7c2ac 612#else
27da23d5
JH
613# ifdef PERL_GLOBAL_STRUCT_PRIVATE
614# define dTHXs dVAR
615# else
616# define dTHXs dNOOP
617# endif
1de7c2ac
GS
618#endif
619
5b692037
JH
620#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN) && !defined(__cplusplus)
621# ifndef PERL_USE_GCC_BRACE_GROUPS
622# define PERL_USE_GCC_BRACE_GROUPS
623# endif
624#endif
625
728e2803 626/*
16a2918c
KW
627=head1 Miscellaneous Functions
628
629=for apidoc AmnUu|void|STMT_START
630
631 STMT_START { statements; } STMT_END;
632
633can be used as a single statement, as in
634
635 if (x) STMT_START { ... } STMT_END; else ...
636
637These are often used in macro definitions. Note that you can't return a value
638out of them.
639
640=for apidoc AmnUhu|void|STMT_END
641
642=cut
643
644 Trying to select a version that gives no warnings...
645*/
728e2803 646#if !(defined(STMT_START) && defined(STMT_END))
5b692037 647# ifdef PERL_USE_GCC_BRACE_GROUPS
a0288114 648# define STMT_START (void)( /* gcc supports "({ STATEMENTS; })" */
728e2803
PP
649# define STMT_END )
650# else
728e2803
PP
651# define STMT_START do
652# define STMT_END while (0)
728e2803
PP
653# endif
654#endif
655
d6f9c181
JH
656#ifndef BYTEORDER /* Should never happen -- byteorder is in config.h */
657# define BYTEORDER 0x1234
79072805
LW
658#endif
659
8ada0baa
JH
660#if 'A' == 65 && 'I' == 73 && 'J' == 74 && 'Z' == 90
661#define ASCIIish
662#else
663#undef ASCIIish
664#endif
665
79072805
LW
666/*
667 * The following contortions are brought to you on behalf of all the
668 * standards, semi-standards, de facto standards, not-so-de-facto standards
669 * of the world, as well as all the other botches anyone ever thought of.
670 * The basic theory is that if we work hard enough here, the rest of the
671 * code can be a lot prettier. Well, so much for theory. Sorry, Henry...
672 */
ac58e20f 673
ee0007ab 674/* define this once if either system, instead of cluttering up the src */
7c458fae 675#if defined(MSDOS) || defined(WIN32) || defined(NETWARE)
ee0007ab
LW
676#define DOSISH 1
677#endif
678
3d97541c 679/* These exist only for back-compat with XS modules. */
8162b70e 680#ifndef PERL_CORE
516e10a9 681#define VOL volatile
3d97541c 682#define CAN_PROTOTYPE
ae75e3d0 683#define _(args) args
6c2ae642 684#define I_LIMITS
36a113ce 685#define I_STDARG
6c2ae642 686#define STANDARD_C
8162b70e 687#endif
663a0e37 688
284167a5
SM
689/* By compiling a perl with -DNO_TAINT_SUPPORT or -DSILENT_NO_TAINT_SUPPORT,
690 * you get a perl without taint support, but doubtlessly with a lesser
691 * degree of support. Do not do so unless you know exactly what it means
692 * technically, have a good reason to do so, and know exactly how the
693 * perl will be used. perls with -DSILENT_NO_TAINT_SUPPORT are considered
694 * a potential security risk due to flat out ignoring the security-relevant
695 * taint flags. This being said, a perl without taint support compiled in
696 * has marginal run-time performance benefits.
697 * SILENT_NO_TAINT_SUPPORT implies NO_TAINT_SUPPORT.
698 * SILENT_NO_TAINT_SUPPORT is the same as NO_TAINT_SUPPORT except it
699 * silently ignores -t/-T instead of throwing an exception.
053a3618
SM
700 *
701 * DANGER! Using NO_TAINT_SUPPORT or SILENT_NO_TAINT_SUPPORT
6ecb40c7 702 * voids your nonexistent warranty!
284167a5 703 */
dc6d7f5c 704#if defined(SILENT_NO_TAINT_SUPPORT) && !defined(NO_TAINT_SUPPORT)
284167a5
SM
705# define NO_TAINT_SUPPORT 1
706#endif
707
708/* NO_TAINT_SUPPORT can be set to transform virtually all taint-related
709 * operations into no-ops for a very modest speed-up. Enable only if you
710 * know what you're doing: tests and CPAN modules' tests are bound to fail.
711 */
dc6d7f5c 712#ifdef NO_TAINT_SUPPORT
284167a5
SM
713# define TAINT NOOP
714# define TAINT_NOT NOOP
715# define TAINT_IF(c) NOOP
716# define TAINT_ENV() NOOP
717# define TAINT_PROPER(s) NOOP
718# define TAINT_set(s) NOOP
719# define TAINT_get 0
720# define TAINTING_get 0
721# define TAINTING_set(s) NOOP
722# define TAINT_WARN_get 0
723# define TAINT_WARN_set(s) NOOP
724#else
bc2f1ca1 725 /* Set to tainted if we are running under tainting mode */
d48c660d 726# define TAINT (PL_tainted = PL_tainting)
bc2f1ca1
KW
727
728# define TAINT_NOT (PL_tainted = FALSE) /* Untaint */
729# define TAINT_IF(c) if (UNLIKELY(c)) { TAINT; } /* Conditionally taint */
2439e033 730# define TAINT_ENV() if (UNLIKELY(PL_tainting)) { taint_env(); }
bc2f1ca1
KW
731 /* croak or warn if tainting */
732# define TAINT_PROPER(s) if (UNLIKELY(PL_tainting)) { \
733 taint_proper(NULL, s); \
734 }
284167a5 735# define TAINT_set(s) (PL_tainted = (s))
ea42b70b
TC
736# define TAINT_get (cBOOL(UNLIKELY(PL_tainted))) /* Is something tainted? */
737# define TAINTING_get (cBOOL(UNLIKELY(PL_tainting))) /* Is taint checking enabled? */
284167a5 738# define TAINTING_set(s) (PL_tainting = (s))
bc2f1ca1
KW
739# define TAINT_WARN_get (PL_taint_warn) /* FALSE => tainting violations
740 are fatal
741 TRUE => they're just
742 warnings */
284167a5
SM
743# define TAINT_WARN_set(s) (PL_taint_warn = (s))
744#endif
a687059c 745
20be6587
DM
746/* flags used internally only within pp_subst and pp_substcont */
747#ifdef PERL_CORE
748# define SUBST_TAINT_STR 1 /* string tainted */
749# define SUBST_TAINT_PAT 2 /* pattern tainted */
750# define SUBST_TAINT_REPL 4 /* replacement tainted */
751# define SUBST_TAINT_RETAINT 8 /* use re'taint' in scope */
752# define SUBST_TAINT_BOOLRET 16 /* return is boolean (don't taint) */
753#endif
754
d460ef45 755/* XXX All process group stuff is handled in pp_sys.c. Should these
a6e633de
PP
756 defines move there? If so, I could simplify this a lot. --AD 9/96.
757*/
758/* Process group stuff changed from traditional BSD to POSIX.
759 perlfunc.pod documents the traditional BSD-style syntax, so we'll
760 try to preserve that, if possible.
761*/
762#ifdef HAS_SETPGID
763# define BSD_SETPGRP(pid, pgrp) setpgid((pid), (pgrp))
da5fdda8
AC
764#elif defined(HAS_SETPGRP) && defined(USE_BSD_SETPGRP)
765# define BSD_SETPGRP(pid, pgrp) setpgrp((pid), (pgrp))
766#elif defined(HAS_SETPGRP2)
767# define BSD_SETPGRP(pid, pgrp) setpgrp2((pid), (pgrp))
a6e633de
PP
768#endif
769#if defined(BSD_SETPGRP) && !defined(HAS_SETPGRP)
770# define HAS_SETPGRP /* Well, effectively it does . . . */
771#endif
772
773/* getpgid isn't POSIX, but at least Solaris and Linux have it, and it makes
774 our life easier :-) so we'll try it.
775*/
776#ifdef HAS_GETPGID
777# define BSD_GETPGRP(pid) getpgid((pid))
da5fdda8
AC
778#elif defined(HAS_GETPGRP) && defined(USE_BSD_GETPGRP)
779# define BSD_GETPGRP(pid) getpgrp((pid))
780#elif defined(HAS_GETPGRP2)
781# define BSD_GETPGRP(pid) getpgrp2((pid))
a6e633de
PP
782#endif
783#if defined(BSD_GETPGRP) && !defined(HAS_GETPGRP)
784# define HAS_GETPGRP /* Well, effectively it does . . . */
785#endif
786
d460ef45 787/* These are not exact synonyms, since setpgrp() and getpgrp() may
a6e633de
PP
788 have different behaviors, but perl.h used to define USE_BSDPGRP
789 (prior to 5.003_05) so some extension might depend on it.
790*/
791#if defined(USE_BSD_SETPGRP) || defined(USE_BSD_GETPGRP)
792# ifndef USE_BSDPGRP
793# define USE_BSDPGRP
794# endif
663a0e37
LW
795#endif
796
486ec47a 797/* HP-UX 10.X CMA (Common Multithreaded Architecture) insists that
8ac5a1fe
MH
798 pthread.h must be included before all other header files.
799*/
3db8f154 800#if defined(USE_ITHREADS) && defined(PTHREAD_H_FIRST) && defined(I_PTHREAD)
8ac5a1fe
MH
801# include <pthread.h>
802#endif
803
58ab9e8b 804#include <sys/types.h>
663a0e37 805
9d82a2b7 806# include <stdarg.h>
760ac839 807
27db2737
JH
808#ifdef I_STDINT
809# include <stdint.h>
810#endif
811
fe14fcc3 812#include <ctype.h>
63b481b9
AC
813#include <float.h>
814#include <limits.h>
a0d0e21e
LW
815
816#ifdef METHOD /* Defined by OSF/1 v3.0 by ctype.h */
817#undef METHOD
a0d0e21e
LW
818#endif
819
12ae5dfc
JH
820#ifdef PERL_MICRO
821# define NO_LOCALE
822#endif
823
4633a7c4 824#ifdef I_LOCALE
36477c24 825# include <locale.h>
4633a7c4
LW
826#endif
827
6ebbc862
KW
828#ifdef I_XLOCALE
829# include <xlocale.h>
830#endif
831
aa8a2baa
KW
832/* If not forbidden, we enable locale handling if either 1) the POSIX 2008
833 * functions are available, or 2) just the setlocale() function. This logic is
834 * repeated in t/loc_tools.pl and makedef.pl; The three should be kept in
835 * sync. */
836#if ! defined(NO_LOCALE)
837
838# if ! defined(NO_POSIX_2008_LOCALE) \
839 && defined(HAS_NEWLOCALE) \
840 && defined(HAS_USELOCALE) \
841 && defined(HAS_DUPLOCALE) \
842 && defined(HAS_FREELOCALE) \
843 && defined(LC_ALL_MASK)
844
845 /* For simplicity, the code is written to assume that any platform advanced
846 * enough to have the Posix 2008 locale functions has LC_ALL. The final
847 * test above makes sure that assumption is valid */
848
849# define HAS_POSIX_2008_LOCALE
850# define USE_LOCALE
851# elif defined(HAS_SETLOCALE)
852# define USE_LOCALE
853# endif
854#endif
855
856#ifdef USE_LOCALE
ccd65d51 857# define HAS_SKIP_LOCALE_INIT /* Solely for XS code to test for this
af573503 858 #define */
36477c24
PP
859# if !defined(NO_LOCALE_COLLATE) && defined(LC_COLLATE) \
860 && defined(HAS_STRXFRM)
861# define USE_LOCALE_COLLATE
862# endif
863# if !defined(NO_LOCALE_CTYPE) && defined(LC_CTYPE)
864# define USE_LOCALE_CTYPE
865# endif
866# if !defined(NO_LOCALE_NUMERIC) && defined(LC_NUMERIC)
867# define USE_LOCALE_NUMERIC
868# endif
fa9b773e
KW
869# if !defined(NO_LOCALE_MESSAGES) && defined(LC_MESSAGES)
870# define USE_LOCALE_MESSAGES
871# endif
872# if !defined(NO_LOCALE_MONETARY) && defined(LC_MONETARY)
873# define USE_LOCALE_MONETARY
874# endif
56e45410
KW
875# if !defined(NO_LOCALE_TIME) && defined(LC_TIME)
876# define USE_LOCALE_TIME
877# endif
9821811f
KW
878# if !defined(NO_LOCALE_ADDRESS) && defined(LC_ADDRESS)
879# define USE_LOCALE_ADDRESS
880# endif
881# if !defined(NO_LOCALE_IDENTIFICATION) && defined(LC_IDENTIFICATION)
882# define USE_LOCALE_IDENTIFICATION
883# endif
884# if !defined(NO_LOCALE_MEASUREMENT) && defined(LC_MEASUREMENT)
885# define USE_LOCALE_MEASUREMENT
886# endif
887# if !defined(NO_LOCALE_PAPER) && defined(LC_PAPER)
888# define USE_LOCALE_PAPER
889# endif
890# if !defined(NO_LOCALE_TELEPHONE) && defined(LC_TELEPHONE)
891# define USE_LOCALE_TELEPHONE
892# endif
a0d0e21e 893
5a53bf38
KW
894/* XXX The next few defines are unfortunately duplicated in makedef.pl, and
895 * changes here MUST also be made there */
896
aa8a2baa
KW
897# if ! defined(HAS_SETLOCALE) && defined(HAS_POSIX_2008_LOCALE)
898# define USE_POSIX_2008_LOCALE
899# ifndef USE_THREAD_SAFE_LOCALE
900# define USE_THREAD_SAFE_LOCALE
901# endif
e1895adc
KW
902 /* If compiled with
903 * -DUSE_THREAD_SAFE_LOCALE, will do so even
904 * on unthreaded builds */
aa8a2baa
KW
905# elif (defined(USE_ITHREADS) || defined(USE_THREAD_SAFE_LOCALE)) \
906 && ( defined(HAS_POSIX_2008_LOCALE) \
907 || (defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1400)) \
908 && ! defined(NO_THREAD_SAFE_LOCALE)
e1895adc
KW
909# ifndef USE_THREAD_SAFE_LOCALE
910# define USE_THREAD_SAFE_LOCALE
911# endif
e9bc6d6b
KW
912# ifdef HAS_POSIX_2008_LOCALE
913# define USE_POSIX_2008_LOCALE
914# endif
915# endif
7f9e9632
KW
916#endif
917
69c5e0db
KW
918/* Microsoft documentation reads in the change log for VS 2015:
919 * "The localeconv function declared in locale.h now works correctly when
920 * per-thread locale is enabled. In previous versions of the library, this
921 * function would return the lconv data for the global locale, not the
922 * thread's locale."
923 */
924#if defined(WIN32) && defined(USE_THREAD_SAFE_LOCALE) && _MSC_VER < 1900
925# define TS_W32_BROKEN_LOCALECONV
926#endif
927
fe14fcc3 928#include <setjmp.h>
79072805 929
a0d0e21e 930#ifdef I_SYS_PARAM
79072805
LW
931# ifdef PARAM_NEEDS_TYPES
932# include <sys/types.h>
933# endif
934# include <sys/param.h>
352d5a3a 935#endif
79072805 936
ce3470dc
AD
937/* On BSD-derived systems, <sys/param.h> defines BSD to a year-month
938 value something like 199306. This may be useful if no more-specific
939 feature test is available.
940*/
941#if defined(BSD)
942# ifndef BSDish
943# define BSDish
944# endif
945#endif
946
dd512de3
AC
947/* Use all the "standard" definitions */
948#include <stdlib.h>
03a14243 949
3f270f98
JH
950/* If this causes problems, set i_unistd=undef in the hint file. */
951#ifdef I_UNISTD
763ee752
AB
952# if defined(__amigaos4__)
953# ifdef I_NETINET_IN
954# include <netinet/in.h>
955# endif
956# endif
3f270f98 957# include <unistd.h>
6e3136a6
AB
958# if defined(__amigaos4__)
959/* Under AmigaOS 4 newlib.library provides an environ. However using
960 * it doesn't give us enough control over inheritance of variables by
961 * subshells etc. so replace with custom version based on abc-shell
962 * code. */
963extern char **myenviron;
964# undef environ
965# define environ myenviron
966# endif
3f270f98
JH
967#endif
968
de8ca8af
NT
969/* for WCOREDUMP */
970#ifdef I_SYS_WAIT
971# include <sys/wait.h>
972#endif
973
9969cdde 974#ifdef __SYMBIAN32__
27da23d5
JH
975# undef _SC_ARG_MAX /* Symbian has _SC_ARG_MAX but no sysconf() */
976#endif
977
21e89b5f 978#if defined(HAS_SYSCALL) && !defined(HAS_SYSCALL_PROTO)
c45598c5 979EXTERN_C int syscall(int, ...);
2ef53570
JH
980#endif
981
21e89b5f 982#if defined(HAS_USLEEP) && !defined(HAS_USLEEP_PROTO)
c45598c5 983EXTERN_C int usleep(unsigned int);
2ef53570
JH
984#endif
985
f2f9e01d
KW
986/* macros for correct constant construction. These are in C99 <stdint.h>
987 * (so they will not be available in strict C89 mode), but they are nice, so
988 * let's define them if necessary. */
989#ifndef UINT16_C
990# if INTSIZE >= 2
991# define UINT16_C(x) ((U16_TYPE)x##U)
992# else
993# define UINT16_C(x) ((U16_TYPE)x##UL)
994# endif
995#endif
1109a392 996
f2f9e01d
KW
997#ifndef UINT32_C
998# if INTSIZE >= 4
999# define UINT32_C(x) ((U32_TYPE)x##U)
1000# else
1001# define UINT32_C(x) ((U32_TYPE)x##UL)
1002# endif
1003#endif
1109a392 1004
f2f9e01d
KW
1005#ifdef I_STDINT
1006 typedef intmax_t PERL_INTMAX_T;
1007 typedef uintmax_t PERL_UINTMAX_T;
1008#endif
1009
1010/* N.B. We use QUADKIND here instead of HAS_QUAD here, because that doesn't
1011 * actually mean what it has always been documented to mean (see RT #119753)
1012 * and is explicitly turned off outside of core with dire warnings about
1013 * removing the undef. */
1014
1015#if defined(QUADKIND)
1016# undef PeRl_INT64_C
1017# undef PeRl_UINT64_C
1018/* Prefer the native integer types (int and long) over long long
1019 * (which is not C89) and Win32-specific __int64. */
1020# if QUADKIND == QUAD_IS_INT && INTSIZE == 8
1021# define PeRl_INT64_C(c) (c)
1022# define PeRl_UINT64_C(c) CAT2(c,U)
1023# endif
1024# if QUADKIND == QUAD_IS_LONG && LONGSIZE == 8
1025# define PeRl_INT64_C(c) CAT2(c,L)
1026# define PeRl_UINT64_C(c) CAT2(c,UL)
1027# endif
1028# if QUADKIND == QUAD_IS_LONG_LONG && defined(HAS_LONG_LONG)
1029# define PeRl_INT64_C(c) CAT2(c,LL)
1030# define PeRl_UINT64_C(c) CAT2(c,ULL)
1031# endif
1032# if QUADKIND == QUAD_IS___INT64
1033# define PeRl_INT64_C(c) CAT2(c,I64)
1034# define PeRl_UINT64_C(c) CAT2(c,UI64)
1035# endif
1036# ifndef PeRl_INT64_C
1037# define PeRl_INT64_C(c) ((I64)(c)) /* last resort */
1038# define PeRl_UINT64_C(c) ((U64TYPE)(c))
1039# endif
1040/* In OS X the INT64_C/UINT64_C are defined with LL/ULL, which will
1041 * not fly with C89-pedantic gcc, so let's undefine them first so that
1042 * we can redefine them with our native integer preferring versions. */
1043# if defined(PERL_DARWIN) && defined(PERL_GCC_PEDANTIC)
1044# undef INT64_C
1045# undef UINT64_C
1046# endif
1047# ifndef INT64_C
1048# define INT64_C(c) PeRl_INT64_C(c)
1049# endif
1050# ifndef UINT64_C
1051# define UINT64_C(c) PeRl_UINT64_C(c)
1052# endif
1109a392 1053
f2f9e01d
KW
1054# ifndef I_STDINT
1055 typedef I64TYPE PERL_INTMAX_T;
1056 typedef U64TYPE PERL_UINTMAX_T;
1109a392 1057# endif
f2f9e01d
KW
1058# ifndef INTMAX_C
1059# define INTMAX_C(c) INT64_C(c)
1060# endif
1061# ifndef UINTMAX_C
1062# define UINTMAX_C(c) UINT64_C(c)
1063# endif
1064
1065#else /* below QUADKIND is undefined */
1066
1067/* Perl doesn't work on 16 bit systems, so must be 32 bit */
1068# ifndef I_STDINT
1069 typedef I32TYPE PERL_INTMAX_T;
1070 typedef U32TYPE PERL_UINTMAX_T;
1071# endif
1072# ifndef INTMAX_C
1073# define INTMAX_C(c) INT32_C(c)
1074# endif
1075# ifndef UINTMAX_C
1076# define UINTMAX_C(c) UINT32_C(c)
1077# endif
1078
1079#endif /* no QUADKIND */
1080
1081#ifdef PERL_CORE
1109a392
MHM
1082
1083/* byte-swapping functions for big-/little-endian conversion */
1084# define _swab_16_(x) ((U16)( \
f2f9e01d
KW
1085 (((U16)(x) & UINT16_C(0x00ff)) << 8) | \
1086 (((U16)(x) & UINT16_C(0xff00)) >> 8) ))
1109a392
MHM
1087
1088# define _swab_32_(x) ((U32)( \
f2f9e01d
KW
1089 (((U32)(x) & UINT32_C(0x000000ff)) << 24) | \
1090 (((U32)(x) & UINT32_C(0x0000ff00)) << 8) | \
1091 (((U32)(x) & UINT32_C(0x00ff0000)) >> 8) | \
1092 (((U32)(x) & UINT32_C(0xff000000)) >> 24) ))
1109a392
MHM
1093
1094# ifdef HAS_QUAD
1095# define _swab_64_(x) ((U64)( \
f2f9e01d
KW
1096 (((U64)(x) & UINT64_C(0x00000000000000ff)) << 56) | \
1097 (((U64)(x) & UINT64_C(0x000000000000ff00)) << 40) | \
1098 (((U64)(x) & UINT64_C(0x0000000000ff0000)) << 24) | \
1099 (((U64)(x) & UINT64_C(0x00000000ff000000)) << 8) | \
1100 (((U64)(x) & UINT64_C(0x000000ff00000000)) >> 8) | \
1101 (((U64)(x) & UINT64_C(0x0000ff0000000000)) >> 24) | \
1102 (((U64)(x) & UINT64_C(0x00ff000000000000)) >> 40) | \
1103 (((U64)(x) & UINT64_C(0xff00000000000000)) >> 56) ))
1109a392
MHM
1104# endif
1105
9c17f24a
NC
1106/* The old value was hard coded at 1008. (4096-16) seems to be a bit faster,
1107 at least on FreeBSD. YMMV, so experiment. */
1108#ifndef PERL_ARENA_SIZE
1109#define PERL_ARENA_SIZE 4080
1110#endif
1111
ccb2c8b8 1112/* Maximum level of recursion */
2b9dff67
RGS
1113#ifndef PERL_SUB_DEPTH_WARN
1114#define PERL_SUB_DEPTH_WARN 100
ccb2c8b8
RGS
1115#endif
1116
1109a392
MHM
1117#endif /* PERL_CORE */
1118
e839e6ed
DM
1119/* Maximum number of args that may be passed to an OP_MULTICONCAT op.
1120 * It determines the size of local arrays in S_maybe_multiconcat() and
1121 * pp_multiconcat().
1122 */
1123#define PERL_MULTICONCAT_MAXARG 64
1124
1125/* The indexes of fields of a multiconcat aux struct.
1126 * The fixed fields are followed by nargs+1 const segment lengths,
1127 * and if utf8 and non-utf8 differ, a second nargs+1 set for utf8.
1128 */
1129
1130#define PERL_MULTICONCAT_IX_NARGS 0 /* number of arguments */
1131#define PERL_MULTICONCAT_IX_PLAIN_PV 1 /* non-utf8 constant string */
1132#define PERL_MULTICONCAT_IX_PLAIN_LEN 2 /* non-utf8 constant string length */
1133#define PERL_MULTICONCAT_IX_UTF8_PV 3 /* utf8 constant string */
1134#define PERL_MULTICONCAT_IX_UTF8_LEN 4 /* utf8 constant string length */
1135#define PERL_MULTICONCAT_IX_LENGTHS 5 /* first of nargs+1 const segment lens */
1136#define PERL_MULTICONCAT_HEADER_SIZE 5 /* The number of fields of a
1137 multiconcat header */
1138
bdf3085f
NC
1139/* We no longer default to creating a new SV for GvSV.
1140 Do this before embed. */
1141#ifndef PERL_CREATE_GVSV
7459f06b
NC
1142# ifndef PERL_DONT_CREATE_GVSV
1143# define PERL_DONT_CREATE_GVSV
1144# endif
bdf3085f
NC
1145#endif
1146
ca0c25f6
NC
1147#if !defined(HAS_WAITPID) && !defined(HAS_WAIT4) || defined(HAS_WAITPID_RUNTIME)
1148#define PERL_USES_PL_PIDSTATUS
1149#endif
1150
739a0b84 1151#if !defined(OS2) && !defined(WIN32) && !defined(DJGPP) && !defined(__SYMBIAN32__)
a62746fa
RGS
1152#define PERL_DEFAULT_DO_EXEC3_IMPLEMENTATION
1153#endif
1154
c31fac66
GS
1155#define MEM_SIZE Size_t
1156
1936d2a7
NC
1157/* Round all values passed to malloc up, by default to a multiple of
1158 sizeof(size_t)
1159*/
1160#ifndef PERL_STRLEN_ROUNDUP_QUANTUM
1161#define PERL_STRLEN_ROUNDUP_QUANTUM Size_t_size
1162#endif
1163
f1200559
WH
1164/* sv_grow() will expand strings by at least a certain percentage of
1165 the previously *used* length to avoid excessive calls to realloc().
1166 The default is 25% of the current length.
1167*/
1168#ifndef PERL_STRLEN_EXPAND_SHIFT
1169# define PERL_STRLEN_EXPAND_SHIFT 2
1170#endif
1171
4bd4e933
SH
1172/* This use of offsetof() requires /Zc:offsetof- for VS2017 (and presumably
1173 * onwards) when building Socket.xs, but we can just use a different definition
1174 * for STRUCT_OFFSET instead. */
1175#if defined(WIN32) && defined(_MSC_VER) && _MSC_VER >= 1910
1176# define STRUCT_OFFSET(s,m) (Size_t)(&(((s *)0)->m))
1177#else
1178# include <stddef.h>
1179# define STRUCT_OFFSET(s,m) offsetof(s,m)
1180#endif
d0b86e2f 1181
86477e89
KW
1182/* ptrdiff_t is C11, so undef it under pedantic builds. (Actually it is
1183 * in C89, but apparently there are platforms where it doesn't exist. See
1184 * thread beginning at http://nntp.perl.org/group/perl.perl5.porters/251541.)
1185 * */
d0b86e2f
BF
1186#ifdef PERL_GCC_PEDANTIC
1187# undef HAS_PTRDIFF_T
51371543
GS
1188#endif
1189
86477e89
KW
1190#ifdef HAS_PTRDIFF_T
1191# define Ptrdiff_t ptrdiff_t
1192#else
1193# define Ptrdiff_t SSize_t
1194#endif
1195
9969cdde 1196#ifndef __SYMBIAN32__
d54fbe84 1197# include <string.h>
51371543
GS
1198#endif
1199
55497cff
PP
1200/* This comes after <stdlib.h> so we don't try to change the standard
1201 * library prototypes; we'll use our own in proto.h instead. */
03a14243 1202
4633a7c4 1203#ifdef MYMALLOC
86058a2d 1204# ifdef PERL_POLLUTE_MALLOC
ee13e175 1205# ifndef PERL_EXTMALLOC_DEF
86058a2d
GS
1206# define Perl_malloc malloc
1207# define Perl_calloc calloc
1208# define Perl_realloc realloc
1209# define Perl_mfree free
ee13e175 1210# endif
86058a2d
GS
1211# else
1212# define EMBEDMYMALLOC /* for compatibility */
1213# endif
827e134a 1214
651b9576
GS
1215# define safemalloc Perl_malloc
1216# define safecalloc Perl_calloc
1217# define saferealloc Perl_realloc
1218# define safefree Perl_mfree
22f7c9c9 1219# define CHECK_MALLOC_TOO_LATE_FOR_(code) STMT_START { \
54725a52 1220 if (!TAINTING_get && MallocCfg_ptr[MallocCfg_cfg_env_read]) \
22f7c9c9
JH
1221 code; \
1222 } STMT_END
1223# define CHECK_MALLOC_TOO_LATE_FOR(ch) \
1224 CHECK_MALLOC_TOO_LATE_FOR_(MALLOC_TOO_LATE_FOR(ch))
1225# define panic_write2(s) write(2, s, strlen(s))
1226# define CHECK_MALLOC_TAINT(newval) \
1227 CHECK_MALLOC_TOO_LATE_FOR_( \
1228 if (newval) { \
acfd4d8e 1229 PERL_UNUSED_RESULT(panic_write2("panic: tainting with $ENV{PERL_MALLOC_OPT}\n"));\
22f7c9c9 1230 exit(1); })
22f7c9c9 1231# define MALLOC_CHECK_TAINT(argc,argv,env) STMT_START { \
b0891165 1232 if (doing_taint(argc,argv,env)) { \
22f7c9c9
JH
1233 MallocCfg_ptr[MallocCfg_skip_cfg_env] = 1; \
1234 }} STMT_END;
f2517201
GS
1235#else /* MYMALLOC */
1236# define safemalloc safesysmalloc
1237# define safecalloc safesyscalloc
1238# define saferealloc safesysrealloc
1239# define safefree safesysfree
22f7c9c9
JH
1240# define CHECK_MALLOC_TOO_LATE_FOR(ch) ((void)0)
1241# define CHECK_MALLOC_TAINT(newval) ((void)0)
1242# define MALLOC_CHECK_TAINT(argc,argv,env)
55497cff 1243#endif /* MYMALLOC */
4633a7c4 1244
fe13d51d 1245/* diag_listed_as: "-T" is on the #! line, it must also be used on the command line */
27da23d5 1246#define TOO_LATE_FOR_(ch,what) Perl_croak(aTHX_ "\"-%c\" is on the #! line, it must also be used on the command line%s", (char)(ch), what)
22f7c9c9
JH
1247#define TOO_LATE_FOR(ch) TOO_LATE_FOR_(ch, "")
1248#define MALLOC_TOO_LATE_FOR(ch) TOO_LATE_FOR_(ch, " with $ENV{PERL_MALLOC_OPT}")
1249#define MALLOC_CHECK_TAINT2(argc,argv) MALLOC_CHECK_TAINT(argc,argv,NULL)
1250
fc36a67e 1251#ifndef memzero
04322328 1252# define memzero(d,l) memset(d,0,l)
d9d8d8de 1253#endif
378cc40b 1254
ae986130 1255#ifdef I_NETINET_IN
79072805 1256# include <netinet/in.h>
ae986130
LW
1257#endif
1258
28e8609d
JH
1259#ifdef I_ARPA_INET
1260# include <arpa/inet.h>
1261#endif
1262
1aef975c 1263#ifdef I_SYS_STAT
84902520 1264# include <sys/stat.h>
1aef975c 1265#endif
79072805 1266
287a962e
JD
1267/* Microsoft VC's sys/stat.h defines all S_Ixxx macros except S_IFIFO.
1268 This definition should ideally go into win32/win32.h, but S_IFIFO is
1269 used later here in perl.h before win32/win32.h is being included. */
1270#if !defined(S_IFIFO) && defined(_S_IFIFO)
1271# define S_IFIFO _S_IFIFO
1272#endif
1273
cc3315ba 1274/* The stat macros for Unisoft System V/88 (and derivatives
a0d0e21e
LW
1275 like UTekV) are broken, sometimes giving false positives. Undefine
1276 them here and let the code below set them to proper values.
1277
1278 The ghs macro stands for GreenHills Software C-1.8.5 which
1279 is the C compiler for sysV88 and the various derivatives.
1280 This header file bug is corrected in gcc-2.5.8 and later versions.
1281 --Kaveh Ghazi (ghazi@noc.rutgers.edu) 10/3/94. */
1282
cc3315ba 1283#if defined(m88k) && defined(ghs)
79072805
LW
1284# undef S_ISDIR
1285# undef S_ISCHR
1286# undef S_ISBLK
1287# undef S_ISREG
1288# undef S_ISFIFO
1289# undef S_ISLNK
ee0007ab 1290#endif
135863df 1291
9245da2a 1292#include <time.h>
663a0e37 1293
fe14fcc3 1294#ifdef I_SYS_TIME
85e6fe83 1295# ifdef I_SYS_TIME_KERNEL
663a0e37
LW
1296# define KERNEL
1297# endif
1298# include <sys/time.h>
85e6fe83 1299# ifdef I_SYS_TIME_KERNEL
663a0e37
LW
1300# undef KERNEL
1301# endif
a687059c 1302#endif
135863df 1303
55497cff 1304#if defined(HAS_TIMES) && defined(I_SYS_TIMES)
85e6fe83 1305# include <sys/times.h>
d9d8d8de 1306#endif
8d063cd8 1307
663a0e37 1308#include <errno.h>
1e743fda 1309
acfe0abc 1310#if defined(WIN32) && defined(PERL_IMPLICIT_SYS)
b4748376
NIS
1311# define WIN32SCK_IS_STDSCK /* don't pull in custom wsock layer */
1312#endif
1313
da70cfc6 1314#if defined(HAS_SOCKET) && !defined(WIN32) /* WIN32 handles sockets via win32.h */
1e743fda
JH
1315# include <sys/socket.h>
1316# if defined(USE_SOCKS) && defined(I_SOCKS)
1317# if !defined(INCLUDE_PROTOTYPES)
1318# define INCLUDE_PROTOTYPES /* for <socks.h> */
1319# define PERL_SOCKS_NEED_PROTOTYPES
1320# endif
1321# include <socks.h>
1322# ifdef PERL_SOCKS_NEED_PROTOTYPES /* keep cpp space clean */
1323# undef INCLUDE_PROTOTYPES
1324# undef PERL_SOCKS_NEED_PROTOTYPES
ed6116ce 1325# endif
d460ef45 1326# endif
1e743fda 1327# ifdef I_NETDB
2986a63f
JH
1328# ifdef NETWARE
1329# include<stdio.h>
1330# endif
1e743fda
JH
1331# include <netdb.h>
1332# endif
1333# ifndef ENOTSOCK
1334# ifdef I_NET_ERRNO
1335# include <net/errno.h>
1336# endif
1337# endif
1338#endif
1339
fa0a29af 1340/* sockatmark() is so new (2001) that many places might have it hidden
29820b6d
MHM
1341 * behind some -D_BLAH_BLAH_SOURCE guard. The __THROW magic is required
1342 * e.g. in Gentoo, see http://bugs.gentoo.org/show_bug.cgi?id=12605 */
fa0a29af 1343#if defined(HAS_SOCKATMARK) && !defined(HAS_SOCKATMARK_PROTO)
29820b6d
MHM
1344# if defined(__THROW) && defined(__GLIBC__)
1345int sockatmark(int) __THROW;
1346# else
2ef53570 1347int sockatmark(int);
29820b6d 1348# endif
2ef53570
JH
1349#endif
1350
7e827271 1351#if defined(__osf__) && defined(__cplusplus) && !defined(_XOPEN_SOURCE_EXTENDED) /* Tru64 "cxx" (C++), see hints/dec_osf.sh for why the _XOPEN_SOURCE_EXTENDED cannot be defined. */
45d3b546
JH
1352EXTERN_C int fchdir(int);
1353EXTERN_C int flock(int, int);
1354EXTERN_C int fseeko(FILE *, off_t, int);
1355EXTERN_C off_t ftello(FILE *);
1356#endif
1357
7e827271 1358#if defined(__SUNPRO_CC) /* SUNWspro CC (C++) */
1ccb7c8d
JH
1359EXTERN_C char *crypt(const char *, const char *);
1360#endif
1361
a54aca4b 1362#if defined(__cplusplus) && defined(__CYGWIN__)
667e2948
SP
1363EXTERN_C char *crypt(const char *, const char *);
1364#endif
1365
131e4949
TC
1366/*
1367=head1 Errno
1368
1369=for apidoc m|void|SETERRNO|int errcode|int vmserrcode
1370
1371Set C<errno>, and on VMS set C<vaxc$errno>.
1372
1373=for apidoc mn|void|dSAVEDERRNO
1374
1375Declare variables needed to save C<errno> and any operating system
1376specific error number.
1377
1378=for apidoc mn|void|dSAVE_ERRNO
1379
1380Declare variables needed to save C<errno> and any operating system
1381specific error number, and save them for optional later restoration
1382by C<RESTORE_ERRNO>.
1383
1384=for apidoc mn|void|SAVE_ERRNO
1385
1386Save C<errno> and any operating system specific error number for
1387optional later restoration by C<RESTORE_ERRNO>. Requires
1388C<dSAVEDERRNO> or C<dSAVE_ERRNO> in scope.
1389
1390=for apidoc mn|void|RESTORE_ERRNO
1391
1392Restore C<errno> and any operating system specific error number that
1393was saved by C<dSAVE_ERRNO> or C<RESTORE_ERRNO>.
1394
1395=cut
1396*/
1397
1e743fda
JH
1398#ifdef SETERRNO
1399# undef SETERRNO /* SOCKS might have defined this */
ed6116ce 1400#endif
f86702cc
PP
1401
1402#ifdef VMS
1403# define SETERRNO(errcode,vmserrcode) \
1404 STMT_START { \
1405 set_errno(errcode); \
1406 set_vaxc_errno(vmserrcode); \
1407 } STMT_END
4ee39169
CS
1408# define dSAVEDERRNO int saved_errno; unsigned saved_vms_errno
1409# define dSAVE_ERRNO int saved_errno = errno; unsigned saved_vms_errno = vaxc$errno
1410# define SAVE_ERRNO ( saved_errno = errno, saved_vms_errno = vaxc$errno )
1411# define RESTORE_ERRNO SETERRNO(saved_errno, saved_vms_errno)
1412
93189314
JH
1413# define LIB_INVARG LIB$_INVARG
1414# define RMS_DIR RMS$_DIR
1415# define RMS_FAC RMS$_FAC
1416# define RMS_FEX RMS$_FEX
1417# define RMS_FNF RMS$_FNF
1418# define RMS_IFI RMS$_IFI
1419# define RMS_ISI RMS$_ISI
1420# define RMS_PRV RMS$_PRV
1421# define SS_ACCVIO SS$_ACCVIO
1422# define SS_DEVOFFLINE SS$_DEVOFFLINE
1423# define SS_IVCHAN SS$_IVCHAN
1424# define SS_NORMAL SS$_NORMAL
8a9f18be 1425# define SS_NOPRIV SS$_NOPRIV
4388f261 1426# define SS_BUFFEROVF SS$_BUFFEROVF
748a9306 1427#else
93189314
JH
1428# define LIB_INVARG 0
1429# define RMS_DIR 0
1430# define RMS_FAC 0
1431# define RMS_FEX 0
1432# define RMS_FNF 0
1433# define RMS_IFI 0
1434# define RMS_ISI 0
1435# define RMS_PRV 0
1436# define SS_ACCVIO 0
1437# define SS_DEVOFFLINE 0
1438# define SS_IVCHAN 0
1439# define SS_NORMAL 0
8a9f18be 1440# define SS_NOPRIV 0
4388f261 1441# define SS_BUFFEROVF 0
748a9306 1442#endif
ed6116ce 1443
6ca940a9
TC
1444#ifdef WIN32
1445# define dSAVEDERRNO int saved_errno; DWORD saved_win32_errno
1446# define dSAVE_ERRNO int saved_errno = errno; DWORD saved_win32_errno = GetLastError()
1447# define SAVE_ERRNO ( saved_errno = errno, saved_win32_errno = GetLastError() )
1448# define RESTORE_ERRNO ( errno = saved_errno, SetLastError(saved_win32_errno) )
1449#endif
1450
1451#ifdef OS2
1452# define dSAVEDERRNO int saved_errno; unsigned long saved_os2_errno
1453# define dSAVE_ERRNO int saved_errno = errno; unsigned long saved_os2_errno = Perl_rc
1454# define SAVE_ERRNO ( saved_errno = errno, saved_os2_errno = Perl_rc )
1455# define RESTORE_ERRNO ( errno = saved_errno, Perl_rc = saved_os2_errno )
1456#endif
1457
1458#ifndef SETERRNO
1459# define SETERRNO(errcode,vmserrcode) (errno = (errcode))
1460#endif
1461
1462#ifndef dSAVEDERRNO
1463# define dSAVEDERRNO int saved_errno
1464# define dSAVE_ERRNO int saved_errno = errno
1465# define SAVE_ERRNO (saved_errno = errno)
1466# define RESTORE_ERRNO (errno = saved_errno)
1467#endif
1468
40bec302
TC
1469/*
1470=head1 Warning and Dieing
1471
1472=for apidoc Amn|SV *|ERRSV
1473
1474Returns the SV for C<$@>, creating it if needed.
1475
1476=for apidoc Am|void|CLEAR_ERRSV
1477
1478Clear the contents of C<$@>, setting it to the empty string.
1479
1480This replaces any read-only SV with a fresh SV and removes any magic.
1481
933e3e63
TC
1482=for apidoc Am|void|SANE_ERRSV
1483
1484Clean up ERRSV so we can safely set it.
1485
1486This replaces any read-only SV with a fresh writable copy and removes
1487any magic.
1488
40bec302
TC
1489=cut
1490*/
1491
f5fa9033 1492#define ERRSV GvSVn(PL_errgv)
dfd167e9 1493
b4f8d149 1494/* contains inlined gv_add_by_type */
dfd167e9 1495#define CLEAR_ERRSV() STMT_START { \
b4f8d149
DD
1496 SV ** const svp = &GvSV(PL_errgv); \
1497 if (!*svp) { \
a1b60c8d 1498 *svp = newSVpvs(""); \
b4f8d149
DD
1499 } else if (SvREADONLY(*svp)) { \
1500 SvREFCNT_dec_NN(*svp); \
b4f8d149 1501 *svp = newSVpvs(""); \
dfd167e9 1502 } else { \
b4f8d149 1503 SV *const errsv = *svp; \
a1b60c8d 1504 SvPVCLEAR(errsv); \
b4f8d149 1505 SvPOK_only(errsv); \
dfd167e9
NC
1506 if (SvMAGICAL(errsv)) { \
1507 mg_free(errsv); \
1508 } \
dfd167e9
NC
1509 } \
1510 } STMT_END
1511
933e3e63
TC
1512/* contains inlined gv_add_by_type */
1513#define SANE_ERRSV() STMT_START { \
1514 SV ** const svp = &GvSV(PL_errgv); \
1515 if (!*svp) { \
1516 *svp = newSVpvs(""); \
1517 } else if (SvREADONLY(*svp)) { \
1518 SV *dupsv = newSVsv(*svp); \
1519 SvREFCNT_dec_NN(*svp); \
1520 *svp = dupsv; \
1521 } else { \
1522 SV *const errsv = *svp; \
1523 if (SvMAGICAL(errsv)) { \
1524 mg_free(errsv); \
1525 } \
1526 } \
1527 } STMT_END
1528
dfd167e9 1529
414bf5ae
MHM
1530#ifdef PERL_CORE
1531# define DEFSV (0 + GvSVn(PL_defgv))
55b5114f
FC
1532# define DEFSV_set(sv) \
1533 (SvREFCNT_dec(GvSV(PL_defgv)), GvSV(PL_defgv) = SvREFCNT_inc(sv))
1534# define SAVE_DEFSV \
1535 ( \
1536 save_gp(PL_defgv, 0), \
1537 GvINTRO_off(PL_defgv), \
1538 SAVEGENERICSV(GvSV(PL_defgv)), \
1539 GvSV(PL_defgv) = NULL \
1540 )
414bf5ae
MHM
1541#else
1542# define DEFSV GvSVn(PL_defgv)
55b5114f
FC
1543# define DEFSV_set(sv) (GvSV(PL_defgv) = (sv))
1544# define SAVE_DEFSV SAVESPTR(GvSV(PL_defgv))
414bf5ae 1545#endif
38a03e6e 1546
55497cff 1547#ifndef errno
5ff3f7a4
GS
1548 extern int errno; /* ANSI allows errno to be an lvalue expr.
1549 * For example in multithreaded environments
1550 * something like this might happen:
1551 * extern int *_errno(void);
1552 * #define errno (*_errno()) */
d9d8d8de 1553#endif
663a0e37 1554
7e996671
KW
1555#define UNKNOWN_ERRNO_MSG "(unknown)"
1556
d1747a5a 1557#ifdef VMS
b21d8587
AC
1558#define Strerror(e) strerror((e), vaxc$errno)
1559#else
1560#define Strerror(e) strerror(e)
35c8bce7 1561#endif
663a0e37 1562
2304df62 1563#ifdef I_SYS_IOCTL
79072805
LW
1564# ifndef _IOCTL_
1565# include <sys/ioctl.h>
1566# endif
a687059c
LW
1567#endif
1568
ee0007ab 1569#if defined(mc300) || defined(mc500) || defined(mc700) || defined(mc6000)
79072805
LW
1570# ifdef HAS_SOCKETPAIR
1571# undef HAS_SOCKETPAIR
1572# endif
2304df62
AD
1573# ifdef I_NDBM
1574# undef I_NDBM
79072805 1575# endif
a687059c
LW
1576#endif
1577
02fc2eee
NC
1578#ifndef HAS_SOCKETPAIR
1579# ifdef HAS_SOCKET
1580# define socketpair Perl_my_socketpair
1581# endif
1582#endif
1583
a687059c 1584#if INTSIZE == 2
79072805
LW
1585# define htoni htons
1586# define ntohi ntohs
a687059c 1587#else
79072805
LW
1588# define htoni htonl
1589# define ntohi ntohl
a687059c
LW
1590#endif
1591
a0d0e21e 1592/* Configure already sets Direntry_t */
35c8bce7 1593#if defined(I_DIRENT)
da5fdda8
AC
1594# include <dirent.h>
1595#elif defined(I_SYS_NDIR)
1596# include <sys/ndir.h>
1597#elif defined(I_SYS_DIR)
1598# include <sys/dir.h>
4633a7c4 1599#endif
a687059c 1600
c623bd54
LW
1601/*
1602 * The following gobbledygook brought to you on behalf of __STDC__.
1603 * (I could just use #ifndef __STDC__, but this is more bulletproof
1604 * in the face of half-implementations.)
1605 */
1606
21e89b5f 1607#if defined(I_SYSMODE)
ca6e1c26
JH
1608#include <sys/mode.h>
1609#endif
1610
c623bd54
LW
1611#ifndef S_IFMT
1612# ifdef _S_IFMT
1613# define S_IFMT _S_IFMT
1614# else
1615# define S_IFMT 0170000
1616# endif
1617#endif
1618
1619#ifndef S_ISDIR
1620# define S_ISDIR(m) ((m & S_IFMT) == S_IFDIR)
1621#endif
1622
1623#ifndef S_ISCHR
1624# define S_ISCHR(m) ((m & S_IFMT) == S_IFCHR)
1625#endif
1626
1627#ifndef S_ISBLK
fe14fcc3
LW
1628# ifdef S_IFBLK
1629# define S_ISBLK(m) ((m & S_IFMT) == S_IFBLK)
1630# else
1631# define S_ISBLK(m) (0)
1632# endif
c623bd54
LW
1633#endif
1634
1635#ifndef S_ISREG
1636# define S_ISREG(m) ((m & S_IFMT) == S_IFREG)
1637#endif
1638
1639#ifndef S_ISFIFO
fe14fcc3
LW
1640# ifdef S_IFIFO
1641# define S_ISFIFO(m) ((m & S_IFMT) == S_IFIFO)
1642# else
1643# define S_ISFIFO(m) (0)
1644# endif
c623bd54
LW
1645#endif
1646
1647#ifndef S_ISLNK
da5fdda8
AC
1648# ifdef _S_ISLNK
1649# define S_ISLNK(m) _S_ISLNK(m)
1650# elif defined(_S_IFLNK)
1651# define S_ISLNK(m) ((m & S_IFMT) == _S_IFLNK)
1652# elif defined(S_IFLNK)
1653# define S_ISLNK(m) ((m & S_IFMT) == S_IFLNK)
1654# else
1655# define S_ISLNK(m) (0)
1656# endif
c623bd54
LW
1657#endif
1658
1659#ifndef S_ISSOCK
da5fdda8
AC
1660# ifdef _S_ISSOCK
1661# define S_ISSOCK(m) _S_ISSOCK(m)
1662# elif defined(_S_IFSOCK)
1663# define S_ISSOCK(m) ((m & S_IFMT) == _S_IFSOCK)
1664# elif defined(S_IFSOCK)
1665# define S_ISSOCK(m) ((m & S_IFMT) == S_IFSOCK)
1666# else
1667# define S_ISSOCK(m) (0)
1668# endif
c623bd54
LW
1669#endif
1670
1671#ifndef S_IRUSR
1672# ifdef S_IREAD
1673# define S_IRUSR S_IREAD
1674# define S_IWUSR S_IWRITE
1675# define S_IXUSR S_IEXEC
1676# else
1677# define S_IRUSR 0400
1678# define S_IWUSR 0200
1679# define S_IXUSR 0100
1680# endif
fac7cdfc 1681#endif
1682
1683#ifndef S_IRGRP
1684# ifdef S_IRUSR
1685# define S_IRGRP (S_IRUSR>>3)
1686# define S_IWGRP (S_IWUSR>>3)
1687# define S_IXGRP (S_IXUSR>>3)
1688# else
1689# define S_IRGRP 0040
1690# define S_IWGRP 0020
1691# define S_IXGRP 0010
1692# endif
1693#endif
1694
1695#ifndef S_IROTH
1696# ifdef S_IRUSR
1697# define S_IROTH (S_IRUSR>>6)
1698# define S_IWOTH (S_IWUSR>>6)
1699# define S_IXOTH (S_IXUSR>>6)
1700# else
1701# define S_IROTH 0040
1702# define S_IWOTH 0020
1703# define S_IXOTH 0010
1704# endif
c623bd54
LW
1705#endif
1706
1707#ifndef S_ISUID
1708# define S_ISUID 04000
1709#endif
1710
1711#ifndef S_ISGID
1712# define S_ISGID 02000
1713#endif
1714
ca6e1c26
JH
1715#ifndef S_IRWXU
1716# define S_IRWXU (S_IRUSR|S_IWUSR|S_IXUSR)
d460ef45 1717#endif
ca6e1c26
JH
1718
1719#ifndef S_IRWXG
1720# define S_IRWXG (S_IRGRP|S_IWGRP|S_IXGRP)
d460ef45 1721#endif
ca6e1c26
JH
1722
1723#ifndef S_IRWXO
1724# define S_IRWXO (S_IROTH|S_IWOTH|S_IXOTH)
d460ef45 1725#endif
ca6e1c26 1726
b6c36746 1727/* Haiku R1 seems to define S_IREAD and S_IWRITE in <posix/fcntl.h>
c21fb2b8
JH
1728 * which would get included through <sys/file.h >, but that is 3000
1729 * lines in the future. --jhi */
1730
b6c36746 1731#if !defined(S_IREAD) && !defined(__HAIKU__)
ca6e1c26
JH
1732# define S_IREAD S_IRUSR
1733#endif
1734
b6c36746 1735#if !defined(S_IWRITE) && !defined(__HAIKU__)
ca6e1c26
JH
1736# define S_IWRITE S_IWUSR
1737#endif
1738
1739#ifndef S_IEXEC
1740# define S_IEXEC S_IXUSR
1741#endif
1742
a0d0e21e 1743#if defined(cray) || defined(gould) || defined(i860) || defined(pyr)
45d8adaa
LW
1744# define SLOPPYDIVIDE
1745#endif
1746
748a9306
LW
1747#ifdef UV
1748#undef UV
1749#endif
1750
f1519f70
AC
1751/* This used to be conditionally defined based on whether we had a sprintf()
1752 * that correctly returns the string length (as required by C89), but we no
1753 * longer need that. XS modules can (and do) use this name, so it must remain
558a6899
KW
1754 * a part of the API that's visible to modules.
1755
bcd83cc8
KW
1756=head1 Miscellaneous Functions
1757
558a6899
KW
1758=for apidoc ATmD|int|my_sprintf|NN char *buffer|NN const char *pat|...
1759
1760Do NOT use this due to the possibility of overflowing C<buffer>. Instead use
1761my_snprintf()
1762
1763=cut
1764*/
f1519f70 1765#define my_sprintf sprintf
ce582cee 1766
5b692037
JH
1767/*
1768 * If we have v?snprintf() and the C99 variadic macros, we can just
1769 * use just the v?snprintf(). It is nice to try to trap the buffer
1770 * overflow, however, so if we are DEBUGGING, and we cannot use the
e5afc1ae
DD
1771 * gcc statement expressions, then use the function wrappers which try
1772 * to trap the overflow. If we can use the gcc statement expressions,
1773 * we can try that even with the version that uses the C99 variadic
1774 * macros.
5b692037
JH
1775 */
1776
1208b3dd 1777/* Note that we do not check against snprintf()/vsnprintf() returning
4059ba87
AC
1778 * negative values because that is non-standard behaviour and we use
1779 * snprintf/vsnprintf only iff HAS_VSNPRINTF has been defined, and
1780 * that should be true only if the snprintf()/vsnprintf() are true
1781 * to the standard. */
1208b3dd 1782
571ee10c 1783#define PERL_SNPRINTF_CHECK(len, max, api) STMT_START { if ((max) > 0 && (Size_t)len > (max)) Perl_croak_nocontext("panic: %s buffer overflow", STRINGIFY(api)); } STMT_END
e8549682 1784
a4eca1d4
JH
1785#ifdef USE_QUADMATH
1786# define my_snprintf Perl_my_snprintf
1787# define PERL_MY_SNPRINTF_GUARDED
4059ba87 1788#elif defined(HAS_SNPRINTF) && defined(HAS_C99_VARIADIC_MACROS) && !(defined(DEBUGGING) && !defined(PERL_USE_GCC_BRACE_GROUPS)) && !defined(PERL_GCC_PEDANTIC)
5b692037 1789# ifdef PERL_USE_GCC_BRACE_GROUPS
e8549682 1790# define my_snprintf(buffer, max, ...) ({ int len = snprintf(buffer, max, __VA_ARGS__); PERL_SNPRINTF_CHECK(len, max, snprintf); len; })
1208b3dd 1791# define PERL_MY_SNPRINTF_GUARDED
5b692037 1792# else
e8549682 1793# define my_snprintf(buffer, max, ...) snprintf(buffer, max, __VA_ARGS__)
5b692037
JH
1794# endif
1795#else
1796# define my_snprintf Perl_my_snprintf
1208b3dd 1797# define PERL_MY_SNPRINTF_GUARDED
5b692037
JH
1798#endif
1799
a4eca1d4
JH
1800/* There is no quadmath_vsnprintf, and therefore my_vsnprintf()
1801 * dies if called under USE_QUADMATH. */
4059ba87 1802#if defined(HAS_VSNPRINTF) && defined(HAS_C99_VARIADIC_MACROS) && !(defined(DEBUGGING) && !defined(PERL_USE_GCC_BRACE_GROUPS)) && !defined(PERL_GCC_PEDANTIC)
5b692037 1803# ifdef PERL_USE_GCC_BRACE_GROUPS
e8549682 1804# define my_vsnprintf(buffer, max, ...) ({ int len = vsnprintf(buffer, max, __VA_ARGS__); PERL_SNPRINTF_CHECK(len, max, vsnprintf); len; })
1208b3dd 1805# define PERL_MY_VSNPRINTF_GUARDED
5b692037 1806# else
e8549682 1807# define my_vsnprintf(buffer, max, ...) vsnprintf(buffer, max, __VA_ARGS__)
5b692037
JH
1808# endif
1809#else
1810# define my_vsnprintf Perl_my_vsnprintf
1208b3dd 1811# define PERL_MY_VSNPRINTF_GUARDED
5b692037 1812#endif
d9fad198 1813
e8549682
JH
1814/* You will definitely need to use the PERL_MY_SNPRINTF_POST_GUARD()
1815 * or PERL_MY_VSNPRINTF_POST_GUARD() if you otherwise decide to ignore
1816 * the result of my_snprintf() or my_vsnprintf(). (No, you should not
1817 * completely ignore it: otherwise you cannot know whether your output
1818 * was too long.)
1819 *
1820 * int len = my_sprintf(buf, max, ...);
1821 * PERL_MY_SNPRINTF_POST_GUARD(len, max);
1822 *
1823 * The trick is that in certain platforms [a] the my_sprintf() already
1824 * contains the sanity check, while in certain platforms [b] it needs
1825 * to be done as a separate step. The POST_GUARD is that step-- in [a]
1826 * platforms the POST_GUARD actually does nothing since the check has
1827 * already been done. Watch out for the max being the same in both calls.
1828 *
1829 * If you actually use the snprintf/vsnprintf return value already,
1830 * you assumedly are checking its validity somehow. But you can
1831 * insert the POST_GUARD() also in that case. */
1832
1833#ifndef PERL_MY_SNPRINTF_GUARDED
1834# define PERL_MY_SNPRINTF_POST_GUARD(len, max) PERL_SNPRINTF_CHECK(len, max, snprintf)
1835#else
1836# define PERL_MY_SNPRINTF_POST_GUARD(len, max) PERL_UNUSED_VAR(len)
1837#endif
1838
1839#ifndef PERL_MY_VSNPRINTF_GUARDED
1840# define PERL_MY_VSNPRINTF_POST_GUARD(len, max) PERL_SNPRINTF_CHECK(len, max, vsnprintf)
1841#else
1842# define PERL_MY_VSNPRINTF_POST_GUARD(len, max) PERL_UNUSED_VAR(len)
1843#endif
1844
a6cc4119
SP
1845#ifdef HAS_STRLCAT
1846# define my_strlcat strlcat
a6cc4119
SP
1847#endif
1848
6dba01e2
KW
1849#if defined(PERL_CORE) || defined(PERL_EXT)
1850# ifdef HAS_MEMRCHR
1851# define my_memrchr memrchr
1852# else
1853# define my_memrchr S_my_memrchr
1854# endif
1855#endif
1856
a6cc4119
SP
1857#ifdef HAS_STRLCPY
1858# define my_strlcpy strlcpy
a6cc4119
SP
1859#endif
1860
aefb3fa0
DIM
1861#ifdef HAS_STRNLEN
1862# define my_strnlen strnlen
aefb3fa0
DIM
1863#endif
1864
05f13f53 1865/*
27d4fb96
PP
1866 The IV type is supposed to be long enough to hold any integral
1867 value or a pointer.
1868 --Andy Dougherty August 1996
1869*/
1870
a22e52b9
JH
1871typedef IVTYPE IV;
1872typedef UVTYPE UV;
8175356b 1873
10cc9d2a 1874#if defined(USE_64_BIT_INT) && defined(HAS_QUAD)
6b8eaf93 1875# if QUADKIND == QUAD_IS_INT64_T && defined(INT64_MAX)
5ff3f7a4
GS
1876# define IV_MAX INT64_MAX
1877# define IV_MIN INT64_MIN
1878# define UV_MAX UINT64_MAX
cae7ae48
JH
1879# ifndef UINT64_MIN
1880# define UINT64_MIN 0
1881# endif
5ff3f7a4
GS
1882# define UV_MIN UINT64_MIN
1883# else
1884# define IV_MAX PERL_QUAD_MAX
1885# define IV_MIN PERL_QUAD_MIN
1886# define UV_MAX PERL_UQUAD_MAX
1887# define UV_MIN PERL_UQUAD_MIN
1888# endif
cf2093f6
JH
1889# define IV_IS_QUAD
1890# define UV_IS_QUAD
79072805 1891#else
8175356b 1892# if defined(INT32_MAX) && IVSIZE == 4
5ff3f7a4
GS
1893# define IV_MAX INT32_MAX
1894# define IV_MIN INT32_MIN
716026f9
DL
1895# ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
1896# define UV_MAX UINT32_MAX
1897# else
1898# define UV_MAX 4294967295U
1899# endif
cae7ae48
JH
1900# ifndef UINT32_MIN
1901# define UINT32_MIN 0
1902# endif
5ff3f7a4
GS
1903# define UV_MIN UINT32_MIN
1904# else
1905# define IV_MAX PERL_LONG_MAX
1906# define IV_MIN PERL_LONG_MIN
1907# define UV_MAX PERL_ULONG_MAX
1908# define UV_MIN PERL_ULONG_MIN
1909# endif
8175356b 1910# if IVSIZE == 8
cf2093f6
JH
1911# define IV_IS_QUAD
1912# define UV_IS_QUAD
de1c2614
JH
1913# ifndef HAS_QUAD
1914# define HAS_QUAD
1915# endif
cf2093f6
JH
1916# else
1917# undef IV_IS_QUAD
1918# undef UV_IS_QUAD
7adf2470 1919#if !defined(PERL_CORE)
e25d460c
NC
1920/* We think that removing this decade-old undef this will cause too much
1921 breakage on CPAN for too little gain. (See RT #119753)
7adf2470 1922 However, we do need HAS_QUAD in the core for use by the drand48 code. */
cb4b14d5 1923# undef HAS_QUAD
e25d460c 1924#endif
cf2093f6 1925# endif
79072805 1926#endif
d7d93a81 1927
6313e544
JH
1928#define Size_t_MAX (~(Size_t)0)
1929#define SSize_t_MAX (SSize_t)(~(Size_t)0 >> 1)
9a543cee 1930
cae7ae48 1931#define IV_DIG (BIT_DIGITS(IVSIZE * 8))
22ec83e3 1932#define UV_DIG (BIT_DIGITS(UVSIZE * 8))
56431972 1933
28e5dec8 1934#ifndef NO_PERL_PRESERVE_IVUV
f5c03d33 1935#define PERL_PRESERVE_IVUV /* We like our integers to stay integers. */
28e5dec8
JH
1936#endif
1937
d460ef45 1938/*
26bb67e2
JH
1939 * The macros INT2PTR and NUM2PTR are (despite their names)
1940 * bi-directional: they will convert int/float to or from pointers.
1941 * However the conversion to int/float are named explicitly:
1942 * PTR2IV, PTR2UV, PTR2NV.
1943 *
1944 * For int conversions we do not need two casts if pointers are
1945 * the same size as IV and UV. Otherwise we need an explicit
1946 * cast (PTRV) to avoid compiler warnings.
1947 */
56431972
RB
1948#if (IVSIZE == PTRSIZE) && (UVSIZE == PTRSIZE)
1949# define PTRV UV
1950# define INT2PTR(any,d) (any)(d)
da5fdda8
AC
1951#elif PTRSIZE == LONGSIZE
1952# define PTRV unsigned long
1953# define PTR2ul(p) (unsigned long)(p)
56431972 1954#else
da5fdda8 1955# define PTRV unsigned
e91e3b10
RB
1956#endif
1957
1958#ifndef INT2PTR
56431972 1959# define INT2PTR(any,d) (any)(PTRV)(d)
42718184 1960#endif
e91e3b10
RB
1961
1962#ifndef PTR2ul
1963# define PTR2ul(p) INT2PTR(unsigned long,p)
1964#endif
1965
56431972
RB
1966#define NUM2PTR(any,d) (any)(PTRV)(d)
1967#define PTR2IV(p) INT2PTR(IV,p)
1968#define PTR2UV(p) INT2PTR(UV,p)
1969#define PTR2NV(p) NUM2PTR(NV,p)
e91e3b10 1970#define PTR2nat(p) (PTRV)(p) /* pointer to integer of PTRSIZE */
d460ef45 1971
8141890a
JH
1972/* According to strict ANSI C89 one cannot freely cast between
1973 * data pointers and function (code) pointers. There are at least
1974 * two ways around this. One (used below) is to do two casts,
1975 * first the other pointer to an (unsigned) integer, and then
1976 * the integer to the other pointer. The other way would be
1977 * to use unions to "overlay" the pointers. For an example of
1978 * the latter technique, see union dirpu in struct xpvio in sv.h.
1979 * The only feasible use is probably temporarily storing
1980 * function pointers in a data pointer (such as a void pointer). */
1981
e91e3b10
RB
1982#define DPTR2FPTR(t,p) ((t)PTR2nat(p)) /* data pointer to function pointer */
1983#define FPTR2DPTR(t,p) ((t)PTR2nat(p)) /* function pointer to data pointer */
8141890a 1984
65202027 1985#ifdef USE_LONG_DOUBLE
f3654d06
JH
1986# if LONG_DOUBLESIZE == DOUBLESIZE
1987# define LONG_DOUBLE_EQUALS_DOUBLE
1988# undef USE_LONG_DOUBLE /* Ouch! */
65202027
DS
1989# endif
1990#endif
1991
2d4389e4
JH
1992/* The following is all to get LDBL_DIG, in order to pick a nice
1993 default value for printing floating point numbers in Gconvert.
1994 (see config.h)
1995*/
63b481b9 1996#ifndef HAS_LDBL_DIG
68d4903c 1997# if LONG_DOUBLESIZE == 10
63b481b9
AC
1998# define LDBL_DIG 18 /* assume IEEE */
1999# elif LONG_DOUBLESIZE == 12
68d4903c 2000# define LDBL_DIG 18 /* gcc? */
63b481b9
AC
2001# elif LONG_DOUBLESIZE == 16
2002# define LDBL_DIG 33 /* assume IEEE */
2003# elif LONG_DOUBLESIZE == DOUBLESIZE
2004# define LDBL_DIG DBL_DIG /* bummer */
68d4903c 2005# endif
2d4389e4
JH
2006#endif
2007
a22e52b9 2008typedef NVTYPE NV;
8175356b 2009
792d8dab
JH
2010#ifdef I_IEEEFP
2011# include <ieeefp.h>
2012#endif
923fc586 2013
bcd8bfa9
JH
2014#if defined(__DECC) && defined(__osf__)
2015/* Also Tru64 cc has broken NaN comparisons. */
2016# define NAN_COMPARE_BROKEN
c32c3de1 2017#endif
6504068e
JH
2018#if defined(__sgi)
2019# define NAN_COMPARE_BROKEN
2020#endif
c32c3de1 2021
65202027 2022#ifdef USE_LONG_DOUBLE
923fc586
JH
2023# ifdef I_SUNMATH
2024# include <sunmath.h>
2025# endif
84e6cb05 2026# if defined(LDBL_DIG)
05b4a618
JH
2027# define NV_DIG LDBL_DIG
2028# ifdef LDBL_MANT_DIG
2029# define NV_MANT_DIG LDBL_MANT_DIG
2030# endif
2031# ifdef LDBL_MIN
2032# define NV_MIN LDBL_MIN
2033# endif
2034# ifdef LDBL_MAX
2035# define NV_MAX LDBL_MAX
2036# endif
2037# ifdef LDBL_MIN_EXP
2038# define NV_MIN_EXP LDBL_MIN_EXP
2039# endif
2040# ifdef LDBL_MAX_EXP
2041# define NV_MAX_EXP LDBL_MAX_EXP
2042# endif
2043# ifdef LDBL_MIN_10_EXP
2044# define NV_MIN_10_EXP LDBL_MIN_10_EXP
2045# endif
2046# ifdef LDBL_MAX_10_EXP
2047# define NV_MAX_10_EXP LDBL_MAX_10_EXP
2048# endif
2049# ifdef LDBL_EPSILON
2050# define NV_EPSILON LDBL_EPSILON
2051# endif
2052# ifdef LDBL_MAX
2053# define NV_MAX LDBL_MAX
20f6aaab 2054/* Having LDBL_MAX doesn't necessarily mean that we have LDBL_MIN... -Allen */
da5fdda8
AC
2055# elif defined(HUGE_VALL)
2056# define NV_MAX HUGE_VALL
f4a14a62
JH
2057# endif
2058# endif
84e6cb05 2059# if defined(HAS_SQRTL)
8a00eddc
JH
2060# define Perl_acos acosl
2061# define Perl_asin asinl
2062# define Perl_atan atanl
940e3d56
JH
2063# define Perl_atan2 atan2l
2064# define Perl_ceil ceill
2065# define Perl_cos cosl
8a00eddc 2066# define Perl_cosh coshl
940e3d56 2067# define Perl_exp expl
55da5e5b 2068/* no Perl_fabs, but there's PERL_ABS */
940e3d56
JH
2069# define Perl_floor floorl
2070# define Perl_fmod fmodl
2071# define Perl_log logl
8a00eddc 2072# define Perl_log10 log10l
940e3d56
JH
2073# define Perl_pow powl
2074# define Perl_sin sinl
8a00eddc 2075# define Perl_sinh sinhl
940e3d56 2076# define Perl_sqrt sqrtl
8a00eddc
JH
2077# define Perl_tan tanl
2078# define Perl_tanh tanhl
68d4903c 2079# endif
a3540c92 2080/* e.g. libsunmath doesn't have modfl and frexpl as of mid-March 2000 */
05b4a618
JH
2081# ifndef Perl_modf
2082# ifdef HAS_MODFL
2083# define Perl_modf(x,y) modfl(x,y)
51997bc3
NC
2084/* eg glibc 2.2 series seems to provide modfl on ppc and arm, but has no
2085 prototype in <math.h> */
05b4a618 2086# ifndef HAS_MODFL_PROTO
a221a8a5 2087EXTERN_C long double modfl(long double, long double *);
05b4a618
JH
2088# endif
2089# elif (defined(HAS_TRUNCL) || defined(HAS_AINTL)) && defined(HAS_COPYSIGNL)
55954f19 2090 extern long double Perl_my_modfl(long double x, long double *ip);
03476e8e 2091# define Perl_modf(x,y) Perl_my_modfl(x,y)
05b4a618 2092# endif
a3540c92 2093# endif
05b4a618
JH
2094# ifndef Perl_frexp
2095# ifdef HAS_FREXPL
2096# define Perl_frexp(x,y) frexpl(x,y)
da5fdda8 2097# elif defined(HAS_ILOGBL) && defined(HAS_SCALBNL)
05b4a618 2098extern long double Perl_my_frexpl(long double x, int *e);
da5fdda8 2099# define Perl_frexp(x,y) Perl_my_frexpl(x,y)
03476e8e 2100# endif
a3540c92 2101# endif
05b4a618
JH
2102# ifndef Perl_ldexp
2103# ifdef HAS_LDEXPL
2104# define Perl_ldexp(x, y) ldexpl(x,y)
da5fdda8
AC
2105# elif defined(HAS_SCALBNL) && FLT_RADIX == 2
2106# define Perl_ldexp(x,y) scalbnl(x,y)
98181445
JH
2107# endif
2108# endif
38dbb4c5 2109# ifndef Perl_isnan
e4c957f4 2110# if defined(HAS_ISNANL) && !(defined(isnan) && defined(HAS_C99))
758a5d79 2111# define Perl_isnan(x) isnanl(x)
a1115378
JH
2112# elif defined(__sgi) && defined(__c99) /* XXX Configure test needed */
2113# define Perl_isnan(x) isnan(x)
758a5d79
JH
2114# endif
2115# endif
2116# ifndef Perl_isinf
e4c957f4 2117# if defined(HAS_ISINFL) && !(defined(isinf) && defined(HAS_C99))
87190886 2118# define Perl_isinf(x) isinfl(x)
a1115378
JH
2119# elif defined(__sgi) && defined(__c99) /* XXX Configure test needed */
2120# define Perl_isinf(x) isinf(x)
bcd8bfa9 2121# elif defined(LDBL_MAX) && !defined(NAN_COMPARE_BROKEN)
efcbf317 2122# define Perl_isinf(x) ((x) > LDBL_MAX || (x) < -LDBL_MAX)
a3540c92
JH
2123# endif
2124# endif
e38461cc
JH
2125# ifndef Perl_isfinite
2126# define Perl_isfinite(x) Perl_isfinitel(x)
2127# endif
84e6cb05
JH
2128#elif defined(USE_QUADMATH) && defined(I_QUADMATH)
2129# include <quadmath.h>
2130# define NV_DIG FLT128_DIG
2131# define NV_MANT_DIG FLT128_MANT_DIG
2132# define NV_MIN FLT128_MIN
2133# define NV_MAX FLT128_MAX
2134# define NV_MIN_EXP FLT128_MIN_EXP
2135# define NV_MAX_EXP FLT128_MAX_EXP
2136# define NV_EPSILON FLT128_EPSILON
2137# define NV_MIN_10_EXP FLT128_MIN_10_EXP
2138# define NV_MAX_10_EXP FLT128_MAX_10_EXP
84e6cb05
JH
2139# define Perl_acos acosq
2140# define Perl_asin asinq
2141# define Perl_atan atanq
2142# define Perl_atan2 atan2q
2143# define Perl_ceil ceilq
2144# define Perl_cos cosq
2145# define Perl_cosh coshq
2146# define Perl_exp expq
2147/* no Perl_fabs, but there's PERL_ABS */
2148# define Perl_floor floorq
2149# define Perl_fmod fmodq
2150# define Perl_log logq
2151# define Perl_log10 log10q
e6081c0e 2152# define Perl_signbit signbitq
84e6cb05
JH
2153# define Perl_pow powq
2154# define Perl_sin sinq
2155# define Perl_sinh sinhq
2156# define Perl_sqrt sqrtq
2157# define Perl_tan tanq
2158# define Perl_tanh tanhq
2159# define Perl_modf(x,y) modfq(x,y)
2160# define Perl_frexp(x,y) frexpq(x,y)
2161# define Perl_ldexp(x, y) ldexpq(x,y)
2162# define Perl_isinf(x) isinfq(x)
2163# define Perl_isnan(x) isnanq(x)
2164# define Perl_isfinite(x) !(isnanq(x) || isinfq(x))
b28053d1
JH
2165# define Perl_fp_class(x) ((x) == 0.0Q ? 0 : isinfq(x) ? 3 : isnanq(x) ? 4 : PERL_ABS(x) < FLT128_MIN ? 2 : 1)
2166# define Perl_fp_class_inf(x) (Perl_fp_class(x) == 3)
2167# define Perl_fp_class_nan(x) (Perl_fp_class(x) == 4)
2168# define Perl_fp_class_norm(x) (Perl_fp_class(x) == 1)
2169# define Perl_fp_class_denorm(x) (Perl_fp_class(x) == 2)
2170# define Perl_fp_class_zero(x) (Perl_fp_class(x) == 0)
65202027 2171#else
2d4389e4 2172# define NV_DIG DBL_DIG
63b481b9
AC
2173# define NV_MANT_DIG DBL_MANT_DIG
2174# define NV_MIN DBL_MIN
2175# define NV_MAX DBL_MAX
2176# define NV_MIN_EXP DBL_MIN_EXP
2177# define NV_MAX_EXP DBL_MAX_EXP
2178# define NV_MIN_10_EXP DBL_MIN_10_EXP
2179# define NV_MAX_10_EXP DBL_MAX_10_EXP
2180# define NV_EPSILON DBL_EPSILON
2181# define NV_MAX DBL_MAX
2182# define NV_MIN DBL_MIN
940e3d56 2183
55da5e5b 2184/* These math interfaces are C89. */
8a00eddc
JH
2185# define Perl_acos acos
2186# define Perl_asin asin
2187# define Perl_atan atan
940e3d56
JH
2188# define Perl_atan2 atan2
2189# define Perl_ceil ceil
2190# define Perl_cos cos
8a00eddc 2191# define Perl_cosh cosh
940e3d56 2192# define Perl_exp exp
55da5e5b 2193/* no Perl_fabs, but there's PERL_ABS */
940e3d56
JH
2194# define Perl_floor floor
2195# define Perl_fmod fmod
2196# define Perl_log log
8a00eddc 2197# define Perl_log10 log10
940e3d56
JH
2198# define Perl_pow pow
2199# define Perl_sin sin
8a00eddc 2200# define Perl_sinh sinh
940e3d56 2201# define Perl_sqrt sqrt
8a00eddc
JH
2202# define Perl_tan tan
2203# define Perl_tanh tanh
940e3d56
JH
2204
2205# define Perl_modf(x,y) modf(x,y)
2206# define Perl_frexp(x,y) frexp(x,y)
2207# define Perl_ldexp(x,y) ldexp(x,y)
2208
1a322af2
JH
2209# ifndef Perl_isnan
2210# ifdef HAS_ISNAN
2211# define Perl_isnan(x) isnan(x)
2212# endif
2213# endif
2214# ifndef Perl_isinf
2215# if defined(HAS_ISINF)
2216# define Perl_isinf(x) isinf(x)
bcd8bfa9 2217# elif defined(DBL_MAX) && !defined(NAN_COMPARE_BROKEN)
efcbf317 2218# define Perl_isinf(x) ((x) > DBL_MAX || (x) < -DBL_MAX)
1a322af2
JH
2219# endif
2220# endif
2221# ifndef Perl_isfinite
2222# ifdef HAS_ISFINITE
2223# define Perl_isfinite(x) isfinite(x)
2224# elif defined(HAS_FINITE)
2225# define Perl_isfinite(x) finite(x)
2226# endif
2227# endif
758a5d79
JH
2228#endif
2229
30404d48
JH
2230/* fpclassify(): C99. It is supposed to be a macro that switches on
2231* the sizeof() of its argument, so there's no need for e.g. fpclassifyl().*/
2232#if !defined(Perl_fp_class) && defined(HAS_FPCLASSIFY)
2233# include <math.h>
25c46df8
JH
2234# if defined(FP_INFINITE) && defined(FP_NAN)
2235# define Perl_fp_class(x) fpclassify(x)
2236# define Perl_fp_class_inf(x) (Perl_fp_class(x)==FP_INFINITE)
2237# define Perl_fp_class_nan(x) (Perl_fp_class(x)==FP_NAN)
2238# define Perl_fp_class_norm(x) (Perl_fp_class(x)==FP_NORMAL)
2239# define Perl_fp_class_denorm(x) (Perl_fp_class(x)==FP_SUBNORMAL)
2240# define Perl_fp_class_zero(x) (Perl_fp_class(x)==FP_ZERO)
2241# elif defined(FP_PLUS_INF) && defined(FP_QNAN)
2242/* Some versions of HP-UX (10.20) have (only) fpclassify() but which is
2243 * actually not the C99 fpclassify, with its own set of return defines. */
2244# define Perl_fp_class(x) fpclassify(x)
2245# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_PLUS_INF)
2246# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_MINUS_INF)
82947af8 2247# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_SNAN)
25c46df8
JH
2248# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_QNAN)
2249# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_PLUS_NORM)
1ae51000 2250# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_MINUS_NORM)
25c46df8
JH
2251# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_PLUS_DENORM)
2252# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_MINUS_DENORM)
2253# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_PLUS_ZERO)
2254# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_MINUS_ZERO)
2255# else
2256# undef Perl_fp_class /* Unknown set of defines */
2257# endif
30404d48
JH
2258#endif
2259
25c46df8
JH
2260/* fp_classify(): Legacy: VMS, maybe Unicos? The values, however,
2261 * are identical to the C99 fpclassify(). */
2262#if !defined(Perl_fp_class) && defined(HAS_FP_CLASSIFY)
2263# include <math.h>
558f3e66
CB
2264# ifdef __VMS
2265 /* FP_INFINITE and others are here rather than in math.h as C99 stipulates */
2266# include <fp.h>
56610b8f
CB
2267 /* oh, and the isnormal macro has a typo in it! */
2268# undef isnormal
2269# define isnormal(x) Perl_fp_class_norm(x)
558f3e66 2270# endif
25c46df8
JH
2271# if defined(FP_INFINITE) && defined(FP_NAN)
2272# define Perl_fp_class(x) fp_classify(x)
2273# define Perl_fp_class_inf(x) (Perl_fp_class(x)==FP_INFINITE)
2274# define Perl_fp_class_nan(x) (Perl_fp_class(x)==FP_NAN)
2275# define Perl_fp_class_norm(x) (Perl_fp_class(x)==FP_NORMAL)
2276# define Perl_fp_class_denorm(x) (Perl_fp_class(x)==FP_SUBNORMAL)
2277# define Perl_fp_class_zero(x) (Perl_fp_class(x)==FP_ZERO)
2278# else
2279# undef Perl_fp_class /* Unknown set of defines */
2280# endif
f279e099 2281#endif
205f51d8 2282
30404d48
JH
2283/* Feel free to check with me for the SGI manpages, SGI testing,
2284 * etcetera, if you want to try getting this to work with IRIX.
2285 *
2286 * - Allen <allens@cpan.org> */
2287
2288/* fpclass(): SysV, at least Solaris and some versions of IRIX. */
758a5d79 2289#if !defined(Perl_fp_class) && (defined(HAS_FPCLASS)||defined(HAS_FPCLASSL))
25c46df8
JH
2290/* Solaris and IRIX have fpclass/fpclassl, but they are using
2291 * an enum typedef, not cpp symbols, and Configure doesn't detect that.
2292 * Define some symbols also as cpp symbols so we can detect them. */
033a6f7a 2293# if defined(__sun) || defined(__sgi) /* XXX Configure test instead */
25c46df8
JH
2294# define FP_PINF FP_PINF
2295# define FP_QNAN FP_QNAN
2296# endif
f279e099 2297# include <math.h>
758a5d79
JH
2298# ifdef I_IEEFP
2299# include <ieeefp.h>
2300# endif
2301# ifdef I_FP
2302# include <fp.h>
2303# endif
2304# if defined(USE_LONG_DOUBLE) && defined(HAS_FPCLASSL)
f279e099 2305# define Perl_fp_class(x) fpclassl(x)
758a5d79 2306# else
f279e099
JH
2307# define Perl_fp_class(x) fpclass(x)
2308# endif
25c46df8 2309# if defined(FP_CLASS_PINF) && defined(FP_CLASS_SNAN)
f279e099
JH
2310# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_CLASS_SNAN)
2311# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_CLASS_QNAN)
2312# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_CLASS_NINF)
2313# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_CLASS_PINF)
2314# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_CLASS_NNORM)
2315# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_CLASS_PNORM)
2316# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_CLASS_NDENORM)
2317# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_CLASS_PDENORM)
2318# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_CLASS_NZERO)
2319# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_CLASS_PZERO)
25c46df8 2320# elif defined(FP_PINF) && defined(FP_QNAN)
f279e099
JH
2321# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_SNAN)
2322# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_QNAN)
2323# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_NINF)
2324# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_PINF)
2325# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_NNORM)
2326# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_PNORM)
2327# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_NDENORM)
2328# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_PDENORM)
2329# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_NZERO)
2330# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_PZERO)
25c46df8
JH
2331# else
2332# undef Perl_fp_class /* Unknown set of defines */
758a5d79 2333# endif
758a5d79
JH
2334#endif
2335
30404d48
JH
2336/* fp_class(): Legacy: at least Tru64, some versions of IRIX. */
2337#if !defined(Perl_fp_class) && (defined(HAS_FP_CLASS)||defined(HAS_FP_CLASSL))
758a5d79
JH
2338# include <math.h>
2339# if !defined(FP_SNAN) && defined(I_FP_CLASS)
2340# include <fp_class.h>
2341# endif
25c46df8 2342# if defined(FP_POS_INF) && defined(FP_QNAN)
033a6f7a 2343# ifdef __sgi /* XXX Configure test instead */
25c46df8
JH
2344# ifdef USE_LONG_DOUBLE
2345# define Perl_fp_class(x) fp_class_l(x)
2346# else
2347# define Perl_fp_class(x) fp_class_d(x)
2348# endif
f279e099 2349# else
25c46df8
JH
2350# if defined(USE_LONG_DOUBLE) && defined(HAS_FP_CLASSL)
2351# define Perl_fp_class(x) fp_classl(x)
2352# else
2353# define Perl_fp_class(x) fp_class(x)
2354# endif
f279e099 2355# endif
25c46df8
JH
2356# if defined(FP_POS_INF) && defined(FP_QNAN)
2357# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_SNAN)
2358# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_QNAN)
2359# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_NEG_INF)
2360# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_POS_INF)
2361# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_NEG_NORM)
2362# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_POS_NORM)
2363# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_NEG_DENORM)
2364# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_POS_DENORM)
2365# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_NEG_ZERO)
2366# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_POS_ZERO)
30404d48 2367# else
25c46df8 2368# undef Perl_fp_class /* Unknown set of defines */
30404d48 2369# endif
f279e099 2370# endif
30404d48
JH
2371#endif
2372
052758ae 2373/* class(), _class(): Legacy: AIX. */
758a5d79
JH
2374#if !defined(Perl_fp_class) && defined(HAS_CLASS)
2375# include <math.h>
25c46df8
JH
2376# if defined(FP_PLUS_NORM) && defined(FP_PLUS_INF)
2377# ifndef _cplusplus
2378# define Perl_fp_class(x) class(x)
2379# else
2380# define Perl_fp_class(x) _class(x)
2381# endif
2382# if defined(FP_PLUS_INF) && defined(FP_NANQ)
2383# define Perl_fp_class_snan(x) (Perl_fp_class(x)==FP_NANS)
2384# define Perl_fp_class_qnan(x) (Perl_fp_class(x)==FP_NANQ)
2385# define Perl_fp_class_ninf(x) (Perl_fp_class(x)==FP_MINUS_INF)
2386# define Perl_fp_class_pinf(x) (Perl_fp_class(x)==FP_PLUS_INF)
2387# define Perl_fp_class_nnorm(x) (Perl_fp_class(x)==FP_MINUS_NORM)
2388# define Perl_fp_class_pnorm(x) (Perl_fp_class(x)==FP_PLUS_NORM)
2389# define Perl_fp_class_ndenorm(x) (Perl_fp_class(x)==FP_MINUS_DENORM)
2390# define Perl_fp_class_pdenorm(x) (Perl_fp_class(x)==FP_PLUS_DENORM)
2391# define Perl_fp_class_nzero(x) (Perl_fp_class(x)==FP_MINUS_ZERO)
2392# define Perl_fp_class_pzero(x) (Perl_fp_class(x)==FP_PLUS_ZERO)
2393# else
2394# undef Perl_fp_class /* Unknown set of defines */
2395# endif
758a5d79 2396# endif
30404d48
JH
2397#endif
2398
2399/* Win32: _fpclass(), _isnan(), _finite(). */
90f54b14 2400#ifdef _MSC_VER
796ea9ea
JH
2401# ifndef Perl_isnan
2402# define Perl_isnan(x) _isnan(x)
2403# endif
2404# ifndef Perl_isfinite
2405# define Perl_isfinite(x) _finite(x)
2406# endif
2407# ifndef Perl_fp_class_snan
25c46df8
JH
2408/* No simple way to #define Perl_fp_class because _fpclass()
2409 * returns a set of bits. */
796ea9ea
JH
2410# define Perl_fp_class_snan(x) (_fpclass(x) & _FPCLASS_SNAN)
2411# define Perl_fp_class_qnan(x) (_fpclass(x) & _FPCLASS_QNAN)
82947af8 2412# define Perl_fp_class_nan(x) (_fpclass(x) & (_FPCLASS_SNAN|_FPCLASS_QNAN))
796ea9ea
JH
2413# define Perl_fp_class_ninf(x) (_fpclass(x) & _FPCLASS_NINF))
2414# define Perl_fp_class_pinf(x) (_fpclass(x) & _FPCLASS_PINF))
2415# define Perl_fp_class_inf(x) (_fpclass(x) & (_FPCLASS_NINF|_FPCLASS_PINF))
2416# define Perl_fp_class_nnorm(x) (_fpclass(x) & _FPCLASS_NN)
2417# define Perl_fp_class_pnorm(x) (_fpclass(x) & _FPCLASS_PN)
2418# define Perl_fp_class_norm(x) (_fpclass(x) & (_FPCLASS_NN|_FPCLASS_PN))
2419# define Perl_fp_class_ndenorm(x) (_fpclass(x) & _FPCLASS_ND)
2420# define Perl_fp_class_pdenorm(x) (_fpclass(x) & _FPCLASS_PD)
2421# define Perl_fp_class_denorm(x) (_fpclass(x) & (_FPCLASS_ND|_FPCLASS_PD))
2422# define Perl_fp_class_nzero(x) (_fpclass(x) & _FPCLASS_NZ)
2423# define Perl_fp_class_pzero(x) (_fpclass(x) & _FPCLASS_PZ)
2424# define Perl_fp_class_zero(x) (_fpclass(x) & (_FPCLASS_NZ|_FPCLASS_PZ))
2425# endif
f279e099
JH
2426#endif
2427
2428#if !defined(Perl_fp_class_inf) && \
2429 defined(Perl_fp_class_pinf) && defined(Perl_fp_class_ninf)
2430# define Perl_fp_class_inf(x) \
2431 (Perl_fp_class_pinf(x) || Perl_fp_class_ninf(x))
2432#endif
2433
2434#if !defined(Perl_fp_class_nan) && \
2435 defined(Perl_fp_class_snan) && defined(Perl_fp_class_qnan)
2436# define Perl_fp_class_nan(x) \
2437 (Perl_fp_class_snan(x) || Perl_fp_class_qnan(x))
2438#endif
2439
2440#if !defined(Perl_fp_class_zero) && \
2441 defined(Perl_fp_class_pzero) && defined(Perl_fp_class_nzero)
2442# define Perl_fp_class_zero(x) \
2443 (Perl_fp_class_pzero(x) || Perl_fp_class_nzero(x))
2444#endif
2445
2446#if !defined(Perl_fp_class_norm) && \
2447 defined(Perl_fp_class_pnorm) && defined(Perl_fp_class_nnorm)
2448# define Perl_fp_class_norm(x) \
2449 (Perl_fp_class_pnorm(x) || Perl_fp_class_nnorm(x))
2450#endif
2451
2452#if !defined(Perl_fp_class_denorm) && \
2453 defined(Perl_fp_class_pdenorm) && defined(Perl_fp_class_ndenorm)
2454# define Perl_fp_class_denorm(x) \
2455 (Perl_fp_class_pdenorm(x) || Perl_fp_class_ndenorm(x))
796ea9ea 2456#endif
758a5d79
JH
2457
2458#ifndef Perl_isnan
1a322af2
JH
2459# ifdef Perl_fp_class_nan
2460# define Perl_isnan(x) Perl_fp_class_nan(x)
da5fdda8
AC
2461# elif defined(HAS_UNORDERED)
2462# define Perl_isnan(x) unordered((x), 0.0)
758a5d79 2463# else
da5fdda8 2464# define Perl_isnan(x) ((x)!=(x))
758a5d79
JH
2465# endif
2466#endif
2467
1a322af2
JH
2468#ifndef Perl_isinf
2469# ifdef Perl_fp_class_inf
2470# define Perl_isinf(x) Perl_fp_class_inf(x)
2471# endif
ca6c63e1
JH
2472#endif
2473
758a5d79 2474#ifndef Perl_isfinite
25c46df8 2475# if defined(HAS_ISFINITE) && !defined(isfinite)
c496ca58 2476# define Perl_isfinite(x) isfinite((double)(x))
4efd38a4 2477# elif defined(HAS_FINITE)
c496ca58 2478# define Perl_isfinite(x) finite((double)(x))
4efd38a4
JH
2479# elif defined(Perl_fp_class_finite)
2480# define Perl_isfinite(x) Perl_fp_class_finite(x)
758a5d79 2481# else
c496ca58
JH
2482/* For the infinities the multiplication returns nan,
2483 * for the nan the multiplication also returns nan,
2484 * for everything else (that is, finite) zero should be returned. */
4efd38a4 2485# define Perl_isfinite(x) (((x) * 0) == 0)
a3540c92 2486# endif
65202027
DS
2487#endif
2488
25c46df8
JH
2489#ifndef Perl_isinf
2490# if defined(Perl_isfinite) && defined(Perl_isnan)
2491# define Perl_isinf(x) !(Perl_isfinite(x)||Perl_isnan(x))
2492# endif
2493#endif
2494
d3685250
JH
2495/* We need Perl_isfinitel (ends with ell) (if available) even when
2496 * not USE_LONG_DOUBLE because the printf code (sv_catpvfn_flags)
2497 * needs that. */
2498#if defined(HAS_LONG_DOUBLE) && !defined(Perl_isfinitel)
2499/* If isfinite() is a macro and looks like we have C99,
2500 * we assume it's the type-aware C99 isfinite(). */
c06e9e3e 2501# if defined(HAS_ISFINITE) && defined(isfinite) && defined(HAS_C99)
d3685250
JH
2502# define Perl_isfinitel(x) isfinite(x)
2503# elif defined(HAS_ISFINITEL)
2504# define Perl_isfinitel(x) isfinitel(x)
2505# elif defined(HAS_FINITEL)
2506# define Perl_isfinitel(x) finitel(x)
2507# elif defined(HAS_INFL) && defined(HAS_NANL)
2508# define Perl_isfinitel(x) !(isinfl(x)||isnanl(x))
16334e6e 2509# else
99bf9e32 2510# define Perl_isfinitel(x) ((x) * 0 == 0) /* See Perl_isfinite. */
d3685250
JH
2511# endif
2512#endif
2513
a36244b7
JH
2514/* The default is to use Perl's own atof() implementation (in numeric.c).
2515 * Usually that is the one to use but for some platforms (e.g. UNICOS)
2516 * it is however best to use the native implementation of atof.
2517 * You can experiment with using your native one by -DUSE_PERL_ATOF=0.
2518 * Some good tests to try out with either setting are t/base/num.t,
205f51d8
AS
2519 * t/op/numconvert.t, and t/op/pack.t. Note that if using long doubles
2520 * you may need to be using a different function than atof! */
a36244b7
JH
2521
2522#ifndef USE_PERL_ATOF
2523# ifndef _UNICOS
2524# define USE_PERL_ATOF
2525# endif
2526#else
2527# if USE_PERL_ATOF == 0
2528# undef USE_PERL_ATOF
2529# endif
2530#endif
2531
2532#ifdef USE_PERL_ATOF
2533# define Perl_atof(s) Perl_my_atof(s)
6928bedc 2534# define Perl_atof2(s, n) Perl_my_atof3(aTHX_ (s), &(n), 0)
a36244b7
JH
2535#else
2536# define Perl_atof(s) (NV)atof(s)
2537# define Perl_atof2(s, n) ((n) = atof(s))
2538#endif
6928bedc 2539#define my_atof2(a,b) my_atof3(a,b,0)
cf2093f6 2540
99abf803
PP
2541/*
2542 * CHAR_MIN and CHAR_MAX are not included here, as the (char) type may be
2543 * ambiguous. It may be equivalent to (signed char) or (unsigned char)
2544 * depending on local options. Until Configure detects this (or at least
2545 * detects whether the "signed" keyword is available) the CHAR ranges
2546 * will not be included. UCHAR functions normally.
2547 * - kja
2548 */
27d4fb96 2549
6ec488b3
AC
2550#define PERL_UCHAR_MIN ((unsigned char)0)
2551#define PERL_UCHAR_MAX ((unsigned char)UCHAR_MAX)
27d4fb96 2552
6ec488b3
AC
2553#define PERL_USHORT_MIN ((unsigned short)0)
2554#define PERL_USHORT_MAX ((unsigned short)USHRT_MAX)
27d4fb96 2555
6ec488b3
AC
2556#define PERL_SHORT_MAX ((short)SHRT_MAX)
2557#define PERL_SHORT_MIN ((short)SHRT_MIN)
27d4fb96 2558
6ec488b3 2559#define PERL_UINT_MAX ((unsigned int)UINT_MAX)
99abf803 2560#define PERL_UINT_MIN ((unsigned int)0)
27d4fb96 2561
6ec488b3
AC
2562#define PERL_INT_MAX ((int)INT_MAX)
2563#define PERL_INT_MIN ((int)INT_MIN)
27d4fb96 2564
6ec488b3 2565#define PERL_ULONG_MAX ((unsigned long)ULONG_MAX)
99abf803 2566#define PERL_ULONG_MIN ((unsigned long)0L)
27d4fb96 2567
6ec488b3
AC
2568#define PERL_LONG_MAX ((long)LONG_MAX)
2569#define PERL_LONG_MIN ((long)LONG_MIN)
760ac839 2570
d7d93a81 2571#ifdef UV_IS_QUAD
99abf803 2572# define PERL_UQUAD_MAX (~(UV)0)
f1c3b19a 2573# define PERL_UQUAD_MIN ((UV)0)
99abf803 2574# define PERL_QUAD_MAX ((IV) (PERL_UQUAD_MAX >> 1))
a6e633de 2575# define PERL_QUAD_MIN (-PERL_QUAD_MAX - ((3 & -1) == 3))
79072805
LW
2576#endif
2577
d78a5caa
KW
2578/*
2579=head1 Numeric functions
2580
2581=for apidoc AmnUh||PERL_INT_MIN
2582=for apidoc AmnUh||PERL_LONG_MAX
2583=for apidoc AmnUh||PERL_LONG_MIN
2584=for apidoc AmnUh||PERL_QUAD_MAX
2585=for apidoc AmnUh||PERL_SHORT_MAX
2586=for apidoc AmnUh||PERL_SHORT_MIN
2587=for apidoc AmnUh||PERL_UCHAR_MAX
2588=for apidoc AmnUh||PERL_UCHAR_MIN
2589=for apidoc AmnUh||PERL_UINT_MAX
2590=for apidoc AmnUh||PERL_ULONG_MAX
2591=for apidoc AmnUh||PERL_ULONG_MIN
2592=for apidoc AmnUh||PERL_UQUAD_MAX
2593=for apidoc AmnUh||PERL_UQUAD_MIN
2594=for apidoc AmnUh||PERL_USHORT_MAX
2595=for apidoc AmnUh||PERL_USHORT_MIN
2596=for apidoc AmnUh||PERL_QUAD_MIN
2597=for apidoc AmnU||PERL_INT_MAX
2598This and
2599C<PERL_INT_MIN>,
2600C<PERL_LONG_MAX>,
2601C<PERL_LONG_MIN>,
2602C<PERL_QUAD_MAX>,
2603C<PERL_SHORT_MAX>,
2604C<PERL_SHORT_MIN>,
2605C<PERL_UCHAR_MAX>,
2606C<PERL_UCHAR_MIN>,
2607C<PERL_UINT_MAX>,
2608C<PERL_ULONG_MAX>,
2609C<PERL_ULONG_MIN>,
2610C<PERL_UQUAD_MAX>,
2611C<PERL_UQUAD_MIN>,
2612C<PERL_USHORT_MAX>,
2613C<PERL_USHORT_MIN>,
2614C<PERL_QUAD_MIN>
2615give the largest and smallest number representable in the current
2616platform in variables of the corresponding types.
2617
2618For signed types, the smallest representable number is the most negative
2619number, the one furthest away from zero.
2620
2621For C99 and later compilers, these correspond to things like C<INT_MAX>, which
2622are available to the C code. But these constants, furnished by Perl,
2623allow code compiled on earlier compilers to portably have access to the same
2624constants.
2625
2626=cut
2627
2628*/
2629
ee0007ab 2630typedef MEM_SIZE STRLEN;
450a55e4 2631
79072805
LW
2632typedef struct op OP;
2633typedef struct cop COP;
2634typedef struct unop UNOP;
2f7c6295 2635typedef struct unop_aux UNOP_AUX;
79072805
LW
2636typedef struct binop BINOP;
2637typedef struct listop LISTOP;
2638typedef struct logop LOGOP;
79072805
LW
2639typedef struct pmop PMOP;
2640typedef struct svop SVOP;
7934575e 2641typedef struct padop PADOP;
79072805 2642typedef struct pvop PVOP;
79072805 2643typedef struct loop LOOP;
b46e009d 2644typedef struct methop METHOP;
79072805 2645
7aef8e5b 2646#ifdef PERL_CORE
8be227ab
FC
2647typedef struct opslab OPSLAB;
2648typedef struct opslot OPSLOT;
2649#endif
2650
52db365a 2651typedef struct block_hooks BHK;
1830b3d9 2652typedef struct custom_op XOP;
52db365a 2653
cd1541b2
NC
2654typedef struct interpreter PerlInterpreter;
2655
d2b3f365 2656/* SGI's <sys/sema.h> has struct sv */
cc3315ba 2657#if defined(__sgi)
d2b3f365 2658# define STRUCT_SV perl_sv
b8d3c5db
JH
2659#else
2660# define STRUCT_SV sv
2661#endif
2662typedef struct STRUCT_SV SV;
79072805
LW
2663typedef struct av AV;
2664typedef struct hv HV;
2665typedef struct cv CV;
d2f13c59 2666typedef struct p5rx REGEXP;
79072805 2667typedef struct gp GP;
0c30d9ec 2668typedef struct gv GV;
8990e307 2669typedef struct io IO;
c09156bb 2670typedef struct context PERL_CONTEXT;
79072805
LW
2671typedef struct block BLOCK;
2672
2673typedef struct magic MAGIC;
2674typedef struct xpv XPV;
2675typedef struct xpviv XPVIV;
ff68c719 2676typedef struct xpvuv XPVUV;
79072805
LW
2677typedef struct xpvnv XPVNV;
2678typedef struct xpvmg XPVMG;
2679typedef struct xpvlv XPVLV;
d361b004 2680typedef struct xpvinvlist XINVLIST;
79072805
LW
2681typedef struct xpvav XPVAV;
2682typedef struct xpvhv XPVHV;
2683typedef struct xpvgv XPVGV;
2684typedef struct xpvcv XPVCV;
2685typedef struct xpvbm XPVBM;
2686typedef struct xpvfm XPVFM;
8990e307 2687typedef struct xpvio XPVIO;
79072805
LW
2688typedef struct mgvtbl MGVTBL;
2689typedef union any ANY;
5f7fde29
GS
2690typedef struct ptr_tbl_ent PTR_TBL_ENT_t;
2691typedef struct ptr_tbl PTR_TBL_t;
8cf8f3d1
NIS
2692typedef struct clone_params CLONE_PARAMS;
2693
0f94cb1f 2694/* a pad is currently just an AV; but that might change,
7261499d
FC
2695 * so hide the type. */
2696typedef struct padlist PADLIST;
67634234 2697typedef AV PAD;
9b7476d7 2698typedef struct padnamelist PADNAMELIST;
0f94cb1f 2699typedef struct padname PADNAME;
67634234 2700
5a736967
DM
2701/* always enable PERL_OP_PARENT */
2702#if !defined(PERL_OP_PARENT)
5d32d268
DM
2703# define PERL_OP_PARENT
2704#endif
2705
93c10d60
FC
2706/* enable PERL_COPY_ON_WRITE by default */
2707#if !defined(PERL_COPY_ON_WRITE) && !defined(PERL_NO_COW)
2708# define PERL_COPY_ON_WRITE
13b0f67d 2709#endif
07d01d6e 2710
93c10d60 2711#ifdef PERL_COPY_ON_WRITE
db2c6cb3 2712# define PERL_ANY_COW
9f351b45
DM
2713#else
2714# define PERL_SAWAMPERSAND
db2c6cb3
FC
2715#endif
2716
40653c20
FC
2717#if defined(PERL_DEBUG_READONLY_OPS) && !defined(USE_ITHREADS)
2718# error PERL_DEBUG_READONLY_OPS only works with ithreads
2719#endif
2720
378cc40b 2721#include "handy.h"
64935bc6 2722#include "charclass_invlists.h"
a0d0e21e 2723
6b8eaf93 2724#if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_RAWIO)
6b8eaf93
JH
2725# if LSEEKSIZE == 8 && !defined(USE_64_BIT_RAWIO)
2726# define USE_64_BIT_RAWIO /* implicit */
2727# endif
4564133c
JH
2728#endif
2729
6b8eaf93
JH
2730/* Notice the use of HAS_FSEEKO: now we are obligated to always use
2731 * fseeko/ftello if possible. Don't go #defining ftell to ftello yourself,
2732 * however, because operating systems like to do that themself. */
2733#ifndef FSEEKSIZE
2734# ifdef HAS_FSEEKO
2735# define FSEEKSIZE LSEEKSIZE
2736# else
2737# define FSEEKSIZE LONGSIZE
d460ef45 2738# endif
6b8eaf93
JH
2739#endif
2740
2741#if defined(USE_LARGE_FILES) && !defined(NO_64_BIT_STDIO)
6b8eaf93
JH
2742# if FSEEKSIZE == 8 && !defined(USE_64_BIT_STDIO)
2743# define USE_64_BIT_STDIO /* implicit */
2744# endif
2745#endif
4564133c 2746
09458382 2747#ifdef USE_64_BIT_RAWIO
d9b3e12d
JH
2748# ifdef HAS_OFF64_T
2749# undef Off_t
2750# define Off_t off64_t
2751# undef LSEEKSIZE
2752# define LSEEKSIZE 8
5ff3f7a4 2753# endif
d9b3e12d
JH
2754/* Most 64-bit environments have defines like _LARGEFILE_SOURCE that
2755 * will trigger defines like the ones below. Some 64-bit environments,
09458382 2756 * however, do not. Therefore we have to explicitly mix and match. */
d9b3e12d
JH
2757# if defined(USE_OPEN64)
2758# define open open64
5ff3f7a4 2759# endif
d9b3e12d
JH
2760# if defined(USE_LSEEK64)
2761# define lseek lseek64
6b8eaf93
JH
2762# else
2763# if defined(USE_LLSEEK)
2764# define lseek llseek
2765# endif
d9b3e12d
JH
2766# endif
2767# if defined(USE_STAT64)
2768# define stat stat64
2769# endif
2770# if defined(USE_FSTAT64)
2771# define fstat fstat64
2772# endif
2773# if defined(USE_LSTAT64)
2774# define lstat lstat64
2775# endif
2776# if defined(USE_FLOCK64)
2777# define flock flock64
2778# endif
2779# if defined(USE_LOCKF64)
2780# define lockf lockf64
2781# endif
2782# if defined(USE_FCNTL64)
2783# define fcntl fcntl64
2784# endif
2785# if defined(USE_TRUNCATE64)
2786# define truncate truncate64
2787# endif
2788# if defined(USE_FTRUNCATE64)
2789# define ftruncate ftruncate64
2790# endif
2791#endif
2792
2793#ifdef USE_64_BIT_STDIO
2794# ifdef HAS_FPOS64_T
2795# undef Fpos_t
2796# define Fpos_t fpos64_t
2797# endif
2798/* Most 64-bit environments have defines like _LARGEFILE_SOURCE that
2799 * will trigger defines like the ones below. Some 64-bit environments,
2800 * however, do not. */
2801# if defined(USE_FOPEN64)
2802# define fopen fopen64
2803# endif
2804# if defined(USE_FSEEK64)
6b8eaf93 2805# define fseek fseek64 /* don't do fseeko here, see perlio.c */
d9b3e12d
JH
2806# endif
2807# if defined(USE_FTELL64)
6b8eaf93 2808# define ftell ftell64 /* don't do ftello here, see perlio.c */
d9b3e12d
JH
2809# endif
2810# if defined(USE_FSETPOS64)
2811# define fsetpos fsetpos64
2812# endif
2813# if defined(USE_FGETPOS64)
2814# define fgetpos fgetpos64
2815# endif
2816# if defined(USE_TMPFILE64)
2817# define tmpfile tmpfile64
2818# endif
2819# if defined(USE_FREOPEN64)
2820# define freopen freopen64
5ff3f7a4
GS
2821# endif
2822#endif
2823
e37778c2 2824#if defined(OS2)
2c2d71f5
JH
2825# include "iperlsys.h"
2826#endif
2827
748a9306 2828#ifdef DOSISH
19848b3f
JH
2829# if defined(OS2)
2830# include "os2ish.h"
2831# else
2832# include "dosish.h"
2833# endif
009819bb 2834#elif defined(VMS)
748a9306 2835# include "vmsish.h"
009819bb 2836#elif defined(PLAN9)
19848b3f 2837# include "./plan9/plan9ish.h"
009819bb 2838#elif defined(__VOS__)
196918b0
PG
2839# ifdef __GNUC__
2840# include "./vos/vosish.h"
2841# else
2842# include "vos/vosish.h"
2843# endif
009819bb 2844#elif defined(__SYMBIAN32__)
27da23d5 2845# include "symbian/symbianish.h"
009819bb 2846#elif defined(__HAIKU__)
df00ff3b 2847# include "haiku/haikuish.h"
009819bb 2848#else
19848b3f 2849# include "unixish.h"
13b6e58c
JH
2850#endif
2851
ea34f6bd 2852#ifdef __amigaos4__
3c0208ad
AB
2853# include "amigaos.h"
2854# undef FD_CLOEXEC /* a lie in AmigaOS */
2855#endif
2856
2946a158 2857/* NSIG logic from Configure --> */
2946a158
JH
2858#ifndef NSIG
2859# ifdef _NSIG
2860# define NSIG (_NSIG)
da5fdda8 2861# elif defined(SIGMAX)
2946a158 2862# define NSIG (SIGMAX+1)
da5fdda8 2863# elif defined(SIG_MAX)
2946a158 2864# define NSIG (SIG_MAX+1)
da5fdda8 2865# elif defined(_SIG_MAX)
2946a158 2866# define NSIG (_SIG_MAX+1)
da5fdda8 2867# elif defined(MAXSIG)
2946a158 2868# define NSIG (MAXSIG+1)
da5fdda8 2869# elif defined(MAX_SIG)
2946a158 2870# define NSIG (MAX_SIG+1)
da5fdda8 2871# elif defined(SIGARRAYSIZE)
2946a158 2872# define NSIG SIGARRAYSIZE /* Assume ary[SIGARRAYSIZE] */
da5fdda8 2873# elif defined(_sys_nsig)
2946a158 2874# define NSIG (_sys_nsig) /* Solaris 2.5 */
da5fdda8
AC
2875# else
2876 /* Default to some arbitrary number that's big enough to get most
2877 * of the common signals. */
2946a158 2878# define NSIG 50
da5fdda8 2879# endif
2946a158
JH
2880#endif
2881/* <-- NSIG logic from Configure */
2882
13b6e58c
JH
2883#ifndef NO_ENVIRON_ARRAY
2884# define USE_ENVIRON_ARRAY
2885#endif
32f822de 2886
e7124897 2887
5e7940ce 2888#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
c99d8cd6
DM
2889 /* having sigaction(2) means that the OS supports both 1-arg and 3-arg
2890 * signal handlers. But the perl core itself only fully supports 1-arg
2891 * handlers, so don't enable for now.
2892 * NB: POSIX::sigaction() supports both.
2893 *
2894 * # define PERL_USE_3ARG_SIGHANDLER
2895 */
5e7940ce
DM
2896#endif
2897
e7124897
DM
2898/* Siginfo_t:
2899 * This is an alias for the OS's siginfo_t, except that where the OS
2900 * doesn't support it, declare a dummy version instead. This allows us to
2901 * have signal handler functions which always have a Siginfo_t parameter
2902 * regardless of platform, (and which will just be passed a NULL value
2903 * where the OS doesn't support HAS_SIGACTION).
2904 */
2905
2906#if defined(HAS_SIGACTION) && defined(SA_SIGINFO)
2907 typedef siginfo_t Siginfo_t;
2908#else
2909 typedef struct {
2910 int si_signo;
2911 } Siginfo_t;
2912#endif
2913
2914
c77b533b
JH
2915/*
2916 * initialise to avoid floating-point exceptions from overflow, etc
2917 */
2918#ifndef PERL_FPU_INIT
2919# ifdef HAS_FPSETMASK
2920# if HAS_FLOATINGPOINT_H
2921# include <floatingpoint.h>
2922# endif
ad3a8c67
NC
2923/* Some operating systems have this as a macro, which in turn expands to a comma
2924 expression, and the last sub-expression is something that gets calculated,
2925 and then they have the gall to warn that a value computed is not used. Hence
2926 cast to void. */
2927# define PERL_FPU_INIT (void)fpsetmask(0)
da5fdda8
AC
2928# elif defined(SIGFPE) && defined(SIG_IGN) && !defined(PERL_MICRO)
2929# define PERL_FPU_INIT PL_sigfpe_saved = (Sighandler_t) signal(SIGFPE, SIG_IGN)
2930# define PERL_FPU_PRE_EXEC { Sigsave_t xfpe; rsignal_save(SIGFPE, PL_sigfpe_saved, &xfpe);
2931# define PERL_FPU_POST_EXEC rsignal_restore(SIGFPE, &xfpe); }
c77b533b 2932# else
da5fdda8 2933# define PERL_FPU_INIT
c77b533b
JH
2934# endif
2935#endif
b35112e7
CS
2936#ifndef PERL_FPU_PRE_EXEC
2937# define PERL_FPU_PRE_EXEC {
2938# define PERL_FPU_POST_EXEC }
2939#endif
c77b533b 2940
a308b05a
JH
2941/* In Tru64 the cc -ieee enables the IEEE math but disables traps.
2942 * We need to reenable the "invalid" trap because otherwise generation
2943 * of NaN values leaves the IEEE fp flags in bad state, leaving any further
2944 * fp ops behaving strangely (Inf + 1 resulting in zero, for example). */
2945#ifdef __osf__
2946# include <machine/fpu.h>
2947# define PERL_SYS_FPU_INIT \
2948 STMT_START { \
2949 ieee_set_fp_control(IEEE_TRAP_ENABLE_INV); \
2950 signal(SIGFPE, SIG_IGN); \
2951 } STMT_END
2952#endif
94a08944
JH
2953/* In IRIX the default for Flush to Zero bit is true,
2954 * which means that results going below the minimum of normal
2955 * floating points go to zero, instead of going denormal/subnormal.
2956 * This is unlike almost any other system running Perl, so let's clear it.
2957 * [perl #123767] IRIX64 blead (ddce084a) opbasic/arith.t failure, originally
2958 * [perl #120426] small numbers shouldn't round to zero if they have extra floating digits
2959 *
2960 * XXX The flush-to-zero behaviour should be a Configure scan.
2961 * To change the behaviour usually requires some system-specific
2962 * incantation, though, like the below. */
2963#ifdef __sgi
2964# include <sys/fpu.h>
2965# define PERL_SYS_FPU_INIT \
2966 STMT_START { \
2967 union fpc_csr csr; \
2968 csr.fc_word = get_fpc_csr(); \
2969 csr.fc_struct.flush = 0; \
2970 set_fpc_csr(csr.fc_word); \
2971 } STMT_END
2972#endif
a308b05a
JH
2973
2974#ifndef PERL_SYS_FPU_INIT
2975# define PERL_SYS_FPU_INIT NOOP
2976#endif
2977
cbec8ebe
DM
2978#ifndef PERL_SYS_INIT3_BODY
2979# define PERL_SYS_INIT3_BODY(argvp,argcp,envp) PERL_SYS_INIT_BODY(argvp,argcp)
ed344e4f
IZ
2980#endif
2981
eb533572 2982/*
dcccc8ff
KW
2983=head1 Miscellaneous Functions
2984
4ad9e498 2985=for apidoc Am|void|PERL_SYS_INIT|int *argc|char*** argv
eb533572 2986Provides system-specific tune up of the C runtime environment necessary to
72d33970 2987run Perl interpreters. This should be called only once, before creating
eb533572
DM
2988any Perl interpreters.
2989
4ad9e498 2990=for apidoc Am|void|PERL_SYS_INIT3|int *argc|char*** argv|char*** env
eb533572 2991Provides system-specific tune up of the C runtime environment necessary to
72d33970 2992run Perl interpreters. This should be called only once, before creating
eb533572
DM
2993any Perl interpreters.
2994
2995=for apidoc Am|void|PERL_SYS_TERM|
2996Provides system-specific clean up of the C runtime environment after
72d33970 2997running Perl interpreters. This should be called only once, after
eb533572
DM
2998freeing any remaining Perl interpreters.
2999
3000=cut
3001 */
3002
cbec8ebe
DM
3003#define PERL_SYS_INIT(argc, argv) Perl_sys_init(argc, argv)
3004#define PERL_SYS_INIT3(argc, argv, env) Perl_sys_init3(argc, argv, env)
d0820ef1 3005#define PERL_SYS_TERM() Perl_sys_term()
cbec8ebe 3006
be708cc0
JH
3007#ifndef PERL_WRITE_MSG_TO_CONSOLE
3008# define PERL_WRITE_MSG_TO_CONSOLE(io, msg, len) PerlIO_write(io, msg, len)
3009#endif
3010
0f31cffe
GS
3011#ifndef MAXPATHLEN
3012# ifdef PATH_MAX
b990c8b2
JH
3013# ifdef _POSIX_PATH_MAX
3014# if PATH_MAX > _POSIX_PATH_MAX
09448d78
JH
3015/* POSIX 1990 (and pre) was ambiguous about whether PATH_MAX
3016 * included the null byte or not. Later amendments of POSIX,
3017 * XPG4, the Austin Group, and the Single UNIX Specification
3018 * all explicitly include the null byte in the PATH_MAX.
3019 * Ditto for _POSIX_PATH_MAX. */
3020# define MAXPATHLEN PATH_MAX
b990c8b2 3021# else
09448d78 3022# define MAXPATHLEN _POSIX_PATH_MAX
b990c8b2
JH
3023# endif
3024# else
3025# define MAXPATHLEN (PATH_MAX+1)
3026# endif
0f31cffe 3027# else
d552bce8 3028# define MAXPATHLEN 1024 /* Err on the large side. */
0f31cffe
GS
3029# endif
3030#endif
3031
3db8f154 3032/* USE_5005THREADS needs to be after unixish.h as <pthread.h> includes
dd96f567 3033 * <sys/signal.h> which defines NSIG - which will stop inclusion of <signal.h>
32f822de
GS
3034 * this results in many functions being undeclared which bothers C++
3035 * May make sense to have threads after "*ish.h" anyway
3036 */
3037
3baee7cc
JH
3038/* clang Thread Safety Analysis/Annotations/Attributes
3039 * http://clang.llvm.org/docs/ThreadSafetyAnalysis.html
3040 *
bdc795f4 3041 * Available since clang 3.6-ish (appeared in 3.4, but shaky still in 3.5).
3baee7cc 3042 * Apple XCode hijacks __clang_major__ and __clang_minor__
bdc795f4
JH
3043 * (6.1 means really clang 3.6), so needs extra hijinks
3044 * (could probably also test the contents of __apple_build_version__).
3baee7cc
JH
3045 */
3046#if defined(USE_ITHREADS) && defined(I_PTHREAD) && \
3047 defined(__clang__) && \
ab0fe36e
DM
3048 !defined(PERL_GLOBAL_STRUCT) && \
3049 !defined(PERL_GLOBAL_STRUCT_PRIVATE) && \
3baee7cc
JH
3050 !defined(SWIG) && \
3051 ((!defined(__apple_build_version__) && \
bdc795f4 3052 ((__clang_major__ == 3 && __clang_minor__ >= 6) || \
8412ccf9 3053 (__clang_major__ >= 4))) || \
3baee7cc 3054 (defined(__apple_build_version__) && \
bdc795f4
JH
3055 ((__clang_major__ == 6 && __clang_minor__ >= 1) || \
3056 (__clang_major__ >= 7))))
3baee7cc
JH
3057# define PERL_TSA__(x) __attribute__((x))
3058# define PERL_TSA_ACTIVE
3059#else
3060# define PERL_TSA__(x) /* No TSA, make TSA attributes no-ops. */
3061# undef PERL_TSA_ACTIVE
3062#endif
3063
3064/* PERL_TSA_CAPABILITY() is used to annotate typedefs.
3065 * typedef old_type PERL_TSA_CAPABILITY("mutex") new_type;
3066 */
3067#define PERL_TSA_CAPABILITY(x) \
3068 PERL_TSA__(capability(x))
3069
3070/* In the below examples the mutex must be lexically visible, usually
3071 * either as global variables, or as function arguments. */
3072
3073/* PERL_TSA_GUARDED_BY() is used to annotate global variables.
3074 *
3075 * Foo foo PERL_TSA_GUARDED_BY(mutex);
3076 */
3077#define PERL_TSA_GUARDED_BY(x) \
3078 PERL_TSA__(guarded_by(x))
3079
3080/* PERL_TSA_PT_GUARDED_BY() is used to annotate global pointers.
3081 * The data _behind_ the pointer is guarded.
3082 *
3083 * Foo* ptr PERL_TSA_PT_GUARDED_BY(mutex);
3084 */
3085#define PERL_TSA_PT_GUARDED_BY(x) \
3086 PERL_TSA__(pt_guarded_by(x))
3087
3088/* PERL_TSA_REQUIRES() is used to annotate functions.
3089 * The caller MUST hold the resource when calling the function.
3090 *
3091 * void Foo() PERL_TSA_REQUIRES(mutex);
3092 */
3093#define PERL_TSA_REQUIRES(x) \
3094 PERL_TSA__(requires_capability(x))
3095
3096/* PERL_TSA_EXCLUDES() is used to annotate functions.
3097 * The caller MUST NOT hold resource when calling the function.
3098 *
3099 * EXCLUDES should be used when the function first acquires
3100 * the resource and then releases it. Use to avoid deadlock.
3101 *
3102 * void Foo() PERL_TSA_EXCLUDES(mutex);
3103 */
3104#define PERL_TSA_EXCLUDES(x) \
3105 PERL_TSA__(locks_excluded(x))
3106
3107/* PERL_TSA_ACQUIRE() is used to annotate functions.
3108 * The caller MUST NOT hold the resource when calling the function,
3109 * and the function will acquire the resource.
3110 *
3111 * void Foo() PERL_TSA_ACQUIRE(mutex);
3112 */
3113#define PERL_TSA_ACQUIRE(x) \
3114 PERL_TSA__(acquire_capability(x))
3115
3116/* PERL_TSA_RELEASE() is used to annotate functions.
3117 * The caller MUST hold the resource when calling the function,
3118 * and the function will release the resource.
3119 *
3120 * void Foo() PERL_TSA_RELEASE(mutex);
3121 */
3122#define PERL_TSA_RELEASE(x) \
3123 PERL_TSA__(release_capability(x))
3124
3125/* PERL_TSA_NO_TSA is used to annotate functions.
3126 * Used when being intentionally unsafe, or when the code is too
3127 * complicated for the analysis. Use sparingly.
3128 *
3129 * void Foo() PERL_TSA_NO_TSA;
3130 */
3131#define PERL_TSA_NO_TSA \
3132 PERL_TSA__(no_thread_safety_analysis)
3133
3134/* There are more annotations/attributes available, see the clang
3135 * documentation for details. */
3136
3db8f154 3137#if defined(USE_ITHREADS)
2986a63f 3138# ifdef NETWARE
da5fdda8
AC
3139# include <nw5thread.h>
3140# elif defined(WIN32)
3141# include <win32thread.h>
3142# elif defined(OS2)
3143# include "os2thread.h"
3144# elif defined(I_MACH_CTHREADS)
3145# include <mach/cthreads.h>
7f3d1cf1
BH
3146typedef cthread_t perl_os_thread;
3147typedef mutex_t perl_mutex;
3148typedef condition_t perl_cond;
3149typedef void * perl_key;
da5fdda8
AC
3150# elif defined(I_PTHREAD) /* Posix threads */
3151# include <pthread.h>
7f3d1cf1 3152typedef pthread_t perl_os_thread;
3baee7cc 3153typedef pthread_mutex_t PERL_TSA_CAPABILITY("mutex") perl_mutex;
7f3d1cf1
BH
3154typedef pthread_cond_t perl_cond;
3155typedef pthread_key_t perl_key;
da5fdda8 3156# endif
3db8f154 3157#endif /* USE_ITHREADS */
c5be433b 3158
3baee7cc
JH
3159#ifdef PERL_TSA_ACTIVE
3160/* Since most pthread mutex interfaces have not been annotated, we
3161 * need to have these wrappers. The NO_TSA annotation is quite ugly
3162 * but it cannot be avoided in plain C, unlike in C++, where one could
3163 * e.g. use ACQUIRE() with no arg on a mutex lock method.
3164 *
3165 * The bodies of these wrappers are in util.c
3166 *
3167 * TODO: however, some platforms are starting to get these clang
3168 * thread safety annotations for pthreads, for example FreeBSD.
3169 * Do we need a way to a bypass these wrappers? */
67083b7b 3170EXTERN_C int perl_tsa_mutex_lock(perl_mutex* mutex)
3baee7cc
JH
3171 PERL_TSA_ACQUIRE(*mutex)
3172 PERL_TSA_NO_TSA;
67083b7b 3173EXTERN_C int perl_tsa_mutex_unlock(perl_mutex* mutex)
3baee7cc
JH
3174 PERL_TSA_RELEASE(*mutex)
3175 PERL_TSA_NO_TSA;
3176#endif
3177
66a93824 3178#if defined(WIN32)
1feb2720 3179# include "win32.h"
c5be433b
GS
3180#endif
3181
2986a63f
JH
3182#ifdef NETWARE
3183# include "netware.h"
3184#endif
3185
e5218da5 3186#define STATUS_UNIX PL_statusvalue
68dc0745 3187#ifdef VMS
6b88bc9c 3188# define STATUS_NATIVE PL_statusvalue_vms
7a7fd8e0
JM
3189/*
3190 * vaxc$errno is only guaranteed to be valid if errno == EVMSERR, otherwise
ff7298cb 3191 * its contents can not be trusted. Unfortunately, Perl seems to check
7a7fd8e0
JM
3192 * it on exit, so it when PL_statusvalue_vms is updated, vaxc$errno should
3193 * be updated also.
3194 */
3195# include <stsdef.h>
3196# include <ssdef.h>
3197/* Presume this because if VMS changes it, it will require a new
3198 * set of APIs for waiting on children for binary compatibility.
3199 */
3200# define child_offset_bits (8)
3201# ifndef C_FAC_POSIX
3202# define C_FAC_POSIX 0x35A000
3203# endif
3204
3205/* STATUS_EXIT - validates and returns a NATIVE exit status code for the
3206 * platform from the existing UNIX or Native status values.
3207 */
3208
37038d91 3209# define STATUS_EXIT \
7a7fd8e0
JM
3210 (((I32)PL_statusvalue_vms == -1 ? SS$_ABORT : PL_statusvalue_vms) | \
3211 (VMSISH_HUSHED ? STS$M_INHIB_MSG : 0))
3212
7a7fd8e0 3213
fb38d079
JM
3214/* STATUS_NATIVE_CHILD_SET - Calculate UNIX status that matches the child
3215 * exit code and shifts the UNIX value over the correct number of bits to
3216 * be a child status. Usually the number of bits is 8, but that could be
3217 * platform dependent. The NATIVE status code is presumed to have either
3218 * from a child process.
7a7fd8e0
JM
3219 */
3220
fb38d079
JM
3221/* This is complicated. The child processes return a true native VMS
3222 status which must be saved. But there is an assumption in Perl that
3223 the UNIX child status has some relationship to errno values, so
00f254e2 3224 Perl tries to translate it to text in some of the tests.
fb38d079
JM
3225 In order to get the string translation correct, for the error, errno
3226 must be EVMSERR, but that generates a different text message
3227 than what the test programs are expecting. So an errno value must
3228 be derived from the native status value when an error occurs.
3229 That will hide the true native status message. With this version of
3230 perl, the true native child status can always be retrieved so that
3231 is not a problem. But in this case, Pl_statusvalue and errno may
3232 have different values in them.
3233 */
7a7fd8e0 3234
fb38d079 3235# define STATUS_NATIVE_CHILD_SET(n) \
68dc0745 3236 STMT_START { \
2fbb330f
JM
3237 I32 evalue = (I32)n; \
3238 if (evalue == EVMSERR) { \
3239 PL_statusvalue_vms = vaxc$errno; \
3240 PL_statusvalue = evalue; \
7a7fd8e0 3241 } else { \
2fbb330f 3242 PL_statusvalue_vms = evalue; \
fb38d079 3243 if (evalue == -1) { \
3280af22 3244 PL_statusvalue = -1; \
7a7fd8e0
JM
3245 PL_statusvalue_vms = SS$_ABORT; /* Should not happen */ \
3246 } else \
fb38d079 3247 PL_statusvalue = Perl_vms_status_to_unix(evalue, 1); \
2fbb330f 3248 set_vaxc_errno(evalue); \
fb38d079
JM
3249 if ((PL_statusvalue_vms & C_FAC_POSIX) == C_FAC_POSIX) \
3250 set_errno(EVMSERR); \
3251 else set_errno(Perl_vms_status_to_unix(evalue, 0)); \
3252 PL_statusvalue = PL_statusvalue << child_offset_bits; \
2fbb330f 3253 } \
68dc0745 3254 } STMT_END
7a7fd8e0 3255
68dc0745 3256# ifdef VMSISH_STATUS
e5218da5 3257# define STATUS_CURRENT (VMSISH_STATUS ? STATUS_NATIVE : STATUS_UNIX)
68dc0745 3258# else
e5218da5 3259# define STATUS_CURRENT STATUS_UNIX
68dc0745 3260# endif
7a7fd8e0
JM
3261
3262 /* STATUS_UNIX_SET - takes a UNIX/POSIX errno value and attempts to update
3263 * the NATIVE status to an equivalent value. Can not be used to translate
3264 * exit code values as exit code values are not guaranteed to have any
3265 * relationship at all to errno values.
3266 * This is used when Perl is forcing errno to have a specific value.
3267 */
e5218da5 3268# define STATUS_UNIX_SET(n) \
68dc0745 3269 STMT_START { \
7a7fd8e0 3270 I32 evalue = (I32)n; \
0968cdad 3271 PL_statusvalue = evalue; \
3280af22 3272 if (PL_statusvalue != -1) { \
0968cdad
JM
3273 if (PL_statusvalue != EVMSERR) { \
3274 PL_statusvalue &= 0xFFFF; \
3275 if (MY_POSIX_EXIT) \
3276 PL_statusvalue_vms=PL_statusvalue ? SS$_ABORT : SS$_NORMAL;\
3277 else PL_statusvalue_vms = Perl_unix_status_to_vms(evalue); \
3278 } \
3279 else { \
3280 PL_statusvalue_vms = vaxc$errno; \
3281 } \
68dc0745 3282 } \
0968cdad
JM
3283 else PL_statusvalue_vms = SS$_ABORT; \
3284 set_vaxc_errno(PL_statusvalue_vms); \
7a7fd8e0
JM
3285 } STMT_END
3286
3287 /* STATUS_UNIX_EXIT_SET - Takes a UNIX/POSIX exit code and sets
e08e1e1d
JM
3288 * the NATIVE error status based on it.
3289 *
3290 * When in the default mode to comply with the Perl VMS documentation,
3291 * 0 is a success and any other code sets the NATIVE status to a failure
3292 * code of SS$_ABORT.
fb38d079
JM
3293 *
3294 * In the new POSIX EXIT mode, native status will be set so that the
3295 * actual exit code will can be retrieved by the calling program or
3296 * shell.
3297 *
3298 * If the exit code is not clearly a UNIX parent or child exit status,
3299 * it will be passed through as a VMS status.
7a7fd8e0
JM
3300 */
3301
fb38d079 3302# define STATUS_UNIX_EXIT_SET(n) \
7a7fd8e0
JM
3303 STMT_START { \
3304 I32 evalue = (I32)n; \
3305 PL_statusvalue = evalue; \
e08e1e1d
JM
3306 if (MY_POSIX_EXIT) { \
3307 if (evalue <= 0xFF00) { \
3308 if (evalue > 0xFF) \
3309 evalue = (evalue >> child_offset_bits) & 0xFF; \
3310 PL_statusvalue_vms = \
3311 (C_FAC_POSIX | (evalue << 3 ) | \
3312 ((evalue == 1) ? (STS$K_ERROR | STS$M_INHIB_MSG) : 1)); \
3313 } else /* forgive them Perl, for they have sinned */ \
3314 PL_statusvalue_vms = evalue; \
3315 } else { \
3316 if (evalue == 0) \
3317 PL_statusvalue_vms = SS$_NORMAL; \
3318 else if (evalue <= 0xFF00) \
3319 PL_statusvalue_vms = SS$_ABORT; \
3320 else { /* forgive them Perl, for they have sinned */ \
3321 if (evalue != EVMSERR) PL_statusvalue_vms = evalue; \
3322 else PL_statusvalue_vms = vaxc$errno; \
3323 /* And obviously used a VMS status value instead of UNIX */ \
3324 PL_statusvalue = EVMSERR; \
3325 } \
3326 set_vaxc_errno(PL_statusvalue_vms); \
3327 } \
68dc0745 3328 } STMT_END
fb38d079 3329
e08e1e1d 3330
6ac6a52b
JM
3331 /* STATUS_EXIT_SET - Takes a NATIVE/UNIX/POSIX exit code
3332 * and sets the NATIVE error status based on it. This special case
3333 * is needed to maintain compatibility with past VMS behavior.
3334 *
3335 * In the default mode on VMS, this number is passed through as
3336 * both the NATIVE and UNIX status. Which makes it different
3337 * that the STATUS_UNIX_EXIT_SET.
3338 *
3339 * In the new POSIX EXIT mode, native status will be set so that the
3340 * actual exit code will can be retrieved by the calling program or
3341 * shell.
3342 *
1a3aec58
JM
3343 * A POSIX exit code is from 0 to 255. If the exit code is higher
3344 * than this, it needs to be assumed that it is a VMS exit code and
3345 * passed through.
6ac6a52b
JM
3346 */
3347
3348# define STATUS_EXIT_SET(n) \
3349 STMT_START { \
3350 I32 evalue = (I32)n; \
3351 PL_statusvalue = evalue; \
3352 if (MY_POSIX_EXIT) \
1a3aec58
JM
3353 if (evalue > 255) PL_statusvalue_vms = evalue; else { \
3354 PL_statusvalue_vms = \
3355 (C_FAC_POSIX | (evalue << 3 ) | \
3356 ((evalue == 1) ? (STS$K_ERROR | STS$M_INHIB_MSG) : 1));} \
6ac6a52b
JM
3357 else \
3358 PL_statusvalue_vms = evalue ? evalue : SS$_NORMAL; \
3359 set_vaxc_errno(PL_statusvalue_vms); \
3360 } STMT_END
3361
fb38d079
JM
3362
3363 /* This macro forces a success status */
7a7fd8e0
JM
3364# define STATUS_ALL_SUCCESS \
3365 (PL_statusvalue = 0, PL_statusvalue_vms = SS$_NORMAL)
fb38d079
JM
3366
3367 /* This macro forces a failure status */
7a7fd8e0
JM
3368# define STATUS_ALL_FAILURE (PL_statusvalue = 1, \
3369 vaxc$errno = PL_statusvalue_vms = MY_POSIX_EXIT ? \
3370 (C_FAC_POSIX | (1 << 3) | STS$K_ERROR | STS$M_INHIB_MSG) : SS$_ABORT)
fb38d079 3371
738ab09f
AB
3372#elif defined(__amigaos4__)
3373 /* A somewhat experimental attempt to simulate posix return code values */
3374# define STATUS_NATIVE PL_statusvalue_posix
3375# define STATUS_NATIVE_CHILD_SET(n) \
3376 STMT_START { \
3377 PL_statusvalue_posix = (n); \
3378 if (PL_statusvalue_posix < 0) { \
3379 PL_statusvalue = -1; \
3380 } \
3381 else { \
3382 PL_statusvalue = n << 8; \
3383 } \
3384 } STMT_END
3385# define STATUS_UNIX_SET(n) \
3386 STMT_START { \
3387 PL_statusvalue = (n); \
3388 if (PL_statusvalue != -1) \
3389 PL_statusvalue &= 0xFFFF; \
3390 } STMT_END
3391# define STATUS_UNIX_EXIT_SET(n) STATUS_UNIX_SET(n)
3392# define STATUS_EXIT_SET(n) STATUS_UNIX_SET(n)
3393# define STATUS_CURRENT STATUS_UNIX
3394# define STATUS_EXIT STATUS_UNIX
3395# define STATUS_ALL_SUCCESS (PL_statusvalue = 0, PL_statusvalue_posix = 0)
3396# define STATUS_ALL_FAILURE (PL_statusvalue = 1, PL_statusvalue_posix = 1)
3397
68dc0745 3398#else
e5218da5 3399# define STATUS_NATIVE PL_statusvalue_posix
e5218da5 3400# if defined(WCOREDUMP)
37038d91 3401# define STATUS_NATIVE_CHILD_SET(n) \
e5218da5
GA
3402 STMT_START { \
3403 PL_statusvalue_posix = (n); \
3404 if (PL_statusvalue_posix == -1) \
3405 PL_statusvalue = -1; \
3406 else { \
3407 PL_statusvalue = \
3408 (WIFEXITED(PL_statusvalue_posix) ? (WEXITSTATUS(PL_statusvalue_posix) << 8) : 0) | \
3409 (WIFSIGNALED(PL_statusvalue_posix) ? (WTERMSIG(PL_statusvalue_posix) & 0x7F) : 0) | \
3410 (WIFSIGNALED(PL_statusvalue_posix) && WCOREDUMP(PL_statusvalue_posix) ? 0x80 : 0); \
3411 } \
3412 } STMT_END
3413# elif defined(WIFEXITED)
37038d91 3414# define STATUS_NATIVE_CHILD_SET(n) \
e5218da5
GA
3415 STMT_START { \
3416 PL_statusvalue_posix = (n); \
3417 if (PL_statusvalue_posix == -1) \
3418 PL_statusvalue = -1; \
3419 else { \
3420 PL_statusvalue = \
3421 (WIFEXITED(PL_statusvalue_posix) ? (WEXITSTATUS(PL_statusvalue_posix) << 8) : 0) | \
3422 (WIFSIGNALED(PL_statusvalue_posix) ? (WTERMSIG(PL_statusvalue_posix) & 0x7F) : 0); \
3423 } \
3424 } STMT_END
3425# else
37038d91 3426# define STATUS_NATIVE_CHILD_SET(n) \
e5218da5
GA
3427 STMT_START { \
3428 PL_statusvalue_posix = (n); \
3429 if (PL_statusvalue_posix == -1) \
3430 PL_statusvalue = -1; \
3431 else { \
3432 PL_statusvalue = \
3433 PL_statusvalue_posix & 0xFFFF; \
3434 } \
3435 } STMT_END
3436# endif
3437# define STATUS_UNIX_SET(n) \
68dc0745 3438 STMT_START { \
3280af22
NIS
3439 PL_statusvalue = (n); \
3440 if (PL_statusvalue != -1) \
3441 PL_statusvalue &= 0xFFFF; \
68dc0745 3442 } STMT_END
7a7fd8e0 3443# define STATUS_UNIX_EXIT_SET(n) STATUS_UNIX_SET(n)
6ac6a52b 3444# define STATUS_EXIT_SET(n) STATUS_UNIX_SET(n)
e5218da5 3445# define STATUS_CURRENT STATUS_UNIX
37038d91 3446# define STATUS_EXIT STATUS_UNIX
e5218da5
GA
3447# define STATUS_ALL_SUCCESS (PL_statusvalue = 0, PL_statusvalue_posix = 0)
3448# define STATUS_ALL_FAILURE (PL_statusvalue = 1, PL_statusvalue_posix = 1)
68dc0745
PP
3449#endif
3450
cc3604b1
GS
3451/* flags in PL_exit_flags for nature of exit() */
3452#define PERL_EXIT_EXPECTED 0x01
31d77e54 3453#define PERL_EXIT_DESTRUCT_END 0x02 /* Run END in perl_destruct */
6136213b
JGM
3454#define PERL_EXIT_WARN 0x04 /* Warn if Perl_my_exit() or Perl_my_failure_exit() called */
3455#define PERL_EXIT_ABORT 0x08 /* Call abort() if Perl_my_exit() or Perl_my_failure_exit() called */
cc3604b1 3456
b79b76e0 3457#ifndef PERL_CORE
a7cb1f99 3458/* format to use for version numbers in file/directory names */
273cf8d1 3459/* XXX move to Configure? */
b79b76e0
NC
3460/* This was only ever used for the current version, and that can be done at
3461 compile time, as PERL_FS_VERSION, so should we just delete it? */
3462# ifndef PERL_FS_VER_FMT
3463# define PERL_FS_VER_FMT "%d.%d.%d"
3464# endif
3465#endif
3466
3467#ifndef PERL_FS_VERSION
3468# define PERL_FS_VERSION PERL_VERSION_STRING
0b94c7bb
GS
3469#endif
3470
45bc9206 3471/* This defines a way to flush all output buffers. This may be a
76549fef
JH
3472 * performance issue, so we allow people to disable it. Also, if
3473 * we are using stdio, there are broken implementations of fflush(NULL)
3474 * out there, Solaris being the most prominent.
45bc9206
GS
3475 */
3476#ifndef PERL_FLUSHALL_FOR_CHILD
97cb92d6 3477# if defined(USE_PERLIO) || defined(FFLUSH_NULL)
66fe083f 3478# define PERL_FLUSHALL_FOR_CHILD PerlIO_flush((PerlIO*)NULL)
da5fdda8
AC
3479# elif defined(FFLUSH_ALL)
3480# define PERL_FLUSHALL_FOR_CHILD my_fflush_all()
767df6a1 3481# else
da5fdda8 3482# define PERL_FLUSHALL_FOR_CHILD NOOP
66fe083f 3483# endif
45bc9206
GS
3484#endif
3485
7766f137
GS
3486#ifndef PERL_WAIT_FOR_CHILDREN
3487# define PERL_WAIT_FOR_CHILDREN NOOP
3488#endif
3489
ba869deb 3490/* the traditional thread-unsafe notion of "current interpreter". */
c5be433b
GS
3491#ifndef PERL_SET_INTERP
3492# define PERL_SET_INTERP(i) (PL_curinterp = (PerlInterpreter*)(i))
3493#endif
3494
3495#ifndef PERL_GET_INTERP
3496# define PERL_GET_INTERP (PL_curinterp)
3497#endif
3498
54aff467 3499#if defined(PERL_IMPLICIT_CONTEXT) && !defined(PERL_GET_THX)
54aff467 3500# ifdef MULTIPLICITY
ba869deb 3501# define PERL_GET_THX ((PerlInterpreter *)PERL_GET_CONTEXT)
54aff467 3502# endif
ba869deb
GS
3503# define PERL_SET_THX(t) PERL_SET_CONTEXT(t)
3504#endif
3505
00f254e2 3506/*
8896765a 3507 This replaces the previous %_ "hack" by the "%p" hacks.
0dbb1585 3508 All that is required is that the perl source does not
00f254e2
KW
3509 use "%-p" or "%-<number>p" or "%<number>p" formats.
3510 These formats will still work in perl code.
486ec47a 3511 See comments in sv.c for further details.
8896765a 3512
8896765a 3513 Robin Barker 2005-07-14
f46d31f2 3514
00f254e2 3515 No longer use %1p for VDf = %vd. RMB 2007-10-19
0dbb1585 3516*/
8896765a
RB
3517
3518#ifndef SVf_
63ce7bf0 3519# define SVf_(n) "-" STRINGIFY(n) "p"
894356b3
GS
3520#endif
3521
8896765a 3522#ifndef SVf
104abab4 3523# define SVf "-p"
d2560b70
RB
3524#endif
3525
014ead4b 3526#ifndef SVf32
8896765a 3527# define SVf32 SVf_(32)
014ead4b
RB
3528#endif
3529
3530#ifndef SVf256
8896765a
RB
3531# define SVf256 SVf_(256)
3532#endif
3533
be2597df
MHM
3534#define SVfARG(p) ((void*)(p))
3535
20023040
FC
3536#ifndef HEKf
3537# define HEKf "2p"
3538#endif
3539
b8fa5213
FC
3540/* Not ideal, but we cannot easily include a number in an already-numeric
3541 * format sequence. */
3542#ifndef HEKf256
3543# define HEKf256 "3p"
3544#endif
3545
20023040
FC
3546#define HEKfARG(p) ((void*)(p))
3547
eafff229
KW
3548/*
3549=for apidoc Amnh||UTF8f
3550=for apidoc Amh||UTF8fARG|bool is_utf8|Size_t byte_len|char *str
3551
3552=cut
3553 * %4p is a custom format
3554 */
b17a0679 3555#ifndef UTF8f
61608bb7 3556# define UTF8f "d%" UVuf "%4p"
b17a0679
FC
3557#endif
3558#define UTF8fARG(u,l,p) (int)cBOOL(u), (UV)(l), (void*)(p)
3559
0f94cb1f
FC
3560#define PNf UTF8f
3561#define PNfARG(pn) (int)1, (UV)PadnameLEN(pn), (void *)PadnamePV(pn)
0d1e9135 3562
f46d31f2 3563#ifdef PERL_CORE
486ec47a 3564/* not used; but needed for backward compatibility with XS code? - RMB */
7776bb98 3565# undef UVf
da5fdda8
AC
3566#elif !defined(UVf)
3567# define UVf UVuf
d2560b70
RB
3568#endif
3569
6ff2ec7d
AC
3570#if !defined(DEBUGGING) && !defined(NDEBUG)
3571# define NDEBUG 1
cd13e623 3572#endif
6ff2ec7d 3573#include <assert.h>
cd13e623 3574
0dbb1585
AL
3575/* For functions that are marked as __attribute__noreturn__, it's not
3576 appropriate to call return. In either case, include the lint directive.
3577 */
3578#ifdef HASATTRIBUTE_NORETURN
bc3d2941 3579# define NORETURN_FUNCTION_END NOT_REACHED;
0dbb1585 3580#else
bc3d2941 3581# define NORETURN_FUNCTION_END NOT_REACHED; return 0
abb2c242
JH
3582#endif
3583
635aebb7 3584#ifdef HAS_BUILTIN_EXPECT
b37c2d43
AL
3585# define EXPECT(expr,val) __builtin_expect(expr,val)
3586#else
3587# define EXPECT(expr,val) (expr)
3588#endif
c515c80f
KW
3589
3590/*
3591=head1 Miscellaneous Functions
3592
3593=for apidoc AmU|bool|LIKELY|const bool expr
3594
3595Returns the input unchanged, but at the same time it gives a branch prediction
3596hint to the compiler that this condition is likely to be true.
3597
3598=for apidoc AmU|bool|UNLIKELY|const bool expr
3599
3600Returns the input unchanged, but at the same time it gives a branch prediction
3601hint to the compiler that this condition is likely to be false.
3602
3603=cut
3604*/
6d5abc62
NC
3605#define LIKELY(cond) EXPECT(cBOOL(cond),TRUE)
3606#define UNLIKELY(cond) EXPECT(cBOOL(cond),FALSE)
c515c80f 3607
635aebb7
AL
3608#ifdef HAS_BUILTIN_CHOOSE_EXPR
3609/* placeholder */
3610#endif
b37c2d43 3611
a36ca6de 3612/* STATIC_ASSERT_DECL/STATIC_ASSERT_STMT are like assert(), but for compile
5074d4c5
LM
3613 time invariants. That is, their argument must be a constant expression that
3614 can be verified by the compiler. This expression can contain anything that's
3615 known to the compiler, e.g. #define constants, enums, or sizeof (...). If
3616 the expression evaluates to 0, compilation fails.
3617 Because they generate no runtime code (i.e. their use is "free"), they're
3618 always active, even under non-DEBUGGING builds.
a36ca6de 3619 STATIC_ASSERT_DECL expands to a declaration and is suitable for use at
5074d4c5
LM
3620 file scope (outside of any function).
3621 STATIC_ASSERT_STMT expands to a statement and is suitable for use inside a
3622 function.
3623*/
f223d81e
KW
3624#if (! defined(__IBMC__) || __IBMC__ >= 1210) \
3625 && (( defined(static_assert) && ( defined(_ISOC11_SOURCE) \
3626 || (__STDC_VERSION__ - 0) >= 201101L)) \
3627 || (defined(__cplusplus) && __cplusplus >= 201103L))
3628/* XXX static_assert is a macro defined in <assert.h> in C11 or a compiler
a36ca6de
LM
3629 builtin in C++11. But IBM XL C V11 does not support _Static_assert, no
3630 matter what <assert.h> says.
6d59e610 3631*/
a36ca6de 3632# define STATIC_ASSERT_DECL(COND) static_assert(COND, #COND)
6d59e610
LM
3633#else
3634/* We use a bit-field instead of an array because gcc accepts
3635 'typedef char x[n]' where n is not a compile-time constant.
3636 We want to enforce constantness.
3637*/
3638# define STATIC_ASSERT_2(COND, SUFFIX) \
3639 typedef struct { \
3640 unsigned int _static_assertion_failed_##SUFFIX : (COND) ? 1 : -1; \
3641 } _static_assertion_failed_##SUFFIX PERL_UNUSED_DECL
3642# define STATIC_ASSERT_1(COND, SUFFIX) STATIC_ASSERT_2(COND, SUFFIX)
a36ca6de 3643# define STATIC_ASSERT_DECL(COND) STATIC_ASSERT_1(COND, __LINE__)
6d59e610
LM
3644#endif
3645/* We need this wrapper even in C11 because 'case X: static_assert(...);' is an
3646 error (static_assert is a declaration, and only statements can have labels).
3647*/
21aae866 3648#define STATIC_ASSERT_STMT(COND) STMT_START { STATIC_ASSERT_DECL(COND); } STMT_END
ae103e09
DD
3649
3650#ifndef __has_builtin
3651# define __has_builtin(x) 0 /* not a clang style compiler */
3652#endif
3653
3654/* ASSUME is like assert(), but it has a benefit in a release build. It is a
3655 hint to a compiler about a statement of fact in a function call free
3656 expression, which allows the compiler to generate better machine code.
3657 In a debug build, ASSUME(x) is a synonym for assert(x). ASSUME(0) means
3658 the control path is unreachable. In a for loop, ASSUME can be used to hint
b8fda935 3659 that a loop will run at least X times. ASSUME is based off MSVC's __assume
ae103e09
DD
3660 intrinsic function, see its documents for more details.
3661*/
3662
3663#ifndef DEBUGGING
3664# if __has_builtin(__builtin_unreachable) \
d1020b40 3665 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5 || __GNUC__ > 4) /* 4.5 -> */
ae103e09
DD
3666# define ASSUME(x) ((x) ? (void) 0 : __builtin_unreachable())
3667# elif defined(_MSC_VER)
3668# define ASSUME(x) __assume(x)
3669# elif defined(__ARMCC_VERSION) /* untested */
3670# define ASSUME(x) __promise(x)
3671# else
3672/* a random compiler might define assert to its own special optimization token
3673 so pass it through to C lib as a last resort */
3674# define ASSUME(x) assert(x)
3675# endif
3676#else
3677# define ASSUME(x) assert(x)
3678#endif
3679
dbc47c6f
KW
3680#if defined(__sun) /* ASSUME() generates warnings on Solaris */
3681# define NOT_REACHED
2145f4b6
LM
3682#elif defined(DEBUGGING) && (__has_builtin(__builtin_unreachable) \
3683 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 5 || __GNUC__ > 4)) /* 4.5 -> */
41bd5b11 3684# define NOT_REACHED STMT_START { ASSUME(!"UNREACHABLE"); __builtin_unreachable(); } STMT_END
dbc47c6f 3685#else
41bd5b11 3686# define NOT_REACHED ASSUME(!"UNREACHABLE")
dbc47c6f 3687#endif
ae103e09 3688
3fc1aec6
PP
3689/* Some unistd.h's give a prototype for pause() even though
3690 HAS_PAUSE ends up undefined. This causes the #define
d2560b70 3691 below to be rejected by the compiler. Sigh.
3fc1aec6
PP
3692*/
3693#ifdef HAS_PAUSE
3694#define Pause pause
3695#else
3696#define Pause() sleep((32767<<16)+32767)
748a9306
LW
3697#endif
3698
3699#ifndef IOCPARM_LEN
3700# ifdef IOCPARM_MASK
61f1c44b 3701 /* on BSDish systems we're safe */
748a9306 3702# define IOCPARM_LEN(x) (((x) >> 16) & IOCPARM_MASK)
da5fdda8 3703# elif defined(_IOC_SIZE) && defined(__GLIBC__)
301eb2f0 3704 /* on Linux systems we're safe; except when we're not [perl #38223] */
da5fdda8
AC
3705# define IOCPARM_LEN(x) (_IOC_SIZE(x) < 256 ? 256 : _IOC_SIZE(x))
3706# else
748a9306 3707 /* otherwise guess at what's safe */
da5fdda8 3708# define IOCPARM_LEN(x) 256
748a9306 3709# endif
a0d0e21e
LW
3710#endif
3711
1761cee5 3712#if defined(__CYGWIN__)
521e0776
GS
3713/* USEMYBINMODE
3714 * This symbol, if defined, indicates that the program should
16fe6d59 3715 * use the routine my_binmode(FILE *fp, char iotype, int mode) to insure
521e0776
GS
3716 * that a file is in "binary" mode -- that is, that no translation
3717 * of bytes occurs on read or write operations.
3718 */
36894f0b 3719# define USEMYBINMODE /**/
77a40698 3720# include <io.h> /* for setmode() prototype */
16fe6d59 3721# define my_binmode(fp, iotype, mode) \
8298454c 3722 cBOOL(PerlLIO_setmode(fileno(fp), mode) != -1)
5aabfad6
PP
3723#endif
3724
88d10794
JH
3725#ifdef __CYGWIN__
3726void init_os_extras(void);
3727#endif
3728
cea2e8a9
GS
3729#ifdef UNION_ANY_DEFINITION
3730UNION_ANY_DEFINITION;
3731#else
3732union any {
3733 void* any_ptr;
c9728782
DM
3734 SV* any_sv;
3735 SV** any_svp;
3736 GV* any_gv;
3737 AV* any_av;
3738 HV* any_hv;
6ffb8402 3739 OP* any_op;
c9728782
DM
3740 char* any_pv;
3741 char** any_pvp;
cea2e8a9 3742 I32 any_i32;
ae103e09 3743 U32 any_u32;
cea2e8a9 3744 IV any_iv;
c6bf6a65 3745 UV any_uv;
cea2e8a9 3746 long any_long;
9febdf04 3747 bool any_bool;
c76ac1ee 3748 void (*any_dptr) (void*);
acfe0abc 3749 void (*any_dxptr) (pTHX_ void*);
cea2e8a9
GS
3750};
3751#endif
3752
acfe0abc 3753typedef I32 (*filter_t) (pTHX_ int, SV *, int);
cea2e8a9
GS
3754
3755#define FILTER_READ(idx, sv, len) filter_read(idx, sv, len)
5486870f
DM
3756#define FILTER_DATA(idx) \
3757 (PL_parser ? AvARRAY(PL_parser->rsfp_filters)[idx] : NULL)
3758#define FILTER_ISREADER(idx) \
3759 (PL_parser && PL_parser->rsfp_filters \
3760 && idx >= AvFILLp(PL_parser->rsfp_filters))
3761#define PERL_FILTER_EXISTS(i) \
3762 (PL_parser && PL_parser->rsfp_filters \
b9f2b683 3763 && (i) <= av_tindex(PL_parser->rsfp_filters))