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.
185 #define HV_FETCH_LVALUE 0x01
186 #define HV_FETCH_JUST_SV 0x02
189 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
202 hek = hv_fetch_common (hv, NULL, key, klen, flags,
203 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0), 0);
204 return hek ? &HeVAL(hek) : NULL;
207 /* returns an HE * structure with the all fields set */
208 /* note that hent_val will be a mortal sv for MAGICAL hashes */
210 =for apidoc hv_fetch_ent
212 Returns the hash entry which corresponds to the specified key in the hash.
213 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
214 if you want the function to compute it. IF C<lval> is set then the fetch
215 will be part of a store. Make sure the return value is non-null before
216 accessing it. The return value when C<tb> is a tied hash is a pointer to a
217 static location, so be sure to make a copy of the structure if you need to
220 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
221 information on how to use this function on tied hashes.
227 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
229 return hv_fetch_common(hv, keysv, NULL, 0, 0, lval ? HV_FETCH_LVALUE : 0,
234 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
235 int flags, int action, register U32 hash)
248 key = SvPV(keysv, klen);
250 is_utf8 = (SvUTF8(keysv) != 0);
252 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
256 if (SvRMAGICAL(hv)) {
257 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
260 /* XXX should be able to skimp on the HE/HEK here when
261 HV_FETCH_JUST_SV is true. */
264 keysv = newSVpvn(key, klen);
269 keysv = newSVsv(keysv);
271 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
274 /* grab a fake HE/HEK pair from the pool or make a new one */
275 entry = PL_hv_fetch_ent_mh;
277 PL_hv_fetch_ent_mh = HeNEXT(entry);
281 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
282 HeKEY_hek(entry) = (HEK*)k;
284 HeNEXT(entry) = Nullhe;
285 HeSVKEY_set(entry, keysv);
287 sv_upgrade(sv, SVt_PVLV);
289 LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
291 /* XXX remove at some point? */
292 if (flags & HVhek_FREEKEY)
297 #ifdef ENV_IS_CASELESS
298 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
300 for (i = 0; i < klen; ++i)
301 if (isLOWER(key[i])) {
302 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
303 (void)strupr(SvPVX(nkeysv));
304 entry = hv_fetch_common(hv, nkeysv, NULL, 0, 0, 0);
305 if (!entry && (action & HV_FETCH_LVALUE))
306 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
308 /* XXX remove at some point? */
309 if (flags & HVhek_FREEKEY)
318 xhv = (XPVHV*)SvANY(hv);
319 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
320 if ((action & HV_FETCH_LVALUE)
321 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
322 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
325 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
326 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
329 /* XXX remove at some point? */
330 if (flags & HVhek_FREEKEY)
338 int oldflags = flags;
339 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
343 flags &= ~HVhek_UTF8;
345 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
346 if (oldflags & HVhek_FREEKEY)
352 PERL_HASH_INTERNAL(hash, key, klen);
353 /* Yes, you do need this even though you are not "storing" because
354 you can flip the flags below if doing an lval lookup. (And that
355 was put in to give the semantics Andreas was expecting.) */
356 flags |= HVhek_REHASH;
358 if (keysv && (SvIsCOW_shared_hash(keysv))) {
361 PERL_HASH(hash, key, klen);
365 masked_flags = (flags & HVhek_MASK);
367 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
368 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
369 for (; entry; entry = HeNEXT(entry)) {
370 if (HeHASH(entry) != hash) /* strings can't be equal */
372 if (HeKLEN(entry) != (I32)klen)
374 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
376 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
378 if ((action & HV_FETCH_LVALUE) && HeKFLAGS(entry) != masked_flags) {
379 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
380 But if entry was set previously with HVhek_WASUTF8 and key now
381 doesn't (or vice versa) then we should change the key's flag,
382 as this is assignment. */
383 if (HvSHAREKEYS(hv)) {
384 /* Need to swap the key we have for a key with the flags we
385 need. As keys are shared we can't just write to the flag,
386 so we share the new one, unshare the old one. */
387 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
388 unshare_hek (HeKEY_hek(entry));
389 HeKEY_hek(entry) = new_hek;
392 HeKFLAGS(entry) = masked_flags;
393 if (masked_flags & HVhek_ENABLEHVKFLAGS)
396 /* if we find a placeholder, we pretend we haven't found anything */
397 if (HeVAL(entry) == &PL_sv_placeholder)
399 if (flags & HVhek_FREEKEY)
403 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
404 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
406 char *env = PerlEnv_ENVgetenv_len(key,&len);
408 /* XXX remove once common API complete */
410 nkeysv = sv_2mortal(newSVpvn(key,klen));
413 sv = newSVpvn(env,len);
415 if (flags & HVhek_FREEKEY)
417 return hv_store_ent(hv,keysv,sv,hash);
421 if (!entry && SvREADONLY(hv)) {
422 S_hv_notallowed(aTHX_ flags, key, klen,
423 "access disallowed key '%"SVf"' in"
426 if (action & HV_FETCH_LVALUE) {
427 /* XXX remove once common API complete */
429 keysv = sv_2mortal(newSVpvn(key,klen));
433 if (flags & HVhek_FREEKEY)
435 if (action & HV_FETCH_LVALUE) {
436 /* gonna assign to this, so it better be there */
438 return hv_store_ent(hv,keysv,sv,hash);
444 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
446 MAGIC *mg = SvMAGIC(hv);
450 if (isUPPER(mg->mg_type)) {
452 switch (mg->mg_type) {
453 case PERL_MAGIC_tied:
455 *needs_store = FALSE;
458 mg = mg->mg_moremagic;
465 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
466 the length of the key. The C<hash> parameter is the precomputed hash
467 value; if it is zero then Perl will compute it. The return value will be
468 NULL if the operation failed or if the value did not need to be actually
469 stored within the hash (as in the case of tied hashes). Otherwise it can
470 be dereferenced to get the original C<SV*>. Note that the caller is
471 responsible for suitably incrementing the reference count of C<val> before
472 the call, and decrementing it if the function returned NULL. Effectively
473 a successful hv_store takes ownership of one reference to C<val>. This is
474 usually what you want; a newly created SV has a reference count of one, so
475 if all your code does is create SVs then store them in a hash, hv_store
476 will own the only reference to the new SV, and your code doesn't need to do
477 anything further to tidy up. hv_store is not implemented as a call to
478 hv_store_ent, and does not create a temporary SV for the key, so if your
479 key data is not already in SV form then use hv_store in preference to
482 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
483 information on how to use this function on tied hashes.
489 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
502 hek = hv_store_common (hv, NULL, key, klen, flags, val, 0);
503 return hek ? &HeVAL(hek) : NULL;
507 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
508 register U32 hash, int flags)
510 HE *hek = hv_store_common (hv, NULL, key, klen, flags, val, hash);
511 return hek ? &HeVAL(hek) : NULL;
515 =for apidoc hv_store_ent
517 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
518 parameter is the precomputed hash value; if it is zero then Perl will
519 compute it. The return value is the new hash entry so created. It will be
520 NULL if the operation failed or if the value did not need to be actually
521 stored within the hash (as in the case of tied hashes). Otherwise the
522 contents of the return value can be accessed using the C<He?> macros
523 described here. Note that the caller is responsible for suitably
524 incrementing the reference count of C<val> before the call, and
525 decrementing it if the function returned NULL. Effectively a successful
526 hv_store_ent takes ownership of one reference to C<val>. This is
527 usually what you want; a newly created SV has a reference count of one, so
528 if all your code does is create SVs then store them in a hash, hv_store
529 will own the only reference to the new SV, and your code doesn't need to do
530 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
531 unlike C<val> it does not take ownership of it, so maintaining the correct
532 reference count on C<key> is entirely the caller's responsibility. hv_store
533 is not implemented as a call to hv_store_ent, and does not create a temporary
534 SV for the key, so if your key data is not already in SV form then use
535 hv_store in preference to hv_store_ent.
537 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
538 information on how to use this function on tied hashes.
544 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
546 return hv_store_common(hv, keysv, NULL, 0, 0, val, hash);
550 S_hv_store_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
551 int flags, SV *val, U32 hash)
565 key = SvPV(keysv, klen);
567 is_utf8 = (SvUTF8(keysv) != 0);
569 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
573 xhv = (XPVHV*)SvANY(hv);
577 hv_magic_check (hv, &needs_copy, &needs_store);
579 bool save_taint = PL_tainted;
580 if (keysv || is_utf8) {
582 keysv = newSVpvn(key, klen);
586 PL_tainted = SvTAINTED(keysv);
587 keysv = sv_2mortal(newSVsv(keysv));
588 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
590 mg_copy((SV*)hv, val, key, klen);
593 TAINT_IF(save_taint);
594 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
595 if (flags & HVhek_FREEKEY)
599 #ifdef ENV_IS_CASELESS
600 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
601 key = savepvn(key,klen);
602 key = (const char*)strupr((char*)key);
605 if (flags & HVhek_FREEKEY)
614 if (flags & HVhek_PLACEHOLD) {
615 /* We have been requested to insert a placeholder. Currently
616 only Storable is allowed to do this. */
617 val = &PL_sv_placeholder;
621 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
623 if (flags & HVhek_FREEKEY) {
624 /* This shouldn't happen if our caller does what we expect,
625 but strictly the API allows it. */
632 flags &= ~HVhek_UTF8;
634 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
635 HvHASKFLAGS_on((SV*)hv);
639 /* We don't have a pointer to the hv, so we have to replicate the
640 flag into every HEK, so that hv_iterkeysv can see it. */
641 flags |= HVhek_REHASH;
642 PERL_HASH_INTERNAL(hash, key, klen);
644 if (keysv && SvIsCOW_shared_hash(keysv)) {
647 PERL_HASH(hash, key, klen);
651 masked_flags = (flags & HVhek_MASK);
653 if (!xhv->xhv_array /* !HvARRAY(hv) */)
654 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
655 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
658 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
659 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
662 for (; entry; ++n_links, entry = HeNEXT(entry)) {
663 if (HeHASH(entry) != hash) /* strings can't be equal */
665 if (HeKLEN(entry) != (I32)klen)
667 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
669 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
671 if (HeVAL(entry) == &PL_sv_placeholder)
672 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
674 SvREFCNT_dec(HeVAL(entry));
676 if (val == &PL_sv_placeholder)
677 xhv->xhv_placeholders++;
679 if (HeKFLAGS(entry) != masked_flags) {
680 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
681 But if entry was set previously with HVhek_WASUTF8 and key now
682 doesn't (or vice versa) then we should change the key's flag,
683 as this is assignment. */
684 if (HvSHAREKEYS(hv)) {
685 /* Need to swap the key we have for a key with the flags we
686 need. As keys are shared we can't just write to the flag,
687 so we share the new one, unshare the old one. */
688 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
689 unshare_hek (HeKEY_hek(entry));
690 HeKEY_hek(entry) = new_hek;
693 HeKFLAGS(entry) = masked_flags;
695 if (flags & HVhek_FREEKEY)
700 if (SvREADONLY(hv)) {
701 S_hv_notallowed(aTHX_ flags, key, klen,
702 "access disallowed key '%"SVf"' to"
707 /* share_hek_flags will do the free for us. This might be considered
710 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
711 else /* gotta do the real thing */
712 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
714 HeNEXT(entry) = *oentry;
717 if (val == &PL_sv_placeholder)
718 xhv->xhv_placeholders++;
720 xhv->xhv_keys++; /* HvKEYS(hv)++ */
721 if (!n_links) { /* initial entry? */
722 xhv->xhv_fill++; /* HvFILL(hv)++ */
723 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
724 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
725 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
726 splits on a rehashed hash, as we're not going to split it again,
727 and if someone is lucky (evil) enough to get all the keys in one
728 list they could exhaust our memory as we repeatedly double the
729 number of buckets on every entry. Linear search feels a less worse
738 =for apidoc hv_delete
740 Deletes a key/value pair in the hash. The value SV is removed from the
741 hash and returned to the caller. The C<klen> is the length of the key.
742 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
749 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
756 k_flags |= HVhek_UTF8;
760 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
764 =for apidoc hv_delete_ent
766 Deletes a key/value pair in the hash. The value SV is removed from the
767 hash and returned to the caller. The C<flags> value will normally be zero;
768 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
769 precomputed hash value, or 0 to ask for it to be computed.
775 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
777 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
781 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
782 int k_flags, I32 d_flags, U32 hash)
787 register HE **oentry;
797 key = SvPV(keysv, klen);
799 is_utf8 = (SvUTF8(keysv) != 0);
801 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
805 if (SvRMAGICAL(hv)) {
808 hv_magic_check (hv, &needs_copy, &needs_store);
811 entry = hv_fetch_common(hv, keysv, key, klen,
812 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
814 sv = entry ? HeVAL(entry) : NULL;
820 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
821 /* No longer an element */
822 sv_unmagic(sv, PERL_MAGIC_tiedelem);
825 return Nullsv; /* element cannot be deleted */
828 #ifdef ENV_IS_CASELESS
829 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
830 /* XXX This code isn't UTF8 clean. */
831 keysv = sv_2mortal(newSVpvn(key,klen));
832 keysave = key = strupr(SvPVX(keysv));
840 xhv = (XPVHV*)SvANY(hv);
841 if (!xhv->xhv_array /* !HvARRAY(hv) */)
845 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
847 if (k_flags & HVhek_FREEKEY) {
848 /* This shouldn't happen if our caller does what we expect,
849 but strictly the API allows it. */
854 k_flags |= HVhek_UTF8;
856 k_flags &= ~HVhek_UTF8;
858 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
859 HvHASKFLAGS_on((SV*)hv);
863 PERL_HASH_INTERNAL(hash, key, klen);
865 if (keysv && (SvIsCOW_shared_hash(keysv))) {
868 PERL_HASH(hash, key, klen);
870 PERL_HASH(hash, key, klen);
873 masked_flags = (k_flags & HVhek_MASK);
875 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
876 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
879 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
880 if (HeHASH(entry) != hash) /* strings can't be equal */
882 if (HeKLEN(entry) != (I32)klen)
884 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
886 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
888 if (k_flags & HVhek_FREEKEY)
891 /* if placeholder is here, it's already been deleted.... */
892 if (HeVAL(entry) == &PL_sv_placeholder)
895 return Nullsv; /* if still SvREADONLY, leave it deleted. */
897 /* okay, really delete the placeholder. */
898 *oentry = HeNEXT(entry);
900 xhv->xhv_fill--; /* HvFILL(hv)-- */
901 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
904 hv_free_ent(hv, entry);
905 xhv->xhv_keys--; /* HvKEYS(hv)-- */
906 if (xhv->xhv_keys == 0)
908 xhv->xhv_placeholders--;
911 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
912 S_hv_notallowed(aTHX_ k_flags, key, klen,
913 "delete readonly key '%"SVf"' from"
917 if (d_flags & G_DISCARD)
920 sv = sv_2mortal(HeVAL(entry));
921 HeVAL(entry) = &PL_sv_placeholder;
925 * If a restricted hash, rather than really deleting the entry, put
926 * a placeholder there. This marks the key as being "approved", so
927 * we can still access via not-really-existing key without raising
930 if (SvREADONLY(hv)) {
931 HeVAL(entry) = &PL_sv_placeholder;
932 /* We'll be saving this slot, so the number of allocated keys
933 * doesn't go down, but the number placeholders goes up */
934 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
936 *oentry = HeNEXT(entry);
938 xhv->xhv_fill--; /* HvFILL(hv)-- */
939 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
942 hv_free_ent(hv, entry);
943 xhv->xhv_keys--; /* HvKEYS(hv)-- */
944 if (xhv->xhv_keys == 0)
949 if (SvREADONLY(hv)) {
950 S_hv_notallowed(aTHX_ k_flags, key, klen,
951 "delete disallowed key '%"SVf"' from"
955 if (k_flags & HVhek_FREEKEY)
961 =for apidoc hv_exists
963 Returns a boolean indicating whether the specified hash key exists. The
964 C<klen> is the length of the key.
970 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
982 return hv_exists_common(hv, NULL, key, klen, flags, 0);
986 =for apidoc hv_exists_ent
988 Returns a boolean indicating whether the specified hash key exists. C<hash>
989 can be a valid precomputed hash value, or 0 to ask for it to be
996 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
998 return hv_exists_common(hv, keysv, NULL, 0, 0, hash);
1002 S_hv_exists_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
1003 int k_flags, U32 hash)
1005 register XPVHV* xhv;
1009 const char *keysave;
1016 key = SvPV(keysv, klen);
1018 is_utf8 = (SvUTF8(keysv) != 0);
1020 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
1024 if (SvRMAGICAL(hv)) {
1025 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1028 if (keysv || is_utf8) {
1030 keysv = newSVpvn(key, klen);
1033 keysv = newSVsv(keysv);
1035 key = (char *)sv_2mortal(keysv);
1039 /* I don't understand why hv_exists_ent has svret and sv,
1040 whereas hv_exists only had one. */
1041 svret = sv_newmortal();
1042 sv = sv_newmortal();
1043 mg_copy((SV*)hv, sv, key, klen);
1044 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1045 return (bool)SvTRUE(svret);
1047 #ifdef ENV_IS_CASELESS
1048 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1049 /* XXX This code isn't UTF8 clean. */
1050 keysv = sv_2mortal(newSVpvn(key,klen));
1051 keysave = key = strupr(SvPVX(keysv));
1058 xhv = (XPVHV*)SvANY(hv);
1059 #ifndef DYNAMIC_ENV_FETCH
1060 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1065 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1067 if (k_flags & HVhek_FREEKEY) {
1068 /* This shouldn't happen if our caller does what we expect,
1069 but strictly the API allows it. */
1074 k_flags |= HVhek_UTF8;
1076 k_flags &= ~HVhek_UTF8;
1078 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1082 PERL_HASH_INTERNAL(hash, key, klen);
1084 PERL_HASH(hash, key, klen);
1086 masked_flags = (k_flags & HVhek_MASK);
1088 #ifdef DYNAMIC_ENV_FETCH
1089 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1092 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1093 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1094 for (; entry; entry = HeNEXT(entry)) {
1095 if (HeHASH(entry) != hash) /* strings can't be equal */
1097 if (HeKLEN(entry) != (I32)klen)
1099 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1101 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1103 if (k_flags & HVhek_FREEKEY)
1105 /* If we find the key, but the value is a placeholder, return false. */
1106 if (HeVAL(entry) == &PL_sv_placeholder)
1110 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1111 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1113 char *env = PerlEnv_ENVgetenv_len(key,&len);
1115 sv = newSVpvn(env,len);
1117 (void)hv_store_ent(hv,keysv,sv,hash);
1118 if (k_flags & HVhek_FREEKEY)
1124 if (k_flags & HVhek_FREEKEY)
1131 S_hsplit(pTHX_ HV *hv)
1133 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1134 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1135 register I32 newsize = oldsize * 2;
1137 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1141 register HE **oentry;
1142 int longest_chain = 0;
1146 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1147 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1153 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1158 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1159 if (oldsize >= 64) {
1160 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1161 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1164 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1168 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1169 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1170 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1173 for (i=0; i<oldsize; i++,aep++) {
1174 int left_length = 0;
1175 int right_length = 0;
1177 if (!*aep) /* non-existent */
1180 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1181 if ((HeHASH(entry) & newsize) != (U32)i) {
1182 *oentry = HeNEXT(entry);
1183 HeNEXT(entry) = *bep;
1185 xhv->xhv_fill++; /* HvFILL(hv)++ */
1191 oentry = &HeNEXT(entry);
1195 if (!*aep) /* everything moved */
1196 xhv->xhv_fill--; /* HvFILL(hv)-- */
1197 /* I think we don't actually need to keep track of the longest length,
1198 merely flag if anything is too long. But for the moment while
1199 developing this code I'll track it. */
1200 if (left_length > longest_chain)
1201 longest_chain = left_length;
1202 if (right_length > longest_chain)
1203 longest_chain = right_length;
1207 /* Pick your policy for "hashing isn't working" here: */
1208 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1213 if (hv == PL_strtab) {
1214 /* Urg. Someone is doing something nasty to the string table.
1219 /* Awooga. Awooga. Pathological data. */
1220 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1221 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1224 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1225 was_shared = HvSHAREKEYS(hv);
1228 HvSHAREKEYS_off(hv);
1231 aep = (HE **) xhv->xhv_array;
1233 for (i=0; i<newsize; i++,aep++) {
1236 /* We're going to trash this HE's next pointer when we chain it
1237 into the new hash below, so store where we go next. */
1238 HE *next = HeNEXT(entry);
1242 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1247 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1248 hash, HeKFLAGS(entry));
1249 unshare_hek (HeKEY_hek(entry));
1250 HeKEY_hek(entry) = new_hek;
1252 /* Not shared, so simply write the new hash in. */
1253 HeHASH(entry) = hash;
1255 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1256 HEK_REHASH_on(HeKEY_hek(entry));
1257 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1259 /* Copy oentry to the correct new chain. */
1260 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1262 xhv->xhv_fill++; /* HvFILL(hv)++ */
1263 HeNEXT(entry) = *bep;
1269 Safefree (xhv->xhv_array);
1270 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1274 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1276 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1277 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1278 register I32 newsize;
1284 register HE **oentry;
1286 newsize = (I32) newmax; /* possible truncation here */
1287 if (newsize != newmax || newmax <= oldsize)
1289 while ((newsize & (1 + ~newsize)) != newsize) {
1290 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1292 if (newsize < newmax)
1294 if (newsize < newmax)
1295 return; /* overflow detection */
1297 a = xhv->xhv_array; /* HvARRAY(hv) */
1300 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1301 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1307 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1312 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1313 if (oldsize >= 64) {
1314 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1315 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1318 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1321 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1324 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1326 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1327 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1328 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1332 for (i=0; i<oldsize; i++,aep++) {
1333 if (!*aep) /* non-existent */
1335 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1336 if ((j = (HeHASH(entry) & newsize)) != i) {
1338 *oentry = HeNEXT(entry);
1339 if (!(HeNEXT(entry) = aep[j]))
1340 xhv->xhv_fill++; /* HvFILL(hv)++ */
1345 oentry = &HeNEXT(entry);
1347 if (!*aep) /* everything moved */
1348 xhv->xhv_fill--; /* HvFILL(hv)-- */
1355 Creates a new HV. The reference count is set to 1.
1364 register XPVHV* xhv;
1366 hv = (HV*)NEWSV(502,0);
1367 sv_upgrade((SV *)hv, SVt_PVHV);
1368 xhv = (XPVHV*)SvANY(hv);
1371 #ifndef NODEFAULT_SHAREKEYS
1372 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1375 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1376 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1377 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1378 (void)hv_iterinit(hv); /* so each() will start off right */
1383 Perl_newHVhv(pTHX_ HV *ohv)
1386 STRLEN hv_max, hv_fill;
1388 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1390 hv_max = HvMAX(ohv);
1392 if (!SvMAGICAL((SV *)ohv)) {
1393 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1395 bool shared = !!HvSHAREKEYS(ohv);
1396 HE **ents, **oents = (HE **)HvARRAY(ohv);
1398 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1401 /* In each bucket... */
1402 for (i = 0; i <= hv_max; i++) {
1403 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1410 /* Copy the linked list of entries. */
1411 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1412 U32 hash = HeHASH(oent);
1413 char *key = HeKEY(oent);
1414 STRLEN len = HeKLEN(oent);
1415 int flags = HeKFLAGS(oent);
1418 HeVAL(ent) = newSVsv(HeVAL(oent));
1420 = shared ? share_hek_flags(key, len, hash, flags)
1421 : save_hek_flags(key, len, hash, flags);
1432 HvFILL(hv) = hv_fill;
1433 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1437 /* Iterate over ohv, copying keys and values one at a time. */
1439 I32 riter = HvRITER(ohv);
1440 HE *eiter = HvEITER(ohv);
1442 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1443 while (hv_max && hv_max + 1 >= hv_fill * 2)
1444 hv_max = hv_max / 2;
1448 while ((entry = hv_iternext_flags(ohv, 0))) {
1449 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1450 newSVsv(HeVAL(entry)), HeHASH(entry),
1453 HvRITER(ohv) = riter;
1454 HvEITER(ohv) = eiter;
1461 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1468 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1469 PL_sub_generation++; /* may be deletion of method from stash */
1471 if (HeKLEN(entry) == HEf_SVKEY) {
1472 SvREFCNT_dec(HeKEY_sv(entry));
1473 Safefree(HeKEY_hek(entry));
1475 else if (HvSHAREKEYS(hv))
1476 unshare_hek(HeKEY_hek(entry));
1478 Safefree(HeKEY_hek(entry));
1483 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1487 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1488 PL_sub_generation++; /* may be deletion of method from stash */
1489 sv_2mortal(HeVAL(entry)); /* free between statements */
1490 if (HeKLEN(entry) == HEf_SVKEY) {
1491 sv_2mortal(HeKEY_sv(entry));
1492 Safefree(HeKEY_hek(entry));
1494 else if (HvSHAREKEYS(hv))
1495 unshare_hek(HeKEY_hek(entry));
1497 Safefree(HeKEY_hek(entry));
1502 =for apidoc hv_clear
1504 Clears a hash, making it empty.
1510 Perl_hv_clear(pTHX_ HV *hv)
1512 register XPVHV* xhv;
1516 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1518 xhv = (XPVHV*)SvANY(hv);
1520 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1521 /* restricted hash: convert all keys to placeholders */
1524 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1525 entry = ((HE**)xhv->xhv_array)[i];
1526 for (; entry; entry = HeNEXT(entry)) {
1527 /* not already placeholder */
1528 if (HeVAL(entry) != &PL_sv_placeholder) {
1529 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1530 SV* keysv = hv_iterkeysv(entry);
1532 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1535 SvREFCNT_dec(HeVAL(entry));
1536 HeVAL(entry) = &PL_sv_placeholder;
1537 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1545 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1546 if (xhv->xhv_array /* HvARRAY(hv) */)
1547 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1548 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1553 HvHASKFLAGS_off(hv);
1558 =for apidoc hv_clear_placeholders
1560 Clears any placeholders from a hash. If a restricted hash has any of its keys
1561 marked as readonly and the key is subsequently deleted, the key is not actually
1562 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1563 it so it will be ignored by future operations such as iterating over the hash,
1564 but will still allow the hash to have a value reaasigned to the key at some
1565 future point. This function clears any such placeholder keys from the hash.
1566 See Hash::Util::lock_keys() for an example of its use.
1572 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1575 items = (I32)HvPLACEHOLDERS(hv);
1578 I32 riter = HvRITER(hv);
1579 HE *eiter = HvEITER(hv);
1581 /* This may look suboptimal with the items *after* the iternext, but
1582 it's quite deliberate. We only get here with items==0 if we've
1583 just deleted the last placeholder in the hash. If we've just done
1584 that then it means that the hash is in lazy delete mode, and the
1585 HE is now only referenced in our iterator. If we just quit the loop
1586 and discarded our iterator then the HE leaks. So we do the && the
1587 other way to ensure iternext is called just one more time, which
1588 has the side effect of triggering the lazy delete. */
1589 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
1591 SV *val = hv_iterval(hv, entry);
1593 if (val == &PL_sv_placeholder) {
1595 /* It seems that I have to go back in the front of the hash
1596 API to delete a hash, even though I have a HE structure
1597 pointing to the very entry I want to delete, and could hold
1598 onto the previous HE that points to it. And it's easier to
1599 go in with SVs as I can then specify the precomputed hash,
1600 and don't have fun and games with utf8 keys. */
1601 SV *key = hv_iterkeysv(entry);
1603 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
1607 HvRITER(hv) = riter;
1608 HvEITER(hv) = eiter;
1613 S_hfreeentries(pTHX_ HV *hv)
1615 register HE **array;
1617 register HE *oentry = Null(HE*);
1628 array = HvARRAY(hv);
1629 /* make everyone else think the array is empty, so that the destructors
1630 * called for freed entries can't recusively mess with us */
1631 HvARRAY(hv) = Null(HE**);
1633 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1639 entry = HeNEXT(entry);
1640 hv_free_ent(hv, oentry);
1645 entry = array[riter];
1648 HvARRAY(hv) = array;
1649 (void)hv_iterinit(hv);
1653 =for apidoc hv_undef
1661 Perl_hv_undef(pTHX_ HV *hv)
1663 register XPVHV* xhv;
1666 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1667 xhv = (XPVHV*)SvANY(hv);
1669 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1672 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1673 Safefree(HvNAME(hv));
1676 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1677 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1678 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1685 =for apidoc hv_iterinit
1687 Prepares a starting point to traverse a hash table. Returns the number of
1688 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1689 currently only meaningful for hashes without tie magic.
1691 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1692 hash buckets that happen to be in use. If you still need that esoteric
1693 value, you can get it through the macro C<HvFILL(tb)>.
1700 Perl_hv_iterinit(pTHX_ HV *hv)
1702 register XPVHV* xhv;
1706 Perl_croak(aTHX_ "Bad hash");
1707 xhv = (XPVHV*)SvANY(hv);
1708 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1709 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1711 hv_free_ent(hv, entry);
1713 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1714 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1715 /* used to be xhv->xhv_fill before 5.004_65 */
1716 return XHvTOTALKEYS(xhv);
1719 =for apidoc hv_iternext
1721 Returns entries from a hash iterator. See C<hv_iterinit>.
1723 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1724 iterator currently points to, without losing your place or invalidating your
1725 iterator. Note that in this case the current entry is deleted from the hash
1726 with your iterator holding the last reference to it. Your iterator is flagged
1727 to free the entry on the next call to C<hv_iternext>, so you must not discard
1728 your iterator immediately else the entry will leak - call C<hv_iternext> to
1729 trigger the resource deallocation.
1735 Perl_hv_iternext(pTHX_ HV *hv)
1737 return hv_iternext_flags(hv, 0);
1741 =for apidoc hv_iternext_flags
1743 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1744 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1745 set the placeholders keys (for restricted hashes) will be returned in addition
1746 to normal keys. By default placeholders are automatically skipped over.
1747 Currently a placeholder is implemented with a value that is
1748 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1749 restricted hashes may change, and the implementation currently is
1750 insufficiently abstracted for any change to be tidy.
1756 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1758 register XPVHV* xhv;
1764 Perl_croak(aTHX_ "Bad hash");
1765 xhv = (XPVHV*)SvANY(hv);
1766 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1768 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1769 SV *key = sv_newmortal();
1771 sv_setsv(key, HeSVKEY_force(entry));
1772 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1778 /* one HE per MAGICAL hash */
1779 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1781 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1783 HeKEY_hek(entry) = hek;
1784 HeKLEN(entry) = HEf_SVKEY;
1786 magic_nextpack((SV*) hv,mg,key);
1788 /* force key to stay around until next time */
1789 HeSVKEY_set(entry, SvREFCNT_inc(key));
1790 return entry; /* beware, hent_val is not set */
1793 SvREFCNT_dec(HeVAL(entry));
1794 Safefree(HeKEY_hek(entry));
1796 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1799 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1800 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1804 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1805 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1806 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1808 /* At start of hash, entry is NULL. */
1811 entry = HeNEXT(entry);
1812 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1814 * Skip past any placeholders -- don't want to include them in
1817 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1818 entry = HeNEXT(entry);
1823 /* OK. Come to the end of the current list. Grab the next one. */
1825 xhv->xhv_riter++; /* HvRITER(hv)++ */
1826 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1827 /* There is no next one. End of the hash. */
1828 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1831 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1832 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1834 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1835 /* If we have an entry, but it's a placeholder, don't count it.
1837 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1838 entry = HeNEXT(entry);
1840 /* Will loop again if this linked list starts NULL
1841 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1842 or if we run through it and find only placeholders. */
1845 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1847 hv_free_ent(hv, oldentry);
1850 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1851 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1853 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1858 =for apidoc hv_iterkey
1860 Returns the key from the current position of the hash iterator. See
1867 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1869 if (HeKLEN(entry) == HEf_SVKEY) {
1871 char *p = SvPV(HeKEY_sv(entry), len);
1876 *retlen = HeKLEN(entry);
1877 return HeKEY(entry);
1881 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1883 =for apidoc hv_iterkeysv
1885 Returns the key as an C<SV*> from the current position of the hash
1886 iterator. The return value will always be a mortal copy of the key. Also
1893 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1895 if (HeKLEN(entry) != HEf_SVKEY) {
1896 HEK *hek = HeKEY_hek(entry);
1897 int flags = HEK_FLAGS(hek);
1900 if (flags & HVhek_WASUTF8) {
1902 Andreas would like keys he put in as utf8 to come back as utf8
1904 STRLEN utf8_len = HEK_LEN(hek);
1905 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1907 sv = newSVpvn ((char*)as_utf8, utf8_len);
1909 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1910 } else if (flags & HVhek_REHASH) {
1911 /* We don't have a pointer to the hv, so we have to replicate the
1912 flag into every HEK. This hv is using custom a hasing
1913 algorithm. Hence we can't return a shared string scalar, as
1914 that would contain the (wrong) hash value, and might get passed
1915 into an hv routine with a regular hash */
1917 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1921 sv = newSVpvn_share(HEK_KEY(hek),
1922 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1925 return sv_2mortal(sv);
1927 return sv_mortalcopy(HeKEY_sv(entry));
1931 =for apidoc hv_iterval
1933 Returns the value from the current position of the hash iterator. See
1940 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1942 if (SvRMAGICAL(hv)) {
1943 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1944 SV* sv = sv_newmortal();
1945 if (HeKLEN(entry) == HEf_SVKEY)
1946 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1947 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1951 return HeVAL(entry);
1955 =for apidoc hv_iternextsv
1957 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1964 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1967 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1969 *key = hv_iterkey(he, retlen);
1970 return hv_iterval(hv, he);
1974 =for apidoc hv_magic
1976 Adds magic to a hash. See C<sv_magic>.
1982 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1984 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1987 #if 0 /* use the macro from hv.h instead */
1990 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1992 return HEK_KEY(share_hek(sv, len, hash));
1997 /* possibly free a shared string if no one has access to it
1998 * len and hash must both be valid for str.
2001 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2003 unshare_hek_or_pvn (NULL, str, len, hash);
2008 Perl_unshare_hek(pTHX_ HEK *hek)
2010 unshare_hek_or_pvn(hek, NULL, 0, 0);
2013 /* possibly free a shared string if no one has access to it
2014 hek if non-NULL takes priority over the other 3, else str, len and hash
2015 are used. If so, len and hash must both be valid for str.
2018 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2020 register XPVHV* xhv;
2022 register HE **oentry;
2025 bool is_utf8 = FALSE;
2027 const char *save = str;
2030 hash = HEK_HASH(hek);
2031 } else if (len < 0) {
2032 STRLEN tmplen = -len;
2034 /* See the note in hv_fetch(). --jhi */
2035 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2038 k_flags = HVhek_UTF8;
2040 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2043 /* what follows is the moral equivalent of:
2044 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2045 if (--*Svp == Nullsv)
2046 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2048 xhv = (XPVHV*)SvANY(PL_strtab);
2049 /* assert(xhv_array != 0) */
2051 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2052 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2054 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2055 if (HeKEY_hek(entry) != hek)
2061 int flags_masked = k_flags & HVhek_MASK;
2062 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2063 if (HeHASH(entry) != hash) /* strings can't be equal */
2065 if (HeKLEN(entry) != len)
2067 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2069 if (HeKFLAGS(entry) != flags_masked)
2077 if (--HeVAL(entry) == Nullsv) {
2078 *oentry = HeNEXT(entry);
2080 xhv->xhv_fill--; /* HvFILL(hv)-- */
2081 Safefree(HeKEY_hek(entry));
2083 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2087 UNLOCK_STRTAB_MUTEX;
2088 if (!found && ckWARN_d(WARN_INTERNAL))
2089 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2090 "Attempt to free non-existent shared string '%s'%s",
2091 hek ? HEK_KEY(hek) : str,
2092 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2093 if (k_flags & HVhek_FREEKEY)
2097 /* get a (constant) string ptr from the global string table
2098 * string will get added if it is not already there.
2099 * len and hash must both be valid for str.
2102 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2104 bool is_utf8 = FALSE;
2106 const char *save = str;
2109 STRLEN tmplen = -len;
2111 /* See the note in hv_fetch(). --jhi */
2112 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2114 /* If we were able to downgrade here, then than means that we were passed
2115 in a key which only had chars 0-255, but was utf8 encoded. */
2118 /* If we found we were able to downgrade the string to bytes, then
2119 we should flag that it needs upgrading on keys or each. Also flag
2120 that we need share_hek_flags to free the string. */
2122 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2125 return share_hek_flags (str, len, hash, flags);
2129 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2131 register XPVHV* xhv;
2133 register HE **oentry;
2136 int flags_masked = flags & HVhek_MASK;
2138 /* what follows is the moral equivalent of:
2140 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2141 hv_store(PL_strtab, str, len, Nullsv, hash);
2143 Can't rehash the shared string table, so not sure if it's worth
2144 counting the number of entries in the linked list
2146 xhv = (XPVHV*)SvANY(PL_strtab);
2147 /* assert(xhv_array != 0) */
2149 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2150 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2151 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2152 if (HeHASH(entry) != hash) /* strings can't be equal */
2154 if (HeKLEN(entry) != len)
2156 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2158 if (HeKFLAGS(entry) != flags_masked)
2165 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2166 HeVAL(entry) = Nullsv;
2167 HeNEXT(entry) = *oentry;
2169 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2170 if (i) { /* initial entry? */
2171 xhv->xhv_fill++; /* HvFILL(hv)++ */
2172 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2177 ++HeVAL(entry); /* use value slot as REFCNT */
2178 UNLOCK_STRTAB_MUTEX;
2180 if (flags & HVhek_FREEKEY)
2183 return HeKEY_hek(entry);
2188 =for apidoc hv_assert
2190 Check that a hash is in an internally consistent state.
2196 Perl_hv_assert(pTHX_ HV *hv)
2200 int placeholders = 0;
2203 I32 riter = HvRITER(hv);
2204 HE *eiter = HvEITER(hv);
2206 (void)hv_iterinit(hv);
2208 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2209 /* sanity check the values */
2210 if (HeVAL(entry) == &PL_sv_placeholder) {
2215 /* sanity check the keys */
2216 if (HeSVKEY(entry)) {
2217 /* Don't know what to check on SV keys. */
2218 } else if (HeKUTF8(entry)) {
2220 if (HeKWASUTF8(entry)) {
2221 PerlIO_printf(Perl_debug_log,
2222 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2223 (int) HeKLEN(entry), HeKEY(entry));
2226 } else if (HeKWASUTF8(entry)) {
2230 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2231 if (HvUSEDKEYS(hv) != real) {
2232 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2233 (int) real, (int) HvUSEDKEYS(hv));
2236 if (HvPLACEHOLDERS(hv) != placeholders) {
2237 PerlIO_printf(Perl_debug_log,
2238 "Count %d placeholder(s), but hash reports %d\n",
2239 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2243 if (withflags && ! HvHASKFLAGS(hv)) {
2244 PerlIO_printf(Perl_debug_log,
2245 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2252 HvRITER(hv) = riter; /* Restore hash iterator state */
2253 HvEITER(hv) = eiter;