+
+/* --- Error messages ------------------------------------------------------ */
+
+STATIC const char vmg_invalid_wiz[] = "Invalid wizard object";
+STATIC const char vmg_wrongargnum[] = "Wrong number of arguments";
+STATIC const char vmg_argstorefailed[] = "Error while storing arguments";
+
+/* --- Signatures ---------------------------------------------------------- */
+
+#define SIG_WZO ((U16) (0x3891))
+#define SIG_WIZ ((U16) (0x3892))
+
+/* --- MGWIZ structure ----------------------------------------------------- */
+
+typedef struct {
+ MGVTBL *vtbl;
+
+ U8 opinfo;
+ U8 uvar;
+
+ SV *cb_data;
+ SV *cb_get, *cb_set, *cb_len, *cb_clear, *cb_free;
+#if MGf_COPY
+ SV *cb_copy;
+#endif /* MGf_COPY */
+#if MGf_DUP
+ SV *cb_dup;
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ SV *cb_local;
+#endif /* MGf_LOCAL */
+#if VMG_UVAR
+ SV *cb_fetch, *cb_store, *cb_exists, *cb_delete;
+#endif /* VMG_UVAR */
+
+#if VMG_MULTIPLICITY
+ tTHX owner;
+#endif /* VMG_MULTIPLICITY */
+} MGWIZ;
+
+STATIC void vmg_mgwiz_free(pTHX_ MGWIZ *w) {
+#define vmg_mgwiz_free(W) vmg_mgwiz_free(aTHX_ (W))
+ if (!w)
+ return;
+
+ /* We reach this point in dirty state when ptable_free() is called from the
+ * atexit cleanup callback, and that the global table still holds a live
+ * wizard. This happens before all the SV bodies are freed, so all the wizard
+ * callbacks are still alive (as they are referenced by the undead wizard).
+ * Hence it is safe to decrement their refcount. Later on, the wizard free
+ * callback itself will trigger when the wizard body is reaped, but it will
+ * be skipped as it guards against dirty state - which is good since nothing
+ * has to be done anymore at that point. */
+
+ SvREFCNT_dec(w->cb_data);
+ SvREFCNT_dec(w->cb_get);
+ SvREFCNT_dec(w->cb_set);
+ SvREFCNT_dec(w->cb_len);
+ SvREFCNT_dec(w->cb_clear);
+ SvREFCNT_dec(w->cb_free);
+#if MGf_COPY
+ SvREFCNT_dec(w->cb_copy);
+#endif /* MGf_COPY */
+#if 0 /* MGf_DUP */
+ SvREFCNT_dec(w->cb_dup);
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ SvREFCNT_dec(w->cb_local);
+#endif /* MGf_LOCAL */
+#if VMG_UVAR
+ SvREFCNT_dec(w->cb_fetch);
+ SvREFCNT_dec(w->cb_store);
+ SvREFCNT_dec(w->cb_exists);
+ SvREFCNT_dec(w->cb_delete);
+#endif /* VMG_UVAR */
+
+ Safefree(w->vtbl);
+ Safefree(w);
+
+ return;
+}
+
+#if VMG_THREADSAFE
+
+#define VMG_CLONE_CB(N) \
+ z->cb_ ## N = (w->cb_ ## N) ? vmg_clone(w->cb_ ## N, w->owner) \
+ : NULL;
+
+STATIC MGWIZ *vmg_mgwiz_clone(pTHX_ const MGWIZ *w) {
+#define vmg_mgwiz_clone(W) vmg_mgwiz_clone(aTHX_ (W))
+ MGVTBL *t;
+ MGWIZ *z;
+
+ if (!w)
+ return NULL;
+
+ Newx(t, 1, MGVTBL);
+ Copy(w->vtbl, t, 1, MGVTBL);
+
+ Newx(z, 1, MGWIZ);
+
+ z->vtbl = t;
+ z->uvar = w->uvar;
+ z->opinfo = w->opinfo;
+
+ VMG_CLONE_CB(data);
+ VMG_CLONE_CB(get);
+ VMG_CLONE_CB(set);
+ VMG_CLONE_CB(len);
+ VMG_CLONE_CB(clear);
+ VMG_CLONE_CB(free);
+#if MGf_COPY
+ VMG_CLONE_CB(copy);
+#endif /* MGf_COPY */
+#if MGf_DUP
+ VMG_CLONE_CB(dup);
+#endif /* MGf_DUP */
+#if MGf_LOCAL
+ VMG_CLONE_CB(local);
+#endif /* MGf_LOCAL */
+#if VMG_UVAR
+ VMG_CLONE_CB(fetch);
+ VMG_CLONE_CB(store);
+ VMG_CLONE_CB(exists);
+ VMG_CLONE_CB(delete);
+#endif /* VMG_UVAR */
+
+ z->owner = aTHX;
+
+ return z;
+}
+
+#endif /* VMG_THREADSAFE */