3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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)
83 int flags_masked = flags & HVhek_MASK;
87 New(54, k, HEK_BASESIZE + len + 2, char);
89 Copy(str, HEK_KEY(hek), len, char);
90 HEK_KEY(hek)[len] = 0;
93 HEK_FLAGS(hek) = (unsigned char)flags_masked;
95 if (flags & HVhek_FREEKEY)
100 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
104 Perl_free_tied_hv_pool(pTHX)
107 HE *he = PL_hv_fetch_ent_mh;
109 Safefree(HeKEY_hek(he));
114 PL_hv_fetch_ent_mh = Nullhe;
117 #if defined(USE_ITHREADS)
119 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
125 /* look for it in the table first */
126 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
130 /* create anew and remember what it is */
132 ptr_table_store(PL_ptr_table, e, ret);
134 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
135 if (HeKLEN(e) == HEf_SVKEY) {
137 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
138 HeKEY_hek(ret) = (HEK*)k;
139 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
142 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
145 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
147 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
150 #endif /* USE_ITHREADS */
153 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
156 SV *sv = sv_newmortal(), *esv = sv_newmortal();
157 if (!(flags & HVhek_FREEKEY)) {
158 sv_setpvn(sv, key, klen);
161 /* Need to free saved eventually assign to mortal SV */
162 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
163 sv_usepvn(sv, (char *) key, klen);
165 if (flags & HVhek_UTF8) {
168 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
169 Perl_croak(aTHX_ SvPVX(esv), sv);
172 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
175 #define HV_FETCH_ISSTORE 0x01
176 #define HV_FETCH_ISEXISTS 0x02
177 #define HV_FETCH_LVALUE 0x04
178 #define HV_FETCH_JUST_SV 0x08
183 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
184 the length of the key. The C<hash> parameter is the precomputed hash
185 value; if it is zero then Perl will compute it. The return value will be
186 NULL if the operation failed or if the value did not need to be actually
187 stored within the hash (as in the case of tied hashes). Otherwise it can
188 be dereferenced to get the original C<SV*>. Note that the caller is
189 responsible for suitably incrementing the reference count of C<val> before
190 the call, and decrementing it if the function returned NULL. Effectively
191 a successful hv_store takes ownership of one reference to C<val>. This is
192 usually what you want; a newly created SV has a reference count of one, so
193 if all your code does is create SVs then store them in a hash, hv_store
194 will own the only reference to the new SV, and your code doesn't need to do
195 anything further to tidy up. hv_store is not implemented as a call to
196 hv_store_ent, and does not create a temporary SV for the key, so if your
197 key data is not already in SV form then use hv_store in preference to
200 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
201 information on how to use this function on tied hashes.
207 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
220 hek = hv_fetch_common (hv, NULL, key, klen, flags,
221 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, 0);
222 return hek ? &HeVAL(hek) : NULL;
226 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
227 register U32 hash, int flags)
229 HE *hek = hv_fetch_common (hv, NULL, key, klen, flags,
230 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
231 return hek ? &HeVAL(hek) : NULL;
235 =for apidoc hv_store_ent
237 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
238 parameter is the precomputed hash value; if it is zero then Perl will
239 compute it. The return value is the new hash entry so created. It will be
240 NULL if the operation failed or if the value did not need to be actually
241 stored within the hash (as in the case of tied hashes). Otherwise the
242 contents of the return value can be accessed using the C<He?> macros
243 described here. Note that the caller is responsible for suitably
244 incrementing the reference count of C<val> before the call, and
245 decrementing it if the function returned NULL. Effectively a successful
246 hv_store_ent takes ownership of one reference to C<val>. This is
247 usually what you want; a newly created SV has a reference count of one, so
248 if all your code does is create SVs then store them in a hash, hv_store
249 will own the only reference to the new SV, and your code doesn't need to do
250 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
251 unlike C<val> it does not take ownership of it, so maintaining the correct
252 reference count on C<key> is entirely the caller's responsibility. hv_store
253 is not implemented as a call to hv_store_ent, and does not create a temporary
254 SV for the key, so if your key data is not already in SV form then use
255 hv_store in preference to hv_store_ent.
257 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
258 information on how to use this function on tied hashes.
264 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
266 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash);
270 =for apidoc hv_exists
272 Returns a boolean indicating whether the specified hash key exists. The
273 C<klen> is the length of the key.
279 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
291 return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0)
298 Returns the SV which corresponds to the specified key in the hash. The
299 C<klen> is the length of the key. If C<lval> is set then the fetch will be
300 part of a store. Check that the return value is non-null before
301 dereferencing it to an C<SV*>.
303 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
304 information on how to use this function on tied hashes.
310 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
323 hek = hv_fetch_common (hv, NULL, key, klen, flags,
324 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0),
326 return hek ? &HeVAL(hek) : NULL;
330 =for apidoc hv_exists_ent
332 Returns a boolean indicating whether the specified hash key exists. C<hash>
333 can be a valid precomputed hash value, or 0 to ask for it to be
340 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
342 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash)
346 /* returns an HE * structure with the all fields set */
347 /* note that hent_val will be a mortal sv for MAGICAL hashes */
349 =for apidoc hv_fetch_ent
351 Returns the hash entry which corresponds to the specified key in the hash.
352 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
353 if you want the function to compute it. IF C<lval> is set then the fetch
354 will be part of a store. Make sure the return value is non-null before
355 accessing it. The return value when C<tb> is a tied hash is a pointer to a
356 static location, so be sure to make a copy of the structure if you need to
359 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
360 information on how to use this function on tied hashes.
366 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
368 return hv_fetch_common(hv, keysv, NULL, 0, 0,
369 (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash);
373 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
374 int flags, int action, SV *val, register U32 hash)
388 if (flags & HVhek_FREEKEY)
390 key = SvPV(keysv, klen);
392 is_utf8 = (SvUTF8(keysv) != 0);
394 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
397 xhv = (XPVHV*)SvANY(hv);
399 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS)))
401 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
404 /* XXX should be able to skimp on the HE/HEK here when
405 HV_FETCH_JUST_SV is true. */
408 keysv = newSVpvn(key, klen);
413 keysv = newSVsv(keysv);
415 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
417 /* grab a fake HE/HEK pair from the pool or make a new one */
418 entry = PL_hv_fetch_ent_mh;
420 PL_hv_fetch_ent_mh = HeNEXT(entry);
424 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
425 HeKEY_hek(entry) = (HEK*)k;
427 HeNEXT(entry) = Nullhe;
428 HeSVKEY_set(entry, keysv);
430 sv_upgrade(sv, SVt_PVLV);
432 /* so we can free entry when freeing sv */
433 LvTARG(sv) = (SV*)entry;
435 /* XXX remove at some point? */
436 if (flags & HVhek_FREEKEY)
441 #ifdef ENV_IS_CASELESS
442 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
444 for (i = 0; i < klen; ++i)
445 if (isLOWER(key[i])) {
446 /* Would be nice if we had a routine to do the
447 copy and upercase in a single pass through. */
448 char *nkey = strupr(savepvn(key,klen));
449 /* Note that this fetch is for nkey (the uppercased
450 key) whereas the store is for key (the original) */
451 entry = hv_fetch_common(hv, Nullsv, nkey, klen,
452 HVhek_FREEKEY, /* free nkey */
453 0 /* non-LVAL fetch */,
454 Nullsv /* no value */,
455 0 /* compute hash */);
456 if (!entry && (action & HV_FETCH_LVALUE)) {
457 /* This call will free key if necessary.
458 Do it this way to encourage compiler to tail
460 entry = hv_fetch_common(hv, keysv, key, klen,
461 flags, HV_FETCH_ISSTORE,
464 if (flags & HVhek_FREEKEY)
472 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
473 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
475 /* I don't understand why hv_exists_ent has svret and sv,
476 whereas hv_exists only had one. */
477 svret = sv_newmortal();
480 if (keysv || is_utf8) {
482 keysv = newSVpvn(key, klen);
485 keysv = newSVsv(keysv);
487 mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
489 mg_copy((SV*)hv, sv, key, klen);
491 if (flags & HVhek_FREEKEY)
493 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
494 /* This cast somewhat evil, but I'm merely using NULL/
495 not NULL to return the boolean exists.
496 And I know hv is not NULL. */
497 return SvTRUE(svret) ? (HE *)hv : NULL;
499 #ifdef ENV_IS_CASELESS
500 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
501 /* XXX This code isn't UTF8 clean. */
502 const char *keysave = key;
503 /* Will need to free this, so set FREEKEY flag. */
504 key = savepvn(key,klen);
505 key = (const char*)strupr((char*)key);
509 if (flags & HVhek_FREEKEY) {
512 flags |= HVhek_FREEKEY;
516 else if (action & HV_FETCH_ISSTORE) {
519 hv_magic_check (hv, &needs_copy, &needs_store);
521 bool save_taint = PL_tainted;
522 if (keysv || is_utf8) {
524 keysv = newSVpvn(key, klen);
528 PL_tainted = SvTAINTED(keysv);
529 keysv = sv_2mortal(newSVsv(keysv));
530 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
532 mg_copy((SV*)hv, val, key, klen);
535 TAINT_IF(save_taint);
536 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
537 if (flags & HVhek_FREEKEY)
541 #ifdef ENV_IS_CASELESS
542 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
543 /* XXX This code isn't UTF8 clean. */
544 const char *keysave = key;
545 /* Will need to free this, so set FREEKEY flag. */
546 key = savepvn(key,klen);
547 key = (const char*)strupr((char*)key);
551 if (flags & HVhek_FREEKEY) {
554 flags |= HVhek_FREEKEY;
561 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
562 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
563 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
564 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
567 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
568 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
570 #ifdef DYNAMIC_ENV_FETCH
571 else if (action & HV_FETCH_ISEXISTS) {
572 /* for an %ENV exists, if we do an insert it's by a recursive
573 store call, so avoid creating HvARRAY(hv) right now. */
577 /* XXX remove at some point? */
578 if (flags & HVhek_FREEKEY)
586 const char *keysave = key;
587 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
591 flags &= ~HVhek_UTF8;
592 if (key != keysave) {
593 if (flags & HVhek_FREEKEY)
595 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
600 PERL_HASH_INTERNAL(hash, key, klen);
601 /* We don't have a pointer to the hv, so we have to replicate the
602 flag into every HEK, so that hv_iterkeysv can see it. */
603 /* And yes, you do need this even though you are not "storing" because
604 you can flip the flags below if doing an lval lookup. (And that
605 was put in to give the semantics Andreas was expecting.) */
606 flags |= HVhek_REHASH;
608 if (keysv && (SvIsCOW_shared_hash(keysv))) {
611 PERL_HASH(hash, key, klen);
615 masked_flags = (flags & HVhek_MASK);
618 #ifdef DYNAMIC_ENV_FETCH
619 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
623 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
624 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
626 for (; entry; ++n_links, entry = HeNEXT(entry)) {
627 if (HeHASH(entry) != hash) /* strings can't be equal */
629 if (HeKLEN(entry) != (I32)klen)
631 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
633 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
636 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
637 if (HeKFLAGS(entry) != masked_flags) {
638 /* We match if HVhek_UTF8 bit in our flags and hash key's
639 match. But if entry was set previously with HVhek_WASUTF8
640 and key now doesn't (or vice versa) then we should change
641 the key's flag, as this is assignment. */
642 if (HvSHAREKEYS(hv)) {
643 /* Need to swap the key we have for a key with the flags we
644 need. As keys are shared we can't just write to the
645 flag, so we share the new one, unshare the old one. */
646 HEK *new_hek = share_hek_flags(key, klen, hash,
648 unshare_hek (HeKEY_hek(entry));
649 HeKEY_hek(entry) = new_hek;
652 HeKFLAGS(entry) = masked_flags;
653 if (masked_flags & HVhek_ENABLEHVKFLAGS)
656 if (HeVAL(entry) == &PL_sv_placeholder) {
657 /* yes, can store into placeholder slot */
658 if (action & HV_FETCH_LVALUE) {
660 /* This preserves behaviour with the old hv_fetch
661 implementation which at this point would bail out
662 with a break; (at "if we find a placeholder, we
663 pretend we haven't found anything")
665 That break mean that if a placeholder were found, it
666 caused a call into hv_store, which in turn would
667 check magic, and if there is no magic end up pretty
668 much back at this point (in hv_store's code). */
671 /* LVAL fetch which actaully needs a store. */
673 xhv->xhv_placeholders--;
676 if (val != &PL_sv_placeholder)
677 xhv->xhv_placeholders--;
680 } else if (action & HV_FETCH_ISSTORE) {
681 SvREFCNT_dec(HeVAL(entry));
684 } else if (HeVAL(entry) == &PL_sv_placeholder) {
685 /* if we find a placeholder, we pretend we haven't found
689 if (flags & HVhek_FREEKEY)
693 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
694 if (!(action & HV_FETCH_ISSTORE)
695 && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
697 char *env = PerlEnv_ENVgetenv_len(key,&len);
699 sv = newSVpvn(env,len);
701 return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv,
707 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
708 S_hv_notallowed(aTHX_ flags, key, klen,
709 "access disallowed key '%"SVf"' in"
712 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
713 /* Not doing some form of store, so return failure. */
714 if (flags & HVhek_FREEKEY)
718 if (action & HV_FETCH_LVALUE) {
721 /* At this point the old hv_fetch code would call to hv_store,
722 which in turn might do some tied magic. So we need to make that
723 magic check happen. */
724 /* gonna assign to this, so it better be there */
725 return hv_fetch_common(hv, keysv, key, klen, flags,
726 HV_FETCH_ISSTORE, val, hash);
727 /* XXX Surely that could leak if the fetch-was-store fails?
728 Just like the hv_fetch. */
732 /* Welcome to hv_store... */
734 if (!xhv->xhv_array) {
735 /* Not sure if we can get here. I think the only case of oentry being
736 NULL is for %ENV with dynamic env fetch. But that should disappear
737 with magic in the previous code. */
738 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
739 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
743 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
746 /* share_hek_flags will do the free for us. This might be considered
749 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
750 else /* gotta do the real thing */
751 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
753 HeNEXT(entry) = *oentry;
756 if (val == &PL_sv_placeholder)
757 xhv->xhv_placeholders++;
758 if (masked_flags & HVhek_ENABLEHVKFLAGS)
761 xhv->xhv_keys++; /* HvKEYS(hv)++ */
762 if (!n_links) { /* initial entry? */
763 xhv->xhv_fill++; /* HvFILL(hv)++ */
764 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
765 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
766 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
767 splits on a rehashed hash, as we're not going to split it again,
768 and if someone is lucky (evil) enough to get all the keys in one
769 list they could exhaust our memory as we repeatedly double the
770 number of buckets on every entry. Linear search feels a less worse
779 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
781 MAGIC *mg = SvMAGIC(hv);
785 if (isUPPER(mg->mg_type)) {
787 switch (mg->mg_type) {
788 case PERL_MAGIC_tied:
790 *needs_store = FALSE;
793 mg = mg->mg_moremagic;
798 =for apidoc hv_scalar
800 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
806 Perl_hv_scalar(pTHX_ HV *hv)
811 if ((SvRMAGICAL(hv) && (mg = mg_find((SV*)hv, PERL_MAGIC_tied)))) {
812 sv = magic_scalarpack(hv, mg);
818 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
819 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
827 =for apidoc hv_delete
829 Deletes a key/value pair in the hash. The value SV is removed from the
830 hash and returned to the caller. The C<klen> is the length of the key.
831 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
838 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
845 k_flags |= HVhek_UTF8;
849 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
853 =for apidoc hv_delete_ent
855 Deletes a key/value pair in the hash. The value SV is removed from the
856 hash and returned to the caller. The C<flags> value will normally be zero;
857 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
858 precomputed hash value, or 0 to ask for it to be computed.
864 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
866 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
870 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
871 int k_flags, I32 d_flags, U32 hash)
876 register HE **oentry;
885 if (k_flags & HVhek_FREEKEY)
887 key = SvPV(keysv, klen);
889 is_utf8 = (SvUTF8(keysv) != 0);
891 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
894 if (SvRMAGICAL(hv)) {
897 hv_magic_check (hv, &needs_copy, &needs_store);
900 entry = hv_fetch_common(hv, keysv, key, klen,
901 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
903 sv = entry ? HeVAL(entry) : NULL;
909 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
910 /* No longer an element */
911 sv_unmagic(sv, PERL_MAGIC_tiedelem);
914 return Nullsv; /* element cannot be deleted */
916 #ifdef ENV_IS_CASELESS
917 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
918 /* XXX This code isn't UTF8 clean. */
919 keysv = sv_2mortal(newSVpvn(key,klen));
920 if (k_flags & HVhek_FREEKEY) {
923 key = strupr(SvPVX(keysv));
932 xhv = (XPVHV*)SvANY(hv);
933 if (!xhv->xhv_array /* !HvARRAY(hv) */)
937 const char *keysave = key;
938 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
941 k_flags |= HVhek_UTF8;
943 k_flags &= ~HVhek_UTF8;
944 if (key != keysave) {
945 if (k_flags & HVhek_FREEKEY) {
946 /* This shouldn't happen if our caller does what we expect,
947 but strictly the API allows it. */
950 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
952 HvHASKFLAGS_on((SV*)hv);
956 PERL_HASH_INTERNAL(hash, key, klen);
958 if (keysv && (SvIsCOW_shared_hash(keysv))) {
961 PERL_HASH(hash, key, klen);
965 masked_flags = (k_flags & HVhek_MASK);
967 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
968 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
971 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
972 if (HeHASH(entry) != hash) /* strings can't be equal */
974 if (HeKLEN(entry) != (I32)klen)
976 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
978 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
981 /* if placeholder is here, it's already been deleted.... */
982 if (HeVAL(entry) == &PL_sv_placeholder)
984 if (k_flags & HVhek_FREEKEY)
988 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
989 S_hv_notallowed(aTHX_ k_flags, key, klen,
990 "delete readonly key '%"SVf"' from"
993 if (k_flags & HVhek_FREEKEY)
996 if (d_flags & G_DISCARD)
999 sv = sv_2mortal(HeVAL(entry));
1000 HeVAL(entry) = &PL_sv_placeholder;
1004 * If a restricted hash, rather than really deleting the entry, put
1005 * a placeholder there. This marks the key as being "approved", so
1006 * we can still access via not-really-existing key without raising
1009 if (SvREADONLY(hv)) {
1010 SvREFCNT_dec(HeVAL(entry));
1011 HeVAL(entry) = &PL_sv_placeholder;
1012 /* We'll be saving this slot, so the number of allocated keys
1013 * doesn't go down, but the number placeholders goes up */
1014 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1016 *oentry = HeNEXT(entry);
1018 xhv->xhv_fill--; /* HvFILL(hv)-- */
1019 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1022 hv_free_ent(hv, entry);
1023 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1024 if (xhv->xhv_keys == 0)
1025 HvHASKFLAGS_off(hv);
1029 if (SvREADONLY(hv)) {
1030 S_hv_notallowed(aTHX_ k_flags, key, klen,
1031 "delete disallowed key '%"SVf"' from"
1035 if (k_flags & HVhek_FREEKEY)
1041 S_hsplit(pTHX_ HV *hv)
1043 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1044 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1045 register I32 newsize = oldsize * 2;
1047 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1051 register HE **oentry;
1052 int longest_chain = 0;
1055 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1056 hv, (int) oldsize);*/
1058 if (HvPLACEHOLDERS(hv) && !SvREADONLY(hv)) {
1059 /* Can make this clear any placeholders first for non-restricted hashes,
1060 even though Storable rebuilds restricted hashes by putting in all the
1061 placeholders (first) before turning on the readonly flag, because
1062 Storable always pre-splits the hash. */
1063 hv_clear_placeholders(hv);
1067 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1068 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1074 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1079 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1080 if (oldsize >= 64) {
1081 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1082 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1085 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1089 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1090 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1091 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1094 for (i=0; i<oldsize; i++,aep++) {
1095 int left_length = 0;
1096 int right_length = 0;
1098 if (!*aep) /* non-existent */
1101 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1102 if ((HeHASH(entry) & newsize) != (U32)i) {
1103 *oentry = HeNEXT(entry);
1104 HeNEXT(entry) = *bep;
1106 xhv->xhv_fill++; /* HvFILL(hv)++ */
1112 oentry = &HeNEXT(entry);
1116 if (!*aep) /* everything moved */
1117 xhv->xhv_fill--; /* HvFILL(hv)-- */
1118 /* I think we don't actually need to keep track of the longest length,
1119 merely flag if anything is too long. But for the moment while
1120 developing this code I'll track it. */
1121 if (left_length > longest_chain)
1122 longest_chain = left_length;
1123 if (right_length > longest_chain)
1124 longest_chain = right_length;
1128 /* Pick your policy for "hashing isn't working" here: */
1129 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1134 if (hv == PL_strtab) {
1135 /* Urg. Someone is doing something nasty to the string table.
1140 /* Awooga. Awooga. Pathological data. */
1141 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1142 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1145 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1146 was_shared = HvSHAREKEYS(hv);
1149 HvSHAREKEYS_off(hv);
1152 aep = (HE **) xhv->xhv_array;
1154 for (i=0; i<newsize; i++,aep++) {
1157 /* We're going to trash this HE's next pointer when we chain it
1158 into the new hash below, so store where we go next. */
1159 HE *next = HeNEXT(entry);
1163 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1168 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1169 hash, HeKFLAGS(entry));
1170 unshare_hek (HeKEY_hek(entry));
1171 HeKEY_hek(entry) = new_hek;
1173 /* Not shared, so simply write the new hash in. */
1174 HeHASH(entry) = hash;
1176 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1177 HEK_REHASH_on(HeKEY_hek(entry));
1178 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1180 /* Copy oentry to the correct new chain. */
1181 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1183 xhv->xhv_fill++; /* HvFILL(hv)++ */
1184 HeNEXT(entry) = *bep;
1190 Safefree (xhv->xhv_array);
1191 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1195 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1197 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1198 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1199 register I32 newsize;
1205 register HE **oentry;
1207 newsize = (I32) newmax; /* possible truncation here */
1208 if (newsize != newmax || newmax <= oldsize)
1210 while ((newsize & (1 + ~newsize)) != newsize) {
1211 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1213 if (newsize < newmax)
1215 if (newsize < newmax)
1216 return; /* overflow detection */
1218 a = xhv->xhv_array; /* HvARRAY(hv) */
1221 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1222 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1228 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1233 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1234 if (oldsize >= 64) {
1235 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1236 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1239 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1242 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1245 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1247 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1248 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1249 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1253 for (i=0; i<oldsize; i++,aep++) {
1254 if (!*aep) /* non-existent */
1256 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1257 if ((j = (HeHASH(entry) & newsize)) != i) {
1259 *oentry = HeNEXT(entry);
1260 if (!(HeNEXT(entry) = aep[j]))
1261 xhv->xhv_fill++; /* HvFILL(hv)++ */
1266 oentry = &HeNEXT(entry);
1268 if (!*aep) /* everything moved */
1269 xhv->xhv_fill--; /* HvFILL(hv)-- */
1276 Creates a new HV. The reference count is set to 1.
1285 register XPVHV* xhv;
1287 hv = (HV*)NEWSV(502,0);
1288 sv_upgrade((SV *)hv, SVt_PVHV);
1289 xhv = (XPVHV*)SvANY(hv);
1292 #ifndef NODEFAULT_SHAREKEYS
1293 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1296 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1297 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1298 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1299 (void)hv_iterinit(hv); /* so each() will start off right */
1304 Perl_newHVhv(pTHX_ HV *ohv)
1307 STRLEN hv_max, hv_fill;
1309 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1311 hv_max = HvMAX(ohv);
1313 if (!SvMAGICAL((SV *)ohv)) {
1314 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1316 bool shared = !!HvSHAREKEYS(ohv);
1317 HE **ents, **oents = (HE **)HvARRAY(ohv);
1319 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1322 /* In each bucket... */
1323 for (i = 0; i <= hv_max; i++) {
1324 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1331 /* Copy the linked list of entries. */
1332 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1333 U32 hash = HeHASH(oent);
1334 char *key = HeKEY(oent);
1335 STRLEN len = HeKLEN(oent);
1336 int flags = HeKFLAGS(oent);
1339 HeVAL(ent) = newSVsv(HeVAL(oent));
1341 = shared ? share_hek_flags(key, len, hash, flags)
1342 : save_hek_flags(key, len, hash, flags);
1353 HvFILL(hv) = hv_fill;
1354 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1358 /* Iterate over ohv, copying keys and values one at a time. */
1360 I32 riter = HvRITER(ohv);
1361 HE *eiter = HvEITER(ohv);
1363 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1364 while (hv_max && hv_max + 1 >= hv_fill * 2)
1365 hv_max = hv_max / 2;
1369 while ((entry = hv_iternext_flags(ohv, 0))) {
1370 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1371 newSVsv(HeVAL(entry)), HeHASH(entry),
1374 HvRITER(ohv) = riter;
1375 HvEITER(ohv) = eiter;
1382 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1389 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1390 PL_sub_generation++; /* may be deletion of method from stash */
1392 if (HeKLEN(entry) == HEf_SVKEY) {
1393 SvREFCNT_dec(HeKEY_sv(entry));
1394 Safefree(HeKEY_hek(entry));
1396 else if (HvSHAREKEYS(hv))
1397 unshare_hek(HeKEY_hek(entry));
1399 Safefree(HeKEY_hek(entry));
1404 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1408 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1409 PL_sub_generation++; /* may be deletion of method from stash */
1410 sv_2mortal(HeVAL(entry)); /* free between statements */
1411 if (HeKLEN(entry) == HEf_SVKEY) {
1412 sv_2mortal(HeKEY_sv(entry));
1413 Safefree(HeKEY_hek(entry));
1415 else if (HvSHAREKEYS(hv))
1416 unshare_hek(HeKEY_hek(entry));
1418 Safefree(HeKEY_hek(entry));
1423 =for apidoc hv_clear
1425 Clears a hash, making it empty.
1431 Perl_hv_clear(pTHX_ HV *hv)
1433 register XPVHV* xhv;
1437 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1439 xhv = (XPVHV*)SvANY(hv);
1441 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1442 /* restricted hash: convert all keys to placeholders */
1445 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1446 entry = ((HE**)xhv->xhv_array)[i];
1447 for (; entry; entry = HeNEXT(entry)) {
1448 /* not already placeholder */
1449 if (HeVAL(entry) != &PL_sv_placeholder) {
1450 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1451 SV* keysv = hv_iterkeysv(entry);
1453 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1456 SvREFCNT_dec(HeVAL(entry));
1457 HeVAL(entry) = &PL_sv_placeholder;
1458 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1466 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1467 if (xhv->xhv_array /* HvARRAY(hv) */)
1468 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1469 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1474 HvHASKFLAGS_off(hv);
1481 =for apidoc hv_clear_placeholders
1483 Clears any placeholders from a hash. If a restricted hash has any of its keys
1484 marked as readonly and the key is subsequently deleted, the key is not actually
1485 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1486 it so it will be ignored by future operations such as iterating over the hash,
1487 but will still allow the hash to have a value reaasigned to the key at some
1488 future point. This function clears any such placeholder keys from the hash.
1489 See Hash::Util::lock_keys() for an example of its use.
1495 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1497 I32 items = (I32)HvPLACEHOLDERS(hv);
1504 /* Loop down the linked list heads */
1506 HE **oentry = &(HvARRAY(hv))[i];
1507 HE *entry = *oentry;
1512 for (; entry; entry = *oentry) {
1513 if (HeVAL(entry) == &PL_sv_placeholder) {
1514 *oentry = HeNEXT(entry);
1515 if (first && !*oentry)
1516 HvFILL(hv)--; /* This linked list is now empty. */
1520 hv_free_ent(hv, entry);
1524 HvTOTALKEYS(hv) -= HvPLACEHOLDERS(hv);
1525 if (HvKEYS(hv) == 0)
1526 HvHASKFLAGS_off(hv);
1527 HvPLACEHOLDERS(hv) = 0;
1531 oentry = &HeNEXT(entry);
1536 /* You can't get here, hence assertion should always fail. */
1537 assert (items == 0);
1542 S_hfreeentries(pTHX_ HV *hv)
1544 register HE **array;
1546 register HE *oentry = Null(HE*);
1557 array = HvARRAY(hv);
1558 /* make everyone else think the array is empty, so that the destructors
1559 * called for freed entries can't recusively mess with us */
1560 HvARRAY(hv) = Null(HE**);
1562 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1568 entry = HeNEXT(entry);
1569 hv_free_ent(hv, oentry);
1574 entry = array[riter];
1577 HvARRAY(hv) = array;
1578 (void)hv_iterinit(hv);
1582 =for apidoc hv_undef
1590 Perl_hv_undef(pTHX_ HV *hv)
1592 register XPVHV* xhv;
1595 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1596 xhv = (XPVHV*)SvANY(hv);
1598 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1601 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1602 Safefree(HvNAME(hv));
1605 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1606 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1607 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1614 =for apidoc hv_iterinit
1616 Prepares a starting point to traverse a hash table. Returns the number of
1617 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1618 currently only meaningful for hashes without tie magic.
1620 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1621 hash buckets that happen to be in use. If you still need that esoteric
1622 value, you can get it through the macro C<HvFILL(tb)>.
1629 Perl_hv_iterinit(pTHX_ HV *hv)
1631 register XPVHV* xhv;
1635 Perl_croak(aTHX_ "Bad hash");
1636 xhv = (XPVHV*)SvANY(hv);
1637 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1638 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1640 hv_free_ent(hv, entry);
1642 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1643 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1644 /* used to be xhv->xhv_fill before 5.004_65 */
1645 return XHvTOTALKEYS(xhv);
1648 =for apidoc hv_iternext
1650 Returns entries from a hash iterator. See C<hv_iterinit>.
1652 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1653 iterator currently points to, without losing your place or invalidating your
1654 iterator. Note that in this case the current entry is deleted from the hash
1655 with your iterator holding the last reference to it. Your iterator is flagged
1656 to free the entry on the next call to C<hv_iternext>, so you must not discard
1657 your iterator immediately else the entry will leak - call C<hv_iternext> to
1658 trigger the resource deallocation.
1664 Perl_hv_iternext(pTHX_ HV *hv)
1666 return hv_iternext_flags(hv, 0);
1670 =for apidoc hv_iternext_flags
1672 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1673 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1674 set the placeholders keys (for restricted hashes) will be returned in addition
1675 to normal keys. By default placeholders are automatically skipped over.
1676 Currently a placeholder is implemented with a value that is
1677 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1678 restricted hashes may change, and the implementation currently is
1679 insufficiently abstracted for any change to be tidy.
1685 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1687 register XPVHV* xhv;
1693 Perl_croak(aTHX_ "Bad hash");
1694 xhv = (XPVHV*)SvANY(hv);
1695 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1697 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1698 SV *key = sv_newmortal();
1700 sv_setsv(key, HeSVKEY_force(entry));
1701 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1707 /* one HE per MAGICAL hash */
1708 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1710 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1712 HeKEY_hek(entry) = hek;
1713 HeKLEN(entry) = HEf_SVKEY;
1715 magic_nextpack((SV*) hv,mg,key);
1717 /* force key to stay around until next time */
1718 HeSVKEY_set(entry, SvREFCNT_inc(key));
1719 return entry; /* beware, hent_val is not set */
1722 SvREFCNT_dec(HeVAL(entry));
1723 Safefree(HeKEY_hek(entry));
1725 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1728 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1729 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1733 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1734 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1735 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1737 /* At start of hash, entry is NULL. */
1740 entry = HeNEXT(entry);
1741 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1743 * Skip past any placeholders -- don't want to include them in
1746 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1747 entry = HeNEXT(entry);
1752 /* OK. Come to the end of the current list. Grab the next one. */
1754 xhv->xhv_riter++; /* HvRITER(hv)++ */
1755 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1756 /* There is no next one. End of the hash. */
1757 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1760 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1761 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1763 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1764 /* If we have an entry, but it's a placeholder, don't count it.
1766 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1767 entry = HeNEXT(entry);
1769 /* Will loop again if this linked list starts NULL
1770 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1771 or if we run through it and find only placeholders. */
1774 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1776 hv_free_ent(hv, oldentry);
1779 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1780 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1782 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1787 =for apidoc hv_iterkey
1789 Returns the key from the current position of the hash iterator. See
1796 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1798 if (HeKLEN(entry) == HEf_SVKEY) {
1800 char *p = SvPV(HeKEY_sv(entry), len);
1805 *retlen = HeKLEN(entry);
1806 return HeKEY(entry);
1810 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1812 =for apidoc hv_iterkeysv
1814 Returns the key as an C<SV*> from the current position of the hash
1815 iterator. The return value will always be a mortal copy of the key. Also
1822 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1824 if (HeKLEN(entry) != HEf_SVKEY) {
1825 HEK *hek = HeKEY_hek(entry);
1826 int flags = HEK_FLAGS(hek);
1829 if (flags & HVhek_WASUTF8) {
1831 Andreas would like keys he put in as utf8 to come back as utf8
1833 STRLEN utf8_len = HEK_LEN(hek);
1834 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1836 sv = newSVpvn ((char*)as_utf8, utf8_len);
1838 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1839 } else if (flags & HVhek_REHASH) {
1840 /* We don't have a pointer to the hv, so we have to replicate the
1841 flag into every HEK. This hv is using custom a hasing
1842 algorithm. Hence we can't return a shared string scalar, as
1843 that would contain the (wrong) hash value, and might get passed
1844 into an hv routine with a regular hash */
1846 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1850 sv = newSVpvn_share(HEK_KEY(hek),
1851 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1854 return sv_2mortal(sv);
1856 return sv_mortalcopy(HeKEY_sv(entry));
1860 =for apidoc hv_iterval
1862 Returns the value from the current position of the hash iterator. See
1869 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1871 if (SvRMAGICAL(hv)) {
1872 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1873 SV* sv = sv_newmortal();
1874 if (HeKLEN(entry) == HEf_SVKEY)
1875 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1876 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1880 return HeVAL(entry);
1884 =for apidoc hv_iternextsv
1886 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1893 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1896 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1898 *key = hv_iterkey(he, retlen);
1899 return hv_iterval(hv, he);
1903 =for apidoc hv_magic
1905 Adds magic to a hash. See C<sv_magic>.
1911 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1913 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1916 #if 0 /* use the macro from hv.h instead */
1919 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1921 return HEK_KEY(share_hek(sv, len, hash));
1926 /* possibly free a shared string if no one has access to it
1927 * len and hash must both be valid for str.
1930 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1932 unshare_hek_or_pvn (NULL, str, len, hash);
1937 Perl_unshare_hek(pTHX_ HEK *hek)
1939 unshare_hek_or_pvn(hek, NULL, 0, 0);
1942 /* possibly free a shared string if no one has access to it
1943 hek if non-NULL takes priority over the other 3, else str, len and hash
1944 are used. If so, len and hash must both be valid for str.
1947 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1949 register XPVHV* xhv;
1951 register HE **oentry;
1954 bool is_utf8 = FALSE;
1956 const char *save = str;
1959 hash = HEK_HASH(hek);
1960 } else if (len < 0) {
1961 STRLEN tmplen = -len;
1963 /* See the note in hv_fetch(). --jhi */
1964 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1967 k_flags = HVhek_UTF8;
1969 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1972 /* what follows is the moral equivalent of:
1973 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1974 if (--*Svp == Nullsv)
1975 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1977 xhv = (XPVHV*)SvANY(PL_strtab);
1978 /* assert(xhv_array != 0) */
1980 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1981 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1983 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1984 if (HeKEY_hek(entry) != hek)
1990 int flags_masked = k_flags & HVhek_MASK;
1991 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1992 if (HeHASH(entry) != hash) /* strings can't be equal */
1994 if (HeKLEN(entry) != len)
1996 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1998 if (HeKFLAGS(entry) != flags_masked)
2006 if (--HeVAL(entry) == Nullsv) {
2007 *oentry = HeNEXT(entry);
2009 xhv->xhv_fill--; /* HvFILL(hv)-- */
2010 Safefree(HeKEY_hek(entry));
2012 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2016 UNLOCK_STRTAB_MUTEX;
2017 if (!found && ckWARN_d(WARN_INTERNAL))
2018 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2019 "Attempt to free non-existent shared string '%s'%s"
2021 hek ? HEK_KEY(hek) : str,
2022 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2023 if (k_flags & HVhek_FREEKEY)
2027 /* get a (constant) string ptr from the global string table
2028 * string will get added if it is not already there.
2029 * len and hash must both be valid for str.
2032 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2034 bool is_utf8 = FALSE;
2036 const char *save = str;
2039 STRLEN tmplen = -len;
2041 /* See the note in hv_fetch(). --jhi */
2042 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2044 /* If we were able to downgrade here, then than means that we were passed
2045 in a key which only had chars 0-255, but was utf8 encoded. */
2048 /* If we found we were able to downgrade the string to bytes, then
2049 we should flag that it needs upgrading on keys or each. Also flag
2050 that we need share_hek_flags to free the string. */
2052 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2055 return share_hek_flags (str, len, hash, flags);
2059 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2061 register XPVHV* xhv;
2063 register HE **oentry;
2066 int flags_masked = flags & HVhek_MASK;
2068 /* what follows is the moral equivalent of:
2070 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2071 hv_store(PL_strtab, str, len, Nullsv, hash);
2073 Can't rehash the shared string table, so not sure if it's worth
2074 counting the number of entries in the linked list
2076 xhv = (XPVHV*)SvANY(PL_strtab);
2077 /* assert(xhv_array != 0) */
2079 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2080 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2081 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2082 if (HeHASH(entry) != hash) /* strings can't be equal */
2084 if (HeKLEN(entry) != len)
2086 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2088 if (HeKFLAGS(entry) != flags_masked)
2095 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags_masked);
2096 HeVAL(entry) = Nullsv;
2097 HeNEXT(entry) = *oentry;
2099 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2100 if (i) { /* initial entry? */
2101 xhv->xhv_fill++; /* HvFILL(hv)++ */
2102 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2107 ++HeVAL(entry); /* use value slot as REFCNT */
2108 UNLOCK_STRTAB_MUTEX;
2110 if (flags & HVhek_FREEKEY)
2113 return HeKEY_hek(entry);
2118 =for apidoc hv_assert
2120 Check that a hash is in an internally consistent state.
2126 Perl_hv_assert(pTHX_ HV *hv)
2130 int placeholders = 0;
2133 I32 riter = HvRITER(hv);
2134 HE *eiter = HvEITER(hv);
2136 (void)hv_iterinit(hv);
2138 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2139 /* sanity check the values */
2140 if (HeVAL(entry) == &PL_sv_placeholder) {
2145 /* sanity check the keys */
2146 if (HeSVKEY(entry)) {
2147 /* Don't know what to check on SV keys. */
2148 } else if (HeKUTF8(entry)) {
2150 if (HeKWASUTF8(entry)) {
2151 PerlIO_printf(Perl_debug_log,
2152 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2153 (int) HeKLEN(entry), HeKEY(entry));
2156 } else if (HeKWASUTF8(entry)) {
2160 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2161 if (HvUSEDKEYS(hv) != real) {
2162 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2163 (int) real, (int) HvUSEDKEYS(hv));
2166 if (HvPLACEHOLDERS(hv) != placeholders) {
2167 PerlIO_printf(Perl_debug_log,
2168 "Count %d placeholder(s), but hash reports %d\n",
2169 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2173 if (withflags && ! HvHASKFLAGS(hv)) {
2174 PerlIO_printf(Perl_debug_log,
2175 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2182 HvRITER(hv) = riter; /* Restore hash iterator state */
2183 HvEITER(hv) = eiter;