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 lval)
191 HE *hek = hv_fetch_common (hv, NULL, key, klen, 0,
192 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0),
194 return hek ? &HeVAL(hek) : NULL;
197 /* returns an HE * structure with the all fields set */
198 /* note that hent_val will be a mortal sv for MAGICAL hashes */
200 =for apidoc hv_fetch_ent
202 Returns the hash entry which corresponds to the specified key in the hash.
203 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
204 if you want the function to compute it. IF C<lval> is set then the fetch
205 will be part of a store. Make sure the return value is non-null before
206 accessing it. The return value when C<tb> is a tied hash is a pointer to a
207 static location, so be sure to make a copy of the structure if you need to
210 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
211 information on how to use this function on tied hashes.
217 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
219 return hv_fetch_common(hv, keysv, NULL, 0, 0, lval ? HV_FETCH_LVALUE : 0,
224 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, I32 klen_i32,
225 int flags, int action, register U32 hash)
239 key = SvPV(keysv, klen);
240 is_utf8 = (SvUTF8(keysv) != 0);
252 if (SvRMAGICAL(hv)) {
253 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
256 /* XXX should be able to skimp on the HE/HEK here when
257 HV_FETCH_JUST_SV is true. */
260 keysv = newSVpvn(key, klen);
265 keysv = newSVsv(keysv);
267 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
270 /* grab a fake HE/HEK pair from the pool or make a new one */
271 entry = PL_hv_fetch_ent_mh;
273 PL_hv_fetch_ent_mh = HeNEXT(entry);
277 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
278 HeKEY_hek(entry) = (HEK*)k;
280 HeNEXT(entry) = Nullhe;
281 HeSVKEY_set(entry, keysv);
283 sv_upgrade(sv, SVt_PVLV);
285 LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
287 /* XXX remove at some point? */
288 if (flags & HVhek_FREEKEY)
293 #ifdef ENV_IS_CASELESS
294 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
296 for (i = 0; i < klen; ++i)
297 if (isLOWER(key[i])) {
298 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
299 (void)strupr(SvPVX(nkeysv));
300 entry = hv_fetch_common(hv, nkeysv, NULL, 0, 0, 0);
301 if (!entry && (action & HV_FETCH_LVALUE))
302 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
304 /* XXX remove at some point? */
305 if (flags & HVhek_FREEKEY)
314 xhv = (XPVHV*)SvANY(hv);
315 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
316 if ((action & HV_FETCH_LVALUE)
317 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
318 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
321 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
322 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
325 /* XXX remove at some point? */
326 if (flags & HVhek_FREEKEY)
334 int oldflags = flags;
335 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
339 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
340 if (oldflags & HVhek_FREEKEY)
346 PERL_HASH_INTERNAL(hash, key, klen);
347 /* Yes, you do need this even though you are not "storing" because
348 you can flip the flags below if doing an lval lookup. (And that
349 was put in to give the semantics Andreas was expecting.) */
350 flags |= HVhek_REHASH;
352 if (keysv && (SvIsCOW_shared_hash(keysv))) {
355 PERL_HASH(hash, key, klen);
359 masked_flags = (flags & HVhek_MASK);
361 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
362 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
363 for (; entry; entry = HeNEXT(entry)) {
364 if (HeHASH(entry) != hash) /* strings can't be equal */
366 if (HeKLEN(entry) != (I32)klen)
368 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
370 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
372 if ((action & HV_FETCH_LVALUE) && HeKFLAGS(entry) != masked_flags) {
373 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
374 But if entry was set previously with HVhek_WASUTF8 and key now
375 doesn't (or vice versa) then we should change the key's flag,
376 as this is assignment. */
377 if (HvSHAREKEYS(hv)) {
378 /* Need to swap the key we have for a key with the flags we
379 need. As keys are shared we can't just write to the flag,
380 so we share the new one, unshare the old one. */
381 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
382 unshare_hek (HeKEY_hek(entry));
383 HeKEY_hek(entry) = new_hek;
386 HeKFLAGS(entry) = masked_flags;
387 if (masked_flags & HVhek_ENABLEHVKFLAGS)
390 /* if we find a placeholder, we pretend we haven't found anything */
391 if (HeVAL(entry) == &PL_sv_placeholder)
393 if (flags & HVhek_FREEKEY)
397 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
398 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
400 char *env = PerlEnv_ENVgetenv_len(key,&len);
402 /* XXX remove once common API complete */
404 nkeysv = sv_2mortal(newSVpvn(key,klen));
407 sv = newSVpvn(env,len);
409 if (flags & HVhek_FREEKEY)
411 return hv_store_ent(hv,keysv,sv,hash);
415 if (!entry && SvREADONLY(hv)) {
416 S_hv_notallowed(aTHX_ flags, key, klen,
417 "access disallowed key '%"SVf"' in"
420 if (action & HV_FETCH_LVALUE) {
421 /* XXX remove once common API complete */
423 keysv = sv_2mortal(newSVpvn(key,klen));
427 if (flags & HVhek_FREEKEY)
429 if (action & HV_FETCH_LVALUE) {
430 /* gonna assign to this, so it better be there */
432 return hv_store_ent(hv,keysv,sv,hash);
438 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
440 MAGIC *mg = SvMAGIC(hv);
444 if (isUPPER(mg->mg_type)) {
446 switch (mg->mg_type) {
447 case PERL_MAGIC_tied:
449 *needs_store = FALSE;
452 mg = mg->mg_moremagic;
459 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
460 the length of the key. The C<hash> parameter is the precomputed hash
461 value; if it is zero then Perl will compute it. The return value will be
462 NULL if the operation failed or if the value did not need to be actually
463 stored within the hash (as in the case of tied hashes). Otherwise it can
464 be dereferenced to get the original C<SV*>. Note that the caller is
465 responsible for suitably incrementing the reference count of C<val> before
466 the call, and decrementing it if the function returned NULL. Effectively
467 a successful hv_store takes ownership of one reference to C<val>. This is
468 usually what you want; a newly created SV has a reference count of one, so
469 if all your code does is create SVs then store them in a hash, hv_store
470 will own the only reference to the new SV, and your code doesn't need to do
471 anything further to tidy up. hv_store is not implemented as a call to
472 hv_store_ent, and does not create a temporary SV for the key, so if your
473 key data is not already in SV form then use hv_store in preference to
476 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
477 information on how to use this function on tied hashes.
483 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
485 bool is_utf8 = FALSE;
486 const char *keysave = key;
495 STRLEN tmplen = klen;
496 /* Just casting the &klen to (STRLEN) won't work well
497 * if STRLEN and I32 are of different widths. --jhi */
498 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
500 /* If we were able to downgrade here, then than means that we were
501 passed in a key which only had chars 0-255, but was utf8 encoded. */
504 /* If we found we were able to downgrade the string to bytes, then
505 we should flag that it needs upgrading on keys or each. */
507 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
510 return hv_store_flags (hv, key, klen, val, hash, flags);
514 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
515 register U32 hash, int flags)
518 register U32 n_links;
520 register HE **oentry;
525 xhv = (XPVHV*)SvANY(hv);
529 hv_magic_check (hv, &needs_copy, &needs_store);
531 if (flags & HVhek_UTF8) {
532 /* This hack based on the code in hv_exists_ent seems to be
533 the easiest way to pass the utf8 flag through and fix
534 the bug in hv_exists for tied hashes with utf8 keys. */
535 SV *keysv = sv_2mortal(newSVpvn(key, klen));
537 mg_copy((SV*)hv, val, (char *)keysv, HEf_SVKEY);
539 mg_copy((SV*)hv, val, key, klen);
541 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
542 if (flags & HVhek_FREEKEY)
546 #ifdef ENV_IS_CASELESS
547 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
548 key = savepvn(key,klen);
549 key = (const char*)strupr((char*)key);
557 HvHASKFLAGS_on((SV*)hv);
560 /* We don't have a pointer to the hv, so we have to replicate the
561 flag into every HEK, so that hv_iterkeysv can see it. */
562 flags |= HVhek_REHASH;
563 PERL_HASH_INTERNAL(hash, key, klen);
565 PERL_HASH(hash, key, klen);
567 if (!xhv->xhv_array /* !HvARRAY(hv) */)
568 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
569 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
572 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
573 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
577 for (entry = *oentry; entry; ++n_links, entry = HeNEXT(entry)) {
578 if (HeHASH(entry) != hash) /* strings can't be equal */
580 if (HeKLEN(entry) != (I32)klen)
582 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
584 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
586 if (HeVAL(entry) == &PL_sv_placeholder)
587 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
589 SvREFCNT_dec(HeVAL(entry));
590 if (flags & HVhek_PLACEHOLD) {
591 /* We have been requested to insert a placeholder. Currently
592 only Storable is allowed to do this. */
593 xhv->xhv_placeholders++;
594 HeVAL(entry) = &PL_sv_placeholder;
598 if (HeKFLAGS(entry) != flags) {
599 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
600 But if entry was set previously with HVhek_WASUTF8 and key now
601 doesn't (or vice versa) then we should change the key's flag,
602 as this is assignment. */
603 if (HvSHAREKEYS(hv)) {
604 /* Need to swap the key we have for a key with the flags we
605 need. As keys are shared we can't just write to the flag,
606 so we share the new one, unshare the old one. */
607 int flags_nofree = flags & ~HVhek_FREEKEY;
608 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
609 unshare_hek (HeKEY_hek(entry));
610 HeKEY_hek(entry) = new_hek;
613 HeKFLAGS(entry) = flags;
615 if (flags & HVhek_FREEKEY)
617 return &HeVAL(entry);
620 if (SvREADONLY(hv)) {
621 S_hv_notallowed(aTHX_ flags, key, klen,
622 "access disallowed key '%"SVf"' to"
627 /* share_hek_flags will do the free for us. This might be considered
630 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
631 else /* gotta do the real thing */
632 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
633 if (flags & HVhek_PLACEHOLD) {
634 /* We have been requested to insert a placeholder. Currently
635 only Storable is allowed to do this. */
636 xhv->xhv_placeholders++;
637 HeVAL(entry) = &PL_sv_placeholder;
640 HeNEXT(entry) = *oentry;
643 xhv->xhv_keys++; /* HvKEYS(hv)++ */
644 if (!n_links) { /* initial entry? */
645 xhv->xhv_fill++; /* HvFILL(hv)++ */
646 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
647 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
648 /* Use the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
649 splits on a rehashed hash, as we're not going to split it again,
650 and if someone is lucky (evil) enough to get all the keys in one
651 list they could exhaust our memory as we repeatedly double the
652 number of buckets on every entry. Linear search feels a less worse
657 return &HeVAL(entry);
661 =for apidoc hv_store_ent
663 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
664 parameter is the precomputed hash value; if it is zero then Perl will
665 compute it. The return value is the new hash entry so created. It will be
666 NULL if the operation failed or if the value did not need to be actually
667 stored within the hash (as in the case of tied hashes). Otherwise the
668 contents of the return value can be accessed using the C<He?> macros
669 described here. Note that the caller is responsible for suitably
670 incrementing the reference count of C<val> before the call, and
671 decrementing it if the function returned NULL. Effectively a successful
672 hv_store_ent takes ownership of one reference to C<val>. This is
673 usually what you want; a newly created SV has a reference count of one, so
674 if all your code does is create SVs then store them in a hash, hv_store
675 will own the only reference to the new SV, and your code doesn't need to do
676 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
677 unlike C<val> it does not take ownership of it, so maintaining the correct
678 reference count on C<key> is entirely the caller's responsibility. hv_store
679 is not implemented as a call to hv_store_ent, and does not create a temporary
680 SV for the key, so if your key data is not already in SV form then use
681 hv_store in preference to hv_store_ent.
683 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
684 information on how to use this function on tied hashes.
690 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
705 xhv = (XPVHV*)SvANY(hv);
709 hv_magic_check (hv, &needs_copy, &needs_store);
711 bool save_taint = PL_tainted;
713 PL_tainted = SvTAINTED(keysv);
714 keysv = sv_2mortal(newSVsv(keysv));
715 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
716 TAINT_IF(save_taint);
717 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
719 #ifdef ENV_IS_CASELESS
720 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
721 key = SvPV(keysv, klen);
722 keysv = sv_2mortal(newSVpvn(key,klen));
723 (void)strupr(SvPVX(keysv));
730 keysave = key = SvPV(keysv, klen);
731 is_utf8 = (SvUTF8(keysv) != 0);
734 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
738 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
739 HvHASKFLAGS_on((SV*)hv);
743 /* We don't have a pointer to the hv, so we have to replicate the
744 flag into every HEK, so that hv_iterkeysv can see it. */
745 flags |= HVhek_REHASH;
746 PERL_HASH_INTERNAL(hash, key, klen);
748 if SvIsCOW_shared_hash(keysv) {
751 PERL_HASH(hash, key, klen);
755 if (!xhv->xhv_array /* !HvARRAY(hv) */)
756 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
757 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
760 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
761 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
764 for (; entry; ++n_links, entry = HeNEXT(entry)) {
765 if (HeHASH(entry) != hash) /* strings can't be equal */
767 if (HeKLEN(entry) != (I32)klen)
769 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
771 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
773 if (HeVAL(entry) == &PL_sv_placeholder)
774 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
776 SvREFCNT_dec(HeVAL(entry));
778 if (HeKFLAGS(entry) != flags) {
779 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
780 But if entry was set previously with HVhek_WASUTF8 and key now
781 doesn't (or vice versa) then we should change the key's flag,
782 as this is assignment. */
783 if (HvSHAREKEYS(hv)) {
784 /* Need to swap the key we have for a key with the flags we
785 need. As keys are shared we can't just write to the flag,
786 so we share the new one, unshare the old one. */
787 int flags_nofree = flags & ~HVhek_FREEKEY;
788 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
789 unshare_hek (HeKEY_hek(entry));
790 HeKEY_hek(entry) = new_hek;
793 HeKFLAGS(entry) = flags;
795 if (flags & HVhek_FREEKEY)
800 if (SvREADONLY(hv)) {
801 S_hv_notallowed(aTHX_ flags, key, klen,
802 "access disallowed key '%"SVf"' to"
807 /* share_hek_flags will do the free for us. This might be considered
810 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
811 else /* gotta do the real thing */
812 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
814 HeNEXT(entry) = *oentry;
817 xhv->xhv_keys++; /* HvKEYS(hv)++ */
818 if (!n_links) { /* initial entry? */
819 xhv->xhv_fill++; /* HvFILL(hv)++ */
820 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
821 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
822 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
823 splits on a rehashed hash, as we're not going to split it again,
824 and if someone is lucky (evil) enough to get all the keys in one
825 list they could exhaust our memory as we repeatedly double the
826 number of buckets on every entry. Linear search feels a less worse
835 =for apidoc hv_delete
837 Deletes a key/value pair in the hash. The value SV is removed from the
838 hash and returned to the caller. The C<klen> is the length of the key.
839 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
846 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
848 return hv_delete_common(hv, NULL, key, klen, flags, 0);
852 =for apidoc hv_delete_ent
854 Deletes a key/value pair in the hash. The value SV is removed from the
855 hash and returned to the caller. The C<flags> value will normally be zero;
856 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
857 precomputed hash value, or 0 to ask for it to be computed.
863 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
865 return hv_delete_common(hv, keysv, NULL, 0, flags, hash);
869 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, I32 klen_i32,
876 register HE **oentry;
886 key = SvPV(keysv, klen);
887 is_utf8 = (SvUTF8(keysv) != 0);
899 if (SvRMAGICAL(hv)) {
902 hv_magic_check (hv, &needs_copy, &needs_store);
907 if ((entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
912 if ((svp = hv_fetch(hv, key, is_utf8 ? -klen : klen, TRUE))) {
921 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
922 /* No longer an element */
923 sv_unmagic(sv, PERL_MAGIC_tiedelem);
926 return Nullsv; /* element cannot be deleted */
929 #ifdef ENV_IS_CASELESS
930 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
931 /* XXX This code isn't UTF8 clean. */
932 keysv = sv_2mortal(newSVpvn(key,klen));
933 keysave = key = strupr(SvPVX(keysv));
940 xhv = (XPVHV*)SvANY(hv);
941 if (!xhv->xhv_array /* !HvARRAY(hv) */)
945 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
947 k_flags = HVhek_UTF8;
949 k_flags |= HVhek_FREEKEY;
953 PERL_HASH_INTERNAL(hash, key, klen);
955 PERL_HASH(hash, key, klen);
958 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
959 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
962 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
963 if (HeHASH(entry) != hash) /* strings can't be equal */
965 if (HeKLEN(entry) != (I32)klen)
967 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
969 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
971 if (k_flags & HVhek_FREEKEY)
974 /* if placeholder is here, it's already been deleted.... */
975 if (HeVAL(entry) == &PL_sv_placeholder)
978 return Nullsv; /* if still SvREADONLY, leave it deleted. */
980 /* okay, really delete the placeholder. */
981 *oentry = HeNEXT(entry);
983 xhv->xhv_fill--; /* HvFILL(hv)-- */
984 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
987 hv_free_ent(hv, entry);
988 xhv->xhv_keys--; /* HvKEYS(hv)-- */
989 if (xhv->xhv_keys == 0)
991 xhv->xhv_placeholders--;
994 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
995 S_hv_notallowed(aTHX_ k_flags, key, klen,
996 "delete readonly key '%"SVf"' from"
1000 if (flags & G_DISCARD)
1003 sv = sv_2mortal(HeVAL(entry));
1004 HeVAL(entry) = &PL_sv_placeholder;
1008 * If a restricted hash, rather than really deleting the entry, put
1009 * a placeholder there. This marks the key as being "approved", so
1010 * we can still access via not-really-existing key without raising
1013 if (SvREADONLY(hv)) {
1014 HeVAL(entry) = &PL_sv_placeholder;
1015 /* We'll be saving this slot, so the number of allocated keys
1016 * doesn't go down, but the number placeholders goes up */
1017 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1019 *oentry = HeNEXT(entry);
1021 xhv->xhv_fill--; /* HvFILL(hv)-- */
1022 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1025 hv_free_ent(hv, entry);
1026 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1027 if (xhv->xhv_keys == 0)
1028 HvHASKFLAGS_off(hv);
1032 if (SvREADONLY(hv)) {
1033 S_hv_notallowed(aTHX_ k_flags, key, klen,
1034 "delete disallowed key '%"SVf"' from"
1038 if (k_flags & HVhek_FREEKEY)
1044 =for apidoc hv_exists
1046 Returns a boolean indicating whether the specified hash key exists. The
1047 C<klen> is the length of the key.
1053 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1055 return hv_exists_common(hv, NULL, key, klen, 0);
1059 =for apidoc hv_exists_ent
1061 Returns a boolean indicating whether the specified hash key exists. C<hash>
1062 can be a valid precomputed hash value, or 0 to ask for it to be
1069 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1071 return hv_exists_common(hv, keysv, NULL, 0, hash);
1075 S_hv_exists_common(pTHX_ HV *hv, SV *keysv, const char *key, I32 klen_i32,
1078 register XPVHV* xhv;
1083 const char *keysave;
1090 key = SvPV(keysv, klen);
1091 is_utf8 = (SvUTF8(keysv) != 0);
1103 if (SvRMAGICAL(hv)) {
1104 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1107 if (keysv || is_utf8) {
1109 keysv = newSVpvn(key, klen);
1112 keysv = newSVsv(keysv);
1114 key = (char *)sv_2mortal(keysv);
1118 /* I don't understand why hv_exists_ent has svret and sv,
1119 whereas hv_exists only had one. */
1120 svret = sv_newmortal();
1121 sv = sv_newmortal();
1122 mg_copy((SV*)hv, sv, key, klen);
1123 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1124 return (bool)SvTRUE(svret);
1126 #ifdef ENV_IS_CASELESS
1127 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1128 /* XXX This code isn't UTF8 clean. */
1129 keysv = sv_2mortal(newSVpvn(key,klen));
1130 keysave = key = strupr(SvPVX(keysv));
1137 xhv = (XPVHV*)SvANY(hv);
1138 #ifndef DYNAMIC_ENV_FETCH
1139 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1144 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1146 k_flags = HVhek_UTF8;
1148 k_flags |= HVhek_FREEKEY;
1151 PERL_HASH_INTERNAL(hash, key, klen);
1153 PERL_HASH(hash, key, klen);
1155 #ifdef DYNAMIC_ENV_FETCH
1156 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1159 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1160 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1161 for (; entry; entry = HeNEXT(entry)) {
1162 if (HeHASH(entry) != hash) /* strings can't be equal */
1164 if (HeKLEN(entry) != (I32)klen)
1166 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1168 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1170 if (k_flags & HVhek_FREEKEY)
1172 /* If we find the key, but the value is a placeholder, return false. */
1173 if (HeVAL(entry) == &PL_sv_placeholder)
1177 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1178 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1180 char *env = PerlEnv_ENVgetenv_len(key,&len);
1182 sv = newSVpvn(env,len);
1184 (void)hv_store_ent(hv,keysv,sv,hash);
1185 if (k_flags & HVhek_FREEKEY)
1191 if (k_flags & HVhek_FREEKEY)
1198 S_hsplit(pTHX_ HV *hv)
1200 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1201 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1202 register I32 newsize = oldsize * 2;
1204 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1208 register HE **oentry;
1209 int longest_chain = 0;
1213 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1214 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1220 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1225 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1226 if (oldsize >= 64) {
1227 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1228 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1231 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1235 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1236 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1237 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1240 for (i=0; i<oldsize; i++,aep++) {
1241 int left_length = 0;
1242 int right_length = 0;
1244 if (!*aep) /* non-existent */
1247 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1248 if ((HeHASH(entry) & newsize) != (U32)i) {
1249 *oentry = HeNEXT(entry);
1250 HeNEXT(entry) = *bep;
1252 xhv->xhv_fill++; /* HvFILL(hv)++ */
1258 oentry = &HeNEXT(entry);
1262 if (!*aep) /* everything moved */
1263 xhv->xhv_fill--; /* HvFILL(hv)-- */
1264 /* I think we don't actually need to keep track of the longest length,
1265 merely flag if anything is too long. But for the moment while
1266 developing this code I'll track it. */
1267 if (left_length > longest_chain)
1268 longest_chain = left_length;
1269 if (right_length > longest_chain)
1270 longest_chain = right_length;
1274 /* Pick your policy for "hashing isn't working" here: */
1275 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1280 if (hv == PL_strtab) {
1281 /* Urg. Someone is doing something nasty to the string table.
1286 /* Awooga. Awooga. Pathological data. */
1287 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1288 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1291 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1292 was_shared = HvSHAREKEYS(hv);
1295 HvSHAREKEYS_off(hv);
1298 aep = (HE **) xhv->xhv_array;
1300 for (i=0; i<newsize; i++,aep++) {
1303 /* We're going to trash this HE's next pointer when we chain it
1304 into the new hash below, so store where we go next. */
1305 HE *next = HeNEXT(entry);
1309 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1314 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1315 hash, HeKFLAGS(entry));
1316 unshare_hek (HeKEY_hek(entry));
1317 HeKEY_hek(entry) = new_hek;
1319 /* Not shared, so simply write the new hash in. */
1320 HeHASH(entry) = hash;
1322 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1323 HEK_REHASH_on(HeKEY_hek(entry));
1324 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1326 /* Copy oentry to the correct new chain. */
1327 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1329 xhv->xhv_fill++; /* HvFILL(hv)++ */
1330 HeNEXT(entry) = *bep;
1336 Safefree (xhv->xhv_array);
1337 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1341 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1343 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1344 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1345 register I32 newsize;
1351 register HE **oentry;
1353 newsize = (I32) newmax; /* possible truncation here */
1354 if (newsize != newmax || newmax <= oldsize)
1356 while ((newsize & (1 + ~newsize)) != newsize) {
1357 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1359 if (newsize < newmax)
1361 if (newsize < newmax)
1362 return; /* overflow detection */
1364 a = xhv->xhv_array; /* HvARRAY(hv) */
1367 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1368 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1374 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1379 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1380 if (oldsize >= 64) {
1381 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1382 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1385 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1388 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1391 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1393 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1394 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1395 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1399 for (i=0; i<oldsize; i++,aep++) {
1400 if (!*aep) /* non-existent */
1402 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1403 if ((j = (HeHASH(entry) & newsize)) != i) {
1405 *oentry = HeNEXT(entry);
1406 if (!(HeNEXT(entry) = aep[j]))
1407 xhv->xhv_fill++; /* HvFILL(hv)++ */
1412 oentry = &HeNEXT(entry);
1414 if (!*aep) /* everything moved */
1415 xhv->xhv_fill--; /* HvFILL(hv)-- */
1422 Creates a new HV. The reference count is set to 1.
1431 register XPVHV* xhv;
1433 hv = (HV*)NEWSV(502,0);
1434 sv_upgrade((SV *)hv, SVt_PVHV);
1435 xhv = (XPVHV*)SvANY(hv);
1438 #ifndef NODEFAULT_SHAREKEYS
1439 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1442 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1443 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1444 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1445 (void)hv_iterinit(hv); /* so each() will start off right */
1450 Perl_newHVhv(pTHX_ HV *ohv)
1453 STRLEN hv_max, hv_fill;
1455 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1457 hv_max = HvMAX(ohv);
1459 if (!SvMAGICAL((SV *)ohv)) {
1460 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1462 bool shared = !!HvSHAREKEYS(ohv);
1463 HE **ents, **oents = (HE **)HvARRAY(ohv);
1465 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1468 /* In each bucket... */
1469 for (i = 0; i <= hv_max; i++) {
1470 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1477 /* Copy the linked list of entries. */
1478 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1479 U32 hash = HeHASH(oent);
1480 char *key = HeKEY(oent);
1481 STRLEN len = HeKLEN(oent);
1482 int flags = HeKFLAGS(oent);
1485 HeVAL(ent) = newSVsv(HeVAL(oent));
1487 = shared ? share_hek_flags(key, len, hash, flags)
1488 : save_hek_flags(key, len, hash, flags);
1499 HvFILL(hv) = hv_fill;
1500 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1504 /* Iterate over ohv, copying keys and values one at a time. */
1506 I32 riter = HvRITER(ohv);
1507 HE *eiter = HvEITER(ohv);
1509 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1510 while (hv_max && hv_max + 1 >= hv_fill * 2)
1511 hv_max = hv_max / 2;
1515 while ((entry = hv_iternext_flags(ohv, 0))) {
1516 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1517 newSVsv(HeVAL(entry)), HeHASH(entry),
1520 HvRITER(ohv) = riter;
1521 HvEITER(ohv) = eiter;
1528 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1535 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1536 PL_sub_generation++; /* may be deletion of method from stash */
1538 if (HeKLEN(entry) == HEf_SVKEY) {
1539 SvREFCNT_dec(HeKEY_sv(entry));
1540 Safefree(HeKEY_hek(entry));
1542 else if (HvSHAREKEYS(hv))
1543 unshare_hek(HeKEY_hek(entry));
1545 Safefree(HeKEY_hek(entry));
1550 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1554 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1555 PL_sub_generation++; /* may be deletion of method from stash */
1556 sv_2mortal(HeVAL(entry)); /* free between statements */
1557 if (HeKLEN(entry) == HEf_SVKEY) {
1558 sv_2mortal(HeKEY_sv(entry));
1559 Safefree(HeKEY_hek(entry));
1561 else if (HvSHAREKEYS(hv))
1562 unshare_hek(HeKEY_hek(entry));
1564 Safefree(HeKEY_hek(entry));
1569 =for apidoc hv_clear
1571 Clears a hash, making it empty.
1577 Perl_hv_clear(pTHX_ HV *hv)
1579 register XPVHV* xhv;
1583 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1585 xhv = (XPVHV*)SvANY(hv);
1587 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1588 /* restricted hash: convert all keys to placeholders */
1591 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1592 entry = ((HE**)xhv->xhv_array)[i];
1593 for (; entry; entry = HeNEXT(entry)) {
1594 /* not already placeholder */
1595 if (HeVAL(entry) != &PL_sv_placeholder) {
1596 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1597 SV* keysv = hv_iterkeysv(entry);
1599 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1602 SvREFCNT_dec(HeVAL(entry));
1603 HeVAL(entry) = &PL_sv_placeholder;
1604 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1612 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1613 if (xhv->xhv_array /* HvARRAY(hv) */)
1614 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1615 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1620 HvHASKFLAGS_off(hv);
1625 S_hfreeentries(pTHX_ HV *hv)
1627 register HE **array;
1629 register HE *oentry = Null(HE*);
1640 array = HvARRAY(hv);
1641 /* make everyone else think the array is empty, so that the destructors
1642 * called for freed entries can't recusively mess with us */
1643 HvARRAY(hv) = Null(HE**);
1645 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1651 entry = HeNEXT(entry);
1652 hv_free_ent(hv, oentry);
1657 entry = array[riter];
1660 HvARRAY(hv) = array;
1661 (void)hv_iterinit(hv);
1665 =for apidoc hv_undef
1673 Perl_hv_undef(pTHX_ HV *hv)
1675 register XPVHV* xhv;
1678 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1679 xhv = (XPVHV*)SvANY(hv);
1681 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1684 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1685 Safefree(HvNAME(hv));
1688 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1689 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1690 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1697 =for apidoc hv_iterinit
1699 Prepares a starting point to traverse a hash table. Returns the number of
1700 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1701 currently only meaningful for hashes without tie magic.
1703 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1704 hash buckets that happen to be in use. If you still need that esoteric
1705 value, you can get it through the macro C<HvFILL(tb)>.
1712 Perl_hv_iterinit(pTHX_ HV *hv)
1714 register XPVHV* xhv;
1718 Perl_croak(aTHX_ "Bad hash");
1719 xhv = (XPVHV*)SvANY(hv);
1720 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1721 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1723 hv_free_ent(hv, entry);
1725 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1726 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1727 /* used to be xhv->xhv_fill before 5.004_65 */
1728 return XHvTOTALKEYS(xhv);
1731 =for apidoc hv_iternext
1733 Returns entries from a hash iterator. See C<hv_iterinit>.
1735 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1736 iterator currently points to, without losing your place or invalidating your
1737 iterator. Note that in this case the current entry is deleted from the hash
1738 with your iterator holding the last reference to it. Your iterator is flagged
1739 to free the entry on the next call to C<hv_iternext>, so you must not discard
1740 your iterator immediately else the entry will leak - call C<hv_iternext> to
1741 trigger the resource deallocation.
1747 Perl_hv_iternext(pTHX_ HV *hv)
1749 return hv_iternext_flags(hv, 0);
1753 =for apidoc hv_iternext_flags
1755 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1756 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1757 set the placeholders keys (for restricted hashes) will be returned in addition
1758 to normal keys. By default placeholders are automatically skipped over.
1759 Currently a placeholder is implemented with a value that is
1760 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1761 restricted hashes may change, and the implementation currently is
1762 insufficiently abstracted for any change to be tidy.
1768 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1770 register XPVHV* xhv;
1776 Perl_croak(aTHX_ "Bad hash");
1777 xhv = (XPVHV*)SvANY(hv);
1778 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1780 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1781 SV *key = sv_newmortal();
1783 sv_setsv(key, HeSVKEY_force(entry));
1784 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1790 /* one HE per MAGICAL hash */
1791 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1793 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1795 HeKEY_hek(entry) = hek;
1796 HeKLEN(entry) = HEf_SVKEY;
1798 magic_nextpack((SV*) hv,mg,key);
1800 /* force key to stay around until next time */
1801 HeSVKEY_set(entry, SvREFCNT_inc(key));
1802 return entry; /* beware, hent_val is not set */
1805 SvREFCNT_dec(HeVAL(entry));
1806 Safefree(HeKEY_hek(entry));
1808 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1811 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1812 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1816 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1817 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1818 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1820 /* At start of hash, entry is NULL. */
1823 entry = HeNEXT(entry);
1824 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1826 * Skip past any placeholders -- don't want to include them in
1829 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1830 entry = HeNEXT(entry);
1835 /* OK. Come to the end of the current list. Grab the next one. */
1837 xhv->xhv_riter++; /* HvRITER(hv)++ */
1838 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1839 /* There is no next one. End of the hash. */
1840 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1843 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1844 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1846 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1847 /* If we have an entry, but it's a placeholder, don't count it.
1849 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1850 entry = HeNEXT(entry);
1852 /* Will loop again if this linked list starts NULL
1853 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1854 or if we run through it and find only placeholders. */
1857 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1859 hv_free_ent(hv, oldentry);
1862 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1863 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1865 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1870 =for apidoc hv_iterkey
1872 Returns the key from the current position of the hash iterator. See
1879 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1881 if (HeKLEN(entry) == HEf_SVKEY) {
1883 char *p = SvPV(HeKEY_sv(entry), len);
1888 *retlen = HeKLEN(entry);
1889 return HeKEY(entry);
1893 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1895 =for apidoc hv_iterkeysv
1897 Returns the key as an C<SV*> from the current position of the hash
1898 iterator. The return value will always be a mortal copy of the key. Also
1905 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1907 if (HeKLEN(entry) != HEf_SVKEY) {
1908 HEK *hek = HeKEY_hek(entry);
1909 int flags = HEK_FLAGS(hek);
1912 if (flags & HVhek_WASUTF8) {
1914 Andreas would like keys he put in as utf8 to come back as utf8
1916 STRLEN utf8_len = HEK_LEN(hek);
1917 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1919 sv = newSVpvn ((char*)as_utf8, utf8_len);
1921 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1922 } else if (flags & HVhek_REHASH) {
1923 /* We don't have a pointer to the hv, so we have to replicate the
1924 flag into every HEK. This hv is using custom a hasing
1925 algorithm. Hence we can't return a shared string scalar, as
1926 that would contain the (wrong) hash value, and might get passed
1927 into an hv routine with a regular hash */
1929 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1933 sv = newSVpvn_share(HEK_KEY(hek),
1934 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1937 return sv_2mortal(sv);
1939 return sv_mortalcopy(HeKEY_sv(entry));
1943 =for apidoc hv_iterval
1945 Returns the value from the current position of the hash iterator. See
1952 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1954 if (SvRMAGICAL(hv)) {
1955 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1956 SV* sv = sv_newmortal();
1957 if (HeKLEN(entry) == HEf_SVKEY)
1958 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1959 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1963 return HeVAL(entry);
1967 =for apidoc hv_iternextsv
1969 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1976 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1979 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1981 *key = hv_iterkey(he, retlen);
1982 return hv_iterval(hv, he);
1986 =for apidoc hv_magic
1988 Adds magic to a hash. See C<sv_magic>.
1994 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1996 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1999 #if 0 /* use the macro from hv.h instead */
2002 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2004 return HEK_KEY(share_hek(sv, len, hash));
2009 /* possibly free a shared string if no one has access to it
2010 * len and hash must both be valid for str.
2013 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2015 unshare_hek_or_pvn (NULL, str, len, hash);
2020 Perl_unshare_hek(pTHX_ HEK *hek)
2022 unshare_hek_or_pvn(hek, NULL, 0, 0);
2025 /* possibly free a shared string if no one has access to it
2026 hek if non-NULL takes priority over the other 3, else str, len and hash
2027 are used. If so, len and hash must both be valid for str.
2030 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2032 register XPVHV* xhv;
2034 register HE **oentry;
2037 bool is_utf8 = FALSE;
2039 const char *save = str;
2042 hash = HEK_HASH(hek);
2043 } else if (len < 0) {
2044 STRLEN tmplen = -len;
2046 /* See the note in hv_fetch(). --jhi */
2047 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2050 k_flags = HVhek_UTF8;
2052 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2055 /* what follows is the moral equivalent of:
2056 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2057 if (--*Svp == Nullsv)
2058 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2060 xhv = (XPVHV*)SvANY(PL_strtab);
2061 /* assert(xhv_array != 0) */
2063 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2064 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2066 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2067 if (HeKEY_hek(entry) != hek)
2073 int flags_masked = k_flags & HVhek_MASK;
2074 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2075 if (HeHASH(entry) != hash) /* strings can't be equal */
2077 if (HeKLEN(entry) != len)
2079 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2081 if (HeKFLAGS(entry) != flags_masked)
2089 if (--HeVAL(entry) == Nullsv) {
2090 *oentry = HeNEXT(entry);
2092 xhv->xhv_fill--; /* HvFILL(hv)-- */
2093 Safefree(HeKEY_hek(entry));
2095 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2099 UNLOCK_STRTAB_MUTEX;
2100 if (!found && ckWARN_d(WARN_INTERNAL))
2101 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2102 "Attempt to free non-existent shared string '%s'%s",
2103 hek ? HEK_KEY(hek) : str,
2104 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2105 if (k_flags & HVhek_FREEKEY)
2109 /* get a (constant) string ptr from the global string table
2110 * string will get added if it is not already there.
2111 * len and hash must both be valid for str.
2114 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2116 bool is_utf8 = FALSE;
2118 const char *save = str;
2121 STRLEN tmplen = -len;
2123 /* See the note in hv_fetch(). --jhi */
2124 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2126 /* If we were able to downgrade here, then than means that we were passed
2127 in a key which only had chars 0-255, but was utf8 encoded. */
2130 /* If we found we were able to downgrade the string to bytes, then
2131 we should flag that it needs upgrading on keys or each. Also flag
2132 that we need share_hek_flags to free the string. */
2134 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2137 return share_hek_flags (str, len, hash, flags);
2141 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2143 register XPVHV* xhv;
2145 register HE **oentry;
2148 int flags_masked = flags & HVhek_MASK;
2150 /* what follows is the moral equivalent of:
2152 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2153 hv_store(PL_strtab, str, len, Nullsv, hash);
2155 Can't rehash the shared string table, so not sure if it's worth
2156 counting the number of entries in the linked list
2158 xhv = (XPVHV*)SvANY(PL_strtab);
2159 /* assert(xhv_array != 0) */
2161 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2162 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2163 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2164 if (HeHASH(entry) != hash) /* strings can't be equal */
2166 if (HeKLEN(entry) != len)
2168 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2170 if (HeKFLAGS(entry) != flags_masked)
2177 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2178 HeVAL(entry) = Nullsv;
2179 HeNEXT(entry) = *oentry;
2181 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2182 if (i) { /* initial entry? */
2183 xhv->xhv_fill++; /* HvFILL(hv)++ */
2184 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2189 ++HeVAL(entry); /* use value slot as REFCNT */
2190 UNLOCK_STRTAB_MUTEX;
2192 if (flags & HVhek_FREEKEY)
2195 return HeKEY_hek(entry);
2200 =for apidoc hv_assert
2202 Check that a hash is in an internally consistent state.
2208 Perl_hv_assert(pTHX_ HV *hv)
2212 int placeholders = 0;
2215 I32 riter = HvRITER(hv);
2216 HE *eiter = HvEITER(hv);
2218 (void)hv_iterinit(hv);
2220 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2221 /* sanity check the values */
2222 if (HeVAL(entry) == &PL_sv_placeholder) {
2227 /* sanity check the keys */
2228 if (HeSVKEY(entry)) {
2229 /* Don't know what to check on SV keys. */
2230 } else if (HeKUTF8(entry)) {
2232 if (HeKWASUTF8(entry)) {
2233 PerlIO_printf(Perl_debug_log,
2234 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2235 (int) HeKLEN(entry), HeKEY(entry));
2238 } else if (HeKWASUTF8(entry)) {
2242 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2243 if (HvUSEDKEYS(hv) != real) {
2244 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2245 (int) real, (int) HvUSEDKEYS(hv));
2248 if (HvPLACEHOLDERS(hv) != placeholders) {
2249 PerlIO_printf(Perl_debug_log,
2250 "Count %d placeholder(s), but hash reports %d\n",
2251 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2255 if (withflags && ! HvHASKFLAGS(hv)) {
2256 PerlIO_printf(Perl_debug_log,
2257 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2264 HvRITER(hv) = riter; /* Restore hash iterator state */
2265 HvEITER(hv) = eiter;