+#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
+
+#if I_WORKAROUND_REQUIRE_PROPAGATION
+# define PTABLE_VAL_FREE(V) \
+ { indirect_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); }
+#else
+# define PTABLE_VAL_FREE(V) SvREFCNT_dec(V)
+#endif
+
+#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. */
+
+#define PTABLE_NAME ptable
+#define PTABLE_VAL_FREE(V) SvREFCNT_dec(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 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) {
+ 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, ¶m);
+ if (stashes) {
+ av_undef(stashes);
+ SvREFCNT_dec(stashes);
+ }
+ }
+
+ 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 */