This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
yet another way of debugging memory allocations
[perl5.git] / handy.h
CommitLineData
a0d0e21e 1/* handy.h
a687059c 2 *
4bb101f2 3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
1d325971 4 * 2000, 2001, 2002, 2004, 2005 by Larry Wall and others
a687059c 5 *
6e21c824
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
LW
9 */
10
85e6fe83 11#if !defined(__STDC__)
378cc40b
LW
12#ifdef NULL
13#undef NULL
14#endif
a687059c
LW
15#ifndef I286
16# define NULL 0
17#else
18# define NULL 0L
19#endif
85e6fe83
LW
20#endif
21
378cc40b 22#define Null(type) ((type)NULL)
954c1994
GS
23
24/*
ccfc67b7 25=head1 Handy Values
954c1994 26
ccfc67b7
JH
27=for apidoc AmU||Nullch
28Null character pointer.
2307c6d0 29
954c1994
GS
30=for apidoc AmU||Nullsv
31Null SV pointer.
32
33=cut
34*/
35
8d063cd8 36#define Nullch Null(char*)
760ac839 37#define Nullfp Null(PerlIO*)
79072805 38#define Nullsv Null(SV*)
8d063cd8 39
641d3f0b
PP
40#ifdef TRUE
41#undef TRUE
42#endif
43#ifdef FALSE
44#undef FALSE
45#endif
46#define TRUE (1)
47#define FALSE (0)
48
27d4fb96
PP
49
50/* XXX Configure ought to have a test for a boolean type, if I can
51 just figure out all the headers such a test needs.
52 Andy Dougherty August 1996
53*/
8e84507e 54/* bool is built-in for g++-2.6.3 and later, which might be used
c1d22f6b
GS
55 for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
56 be sure _G_config.h will be included before this file. _G_config.h
8e84507e 57 also defines _G_HAVE_BOOL for both gcc and g++, but only g++
c1d22f6b
GS
58 actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
59 g++ can be identified by __GNUG__.
60 Andy Dougherty February 2000
5d94fbed 61*/
c1d22f6b 62#ifdef __GNUG__ /* GNU g++ has bool built-in */
5d94fbed 63# ifndef HAS_BOOL
c1d22f6b 64# define HAS_BOOL 1
5d94fbed 65# endif
5d94fbed
AD
66#endif
67
641d3f0b
PP
68/* The NeXT dynamic loader headers will not build with the bool macro
69 So declare them now to clear confusion.
70*/
8f1f23e8 71#if defined(NeXT) || defined(__NeXT__)
641d3f0b
PP
72# undef FALSE
73# undef TRUE
74 typedef enum bool { FALSE = 0, TRUE = 1 } bool;
75# define ENUM_BOOL 1
76# ifndef HAS_BOOL
77# define HAS_BOOL 1
78# endif /* !HAS_BOOL */
8f1f23e8 79#endif /* NeXT || __NeXT__ */
641d3f0b 80
5d94fbed 81#ifndef HAS_BOOL
61bb5906 82# if defined(UTS) || defined(VMS)
5d94fbed
AD
83# define bool int
84# else
85# define bool char
86# endif
c1d22f6b 87# define HAS_BOOL 1
a687059c 88#endif
0d3e774c 89
27d4fb96
PP
90/* XXX A note on the perl source internal type system. The
91 original intent was that I32 be *exactly* 32 bits.
92
93 Currently, we only guarantee that I32 is *at least* 32 bits.
94 Specifically, if int is 64 bits, then so is I32. (This is the case
95 for the Cray.) This has the advantage of meshing nicely with
96 standard library calls (where we pass an I32 and the library is
97 expecting an int), but the disadvantage that an I32 is not 32 bits.
98 Andy Dougherty August 1996
24fef2a7 99
dc45a647
MB
100 There is no guarantee that there is *any* integral type with
101 exactly 32 bits. It is perfectly legal for a system to have
102 sizeof(short) == sizeof(int) == sizeof(long) == 8.
693762b4 103
dc45a647
MB
104 Similarly, there is no guarantee that I16 and U16 have exactly 16
105 bits.
693762b4 106
8e84507e
NIS
107 For dealing with issues that may arise from various 32/64-bit
108 systems, we will ask Configure to check out
8175356b 109
dc45a647
MB
110 SHORTSIZE == sizeof(short)
111 INTSIZE == sizeof(int)
112 LONGSIZE == sizeof(long)
113 LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
114 PTRSIZE == sizeof(void *)
115 DOUBLESIZE == sizeof(double)
116 LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
8175356b 117
27d4fb96
PP
118*/
119
69512466
JH
120#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
121# include <inttypes.h>
dd0eed91
JH
122# ifdef INT32_MIN_BROKEN
123# undef INT32_MIN
124# define INT32_MIN (-2147483647-1)
125# endif
126# ifdef INT64_MIN_BROKEN
127# undef INT64_MIN
128# define INT64_MIN (-9223372036854775807LL-1)
129# endif
69512466
JH
130#endif
131
8175356b
JH
132typedef I8TYPE I8;
133typedef U8TYPE U8;
134typedef I16TYPE I16;
135typedef U16TYPE U16;
136typedef I32TYPE I32;
137typedef U32TYPE U32;
6b8eaf93
JH
138#ifdef PERL_CORE
139# ifdef HAS_QUAD
8175356b
JH
140typedef I64TYPE I64;
141typedef U64TYPE U64;
6b8eaf93
JH
142# endif
143#endif /* PERL_CORE */
8175356b 144
69512466
JH
145#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
146# ifndef UINT64_C /* usually from <inttypes.h> */
147# if defined(HAS_LONG_LONG) && QUADKIND == QUAD_IS_LONG_LONG
148# define INT64_C(c) CAT2(c,LL)
149# define UINT64_C(c) CAT2(c,ULL)
150# else
151# if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
152# define INT64_C(c) CAT2(c,L)
153# define UINT64_C(c) CAT2(c,UL)
154# else
155# define INT64_C(c) ((I64TYPE)(c))
156# define UINT64_C(c) ((U64TYPE)(c))
157# endif
158# endif
e8c95190
JH
159# endif
160#endif
dc750f2e 161
cae3d67c 162/* HMB H.Merijn Brand - a placeholder for preparing Configure patches */
0a0abfba 163#if defined(LIBM_LIB_VERSION)
cae3d67c 164/* Not (yet) used at top level, but mention them for metaconfig */
dc750f2e 165#endif
e8c95190 166
a22e52b9
JH
167/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
168 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
169
d8668976 170#if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
5ff3f7a4 171
5ff3f7a4
GS
172/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
173 Please search CHAR_MAX in perl.h for further details. */
174#define U8_MAX UINT8_MAX
175#define U8_MIN UINT8_MIN
176
5ff3f7a4
GS
177#define I16_MAX INT16_MAX
178#define I16_MIN INT16_MIN
179#define U16_MAX UINT16_MAX
180#define U16_MIN UINT16_MIN
181
5ff3f7a4
GS
182#define I32_MAX INT32_MAX
183#define I32_MIN INT32_MIN
0e983133
GS
184#ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
185# define U32_MAX UINT32_MAX
186#else
187# define U32_MAX 4294967295U
188#endif
5ff3f7a4
GS
189#define U32_MIN UINT32_MIN
190
191#else
192
5c9fa16e
KA
193/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
194 Please search CHAR_MAX in perl.h for further details. */
27d4fb96
PP
195#define U8_MAX PERL_UCHAR_MAX
196#define U8_MIN PERL_UCHAR_MIN
79072805 197
27d4fb96
PP
198#define I16_MAX PERL_SHORT_MAX
199#define I16_MIN PERL_SHORT_MIN
200#define U16_MAX PERL_USHORT_MAX
201#define U16_MIN PERL_USHORT_MIN
79072805 202
c4f23d77 203#if LONGSIZE > 4
27d4fb96
PP
204# define I32_MAX PERL_INT_MAX
205# define I32_MIN PERL_INT_MIN
206# define U32_MAX PERL_UINT_MAX
207# define U32_MIN PERL_UINT_MIN
79072805 208#else
27d4fb96
PP
209# define I32_MAX PERL_LONG_MAX
210# define I32_MIN PERL_LONG_MIN
211# define U32_MAX PERL_ULONG_MAX
212# define U32_MIN PERL_ULONG_MIN
79072805
LW
213#endif
214
5ff3f7a4
GS
215#endif
216
58a9a5d5 217/* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
fc36a67e
PP
218#define BIT_DIGITS(N) (((N)*146)/485 + 1) /* log2(10) =~ 146/485 */
219#define TYPE_DIGITS(T) BIT_DIGITS(sizeof(T) * 8)
220#define TYPE_CHARS(T) (TYPE_DIGITS(T) + 2) /* sign, NUL */
221
ff68c719 222#define Ctl(ch) ((ch) & 037)
8d063cd8 223
954c1994 224/*
ccfc67b7
JH
225=head1 Miscellaneous Functions
226
954c1994
GS
227=for apidoc Am|bool|strNE|char* s1|char* s2
228Test two strings to see if they are different. Returns true or
229false.
230
231=for apidoc Am|bool|strEQ|char* s1|char* s2
232Test two strings to see if they are equal. Returns true or false.
233
234=for apidoc Am|bool|strLT|char* s1|char* s2
235Test two strings to see if the first, C<s1>, is less than the second,
236C<s2>. Returns true or false.
237
238=for apidoc Am|bool|strLE|char* s1|char* s2
239Test two strings to see if the first, C<s1>, is less than or equal to the
240second, C<s2>. Returns true or false.
241
242=for apidoc Am|bool|strGT|char* s1|char* s2
243Test two strings to see if the first, C<s1>, is greater than the second,
244C<s2>. Returns true or false.
245
246=for apidoc Am|bool|strGE|char* s1|char* s2
247Test two strings to see if the first, C<s1>, is greater than or equal to
248the second, C<s2>. Returns true or false.
249
250=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
251Test two strings to see if they are different. The C<len> parameter
252indicates the number of bytes to compare. Returns true or false. (A
253wrapper for C<strncmp>).
254
255=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
256Test two strings to see if they are equal. The C<len> parameter indicates
257the number of bytes to compare. Returns true or false. (A wrapper for
258C<strncmp>).
259
260=cut
261*/
262
8d063cd8
LW
263#define strNE(s1,s2) (strcmp(s1,s2))
264#define strEQ(s1,s2) (!strcmp(s1,s2))
265#define strLT(s1,s2) (strcmp(s1,s2) < 0)
266#define strLE(s1,s2) (strcmp(s1,s2) <= 0)
267#define strGT(s1,s2) (strcmp(s1,s2) > 0)
268#define strGE(s1,s2) (strcmp(s1,s2) >= 0)
269#define strnNE(s1,s2,l) (strncmp(s1,s2,l))
270#define strnEQ(s1,s2,l) (!strncmp(s1,s2,l))
378cc40b 271
36477c24
PP
272#ifdef HAS_MEMCMP
273# define memNE(s1,s2,l) (memcmp(s1,s2,l))
274# define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
275#else
276# define memNE(s1,s2,l) (bcmp(s1,s2,l))
277# define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
278#endif
279
bbce6d69
PP
280/*
281 * Character classes.
282 *
283 * Unfortunately, the introduction of locales means that we
284 * can't trust isupper(), etc. to tell the truth. And when
285 * it comes to /\w+/ with tainting enabled, we *must* be able
286 * to trust our character classes.
287 *
288 * Therefore, the default tests in the text of Perl will be
289 * independent of locale. Any code that wants to depend on
290 * the current locale will use the tests that begin with "lc".
291 */
292
2304df62
AD
293#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
294# ifndef CTYPE256
295# define CTYPE256
296# endif
297#endif
298
954c1994 299/*
ccfc67b7
JH
300
301=head1 Character classes
302
954c1994 303=for apidoc Am|bool|isALNUM|char ch
4375e838 304Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
f1cbbd6e 305character (including underscore) or digit.
954c1994
GS
306
307=for apidoc Am|bool|isALPHA|char ch
4375e838 308Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
954c1994
GS
309character.
310
311=for apidoc Am|bool|isSPACE|char ch
312Returns a boolean indicating whether the C C<char> is whitespace.
313
314=for apidoc Am|bool|isDIGIT|char ch
4375e838 315Returns a boolean indicating whether the C C<char> is an ASCII
954c1994
GS
316digit.
317
318=for apidoc Am|bool|isUPPER|char ch
319Returns a boolean indicating whether the C C<char> is an uppercase
320character.
321
322=for apidoc Am|bool|isLOWER|char ch
323Returns a boolean indicating whether the C C<char> is a lowercase
324character.
325
326=for apidoc Am|char|toUPPER|char ch
327Converts the specified character to uppercase.
328
329=for apidoc Am|char|toLOWER|char ch
330Converts the specified character to lowercase.
331
332=cut
333*/
334
bbce6d69
PP
335#define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
336#define isIDFIRST(c) (isALPHA(c) || (c) == '_')
337#define isALPHA(c) (isUPPER(c) || isLOWER(c))
338#define isSPACE(c) \
339 ((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
aaa51d5e
JF
340#define isPSXSPC(c) (isSPACE(c) || (c) == '\v')
341#define isBLANK(c) ((c) == ' ' || (c) == '\t')
bbce6d69 342#define isDIGIT(c) ((c) >= '0' && (c) <= '9')
9d116dd7
JH
343#ifdef EBCDIC
344 /* In EBCDIC we do not do locales: therefore() isupper() is fine. */
345# define isUPPER(c) isupper(c)
346# define isLOWER(c) islower(c)
b8c5462f
JH
347# define isALNUMC(c) isalnum(c)
348# define isASCII(c) isascii(c)
349# define isCNTRL(c) iscntrl(c)
350# define isGRAPH(c) isgraph(c)
9d116dd7 351# define isPRINT(c) isprint(c)
b8c5462f
JH
352# define isPUNCT(c) ispunct(c)
353# define isXDIGIT(c) isxdigit(c)
9d116dd7
JH
354# define toUPPER(c) toupper(c)
355# define toLOWER(c) tolower(c)
356#else
357# define isUPPER(c) ((c) >= 'A' && (c) <= 'Z')
358# define isLOWER(c) ((c) >= 'a' && (c) <= 'z')
b8c5462f
JH
359# define isALNUMC(c) (isALPHA(c) || isDIGIT(c))
360# define isASCII(c) ((c) <= 127)
7be5a6cf 361# define isCNTRL(c) ((c) < ' ' || (c) == 127)
b8c5462f 362# define isGRAPH(c) (isALNUM(c) || isPUNCT(c))
f79b3095 363# define isPRINT(c) (((c) > 32 && (c) < 127) || (c) == ' ')
b8c5462f 364# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
c302d089 365# define isXDIGIT(c) (isDIGIT(c) || ((c) >= 'a' && (c) <= 'f') || ((c) >= 'A' && (c) <= 'F'))
9d116dd7
JH
366# define toUPPER(c) (isLOWER(c) ? (c) - ('a' - 'A') : (c))
367# define toLOWER(c) (isUPPER(c) ? (c) + ('a' - 'A') : (c))
368#endif
bbce6d69
PP
369
370#ifdef USE_NEXT_CTYPE
371
372# define isALNUM_LC(c) \
37bd1396 373 (NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
ff68c719
PP
374# define isIDFIRST_LC(c) \
375 (NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
376# define isALPHA_LC(c) NXIsAlpha((unsigned int)(c))
377# define isSPACE_LC(c) NXIsSpace((unsigned int)(c))
378# define isDIGIT_LC(c) NXIsDigit((unsigned int)(c))
379# define isUPPER_LC(c) NXIsUpper((unsigned int)(c))
380# define isLOWER_LC(c) NXIsLower((unsigned int)(c))
37bd1396 381# define isALNUMC_LC(c) NXIsAlNum((unsigned int)(c))
b8c5462f
JH
382# define isCNTRL_LC(c) NXIsCntrl((unsigned int)(c))
383# define isGRAPH_LC(c) NXIsGraph((unsigned int)(c))
ff68c719 384# define isPRINT_LC(c) NXIsPrint((unsigned int)(c))
b8c5462f 385# define isPUNCT_LC(c) NXIsPunct((unsigned int)(c))
ff68c719
PP
386# define toUPPER_LC(c) NXToUpper((unsigned int)(c))
387# define toLOWER_LC(c) NXToLower((unsigned int)(c))
bbce6d69
PP
388
389#else /* !USE_NEXT_CTYPE */
b8c5462f 390
bbce6d69
PP
391# if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
392
b8c5462f 393# define isALNUM_LC(c) (isalnum((unsigned char)(c)) || (char)(c) == '_')
ff68c719 394# define isIDFIRST_LC(c) (isalpha((unsigned char)(c)) || (char)(c) == '_')
bbce6d69
PP
395# define isALPHA_LC(c) isalpha((unsigned char)(c))
396# define isSPACE_LC(c) isspace((unsigned char)(c))
397# define isDIGIT_LC(c) isdigit((unsigned char)(c))
398# define isUPPER_LC(c) isupper((unsigned char)(c))
399# define isLOWER_LC(c) islower((unsigned char)(c))
b8c5462f
JH
400# define isALNUMC_LC(c) isalnum((unsigned char)(c))
401# define isCNTRL_LC(c) iscntrl((unsigned char)(c))
402# define isGRAPH_LC(c) isgraph((unsigned char)(c))
bbce6d69 403# define isPRINT_LC(c) isprint((unsigned char)(c))
b8c5462f 404# define isPUNCT_LC(c) ispunct((unsigned char)(c))
bbce6d69
PP
405# define toUPPER_LC(c) toupper((unsigned char)(c))
406# define toLOWER_LC(c) tolower((unsigned char)(c))
407
408# else
409
b8c5462f 410# define isALNUM_LC(c) (isascii(c) && (isalnum(c) || (c) == '_'))
bbce6d69
PP
411# define isIDFIRST_LC(c) (isascii(c) && (isalpha(c) || (c) == '_'))
412# define isALPHA_LC(c) (isascii(c) && isalpha(c))
413# define isSPACE_LC(c) (isascii(c) && isspace(c))
414# define isDIGIT_LC(c) (isascii(c) && isdigit(c))
415# define isUPPER_LC(c) (isascii(c) && isupper(c))
416# define isLOWER_LC(c) (isascii(c) && islower(c))
b8c5462f
JH
417# define isALNUMC_LC(c) (isascii(c) && isalnum(c))
418# define isCNTRL_LC(c) (isascii(c) && iscntrl(c))
419# define isGRAPH_LC(c) (isascii(c) && isgraph(c))
bbce6d69 420# define isPRINT_LC(c) (isascii(c) && isprint(c))
b8c5462f 421# define isPUNCT_LC(c) (isascii(c) && ispunct(c))
bbce6d69
PP
422# define toUPPER_LC(c) toupper(c)
423# define toLOWER_LC(c) tolower(c)
424
425# endif
a0d0e21e 426#endif /* USE_NEXT_CTYPE */
55204971 427
aaa51d5e
JF
428#define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
429#define isBLANK_LC(c) isBLANK(c) /* could be wrong */
430
a0ed51b3
LW
431#define isALNUM_uni(c) is_uni_alnum(c)
432#define isIDFIRST_uni(c) is_uni_idfirst(c)
433#define isALPHA_uni(c) is_uni_alpha(c)
434#define isSPACE_uni(c) is_uni_space(c)
435#define isDIGIT_uni(c) is_uni_digit(c)
436#define isUPPER_uni(c) is_uni_upper(c)
437#define isLOWER_uni(c) is_uni_lower(c)
b8c5462f
JH
438#define isALNUMC_uni(c) is_uni_alnumc(c)
439#define isASCII_uni(c) is_uni_ascii(c)
440#define isCNTRL_uni(c) is_uni_cntrl(c)
441#define isGRAPH_uni(c) is_uni_graph(c)
a0ed51b3 442#define isPRINT_uni(c) is_uni_print(c)
b8c5462f
JH
443#define isPUNCT_uni(c) is_uni_punct(c)
444#define isXDIGIT_uni(c) is_uni_xdigit(c)
a2a2844f
JH
445#define toUPPER_uni(c,s,l) to_uni_upper(c,s,l)
446#define toTITLE_uni(c,s,l) to_uni_title(c,s,l)
447#define toLOWER_uni(c,s,l) to_uni_lower(c,s,l)
b8d68ded 448#define toFOLD_uni(c,s,l) to_uni_fold(c,s,l)
a0ed51b3 449
aaa51d5e
JF
450#define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
451#define isBLANK_uni(c) isBLANK(c) /* could be wrong */
452
9041c2e3
NIS
453#define isALNUM_LC_uvchr(c) (c < 256 ? isALNUM_LC(c) : is_uni_alnum_lc(c))
454#define isIDFIRST_LC_uvchr(c) (c < 256 ? isIDFIRST_LC(c) : is_uni_idfirst_lc(c))
455#define isALPHA_LC_uvchr(c) (c < 256 ? isALPHA_LC(c) : is_uni_alpha_lc(c))
456#define isSPACE_LC_uvchr(c) (c < 256 ? isSPACE_LC(c) : is_uni_space_lc(c))
457#define isDIGIT_LC_uvchr(c) (c < 256 ? isDIGIT_LC(c) : is_uni_digit_lc(c))
458#define isUPPER_LC_uvchr(c) (c < 256 ? isUPPER_LC(c) : is_uni_upper_lc(c))
459#define isLOWER_LC_uvchr(c) (c < 256 ? isLOWER_LC(c) : is_uni_lower_lc(c))
460#define isALNUMC_LC_uvchr(c) (c < 256 ? isALNUMC_LC(c) : is_uni_alnumc_lc(c))
461#define isCNTRL_LC_uvchr(c) (c < 256 ? isCNTRL_LC(c) : is_uni_cntrl_lc(c))
462#define isGRAPH_LC_uvchr(c) (c < 256 ? isGRAPH_LC(c) : is_uni_graph_lc(c))
463#define isPRINT_LC_uvchr(c) (c < 256 ? isPRINT_LC(c) : is_uni_print_lc(c))
464#define isPUNCT_LC_uvchr(c) (c < 256 ? isPUNCT_LC(c) : is_uni_punct_lc(c))
a0ed51b3 465
aaa51d5e
JF
466#define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
467#define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
468
a0ed51b3 469#define isALNUM_utf8(p) is_utf8_alnum(p)
82686b01
JH
470/* The ID_Start of Unicode is quite limiting: it assumes a L-class
471 * character (meaning that you cannot have, say, a CJK character).
472 * Instead, let's allow ID_Continue but not digits. */
473#define isIDFIRST_utf8(p) (is_utf8_idcont(p) && !is_utf8_digit(p))
a0ed51b3
LW
474#define isALPHA_utf8(p) is_utf8_alpha(p)
475#define isSPACE_utf8(p) is_utf8_space(p)
476#define isDIGIT_utf8(p) is_utf8_digit(p)
477#define isUPPER_utf8(p) is_utf8_upper(p)
478#define isLOWER_utf8(p) is_utf8_lower(p)
b8c5462f
JH
479#define isALNUMC_utf8(p) is_utf8_alnumc(p)
480#define isASCII_utf8(p) is_utf8_ascii(p)
481#define isCNTRL_utf8(p) is_utf8_cntrl(p)
482#define isGRAPH_utf8(p) is_utf8_graph(p)
a0ed51b3 483#define isPRINT_utf8(p) is_utf8_print(p)
b8c5462f
JH
484#define isPUNCT_utf8(p) is_utf8_punct(p)
485#define isXDIGIT_utf8(p) is_utf8_xdigit(p)
a2a2844f
JH
486#define toUPPER_utf8(p,s,l) to_utf8_upper(p,s,l)
487#define toTITLE_utf8(p,s,l) to_utf8_title(p,s,l)
488#define toLOWER_utf8(p,s,l) to_utf8_lower(p,s,l)
a0ed51b3 489
aaa51d5e
JF
490#define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
491#define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
492
9041c2e3
NIS
493#define isALNUM_LC_utf8(p) isALNUM_LC_uvchr(utf8_to_uvchr(p, 0))
494#define isIDFIRST_LC_utf8(p) isIDFIRST_LC_uvchr(utf8_to_uvchr(p, 0))
495#define isALPHA_LC_utf8(p) isALPHA_LC_uvchr(utf8_to_uvchr(p, 0))
496#define isSPACE_LC_utf8(p) isSPACE_LC_uvchr(utf8_to_uvchr(p, 0))
497#define isDIGIT_LC_utf8(p) isDIGIT_LC_uvchr(utf8_to_uvchr(p, 0))
498#define isUPPER_LC_utf8(p) isUPPER_LC_uvchr(utf8_to_uvchr(p, 0))
499#define isLOWER_LC_utf8(p) isLOWER_LC_uvchr(utf8_to_uvchr(p, 0))
500#define isALNUMC_LC_utf8(p) isALNUMC_LC_uvchr(utf8_to_uvchr(p, 0))
501#define isCNTRL_LC_utf8(p) isCNTRL_LC_uvchr(utf8_to_uvchr(p, 0))
502#define isGRAPH_LC_utf8(p) isGRAPH_LC_uvchr(utf8_to_uvchr(p, 0))
503#define isPRINT_LC_utf8(p) isPRINT_LC_uvchr(utf8_to_uvchr(p, 0))
504#define isPUNCT_LC_utf8(p) isPUNCT_LC_uvchr(utf8_to_uvchr(p, 0))
a0ed51b3 505
aaa51d5e
JF
506#define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
507#define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
508
9d116dd7 509#ifdef EBCDIC
20b634c2
JH
510# ifdef PERL_IMPLICIT_CONTEXT
511# define toCTRL(c) Perl_ebcdic_control(aTHX_ c)
512# else
513# define toCTRL Perl_ebcdic_control
514# endif
9d116dd7
JH
515#else
516 /* This conversion works both ways, strangely enough. */
517# define toCTRL(c) (toUPPER(c) ^ 64)
518#endif
bbce6d69 519
dea28490
JJ
520/* Line numbers are unsigned, 32 bits. */
521typedef U32 line_t;
dea28490 522#define NOLINE ((line_t) 4294967295UL)
378cc40b 523
8c52afec 524
8e84507e 525/*
ccfc67b7
JH
526=head1 SV Manipulation Functions
527
954c1994
GS
528=for apidoc Am|SV*|NEWSV|int id|STRLEN len
529Creates a new SV. A non-zero C<len> parameter indicates the number of
530bytes of preallocated string space the SV should have. An extra byte for a
531tailing NUL is also reserved. (SvPOK is not set for the SV even if string
8e84507e 532space is allocated.) The reference count for the new SV is set to 1.
954c1994
GS
533C<id> is an integer id between 0 and 1299 (used to identify leaks).
534
ccfc67b7
JH
535=head1 Memory Management
536
a02a5408 537=for apidoc Am|void|Newx|void* ptr|int nitems|type
954c1994
GS
538The XSUB-writer's interface to the C C<malloc> function.
539
a02a5408 540=for apidoc Am|void|Newxc|void* ptr|int nitems|type|cast
954c1994
GS
541The XSUB-writer's interface to the C C<malloc> function, with
542cast.
543
a02a5408 544=for apidoc Am|void|Newxz|void* ptr|int nitems|type
954c1994
GS
545The XSUB-writer's interface to the C C<malloc> function. The allocated
546memory is zeroed with C<memzero>.
547
a02a5408
JC
548In 5.9.3, we removed the 1st parameter, a debug aid, from the api. It
549was used to uniquely identify each usage of these allocation
550functions, but was deemed unnecessary with the availability of better
551memory tracking tools, valgrind for example.
552
954c1994
GS
553=for apidoc Am|void|Renew|void* ptr|int nitems|type
554The XSUB-writer's interface to the C C<realloc> function.
555
556=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
557The XSUB-writer's interface to the C C<realloc> function, with
558cast.
559
49b8b560 560=for apidoc Am|void|Safefree|void* ptr
954c1994
GS
561The XSUB-writer's interface to the C C<free> function.
562
563=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
564The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
565source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
566the type. Can do overlapping moves. See also C<Copy>.
567
e90e2364
NC
568=for apidoc Am|void *|MoveD|void* src|void* dest|int nitems|type
569Like C<Move> but returns dest. Useful for encouraging compilers to tail-call
570optimise.
571
954c1994
GS
572=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
573The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
574source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
575the type. May fail on overlapping copies. See also C<Move>.
576
e90e2364
NC
577=for apidoc Am|void *|CopyD|void* src|void* dest|int nitems|type
578
579Like C<Copy> but returns dest. Useful for encouraging compilers to tail-call
580optimise.
581
954c1994
GS
582=for apidoc Am|void|Zero|void* dest|int nitems|type
583
584The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
585destination, C<nitems> is the number of items, and C<type> is the type.
586
e90e2364
NC
587=for apidoc Am|void *|ZeroD|void* dest|int nitems|type
588
589Like C<Zero> but returns dest. Useful for encouraging compilers to tail-call
590optimise.
591
954c1994 592=for apidoc Am|void|StructCopy|type src|type dest|type
4375e838 593This is an architecture-independent macro to copy one structure to another.
954c1994 594
9965345d
JH
595=for apidoc Am|void|Poison|void* dest|int nitems|type
596
597Fill up memory with a pattern (byte 0xAB over and over again) that
598hopefully catches attempts to access uninitialized memory.
599
600=cut */
954c1994 601
ff06c60c
IZ
602#define NEWSV(x,len) newSV(len)
603
27d5b266
JH
604#ifdef PERL_MALLOC_WRAP
605#define MEM_WRAP_CHECK(n,t) \
ed87b6ed 606 (void)((sizeof(t)>1?(n):1)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(PL_memory_wrap),0):0)
27d5b266 607#define MEM_WRAP_CHECK_1(n,t,a) \
ed87b6ed 608 (void)((sizeof(t)>1?(n):1)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a),0):0)
27d5b266 609#define MEM_WRAP_CHECK_2(n,t,a,b) \
ed87b6ed 610 (void)((sizeof(t)>1?(n):1)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a,b),0):0)
8b44ba4c 611#define MEM_WRAP_CHECK_(n,t) MEM_WRAP_CHECK(n,t),
27d5b266 612
6fff79ce 613#define PERL_STRLEN_ROUNDUP(n) ((void)(((n) > (MEM_SIZE)~0 - 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 614
27d5b266
JH
615#else
616
410319be
NC
617#define MEM_WRAP_CHECK(n,t)
618#define MEM_WRAP_CHECK_1(n,t,a)
619#define MEM_WRAP_CHECK_2(n,t,a,b)
8b44ba4c
NC
620#define MEM_WRAP_CHECK_(n,t)
621
6fff79ce 622#define PERL_STRLEN_ROUNDUP(n) (((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
27d5b266 623
1936d2a7 624#endif
8b44ba4c 625
fe4f188c
JH
626#ifdef PERL_MEM_LOG
627Malloc_t Perl_mem_log_alloc(const UV n, const UV typesize, const char *typename, Malloc_t newalloc, const char *filename, const int linenumber);
628Malloc_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);
629Malloc_t Perl_mem_log_free(Malloc_t oldalloc, const char *filename, const int linenumber);
630#endif
631
632#ifdef PERL_MEM_LOG
633#define MEM_LOG_ALLOC(n,t,a) Perl_mem_log_alloc(n,sizeof(t),STRINGIFY(t),a,__FILE__,__LINE__)
634#define MEM_LOG_REALLOC(n,t,v,a) Perl_mem_log_realloc(n,sizeof(t),STRINGIFY(t),v,a,__FILE__,__LINE__)
635#define MEM_LOG_FREE(a) Perl_mem_log_free(a,__FILE__,__LINE__)
636#endif
637
638#ifndef MEM_LOG_ALLOC
639#define MEM_LOG_ALLOC(n,t,a) (a)
640#endif
641#ifndef MEM_LOG_REALLOC
642#define MEM_LOG_REALLOC(n,t,v,a) (a)
643#endif
644#ifndef MEM_LOG_FREE
645#define MEM_LOG_FREE(a) (a)
646#endif
647
648#define Newx(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(t*)safemalloc((MEM_SIZE)((n)*sizeof(t))))))
649#define Newxc(v,n,t,c) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(c*)safemalloc((MEM_SIZE)((n)*sizeof(t))))))
650#define Newxz(v,n,t) (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_ALLOC(n,t,(t*)safemalloc((MEM_SIZE)((n)*sizeof(t)))))), \
ff68c719 651 memzero((char*)(v), (n)*sizeof(t))
a02a5408
JC
652/* pre 5.9.x compatibility */
653#define New(x,v,n,t) Newx(v,n,t)
654#define Newc(x,v,n,t,c) Newxc(v,n,t,c)
655#define Newc(x,v,n,t,c) Newxc(v,n,t,c)
656
ff68c719 657#define Renew(v,n,t) \
fe4f188c 658 (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_REALLOC(n,t,v,(t*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
ff68c719 659#define Renewc(v,n,t,c) \
fe4f188c 660 (v = (MEM_WRAP_CHECK_(n,t) MEM_LOG_REALLOC(n,t,v,(c*)saferealloc((Malloc_t)(v),(MEM_SIZE)((n)*sizeof(t))))))
94010e71
NC
661
662#ifdef PERL_POISON
663#define Safefree(d) \
fe4f188c 664 ((d) ? (void)(safefree(MEM_LOG_FREE((Malloc_t)(d)))), Poison(&(d), 1, Malloc_t)) : (void) 0)
94010e71 665#else
fe4f188c 666#define Safefree(d) safefree(MEM_LOG_FREE((Malloc_t)(d)))
94010e71 667#endif
55497cff 668
8b44ba4c
NC
669#define Move(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
670#define Copy(s,d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
671#define Zero(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memzero((char*)(d), (n) * sizeof(t)))
55497cff 672
8b44ba4c
NC
673#define MoveD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) memmove((char*)(d),(const char*)(s), (n) * sizeof(t)))
674#define CopyD(s,d,n,t) (MEM_WRAP_CHECK_(n,t) memcpy((char*)(d),(const char*)(s), (n) * sizeof(t)))
e90e2364 675#ifdef HAS_MEMSET
8b44ba4c 676#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)))
e90e2364 677#else
8b44ba4c
NC
678/* Using bzero(), which returns void. */
679#define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)),d)
e90e2364
NC
680#endif
681
8b44ba4c 682#define Poison(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), 0xAB, (n) * sizeof(t)))
27d5b266 683
2304df62 684#ifdef USE_STRUCT_COPY
ff68c719 685#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
bee1dbe2
LW
686#else
687#define StructCopy(s,d,t) Copy(s,d,1,t)
688#endif
2cc61e15 689
622913ab
IZ
690#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
691
2cc61e15
DD
692#ifdef NEED_VA_COPY
693# ifdef va_copy
694# define Perl_va_copy(s, d) va_copy(d, s)
2cc61e15 695# else
a1866d1b
JH
696# if defined(__va_copy)
697# define Perl_va_copy(s, d) __va_copy(d, s)
698# else
699# define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
700# endif
2cc61e15
DD
701# endif
702#endif
703
472d47bc
SB
704/* convenience debug macros */
705#ifdef USE_ITHREADS
706#define pTHX_FORMAT "Perl interpreter: 0x%p"
707#define pTHX__FORMAT ", Perl interpreter: 0x%p"
f54cb97a
AL
708#define pTHX_VALUE_ (void *)my_perl,
709#define pTHX_VALUE (void *)my_perl
710#define pTHX__VALUE_ ,(void *)my_perl,
711#define pTHX__VALUE ,(void *)my_perl
472d47bc
SB
712#else
713#define pTHX_FORMAT
714#define pTHX__FORMAT
715#define pTHX_VALUE_
716#define pTHX_VALUE
717#define pTHX__VALUE_
718#define pTHX__VALUE
719#endif /* USE_ITHREADS */