+#ifndef sv_catpvn_nomg
+# define sv_catpvn_nomg sv_catpvn
+#endif
+
+#ifndef mPUSHp
+# define mPUSHp(P, L) PUSHs(sv_2mortal(newSVpvn((P), (L))))
+#endif
+
+#ifndef mPUSHu
+# define mPUSHu(U) PUSHs(sv_2mortal(newSVuv(U)))
+#endif
+
+#ifndef HvNAME_get
+# define HvNAME_get(H) HvNAME(H)
+#endif
+
+#ifndef HvNAMELEN_get
+# define HvNAMELEN_get(H) strlen(HvNAME_get(H))
+#endif
+
+#define I_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
+
+#if I_HAS_PERL(5, 10, 0) || defined(PL_parser)
+# ifndef PL_lex_inwhat
+# define PL_lex_inwhat PL_parser->lex_inwhat
+# endif
+# ifndef PL_linestr
+# define PL_linestr PL_parser->linestr
+# endif
+# ifndef PL_bufptr
+# define PL_bufptr PL_parser->bufptr
+# endif
+# ifndef PL_oldbufptr
+# define PL_oldbufptr PL_parser->oldbufptr
+# endif
+#else
+# ifndef PL_lex_inwhat
+# define PL_lex_inwhat PL_Ilex_inwhat
+# endif
+# ifndef PL_linestr
+# define PL_linestr PL_Ilinestr
+# endif
+# ifndef PL_bufptr
+# define PL_bufptr PL_Ibufptr
+# endif
+# ifndef PL_oldbufptr
+# define PL_oldbufptr PL_Ioldbufptr
+# endif
+#endif
+
+#ifndef I_WORKAROUND_REQUIRE_PROPAGATION
+# define I_WORKAROUND_REQUIRE_PROPAGATION !I_HAS_PERL(5, 10, 1)
+#endif
+
+/* ... Thread safety and multiplicity ...................................... */
+
+#ifndef I_MULTIPLICITY
+# if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
+# define I_MULTIPLICITY 1
+# else
+# define I_MULTIPLICITY 0
+# endif
+#endif
+#if I_MULTIPLICITY && !defined(tTHX)
+# define tTHX PerlInterpreter*
+#endif
+
+#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))
+# define I_THREADSAFE 1
+# ifndef MY_CXT_CLONE
+# define MY_CXT_CLONE \
+ dMY_CXT_SV; \
+ my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
+ Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
+ sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
+# endif
+#else
+# define I_THREADSAFE 0
+# undef dMY_CXT
+# define dMY_CXT dNOOP
+# undef MY_CXT
+# define MY_CXT indirect_globaldata
+# undef START_MY_CXT
+# define START_MY_CXT STATIC my_cxt_t MY_CXT;
+# undef MY_CXT_INIT
+# define MY_CXT_INIT NOOP
+# undef MY_CXT_CLONE
+# define MY_CXT_CLONE NOOP
+#endif
+
+/* --- Helpers ------------------------------------------------------------- */
+
+/* ... Thread-safe hints ................................................... */
+
+/* If any of those are true, we need to store the hint in a global table. */
+
+#if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
+
+typedef struct {
+ SV *code;
+#if I_WORKAROUND_REQUIRE_PROPAGATION
+ I32 requires;
+#endif
+} indirect_hint_t;
+
+#define PTABLE_NAME ptable_hints
+
+#define PTABLE_VAL_FREE(V) \
+ { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
+
+#define pPTBL pTHX
+#define pPTBL_ pTHX_
+#define aPTBL aTHX
+#define aPTBL_ aTHX_
+
+#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))
+
+#endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */
+
+/* Define the op->str ptable here because we need to be able to clean it during
+ * thread cleanup. */
+
+typedef struct {
+ const char *pos;
+ char *buf;
+ STRLEN len, size;
+ line_t line;
+} indirect_op_info_t;
+
+#define PTABLE_NAME ptable
+#define PTABLE_VAL_FREE(V) if (V) { Safefree(((indirect_op_info_t *) (V))->buf); Safefree(V); }
+
+#define pPTBL pTHX
+#define pPTBL_ pTHX_
+#define aPTBL aTHX
+#define aPTBL_ aTHX_
+
+#include "ptable.h"
+
+#define ptable_store(T, K, V) ptable_store(aTHX_ (T), (K), (V))
+#define ptable_clear(T) ptable_clear(aTHX_ (T))
+#define ptable_free(T) ptable_free(aTHX_ (T))
+
+#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
+
+typedef struct {
+#if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
+ ptable *tbl; /* It really is a ptable_hints */
+#endif
+ ptable *map;
+ const char *linestr;
+#if I_THREADSAFE
+ tTHX owner;
+#endif
+} my_cxt_t;
+
+START_MY_CXT
+
+#if I_THREADSAFE
+
+STATIC SV *indirect_clone(pTHX_ SV *sv, tTHX owner) {
+#define indirect_clone(S, O) indirect_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, ¶m);
+
+ if (stashes) {
+ av_undef(stashes);
+ SvREFCNT_dec(stashes);
+ }
+
+ return SvREFCNT_inc(dupsv);
+}
+
+STATIC void indirect_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
+ my_cxt_t *ud = ud_;
+ indirect_hint_t *h1 = ent->val;
+ indirect_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
+
+ *h2 = *h1;
+
+ if (ud->owner != aTHX)
+ h2->code = indirect_clone(h1->code, ud->owner);
+
+ ptable_hints_store(ud->tbl, ent->key, h2);
+ SvREFCNT_inc(h2->code);
+}
+
+STATIC void indirect_thread_cleanup(pTHX_ void *);
+
+STATIC void indirect_thread_cleanup(pTHX_ void *ud) {
+ int *level = ud;
+
+ if (*level) {
+ *level = 0;
+ LEAVE;
+ SAVEDESTRUCTOR_X(indirect_thread_cleanup, level);
+ ENTER;
+ } else {
+ dMY_CXT;
+ PerlMemShared_free(level);
+ ptable_free(MY_CXT.map);
+ ptable_hints_free(MY_CXT.tbl);
+ }
+}
+
+#endif /* I_THREADSAFE */
+
+#if I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION
+
+STATIC SV *indirect_tag(pTHX_ SV *value) {
+#define indirect_tag(V) indirect_tag(aTHX_ (V))
+ indirect_hint_t *h;
+ dMY_CXT;
+
+ value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
+
+ h = PerlMemShared_malloc(sizeof *h);
+ h->code = SvREFCNT_inc(value);
+
+#if I_WORKAROUND_REQUIRE_PROPAGATION
+ {
+ const PERL_SI *si;
+ I32 requires = 0;
+
+ for (si = PL_curstackinfo; si; si = si->si_prev) {
+ I32 cxix;
+
+ for (cxix = si->si_cxix; cxix >= 0; --cxix) {
+ const PERL_CONTEXT *cx = si->si_cxstack + cxix;
+
+ if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE)
+ ++requires;
+ }
+ }
+
+ h->requires = requires;
+ }
+#endif
+
+ /* 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(MY_CXT.tbl, value, h);
+
+ return newSVuv(PTR2UV(value));
+}
+
+STATIC SV *indirect_detag(pTHX_ const SV *hint) {
+#define indirect_detag(H) indirect_detag(aTHX_ (H))
+ indirect_hint_t *h;
+ dMY_CXT;
+
+ if (!(hint && SvOK(hint) && SvIOK(hint)))
+ return NULL;
+
+ h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
+
+#if I_WORKAROUND_REQUIRE_PROPAGATION
+ {
+ const PERL_SI *si;
+ I32 requires = 0;
+
+ for (si = PL_curstackinfo; si; si = si->si_prev) {
+ I32 cxix;
+
+ for (cxix = si->si_cxix; cxix >= 0; --cxix) {
+ const PERL_CONTEXT *cx = si->si_cxstack + cxix;
+
+ if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE
+ && ++requires > h->requires)
+ return NULL;
+ }
+ }
+ }
+#endif
+
+ return h->code;
+}
+
+#else
+
+STATIC SV *indirect_tag(pTHX_ SV *value) {
+#define indirect_tag(V) indirect_tag(aTHX_ (V))
+ UV tag = 0;
+
+ if (SvOK(value) && SvROK(value)) {
+ value = SvRV(value);
+ SvREFCNT_inc(value);
+ tag = PTR2UV(value);
+ }
+
+ return newSVuv(tag);
+}
+
+#define indirect_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
+
+#endif /* I_THREADSAFE || I_WORKAROUND_REQUIRE_PROPAGATION */