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 =for apidoc hv_clear_placeholders
1627 Clears any placeholders from a hash. If a restricted hash has any of its keys
1628 marked as readonly and the key is subsequently deleted, the key is not actually
1629 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1630 it so it will be ignored by future operations such as iterating over the hash,
1631 but will still allow the hash to have a value reaasigned to the key at some
1632 future point. This function clears any such placeholder keys from the hash.
1633 See Hash::Util::lock_keys() for an example of its use.
1639 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1642 items = (I32)HvPLACEHOLDERS(hv);
1645 I32 riter = HvRITER(hv);
1646 HE *eiter = HvEITER(hv);
1648 /* This may look suboptimal with the items *after* the iternext, but
1649 it's quite deliberate. We only get here with items==0 if we've
1650 just deleted the last placeholder in the hash. If we've just done
1651 that then it means that the hash is in lazy delete mode, and the
1652 HE is now only referenced in our iterator. If we just quit the loop
1653 and discarded our iterator then the HE leaks. So we do the && the
1654 other way to ensure iternext is called just one more time, which
1655 has the side effect of triggering the lazy delete. */
1656 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
1658 SV *val = hv_iterval(hv, entry);
1660 if (val == &PL_sv_placeholder) {
1662 /* It seems that I have to go back in the front of the hash
1663 API to delete a hash, even though I have a HE structure
1664 pointing to the very entry I want to delete, and could hold
1665 onto the previous HE that points to it. And it's easier to
1666 go in with SVs as I can then specify the precomputed hash,
1667 and don't have fun and games with utf8 keys. */
1668 SV *key = hv_iterkeysv(entry);
1670 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
1674 HvRITER(hv) = riter;
1675 HvEITER(hv) = eiter;
1680 S_hfreeentries(pTHX_ HV *hv)
1682 register HE **array;
1684 register HE *oentry = Null(HE*);
1695 array = HvARRAY(hv);
1696 /* make everyone else think the array is empty, so that the destructors
1697 * called for freed entries can't recusively mess with us */
1698 HvARRAY(hv) = Null(HE**);
1700 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1706 entry = HeNEXT(entry);
1707 hv_free_ent(hv, oentry);
1712 entry = array[riter];
1715 HvARRAY(hv) = array;
1716 (void)hv_iterinit(hv);
1720 =for apidoc hv_undef
1728 Perl_hv_undef(pTHX_ HV *hv)
1730 register XPVHV* xhv;
1733 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1734 xhv = (XPVHV*)SvANY(hv);
1736 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1739 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1740 Safefree(HvNAME(hv));
1743 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1744 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1745 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1752 =for apidoc hv_iterinit
1754 Prepares a starting point to traverse a hash table. Returns the number of
1755 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1756 currently only meaningful for hashes without tie magic.
1758 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1759 hash buckets that happen to be in use. If you still need that esoteric
1760 value, you can get it through the macro C<HvFILL(tb)>.
1767 Perl_hv_iterinit(pTHX_ HV *hv)
1769 register XPVHV* xhv;
1773 Perl_croak(aTHX_ "Bad hash");
1774 xhv = (XPVHV*)SvANY(hv);
1775 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1776 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1778 hv_free_ent(hv, entry);
1780 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1781 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1782 /* used to be xhv->xhv_fill before 5.004_65 */
1783 return XHvTOTALKEYS(xhv);
1786 =for apidoc hv_iternext
1788 Returns entries from a hash iterator. See C<hv_iterinit>.
1790 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1791 iterator currently points to, without losing your place or invalidating your
1792 iterator. Note that in this case the current entry is deleted from the hash
1793 with your iterator holding the last reference to it. Your iterator is flagged
1794 to free the entry on the next call to C<hv_iternext>, so you must not discard
1795 your iterator immediately else the entry will leak - call C<hv_iternext> to
1796 trigger the resource deallocation.
1802 Perl_hv_iternext(pTHX_ HV *hv)
1804 return hv_iternext_flags(hv, 0);
1808 =for apidoc hv_iternext_flags
1810 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1811 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1812 set the placeholders keys (for restricted hashes) will be returned in addition
1813 to normal keys. By default placeholders are automatically skipped over.
1814 Currently a placeholder is implemented with a value that is
1815 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1816 restricted hashes may change, and the implementation currently is
1817 insufficiently abstracted for any change to be tidy.
1823 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1825 register XPVHV* xhv;
1831 Perl_croak(aTHX_ "Bad hash");
1832 xhv = (XPVHV*)SvANY(hv);
1833 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1835 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1836 SV *key = sv_newmortal();
1838 sv_setsv(key, HeSVKEY_force(entry));
1839 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1845 /* one HE per MAGICAL hash */
1846 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1848 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1850 HeKEY_hek(entry) = hek;
1851 HeKLEN(entry) = HEf_SVKEY;
1853 magic_nextpack((SV*) hv,mg,key);
1855 /* force key to stay around until next time */
1856 HeSVKEY_set(entry, SvREFCNT_inc(key));
1857 return entry; /* beware, hent_val is not set */
1860 SvREFCNT_dec(HeVAL(entry));
1861 Safefree(HeKEY_hek(entry));
1863 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1866 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1867 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1871 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1872 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1873 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1875 /* At start of hash, entry is NULL. */
1878 entry = HeNEXT(entry);
1879 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1881 * Skip past any placeholders -- don't want to include them in
1884 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1885 entry = HeNEXT(entry);
1890 /* OK. Come to the end of the current list. Grab the next one. */
1892 xhv->xhv_riter++; /* HvRITER(hv)++ */
1893 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1894 /* There is no next one. End of the hash. */
1895 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1898 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1899 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1901 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1902 /* If we have an entry, but it's a placeholder, don't count it.
1904 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1905 entry = HeNEXT(entry);
1907 /* Will loop again if this linked list starts NULL
1908 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1909 or if we run through it and find only placeholders. */
1912 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1914 hv_free_ent(hv, oldentry);
1917 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1918 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1920 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1925 =for apidoc hv_iterkey
1927 Returns the key from the current position of the hash iterator. See
1934 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1936 if (HeKLEN(entry) == HEf_SVKEY) {
1938 char *p = SvPV(HeKEY_sv(entry), len);
1943 *retlen = HeKLEN(entry);
1944 return HeKEY(entry);
1948 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1950 =for apidoc hv_iterkeysv
1952 Returns the key as an C<SV*> from the current position of the hash
1953 iterator. The return value will always be a mortal copy of the key. Also
1960 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1962 if (HeKLEN(entry) != HEf_SVKEY) {
1963 HEK *hek = HeKEY_hek(entry);
1964 int flags = HEK_FLAGS(hek);
1967 if (flags & HVhek_WASUTF8) {
1969 Andreas would like keys he put in as utf8 to come back as utf8
1971 STRLEN utf8_len = HEK_LEN(hek);
1972 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1974 sv = newSVpvn ((char*)as_utf8, utf8_len);
1976 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1977 } else if (flags & HVhek_REHASH) {
1978 /* We don't have a pointer to the hv, so we have to replicate the
1979 flag into every HEK. This hv is using custom a hasing
1980 algorithm. Hence we can't return a shared string scalar, as
1981 that would contain the (wrong) hash value, and might get passed
1982 into an hv routine with a regular hash */
1984 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1988 sv = newSVpvn_share(HEK_KEY(hek),
1989 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1992 return sv_2mortal(sv);
1994 return sv_mortalcopy(HeKEY_sv(entry));
1998 =for apidoc hv_iterval
2000 Returns the value from the current position of the hash iterator. See
2007 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2009 if (SvRMAGICAL(hv)) {
2010 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2011 SV* sv = sv_newmortal();
2012 if (HeKLEN(entry) == HEf_SVKEY)
2013 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2014 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2018 return HeVAL(entry);
2022 =for apidoc hv_iternextsv
2024 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2031 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2034 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
2036 *key = hv_iterkey(he, retlen);
2037 return hv_iterval(hv, he);
2041 =for apidoc hv_magic
2043 Adds magic to a hash. See C<sv_magic>.
2049 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2051 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2054 #if 0 /* use the macro from hv.h instead */
2057 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2059 return HEK_KEY(share_hek(sv, len, hash));
2064 /* possibly free a shared string if no one has access to it
2065 * len and hash must both be valid for str.
2068 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2070 unshare_hek_or_pvn (NULL, str, len, hash);
2075 Perl_unshare_hek(pTHX_ HEK *hek)
2077 unshare_hek_or_pvn(hek, NULL, 0, 0);
2080 /* possibly free a shared string if no one has access to it
2081 hek if non-NULL takes priority over the other 3, else str, len and hash
2082 are used. If so, len and hash must both be valid for str.
2085 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2087 register XPVHV* xhv;
2089 register HE **oentry;
2092 bool is_utf8 = FALSE;
2094 const char *save = str;
2097 hash = HEK_HASH(hek);
2098 } else if (len < 0) {
2099 STRLEN tmplen = -len;
2101 /* See the note in hv_fetch(). --jhi */
2102 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2105 k_flags = HVhek_UTF8;
2107 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2110 /* what follows is the moral equivalent of:
2111 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2112 if (--*Svp == Nullsv)
2113 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2115 xhv = (XPVHV*)SvANY(PL_strtab);
2116 /* assert(xhv_array != 0) */
2118 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2119 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2121 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2122 if (HeKEY_hek(entry) != hek)
2128 int flags_masked = k_flags & HVhek_MASK;
2129 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2130 if (HeHASH(entry) != hash) /* strings can't be equal */
2132 if (HeKLEN(entry) != len)
2134 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2136 if (HeKFLAGS(entry) != flags_masked)
2144 if (--HeVAL(entry) == Nullsv) {
2145 *oentry = HeNEXT(entry);
2147 xhv->xhv_fill--; /* HvFILL(hv)-- */
2148 Safefree(HeKEY_hek(entry));
2150 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2154 UNLOCK_STRTAB_MUTEX;
2155 if (!found && ckWARN_d(WARN_INTERNAL))
2156 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2157 "Attempt to free non-existent shared string '%s'%s",
2158 hek ? HEK_KEY(hek) : str,
2159 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2160 if (k_flags & HVhek_FREEKEY)
2164 /* get a (constant) string ptr from the global string table
2165 * string will get added if it is not already there.
2166 * len and hash must both be valid for str.
2169 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2171 bool is_utf8 = FALSE;
2173 const char *save = str;
2176 STRLEN tmplen = -len;
2178 /* See the note in hv_fetch(). --jhi */
2179 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2181 /* If we were able to downgrade here, then than means that we were passed
2182 in a key which only had chars 0-255, but was utf8 encoded. */
2185 /* If we found we were able to downgrade the string to bytes, then
2186 we should flag that it needs upgrading on keys or each. Also flag
2187 that we need share_hek_flags to free the string. */
2189 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2192 return share_hek_flags (str, len, hash, flags);
2196 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2198 register XPVHV* xhv;
2200 register HE **oentry;
2203 int flags_masked = flags & HVhek_MASK;
2205 /* what follows is the moral equivalent of:
2207 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2208 hv_store(PL_strtab, str, len, Nullsv, hash);
2210 Can't rehash the shared string table, so not sure if it's worth
2211 counting the number of entries in the linked list
2213 xhv = (XPVHV*)SvANY(PL_strtab);
2214 /* assert(xhv_array != 0) */
2216 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2217 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2218 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2219 if (HeHASH(entry) != hash) /* strings can't be equal */
2221 if (HeKLEN(entry) != len)
2223 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2225 if (HeKFLAGS(entry) != flags_masked)
2232 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2233 HeVAL(entry) = Nullsv;
2234 HeNEXT(entry) = *oentry;
2236 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2237 if (i) { /* initial entry? */
2238 xhv->xhv_fill++; /* HvFILL(hv)++ */
2239 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2244 ++HeVAL(entry); /* use value slot as REFCNT */
2245 UNLOCK_STRTAB_MUTEX;
2247 if (flags & HVhek_FREEKEY)
2250 return HeKEY_hek(entry);
2255 =for apidoc hv_assert
2257 Check that a hash is in an internally consistent state.
2263 Perl_hv_assert(pTHX_ HV *hv)
2267 int placeholders = 0;
2270 I32 riter = HvRITER(hv);
2271 HE *eiter = HvEITER(hv);
2273 (void)hv_iterinit(hv);
2275 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2276 /* sanity check the values */
2277 if (HeVAL(entry) == &PL_sv_placeholder) {
2282 /* sanity check the keys */
2283 if (HeSVKEY(entry)) {
2284 /* Don't know what to check on SV keys. */
2285 } else if (HeKUTF8(entry)) {
2287 if (HeKWASUTF8(entry)) {
2288 PerlIO_printf(Perl_debug_log,
2289 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2290 (int) HeKLEN(entry), HeKEY(entry));
2293 } else if (HeKWASUTF8(entry)) {
2297 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2298 if (HvUSEDKEYS(hv) != real) {
2299 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2300 (int) real, (int) HvUSEDKEYS(hv));
2303 if (HvPLACEHOLDERS(hv) != placeholders) {
2304 PerlIO_printf(Perl_debug_log,
2305 "Count %d placeholder(s), but hash reports %d\n",
2306 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2310 if (withflags && ! HvHASKFLAGS(hv)) {
2311 PerlIO_printf(Perl_debug_log,
2312 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2319 HvRITER(hv) = riter; /* Restore hash iterator state */
2320 HvEITER(hv) = eiter;