This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make sure the stack is balanced in the case that we fake the result of unsupported...
[perl5.git] / hv.c
diff --git a/hv.c b/hv.c
index 6d6edb2..e221499 100644 (file)
--- a/hv.c
+++ b/hv.c
@@ -817,7 +817,7 @@ Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
 
        xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
        if (!counter) {                         /* initial entry? */
-       } else if (xhv->xhv_keys > (IV)xhv->xhv_max) {
+       } else if (xhv->xhv_keys > xhv->xhv_max) {
            hsplit(hv);
        } else if(!HvREHASH(hv)) {
            U32 n_links = 1;
@@ -1085,7 +1085,6 @@ S_hsplit(pTHX_ HV *hv)
     register I32 i;
     char *a = (char*) HvARRAY(hv);
     register HE **aep;
-    register HE **oentry;
     int longest_chain = 0;
     int was_shared;
 
@@ -1142,25 +1141,26 @@ S_hsplit(pTHX_ HV *hv)
     for (i=0; i<oldsize; i++,aep++) {
        int left_length = 0;
        int right_length = 0;
-       register HE *entry;
+       HE **oentry = aep;
+       HE *entry = *aep;
        register HE **bep;
 
-       if (!*aep)                              /* non-existent */
+       if (!entry)                             /* non-existent */
            continue;
        bep = aep+oldsize;
-       for (oentry = aep, entry = *aep; entry; entry = *oentry) {
+       do {
            if ((HeHASH(entry) & newsize) != (U32)i) {
                *oentry = HeNEXT(entry);
                HeNEXT(entry) = *bep;
                *bep = entry;
                right_length++;
-               continue;
            }
            else {
                oentry = &HeNEXT(entry);
                left_length++;
            }
-       }
+           entry = *oentry;
+       } while (entry);
        /* I think we don't actually need to keep track of the longest length,
           merely flag if anything is too long. But for the moment while
           developing this code I'll track it.  */
@@ -1250,8 +1250,6 @@ Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
     register I32 i;
     register char *a;
     register HE **aep;
-    register HE *entry;
-    register HE **oentry;
 
     PERL_ARGS_ASSERT_HV_KSPLIT;
 
@@ -1311,9 +1309,12 @@ Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
 
     aep = (HE**)a;
     for (i=0; i<oldsize; i++,aep++) {
-       if (!*aep)                              /* non-existent */
+       HE **oentry = aep;
+       HE *entry = *aep;
+
+       if (!entry)                             /* non-existent */
            continue;
-       for (oentry = aep, entry = *aep; entry; entry = *oentry) {
+       do {
            register I32 j = (HeHASH(entry) & newsize);
 
            if (j != i) {
@@ -1321,11 +1322,11 @@ Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
                *oentry = HeNEXT(entry);
                HeNEXT(entry) = aep[j];
                aep[j] = entry;
-               continue;
            }
            else
                oentry = &HeNEXT(entry);
-       }
+           entry = *oentry;
+       } while (entry);
     }
 }
 
@@ -1457,8 +1458,8 @@ Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
     if (!entry)
        return;
     val = HeVAL(entry);
-    if (HvNAME(hv) && anonymise_cv(HvNAME_HEK(hv), val) && GvCVu(val))
-       mro_method_changed_in(hv);
+    if (val && isGV(val) && isGV_with_GP(val) && GvCVu(val) && HvNAME_get(hv))
+        mro_method_changed_in(hv);     /* deletion of method from stash */
     SvREFCNT_dec(val);
     if (HeKLEN(entry) == HEf_SVKEY) {
        SvREFCNT_dec(HeKEY_sv(entry));
@@ -1471,33 +1472,6 @@ Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
     del_HE(entry);
 }
 
-static I32
-S_anonymise_cv(pTHX_ HEK *stash, SV *val)
-{
-    CV *cv;
-
-    PERL_ARGS_ASSERT_ANONYMISE_CV;
-
-    if (val && isGV(val) && isGV_with_GP(val) && (cv = GvCV(val))) {
-       if ((SV *)CvGV(cv) == val) {
-           GV *anongv;
-
-           if (stash) {
-               SV *gvname = newSVhek(stash);
-               sv_catpvs(gvname, "::__ANON__");
-               anongv = gv_fetchsv(gvname, GV_ADDMULTI, SVt_PVCV);
-               SvREFCNT_dec(gvname);
-           } else {
-               anongv = gv_fetchpvs("__ANON__::__ANON__", GV_ADDMULTI,
-                                    SVt_PVCV);
-           }
-           CvGV(cv) = anongv;
-           CvANON_on(cv);
-           return 1;
-       }
-    }
-    return 0;
-}
 
 void
 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
@@ -1661,22 +1635,6 @@ S_hfreeentries(pTHX_ HV *hv)
     if (!orig_array)
        return;
 
-    if (HvNAME(hv) && orig_array != NULL) {
-       /* symbol table: make all the contained subs ANON */
-       STRLEN i;
-       XPVHV *xhv = (XPVHV*)SvANY(hv);
-
-       for (i = 0; i <= xhv->xhv_max; i++) {
-           HE *entry = (HvARRAY(hv))[i];
-           for (; entry; entry = HeNEXT(entry)) {
-               SV *val = HeVAL(entry);
-               /* we need to put the subs in the __ANON__ symtable, as
-                * this one is being cleared. */
-               anonymise_cv(NULL, val);
-           }
-       }
-    }
-
     if (SvOOK(hv)) {
        /* If the hash is actually a symbol table with a name, look after the
           name.  */
@@ -1708,25 +1666,43 @@ S_hfreeentries(pTHX_ HV *hv)
            HE *entry;
             struct mro_meta *meta;
            struct xpvhv_aux *iter = HvAUX(hv);
-           /* If there are weak references to this HV, we need to avoid
-              freeing them up here.  In particular we need to keep the AV
-              visible as what we're deleting might well have weak references
-              back to this HV, so the for loop below may well trigger
-              the removal of backreferences from this array.  */
+           /* weak references: if called from sv_clear(), the backrefs
+            * should already have been killed; if there are any left, its
+            * because we're doing hv_clear() or hv_undef(), and the HV
+            * will continue to live.
+            * Because while freeing the entries we fake up a NULL HvARRAY
+            * (and hence HvAUX), we need to store the backref array
+            * somewhere else; but it still needs to be visible in case
+            * any the things we free happen to call sv_del_backref().
+            * We do this by storing it in magic instead.
+            * If, during the entry freeing, a destructor happens to add
+            * a new weak backref, then sv_add_backref will look in both
+            * places (magic in HvAUX) for the AV, but will create a new
+            * AV in HvAUX if it can't find one (if it finds it in magic,
+            * it moves it back into HvAUX. So at the end of the iteration
+            * we have to allow for this. */
+
 
            if (iter->xhv_backreferences) {
-               /* So donate them to regular backref magic to keep them safe.
-                  The sv_magic will increase the reference count of the AV,
-                  so we need to drop it first. */
-               SvREFCNT_dec(iter->xhv_backreferences);
-               if (AvFILLp(iter->xhv_backreferences) == -1) {
-                   /* Turns out that the array is empty. Just free it.  */
+               if (SvTYPE(iter->xhv_backreferences) == SVt_PVAV) {
+                   /* The sv_magic will increase the reference count of the AV,
+                      so we need to drop it first. */
                    SvREFCNT_dec(iter->xhv_backreferences);
+                   if (AvFILLp(iter->xhv_backreferences) == -1) {
+                       /* Turns out that the array is empty. Just free it.  */
+                       SvREFCNT_dec(iter->xhv_backreferences);
 
-               } else {
-                   sv_magic(MUTABLE_SV(hv),
-                            MUTABLE_SV(iter->xhv_backreferences),
-                            PERL_MAGIC_backref, NULL, 0);
+                   } else {
+                       sv_magic(MUTABLE_SV(hv),
+                                MUTABLE_SV(iter->xhv_backreferences),
+                                PERL_MAGIC_backref, NULL, 0);
+                   }
+               }
+               else {
+                   MAGIC *mg;
+                   sv_magic(MUTABLE_SV(hv), NULL, PERL_MAGIC_backref, NULL, 0);
+                   mg = mg_find(MUTABLE_SV(hv), PERL_MAGIC_backref);
+                   mg->mg_obj = (SV*)iter->xhv_backreferences;
                }
                iter->xhv_backreferences = NULL;
            }
@@ -1764,7 +1740,7 @@ S_hfreeentries(pTHX_ HV *hv)
        }
 
        /* make everyone else think the array is empty, so that the destructors
-        * called for freed entries can't recusively mess with us */
+        * called for freed entries can't recursively mess with us */
        HvARRAY(hv) = NULL;
        ((XPVHV*) SvANY(hv))->xhv_keys = 0;
 
@@ -2081,7 +2057,8 @@ Perl_hv_kill_backrefs(pTHX_ HV *hv) {
     if (av) {
        HvAUX(hv)->xhv_backreferences = 0;
        Perl_sv_kill_backrefs(aTHX_ MUTABLE_SV(hv), av);
-       SvREFCNT_dec(av);
+       if (SvTYPE(av) == SVt_PVAV)
+           SvREFCNT_dec(av);
     }
 }
 
@@ -2561,7 +2538,7 @@ S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
 
        xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
        if (!next) {                    /* initial entry? */
-       } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
+       } else if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
                hsplit(PL_strtab);
        }
     }