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