Update from y2038
[p5sagit/p5-mst-13.2.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
663f364b 4 * 2000, 2001, 2002, 2004, 2005, 2006, 2007, by Larry Wall and others
a687059c 5 *
6e21c824 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 */
10
85e6fe83 11#if !defined(__STDC__)
378cc40b 12#ifdef NULL
13#undef NULL
14#endif
a687059c 15#ifndef I286
16# define NULL 0
17#else
18# define NULL 0L
19#endif
85e6fe83 20#endif
21
24792b8d 22#ifndef PERL_CORE
23# define Null(type) ((type)NULL)
954c1994 24
25/*
ccfc67b7 26=head1 Handy Values
954c1994 27
3609ea0d 28=for apidoc AmU||Nullch
24792b8d 29Null character pointer. (No longer available when C<PERL_CORE> is defined.)
2307c6d0 30
954c1994 31=for apidoc AmU||Nullsv
24792b8d 32Null SV pointer. (No longer available when C<PERL_CORE> is defined.)
954c1994 33
34=cut
35*/
36
24792b8d 37# define Nullch Null(char*)
38# define Nullfp Null(PerlIO*)
39# define Nullsv Null(SV*)
40#endif
8d063cd8 41
641d3f0b 42#ifdef TRUE
43#undef TRUE
44#endif
45#ifdef FALSE
46#undef FALSE
47#endif
48#define TRUE (1)
49#define FALSE (0)
50
27d4fb96 51
52/* XXX Configure ought to have a test for a boolean type, if I can
53 just figure out all the headers such a test needs.
54 Andy Dougherty August 1996
55*/
8e84507e 56/* bool is built-in for g++-2.6.3 and later, which might be used
c1d22f6b 57 for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
58 be sure _G_config.h will be included before this file. _G_config.h
8e84507e 59 also defines _G_HAVE_BOOL for both gcc and g++, but only g++
c1d22f6b 60 actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
61 g++ can be identified by __GNUG__.
62 Andy Dougherty February 2000
5d94fbed 63*/
3609ea0d 64#ifdef __GNUG__ /* GNU g++ has bool built-in */
5d94fbed 65# ifndef HAS_BOOL
c1d22f6b 66# define HAS_BOOL 1
5d94fbed 67# endif
5d94fbed 68#endif
69
641d3f0b 70/* The NeXT dynamic loader headers will not build with the bool macro
71 So declare them now to clear confusion.
72*/
8f1f23e8 73#if defined(NeXT) || defined(__NeXT__)
641d3f0b 74# undef FALSE
75# undef TRUE
76 typedef enum bool { FALSE = 0, TRUE = 1 } bool;
77# define ENUM_BOOL 1
78# ifndef HAS_BOOL
79# define HAS_BOOL 1
80# endif /* !HAS_BOOL */
8f1f23e8 81#endif /* NeXT || __NeXT__ */
641d3f0b 82
5d94fbed 83#ifndef HAS_BOOL
61bb5906 84# if defined(UTS) || defined(VMS)
5d94fbed 85# define bool int
86# else
87# define bool char
88# endif
c1d22f6b 89# define HAS_BOOL 1
a687059c 90#endif
0d3e774c 91
46c6c7e2 92/* Try to figure out __func__ or __FUNCTION__ equivalent, if any.
e352bcff 93 * XXX Should really be a Configure probe, with HAS__FUNCTION__
94 * and FUNCTION__ as results.
95 * XXX Similarly, a Configure probe for __FILE__ and __LINE__ is needed. */
46c6c7e2 96#if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) || (defined(__SUNPRO_C)) /* C99 or close enough. */
97# define FUNCTION__ __func__
98#else
99# if (defined(_MSC_VER) && _MSC_VER < 1300) || /* Pre-MSVC 7.0 has neither __func__ nor __FUNCTION and no good workarounds, either. */ \
be73ebb0 100 (defined(__DECC_VER)) /* Tru64 or VMS, and strict C89 being used, but not modern enough cc (in Tur64, -c99 not known, only -std1). */
46c6c7e2 101# define FUNCTION__ ""
102# else
103# define FUNCTION__ __FUNCTION__ /* Common extension. */
104# endif
105#endif
106
27d4fb96 107/* XXX A note on the perl source internal type system. The
108 original intent was that I32 be *exactly* 32 bits.
109
110 Currently, we only guarantee that I32 is *at least* 32 bits.
111 Specifically, if int is 64 bits, then so is I32. (This is the case
112 for the Cray.) This has the advantage of meshing nicely with
113 standard library calls (where we pass an I32 and the library is
114 expecting an int), but the disadvantage that an I32 is not 32 bits.
115 Andy Dougherty August 1996
24fef2a7 116
dc45a647 117 There is no guarantee that there is *any* integral type with
118 exactly 32 bits. It is perfectly legal for a system to have
119 sizeof(short) == sizeof(int) == sizeof(long) == 8.
693762b4 120
dc45a647 121 Similarly, there is no guarantee that I16 and U16 have exactly 16
122 bits.
693762b4 123
8e84507e 124 For dealing with issues that may arise from various 32/64-bit
125 systems, we will ask Configure to check out
8175356b 126
3609ea0d 127 SHORTSIZE == sizeof(short)
128 INTSIZE == sizeof(int)
129 LONGSIZE == sizeof(long)
dc45a647 130 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
3609ea0d 131 PTRSIZE == sizeof(void *)
dc45a647 132 DOUBLESIZE == sizeof(double)
133 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
8175356b 134
27d4fb96 135*/
136
69512466 137#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
138# include <inttypes.h>
dd0eed91 139# ifdef INT32_MIN_BROKEN
140# undef INT32_MIN
141# define INT32_MIN (-2147483647-1)
142# endif
143# ifdef INT64_MIN_BROKEN
144# undef INT64_MIN
145# define INT64_MIN (-9223372036854775807LL-1)
146# endif
69512466 147#endif
148
8175356b 149typedef I8TYPE I8;
150typedef U8TYPE U8;
151typedef I16TYPE I16;
152typedef U16TYPE U16;
153typedef I32TYPE I32;
154typedef U32TYPE U32;
6b8eaf93 155#ifdef PERL_CORE
156# ifdef HAS_QUAD
8175356b 157typedef I64TYPE I64;
158typedef U64TYPE U64;
6b8eaf93 159# endif
160#endif /* PERL_CORE */
8175356b 161
69512466 162#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
163# ifndef UINT64_C /* usually from <inttypes.h> */
164# if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
165# define INT64_C(c) CAT2(c,LL)
166# define UINT64_C(c) CAT2(c,ULL)
167# else
168# if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
169# define INT64_C(c) CAT2(c,L)
170# define UINT64_C(c) CAT2(c,UL)
171# else
172# define INT64_C(c) ((I64TYPE)(c))
173# define UINT64_C(c) ((U64TYPE)(c))
174# endif
175# endif
e8c95190 176# endif
177#endif
dc750f2e 178
cae3d67c 179/* HMB H.Merijn Brand - a placeholder for preparing Configure patches */
cbb9e8a7 180#if defined(LOCALTIME_R_NEEDS_TZSET) && defined(HAS_PSEUDOFORK) && defined(USE_DTRACE) && defined(GMTIME_MAX) && defined(GMTIME_MIN) && defined(HAS_TIMEGM)
cae3d67c 181/* Not (yet) used at top level, but mention them for metaconfig */
dc750f2e 182#endif
e8c95190 183
a22e52b9 184/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
185 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
186
d8668976 187#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
5ff3f7a4 188
5ff3f7a4 189/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
190 Please search CHAR_MAX in perl.h for further details. */
191#define U8_MAX UINT8_MAX
192#define U8_MIN UINT8_MIN
193
5ff3f7a4 194#define I16_MAX INT16_MAX
195#define I16_MIN INT16_MIN
196#define U16_MAX UINT16_MAX
197#define U16_MIN UINT16_MIN
198
5ff3f7a4 199#define I32_MAX INT32_MAX
200#define I32_MIN INT32_MIN
0e983133 201#ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
202# define U32_MAX UINT32_MAX
203#else
204# define U32_MAX 4294967295U
205#endif
5ff3f7a4 206#define U32_MIN UINT32_MIN
207
208#else
209
5c9fa16e 210/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
211 Please search CHAR_MAX in perl.h for further details. */
27d4fb96 212#define U8_MAX PERL_UCHAR_MAX
213#define U8_MIN PERL_UCHAR_MIN
79072805 214
27d4fb96 215#define I16_MAX PERL_SHORT_MAX
216#define I16_MIN PERL_SHORT_MIN
217#define U16_MAX PERL_USHORT_MAX
218#define U16_MIN PERL_USHORT_MIN
79072805 219
c4f23d77 220#if LONGSIZE > 4
27d4fb96 221# define I32_MAX PERL_INT_MAX
222# define I32_MIN PERL_INT_MIN
223# define U32_MAX PERL_UINT_MAX
224# define U32_MIN PERL_UINT_MIN
79072805 225#else
27d4fb96 226# define I32_MAX PERL_LONG_MAX
227# define I32_MIN PERL_LONG_MIN
228# define U32_MAX PERL_ULONG_MAX
229# define U32_MIN PERL_ULONG_MIN
79072805 230#endif
231
5ff3f7a4 232#endif
233
58a9a5d5 234/* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
fc36a67e 235#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
236#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
237#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
238
ff68c719 239#define Ctl(ch) ((ch) & 037)
8d063cd8 240
3fe05580 241/*
242=head1 SV-Body Allocation
243
244=for apidoc Ama|SV*|newSVpvs|const char* s
245Like C<newSVpvn>, but takes a literal string instead of a string/length pair.
246
84bafc02 247=for apidoc Ama|SV*|newSVpvs_flags|const char* s|U32 flags
248Like C<newSVpvn_flags>, but takes a literal string instead of a string/length
249pair.
250
3fe05580 251=for apidoc Ama|SV*|newSVpvs_share|const char* s
252Like C<newSVpvn_share>, but takes a literal string instead of a string/length
253pair and omits the hash parameter.
254
7b4e7469 255=for apidoc Am|void|sv_catpvs|SV* sv|const char* s
3fe05580 256Like C<sv_catpvn>, but takes a literal string instead of a string/length pair.
257
7b4e7469 258=for apidoc Am|void|sv_setpvs|SV* sv|const char* s
3fe05580 259Like C<sv_setpvn>, but takes a literal string instead of a string/length pair.
260
261=head1 Memory Management
262
263=for apidoc Ama|char*|savepvs|const char* s
264Like C<savepvn>, but takes a literal string instead of a string/length pair.
265
266=head1 GV Functions
267
268=for apidoc Am|HV*|gv_stashpvs|const char* name|I32 create
269Like C<gv_stashpvn>, but takes a literal string instead of a string/length pair.
270
271=head1 Hash Manipulation Functions
272
273=for apidoc Am|SV**|hv_fetchs|HV* tb|const char* key|I32 lval
274Like C<hv_fetch>, but takes a literal string instead of a string/length pair.
275
276=for apidoc Am|SV**|hv_stores|HV* tb|const char* key|NULLOK SV* val
277Like C<hv_store>, but takes a literal string instead of a string/length pair
278and omits the hash parameter.
279
280=cut
281*/
282
65177ae0 283/* concatenating with "" ensures that only literal strings are accepted as argument */
2efa8cc7 284#define STR_WITH_LEN(s) (s ""), (sizeof(s)-1)
285
ba3a79e7 286/* note that STR_WITH_LEN() can't be used as argument to macros or functions that
287 * under some configurations might be macros, which means that it requires the full
288 * Perl_xxx(aTHX_ ...) form for any API calls where it's used.
289 */
290
291/* STR_WITH_LEN() shortcuts */
292#define newSVpvs(str) Perl_newSVpvn(aTHX_ STR_WITH_LEN(str))
84bafc02 293#define newSVpvs_flags(str,flags) \
294 Perl_newSVpvn_flags(aTHX_ STR_WITH_LEN(str), flags)
ba3a79e7 295#define newSVpvs_share(str) Perl_newSVpvn_share(aTHX_ STR_WITH_LEN(str), 0)
296#define sv_catpvs(sv, str) Perl_sv_catpvn_flags(aTHX_ sv, STR_WITH_LEN(str), SV_GMAGIC)
3fe05580 297#define sv_setpvs(sv, str) Perl_sv_setpvn(aTHX_ sv, STR_WITH_LEN(str))
ba3a79e7 298#define savepvs(str) Perl_savepvn(aTHX_ STR_WITH_LEN(str))
299#define gv_stashpvs(str, create) Perl_gv_stashpvn(aTHX_ STR_WITH_LEN(str), create)
5c1737d1 300#define gv_fetchpvs(namebeg, add, sv_type) Perl_gv_fetchpvn_flags(aTHX_ STR_WITH_LEN(namebeg), add, sv_type)
a038e571 301#define hv_fetchs(hv,key,lval) \
302 ((SV **)Perl_hv_common(aTHX_ (hv), NULL, STR_WITH_LEN(key), 0, \
303 (lval) ? (HV_FETCH_JUST_SV | HV_FETCH_LVALUE) \
304 : HV_FETCH_JUST_SV, NULL, 0))
305
306#define hv_stores(hv,key,val) \
307 ((SV **)Perl_hv_common(aTHX_ (hv), NULL, STR_WITH_LEN(key), 0, \
308 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), (val), 0))
ba3a79e7 309
5c1737d1 310
954c1994 311/*
ccfc67b7 312=head1 Miscellaneous Functions
313
954c1994 314=for apidoc Am|bool|strNE|char* s1|char* s2
315Test two strings to see if they are different. Returns true or
316false.
317
318=for apidoc Am|bool|strEQ|char* s1|char* s2
319Test two strings to see if they are equal. Returns true or false.
320
321=for apidoc Am|bool|strLT|char* s1|char* s2
322Test two strings to see if the first, C<s1>, is less than the second,
323C<s2>. Returns true or false.
324
325=for apidoc Am|bool|strLE|char* s1|char* s2
326Test two strings to see if the first, C<s1>, is less than or equal to the
327second, C<s2>. Returns true or false.
328
329=for apidoc Am|bool|strGT|char* s1|char* s2
330Test two strings to see if the first, C<s1>, is greater than the second,
331C<s2>. Returns true or false.
332
333=for apidoc Am|bool|strGE|char* s1|char* s2
334Test two strings to see if the first, C<s1>, is greater than or equal to
335the second, C<s2>. Returns true or false.
336
337=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
338Test two strings to see if they are different. The C<len> parameter
339indicates the number of bytes to compare. Returns true or false. (A
340wrapper for C<strncmp>).
341
342=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
343Test two strings to see if they are equal. The C<len> parameter indicates
344the number of bytes to compare. Returns true or false. (A wrapper for
345C<strncmp>).
346
347=cut
348*/
349
8d063cd8 350#define strNE(s1,s2) (strcmp(s1,s2))
351#define strEQ(s1,s2) (!strcmp(s1,s2))
352#define strLT(s1,s2) (strcmp(s1,s2) < 0)
353#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
354#define strGT(s1,s2) (strcmp(s1,s2) > 0)
355#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
356#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
357#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 358
36477c24 359#ifdef HAS_MEMCMP
360# define memNE(s1,s2,l) (memcmp(s1,s2,l))
361# define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
362#else
363# define memNE(s1,s2,l) (bcmp(s1,s2,l))
364# define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
365#endif
366
568a785a 367#define memEQs(s1, l, s2) \
368 (sizeof(s2)-1 == l && memEQ(s1, (s2 ""), (sizeof(s2)-1)))
369#define memNEs(s1, l, s2) !memEQs(s1, l, s2)
370
bbce6d69 371/*
372 * Character classes.
373 *
374 * Unfortunately, the introduction of locales means that we
375 * can't trust isupper(), etc. to tell the truth. And when
376 * it comes to /\w+/ with tainting enabled, we *must* be able
377 * to trust our character classes.
378 *
379 * Therefore, the default tests in the text of Perl will be
380 * independent of locale. Any code that wants to depend on
381 * the current locale will use the tests that begin with "lc".
382 */
383
2304df62 384#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
385# ifndef CTYPE256
386# define CTYPE256
387# endif
388#endif
389
954c1994 390/*
ccfc67b7 391
392=head1 Character classes
393
954c1994 394=for apidoc Am|bool|isALNUM|char ch
4375e838 395Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
f1cbbd6e 396character (including underscore) or digit.
954c1994 397
398=for apidoc Am|bool|isALPHA|char ch
4375e838 399Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
954c1994 400character.
401
402=for apidoc Am|bool|isSPACE|char ch
403Returns a boolean indicating whether the C C<char> is whitespace.
404
405=for apidoc Am|bool|isDIGIT|char ch
4375e838 406Returns a boolean indicating whether the C C<char> is an ASCII
954c1994 407digit.
408
409=for apidoc Am|bool|isUPPER|char ch
410Returns a boolean indicating whether the C C<char> is an uppercase
411character.
412
413=for apidoc Am|bool|isLOWER|char ch
414Returns a boolean indicating whether the C C<char> is a lowercase
415character.
416
417=for apidoc Am|char|toUPPER|char ch
418Converts the specified character to uppercase.
419
420=for apidoc Am|char|toLOWER|char ch
421Converts the specified character to lowercase.
422
423=cut
424*/
425
bbce6d69 426#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
427#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
428#define isALPHA(c) (isUPPER(c) || isLOWER(c))
429#define isSPACE(c) \
430 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
aaa51d5e 431#define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
432#define isBLANK(c) ((c) == ' ' || (c) == '\t')
bbce6d69 433#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
9d116dd7 434#ifdef EBCDIC
435 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
436# define isUPPER(c) isupper(c)
437# define isLOWER(c) islower(c)
b8c5462f 438# define isALNUMC(c) isalnum(c)
439# define isASCII(c) isascii(c)
440# define isCNTRL(c) iscntrl(c)
441# define isGRAPH(c) isgraph(c)
9d116dd7 442# define isPRINT(c) isprint(c)
b8c5462f 443# define isPUNCT(c) ispunct(c)
444# define isXDIGIT(c) isxdigit(c)
9d116dd7 445# define toUPPER(c) toupper(c)
446# define toLOWER(c) tolower(c)
447#else
448# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
449# define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
b8c5462f 450# define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
451# define isASCII(c) ((c) <= 127)
7be5a6cf 452# define isCNTRL(c) ((c) < ' ' || (c) == 127)
b8c5462f 453# define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
dc7a1c02 454# define isPRINT(c) (((c) >= 32 && (c) < 127))
b8c5462f 455# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
c302d089 456# define isXDIGIT(c) (isDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
9d116dd7 457# define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
458# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
459#endif
bbce6d69 460
461#ifdef USE_NEXT_CTYPE
462
463# define isALNUM_LC(c) \
37bd1396 464 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
ff68c719 465# define isIDFIRST_LC(c) \
466 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
467# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
468# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
469# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
470# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
471# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
37bd1396 472# define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
b8c5462f 473# define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
474# define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
ff68c719 475# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
b8c5462f 476# define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
ff68c719 477# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
478# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69 479
480#else /* !USE_NEXT_CTYPE */
b8c5462f 481
bbce6d69 482# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
483
b8c5462f 484# define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
ff68c719 485# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69 486# define isALPHA_LC(c) isalpha((unsigned char)(c))
487# define isSPACE_LC(c) isspace((unsigned char)(c))
488# define isDIGIT_LC(c) isdigit((unsigned char)(c))
489# define isUPPER_LC(c) isupper((unsigned char)(c))
490# define isLOWER_LC(c) islower((unsigned char)(c))
b8c5462f 491# define isALNUMC_LC(c) isalnum((unsigned char)(c))
492# define isCNTRL_LC(c) iscntrl((unsigned char)(c))
493# define isGRAPH_LC(c) isgraph((unsigned char)(c))
bbce6d69 494# define isPRINT_LC(c) isprint((unsigned char)(c))
b8c5462f 495# define isPUNCT_LC(c) ispunct((unsigned char)(c))
bbce6d69 496# define toUPPER_LC(c) toupper((unsigned char)(c))
497# define toLOWER_LC(c) tolower((unsigned char)(c))
498
499# else
500
3609ea0d 501# define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
bbce6d69 502# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
503# define isALPHA_LC(c) (isascii(c) && isalpha(c))
504# define isSPACE_LC(c) (isascii(c) && isspace(c))
505# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
506# define isUPPER_LC(c) (isascii(c) && isupper(c))
507# define isLOWER_LC(c) (isascii(c) && islower(c))
b8c5462f 508# define isALNUMC_LC(c) (isascii(c) && isalnum(c))
509# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
510# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
bbce6d69 511# define isPRINT_LC(c) (isascii(c) && isprint(c))
b8c5462f 512# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
bbce6d69 513# define toUPPER_LC(c) toupper(c)
514# define toLOWER_LC(c) tolower(c)
515
516# endif
a0d0e21e 517#endif /* USE_NEXT_CTYPE */
55204971 518
aaa51d5e 519#define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
520#define isBLANK_LC(c) isBLANK(c) /* could be wrong */
521
a0ed51b3 522#define isALNUM_uni(c) is_uni_alnum(c)
523#define isIDFIRST_uni(c) is_uni_idfirst(c)
524#define isALPHA_uni(c) is_uni_alpha(c)
525#define isSPACE_uni(c) is_uni_space(c)
526#define isDIGIT_uni(c) is_uni_digit(c)
527#define isUPPER_uni(c) is_uni_upper(c)
528#define isLOWER_uni(c) is_uni_lower(c)
b8c5462f 529#define isALNUMC_uni(c) is_uni_alnumc(c)
530#define isASCII_uni(c) is_uni_ascii(c)
531#define isCNTRL_uni(c) is_uni_cntrl(c)
532#define isGRAPH_uni(c) is_uni_graph(c)
a0ed51b3 533#define isPRINT_uni(c) is_uni_print(c)
b8c5462f 534#define isPUNCT_uni(c) is_uni_punct(c)
535#define isXDIGIT_uni(c) is_uni_xdigit(c)
a2a2844f 536#define toUPPER_uni(c,s,l) to_uni_upper(c,s,l)
537#define toTITLE_uni(c,s,l) to_uni_title(c,s,l)
538#define toLOWER_uni(c,s,l) to_uni_lower(c,s,l)
b8d68ded 539#define toFOLD_uni(c,s,l) to_uni_fold(c,s,l)
a0ed51b3 540
aaa51d5e 541#define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
542#define isBLANK_uni(c) isBLANK(c) /* could be wrong */
543
9041c2e3 544#define isALNUM_LC_uvchr(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
545#define isIDFIRST_LC_uvchr(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
546#define isALPHA_LC_uvchr(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
547#define isSPACE_LC_uvchr(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
548#define isDIGIT_LC_uvchr(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
549#define isUPPER_LC_uvchr(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
550#define isLOWER_LC_uvchr(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
551#define isALNUMC_LC_uvchr(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
552#define isCNTRL_LC_uvchr(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
553#define isGRAPH_LC_uvchr(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
554#define isPRINT_LC_uvchr(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
555#define isPUNCT_LC_uvchr(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
a0ed51b3 556
aaa51d5e 557#define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
558#define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
559
a0ed51b3 560#define isALNUM_utf8(p) is_utf8_alnum(p)
82686b01 561/* The ID_Start of Unicode is quite limiting: it assumes a L-class
562 * character (meaning that you cannot have, say, a CJK character).
563 * Instead, let's allow ID_Continue but not digits. */
564#define isIDFIRST_utf8(p) (is_utf8_idcont(p) && !is_utf8_digit(p))
a0ed51b3 565#define isALPHA_utf8(p) is_utf8_alpha(p)
566#define isSPACE_utf8(p) is_utf8_space(p)
567#define isDIGIT_utf8(p) is_utf8_digit(p)
568#define isUPPER_utf8(p) is_utf8_upper(p)
569#define isLOWER_utf8(p) is_utf8_lower(p)
b8c5462f 570#define isALNUMC_utf8(p) is_utf8_alnumc(p)
571#define isASCII_utf8(p) is_utf8_ascii(p)
572#define isCNTRL_utf8(p) is_utf8_cntrl(p)
573#define isGRAPH_utf8(p) is_utf8_graph(p)
a0ed51b3 574#define isPRINT_utf8(p) is_utf8_print(p)
b8c5462f 575#define isPUNCT_utf8(p) is_utf8_punct(p)
576#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
a2a2844f 577#define toUPPER_utf8(p,s,l) to_utf8_upper(p,s,l)
578#define toTITLE_utf8(p,s,l) to_utf8_title(p,s,l)
579#define toLOWER_utf8(p,s,l) to_utf8_lower(p,s,l)
a0ed51b3 580
aaa51d5e 581#define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
582#define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
583
9041c2e3 584#define isALNUM_LC_utf8(p) isALNUM_LC_uvchr(utf8_to_uvchr(p, 0))
585#define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uvchr(utf8_to_uvchr(p, 0))
586#define isALPHA_LC_utf8(p) isALPHA_LC_uvchr(utf8_to_uvchr(p, 0))
587#define isSPACE_LC_utf8(p) isSPACE_LC_uvchr(utf8_to_uvchr(p, 0))
588#define isDIGIT_LC_utf8(p) isDIGIT_LC_uvchr(utf8_to_uvchr(p, 0))
589#define isUPPER_LC_utf8(p) isUPPER_LC_uvchr(utf8_to_uvchr(p, 0))
590#define isLOWER_LC_utf8(p) isLOWER_LC_uvchr(utf8_to_uvchr(p, 0))
591#define isALNUMC_LC_utf8(p) isALNUMC_LC_uvchr(utf8_to_uvchr(p, 0))
592#define isCNTRL_LC_utf8(p) isCNTRL_LC_uvchr(utf8_to_uvchr(p, 0))
593#define isGRAPH_LC_utf8(p) isGRAPH_LC_uvchr(utf8_to_uvchr(p, 0))
594#define isPRINT_LC_utf8(p) isPRINT_LC_uvchr(utf8_to_uvchr(p, 0))
595#define isPUNCT_LC_utf8(p) isPUNCT_LC_uvchr(utf8_to_uvchr(p, 0))
a0ed51b3 596
aaa51d5e 597#define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
598#define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
599
9d116dd7 600#ifdef EBCDIC
20b634c2 601# ifdef PERL_IMPLICIT_CONTEXT
602# define toCTRL(c) Perl_ebcdic_control(aTHX_ c)
603# else
604# define toCTRL Perl_ebcdic_control
605# endif
9d116dd7 606#else
607 /* This conversion works both ways, strangely enough. */
608# define toCTRL(c) (toUPPER(c) ^ 64)
609#endif
bbce6d69 610
dea28490 611/* Line numbers are unsigned, 32 bits. */
612typedef U32 line_t;
dea28490 613#define NOLINE ((line_t) 4294967295UL)
378cc40b 614
8c52afec 615
8e84507e 616/*
ccfc67b7 617=head1 Memory Management
618
a02a5408 619=for apidoc Am|void|Newx|void* ptr|int nitems|type
954c1994 620The XSUB-writer's interface to the C C<malloc> function.
621
c5008215 622In 5.9.3, Newx() and friends replace the older New() API, and drops
623the first parameter, I<x>, a debug aid which allowed callers to identify
37b8b4c9 624themselves. This aid has been superseded by a new build option,
c5008215 625PERL_MEM_LOG (see L<perlhack/PERL_MEM_LOG>). The older API is still
626there for use in XS modules supporting older perls.
627
a02a5408 628=for apidoc Am|void|Newxc|void* ptr|int nitems|type|cast
954c1994 629The XSUB-writer's interface to the C C<malloc> function, with
c5008215 630cast. See also C<Newx>.
954c1994 631
a02a5408 632=for apidoc Am|void|Newxz|void* ptr|int nitems|type
954c1994 633The XSUB-writer's interface to the C C<malloc> function. The allocated
c5008215 634memory is zeroed with C<memzero>. See also C<Newx>.
a02a5408 635
954c1994 636=for apidoc Am|void|Renew|void* ptr|int nitems|type
637The XSUB-writer's interface to the C C<realloc> function.
638
639=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
640The XSUB-writer's interface to the C C<realloc> function, with
641cast.
642
49b8b560 643=for apidoc Am|void|Safefree|void* ptr
954c1994 644The XSUB-writer's interface to the C C<free> function.
645
646=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
647The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
648source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
649the type. Can do overlapping moves. See also C<Copy>.
650
e90e2364 651=for apidoc Am|void *|MoveD|void* src|void* dest|int nitems|type
652Like C<Move> but returns dest. Useful for encouraging compilers to tail-call
653optimise.
654
954c1994 655=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
656The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
657source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
658the type. May fail on overlapping copies. See also C<Move>.
659
e90e2364 660=for apidoc Am|void *|CopyD|void* src|void* dest|int nitems|type
661
662Like C<Copy> but returns dest. Useful for encouraging compilers to tail-call
663optimise.
664
954c1994 665=for apidoc Am|void|Zero|void* dest|int nitems|type
666
667The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
668destination, C<nitems> is the number of items, and C<type> is the type.
669
e90e2364 670=for apidoc Am|void *|ZeroD|void* dest|int nitems|type
671
672Like C<Zero> but returns dest. Useful for encouraging compilers to tail-call
673optimise.
674
954c1994 675=for apidoc Am|void|StructCopy|type src|type dest|type
4375e838 676This is an architecture-independent macro to copy one structure to another.
954c1994 677
7e337ee0 678=for apidoc Am|void|PoisonWith|void* dest|int nitems|type|U8 byte
679
680Fill up memory with a byte pattern (a byte repeated over and over
681again) that hopefully catches attempts to access uninitialized memory.
682
683=for apidoc Am|void|PoisonNew|void* dest|int nitems|type
684
685PoisonWith(0xAB) for catching access to allocated but uninitialized memory.
686
1c12ffb4 687=for apidoc Am|void|PoisonFree|void* dest|int nitems|type
7e337ee0 688
689PoisonWith(0xEF) for catching access to freed memory.
690
9965345d 691=for apidoc Am|void|Poison|void* dest|int nitems|type
692
7e337ee0 693PoisonWith(0xEF) for catching access to freed memory.
9965345d 694
695=cut */
954c1994 696
561b68a9 697/* Maintained for backwards-compatibility only. Use newSV() instead. */
698#ifndef PERL_CORE
ff06c60c 699#define NEWSV(x,len) newSV(len)
561b68a9 700#endif
ff06c60c 701
19a94d75 702#define MEM_SIZE_MAX ((MEM_SIZE)~0)
703
2720f793 704/* The +0.0 in MEM_WRAP_CHECK_ is an attempt to foil
705 * overly eager compilers that will bleat about e.g.
706 * (U16)n > (size_t)~0/sizeof(U16) always being false. */
27d5b266 707#ifdef PERL_MALLOC_WRAP
fabdb6c0 708#define MEM_WRAP_CHECK(n,t) MEM_WRAP_CHECK_1(n,t,PL_memory_wrap)
27d5b266 709#define MEM_WRAP_CHECK_1(n,t,a) \
19a94d75 710 (void)(sizeof(t) > 1 && ((MEM_SIZE)(n)+0.0) > MEM_SIZE_MAX/sizeof(t) && (Perl_croak_nocontext(a),0))
8b44ba4c 711#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
27d5b266 712
19a94d75 713#define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > MEM_SIZE_MAX - 2 * PERL_STRLEN_ROUNDUP_QUANTUM) ? (Perl_croak_nocontext(PL_memory_wrap),0):0),((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
1936d2a7 714
27d5b266 715#else
716
410319be 717#define MEM_WRAP_CHECK(n,t)
718#define MEM_WRAP_CHECK_1(n,t,a)
719#define MEM_WRAP_CHECK_2(n,t,a,b)
8b44ba4c 720#define MEM_WRAP_CHECK_(n,t)
721
6fff79ce 722#define PERL_STRLEN_ROUNDUP(n) (((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
27d5b266 723
1936d2a7 724#endif
8b44ba4c 725
fe4f188c 726#ifdef PERL_MEM_LOG
46c6c7e2 727/*
9f653bb5 728 * If PERL_MEM_LOG is defined, all Newx()s, Renew()s, and Safefree()s
46c6c7e2 729 * go through functions, which are handy for debugging breakpoints, but
730 * which more importantly get the immediate calling environment (file and
e352bcff 731 * line number, and C function name if available) passed in. This info can
732 * then be used for logging the calls, for which one gets a sample
733 * implementation if PERL_MEM_LOG_STDERR is defined.
3609ea0d 734 *
46c6c7e2 735 * Known problems:
736 * - all memory allocs do not get logged, only those
737 * that go through Newx() and derivatives (while all
738 * Safefrees do get logged)
739 * - __FILE__ and __LINE__ do not work everywhere
740 * - __func__ or __FUNCTION__ even less so
741 * - I think more goes on after the perlio frees but
742 * the thing is that STDERR gets closed (as do all
743 * the file descriptors)
744 * - no deeper calling stack than the caller of the Newx()
745 * or the kind, but do I look like a C reflection/introspection
746 * utility to you?
747 * - the function prototypes for the logging functions
748 * probably should maybe be somewhere else than handy.h
749 * - one could consider inlining (macrofying) the logging
750 * for speed, but I am too lazy
751 * - one could imagine recording the allocations in a hash,
752 * (keyed by the allocation address?), and maintain that
753 * through reallocs and frees, but how to do that without
754 * any News() happening...?
755 */
756
757Malloc_t Perl_mem_log_alloc(const UV n, const UV typesize, const char *typename, Malloc_t newalloc, const char *filename, const int linenumber, const char *funcname);
758
759Malloc_t Perl_mem_log_realloc(const UV n, const UV typesize, const char *typename, Malloc_t oldalloc, Malloc_t newalloc, const char *filename, const int linenumber, const char *funcname);
760
761Malloc_t Perl_mem_log_free(Malloc_t oldalloc, const char *filename, const int linenumber, const char *funcname);
762
fe4f188c 763#endif
764
765#ifdef PERL_MEM_LOG
46c6c7e2 766#define MEM_LOG_ALLOC(n,t,a) Perl_mem_log_alloc(n,sizeof(t),STRINGIFY(t),a,__FILE__,__LINE__,FUNCTION__)
767#define MEM_LOG_REALLOC(n,t,v,a) Perl_mem_log_realloc(n,sizeof(t),STRINGIFY(t),v,a,__FILE__,__LINE__,FUNCTION__)
768#define MEM_LOG_FREE(a) Perl_mem_log_free(a,__FILE__,__LINE__,FUNCTION__)
fe4f188c 769#endif
770
771#ifndef MEM_LOG_ALLOC
772#define MEM_LOG_ALLOC(n,t,a) (a)
773#endif
774#ifndef MEM_LOG_REALLOC
775#define MEM_LOG_REALLOC(n,t,v,a) (a)
776#endif
777#ifndef MEM_LOG_FREE
778#define MEM_LOG_FREE(a) (a)
779#endif
780
781#define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))))
782#define Newxc(v,n,t,c) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))))
3341f577 783#define Newxz(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(t*)safecalloc((n),sizeof(t)))))
a6f6820f 784
785#ifndef PERL_CORE
a02a5408 786/* pre 5.9.x compatibility */
787#define New(x,v,n,t) Newx(v,n,t)
788#define Newc(x,v,n,t,c) Newxc(v,n,t,c)
4541904d 789#define Newz(x,v,n,t) Newxz(v,n,t)
a6f6820f 790#endif
a02a5408 791
ff68c719 792#define Renew(v,n,t) \
fe4f188c 793 (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_REALLOC(n,t,v,(t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
ff68c719 794#define Renewc(v,n,t,c) \
fe4f188c 795 (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_REALLOC(n,t,v,(c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
94010e71 796
797#ifdef PERL_POISON
798#define Safefree(d) \
06c0cc96 799 ((d) ? (void)(safefree(MEM_LOG_FREE((Malloc_t)(d))), Poison(&(d), 1, Malloc_t)) : (void) 0)
94010e71 800#else
fe4f188c 801#define Safefree(d) safefree(MEM_LOG_FREE((Malloc_t)(d)))
94010e71 802#endif
55497cff 803
8b44ba4c 804#define Move(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
805#define Copy(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
806#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memzero((char*)(d), (n) * sizeof(t)))
55497cff 807
8b44ba4c 808#define MoveD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
809#define CopyD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
e90e2364 810#ifdef HAS_MEMSET
8b44ba4c 811#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)))
e90e2364 812#else
8b44ba4c 813/* Using bzero(), which returns void. */
814#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)),d)
e90e2364 815#endif
816
7e337ee0 817#define PoisonWith(d,n,t,b) (MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), (U8)(b), (n) * sizeof(t)))
818#define PoisonNew(d,n,t) PoisonWith(d,n,t,0xAB)
819#define PoisonFree(d,n,t) PoisonWith(d,n,t,0xEF)
820#define Poison(d,n,t) PoisonFree(d,n,t)
27d5b266 821
2304df62 822#ifdef USE_STRUCT_COPY
ff68c719 823#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2 824#else
825#define StructCopy(s,d,t) Copy(s,d,1,t)
826#endif
2cc61e15 827
622913ab 828#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
829
2cc61e15 830#ifdef NEED_VA_COPY
831# ifdef va_copy
832# define Perl_va_copy(s, d) va_copy(d, s)
2cc61e15 833# else
a1866d1b 834# if defined(__va_copy)
835# define Perl_va_copy(s, d) __va_copy(d, s)
836# else
837# define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
838# endif
2cc61e15 839# endif
840#endif
841
472d47bc 842/* convenience debug macros */
843#ifdef USE_ITHREADS
844#define pTHX_FORMAT "Perl interpreter: 0x%p"
845#define pTHX__FORMAT ", Perl interpreter: 0x%p"
f54cb97a 846#define pTHX_VALUE_ (void *)my_perl,
847#define pTHX_VALUE (void *)my_perl
848#define pTHX__VALUE_ ,(void *)my_perl,
849#define pTHX__VALUE ,(void *)my_perl
472d47bc 850#else
3609ea0d 851#define pTHX_FORMAT
472d47bc 852#define pTHX__FORMAT
3609ea0d 853#define pTHX_VALUE_
472d47bc 854#define pTHX_VALUE
3609ea0d 855#define pTHX__VALUE_
472d47bc 856#define pTHX__VALUE
857#endif /* USE_ITHREADS */
3609ea0d 858
e9a8c099 859/*
860 * Local variables:
861 * c-indentation-style: bsd
862 * c-basic-offset: 4
863 * indent-tabs-mode: t
864 * End:
865 *
866 * ex: set ts=8 sts=4 sw=4 noet:
867 */