From 6fe12e961c5d3cd304646808c8c4a231be32d57e Mon Sep 17 00:00:00 2001 From: "Paul \"LeoNerd\" Evans" Date: Fri, 19 Jul 2024 23:36:43 +0100 Subject: [PATCH] Define and use GvREFCNT_inc*() macro family --- gv.h | 18 ++++++++++++++++++ perl.c | 12 +++--------- pp_sort.c | 8 ++++---- sv.c | 2 +- 4 files changed, 26 insertions(+), 14 deletions(-) diff --git a/gv.h b/gv.h index 1dd38b43d04c7..380054bc5626d 100644 --- a/gv.h +++ b/gv.h @@ -23,6 +23,24 @@ struct gp { HEK * gp_file_hek; /* file first declared in (for -w) */ }; +/* +=for apidoc_section $GV + +=for apidoc Am|GV *|GvREFCNT_inc|GV *gv +=for apidoc_item |GV *|GvREFCNT_inc_simple|GV *gv +=for apidoc_item |GV *|GvREFCNT_inc_simple_NN|GV *gv + +These all increment the reference count of the given SV, which must be a GV. +They are useful when assigning the result into a typed pointer as they avoid +the need to cast the result to the appropriate type. + +=cut +*/ + +#define GvREFCNT_inc(gv) ((GV *)SvREFCNT_inc((SV *)gv)) +#define GvREFCNT_inc_simple(gv) ((GV *)SvREFCNT_inc_simple((SV *)gv)) +#define GvREFCNT_inc_simple_NN(gv) ((GV *)SvREFCNT_inc_simple_NN((SV *)gv)) + #define GvXPVGV(gv) ((XPVGV*)SvANY(gv)) diff --git a/perl.c b/perl.c index 177531b945665..e52bf78202efc 100644 --- a/perl.c +++ b/perl.c @@ -4475,15 +4475,9 @@ Perl_init_debugger(pTHX) PL_curstash = (HV *)SvREFCNT_inc_simple(PL_debstash); Perl_init_dbargs(aTHX); - PL_DBgv = MUTABLE_GV( - SvREFCNT_inc(gv_fetchpvs("DB::DB", GV_ADDMULTI, SVt_PVGV)) - ); - PL_DBline = MUTABLE_GV( - SvREFCNT_inc(gv_fetchpvs("DB::dbline", GV_ADDMULTI, SVt_PVAV)) - ); - PL_DBsub = MUTABLE_GV(SvREFCNT_inc( - gv_HVadd(gv_fetchpvs("DB::sub", GV_ADDMULTI, SVt_PVHV)) - )); + PL_DBgv = GvREFCNT_inc(gv_fetchpvs("DB::DB", GV_ADDMULTI, SVt_PVGV)); + PL_DBline = GvREFCNT_inc(gv_fetchpvs("DB::dbline", GV_ADDMULTI, SVt_PVAV)); + PL_DBsub = GvREFCNT_inc(gv_HVadd(gv_fetchpvs("DB::sub", GV_ADDMULTI, SVt_PVHV))); PL_DBsingle = GvSV((gv_fetchpvs("DB::single", GV_ADDMULTI, SVt_PV))); if (!SvIOK(PL_DBsingle)) sv_setiv(PL_DBsingle, 0); diff --git a/pp_sort.c b/pp_sort.c index 503d399cc2e02..e5fdc27f958ad 100644 --- a/pp_sort.c +++ b/pp_sort.c @@ -923,12 +923,12 @@ PP(pp_sort) /* standard perl sub with values passed as $a and $b */ SAVEGENERICSV(PL_firstgv); SAVEGENERICSV(PL_secondgv); - PL_firstgv = MUTABLE_GV(SvREFCNT_inc( + PL_firstgv = GvREFCNT_inc( gv_fetchpvs("a", GV_ADD|GV_NOTQUAL, SVt_PV) - )); - PL_secondgv = MUTABLE_GV(SvREFCNT_inc( + ); + PL_secondgv = GvREFCNT_inc( gv_fetchpvs("b", GV_ADD|GV_NOTQUAL, SVt_PV) - )); + ); /* make sure the GP isn't removed out from under us for * the SAVESPTR() */ save_gp(PL_firstgv, 0); diff --git a/sv.c b/sv.c index 009d3bab264e4..25624b4ba6f44 100644 --- a/sv.c +++ b/sv.c @@ -6687,7 +6687,7 @@ S_anonymise_cv_maybe(pTHX_ GV *gv, CV* cv) CvANON_on(cv); CvCVGV_RC_on(cv); - SvANY(cv)->xcv_gv_u.xcv_gv = MUTABLE_GV(SvREFCNT_inc(anongv)); + SvANY(cv)->xcv_gv_u.xcv_gv = GvREFCNT_inc_simple(anongv); }