3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
4 * 2000, 2001, 2002, 2004, 2005 by Larry Wall and others
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.
11 #if !defined(__STDC__)
22 #define Null(type) ((type)NULL)
27 =for apidoc AmU||Nullch
28 Null character pointer.
30 =for apidoc AmU||Nullsv
36 #define Nullch Null(char*)
37 #define Nullfp Null(PerlIO*)
38 #define Nullsv Null(SV*)
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
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
62 #ifdef __GNUG__ /* GNU g++ has bool built-in */
68 /* The NeXT dynamic loader headers will not build with the bool macro
69 So declare them now to clear confusion.
71 #if defined(NeXT) || defined(__NeXT__)
74 typedef enum bool { FALSE = 0, TRUE = 1 } bool;
78 # endif /* !HAS_BOOL */
79 #endif /* NeXT || __NeXT__ */
82 # if defined(UTS) || defined(VMS)
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__
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__ ""
99 # define FUNCTION__ __FUNCTION__ /* Common extension. */
103 /* XXX A note on the perl source internal type system. The
104 original intent was that I32 be *exactly* 32 bits.
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
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.
117 Similarly, there is no guarantee that I16 and U16 have exactly 16
120 For dealing with issues that may arise from various 32/64-bit
121 systems, we will ask Configure to check out
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).
133 #ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
134 # include <inttypes.h>
135 # ifdef INT32_MIN_BROKEN
137 # define INT32_MIN (-2147483647-1)
139 # ifdef INT64_MIN_BROKEN
141 # define INT64_MIN (-9223372036854775807LL-1)
156 #endif /* PERL_CORE */
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)
164 # if LONGSIZE == 8 && QUADKIND == QUAD_IS_LONG
165 # define INT64_C(c) CAT2(c,L)
166 # define UINT64_C(c) CAT2(c,UL)
168 # define INT64_C(c) ((I64TYPE)(c))
169 # define UINT64_C(c) ((U64TYPE)(c))
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 */
180 /* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
181 I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
183 #if defined(UINT8_MAX) && defined(INT16_MAX) && defined(INT32_MAX)
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
190 #define I16_MAX INT16_MAX
191 #define I16_MIN INT16_MIN
192 #define U16_MAX UINT16_MAX
193 #define U16_MIN UINT16_MIN
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
200 # define U32_MAX 4294967295U
202 #define U32_MIN UINT32_MIN
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
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
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
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
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 */
235 #define Ctl(ch) ((ch) & 037)
238 =head1 Miscellaneous Functions
240 =for apidoc Am|bool|strNE|char* s1|char* s2
241 Test two strings to see if they are different. Returns true or
244 =for apidoc Am|bool|strEQ|char* s1|char* s2
245 Test two strings to see if they are equal. Returns true or false.
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.
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.
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.
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.
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>).
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
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))
286 # define memNE(s1,s2,l) (memcmp(s1,s2,l))
287 # define memEQ(s1,s2,l) (!memcmp(s1,s2,l))
289 # define memNE(s1,s2,l) (bcmp(s1,s2,l))
290 # define memEQ(s1,s2,l) (!bcmp(s1,s2,l))
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.
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".
306 #ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
314 =head1 Character classes
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.
320 =for apidoc Am|bool|isALPHA|char ch
321 Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
324 =for apidoc Am|bool|isSPACE|char ch
325 Returns a boolean indicating whether the C C<char> is whitespace.
327 =for apidoc Am|bool|isDIGIT|char ch
328 Returns a boolean indicating whether the C C<char> is an ASCII
331 =for apidoc Am|bool|isUPPER|char ch
332 Returns a boolean indicating whether the C C<char> is an uppercase
335 =for apidoc Am|bool|isLOWER|char ch
336 Returns a boolean indicating whether the C C<char> is a lowercase
339 =for apidoc Am|char|toUPPER|char ch
340 Converts the specified character to uppercase.
342 =for apidoc Am|char|toLOWER|char ch
343 Converts the specified character to lowercase.
348 #define isALNUM(c) (isALPHA(c) || isDIGIT(c) || (c) == '_')
349 #define isIDFIRST(c) (isALPHA(c) || (c) == '_')
350 #define isALPHA(c) (isUPPER(c) || isLOWER(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')
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)
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))
383 #ifdef USE_NEXT_CTYPE
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))
402 #else /* !USE_NEXT_CTYPE */
404 # if defined(CTYPE256) || (!defined(isascii) && !defined(HAS_ISASCII))
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))
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)
439 #endif /* USE_NEXT_CTYPE */
441 #define isPSXSPC_LC(c) (isSPACE_LC(c) || (c) == '\v')
442 #define isBLANK_LC(c) isBLANK(c) /* could be wrong */
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)
463 #define isPSXSPC_uni(c) (isSPACE_uni(c) ||(c) == '\f')
464 #define isBLANK_uni(c) isBLANK(c) /* could be wrong */
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))
479 #define isPSXSPC_LC_uni(c) (isSPACE_LC_uni(c) ||(c) == '\f')
480 #define isBLANK_LC_uni(c) isBLANK(c) /* could be wrong */
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)
503 #define isPSXSPC_utf8(c) (isSPACE_utf8(c) ||(c) == '\f')
504 #define isBLANK_utf8(c) isBLANK(c) /* could be wrong */
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))
519 #define isPSXSPC_LC_utf8(c) (isSPACE_LC_utf8(c) ||(c) == '\f')
520 #define isBLANK_LC_utf8(c) isBLANK(c) /* could be wrong */
523 # ifdef PERL_IMPLICIT_CONTEXT
524 # define toCTRL(c) Perl_ebcdic_control(aTHX_ c)
526 # define toCTRL Perl_ebcdic_control
529 /* This conversion works both ways, strangely enough. */
530 # define toCTRL(c) (toUPPER(c) ^ 64)
533 /* Line numbers are unsigned, 32 bits. */
535 #define NOLINE ((line_t) 4294967295UL)
539 =head1 SV Manipulation Functions
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).
548 =head1 Memory Management
550 =for apidoc Am|void|Newx|void* ptr|int nitems|type
551 The XSUB-writer's interface to the C C<malloc> function.
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.
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>.
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>.
567 =for apidoc Am|void|Renew|void* ptr|int nitems|type
568 The XSUB-writer's interface to the C C<realloc> function.
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
574 =for apidoc Am|void|Safefree|void* ptr
575 The XSUB-writer's interface to the C C<free> function.
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>.
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
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>.
591 =for apidoc Am|void *|CopyD|void* src|void* dest|int nitems|type
593 Like C<Copy> but returns dest. Useful for encouraging compilers to tail-call
596 =for apidoc Am|void|Zero|void* dest|int nitems|type
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.
601 =for apidoc Am|void *|ZeroD|void* dest|int nitems|type
603 Like C<Zero> but returns dest. Useful for encouraging compilers to tail-call
606 =for apidoc Am|void|StructCopy|type src|type dest|type
607 This is an architecture-independent macro to copy one structure to another.
609 =for apidoc Am|void|Poison|void* dest|int nitems|type
611 Fill up memory with a pattern (byte 0xAB over and over again) that
612 hopefully catches attempts to access uninitialized memory.
616 #define NEWSV(x,len) newSV(len)
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),
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)))
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)
636 #define PERL_STRLEN_ROUNDUP(n) (((n-1+PERL_STRLEN_ROUNDUP_QUANTUM)&~((MEM_SIZE)PERL_STRLEN_ROUNDUP_QUANTUM-1)))
642 * If PERL_MEM_LOG is defined, all New()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
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
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...?
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);
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);
675 Malloc_t Perl_mem_log_free(Malloc_t oldalloc, const char *filename, const int linenumber, const char *funcname);
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__)
685 #ifndef MEM_LOG_ALLOC
686 #define MEM_LOG_ALLOC(n,t,a) (a)
688 #ifndef MEM_LOG_REALLOC
689 #define MEM_LOG_REALLOC(n,t,v,a) (a)
692 #define MEM_LOG_FREE(a) (a)
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)
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))))))
710 #define Safefree(d) \
711 ((d) ? (void)(safefree(MEM_LOG_FREE((Malloc_t)(d)))), Poison(&(d), 1, Malloc_t)) : (void) 0)
713 #define Safefree(d) safefree(MEM_LOG_FREE((Malloc_t)(d)))
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)))
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)))
723 #define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)))
725 /* Using bzero(), which returns void. */
726 #define ZeroD(d,n,t) (MEM_WRAP_CHECK_(n,t) memzero((char*)(d), (n) * sizeof(t)),d)
729 #define Poison(d,n,t) (MEM_WRAP_CHECK_(n,t) (void)memset((char*)(d), 0xAB, (n) * sizeof(t)))
731 #ifdef USE_STRUCT_COPY
732 #define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
734 #define StructCopy(s,d,t) Copy(s,d,1,t)
737 #define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
741 # define Perl_va_copy(s, d) va_copy(d, s)
743 # if defined(__va_copy)
744 # define Perl_va_copy(s, d) __va_copy(d, s)
746 # define Perl_va_copy(s, d) Copy(s, d, 1, va_list)
751 /* convenience debug macros */
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
766 #endif /* USE_ITHREADS */