From: Vincent Pit Date: Wed, 11 Mar 2015 00:58:17 +0000 (-0300) Subject: Factor the sv_magicext() calls into one vmg_sv_magicext() X-Git-Tag: v0.56~6 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=409cfea21be163f1168685893a9a61a5cfc45b8e;p=perl%2Fmodules%2FVariable-Magic.git Factor the sv_magicext() calls into one vmg_sv_magicext() This new function effectively steals the given object. --- diff --git a/Magic.xs b/Magic.xs index 4c5cd5c..c6d7454 100644 --- a/Magic.xs +++ b/Magic.xs @@ -276,6 +276,24 @@ STATIC OP *vmg_trampoline_bump(pTHX_ vmg_trampoline *t, SV *sv, OP *o) { #endif /* VMG_NEEDS_TRAMPOLINE */ +/* --- Cleaner version of sv_magicext() ------------------------------------ */ + +STATIC MAGIC *vmg_sv_magicext(pTHX_ SV *sv, SV *obj, const MGVTBL *vtbl, const void *ptr, I32 len) { +#define vmg_sv_magicext(S, O, V, P, L) vmg_sv_magicext(aTHX_ (S), (O), (V), (P), (L)) + MAGIC *mg; + + mg = sv_magicext(sv, obj, PERL_MAGIC_ext, vtbl, ptr, len); + if (!mg) + return NULL; + + mg->mg_private = 0; + + if (mg->mg_flags & MGf_REFCOUNTED) + SvREFCNT_dec(obj); + + return mg; +} + /* --- Safe version of call_sv() ------------------------------------------- */ STATIC I32 vmg_call_sv(pTHX_ SV *sv, I32 flags, int (*cleanup)(pTHX_ void *), void *ud) { @@ -705,11 +723,10 @@ STATIC SV *vmg_wizard_sv_new(pTHX_ const vmg_wizard *w) { #endif if (w) { - MAGIC *mg = sv_magicext(wiz, NULL, PERL_MAGIC_ext, &vmg_wizard_sv_vtbl, - (const char *) w, 0); - mg->mg_private = 0; + MAGIC *mg; + mg = vmg_sv_magicext(wiz, NULL, &vmg_wizard_sv_vtbl, w, 0); #if VMG_THREADSAFE - mg->mg_flags |= MGf_DUP; + mg->mg_flags |= MGf_DUP; #endif } SvREADONLY_on(wiz); @@ -900,11 +917,7 @@ STATIC UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, data = (w->cb_data) ? vmg_data_new(w->cb_data, sv, args, items) : NULL; t = vmg_vtable_vtbl(w->vtable); - mg = sv_magicext(sv, data, PERL_MAGIC_ext, t, (const char *) wiz, HEf_SVKEY); - mg->mg_private = 0; - - /* sv_magicext() calls mg_magical and increments data's refcount */ - SvREFCNT_dec(data); + mg = vmg_sv_magicext(sv, data, t, wiz, HEf_SVKEY); if (t->svt_copy) mg->mg_flags |= MGf_COPY; @@ -1181,8 +1194,7 @@ STATIC SV *vmg_dispell_guard_new(pTHX_ MAGIC *root) { SV *guard; guard = sv_newmortal(); - sv_magicext(guard, NULL, PERL_MAGIC_ext, &vmg_dispell_guard_vtbl, - (char *) root, 0); + vmg_sv_magicext(guard, NULL, &vmg_dispell_guard_vtbl, root, 0); return guard; } @@ -1432,22 +1444,16 @@ STATIC int vmg_svt_free_cleanup(pTHX_ void *ud_) { PL_op = vmg_trampoline_bump(&MY_CXT.propagate_errsv, errsv, PL_op); } else if (optype == OP_LEAVEEVAL) { SV *guard = sv_newmortal(); - sv_magicext(guard, errsv, PERL_MAGIC_ext, &vmg_propagate_errsv_vtbl, - NULL, 0); - SvREFCNT_dec(errsv); + vmg_sv_magicext(guard, errsv, &vmg_propagate_errsv_vtbl, NULL, 0); } #else /* !VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */ # if !VMG_HAS_PERL(5, 8, 9) { SV *guard = sv_newmortal(); - sv_magicext(guard, errsv, PERL_MAGIC_ext, &vmg_propagate_errsv_vtbl, - NULL, 0); - SvREFCNT_dec(errsv); + vmg_sv_magicext(guard, errsv, &vmg_propagate_errsv_vtbl, NULL, 0); } # else - sv_magicext(ERRSV, errsv, PERL_MAGIC_ext, &vmg_propagate_errsv_vtbl, - NULL, 0); - SvREFCNT_dec(errsv); + vmg_sv_magicext(ERRSV, errsv, &vmg_propagate_errsv_vtbl, NULL, 0); # endif #endif /* VMG_PROPAGATE_ERRSV_NEEDS_TRAMPOLINE */