]> git.vpit.fr Git - perl/modules/Sub-Op.git/blobdiff - Op.xs
Store the custom op name in a pointer table
[perl/modules/Sub-Op.git] / Op.xs
diff --git a/Op.xs b/Op.xs
index b1ecdb483d46f79365aa519e1b40460751311607..fe67d0838e80e459cefcb49c9bcee3d9af81d18a 100644 (file)
--- a/Op.xs
+++ b/Op.xs
@@ -92,6 +92,25 @@ STATIC SV *so_clone(pTHX_ SV *sv, tTHX owner) {
 
 #endif /* SO_THREADSAFE */
 
+#define PTABLE_NAME        ptable
+#define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
+
+#include "ptable.h"
+
+/* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
+#define ptable_store(T, K, V) ptable_store(aPTBLMS_ (T), (K), (V))
+
+STATIC ptable *sub_op_name = NULL;
+
+#ifdef USE_ITHREADS
+STATIC perl_mutex sub_op_name_mutex;
+#endif
+
+typedef struct {
+ STRLEN len;
+ char   buf;
+} sub_op_name_t;
+
 /* --- Public API ---------------------------------------------------------- */
 
 #include "sub_op.h"
@@ -255,6 +274,20 @@ STATIC OP *sub_op_ck_entersub(pTHX_ OP *o) {
    }
 
    sub_op_linklist(o);
+
+   {
+    sub_op_name_t *on = PerlMemShared_malloc(sizeof(*on) + len);
+    Copy(name, &on->buf, len, char);
+    (&on->buf)[len] = '\0';
+    on->len = len;
+#ifdef USE_ITHREADS
+    MUTEX_LOCK(&sub_op_name_mutex);
+#endif /* USE_ITHREADS */
+    ptable_store(sub_op_name, o, on);
+#ifdef USE_ITHREADS
+    MUTEX_UNLOCK(&sub_op_name_mutex);
+#endif /* USE_ITHREADS */
+   }
   }
  }
 
@@ -309,6 +342,11 @@ PROTOTYPES: ENABLE
 
 BOOT:
 {
+ sub_op_name = ptable_new();
+#ifdef USE_ITHREADS
+ MUTEX_INIT(&sub_op_name_mutex);
+#endif
+
  MY_CXT_INIT;
  MY_CXT.map         = newHV();
  MY_CXT.next_pkg    = newAV();
@@ -370,18 +408,21 @@ _custom_name(SV *op)
 PROTOTYPE: $
 PREINIT:
  OP *o;
- SV *key;
- HE *he;
+ sub_op_name_t *on;
 PPCODE:
  if (!SvROK(op))
   XSRETURN_UNDEF;
  o = INT2PTR(OP *, SvIV(SvRV(op)));
  if (!o || o->op_type != OP_CUSTOM)
   XSRETURN_UNDEF;
- key = newSViv(PTR2IV(o->op_ppaddr));
- he  = hv_fetch_ent(PL_custom_op_names, key, 0, 0);
- SvREFCNT_dec(key);
- if (!he)
+#ifdef USE_ITHREADS
+ MUTEX_LOCK(&sub_op_name_mutex);
+#endif /* USE_ITHREADS */
+ on = ptable_fetch(sub_op_name, o);
+#ifdef USE_ITHREADS
+ MUTEX_UNLOCK(&sub_op_name_mutex);
+#endif /* USE_ITHREADS */
+ if (!on)
   XSRETURN_UNDEF;
- ST(0) = sv_mortalcopy(HeVAL(he));
+ ST(0) = sv_2mortal(newSVpvn(&on->buf, on->len));
  XSRETURN(1);