Commit 9e7ee30035 for perl
commit 9e7ee300352d283feb6829ee968c425db388dc5f
Author: Paul Evans <leonerd@leonerd.org.uk>
Date: Wed Oct 7 19:04:37 2026 +0100
Add MgAUXSV_set_{inc,weak} helpers
These allow an AUXSV to be set on a Magicv2 structure and also set
whether the new one is weak or not, handling reference counts correctly,
and also clearing the old one at the right time. Doing this previously
with only `MgAUXSV_set()` was more difficult.
Also while we're here, convert the existing `MgAUXSV_set` from a pure
macro into the same macro-wrapped static inline function that these two
new additions are.
diff --git a/embed.fnc b/embed.fnc
index 2058bb2433..25034b90c4 100644
--- a/embed.fnc
+++ b/embed.fnc
@@ -2290,6 +2290,14 @@ Adfpt |SV * |mess |NN const char *pat \
Adp |SV * |mess_sv |NN SV *basemsg \
|bool consume
CTdop |Free_t |mfree |Malloc_t where
+
+Adipx |void |MgAUXSV_set |NN MAGIC *mg \
+ |NULLOK SV *sv
+Adipx |void |MgAUXSV_set_inc|NN MAGIC *mg \
+ |NULLOK SV *sv
+Adipx |void |MgAUXSV_set_weak \
+ |NN MAGIC *mg \
+ |NULLOK SV *sv
Adp |int |mg_clear |NN SV *sv
Adp |int |mg_copy |NN SV *sv \
|NN SV *nsv \
diff --git a/embed.h b/embed.h
index 9dec7aa656..8bb2d22f46 100644
--- a/embed.h
+++ b/embed.h
@@ -96,6 +96,9 @@
# define AvFILL_(a) Perl_AvFILL_(aTHX_ a)
# define Gv_AMupdate(a,b) Perl_Gv_AMupdate(aTHX_ a,b)
+# define MgAUXSV_set(a,b) Perl_MgAUXSV_set(aTHX_ a,b)
+# define MgAUXSV_set_inc(a,b) Perl_MgAUXSV_set_inc(aTHX_ a,b)
+# define MgAUXSV_set_weak(a,b) Perl_MgAUXSV_set_weak(aTHX_ a,b)
# define SvAMAGIC_off Perl_SvAMAGIC_off
# define SvAMAGIC_on Perl_SvAMAGIC_on
# define SvGETMAGIC(a) Perl_SvGETMAGIC(aTHX_ a)
diff --git a/inline.h b/inline.h
index de91b3f644..dfa9ac4f1c 100644
--- a/inline.h
+++ b/inline.h
@@ -399,6 +399,38 @@ S_MgBYTEPOS(pTHX_ MAGIC *mg, SV *sv, const char *s, STRLEN len)
}
#endif
+PERL_STATIC_INLINE void
+Perl_MgAUXSV_set(pTHX_ MAGIC *mg, SV *sv)
+{
+ PERL_ARGS_ASSERT_MGAUXSV_SET;
+
+ if (MgAUXSV(mg) && !MgWEAK_AUXSV(mg))
+ SvREFCNT_dec(MgAUXSV(mg));
+ mg->mg_obj = sv;
+}
+
+PERL_STATIC_INLINE void
+Perl_MgAUXSV_set_inc(pTHX_ MAGIC *mg, SV *sv)
+{
+ PERL_ARGS_ASSERT_MGAUXSV_SET_INC;
+
+ if (MgAUXSV(mg) && !MgWEAK_AUXSV(mg))
+ SvREFCNT_dec(MgAUXSV(mg));
+ MgWEAK_AUXSV_off(mg);
+ mg->mg_obj = SvREFCNT_inc(sv);
+}
+
+PERL_STATIC_INLINE void
+Perl_MgAUXSV_set_weak(pTHX_ MAGIC *mg, SV *sv)
+{
+ PERL_ARGS_ASSERT_MGAUXSV_SET_WEAK;
+
+ if (MgAUXSV(mg) && !MgWEAK_AUXSV(mg))
+ SvREFCNT_dec(MgAUXSV(mg));
+ MgWEAK_AUXSV_on(mg);
+ mg->mg_obj = sv;
+}
+
/* ------------------------------- pad.h ------------------------------ */
#if defined(PERL_IN_PAD_C) || defined(PERL_IN_OP_C)
diff --git a/mg.h b/mg.h
index 1af613c2c2..85e2fd0ceb 100644
--- a/mg.h
+++ b/mg.h
@@ -303,20 +303,33 @@ Mutator macros to turn on or off the L</MgWEAK_AUXSV> flag.
#define MgWEAK_AUXSV_off(mg) (MgFLAGS(mg) |= MGv2f_REFCOUNTED_AUXSV)
/*
-=for apidoc Am|void|MgAUXSV_set|MAGIC *mg|SV *auxsv
+=for apidoc MgAUXSV_set
Sets a new value of the stored auxilliary SV pointer in the Magic v2
structure. This does I<not> increment the reference count of the new value.
It will decrement the count from the old value, if the L</MgWEAK_AUXSV> flag is
not set.
+=for apidoc MgAUXSV_set_inc
+Sets a new value of the stored auxilliary SV pointer in the Magic v2
+structure, marking that it is not weak (i.e. the L</MgWEAK_AUXSV> flag is
+cleared), and increments the reference count of the stored pointer.
+
+It will decrement the reference count from the old value, if the weak flag
+was previously clear.
+
+=for apidoc MgAUXSV_set_weak
+Sets a new value of the stored auxilliary SV pointer in the Magic v2
+structure, marking that it is weak (i.e. the L</MgWEAK_AUXSV> flag is
+set). It does not increments the reference count of the stored pointer.
+
+It will decrement the reference count from the old value, if the weak flag
+was previously clear.
+
=cut
+
+The above three API functions are implemented in inline.h
+
*/
-#define MgAUXSV_set(mg, sv) \
- STMT_START { \
- MAGIC *mg_ = (mg); \
- if(MgAUXSV(mg_) && !MgWEAK_AUXSV(mg_)) SvREFCNT_dec(MgAUXSV(mg_)); \
- (mg_)->mg_obj = (sv); \
- } STMT_END
/*
=for apidoc Am|void *|MgPTR|MAGIC *mg
diff --git a/proto.h b/proto.h
index 7ad89638c6..712761f9f6 100644
--- a/proto.h
+++ b/proto.h
@@ -17563,6 +17563,33 @@ Perl_CvGV(pTHX_ CV *sv)
PERL_UNUSED_CONTEXT_FOR_ARGS_ASSERT; \
} STMT_END
+PERL_STATIC_INLINE void
+Perl_MgAUXSV_set(pTHX_ MAGIC *mg, SV *sv)
+ Perl_attribute_nonnull_aTHX
+ Perl_attribute_nonnull(pTHX_1);
+# define PERL_ARGS_ASSERT_MGAUXSV_SET \
+ STMT_START { Perl_assert_aTHX; assert(mg); \
+ PERL_UNUSED_CONTEXT_FOR_ARGS_ASSERT; \
+ } STMT_END
+
+PERL_STATIC_INLINE void
+Perl_MgAUXSV_set_inc(pTHX_ MAGIC *mg, SV *sv)
+ Perl_attribute_nonnull_aTHX
+ Perl_attribute_nonnull(pTHX_1);
+# define PERL_ARGS_ASSERT_MGAUXSV_SET_INC \
+ STMT_START { Perl_assert_aTHX; assert(mg); \
+ PERL_UNUSED_CONTEXT_FOR_ARGS_ASSERT; \
+ } STMT_END
+
+PERL_STATIC_INLINE void
+Perl_MgAUXSV_set_weak(pTHX_ MAGIC *mg, SV *sv)
+ Perl_attribute_nonnull_aTHX
+ Perl_attribute_nonnull(pTHX_1);
+# define PERL_ARGS_ASSERT_MGAUXSV_SET_WEAK \
+ STMT_START { Perl_assert_aTHX; assert(mg); \
+ PERL_UNUSED_CONTEXT_FOR_ARGS_ASSERT; \
+ } STMT_END
+
PERL_STATIC_INLINE Stack_off_t
Perl_POPMARK(pTHX)
Perl_attribute_nonnull_aTHX;