Skip to content

Commit 00852b4

Browse files
committed
sv.h - add SvREFCNT_dec_set_NULL()
and also SvREFCNT_dec_ret_NULL() which is used to implement SvREFCNT_dec_set_NULL(). The set_NULL() macro is intended to be used to replace code like this: if (sv) { SvREFCNT_dec_NN(sv); sv = NULL; } The function form just facilitates it, and can be used in situations where returning NULL after decrementing a refcount would be reduce code complexity.
1 parent 567a365 commit 00852b4

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

embed.fnc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3202,6 +3202,10 @@ Adp |SV * |sv_ref |NULLOK SV *dst \
32023202
|const int ob
32033203
AMdip |void |SvREFCNT_dec |NULLOK SV *sv
32043204
AMdip |void |SvREFCNT_dec_NN|NN SV *sv
3205+
Adip |SV * |SvREFCNT_dec_ret_NULL \
3206+
|NULLOK SV *sv
3207+
Adm |void |SvREFCNT_dec_set_NULL \
3208+
|NULLOK SV *sv
32053209
AMTdip |SV * |SvREFCNT_inc |NULLOK SV *sv
32063210
AMTdip |SV * |SvREFCNT_inc_NN|NN SV *sv
32073211
AMTdip |void |SvREFCNT_inc_void \

embed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,7 @@
107107
# define SvNV(a) Perl_SvNV(aTHX_ a)
108108
# define SvNV_nomg(a) Perl_SvNV_nomg(aTHX_ a)
109109
# define SvPVXtrue(a) Perl_SvPVXtrue(aTHX_ a)
110+
# define SvREFCNT_dec_ret_NULL(a) Perl_SvREFCNT_dec_ret_NULL(aTHX_ a)
110111
# define SvTRUE(a) Perl_SvTRUE(aTHX_ a)
111112
# define SvTRUE_NN(a) Perl_SvTRUE_NN(aTHX_ a)
112113
# define SvTRUE_common(a,b) Perl_SvTRUE_common(aTHX_ a,b)

proto.h

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

sv.h

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,13 +336,30 @@ effects and you don't need the return value.
336336
C<SvREFCNT_inc_simple_void_NN> can only be used with expressions without side
337337
effects, you don't need the return value, and you know C<sv> is not C<NULL>.
338338
339-
=for apidoc SvREFCNT_dec
340-
=for apidoc_item SvREFCNT_dec_NN
339+
=for apidoc SvREFCNT_dec
340+
=for apidoc_item SvREFCNT_dec_set_NULL
341+
=for apidoc_item |SV*|SvREFCNT_dec_ret_NULL|SV *sv
342+
=for apidoc_item SvREFCNT_dec_NN
341343
342344
These decrement the reference count of the given SV.
343345
344346
C<SvREFCNT_dec_NN> may only be used when C<sv> is known to not be C<NULL>.
345347
348+
The function C<SvREFCNT_dec_ret_NULL()> is identical to the
349+
C<SvREFCNT_dec()> except it returns a NULL C<SV *>. It is used by
350+
C<SvREFCNT_dec_set_NULL()> which is a macro which will, when passed a
351+
non-NULL argument, decrement the reference count of its argument and
352+
then set it to NULL. You can replace code of the following form:
353+
354+
if (sv) {
355+
SvREFCNT_dec_NN(sv);
356+
sv = NULL;
357+
}
358+
359+
with
360+
361+
SvREFCNT_dec_set_NULL(sv);
362+
346363
=for apidoc Am|svtype|SvTYPE|SV* sv
347364
Returns the type of the SV. See C<L</svtype>>.
348365
@@ -375,6 +392,10 @@ perform the upgrade if necessary. See C<L</svtype>>.
375392
#define SvREFCNT_inc_simple_void_NN(sv) (void)(++SvREFCNT(MUTABLE_SV(sv)))
376393

377394
#define SvREFCNT_dec(sv) Perl_SvREFCNT_dec(aTHX_ MUTABLE_SV(sv))
395+
#define SvREFCNT_dec_set_NULL(sv) \
396+
STMT_START { \
397+
sv = Perl_SvREFCNT_dec_ret_NULL(aTHX_ MUTABLE_SV(sv)); \
398+
} STMT_END
378399
#define SvREFCNT_dec_NN(sv) Perl_SvREFCNT_dec_NN(aTHX_ MUTABLE_SV(sv))
379400

380401
#define SVTYPEMASK 0xff

sv_inline.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,6 +664,7 @@ Perl_SvREFCNT_inc(SV *sv)
664664
SvREFCNT(sv)++;
665665
return sv;
666666
}
667+
667668
PERL_STATIC_INLINE SV *
668669
Perl_SvREFCNT_inc_NN(SV *sv)
669670
{
@@ -672,12 +673,14 @@ Perl_SvREFCNT_inc_NN(SV *sv)
672673
SvREFCNT(sv)++;
673674
return sv;
674675
}
676+
675677
PERL_STATIC_INLINE void
676678
Perl_SvREFCNT_inc_void(SV *sv)
677679
{
678680
if (LIKELY(sv != NULL))
679681
SvREFCNT(sv)++;
680682
}
683+
681684
PERL_STATIC_INLINE void
682685
Perl_SvREFCNT_dec(pTHX_ SV *sv)
683686
{
@@ -690,6 +693,15 @@ Perl_SvREFCNT_dec(pTHX_ SV *sv)
690693
}
691694
}
692695

696+
PERL_STATIC_INLINE SV *
697+
Perl_SvREFCNT_dec_ret_NULL(pTHX_ SV *sv)
698+
{
699+
PERL_ARGS_ASSERT_SVREFCNT_DEC_RET_NULL;
700+
Perl_SvREFCNT_dec(aTHX_ sv);
701+
return NULL;
702+
}
703+
704+
693705
PERL_STATIC_INLINE void
694706
Perl_SvREFCNT_dec_NN(pTHX_ SV *sv)
695707
{

0 commit comments

Comments
 (0)