3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 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
18 A HV structure represents a Perl hash. It consists mainly of an array
19 of pointers, each of which points to a linked list of HE structures. The
20 array is indexed by the hash function of the key, so each linked list
21 represents all the hash entries with the same hash value. Each HE contains
22 a pointer to the actual value, plus a pointer to a HEK structure which
23 holds the key and hash value.
31 #define PERL_HASH_INTERNAL_ACCESS
34 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
36 static const char S_strtab_error[]
37 = "Cannot modify shared string table in hv_%s";
43 HE* he = (HE*) Perl_get_arena(aTHX_ PERL_ARENA_SIZE, HE_SVSLOT);
44 HE * const heend = &he[PERL_ARENA_SIZE / sizeof(HE) - 1];
46 PL_body_roots[HE_SVSLOT] = he;
48 HeNEXT(he) = (HE*)(he + 1);
56 #define new_HE() (HE*)safemalloc(sizeof(HE))
57 #define del_HE(p) safefree((char*)p)
66 void ** const root = &PL_body_roots[HE_SVSLOT];
76 #define new_HE() new_he()
79 HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \
80 PL_body_roots[HE_SVSLOT] = p; \
88 S_save_hek_flags(const char *str, I32 len, U32 hash, int flags)
90 const int flags_masked = flags & HVhek_MASK;
94 Newx(k, HEK_BASESIZE + len + 2, char);
96 Copy(str, HEK_KEY(hek), len, char);
97 HEK_KEY(hek)[len] = 0;
100 HEK_FLAGS(hek) = (unsigned char)flags_masked | HVhek_UNSHARED;
102 if (flags & HVhek_FREEKEY)
107 /* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent
111 Perl_free_tied_hv_pool(pTHX)
114 HE *he = PL_hv_fetch_ent_mh;
117 Safefree(HeKEY_hek(he));
121 PL_hv_fetch_ent_mh = NULL;
124 #if defined(USE_ITHREADS)
126 Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param)
128 HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
130 PERL_UNUSED_ARG(param);
133 /* We already shared this hash key. */
134 (void)share_hek_hek(shared);
138 = share_hek_flags(HEK_KEY(source), HEK_LEN(source),
139 HEK_HASH(source), HEK_FLAGS(source));
140 ptr_table_store(PL_ptr_table, source, shared);
146 Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param)
152 /* look for it in the table first */
153 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
157 /* create anew and remember what it is */
159 ptr_table_store(PL_ptr_table, e, ret);
161 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
162 if (HeKLEN(e) == HEf_SVKEY) {
164 Newx(k, HEK_BASESIZE + sizeof(SV*), char);
165 HeKEY_hek(ret) = (HEK*)k;
166 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
169 /* This is hek_dup inlined, which seems to be important for speed
171 HEK * const source = HeKEY_hek(e);
172 HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
175 /* We already shared this hash key. */
176 (void)share_hek_hek(shared);
180 = share_hek_flags(HEK_KEY(source), HEK_LEN(source),
181 HEK_HASH(source), HEK_FLAGS(source));
182 ptr_table_store(PL_ptr_table, source, shared);
184 HeKEY_hek(ret) = shared;
187 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
189 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
192 #endif /* USE_ITHREADS */
195 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
198 SV * const sv = sv_newmortal();
199 if (!(flags & HVhek_FREEKEY)) {
200 sv_setpvn(sv, key, klen);
203 /* Need to free saved eventually assign to mortal SV */
204 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
205 sv_usepvn(sv, (char *) key, klen);
207 if (flags & HVhek_UTF8) {
210 Perl_croak(aTHX_ msg, SVfARG(sv));
213 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
219 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
220 the length of the key. The C<hash> parameter is the precomputed hash
221 value; if it is zero then Perl will compute it. The return value will be
222 NULL if the operation failed or if the value did not need to be actually
223 stored within the hash (as in the case of tied hashes). Otherwise it can
224 be dereferenced to get the original C<SV*>. Note that the caller is
225 responsible for suitably incrementing the reference count of C<val> before
226 the call, and decrementing it if the function returned NULL. Effectively
227 a successful hv_store takes ownership of one reference to C<val>. This is
228 usually what you want; a newly created SV has a reference count of one, so
229 if all your code does is create SVs then store them in a hash, hv_store
230 will own the only reference to the new SV, and your code doesn't need to do
231 anything further to tidy up. hv_store is not implemented as a call to
232 hv_store_ent, and does not create a temporary SV for the key, so if your
233 key data is not already in SV form then use hv_store in preference to
236 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
237 information on how to use this function on tied hashes.
243 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
255 return (SV **) hv_common(hv, NULL, key, klen, flags,
256 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
260 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
261 register U32 hash, int flags)
263 return (SV**) hv_common(hv, NULL, key, klen, flags,
264 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
268 =for apidoc hv_store_ent
270 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
271 parameter is the precomputed hash value; if it is zero then Perl will
272 compute it. The return value is the new hash entry so created. It will be
273 NULL if the operation failed or if the value did not need to be actually
274 stored within the hash (as in the case of tied hashes). Otherwise the
275 contents of the return value can be accessed using the C<He?> macros
276 described here. Note that the caller is responsible for suitably
277 incrementing the reference count of C<val> before the call, and
278 decrementing it if the function returned NULL. Effectively a successful
279 hv_store_ent takes ownership of one reference to C<val>. This is
280 usually what you want; a newly created SV has a reference count of one, so
281 if all your code does is create SVs then store them in a hash, hv_store
282 will own the only reference to the new SV, and your code doesn't need to do
283 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
284 unlike C<val> it does not take ownership of it, so maintaining the correct
285 reference count on C<key> is entirely the caller's responsibility. hv_store
286 is not implemented as a call to hv_store_ent, and does not create a temporary
287 SV for the key, so if your key data is not already in SV form then use
288 hv_store in preference to hv_store_ent.
290 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
291 information on how to use this function on tied hashes.
297 =for apidoc hv_exists
299 Returns a boolean indicating whether the specified hash key exists. The
300 C<klen> is the length of the key.
306 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
318 return hv_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0)
325 Returns the SV which corresponds to the specified key in the hash. The
326 C<klen> is the length of the key. If C<lval> is set then the fetch will be
327 part of a store. Check that the return value is non-null before
328 dereferencing it to an C<SV*>.
330 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
331 information on how to use this function on tied hashes.
337 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
349 return (SV **) hv_common(hv, NULL, key, klen, flags,
350 lval ? (HV_FETCH_JUST_SV | HV_FETCH_LVALUE)
351 : HV_FETCH_JUST_SV, NULL, 0);
355 =for apidoc hv_exists_ent
357 Returns a boolean indicating whether the specified hash key exists. C<hash>
358 can be a valid precomputed hash value, or 0 to ask for it to be
364 /* returns an HE * structure with the all fields set */
365 /* note that hent_val will be a mortal sv for MAGICAL hashes */
367 =for apidoc hv_fetch_ent
369 Returns the hash entry which corresponds to the specified key in the hash.
370 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
371 if you want the function to compute it. IF C<lval> is set then the fetch
372 will be part of a store. Make sure the return value is non-null before
373 accessing it. The return value when C<tb> is a tied hash is a pointer to a
374 static location, so be sure to make a copy of the structure if you need to
377 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
378 information on how to use this function on tied hashes.
384 Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
385 int flags, int action, SV *val, register U32 hash)
394 const int return_svp = action & HV_FETCH_JUST_SV;
398 if (SvTYPE(hv) == SVTYPEMASK)
401 assert(SvTYPE(hv) == SVt_PVHV);
403 if (SvSMAGICAL(hv) && SvGMAGICAL(hv) && !(action & HV_DISABLE_UVAR_XKEY)) {
405 if ((mg = mg_find((SV*)hv, PERL_MAGIC_uvar))) {
406 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
407 if (uf->uf_set == NULL) {
408 SV* obj = mg->mg_obj;
411 keysv = sv_2mortal(newSVpvn(key, klen));
412 if (flags & HVhek_UTF8)
416 mg->mg_obj = keysv; /* pass key */
417 uf->uf_index = action; /* pass action */
418 magic_getuvar((SV*)hv, mg);
419 keysv = mg->mg_obj; /* may have changed */
422 /* If the key may have changed, then we need to invalidate
423 any passed-in computed hash value. */
429 if (flags & HVhek_FREEKEY)
431 key = SvPV_const(keysv, klen);
433 is_utf8 = (SvUTF8(keysv) != 0);
435 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
438 if (action & HV_DELETE) {
439 return (void *) hv_delete_common(hv, keysv, key, klen,
440 flags | (is_utf8 ? HVhek_UTF8 : 0),
444 xhv = (XPVHV*)SvANY(hv);
446 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) {
447 if ( mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv))
449 /* FIXME should be able to skimp on the HE/HEK here when
450 HV_FETCH_JUST_SV is true. */
452 keysv = newSVpvn(key, klen);
457 keysv = newSVsv(keysv);
460 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
462 /* grab a fake HE/HEK pair from the pool or make a new one */
463 entry = PL_hv_fetch_ent_mh;
465 PL_hv_fetch_ent_mh = HeNEXT(entry);
469 Newx(k, HEK_BASESIZE + sizeof(SV*), char);
470 HeKEY_hek(entry) = (HEK*)k;
472 HeNEXT(entry) = NULL;
473 HeSVKEY_set(entry, keysv);
475 sv_upgrade(sv, SVt_PVLV);
477 /* so we can free entry when freeing sv */
478 LvTARG(sv) = (SV*)entry;
480 /* XXX remove at some point? */
481 if (flags & HVhek_FREEKEY)
485 return entry ? (void *) &HeVAL(entry) : NULL;
487 return (void *) entry;
489 #ifdef ENV_IS_CASELESS
490 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
492 for (i = 0; i < klen; ++i)
493 if (isLOWER(key[i])) {
494 /* Would be nice if we had a routine to do the
495 copy and upercase in a single pass through. */
496 const char * const nkey = strupr(savepvn(key,klen));
497 /* Note that this fetch is for nkey (the uppercased
498 key) whereas the store is for key (the original) */
499 void *result = hv_common(hv, NULL, nkey, klen,
500 HVhek_FREEKEY, /* free nkey */
501 0 /* non-LVAL fetch */
502 | HV_DISABLE_UVAR_XKEY
505 0 /* compute hash */);
506 if (!entry && (action & HV_FETCH_LVALUE)) {
507 /* This call will free key if necessary.
508 Do it this way to encourage compiler to tail
510 result = hv_common(hv, keysv, key, klen, flags,
512 | HV_DISABLE_UVAR_XKEY
516 if (flags & HVhek_FREEKEY)
524 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
525 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
526 /* I don't understand why hv_exists_ent has svret and sv,
527 whereas hv_exists only had one. */
528 SV * const svret = sv_newmortal();
531 if (keysv || is_utf8) {
533 keysv = newSVpvn(key, klen);
536 keysv = newSVsv(keysv);
538 mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
540 mg_copy((SV*)hv, sv, key, klen);
542 if (flags & HVhek_FREEKEY)
544 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
545 /* This cast somewhat evil, but I'm merely using NULL/
546 not NULL to return the boolean exists.
547 And I know hv is not NULL. */
548 return SvTRUE(svret) ? (void *)hv : NULL;
550 #ifdef ENV_IS_CASELESS
551 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
552 /* XXX This code isn't UTF8 clean. */
553 char * const keysave = (char * const)key;
554 /* Will need to free this, so set FREEKEY flag. */
555 key = savepvn(key,klen);
556 key = (const char*)strupr((char*)key);
561 if (flags & HVhek_FREEKEY) {
564 flags |= HVhek_FREEKEY;
568 else if (action & HV_FETCH_ISSTORE) {
571 hv_magic_check (hv, &needs_copy, &needs_store);
573 const bool save_taint = PL_tainted;
574 if (keysv || is_utf8) {
576 keysv = newSVpvn(key, klen);
580 PL_tainted = SvTAINTED(keysv);
581 keysv = sv_2mortal(newSVsv(keysv));
582 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
584 mg_copy((SV*)hv, val, key, klen);
587 TAINT_IF(save_taint);
589 if (flags & HVhek_FREEKEY)
593 #ifdef ENV_IS_CASELESS
594 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
595 /* XXX This code isn't UTF8 clean. */
596 const char *keysave = key;
597 /* Will need to free this, so set FREEKEY flag. */
598 key = savepvn(key,klen);
599 key = (const char*)strupr((char*)key);
604 if (flags & HVhek_FREEKEY) {
607 flags |= HVhek_FREEKEY;
615 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
616 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
617 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
622 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
624 HvARRAY(hv) = (HE**)array;
626 #ifdef DYNAMIC_ENV_FETCH
627 else if (action & HV_FETCH_ISEXISTS) {
628 /* for an %ENV exists, if we do an insert it's by a recursive
629 store call, so avoid creating HvARRAY(hv) right now. */
633 /* XXX remove at some point? */
634 if (flags & HVhek_FREEKEY)
642 char * const keysave = (char *)key;
643 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
647 flags &= ~HVhek_UTF8;
648 if (key != keysave) {
649 if (flags & HVhek_FREEKEY)
651 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
656 PERL_HASH_INTERNAL(hash, key, klen);
657 /* We don't have a pointer to the hv, so we have to replicate the
658 flag into every HEK, so that hv_iterkeysv can see it. */
659 /* And yes, you do need this even though you are not "storing" because
660 you can flip the flags below if doing an lval lookup. (And that
661 was put in to give the semantics Andreas was expecting.) */
662 flags |= HVhek_REHASH;
664 if (keysv && (SvIsCOW_shared_hash(keysv))) {
665 hash = SvSHARED_HASH(keysv);
667 PERL_HASH(hash, key, klen);
671 masked_flags = (flags & HVhek_MASK);
673 #ifdef DYNAMIC_ENV_FETCH
674 if (!HvARRAY(hv)) entry = NULL;
678 entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)];
680 for (; entry; 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) ^ masked_flags) & HVhek_UTF8)
690 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
691 if (HeKFLAGS(entry) != masked_flags) {
692 /* We match if HVhek_UTF8 bit in our flags and hash key's
693 match. But if entry was set previously with HVhek_WASUTF8
694 and key now doesn't (or vice versa) then we should change
695 the key's flag, as this is assignment. */
696 if (HvSHAREKEYS(hv)) {
697 /* Need to swap the key we have for a key with the flags we
698 need. As keys are shared we can't just write to the
699 flag, so we share the new one, unshare the old one. */
700 HEK * const new_hek = share_hek_flags(key, klen, hash,
702 unshare_hek (HeKEY_hek(entry));
703 HeKEY_hek(entry) = new_hek;
705 else if (hv == PL_strtab) {
706 /* PL_strtab is usually the only hash without HvSHAREKEYS,
707 so putting this test here is cheap */
708 if (flags & HVhek_FREEKEY)
710 Perl_croak(aTHX_ S_strtab_error,
711 action & HV_FETCH_LVALUE ? "fetch" : "store");
714 HeKFLAGS(entry) = masked_flags;
715 if (masked_flags & HVhek_ENABLEHVKFLAGS)
718 if (HeVAL(entry) == &PL_sv_placeholder) {
719 /* yes, can store into placeholder slot */
720 if (action & HV_FETCH_LVALUE) {
722 /* This preserves behaviour with the old hv_fetch
723 implementation which at this point would bail out
724 with a break; (at "if we find a placeholder, we
725 pretend we haven't found anything")
727 That break mean that if a placeholder were found, it
728 caused a call into hv_store, which in turn would
729 check magic, and if there is no magic end up pretty
730 much back at this point (in hv_store's code). */
733 /* LVAL fetch which actaully needs a store. */
735 HvPLACEHOLDERS(hv)--;
738 if (val != &PL_sv_placeholder)
739 HvPLACEHOLDERS(hv)--;
742 } else if (action & HV_FETCH_ISSTORE) {
743 SvREFCNT_dec(HeVAL(entry));
746 } else if (HeVAL(entry) == &PL_sv_placeholder) {
747 /* if we find a placeholder, we pretend we haven't found
751 if (flags & HVhek_FREEKEY)
754 return entry ? (void *) &HeVAL(entry) : NULL;
758 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
759 if (!(action & HV_FETCH_ISSTORE)
760 && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
762 const char * const env = PerlEnv_ENVgetenv_len(key,&len);
764 sv = newSVpvn(env,len);
766 return hv_common(hv, keysv, key, klen, flags,
767 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
773 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
774 hv_notallowed(flags, key, klen,
775 "Attempt to access disallowed key '%"SVf"' in"
776 " a restricted hash");
778 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
779 /* Not doing some form of store, so return failure. */
780 if (flags & HVhek_FREEKEY)
784 if (action & HV_FETCH_LVALUE) {
787 /* At this point the old hv_fetch code would call to hv_store,
788 which in turn might do some tied magic. So we need to make that
789 magic check happen. */
790 /* gonna assign to this, so it better be there */
791 /* If a fetch-as-store fails on the fetch, then the action is to
792 recurse once into "hv_store". If we didn't do this, then that
793 recursive call would call the key conversion routine again.
794 However, as we replace the original key with the converted
795 key, this would result in a double conversion, which would show
796 up as a bug if the conversion routine is not idempotent. */
797 return hv_common(hv, keysv, key, klen, flags,
798 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
800 /* XXX Surely that could leak if the fetch-was-store fails?
801 Just like the hv_fetch. */
805 /* Welcome to hv_store... */
808 /* Not sure if we can get here. I think the only case of oentry being
809 NULL is for %ENV with dynamic env fetch. But that should disappear
810 with magic in the previous code. */
813 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
815 HvARRAY(hv) = (HE**)array;
818 oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max];
821 /* share_hek_flags will do the free for us. This might be considered
824 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
825 else if (hv == PL_strtab) {
826 /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting
827 this test here is cheap */
828 if (flags & HVhek_FREEKEY)
830 Perl_croak(aTHX_ S_strtab_error,
831 action & HV_FETCH_LVALUE ? "fetch" : "store");
833 else /* gotta do the real thing */
834 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
836 HeNEXT(entry) = *oentry;
839 if (val == &PL_sv_placeholder)
840 HvPLACEHOLDERS(hv)++;
841 if (masked_flags & HVhek_ENABLEHVKFLAGS)
845 const HE *counter = HeNEXT(entry);
847 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
848 if (!counter) { /* initial entry? */
849 xhv->xhv_fill++; /* HvFILL(hv)++ */
850 } else if (xhv->xhv_keys > (IV)xhv->xhv_max) {
852 } else if(!HvREHASH(hv)) {
855 while ((counter = HeNEXT(counter)))
858 if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) {
859 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit
860 bucket splits on a rehashed hash, as we're not going to
861 split it again, and if someone is lucky (evil) enough to
862 get all the keys in one list they could exhaust our memory
863 as we repeatedly double the number of buckets on every
864 entry. Linear search feels a less worse thing to do. */
871 return entry ? (void *) &HeVAL(entry) : NULL;
873 return (void *) entry;
877 S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store)
879 const MAGIC *mg = SvMAGIC(hv);
883 if (isUPPER(mg->mg_type)) {
885 if (mg->mg_type == PERL_MAGIC_tied) {
886 *needs_store = FALSE;
887 return; /* We've set all there is to set. */
890 mg = mg->mg_moremagic;
895 =for apidoc hv_scalar
897 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
903 Perl_hv_scalar(pTHX_ HV *hv)
907 if (SvRMAGICAL(hv)) {
908 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_tied);
910 return magic_scalarpack(hv, mg);
915 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
916 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
924 =for apidoc hv_delete
926 Deletes a key/value pair in the hash. The value SV is removed from the
927 hash and returned to the caller. The C<klen> is the length of the key.
928 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
935 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
942 k_flags = HVhek_UTF8;
947 return (SV *) hv_common(hv, NULL, key, klen, k_flags, flags | HV_DELETE,
952 =for apidoc hv_delete_ent
954 Deletes a key/value pair in the hash. The value SV is removed from the
955 hash and returned to the caller. The C<flags> value will normally be zero;
956 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
957 precomputed hash value, or 0 to ask for it to be computed.
963 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
964 int k_flags, I32 d_flags, U32 hash)
969 register HE **oentry;
970 HE *const *first_entry;
971 bool is_utf8 = (k_flags & HVhek_UTF8) ? TRUE : FALSE;
974 if (SvRMAGICAL(hv)) {
977 hv_magic_check (hv, &needs_copy, &needs_store);
981 entry = (HE *) hv_common(hv, keysv, key, klen,
982 k_flags & ~HVhek_FREEKEY,
983 HV_FETCH_LVALUE|HV_DISABLE_UVAR_XKEY,
985 sv = entry ? HeVAL(entry) : NULL;
991 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
992 /* No longer an element */
993 sv_unmagic(sv, PERL_MAGIC_tiedelem);
996 return NULL; /* element cannot be deleted */
998 #ifdef ENV_IS_CASELESS
999 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1000 /* XXX This code isn't UTF8 clean. */
1001 keysv = sv_2mortal(newSVpvn(key,klen));
1002 if (k_flags & HVhek_FREEKEY) {
1005 key = strupr(SvPVX(keysv));
1014 xhv = (XPVHV*)SvANY(hv);
1019 const char * const keysave = key;
1020 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1023 k_flags |= HVhek_UTF8;
1025 k_flags &= ~HVhek_UTF8;
1026 if (key != keysave) {
1027 if (k_flags & HVhek_FREEKEY) {
1028 /* This shouldn't happen if our caller does what we expect,
1029 but strictly the API allows it. */
1032 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1034 HvHASKFLAGS_on((SV*)hv);
1038 PERL_HASH_INTERNAL(hash, key, klen);
1040 if (keysv && (SvIsCOW_shared_hash(keysv))) {
1041 hash = SvSHARED_HASH(keysv);
1043 PERL_HASH(hash, key, klen);
1047 masked_flags = (k_flags & HVhek_MASK);
1049 first_entry = oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)];
1051 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) {
1053 if (HeHASH(entry) != hash) /* strings can't be equal */
1055 if (HeKLEN(entry) != (I32)klen)
1057 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1059 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1062 if (hv == PL_strtab) {
1063 if (k_flags & HVhek_FREEKEY)
1065 Perl_croak(aTHX_ S_strtab_error, "delete");
1068 /* if placeholder is here, it's already been deleted.... */
1069 if (HeVAL(entry) == &PL_sv_placeholder) {
1070 if (k_flags & HVhek_FREEKEY)
1074 if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1075 hv_notallowed(k_flags, key, klen,
1076 "Attempt to delete readonly key '%"SVf"' from"
1077 " a restricted hash");
1079 if (k_flags & HVhek_FREEKEY)
1082 if (d_flags & G_DISCARD)
1085 sv = sv_2mortal(HeVAL(entry));
1086 HeVAL(entry) = &PL_sv_placeholder;
1090 * If a restricted hash, rather than really deleting the entry, put
1091 * a placeholder there. This marks the key as being "approved", so
1092 * we can still access via not-really-existing key without raising
1095 if (SvREADONLY(hv)) {
1096 SvREFCNT_dec(HeVAL(entry));
1097 HeVAL(entry) = &PL_sv_placeholder;
1098 /* We'll be saving this slot, so the number of allocated keys
1099 * doesn't go down, but the number placeholders goes up */
1100 HvPLACEHOLDERS(hv)++;
1102 *oentry = HeNEXT(entry);
1104 xhv->xhv_fill--; /* HvFILL(hv)-- */
1106 if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */)
1109 hv_free_ent(hv, entry);
1110 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
1111 if (xhv->xhv_keys == 0)
1112 HvHASKFLAGS_off(hv);
1116 if (SvREADONLY(hv)) {
1117 hv_notallowed(k_flags, key, klen,
1118 "Attempt to delete disallowed key '%"SVf"' from"
1119 " a restricted hash");
1122 if (k_flags & HVhek_FREEKEY)
1128 S_hsplit(pTHX_ HV *hv)
1131 register XPVHV* const xhv = (XPVHV*)SvANY(hv);
1132 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1133 register I32 newsize = oldsize * 2;
1135 char *a = (char*) HvARRAY(hv);
1137 register HE **oentry;
1138 int longest_chain = 0;
1141 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1142 (void*)hv, (int) oldsize);*/
1144 if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) {
1145 /* Can make this clear any placeholders first for non-restricted hashes,
1146 even though Storable rebuilds restricted hashes by putting in all the
1147 placeholders (first) before turning on the readonly flag, because
1148 Storable always pre-splits the hash. */
1149 hv_clear_placeholders(hv);
1153 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1154 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1155 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1161 Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1164 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1165 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1170 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1172 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1174 if (oldsize >= 64) {
1175 offer_nice_chunk(HvARRAY(hv),
1176 PERL_HV_ARRAY_ALLOC_BYTES(oldsize)
1177 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0));
1180 Safefree(HvARRAY(hv));
1184 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1185 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1186 HvARRAY(hv) = (HE**) a;
1189 for (i=0; i<oldsize; i++,aep++) {
1190 int left_length = 0;
1191 int right_length = 0;
1195 if (!*aep) /* non-existent */
1198 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1199 if ((HeHASH(entry) & newsize) != (U32)i) {
1200 *oentry = HeNEXT(entry);
1201 HeNEXT(entry) = *bep;
1203 xhv->xhv_fill++; /* HvFILL(hv)++ */
1209 oentry = &HeNEXT(entry);
1213 if (!*aep) /* everything moved */
1214 xhv->xhv_fill--; /* HvFILL(hv)-- */
1215 /* I think we don't actually need to keep track of the longest length,
1216 merely flag if anything is too long. But for the moment while
1217 developing this code I'll track it. */
1218 if (left_length > longest_chain)
1219 longest_chain = left_length;
1220 if (right_length > longest_chain)
1221 longest_chain = right_length;
1225 /* Pick your policy for "hashing isn't working" here: */
1226 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1231 if (hv == PL_strtab) {
1232 /* Urg. Someone is doing something nasty to the string table.
1237 /* Awooga. Awooga. Pathological data. */
1238 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", (void*)hv,
1239 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1242 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1243 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1245 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1248 was_shared = HvSHAREKEYS(hv);
1251 HvSHAREKEYS_off(hv);
1256 for (i=0; i<newsize; i++,aep++) {
1257 register HE *entry = *aep;
1259 /* We're going to trash this HE's next pointer when we chain it
1260 into the new hash below, so store where we go next. */
1261 HE * const next = HeNEXT(entry);
1266 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1271 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1272 hash, HeKFLAGS(entry));
1273 unshare_hek (HeKEY_hek(entry));
1274 HeKEY_hek(entry) = new_hek;
1276 /* Not shared, so simply write the new hash in. */
1277 HeHASH(entry) = hash;
1279 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1280 HEK_REHASH_on(HeKEY_hek(entry));
1281 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1283 /* Copy oentry to the correct new chain. */
1284 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1286 xhv->xhv_fill++; /* HvFILL(hv)++ */
1287 HeNEXT(entry) = *bep;
1293 Safefree (HvARRAY(hv));
1294 HvARRAY(hv) = (HE **)a;
1298 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1301 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1302 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1303 register I32 newsize;
1308 register HE **oentry;
1310 newsize = (I32) newmax; /* possible truncation here */
1311 if (newsize != newmax || newmax <= oldsize)
1313 while ((newsize & (1 + ~newsize)) != newsize) {
1314 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1316 if (newsize < newmax)
1318 if (newsize < newmax)
1319 return; /* overflow detection */
1321 a = (char *) HvARRAY(hv);
1324 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1325 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1326 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1332 Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1335 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1336 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1341 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1343 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1345 if (oldsize >= 64) {
1346 offer_nice_chunk(HvARRAY(hv),
1347 PERL_HV_ARRAY_ALLOC_BYTES(oldsize)
1348 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0));
1351 Safefree(HvARRAY(hv));
1354 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1357 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1359 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1360 HvARRAY(hv) = (HE **) a;
1361 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1365 for (i=0; i<oldsize; i++,aep++) {
1366 if (!*aep) /* non-existent */
1368 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1369 register I32 j = (HeHASH(entry) & newsize);
1373 *oentry = HeNEXT(entry);
1374 if (!(HeNEXT(entry) = aep[j]))
1375 xhv->xhv_fill++; /* HvFILL(hv)++ */
1380 oentry = &HeNEXT(entry);
1382 if (!*aep) /* everything moved */
1383 xhv->xhv_fill--; /* HvFILL(hv)-- */
1390 Creates a new HV. The reference count is set to 1.
1398 register XPVHV* xhv;
1399 HV * const hv = (HV*)newSV_type(SVt_PVHV);
1400 xhv = (XPVHV*)SvANY(hv);
1402 #ifndef NODEFAULT_SHAREKEYS
1403 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1406 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1407 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1412 Perl_newHVhv(pTHX_ HV *ohv)
1414 HV * const hv = newHV();
1415 STRLEN hv_max, hv_fill;
1417 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1419 hv_max = HvMAX(ohv);
1421 if (!SvMAGICAL((SV *)ohv)) {
1422 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1424 const bool shared = !!HvSHAREKEYS(ohv);
1425 HE **ents, ** const oents = (HE **)HvARRAY(ohv);
1427 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1430 /* In each bucket... */
1431 for (i = 0; i <= hv_max; i++) {
1433 HE *oent = oents[i];
1440 /* Copy the linked list of entries. */
1441 for (; oent; oent = HeNEXT(oent)) {
1442 const U32 hash = HeHASH(oent);
1443 const char * const key = HeKEY(oent);
1444 const STRLEN len = HeKLEN(oent);
1445 const int flags = HeKFLAGS(oent);
1446 HE * const ent = new_HE();
1448 HeVAL(ent) = newSVsv(HeVAL(oent));
1450 = shared ? share_hek_flags(key, len, hash, flags)
1451 : save_hek_flags(key, len, hash, flags);
1462 HvFILL(hv) = hv_fill;
1463 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1467 /* Iterate over ohv, copying keys and values one at a time. */
1469 const I32 riter = HvRITER_get(ohv);
1470 HE * const eiter = HvEITER_get(ohv);
1472 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1473 while (hv_max && hv_max + 1 >= hv_fill * 2)
1474 hv_max = hv_max / 2;
1478 while ((entry = hv_iternext_flags(ohv, 0))) {
1479 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1480 newSVsv(HeVAL(entry)), HeHASH(entry),
1483 HvRITER_set(ohv, riter);
1484 HvEITER_set(ohv, eiter);
1490 /* A rather specialised version of newHVhv for copying %^H, ensuring all the
1491 magic stays on it. */
1493 Perl_hv_copy_hints_hv(pTHX_ HV *const ohv)
1495 HV * const hv = newHV();
1498 if (ohv && (hv_fill = HvFILL(ohv))) {
1499 STRLEN hv_max = HvMAX(ohv);
1501 const I32 riter = HvRITER_get(ohv);
1502 HE * const eiter = HvEITER_get(ohv);
1504 while (hv_max && hv_max + 1 >= hv_fill * 2)
1505 hv_max = hv_max / 2;
1509 while ((entry = hv_iternext_flags(ohv, 0))) {
1510 SV *const sv = newSVsv(HeVAL(entry));
1511 sv_magic(sv, NULL, PERL_MAGIC_hintselem,
1512 (char *)newSVhek (HeKEY_hek(entry)), HEf_SVKEY);
1513 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1514 sv, HeHASH(entry), HeKFLAGS(entry));
1516 HvRITER_set(ohv, riter);
1517 HvEITER_set(ohv, eiter);
1519 hv_magic(hv, NULL, PERL_MAGIC_hints);
1524 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1532 if (val && isGV(val) && GvCVu(val) && HvNAME_get(hv))
1533 mro_method_changed_in(hv); /* deletion of method from stash */
1535 if (HeKLEN(entry) == HEf_SVKEY) {
1536 SvREFCNT_dec(HeKEY_sv(entry));
1537 Safefree(HeKEY_hek(entry));
1539 else if (HvSHAREKEYS(hv))
1540 unshare_hek(HeKEY_hek(entry));
1542 Safefree(HeKEY_hek(entry));
1547 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1552 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */
1553 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */
1554 if (HeKLEN(entry) == HEf_SVKEY) {
1555 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry)));
1557 hv_free_ent(hv, entry);
1561 =for apidoc hv_clear
1563 Clears a hash, making it empty.
1569 Perl_hv_clear(pTHX_ HV *hv)
1572 register XPVHV* xhv;
1576 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1578 xhv = (XPVHV*)SvANY(hv);
1580 if (SvREADONLY(hv) && HvARRAY(hv) != NULL) {
1581 /* restricted hash: convert all keys to placeholders */
1583 for (i = 0; i <= xhv->xhv_max; i++) {
1584 HE *entry = (HvARRAY(hv))[i];
1585 for (; entry; entry = HeNEXT(entry)) {
1586 /* not already placeholder */
1587 if (HeVAL(entry) != &PL_sv_placeholder) {
1588 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1589 SV* const keysv = hv_iterkeysv(entry);
1591 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1594 SvREFCNT_dec(HeVAL(entry));
1595 HeVAL(entry) = &PL_sv_placeholder;
1596 HvPLACEHOLDERS(hv)++;
1604 HvPLACEHOLDERS_set(hv, 0);
1606 Zero(HvARRAY(hv), xhv->xhv_max+1 /* HvMAX(hv)+1 */, HE*);
1611 HvHASKFLAGS_off(hv);
1616 mro_isa_changed_in(hv);
1617 HvEITER_set(hv, NULL);
1622 =for apidoc hv_clear_placeholders
1624 Clears any placeholders from a hash. If a restricted hash has any of its keys
1625 marked as readonly and the key is subsequently deleted, the key is not actually
1626 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1627 it so it will be ignored by future operations such as iterating over the hash,
1628 but will still allow the hash to have a value reassigned to the key at some
1629 future point. This function clears any such placeholder keys from the hash.
1630 See Hash::Util::lock_keys() for an example of its use.
1636 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1639 const U32 items = (U32)HvPLACEHOLDERS_get(hv);
1642 clear_placeholders(hv, items);
1646 S_clear_placeholders(pTHX_ HV *hv, U32 items)
1656 /* Loop down the linked list heads */
1658 HE **oentry = &(HvARRAY(hv))[i];
1661 while ((entry = *oentry)) {
1662 if (HeVAL(entry) == &PL_sv_placeholder) {
1663 *oentry = HeNEXT(entry);
1664 if (first && !*oentry)
1665 HvFILL(hv)--; /* This linked list is now empty. */
1666 if (entry == HvEITER_get(hv))
1669 hv_free_ent(hv, entry);
1673 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv);
1674 if (HvKEYS(hv) == 0)
1675 HvHASKFLAGS_off(hv);
1676 HvPLACEHOLDERS_set(hv, 0);
1680 oentry = &HeNEXT(entry);
1685 /* You can't get here, hence assertion should always fail. */
1686 assert (items == 0);
1691 S_hfreeentries(pTHX_ HV *hv)
1693 /* This is the array that we're going to restore */
1694 HE **const orig_array = HvARRAY(hv);
1702 /* If the hash is actually a symbol table with a name, look after the
1704 struct xpvhv_aux *iter = HvAUX(hv);
1706 name = iter->xhv_name;
1707 iter->xhv_name = NULL;
1712 /* orig_array remains unchanged throughout the loop. If after freeing all
1713 the entries it turns out that one of the little blighters has triggered
1714 an action that has caused HvARRAY to be re-allocated, then we set
1715 array to the new HvARRAY, and try again. */
1718 /* This is the one we're going to try to empty. First time round
1719 it's the original array. (Hopefully there will only be 1 time
1721 HE ** const array = HvARRAY(hv);
1724 /* Because we have taken xhv_name out, the only allocated pointer
1725 in the aux structure that might exist is the backreference array.
1730 struct mro_meta *meta;
1731 struct xpvhv_aux *iter = HvAUX(hv);
1732 /* If there are weak references to this HV, we need to avoid
1733 freeing them up here. In particular we need to keep the AV
1734 visible as what we're deleting might well have weak references
1735 back to this HV, so the for loop below may well trigger
1736 the removal of backreferences from this array. */
1738 if (iter->xhv_backreferences) {
1739 /* So donate them to regular backref magic to keep them safe.
1740 The sv_magic will increase the reference count of the AV,
1741 so we need to drop it first. */
1742 SvREFCNT_dec(iter->xhv_backreferences);
1743 if (AvFILLp(iter->xhv_backreferences) == -1) {
1744 /* Turns out that the array is empty. Just free it. */
1745 SvREFCNT_dec(iter->xhv_backreferences);
1748 sv_magic((SV*)hv, (SV*)iter->xhv_backreferences,
1749 PERL_MAGIC_backref, NULL, 0);
1751 iter->xhv_backreferences = NULL;
1754 entry = iter->xhv_eiter; /* HvEITER(hv) */
1755 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1757 hv_free_ent(hv, entry);
1759 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1760 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1762 if((meta = iter->xhv_mro_meta)) {
1763 if(meta->mro_linear_dfs) SvREFCNT_dec(meta->mro_linear_dfs);
1764 if(meta->mro_linear_c3) SvREFCNT_dec(meta->mro_linear_c3);
1765 if(meta->mro_nextmethod) SvREFCNT_dec(meta->mro_nextmethod);
1767 iter->xhv_mro_meta = NULL;
1770 /* There are now no allocated pointers in the aux structure. */
1772 SvFLAGS(hv) &= ~SVf_OOK; /* Goodbye, aux structure. */
1773 /* What aux structure? */
1776 /* make everyone else think the array is empty, so that the destructors
1777 * called for freed entries can't recusively mess with us */
1780 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1784 /* Loop down the linked list heads */
1785 HE *entry = array[i];
1788 register HE * const oentry = entry;
1789 entry = HeNEXT(entry);
1790 hv_free_ent(hv, oentry);
1794 /* As there are no allocated pointers in the aux structure, it's now
1795 safe to free the array we just cleaned up, if it's not the one we're
1796 going to put back. */
1797 if (array != orig_array) {
1802 /* Good. No-one added anything this time round. */
1807 /* Someone attempted to iterate or set the hash name while we had
1808 the array set to 0. We'll catch backferences on the next time
1809 round the while loop. */
1810 assert(HvARRAY(hv));
1812 if (HvAUX(hv)->xhv_name) {
1813 unshare_hek_or_pvn(HvAUX(hv)->xhv_name, 0, 0, 0);
1817 if (--attempts == 0) {
1818 Perl_die(aTHX_ "panic: hfreeentries failed to free hash - something is repeatedly re-creating entries");
1822 HvARRAY(hv) = orig_array;
1824 /* If the hash was actually a symbol table, put the name back. */
1826 /* We have restored the original array. If name is non-NULL, then
1827 the original array had an aux structure at the end. So this is
1829 SvFLAGS(hv) |= SVf_OOK;
1830 HvAUX(hv)->xhv_name = name;
1835 =for apidoc hv_undef
1843 Perl_hv_undef(pTHX_ HV *hv)
1846 register XPVHV* xhv;
1851 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1852 xhv = (XPVHV*)SvANY(hv);
1854 if ((name = HvNAME_get(hv)) && !PL_dirty)
1855 mro_isa_changed_in(hv);
1860 hv_delete(PL_stashcache, name, HvNAMELEN_get(hv), G_DISCARD);
1861 hv_name_set(hv, NULL, 0, 0);
1863 SvFLAGS(hv) &= ~SVf_OOK;
1864 Safefree(HvARRAY(hv));
1865 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1867 HvPLACEHOLDERS_set(hv, 0);
1873 static struct xpvhv_aux*
1874 S_hv_auxinit(HV *hv) {
1875 struct xpvhv_aux *iter;
1879 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1880 + sizeof(struct xpvhv_aux), char);
1882 array = (char *) HvARRAY(hv);
1883 Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1884 + sizeof(struct xpvhv_aux), char);
1886 HvARRAY(hv) = (HE**) array;
1887 /* SvOOK_on(hv) attacks the IV flags. */
1888 SvFLAGS(hv) |= SVf_OOK;
1891 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1892 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1894 iter->xhv_backreferences = 0;
1895 iter->xhv_mro_meta = NULL;
1900 =for apidoc hv_iterinit
1902 Prepares a starting point to traverse a hash table. Returns the number of
1903 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1904 currently only meaningful for hashes without tie magic.
1906 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1907 hash buckets that happen to be in use. If you still need that esoteric
1908 value, you can get it through the macro C<HvFILL(tb)>.
1915 Perl_hv_iterinit(pTHX_ HV *hv)
1918 Perl_croak(aTHX_ "Bad hash");
1921 struct xpvhv_aux * const iter = HvAUX(hv);
1922 HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */
1923 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1925 hv_free_ent(hv, entry);
1927 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1928 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1933 /* used to be xhv->xhv_fill before 5.004_65 */
1934 return HvTOTALKEYS(hv);
1938 Perl_hv_riter_p(pTHX_ HV *hv) {
1939 struct xpvhv_aux *iter;
1942 Perl_croak(aTHX_ "Bad hash");
1944 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1945 return &(iter->xhv_riter);
1949 Perl_hv_eiter_p(pTHX_ HV *hv) {
1950 struct xpvhv_aux *iter;
1953 Perl_croak(aTHX_ "Bad hash");
1955 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1956 return &(iter->xhv_eiter);
1960 Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) {
1961 struct xpvhv_aux *iter;
1964 Perl_croak(aTHX_ "Bad hash");
1972 iter = hv_auxinit(hv);
1974 iter->xhv_riter = riter;
1978 Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) {
1979 struct xpvhv_aux *iter;
1982 Perl_croak(aTHX_ "Bad hash");
1987 /* 0 is the default so don't go malloc()ing a new structure just to
1992 iter = hv_auxinit(hv);
1994 iter->xhv_eiter = eiter;
1998 Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2001 struct xpvhv_aux *iter;
2004 PERL_UNUSED_ARG(flags);
2007 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2011 if (iter->xhv_name) {
2012 unshare_hek_or_pvn(iter->xhv_name, 0, 0, 0);
2018 iter = hv_auxinit(hv);
2020 PERL_HASH(hash, name, len);
2021 iter->xhv_name = name ? share_hek(name, len, hash) : NULL;
2025 Perl_hv_backreferences_p(pTHX_ HV *hv) {
2026 struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2027 PERL_UNUSED_CONTEXT;
2028 return &(iter->xhv_backreferences);
2032 Perl_hv_kill_backrefs(pTHX_ HV *hv) {
2038 av = HvAUX(hv)->xhv_backreferences;
2041 HvAUX(hv)->xhv_backreferences = 0;
2042 Perl_sv_kill_backrefs(aTHX_ (SV*) hv, av);
2047 hv_iternext is implemented as a macro in hv.h
2049 =for apidoc hv_iternext
2051 Returns entries from a hash iterator. See C<hv_iterinit>.
2053 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
2054 iterator currently points to, without losing your place or invalidating your
2055 iterator. Note that in this case the current entry is deleted from the hash
2056 with your iterator holding the last reference to it. Your iterator is flagged
2057 to free the entry on the next call to C<hv_iternext>, so you must not discard
2058 your iterator immediately else the entry will leak - call C<hv_iternext> to
2059 trigger the resource deallocation.
2061 =for apidoc hv_iternext_flags
2063 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
2064 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
2065 set the placeholders keys (for restricted hashes) will be returned in addition
2066 to normal keys. By default placeholders are automatically skipped over.
2067 Currently a placeholder is implemented with a value that is
2068 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
2069 restricted hashes may change, and the implementation currently is
2070 insufficiently abstracted for any change to be tidy.
2076 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2079 register XPVHV* xhv;
2083 struct xpvhv_aux *iter;
2086 Perl_croak(aTHX_ "Bad hash");
2088 xhv = (XPVHV*)SvANY(hv);
2091 /* Too many things (well, pp_each at least) merrily assume that you can
2092 call iv_iternext without calling hv_iterinit, so we'll have to deal
2098 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2099 if (SvMAGICAL(hv) && SvRMAGICAL(hv)) {
2100 if ( ( mg = mg_find((SV*)hv, PERL_MAGIC_tied) ) ) {
2101 SV * const key = sv_newmortal();
2103 sv_setsv(key, HeSVKEY_force(entry));
2104 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
2110 /* one HE per MAGICAL hash */
2111 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
2113 Newxz(k, HEK_BASESIZE + sizeof(SV*), char);
2115 HeKEY_hek(entry) = hek;
2116 HeKLEN(entry) = HEf_SVKEY;
2118 magic_nextpack((SV*) hv,mg,key);
2120 /* force key to stay around until next time */
2121 HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key));
2122 return entry; /* beware, hent_val is not set */
2125 SvREFCNT_dec(HeVAL(entry));
2126 Safefree(HeKEY_hek(entry));
2128 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
2132 #if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */
2133 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
2136 /* The prime_env_iter() on VMS just loaded up new hash values
2137 * so the iteration count needs to be reset back to the beginning
2141 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2146 /* hv_iterint now ensures this. */
2147 assert (HvARRAY(hv));
2149 /* At start of hash, entry is NULL. */
2152 entry = HeNEXT(entry);
2153 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2155 * Skip past any placeholders -- don't want to include them in
2158 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
2159 entry = HeNEXT(entry);
2164 /* OK. Come to the end of the current list. Grab the next one. */
2166 iter->xhv_riter++; /* HvRITER(hv)++ */
2167 if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2168 /* There is no next one. End of the hash. */
2169 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
2172 entry = (HvARRAY(hv))[iter->xhv_riter];
2174 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2175 /* If we have an entry, but it's a placeholder, don't count it.
2177 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2178 entry = HeNEXT(entry);
2180 /* Will loop again if this linked list starts NULL
2181 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2182 or if we run through it and find only placeholders. */
2185 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2187 hv_free_ent(hv, oldentry);
2190 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
2191 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", (void*)hv, (void*)entry);*/
2193 iter->xhv_eiter = entry; /* HvEITER(hv) = entry */
2198 =for apidoc hv_iterkey
2200 Returns the key from the current position of the hash iterator. See
2207 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
2209 if (HeKLEN(entry) == HEf_SVKEY) {
2211 char * const p = SvPV(HeKEY_sv(entry), len);
2216 *retlen = HeKLEN(entry);
2217 return HeKEY(entry);
2221 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2223 =for apidoc hv_iterkeysv
2225 Returns the key as an C<SV*> from the current position of the hash
2226 iterator. The return value will always be a mortal copy of the key. Also
2233 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2235 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
2239 =for apidoc hv_iterval
2241 Returns the value from the current position of the hash iterator. See
2248 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2250 if (SvRMAGICAL(hv)) {
2251 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2252 SV* const sv = sv_newmortal();
2253 if (HeKLEN(entry) == HEf_SVKEY)
2254 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2256 mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2260 return HeVAL(entry);
2264 =for apidoc hv_iternextsv
2266 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2273 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2275 HE * const he = hv_iternext_flags(hv, 0);
2279 *key = hv_iterkey(he, retlen);
2280 return hv_iterval(hv, he);
2287 =for apidoc hv_magic
2289 Adds magic to a hash. See C<sv_magic>.
2294 /* possibly free a shared string if no one has access to it
2295 * len and hash must both be valid for str.
2298 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2300 unshare_hek_or_pvn (NULL, str, len, hash);
2305 Perl_unshare_hek(pTHX_ HEK *hek)
2308 unshare_hek_or_pvn(hek, NULL, 0, 0);
2311 /* possibly free a shared string if no one has access to it
2312 hek if non-NULL takes priority over the other 3, else str, len and hash
2313 are used. If so, len and hash must both be valid for str.
2316 S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash)
2319 register XPVHV* xhv;
2321 register HE **oentry;
2323 bool is_utf8 = FALSE;
2325 const char * const save = str;
2326 struct shared_he *he = NULL;
2329 /* Find the shared he which is just before us in memory. */
2330 he = (struct shared_he *)(((char *)hek)
2331 - STRUCT_OFFSET(struct shared_he,
2334 /* Assert that the caller passed us a genuine (or at least consistent)
2336 assert (he->shared_he_he.hent_hek == hek);
2339 if (he->shared_he_he.he_valu.hent_refcount - 1) {
2340 --he->shared_he_he.he_valu.hent_refcount;
2341 UNLOCK_STRTAB_MUTEX;
2344 UNLOCK_STRTAB_MUTEX;
2346 hash = HEK_HASH(hek);
2347 } else if (len < 0) {
2348 STRLEN tmplen = -len;
2350 /* See the note in hv_fetch(). --jhi */
2351 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2354 k_flags = HVhek_UTF8;
2356 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2359 /* what follows was the moral equivalent of:
2360 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2362 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2364 xhv = (XPVHV*)SvANY(PL_strtab);
2365 /* assert(xhv_array != 0) */
2367 first = oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)];
2369 const HE *const he_he = &(he->shared_he_he);
2370 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2375 const int flags_masked = k_flags & HVhek_MASK;
2376 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2377 if (HeHASH(entry) != hash) /* strings can't be equal */
2379 if (HeKLEN(entry) != len)
2381 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2383 if (HeKFLAGS(entry) != flags_masked)
2390 if (--entry->he_valu.hent_refcount == 0) {
2391 *oentry = HeNEXT(entry);
2393 /* There are now no entries in our slot. */
2394 xhv->xhv_fill--; /* HvFILL(hv)-- */
2397 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
2401 UNLOCK_STRTAB_MUTEX;
2402 if (!entry && ckWARN_d(WARN_INTERNAL))
2403 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2404 "Attempt to free non-existent shared string '%s'%s"
2406 hek ? HEK_KEY(hek) : str,
2407 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2408 if (k_flags & HVhek_FREEKEY)
2412 /* get a (constant) string ptr from the global string table
2413 * string will get added if it is not already there.
2414 * len and hash must both be valid for str.
2417 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2419 bool is_utf8 = FALSE;
2421 const char * const save = str;
2424 STRLEN tmplen = -len;
2426 /* See the note in hv_fetch(). --jhi */
2427 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2429 /* If we were able to downgrade here, then than means that we were passed
2430 in a key which only had chars 0-255, but was utf8 encoded. */
2433 /* If we found we were able to downgrade the string to bytes, then
2434 we should flag that it needs upgrading on keys or each. Also flag
2435 that we need share_hek_flags to free the string. */
2437 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2440 return share_hek_flags (str, len, hash, flags);
2444 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2448 const int flags_masked = flags & HVhek_MASK;
2449 const U32 hindex = hash & (I32) HvMAX(PL_strtab);
2451 /* what follows is the moral equivalent of:
2453 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2454 hv_store(PL_strtab, str, len, NULL, hash);
2456 Can't rehash the shared string table, so not sure if it's worth
2457 counting the number of entries in the linked list
2459 register XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab);
2460 /* assert(xhv_array != 0) */
2462 entry = (HvARRAY(PL_strtab))[hindex];
2463 for (;entry; entry = HeNEXT(entry)) {
2464 if (HeHASH(entry) != hash) /* strings can't be equal */
2466 if (HeKLEN(entry) != len)
2468 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2470 if (HeKFLAGS(entry) != flags_masked)
2476 /* What used to be head of the list.
2477 If this is NULL, then we're the first entry for this slot, which
2478 means we need to increate fill. */
2479 struct shared_he *new_entry;
2482 HE **const head = &HvARRAY(PL_strtab)[hindex];
2483 HE *const next = *head;
2485 /* We don't actually store a HE from the arena and a regular HEK.
2486 Instead we allocate one chunk of memory big enough for both,
2487 and put the HEK straight after the HE. This way we can find the
2488 HEK directly from the HE.
2491 Newx(k, STRUCT_OFFSET(struct shared_he,
2492 shared_he_hek.hek_key[0]) + len + 2, char);
2493 new_entry = (struct shared_he *)k;
2494 entry = &(new_entry->shared_he_he);
2495 hek = &(new_entry->shared_he_hek);
2497 Copy(str, HEK_KEY(hek), len, char);
2498 HEK_KEY(hek)[len] = 0;
2500 HEK_HASH(hek) = hash;
2501 HEK_FLAGS(hek) = (unsigned char)flags_masked;
2503 /* Still "point" to the HEK, so that other code need not know what
2505 HeKEY_hek(entry) = hek;
2506 entry->he_valu.hent_refcount = 0;
2507 HeNEXT(entry) = next;
2510 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
2511 if (!next) { /* initial entry? */
2512 xhv->xhv_fill++; /* HvFILL(hv)++ */
2513 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2518 ++entry->he_valu.hent_refcount;
2519 UNLOCK_STRTAB_MUTEX;
2521 if (flags & HVhek_FREEKEY)
2524 return HeKEY_hek(entry);
2528 Perl_hv_placeholders_p(pTHX_ HV *hv)
2531 MAGIC *mg = mg_find((SV*)hv, PERL_MAGIC_rhash);
2534 mg = sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, 0);
2537 Perl_die(aTHX_ "panic: hv_placeholders_p");
2540 return &(mg->mg_len);
2545 Perl_hv_placeholders_get(pTHX_ HV *hv)
2548 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_rhash);
2550 return mg ? mg->mg_len : 0;
2554 Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph)
2557 MAGIC * const mg = mg_find((SV*)hv, PERL_MAGIC_rhash);
2562 if (!sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, ph))
2563 Perl_die(aTHX_ "panic: hv_placeholders_set");
2565 /* else we don't need to add magic to record 0 placeholders. */
2569 S_refcounted_he_value(pTHX_ const struct refcounted_he *he)
2573 switch(he->refcounted_he_data[0] & HVrhek_typemask) {
2578 value = &PL_sv_placeholder;
2581 value = newSViv(he->refcounted_he_val.refcounted_he_u_iv);
2584 value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv);
2587 case HVrhek_PV_UTF8:
2588 /* Create a string SV that directly points to the bytes in our
2590 value = newSV_type(SVt_PV);
2591 SvPV_set(value, (char *) he->refcounted_he_data + 1);
2592 SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len);
2593 /* This stops anything trying to free it */
2594 SvLEN_set(value, 0);
2596 SvREADONLY_on(value);
2597 if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8)
2601 Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %x",
2602 he->refcounted_he_data[0]);
2608 =for apidoc refcounted_he_chain_2hv
2610 Generates and returns a C<HV *> by walking up the tree starting at the passed
2611 in C<struct refcounted_he *>.
2616 Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain)
2620 U32 placeholders = 0;
2621 /* We could chase the chain once to get an idea of the number of keys,
2622 and call ksplit. But for now we'll make a potentially inefficient
2623 hash with only 8 entries in its array. */
2624 const U32 max = HvMAX(hv);
2628 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char);
2629 HvARRAY(hv) = (HE**)array;
2634 U32 hash = chain->refcounted_he_hash;
2636 U32 hash = HEK_HASH(chain->refcounted_he_hek);
2638 HE **oentry = &((HvARRAY(hv))[hash & max]);
2639 HE *entry = *oentry;
2642 for (; entry; entry = HeNEXT(entry)) {
2643 if (HeHASH(entry) == hash) {
2644 /* We might have a duplicate key here. If so, entry is older
2645 than the key we've already put in the hash, so if they are
2646 the same, skip adding entry. */
2648 const STRLEN klen = HeKLEN(entry);
2649 const char *const key = HeKEY(entry);
2650 if (klen == chain->refcounted_he_keylen
2651 && (!!HeKUTF8(entry)
2652 == !!(chain->refcounted_he_data[0] & HVhek_UTF8))
2653 && memEQ(key, REF_HE_KEY(chain), klen))
2656 if (HeKEY_hek(entry) == chain->refcounted_he_hek)
2658 if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek)
2659 && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek)
2660 && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek),
2671 = share_hek_flags(REF_HE_KEY(chain),
2672 chain->refcounted_he_keylen,
2673 chain->refcounted_he_hash,
2674 (chain->refcounted_he_data[0]
2675 & (HVhek_UTF8|HVhek_WASUTF8)));
2677 HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek);
2679 value = refcounted_he_value(chain);
2680 if (value == &PL_sv_placeholder)
2682 HeVAL(entry) = value;
2684 /* Link it into the chain. */
2685 HeNEXT(entry) = *oentry;
2686 if (!HeNEXT(entry)) {
2687 /* initial entry. */
2695 chain = chain->refcounted_he_next;
2699 clear_placeholders(hv, placeholders);
2700 HvTOTALKEYS(hv) -= placeholders;
2703 /* We could check in the loop to see if we encounter any keys with key
2704 flags, but it's probably not worth it, as this per-hash flag is only
2705 really meant as an optimisation for things like Storable. */
2707 DEBUG_A(Perl_hv_assert(aTHX_ hv));
2713 Perl_refcounted_he_fetch(pTHX_ const struct refcounted_he *chain, SV *keysv,
2714 const char *key, STRLEN klen, int flags, U32 hash)
2717 /* Just to be awkward, if you're using this interface the UTF-8-or-not-ness
2718 of your key has to exactly match that which is stored. */
2719 SV *value = &PL_sv_placeholder;
2723 if (flags & HVhek_FREEKEY)
2725 key = SvPV_const(keysv, klen);
2727 is_utf8 = (SvUTF8(keysv) != 0);
2729 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
2733 if (keysv && (SvIsCOW_shared_hash(keysv))) {
2734 hash = SvSHARED_HASH(keysv);
2736 PERL_HASH(hash, key, klen);
2740 for (; chain; chain = chain->refcounted_he_next) {
2742 if (hash != chain->refcounted_he_hash)
2744 if (klen != chain->refcounted_he_keylen)
2746 if (memNE(REF_HE_KEY(chain),key,klen))
2748 if (!!is_utf8 != !!(chain->refcounted_he_data[0] & HVhek_UTF8))
2751 if (hash != HEK_HASH(chain->refcounted_he_hek))
2753 if (klen != (STRLEN)HEK_LEN(chain->refcounted_he_hek))
2755 if (memNE(HEK_KEY(chain->refcounted_he_hek),key,klen))
2757 if (!!is_utf8 != !!HEK_UTF8(chain->refcounted_he_hek))
2761 value = sv_2mortal(refcounted_he_value(chain));
2765 if (flags & HVhek_FREEKEY)
2772 =for apidoc refcounted_he_new
2774 Creates a new C<struct refcounted_he>. As S<key> is copied, and value is
2775 stored in a compact form, all references remain the property of the caller.
2776 The C<struct refcounted_he> is returned with a reference count of 1.
2781 struct refcounted_he *
2782 Perl_refcounted_he_new(pTHX_ struct refcounted_he *const parent,
2783 SV *const key, SV *const value) {
2785 struct refcounted_he *he;
2787 const char *key_p = SvPV_const(key, key_len);
2788 STRLEN value_len = 0;
2789 const char *value_p = NULL;
2794 bool is_utf8 = SvUTF8(key) ? TRUE : FALSE;
2797 value_type = HVrhek_PV;
2798 } else if (SvIOK(value)) {
2799 value_type = HVrhek_IV;
2800 } else if (value == &PL_sv_placeholder) {
2801 value_type = HVrhek_delete;
2802 } else if (!SvOK(value)) {
2803 value_type = HVrhek_undef;
2805 value_type = HVrhek_PV;
2808 if (value_type == HVrhek_PV) {
2809 value_p = SvPV_const(value, value_len);
2810 key_offset = value_len + 2;
2817 he = (struct refcounted_he*)
2818 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
2822 he = (struct refcounted_he*)
2823 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
2828 he->refcounted_he_next = parent;
2830 if (value_type == HVrhek_PV) {
2831 Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char);
2832 he->refcounted_he_val.refcounted_he_u_len = value_len;
2833 /* Do it this way so that the SvUTF8() test is after the SvPV, in case
2834 the value is overloaded, and doesn't yet have the UTF-8flag set. */
2836 value_type = HVrhek_PV_UTF8;
2837 } else if (value_type == HVrhek_IV) {
2839 he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value);
2840 value_type = HVrhek_UV;
2842 he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value);
2848 /* Hash keys are always stored normalised to (yes) ISO-8859-1.
2849 As we're going to be building hash keys from this value in future,
2850 normalise it now. */
2851 key_p = (char*)bytes_from_utf8((const U8*)key_p, &key_len, &is_utf8);
2852 flags |= is_utf8 ? HVhek_UTF8 : HVhek_WASUTF8;
2854 PERL_HASH(hash, key_p, key_len);
2857 he->refcounted_he_hash = hash;
2858 he->refcounted_he_keylen = key_len;
2859 Copy(key_p, he->refcounted_he_data + key_offset, key_len, char);
2861 he->refcounted_he_hek = share_hek_flags(key_p, key_len, hash, flags);
2864 if (flags & HVhek_WASUTF8) {
2865 /* If it was downgraded from UTF-8, then the pointer returned from
2866 bytes_from_utf8 is an allocated pointer that we must free. */
2870 he->refcounted_he_data[0] = flags;
2871 he->refcounted_he_refcnt = 1;
2877 =for apidoc refcounted_he_free
2879 Decrements the reference count of the passed in C<struct refcounted_he *>
2880 by one. If the reference count reaches zero the structure's memory is freed,
2881 and C<refcounted_he_free> iterates onto the parent node.
2887 Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) {
2889 PERL_UNUSED_CONTEXT;
2892 struct refcounted_he *copy;
2896 new_count = --he->refcounted_he_refcnt;
2897 HINTS_REFCNT_UNLOCK;
2903 #ifndef USE_ITHREADS
2904 unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0);
2907 he = he->refcounted_he_next;
2908 PerlMemShared_free(copy);
2913 =for apidoc hv_assert
2915 Check that a hash is in an internally consistent state.
2923 Perl_hv_assert(pTHX_ HV *hv)
2928 int placeholders = 0;
2931 const I32 riter = HvRITER_get(hv);
2932 HE *eiter = HvEITER_get(hv);
2934 (void)hv_iterinit(hv);
2936 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2937 /* sanity check the values */
2938 if (HeVAL(entry) == &PL_sv_placeholder)
2942 /* sanity check the keys */
2943 if (HeSVKEY(entry)) {
2944 NOOP; /* Don't know what to check on SV keys. */
2945 } else if (HeKUTF8(entry)) {
2947 if (HeKWASUTF8(entry)) {
2948 PerlIO_printf(Perl_debug_log,
2949 "hash key has both WASUTF8 and UTF8: '%.*s'\n",
2950 (int) HeKLEN(entry), HeKEY(entry));
2953 } else if (HeKWASUTF8(entry))
2956 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2957 static const char bad_count[] = "Count %d %s(s), but hash reports %d\n";
2958 const int nhashkeys = HvUSEDKEYS(hv);
2959 const int nhashplaceholders = HvPLACEHOLDERS_get(hv);
2961 if (nhashkeys != real) {
2962 PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys );
2965 if (nhashplaceholders != placeholders) {
2966 PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders );
2970 if (withflags && ! HvHASKFLAGS(hv)) {
2971 PerlIO_printf(Perl_debug_log,
2972 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2979 HvRITER_set(hv, riter); /* Restore hash iterator state */
2980 HvEITER_set(hv, eiter);
2987 * c-indentation-style: bsd
2989 * indent-tabs-mode: t
2992 * ex: set ts=8 sts=4 sw=4 noet: