PVFMs don't need CvDEPTH, and PVCVs don't use SvIVX, so moving
[p5sagit/p5-mst-13.2.git] / sv.h
diff --git a/sv.h b/sv.h
index 6983f45..88d3cf8 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -1,7 +1,7 @@
 /*    sv.h
  *
  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
- *    2000, 2001, 2002, 2003, 2004, 2005 by Larry Wall and others
+ *    2000, 2001, 2002, 2003, 2004, 2005, 2006, 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.
@@ -59,22 +59,48 @@ typedef enum {
        SVt_PVHV,       /* 12 */
        SVt_PVCV,       /* 13 */
        SVt_PVFM,       /* 14 */
-       SVt_PVIO        /* 15 */
+       SVt_PVIO,       /* 15 */
+       SVt_LAST        /* keep last in enum. used to size arrays */
 } svtype;
 
+/* There is collusion here with sv_clear - sv_clear exits early for SVt_NULL
+   and SVt_IV, so never reaches the clause at the end that uses
+   sv_type_details->body_size to determine whether to call safefree(). Hence
+   body_size can be set no-zero to record the size of PTEs and HEs, without
+   fear of bogus frees.  */
+#ifdef PERL_IN_SV_C
+#define PTE_SVSLOT     SVt_IV
+#endif
+#if defined(PERL_IN_HV_C) || defined(PERL_IN_XS_APITEST)
+#define HE_SVSLOT      SVt_NULL
+#endif
+
+/* typedefs to eliminate some typing */
+typedef struct he HE;
+typedef struct hek HEK;
+
 /* Using C's structural equivalence to help emulate C++ inheritance here... */
 
+/* start with 2 sv-head building blocks */
+#define _SV_HEAD(ptrtype) \
+    ptrtype    sv_any;         /* pointer to body */   \
+    U32                sv_refcnt;      /* how many references to us */ \
+    U32                sv_flags        /* what we are */
+
+#define _SV_HEAD_UNION \
+    union {                            \
+       IV      svu_iv;                 \
+       UV      svu_uv;                 \
+       SV*     svu_rv;         /* pointer to another SV */             \
+       char*   svu_pv;         /* pointer to malloced string */        \
+       SV**    svu_array;              \
+       HE**    svu_hash;               \
+    }  sv_u
+
+
 struct STRUCT_SV {             /* struct sv { */
-    void*      sv_any;         /* pointer to something */
-    U32                sv_refcnt;      /* how many references to us */
-    U32                sv_flags;       /* what we are */
-    union {
-       IV      svu_iv;
-       UV      svu_uv;
-       SV*     svu_rv;         /* pointer to another SV */
-       char*   svu_pv;         /* pointer to malloced string */
-       SV**    svu_array;
-    }          sv_u;
+    _SV_HEAD(void*);
+    _SV_HEAD_UNION;
 #ifdef DEBUG_LEAKING_SCALARS
     unsigned   sv_debug_optype:9;      /* the type of OP that allocated us */
     unsigned   sv_debug_inpad:1;       /* was allocated in a pad for an OP */
@@ -85,70 +111,33 @@ struct STRUCT_SV {         /* struct sv { */
 };
 
 struct gv {
-    XPVGV*     sv_any;         /* pointer to something */
-    U32                sv_refcnt;      /* how many references to us */
-    U32                sv_flags;       /* what we are */
-    union {
-       IV      svu_iv;
-       UV      svu_uv;
-       SV*     svu_rv;
-       char*   svu_pv;
-       SV**    svu_array;
-    }          sv_u;
+    _SV_HEAD(XPVGV*);          /* pointer to xpvgv body */
+    _SV_HEAD_UNION;
 };
 
 struct cv {
-    XPVCV*     sv_any;         /* pointer to something */
-    U32                sv_refcnt;      /* how many references to us */
-    U32                sv_flags;       /* what we are */
-    union {
-       IV      svu_iv;
-       UV      svu_uv;
-       SV*     svu_rv;
-       char*   svu_pv;
-       SV**    svu_array;
-    }          sv_u;
+    _SV_HEAD(XPVCV*);          /* pointer to xpvcv body */
+    _SV_HEAD_UNION;
 };
 
 struct av {
-    XPVAV*     sv_any;         /* pointer to something */
-    U32                sv_refcnt;      /* how many references to us */
-    U32                sv_flags;       /* what we are */
-    union {
-       IV      svu_iv;
-       UV      svu_uv;
-       SV*     svu_rv;
-       char*   svu_pv;         /* pointer to first array element */
-       SV**    svu_array;
-    }          sv_u;
+    _SV_HEAD(XPVAV*);          /* pointer to xpvav body */
+    _SV_HEAD_UNION;
 };
 
 struct hv {
-    XPVHV*     sv_any;         /* pointer to something */
-    U32                sv_refcnt;      /* how many references to us */
-    U32                sv_flags;       /* what we are */
-    union {
-       IV      svu_iv;
-       UV      svu_uv;
-       SV*     svu_rv;
-       char*   svu_pv;
-       SV**    svu_array;
-    }          sv_u;
+    _SV_HEAD(XPVHV*);          /* pointer to xpvhv body */
+    _SV_HEAD_UNION;
 };
 
 struct io {
-    XPVIO*     sv_any;         /* pointer to something */
-    U32                sv_refcnt;      /* how many references to us */
-    U32                sv_flags;       /* what we are */
-    union {
-       IV      svu_iv;
-       UV      svu_uv;
-       SV*     svu_rv;
-       char*   svu_pv;
-       SV**    svu_array;
-    }          sv_u;
+    _SV_HEAD(XPVIO*);          /* pointer to xpvio body */
+    _SV_HEAD_UNION;
 };
 
+#undef _SV_HEAD
+#undef _SV_HEAD_UNION          /* ensure no pollution */
+
 /*
 =head1 SV Manipulation Functions
 
@@ -185,7 +174,7 @@ perform the upgrade if necessary.  See C<svtype>.
     })
 #else
 #  define SvREFCNT_inc(sv)     \
-       ((PL_Sv=(SV*)(sv)), (PL_Sv && ++(SvREFCNT(PL_Sv))), (SV*)PL_Sv)
+       ((PL_Sv=(SV*)(sv)) ? ((++(SvREFCNT(PL_Sv))),(PL_Sv)) : NULL)
 #endif
 
 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
@@ -208,6 +197,11 @@ perform the upgrade if necessary.  See C<svtype>.
 #define SVTYPEMASK     0xff
 #define SvTYPE(sv)     ((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
+   them all by using a consistent macro.  */
+#define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK)
+
 #define SvUPGRADE(sv, mt) (SvTYPE(sv) >= (mt) || (sv_upgrade(sv, mt), 1))
 
 #define SVs_PADSTALE   0x00000100      /* lexical has gone out of scope */
@@ -300,6 +294,7 @@ struct xpviv {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
 };
 
@@ -313,6 +308,7 @@ typedef struct {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
 } xpviv_allocated;
 #endif
@@ -340,6 +336,7 @@ struct xpvnv {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
 };
 
@@ -352,6 +349,7 @@ struct xpvmg {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
     MAGIC*     xmg_magic;      /* linked list of magicalness */
     HV*                xmg_stash;      /* class package */
@@ -365,6 +363,7 @@ struct xpvlv {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
     MAGIC*     xmg_magic;      /* linked list of magicalness */
     HV*                xmg_stash;      /* class package */
@@ -391,6 +390,7 @@ struct xpvgv {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
     MAGIC*     xmg_magic;      /* linked list of magicalness */
     HV*                xmg_stash;      /* class package */
@@ -410,6 +410,7 @@ struct xpvbm {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
     MAGIC*     xmg_magic;      /* linked list of magicalness */
     HV*                xmg_stash;      /* class package */
@@ -428,30 +429,66 @@ struct xpvfm {
     STRLEN     xpv_cur;        /* length of svu_pv as a C string */
     STRLEN     xpv_len;        /* allocated size */
     union {
-       IV      xivu_iv;        /* integer value or pv offset */
+       IV      xivu_iv;        /* PVFMs use the pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
     MAGIC*     xmg_magic;      /* linked list of magicalness */
     HV*                xmg_stash;      /* class package */
 
     HV *       xcv_stash;
-    OP *       xcv_start;
-    OP *       xcv_root;
-    void      (*xcv_xsub)(pTHX_ CV*);
-    ANY                xcv_xsubany;
+    union {
+       OP *    xcv_start;
+       ANY     xcv_xsubany;
+    }          xcv_start_u;
+    union {
+       OP *    xcv_root;
+       void    (*xcv_xsub) (pTHX_ CV*);
+    }          xcv_root_u;
     GV *       xcv_gv;
     char *     xcv_file;
-    long       xcv_depth;      /* >= 2 indicates recursive call */
     AV *       xcv_padlist;
     CV *       xcv_outside;
-    cv_flags_t xcv_flags;
     U32                xcv_outside_seq; /* the COP sequence (at the point of our
                                  * compilation) in the lexically enclosing
                                  * sub */
+    cv_flags_t xcv_flags;
     IV         xfm_lines;
 };
 
+typedef struct {
+    STRLEN     xpv_cur;        /* length of svu_pv as a C string */
+    STRLEN     xpv_len;        /* allocated size */
+    union {
+       IV      xivu_iv;        /* PVFMs use the pv offset */
+       UV      xivu_uv;
+       void *  xivu_p1;
+       I32     xivu_i32;
+    }          xiv_u;
+    MAGIC*     xmg_magic;      /* linked list of magicalness */
+    HV*                xmg_stash;      /* class package */
+
+    HV *       xcv_stash;
+    union {
+       OP *    xcv_start;
+       ANY     xcv_xsubany;
+    }          xcv_start_u;
+    union {
+       OP *    xcv_root;
+       void    (*xcv_xsub) (pTHX_ CV*);
+    }          xcv_root_u;
+    GV *       xcv_gv;
+    char *     xcv_file;
+    AV *       xcv_padlist;
+    CV *       xcv_outside;
+    U32                xcv_outside_seq; /* the COP sequence (at the point of our
+                                 * compilation) in the lexically enclosing
+                                 * sub */
+    cv_flags_t xcv_flags;
+    IV         xfm_lines;
+} xpvfm_allocated;
+
 struct xpvio {
     NV         xnv_nv;         /* numeric value, if any */
     STRLEN     xpv_cur;        /* length of svu_pv as a C string */
@@ -460,6 +497,7 @@ struct xpvio {
        IV      xivu_iv;        /* integer value or pv offset */
        UV      xivu_uv;
        void *  xivu_p1;
+       I32     xivu_i32;
     }          xiv_u;
     MAGIC*     xmg_magic;      /* linked list of magicalness */
     HV*                xmg_stash;      /* class package */
@@ -762,7 +800,8 @@ in gv.h: */
                                                  SVf_IVisUV),          \
                                    SvFLAGS(sv) |= (SVf_POK|SVp_POK))
 
-#define SvVOK(sv)              (SvMAGICAL(sv) && mg_find(sv,'V'))
+#define SvVOK(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)))
@@ -881,8 +920,6 @@ in gv.h: */
 #  else
 #  define SvPVX(sv) SvPVX_mutable(sv)
 #  endif
-#  define SvPVX_mutable(sv)    (0 + (sv)->sv_u.svu_pv)
-#  define SvPVX_const(sv)      ((const char*)(0 + (sv)->sv_u.svu_pv))
 #  define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
 #  define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
@@ -904,25 +941,68 @@ in gv.h: */
 #  define SvSTASH(sv)     (0 + ((XPVMG*)  SvANY(sv))->xmg_stash)
 #  endif
 #else
-#  define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
-#  define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
-#  define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_nv
 #  define SvPVX(sv) ((sv)->sv_u.svu_pv)
-#  define SvPVX_mutable(sv)    SvPVX(sv)
-#  define SvPVX_const(sv)      ((const char*)SvPVX(sv))
 #  define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
 #  define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
 #  define SvEND(sv) ((sv)->sv_u.svu_pv + ((XPV*)SvANY(sv))->xpv_cur)
 
-#  ifdef DEBUGGING
-#    define SvMAGIC(sv)        (*(assert(SvTYPE(sv) >= SVt_PVMG), &((XPVMG*)  SvANY(sv))->xmg_magic))
-#    define SvSTASH(sv)        (*(assert(SvTYPE(sv) >= SVt_PVMG), &((XPVMG*)  SvANY(sv))->xmg_stash))
+#  if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
+/* These get expanded inside other macros that already use a variable _sv  */
+#    define SvIVX(sv)                                                  \
+       (*({ SV *const _svi = (SV *) sv;                                \
+           assert(SvTYPE(_svi) == SVt_IV || SvTYPE(_svi) >= SVt_PVIV); \
+           assert(SvTYPE(_svi) != SVt_PVAV);                           \
+           assert(SvTYPE(_svi) != SVt_PVHV);                           \
+           assert(SvTYPE(_svi) != SVt_PVCV);                           \
+           &(((XPVIV*) SvANY(_svi))->xiv_iv);                          \
+        }))
+#    define SvUVX(sv)                                                  \
+       (*({ SV *const _svi = (SV *) sv;                                \
+           assert(SvTYPE(_svi) == SVt_IV || SvTYPE(_svi) >= SVt_PVIV); \
+           assert(SvTYPE(_svi) != SVt_PVAV);                           \
+           assert(SvTYPE(_svi) != SVt_PVHV);                           \
+           assert(SvTYPE(_svi) != SVt_PVCV);                           \
+           &(((XPVUV*) SvANY(_svi))->xuv_uv);                          \
+        }))
+#    define SvNVX(sv)                                                  \
+       (*({ SV *const _svi = (SV *) sv;                                \
+           assert(SvTYPE(_svi) == SVt_NV || SvTYPE(_svi) >= SVt_PVNV); \
+           assert(SvTYPE(_svi) != SVt_PVAV);                           \
+           assert(SvTYPE(_svi) != SVt_PVHV);                           \
+           assert(SvTYPE(_svi) != SVt_PVFM);                           \
+          &(((XPVNV*) SvANY(_svi))->xnv_nv);                           \
+        }))
+#    define SvMAGIC(sv)                                                        \
+       (*({ SV *const _svi = (SV *) sv;                                \
+           assert(SvTYPE(_svi) >= SVt_PVMG);                           \
+           &(((XPVMG*) SvANY(_svi))->xmg_magic);                       \
+         }))
+#    define SvSTASH(sv)                                                        \
+       (*({ SV *const _svi = (SV *) sv;                                \
+           assert(SvTYPE(_svi) >= SVt_PVMG);                           \
+           &(((XPVMG*) SvANY(_svi))->xmg_stash);                       \
+         }))
 #  else
+#    define SvIVX(sv) ((XPVIV*) SvANY(sv))->xiv_iv
+#    define SvUVX(sv) ((XPVUV*) SvANY(sv))->xuv_uv
+#    define SvNVX(sv) ((XPVNV*) SvANY(sv))->xnv_nv
 #    define SvMAGIC(sv)        ((XPVMG*)  SvANY(sv))->xmg_magic
 #    define SvSTASH(sv)        ((XPVMG*)  SvANY(sv))->xmg_stash
 #  endif
 #endif
 
+#ifndef PERL_POISON
+/* Given that these two are new, there can't be any existing code using them
+ *  as LVALUEs  */
+#  define SvPVX_mutable(sv)    (0 + (sv)->sv_u.svu_pv)
+#  define SvPVX_const(sv)      ((const char*)(0 + (sv)->sv_u.svu_pv))
+#else
+/* Except for the poison code, which uses & to scribble over the pointer after
+   free() is called.  */
+#  define SvPVX_mutable(sv)    ((sv)->sv_u.svu_pv)
+#  define SvPVX_const(sv)      ((const char*)((sv)->sv_u.svu_pv))
+#endif
+
 #define SvIVXx(sv) SvIVX(sv)
 #define SvUVXx(sv) SvUVX(sv)
 #define SvNVXx(sv) SvNVX(sv)
@@ -1056,6 +1136,8 @@ Taints an SV if tainting is enabled.
 =cut
 */
 
+#define sv_taint(sv)     sv_magic((sv), NULL, PERL_MAGIC_taint, NULL, 0)
+
 #define SvTAINTED(sv)    (SvMAGICAL(sv) && sv_tainted(sv))
 #define SvTAINTED_on(sv)  STMT_START{ if(PL_tainting){sv_taint(sv);}   }STMT_END
 #define SvTAINTED_off(sv) STMT_START{ if(PL_tainting){sv_untaint(sv);} }STMT_END
@@ -1210,6 +1292,10 @@ Like C<sv_catsv> but doesn't process magic.
     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
      ? ((lp = SvCUR(sv)), SvPVX_const(sv)) : \
      (const char*) sv_2pv_flags(sv, &lp, flags|SV_CONST_RETURN))
+#define SvPV_flags_const_nolen(sv, flags) \
+    ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
+     ? SvPVX_const(sv) : \
+     (const char*) sv_2pv_flags(sv, 0, flags|SV_CONST_RETURN))
 #define SvPV_flags_mutable(sv, lp, flags) \
     ((SvFLAGS(sv) & (SVf_POK)) == SVf_POK \
      ? ((lp = SvCUR(sv)), SvPVX_mutable(sv)) : \
@@ -1220,6 +1306,7 @@ Like C<sv_catsv> but doesn't process magic.
 #define SvPV_force_mutable(sv, lp) SvPV_force_flags_mutable(sv, lp, SV_GMAGIC)
 
 #define SvPV_force_nomg(sv, lp) SvPV_force_flags(sv, lp, 0)
+#define SvPV_force_nomg_nolen(sv) SvPV_force_flags_nolen(sv, 0)
 
 #define SvPV_force_flags(sv, lp, flags) \
     ((SvFLAGS(sv) & (SVf_POK|SVf_THINKFIRST)) == SVf_POK \
@@ -1242,6 +1329,7 @@ Like C<sv_catsv> but doesn't process magic.
 
 #define SvPV_nomg(sv, lp) SvPV_flags(sv, lp, 0)
 #define SvPV_nomg_const(sv, lp) SvPV_flags_const(sv, lp, 0)
+#define SvPV_nomg_const_nolen(sv) SvPV_flags_const_nolen(sv, 0)
 
 /* ----*/
 
@@ -1362,6 +1450,10 @@ Like C<sv_catsv> but doesn't process magic.
 #define SV_NOSTEAL             16
 #define SV_CONST_RETURN                32
 #define SV_MUTABLE_RETURN      64
+#define SV_SMAGIC              128
+
+#define sv_unref(sv)           sv_unref_flags(sv, 0)
+#define sv_force_normal(sv)    sv_force_normal_flags(sv, 0)
 
 /* 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
@@ -1402,8 +1494,14 @@ Like C<sv_catsv> but doesn't process magic.
 #define sv_setsv_nomg(dsv, ssv) sv_setsv_flags(dsv, ssv, 0)
 #define sv_catsv(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC)
 #define sv_catsv_nomg(dsv, ssv) sv_catsv_flags(dsv, ssv, 0)
+#define sv_catsv_mg(dsv, ssv) sv_catsv_flags(dsv, ssv, SV_GMAGIC|SV_SMAGIC)
 #define sv_catpvn(dsv, sstr, slen) sv_catpvn_flags(dsv, sstr, slen, SV_GMAGIC)
+#define sv_catpvn_mg(sv, sstr, slen) \
+       sv_catpvn_flags(sv, sstr, slen, SV_GMAGIC|SV_SMAGIC);
 #define sv_2pv(sv, lp) sv_2pv_flags(sv, lp, SV_GMAGIC)
+#define sv_2pv_nolen(sv) sv_2pv(sv, 0)
+#define sv_2pvbyte_nolen(sv) sv_2pvbyte(sv, 0)
+#define sv_2pvutf8_nolen(sv) sv_2pvutf8(sv, 0)
 #define sv_2pv_nomg(sv, lp) sv_2pv_flags(sv, lp, 0)
 #define sv_pvn_force(sv, lp) sv_pvn_force_flags(sv, lp, SV_GMAGIC)
 #define sv_utf8_upgrade(sv) sv_utf8_upgrade_flags(sv, SV_GMAGIC)