in reply to Re^2: Intercept any changes to the sv_refcnt/SvREFCNT of an object
in thread Intercept any changes to the sv_refcnt/SvREFCNT of an object
As incrementing/decrementing the reference count happens (very) often, these are implemented as C macros and not hookable function calls in sv.c:
#if defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN) # define SvREFCNT_inc(sv) \ ({ \ SV * const _sv = MUTABLE_SV(sv); \ if (_sv) \ (SvREFCNT(_sv))++; \ _sv; \ }) # define SvREFCNT_inc_simple(sv) \ ({ \ if (sv) \ (SvREFCNT(sv))++; \ MUTABLE_SV(sv); \ }) # define SvREFCNT_inc_NN(sv) \ ({ \ SV * const _sv = MUTABLE_SV(sv); \ SvREFCNT(_sv)++; \ _sv; \ }) # define SvREFCNT_inc_void(sv) \ ({ \ SV * const _sv = MUTABLE_SV(sv); \ if (_sv) \ (void)(SvREFCNT(_sv)++); \ }) #else # define SvREFCNT_inc(sv) \ ((PL_Sv=MUTABLE_SV(sv)) ? (++(SvREFCNT(PL_Sv)),PL_Sv) : NULL) # define SvREFCNT_inc_simple(sv) \ ((sv) ? (SvREFCNT(sv)++,MUTABLE_SV(sv)) : NULL) # define SvREFCNT_inc_NN(sv) \ (PL_Sv=MUTABLE_SV(sv),++(SvREFCNT(PL_Sv)),PL_Sv) # define SvREFCNT_inc_void(sv) \ (void)((PL_Sv=MUTABLE_SV(sv)) ? ++(SvREFCNT(PL_Sv)) : 0) #endif
The "easy" way is to just redefine these macros and recompile Perl. You should be aware that invoking Perl code from within such a hook would be very unwise, as Perl code inevitably will allocate values whose refcount needs to be incremented, which would invoke your hook, recursively.
This is also the hard way.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Intercept any changes to the sv_refcnt/SvREFCNT of an object
by vernonlyon (Novice) on Sep 13, 2011 at 16:50 UTC |