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 */
124 STATIC SV *a_clone(pTHX_ SV *sv, tTHX owner) {
125 #define a_clone(S, O) a_clone(aTHX_ (S), (O))
130 if (SvTYPE(sv) == SVt_PVHV && HvNAME_get(sv))
133 param.stashes = stashes;
135 param.proto_perl = owner;
137 dupsv = sv_dup(sv, ¶m);
141 SvREFCNT_dec(stashes);
144 return SvREFCNT_inc(dupsv);
147 STATIC void a_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
149 a_hint_t *h1 = ent->val;
152 if (ud->owner == aTHX)
155 h2 = PerlMemShared_malloc(sizeof *h2);
157 h2->require_tag = PTR2IV(a_clone(INT2PTR(SV *, h1->require_tag), ud->owner));
159 ptable_hints_store(ud->tbl, ent->key, h2);
164 STATIC void a_thread_cleanup(pTHX_ void *ud) {
167 ptable_hints_free(MY_CXT.tbl);
170 #endif /* A_THREADSAFE */
172 STATIC IV a_require_tag(pTHX) {
173 #define a_require_tag() a_require_tag(aTHX)
174 const CV *cv, *outside;
179 /* If for some reason the pragma is operational at run-time, try to discover
180 * the current cv in use. */
183 for (si = PL_curstackinfo; si; si = si->si_prev) {
186 for (cxix = si->si_cxix; cxix >= 0; --cxix) {
187 const PERL_CONTEXT *cx = si->si_cxstack + cxix;
189 switch (CxTYPE(cx)) {
192 /* The propagation workaround is only needed up to 5.10.0 and at that
193 * time format and sub contexts were still identical. And even later the
194 * cv members offsets should have been kept the same. */
196 goto get_enclosing_cv;
198 cv = cx->blk_eval.cv;
199 goto get_enclosing_cv;
210 for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
216 STATIC SV *a_tag(pTHX_ UV bits) {
217 #define a_tag(B) a_tag(aTHX_ (B))
221 h = PerlMemShared_malloc(sizeof *h);
223 h->require_tag = a_require_tag();
226 /* We only need for the key to be an unique tag for looking up the value later.
227 * Allocated memory provides convenient unique identifiers, so that's why we
228 * use the hint as the key itself. */
229 ptable_hints_store(MY_CXT.tbl, h, h);
230 #endif /* A_THREADSAFE */
232 return newSViv(PTR2IV(h));
235 STATIC UV a_detag(pTHX_ const SV *hint) {
236 #define a_detag(H) a_detag(aTHX_ (H))
240 if (!(hint && SvIOK(hint)))
243 h = INT2PTR(a_hint_t *, SvIVX(hint));
245 h = ptable_fetch(MY_CXT.tbl, h);
246 #endif /* A_THREADSAFE */
248 if (a_require_tag() != h->require_tag)
254 #else /* A_WORKAROUND_REQUIRE_PROPAGATION */
256 #define a_tag(B) newSVuv(B)
257 /* PVs fetched from the hints chain have their SvLEN set to zero, so get the UV
264 ? sv_2uv(SvLEN(H) ? (H) : sv_mortalcopy(H)) \
270 #endif /* !A_WORKAROUND_REQUIRE_PROPAGATION */
272 /* Used both for hints and op flags */
273 #define A_HINT_STRICT 1
274 #define A_HINT_WARN 2
275 #define A_HINT_FETCH 4
276 #define A_HINT_STORE 8
277 #define A_HINT_EXISTS 16
278 #define A_HINT_DELETE 32
279 #define A_HINT_NOTIFY (A_HINT_STRICT|A_HINT_WARN)
280 #define A_HINT_DO (A_HINT_FETCH|A_HINT_STORE|A_HINT_EXISTS|A_HINT_DELETE)
281 #define A_HINT_MASK (A_HINT_NOTIFY|A_HINT_DO)
283 /* Only used in op flags */
284 #define A_HINT_ROOT 64
285 #define A_HINT_DEREF 128
287 STATIC U32 a_hash = 0;
289 STATIC UV a_hint(pTHX) {
290 #define a_hint() a_hint(aTHX)
292 #if A_HAS_PERL(5, 9, 5)
293 hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
295 __PACKAGE__, __PACKAGE_LEN__,
299 SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, a_hash);
304 return a_detag(hint);
307 /* ... op => info map ...................................................... */
315 #define PTABLE_NAME ptable_map
316 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
320 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
321 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
323 STATIC ptable *a_op_map = NULL;
327 #define dA_MAP_THX a_op_info a_op_map_tmp_oi
329 STATIC perl_mutex a_op_map_mutex;
331 STATIC const a_op_info *a_map_fetch(const OP *o, a_op_info *oi) {
332 const a_op_info *val;
334 MUTEX_LOCK(&a_op_map_mutex);
336 val = ptable_fetch(a_op_map, o);
342 MUTEX_UNLOCK(&a_op_map_mutex);
347 #define a_map_fetch(O) a_map_fetch((O), &a_op_map_tmp_oi)
349 #else /* USE_ITHREADS */
351 #define dA_MAP_THX dNOOP
353 #define a_map_fetch(O) ptable_fetch(a_op_map, (O))
355 #endif /* !USE_ITHREADS */
357 STATIC const a_op_info *a_map_store_locked(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
358 #define a_map_store_locked(O, PP, N, F) a_map_store_locked(aPTBLMS_ (O), (PP), (N), (F))
361 if (!(oi = ptable_fetch(a_op_map, o))) {
362 oi = PerlMemShared_malloc(sizeof *oi);
363 ptable_map_store(a_op_map, o, oi);
373 STATIC void a_map_store(pPTBLMS_ const OP *o, OP *(*old_pp)(pTHX), void *next, UV flags) {
374 #define a_map_store(O, PP, N, F) a_map_store(aPTBLMS_ (O), (PP), (N), (F))
377 MUTEX_LOCK(&a_op_map_mutex);
380 a_map_store_locked(o, old_pp, next, flags);
383 MUTEX_UNLOCK(&a_op_map_mutex);
387 STATIC void a_map_delete(pTHX_ const OP *o) {
388 #define a_map_delete(O) a_map_delete(aTHX_ (O))
390 MUTEX_LOCK(&a_op_map_mutex);
393 ptable_map_store(a_op_map, o, NULL);
396 MUTEX_UNLOCK(&a_op_map_mutex);
400 STATIC const OP *a_map_descend(const OP *o) {
401 switch (PL_opargs[o->op_type] & OA_CLASS_MASK) {
405 case OA_BASEOP_OR_UNOP:
406 return cUNOPo->op_first;
409 return cLISTOPo->op_last;
415 STATIC void a_map_store_root(pPTBLMS_ const OP *root, OP *(*old_pp)(pTHX), UV flags) {
416 #define a_map_store_root(R, PP, F) a_map_store_root(aPTBLMS_ (R), (PP), (F))
417 const a_op_info *roi;
422 MUTEX_LOCK(&a_op_map_mutex);
425 roi = a_map_store_locked(o, old_pp, (OP *) root, flags | A_HINT_ROOT);
427 while (o->op_flags & OPf_KIDS) {
428 o = a_map_descend(o);
431 if ((oi = ptable_fetch(a_op_map, o))) {
432 oi->flags &= ~A_HINT_ROOT;
433 oi->next = (a_op_info *) roi;
439 MUTEX_UNLOCK(&a_op_map_mutex);
445 STATIC void a_map_update_flags_topdown(const OP *root, UV flags) {
450 MUTEX_LOCK(&a_op_map_mutex);
453 flags &= ~A_HINT_ROOT;
456 if ((oi = ptable_fetch(a_op_map, o)))
457 oi->flags = (oi->flags & A_HINT_ROOT) | flags;
458 if (!(o->op_flags & OPf_KIDS))
460 o = a_map_descend(o);
464 MUTEX_UNLOCK(&a_op_map_mutex);
470 #define a_map_cancel(R) a_map_update_flags_topdown((R), 0)
472 STATIC void a_map_update_flags_bottomup(const OP *o, UV flags, UV rflags) {
476 MUTEX_LOCK(&a_op_map_mutex);
479 flags &= ~A_HINT_ROOT;
480 rflags |= A_HINT_ROOT;
482 oi = ptable_fetch(a_op_map, o);
483 while (!(oi->flags & A_HINT_ROOT)) {
490 MUTEX_UNLOCK(&a_op_map_mutex);
496 /* ... Decide whether this expression should be autovivified or not ........ */
498 STATIC UV a_map_resolve(const OP *o, const a_op_info *oi) {
499 UV flags = 0, rflags;
501 const a_op_info *roi = oi;
503 while (!(roi->flags & A_HINT_ROOT))
508 rflags = roi->flags & ~A_HINT_ROOT;
513 if (root->op_flags & OPf_MOD) {
514 if (rflags & A_HINT_STORE)
515 flags = (A_HINT_STORE|A_HINT_DEREF);
516 } else if (rflags & A_HINT_FETCH)
517 flags = (A_HINT_FETCH|A_HINT_DEREF);
521 a_map_update_flags_bottomup(o, 0, 0);
525 flags |= (rflags & A_HINT_NOTIFY);
526 a_map_update_flags_bottomup(o, flags, 0);
528 return oi->flags & A_HINT_ROOT ? 0 : flags;
531 /* ... Inspired from pp_defined() .......................................... */
533 STATIC int a_undef(pTHX_ SV *sv) {
534 #define a_undef(S) a_undef(aTHX_ (S))
535 switch (SvTYPE(sv)) {
539 if (AvMAX(sv) >= 0 || SvGMAGICAL(sv)
540 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
544 if (HvARRAY(sv) || SvGMAGICAL(sv)
545 || (SvRMAGICAL(sv) && mg_find(sv, PERL_MAGIC_tied)))
557 /* --- PP functions -------------------------------------------------------- */
559 /* Be aware that we restore PL_op->op_ppaddr from the pointer table old_pp
560 * value, another extension might have saved our pp replacement as the ppaddr
561 * for this op, so this doesn't ensure that our function will never be called
562 * again. That's why we don't remove the op info from our map, so that it can
563 * still run correctly if required. */
565 /* ... pp_rv2av ............................................................ */
567 STATIC OP *a_pp_rv2av(pTHX) {
572 oi = a_map_fetch(PL_op);
574 if (oi->flags & A_HINT_DEREF) {
576 /* We always need to push an empty array to fool the pp_aelem() that comes
580 av = sv_2mortal((SV *) newAV());
585 PL_op->op_ppaddr = oi->old_pp;
588 return CALL_FPTR(oi->old_pp)(aTHX);
591 /* ... pp_rv2hv ............................................................ */
593 STATIC OP *a_pp_rv2hv_simple(pTHX) {
598 oi = a_map_fetch(PL_op);
600 if (oi->flags & A_HINT_DEREF) {
604 PL_op->op_ppaddr = oi->old_pp;
607 return CALL_FPTR(oi->old_pp)(aTHX);
610 STATIC OP *a_pp_rv2hv(pTHX) {
615 oi = a_map_fetch(PL_op);
617 if (oi->flags & A_HINT_DEREF) {
621 hv = sv_2mortal((SV *) newHV());
626 PL_op->op_ppaddr = oi->old_pp;
629 return CALL_FPTR(oi->old_pp)(aTHX);
632 /* ... pp_deref (aelem,helem,rv2sv,padsv) .................................. */
634 STATIC OP *a_pp_deref(pTHX) {
640 oi = a_map_fetch(PL_op);
643 if (flags & A_HINT_DEREF) {
648 old_private = PL_op->op_private;
649 PL_op->op_private = ((old_private & ~OPpDEREF) | OPpLVAL_DEFER);
650 o = CALL_FPTR(oi->old_pp)(aTHX);
651 PL_op->op_private = old_private;
653 if (flags & (A_HINT_NOTIFY|A_HINT_STORE)) {
656 if (flags & A_HINT_STRICT)
657 croak("Reference vivification forbidden");
658 else if (flags & A_HINT_WARN)
659 warn("Reference was vivified");
660 else /* A_HINT_STORE */
661 croak("Can't vivify reference");
666 } else if ((flags & ~A_HINT_ROOT)
667 && (PL_op->op_private & OPpDEREF || flags & A_HINT_ROOT)) {
668 /* Decide if the expression must autovivify or not.
669 * This branch should be called only once by expression. */
670 flags = a_map_resolve(PL_op, oi);
672 /* We need the updated flags value in the deref branch. */
673 if (flags & A_HINT_DEREF)
677 /* This op doesn't need to skip autovivification, so restore the original
679 PL_op->op_ppaddr = oi->old_pp;
681 return CALL_FPTR(oi->old_pp)(aTHX);
684 /* ... pp_root (exists,delete,keys,values) ................................. */
686 STATIC OP *a_pp_root_unop(pTHX) {
691 /* Can only be reached by keys or values */
692 if (GIMME_V == G_SCALAR) {
701 const a_op_info *oi = a_map_fetch(PL_op);
702 return CALL_FPTR(oi->old_pp)(aTHX);
706 STATIC OP *a_pp_root_binop(pTHX) {
709 if (a_undef(TOPm1s)) {
712 if (PL_op->op_type == OP_EXISTS)
720 const a_op_info *oi = a_map_fetch(PL_op);
721 return CALL_FPTR(oi->old_pp)(aTHX);
725 /* --- Check functions ----------------------------------------------------- */
727 STATIC void a_recheck_rv2xv(pTHX_ OP *o, OPCODE type, OP *(*new_pp)(pTHX)) {
728 #define a_recheck_rv2xv(O, T, PP) a_recheck_rv2xv(aTHX_ (O), (T), (PP))
730 if (o->op_type == type && o->op_ppaddr != new_pp
731 && cUNOPo->op_first->op_type != OP_GV) {
733 const a_op_info *oi = a_map_fetch(o);
735 a_map_store(o, o->op_ppaddr, oi->next, oi->flags);
736 o->op_ppaddr = new_pp;
743 /* ... ck_pad{any,sv} ...................................................... */
745 /* Sadly, the PADSV OPs we are interested in don't trigger the padsv check
746 * function, but are instead manually mutated from a PADANY. This is why we set
747 * PL_ppaddr[OP_PADSV] in the padany check function so that PADSV OPs will have
748 * their op_ppaddr set to our pp_padsv. PL_ppaddr[OP_PADSV] is then reset at the
749 * beginning of every ck_pad{any,sv}. Some unwanted OPs can still call our
750 * pp_padsv, but much less than if we would have set PL_ppaddr[OP_PADSV]
753 STATIC OP *(*a_pp_padsv_saved)(pTHX) = 0;
755 STATIC void a_pp_padsv_save(void) {
756 if (a_pp_padsv_saved)
759 a_pp_padsv_saved = PL_ppaddr[OP_PADSV];
760 PL_ppaddr[OP_PADSV] = a_pp_deref;
763 STATIC void a_pp_padsv_restore(OP *o) {
764 if (!a_pp_padsv_saved)
767 if (o->op_ppaddr == a_pp_deref)
768 o->op_ppaddr = a_pp_padsv_saved;
770 PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
771 a_pp_padsv_saved = 0;
774 STATIC OP *(*a_old_ck_padany)(pTHX_ OP *) = 0;
776 STATIC OP *a_ck_padany(pTHX_ OP *o) {
779 a_pp_padsv_restore(o);
781 o = CALL_FPTR(a_old_ck_padany)(aTHX_ o);
784 if (hint & A_HINT_DO) {
786 a_map_store_root(o, a_pp_padsv_saved, hint);
793 STATIC OP *(*a_old_ck_padsv)(pTHX_ OP *) = 0;
795 STATIC OP *a_ck_padsv(pTHX_ OP *o) {
798 a_pp_padsv_restore(o);
800 o = CALL_FPTR(a_old_ck_padsv)(aTHX_ o);
803 if (hint & A_HINT_DO) {
804 a_map_store_root(o, o->op_ppaddr, hint);
805 o->op_ppaddr = a_pp_deref;
812 /* ... ck_deref (aelem,helem,rv2sv) ........................................ */
814 /* Those ops appear both at the root and inside an expression but there's no
815 * way to distinguish both situations. Worse, we can't even know if we are in a
816 * modifying context, so the expression can't be resolved yet. It will be at the
817 * first invocation of a_pp_deref() for this expression. */
819 STATIC OP *(*a_old_ck_aelem)(pTHX_ OP *) = 0;
820 STATIC OP *(*a_old_ck_helem)(pTHX_ OP *) = 0;
821 STATIC OP *(*a_old_ck_rv2sv)(pTHX_ OP *) = 0;
823 STATIC OP *a_ck_deref(pTHX_ OP *o) {
824 OP * (*old_ck)(pTHX_ OP *o) = 0;
827 switch (o->op_type) {
829 old_ck = a_old_ck_aelem;
830 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
831 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2AV, a_pp_rv2av);
834 old_ck = a_old_ck_helem;
835 if ((hint & A_HINT_DO) && !(hint & A_HINT_STRICT))
836 a_recheck_rv2xv(cUNOPo->op_first, OP_RV2HV, a_pp_rv2hv_simple);
839 old_ck = a_old_ck_rv2sv;
842 o = CALL_FPTR(old_ck)(aTHX_ o);
844 if (hint & A_HINT_DO) {
845 a_map_store_root(o, o->op_ppaddr, hint);
846 o->op_ppaddr = a_pp_deref;
853 /* ... ck_rv2xv (rv2av,rv2hv) .............................................. */
855 /* Those ops also appear both inisde and at the root, hence the caveats for
856 * a_ck_deref() still apply here. Since a padsv/rv2sv must appear before a
857 * rv2[ah]v, resolution is handled by the first call to a_pp_deref() in the
860 STATIC OP *(*a_old_ck_rv2av)(pTHX_ OP *) = 0;
861 STATIC OP *(*a_old_ck_rv2hv)(pTHX_ OP *) = 0;
863 STATIC OP *a_ck_rv2xv(pTHX_ OP *o) {
864 OP * (*old_ck)(pTHX_ OP *o) = 0;
865 OP * (*new_pp)(pTHX) = 0;
868 switch (o->op_type) {
869 case OP_RV2AV: old_ck = a_old_ck_rv2av; new_pp = a_pp_rv2av; break;
870 case OP_RV2HV: old_ck = a_old_ck_rv2hv; new_pp = a_pp_rv2hv_simple; break;
872 o = CALL_FPTR(old_ck)(aTHX_ o);
874 if (cUNOPo->op_first->op_type == OP_GV)
878 if (hint & A_HINT_DO && !(hint & A_HINT_STRICT)) {
879 a_map_store_root(o, o->op_ppaddr, hint);
880 o->op_ppaddr = new_pp;
887 /* ... ck_xslice (aslice,hslice) ........................................... */
889 /* I think those are only found at the root, but there's nothing that really
890 * prevent them to be inside the expression too. We only need to update the
891 * root so that the rest of the expression will see the right context when
892 * resolving. That's why we don't replace the ppaddr. */
894 STATIC OP *(*a_old_ck_aslice)(pTHX_ OP *) = 0;
895 STATIC OP *(*a_old_ck_hslice)(pTHX_ OP *) = 0;
897 STATIC OP *a_ck_xslice(pTHX_ OP *o) {
898 OP * (*old_ck)(pTHX_ OP *o) = 0;
901 switch (o->op_type) {
903 old_ck = a_old_ck_aslice;
906 old_ck = a_old_ck_hslice;
907 if (hint & A_HINT_DO)
908 a_recheck_rv2xv(cUNOPo->op_first->op_sibling, OP_RV2HV, a_pp_rv2hv);
911 o = CALL_FPTR(old_ck)(aTHX_ o);
913 if (hint & A_HINT_DO) {
914 a_map_store_root(o, 0, hint);
921 /* ... ck_root (exists,delete,keys,values) ................................. */
923 /* Those ops are only found at the root of a dereferencing expression. We can
924 * then resolve at compile time if vivification must take place or not. */
926 STATIC OP *(*a_old_ck_exists)(pTHX_ OP *) = 0;
927 STATIC OP *(*a_old_ck_delete)(pTHX_ OP *) = 0;
928 STATIC OP *(*a_old_ck_keys) (pTHX_ OP *) = 0;
929 STATIC OP *(*a_old_ck_values)(pTHX_ OP *) = 0;
931 STATIC OP *a_ck_root(pTHX_ OP *o) {
932 OP * (*old_ck)(pTHX_ OP *o) = 0;
933 OP * (*new_pp)(pTHX) = 0;
934 bool enabled = FALSE;
937 switch (o->op_type) {
939 old_ck = a_old_ck_exists;
940 new_pp = a_pp_root_binop;
941 enabled = hint & A_HINT_EXISTS;
944 old_ck = a_old_ck_delete;
945 new_pp = a_pp_root_binop;
946 enabled = hint & A_HINT_DELETE;
949 old_ck = a_old_ck_keys;
950 new_pp = a_pp_root_unop;
951 enabled = hint & A_HINT_FETCH;
954 old_ck = a_old_ck_values;
955 new_pp = a_pp_root_unop;
956 enabled = hint & A_HINT_FETCH;
959 o = CALL_FPTR(old_ck)(aTHX_ o);
961 if (hint & A_HINT_DO) {
963 a_map_update_flags_topdown(o, hint | A_HINT_DEREF);
964 a_map_store_root(o, o->op_ppaddr, hint);
965 o->op_ppaddr = new_pp;
975 STATIC U32 a_initialized = 0;
977 STATIC void a_teardown(pTHX_ void *root) {
987 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
990 ptable_hints_free(MY_CXT.tbl);
994 PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_old_ck_padany);
996 PL_check[OP_PADSV] = MEMBER_TO_FPTR(a_old_ck_padsv);
999 PL_check[OP_AELEM] = MEMBER_TO_FPTR(a_old_ck_aelem);
1001 PL_check[OP_HELEM] = MEMBER_TO_FPTR(a_old_ck_helem);
1003 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(a_old_ck_rv2sv);
1006 PL_check[OP_RV2AV] = MEMBER_TO_FPTR(a_old_ck_rv2av);
1008 PL_check[OP_RV2HV] = MEMBER_TO_FPTR(a_old_ck_rv2hv);
1011 PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_old_ck_aslice);
1012 a_old_ck_aslice = 0;
1013 PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_old_ck_hslice);
1014 a_old_ck_hslice = 0;
1016 PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_old_ck_exists);
1017 a_old_ck_exists = 0;
1018 PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_old_ck_delete);
1019 a_old_ck_delete = 0;
1020 PL_check[OP_KEYS] = MEMBER_TO_FPTR(a_old_ck_keys);
1022 PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_old_ck_values);
1023 a_old_ck_values = 0;
1025 if (a_pp_padsv_saved) {
1026 PL_ppaddr[OP_PADSV] = a_pp_padsv_saved;
1027 a_pp_padsv_saved = 0;
1033 STATIC void a_setup(pTHX) {
1034 #define a_setup() a_setup(aTHX)
1038 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1041 MY_CXT.tbl = ptable_new();
1042 MY_CXT.owner = aTHX;
1046 a_old_ck_padany = PL_check[OP_PADANY];
1047 PL_check[OP_PADANY] = MEMBER_TO_FPTR(a_ck_padany);
1048 a_old_ck_padsv = PL_check[OP_PADSV];
1049 PL_check[OP_PADSV] = MEMBER_TO_FPTR(a_ck_padsv);
1051 a_old_ck_aelem = PL_check[OP_AELEM];
1052 PL_check[OP_AELEM] = MEMBER_TO_FPTR(a_ck_deref);
1053 a_old_ck_helem = PL_check[OP_HELEM];
1054 PL_check[OP_HELEM] = MEMBER_TO_FPTR(a_ck_deref);
1055 a_old_ck_rv2sv = PL_check[OP_RV2SV];
1056 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(a_ck_deref);
1058 a_old_ck_rv2av = PL_check[OP_RV2AV];
1059 PL_check[OP_RV2AV] = MEMBER_TO_FPTR(a_ck_rv2xv);
1060 a_old_ck_rv2hv = PL_check[OP_RV2HV];
1061 PL_check[OP_RV2HV] = MEMBER_TO_FPTR(a_ck_rv2xv);
1063 a_old_ck_aslice = PL_check[OP_ASLICE];
1064 PL_check[OP_ASLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1065 a_old_ck_hslice = PL_check[OP_HSLICE];
1066 PL_check[OP_HSLICE] = MEMBER_TO_FPTR(a_ck_xslice);
1068 a_old_ck_exists = PL_check[OP_EXISTS];
1069 PL_check[OP_EXISTS] = MEMBER_TO_FPTR(a_ck_root);
1070 a_old_ck_delete = PL_check[OP_DELETE];
1071 PL_check[OP_DELETE] = MEMBER_TO_FPTR(a_ck_root);
1072 a_old_ck_keys = PL_check[OP_KEYS];
1073 PL_check[OP_KEYS] = MEMBER_TO_FPTR(a_ck_root);
1074 a_old_ck_values = PL_check[OP_VALUES];
1075 PL_check[OP_VALUES] = MEMBER_TO_FPTR(a_ck_root);
1078 call_atexit(a_teardown, aTHX);
1080 call_atexit(a_teardown, NULL);
1086 STATIC U32 a_booted = 0;
1088 /* --- XS ------------------------------------------------------------------ */
1090 MODULE = autovivification PACKAGE = autovivification
1099 a_op_map = ptable_new();
1101 MUTEX_INIT(&a_op_map_mutex);
1104 PERL_HASH(a_hash, __PACKAGE__, __PACKAGE_LEN__);
1106 stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1107 newCONSTSUB(stash, "A_HINT_STRICT", newSVuv(A_HINT_STRICT));
1108 newCONSTSUB(stash, "A_HINT_WARN", newSVuv(A_HINT_WARN));
1109 newCONSTSUB(stash, "A_HINT_FETCH", newSVuv(A_HINT_FETCH));
1110 newCONSTSUB(stash, "A_HINT_STORE", newSVuv(A_HINT_STORE));
1111 newCONSTSUB(stash, "A_HINT_EXISTS", newSVuv(A_HINT_EXISTS));
1112 newCONSTSUB(stash, "A_HINT_DELETE", newSVuv(A_HINT_DELETE));
1113 newCONSTSUB(stash, "A_HINT_MASK", newSVuv(A_HINT_MASK));
1114 newCONSTSUB(stash, "A_THREADSAFE", newSVuv(A_THREADSAFE));
1115 newCONSTSUB(stash, "A_FORKSAFE", newSVuv(A_FORKSAFE));
1121 #if A_THREADSAFE && A_WORKAROUND_REQUIRE_PROPAGATION
1132 ud.tbl = t = ptable_new();
1133 ud.owner = MY_CXT.owner;
1134 ptable_walk(MY_CXT.tbl, a_ptable_clone, &ud);
1139 MY_CXT.owner = aTHX;
1141 reap(3, a_thread_cleanup, NULL);
1150 RETVAL = a_tag(SvOK(hint) ? SvUV(hint) : 0);
1160 RETVAL = newSVuv(a_detag(tag));