Default to allocating the correct size for the array in the HV.
[p5sagit/p5-mst-13.2.git] / sv.h
diff --git a/sv.h b/sv.h
index 511b547..9416d53 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -136,7 +136,7 @@ perform the upgrade if necessary.  See C<svtype>.
 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
 #  define SvREFCNT_inc(sv)             \
     ({                                 \
-       SV *_sv = (SV*)(sv);            \
+       SV * const _sv = (SV*)(sv);     \
        if (_sv)                        \
             (SvREFCNT(_sv))++;         \
        _sv;                            \
@@ -149,7 +149,7 @@ perform the upgrade if necessary.  See C<svtype>.
 #if defined(__GNUC__) && !defined(__STRICT_ANSI__) && !defined(PERL_GCC_PEDANTIC)
 #  define SvREFCNT_dec(sv)             \
     ({                                 \
-       SV *_sv = (SV*)(sv);            \
+       SV * const _sv = (SV*)(sv);     \
        if (_sv) {                      \
            if (SvREFCNT(_sv)) {        \
                if (--(SvREFCNT(_sv)) == 0) \
@@ -221,6 +221,7 @@ perform the upgrade if necessary.  See C<svtype>.
 
 #define SVrepl_EVAL    0x40000000      /* Replacement part of s///e */
 
+#define SVphv_CLONEABLE        0x08000000      /* for stashes: clone its objects */
 #define SVphv_REHASH   0x10000000      /* HV is recalculating hash values */
 #define SVphv_SHAREKEYS 0x20000000     /* keys live on shared string table */
 #define SVphv_LAZYDEL  0x40000000      /* entry in xhv_eiter must be deleted */
@@ -534,8 +535,32 @@ See C<SvCUR>.  Access the character as *(SvEND(sv)).
 =for apidoc Am|HV*|SvSTASH|SV* sv
 Returns the stash of the SV.
 
+=for apidoc Am|void|SvIV_set|SV* sv|IV val
+Set the value of the IV pointer in sv to val.
+
+=for apidoc Am|void|SvNV_set|SV* sv|NV val
+Set the value of the IV pointer in sv to val.
+
+=for apidoc Am|void|SvPV_set|SV* sv|char* val
+Set the value of the PV pointer in sv to val.
+
+=for apidoc Am|void|SvUV_set|SV* sv|UV val
+Set the value of the PV pointer in sv to val.
+
+=for apidoc Am|void|SvRV_set|SV* sv|SV* val
+Set the value of the RV pointer in sv to val.
+
+=for apidoc Am|void|SvMAGIC_set|SV* sv|MAGIC* val
+Set the value of the MAGIC pointer in sv to val.
+
+=for apidoc Am|void|SvSTASH_set|SV* sv|STASH* val
+Set the value of the STASH pointer in sv to val.
+
 =for apidoc Am|void|SvCUR_set|SV* sv|STRLEN len
-Set the length of the string which is in the SV.  See C<SvCUR>.
+Set the current length of the string which is in the SV.  See C<SvCUR>.
+
+=for apidoc Am|void|SvLEN_set|SV* sv|STRLEN len
+Set the actual length of the string which is in the SV.
 
 =cut
 */
@@ -733,29 +758,46 @@ in gv.h: */
 #define SvREPADTMP_off(sv)     (SvFLAGS(sv) &= ~SVf_FAKE)
 #endif
 
+#ifdef PERL_DEBUG_COW
+#define SvRV(sv) (0 + ((XRV*)  SvANY(sv))->xrv_rv)
+#else
 #define SvRV(sv) ((XRV*)  SvANY(sv))->xrv_rv
+#endif
 #define SvRVx(sv) SvRV(sv)
 
 #ifdef PERL_DEBUG_COW
-#define SvIVX(sv) (0 + ((XPVIV*)  SvANY(sv))->xiv_iv)
-#define SvUVX(sv) ((XPVUV*)  SvANY(sv))->xuv_uv
-#define SvNVX(sv) (0.0 + ((XPVNV*)SvANY(sv))->xnv_nv)
+#define SvIVX(sv) (0 + ((XPVIV*) SvANY(sv))->xiv_iv)
+#define SvUVX(sv) (0 + ((XPVUV*) SvANY(sv))->xuv_uv)
+#define SvNVX(sv) (0 + ((XPVNV*) SvANY(sv))->xnv_nv)
+#define SvPVX(sv) (0 + ((XPV*) SvANY(sv))->xpv_pv)
+#define SvCUR(sv) (0 + ((XPV*) SvANY(sv))->xpv_cur)
+#define SvLEN(sv) (0 + ((XPV*) SvANY(sv))->xpv_len)
+#define SvEND(sv) (((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
+
+#ifdef DEBUGGING
+#  ifdef PERL_IN_SV_C
+/* Can't make this RVALUE because of Perl_sv_unmagic.  */
+#    define SvMAGIC(sv)        (*(assert(SvTYPE(sv) >= SVt_PVMG), &((XPVMG*)  SvANY(sv))->xmg_magic))
+#  else
+#    define SvMAGIC(sv)        (0 + *(assert(SvTYPE(sv) >= SVt_PVMG), &((XPVMG*)  SvANY(sv))->xmg_magic))
+#  endif
+#define SvSTASH(sv)    (0 + *(assert(SvTYPE(sv) >= SVt_PVMG), &((XPVMG*)  SvANY(sv))->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
+#  ifdef PERL_IN_SV_C
+#    define SvMAGIC(sv) ((XPVMG*)  SvANY(sv))->xmg_magic
+#  else
+#    define SvMAGIC(sv) (0 + ((XPVMG*)  SvANY(sv))->xmg_magic)
+#  endif
+#define SvSTASH(sv)     (0 + ((XPVMG*)  SvANY(sv))->xmg_stash)
 #endif
-#define SvPVX(sv)  ((XPV*)  SvANY(sv))->xpv_pv
-#define SvCUR(sv) ((XPV*)  SvANY(sv))->xpv_cur
-#define SvLEN(sv) ((XPV*)  SvANY(sv))->xpv_len
-#define SvEND(sv)(((XPV*)  SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
-
-#define SvIVXx(sv) SvIVX(sv)
-#define SvUVXx(sv) SvUVX(sv)
-#define SvNVXx(sv) SvNVX(sv)
-#define SvPVXx(sv) SvPVX(sv)
-#define SvLENx(sv) SvLEN(sv)
-#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
+#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) ((XPV*) SvANY(sv))->xpv_pv
+#define SvCUR(sv) ((XPV*) SvANY(sv))->xpv_cur
+#define SvLEN(sv) ((XPV*) SvANY(sv))->xpv_len
+#define SvEND(sv) (((XPV*) SvANY(sv))->xpv_pv + ((XPV*)SvANY(sv))->xpv_cur)
 
 #ifdef DEBUGGING
 #define SvMAGIC(sv)    (*(assert(SvTYPE(sv) >= SVt_PVMG), &((XPVMG*)  SvANY(sv))->xmg_magic))
@@ -765,6 +807,16 @@ in gv.h: */
 #define SvSTASH(sv)    ((XPVMG*)  SvANY(sv))->xmg_stash
 #endif
 
+#endif
+
+#define SvIVXx(sv) SvIVX(sv)
+#define SvUVXx(sv) SvUVX(sv)
+#define SvNVXx(sv) SvNVX(sv)
+#define SvPVXx(sv) SvPVX(sv)
+#define SvLENx(sv) SvLEN(sv)
+#define SvENDx(sv) ((PL_Sv = (sv)), SvEND(PL_Sv))
+
+
 /* 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 */
@@ -779,23 +831,54 @@ in gv.h: */
                (((XPVNV*)SvANY(sv))->xnv_nv = (val)); } STMT_END
 #define SvPV_set(sv, val) \
        STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
-               (SvPVX(sv) = (val)); } STMT_END
+               (((XPV*)  SvANY(sv))->xpv_pv = (val)); } STMT_END
 #define SvUV_set(sv, val) \
        STMT_START { assert(SvTYPE(sv) == SVt_IV || SvTYPE(sv) >= SVt_PVIV); \
                (((XPVUV*)SvANY(sv))->xuv_uv = (val)); } STMT_END
+#define SvRV_set(sv, val) \
+        STMT_START { assert(SvTYPE(sv) >=  SVt_RV); \
+                (((XRV*)SvANY(sv))->xrv_rv = (val)); } STMT_END
+#define SvMAGIC_set(sv, val) \
+        STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
+                (((XPVMG*)SvANY(sv))->xmg_magic = (val)); } STMT_END
+#define SvSTASH_set(sv, val) \
+        STMT_START { assert(SvTYPE(sv) >= SVt_PVMG); \
+                (((XPVMG*)  SvANY(sv))->xmg_stash = (val)); } STMT_END
 #define SvCUR_set(sv, val) \
        STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
-               (SvCUR(sv) = (val)); } STMT_END
+               (((XPV*)  SvANY(sv))->xpv_cur = (val)); } STMT_END
 #define SvLEN_set(sv, val) \
        STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
-               (SvLEN(sv) = (val)); } STMT_END
+               (((XPV*)  SvANY(sv))->xpv_len = (val)); } STMT_END
 #define SvEND_set(sv, val) \
        STMT_START { assert(SvTYPE(sv) >= SVt_PV); \
                (SvCUR(sv) = (val) - SvPVX(sv)); } STMT_END
 
 #define SvPV_renew(sv,n) \
-  (SvPV_set((sv), (MEM_WRAP_CHECK_(n,char) \
-                  (char*)saferealloc((Malloc_t)SvPVX(sv),(MEM_SIZE)((n))))))
+       STMT_START { SvLEN_set(sv, n); \
+               SvPV_set((sv), (MEM_WRAP_CHECK_(n,char)                 \
+                               (char*)saferealloc((Malloc_t)SvPVX(sv), \
+                                                  (MEM_SIZE)((n)))));  \
+                } STMT_END
+
+#define SvPV_shrink_to_cur(sv) STMT_START { \
+                  const STRLEN _lEnGtH = SvCUR(sv) + 1; \
+                  SvPV_renew(sv, _lEnGtH); \
+                } STMT_END
+
+#define SvPV_free(sv) \
+       STMT_START { assert(SvTYPE(sv) >= SVt_PV);      \
+               if (SvLEN(sv)) {                        \
+                   if(SvOOK(sv)) {                     \
+                     Safefree(SvPVX(sv) - SvIVX(sv));  \
+                     SvFLAGS(sv) &= ~SVf_OOK;          \
+                   } else {                            \
+                     Safefree(SvPVX(sv));              \
+                   }                                   \
+               }                                       \
+       } STMT_END
+
+#define SvPVX_const(sv)        ((const char*)SvPVX(sv))
 
 #define BmRARE(sv)     ((XPVBM*)  SvANY(sv))->xbm_rare
 #define BmUSEFUL(sv)   ((XPVBM*)  SvANY(sv))->xbm_useful