X-Git-Url: http://git.vpit.fr/?a=blobdiff_plain;f=Types.xs;h=ff9e398f6242d625c40706a095161c8b37cb354e;hb=158a53a77c240a1fed59318ecacb5bb20eb11f1d;hp=29e8d87ff13b163f9f318f6afb8363036e0d7b0e;hpb=4c2bd13c1fbd960115c9c623791811d5db1112f5;p=perl%2Fmodules%2FLexical-Types.git diff --git a/Types.xs b/Types.xs index 29e8d87..ff9e398 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 @@ -31,8 +35,8 @@ # define HvNAMELEN_get(H) strlen(HvNAME_get(H)) #endif -#ifndef SvIS_FREED -# define SvIS_FREED(sv) ((sv)->sv_flags == SVTYPEMASK) +#ifndef SvREFCNT_inc_simple_NN +# define SvREFCNT_inc_simple_NN SvREFCNT_inc #endif /* ... Thread safety and multiplicity ...................................... */ @@ -59,16 +63,63 @@ # 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_WORKAROUND_REQUIRE_PROPAGATION + +typedef struct { + SV *code; + UV requires; +} lt_hint_t; + +#define LT_HINT_STRUCT 1 + +#define LT_HINT_CODE(H) ((H)->code) + +#define LT_HINT_FREE(H) { \ + lt_hint_t *h = (H); \ + SvREFCNT_dec(h->code); \ + PerlMemShared_free(h); \ +} + +#else /* LT_WORKAROUND_REQUIRE_PROPAGATION */ + +typedef SV lt_hint_t; + +#define LT_HINT_STRUCT 0 + +#define LT_HINT_CODE(H) (H) + +#define LT_HINT_FREE(H) SvREFCNT_dec(H); + +#endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */ + #if LT_THREADSAFE #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_FREE(V) #define pPTBL pTHX #define pPTBL_ pTHX_ @@ -80,34 +131,75 @@ #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 */ + +/* ... Global data ......................................................... */ + #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION typedef struct { - ptable *tbl; +#if LT_THREADSAFE + ptable *tbl; /* It really is a ptable_hints */ tTHX owner; +#endif + SV *default_meth; + 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 SvREFCNT_inc(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; + + if (ud->owner == aTHX) + return; + +#if LT_HINT_STRUCT + + h2 = PerlMemShared_malloc(sizeof *h2); + h2->code = lt_clone(h1->code, ud->owner); + SvREFCNT_inc(h2->code); +#if LT_WORKAROUND_REQUIRE_PROPAGATION + h2->requires = h1->requires; +#endif + +#else /* LT_HINT_STRUCT */ + + h2 = lt_clone(h1, ud->owner); + SvREFCNT_inc(h2); + +#endif /* !LT_HINT_STRUCT */ + + ptable_hints_store(ud->tbl, ent->key, h2); } STATIC void lt_thread_cleanup(pTHX_ void *); @@ -127,56 +219,97 @@ STATIC void lt_thread_cleanup(pTHX_ void *ud) { } } +#endif /* LT_THREADSAFE */ + +/* ... Hint tags ........................................................... */ + STATIC SV *lt_tag(pTHX_ SV *value) { #define lt_tag(V) lt_tag(aTHX_ (V)) + lt_hint_t *h; + SV *code = NULL; dMY_CXT; - value = SvOK(value) && SvROK(value) ? SvRV(value) : NULL; + if (SvROK(value)) { + value = SvRV(value); + if (SvTYPE(value) >= SVt_PVCV) { + code = value; + SvREFCNT_inc_simple_NN(code); + } + } + +#if LT_HINT_STRUCT + h = PerlMemShared_malloc(sizeof *h); + h->code = code; + +#if LT_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 /* LT_WORKAROUND_REQUIRE_PROPAGATION */ + +#else /* LT_HINT_STRUCT */ + h = code; +#endif /* !LT_HINT_STRUCT */ + +#if LT_THREADSAFE /* 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); + * use the hint as the key itself. */ + ptable_hints_store(MY_CXT.tbl, h, h); +#endif /* LT_THREADSAFE */ - return newSVuv(PTR2UV(value)); + return newSViv(PTR2IV(h)); } 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)) - croak("Wrong hint"); + if (!(hint && SvIOK(hint))) + return NULL; - tag = INT2PTR(void *, SvIVX(hint)); - { - dMY_CXT; - value = ptable_fetch(MY_CXT.tbl, tag); - } + h = INT2PTR(lt_hint_t *, SvIVX(hint)); +#if LT_THREADSAFE + h = ptable_fetch(MY_CXT.tbl, h); +#endif /* LT_THREADSAFE */ - return value; -} +#if LT_WORKAROUND_REQUIRE_PROPAGATION + { + const PERL_SI *si; + I32 requires = 0; -#else + for (si = PL_curstackinfo; si; si = si->si_prev) { + I32 cxix; -STATIC SV *lt_tag(pTHX_ SV *value) { -#define lt_tag(V) lt_tag(aTHX_ (V)) - UV tag = 0; + for (cxix = si->si_cxix; cxix >= 0; --cxix) { + const PERL_CONTEXT *cx = si->si_cxstack + cxix; - if (SvOK(value) && SvROK(value)) { - value = SvRV(value); - SvREFCNT_inc(value); - tag = PTR2UV(value); + if (CxTYPE(cx) == CXt_EVAL && cx->blk_eval.old_op_type == OP_REQUIRE + && ++requires > h->requires) + return NULL; + } + } } +#endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */ - return newSVuv(tag); + return LT_HINT_CODE(h); } -#define lt_detag(H) INT2PTR(SV *, SvUVX(H)) - -#endif /* LT_THREADSAFE */ - STATIC U32 lt_hash = 0; STATIC SV *lt_hint(pTHX) { @@ -194,7 +327,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 +347,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 +369,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 @@ -263,6 +438,19 @@ STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) { return val; } +STATIC void lt_map_delete(pTHX_ const OP *o) { +#define lt_map_delete(O) lt_map_delete(aTHX_ (O)) +#ifdef USE_ITHREADS + MUTEX_LOCK(<_op_map_mutex); +#endif + + ptable_map_store(lt_op_map, o, NULL); + +#ifdef USE_ITHREADS + MUTEX_UNLOCK(<_op_map_mutex); +#endif +} + /* --- Hooks --------------------------------------------------------------- */ /* ... Our pp_padsv ........................................................ */ @@ -275,20 +463,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) { @@ -312,25 +520,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} .................................................. */ @@ -343,69 +553,65 @@ STATIC void lt_pp_padsv_restore(OP *o) { * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV] * globally. */ -STATIC SV *lt_default_meth = NULL; - 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 *orig_meth = MY_CXT.default_meth; SV *type_pkg = NULL; SV *type_meth = NULL; - SV *code = lt_detag(hint); - - SvREADONLY_on(orig_pkg); + int items; - if (code) { - int items; - dSP; + dSP; - ENTER; - SAVETMPS; - - PUSHMARK(SP); - EXTEND(SP, 2); - PUSHs(orig_pkg); - PUSHs(orig_meth); - PUTBACK; - - 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; @@ -419,18 +625,24 @@ 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); } -skip: return o; } 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); + return CALL_FPTR(lt_old_ck_padsv)(aTHX_ o); } @@ -446,20 +658,21 @@ BOOT: { if (!lt_initialized++) { HV *stash; -#if LT_THREADSAFE + MY_CXT_INIT; - MY_CXT.tbl = ptable_new(); - MY_CXT.owner = aTHX; +#if LT_THREADSAFE + MY_CXT.tbl = ptable_new(); + MY_CXT.owner = aTHX; #endif + MY_CXT.pp_padsv_saved = 0; + MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11); + SvREADONLY_on(MY_CXT.default_meth); lt_op_map = ptable_new(); #ifdef USE_ITHREADS MUTEX_INIT(<_op_map_mutex); #endif - lt_default_meth = newSVpvn("TYPEDSCALAR", 11); - SvREADONLY_on(lt_default_meth); - PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__); lt_old_ck_padany = PL_check[OP_PADANY]; @@ -480,6 +693,7 @@ PROTOTYPE: DISABLE PREINIT: ptable *t; int *level; + SV *cloned_default_meth; CODE: { my_cxt_t ud; @@ -487,11 +701,14 @@ CODE: ud.tbl = t = ptable_new(); ud.owner = MY_CXT.owner; ptable_walk(MY_CXT.tbl, lt_ptable_hints_clone, &ud); + cloned_default_meth = lt_clone(MY_CXT.default_meth, MY_CXT.owner); } { 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; + MY_CXT.default_meth = cloned_default_meth; } { level = PerlMemShared_malloc(sizeof *level);