]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Prevent bogus invalid syntaxes caused by reallocated memory chunks
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #ifndef NOOP
15 # define NOOP
16 #endif
17
18 #ifndef dNOOP
19 # define dNOOP
20 #endif
21
22 #ifndef SvPV_const
23 # define SvPV_const SvPV
24 #endif
25
26 #ifndef SvPV_nolen_const
27 # define SvPV_nolen_const SvPV_nolen
28 #endif
29
30 #ifndef SvPVX_const
31 # define SvPVX_const SvPVX
32 #endif
33
34 #ifndef sv_catpvn_nomg
35 # define sv_catpvn_nomg sv_catpvn
36 #endif
37
38 #ifndef mPUSHu
39 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
40 #endif
41
42 #ifndef HvNAME_get
43 # define HvNAME_get(H) HvNAME(H)
44 #endif
45
46 #ifndef HvNAMELEN_get
47 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
48 #endif
49
50 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
51
52 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
53 # ifndef PL_lex_inwhat
54 #  define PL_lex_inwhat PL_parser->lex_inwhat
55 # endif
56 # ifndef PL_linestr
57 #  define PL_linestr PL_parser->linestr
58 # endif
59 # ifndef PL_bufptr
60 #  define PL_bufptr PL_parser->bufptr
61 # endif
62 # ifndef PL_oldbufptr
63 #  define PL_oldbufptr PL_parser->oldbufptr
64 # endif
65 #else
66 # ifndef PL_lex_inwhat
67 #  define PL_lex_inwhat PL_Ilex_inwhat
68 # endif
69 # ifndef PL_linestr
70 #  define PL_linestr PL_Ilinestr
71 # endif
72 # ifndef PL_bufptr
73 #  define PL_bufptr PL_Ibufptr
74 # endif
75 # ifndef PL_oldbufptr
76 #  define PL_oldbufptr PL_Ioldbufptr
77 # endif
78 #endif
79
80 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
81 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
82 #endif
83
84 /* ... Thread safety and multiplicity ...................................... */
85
86 #ifndef I_MULTIPLICITY
87 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
88 #  define I_MULTIPLICITY 1
89 # else
90 #  define I_MULTIPLICITY 0
91 # endif
92 #endif
93 #if I_MULTIPLICITY && !defined(tTHX)
94 # define tTHX PerlInterpreter*
95 #endif
96
97 #if I_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
98 # define I_THREADSAFE 1
99 # ifndef MY_CXT_CLONE
100 #  define MY_CXT_CLONE \
101     dMY_CXT_SV;                                                      \
102     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
103     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
104     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
105 # endif
106 #else
107 # define I_THREADSAFE 0
108 # undef  dMY_CXT
109 # define dMY_CXT      dNOOP
110 # undef  MY_CXT
111 # define MY_CXT       indirect_globaldata
112 # undef  START_MY_CXT
113 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
114 # undef  MY_CXT_INIT
115 # define MY_CXT_INIT  NOOP
116 # undef  MY_CXT_CLONE
117 # define MY_CXT_CLONE NOOP
118 #endif
119
120 /* --- Helpers ------------------------------------------------------------- */
121
122 /* ... Thread-safe hints ................................................... */
123
124 /* If any of those are true, we need to store the hint in a global table. */
125
126 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
127
128 typedef struct {
129  SV  *code;
130 #if I_WORKAROUND_REQUIRE_PROPAGATION
131  I32  requires;
132 #endif
133 } indirect_hint_t;
134
135 #define PTABLE_NAME ptable_hints
136
137 #if I_WORKAROUND_REQUIRE_PROPAGATION
138 # define PTABLE_VAL_FREE(V) \
139    { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
140 #else
141 # define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
142 #endif
143
144 #define pPTBL  pTHX
145 #define pPTBL_ pTHX_
146 #define aPTBL  aTHX
147 #define aPTBL_ aTHX_
148
149 #include "ptable.h"
150
151 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
152 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
153
154 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
155
156 /* Define the op->str ptable here because we need to be able to clean it during
157  * thread cleanup. */
158
159 #define PTABLE_NAME        ptable
160 #define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
161
162 #define pPTBL  pTHX
163 #define pPTBL_ pTHX_
164 #define aPTBL  aTHX
165 #define aPTBL_ aTHX_
166
167 #include "ptable.h"
168
169 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
170 #define ptable_delete(T, K)   ptable_delete(aTHX_ (T), (K))
171 #define ptable_clear(T)       ptable_clear(aTHX_ (T))
172 #define ptable_free(T)        ptable_free(aTHX_ (T))
173
174 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
175
176 typedef struct {
177 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
178  ptable     *tbl; /* It really is a ptable_hints */
179 #endif
180  ptable     *map;
181  const char *linestr;
182 #if I_THREADSAFE
183  tTHX        owner;
184 #endif
185 } my_cxt_t;
186
187 START_MY_CXT
188
189 #if I_THREADSAFE
190
191 STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
192  my_cxt_t        *ud = ud_;
193  indirect_hint_t *h1 = ent->val;
194  indirect_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
195
196  *h2 = *h1;
197
198  if (ud->owner != aTHX) {
199   SV *val = h1->code;
200   CLONE_PARAMS param;
201   AV *stashes = (SvTYPE(val) == SVt_PVHV && HvNAME_get(val)) ? newAV() : NULL;
202   param.stashes    = stashes;
203   param.flags      = 0;
204   param.proto_perl = ud->owner;
205   h2->code = sv_dup(val, &param);
206   if (stashes) {
207    av_undef(stashes);
208    SvREFCNT_dec(stashes);
209   }
210  }
211
212  ptable_hints_store(ud->tbl, ent->key, h2);
213  SvREFCNT_inc(h2->code);
214 }
215
216 STATIC void indirect_thread_cleanup(pTHX_ void *);
217
218 STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
219  int *level = ud;
220
221  if (*level) {
222   *level = 0;
223   LEAVE;
224   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
225   ENTER;
226  } else {
227   dMY_CXT;
228   PerlMemShared_free(level);
229   ptable_free(MY_CXT.map);
230   ptable_hints_free(MY_CXT.tbl);
231  }
232 }
233
234 #endif /* I_THREADSAFE */
235
236 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
237
238 STATIC SV *indirect_tag(pTHX_ SV *value) {
239 #define indirect_tag(V) indirect_tag(aTHX_ (V))
240  indirect_hint_t *h;
241  dMY_CXT;
242
243  value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
244
245  h = PerlMemShared_malloc(sizeof *h);
246  h->code = SvREFCNT_inc(value);
247
248 #if I_WORKAROUND_REQUIRE_PROPAGATION
249  {
250   const PERL_SI *si;
251   I32            requires = 0;
252
253   for (si = PL_curstackinfo; si; si = si->si_prev) {
254    I32 cxix;
255
256    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
257     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
258
259     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
260      ++requires;
261    }
262   }
263
264   h->requires = requires;
265  }
266 #endif
267
268  /* We only need for the key to be an unique tag for looking up the value later.
269   * Allocated memory provides convenient unique identifiers, so that's why we
270   * use the value pointer as the key itself. */
271  ptable_hints_store(MY_CXT.tbl, value, h);
272
273  return newSVuv(PTR2UV(value));
274 }
275
276 STATIC SV *indirect_detag(pTHX_ const SV *hint) {
277 #define indirect_detag(H) indirect_detag(aTHX_ (H))
278  indirect_hint_t *h;
279  dMY_CXT;
280
281  if (!(hint && SvOK(hint) && SvIOK(hint)))
282   return NULL;
283
284  h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
285
286 #if I_WORKAROUND_REQUIRE_PROPAGATION
287  {
288   const PERL_SI *si;
289   I32            requires = 0;
290
291   for (si = PL_curstackinfo; si; si = si->si_prev) {
292    I32 cxix;
293
294    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
295     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
296
297     if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
298                                && ++requires > h->requires)
299      return NULL;
300    }
301   }
302  }
303 #endif
304
305  return h->code;
306 }
307
308 #else
309
310 STATIC SV *indirect_tag(pTHX_ SV *value) {
311 #define indirect_tag(V) indirect_tag(aTHX_ (V))
312  UV tag = 0;
313
314  if (SvOK(value) && SvROK(value)) {
315   value = SvRV(value);
316   SvREFCNT_inc(value);
317   tag = PTR2UV(value);
318  }
319
320  return newSVuv(tag);
321 }
322
323 #define indirect_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
324
325 #endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
326
327 STATIC U32 indirect_hash = 0;
328
329 STATIC SV *indirect_hint(pTHX) {
330 #define indirect_hint() indirect_hint(aTHX)
331  SV *hint, *code;
332 #if I_HAS_PERL(5, 9, 5)
333  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
334                                        NULL,
335                                        __PACKAGE__, __PACKAGE_LEN__,
336                                        0,
337                                        indirect_hash);
338 #else
339  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__,
340                                                                  indirect_hash);
341  if (!val)
342   return 0;
343  hint = *val;
344 #endif
345  return indirect_detag(hint);
346 }
347
348 /* ... op -> source position ............................................... */
349
350 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
351 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
352  dMY_CXT;
353  SV *val;
354
355  /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
356   * In this case the linestr has temporarly changed, but the old buffer should
357   * still be alive somewhere. */
358
359  if (!PL_lex_inwhat) {
360   const char *pl_linestr = SvPVX_const(PL_linestr);
361   if (MY_CXT.linestr != pl_linestr) {
362    ptable_clear(MY_CXT.map);
363    MY_CXT.linestr = pl_linestr;
364   }
365  }
366
367  val = newSVsv(sv);
368  SvUPGRADE(val, SVt_PVIV);
369  SvUVX(val) = PTR2UV(src);
370  SvIOK_on(val);
371  SvIsUV_on(val);
372  SvREADONLY_on(val);
373
374  ptable_store(MY_CXT.map, o, val);
375 }
376
377 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
378 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
379  dMY_CXT;
380  SV *val;
381
382  if (MY_CXT.linestr != SvPVX_const(PL_linestr))
383   return NULL;
384
385  val = ptable_fetch(MY_CXT.map, o);
386  if (!val) {
387   *name = NULL;
388   return NULL;
389  }
390
391  *name = val;
392  return INT2PTR(const char *, SvUVX(val));
393 }
394
395 STATIC void indirect_map_delete(pTHX_ const OP *o) {
396 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
397  dMY_CXT;
398
399  ptable_delete(MY_CXT.map, o);
400 }
401
402 /* --- Check functions ----------------------------------------------------- */
403
404 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
405 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
406  STRLEN len;
407  const char *p = NULL, *r = SvPV_const(sv, len);
408
409  if (len >= 1 && *r == '$') {
410   ++r;
411   --len;
412   s = strchr(s, '$');
413   if (!s)
414    return NULL;
415  }
416
417  p = strstr(s, r);
418  while (p) {
419   p += len;
420   if (!isALNUM(*p))
421    break;
422   p = strstr(p + 1, r);
423  }
424
425  return p;
426 }
427
428 /* ... ck_const ............................................................ */
429
430 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
431
432 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
433  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
434
435  if (indirect_hint()) {
436   SV *sv = cSVOPo_sv;
437   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
438    indirect_map_store(o, indirect_find(sv, PL_oldbufptr), sv);
439    return o;
440   }
441  }
442
443  indirect_map_delete(o);
444  return o;
445 }
446
447 /* ... ck_rv2sv ............................................................ */
448
449 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
450
451 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
452  if (indirect_hint()) {
453   OP *op = cUNOPo->op_first;
454   SV *sv;
455   const char *name = NULL, *s;
456   STRLEN len;
457   OPCODE type = (OPCODE) op->op_type;
458
459   switch (type) {
460    case OP_GV:
461    case OP_GVSV: {
462     GV *gv = cGVOPx_gv(op);
463     name = GvNAME(gv);
464     len  = GvNAMELEN(gv);
465     break;
466    }
467    default:
468     if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
469      SV *nsv = cSVOPx_sv(op);
470      if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
471       name = SvPV_const(nsv, len);
472     }
473   }
474   if (!name)
475    goto done;
476
477   sv = sv_2mortal(newSVpvn("$", 1));
478   sv_catpvn_nomg(sv, name, len);
479   s = indirect_find(sv, PL_oldbufptr);
480   if (!s) { /* If it failed, retry without the current stash */
481    const char *stash = HvNAME_get(PL_curstash);
482    STRLEN stashlen = HvNAMELEN_get(PL_curstash);
483
484    if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
485        || name[stashlen] != ':' || name[stashlen+1] != ':') {
486     /* Failed again ? Try to remove main */
487     stash = "main";
488     stashlen = 4;
489     if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
490         || name[stashlen] != ':' || name[stashlen+1] != ':')
491      goto done;
492    }
493
494    sv_setpvn(sv, "$", 1);
495    stashlen += 2;
496    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
497    s = indirect_find(sv, PL_oldbufptr);
498    if (!s)
499     goto done;
500   }
501
502   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
503   indirect_map_store(o, s, sv);
504   return o;
505  }
506
507 done:
508  o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
509
510  indirect_map_delete(o);
511  return o;
512 }
513
514 /* ... ck_padany ........................................................... */
515
516 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
517
518 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
519  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
520
521  if (indirect_hint()) {
522   SV *sv;
523   const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
524
525   while (s < t && isSPACE(*s)) ++s;
526   if (*s == '$' && ++s <= t) {
527    while (s < t && isSPACE(*s)) ++s;
528    while (s < t && isSPACE(*t)) --t;
529    sv = sv_2mortal(newSVpvn("$", 1));
530    sv_catpvn_nomg(sv, s, t - s + 1);
531    indirect_map_store(o, s, sv);
532    return o;
533   }
534  }
535
536  indirect_map_delete(o);
537  return o;
538 }
539
540 /* ... ck_method ........................................................... */
541
542 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
543
544 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
545  if (indirect_hint()) {
546   OP *op = cUNOPo->op_first;
547   SV *sv;
548   const char *s = indirect_map_fetch(op, &sv);
549   if (!s) {
550    sv = cSVOPx_sv(op);
551    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
552     goto done;
553    sv = sv_mortalcopy(sv);
554    s  = indirect_find(sv, PL_oldbufptr);
555   }
556   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
557   /* o may now be a method_named */
558   indirect_map_store(o, s, sv);
559   return o;
560  }
561
562 done:
563  o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
564
565  indirect_map_delete(o);
566  return o;
567 }
568
569 /* ... ck_entersub ......................................................... */
570
571 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
572
573 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
574  SV *code = indirect_hint();
575
576  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
577
578  if (code) {
579   const char *mpos, *opos;
580   SV *mnamesv, *onamesv;
581   OP *mop, *oop;
582   LISTOP *lop;
583
584   oop = o;
585   do {
586    lop = (LISTOP *) oop;
587    if (!(lop->op_flags & OPf_KIDS))
588     goto done;
589    oop = lop->op_first;
590   } while (oop->op_type != OP_PUSHMARK);
591   oop = oop->op_sibling;
592   mop = lop->op_last;
593
594   if (!oop)
595    goto done;
596
597   switch (oop->op_type) {
598    case OP_CONST:
599    case OP_RV2SV:
600    case OP_PADSV:
601     break;
602    default:
603     goto done;
604   }
605
606   if (mop->op_type == OP_METHOD)
607    mop = cUNOPx(mop)->op_first;
608   else if (mop->op_type != OP_METHOD_NAMED)
609    goto done;
610
611   mpos = indirect_map_fetch(mop, &mnamesv);
612   if (!mpos)
613    goto done;
614
615   opos = indirect_map_fetch(oop, &onamesv);
616   if (!opos)
617    goto done;
618
619   if (mpos < opos) {
620    SV     *file;
621    line_t  line;
622    dSP;
623
624    ENTER;
625    SAVETMPS;
626
627    onamesv = sv_mortalcopy(onamesv);
628    mnamesv = sv_mortalcopy(mnamesv);
629
630 #ifdef USE_ITHREADS
631    file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
632 #else
633    file = sv_mortalcopy(CopFILESV(&PL_compiling));
634 #endif
635    line = CopLINE(&PL_compiling);
636
637    PUSHMARK(SP);
638    EXTEND(SP, 4);
639    PUSHs(onamesv);
640    PUSHs(mnamesv);
641    PUSHs(file);
642    mPUSHu(line);
643    PUTBACK;
644
645    call_sv(code, G_VOID);
646
647    PUTBACK;
648
649    FREETMPS;
650    LEAVE;
651   }
652  }
653
654 done:
655  return o;
656 }
657
658 STATIC U32 indirect_initialized = 0;
659
660 /* --- XS ------------------------------------------------------------------ */
661
662 MODULE = indirect      PACKAGE = indirect
663
664 PROTOTYPES: ENABLE
665
666 BOOT:
667 {
668  if (!indirect_initialized++) {
669   HV *stash;
670
671   MY_CXT_INIT;
672   MY_CXT.map     = ptable_new();
673   MY_CXT.linestr = NULL;
674 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
675   MY_CXT.tbl     = ptable_new();
676 #endif
677 #if I_THREADSAFE
678   MY_CXT.owner   = aTHX;
679 #endif
680
681   PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
682
683   indirect_old_ck_const    = PL_check[OP_CONST];
684   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
685   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
686   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
687   indirect_old_ck_padany   = PL_check[OP_PADANY];
688   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
689   indirect_old_ck_method   = PL_check[OP_METHOD];
690   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
691   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
692   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
693
694   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
695   newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
696  }
697 }
698
699 #if I_THREADSAFE
700
701 void
702 CLONE(...)
703 PROTOTYPE: DISABLE
704 PREINIT:
705  ptable *t;
706  int    *level;
707 CODE:
708  {
709   my_cxt_t ud;
710   dMY_CXT;
711   ud.tbl   = t = ptable_new();
712   ud.owner = MY_CXT.owner;
713   ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
714  }
715  {
716   MY_CXT_CLONE;
717   MY_CXT.map     = ptable_new();
718   MY_CXT.linestr = NULL;
719   MY_CXT.tbl     = t;
720   MY_CXT.owner   = aTHX;
721  }
722  {
723   level = PerlMemShared_malloc(sizeof *level);
724   *level = 1;
725   LEAVE;
726   SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
727   ENTER;
728  }
729
730 #endif
731
732 SV *
733 _tag(SV *value)
734 PROTOTYPE: $
735 CODE:
736  RETVAL = indirect_tag(value);
737 OUTPUT:
738  RETVAL