1 /* This file is part of the indirect Perl module.
2 * See http://search.cpan.org/dist/indirect/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "indirect"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
23 # define Newx(v, n, c) New(0, v, n, c)
27 # define SvPV_const SvPV
30 #ifndef SvPV_nolen_const
31 # define SvPV_nolen_const SvPV_nolen
35 # define SvPVX_const SvPVX
38 #ifndef SvREFCNT_inc_simple_void_NN
39 # ifdef SvREFCNT_inc_simple_NN
40 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc_simple_NN
42 # define SvREFCNT_inc_simple_void_NN SvREFCNT_inc
46 #ifndef sv_catpvn_nomg
47 # define sv_catpvn_nomg sv_catpvn
51 # define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
55 # define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
59 # define HvNAME_get(H) HvNAME(H)
63 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
68 # define OpSIBLING(O) OP_SIBLING(O)
70 # define OpSIBLING(O) ((O)->op_sibling)
74 #define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
76 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
78 # define PL_linestr PL_parser->linestr
81 # define PL_bufptr PL_parser->bufptr
84 # define PL_oldbufptr PL_parser->oldbufptr
86 # ifndef PL_lex_inwhat
87 # define PL_lex_inwhat PL_parser->lex_inwhat
91 # define PL_linestr PL_Ilinestr
94 # define PL_bufptr PL_Ibufptr
97 # define PL_oldbufptr PL_Ioldbufptr
99 # ifndef PL_lex_inwhat
100 # define PL_lex_inwhat PL_Ilex_inwhat
104 #ifndef I_WORKAROUND_REQUIRE_PROPAGATION
105 # define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
108 /* ... Thread safety and multiplicity ...................................... */
110 /* Safe unless stated otherwise in Makefile.PL */
112 # define I_FORKSAFE 1
115 #ifndef I_MULTIPLICITY
116 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
117 # define I_MULTIPLICITY 1
119 # define I_MULTIPLICITY 0
122 #if I_MULTIPLICITY && !defined(tTHX)
123 # define tTHX PerlInterpreter*
126 #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))
127 # define I_THREADSAFE 1
128 # ifndef MY_CXT_CLONE
129 # define MY_CXT_CLONE \
131 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
132 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
133 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
136 # define I_THREADSAFE 0
138 # define dMY_CXT dNOOP
140 # define MY_CXT indirect_globaldata
142 # define START_MY_CXT static my_cxt_t MY_CXT;
144 # define MY_CXT_INIT NOOP
146 # define MY_CXT_CLONE NOOP
149 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
150 # define I_CHECK_MUTEX_LOCK OP_CHECK_MUTEX_LOCK
151 # define I_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
153 # define I_CHECK_MUTEX_LOCK OP_REFCNT_LOCK
154 # define I_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
157 typedef OP *(*indirect_ck_t)(pTHX_ OP *);
159 #ifdef wrap_op_checker
161 # define indirect_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
165 static void indirect_ck_replace(pTHX_ OPCODE type, indirect_ck_t new_ck, indirect_ck_t *old_ck_p) {
166 #define indirect_ck_replace(T, NC, OCP) indirect_ck_replace(aTHX_ (T), (NC), (OCP))
169 *old_ck_p = PL_check[type];
170 PL_check[type] = new_ck;
172 I_CHECK_MUTEX_UNLOCK;
177 static void indirect_ck_restore(pTHX_ OPCODE type, indirect_ck_t *old_ck_p) {
178 #define indirect_ck_restore(T, OCP) indirect_ck_restore(aTHX_ (T), (OCP))
181 PL_check[type] = *old_ck_p;
184 I_CHECK_MUTEX_UNLOCK;
187 /* --- Helpers ------------------------------------------------------------- */
189 /* ... Thread-safe hints ................................................... */
191 #if I_WORKAROUND_REQUIRE_PROPAGATION
198 #define I_HINT_STRUCT 1
200 #define I_HINT_CODE(H) ((H)->code)
202 #define I_HINT_FREE(H) { \
203 indirect_hint_t *h = (H); \
204 SvREFCNT_dec(h->code); \
205 PerlMemShared_free(h); \
208 #else /* I_WORKAROUND_REQUIRE_PROPAGATION */
210 typedef SV indirect_hint_t;
212 #define I_HINT_STRUCT 0
214 #define I_HINT_CODE(H) (H)
216 #define I_HINT_FREE(H) SvREFCNT_dec(H);
218 #endif /* !I_WORKAROUND_REQUIRE_PROPAGATION */
222 #define PTABLE_NAME ptable_hints
223 #define PTABLE_VAL_FREE(V) I_HINT_FREE(V)
232 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
233 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
235 #endif /* I_THREADSAFE */
237 /* Define the op->str ptable here because we need to be able to clean it during
246 } indirect_op_info_t;
248 #define PTABLE_NAME ptable
249 #define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
258 #define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
259 #define ptable_delete(T, K) ptable_delete(aTHX_ (T), (K))
260 #define ptable_clear(T) ptable_clear(aTHX_ (T))
261 #define ptable_free(T) ptable_free(aTHX_ (T))
263 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
267 ptable *tbl; /* It really is a ptable_hints */
280 #if I_HAS_PERL(5, 13, 2)
281 CLONE_PARAMS *params;
285 } indirect_ptable_clone_ud;
287 #if I_HAS_PERL(5, 13, 2)
288 # define indirect_ptable_clone_ud_init(U, T, O) \
290 (U).params = Perl_clone_params_new((O), aTHX)
291 # define indirect_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
292 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
294 # define indirect_ptable_clone_ud_init(U, T, O) \
296 (U).params.stashes = newAV(); \
297 (U).params.flags = 0; \
298 (U).params.proto_perl = (O)
299 # define indirect_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
300 # define indirect_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
303 static void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
304 indirect_ptable_clone_ud *ud = ud_;
305 indirect_hint_t *h1 = ent->val;
310 h2 = PerlMemShared_malloc(sizeof *h2);
311 h2->code = indirect_dup_inc(h1->code, ud);
312 #if I_WORKAROUND_REQUIRE_PROPAGATION
313 h2->require_tag = PTR2IV(indirect_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
316 #else /* I_HINT_STRUCT */
318 h2 = indirect_dup_inc(h1, ud);
320 #endif /* !I_HINT_STRUCT */
322 ptable_hints_store(ud->tbl, ent->key, h2);
325 static void indirect_thread_cleanup(pTHX_ void *ud) {
328 SvREFCNT_dec(MY_CXT.global_code);
329 MY_CXT.global_code = NULL;
330 ptable_free(MY_CXT.map);
332 ptable_hints_free(MY_CXT.tbl);
336 static int indirect_endav_free(pTHX_ SV *sv, MAGIC *mg) {
337 SAVEDESTRUCTOR_X(indirect_thread_cleanup, NULL);
342 static MGVTBL indirect_endav_vtbl = {
359 #endif /* I_THREADSAFE */
361 #if I_WORKAROUND_REQUIRE_PROPAGATION
362 static IV indirect_require_tag(pTHX) {
363 #define indirect_require_tag() indirect_require_tag(aTHX)
364 const CV *cv, *outside;
369 /* If for some reason the pragma is operational at run-time, try to discover
370 * the current cv in use. */
373 for (si = PL_curstackinfo; si; si = si->si_prev) {
376 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
377 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
379 switch (CxTYPE(cx)) {
382 /* The propagation workaround is only needed up to 5.10.0 and at that
383 * time format and sub contexts were still identical. And even later the
384 * cv members offsets should have been kept the same. */
386 goto get_enclosing_cv;
388 cv = cx->blk_eval.cv;
389 goto get_enclosing_cv;
400 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
405 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
407 static SV *indirect_tag(pTHX_ SV *value) {
408 #define indirect_tag(V) indirect_tag(aTHX_ (V))
416 #endif /* I_THREADSAFE */
420 if (SvTYPE(value) >= SVt_PVCV) {
422 SvREFCNT_inc_simple_void_NN(code);
427 h = PerlMemShared_malloc(sizeof *h);
429 # if I_WORKAROUND_REQUIRE_PROPAGATION
430 h->require_tag = indirect_require_tag();
431 # endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
432 #else /* I_HINT_STRUCT */
434 #endif /* !I_HINT_STRUCT */
437 /* We only need for the key to be an unique tag for looking up the value later
438 * Allocated memory provides convenient unique identifiers, so that's why we
439 * use the hint as the key itself. */
440 ptable_hints_store(MY_CXT.tbl, h, h);
441 #endif /* I_THREADSAFE */
443 return newSViv(PTR2IV(h));
446 static SV *indirect_detag(pTHX_ const SV *hint) {
447 #define indirect_detag(H) indirect_detag(aTHX_ (H))
449 #if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
456 #endif /* I_THREADSAFE */
458 h = INT2PTR(indirect_hint_t *, SvIVX(hint));
460 h = ptable_fetch(MY_CXT.tbl, h);
461 #endif /* I_THREADSAFE */
463 #if I_WORKAROUND_REQUIRE_PROPAGATION
464 if (indirect_require_tag() != h->require_tag)
465 return MY_CXT.global_code;
466 #endif /* I_WORKAROUND_REQUIRE_PROPAGATION */
468 return I_HINT_CODE(h);
471 static VOL U32 indirect_hash = 0;
473 static SV *indirect_hint(pTHX) {
474 #define indirect_hint() indirect_hint(aTHX)
480 #if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
485 #ifdef cop_hints_fetch_pvn
486 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__,
488 #elif I_HAS_PERL(5, 9, 5)
489 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
491 __PACKAGE__, __PACKAGE_LEN__,
496 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
502 if (hint && SvIOK(hint))
503 return indirect_detag(hint);
506 return MY_CXT.global_code;
510 /* ... op -> source position ............................................... */
512 static void indirect_map_store(pTHX_ const OP *o, STRLEN pos, SV *sv, line_t line) {
513 #define indirect_map_store(O, P, N, L) indirect_map_store(aTHX_ (O), (P), (N), (L))
514 indirect_op_info_t *oi;
519 /* No need to check for MY_CXT.map != NULL because this code path is always
520 * guarded by indirect_hint(). */
522 if (!(oi = ptable_fetch(MY_CXT.map, o))) {
523 Newx(oi, 1, indirect_op_info_t);
524 ptable_store(MY_CXT.map, o, oi);
530 s = SvPV_const(sv, len);
536 if (len > oi->size) {
538 Newx(oi->buf, len, char);
541 Copy(s, oi->buf, len, char);
548 static const indirect_op_info_t *indirect_map_fetch(pTHX_ const OP *o) {
549 #define indirect_map_fetch(O) indirect_map_fetch(aTHX_ (O))
552 /* No need to check for MY_CXT.map != NULL because this code path is always
553 * guarded by indirect_hint(). */
555 return ptable_fetch(MY_CXT.map, o);
558 static void indirect_map_delete(pTHX_ const OP *o) {
559 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
563 ptable_delete(MY_CXT.map, o);
566 /* --- Check functions ----------------------------------------------------- */
568 static int indirect_find(pTHX_ SV *name_sv, const char *line_bufptr, STRLEN *name_pos) {
569 #define indirect_find(NSV, LBP, NP) indirect_find(aTHX_ (NSV), (LBP), (NP))
570 STRLEN name_len, line_len;
571 const char *name, *name_end;
572 const char *line, *line_end;
575 line = SvPV_const(PL_linestr, line_len);
576 line_end = line + line_len;
578 name = SvPV_const(name_sv, name_len);
579 if (name_len >= 1 && *name == '$') {
582 while (line_bufptr < line_end && *line_bufptr != '$')
584 if (line_bufptr >= line_end)
587 name_end = name + name_len;
591 p = ninstr(p, line_end, name, name_end);
594 if (!isALNUM(p[name_len]))
596 /* p points to a word that has name as prefix, skip the rest of the word */
602 *name_pos = p - line;
607 /* ... ck_const ............................................................ */
609 static OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
611 static OP *indirect_ck_const(pTHX_ OP *o) {
612 o = indirect_old_ck_const(aTHX_ o);
614 if (indirect_hint()) {
617 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV)) {
620 if (indirect_find(sv, PL_oldbufptr, &pos)) {
623 /* If the constant is equal to the current package name, try to look for
624 * a "__PACKAGE__" coming before what we got. We only need to check this
625 * when we already had a match because __PACKAGE__ can only appear in
626 * direct method calls ("new __PACKAGE__" is a syntax error). */
629 && len == (STRLEN) HvNAMELEN_get(PL_curstash)
630 && memcmp(SvPVX(sv), HvNAME_get(PL_curstash), len) == 0) {
632 SV *pkg = sv_newmortal();
633 sv_setpvn(pkg, "__PACKAGE__", sizeof("__PACKAGE__")-1);
635 if (indirect_find(pkg, PL_oldbufptr, &pos_pkg) && pos_pkg < pos) {
641 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
647 indirect_map_delete(o);
651 /* ... ck_rv2sv ............................................................ */
653 static OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
655 static OP *indirect_ck_rv2sv(pTHX_ OP *o) {
656 if (indirect_hint()) {
657 OP *op = cUNOPo->op_first;
659 const char *name = NULL;
661 OPCODE type = (OPCODE) op->op_type;
666 GV *gv = cGVOPx_gv(op);
672 if ((PL_opargs[type] & OA_CLASS_MASK) == OA_SVOP) {
673 SV *nsv = cSVOPx_sv(op);
674 if (SvPOK(nsv) && (SvTYPE(nsv) >= SVt_PV))
675 name = SvPV_const(nsv, len);
681 sv = sv_2mortal(newSVpvn("$", 1));
682 sv_catpvn_nomg(sv, name, len);
683 if (!indirect_find(sv, PL_oldbufptr, &pos)) {
684 /* If it failed, retry without the current stash */
685 const char *stash = HvNAME_get(PL_curstash);
686 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
688 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
689 || name[stashlen] != ':' || name[stashlen+1] != ':') {
690 /* Failed again ? Try to remove main */
693 if ((len < stashlen + 2) || strnNE(name, stash, stashlen)
694 || name[stashlen] != ':' || name[stashlen+1] != ':')
698 sv_setpvn(sv, "$", 1);
700 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
701 if (!indirect_find(sv, PL_oldbufptr, &pos))
705 o = indirect_old_ck_rv2sv(aTHX_ o);
707 indirect_map_store(o, pos, sv, CopLINE(&PL_compiling));
712 o = indirect_old_ck_rv2sv(aTHX_ o);
714 indirect_map_delete(o);
718 /* ... ck_padany ........................................................... */
720 static OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
722 static OP *indirect_ck_padany(pTHX_ OP *o) {
723 o = indirect_old_ck_padany(aTHX_ o);
725 if (indirect_hint()) {
727 const char *s = PL_oldbufptr, *t = PL_bufptr - 1;
729 while (s < t && isSPACE(*s)) ++s;
730 if (*s == '$' && ++s <= t) {
731 while (s < t && isSPACE(*s)) ++s;
732 while (s < t && isSPACE(*t)) --t;
733 sv = sv_2mortal(newSVpvn("$", 1));
734 sv_catpvn_nomg(sv, s, t - s + 1);
735 indirect_map_store(o, s - SvPVX_const(PL_linestr),
736 sv, CopLINE(&PL_compiling));
741 indirect_map_delete(o);
745 /* ... ck_scope ............................................................ */
747 static OP *(*indirect_old_ck_scope) (pTHX_ OP *) = 0;
748 static OP *(*indirect_old_ck_lineseq)(pTHX_ OP *) = 0;
750 static OP *indirect_ck_scope(pTHX_ OP *o) {
751 OP *(*old_ck)(pTHX_ OP *) = 0;
753 switch (o->op_type) {
754 case OP_SCOPE: old_ck = indirect_old_ck_scope; break;
755 case OP_LINESEQ: old_ck = indirect_old_ck_lineseq; break;
759 if (indirect_hint()) {
760 indirect_map_store(o, PL_oldbufptr - SvPVX_const(PL_linestr),
761 NULL, CopLINE(&PL_compiling));
765 indirect_map_delete(o);
769 /* We don't need to clean the map entries for leave ops because they can only
770 * be created by mutating from a lineseq. */
772 /* ... ck_method ........................................................... */
774 static OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
776 static OP *indirect_ck_method(pTHX_ OP *o) {
777 if (indirect_hint()) {
778 OP *op = cUNOPo->op_first;
780 /* Indirect method call is only possible when the method is a bareword, so
781 * don't trip up on $obj->$meth. */
782 if (op && op->op_type == OP_CONST) {
783 const indirect_op_info_t *oi = indirect_map_fetch(op);
791 sv = sv_2mortal(newSVpvn(oi->buf, oi->len));
793 /* Keep the old line so that we really point to the first line of the
797 o = indirect_old_ck_method(aTHX_ o);
798 /* o may now be a method_named */
800 indirect_map_store(o, pos, sv, line);
806 o = indirect_old_ck_method(aTHX_ o);
808 indirect_map_delete(o);
812 /* ... ck_method_named ..................................................... */
814 /* "use foo/no foo" compiles its call to import/unimport directly to a
815 * method_named op. */
817 static OP *(*indirect_old_ck_method_named)(pTHX_ OP *) = 0;
819 static OP *indirect_ck_method_named(pTHX_ OP *o) {
820 if (indirect_hint()) {
826 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
828 sv = sv_mortalcopy(sv);
830 if (!indirect_find(sv, PL_oldbufptr, &pos))
832 line = CopLINE(&PL_compiling);
834 o = indirect_old_ck_method_named(aTHX_ o);
836 indirect_map_store(o, pos, sv, line);
841 o = indirect_old_ck_method_named(aTHX_ o);
843 indirect_map_delete(o);
847 /* ... ck_entersub ......................................................... */
849 static OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
851 static OP *indirect_ck_entersub(pTHX_ OP *o) {
852 SV *code = indirect_hint();
854 o = indirect_old_ck_entersub(aTHX_ o);
857 const indirect_op_info_t *moi, *ooi;
863 lop = (LISTOP *) oop;
864 if (!(lop->op_flags & OPf_KIDS))
867 } while (oop->op_type != OP_PUSHMARK);
868 oop = OpSIBLING(oop);
874 switch (oop->op_type) {
885 if (mop->op_type == OP_METHOD)
886 mop = cUNOPx(mop)->op_first;
887 else if (mop->op_type != OP_METHOD_NAMED)
890 moi = indirect_map_fetch(mop);
894 ooi = indirect_map_fetch(oop);
898 /* When positions are identical, the method and the object must have the
899 * same name. But it also means that it is an indirect call, as "foo->foo"
900 * results in different positions. */
901 if ( moi->line < ooi->line
902 || (moi->line == ooi->line && moi->pos <= ooi->pos)) {
910 file = sv_2mortal(newSVpv(CopFILE(&PL_compiling), 0));
912 file = sv_mortalcopy(CopFILESV(&PL_compiling));
917 mPUSHp(ooi->buf, ooi->len);
918 mPUSHp(moi->buf, moi->len);
923 call_sv(code, G_VOID);
936 /* --- Global setup/teardown ------------------------------------------------ */
938 static VOL U32 indirect_initialized = 0;
940 static void indirect_global_teardown(pTHX_ void *root) {
941 if (!indirect_initialized)
949 indirect_ck_restore(OP_CONST, &indirect_old_ck_const);
950 indirect_ck_restore(OP_RV2SV, &indirect_old_ck_rv2sv);
951 indirect_ck_restore(OP_PADANY, &indirect_old_ck_padany);
952 indirect_ck_restore(OP_SCOPE, &indirect_old_ck_scope);
953 indirect_ck_restore(OP_LINESEQ, &indirect_old_ck_lineseq);
955 indirect_ck_restore(OP_METHOD, &indirect_old_ck_method);
956 indirect_ck_restore(OP_METHOD_NAMED, &indirect_old_ck_method_named);
957 indirect_ck_restore(OP_ENTERSUB, &indirect_old_ck_entersub);
959 indirect_initialized = 0;
964 static void indirect_global_setup(pTHX) {
965 #define indirect_global_setup() indirect_global_setup(aTHX)
968 if (indirect_initialized)
971 PERL_HASH(indirect_hash, __PACKAGE__, __PACKAGE_LEN__);
973 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
974 newCONSTSUB(stash, "I_THREADSAFE", newSVuv(I_THREADSAFE));
975 newCONSTSUB(stash, "I_FORKSAFE", newSVuv(I_FORKSAFE));
977 indirect_ck_replace(OP_CONST, indirect_ck_const, &indirect_old_ck_const);
978 indirect_ck_replace(OP_RV2SV, indirect_ck_rv2sv, &indirect_old_ck_rv2sv);
979 indirect_ck_replace(OP_PADANY, indirect_ck_padany, &indirect_old_ck_padany);
980 indirect_ck_replace(OP_SCOPE, indirect_ck_scope, &indirect_old_ck_scope);
981 indirect_ck_replace(OP_LINESEQ, indirect_ck_scope, &indirect_old_ck_lineseq);
983 indirect_ck_replace(OP_METHOD, indirect_ck_method,
984 &indirect_old_ck_method);
985 indirect_ck_replace(OP_METHOD_NAMED, indirect_ck_method_named,
986 &indirect_old_ck_method_named);
987 indirect_ck_replace(OP_ENTERSUB, indirect_ck_entersub,
988 &indirect_old_ck_entersub);
991 call_atexit(indirect_global_teardown, aTHX);
993 call_atexit(indirect_global_teardown, NULL);
996 indirect_initialized = 1;
1001 /* --- Interpreter setup/teardown ------------------------------------------ */
1003 static void indirect_local_teardown(pTHX_ void *param) {
1006 ptable_free(MY_CXT.map);
1010 ptable_hints_free(MY_CXT.tbl);
1017 static void indirect_local_setup(pTHX) {
1018 #define indirect_local_setup() indirect_local_setup(aTHX)
1022 MY_CXT.tbl = ptable_new();
1023 MY_CXT.owner = aTHX;
1026 MY_CXT.map = ptable_new();
1027 MY_CXT.global_code = NULL;
1029 call_atexit(indirect_local_teardown, NULL);
1034 /* --- XS ------------------------------------------------------------------ */
1036 MODULE = indirect PACKAGE = indirect
1042 indirect_global_setup();
1043 indirect_local_setup();
1053 SV *global_code_dup;
1057 indirect_ptable_clone_ud ud;
1060 indirect_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1061 ptable_walk(MY_CXT.tbl, indirect_ptable_clone, &ud);
1062 global_code_dup = indirect_dup_inc(MY_CXT.global_code, &ud);
1063 indirect_ptable_clone_ud_deinit(ud);
1067 MY_CXT.map = ptable_new();
1069 MY_CXT.owner = aTHX;
1070 MY_CXT.global_code = global_code_dup;
1072 gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1078 if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1080 sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &indirect_endav_vtbl, NULL, 0);
1085 _THREAD_CLEANUP(...)
1088 indirect_thread_cleanup(aTHX_ NULL);
1097 RETVAL = indirect_tag(value);
1107 else if (SvROK(code))
1111 SvREFCNT_dec(MY_CXT.global_code);
1112 MY_CXT.global_code = SvREFCNT_inc(code);