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);
510 if (flags & HVhek_FREEKEY) {
513 flags |= HVhek_FREEKEY;
517 else if (action & HV_FETCH_ISSTORE) {
520 hv_magic_check (hv, &needs_copy, &needs_store);
522 bool save_taint = PL_tainted;
523 if (keysv || is_utf8) {
525 keysv = newSVpvn(key, klen);
529 PL_tainted = SvTAINTED(keysv);
530 keysv = sv_2mortal(newSVsv(keysv));
531 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
533 mg_copy((SV*)hv, val, key, klen);
536 TAINT_IF(save_taint);
537 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
538 if (flags & HVhek_FREEKEY)
542 #ifdef ENV_IS_CASELESS
543 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
544 /* XXX This code isn't UTF8 clean. */
545 const char *keysave = key;
546 /* Will need to free this, so set FREEKEY flag. */
547 key = savepvn(key,klen);
548 key = (const char*)strupr((char*)key);
553 if (flags & HVhek_FREEKEY) {
556 flags |= HVhek_FREEKEY;
563 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
564 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
565 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
566 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
569 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
570 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
572 #ifdef DYNAMIC_ENV_FETCH
573 else if (action & HV_FETCH_ISEXISTS) {
574 /* for an %ENV exists, if we do an insert it's by a recursive
575 store call, so avoid creating HvARRAY(hv) right now. */
579 /* XXX remove at some point? */
580 if (flags & HVhek_FREEKEY)
588 const char *keysave = key;
589 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
593 flags &= ~HVhek_UTF8;
594 if (key != keysave) {
595 if (flags & HVhek_FREEKEY)
597 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
602 PERL_HASH_INTERNAL(hash, key, klen);
603 /* We don't have a pointer to the hv, so we have to replicate the
604 flag into every HEK, so that hv_iterkeysv can see it. */
605 /* And yes, you do need this even though you are not "storing" because
606 you can flip the flags below if doing an lval lookup. (And that
607 was put in to give the semantics Andreas was expecting.) */
608 flags |= HVhek_REHASH;
610 if (keysv && (SvIsCOW_shared_hash(keysv))) {
613 PERL_HASH(hash, key, klen);
617 masked_flags = (flags & HVhek_MASK);
620 #ifdef DYNAMIC_ENV_FETCH
621 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
625 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
626 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
628 for (; entry; ++n_links, entry = HeNEXT(entry)) {
629 if (HeHASH(entry) != hash) /* strings can't be equal */
631 if (HeKLEN(entry) != (I32)klen)
633 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
635 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
638 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
639 if (HeKFLAGS(entry) != masked_flags) {
640 /* We match if HVhek_UTF8 bit in our flags and hash key's
641 match. But if entry was set previously with HVhek_WASUTF8
642 and key now doesn't (or vice versa) then we should change
643 the key's flag, as this is assignment. */
644 if (HvSHAREKEYS(hv)) {
645 /* Need to swap the key we have for a key with the flags we
646 need. As keys are shared we can't just write to the
647 flag, so we share the new one, unshare the old one. */
648 HEK *new_hek = share_hek_flags(key, klen, hash,
650 unshare_hek (HeKEY_hek(entry));
651 HeKEY_hek(entry) = new_hek;
654 HeKFLAGS(entry) = masked_flags;
655 if (masked_flags & HVhek_ENABLEHVKFLAGS)
658 if (HeVAL(entry) == &PL_sv_placeholder) {
659 /* yes, can store into placeholder slot */
660 if (action & HV_FETCH_LVALUE) {
662 /* This preserves behaviour with the old hv_fetch
663 implementation which at this point would bail out
664 with a break; (at "if we find a placeholder, we
665 pretend we haven't found anything")
667 That break mean that if a placeholder were found, it
668 caused a call into hv_store, which in turn would
669 check magic, and if there is no magic end up pretty
670 much back at this point (in hv_store's code). */
673 /* LVAL fetch which actaully needs a store. */
675 xhv->xhv_placeholders--;
678 if (val != &PL_sv_placeholder)
679 xhv->xhv_placeholders--;
682 } else if (action & HV_FETCH_ISSTORE) {
683 SvREFCNT_dec(HeVAL(entry));
686 } else if (HeVAL(entry) == &PL_sv_placeholder) {
687 /* if we find a placeholder, we pretend we haven't found
691 if (flags & HVhek_FREEKEY)
695 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
696 if (!(action & HV_FETCH_ISSTORE)
697 && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
699 char *env = PerlEnv_ENVgetenv_len(key,&len);
701 sv = newSVpvn(env,len);
703 return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv,
709 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
710 S_hv_notallowed(aTHX_ flags, key, klen,
711 "access disallowed key '%"SVf"' in"
714 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
715 /* Not doing some form of store, so return failure. */
716 if (flags & HVhek_FREEKEY)
720 if (action & HV_FETCH_LVALUE) {
723 /* At this point the old hv_fetch code would call to hv_store,
724 which in turn might do some tied magic. So we need to make that
725 magic check happen. */
726 /* gonna assign to this, so it better be there */
727 return hv_fetch_common(hv, keysv, key, klen, flags,
728 HV_FETCH_ISSTORE, val, hash);
729 /* XXX Surely that could leak if the fetch-was-store fails?
730 Just like the hv_fetch. */
734 /* Welcome to hv_store... */
736 if (!xhv->xhv_array) {
737 /* Not sure if we can get here. I think the only case of oentry being
738 NULL is for %ENV with dynamic env fetch. But that should disappear
739 with magic in the previous code. */
740 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
741 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
745 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
748 /* share_hek_flags will do the free for us. This might be considered
751 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
752 else /* gotta do the real thing */
753 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
755 HeNEXT(entry) = *oentry;
758 if (val == &PL_sv_placeholder)
759 xhv->xhv_placeholders++;
760 if (masked_flags & HVhek_ENABLEHVKFLAGS)
763 xhv->xhv_keys++; /* HvKEYS(hv)++ */
764 if (!n_links) { /* initial entry? */
765 xhv->xhv_fill++; /* HvFILL(hv)++ */
766 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
767 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
768 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
769 splits on a rehashed hash, as we're not going to split it again,
770 and if someone is lucky (evil) enough to get all the keys in one
771 list they could exhaust our memory as we repeatedly double the
772 number of buckets on every entry. Linear search feels a less worse
781 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
783 MAGIC *mg = SvMAGIC(hv);
787 if (isUPPER(mg->mg_type)) {
789 switch (mg->mg_type) {
790 case PERL_MAGIC_tied:
792 *needs_store = FALSE;
795 mg = mg->mg_moremagic;
800 =for apidoc hv_scalar
802 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
808 Perl_hv_scalar(pTHX_ HV *hv)
813 if ((SvRMAGICAL(hv) && (mg = mg_find((SV*)hv, PERL_MAGIC_tied)))) {
814 sv = magic_scalarpack(hv, mg);
820 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
821 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
829 =for apidoc hv_delete
831 Deletes a key/value pair in the hash. The value SV is removed from the
832 hash and returned to the caller. The C<klen> is the length of the key.
833 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
840 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
847 k_flags |= HVhek_UTF8;
851 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
855 =for apidoc hv_delete_ent
857 Deletes a key/value pair in the hash. The value SV is removed from the
858 hash and returned to the caller. The C<flags> value will normally be zero;
859 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
860 precomputed hash value, or 0 to ask for it to be computed.
866 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
868 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
872 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
873 int k_flags, I32 d_flags, U32 hash)
878 register HE **oentry;
887 if (k_flags & HVhek_FREEKEY)
889 key = SvPV(keysv, klen);
891 is_utf8 = (SvUTF8(keysv) != 0);
893 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
896 if (SvRMAGICAL(hv)) {
899 hv_magic_check (hv, &needs_copy, &needs_store);
902 entry = hv_fetch_common(hv, keysv, key, klen,
903 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
905 sv = entry ? HeVAL(entry) : NULL;
911 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
912 /* No longer an element */
913 sv_unmagic(sv, PERL_MAGIC_tiedelem);
916 return Nullsv; /* element cannot be deleted */
918 #ifdef ENV_IS_CASELESS
919 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
920 /* XXX This code isn't UTF8 clean. */
921 keysv = sv_2mortal(newSVpvn(key,klen));
922 if (k_flags & HVhek_FREEKEY) {
925 key = strupr(SvPVX(keysv));
934 xhv = (XPVHV*)SvANY(hv);
935 if (!xhv->xhv_array /* !HvARRAY(hv) */)
939 const char *keysave = key;
940 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
943 k_flags |= HVhek_UTF8;
945 k_flags &= ~HVhek_UTF8;
946 if (key != keysave) {
947 if (k_flags & HVhek_FREEKEY) {
948 /* This shouldn't happen if our caller does what we expect,
949 but strictly the API allows it. */
952 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
954 HvHASKFLAGS_on((SV*)hv);
958 PERL_HASH_INTERNAL(hash, key, klen);
960 if (keysv && (SvIsCOW_shared_hash(keysv))) {
963 PERL_HASH(hash, key, klen);
967 masked_flags = (k_flags & HVhek_MASK);
969 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
970 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
973 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
974 if (HeHASH(entry) != hash) /* strings can't be equal */
976 if (HeKLEN(entry) != (I32)klen)
978 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
980 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
983 /* if placeholder is here, it's already been deleted.... */
984 if (HeVAL(entry) == &PL_sv_placeholder)
986 if (k_flags & HVhek_FREEKEY)
990 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
991 S_hv_notallowed(aTHX_ k_flags, key, klen,
992 "delete readonly key '%"SVf"' from"
995 if (k_flags & HVhek_FREEKEY)
998 if (d_flags & G_DISCARD)
1001 sv = sv_2mortal(HeVAL(entry));
1002 HeVAL(entry) = &PL_sv_placeholder;
1006 * If a restricted hash, rather than really deleting the entry, put
1007 * a placeholder there. This marks the key as being "approved", so
1008 * we can still access via not-really-existing key without raising
1011 if (SvREADONLY(hv)) {
1012 SvREFCNT_dec(HeVAL(entry));
1013 HeVAL(entry) = &PL_sv_placeholder;
1014 /* We'll be saving this slot, so the number of allocated keys
1015 * doesn't go down, but the number placeholders goes up */
1016 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1018 *oentry = HeNEXT(entry);
1020 xhv->xhv_fill--; /* HvFILL(hv)-- */
1021 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1024 hv_free_ent(hv, entry);
1025 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1026 if (xhv->xhv_keys == 0)
1027 HvHASKFLAGS_off(hv);
1031 if (SvREADONLY(hv)) {
1032 S_hv_notallowed(aTHX_ k_flags, key, klen,
1033 "delete disallowed key '%"SVf"' from"
1037 if (k_flags & HVhek_FREEKEY)
1043 S_hsplit(pTHX_ HV *hv)
1045 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1046 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1047 register I32 newsize = oldsize * 2;
1049 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1053 register HE **oentry;
1054 int longest_chain = 0;
1057 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1058 hv, (int) oldsize);*/
1060 if (HvPLACEHOLDERS(hv) && !SvREADONLY(hv)) {
1061 /* Can make this clear any placeholders first for non-restricted hashes,
1062 even though Storable rebuilds restricted hashes by putting in all the
1063 placeholders (first) before turning on the readonly flag, because
1064 Storable always pre-splits the hash. */
1065 hv_clear_placeholders(hv);
1069 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1070 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1076 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1081 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1082 if (oldsize >= 64) {
1083 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1084 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1087 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1091 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1092 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1093 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1096 for (i=0; i<oldsize; i++,aep++) {
1097 int left_length = 0;
1098 int right_length = 0;
1100 if (!*aep) /* non-existent */
1103 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1104 if ((HeHASH(entry) & newsize) != (U32)i) {
1105 *oentry = HeNEXT(entry);
1106 HeNEXT(entry) = *bep;
1108 xhv->xhv_fill++; /* HvFILL(hv)++ */
1114 oentry = &HeNEXT(entry);
1118 if (!*aep) /* everything moved */
1119 xhv->xhv_fill--; /* HvFILL(hv)-- */
1120 /* I think we don't actually need to keep track of the longest length,
1121 merely flag if anything is too long. But for the moment while
1122 developing this code I'll track it. */
1123 if (left_length > longest_chain)
1124 longest_chain = left_length;
1125 if (right_length > longest_chain)
1126 longest_chain = right_length;
1130 /* Pick your policy for "hashing isn't working" here: */
1131 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1136 if (hv == PL_strtab) {
1137 /* Urg. Someone is doing something nasty to the string table.
1142 /* Awooga. Awooga. Pathological data. */
1143 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1144 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1147 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1148 was_shared = HvSHAREKEYS(hv);
1151 HvSHAREKEYS_off(hv);
1154 aep = (HE **) xhv->xhv_array;
1156 for (i=0; i<newsize; i++,aep++) {
1159 /* We're going to trash this HE's next pointer when we chain it
1160 into the new hash below, so store where we go next. */
1161 HE *next = HeNEXT(entry);
1165 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1170 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1171 hash, HeKFLAGS(entry));
1172 unshare_hek (HeKEY_hek(entry));
1173 HeKEY_hek(entry) = new_hek;
1175 /* Not shared, so simply write the new hash in. */
1176 HeHASH(entry) = hash;
1178 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1179 HEK_REHASH_on(HeKEY_hek(entry));
1180 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1182 /* Copy oentry to the correct new chain. */
1183 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1185 xhv->xhv_fill++; /* HvFILL(hv)++ */
1186 HeNEXT(entry) = *bep;
1192 Safefree (xhv->xhv_array);
1193 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1197 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1199 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1200 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1201 register I32 newsize;
1207 register HE **oentry;
1209 newsize = (I32) newmax; /* possible truncation here */
1210 if (newsize != newmax || newmax <= oldsize)
1212 while ((newsize & (1 + ~newsize)) != newsize) {
1213 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1215 if (newsize < newmax)
1217 if (newsize < newmax)
1218 return; /* overflow detection */
1220 a = xhv->xhv_array; /* HvARRAY(hv) */
1223 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1224 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1230 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1235 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1236 if (oldsize >= 64) {
1237 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1238 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1241 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1244 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1247 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1249 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1250 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1251 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1255 for (i=0; i<oldsize; i++,aep++) {
1256 if (!*aep) /* non-existent */
1258 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1259 if ((j = (HeHASH(entry) & newsize)) != i) {
1261 *oentry = HeNEXT(entry);
1262 if (!(HeNEXT(entry) = aep[j]))
1263 xhv->xhv_fill++; /* HvFILL(hv)++ */
1268 oentry = &HeNEXT(entry);
1270 if (!*aep) /* everything moved */
1271 xhv->xhv_fill--; /* HvFILL(hv)-- */
1278 Creates a new HV. The reference count is set to 1.
1287 register XPVHV* xhv;
1289 hv = (HV*)NEWSV(502,0);
1290 sv_upgrade((SV *)hv, SVt_PVHV);
1291 xhv = (XPVHV*)SvANY(hv);
1294 #ifndef NODEFAULT_SHAREKEYS
1295 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1298 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1299 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1300 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1301 (void)hv_iterinit(hv); /* so each() will start off right */
1306 Perl_newHVhv(pTHX_ HV *ohv)
1309 STRLEN hv_max, hv_fill;
1311 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1313 hv_max = HvMAX(ohv);
1315 if (!SvMAGICAL((SV *)ohv)) {
1316 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1318 bool shared = !!HvSHAREKEYS(ohv);
1319 HE **ents, **oents = (HE **)HvARRAY(ohv);
1321 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1324 /* In each bucket... */
1325 for (i = 0; i <= hv_max; i++) {
1326 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1333 /* Copy the linked list of entries. */
1334 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1335 U32 hash = HeHASH(oent);
1336 char *key = HeKEY(oent);
1337 STRLEN len = HeKLEN(oent);
1338 int flags = HeKFLAGS(oent);
1341 HeVAL(ent) = newSVsv(HeVAL(oent));
1343 = shared ? share_hek_flags(key, len, hash, flags)
1344 : save_hek_flags(key, len, hash, flags);
1355 HvFILL(hv) = hv_fill;
1356 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1360 /* Iterate over ohv, copying keys and values one at a time. */
1362 I32 riter = HvRITER(ohv);
1363 HE *eiter = HvEITER(ohv);
1365 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1366 while (hv_max && hv_max + 1 >= hv_fill * 2)
1367 hv_max = hv_max / 2;
1371 while ((entry = hv_iternext_flags(ohv, 0))) {
1372 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1373 newSVsv(HeVAL(entry)), HeHASH(entry),
1376 HvRITER(ohv) = riter;
1377 HvEITER(ohv) = eiter;
1384 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1391 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1392 PL_sub_generation++; /* may be deletion of method from stash */
1394 if (HeKLEN(entry) == HEf_SVKEY) {
1395 SvREFCNT_dec(HeKEY_sv(entry));
1396 Safefree(HeKEY_hek(entry));
1398 else if (HvSHAREKEYS(hv))
1399 unshare_hek(HeKEY_hek(entry));
1401 Safefree(HeKEY_hek(entry));
1406 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1410 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1411 PL_sub_generation++; /* may be deletion of method from stash */
1412 sv_2mortal(HeVAL(entry)); /* free between statements */
1413 if (HeKLEN(entry) == HEf_SVKEY) {
1414 sv_2mortal(HeKEY_sv(entry));
1415 Safefree(HeKEY_hek(entry));
1417 else if (HvSHAREKEYS(hv))
1418 unshare_hek(HeKEY_hek(entry));
1420 Safefree(HeKEY_hek(entry));
1425 =for apidoc hv_clear
1427 Clears a hash, making it empty.
1433 Perl_hv_clear(pTHX_ HV *hv)
1435 register XPVHV* xhv;
1439 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1441 xhv = (XPVHV*)SvANY(hv);
1443 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1444 /* restricted hash: convert all keys to placeholders */
1447 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1448 entry = ((HE**)xhv->xhv_array)[i];
1449 for (; entry; entry = HeNEXT(entry)) {
1450 /* not already placeholder */
1451 if (HeVAL(entry) != &PL_sv_placeholder) {
1452 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1453 SV* keysv = hv_iterkeysv(entry);
1455 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1458 SvREFCNT_dec(HeVAL(entry));
1459 HeVAL(entry) = &PL_sv_placeholder;
1460 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1468 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1469 if (xhv->xhv_array /* HvARRAY(hv) */)
1470 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1471 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1476 HvHASKFLAGS_off(hv);
1483 =for apidoc hv_clear_placeholders
1485 Clears any placeholders from a hash. If a restricted hash has any of its keys
1486 marked as readonly and the key is subsequently deleted, the key is not actually
1487 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1488 it so it will be ignored by future operations such as iterating over the hash,
1489 but will still allow the hash to have a value reaasigned to the key at some
1490 future point. This function clears any such placeholder keys from the hash.
1491 See Hash::Util::lock_keys() for an example of its use.
1497 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1499 I32 items = (I32)HvPLACEHOLDERS(hv);
1506 /* Loop down the linked list heads */
1508 HE **oentry = &(HvARRAY(hv))[i];
1509 HE *entry = *oentry;
1514 for (; entry; entry = *oentry) {
1515 if (HeVAL(entry) == &PL_sv_placeholder) {
1516 *oentry = HeNEXT(entry);
1517 if (first && !*oentry)
1518 HvFILL(hv)--; /* This linked list is now empty. */
1522 hv_free_ent(hv, entry);
1526 HvTOTALKEYS(hv) -= HvPLACEHOLDERS(hv);
1527 if (HvKEYS(hv) == 0)
1528 HvHASKFLAGS_off(hv);
1529 HvPLACEHOLDERS(hv) = 0;
1533 oentry = &HeNEXT(entry);
1538 /* You can't get here, hence assertion should always fail. */
1539 assert (items == 0);
1544 S_hfreeentries(pTHX_ HV *hv)
1546 register HE **array;
1548 register HE *oentry = Null(HE*);
1559 array = HvARRAY(hv);
1560 /* make everyone else think the array is empty, so that the destructors
1561 * called for freed entries can't recusively mess with us */
1562 HvARRAY(hv) = Null(HE**);
1564 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1570 entry = HeNEXT(entry);
1571 hv_free_ent(hv, oentry);
1576 entry = array[riter];
1579 HvARRAY(hv) = array;
1580 (void)hv_iterinit(hv);
1584 =for apidoc hv_undef
1592 Perl_hv_undef(pTHX_ HV *hv)
1594 register XPVHV* xhv;
1597 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1598 xhv = (XPVHV*)SvANY(hv);
1600 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1603 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1604 Safefree(HvNAME(hv));
1607 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1608 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1609 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1616 =for apidoc hv_iterinit
1618 Prepares a starting point to traverse a hash table. Returns the number of
1619 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1620 currently only meaningful for hashes without tie magic.
1622 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1623 hash buckets that happen to be in use. If you still need that esoteric
1624 value, you can get it through the macro C<HvFILL(tb)>.
1631 Perl_hv_iterinit(pTHX_ HV *hv)
1633 register XPVHV* xhv;
1637 Perl_croak(aTHX_ "Bad hash");
1638 xhv = (XPVHV*)SvANY(hv);
1639 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1640 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1642 hv_free_ent(hv, entry);
1644 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1645 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1646 /* used to be xhv->xhv_fill before 5.004_65 */
1647 return XHvTOTALKEYS(xhv);
1650 =for apidoc hv_iternext
1652 Returns entries from a hash iterator. See C<hv_iterinit>.
1654 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1655 iterator currently points to, without losing your place or invalidating your
1656 iterator. Note that in this case the current entry is deleted from the hash
1657 with your iterator holding the last reference to it. Your iterator is flagged
1658 to free the entry on the next call to C<hv_iternext>, so you must not discard
1659 your iterator immediately else the entry will leak - call C<hv_iternext> to
1660 trigger the resource deallocation.
1666 Perl_hv_iternext(pTHX_ HV *hv)
1668 return hv_iternext_flags(hv, 0);
1672 =for apidoc hv_iternext_flags
1674 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1675 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1676 set the placeholders keys (for restricted hashes) will be returned in addition
1677 to normal keys. By default placeholders are automatically skipped over.
1678 Currently a placeholder is implemented with a value that is
1679 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1680 restricted hashes may change, and the implementation currently is
1681 insufficiently abstracted for any change to be tidy.
1687 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1689 register XPVHV* xhv;
1695 Perl_croak(aTHX_ "Bad hash");
1696 xhv = (XPVHV*)SvANY(hv);
1697 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1699 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1700 SV *key = sv_newmortal();
1702 sv_setsv(key, HeSVKEY_force(entry));
1703 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1709 /* one HE per MAGICAL hash */
1710 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1712 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1714 HeKEY_hek(entry) = hek;
1715 HeKLEN(entry) = HEf_SVKEY;
1717 magic_nextpack((SV*) hv,mg,key);
1719 /* force key to stay around until next time */
1720 HeSVKEY_set(entry, SvREFCNT_inc(key));
1721 return entry; /* beware, hent_val is not set */
1724 SvREFCNT_dec(HeVAL(entry));
1725 Safefree(HeKEY_hek(entry));
1727 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1730 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1731 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1735 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1736 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1737 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1739 /* At start of hash, entry is NULL. */
1742 entry = HeNEXT(entry);
1743 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1745 * Skip past any placeholders -- don't want to include them in
1748 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1749 entry = HeNEXT(entry);
1754 /* OK. Come to the end of the current list. Grab the next one. */
1756 xhv->xhv_riter++; /* HvRITER(hv)++ */
1757 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1758 /* There is no next one. End of the hash. */
1759 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1762 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1763 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1765 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1766 /* If we have an entry, but it's a placeholder, don't count it.
1768 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1769 entry = HeNEXT(entry);
1771 /* Will loop again if this linked list starts NULL
1772 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1773 or if we run through it and find only placeholders. */
1776 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1778 hv_free_ent(hv, oldentry);
1781 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1782 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1784 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1789 =for apidoc hv_iterkey
1791 Returns the key from the current position of the hash iterator. See
1798 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1800 if (HeKLEN(entry) == HEf_SVKEY) {
1802 char *p = SvPV(HeKEY_sv(entry), len);
1807 *retlen = HeKLEN(entry);
1808 return HeKEY(entry);
1812 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1814 =for apidoc hv_iterkeysv
1816 Returns the key as an C<SV*> from the current position of the hash
1817 iterator. The return value will always be a mortal copy of the key. Also
1824 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1826 if (HeKLEN(entry) != HEf_SVKEY) {
1827 HEK *hek = HeKEY_hek(entry);
1828 int flags = HEK_FLAGS(hek);
1831 if (flags & HVhek_WASUTF8) {
1833 Andreas would like keys he put in as utf8 to come back as utf8
1835 STRLEN utf8_len = HEK_LEN(hek);
1836 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1838 sv = newSVpvn ((char*)as_utf8, utf8_len);
1840 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1841 } else if (flags & HVhek_REHASH) {
1842 /* We don't have a pointer to the hv, so we have to replicate the
1843 flag into every HEK. This hv is using custom a hasing
1844 algorithm. Hence we can't return a shared string scalar, as
1845 that would contain the (wrong) hash value, and might get passed
1846 into an hv routine with a regular hash */
1848 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1852 sv = newSVpvn_share(HEK_KEY(hek),
1853 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1856 return sv_2mortal(sv);
1858 return sv_mortalcopy(HeKEY_sv(entry));
1862 =for apidoc hv_iterval
1864 Returns the value from the current position of the hash iterator. See
1871 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1873 if (SvRMAGICAL(hv)) {
1874 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1875 SV* sv = sv_newmortal();
1876 if (HeKLEN(entry) == HEf_SVKEY)
1877 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1878 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1882 return HeVAL(entry);
1886 =for apidoc hv_iternextsv
1888 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1895 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1898 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1900 *key = hv_iterkey(he, retlen);
1901 return hv_iterval(hv, he);
1905 =for apidoc hv_magic
1907 Adds magic to a hash. See C<sv_magic>.
1913 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1915 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1918 #if 0 /* use the macro from hv.h instead */
1921 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1923 return HEK_KEY(share_hek(sv, len, hash));
1928 /* possibly free a shared string if no one has access to it
1929 * len and hash must both be valid for str.
1932 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1934 unshare_hek_or_pvn (NULL, str, len, hash);
1939 Perl_unshare_hek(pTHX_ HEK *hek)
1941 unshare_hek_or_pvn(hek, NULL, 0, 0);
1944 /* possibly free a shared string if no one has access to it
1945 hek if non-NULL takes priority over the other 3, else str, len and hash
1946 are used. If so, len and hash must both be valid for str.
1949 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1951 register XPVHV* xhv;
1953 register HE **oentry;
1956 bool is_utf8 = FALSE;
1958 const char *save = str;
1961 hash = HEK_HASH(hek);
1962 } else if (len < 0) {
1963 STRLEN tmplen = -len;
1965 /* See the note in hv_fetch(). --jhi */
1966 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1969 k_flags = HVhek_UTF8;
1971 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1974 /* what follows is the moral equivalent of:
1975 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1976 if (--*Svp == Nullsv)
1977 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1979 xhv = (XPVHV*)SvANY(PL_strtab);
1980 /* assert(xhv_array != 0) */
1982 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1983 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1985 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1986 if (HeKEY_hek(entry) != hek)
1992 int flags_masked = k_flags & HVhek_MASK;
1993 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1994 if (HeHASH(entry) != hash) /* strings can't be equal */
1996 if (HeKLEN(entry) != len)
1998 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2000 if (HeKFLAGS(entry) != flags_masked)
2008 if (--HeVAL(entry) == Nullsv) {
2009 *oentry = HeNEXT(entry);
2011 xhv->xhv_fill--; /* HvFILL(hv)-- */
2012 Safefree(HeKEY_hek(entry));
2014 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2018 UNLOCK_STRTAB_MUTEX;
2019 if (!found && ckWARN_d(WARN_INTERNAL))
2020 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2021 "Attempt to free non-existent shared string '%s'%s"
2023 hek ? HEK_KEY(hek) : str,
2024 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2025 if (k_flags & HVhek_FREEKEY)
2029 /* get a (constant) string ptr from the global string table
2030 * string will get added if it is not already there.
2031 * len and hash must both be valid for str.
2034 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2036 bool is_utf8 = FALSE;
2038 const char *save = str;
2041 STRLEN tmplen = -len;
2043 /* See the note in hv_fetch(). --jhi */
2044 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2046 /* If we were able to downgrade here, then than means that we were passed
2047 in a key which only had chars 0-255, but was utf8 encoded. */
2050 /* If we found we were able to downgrade the string to bytes, then
2051 we should flag that it needs upgrading on keys or each. Also flag
2052 that we need share_hek_flags to free the string. */
2054 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2057 return share_hek_flags (str, len, hash, flags);
2061 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2063 register XPVHV* xhv;
2065 register HE **oentry;
2068 int flags_masked = flags & HVhek_MASK;
2070 /* what follows is the moral equivalent of:
2072 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2073 hv_store(PL_strtab, str, len, Nullsv, hash);
2075 Can't rehash the shared string table, so not sure if it's worth
2076 counting the number of entries in the linked list
2078 xhv = (XPVHV*)SvANY(PL_strtab);
2079 /* assert(xhv_array != 0) */
2081 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2082 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2083 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2084 if (HeHASH(entry) != hash) /* strings can't be equal */
2086 if (HeKLEN(entry) != len)
2088 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2090 if (HeKFLAGS(entry) != flags_masked)
2097 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags_masked);
2098 HeVAL(entry) = Nullsv;
2099 HeNEXT(entry) = *oentry;
2101 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2102 if (i) { /* initial entry? */
2103 xhv->xhv_fill++; /* HvFILL(hv)++ */
2104 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2109 ++HeVAL(entry); /* use value slot as REFCNT */
2110 UNLOCK_STRTAB_MUTEX;
2112 if (flags & HVhek_FREEKEY)
2115 return HeKEY_hek(entry);
2120 =for apidoc hv_assert
2122 Check that a hash is in an internally consistent state.
2128 Perl_hv_assert(pTHX_ HV *hv)
2132 int placeholders = 0;
2135 I32 riter = HvRITER(hv);
2136 HE *eiter = HvEITER(hv);
2138 (void)hv_iterinit(hv);
2140 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2141 /* sanity check the values */
2142 if (HeVAL(entry) == &PL_sv_placeholder) {
2147 /* sanity check the keys */
2148 if (HeSVKEY(entry)) {
2149 /* Don't know what to check on SV keys. */
2150 } else if (HeKUTF8(entry)) {
2152 if (HeKWASUTF8(entry)) {
2153 PerlIO_printf(Perl_debug_log,
2154 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2155 (int) HeKLEN(entry), HeKEY(entry));
2158 } else if (HeKWASUTF8(entry)) {
2162 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2163 if (HvUSEDKEYS(hv) != real) {
2164 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2165 (int) real, (int) HvUSEDKEYS(hv));
2168 if (HvPLACEHOLDERS(hv) != placeholders) {
2169 PerlIO_printf(Perl_debug_log,
2170 "Count %d placeholder(s), but hash reports %d\n",
2171 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2175 if (withflags && ! HvHASKFLAGS(hv)) {
2176 PerlIO_printf(Perl_debug_log,
2177 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2184 HvRITER(hv) = riter; /* Restore hash iterator state */
2185 HvEITER(hv) = eiter;