From: Vincent Pit Date: Fri, 10 Aug 2012 16:47:59 +0000 (+0200) Subject: Rename vmg_uvar_del() to vmg_mg_del() to reuse its logic X-Git-Tag: v0.51~9 X-Git-Url: http://git.vpit.fr/?a=commitdiff_plain;h=056b5122597d90171577d0fad01bf17d08e401e8;p=perl%2Fmodules%2FVariable-Magic.git Rename vmg_uvar_del() to vmg_mg_del() to reuse its logic The new vmg_mg_del() is now also used to remove our PERL_MAGIC_ext tokens. --- diff --git a/Magic.xs b/Magic.xs index 24f8d5c..94d816f 100644 --- a/Magic.xs +++ b/Magic.xs @@ -755,6 +755,7 @@ STATIC SV *vmg_data_get(pTHX_ SV *sv, const vmg_wizard *w) { /* ... Magic cast/dispell .................................................. */ #if VMG_UVAR + STATIC I32 vmg_svt_val(pTHX_ IV, SV *); typedef struct { @@ -762,17 +763,32 @@ typedef struct { struct ufuncs old_uf; } vmg_uvar_ud; -STATIC void vmg_uvar_del(SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) { - if (prevmagic) { +#endif /* VMG_UVAR */ + +STATIC void vmg_mg_del(pTHX_ SV *sv, MAGIC *prevmagic, MAGIC *mg, MAGIC *moremagic) { +#define vmg_mg_del(S, P, M, N) vmg_mg_del(aTHX_ (S), (P), (M), (N)) + if (prevmagic) prevmagic->mg_moremagic = moremagic; - } else { + else SvMAGIC_set(sv, moremagic); - } mg->mg_moremagic = NULL; - Safefree(mg->mg_ptr); + + /* Destroy private data */ +#if VMG_UVAR + if (mg->mg_type == PERL_MAGIC_uvar) { + Safefree(mg->mg_ptr); + } else { +#endif /* VMG_UVAR */ + if (mg->mg_obj != sv) + SvREFCNT_dec(mg->mg_obj); + /* Unreference the wizard */ + SvREFCNT_dec((SV *) mg->mg_ptr); +#if VMG_UVAR + } +#endif /* VMG_UVAR */ + Safefree(mg); } -#endif /* VMG_UVAR */ STATIC UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, I32 items) { #define vmg_cast(S, W, WIZ, A, I) vmg_cast(aTHX_ (S), (W), (WIZ), (A), (I)) @@ -845,7 +861,7 @@ STATIC UV vmg_cast(pTHX_ SV *sv, const vmg_wizard *w, const SV *wiz, SV **args, } else { /* It's another uvar magic, backup it and replace it by ours. */ ud.old_uf = *uf; - vmg_uvar_del(sv, prevmagic, mg, moremagic); + vmg_mg_del(sv, prevmagic, mg, moremagic); } } @@ -898,19 +914,7 @@ STATIC UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) { if (!mg) return 0; - if (prevmagic) { - prevmagic->mg_moremagic = moremagic; - } else { - SvMAGIC_set(sv, moremagic); - } - mg->mg_moremagic = NULL; - - /* Destroy private data */ - if (mg->mg_obj != sv) - SvREFCNT_dec(mg->mg_obj); - /* Unreference the wizard */ - SvREFCNT_dec((SV *) mg->mg_ptr); - Safefree(mg); + vmg_mg_del(sv, prevmagic, mg, moremagic); #if VMG_UVAR if (uvars == 1 && SvTYPE(sv) >= SVt_PVHV) { @@ -945,7 +949,7 @@ STATIC UV vmg_dispell(pTHX_ SV *sv, const vmg_wizard *w) { mg->mg_len = sizeof(*uf); } else { /* Remove the uvar magic. */ - vmg_uvar_del(sv, prevmagic, mg, moremagic); + vmg_mg_del(sv, prevmagic, mg, moremagic); } } } @@ -1191,14 +1195,10 @@ STATIC int vmg_svt_free_cleanup(pTHX_ void *ud) { /* We are about to croak() while sv is being destroyed. Try to clean up * things a bit. */ mg = SvMAGIC(sv); - SvREFCNT_dec((SV *) mg->mg_ptr); - /* mg->mg_obj may not be refcounted if the data constructor returned the - * variable itself. */ - if (mg->mg_flags & MGf_REFCOUNTED) - SvREFCNT_dec(mg->mg_obj); - SvMAGIC_set(sv, mg->mg_moremagic); - Safefree(mg); - mg_magical(sv); + if (mg) { + vmg_mg_del(sv, NULL, mg, mg->mg_moremagic); + mg_magical(sv); + } SvREFCNT_dec(sv); /* After that, propagate the error upwards. */