Skip to content

Commit

Permalink
Replace sv_newmortal + sv_setsv_nomg with sv_mortalcopy_flags
Browse files Browse the repository at this point in the history
Creating a new mortal copy of an existing SV can also be done via:
* `newsv = sv_newmortal();`
* `sv_setsv_nomg(newsv, sv);`

The same outcome can be achieved in a single function call:
* `newsv = sv_mortalcopy_flags(sv, SV_DO_COW_SVSETSV);`
  • Loading branch information
richardleach committed Feb 3, 2025
1 parent 54858f1 commit fb6608f
Show file tree
Hide file tree
Showing 5 changed files with 6 additions and 12 deletions.
3 changes: 1 addition & 2 deletions pp.c
Original file line number Diff line number Diff line change
Expand Up @@ -4063,8 +4063,7 @@ PP(pp_chr)
{
if (ckWARN(WARN_UTF8)) {
if (SvGMAGICAL(top)) {
SV *top2 = sv_newmortal();
sv_setsv_nomg(top2, top);
SV *top2 = sv_mortalcopy_flags(top, SV_DO_COW_SVSETSV);
top = top2;
}
Perl_warner(aTHX_ packWARN(WARN_UTF8),
Expand Down
3 changes: 1 addition & 2 deletions pp_ctl.c
Original file line number Diff line number Diff line change
Expand Up @@ -4738,8 +4738,7 @@ S_require_file(pTHX_ SV *sv)
loader = *av_fetch(AV_FROM_REF(loader), 0, TRUE);
if (SvGMAGICAL(loader)) {
SvGETMAGIC(loader);
SV *l = sv_newmortal();
sv_setsv_nomg(l, loader);
SV *l = sv_mortalcopy_flags(loader, SV_DO_COW_SVSETSV);
loader = l;
}
}
Expand Down
3 changes: 1 addition & 2 deletions pp_hot.c
Original file line number Diff line number Diff line change
Expand Up @@ -6745,8 +6745,7 @@ Perl_vivify_ref(pTHX_ SV *sv, U32 to_what)
if (SvGMAGICAL(sv)) {
/* copy the sv without magic to prevent magic from being
executed twice */
SV* msv = sv_newmortal();
sv_setsv_nomg(msv, sv);
SV* msv = sv_mortalcopy_flags(sv, SV_DO_COW_SVSETSV);
return msv;
}
return sv;
Expand Down
6 changes: 2 additions & 4 deletions pp_sys.c
Original file line number Diff line number Diff line change
Expand Up @@ -563,14 +563,12 @@ PP_wrapped(pp_warn, 0, 1)
SvGETMAGIC(errsv);
if (SvROK(errsv)) {
if (SvGMAGICAL(errsv)) {
exsv = sv_newmortal();
sv_setsv_nomg(exsv, errsv);
exsv = sv_mortalcopy_flags(errsv, SV_DO_COW_SVSETSV);
}
else exsv = errsv;
}
else if (SvPOKp(errsv) ? SvCUR(errsv) : SvNIOKp(errsv)) {
exsv = sv_newmortal();
sv_setsv_nomg(exsv, errsv);
exsv = sv_mortalcopy_flags(errsv, SV_DO_COW_SVSETSV);
sv_catpvs(exsv, "\t...caught");
}
else {
Expand Down
3 changes: 1 addition & 2 deletions sv.c
Original file line number Diff line number Diff line change
Expand Up @@ -10316,8 +10316,7 @@ Perl_sv_2io(pTHX_ SV *const sv)
if (!io) {
SV *newsv = sv;
if (SvGMAGICAL(sv)) {
newsv = sv_newmortal();
sv_setsv_nomg(newsv, sv);
newsv = sv_mortalcopy_flags(sv, SV_DO_COW_SVSETSV);
}
Perl_croak(aTHX_ "Bad filehandle: %" SVf, SVfARG(newsv));
}
Expand Down

0 comments on commit fb6608f

Please sign in to comment.