+#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);