This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Un-TODO some B tests
[perl5.git] / sv.h
diff --git a/sv.h b/sv.h
index 7445c76..ab09bba 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -150,6 +150,44 @@ Returns the value of the object's reference count.
 =for apidoc Am|SV*|SvREFCNT_inc|SV* sv
 Increments the reference count of the given SV.
 
+All of the following SvREFCNT_inc* macros are optimized versions of
+SvREFCNT_inc, and can be replaced with SvREFCNT_inc.
+
+=for apidoc Am|SV*|SvREFCNT_inc_NN|SV* sv
+Same as SvREFCNT_inc, but can only be used if you know I<sv>
+is not NULL.  Since we don't have to check the NULLness, it's faster
+and smaller.
+
+=for apidoc Am|void|SvREFCNT_inc_void|SV* sv
+Same as SvREFCNT_inc, but can only be used if you don't need the
+return value.  The macro doesn't need to return a meaningful value.
+
+=for apidoc Am|void|SvREFCNT_inc_void_NN|SV* sv
+Same as SvREFCNT_inc, but can only be used if you don't need the return
+value, and you know that I<sv> is not NULL.  The macro doesn't need
+to return a meaningful value, or check for NULLness, so it's smaller
+and faster.
+
+=for apidoc Am|SV*|SvREFCNT_inc_simple|SV* sv
+Same as SvREFCNT_inc, but can only be used with simple variables, not
+expressions or pointer dereferences.  Since we don't have to store a
+temporary value, it's faster.
+
+=for apidoc Am|SV*|SvREFCNT_inc_simple_NN|SV* sv
+Same as SvREFCNT_inc_simple, but can only be used if you know I<sv>
+is not NULL.  Since we don't have to check the NULLness, it's faster
+and smaller.
+
+=for apidoc Am|void|SvREFCNT_inc_simple_void|SV* sv
+Same as SvREFCNT_inc_simple, but can only be used if you don't need the
+return value.  The macro doesn't need to return a meaningful value.
+
+=for apidoc Am|void|SvREFCNT_inc_simple_void_NN|SV* sv
+Same as SvREFCNT_inc, but can only be used if you don't need the return
+value, and you know that I<sv> is not NULL.  The macro doesn't need
+to return a meaningful value, or check for NULLness, so it's smaller
+and faster.
+
 =for apidoc Am|void|SvREFCNT_dec|SV* sv
 Decrements the reference count of the given SV.
 
@@ -175,11 +213,41 @@ perform the upgrade if necessary.  See C<svtype>.
             (SvREFCNT(_sv))++;         \
        _sv;                            \
     })
+#  define SvREFCNT_inc_simple(sv)      \
+    ({                                 \
+       if (sv)                         \
+            (SvREFCNT(sv))++;          \
+       (SV *)(sv);                             \
+    })
+#  define SvREFCNT_inc_NN(sv)          \
+    ({                                 \
+       SV * const _sv = (SV*)(sv);     \
+       SvREFCNT(_sv)++;                \
+       _sv;                            \
+    })
+#  define SvREFCNT_inc_void(sv)                \
+    ({                                 \
+       SV * const _sv = (SV*)(sv);     \
+       if (_sv)                        \
+           (void)(SvREFCNT(_sv)++);    \
+    })
 #else
 #  define SvREFCNT_inc(sv)     \
-       ((PL_Sv=(SV*)(sv)) ? ((++(SvREFCNT(PL_Sv))),(PL_Sv)) : NULL)
+       ((PL_Sv=(SV*)(sv)) ? (++(SvREFCNT(PL_Sv)),PL_Sv) : NULL)
+#  define SvREFCNT_inc_simple(sv) \
+       ((sv) ? (SvREFCNT(sv)++,(SV*)(sv)) : NULL)
+#  define SvREFCNT_inc_NN(sv) \
+       (PL_Sv=(SV*)(sv),++(SvREFCNT(PL_Sv)),PL_Sv)
+#  define SvREFCNT_inc_void(sv) \
+       (void)((PL_Sv=(SV*)(sv)) ? ++(SvREFCNT(PL_Sv)) : 0)
 #endif
 
+/* These guys don't need the curly blocks */
+#define SvREFCNT_inc_simple_void(sv)   STMT_START { if (sv) SvREFCNT(sv)++; } STMT_END
+#define SvREFCNT_inc_simple_NN(sv)     (++(SvREFCNT(sv)),(SV*)(sv))
+#define SvREFCNT_inc_void_NN(sv)       (void)(++SvREFCNT((SV*)(sv)))
+#define SvREFCNT_inc_simple_void_NN(sv)        (void)(++SvREFCNT((SV*)(sv)))
+
 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
 #  define SvREFCNT_dec(sv)             \
     ({                                 \
@@ -198,7 +266,7 @@ perform the upgrade if necessary.  See C<svtype>.
 #endif
 
 #define SVTYPEMASK     0xff
-#define SvTYPE(sv)     ((sv)->sv_flags & SVTYPEMASK)
+#define SvTYPE(sv)     (svtype)((sv)->sv_flags & SVTYPEMASK)
 
 /* Sadly there are some parts of the core that have pointers to already-freed
    SV heads, and rely on being able to tell that they are now free. So mark
@@ -219,6 +287,7 @@ perform the upgrade if necessary.  See C<svtype>.
 #define SVphv_CLONEABLE        0x00008000      /* PVHV (stashes) clone its objects */
 
 #define SVs_PADSTALE   0x00010000      /* lexical has gone out of scope */
+#define SVpad_STATE    0x00010000      /* pad name is a "state" var */
 #define SVs_PADTMP     0x00020000      /* in use as tmp */
 #define SVpad_TYPED    0x00020000      /* pad name is a Typed Lexical */
 #define SVs_PADMY      0x00040000      /* in use a "my" variable */
@@ -242,8 +311,9 @@ perform the upgrade if necessary.  See C<svtype>.
                                              fact an offset [SvREPADTMP(sv)]
                                           5: On a pad name SV, that slot in the
                                              frame AV is a REFCNT'ed reference
-                                             to a lexical from "outside".
-                                       */
+                                             to a lexical from "outside". */
+#define SVphv_REHASH   SVf_FAKE        /* 6: On a PVHV, hash values are being
+                                             recalculated */
 #define SVf_OOK                0x02000000      /* has valid offset value
                                           For a PVHV this means that a
                                           hv_aux struct is present after the
@@ -269,13 +339,12 @@ perform the upgrade if necessary.  See C<svtype>.
 /* Some private flags. */
 
 /* PVHV */
-#define SVphv_REHASH   0x10000000      /* PVHV is recalculating hash values */
-/* PVHV */
 #define SVphv_SHAREKEYS 0x20000000     /* PVHV
                                           keys live on shared string table */
 /* PVNV, PVMG, PVGV, presumably only inside pads */
 #define SVpad_NAME     0x40000000      /* This SV is a name in the PAD, so
-                                          SVpad_TYPED and SVpad_OUR apply */
+                                          SVpad_TYPED, SVpad_OUR and
+                                          SVpad_STATE apply */
 /* PVAV */
 #define SVpav_REAL     0x40000000      /* free old entries */
 /* PVHV */
@@ -330,6 +399,7 @@ struct xpviv {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
 };
 
@@ -344,6 +414,7 @@ typedef struct {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
 } xpviv_allocated;
 #endif
@@ -361,6 +432,7 @@ struct xpvuv {
        IV      xuvu_iv;
        UV      xuvu_uv;        /* unsigned value or pv offset */
        void *  xuvu_p1;
+       HEK *   xivu_namehek;
     }          xuv_u;
 };
 
@@ -378,6 +450,7 @@ struct xpvnv {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
 };
 
@@ -394,6 +467,7 @@ struct xpvmg {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -414,6 +488,7 @@ struct xpvlv {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;   /* GvNAME */
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -421,11 +496,6 @@ struct xpvlv {
     } xmg_u;
     HV*                xmg_stash;      /* class package */
 
-    /* a full glob fits into this */
-    char*      xgv_name;
-    STRLEN     xgv_namelen;
-    U8         xgv_flags;
-
     STRLEN     xlv_targoff;
     STRLEN     xlv_targlen;
     SV*                xlv_targ;
@@ -438,13 +508,14 @@ struct xpvgv {
        NV      xnv_nv;
        HV *    xgv_stash;      /* The stash of this GV */
     }          xnv_u;
-    STRLEN     xpv_cur;        /* length of svu_pv as a C string */
-    STRLEN     xpv_len;        /* allocated size */
+    STRLEN     xpv_cur;        /* xgv_flags */
+    STRLEN     xpv_len;        /* 0 */
     union {
-       IV      xivu_iv;        /* integer value or pv offset */
+       IV      xivu_iv;
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;   /* GvNAME */
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -452,9 +523,6 @@ struct xpvgv {
     } xmg_u;
     HV*                xmg_stash;      /* class package */
 
-    char*      xgv_name;
-    STRLEN     xgv_namelen;
-    U8         xgv_flags;
 };
 
 struct xpvbm {
@@ -469,6 +537,7 @@ struct xpvbm {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -497,6 +566,7 @@ struct xpvfm {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -532,6 +602,7 @@ typedef struct {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -571,6 +642,7 @@ struct xpvio {
        UV      xivu_uv;
        void *  xivu_p1;
        I32     xivu_i32;
+       HEK *   xivu_namehek;
     }          xiv_u;
     union {
        MAGIC*  xmg_magic;      /* linked list of magicalness */
@@ -770,7 +842,7 @@ Set the value of the RV pointer in sv to val.  See C<SvIV_set>.
 =for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
 Set the value of the MAGIC pointer in sv to val.  See C<SvIV_set>.
 
-=for apidoc Am|void|SvSTASH_set|SV* sv|STASH* val
+=for apidoc Am|void|SvSTASH_set|SV* sv|HV* val
 Set the value of the STASH pointer in sv to val.  See C<SvIV_set>.
 
 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
@@ -798,11 +870,11 @@ Set the actual length of the string which is in the SV.  See C<SvIV_set>.
 
 #define SvOK(sv)               (SvFLAGS(sv) & SVf_OK)
 #define SvOK_off(sv)           (assert_not_ROK(sv) assert_not_glob(sv) \
-                                SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
+                                SvFLAGS(sv) &= ~(SVf_OK|               \
                                                  SVf_IVisUV|SVf_UTF8), \
                                                        SvOOK_off(sv))
 #define SvOK_off_exc_UV(sv)    (assert_not_ROK(sv)                     \
-                                SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
+                                SvFLAGS(sv) &= ~(SVf_OK|               \
                                                  SVf_UTF8),            \
                                                        SvOOK_off(sv))
 
@@ -871,16 +943,20 @@ in gv.h: */
                                 SvFLAGS(sv) |= (SVf_POK|SVp_POK))
 #define SvPOK_off(sv)          (SvFLAGS(sv) &= ~(SVf_POK|SVp_POK))
 #define SvPOK_only(sv)         (assert_not_ROK(sv) assert_not_glob(sv) \
-                                SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
+                                SvFLAGS(sv) &= ~(SVf_OK|               \
                                                  SVf_IVisUV|SVf_UTF8), \
                                    SvFLAGS(sv) |= (SVf_POK|SVp_POK))
 #define SvPOK_only_UTF8(sv)    (assert_not_ROK(sv) assert_not_glob(sv) \
-                                SvFLAGS(sv) &= ~(SVf_OK|SVf_AMAGIC|    \
+                                SvFLAGS(sv) &= ~(SVf_OK|               \
                                                  SVf_IVisUV),          \
                                    SvFLAGS(sv) |= (SVf_POK|SVp_POK))
 
 #define SvVOK(sv)              (SvMAGICAL(sv)                          \
+                                && mg_find(sv,PERL_MAGIC_vstring))
+/* returns the vstring magic, if any */
+#define SvVSTRING_mg(sv)       (SvMAGICAL(sv) \
                                 ? mg_find(sv,PERL_MAGIC_vstring) : NULL)
+
 #define SvOOK(sv)              (SvFLAGS(sv) & SVf_OOK)
 #define SvOOK_on(sv)           ((void)SvIOK_off(sv), SvFLAGS(sv) |= SVf_OOK)
 #define SvOOK_off(sv)          ((void)(SvOOK(sv) && sv_backoff(sv)))
@@ -891,7 +967,7 @@ in gv.h: */
 
 #define SvROK(sv)              (SvFLAGS(sv) & SVf_ROK)
 #define SvROK_on(sv)           (SvFLAGS(sv) |= SVf_ROK)
-#define SvROK_off(sv)          (SvFLAGS(sv) &= ~(SVf_ROK|SVf_AMAGIC))
+#define SvROK_off(sv)          (SvFLAGS(sv) &= ~(SVf_ROK))
 
 #define SvMAGICAL(sv)          (SvFLAGS(sv) & (SVs_GMG|SVs_SMG|SVs_RMG))
 #define SvMAGICAL_on(sv)       (SvFLAGS(sv) |= (SVs_GMG|SVs_SMG|SVs_RMG))
@@ -909,17 +985,36 @@ in gv.h: */
 #define SvRMAGICAL_on(sv)      (SvFLAGS(sv) |= SVs_RMG)
 #define SvRMAGICAL_off(sv)     (SvFLAGS(sv) &= ~SVs_RMG)
 
-#define SvAMAGIC(sv)           (SvFLAGS(sv) & SVf_AMAGIC)
-#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 SvAMAGIC(sv)           (SvROK(sv) && (SvFLAGS(SvRV(sv)) & SVf_AMAGIC))
+#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
+#  define SvAMAGIC_on(sv)      ({ SV * const kloink = sv;              \
+                                  assert(SvROK(kloink));               \
+                                  SvFLAGS(SvRV(kloink)) |= SVf_AMAGIC; \
+                               })
+#  define SvAMAGIC_off(sv)     ({ SV * const kloink = sv;              \
+                                  if(SvROK(kloink))                    \
+                                       SvFLAGS(SvRV(kloink)) &= ~SVf_AMAGIC;\
+                               })
+#else
+#  define SvAMAGIC_on(sv)      (SvFLAGS(SvRV(sv)) |= SVf_AMAGIC)
+#  define SvAMAGIC_off(sv) \
+       (SvROK(sv) && (SvFLAGS(SvRV(sv)) &= ~SVf_AMAGIC))
+#endif
 
 /*
-#define Gv_AMG(stash) \
-        (HV_AMAGICmb(stash) && \
-         ((!HV_AMAGICbad(stash) && HV_AMAGIC(stash)) || Gv_AMupdate(stash)))
+=for apidoc Am|char*|SvGAMAGIC|SV* sv
+
+Returns true if the SV has get magic or overloading. If either is true then
+the scalar is active data, and has the potential to return a new value every
+time it is accessed. Hence you must be careful to only read it once per user
+logical operation and work with that returned value. If neither is true then
+the scalar's value cannot change unless written to.
+
+=cut
 */
+
+#define SvGAMAGIC(sv)           (SvGMAGICAL(sv) || SvAMAGIC(sv))
+
 #define Gv_AMG(stash)           (PL_amagic_generation && Gv_AMupdate(stash))
 
 #define SvWEAKREF(sv)          ((SvFLAGS(sv) & (SVf_ROK|SVprv_WEAKREF)) \
@@ -988,6 +1083,10 @@ in gv.h: */
        ((SvFLAGS(sv) & (SVpad_NAME|SVpad_OUR)) == (SVpad_NAME|SVpad_OUR))
 #define SvPAD_OUR_on(sv)       (SvFLAGS(sv) |= SVpad_NAME|SVpad_OUR)
 
+#define SvPAD_STATE(sv)        \
+       ((SvFLAGS(sv) & (SVpad_NAME|SVpad_STATE)) == (SVpad_NAME|SVpad_STATE))
+#define SvPAD_STATE_on(sv)     (SvFLAGS(sv) |= SVpad_NAME|SVpad_STATE)
+
 #define OURSTASH(sv)   \
        (SvPAD_OUR(sv) ? ((XPVMG*) SvANY(sv))->xmg_u.xmg_ourstash : NULL)
 #define OURSTASH_set(sv, st)                                   \
@@ -1078,6 +1177,8 @@ in gv.h: */
 #    define SvMAGIC(sv)                                                        \
        (*({ SV *const _svi = (SV *) sv;                                \
            assert(SvTYPE(_svi) >= SVt_PVMG);                           \
+           if(SvTYPE(_svi) == SVt_PVMG)                                \
+               assert(!SvPAD_OUR(_svi));                               \
            &(((XPVMG*) SvANY(_svi))->xmg_u.xmg_magic);                 \
          }))
 #    define SvSTASH(sv)                                                        \
@@ -1563,9 +1664,12 @@ Like C<sv_catsv> but doesn't process magic.
 #define SV_CONST_RETURN                32
 #define SV_MUTABLE_RETURN      64
 #define SV_SMAGIC              128
+#define SV_HAS_TRAILING_NUL    256
 
 #define sv_unref(sv)           sv_unref_flags(sv, 0)
 #define sv_force_normal(sv)    sv_force_normal_flags(sv, 0)
+#define sv_usepvn(sv, p, l)    sv_usepvn_flags(sv, p, l, 0)
+#define sv_usepvn_mg(sv, p, l) sv_usepvn_flags(sv, p, l, SV_SMAGIC)
 
 /* We are about to replace the SV's current value. So if it's copy on write
    we need to normalise it. Use the SV_COW_DROP_PV flag hint to say that
@@ -1586,7 +1690,7 @@ Like C<sv_catsv> but doesn't process magic.
 
 #define CAN_COW_MASK   (SVs_OBJECT|SVs_GMG|SVs_SMG|SVs_RMG|SVf_IOK|SVf_NOK| \
                         SVf_POK|SVf_ROK|SVp_IOK|SVp_NOK|SVp_POK|SVf_FAKE| \
-                        SVf_OOK|SVf_BREAK|SVf_READONLY|SVf_AMAGIC)
+                        SVf_OOK|SVf_BREAK|SVf_READONLY)
 #define CAN_COW_FLAGS  (SVp_POK|SVf_POK)
 
 #define SV_CHECK_THINKFIRST(sv) if (SvTHINKFIRST(sv)) \