+/* ... Thread-safe hints ................................................... */
+
+/* If any of those is true, we need to store the hint in a global table. */
+
+#if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
+
+typedef struct {
+ SV *code;
+#if LT_WORKAROUND_REQUIRE_PROPAGATION
+ UV requires;
+#endif
+} lt_hint_t;
+
+#define PTABLE_NAME ptable_hints
+#define PTABLE_VAL_FREE(V) { lt_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 /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */
+
+/* ... Global data ......................................................... */
+
+#define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
+
+typedef struct {
+#if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
+ ptable *tbl; /* It really is a ptable_hints */
+#endif
+#if LT_THREADSAFE
+ tTHX owner;
+#endif
+ OP * (*pp_padsv_saved)(pTHX);
+} my_cxt_t;
+
+START_MY_CXT
+
+/* ... Cloning global data ................................................. */
+
+#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, ¶m);
+
+ 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;
+ lt_hint_t *h2 = PerlMemShared_malloc(sizeof *h2);
+
+ *h2 = *h1;
+
+ if (ud->owner != aTHX)
+ h2->code = lt_clone(h1->code, ud->owner);
+
+ ptable_hints_store(ud->tbl, ent->key, h2);
+ SvREFCNT_inc(h2->code);
+}
+
+STATIC void lt_thread_cleanup(pTHX_ void *);
+
+STATIC void lt_thread_cleanup(pTHX_ void *ud) {
+ int *level = ud;
+
+ if (*level) {
+ *level = 0;
+ LEAVE;
+ SAVEDESTRUCTOR_X(lt_thread_cleanup, level);
+ ENTER;
+ } else {
+ dMY_CXT;
+ PerlMemShared_free(level);
+ ptable_hints_free(MY_CXT.tbl);
+ }
+}
+
+#endif /* LT_THREADSAFE */
+
+/* ... Hint tags ........................................................... */
+
+#if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION
+
+STATIC SV *lt_tag(pTHX_ SV *value) {
+#define lt_tag(V) lt_tag(aTHX_ (V))
+ lt_hint_t *h;
+ dMY_CXT;
+
+ value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL;
+
+ h = PerlMemShared_malloc(sizeof *h);
+ h->code = SvREFCNT_inc(value);
+
+#if LT_WORKAROUND_REQUIRE_PROPAGATION
+ {
+ const PERL_SI *si;
+ UV 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 *lt_detag(pTHX_ const SV *hint) {
+#define lt_detag(H) lt_detag(aTHX_ (H))
+ lt_hint_t *h;
+ dMY_CXT;
+
+ if (!(hint && SvOK(hint) && SvIOK(hint)))
+ return NULL;
+
+ h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint)));
+
+#if LT_WORKAROUND_REQUIRE_PROPAGATION
+ {
+ const PERL_SI *si;
+ UV 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 *lt_tag(pTHX_ SV *value) {
+#define lt_tag(V) lt_tag(aTHX_ (V))
+ UV tag = 0;
+
+ if (SvOK(value) && SvROK(value)) {
+ value = SvRV(value);
+ SvREFCNT_inc(value);
+ tag = PTR2UV(value);
+ }
+
+ return newSVuv(tag);
+}
+
+#define lt_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL)
+
+#endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */