1 /* This file is part of the autovivification Perl module.
2 * See http://search.cpan.org/dist/autovivification/ */
4 #define PERL_NO_GET_CONTEXT
9 #define __PACKAGE__ "autovivification"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
12 /* --- Compatibility wrappers ---------------------------------------------- */
15 # define HvNAME_get(H) HvNAME(H)
19 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
22 #define A_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
25 #if defined(ENTER_with_name) && !A_HAS_PERL(5, 11, 4)
26 # define ENTERn(N) ENTER_with_name(N)
28 # define ENTERn(N) ENTER
32 #if defined(LEAVE_with_name) && !A_HAS_PERL(5, 11, 4)
33 # define LEAVEn(N) LEAVE_with_name(N)
35 # define LEAVEn(N) LEAVE
38 #ifndef A_WORKAROUND_REQUIRE_PROPAGATION
39 # define A_WORKAROUND_REQUIRE_PROPAGATION !A_HAS_PERL(5, 10, 1)
42 /* ... Thread safety and multiplicity ...................................... */
44 /* Always safe when the workaround isn't needed */
45 #if !A_WORKAROUND_REQUIRE_PROPAGATION
48 /* Otherwise, safe unless Makefile.PL says it's Win32 */
49 #elif !defined(A_FORKSAFE)
53 #ifndef A_MULTIPLICITY
54 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
55 # define A_MULTIPLICITY 1
57 # define A_MULTIPLICITY 0
60 #if A_MULTIPLICITY && !defined(tTHX)
61 # define tTHX PerlInterpreter*
64 #if A_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))
65 # define A_THREADSAFE 1
67 # define MY_CXT_CLONE \
69 my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
70 Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
71 sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
74 # define A_THREADSAFE 0
76 # define dMY_CXT dNOOP
78 # define MY_CXT a_globaldata
80 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
82 # define MY_CXT_INIT NOOP
84 # define MY_CXT_CLONE NOOP
87 /* --- Helpers ------------------------------------------------------------- */
89 /* ... Thread-safe hints ................................................... */
91 #if A_WORKAROUND_REQUIRE_PROPAGATION
98 #define A_HINT_FREE(H) PerlMemShared_free(H)
102 #define PTABLE_NAME ptable_hints
103 #define PTABLE_VAL_FREE(V) A_HINT_FREE(V)
112 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
113 #define ptable_hints_free(T) ptable_hints_free(aTHX_ (T))
115 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
118 ptable *tbl; /* It really is a ptable_hints */
126 #if A_HAS_PERL(5, 13, 2)
127 CLONE_PARAMS *params;
133 #if A_HAS_PERL(5, 13, 2)
134 # define a_ptable_clone_ud_init(U, T, O) \
136 (U).params = Perl_clone_params_new((O), aTHX)
137 # define a_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
138 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), (U)->params))
140 # define a_ptable_clone_ud_init(U, T, O) \
142 (U).params.stashes = newAV(); \
143 (U).params.flags = 0; \
144 (U).params.proto_perl = (O)
145 # define a_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
146 # define a_dup_inc(S, U) SvREFCNT_inc(sv_dup((S), &((U)->params)))
149 STATIC void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
150 a_ptable_clone_ud *ud = ud_;
151 a_hint_t *h1 = ent->val;
154 h2 = PerlMemShared_malloc(sizeof *h2);
156 h2->require_tag = PTR2IV(a_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
158 ptable_hints_store(ud->tbl, ent->key, h2);
163 STATIC void a_thread_cleanup(pTHX_ void *ud) {
166 ptable_hints_free(MY_CXT.tbl);
169 #endif /* A_THREADSAFE */
171 STATIC IV a_require_tag(pTHX) {
172 #define a_require_tag() a_require_tag(aTHX)
173 const CV *cv, *outside;
178 /* If for some reason the pragma is operational at run-time, try to discover
179 * the current cv in use. */
182 for (si = PL_curstackinfo; si; si = si->si_prev) {
185 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
186 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
188 switch (CxTYPE(cx)) {
191 /* The propagation workaround is only needed up to 5.10.0 and at that
192 * time format and sub contexts were still identical. And even later the
193 * cv members offsets should have been kept the same. */
195 goto get_enclosing_cv;
197 cv = cx->blk_eval.cv;
198 goto get_enclosing_cv;
209 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
215 STATIC SV *a_tag(pTHX_ UV bits) {
216 #define a_tag(B) a_tag(aTHX_ (B))
220 h = PerlMemShared_malloc(sizeof *h);
222 h->require_tag = a_require_tag();
225 /* We only need for the key to be an unique tag for looking up the value later.
226 * Allocated memory provides convenient unique identifiers, so that's why we
227 * use the hint as the key itself. */
228 ptable_hints_store(MY_CXT.tbl, h, h);
229 #endif /* A_THREADSAFE */
231 return newSViv(PTR2IV(h));
234 STATIC UV a_detag(pTHX_ const SV *hint) {
235 #define a_detag(H) a_detag(aTHX_ (H))
239 if (!(hint && SvIOK(hint)))
242 h = INT2PTR(a_hint_t *, SvIVX(hint));
244 h = ptable_fetch(MY_CXT.tbl, h);
245 #endif /* A_THREADSAFE */
247 if (a_require_tag() != h->require_tag)
253 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
255 #define a_tag(B) newSVuv(B)
256 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
263 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
269 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
271 /* Used both for hints and op flags */
272 #define A_HINT_STRICT 1
273 #define A_HINT_WARN 2
274 #define A_HINT_FETCH 4
275 #define A_HINT_STORE 8
276 #define A_HINT_EXISTS 16
277 #define A_HINT_DELETE 32
278 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
279 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
280 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
282 /* Only used in op flags */
283 #define A_HINT_ROOT 64
284 #define A_HINT_DEREF 128
286 STATIC U32 a_hash = 0;
288 STATIC UV a_hint(pTHX) {
289 #define a_hint() a_hint(aTHX)
291 #ifdef cop_hints_fetch_pvn
292 hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, a_hash, 0);
293 #elif A_HAS_PERL(5, 9, 5)
294 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
296 __PACKAGE__, __PACKAGE_LEN__,
300 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, a_hash);
305 return a_detag(hint);
308 /* ... op => info map ...................................................... */
316 #define PTABLE_NAME ptable_map
317 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
321 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
322 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
324 STATIC ptable *a_op_map = NULL;
328 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
330 STATIC perl_mutex a_op_map_mutex;
332 #define A_LOCK(M) MUTEX_LOCK(M)
333 #define A_UNLOCK(M) MUTEX_UNLOCK(M)
335 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
336 const a_op_info *val;
338 A_LOCK(&a_op_map_mutex);
340 val = ptable_fetch(a_op_map, o);
346 A_UNLOCK(&a_op_map_mutex);
351 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
353 #else /* USE_ITHREADS */
355 #define dA_MAP_THX dNOOP
357 #define A_LOCK(M) NOOP
358 #define A_UNLOCK(M) NOOP
360 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
362 #endif /* !USE_ITHREADS */
364 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
365 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
368 if (!(oi = ptable_fetch(a_op_map, o))) {
369 oi = PerlMemShared_malloc(sizeof *oi);
370 ptable_map_store(a_op_map, o, oi);
380 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
381 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
382 A_LOCK(&a_op_map_mutex);
384 a_map_store_locked(o, old_pp, next, flags);
386 A_UNLOCK(&a_op_map_mutex);
389 STATIC void a_map_delete(pTHX_ const OP *o) {
390 #define a_map_delete(O) a_map_delete(aTHX_ (O))
391 A_LOCK(&a_op_map_mutex);
393 ptable_map_delete(a_op_map, o);
395 A_UNLOCK(&a_op_map_mutex);
398 STATIC const OP *a_map_descend(const OP *o) {
399 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
403 case OA_BASEOP_OR_UNOP:
404 return cUNOPo->op_first;
407 return cLISTOPo->op_last;
413 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
414 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
415 const a_op_info *roi;
419 A_LOCK(&a_op_map_mutex);
421 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
423 while (o->op_flags & OPf_KIDS) {
424 o = a_map_descend(o);
427 if ((oi = ptable_fetch(a_op_map, o))) {
428 oi->flags &= ~A_HINT_ROOT;
429 oi->next = (a_op_info *) roi;
434 A_UNLOCK(&a_op_map_mutex);
439 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
443 A_LOCK(&a_op_map_mutex);
445 flags &= ~A_HINT_ROOT;
448 if ((oi = ptable_fetch(a_op_map, o)))
449 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
450 if (!(o->op_flags & OPf_KIDS))
452 o = a_map_descend(o);
455 A_UNLOCK(&a_op_map_mutex);
460 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
462 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
465 A_LOCK(&a_op_map_mutex);
467 flags &= ~A_HINT_ROOT;
468 rflags |= A_HINT_ROOT;
470 oi = ptable_fetch(a_op_map, o);
471 while (!(oi->flags & A_HINT_ROOT)) {
477 A_UNLOCK(&a_op_map_mutex);
482 /* ... Decide whether this expression should be autovivified or not ........ */
484 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
485 UV flags = 0, rflags;
487 const a_op_info *roi = oi;
489 while (!(roi->flags & A_HINT_ROOT))
494 rflags = roi->flags & ~A_HINT_ROOT;
499 if (root->op_flags & OPf_MOD) {
500 if (rflags & A_HINT_STORE)
501 flags = (A_HINT_STORE|A_HINT_DEREF);
502 } else if (rflags & A_HINT_FETCH)
503 flags = (A_HINT_FETCH|A_HINT_DEREF);
507 a_map_update_flags_bottomup(o, 0, 0);
511 flags |= (rflags & A_HINT_NOTIFY);
512 a_map_update_flags_bottomup(o, flags, 0);
514 return oi->flags & A_HINT_ROOT ? 0 : flags;
517 /* ... Inspired from pp_defined() .......................................... */
519 STATIC int a_undef(pTHX_ SV *sv) {
520 #define a_undef(S) a_undef(aTHX_ (S))
521 switch (SvTYPE(sv)) {
525 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
526 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
530 if (HvARRAY(sv) || SvGMAGICAL(sv)
531 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
543 /* --- PP functions -------------------------------------------------------- */
545 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
546 * value, another extension might have saved our pp replacement as the ppaddr
547 * for this op, so this doesn't ensure that our function will never be called
548 * again. That's why we don't remove the op info from our map, so that it can
549 * still run correctly if required. */
551 /* ... pp_rv2av ............................................................ */
553 STATIC OP *a_pp_rv2av(pTHX) {
558 oi = a_map_fetch(PL_op);
560 if (oi->flags & A_HINT_DEREF) {
562 /* We always need to push an empty array to fool the pp_aelem() that comes
566 av = sv_2mortal((SV *) newAV());
571 PL_op->op_ppaddr = oi->old_pp;
574 return oi->old_pp(aTHX);
577 /* ... pp_rv2hv ............................................................ */
579 STATIC OP *a_pp_rv2hv_simple(pTHX) {
584 oi = a_map_fetch(PL_op);
586 if (oi->flags & A_HINT_DEREF) {
590 PL_op->op_ppaddr = oi->old_pp;
593 return oi->old_pp(aTHX);
596 STATIC OP *a_pp_rv2hv(pTHX) {
601 oi = a_map_fetch(PL_op);
603 if (oi->flags & A_HINT_DEREF) {
607 hv = sv_2mortal((SV *) newHV());
612 PL_op->op_ppaddr = oi->old_pp;
615 return oi->old_pp(aTHX);
618 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
620 STATIC OP *a_pp_deref(pTHX) {
626 oi = a_map_fetch(PL_op);
629 if (flags & A_HINT_DEREF) {
634 old_private = PL_op->op_private;
635 PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
636 o = oi->old_pp(aTHX);
637 PL_op->op_private = old_private;
639 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
642 if (flags & A_HINT_STRICT)
643 croak("Reference vivification forbidden");
644 else if (flags & A_HINT_WARN)
645 warn("Reference was vivified");
646 else /* A_HINT_STORE */
647 croak("Can't vivify reference");
652 } else if ((flags & ~A_HINT_ROOT)
653 && (PL_op->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
654 /* Decide if the expression must autovivify or not.
655 * This branch should be called only once by expression. */
656 flags = a_map_resolve(PL_op, oi);
658 /* We need the updated flags value in the deref branch. */
659 if (flags & A_HINT_DEREF)
663 /* This op doesn't need to skip autovivification, so restore the original
665 PL_op->op_ppaddr = oi->old_pp;
667 return oi->old_pp(aTHX);
670 /* ... pp_root (exists,delete,keys,values) ................................. */
672 STATIC OP *a_pp_root_unop(pTHX) {
677 /* Can only be reached by keys or values */
678 if (GIMME_V == G_SCALAR) {
687 const a_op_info *oi = a_map_fetch(PL_op);
688 return oi->old_pp(aTHX);
692 STATIC OP *a_pp_root_binop(pTHX) {
695 if (a_undef(TOPm1s)) {
698 if (PL_op->op_type == OP_EXISTS)
706 const a_op_info *oi = a_map_fetch(PL_op);
707 return oi->old_pp(aTHX);
711 /* --- Check functions ----------------------------------------------------- */
713 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
714 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
716 if (o->op_type == type && o->op_ppaddr != new_pp
717 && cUNOPo->op_first->op_type != OP_GV) {
719 const a_op_info *oi = a_map_fetch(o);
721 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
722 o->op_ppaddr = new_pp;
729 /* ... ck_pad{any,sv} ...................................................... */
731 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
732 * function, but are instead manually mutated from a PADANY. This is why we set
733 * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
734 * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
735 * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
736 * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
739 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
741 STATIC void a_pp_padsv_save(void) {
742 if (a_pp_padsv_saved)
745 a_pp_padsv_saved = PL_ppaddr[OP_PADSV];
746 PL_ppaddr[OP_PADSV] = a_pp_deref;
749 STATIC void a_pp_padsv_restore(OP *o) {
750 if (!a_pp_padsv_saved)
753 if (o->op_ppaddr == a_pp_deref)
754 o->op_ppaddr = a_pp_padsv_saved;
756 PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
757 a_pp_padsv_saved = 0;
760 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
762 STATIC OP *a_ck_padany(pTHX_ OP *o) {
765 a_pp_padsv_restore(o);
767 o = a_old_ck_padany(aTHX_ o);
770 if (hint & A_HINT_DO) {
772 a_map_store_root(o, a_pp_padsv_saved, hint);
779 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
781 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
784 a_pp_padsv_restore(o);
786 o = a_old_ck_padsv(aTHX_ o);
789 if (hint & A_HINT_DO) {
790 a_map_store_root(o, o->op_ppaddr, hint);
791 o->op_ppaddr = a_pp_deref;
798 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
800 /* Those ops appear both at the root and inside an expression but there's no
801 * way to distinguish both situations. Worse, we can't even know if we are in a
802 * modifying context, so the expression can't be resolved yet. It will be at the
803 * first invocation of a_pp_deref() for this expression. */
805 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
806 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
807 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
809 STATIC OP *a_ck_deref(pTHX_ OP *o) {
810 OP * (*old_ck)(pTHX_ OP *o) = 0;
813 switch (o->op_type) {
815 old_ck = a_old_ck_aelem;
816 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
817 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
820 old_ck = a_old_ck_helem;
821 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
822 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
825 old_ck = a_old_ck_rv2sv;
830 if (hint & A_HINT_DO) {
831 a_map_store_root(o, o->op_ppaddr, hint);
832 o->op_ppaddr = a_pp_deref;
839 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
841 /* Those ops also appear both inisde and at the root, hence the caveats for
842 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
843 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
846 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
847 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
849 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
850 OP * (*old_ck)(pTHX_ OP *o) = 0;
851 OP * (*new_pp)(pTHX) = 0;
854 switch (o->op_type) {
855 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
856 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
860 if (cUNOPo->op_first->op_type == OP_GV)
864 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
865 a_map_store_root(o, o->op_ppaddr, hint);
866 o->op_ppaddr = new_pp;
873 /* ... ck_xslice (aslice,hslice) ........................................... */
875 /* I think those are only found at the root, but there's nothing that really
876 * prevent them to be inside the expression too. We only need to update the
877 * root so that the rest of the expression will see the right context when
878 * resolving. That's why we don't replace the ppaddr. */
880 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
881 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
883 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
884 OP * (*old_ck)(pTHX_ OP *o) = 0;
887 switch (o->op_type) {
889 old_ck = a_old_ck_aslice;
892 old_ck = a_old_ck_hslice;
893 if (hint & A_HINT_DO)
894 a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
899 if (hint & A_HINT_DO) {
900 a_map_store_root(o, 0, hint);
907 /* ... ck_root (exists,delete,keys,values) ................................. */
909 /* Those ops are only found at the root of a dereferencing expression. We can
910 * then resolve at compile time if vivification must take place or not. */
912 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
913 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
914 STATIC OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
915 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
917 STATIC OP *a_ck_root(pTHX_ OP *o) {
918 OP * (*old_ck)(pTHX_ OP *o) = 0;
919 OP * (*new_pp)(pTHX) = 0;
920 bool enabled = FALSE;
923 switch (o->op_type) {
925 old_ck = a_old_ck_exists;
926 new_pp = a_pp_root_binop;
927 enabled = hint & A_HINT_EXISTS;
930 old_ck = a_old_ck_delete;
931 new_pp = a_pp_root_binop;
932 enabled = hint & A_HINT_DELETE;
935 old_ck = a_old_ck_keys;
936 new_pp = a_pp_root_unop;
937 enabled = hint & A_HINT_FETCH;
940 old_ck = a_old_ck_values;
941 new_pp = a_pp_root_unop;
942 enabled = hint & A_HINT_FETCH;
947 if (hint & A_HINT_DO) {
949 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
950 a_map_store_root(o, o->op_ppaddr, hint);
951 o->op_ppaddr = new_pp;
961 STATIC U32 a_initialized = 0;
963 STATIC void a_teardown(pTHX_ void *root) {
973 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
976 ptable_hints_free(MY_CXT.tbl);
980 PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_old_ck_padany);
982 PL_check[OP_PADSV] = MEMBER_TO_FPTR(a_old_ck_padsv);
985 PL_check[OP_AELEM] = MEMBER_TO_FPTR(a_old_ck_aelem);
987 PL_check[OP_HELEM] = MEMBER_TO_FPTR(a_old_ck_helem);
989 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(a_old_ck_rv2sv);
992 PL_check[OP_RV2AV] = MEMBER_TO_FPTR(a_old_ck_rv2av);
994 PL_check[OP_RV2HV] = MEMBER_TO_FPTR(a_old_ck_rv2hv);
997 PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_old_ck_aslice);
999 PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_old_ck_hslice);
1000 a_old_ck_hslice = 0;
1002 PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_old_ck_exists);
1003 a_old_ck_exists = 0;
1004 PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_old_ck_delete);
1005 a_old_ck_delete = 0;
1006 PL_check[OP_KEYS] = MEMBER_TO_FPTR(a_old_ck_keys);
1008 PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_old_ck_values);
1009 a_old_ck_values = 0;
1011 if (a_pp_padsv_saved) {
1012 PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
1013 a_pp_padsv_saved = 0;
1019 STATIC void a_setup(pTHX) {
1020 #define a_setup() a_setup(aTHX)
1024 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1027 MY_CXT.tbl = ptable_new();
1028 MY_CXT.owner = aTHX;
1032 a_old_ck_padany = PL_check[OP_PADANY];
1033 PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
1034 a_old_ck_padsv = PL_check[OP_PADSV];
1035 PL_check[OP_PADSV] = MEMBER_TO_FPTR(a_ck_padsv);
1037 a_old_ck_aelem = PL_check[OP_AELEM];
1038 PL_check[OP_AELEM] = MEMBER_TO_FPTR(a_ck_deref);
1039 a_old_ck_helem = PL_check[OP_HELEM];
1040 PL_check[OP_HELEM] = MEMBER_TO_FPTR(a_ck_deref);
1041 a_old_ck_rv2sv = PL_check[OP_RV2SV];
1042 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(a_ck_deref);
1044 a_old_ck_rv2av = PL_check[OP_RV2AV];
1045 PL_check[OP_RV2AV] = MEMBER_TO_FPTR(a_ck_rv2xv);
1046 a_old_ck_rv2hv = PL_check[OP_RV2HV];
1047 PL_check[OP_RV2HV] = MEMBER_TO_FPTR(a_ck_rv2xv);
1049 a_old_ck_aslice = PL_check[OP_ASLICE];
1050 PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1051 a_old_ck_hslice = PL_check[OP_HSLICE];
1052 PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1054 a_old_ck_exists = PL_check[OP_EXISTS];
1055 PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
1056 a_old_ck_delete = PL_check[OP_DELETE];
1057 PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
1058 a_old_ck_keys = PL_check[OP_KEYS];
1059 PL_check[OP_KEYS] = MEMBER_TO_FPTR(a_ck_root);
1060 a_old_ck_values = PL_check[OP_VALUES];
1061 PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
1064 call_atexit(a_teardown, aTHX);
1066 call_atexit(a_teardown, NULL);
1072 STATIC U32 a_booted = 0;
1074 /* --- XS ------------------------------------------------------------------ */
1076 MODULE = autovivification PACKAGE = autovivification
1085 a_op_map = ptable_new();
1087 MUTEX_INIT(&a_op_map_mutex);
1090 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1092 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1093 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1094 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1095 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1096 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1097 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1098 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1099 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1100 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1101 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1107 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1116 a_ptable_clone_ud ud;
1120 a_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1121 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1122 a_ptable_clone_ud_deinit(ud);
1127 MY_CXT.owner = aTHX;
1129 reap(3, a_thread_cleanup, NULL);
1138 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1148 RETVAL = newSVuv(a_detag(tag));