This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate Memoize 0.64. Few tweaks were required in
[perl5.git] / sv.h
diff --git a/sv.h b/sv.h
index d8cd487..034b51e 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -1,6 +1,6 @@
 /*    sv.h
  *
- *    Copyright (c) 1991-1999, Larry Wall
+ *    Copyright (c) 1991-2001, Larry Wall
  *
  *    You may distribute under the terms of either the GNU General Public
  *    License or the Artistic License, as specified in the README file.
@@ -61,7 +61,7 @@ typedef enum {
 
 /* Using C's structural equivalence to help emulate C++ inheritance here... */
 
-struct sv {
+struct STRUCT_SV {
     void*      sv_any;         /* pointer to something */
     U32                sv_refcnt;      /* how many references to us */
     U32                sv_flags;       /* what we are */
@@ -123,21 +123,26 @@ perform the upgrade if necessary.  See C<svtype>.
 
 #ifdef USE_THREADS
 
-#  ifdef EMULATE_ATOMIC_REFCOUNTS
-#    define ATOMIC_INC(count) STMT_START {     \
-       MUTEX_LOCK(&PL_svref_mutex);            \
-       ++count;                                \
-       MUTEX_UNLOCK(&PL_svref_mutex);          \
-     } STMT_END
-#    define ATOMIC_DEC_AND_TEST(res,count) STMT_START {        \
-       MUTEX_LOCK(&PL_svref_mutex);                    \
-       res = (--count == 0);                           \
-       MUTEX_UNLOCK(&PL_svref_mutex);                  \
-     } STMT_END
-#  else
-#    define ATOMIC_INC(count) atomic_inc(&count)
-#    define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
-#  endif /* EMULATE_ATOMIC_REFCOUNTS */
+#  if defined(VMS)
+#    define ATOMIC_INC(count) __ATOMIC_INCREMENT_LONG(&count)
+#    define ATOMIC_DEC_AND_TEST(res,count) res=(1==__ATOMIC_DECREMENT_LONG(&count))
+ #  else
+#    ifdef EMULATE_ATOMIC_REFCOUNTS
+ #      define ATOMIC_INC(count) STMT_START {  \
+         MUTEX_LOCK(&PL_svref_mutex);          \
+         ++count;                              \
+         MUTEX_UNLOCK(&PL_svref_mutex);                \
+       } STMT_END
+#      define ATOMIC_DEC_AND_TEST(res,count) STMT_START {      \
+         MUTEX_LOCK(&PL_svref_mutex);                  \
+         res = (--count == 0);                         \
+         MUTEX_UNLOCK(&PL_svref_mutex);                        \
+       } STMT_END
+#    else
+#      define ATOMIC_INC(count) atomic_inc(&count)
+#      define ATOMIC_DEC_AND_TEST(res,count) (res = atomic_dec_and_test(&count))
+#    endif /* EMULATE_ATOMIC_REFCOUNTS */
+#  endif /* VMS */
 #else
 #  define ATOMIC_INC(count) (++count)
 #  define ATOMIC_DEC_AND_TEST(res, count) (res = (--count == 0))
@@ -153,7 +158,12 @@ perform the upgrade if necessary.  See C<svtype>.
     })
 #else
 #  if defined(CRIPPLED_CC) || defined(USE_THREADS)
-#    define SvREFCNT_inc(sv) sv_newref((SV*)sv)
+#    if defined(VMS) && defined(__ALPHA)
+#      define SvREFCNT_inc(sv) \
+          (PL_Sv=(SV*)(sv), (PL_Sv && __ATOMIC_INCREMENT_LONG(&(SvREFCNT(PL_Sv)))), (SV *)PL_Sv)
+#    else
+#      define SvREFCNT_inc(sv) sv_newref((SV*)sv)
+#    endif
 #  else
 #    define SvREFCNT_inc(sv)   \
        ((PL_Sv=(SV*)(sv)), (PL_Sv && ATOMIC_INC(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
@@ -194,7 +204,7 @@ perform the upgrade if necessary.  See C<svtype>.
 
 #define SVf_UTF8        0x20000000      /* SvPVX is UTF-8 encoded */
 
-#define SVf_THINKFIRST (SVf_READONLY|SVf_ROK|SVf_FAKE|SVf_UTF8)
+#define SVf_THINKFIRST (SVf_READONLY|SVf_ROK|SVf_FAKE)
 
 #define SVf_OK         (SVf_IOK|SVf_NOK|SVf_POK|SVf_ROK| \
                         SVp_IOK|SVp_NOK|SVp_POK)
@@ -207,6 +217,7 @@ perform the upgrade if necessary.  See C<svtype>.
 
 /* SVpad_OUR may be set on SVt_PV{NV,MG,GV} types */
 #define SVpad_OUR      0x80000000      /* pad name is "our" instead of "my" */
+#define SVpad_TYPED    0x40000000      /* Typed Lexical */
 
 #define SVf_IVisUV     0x80000000      /* use XPVUV instead of XPVIV */
 
@@ -353,7 +364,19 @@ struct xpvio {
 
     PerlIO *   xio_ifp;        /* ifp and ofp are normally the same */
     PerlIO *   xio_ofp;        /* but sockets need separate streams */
-    DIR *      xio_dirp;       /* for opendir, readdir, etc */
+    /* Cray addresses everything by word boundaries (64 bits) and
+     * code and data pointers cannot be mixed (which is exactly what
+     * Perl_filter_add() tries to do with the dirp), hence the following
+     * union trick (as suggested by Gurusamy Sarathy).
+     * For further information see Geir Johansen's problem report titled
+       [ID 20000612.002] Perl problem on Cray system
+     * The any pointer (known as IoANY()) will also be a good place
+     * to hang any IO disciplines to.
+     */
+    union {
+       DIR *   xiou_dirp;      /* for opendir, readdir, etc */
+       void *  xiou_any;       /* for alignment */
+    } xio_dirpu;
     long       xio_lines;      /* $. */
     long       xio_page;       /* $% */
     long       xio_page_len;   /* $= */
@@ -368,6 +391,8 @@ struct xpvio {
     char       xio_type;
     char       xio_flags;
 };
+#define xio_dirp       xio_dirpu.xiou_dirp
+#define xio_any                xio_dirpu.xiou_any
 
 #define IOf_ARGV       1       /* this fp iterates over ARGV */
 #define IOf_START      2       /* check for null ARGV and substitute '-' */
@@ -418,6 +443,18 @@ Unsets the IV status of an SV.
 =for apidoc Am|void|SvIOK_only|SV* sv
 Tells an SV that it is an integer and disables all other OK bits.
 
+=for apidoc Am|void|SvIOK_only_UV|SV* sv
+Tells and SV that it is an unsigned integer and disables all other OK bits.
+
+=for apidoc Am|void|SvIOK_UV|SV* sv
+Returns a boolean indicating whether the SV contains an unsigned integer.
+
+=for apidoc Am|void|SvUOK|SV* sv
+Returns a boolean indicating whether the SV contains an unsigned integer.
+
+=for apidoc Am|void|SvIOK_notUV|SV* sv
+Returns a boolean indicating whether the SV contains an signed integer.
+
 =for apidoc Am|bool|SvNOK|SV* sv
 Returns a boolean indicating whether the SV contains a double.
 
@@ -442,6 +479,7 @@ Unsets the PV status of an SV.
 
 =for apidoc Am|void|SvPOK_only|SV* sv
 Tells an SV that it is a string and disables all other OK bits.
+Will also turn off the UTF8 status.
 
 =for apidoc Am|bool|SvOOK|SV* sv
 Returns a boolean indicating whether the SvIVX is a valid offset value for
@@ -481,7 +519,8 @@ string.
 Returns the length of the string which is in the SV.  See C<SvLEN>.
 
 =for apidoc Am|STRLEN|SvLEN|SV* sv
-Returns the size of the string buffer in the SV.  See C<SvCUR>.
+Returns the size of the string buffer in the SV, not including any part
+attributable to C<SvOOK>.  See C<SvCUR>.
 
 =for apidoc Am|char*|SvEND|SV* sv
 Returns a pointer to the last character in the string which is in the SV.
@@ -511,23 +550,24 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
 
 #define SvOKp(sv)              (SvFLAGS(sv) & (SVp_IOK|SVp_NOK|SVp_POK))
 #define SvIOKp(sv)             (SvFLAGS(sv) & SVp_IOK)
-#define SvIOKp_on(sv)          (SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
+#define SvIOKp_on(sv)          ((void)SvOOK_off(sv), SvFLAGS(sv) |= SVp_IOK)
 #define SvNOKp(sv)             (SvFLAGS(sv) & SVp_NOK)
 #define SvNOKp_on(sv)          (SvFLAGS(sv) |= SVp_NOK)
 #define SvPOKp(sv)             (SvFLAGS(sv) & SVp_POK)
 #define SvPOKp_on(sv)          (SvFLAGS(sv) |= SVp_POK)
 
 #define SvIOK(sv)              (SvFLAGS(sv) & SVf_IOK)
-#define SvIOK_on(sv)           (SvOOK_off(sv), \
+#define SvIOK_on(sv)           ((void)SvOOK_off(sv), \
                                    SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
 #define SvIOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_IOK|SVp_IOK|SVf_IVisUV))
-#define SvIOK_only(sv)         (SvOK_off(sv), \
+#define SvIOK_only(sv)         ((void)SvOK_off(sv), \
                                    SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
-#define SvIOK_only_UV(sv)      (SvOK_off_exc_UV(sv), \
+#define SvIOK_only_UV(sv)      ((void)SvOK_off_exc_UV(sv), \
                                    SvFLAGS(sv) |= (SVf_IOK|SVp_IOK))
 
 #define SvIOK_UV(sv)           ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
                                 == (SVf_IOK|SVf_IVisUV))
+#define SvUOK(sv)              SvIOK_UV(sv)
 #define SvIOK_notUV(sv)                ((SvFLAGS(sv) & (SVf_IOK|SVf_IVisUV))   \
                                 == SVf_IOK)
 
@@ -538,9 +578,27 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
 #define SvNOK(sv)              (SvFLAGS(sv) & SVf_NOK)
 #define SvNOK_on(sv)           (SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
 #define SvNOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_NOK|SVp_NOK))
-#define SvNOK_only(sv)         (SvOK_off(sv), \
+#define SvNOK_only(sv)         ((void)SvOK_off(sv), \
                                    SvFLAGS(sv) |= (SVf_NOK|SVp_NOK))
 
+/*
+=for apidoc Am|void|SvUTF8|SV* sv
+Returns a boolean indicating whether the SV contains UTF-8 encoded data.
+
+=for apidoc Am|void|SvUTF8_on|SV *sv
+Turn on the UTF8 status of an SV (the data is not changed, just the flag).
+Do not use frivolously.
+
+=for apidoc Am|void|SvUTF8_off|SV *sv
+Unsets the UTF8 status of an SV.
+
+=for apidoc Am|void|SvPOK_only_UTF8|SV* sv
+Tells an SV that it is a string and disables all other OK bits,
+and leaves the UTF8 status as it was.
+
+=cut
+ */
+
 #define SvUTF8(sv)             (SvFLAGS(sv) & SVf_UTF8)
 #define SvUTF8_on(sv)          (SvFLAGS(sv) |= (SVf_UTF8))
 #define SvUTF8_off(sv)         (SvFLAGS(sv) &= ~(SVf_UTF8))
@@ -556,7 +614,7 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
                                    SvFLAGS(sv) |= (SVf_POK|SVp_POK))
 
 #define SvOOK(sv)              (SvFLAGS(sv) & SVf_OOK)
-#define SvOOK_on(sv)           (SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
+#define SvOOK_on(sv)           ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
 #define SvOOK_off(sv)          (SvOOK(sv) && sv_backoff(sv))
 
 #define SvFAKE(sv)             (SvFLAGS(sv) & SVf_FAKE)
@@ -587,6 +645,8 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
 #define SvAMAGIC_on(sv)                (SvFLAGS(sv) |= SVf_AMAGIC)
 #define SvAMAGIC_off(sv)       (SvFLAGS(sv) &= ~SVf_AMAGIC)
 
+#define SvGAMAGIC(sv)           (SvFLAGS(sv) & (SVs_GMG|SVf_AMAGIC)) 
+
 /*
 #define Gv_AMG(stash) \
         (HV_AMAGICmb(stash) && \
@@ -661,6 +721,12 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
 #define SvMAGIC(sv)    ((XPVMG*)  SvANY(sv))->xmg_magic
 #define SvSTASH(sv)    ((XPVMG*)  SvANY(sv))->xmg_stash
 
+/* Ask a scalar nicely to try to become an IV, if possible.
+   Not guaranteed to stay returning void */
+/* Macro won't actually call sv_2iv if already IOK */
+#define SvIV_please(sv) \
+       STMT_START {if (!SvIOKp(sv) && (SvNOK(sv) || SvPOK(sv))) \
+               (void) SvIV(sv); } STMT_END
 #define SvIV_set(sv, val) \
        STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
                (((XPVIV*)  SvANY(sv))->xiv_iv = val); } STMT_END
@@ -694,6 +760,7 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
 #define IoIFP(sv)      ((XPVIO*)  SvANY(sv))->xio_ifp
 #define IoOFP(sv)      ((XPVIO*)  SvANY(sv))->xio_ofp
 #define IoDIRP(sv)     ((XPVIO*)  SvANY(sv))->xio_dirp
+#define IoANY(sv)      ((XPVIO*)  SvANY(sv))->xio_any
 #define IoLINES(sv)    ((XPVIO*)  SvANY(sv))->xio_lines
 #define IoPAGE(sv)     ((XPVIO*)  SvANY(sv))->xio_page
 #define IoPAGE_LEN(sv) ((XPVIO*)  SvANY(sv))->xio_page_len
@@ -708,6 +775,16 @@ Set the length of the string which is in the SV.  See C<SvCUR>.
 #define IoTYPE(sv)     ((XPVIO*)  SvANY(sv))->xio_type
 #define IoFLAGS(sv)    ((XPVIO*)  SvANY(sv))->xio_flags
 
+/* IoTYPE(sv) is a single character telling the type of I/O connection. */
+#define IoTYPE_RDONLY  '<'
+#define IoTYPE_WRONLY  '>'
+#define IoTYPE_RDWR    '+'
+#define IoTYPE_APPEND  'a'
+#define IoTYPE_PIPE    '|'
+#define IoTYPE_STD     '-'     /* stdin or stdout */
+#define IoTYPE_SOCKET  's'
+#define IoTYPE_CLOSED  ' '
+
 /*
 =for apidoc Am|bool|SvTAINTED|SV* sv
 Checks to see if an SV is tainted. Returns TRUE if it is, FALSE if
@@ -737,7 +814,6 @@ Taints an SV if tainting is enabled
 #define SvTAINT(sv)                    \
     STMT_START {                       \
        if (PL_tainting) {              \
-           dTHR;                       \
            if (PL_tainted)             \
                SvTAINTED_on(sv);       \
        }                               \
@@ -815,16 +891,51 @@ false, defined or undefined.  Does not handle 'get' magic.
 #undef SvNV
 #define SvNV(sv) (SvNOK(sv) ? SvNVX(sv) : sv_2nv(sv))
 
+/* flag values for sv_*_flags functions */
+#define SV_IMMEDIATE_UNREF     1
+#define SV_GMAGIC              2
+
+#define sv_setsv_macro(dsv, ssv) sv_setsv_flags(dsv, ssv, SV_GMAGIC)
+#define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
+#define sv_catsv_macro(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
+#define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
+#define sv_catpvn_macro(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
+#define sv_catpvn_nomg(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, 0)
+#define sv_2pv_macro(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
+#define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
+#define sv_pvn_force_macro(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
+#define sv_pvn_force_nomg(sv, lp) sv_pvn_force_flags(sv, lp, 0)
+#define sv_utf8_upgrade_macro(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)
+#define sv_utf8_upgrade_nomg(sv) sv_utf8_upgrade_flags(sv, 0)
+
+/* function style also available for bincompat */
+#define sv_setsv(dsv, ssv) sv_setsv_macro(dsv, ssv)
+#define sv_catsv(dsv, ssv) sv_catsv_macro(dsv, ssv)
+#define sv_catpvn(dsv, sstr, slen) sv_catpvn_macro(dsv, sstr, slen)
+#define sv_2pv(sv, lp) sv_2pv_macro(sv, lp)
+#define sv_pvn_force(sv, lp) sv_pvn_force_macro(sv, lp)
+#define sv_utf8_upgrade(sv) sv_utf8_upgrade_macro(sv)
+
 #undef SvPV
-#define SvPV(sv, lp) \
-    ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
-     ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv(sv, &lp))
+#define SvPV(sv, lp) SvPV_flags(sv, lp, SV_GMAGIC)
 
+#undef SvPV_nomg
+#define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
+
+#undef SvPV_flags
+#define SvPV_flags(sv, lp, flags) \
+    ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
+     ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_2pv_flags(sv, &lp, flags))
 
 #undef SvPV_force
-#define SvPV_force(sv, lp) \
+#define SvPV_force(sv, lp) SvPV_force_flags(sv, lp, SV_GMAGIC)
+#undef SvPV_force_nomg
+#define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
+
+#undef SvPV_force_flags
+#define SvPV_force_flags(sv, lp, flags) \
     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
-     ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force(sv, &lp))
+    ? ((lp = SvCUR(sv)), SvPVX(sv)) : sv_pvn_force_flags(sv, &lp, flags))
 
 #undef SvPV_nolen
 #define SvPV_nolen(sv) \
@@ -978,7 +1089,7 @@ more than once.
 Calls a non-destructive version of C<sv_setsv> if dsv is not the same as
 ssv. May evaluate arguments more than once.
 
-=for apidoc Am|void|SvGROW|SV* sv|STRLEN len
+=for apidoc Am|char *|SvGROW|SV* sv|STRLEN len
 Expands the character buffer in the SV so that it has room for the
 indicated number of bytes (remember to reserve space for an extra trailing
 NUL character).  Calls C<sv_grow> to perform the expansion if necessary. 
@@ -1032,3 +1143,7 @@ Returns a pointer to the character buffer.
 
 #define SvGROW(sv,len) (SvLEN(sv) < (len) ? sv_grow(sv,len) : SvPVX(sv))
 #define Sv_Grow sv_grow
+
+#define CLONEf_COPY_STACKS 1
+#define CLONEf_KEEP_PTR_TABLE 2
+