X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=Types.xs;h=6a159bcdc2775cc517d8c11275237a197a934a0f;hb=4a583842927b16471bea479730a14b0cfc82eba5;hp=bcf5fa15c9ac6937ac4e74347ff1a0b1b64631e3;hpb=6e9765def6efaf02330bf276fc8006e13769d035;p=perl%2Fmodules%2FLexical-Types.git diff --git a/Types.xs b/Types.xs index bcf5fa1..6a159bc 100644 --- a/Types.xs +++ b/Types.xs @@ -23,6 +23,10 @@ # endif #endif +#ifndef LT_WORKAROUND_REQUIRE_PROPAGATION +# define LT_WORKAROUND_REQUIRE_PROPAGATION !LT_HAS_PERL(5, 10, 1) +#endif + #ifndef HvNAME_get # define HvNAME_get(H) HvNAME(H) #endif @@ -59,16 +63,43 @@ # endif #else # define LT_THREADSAFE 0 +# undef dMY_CXT +# define dMY_CXT dNOOP +# undef MY_CXT +# define MY_CXT lt_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 +# undef pMY_CXT +# define pMY_CXT +# undef pMY_CXT_ +# define pMY_CXT_ +# undef aMY_CXT +# define aMY_CXT +# undef aMY_CXT_ +# define aMY_CXT_ #endif /* --- Helpers ------------------------------------------------------------- */ /* ... Thread-safe hints ................................................... */ -#if LT_THREADSAFE +/* 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) if ((V) && !SvIS_FREED((SV *) (V))) SvREFCNT_dec(V) +#define PTABLE_VAL_FREE(V) { lt_hint_t *h = (V); SvREFCNT_dec(h->code); PerlMemShared_free(h); } #define pPTBL pTHX #define pPTBL_ pTHX_ @@ -80,34 +111,63 @@ #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 { - ptable *tbl; +#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 -STATIC void lt_ptable_hints_clone(pTHX_ ptable_ent *ent, void *ud_) { - my_cxt_t *ud = ud_; - SV *val = ent->val; - - if (ud->owner != aTHX) { - 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; - val = sv_dup(val, ¶m); - if (stashes) { - av_undef(stashes); - SvREFCNT_dec(stashes); - } +/* ... 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); } - ptable_hints_store(ud->tbl, ent->key, val); - SvREFCNT_inc(val); + 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 *); @@ -127,35 +187,80 @@ STATIC void lt_thread_cleanup(pTHX_ void *ud) { } } +#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, value); - SvREFCNT_inc(value); + 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)) - void *tag; - SV *value; + lt_hint_t *h; + dMY_CXT; + + if (!(hint && SvOK(hint) && SvIOK(hint))) + return NULL; - if (!hint || !SvOK(hint) || !SvIOK(hint)) - croak("Wrong hint"); + h = ptable_fetch(MY_CXT.tbl, INT2PTR(void *, SvUVX(hint))); - tag = INT2PTR(void *, SvIVX(hint)); +#if LT_WORKAROUND_REQUIRE_PROPAGATION { - dMY_CXT; - value = ptable_fetch(MY_CXT.tbl, tag); + 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 value; + return h->code; } #else @@ -173,9 +278,9 @@ STATIC SV *lt_tag(pTHX_ SV *value) { return newSVuv(tag); } -#define lt_detag(H) INT2PTR(SV *, SvUVX(H)) +#define lt_detag(H) (((H) && SvOK(H)) ? INT2PTR(SV *, SvUVX(H)) : NULL) -#endif /* LT_THREADSAFE */ +#endif /* LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION */ STATIC U32 lt_hash = 0; @@ -194,7 +299,7 @@ STATIC SV *lt_hint(pTHX) { return 0; hint = *val; #endif - return (hint && SvOK(hint)) ? hint : NULL; + return lt_detag(hint); } /* ... op => info map ...................................................... */ @@ -214,14 +319,19 @@ STATIC perl_mutex lt_op_map_mutex; #endif typedef struct { +#ifdef MULTIPLICITY + STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len; + char *buf; +#else /* MULTIPLICITY */ SV *orig_pkg; SV *type_pkg; SV *type_meth; +#endif /* !MULTIPLICITY */ OP *(*pp_padsv)(pTHX); } lt_op_info; -STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) { -#define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aPTBLMS_ (O), (OP), (TP), (TM), (PP)) +STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*pp_padsv)(pTHX)) { +#define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP)) lt_op_info *oi; #ifdef USE_ITHREADS @@ -231,11 +341,48 @@ STATIC void lt_map_store(pPTBLMS_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *t if (!(oi = ptable_fetch(lt_op_map, o))) { oi = PerlMemShared_malloc(sizeof *oi); ptable_map_store(lt_op_map, o, oi); +#ifdef MULTIPLICITY + oi->buf = NULL; + oi->buf_size = 0; +#else /* MULTIPLICITY */ + } else { + SvREFCNT_dec(oi->orig_pkg); + SvREFCNT_dec(oi->type_pkg); + SvREFCNT_dec(oi->type_meth); +#endif /* !MULTIPLICITY */ } +#ifdef MULTIPLICITY + { + STRLEN op_len = SvCUR(orig_pkg); + STRLEN tp_len = SvCUR(type_pkg); + STRLEN tm_len = SvCUR(type_meth); + STRLEN new_buf_size = op_len + tp_len + tm_len; + char *buf; + if (new_buf_size > oi->buf_size) { + PerlMemShared_free(oi->buf); + oi->buf = PerlMemShared_malloc(new_buf_size); + oi->buf_size = new_buf_size; + } + buf = oi->buf; + Copy(SvPVX(orig_pkg), buf, op_len, char); + buf += op_len; + Copy(SvPVX(type_pkg), buf, tp_len, char); + buf += tp_len; + Copy(SvPVX(type_meth), buf, tm_len, char); + oi->orig_pkg_len = op_len; + oi->type_pkg_len = tp_len; + oi->type_meth_len = tm_len; + SvREFCNT_dec(orig_pkg); + SvREFCNT_dec(type_pkg); + SvREFCNT_dec(type_meth); + } +#else /* MULTIPLICITY */ oi->orig_pkg = orig_pkg; oi->type_pkg = type_pkg; oi->type_meth = type_meth; +#endif /* !MULTIPLICITY */ + oi->pp_padsv = pp_padsv; #ifdef USE_ITHREADS @@ -288,20 +435,40 @@ STATIC OP *lt_pp_padsv(pTHX) { SV *sv = PAD_SVl(targ); if (sv) { + SV *orig_pkg, *type_pkg, *type_meth; int items; dSP; ENTER; SAVETMPS; +#ifdef MULTIPLICITY + { + STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len; + char *buf = oi.buf; + orig_pkg = sv_2mortal(newSVpvn(buf, op_len)); + SvREADONLY_on(orig_pkg); + buf += op_len; + type_pkg = sv_2mortal(newSVpvn(buf, tp_len)); + SvREADONLY_on(type_pkg); + buf += tp_len; + type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len)); + SvREADONLY_on(type_meth); + } +#else /* MULTIPLICITY */ + orig_pkg = oi.orig_pkg; + type_pkg = oi.type_pkg; + type_meth = oi.type_meth; +#endif /* !MULTIPLICITY */ + PUSHMARK(SP); EXTEND(SP, 3); - PUSHs(oi.type_pkg); + PUSHs(type_pkg); PUSHs(sv); - PUSHs(oi.orig_pkg); + PUSHs(orig_pkg); PUTBACK; - items = call_sv(oi.type_meth, G_ARRAY | G_METHOD); + items = call_sv(type_meth, G_ARRAY | G_METHOD); SPAGAIN; switch (items) { @@ -325,25 +492,27 @@ STATIC OP *lt_pp_padsv(pTHX) { return CALL_FPTR(PL_ppaddr[OP_PADSV])(aTHX); } -STATIC OP *(*lt_pp_padsv_saved)(pTHX) = 0; - -STATIC void lt_pp_padsv_save(void) { - if (lt_pp_padsv_saved) +STATIC void lt_pp_padsv_save(pMY_CXT) { +#define lt_pp_padsv_save() lt_pp_padsv_save(aMY_CXT) + if (MY_CXT.pp_padsv_saved) return; - lt_pp_padsv_saved = PL_ppaddr[OP_PADSV]; - PL_ppaddr[OP_PADSV] = lt_pp_padsv; + MY_CXT.pp_padsv_saved = PL_ppaddr[OP_PADSV]; + PL_ppaddr[OP_PADSV] = lt_pp_padsv; } -STATIC void lt_pp_padsv_restore(OP *o) { - if (!lt_pp_padsv_saved) +STATIC void lt_pp_padsv_restore(pMY_CXT_ OP *o) { +#define lt_pp_padsv_restore(O) lt_pp_padsv_restore(aMY_CXT_ (O)) + OP *(*saved)(pTHX) = MY_CXT.pp_padsv_saved; + + if (!saved) return; if (o->op_ppaddr == lt_pp_padsv) - o->op_ppaddr = lt_pp_padsv_saved; + o->op_ppaddr = saved; - PL_ppaddr[OP_PADSV] = lt_pp_padsv_saved; - lt_pp_padsv_saved = 0; + PL_ppaddr[OP_PADSV] = saved; + MY_CXT.pp_padsv_saved = 0; } /* ... Our ck_pad{any,sv} .................................................. */ @@ -362,63 +531,61 @@ STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0; STATIC OP *lt_ck_padany(pTHX_ OP *o) { HV *stash; - SV *hint; + SV *code; + dMY_CXT; lt_pp_padsv_restore(o); o = CALL_FPTR(lt_old_ck_padany)(aTHX_ o); stash = PL_in_my_stash; - if (stash && (hint = lt_hint())) { + if (stash && (code = lt_hint())) { SV *orig_pkg = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash)); SV *orig_meth = lt_default_meth; SV *type_pkg = NULL; SV *type_meth = NULL; - SV *code = lt_detag(hint); - - SvREADONLY_on(orig_pkg); - - if (code) { - int items; - dSP; + int items; - ENTER; - SAVETMPS; - - PUSHMARK(SP); - EXTEND(SP, 2); - PUSHs(orig_pkg); - PUSHs(orig_meth); - PUTBACK; + dSP; - items = call_sv(code, G_ARRAY); + SvREADONLY_on(orig_pkg); - SPAGAIN; - if (items > 2) - croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items); - if (items == 0) { - SvREFCNT_dec(orig_pkg); - goto skip; - } else { - SV *rsv; - if (items > 1) { - rsv = POPs; - if (SvOK(rsv)) { - type_meth = newSVsv(rsv); - SvREADONLY_on(type_meth); - } - } + ENTER; + SAVETMPS; + + PUSHMARK(SP); + EXTEND(SP, 2); + PUSHs(orig_pkg); + PUSHs(orig_meth); + PUTBACK; + + items = call_sv(code, G_ARRAY); + + SPAGAIN; + if (items > 2) + croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items); + if (items == 0) { + SvREFCNT_dec(orig_pkg); + goto skip; + } else { + SV *rsv; + if (items > 1) { rsv = POPs; if (SvOK(rsv)) { - type_pkg = newSVsv(rsv); - SvREADONLY_on(type_pkg); + type_meth = newSVsv(rsv); + SvREADONLY_on(type_meth); } } - PUTBACK; - - FREETMPS; - LEAVE; + rsv = POPs; + if (SvOK(rsv)) { + type_pkg = newSVsv(rsv); + SvREADONLY_on(type_pkg); + } } + PUTBACK; + + FREETMPS; + LEAVE; if (!type_pkg) { type_pkg = orig_pkg; @@ -432,7 +599,7 @@ STATIC OP *lt_ck_padany(pTHX_ OP *o) { lt_pp_padsv_save(); - lt_map_store(o, orig_pkg, type_pkg, type_meth, lt_pp_padsv_saved); + lt_map_store(o, orig_pkg, type_pkg, type_meth, MY_CXT.pp_padsv_saved); } else { skip: lt_map_delete(o); @@ -444,6 +611,8 @@ skip: STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0; STATIC OP *lt_ck_padsv(pTHX_ OP *o) { + dMY_CXT; + lt_pp_padsv_restore(o); lt_map_delete(o); @@ -463,11 +632,15 @@ BOOT: { if (!lt_initialized++) { HV *stash; -#if LT_THREADSAFE + MY_CXT_INIT; - MY_CXT.tbl = ptable_new(); - MY_CXT.owner = aTHX; +#if LT_THREADSAFE || LT_WORKAROUND_REQUIRE_PROPAGATION + MY_CXT.tbl = ptable_new(); +#endif +#if LT_THREADSAFE + MY_CXT.owner = aTHX; #endif + MY_CXT.pp_padsv_saved = 0; lt_op_map = ptable_new(); #ifdef USE_ITHREADS @@ -507,8 +680,9 @@ CODE: } { MY_CXT_CLONE; - MY_CXT.tbl = t; - MY_CXT.owner = aTHX; + MY_CXT.tbl = t; + MY_CXT.owner = aTHX; + MY_CXT.pp_padsv_saved = 0; } { level = PerlMemShared_malloc(sizeof *level);