]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blobdiff - Types.xs
Correct the refcount of the source SVs that aren't actually stored
[perl/modules/Lexical-Types.git] / Types.xs
index f22d034577974e4dbd7dea2575e87f15b006b80e..6a159bcdc2775cc517d8c11275237a197a934a0f 100644 (file)
--- a/Types.xs
+++ b/Types.xs
@@ -133,6 +133,29 @@ START_MY_CXT
 
 #if LT_THREADSAFE
 
+STATIC SV *lt_clone(pTHX_ SV *sv, tTHX owner) {
+#define lt_clone(S, O) lt_clone(aTHX_ (S), (O))
+ CLONE_PARAMS  param;
+ AV           *stashes = NULL;
+ SV           *dupsv;
+
+ if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
+  stashes = newAV();
+
+ param.stashes    = stashes;
+ param.flags      = 0;
+ param.proto_perl = owner;
+
+ dupsv = sv_dup(sv, &param);
+
+ if (stashes) {
+  av_undef(stashes);
+  SvREFCNT_dec(stashes);
+ }
+
+ return dupsv;
+}
+
 STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
  my_cxt_t  *ud  = ud_;
  lt_hint_t *h1 = ent->val;
@@ -140,19 +163,8 @@ STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) {
 
  *h2 = *h1;
 
- if (ud->owner != aTHX) {
-  SV *val = h1->code;
-  CLONE_PARAMS param;
-  AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
-  param.stashes    = stashes;
-  param.flags      = 0;
-  param.proto_perl = ud->owner;
-  h2->code = sv_dup(val, &param);
-  if (stashes) {
-   av_undef(stashes);
-   SvREFCNT_dec(stashes);
-  }
- }
+ if (ud->owner != aTHX)
+  h2->code = lt_clone(h1->code, ud->owner);
 
  ptable_hints_store(ud->tbl, ent->key, h2);
  SvREFCNT_inc(h2->code);
@@ -307,14 +319,19 @@ STATIC perl_mutex lt_op_map_mutex;
 #endif
 
 typedef struct {
+#ifdef MULTIPLICITY
+ STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
+ char *buf;
+#else /* MULTIPLICITY */
  SV *orig_pkg;
  SV *type_pkg;
  SV *type_meth;
+#endif /* !MULTIPLICITY */
  OP *(*pp_padsv)(pTHX);
 } lt_op_info;
 
-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))
+STATIC void lt_map_store(pTHX_ 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(aTHX_ (O), (OP), (TP), (TM), (PP))
  lt_op_info *oi;
 
 #ifdef USE_ITHREADS
@@ -324,11 +341,48 @@ STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *t
  if (!(oi = ptable_fetch(lt_op_map, o))) {
   oi = PerlMemShared_malloc(sizeof *oi);
   ptable_map_store(lt_op_map, o, oi);
+#ifdef MULTIPLICITY
+  oi->buf      = NULL;
+  oi->buf_size = 0;
+#else /* MULTIPLICITY */
+ } else {
+  SvREFCNT_dec(oi->orig_pkg);
+  SvREFCNT_dec(oi->type_pkg);
+  SvREFCNT_dec(oi->type_meth);
+#endif /* !MULTIPLICITY */
  }
 
+#ifdef MULTIPLICITY
+ {
+  STRLEN op_len       = SvCUR(orig_pkg);
+  STRLEN tp_len       = SvCUR(type_pkg);
+  STRLEN tm_len       = SvCUR(type_meth);
+  STRLEN new_buf_size = op_len + tp_len + tm_len;
+  char *buf;
+  if (new_buf_size > oi->buf_size) {
+   PerlMemShared_free(oi->buf);
+   oi->buf      = PerlMemShared_malloc(new_buf_size);
+   oi->buf_size = new_buf_size;
+  }
+  buf  = oi->buf;
+  Copy(SvPVX(orig_pkg),  buf, op_len, char);
+  buf += op_len;
+  Copy(SvPVX(type_pkg),  buf, tp_len, char);
+  buf += tp_len;
+  Copy(SvPVX(type_meth), buf, tm_len, char);
+  oi->orig_pkg_len  = op_len;
+  oi->type_pkg_len  = tp_len;
+  oi->type_meth_len = tm_len;
+  SvREFCNT_dec(orig_pkg);
+  SvREFCNT_dec(type_pkg);
+  SvREFCNT_dec(type_meth);
+ }
+#else /* MULTIPLICITY */
  oi->orig_pkg  = orig_pkg;
  oi->type_pkg  = type_pkg;
  oi->type_meth = type_meth;
+#endif /* !MULTIPLICITY */
+
  oi->pp_padsv  = pp_padsv;
 
 #ifdef USE_ITHREADS
@@ -381,20 +435,40 @@ STATIC OP *lt_pp_padsv(pTHX) {
   SV *sv         = PAD_SVl(targ);
 
   if (sv) {
+   SV *orig_pkg, *type_pkg, *type_meth;
    int items;
    dSP;
 
    ENTER;
    SAVETMPS;
 
+#ifdef MULTIPLICITY
+   {
+    STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len;
+    char *buf = oi.buf;
+    orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
+    SvREADONLY_on(orig_pkg);
+    buf      += op_len;
+    type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
+    SvREADONLY_on(type_pkg);
+    buf      += tp_len;
+    type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len));
+    SvREADONLY_on(type_meth);
+   }
+#else /* MULTIPLICITY */
+   orig_pkg  = oi.orig_pkg;
+   type_pkg  = oi.type_pkg;
+   type_meth = oi.type_meth;
+#endif /* !MULTIPLICITY */
+
    PUSHMARK(SP);
    EXTEND(SP, 3);
-   PUSHs(oi.type_pkg);
+   PUSHs(type_pkg);
    PUSHs(sv);
-   PUSHs(oi.orig_pkg);
+   PUSHs(orig_pkg);
    PUTBACK;
 
-   items = call_sv(oi.type_meth, G_ARRAY | G_METHOD);
+   items = call_sv(type_meth, G_ARRAY | G_METHOD);
 
    SPAGAIN;
    switch (items) {