]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - Magic.xs
Skip NULL values when walking a table
[perl/modules/Variable-Magic.git] / Magic.xs
index e336abe648bb8efc737b08bb2378bdaaa433ddac..2de483fd63c10169fbb789cb5413ca231340ea26 100644 (file)
--- a/Magic.xs
+++ b/Magic.xs
 
 #define __PACKAGE__ "Variable::Magic"
 
+#undef VOID2
+#ifdef __cplusplus
+# define VOID2(T, P) static_cast<T>(P)
+#else
+# define VOID2(T, P) (P)
+#endif
+
 #ifndef VMG_PERL_PATCHLEVEL
 # ifdef PERL_PATCHNUM
 #  define VMG_PERL_PATCHLEVEL PERL_PATCHNUM
@@ -449,6 +456,15 @@ STATIC void vmg_mgwiz_free(pTHX_ MGWIZ *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);
@@ -534,7 +550,7 @@ STATIC MGWIZ *vmg_mgwiz_clone(pTHX_ const MGWIZ *w) {
 #if VMG_THREADSAFE
 
 #define PTABLE_NAME        ptable
-#define PTABLE_VAL_FREE(V) vmg_mgwiz_free(V)
+#define PTABLE_VAL_FREE(V) vmg_mgwiz_free(VOID2(MGWIZ *, (V)))
 
 #define pPTBL  pTHX
 #define pPTBL_ pTHX_
@@ -564,13 +580,13 @@ START_MY_CXT
 #if VMG_THREADSAFE
 
 STATIC void vmg_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
- my_cxt_t *ud = ud_;
+ my_cxt_t *ud = VOID2(my_cxt_t *, ud_);
  MGWIZ *w;
 
  if (ud->owner == aTHX)
   return;
 
- w = vmg_mgwiz_clone(ent->val);
+ w = vmg_mgwiz_clone(VOID2(MGWIZ *, ent->val));
  if (w)
   ptable_store(ud->wizards, ent->key, w);
 }
@@ -663,7 +679,7 @@ STATIC const MGWIZ *vmg_wizard_mgwiz(pTHX_ const SV *wiz) {
 
  {
   dMY_CXT;
-  return ptable_fetch(MY_CXT.wizards, w);
+  return VOID2(const MGWIZ *, ptable_fetch(MY_CXT.wizards, w));
  }
 }
 
@@ -957,7 +973,7 @@ STATIC void vmg_op_info_init(pTHX_ unsigned int opinfo) {
   case VMG_OP_INFO_OBJECT: {
    dMY_CXT;
    if (!MY_CXT.b__op_stashes[0]) {
-    opclass c;
+    int c;
     require_pv("B.pm");
     for (c = OPc_NULL; c < OPc_MAX; ++c)
      MY_CXT.b__op_stashes[c] = gv_stashpv(vmg_opclassnames[c], 1);
@@ -1078,7 +1094,7 @@ STATIC U32 vmg_svt_len(pTHX_ SV *sv, MAGIC *mg) {
  if (t < SVt_PVAV) {
   STRLEN l;
 #if VMG_HAS_PERL(5, 9, 3)
-  const U8 *s = SvPV_const(sv, l);
+  const U8 *s = VOID2(const U8 *, VOID2(const void *, SvPV_const(sv, l)));
 #else
   U8 *s = SvPV(sv, l);
 #endif
@@ -1381,7 +1397,7 @@ PROTOTYPE: DISABLE
 PREINIT:
  ptable *t;
  U32     had_b__op_stash = 0;
opclass c;
int     c;
 PPCODE:
  {
   my_cxt_t ud;