]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blobdiff - Types.xs
Clean up map entries associated to uncatched OPs
[perl/modules/Lexical-Types.git] / Types.xs
index 0644b00fadc02cac5f2a7d62433fff146310bd9e..bcf5fa15c9ac6937ac4e74347ff1a0b1b64631e3 100644 (file)
--- a/Types.xs
+++ b/Types.xs
@@ -77,6 +77,9 @@
 
 #include "ptable.h"
 
+#define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
+#define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
+
 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
 
 typedef struct {
@@ -103,7 +106,7 @@ STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
   }
  }
 
- ptable_hints_store(aPTBL_ ud->tbl, ent->key, val);
+ ptable_hints_store(ud->tbl, ent->key, val);
  SvREFCNT_inc(val);
 }
 
@@ -111,7 +114,6 @@ STATIC void lt_thread_cleanup(pTHX_ void *);
 
 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
  int *level = ud;
- SV  *id;
 
  if (*level) {
   *level = 0;
@@ -121,19 +123,19 @@ STATIC void lt_thread_cleanup(pTHX_ void *ud) {
  } else {
   dMY_CXT;
   PerlMemShared_free(level);
-  ptable_hints_free(aPTBL_ MY_CXT.tbl);
+  ptable_hints_free(MY_CXT.tbl);
  }
 }
 
-STATIC SV *lt_tag(pPTBL_ SV *value) {
-#define lt_tag(V) lt_tag(aPTBL_ (V))
+STATIC SV *lt_tag(pTHX_ SV *value) {
+#define lt_tag(V) lt_tag(aTHX_ (V))
  dMY_CXT;
 
  value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
  /* We only need for the key to be an unique tag for looking up the value later.
   * Allocated memory provides convenient unique identifiers, so that's why we
   * use the value pointer as the key itself. */
- ptable_hints_store(aPTBL_ MY_CXT.tbl, value, value);
+ ptable_hints_store(MY_CXT.tbl, value, value);
  SvREFCNT_inc(value);
 
  return newSVuv(PTR2UV(value));
@@ -179,28 +181,32 @@ STATIC U32 lt_hash = 0;
 
 STATIC SV *lt_hint(pTHX) {
 #define lt_hint() lt_hint(aTHX)
- SV *id;
-#if LT_HAS_PERL(5, 10, 0)
id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
-                                     NULL,
-                                     __PACKAGE__, __PACKAGE_LEN__,
-                                     0,
-                                     lt_hash);
+ SV *hint;
+#if LT_HAS_PERL(5, 9, 5)
hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
+                                       NULL,
+                                       __PACKAGE__, __PACKAGE_LEN__,
+                                       0,
+                                       lt_hash);
 #else
  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, lt_hash);
  if (!val)
   return 0;
id = *val;
hint = *val;
 #endif
- return (id && SvOK(id)) ? id : NULL;
+ return (hint && SvOK(hint)) ? hint : NULL;
 }
 
 /* ... op => info map ...................................................... */
 
+#define PTABLE_NAME        ptable_map
 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
 
 #include "ptable.h"
 
+/* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
+#define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
+
 STATIC ptable *lt_op_map = NULL;
 
 #ifdef USE_ITHREADS
@@ -214,8 +220,8 @@ typedef struct {
  OP *(*pp_padsv)(pTHX);
 } lt_op_info;
 
-STATIC void lt_map_store(pPTBL_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) {
-#define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aPTBL_ (O), (OP), (TP), (TM), (PP))
+STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) {
+#define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aPTBLMS_ (O), (OP), (TP), (TM), (PP))
  lt_op_info *oi;
 
 #ifdef USE_ITHREADS
@@ -224,7 +230,7 @@ STATIC void lt_map_store(pPTBL_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *typ
 
  if (!(oi = ptable_fetch(lt_op_map, o))) {
   oi = PerlMemShared_malloc(sizeof *oi);
-  ptable_store(aPTBL_ lt_op_map, o, oi);
+  ptable_map_store(lt_op_map, o, oi);
  }
 
  oi->orig_pkg  = orig_pkg;
@@ -257,6 +263,19 @@ STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
  return val;
 }
 
+STATIC void lt_map_delete(pTHX_ const OP *o) {
+#define lt_map_delete(O) lt_map_delete(aTHX_ (O))
+#ifdef USE_ITHREADS
+ MUTEX_LOCK(&lt_op_map_mutex);
+#endif
+
+ ptable_map_store(lt_op_map, o, NULL);
+
+#ifdef USE_ITHREADS
+ MUTEX_UNLOCK(&lt_op_map_mutex);
+#endif
+}
+
 /* --- Hooks --------------------------------------------------------------- */
 
 /* ... Our pp_padsv ........................................................ */
@@ -414,9 +433,11 @@ STATIC OP *lt_ck_padany(pTHX_ OP *o) {
   lt_pp_padsv_save();
 
   lt_map_store(o, orig_pkg, type_pkg, type_meth, lt_pp_padsv_saved);
+ } else {
+skip:
+  lt_map_delete(o);
  }
 
-skip:
  return o;
 }
 
@@ -425,6 +446,8 @@ STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
  lt_pp_padsv_restore(o);
 
+ lt_map_delete(o);
+
  return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o);
 }