Take code that occurs in three places to take a scalar and ready it to
Nicholas Clark [Wed, 26 Dec 2007 18:12:32 +0000 (18:12 +0000)]
hold a reference, and convert it to a macro define prepare_SV_for_RV().

p4raw-id: //depot/perl@32737

pp.c
pp_hot.c
sv.c
sv.h

diff --git a/pp.c b/pp.c
index 08ebe5e..ed3ae32 100644 (file)
--- a/pp.c
+++ b/pp.c
@@ -172,13 +172,7 @@ PP(pp_rv2gv)
                        const char * const name = CopSTASHPV(PL_curcop);
                        gv = newGVgen(name);
                    }
-                   if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)
-                       sv_upgrade(sv, SVt_IV);
-                   else if (SvPVX_const(sv)) {
-                       SvPV_free(sv);
-                       SvLEN_set(sv, 0);
-                        SvCUR_set(sv, 0);
-                   }
+                   prepare_SV_for_RV(sv);
                    SvRV_set(sv, (SV*)gv);
                    SvROK_on(sv);
                    SvSETMAGIC(sv);
index 276010c..764d5be 100644 (file)
--- a/pp_hot.c
+++ b/pp_hot.c
@@ -2940,13 +2940,7 @@ Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
     if (!SvOK(sv)) {
        if (SvREADONLY(sv))
            Perl_croak(aTHX_ PL_no_modify);
-       if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)
-           sv_upgrade(sv, SVt_IV);
-       else if (SvTYPE(sv) >= SVt_PV) {
-           SvPV_free(sv);
-            SvLEN_set(sv, 0);
-           SvCUR_set(sv, 0);
-       }
+       prepare_SV_for_RV(sv);
        switch (to_what) {
        case OPpDEREF_SV:
            SvRV_set(sv, newSV(0));
diff --git a/sv.c b/sv.c
index 7b49ce2..577c134 100644 (file)
--- a/sv.c
+++ b/sv.c
@@ -7857,12 +7857,8 @@ Perl_newSVrv(pTHX_ SV *rv, const char *classname)
        sv_upgrade(rv, SVt_IV);
     } else if (SvROK(rv)) {
        SvREFCNT_dec(SvRV(rv));
-    } else if (SvTYPE(rv) < SVt_PV && SvTYPE(rv) != SVt_IV)
-       sv_upgrade(rv, SVt_IV);
-    else if (SvTYPE(rv) >= SVt_PV) {
-       SvPV_free(rv);
-       SvCUR_set(rv, 0);
-       SvLEN_set(rv, 0);
+    } else {
+       prepare_SV_for_RV(rv);
     }
 
     SvOK_off(rv);
diff --git a/sv.h b/sv.h
index 37b79c9..8e2417c 100644 (file)
--- a/sv.h
+++ b/sv.h
@@ -1441,6 +1441,20 @@ the scalar's value cannot change unless written to.
                     }                                                  \
                 } STMT_END
 
+#ifdef PERL_CORE
+/* Code that crops up in three places to take a scalar and ready it to hold
+   a reference */
+#  define prepare_SV_for_RV(sv)                                                \
+    STMT_START {                                                       \
+                   if (SvTYPE(sv) < SVt_PV && SvTYPE(sv) != SVt_IV)    \
+                       sv_upgrade(sv, SVt_IV);                         \
+                   else if (SvPVX_const(sv)) {                         \
+                       SvPV_free(sv);                                  \
+                       SvLEN_set(sv, 0);                               \
+                        SvCUR_set(sv, 0);                              \
+                   }                                                   \
+                } STMT_END
+#endif
 
 #define PERL_FBM_TABLE_OFFSET 1        /* Number of bytes between EOS and table */