3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
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.
12 * "I sit beside the fire and think of all that I have seen." --Bilbo
16 =head1 Hash Manipulation Functions
21 #define PERL_HASH_INTERNAL_ACCESS
24 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
34 PL_he_root = HeNEXT(he);
43 HeNEXT(p) = (HE*)PL_he_root;
54 New(54, ptr, 1008/sizeof(XPV), XPV);
55 ptr->xpv_pv = (char*)PL_he_arenaroot;
56 PL_he_arenaroot = ptr;
59 heend = &he[1008 / sizeof(HE) - 1];
62 HeNEXT(he) = (HE*)(he + 1);
70 #define new_HE() (HE*)safemalloc(sizeof(HE))
71 #define del_HE(p) safefree((char*)p)
75 #define new_HE() new_he()
76 #define del_HE(p) del_he(p)
81 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
86 New(54, k, HEK_BASESIZE + len + 2, char);
88 Copy(str, HEK_KEY(hek), len, char);
89 HEK_KEY(hek)[len] = 0;
92 HEK_FLAGS(hek) = (unsigned char)flags;
96 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
100 Perl_free_tied_hv_pool(pTHX)
103 HE *he = PL_hv_fetch_ent_mh;
105 Safefree(HeKEY_hek(he));
110 PL_hv_fetch_ent_mh = Nullhe;
113 #if defined(USE_ITHREADS)
115 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
121 /* look for it in the table first */
122 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
126 /* create anew and remember what it is */
128 ptr_table_store(PL_ptr_table, e, ret);
130 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
131 if (HeKLEN(e) == HEf_SVKEY) {
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));
138 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
141 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
143 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
146 #endif /* USE_ITHREADS */
149 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
152 SV *sv = sv_newmortal(), *esv = sv_newmortal();
153 if (!(flags & HVhek_FREEKEY)) {
154 sv_setpvn(sv, key, klen);
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);
161 if (flags & HVhek_UTF8) {
164 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
165 Perl_croak(aTHX_ SvPVX(esv), sv);
168 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
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*>.
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.
187 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
189 bool is_utf8 = FALSE;
190 const char *keysave = key;
199 STRLEN tmplen = klen;
200 /* Just casting the &klen to (STRLEN) won't work well
201 * if STRLEN and I32 are of different widths. --jhi */
202 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
204 /* If we were able to downgrade here, then than means that we were
205 passed in a key which only had chars 0-255, but was utf8 encoded. */
208 /* If we found we were able to downgrade the string to bytes, then
209 we should flag that it needs upgrading on keys or each. */
211 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
214 return hv_fetch_flags (hv, key, klen, lval, flags);
218 S_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen, I32 lval, int flags)
228 if (SvRMAGICAL(hv)) {
229 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
231 sv_upgrade(sv, SVt_PVLV);
232 if (flags & HVhek_UTF8) {
233 /* This hack based on the code in hv_exists_ent seems to be
234 the easiest way to pass the utf8 flag through and fix
235 the bug in hv_exists for tied hashes with utf8 keys. */
236 SV *keysv = sv_2mortal(newSVpvn(key, klen));
238 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
240 mg_copy((SV*)hv, sv, key, klen);
242 if (flags & HVhek_FREEKEY)
245 LvTARG(sv) = sv; /* fake (SV**) */
246 return &(LvTARG(sv));
248 #ifdef ENV_IS_CASELESS
249 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
251 for (i = 0; i < klen; ++i)
252 if (isLOWER(key[i])) {
253 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
254 SV **ret = hv_fetch(hv, nkey, klen, 0);
256 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
258 } else if (flags & HVhek_FREEKEY)
266 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
267 avoid unnecessary pointer dereferencing. */
268 xhv = (XPVHV*)SvANY(hv);
269 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
271 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
272 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
275 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
276 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
279 if (flags & HVhek_FREEKEY)
286 PERL_HASH_INTERNAL(hash, key, klen);
287 /* Yes, you do need this even though you are not "storing" because
288 you can flip the flags below if doing an lval lookup. (And that
289 was put in to give the semantics Andreas was expecting.) */
290 flags |= HVhek_REHASH;
292 PERL_HASH(hash, key, klen);
295 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
296 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
297 for (; entry; entry = HeNEXT(entry)) {
298 if (!HeKEY_hek(entry))
300 if (HeHASH(entry) != hash) /* strings can't be equal */
302 if (HeKLEN(entry) != (I32)klen)
304 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
306 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
307 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
308 xor is true if bits differ, in which case this isn't a match. */
309 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
311 if (lval && HeKFLAGS(entry) != flags) {
312 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
313 But if entry was set previously with HVhek_WASUTF8 and key now
314 doesn't (or vice versa) then we should change the key's flag,
315 as this is assignment. */
316 if (HvSHAREKEYS(hv)) {
317 /* Need to swap the key we have for a key with the flags we
318 need. As keys are shared we can't just write to the flag,
319 so we share the new one, unshare the old one. */
320 int flags_nofree = flags & ~HVhek_FREEKEY;
321 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
322 unshare_hek (HeKEY_hek(entry));
323 HeKEY_hek(entry) = new_hek;
326 HeKFLAGS(entry) = flags;
327 if (flags & HVhek_ENABLEHVKFLAGS)
330 if (flags & HVhek_FREEKEY)
332 /* if we find a placeholder, we pretend we haven't found anything */
333 if (HeVAL(entry) == &PL_sv_placeholder)
335 return &HeVAL(entry);
338 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
339 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
341 char *env = PerlEnv_ENVgetenv_len(key,&len);
343 sv = newSVpvn(env,len);
345 if (flags & HVhek_FREEKEY)
347 return hv_store(hv,key,klen,sv,hash);
351 if (!entry && SvREADONLY(hv)) {
352 S_hv_notallowed(aTHX_ flags, key, klen,
353 "access disallowed key '%"SVf"' in"
356 if (lval) { /* gonna assign to this, so it better be there */
358 return hv_store_flags(hv,key,klen,sv,hash,flags);
360 if (flags & HVhek_FREEKEY)
365 /* returns an HE * structure with the all fields set */
366 /* note that hent_val will be a mortal sv for MAGICAL hashes */
368 =for apidoc hv_fetch_ent
370 Returns the hash entry which corresponds to the specified key in the hash.
371 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
372 if you want the function to compute it. IF C<lval> is set then the fetch
373 will be part of a store. Make sure the return value is non-null before
374 accessing it. The return value when C<tb> is a tied hash is a pointer to a
375 static location, so be sure to make a copy of the structure if you need to
378 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
379 information on how to use this function on tied hashes.
385 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
399 if (SvRMAGICAL(hv)) {
400 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
402 keysv = newSVsv(keysv);
403 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
404 /* grab a fake HE/HEK pair from the pool or make a new one */
405 entry = PL_hv_fetch_ent_mh;
407 PL_hv_fetch_ent_mh = HeNEXT(entry);
411 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
412 HeKEY_hek(entry) = (HEK*)k;
414 HeNEXT(entry) = Nullhe;
415 HeSVKEY_set(entry, keysv);
417 sv_upgrade(sv, SVt_PVLV);
419 LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
422 #ifdef ENV_IS_CASELESS
423 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
425 key = SvPV(keysv, klen);
426 for (i = 0; i < klen; ++i)
427 if (isLOWER(key[i])) {
428 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
429 (void)strupr(SvPVX(nkeysv));
430 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
432 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
439 keysave = key = SvPV(keysv, klen);
440 xhv = (XPVHV*)SvANY(hv);
441 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
443 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
444 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
447 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
448 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
454 is_utf8 = (SvUTF8(keysv)!=0);
457 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
461 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
465 PERL_HASH_INTERNAL(hash, key, klen);
466 /* Yes, you do need this even though you are not "storing" because
467 you can flip the flags below if doing an lval lookup. (And that
468 was put in to give the semantics Andreas was expecting.) */
469 flags |= HVhek_REHASH;
471 if SvIsCOW_shared_hash(keysv) {
474 PERL_HASH(hash, key, klen);
478 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
479 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
480 for (; entry; entry = HeNEXT(entry)) {
481 if (HeHASH(entry) != hash) /* strings can't be equal */
483 if (HeKLEN(entry) != (I32)klen)
485 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
487 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
489 if (lval && HeKFLAGS(entry) != flags) {
490 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
491 But if entry was set previously with HVhek_WASUTF8 and key now
492 doesn't (or vice versa) then we should change the key's flag,
493 as this is assignment. */
494 if (HvSHAREKEYS(hv)) {
495 /* Need to swap the key we have for a key with the flags we
496 need. As keys are shared we can't just write to the flag,
497 so we share the new one, unshare the old one. */
498 int flags_nofree = flags & ~HVhek_FREEKEY;
499 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
500 unshare_hek (HeKEY_hek(entry));
501 HeKEY_hek(entry) = new_hek;
504 HeKFLAGS(entry) = flags;
505 if (flags & HVhek_ENABLEHVKFLAGS)
510 /* if we find a placeholder, we pretend we haven't found anything */
511 if (HeVAL(entry) == &PL_sv_placeholder)
515 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
516 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
518 char *env = PerlEnv_ENVgetenv_len(key,&len);
520 sv = newSVpvn(env,len);
522 return hv_store_ent(hv,keysv,sv,hash);
526 if (!entry && SvREADONLY(hv)) {
527 S_hv_notallowed(aTHX_ flags, key, klen,
528 "access disallowed key '%"SVf"' in"
531 if (flags & HVhek_FREEKEY)
533 if (lval) { /* gonna assign to this, so it better be there */
535 return hv_store_ent(hv,keysv,sv,hash);
541 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
543 MAGIC *mg = SvMAGIC(hv);
547 if (isUPPER(mg->mg_type)) {
549 switch (mg->mg_type) {
550 case PERL_MAGIC_tied:
552 *needs_store = FALSE;
555 mg = mg->mg_moremagic;
562 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
563 the length of the key. The C<hash> parameter is the precomputed hash
564 value; if it is zero then Perl will compute it. The return value will be
565 NULL if the operation failed or if the value did not need to be actually
566 stored within the hash (as in the case of tied hashes). Otherwise it can
567 be dereferenced to get the original C<SV*>. Note that the caller is
568 responsible for suitably incrementing the reference count of C<val> before
569 the call, and decrementing it if the function returned NULL. Effectively
570 a successful hv_store takes ownership of one reference to C<val>. This is
571 usually what you want; a newly created SV has a reference count of one, so
572 if all your code does is create SVs then store them in a hash, hv_store
573 will own the only reference to the new SV, and your code doesn't need to do
574 anything further to tidy up. hv_store is not implemented as a call to
575 hv_store_ent, and does not create a temporary SV for the key, so if your
576 key data is not already in SV form then use hv_store in preference to
579 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
580 information on how to use this function on tied hashes.
586 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
588 bool is_utf8 = FALSE;
589 const char *keysave = key;
598 STRLEN tmplen = klen;
599 /* Just casting the &klen to (STRLEN) won't work well
600 * if STRLEN and I32 are of different widths. --jhi */
601 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
603 /* If we were able to downgrade here, then than means that we were
604 passed in a key which only had chars 0-255, but was utf8 encoded. */
607 /* If we found we were able to downgrade the string to bytes, then
608 we should flag that it needs upgrading on keys or each. */
610 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
613 return hv_store_flags (hv, key, klen, val, hash, flags);
617 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
618 register U32 hash, int flags)
621 register U32 n_links;
623 register HE **oentry;
628 xhv = (XPVHV*)SvANY(hv);
632 hv_magic_check (hv, &needs_copy, &needs_store);
634 if (flags & HVhek_UTF8) {
635 /* This hack based on the code in hv_exists_ent seems to be
636 the easiest way to pass the utf8 flag through and fix
637 the bug in hv_exists for tied hashes with utf8 keys. */
638 SV *keysv = sv_2mortal(newSVpvn(key, klen));
640 mg_copy((SV*)hv, val, (char *)keysv, HEf_SVKEY);
642 mg_copy((SV*)hv, val, key, klen);
644 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
645 if (flags & HVhek_FREEKEY)
649 #ifdef ENV_IS_CASELESS
650 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
651 key = savepvn(key,klen);
652 key = (const char*)strupr((char*)key);
660 HvHASKFLAGS_on((SV*)hv);
663 /* We don't have a pointer to the hv, so we have to replicate the
664 flag into every HEK, so that hv_iterkeysv can see it. */
665 flags |= HVhek_REHASH;
666 PERL_HASH_INTERNAL(hash, key, klen);
668 PERL_HASH(hash, key, klen);
670 if (!xhv->xhv_array /* !HvARRAY(hv) */)
671 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
672 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
675 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
676 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
680 for (entry = *oentry; entry; ++n_links, entry = HeNEXT(entry)) {
681 if (HeHASH(entry) != hash) /* strings can't be equal */
683 if (HeKLEN(entry) != (I32)klen)
685 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
687 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
689 if (HeVAL(entry) == &PL_sv_placeholder)
690 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
692 SvREFCNT_dec(HeVAL(entry));
693 if (flags & HVhek_PLACEHOLD) {
694 /* We have been requested to insert a placeholder. Currently
695 only Storable is allowed to do this. */
696 xhv->xhv_placeholders++;
697 HeVAL(entry) = &PL_sv_placeholder;
701 if (HeKFLAGS(entry) != flags) {
702 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
703 But if entry was set previously with HVhek_WASUTF8 and key now
704 doesn't (or vice versa) then we should change the key's flag,
705 as this is assignment. */
706 if (HvSHAREKEYS(hv)) {
707 /* Need to swap the key we have for a key with the flags we
708 need. As keys are shared we can't just write to the flag,
709 so we share the new one, unshare the old one. */
710 int flags_nofree = flags & ~HVhek_FREEKEY;
711 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
712 unshare_hek (HeKEY_hek(entry));
713 HeKEY_hek(entry) = new_hek;
716 HeKFLAGS(entry) = flags;
718 if (flags & HVhek_FREEKEY)
720 return &HeVAL(entry);
723 if (SvREADONLY(hv)) {
724 S_hv_notallowed(aTHX_ flags, key, klen,
725 "access disallowed key '%"SVf"' to"
730 /* share_hek_flags will do the free for us. This might be considered
733 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
734 else /* gotta do the real thing */
735 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
736 if (flags & HVhek_PLACEHOLD) {
737 /* We have been requested to insert a placeholder. Currently
738 only Storable is allowed to do this. */
739 xhv->xhv_placeholders++;
740 HeVAL(entry) = &PL_sv_placeholder;
743 HeNEXT(entry) = *oentry;
746 xhv->xhv_keys++; /* HvKEYS(hv)++ */
747 if (!n_links) { /* initial entry? */
748 xhv->xhv_fill++; /* HvFILL(hv)++ */
749 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
750 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
751 /* Use the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
752 splits on a rehashed hash, as we're not going to split it again,
753 and if someone is lucky (evil) enough to get all the keys in one
754 list they could exhaust our memory as we repeatedly double the
755 number of buckets on every entry. Linear search feels a less worse
760 return &HeVAL(entry);
764 =for apidoc hv_store_ent
766 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
767 parameter is the precomputed hash value; if it is zero then Perl will
768 compute it. The return value is the new hash entry so created. It will be
769 NULL if the operation failed or if the value did not need to be actually
770 stored within the hash (as in the case of tied hashes). Otherwise the
771 contents of the return value can be accessed using the C<He?> macros
772 described here. Note that the caller is responsible for suitably
773 incrementing the reference count of C<val> before the call, and
774 decrementing it if the function returned NULL. Effectively a successful
775 hv_store_ent takes ownership of one reference to C<val>. This is
776 usually what you want; a newly created SV has a reference count of one, so
777 if all your code does is create SVs then store them in a hash, hv_store
778 will own the only reference to the new SV, and your code doesn't need to do
779 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
780 unlike C<val> it does not take ownership of it, so maintaining the correct
781 reference count on C<key> is entirely the caller's responsibility. hv_store
782 is not implemented as a call to hv_store_ent, and does not create a temporary
783 SV for the key, so if your key data is not already in SV form then use
784 hv_store in preference to hv_store_ent.
786 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
787 information on how to use this function on tied hashes.
793 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
808 xhv = (XPVHV*)SvANY(hv);
812 hv_magic_check (hv, &needs_copy, &needs_store);
814 bool save_taint = PL_tainted;
816 PL_tainted = SvTAINTED(keysv);
817 keysv = sv_2mortal(newSVsv(keysv));
818 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
819 TAINT_IF(save_taint);
820 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
822 #ifdef ENV_IS_CASELESS
823 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
824 key = SvPV(keysv, klen);
825 keysv = sv_2mortal(newSVpvn(key,klen));
826 (void)strupr(SvPVX(keysv));
833 keysave = key = SvPV(keysv, klen);
834 is_utf8 = (SvUTF8(keysv) != 0);
837 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
841 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
842 HvHASKFLAGS_on((SV*)hv);
846 /* We don't have a pointer to the hv, so we have to replicate the
847 flag into every HEK, so that hv_iterkeysv can see it. */
848 flags |= HVhek_REHASH;
849 PERL_HASH_INTERNAL(hash, key, klen);
851 if SvIsCOW_shared_hash(keysv) {
854 PERL_HASH(hash, key, klen);
858 if (!xhv->xhv_array /* !HvARRAY(hv) */)
859 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
860 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
863 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
864 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
867 for (; entry; ++n_links, entry = HeNEXT(entry)) {
868 if (HeHASH(entry) != hash) /* strings can't be equal */
870 if (HeKLEN(entry) != (I32)klen)
872 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
874 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
876 if (HeVAL(entry) == &PL_sv_placeholder)
877 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
879 SvREFCNT_dec(HeVAL(entry));
881 if (HeKFLAGS(entry) != flags) {
882 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
883 But if entry was set previously with HVhek_WASUTF8 and key now
884 doesn't (or vice versa) then we should change the key's flag,
885 as this is assignment. */
886 if (HvSHAREKEYS(hv)) {
887 /* Need to swap the key we have for a key with the flags we
888 need. As keys are shared we can't just write to the flag,
889 so we share the new one, unshare the old one. */
890 int flags_nofree = flags & ~HVhek_FREEKEY;
891 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
892 unshare_hek (HeKEY_hek(entry));
893 HeKEY_hek(entry) = new_hek;
896 HeKFLAGS(entry) = flags;
898 if (flags & HVhek_FREEKEY)
903 if (SvREADONLY(hv)) {
904 S_hv_notallowed(aTHX_ flags, key, klen,
905 "access disallowed key '%"SVf"' to"
910 /* share_hek_flags will do the free for us. This might be considered
913 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
914 else /* gotta do the real thing */
915 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
917 HeNEXT(entry) = *oentry;
920 xhv->xhv_keys++; /* HvKEYS(hv)++ */
921 if (!n_links) { /* initial entry? */
922 xhv->xhv_fill++; /* HvFILL(hv)++ */
923 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
924 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
925 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
926 splits on a rehashed hash, as we're not going to split it again,
927 and if someone is lucky (evil) enough to get all the keys in one
928 list they could exhaust our memory as we repeatedly double the
929 number of buckets on every entry. Linear search feels a less worse
938 =for apidoc hv_delete
940 Deletes a key/value pair in the hash. The value SV is removed from the
941 hash and returned to the caller. The C<klen> is the length of the key.
942 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
949 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
955 register HE **oentry;
958 bool is_utf8 = FALSE;
960 const char *keysave = key;
968 if (SvRMAGICAL(hv)) {
971 hv_magic_check (hv, &needs_copy, &needs_store);
973 // XXX PerlIO_printf(PerlIO_stderr(), "%d %d\n", is_utf8, klen);
975 && (svp = hv_fetch(hv, key, is_utf8 ? -klen : klen, TRUE))) {
981 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
982 /* No longer an element */
983 sv_unmagic(sv, PERL_MAGIC_tiedelem);
986 return Nullsv; /* element cannot be deleted */
988 #ifdef ENV_IS_CASELESS
989 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
990 sv = sv_2mortal(newSVpvn(key,klen));
991 key = strupr(SvPVX(sv));
996 xhv = (XPVHV*)SvANY(hv);
997 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1001 STRLEN tmplen = klen;
1002 /* See the note in hv_fetch(). --jhi */
1003 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1006 k_flags = HVhek_UTF8;
1008 k_flags |= HVhek_FREEKEY;
1012 PERL_HASH_INTERNAL(hash, key, klen);
1014 PERL_HASH(hash, key, klen);
1017 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1018 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1021 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1022 if (HeHASH(entry) != hash) /* strings can't be equal */
1024 if (HeKLEN(entry) != (I32)klen)
1026 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1028 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1030 if (k_flags & HVhek_FREEKEY)
1032 /* if placeholder is here, it's already been deleted.... */
1033 if (HeVAL(entry) == &PL_sv_placeholder)
1036 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1038 /* okay, really delete the placeholder... */
1039 *oentry = HeNEXT(entry);
1041 xhv->xhv_fill--; /* HvFILL(hv)-- */
1042 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1045 hv_free_ent(hv, entry);
1046 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1047 if (xhv->xhv_keys == 0)
1048 HvHASKFLAGS_off(hv);
1049 xhv->xhv_placeholders--;
1053 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1054 S_hv_notallowed(aTHX_ k_flags, key, klen,
1055 "delete readonly key '%"SVf"' from"
1059 if (flags & G_DISCARD)
1062 sv = sv_2mortal(HeVAL(entry));
1063 HeVAL(entry) = &PL_sv_placeholder;
1067 * If a restricted hash, rather than really deleting the entry, put
1068 * a placeholder there. This marks the key as being "approved", so
1069 * we can still access via not-really-existing key without raising
1072 if (SvREADONLY(hv)) {
1073 HeVAL(entry) = &PL_sv_placeholder;
1074 /* We'll be saving this slot, so the number of allocated keys
1075 * doesn't go down, but the number placeholders goes up */
1076 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1078 *oentry = HeNEXT(entry);
1080 xhv->xhv_fill--; /* HvFILL(hv)-- */
1081 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1084 hv_free_ent(hv, entry);
1085 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1086 if (xhv->xhv_keys == 0)
1087 HvHASKFLAGS_off(hv);
1091 if (SvREADONLY(hv)) {
1092 S_hv_notallowed(aTHX_ k_flags, key, klen,
1093 "access disallowed key '%"SVf"' from"
1097 if (k_flags & HVhek_FREEKEY)
1103 =for apidoc hv_delete_ent
1105 Deletes a key/value pair in the hash. The value SV is removed from the
1106 hash and returned to the caller. The C<flags> value will normally be zero;
1107 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
1108 precomputed hash value, or 0 to ask for it to be computed.
1114 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
1116 register XPVHV* xhv;
1121 register HE **oentry;
1129 if (SvRMAGICAL(hv)) {
1132 hv_magic_check (hv, &needs_copy, &needs_store);
1134 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
1136 if (SvMAGICAL(sv)) {
1140 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
1141 /* No longer an element */
1142 sv_unmagic(sv, PERL_MAGIC_tiedelem);
1145 return Nullsv; /* element cannot be deleted */
1147 #ifdef ENV_IS_CASELESS
1148 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1149 key = SvPV(keysv, klen);
1150 keysv = sv_2mortal(newSVpvn(key,klen));
1151 (void)strupr(SvPVX(keysv));
1157 xhv = (XPVHV*)SvANY(hv);
1158 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1161 keysave = key = SvPV(keysv, klen);
1162 is_utf8 = (SvUTF8(keysv) != 0);
1165 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1167 k_flags = HVhek_UTF8;
1169 k_flags |= HVhek_FREEKEY;
1173 PERL_HASH_INTERNAL(hash, key, klen);
1175 PERL_HASH(hash, key, klen);
1178 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1179 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1182 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1183 if (HeHASH(entry) != hash) /* strings can't be equal */
1185 if (HeKLEN(entry) != (I32)klen)
1187 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1189 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1191 if (k_flags & HVhek_FREEKEY)
1194 /* if placeholder is here, it's already been deleted.... */
1195 if (HeVAL(entry) == &PL_sv_placeholder)
1198 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1200 /* okay, really delete the placeholder. */
1201 *oentry = HeNEXT(entry);
1203 xhv->xhv_fill--; /* HvFILL(hv)-- */
1204 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1207 hv_free_ent(hv, entry);
1208 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1209 if (xhv->xhv_keys == 0)
1210 HvHASKFLAGS_off(hv);
1211 xhv->xhv_placeholders--;
1214 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1215 S_hv_notallowed(aTHX_ k_flags, key, klen,
1216 "delete readonly key '%"SVf"' from"
1220 if (flags & G_DISCARD)
1223 sv = sv_2mortal(HeVAL(entry));
1224 HeVAL(entry) = &PL_sv_placeholder;
1228 * If a restricted hash, rather than really deleting the entry, put
1229 * a placeholder there. This marks the key as being "approved", so
1230 * we can still access via not-really-existing key without raising
1233 if (SvREADONLY(hv)) {
1234 HeVAL(entry) = &PL_sv_placeholder;
1235 /* We'll be saving this slot, so the number of allocated keys
1236 * doesn't go down, but the number placeholders goes up */
1237 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1239 *oentry = HeNEXT(entry);
1241 xhv->xhv_fill--; /* HvFILL(hv)-- */
1242 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1245 hv_free_ent(hv, entry);
1246 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1247 if (xhv->xhv_keys == 0)
1248 HvHASKFLAGS_off(hv);
1252 if (SvREADONLY(hv)) {
1253 S_hv_notallowed(aTHX_ k_flags, key, klen,
1254 "delete disallowed key '%"SVf"' from"
1258 if (k_flags & HVhek_FREEKEY)
1264 =for apidoc hv_exists
1266 Returns a boolean indicating whether the specified hash key exists. The
1267 C<klen> is the length of the key.
1273 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1275 register XPVHV* xhv;
1279 bool is_utf8 = FALSE;
1280 const char *keysave = key;
1291 if (SvRMAGICAL(hv)) {
1292 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1293 sv = sv_newmortal();
1295 /* This hack based on the code in hv_exists_ent seems to be
1296 the easiest way to pass the utf8 flag through and fix
1297 the bug in hv_exists for tied hashes with utf8 keys. */
1298 SV *keysv = sv_2mortal(newSVpvn(key, klen));
1300 key = (char *)keysv;
1303 mg_copy((SV*)hv, sv, key, klen);
1304 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1305 return (bool)SvTRUE(sv);
1307 #ifdef ENV_IS_CASELESS
1308 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1309 sv = sv_2mortal(newSVpvn(key,klen));
1310 key = strupr(SvPVX(sv));
1315 xhv = (XPVHV*)SvANY(hv);
1316 #ifndef DYNAMIC_ENV_FETCH
1317 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1322 STRLEN tmplen = klen;
1323 /* See the note in hv_fetch(). --jhi */
1324 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1327 k_flags = HVhek_UTF8;
1329 k_flags |= HVhek_FREEKEY;
1333 PERL_HASH_INTERNAL(hash, key, klen);
1335 PERL_HASH(hash, key, klen);
1338 #ifdef DYNAMIC_ENV_FETCH
1339 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1342 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1343 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1344 for (; entry; entry = HeNEXT(entry)) {
1345 if (HeHASH(entry) != hash) /* strings can't be equal */
1347 if (HeKLEN(entry) != klen)
1349 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1351 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1353 if (k_flags & HVhek_FREEKEY)
1355 /* If we find the key, but the value is a placeholder, return false. */
1356 if (HeVAL(entry) == &PL_sv_placeholder)
1361 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1362 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1364 char *env = PerlEnv_ENVgetenv_len(key,&len);
1366 sv = newSVpvn(env,len);
1368 (void)hv_store(hv,key,klen,sv,hash);
1369 if (k_flags & HVhek_FREEKEY)
1375 if (k_flags & HVhek_FREEKEY)
1382 =for apidoc hv_exists_ent
1384 Returns a boolean indicating whether the specified hash key exists. C<hash>
1385 can be a valid precomputed hash value, or 0 to ask for it to be
1392 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1394 register XPVHV* xhv;
1406 if (SvRMAGICAL(hv)) {
1407 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1408 SV* svret = sv_newmortal();
1409 sv = sv_newmortal();
1410 keysv = sv_2mortal(newSVsv(keysv));
1411 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1412 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1413 return (bool)SvTRUE(svret);
1415 #ifdef ENV_IS_CASELESS
1416 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1417 key = SvPV(keysv, klen);
1418 keysv = sv_2mortal(newSVpvn(key,klen));
1419 (void)strupr(SvPVX(keysv));
1425 xhv = (XPVHV*)SvANY(hv);
1426 #ifndef DYNAMIC_ENV_FETCH
1427 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1431 keysave = key = SvPV(keysv, klen);
1432 is_utf8 = (SvUTF8(keysv) != 0);
1434 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1436 k_flags = HVhek_UTF8;
1438 k_flags |= HVhek_FREEKEY;
1441 PERL_HASH_INTERNAL(hash, key, klen);
1443 PERL_HASH(hash, key, klen);
1445 #ifdef DYNAMIC_ENV_FETCH
1446 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1449 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1450 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1451 for (; entry; entry = HeNEXT(entry)) {
1452 if (HeHASH(entry) != hash) /* strings can't be equal */
1454 if (HeKLEN(entry) != (I32)klen)
1456 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1458 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1460 if (k_flags & HVhek_FREEKEY)
1462 /* If we find the key, but the value is a placeholder, return false. */
1463 if (HeVAL(entry) == &PL_sv_placeholder)
1467 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1468 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1470 char *env = PerlEnv_ENVgetenv_len(key,&len);
1472 sv = newSVpvn(env,len);
1474 (void)hv_store_ent(hv,keysv,sv,hash);
1475 if (k_flags & HVhek_FREEKEY)
1481 if (k_flags & HVhek_FREEKEY)
1487 S_hsplit(pTHX_ HV *hv)
1489 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1490 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1491 register I32 newsize = oldsize * 2;
1493 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1497 register HE **oentry;
1498 int longest_chain = 0;
1502 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1503 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1509 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1514 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1515 if (oldsize >= 64) {
1516 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1517 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1520 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1524 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1525 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1526 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1529 for (i=0; i<oldsize; i++,aep++) {
1530 int left_length = 0;
1531 int right_length = 0;
1533 if (!*aep) /* non-existent */
1536 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1537 if ((HeHASH(entry) & newsize) != (U32)i) {
1538 *oentry = HeNEXT(entry);
1539 HeNEXT(entry) = *bep;
1541 xhv->xhv_fill++; /* HvFILL(hv)++ */
1547 oentry = &HeNEXT(entry);
1551 if (!*aep) /* everything moved */
1552 xhv->xhv_fill--; /* HvFILL(hv)-- */
1553 /* I think we don't actually need to keep track of the longest length,
1554 merely flag if anything is too long. But for the moment while
1555 developing this code I'll track it. */
1556 if (left_length > longest_chain)
1557 longest_chain = left_length;
1558 if (right_length > longest_chain)
1559 longest_chain = right_length;
1563 /* Pick your policy for "hashing isn't working" here: */
1564 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1569 if (hv == PL_strtab) {
1570 /* Urg. Someone is doing something nasty to the string table.
1575 /* Awooga. Awooga. Pathological data. */
1576 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1577 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1580 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1581 was_shared = HvSHAREKEYS(hv);
1584 HvSHAREKEYS_off(hv);
1587 aep = (HE **) xhv->xhv_array;
1589 for (i=0; i<newsize; i++,aep++) {
1592 /* We're going to trash this HE's next pointer when we chain it
1593 into the new hash below, so store where we go next. */
1594 HE *next = HeNEXT(entry);
1598 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1603 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1604 hash, HeKFLAGS(entry));
1605 unshare_hek (HeKEY_hek(entry));
1606 HeKEY_hek(entry) = new_hek;
1608 /* Not shared, so simply write the new hash in. */
1609 HeHASH(entry) = hash;
1611 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1612 HEK_REHASH_on(HeKEY_hek(entry));
1613 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1615 /* Copy oentry to the correct new chain. */
1616 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1618 xhv->xhv_fill++; /* HvFILL(hv)++ */
1619 HeNEXT(entry) = *bep;
1625 Safefree (xhv->xhv_array);
1626 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1630 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1632 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1633 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1634 register I32 newsize;
1640 register HE **oentry;
1642 newsize = (I32) newmax; /* possible truncation here */
1643 if (newsize != newmax || newmax <= oldsize)
1645 while ((newsize & (1 + ~newsize)) != newsize) {
1646 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1648 if (newsize < newmax)
1650 if (newsize < newmax)
1651 return; /* overflow detection */
1653 a = xhv->xhv_array; /* HvARRAY(hv) */
1656 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1657 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1663 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1668 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1669 if (oldsize >= 64) {
1670 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1671 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1674 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1677 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1680 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1682 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1683 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1684 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1688 for (i=0; i<oldsize; i++,aep++) {
1689 if (!*aep) /* non-existent */
1691 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1692 if ((j = (HeHASH(entry) & newsize)) != i) {
1694 *oentry = HeNEXT(entry);
1695 if (!(HeNEXT(entry) = aep[j]))
1696 xhv->xhv_fill++; /* HvFILL(hv)++ */
1701 oentry = &HeNEXT(entry);
1703 if (!*aep) /* everything moved */
1704 xhv->xhv_fill--; /* HvFILL(hv)-- */
1711 Creates a new HV. The reference count is set to 1.
1720 register XPVHV* xhv;
1722 hv = (HV*)NEWSV(502,0);
1723 sv_upgrade((SV *)hv, SVt_PVHV);
1724 xhv = (XPVHV*)SvANY(hv);
1727 #ifndef NODEFAULT_SHAREKEYS
1728 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1731 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1732 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1733 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1734 (void)hv_iterinit(hv); /* so each() will start off right */
1739 Perl_newHVhv(pTHX_ HV *ohv)
1742 STRLEN hv_max, hv_fill;
1744 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1746 hv_max = HvMAX(ohv);
1748 if (!SvMAGICAL((SV *)ohv)) {
1749 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1751 bool shared = !!HvSHAREKEYS(ohv);
1752 HE **ents, **oents = (HE **)HvARRAY(ohv);
1754 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1757 /* In each bucket... */
1758 for (i = 0; i <= hv_max; i++) {
1759 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1766 /* Copy the linked list of entries. */
1767 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1768 U32 hash = HeHASH(oent);
1769 char *key = HeKEY(oent);
1770 STRLEN len = HeKLEN(oent);
1771 int flags = HeKFLAGS(oent);
1774 HeVAL(ent) = newSVsv(HeVAL(oent));
1776 = shared ? share_hek_flags(key, len, hash, flags)
1777 : save_hek_flags(key, len, hash, flags);
1788 HvFILL(hv) = hv_fill;
1789 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1793 /* Iterate over ohv, copying keys and values one at a time. */
1795 I32 riter = HvRITER(ohv);
1796 HE *eiter = HvEITER(ohv);
1798 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1799 while (hv_max && hv_max + 1 >= hv_fill * 2)
1800 hv_max = hv_max / 2;
1804 while ((entry = hv_iternext_flags(ohv, 0))) {
1805 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1806 newSVsv(HeVAL(entry)), HeHASH(entry),
1809 HvRITER(ohv) = riter;
1810 HvEITER(ohv) = eiter;
1817 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1824 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1825 PL_sub_generation++; /* may be deletion of method from stash */
1827 if (HeKLEN(entry) == HEf_SVKEY) {
1828 SvREFCNT_dec(HeKEY_sv(entry));
1829 Safefree(HeKEY_hek(entry));
1831 else if (HvSHAREKEYS(hv))
1832 unshare_hek(HeKEY_hek(entry));
1834 Safefree(HeKEY_hek(entry));
1839 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1843 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1844 PL_sub_generation++; /* may be deletion of method from stash */
1845 sv_2mortal(HeVAL(entry)); /* free between statements */
1846 if (HeKLEN(entry) == HEf_SVKEY) {
1847 sv_2mortal(HeKEY_sv(entry));
1848 Safefree(HeKEY_hek(entry));
1850 else if (HvSHAREKEYS(hv))
1851 unshare_hek(HeKEY_hek(entry));
1853 Safefree(HeKEY_hek(entry));
1858 =for apidoc hv_clear
1860 Clears a hash, making it empty.
1866 Perl_hv_clear(pTHX_ HV *hv)
1868 register XPVHV* xhv;
1872 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1874 xhv = (XPVHV*)SvANY(hv);
1876 if (SvREADONLY(hv)) {
1877 /* restricted hash: convert all keys to placeholders */
1880 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1881 entry = ((HE**)xhv->xhv_array)[i];
1882 for (; entry; entry = HeNEXT(entry)) {
1883 /* not already placeholder */
1884 if (HeVAL(entry) != &PL_sv_placeholder) {
1885 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1886 SV* keysv = hv_iterkeysv(entry);
1888 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1891 SvREFCNT_dec(HeVAL(entry));
1892 HeVAL(entry) = &PL_sv_placeholder;
1893 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1901 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1902 if (xhv->xhv_array /* HvARRAY(hv) */)
1903 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1904 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1909 HvHASKFLAGS_off(hv);
1914 S_hfreeentries(pTHX_ HV *hv)
1916 register HE **array;
1918 register HE *oentry = Null(HE*);
1929 array = HvARRAY(hv);
1930 /* make everyone else think the array is empty, so that the destructors
1931 * called for freed entries can't recusively mess with us */
1932 HvARRAY(hv) = Null(HE**);
1934 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1940 entry = HeNEXT(entry);
1941 hv_free_ent(hv, oentry);
1946 entry = array[riter];
1949 HvARRAY(hv) = array;
1950 (void)hv_iterinit(hv);
1954 =for apidoc hv_undef
1962 Perl_hv_undef(pTHX_ HV *hv)
1964 register XPVHV* xhv;
1967 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1968 xhv = (XPVHV*)SvANY(hv);
1970 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1973 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1974 Safefree(HvNAME(hv));
1977 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1978 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1979 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1986 =for apidoc hv_iterinit
1988 Prepares a starting point to traverse a hash table. Returns the number of
1989 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1990 currently only meaningful for hashes without tie magic.
1992 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1993 hash buckets that happen to be in use. If you still need that esoteric
1994 value, you can get it through the macro C<HvFILL(tb)>.
2001 Perl_hv_iterinit(pTHX_ HV *hv)
2003 register XPVHV* xhv;
2007 Perl_croak(aTHX_ "Bad hash");
2008 xhv = (XPVHV*)SvANY(hv);
2009 entry = xhv->xhv_eiter; /* HvEITER(hv) */
2010 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2012 hv_free_ent(hv, entry);
2014 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
2015 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
2016 /* used to be xhv->xhv_fill before 5.004_65 */
2017 return XHvTOTALKEYS(xhv);
2020 =for apidoc hv_iternext
2022 Returns entries from a hash iterator. See C<hv_iterinit>.
2024 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
2025 iterator currently points to, without losing your place or invalidating your
2026 iterator. Note that in this case the current entry is deleted from the hash
2027 with your iterator holding the last reference to it. Your iterator is flagged
2028 to free the entry on the next call to C<hv_iternext>, so you must not discard
2029 your iterator immediately else the entry will leak - call C<hv_iternext> to
2030 trigger the resource deallocation.
2036 Perl_hv_iternext(pTHX_ HV *hv)
2038 return hv_iternext_flags(hv, 0);
2042 =for apidoc hv_iternext_flags
2044 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
2045 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
2046 set the placeholders keys (for restricted hashes) will be returned in addition
2047 to normal keys. By default placeholders are automatically skipped over.
2048 Currently a placeholder is implemented with a value that is
2049 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
2050 restricted hashes may change, and the implementation currently is
2051 insufficiently abstracted for any change to be tidy.
2057 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2059 register XPVHV* xhv;
2065 Perl_croak(aTHX_ "Bad hash");
2066 xhv = (XPVHV*)SvANY(hv);
2067 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
2069 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
2070 SV *key = sv_newmortal();
2072 sv_setsv(key, HeSVKEY_force(entry));
2073 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
2079 /* one HE per MAGICAL hash */
2080 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
2082 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
2084 HeKEY_hek(entry) = hek;
2085 HeKLEN(entry) = HEf_SVKEY;
2087 magic_nextpack((SV*) hv,mg,key);
2089 /* force key to stay around until next time */
2090 HeSVKEY_set(entry, SvREFCNT_inc(key));
2091 return entry; /* beware, hent_val is not set */
2094 SvREFCNT_dec(HeVAL(entry));
2095 Safefree(HeKEY_hek(entry));
2097 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
2100 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
2101 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
2105 if (!xhv->xhv_array /* !HvARRAY(hv) */)
2106 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
2107 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
2109 /* At start of hash, entry is NULL. */
2112 entry = HeNEXT(entry);
2113 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2115 * Skip past any placeholders -- don't want to include them in
2118 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
2119 entry = HeNEXT(entry);
2124 /* OK. Come to the end of the current list. Grab the next one. */
2126 xhv->xhv_riter++; /* HvRITER(hv)++ */
2127 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2128 /* There is no next one. End of the hash. */
2129 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
2132 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
2133 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
2135 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2136 /* If we have an entry, but it's a placeholder, don't count it.
2138 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2139 entry = HeNEXT(entry);
2141 /* Will loop again if this linked list starts NULL
2142 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2143 or if we run through it and find only placeholders. */
2146 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2148 hv_free_ent(hv, oldentry);
2151 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
2152 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
2154 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
2159 =for apidoc hv_iterkey
2161 Returns the key from the current position of the hash iterator. See
2168 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
2170 if (HeKLEN(entry) == HEf_SVKEY) {
2172 char *p = SvPV(HeKEY_sv(entry), len);
2177 *retlen = HeKLEN(entry);
2178 return HeKEY(entry);
2182 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2184 =for apidoc hv_iterkeysv
2186 Returns the key as an C<SV*> from the current position of the hash
2187 iterator. The return value will always be a mortal copy of the key. Also
2194 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2196 if (HeKLEN(entry) != HEf_SVKEY) {
2197 HEK *hek = HeKEY_hek(entry);
2198 int flags = HEK_FLAGS(hek);
2201 if (flags & HVhek_WASUTF8) {
2203 Andreas would like keys he put in as utf8 to come back as utf8
2205 STRLEN utf8_len = HEK_LEN(hek);
2206 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
2208 sv = newSVpvn ((char*)as_utf8, utf8_len);
2210 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
2211 } else if (flags & HVhek_REHASH) {
2212 /* We don't have a pointer to the hv, so we have to replicate the
2213 flag into every HEK. This hv is using custom a hasing
2214 algorithm. Hence we can't return a shared string scalar, as
2215 that would contain the (wrong) hash value, and might get passed
2216 into an hv routine with a regular hash */
2218 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
2222 sv = newSVpvn_share(HEK_KEY(hek),
2223 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
2226 return sv_2mortal(sv);
2228 return sv_mortalcopy(HeKEY_sv(entry));
2232 =for apidoc hv_iterval
2234 Returns the value from the current position of the hash iterator. See
2241 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2243 if (SvRMAGICAL(hv)) {
2244 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2245 SV* sv = sv_newmortal();
2246 if (HeKLEN(entry) == HEf_SVKEY)
2247 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2248 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2252 return HeVAL(entry);
2256 =for apidoc hv_iternextsv
2258 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2265 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2268 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
2270 *key = hv_iterkey(he, retlen);
2271 return hv_iterval(hv, he);
2275 =for apidoc hv_magic
2277 Adds magic to a hash. See C<sv_magic>.
2283 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2285 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2288 #if 0 /* use the macro from hv.h instead */
2291 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2293 return HEK_KEY(share_hek(sv, len, hash));
2298 /* possibly free a shared string if no one has access to it
2299 * len and hash must both be valid for str.
2302 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2304 unshare_hek_or_pvn (NULL, str, len, hash);
2309 Perl_unshare_hek(pTHX_ HEK *hek)
2311 unshare_hek_or_pvn(hek, NULL, 0, 0);
2314 /* possibly free a shared string if no one has access to it
2315 hek if non-NULL takes priority over the other 3, else str, len and hash
2316 are used. If so, len and hash must both be valid for str.
2319 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2321 register XPVHV* xhv;
2323 register HE **oentry;
2326 bool is_utf8 = FALSE;
2328 const char *save = str;
2331 hash = HEK_HASH(hek);
2332 } else if (len < 0) {
2333 STRLEN tmplen = -len;
2335 /* See the note in hv_fetch(). --jhi */
2336 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2339 k_flags = HVhek_UTF8;
2341 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2344 /* what follows is the moral equivalent of:
2345 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2346 if (--*Svp == Nullsv)
2347 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2349 xhv = (XPVHV*)SvANY(PL_strtab);
2350 /* assert(xhv_array != 0) */
2352 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2353 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2355 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2356 if (HeKEY_hek(entry) != hek)
2362 int flags_masked = k_flags & HVhek_MASK;
2363 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2364 if (HeHASH(entry) != hash) /* strings can't be equal */
2366 if (HeKLEN(entry) != len)
2368 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2370 if (HeKFLAGS(entry) != flags_masked)
2378 if (--HeVAL(entry) == Nullsv) {
2379 *oentry = HeNEXT(entry);
2381 xhv->xhv_fill--; /* HvFILL(hv)-- */
2382 Safefree(HeKEY_hek(entry));
2384 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2388 UNLOCK_STRTAB_MUTEX;
2389 if (!found && ckWARN_d(WARN_INTERNAL))
2390 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2391 "Attempt to free non-existent shared string '%s'%s",
2392 hek ? HEK_KEY(hek) : str,
2393 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2394 if (k_flags & HVhek_FREEKEY)
2398 /* get a (constant) string ptr from the global string table
2399 * string will get added if it is not already there.
2400 * len and hash must both be valid for str.
2403 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2405 bool is_utf8 = FALSE;
2407 const char *save = str;
2410 STRLEN tmplen = -len;
2412 /* See the note in hv_fetch(). --jhi */
2413 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2415 /* If we were able to downgrade here, then than means that we were passed
2416 in a key which only had chars 0-255, but was utf8 encoded. */
2419 /* If we found we were able to downgrade the string to bytes, then
2420 we should flag that it needs upgrading on keys or each. Also flag
2421 that we need share_hek_flags to free the string. */
2423 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2426 return share_hek_flags (str, len, hash, flags);
2430 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2432 register XPVHV* xhv;
2434 register HE **oentry;
2437 int flags_masked = flags & HVhek_MASK;
2439 /* what follows is the moral equivalent of:
2441 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2442 hv_store(PL_strtab, str, len, Nullsv, hash);
2444 Can't rehash the shared string table, so not sure if it's worth
2445 counting the number of entries in the linked list
2447 xhv = (XPVHV*)SvANY(PL_strtab);
2448 /* assert(xhv_array != 0) */
2450 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2451 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2452 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2453 if (HeHASH(entry) != hash) /* strings can't be equal */
2455 if (HeKLEN(entry) != len)
2457 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2459 if (HeKFLAGS(entry) != flags_masked)
2466 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2467 HeVAL(entry) = Nullsv;
2468 HeNEXT(entry) = *oentry;
2470 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2471 if (i) { /* initial entry? */
2472 xhv->xhv_fill++; /* HvFILL(hv)++ */
2473 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2478 ++HeVAL(entry); /* use value slot as REFCNT */
2479 UNLOCK_STRTAB_MUTEX;
2481 if (flags & HVhek_FREEKEY)
2484 return HeKEY_hek(entry);
2489 =for apidoc hv_assert
2491 Check that a hash is in an internally consistent state.
2497 Perl_hv_assert(pTHX_ HV *hv)
2501 int placeholders = 0;
2504 I32 riter = HvRITER(hv);
2505 HE *eiter = HvEITER(hv);
2507 (void)hv_iterinit(hv);
2509 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2510 /* sanity check the values */
2511 if (HeVAL(entry) == &PL_sv_placeholder) {
2516 /* sanity check the keys */
2517 if (HeSVKEY(entry)) {
2518 /* Don't know what to check on SV keys. */
2519 } else if (HeKUTF8(entry)) {
2521 if (HeKWASUTF8(entry)) {
2522 PerlIO_printf(Perl_debug_log,
2523 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2524 (int) HeKLEN(entry), HeKEY(entry));
2527 } else if (HeKWASUTF8(entry)) {
2531 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2532 if (HvUSEDKEYS(hv) != real) {
2533 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2534 (int) real, (int) HvUSEDKEYS(hv));
2537 if (HvPLACEHOLDERS(hv) != placeholders) {
2538 PerlIO_printf(Perl_debug_log,
2539 "Count %d placeholder(s), but hash reports %d\n",
2540 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2544 if (withflags && ! HvHASKFLAGS(hv)) {
2545 PerlIO_printf(Perl_debug_log,
2546 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2553 HvRITER(hv) = riter; /* Restore hash iterator state */
2554 HvEITER(hv) = eiter;