/* ... Magic cast/dispell .................................................. */
#if VMG_UVAR
+
STATIC I32 vmg_svt_val(pTHX_ IV, SV *);
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))
} 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);
}
}
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) {
mg->mg_len = sizeof(*uf);
} else {
/* Remove the uvar magic. */
- vmg_uvar_del(sv, prevmagic, mg, moremagic);
+ vmg_mg_del(sv, prevmagic, mg, moremagic);
}
}
}
/* 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. */