/* handy.h
*
* Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1999,
* 2000, 2001, 2002, 2004, by Larry Wall and others
*
* You may distribute under the terms of either the GNU General Public
* License or the Artistic License, as specified in the README file.
*
*/
#if !defined(__STDC__)
#ifdef NULL
#endif
#ifndef I286
# define NULL 0
#else
# define NULL 0L
#endif
#endif
/*
=head1 Handy Values
=for apidoc AmU||Nullch
Null character pointer.
=for apidoc AmU||Nullsv
Null SV pointer.
=cut
*/
#ifdef TRUE
#endif
#ifdef FALSE
#endif
#define FALSE (0)
/* XXX Configure ought to have a test for a boolean type, if I can
just figure out all the headers such a test needs.
Andy Dougherty August 1996
*/
/* bool is built-in for g++-2.6.3 and later, which might be used
for extensions. <_G_config.h> defines _G_HAVE_BOOL, but we can't
be sure _G_config.h will be included before this file. _G_config.h
also defines _G_HAVE_BOOL for both gcc and g++, but only g++
actually has bool. Hence, _G_HAVE_BOOL is pretty useless for us.
g++ can be identified by __GNUG__.
Andy Dougherty February 2000
*/
#ifdef __GNUG__ /* GNU g++ has bool built-in */
# ifndef HAS_BOOL
# endif
#endif
/* The NeXT dynamic loader headers will not build with the bool macro
So declare them now to clear confusion.
*/
# ifndef HAS_BOOL
# endif /* !HAS_BOOL */
#endif /* NeXT || __NeXT__ */
#ifndef HAS_BOOL
# define bool int
# else
# define bool char
# endif
#endif
/* XXX A note on the perl source internal type system. The
original intent was that I32 be *exactly* 32 bits.
Currently, we only guarantee that I32 is *at least* 32 bits.
Specifically, if int is 64 bits, then so is I32. (This is the case
for the Cray.) This has the advantage of meshing nicely with
standard library calls (where we pass an I32 and the library is
expecting an int), but the disadvantage that an I32 is not 32 bits.
Andy Dougherty August 1996
There is no guarantee that there is *any* integral type with
exactly 32 bits. It is perfectly legal for a system to have
sizeof(short) == sizeof(int) == sizeof(long) == 8.
Similarly, there is no guarantee that I16 and U16 have exactly 16
bits.
For dealing with issues that may arise from various 32/64-bit
systems, we will ask Configure to check out
SHORTSIZE == sizeof(short)
INTSIZE == sizeof(int)
LONGSIZE == sizeof(long)
LONGLONGSIZE == sizeof(long long) (if HAS_LONG_LONG)
PTRSIZE == sizeof(void *)
DOUBLESIZE == sizeof(double)
LONG_DOUBLESIZE == sizeof(long double) (if HAS_LONG_DOUBLE).
*/
#ifdef I_INTTYPES /* e.g. Linux has int64_t without <inttypes.h> */
# include <inttypes.h>
# ifdef INT32_MIN_BROKEN
# endif
# ifdef INT64_MIN_BROKEN
# endif
#endif
#ifdef PERL_CORE
# ifdef HAS_QUAD
# endif
#endif /* PERL_CORE */
#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
# ifndef UINT64_C /* usually from <inttypes.h> */
# else
# else
# endif
# endif
# endif
#endif
/* Mention I8SIZE, U8SIZE, I16SIZE, U16SIZE, I32SIZE, U32SIZE,
I64SIZE, and U64SIZE here so that metaconfig pulls them in. */
/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
Please search CHAR_MAX in perl.h for further details. */
#ifndef UINT32_MAX_BROKEN /* e.g. HP-UX with gcc messes this up */
#else
#endif
#else
/* I8_MAX and I8_MIN constants are not defined, as I8 is an ambiguous type.
Please search CHAR_MAX in perl.h for further details. */
#if LONGSIZE > 4
#else
#endif
#endif
/* log(2) is pretty close to 0.30103, just in case anyone is grepping for it */
/*
=head1 Miscellaneous Functions
=for apidoc Am|bool|strNE|char* s1|char* s2
Test two strings to see if they are different. Returns true or
false.
=for apidoc Am|bool|strEQ|char* s1|char* s2
Test two strings to see if they are equal. Returns true or false.
=for apidoc Am|bool|strLT|char* s1|char* s2
Test two strings to see if the first, C<s1>, is less than the second,
C<s2>. Returns true or false.
=for apidoc Am|bool|strLE|char* s1|char* s2
Test two strings to see if the first, C<s1>, is less than or equal to the
second, C<s2>. Returns true or false.
=for apidoc Am|bool|strGT|char* s1|char* s2
Test two strings to see if the first, C<s1>, is greater than the second,
C<s2>. Returns true or false.
=for apidoc Am|bool|strGE|char* s1|char* s2
Test two strings to see if the first, C<s1>, is greater than or equal to
the second, C<s2>. Returns true or false.
=for apidoc Am|bool|strnNE|char* s1|char* s2|STRLEN len
Test two strings to see if they are different. The C<len> parameter
indicates the number of bytes to compare. Returns true or false. (A
wrapper for C<strncmp>).
=for apidoc Am|bool|strnEQ|char* s1|char* s2|STRLEN len
Test two strings to see if they are equal. The C<len> parameter indicates
the number of bytes to compare. Returns true or false. (A wrapper for
C<strncmp>).
=cut
*/
#ifdef HAS_MEMCMP
#else
#endif
/*
* Character classes.
*
* Unfortunately, the introduction of locales means that we
* can't trust isupper(), etc. to tell the truth. And when
* it comes to /\w+/ with tainting enabled, we *must* be able
* to trust our character classes.
*
* Therefore, the default tests in the text of Perl will be
* independent of locale. Any code that wants to depend on
* the current locale will use the tests that begin with "lc".
*/
#ifdef HAS_SETLOCALE /* XXX Is there a better test for this? */
# ifndef CTYPE256
# define CTYPE256
# endif
#endif
/*
=head1 Character classes
=for apidoc Am|bool|isALNUM|char ch
Returns a boolean indicating whether the C C<char> is an ASCII alphanumeric
character (including underscore) or digit.
=for apidoc Am|bool|isALPHA|char ch
Returns a boolean indicating whether the C C<char> is an ASCII alphabetic
character.
=for apidoc Am|bool|isSPACE|char ch
Returns a boolean indicating whether the C C<char> is whitespace.
=for apidoc Am|bool|isDIGIT|char ch
Returns a boolean indicating whether the C C<char> is an ASCII
digit.
=for apidoc Am|bool|isUPPER|char ch
Returns a boolean indicating whether the C C<char> is an uppercase
character.
=for apidoc Am|bool|isLOWER|char ch
Returns a boolean indicating whether the C C<char> is a lowercase
character.
=for apidoc Am|char|toUPPER|char ch
Converts the specified character to uppercase.
=for apidoc Am|char|toLOWER|char ch
Converts the specified character to lowercase.
=cut
*/
#define isSPACE(c) \
((c) == ' ' || (c) == '\t' || (c) == '\n' || (c) =='\r' || (c) == '\f')
#ifdef EBCDIC
/* In EBCDIC we do not do locales: therefore() isupper() is fine. */
#else
# define isPUNCT(c) (((c) >= 33 && (c) <= 47) || ((c) >= 58 && (c) <= 64) || ((c) >= 91 && (c) <= 96) || ((c) >= 123 && (c) <= 126))
#endif
#ifdef USE_NEXT_CTYPE
# define isALNUM_LC(c) \
(NXIsAlNum((unsigned int)(c)) || (char)(c) == '_')
# define isIDFIRST_LC(c) \
(NXIsAlpha((unsigned int)(c)) || (char)(c) == '_')
#else /* !USE_NEXT_CTYPE */
# else
# endif
#endif /* USE_NEXT_CTYPE */
/* The ID_Start of Unicode is quite limiting: it assumes a L-class
* character (meaning that you cannot have, say, a CJK character).
* Instead, let's allow ID_Continue but not digits. */
#ifdef EBCDIC
# ifdef PERL_IMPLICIT_CONTEXT
# else
# endif
#else
/* This conversion works both ways, strangely enough. */
#endif
/* Line numbers are unsigned, 32 bits. */
#ifdef lint
#else
#endif
/*
=head1 SV Manipulation Functions
=for apidoc Am|SV*|NEWSV|int id|STRLEN len
Creates a new SV. A non-zero C<len> parameter indicates the number of
bytes of preallocated string space the SV should have. An extra byte for a
tailing NUL is also reserved. (SvPOK is not set for the SV even if string
space is allocated.) The reference count for the new SV is set to 1.
C<id> is an integer id between 0 and 1299 (used to identify leaks).
=head1 Memory Management
=for apidoc Am|void|New|int id|void* ptr|int nitems|type
The XSUB-writer's interface to the C C<malloc> function.
=for apidoc Am|void|Newc|int id|void* ptr|int nitems|type|cast
The XSUB-writer's interface to the C C<malloc> function, with
cast.
=for apidoc Am|void|Newz|int id|void* ptr|int nitems|type
The XSUB-writer's interface to the C C<malloc> function. The allocated
memory is zeroed with C<memzero>.
=for apidoc Am|void|Renew|void* ptr|int nitems|type
The XSUB-writer's interface to the C C<realloc> function.
=for apidoc Am|void|Renewc|void* ptr|int nitems|type|cast
The XSUB-writer's interface to the C C<realloc> function, with
cast.
=for apidoc Am|void|Safefree|void* ptr
The XSUB-writer's interface to the C C<free> function.
=for apidoc Am|void|Move|void* src|void* dest|int nitems|type
The XSUB-writer's interface to the C C<memmove> function. The C<src> is the
source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
the type. Can do overlapping moves. See also C<Copy>.
=for apidoc Am|void|Copy|void* src|void* dest|int nitems|type
The XSUB-writer's interface to the C C<memcpy> function. The C<src> is the
source, C<dest> is the destination, C<nitems> is the number of items, and C<type> is
the type. May fail on overlapping copies. See also C<Move>.
=for apidoc Am|void|Zero|void* dest|int nitems|type
The XSUB-writer's interface to the C C<memzero> function. The C<dest> is the
destination, C<nitems> is the number of items, and C<type> is the type.
=for apidoc Am|void|StructCopy|type src|type dest|type
This is an architecture-independent macro to copy one structure to another.
=for apidoc Am|void|Poison|void* dest|int nitems|type
Fill up memory with a pattern (byte 0xAB over and over again) that
hopefully catches attempts to access uninitialized memory.
=cut */
#ifndef lint
#ifdef PERL_MALLOC_WRAP
#define MEM_WRAP_CHECK(n,t) \
#define MEM_WRAP_CHECK_1(n,t,a) \
(void)((n)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a),0):0)
#define MEM_WRAP_CHECK_2(n,t,a,b) \
(void)((n)>((MEM_SIZE)~0)/sizeof(t)?(Perl_croak_nocontext(a,b),0):0)
memzero((char*)(v), (n)*sizeof(t))
#define Renew(v,n,t) \
#define Renewc(v,n,t,c) \
#else
#define MEM_WRAP_CHECK(n,t)
#define MEM_WRAP_CHECK_1(n,t,a)
#define MEM_WRAP_CHECK_2(n,t,a,b)
memzero((char*)(v), (n)*sizeof(t))
#define Renew(v,n,t) \
#define Renewc(v,n,t,c) \
#endif
#else /* lint */
#define Move(s,d,n,t)
#define Copy(s,d,n,t)
#define Zero(d,n,t)
#define Poison(d,n,t)
#define Safefree(d) (d) = (d)
#endif /* lint */
#ifdef USE_STRUCT_COPY
#define StructCopy(s,d,t) (*((t*)(d)) = *((t*)(s)))
#else
#endif
#define C_ARRAY_LENGTH(a) (sizeof(a)/sizeof((a)[0]))
#ifdef NEED_VA_COPY
# ifdef va_copy
# else
# if defined(__va_copy)
# else
# endif
# endif
#endif