Shift negative klen/flags games from hv_store_common out to hv_store
[p5sagit/p5-mst-13.2.git] / hv.c
1 /*    hv.c
2  *
3  *    Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4  *    2000, 2001, 2002, 2003, by Larry Wall and others
5  *
6  *    You may distribute under the terms of either the GNU General Public
7  *    License or the Artistic License, as specified in the README file.
8  *
9  */
10
11 /*
12  * "I sit beside the fire and think of all that I have seen."  --Bilbo
13  */
14
15 /* 
16 =head1 Hash Manipulation Functions
17 */
18
19 #include "EXTERN.h"
20 #define PERL_IN_HV_C
21 #define PERL_HASH_INTERNAL_ACCESS
22 #include "perl.h"
23
24 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
25
26 STATIC HE*
27 S_new_he(pTHX)
28 {
29     HE* he;
30     LOCK_SV_MUTEX;
31     if (!PL_he_root)
32         more_he();
33     he = PL_he_root;
34     PL_he_root = HeNEXT(he);
35     UNLOCK_SV_MUTEX;
36     return he;
37 }
38
39 STATIC void
40 S_del_he(pTHX_ HE *p)
41 {
42     LOCK_SV_MUTEX;
43     HeNEXT(p) = (HE*)PL_he_root;
44     PL_he_root = p;
45     UNLOCK_SV_MUTEX;
46 }
47
48 STATIC void
49 S_more_he(pTHX)
50 {
51     register HE* he;
52     register HE* heend;
53     XPV *ptr;
54     New(54, ptr, 1008/sizeof(XPV), XPV);
55     ptr->xpv_pv = (char*)PL_he_arenaroot;
56     PL_he_arenaroot = ptr;
57
58     he = (HE*)ptr;
59     heend = &he[1008 / sizeof(HE) - 1];
60     PL_he_root = ++he;
61     while (he < heend) {
62         HeNEXT(he) = (HE*)(he + 1);
63         he++;
64     }
65     HeNEXT(he) = 0;
66 }
67
68 #ifdef PURIFY
69
70 #define new_HE() (HE*)safemalloc(sizeof(HE))
71 #define del_HE(p) safefree((char*)p)
72
73 #else
74
75 #define new_HE() new_he()
76 #define del_HE(p) del_he(p)
77
78 #endif
79
80 STATIC HEK *
81 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
82 {
83     char *k;
84     register HEK *hek;
85
86     New(54, k, HEK_BASESIZE + len + 2, char);
87     hek = (HEK*)k;
88     Copy(str, HEK_KEY(hek), len, char);
89     HEK_KEY(hek)[len] = 0;
90     HEK_LEN(hek) = len;
91     HEK_HASH(hek) = hash;
92     HEK_FLAGS(hek) = (unsigned char)flags;
93     return hek;
94 }
95
96 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
97  * for tied hashes */
98
99 void
100 Perl_free_tied_hv_pool(pTHX)
101 {
102     HE *ohe;
103     HE *he = PL_hv_fetch_ent_mh;
104     while (he) {
105         Safefree(HeKEY_hek(he));
106         ohe = he;
107         he = HeNEXT(he);
108         del_HE(ohe);
109     }
110     PL_hv_fetch_ent_mh = Nullhe;
111 }
112
113 #if defined(USE_ITHREADS)
114 HE *
115 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
116 {
117     HE *ret;
118
119     if (!e)
120         return Nullhe;
121     /* look for it in the table first */
122     ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
123     if (ret)
124         return ret;
125
126     /* create anew and remember what it is */
127     ret = new_HE();
128     ptr_table_store(PL_ptr_table, e, ret);
129
130     HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
131     if (HeKLEN(e) == HEf_SVKEY) {
132         char *k;
133         New(54, k, HEK_BASESIZE + sizeof(SV*), char);
134         HeKEY_hek(ret) = (HEK*)k;
135         HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
136     }
137     else if (shared)
138         HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
139                                          HeKFLAGS(e));
140     else
141         HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
142                                         HeKFLAGS(e));
143     HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
144     return ret;
145 }
146 #endif  /* USE_ITHREADS */
147
148 static void
149 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
150                 const char *msg)
151 {
152     SV *sv = sv_newmortal(), *esv = sv_newmortal();
153     if (!(flags & HVhek_FREEKEY)) {
154         sv_setpvn(sv, key, klen);
155     }
156     else {
157         /* Need to free saved eventually assign to mortal SV */
158         /* XXX is this line an error ???:  SV *sv = sv_newmortal(); */
159         sv_usepvn(sv, (char *) key, klen);
160     }
161     if (flags & HVhek_UTF8) {
162         SvUTF8_on(sv);
163     }
164     Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
165     Perl_croak(aTHX_ SvPVX(esv), sv);
166 }
167
168 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
169  * contains an SV* */
170
171 /*
172 =for apidoc hv_fetch
173
174 Returns the SV which corresponds to the specified key in the hash.  The
175 C<klen> is the length of the key.  If C<lval> is set then the fetch will be
176 part of a store.  Check that the return value is non-null before
177 dereferencing it to an C<SV*>.
178
179 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
180 information on how to use this function on tied hashes.
181
182 =cut
183 */
184
185 #define HV_FETCH_LVALUE  0x01
186 #define HV_FETCH_JUST_SV 0x02
187
188 SV**
189 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
190 {
191     HE *hek;
192     STRLEN klen;
193     int flags;
194
195     if (klen_i32 < 0) {
196         klen = -klen_i32;
197         flags = HVhek_UTF8;
198     } else {
199         klen = klen_i32;
200         flags = 0;
201     }
202     hek = hv_fetch_common (hv, NULL, key, klen, flags,
203                            HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0), 0);
204     return hek ? &HeVAL(hek) : NULL;
205 }
206
207 /* returns an HE * structure with the all fields set */
208 /* note that hent_val will be a mortal sv for MAGICAL hashes */
209 /*
210 =for apidoc hv_fetch_ent
211
212 Returns the hash entry which corresponds to the specified key in the hash.
213 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
214 if you want the function to compute it.  IF C<lval> is set then the fetch
215 will be part of a store.  Make sure the return value is non-null before
216 accessing it.  The return value when C<tb> is a tied hash is a pointer to a
217 static location, so be sure to make a copy of the structure if you need to
218 store it somewhere.
219
220 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
221 information on how to use this function on tied hashes.
222
223 =cut
224 */
225
226 HE *
227 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
228 {
229     return hv_fetch_common(hv, keysv, NULL, 0, 0, lval ? HV_FETCH_LVALUE : 0,
230                            hash);
231 }
232
233 HE *
234 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
235                   int flags, int action, register U32 hash)
236 {
237     register XPVHV* xhv;
238     register HE *entry;
239     SV *sv;
240     bool is_utf8;
241     const char *keysave;
242     int masked_flags;
243
244     if (!hv)
245         return 0;
246
247     if (keysv) {
248         key = SvPV(keysv, klen);
249         flags = 0;
250         is_utf8 = (SvUTF8(keysv) != 0);
251     } else {
252         is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
253     }
254     keysave = key;
255
256     if (SvRMAGICAL(hv)) {
257         if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
258             sv = sv_newmortal();
259
260             /* XXX should be able to skimp on the HE/HEK here when
261                HV_FETCH_JUST_SV is true.  */
262
263             if (!keysv) {
264                 keysv = newSVpvn(key, klen);
265                 if (is_utf8) {
266                     SvUTF8_on(keysv);
267                 }
268             } else {
269                 keysv = newSVsv(keysv);
270             }
271             mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
272
273
274             /* grab a fake HE/HEK pair from the pool or make a new one */
275             entry = PL_hv_fetch_ent_mh;
276             if (entry)
277                 PL_hv_fetch_ent_mh = HeNEXT(entry);
278             else {
279                 char *k;
280                 entry = new_HE();
281                 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
282                 HeKEY_hek(entry) = (HEK*)k;
283             }
284             HeNEXT(entry) = Nullhe;
285             HeSVKEY_set(entry, keysv);
286             HeVAL(entry) = sv;
287             sv_upgrade(sv, SVt_PVLV);
288             LvTYPE(sv) = 'T';
289             LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
290
291             /* XXX remove at some point? */
292             if (flags & HVhek_FREEKEY)
293                 Safefree(key);
294
295             return entry;
296         }
297 #ifdef ENV_IS_CASELESS
298         else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
299             U32 i;
300             for (i = 0; i < klen; ++i)
301                 if (isLOWER(key[i])) {
302                     SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
303                     (void)strupr(SvPVX(nkeysv));
304                     entry = hv_fetch_common(hv, nkeysv, NULL, 0, 0, 0);
305                     if (!entry && (action & HV_FETCH_LVALUE))
306                         entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
307
308                     /* XXX remove at some point? */
309                     if (flags & HVhek_FREEKEY)
310                         Safefree(key);
311
312                     return entry;
313                 }
314         }
315 #endif
316     }
317
318     xhv = (XPVHV*)SvANY(hv);
319     if (!xhv->xhv_array /* !HvARRAY(hv) */) {
320         if ((action & HV_FETCH_LVALUE)
321 #ifdef DYNAMIC_ENV_FETCH  /* if it's an %ENV lookup, we may get it on the fly */
322                  || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
323 #endif
324                                                                   )
325             Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
326                  PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
327                  char);
328         else {
329             /* XXX remove at some point? */
330             if (flags & HVhek_FREEKEY)
331                 Safefree(key);
332
333             return 0;
334         }
335     }
336
337     if (is_utf8) {
338         int oldflags = flags;
339         key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
340         if (is_utf8)
341             flags |= HVhek_UTF8;
342         else
343             flags &= ~HVhek_UTF8;
344         if (key != keysave)
345             flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
346         if (oldflags & HVhek_FREEKEY)
347             Safefree(keysave);
348
349     }
350
351     if (HvREHASH(hv)) {
352         PERL_HASH_INTERNAL(hash, key, klen);
353         /* Yes, you do need this even though you are not "storing" because
354            you can flip the flags below if doing an lval lookup.  (And that
355            was put in to give the semantics Andreas was expecting.)  */
356         flags |= HVhek_REHASH;
357     } else if (!hash) {
358         if (keysv && (SvIsCOW_shared_hash(keysv))) {
359             hash = SvUVX(keysv);
360         } else {
361             PERL_HASH(hash, key, klen);
362         }
363     }
364
365     masked_flags = (flags & HVhek_MASK);
366
367     /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
368     entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
369     for (; entry; entry = HeNEXT(entry)) {
370         if (HeHASH(entry) != hash)              /* strings can't be equal */
371             continue;
372         if (HeKLEN(entry) != (I32)klen)
373             continue;
374         if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen))        /* is this it? */
375             continue;
376         if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
377             continue;
378         if ((action & HV_FETCH_LVALUE) && HeKFLAGS(entry) != masked_flags) {
379             /* We match if HVhek_UTF8 bit in our flags and hash key's match.
380                But if entry was set previously with HVhek_WASUTF8 and key now
381                doesn't (or vice versa) then we should change the key's flag,
382                as this is assignment.  */
383             if (HvSHAREKEYS(hv)) {
384                 /* Need to swap the key we have for a key with the flags we
385                    need. As keys are shared we can't just write to the flag,
386                    so we share the new one, unshare the old one.  */
387                 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
388                 unshare_hek (HeKEY_hek(entry));
389                 HeKEY_hek(entry) = new_hek;
390             }
391             else
392                 HeKFLAGS(entry) = masked_flags;
393             if (masked_flags & HVhek_ENABLEHVKFLAGS)
394                 HvHASKFLAGS_on(hv);
395         }
396         /* if we find a placeholder, we pretend we haven't found anything */
397         if (HeVAL(entry) == &PL_sv_placeholder)
398             break;
399         if (flags & HVhek_FREEKEY)
400             Safefree(key);
401         return entry;
402     }
403 #ifdef DYNAMIC_ENV_FETCH  /* %ENV lookup?  If so, try to fetch the value now */
404     if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
405         unsigned long len;
406         char *env = PerlEnv_ENVgetenv_len(key,&len);
407         if (env) {
408             /* XXX remove once common API complete  */
409             if (!keysv) {
410                 nkeysv = sv_2mortal(newSVpvn(key,klen));
411             }
412
413             sv = newSVpvn(env,len);
414             SvTAINTED_on(sv);
415             if (flags & HVhek_FREEKEY)
416                 Safefree(key);
417             return hv_store_ent(hv,keysv,sv,hash);
418         }
419     }
420 #endif
421     if (!entry && SvREADONLY(hv)) {
422         S_hv_notallowed(aTHX_ flags, key, klen,
423                         "access disallowed key '%"SVf"' in"
424                         );
425     }
426     if (action & HV_FETCH_LVALUE) {
427         /* XXX remove once common API complete  */
428         if (!keysv) {
429             keysv = sv_2mortal(newSVpvn(key,klen));
430         }
431     }
432
433     if (flags & HVhek_FREEKEY)
434         Safefree(key);
435     if (action & HV_FETCH_LVALUE) {
436         /* gonna assign to this, so it better be there */
437         sv = NEWSV(61,0);
438         return hv_store_ent(hv,keysv,sv,hash);
439     }
440     return 0;
441 }
442
443 STATIC void
444 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
445 {
446     MAGIC *mg = SvMAGIC(hv);
447     *needs_copy = FALSE;
448     *needs_store = TRUE;
449     while (mg) {
450         if (isUPPER(mg->mg_type)) {
451             *needs_copy = TRUE;
452             switch (mg->mg_type) {
453             case PERL_MAGIC_tied:
454             case PERL_MAGIC_sig:
455                 *needs_store = FALSE;
456             }
457         }
458         mg = mg->mg_moremagic;
459     }
460 }
461
462 /*
463 =for apidoc hv_store
464
465 Stores an SV in a hash.  The hash key is specified as C<key> and C<klen> is
466 the length of the key.  The C<hash> parameter is the precomputed hash
467 value; if it is zero then Perl will compute it.  The return value will be
468 NULL if the operation failed or if the value did not need to be actually
469 stored within the hash (as in the case of tied hashes).  Otherwise it can
470 be dereferenced to get the original C<SV*>.  Note that the caller is
471 responsible for suitably incrementing the reference count of C<val> before
472 the call, and decrementing it if the function returned NULL.  Effectively
473 a successful hv_store takes ownership of one reference to C<val>.  This is
474 usually what you want; a newly created SV has a reference count of one, so
475 if all your code does is create SVs then store them in a hash, hv_store
476 will own the only reference to the new SV, and your code doesn't need to do
477 anything further to tidy up.  hv_store is not implemented as a call to
478 hv_store_ent, and does not create a temporary SV for the key, so if your
479 key data is not already in SV form then use hv_store in preference to
480 hv_store_ent.
481
482 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
483 information on how to use this function on tied hashes.
484
485 =cut
486 */
487
488 SV**
489 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
490 {
491     HE *hek;
492     STRLEN klen;
493     int flags;
494
495     if (klen_i32 < 0) {
496         klen = -klen_i32;
497         flags = HVhek_UTF8;
498     } else {
499         klen = klen_i32;
500         flags = 0;
501     }
502     hek = hv_store_common (hv, NULL, key, klen, flags, val, 0);
503     return hek ? &HeVAL(hek) : NULL;
504 }
505
506 SV**
507 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
508                  register U32 hash, int flags)
509 {
510     HE *hek = hv_store_common (hv, NULL, key, klen, flags, val, hash);
511     return hek ? &HeVAL(hek) : NULL;
512 }
513
514 /*
515 =for apidoc hv_store_ent
516
517 Stores C<val> in a hash.  The hash key is specified as C<key>.  The C<hash>
518 parameter is the precomputed hash value; if it is zero then Perl will
519 compute it.  The return value is the new hash entry so created.  It will be
520 NULL if the operation failed or if the value did not need to be actually
521 stored within the hash (as in the case of tied hashes).  Otherwise the
522 contents of the return value can be accessed using the C<He?> macros
523 described here.  Note that the caller is responsible for suitably
524 incrementing the reference count of C<val> before the call, and
525 decrementing it if the function returned NULL.  Effectively a successful
526 hv_store_ent takes ownership of one reference to C<val>.  This is
527 usually what you want; a newly created SV has a reference count of one, so
528 if all your code does is create SVs then store them in a hash, hv_store
529 will own the only reference to the new SV, and your code doesn't need to do
530 anything further to tidy up.  Note that hv_store_ent only reads the C<key>;
531 unlike C<val> it does not take ownership of it, so maintaining the correct
532 reference count on C<key> is entirely the caller's responsibility.  hv_store
533 is not implemented as a call to hv_store_ent, and does not create a temporary
534 SV for the key, so if your key data is not already in SV form then use
535 hv_store in preference to hv_store_ent.
536
537 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
538 information on how to use this function on tied hashes.
539
540 =cut
541 */
542
543 HE *
544 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
545 {
546   return hv_store_common(hv, keysv, NULL, 0, 0, val, hash);
547 }
548
549 HE *
550 S_hv_store_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
551                   int flags, SV *val, U32 hash)
552 {
553     XPVHV* xhv;
554     U32 n_links;
555     HE *entry;
556     HE **oentry;
557     bool is_utf8;
558     const char *keysave;
559     int masked_flags;
560
561     if (!hv)
562         return 0;
563
564     if (keysv) {
565         key = SvPV(keysv, klen);
566         flags = 0;
567         is_utf8 = (SvUTF8(keysv) != 0);
568     } else {
569         is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
570     }
571     keysave = key;
572
573     xhv = (XPVHV*)SvANY(hv);
574     if (SvMAGICAL(hv)) {
575         bool needs_copy;
576         bool needs_store;
577         hv_magic_check (hv, &needs_copy, &needs_store);
578         if (needs_copy) {
579             bool save_taint = PL_tainted;       
580             if (keysv || is_utf8) {
581                 if (!keysv) {
582                     keysv = newSVpvn(key, klen);
583                     SvUTF8_on(keysv);
584                 }
585                 if (PL_tainting)
586                     PL_tainted = SvTAINTED(keysv);
587                 keysv = sv_2mortal(newSVsv(keysv));
588                 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
589             } else {
590                 mg_copy((SV*)hv, val, key, klen);
591             }
592
593             TAINT_IF(save_taint);
594             if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
595                 if (flags & HVhek_FREEKEY)
596                     Safefree(key);
597                 return Nullhe;
598             }
599 #ifdef ENV_IS_CASELESS
600             else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
601                 key = savepvn(key,klen);
602                 key = (const char*)strupr((char*)key);
603                 hash = 0;
604
605                 if (flags & HVhek_FREEKEY)
606                     Safefree(keysave);
607                 keysave = key;
608             }
609 #endif
610         }
611     }
612
613
614     if (flags & HVhek_PLACEHOLD) {
615         /* We have been requested to insert a placeholder. Currently
616            only Storable is allowed to do this.  */
617         val = &PL_sv_placeholder;
618     }
619
620     if (is_utf8) {
621         key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
622
623         if (flags & HVhek_FREEKEY) {
624             /* This shouldn't happen if our caller does what we expect,
625                but strictly the API allows it.  */
626             Safefree(keysave);
627         }
628
629         if (is_utf8)
630             flags |= HVhek_UTF8;
631         else
632             flags &= ~HVhek_UTF8;
633         if (key != keysave)
634             flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
635         HvHASKFLAGS_on((SV*)hv);
636     }
637
638     if (HvREHASH(hv)) {
639         /* We don't have a pointer to the hv, so we have to replicate the
640            flag into every HEK, so that hv_iterkeysv can see it.  */
641         flags |= HVhek_REHASH;
642         PERL_HASH_INTERNAL(hash, key, klen);
643     } else if (!hash) {
644         if (keysv && SvIsCOW_shared_hash(keysv)) {
645             hash = SvUVX(keysv);
646         } else {
647             PERL_HASH(hash, key, klen);
648         }
649     }
650
651     masked_flags = (flags & HVhek_MASK);
652
653     if (!xhv->xhv_array /* !HvARRAY(hv) */)
654         Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
655              PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
656              char);
657
658     /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
659     oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
660     n_links = 0;
661     entry = *oentry;
662     for (; entry; ++n_links, entry = HeNEXT(entry)) {
663         if (HeHASH(entry) != hash)              /* strings can't be equal */
664             continue;
665         if (HeKLEN(entry) != (I32)klen)
666             continue;
667         if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen))        /* is this it? */
668             continue;
669         if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
670             continue;
671         if (HeVAL(entry) == &PL_sv_placeholder)
672             xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
673         else
674             SvREFCNT_dec(HeVAL(entry));
675         HeVAL(entry) = val;
676         if (val == &PL_sv_placeholder)
677             xhv->xhv_placeholders++;
678
679         if (HeKFLAGS(entry) != masked_flags) {
680             /* We match if HVhek_UTF8 bit in our flags and hash key's match.
681                But if entry was set previously with HVhek_WASUTF8 and key now
682                doesn't (or vice versa) then we should change the key's flag,
683                as this is assignment.  */
684             if (HvSHAREKEYS(hv)) {
685                 /* Need to swap the key we have for a key with the flags we
686                    need. As keys are shared we can't just write to the flag,
687                    so we share the new one, unshare the old one.  */
688                 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
689                 unshare_hek (HeKEY_hek(entry));
690                 HeKEY_hek(entry) = new_hek;
691             }
692             else
693                 HeKFLAGS(entry) = masked_flags;
694         }
695         if (flags & HVhek_FREEKEY)
696             Safefree(key);
697         return entry;
698     }
699
700     if (SvREADONLY(hv)) {
701         S_hv_notallowed(aTHX_ flags, key, klen,
702                         "access disallowed key '%"SVf"' to"
703                         );
704     }
705
706     entry = new_HE();
707     /* share_hek_flags will do the free for us.  This might be considered
708        bad API design.  */
709     if (HvSHAREKEYS(hv))
710         HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
711     else                                       /* gotta do the real thing */
712         HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
713     HeVAL(entry) = val;
714     HeNEXT(entry) = *oentry;
715     *oentry = entry;
716
717     if (val == &PL_sv_placeholder)
718         xhv->xhv_placeholders++;
719
720     xhv->xhv_keys++; /* HvKEYS(hv)++ */
721     if (!n_links) {                             /* initial entry? */
722         xhv->xhv_fill++; /* HvFILL(hv)++ */
723     } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
724                || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
725         /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
726            splits on a rehashed hash, as we're not going to split it again,
727            and if someone is lucky (evil) enough to get all the keys in one
728            list they could exhaust our memory as we repeatedly double the
729            number of buckets on every entry. Linear search feels a less worse
730            thing to do.  */
731         hsplit(hv);
732     }
733
734     return entry;
735 }
736
737 /*
738 =for apidoc hv_delete
739
740 Deletes a key/value pair in the hash.  The value SV is removed from the
741 hash and returned to the caller.  The C<klen> is the length of the key.
742 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
743 will be returned.
744
745 =cut
746 */
747
748 SV *
749 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
750 {
751     STRLEN klen;
752     int k_flags = 0;
753
754     if (klen_i32 < 0) {
755         klen = -klen_i32;
756         k_flags |= HVhek_UTF8;
757     } else {
758         klen = klen_i32;
759     }
760     return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
761 }
762
763 /*
764 =for apidoc hv_delete_ent
765
766 Deletes a key/value pair in the hash.  The value SV is removed from the
767 hash and returned to the caller.  The C<flags> value will normally be zero;
768 if set to G_DISCARD then NULL will be returned.  C<hash> can be a valid
769 precomputed hash value, or 0 to ask for it to be computed.
770
771 =cut
772 */
773
774 SV *
775 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
776 {
777     return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
778 }
779
780 SV *
781 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
782                    int k_flags, I32 d_flags, U32 hash)
783 {
784     register XPVHV* xhv;
785     register I32 i;
786     register HE *entry;
787     register HE **oentry;
788     SV *sv;
789     bool is_utf8;
790     const char *keysave;
791     int masked_flags;
792
793     if (!hv)
794         return Nullsv;
795
796     if (keysv) {
797         key = SvPV(keysv, klen);
798         k_flags = 0;
799         is_utf8 = (SvUTF8(keysv) != 0);
800     } else {
801         is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
802     }
803     keysave = key;
804
805     if (SvRMAGICAL(hv)) {
806         bool needs_copy;
807         bool needs_store;
808         hv_magic_check (hv, &needs_copy, &needs_store);
809
810         if (needs_copy) {
811             entry = hv_fetch_common(hv, keysv, key, klen,
812                                     k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
813                                     hash);
814             sv = entry ? HeVAL(entry) : NULL;
815             if (sv) {
816                 if (SvMAGICAL(sv)) {
817                     mg_clear(sv);
818                 }
819                 if (!needs_store) {
820                     if (mg_find(sv, PERL_MAGIC_tiedelem)) {
821                         /* No longer an element */
822                         sv_unmagic(sv, PERL_MAGIC_tiedelem);
823                         return sv;
824                     }           
825                     return Nullsv;              /* element cannot be deleted */
826                 }
827             }
828 #ifdef ENV_IS_CASELESS
829             else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
830                 /* XXX This code isn't UTF8 clean.  */
831                 keysv = sv_2mortal(newSVpvn(key,klen));
832                 keysave = key = strupr(SvPVX(keysv));
833                 is_utf8 = 0;
834                 k_flags = 0;
835                 hash = 0;
836             }
837 #endif
838         }
839     }
840     xhv = (XPVHV*)SvANY(hv);
841     if (!xhv->xhv_array /* !HvARRAY(hv) */)
842         return Nullsv;
843
844     if (is_utf8) {
845         key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
846
847         if (k_flags & HVhek_FREEKEY) {
848             /* This shouldn't happen if our caller does what we expect,
849                but strictly the API allows it.  */
850             Safefree(keysave);
851         }
852
853         if (is_utf8)
854             k_flags |= HVhek_UTF8;
855         else
856             k_flags &= ~HVhek_UTF8;
857         if (key != keysave)
858             k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
859         HvHASKFLAGS_on((SV*)hv);
860     }
861
862     if (HvREHASH(hv)) {
863         PERL_HASH_INTERNAL(hash, key, klen);
864     } else if (!hash) {
865         if (keysv && (SvIsCOW_shared_hash(keysv))) {
866             hash = SvUVX(keysv);
867         } else {
868             PERL_HASH(hash, key, klen);
869         }
870         PERL_HASH(hash, key, klen);
871     }
872
873     masked_flags = (k_flags & HVhek_MASK);
874
875     /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
876     oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
877     entry = *oentry;
878     i = 1;
879     for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
880         if (HeHASH(entry) != hash)              /* strings can't be equal */
881             continue;
882         if (HeKLEN(entry) != (I32)klen)
883             continue;
884         if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen))        /* is this it? */
885             continue;
886         if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
887             continue;
888         if (k_flags & HVhek_FREEKEY)
889             Safefree(key);
890
891         /* if placeholder is here, it's already been deleted.... */
892         if (HeVAL(entry) == &PL_sv_placeholder)
893         {
894             if (SvREADONLY(hv))
895                 return Nullsv; /* if still SvREADONLY, leave it deleted. */
896
897            /* okay, really delete the placeholder. */
898            *oentry = HeNEXT(entry);
899            if (i && !*oentry)
900                xhv->xhv_fill--; /* HvFILL(hv)-- */
901            if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
902                HvLAZYDEL_on(hv);
903            else
904                hv_free_ent(hv, entry);
905            xhv->xhv_keys--; /* HvKEYS(hv)-- */
906            if (xhv->xhv_keys == 0)
907                HvHASKFLAGS_off(hv);
908            xhv->xhv_placeholders--;
909            return Nullsv;
910         }
911         else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
912             S_hv_notallowed(aTHX_ k_flags, key, klen,
913                             "delete readonly key '%"SVf"' from"
914                             );
915         }
916
917         if (d_flags & G_DISCARD)
918             sv = Nullsv;
919         else {
920             sv = sv_2mortal(HeVAL(entry));
921             HeVAL(entry) = &PL_sv_placeholder;
922         }
923
924         /*
925          * If a restricted hash, rather than really deleting the entry, put
926          * a placeholder there. This marks the key as being "approved", so
927          * we can still access via not-really-existing key without raising
928          * an error.
929          */
930         if (SvREADONLY(hv)) {
931             HeVAL(entry) = &PL_sv_placeholder;
932             /* We'll be saving this slot, so the number of allocated keys
933              * doesn't go down, but the number placeholders goes up */
934             xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
935         } else {
936             *oentry = HeNEXT(entry);
937             if (i && !*oentry)
938                 xhv->xhv_fill--; /* HvFILL(hv)-- */
939             if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
940                 HvLAZYDEL_on(hv);
941             else
942                 hv_free_ent(hv, entry);
943             xhv->xhv_keys--; /* HvKEYS(hv)-- */
944             if (xhv->xhv_keys == 0)
945                 HvHASKFLAGS_off(hv);
946         }
947         return sv;
948     }
949     if (SvREADONLY(hv)) {
950         S_hv_notallowed(aTHX_ k_flags, key, klen,
951                         "delete disallowed key '%"SVf"' from"
952                         );
953     }
954
955     if (k_flags & HVhek_FREEKEY)
956         Safefree(key);
957     return Nullsv;
958 }
959
960 /*
961 =for apidoc hv_exists
962
963 Returns a boolean indicating whether the specified hash key exists.  The
964 C<klen> is the length of the key.
965
966 =cut
967 */
968
969 bool
970 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
971 {
972     STRLEN klen;
973     int flags;
974
975     if (klen_i32 < 0) {
976         klen = -klen_i32;
977         flags = HVhek_UTF8;
978     } else {
979         klen = klen_i32;
980         flags = 0;
981     }
982     return hv_exists_common(hv, NULL, key, klen, flags, 0);
983 }
984
985 /*
986 =for apidoc hv_exists_ent
987
988 Returns a boolean indicating whether the specified hash key exists. C<hash>
989 can be a valid precomputed hash value, or 0 to ask for it to be
990 computed.
991
992 =cut
993 */
994
995 bool
996 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
997 {
998     return hv_exists_common(hv, keysv, NULL, 0, 0, hash);
999 }
1000
1001 bool
1002 S_hv_exists_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
1003                    int k_flags, U32 hash)
1004 {
1005     register XPVHV* xhv;
1006     register HE *entry;
1007     SV *sv;
1008     bool is_utf8;
1009     const char *keysave;
1010     int masked_flags;
1011
1012     if (!hv)
1013         return 0;
1014
1015     if (keysv) {
1016         key = SvPV(keysv, klen);
1017         k_flags = 0;
1018         is_utf8 = (SvUTF8(keysv) != 0);
1019     } else {
1020         is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
1021     }
1022     keysave = key;
1023
1024     if (SvRMAGICAL(hv)) {
1025         if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1026             SV* svret;
1027
1028             if (keysv || is_utf8) {
1029                 if (!keysv) {
1030                     keysv = newSVpvn(key, klen);
1031                     SvUTF8_on(keysv);
1032                 } else {
1033                     keysv = newSVsv(keysv);
1034                 }
1035                 key = (char *)sv_2mortal(keysv);
1036                 klen = HEf_SVKEY;
1037             }
1038
1039             /* I don't understand why hv_exists_ent has svret and sv,
1040                whereas hv_exists only had one.  */
1041             svret = sv_newmortal();
1042             sv = sv_newmortal();
1043             mg_copy((SV*)hv, sv, key, klen);
1044             magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1045             return (bool)SvTRUE(svret);
1046         }
1047 #ifdef ENV_IS_CASELESS
1048         else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1049             /* XXX This code isn't UTF8 clean.  */
1050             keysv = sv_2mortal(newSVpvn(key,klen));
1051             keysave = key = strupr(SvPVX(keysv));
1052             is_utf8 = 0;
1053             hash = 0;
1054         }
1055 #endif
1056     }
1057
1058     xhv = (XPVHV*)SvANY(hv);
1059 #ifndef DYNAMIC_ENV_FETCH
1060     if (!xhv->xhv_array /* !HvARRAY(hv) */)
1061         return 0;
1062 #endif
1063
1064     if (is_utf8) {
1065         key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1066
1067         if (k_flags & HVhek_FREEKEY) {
1068             /* This shouldn't happen if our caller does what we expect,
1069                but strictly the API allows it.  */
1070             Safefree(keysave);
1071         }
1072
1073         if (is_utf8)
1074             k_flags |= HVhek_UTF8;
1075         else
1076             k_flags &= ~HVhek_UTF8;
1077         if (key != keysave)
1078             k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1079     }
1080
1081     if (HvREHASH(hv)) {
1082         PERL_HASH_INTERNAL(hash, key, klen);
1083     } else if (!hash)
1084         PERL_HASH(hash, key, klen);
1085
1086     masked_flags = (k_flags & HVhek_MASK);
1087
1088 #ifdef DYNAMIC_ENV_FETCH
1089     if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1090     else
1091 #endif
1092     /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1093     entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1094     for (; entry; entry = HeNEXT(entry)) {
1095         if (HeHASH(entry) != hash)              /* strings can't be equal */
1096             continue;
1097         if (HeKLEN(entry) != (I32)klen)
1098             continue;
1099         if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen))        /* is this it? */
1100             continue;
1101         if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1102             continue;
1103         if (k_flags & HVhek_FREEKEY)
1104             Safefree(key);
1105         /* If we find the key, but the value is a placeholder, return false. */
1106         if (HeVAL(entry) == &PL_sv_placeholder)
1107             return FALSE;
1108         return TRUE;
1109     }
1110 #ifdef DYNAMIC_ENV_FETCH  /* is it out there? */
1111     if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1112         unsigned long len;
1113         char *env = PerlEnv_ENVgetenv_len(key,&len);
1114         if (env) {
1115             sv = newSVpvn(env,len);
1116             SvTAINTED_on(sv);
1117             (void)hv_store_ent(hv,keysv,sv,hash);
1118             if (k_flags & HVhek_FREEKEY)
1119                 Safefree(key);
1120             return TRUE;
1121         }
1122     }
1123 #endif
1124     if (k_flags & HVhek_FREEKEY)
1125         Safefree(key);
1126     return FALSE;
1127 }
1128
1129
1130 STATIC void
1131 S_hsplit(pTHX_ HV *hv)
1132 {
1133     register XPVHV* xhv = (XPVHV*)SvANY(hv);
1134     I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1135     register I32 newsize = oldsize * 2;
1136     register I32 i;
1137     register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1138     register HE **aep;
1139     register HE **bep;
1140     register HE *entry;
1141     register HE **oentry;
1142     int longest_chain = 0;
1143     int was_shared;
1144
1145     PL_nomemok = TRUE;
1146 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1147     Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1148     if (!a) {
1149       PL_nomemok = FALSE;
1150       return;
1151     }
1152 #else
1153     New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1154     if (!a) {
1155       PL_nomemok = FALSE;
1156       return;
1157     }
1158     Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1159     if (oldsize >= 64) {
1160         offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1161                         PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1162     }
1163     else
1164         Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1165 #endif
1166
1167     PL_nomemok = FALSE;
1168     Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char);     /* zero 2nd half*/
1169     xhv->xhv_max = --newsize;   /* HvMAX(hv) = --newsize */
1170     xhv->xhv_array = a;         /* HvARRAY(hv) = a */
1171     aep = (HE**)a;
1172
1173     for (i=0; i<oldsize; i++,aep++) {
1174         int left_length = 0;
1175         int right_length = 0;
1176
1177         if (!*aep)                              /* non-existent */
1178             continue;
1179         bep = aep+oldsize;
1180         for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1181             if ((HeHASH(entry) & newsize) != (U32)i) {
1182                 *oentry = HeNEXT(entry);
1183                 HeNEXT(entry) = *bep;
1184                 if (!*bep)
1185                     xhv->xhv_fill++; /* HvFILL(hv)++ */
1186                 *bep = entry;
1187                 right_length++;
1188                 continue;
1189             }
1190             else {
1191                 oentry = &HeNEXT(entry);
1192                 left_length++;
1193             }
1194         }
1195         if (!*aep)                              /* everything moved */
1196             xhv->xhv_fill--; /* HvFILL(hv)-- */
1197         /* I think we don't actually need to keep track of the longest length,
1198            merely flag if anything is too long. But for the moment while
1199            developing this code I'll track it.  */
1200         if (left_length > longest_chain)
1201             longest_chain = left_length;
1202         if (right_length > longest_chain)
1203             longest_chain = right_length;
1204     }
1205
1206
1207     /* Pick your policy for "hashing isn't working" here:  */
1208     if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked?  */
1209         || HvREHASH(hv)) {
1210         return;
1211     }
1212
1213     if (hv == PL_strtab) {
1214         /* Urg. Someone is doing something nasty to the string table.
1215            Can't win.  */
1216         return;
1217     }
1218
1219     /* Awooga. Awooga. Pathological data.  */
1220     /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1221       longest_chain, HvTOTALKEYS(hv), HvFILL(hv),  1+HvMAX(hv));*/
1222
1223     ++newsize;
1224     Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1225     was_shared = HvSHAREKEYS(hv);
1226
1227     xhv->xhv_fill = 0;
1228     HvSHAREKEYS_off(hv);
1229     HvREHASH_on(hv);
1230
1231     aep = (HE **) xhv->xhv_array;
1232
1233     for (i=0; i<newsize; i++,aep++) {
1234         entry = *aep;
1235         while (entry) {
1236             /* We're going to trash this HE's next pointer when we chain it
1237                into the new hash below, so store where we go next.  */
1238             HE *next = HeNEXT(entry);
1239             UV hash;
1240
1241             /* Rehash it */
1242             PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1243
1244             if (was_shared) {
1245                 /* Unshare it.  */
1246                 HEK *new_hek
1247                     = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1248                                      hash, HeKFLAGS(entry));
1249                 unshare_hek (HeKEY_hek(entry));
1250                 HeKEY_hek(entry) = new_hek;
1251             } else {
1252                 /* Not shared, so simply write the new hash in. */
1253                 HeHASH(entry) = hash;
1254             }
1255             /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1256             HEK_REHASH_on(HeKEY_hek(entry));
1257             /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1258
1259             /* Copy oentry to the correct new chain.  */
1260             bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1261             if (!*bep)
1262                     xhv->xhv_fill++; /* HvFILL(hv)++ */
1263             HeNEXT(entry) = *bep;
1264             *bep = entry;
1265
1266             entry = next;
1267         }
1268     }
1269     Safefree (xhv->xhv_array);
1270     xhv->xhv_array = a;         /* HvARRAY(hv) = a */
1271 }
1272
1273 void
1274 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1275 {
1276     register XPVHV* xhv = (XPVHV*)SvANY(hv);
1277     I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1278     register I32 newsize;
1279     register I32 i;
1280     register I32 j;
1281     register char *a;
1282     register HE **aep;
1283     register HE *entry;
1284     register HE **oentry;
1285
1286     newsize = (I32) newmax;                     /* possible truncation here */
1287     if (newsize != newmax || newmax <= oldsize)
1288         return;
1289     while ((newsize & (1 + ~newsize)) != newsize) {
1290         newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1291     }
1292     if (newsize < newmax)
1293         newsize *= 2;
1294     if (newsize < newmax)
1295         return;                                 /* overflow detection */
1296
1297     a = xhv->xhv_array; /* HvARRAY(hv) */
1298     if (a) {
1299         PL_nomemok = TRUE;
1300 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1301         Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1302         if (!a) {
1303           PL_nomemok = FALSE;
1304           return;
1305         }
1306 #else
1307         New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1308         if (!a) {
1309           PL_nomemok = FALSE;
1310           return;
1311         }
1312         Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1313         if (oldsize >= 64) {
1314             offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1315                             PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1316         }
1317         else
1318             Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1319 #endif
1320         PL_nomemok = FALSE;
1321         Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1322     }
1323     else {
1324         Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1325     }
1326     xhv->xhv_max = --newsize;   /* HvMAX(hv) = --newsize */
1327     xhv->xhv_array = a;         /* HvARRAY(hv) = a */
1328     if (!xhv->xhv_fill /* !HvFILL(hv) */)       /* skip rest if no entries */
1329         return;
1330
1331     aep = (HE**)a;
1332     for (i=0; i<oldsize; i++,aep++) {
1333         if (!*aep)                              /* non-existent */
1334             continue;
1335         for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1336             if ((j = (HeHASH(entry) & newsize)) != i) {
1337                 j -= i;
1338                 *oentry = HeNEXT(entry);
1339                 if (!(HeNEXT(entry) = aep[j]))
1340                     xhv->xhv_fill++; /* HvFILL(hv)++ */
1341                 aep[j] = entry;
1342                 continue;
1343             }
1344             else
1345                 oentry = &HeNEXT(entry);
1346         }
1347         if (!*aep)                              /* everything moved */
1348             xhv->xhv_fill--; /* HvFILL(hv)-- */
1349     }
1350 }
1351
1352 /*
1353 =for apidoc newHV
1354
1355 Creates a new HV.  The reference count is set to 1.
1356
1357 =cut
1358 */
1359
1360 HV *
1361 Perl_newHV(pTHX)
1362 {
1363     register HV *hv;
1364     register XPVHV* xhv;
1365
1366     hv = (HV*)NEWSV(502,0);
1367     sv_upgrade((SV *)hv, SVt_PVHV);
1368     xhv = (XPVHV*)SvANY(hv);
1369     SvPOK_off(hv);
1370     SvNOK_off(hv);
1371 #ifndef NODEFAULT_SHAREKEYS
1372     HvSHAREKEYS_on(hv);         /* key-sharing on by default */
1373 #endif
1374
1375     xhv->xhv_max    = 7;        /* HvMAX(hv) = 7 (start with 8 buckets) */
1376     xhv->xhv_fill   = 0;        /* HvFILL(hv) = 0 */
1377     xhv->xhv_pmroot = 0;        /* HvPMROOT(hv) = 0 */
1378     (void)hv_iterinit(hv);      /* so each() will start off right */
1379     return hv;
1380 }
1381
1382 HV *
1383 Perl_newHVhv(pTHX_ HV *ohv)
1384 {
1385     HV *hv = newHV();
1386     STRLEN hv_max, hv_fill;
1387
1388     if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1389         return hv;
1390     hv_max = HvMAX(ohv);
1391
1392     if (!SvMAGICAL((SV *)ohv)) {
1393         /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1394         STRLEN i;
1395         bool shared = !!HvSHAREKEYS(ohv);
1396         HE **ents, **oents = (HE **)HvARRAY(ohv);
1397         char *a;
1398         New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1399         ents = (HE**)a;
1400
1401         /* In each bucket... */
1402         for (i = 0; i <= hv_max; i++) {
1403             HE *prev = NULL, *ent = NULL, *oent = oents[i];
1404
1405             if (!oent) {
1406                 ents[i] = NULL;
1407                 continue;
1408             }
1409
1410             /* Copy the linked list of entries. */
1411             for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1412                 U32 hash   = HeHASH(oent);
1413                 char *key  = HeKEY(oent);
1414                 STRLEN len = HeKLEN(oent);
1415                 int flags  = HeKFLAGS(oent);
1416
1417                 ent = new_HE();
1418                 HeVAL(ent)     = newSVsv(HeVAL(oent));
1419                 HeKEY_hek(ent)
1420                     = shared ? share_hek_flags(key, len, hash, flags)
1421                              :  save_hek_flags(key, len, hash, flags);
1422                 if (prev)
1423                     HeNEXT(prev) = ent;
1424                 else
1425                     ents[i] = ent;
1426                 prev = ent;
1427                 HeNEXT(ent) = NULL;
1428             }
1429         }
1430
1431         HvMAX(hv)   = hv_max;
1432         HvFILL(hv)  = hv_fill;
1433         HvTOTALKEYS(hv)  = HvTOTALKEYS(ohv);
1434         HvARRAY(hv) = ents;
1435     }
1436     else {
1437         /* Iterate over ohv, copying keys and values one at a time. */
1438         HE *entry;
1439         I32 riter = HvRITER(ohv);
1440         HE *eiter = HvEITER(ohv);
1441
1442         /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1443         while (hv_max && hv_max + 1 >= hv_fill * 2)
1444             hv_max = hv_max / 2;
1445         HvMAX(hv) = hv_max;
1446
1447         hv_iterinit(ohv);
1448         while ((entry = hv_iternext_flags(ohv, 0))) {
1449             hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1450                            newSVsv(HeVAL(entry)), HeHASH(entry),
1451                            HeKFLAGS(entry));
1452         }
1453         HvRITER(ohv) = riter;
1454         HvEITER(ohv) = eiter;
1455     }
1456
1457     return hv;
1458 }
1459
1460 void
1461 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1462 {
1463     SV *val;
1464
1465     if (!entry)
1466         return;
1467     val = HeVAL(entry);
1468     if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1469         PL_sub_generation++;    /* may be deletion of method from stash */
1470     SvREFCNT_dec(val);
1471     if (HeKLEN(entry) == HEf_SVKEY) {
1472         SvREFCNT_dec(HeKEY_sv(entry));
1473         Safefree(HeKEY_hek(entry));
1474     }
1475     else if (HvSHAREKEYS(hv))
1476         unshare_hek(HeKEY_hek(entry));
1477     else
1478         Safefree(HeKEY_hek(entry));
1479     del_HE(entry);
1480 }
1481
1482 void
1483 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1484 {
1485     if (!entry)
1486         return;
1487     if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1488         PL_sub_generation++;    /* may be deletion of method from stash */
1489     sv_2mortal(HeVAL(entry));   /* free between statements */
1490     if (HeKLEN(entry) == HEf_SVKEY) {
1491         sv_2mortal(HeKEY_sv(entry));
1492         Safefree(HeKEY_hek(entry));
1493     }
1494     else if (HvSHAREKEYS(hv))
1495         unshare_hek(HeKEY_hek(entry));
1496     else
1497         Safefree(HeKEY_hek(entry));
1498     del_HE(entry);
1499 }
1500
1501 /*
1502 =for apidoc hv_clear
1503
1504 Clears a hash, making it empty.
1505
1506 =cut
1507 */
1508
1509 void
1510 Perl_hv_clear(pTHX_ HV *hv)
1511 {
1512     register XPVHV* xhv;
1513     if (!hv)
1514         return;
1515
1516     DEBUG_A(Perl_hv_assert(aTHX_ hv));
1517
1518     xhv = (XPVHV*)SvANY(hv);
1519
1520     if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1521         /* restricted hash: convert all keys to placeholders */
1522         I32 i;
1523         HE* entry;
1524         for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1525             entry = ((HE**)xhv->xhv_array)[i];
1526             for (; entry; entry = HeNEXT(entry)) {
1527                 /* not already placeholder */
1528                 if (HeVAL(entry) != &PL_sv_placeholder) {
1529                     if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1530                         SV* keysv = hv_iterkeysv(entry);
1531                         Perl_croak(aTHX_
1532         "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1533                                    keysv);
1534                     }
1535                     SvREFCNT_dec(HeVAL(entry));
1536                     HeVAL(entry) = &PL_sv_placeholder;
1537                     xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1538                 }
1539             }
1540         }
1541         return;
1542     }
1543
1544     hfreeentries(hv);
1545     xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1546     if (xhv->xhv_array /* HvARRAY(hv) */)
1547         (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1548                       (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1549
1550     if (SvRMAGICAL(hv))
1551         mg_clear((SV*)hv);
1552
1553     HvHASKFLAGS_off(hv);
1554     HvREHASH_off(hv);
1555 }
1556
1557 /*
1558 =for apidoc hv_clear_placeholders
1559
1560 Clears any placeholders from a hash.  If a restricted hash has any of its keys
1561 marked as readonly and the key is subsequently deleted, the key is not actually
1562 deleted but is marked by assigning it a value of &PL_sv_placeholder.  This tags
1563 it so it will be ignored by future operations such as iterating over the hash,
1564 but will still allow the hash to have a value reaasigned to the key at some
1565 future point.  This function clears any such placeholder keys from the hash.
1566 See Hash::Util::lock_keys() for an example of its use.
1567
1568 =cut
1569 */
1570
1571 void
1572 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1573 {
1574     I32 items;
1575     items = (I32)HvPLACEHOLDERS(hv);
1576     if (items) {
1577         HE *entry;
1578         I32 riter = HvRITER(hv);
1579         HE *eiter = HvEITER(hv);
1580         hv_iterinit(hv);
1581         /* This may look suboptimal with the items *after* the iternext, but
1582            it's quite deliberate. We only get here with items==0 if we've
1583            just deleted the last placeholder in the hash. If we've just done
1584            that then it means that the hash is in lazy delete mode, and the
1585            HE is now only referenced in our iterator. If we just quit the loop
1586            and discarded our iterator then the HE leaks. So we do the && the
1587            other way to ensure iternext is called just one more time, which
1588            has the side effect of triggering the lazy delete.  */
1589         while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
1590             && items) {
1591             SV *val = hv_iterval(hv, entry);
1592
1593             if (val == &PL_sv_placeholder) {
1594
1595                 /* It seems that I have to go back in the front of the hash
1596                    API to delete a hash, even though I have a HE structure
1597                    pointing to the very entry I want to delete, and could hold
1598                    onto the previous HE that points to it. And it's easier to
1599                    go in with SVs as I can then specify the precomputed hash,
1600                    and don't have fun and games with utf8 keys.  */
1601                 SV *key = hv_iterkeysv(entry);
1602
1603                 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
1604                 items--;
1605             }
1606         }
1607         HvRITER(hv) = riter;
1608         HvEITER(hv) = eiter;
1609     }
1610 }
1611
1612 STATIC void
1613 S_hfreeentries(pTHX_ HV *hv)
1614 {
1615     register HE **array;
1616     register HE *entry;
1617     register HE *oentry = Null(HE*);
1618     I32 riter;
1619     I32 max;
1620
1621     if (!hv)
1622         return;
1623     if (!HvARRAY(hv))
1624         return;
1625
1626     riter = 0;
1627     max = HvMAX(hv);
1628     array = HvARRAY(hv);
1629     /* make everyone else think the array is empty, so that the destructors
1630      * called for freed entries can't recusively mess with us */
1631     HvARRAY(hv) = Null(HE**); 
1632     HvFILL(hv) = 0;
1633     ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1634
1635     entry = array[0];
1636     for (;;) {
1637         if (entry) {
1638             oentry = entry;
1639             entry = HeNEXT(entry);
1640             hv_free_ent(hv, oentry);
1641         }
1642         if (!entry) {
1643             if (++riter > max)
1644                 break;
1645             entry = array[riter];
1646         }
1647     }
1648     HvARRAY(hv) = array;
1649     (void)hv_iterinit(hv);
1650 }
1651
1652 /*
1653 =for apidoc hv_undef
1654
1655 Undefines the hash.
1656
1657 =cut
1658 */
1659
1660 void
1661 Perl_hv_undef(pTHX_ HV *hv)
1662 {
1663     register XPVHV* xhv;
1664     if (!hv)
1665         return;
1666     DEBUG_A(Perl_hv_assert(aTHX_ hv));
1667     xhv = (XPVHV*)SvANY(hv);
1668     hfreeentries(hv);
1669     Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1670     if (HvNAME(hv)) {
1671         if(PL_stashcache)
1672             hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1673         Safefree(HvNAME(hv));
1674         HvNAME(hv) = 0;
1675     }
1676     xhv->xhv_max   = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1677     xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1678     xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1679
1680     if (SvRMAGICAL(hv))
1681         mg_clear((SV*)hv);
1682 }
1683
1684 /*
1685 =for apidoc hv_iterinit
1686
1687 Prepares a starting point to traverse a hash table.  Returns the number of
1688 keys in the hash (i.e. the same as C<HvKEYS(tb)>).  The return value is
1689 currently only meaningful for hashes without tie magic.
1690
1691 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1692 hash buckets that happen to be in use.  If you still need that esoteric
1693 value, you can get it through the macro C<HvFILL(tb)>.
1694
1695
1696 =cut
1697 */
1698
1699 I32
1700 Perl_hv_iterinit(pTHX_ HV *hv)
1701 {
1702     register XPVHV* xhv;
1703     HE *entry;
1704
1705     if (!hv)
1706         Perl_croak(aTHX_ "Bad hash");
1707     xhv = (XPVHV*)SvANY(hv);
1708     entry = xhv->xhv_eiter; /* HvEITER(hv) */
1709     if (entry && HvLAZYDEL(hv)) {       /* was deleted earlier? */
1710         HvLAZYDEL_off(hv);
1711         hv_free_ent(hv, entry);
1712     }
1713     xhv->xhv_riter = -1;        /* HvRITER(hv) = -1 */
1714     xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1715     /* used to be xhv->xhv_fill before 5.004_65 */
1716     return XHvTOTALKEYS(xhv);
1717 }
1718 /*
1719 =for apidoc hv_iternext
1720
1721 Returns entries from a hash iterator.  See C<hv_iterinit>.
1722
1723 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1724 iterator currently points to, without losing your place or invalidating your
1725 iterator.  Note that in this case the current entry is deleted from the hash
1726 with your iterator holding the last reference to it.  Your iterator is flagged
1727 to free the entry on the next call to C<hv_iternext>, so you must not discard
1728 your iterator immediately else the entry will leak - call C<hv_iternext> to
1729 trigger the resource deallocation.
1730
1731 =cut
1732 */
1733
1734 HE *
1735 Perl_hv_iternext(pTHX_ HV *hv)
1736 {
1737     return hv_iternext_flags(hv, 0);
1738 }
1739
1740 /*
1741 =for apidoc hv_iternext_flags
1742
1743 Returns entries from a hash iterator.  See C<hv_iterinit> and C<hv_iternext>.
1744 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1745 set the placeholders keys (for restricted hashes) will be returned in addition
1746 to normal keys. By default placeholders are automatically skipped over.
1747 Currently a placeholder is implemented with a value that is
1748 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1749 restricted hashes may change, and the implementation currently is
1750 insufficiently abstracted for any change to be tidy.
1751
1752 =cut
1753 */
1754
1755 HE *
1756 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1757 {
1758     register XPVHV* xhv;
1759     register HE *entry;
1760     HE *oldentry;
1761     MAGIC* mg;
1762
1763     if (!hv)
1764         Perl_croak(aTHX_ "Bad hash");
1765     xhv = (XPVHV*)SvANY(hv);
1766     oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1767
1768     if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1769         SV *key = sv_newmortal();
1770         if (entry) {
1771             sv_setsv(key, HeSVKEY_force(entry));
1772             SvREFCNT_dec(HeSVKEY(entry));       /* get rid of previous key */
1773         }
1774         else {
1775             char *k;
1776             HEK *hek;
1777
1778             /* one HE per MAGICAL hash */
1779             xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1780             Zero(entry, 1, HE);
1781             Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1782             hek = (HEK*)k;
1783             HeKEY_hek(entry) = hek;
1784             HeKLEN(entry) = HEf_SVKEY;
1785         }
1786         magic_nextpack((SV*) hv,mg,key);
1787         if (SvOK(key)) {
1788             /* force key to stay around until next time */
1789             HeSVKEY_set(entry, SvREFCNT_inc(key));
1790             return entry;               /* beware, hent_val is not set */
1791         }
1792         if (HeVAL(entry))
1793             SvREFCNT_dec(HeVAL(entry));
1794         Safefree(HeKEY_hek(entry));
1795         del_HE(entry);
1796         xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1797         return Null(HE*);
1798     }
1799 #ifdef DYNAMIC_ENV_FETCH  /* set up %ENV for iteration */
1800     if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1801         prime_env_iter();
1802 #endif
1803
1804     if (!xhv->xhv_array /* !HvARRAY(hv) */)
1805         Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1806              PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1807              char);
1808     /* At start of hash, entry is NULL.  */
1809     if (entry)
1810     {
1811         entry = HeNEXT(entry);
1812         if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1813             /*
1814              * Skip past any placeholders -- don't want to include them in
1815              * any iteration.
1816              */
1817             while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1818                 entry = HeNEXT(entry);
1819             }
1820         }
1821     }
1822     while (!entry) {
1823         /* OK. Come to the end of the current list.  Grab the next one.  */
1824
1825         xhv->xhv_riter++; /* HvRITER(hv)++ */
1826         if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1827             /* There is no next one.  End of the hash.  */
1828             xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1829             break;
1830         }
1831         /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1832         entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1833
1834         if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1835             /* If we have an entry, but it's a placeholder, don't count it.
1836                Try the next.  */
1837             while (entry && HeVAL(entry) == &PL_sv_placeholder)
1838                 entry = HeNEXT(entry);
1839         }
1840         /* Will loop again if this linked list starts NULL
1841            (for HV_ITERNEXT_WANTPLACEHOLDERS)
1842            or if we run through it and find only placeholders.  */
1843     }
1844
1845     if (oldentry && HvLAZYDEL(hv)) {            /* was deleted earlier? */
1846         HvLAZYDEL_off(hv);
1847         hv_free_ent(hv, oldentry);
1848     }
1849
1850     /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1851       PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1852
1853     xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1854     return entry;
1855 }
1856
1857 /*
1858 =for apidoc hv_iterkey
1859
1860 Returns the key from the current position of the hash iterator.  See
1861 C<hv_iterinit>.
1862
1863 =cut
1864 */
1865
1866 char *
1867 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1868 {
1869     if (HeKLEN(entry) == HEf_SVKEY) {
1870         STRLEN len;
1871         char *p = SvPV(HeKEY_sv(entry), len);
1872         *retlen = len;
1873         return p;
1874     }
1875     else {
1876         *retlen = HeKLEN(entry);
1877         return HeKEY(entry);
1878     }
1879 }
1880
1881 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1882 /*
1883 =for apidoc hv_iterkeysv
1884
1885 Returns the key as an C<SV*> from the current position of the hash
1886 iterator.  The return value will always be a mortal copy of the key.  Also
1887 see C<hv_iterinit>.
1888
1889 =cut
1890 */
1891
1892 SV *
1893 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1894 {
1895     if (HeKLEN(entry) != HEf_SVKEY) {
1896         HEK *hek = HeKEY_hek(entry);
1897         int flags = HEK_FLAGS(hek);
1898         SV *sv;
1899
1900         if (flags & HVhek_WASUTF8) {
1901             /* Trouble :-)
1902                Andreas would like keys he put in as utf8 to come back as utf8
1903             */
1904             STRLEN utf8_len = HEK_LEN(hek);
1905             U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1906
1907             sv = newSVpvn ((char*)as_utf8, utf8_len);
1908             SvUTF8_on (sv);
1909             Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1910         } else if (flags & HVhek_REHASH) {
1911             /* We don't have a pointer to the hv, so we have to replicate the
1912                flag into every HEK. This hv is using custom a hasing
1913                algorithm. Hence we can't return a shared string scalar, as
1914                that would contain the (wrong) hash value, and might get passed
1915                into an hv routine with a regular hash  */
1916
1917             sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1918             if (HEK_UTF8(hek))
1919                 SvUTF8_on (sv);
1920         } else {
1921             sv = newSVpvn_share(HEK_KEY(hek),
1922                                 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1923                                 HEK_HASH(hek));
1924         }
1925         return sv_2mortal(sv);
1926     }
1927     return sv_mortalcopy(HeKEY_sv(entry));
1928 }
1929
1930 /*
1931 =for apidoc hv_iterval
1932
1933 Returns the value from the current position of the hash iterator.  See
1934 C<hv_iterkey>.
1935
1936 =cut
1937 */
1938
1939 SV *
1940 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1941 {
1942     if (SvRMAGICAL(hv)) {
1943         if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1944             SV* sv = sv_newmortal();
1945             if (HeKLEN(entry) == HEf_SVKEY)
1946                 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1947             else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1948             return sv;
1949         }
1950     }
1951     return HeVAL(entry);
1952 }
1953
1954 /*
1955 =for apidoc hv_iternextsv
1956
1957 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1958 operation.
1959
1960 =cut
1961 */
1962
1963 SV *
1964 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1965 {
1966     HE *he;
1967     if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1968         return NULL;
1969     *key = hv_iterkey(he, retlen);
1970     return hv_iterval(hv, he);
1971 }
1972
1973 /*
1974 =for apidoc hv_magic
1975
1976 Adds magic to a hash.  See C<sv_magic>.
1977
1978 =cut
1979 */
1980
1981 void
1982 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1983 {
1984     sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1985 }
1986
1987 #if 0 /* use the macro from hv.h instead */
1988
1989 char*   
1990 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1991 {
1992     return HEK_KEY(share_hek(sv, len, hash));
1993 }
1994
1995 #endif
1996
1997 /* possibly free a shared string if no one has access to it
1998  * len and hash must both be valid for str.
1999  */
2000 void
2001 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2002 {
2003     unshare_hek_or_pvn (NULL, str, len, hash);
2004 }
2005
2006
2007 void
2008 Perl_unshare_hek(pTHX_ HEK *hek)
2009 {
2010     unshare_hek_or_pvn(hek, NULL, 0, 0);
2011 }
2012
2013 /* possibly free a shared string if no one has access to it
2014    hek if non-NULL takes priority over the other 3, else str, len and hash
2015    are used.  If so, len and hash must both be valid for str.
2016  */
2017 STATIC void
2018 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2019 {
2020     register XPVHV* xhv;
2021     register HE *entry;
2022     register HE **oentry;
2023     register I32 i = 1;
2024     I32 found = 0;
2025     bool is_utf8 = FALSE;
2026     int k_flags = 0;
2027     const char *save = str;
2028
2029     if (hek) {
2030         hash = HEK_HASH(hek);
2031     } else if (len < 0) {
2032         STRLEN tmplen = -len;
2033         is_utf8 = TRUE;
2034         /* See the note in hv_fetch(). --jhi */
2035         str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2036         len = tmplen;
2037         if (is_utf8)
2038             k_flags = HVhek_UTF8;
2039         if (str != save)
2040             k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2041     }
2042
2043     /* what follows is the moral equivalent of:
2044     if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2045         if (--*Svp == Nullsv)
2046             hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2047     } */
2048     xhv = (XPVHV*)SvANY(PL_strtab);
2049     /* assert(xhv_array != 0) */
2050     LOCK_STRTAB_MUTEX;
2051     /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2052     oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2053     if (hek) {
2054         for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2055             if (HeKEY_hek(entry) != hek)
2056                 continue;
2057             found = 1;
2058             break;
2059         }
2060     } else {
2061         int flags_masked = k_flags & HVhek_MASK;
2062         for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2063             if (HeHASH(entry) != hash)          /* strings can't be equal */
2064                 continue;
2065             if (HeKLEN(entry) != len)
2066                 continue;
2067             if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len))     /* is this it? */
2068                 continue;
2069             if (HeKFLAGS(entry) != flags_masked)
2070                 continue;
2071             found = 1;
2072             break;
2073         }
2074     }
2075
2076     if (found) {
2077         if (--HeVAL(entry) == Nullsv) {
2078             *oentry = HeNEXT(entry);
2079             if (i && !*oentry)
2080                 xhv->xhv_fill--; /* HvFILL(hv)-- */
2081             Safefree(HeKEY_hek(entry));
2082             del_HE(entry);
2083             xhv->xhv_keys--; /* HvKEYS(hv)-- */
2084         }
2085     }
2086
2087     UNLOCK_STRTAB_MUTEX;
2088     if (!found && ckWARN_d(WARN_INTERNAL))
2089         Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2090                     "Attempt to free non-existent shared string '%s'%s",
2091                     hek ? HEK_KEY(hek) : str,
2092                     (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2093     if (k_flags & HVhek_FREEKEY)
2094         Safefree(str);
2095 }
2096
2097 /* get a (constant) string ptr from the global string table
2098  * string will get added if it is not already there.
2099  * len and hash must both be valid for str.
2100  */
2101 HEK *
2102 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2103 {
2104     bool is_utf8 = FALSE;
2105     int flags = 0;
2106     const char *save = str;
2107
2108     if (len < 0) {
2109       STRLEN tmplen = -len;
2110       is_utf8 = TRUE;
2111       /* See the note in hv_fetch(). --jhi */
2112       str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2113       len = tmplen;
2114       /* If we were able to downgrade here, then than means that we were passed
2115          in a key which only had chars 0-255, but was utf8 encoded.  */
2116       if (is_utf8)
2117           flags = HVhek_UTF8;
2118       /* If we found we were able to downgrade the string to bytes, then
2119          we should flag that it needs upgrading on keys or each.  Also flag
2120          that we need share_hek_flags to free the string.  */
2121       if (str != save)
2122           flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2123     }
2124
2125     return share_hek_flags (str, len, hash, flags);
2126 }
2127
2128 STATIC HEK *
2129 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2130 {
2131     register XPVHV* xhv;
2132     register HE *entry;
2133     register HE **oentry;
2134     register I32 i = 1;
2135     I32 found = 0;
2136     int flags_masked = flags & HVhek_MASK;
2137
2138     /* what follows is the moral equivalent of:
2139
2140     if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2141         hv_store(PL_strtab, str, len, Nullsv, hash);
2142
2143         Can't rehash the shared string table, so not sure if it's worth
2144         counting the number of entries in the linked list
2145     */
2146     xhv = (XPVHV*)SvANY(PL_strtab);
2147     /* assert(xhv_array != 0) */
2148     LOCK_STRTAB_MUTEX;
2149     /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2150     oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2151     for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2152         if (HeHASH(entry) != hash)              /* strings can't be equal */
2153             continue;
2154         if (HeKLEN(entry) != len)
2155             continue;
2156         if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2157             continue;
2158         if (HeKFLAGS(entry) != flags_masked)
2159             continue;
2160         found = 1;
2161         break;
2162     }
2163     if (!found) {
2164         entry = new_HE();
2165         HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2166         HeVAL(entry) = Nullsv;
2167         HeNEXT(entry) = *oentry;
2168         *oentry = entry;
2169         xhv->xhv_keys++; /* HvKEYS(hv)++ */
2170         if (i) {                                /* initial entry? */
2171             xhv->xhv_fill++; /* HvFILL(hv)++ */
2172         } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2173                 hsplit(PL_strtab);
2174         }
2175     }
2176
2177     ++HeVAL(entry);                             /* use value slot as REFCNT */
2178     UNLOCK_STRTAB_MUTEX;
2179
2180     if (flags & HVhek_FREEKEY)
2181         Safefree(str);
2182
2183     return HeKEY_hek(entry);
2184 }
2185
2186
2187 /*
2188 =for apidoc hv_assert
2189
2190 Check that a hash is in an internally consistent state.
2191
2192 =cut
2193 */
2194
2195 void
2196 Perl_hv_assert(pTHX_ HV *hv)
2197 {
2198   HE* entry;
2199   int withflags = 0;
2200   int placeholders = 0;
2201   int real = 0;
2202   int bad = 0;
2203   I32 riter = HvRITER(hv);
2204   HE *eiter = HvEITER(hv);
2205
2206   (void)hv_iterinit(hv);
2207
2208   while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2209     /* sanity check the values */
2210     if (HeVAL(entry) == &PL_sv_placeholder) {
2211       placeholders++;
2212     } else {
2213       real++;
2214     }
2215     /* sanity check the keys */
2216     if (HeSVKEY(entry)) {
2217       /* Don't know what to check on SV keys.  */
2218     } else if (HeKUTF8(entry)) {
2219       withflags++;
2220        if (HeKWASUTF8(entry)) {
2221          PerlIO_printf(Perl_debug_log,
2222                        "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2223                        (int) HeKLEN(entry),  HeKEY(entry));
2224          bad = 1;
2225        }
2226     } else if (HeKWASUTF8(entry)) {
2227       withflags++;
2228     }
2229   }
2230   if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2231     if (HvUSEDKEYS(hv) != real) {
2232       PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2233                     (int) real, (int) HvUSEDKEYS(hv));
2234       bad = 1;
2235     }
2236     if (HvPLACEHOLDERS(hv) != placeholders) {
2237       PerlIO_printf(Perl_debug_log,
2238                     "Count %d placeholder(s), but hash reports %d\n",
2239                     (int) placeholders, (int) HvPLACEHOLDERS(hv));
2240       bad = 1;
2241     }
2242   }
2243   if (withflags && ! HvHASKFLAGS(hv)) {
2244     PerlIO_printf(Perl_debug_log,
2245                   "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2246                   withflags);
2247     bad = 1;
2248   }
2249   if (bad) {
2250     sv_dump((SV *)hv);
2251   }
2252   HvRITER(hv) = riter;          /* Restore hash iterator state */
2253   HvEITER(hv) = eiter;
2254 }