From: Nicholas Clark Date: Wed, 26 Dec 2007 18:12:32 +0000 (+0000) Subject: Take code that occurs in three places to take a scalar and ready it to X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=43230e26bd52e1dcdb541bb4a927c941262b74ed;p=p5sagit%2Fp5-mst-13.2.git Take code that occurs in three places to take a scalar and ready it to hold a reference, and convert it to a macro define prepare_SV_for_RV(). p4raw-id: //depot/perl@32737 --- diff --git a/pp.c b/pp.c index 08ebe5e..ed3ae32 100644 --- 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); diff --git a/pp_hot.c b/pp_hot.c index 276010c..764d5be 100644 --- 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 --- 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 --- 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 */