From 676a626cda7c01b054108a863daaae03a10ff3cd Mon Sep 17 00:00:00 2001 From: Nicholas Clark Date: Mon, 12 Sep 2005 21:21:39 +0000 Subject: [PATCH] Integrate: [ 24348] Add a new macro SvPV_free() which undoes OOK and free()s the PVX(), becase there's a lot of code around that calls SvOOK_off(), memmov()s the buffer, then promptly free()s it. So avoid the needless memmov(). [ 24351] Refactor Perl_sv_utf8_upgrade_flags to use SvPV_free p4raw-link: @24351 on //depot/perl: c4e7c7127c0c9e659689937dc3312255631d7334 p4raw-link: @24348 on //depot/perl: 8bd4d4c5ee440ccae167e2a7f5bf6f74ff02916b p4raw-id: //depot/maint-5.8/perl@25394 p4raw-integrated: from //depot/perl@24348 'edit in' pp_ctl.c pp_hot.c (@24271..) sv.c (@24344..) 'merge in' sv.h (@24277..) perl.c (@24338..) pp.c (@24339..) --- perl.c | 3 +-- pp.c | 7 ++---- pp_ctl.c | 4 +--- pp_hot.c | 7 ++---- sv.c | 74 ++++++++++++++++++++++++++-------------------------------------- sv.h | 12 +++++++++++ 6 files changed, 48 insertions(+), 59 deletions(-) diff --git a/perl.c b/perl.c index 83e518d..f9fe5f2 100644 --- a/perl.c +++ b/perl.c @@ -1062,8 +1062,7 @@ perl_destruct(pTHXx) } /* we know that type >= SVt_PV */ - SvOOK_off(PL_mess_sv); - Safefree(SvPVX(PL_mess_sv)); + SvPV_free(PL_mess_sv); Safefree(SvANY(PL_mess_sv)); Safefree(PL_mess_sv); PL_mess_sv = Nullsv; diff --git a/pp.c b/pp.c index 1252e01..61258c7 100644 --- a/pp.c +++ b/pp.c @@ -178,9 +178,7 @@ PP(pp_rv2gv) if (SvTYPE(sv) < SVt_RV) sv_upgrade(sv, SVt_RV); if (SvPVX(sv)) { - SvOOK_off(sv); /* backoff */ - if (SvLEN(sv)) - Safefree(SvPVX(sv)); + SvPV_free(sv); SvLEN_set(sv, 0); SvCUR_set(sv, 0); } @@ -831,8 +829,7 @@ PP(pp_undef) break; default: if (SvTYPE(sv) >= SVt_PV && SvPVX(sv) && SvLEN(sv)) { - SvOOK_off(sv); - Safefree(SvPVX(sv)); + SvPV_free(sv); SvPV_set(sv, Nullch); SvLEN_set(sv, 0); } diff --git a/pp_ctl.c b/pp_ctl.c index a632464..332e2d9 100644 --- a/pp_ctl.c +++ b/pp_ctl.c @@ -207,9 +207,7 @@ PP(pp_substcont) } cx->sb_rxtainted |= RX_MATCH_TAINTED(rx); - SvOOK_off(targ); - if (SvLEN(targ)) - Safefree(SvPVX(targ)); + SvPV_free(targ); SvPV_set(targ, SvPVX(dstr)); SvCUR_set(targ, SvCUR(dstr)); SvLEN_set(targ, SvLEN(dstr)); diff --git a/pp_hot.c b/pp_hot.c index f09485b..44a6232 100644 --- a/pp_hot.c +++ b/pp_hot.c @@ -2262,9 +2262,7 @@ PP(pp_subst) else sv_catpvn(dstr, s, strend - s); - SvOOK_off(TARG); - if (SvLEN(TARG)) - Safefree(SvPVX(TARG)); + SvPV_free(TARG); SvPV_set(TARG, SvPVX(dstr)); SvCUR_set(TARG, SvCUR(dstr)); SvLEN_set(TARG, SvLEN(dstr)); @@ -3076,8 +3074,7 @@ Perl_vivify_ref(pTHX_ SV *sv, U32 to_what) if (SvTYPE(sv) < SVt_RV) sv_upgrade(sv, SVt_RV); else if (SvTYPE(sv) >= SVt_PV) { - SvOOK_off(sv); - Safefree(SvPVX(sv)); + SvPV_free(sv); SvLEN_set(sv, 0); SvCUR_set(sv, 0); } diff --git a/sv.c b/sv.c index 9e42637..e35af0c 100644 --- a/sv.c +++ b/sv.c @@ -3435,9 +3435,6 @@ use the Encode extension for that. STRLEN Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags) { - U8 *s, *t, *e; - int hibit = 0; - if (sv == &PL_sv_undef) return 0; if (!SvPOK(sv)) { @@ -3462,31 +3459,32 @@ Perl_sv_utf8_upgrade_flags(pTHX_ register SV *sv, I32 flags) if (PL_encoding && !(flags & SV_UTF8_NO_ENCODING)) sv_recode_to_utf8(sv, PL_encoding); else { /* Assume Latin-1/EBCDIC */ - /* This function could be much more efficient if we - * had a FLAG in SVs to signal if there are any hibit - * chars in the PV. Given that there isn't such a flag - * make the loop as fast as possible. */ - s = (U8 *) SvPVX(sv); - e = (U8 *) SvEND(sv); - t = s; - while (t < e) { - U8 ch = *t++; - if ((hibit = !NATIVE_IS_INVARIANT(ch))) - break; - } - if (hibit) { - STRLEN len; - (void)SvOOK_off(sv); - s = (U8*)SvPVX(sv); - len = SvCUR(sv) + 1; /* Plus the \0 */ - SvPV_set(sv, (char*)bytes_to_utf8((U8*)s, &len)); - SvCUR_set(sv, len - 1); - if (SvLEN(sv) != 0) - Safefree(s); /* No longer using what was there before. */ - SvLEN_set(sv, len); /* No longer know the real size. */ - } - /* Mark as UTF-8 even if no hibit - saves scanning loop */ - SvUTF8_on(sv); + /* This function could be much more efficient if we + * had a FLAG in SVs to signal if there are any hibit + * chars in the PV. Given that there isn't such a flag + * make the loop as fast as possible. */ + U8 *s = (U8 *) SvPVX(sv); + U8 *e = (U8 *) SvEND(sv); + U8 *t = s; + int hibit = 0; + + while (t < e) { + U8 ch = *t++; + if ((hibit = !NATIVE_IS_INVARIANT(ch))) + break; + } + if (hibit) { + STRLEN len = SvCUR(sv) + 1; /* Plus the \0 */ + s = bytes_to_utf8((U8*)s, &len); + + SvPV_free(sv); /* No longer using what was there before. */ + + SvPV_set(sv, (char*)s); + SvCUR_set(sv, len - 1); + SvLEN_set(sv, len); /* No longer know the real size. */ + } + /* Mark as UTF-8 even if no hibit - saves scanning loop */ + SvUTF8_on(sv); } return SvCUR(sv); } @@ -3956,16 +3954,7 @@ Perl_sv_setsv_flags(pTHX_ SV *dstr, register SV *sstr, I32 flags) return; } if (SvPVX(dstr)) { - if (SvLEN(dstr)) { - /* Unwrap the OOK offset by hand, to save a needless - memmove on memory that's about to be free()d. */ - char *pv = SvPVX(dstr); - if (SvOOK(dstr)) { - pv -= SvIVX(dstr); - SvFLAGS(dstr) &= ~SVf_OOK; - } - Safefree(pv); - } + SvPV_free(dstr); SvLEN_set(dstr, 0); SvCUR_set(dstr, 0); } @@ -4235,9 +4224,8 @@ Perl_sv_usepvn(pTHX_ register SV *sv, register char *ptr, register STRLEN len) (void)SvOK_off(sv); return; } - (void)SvOOK_off(sv); - if (SvPVX(sv) && SvLEN(sv)) - Safefree(SvPVX(sv)); + if (SvPVX(sv)) + SvPV_free(sv); Renew(ptr, len+1, char); SvPV_set(sv, ptr); SvCUR_set(sv, len); @@ -7738,9 +7726,7 @@ Perl_newSVrv(pTHX_ SV *rv, const char *classname) if (SvTYPE(rv) < SVt_RV) sv_upgrade(rv, SVt_RV); else if (SvTYPE(rv) > SVt_RV) { - SvOOK_off(rv); - if (SvPVX(rv) && SvLEN(rv)) - Safefree(SvPVX(rv)); + SvPV_free(rv); SvCUR_set(rv, 0); SvLEN_set(rv, 0); } diff --git a/sv.h b/sv.h index 69cbb34..ab73796 100644 --- a/sv.h +++ b/sv.h @@ -801,6 +801,18 @@ and leaves the UTF-8 status as it was. STMT_START { assert(SvTYPE(sv) >= SVt_PV); \ (SvCUR(sv) = (val) - SvPVX(sv)); } 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 BmRARE(sv) ((XPVBM*) SvANY(sv))->xbm_rare #define BmUSEFUL(sv) ((XPVBM*) SvANY(sv))->xbm_useful #define BmPREVIOUS(sv) ((XPVBM*) SvANY(sv))->xbm_previous -- 1.8.3.1