3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "I sit beside the fire and think of all that I have seen." --Bilbo
16 =head1 Hash Manipulation Functions
31 PL_he_root = HeNEXT(he);
40 HeNEXT(p) = (HE*)PL_he_root;
51 New(54, ptr, 1008/sizeof(XPV), XPV);
52 ptr->xpv_pv = (char*)PL_he_arenaroot;
53 PL_he_arenaroot = ptr;
56 heend = &he[1008 / sizeof(HE) - 1];
59 HeNEXT(he) = (HE*)(he + 1);
67 #define new_HE() (HE*)safemalloc(sizeof(HE))
68 #define del_HE(p) safefree((char*)p)
72 #define new_HE() new_he()
73 #define del_HE(p) del_he(p)
78 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
83 New(54, k, HEK_BASESIZE + len + 2, char);
85 Copy(str, HEK_KEY(hek), len, char);
86 HEK_KEY(hek)[len] = 0;
89 HEK_FLAGS(hek) = (unsigned char)flags;
93 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
97 Perl_free_tied_hv_pool(pTHX)
100 HE *he = PL_hv_fetch_ent_mh;
102 Safefree(HeKEY_hek(he));
107 PL_hv_fetch_ent_mh = Nullhe;
110 #if defined(USE_ITHREADS)
112 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
118 /* look for it in the table first */
119 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
123 /* create anew and remember what it is */
125 ptr_table_store(PL_ptr_table, e, ret);
127 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
128 if (HeKLEN(e) == HEf_SVKEY) {
130 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
131 HeKEY_hek(ret) = (HEK*)k;
132 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
135 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
138 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
140 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
143 #endif /* USE_ITHREADS */
146 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
149 SV *sv = sv_newmortal(), *esv = sv_newmortal();
150 if (!(flags & HVhek_FREEKEY)) {
151 sv_setpvn(sv, key, klen);
154 /* Need to free saved eventually assign to mortal SV */
155 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
156 sv_usepvn(sv, (char *) key, klen);
158 if (flags & HVhek_UTF8) {
161 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
162 Perl_croak(aTHX_ SvPVX(esv), sv);
165 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
171 Returns the SV which corresponds to the specified key in the hash. The
172 C<klen> is the length of the key. If C<lval> is set then the fetch will be
173 part of a store. Check that the return value is non-null before
174 dereferencing it to an C<SV*>.
176 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
177 information on how to use this function on tied hashes.
184 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
186 bool is_utf8 = FALSE;
187 const char *keysave = key;
196 STRLEN tmplen = klen;
197 /* Just casting the &klen to (STRLEN) won't work well
198 * if STRLEN and I32 are of different widths. --jhi */
199 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
201 /* If we were able to downgrade here, then than means that we were
202 passed in a key which only had chars 0-255, but was utf8 encoded. */
205 /* If we found we were able to downgrade the string to bytes, then
206 we should flag that it needs upgrading on keys or each. */
208 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
211 return hv_fetch_flags (hv, key, klen, lval, flags);
215 S_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen, I32 lval, int flags)
225 if (SvRMAGICAL(hv)) {
226 /* All this clause seems to be utf8 unaware.
227 By moving the utf8 stuff out to hv_fetch_flags I need to ensure
228 key doesn't leak. I've not tried solving the utf8-ness.
231 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
233 sv_upgrade(sv, SVt_PVLV);
234 mg_copy((SV*)hv, sv, key, klen);
235 if (flags & HVhek_FREEKEY)
238 LvTARG(sv) = sv; /* fake (SV**) */
239 return &(LvTARG(sv));
241 #ifdef ENV_IS_CASELESS
242 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
244 for (i = 0; i < klen; ++i)
245 if (isLOWER(key[i])) {
246 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
247 SV **ret = hv_fetch(hv, nkey, klen, 0);
249 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
251 } else if (flags & HVhek_FREEKEY)
259 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
260 avoid unnecessary pointer dereferencing. */
261 xhv = (XPVHV*)SvANY(hv);
262 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
264 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
265 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
268 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
269 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
272 if (flags & HVhek_FREEKEY)
279 PERL_HASH_INTERNAL(hash, key, klen);
281 PERL_HASH(hash, key, klen);
284 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
285 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
286 for (; entry; entry = HeNEXT(entry)) {
287 if (!HeKEY_hek(entry))
289 if (HeHASH(entry) != hash) /* strings can't be equal */
291 if (HeKLEN(entry) != (I32)klen)
293 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
295 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
296 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
297 xor is true if bits differ, in which case this isn't a match. */
298 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
300 if (lval && HeKFLAGS(entry) != flags) {
301 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
302 But if entry was set previously with HVhek_WASUTF8 and key now
303 doesn't (or vice versa) then we should change the key's flag,
304 as this is assignment. */
305 if (HvSHAREKEYS(hv)) {
306 /* Need to swap the key we have for a key with the flags we
307 need. As keys are shared we can't just write to the flag,
308 so we share the new one, unshare the old one. */
309 int flags_nofree = flags & ~HVhek_FREEKEY;
310 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
311 unshare_hek (HeKEY_hek(entry));
312 HeKEY_hek(entry) = new_hek;
315 HeKFLAGS(entry) = flags;
319 if (flags & HVhek_FREEKEY)
321 /* if we find a placeholder, we pretend we haven't found anything */
322 if (HeVAL(entry) == &PL_sv_placeholder)
324 return &HeVAL(entry);
327 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
328 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
330 char *env = PerlEnv_ENVgetenv_len(key,&len);
332 sv = newSVpvn(env,len);
334 if (flags & HVhek_FREEKEY)
336 return hv_store(hv,key,klen,sv,hash);
340 if (!entry && SvREADONLY(hv)) {
341 S_hv_notallowed(aTHX_ flags, key, klen,
342 "access disallowed key '%"SVf"' in"
345 if (lval) { /* gonna assign to this, so it better be there */
347 return hv_store_flags(hv,key,klen,sv,hash,flags);
349 if (flags & HVhek_FREEKEY)
354 /* returns an HE * structure with the all fields set */
355 /* note that hent_val will be a mortal sv for MAGICAL hashes */
357 =for apidoc hv_fetch_ent
359 Returns the hash entry which corresponds to the specified key in the hash.
360 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
361 if you want the function to compute it. IF C<lval> is set then the fetch
362 will be part of a store. Make sure the return value is non-null before
363 accessing it. The return value when C<tb> is a tied hash is a pointer to a
364 static location, so be sure to make a copy of the structure if you need to
367 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
368 information on how to use this function on tied hashes.
374 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
388 if (SvRMAGICAL(hv)) {
389 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
391 keysv = newSVsv(keysv);
392 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
393 /* grab a fake HE/HEK pair from the pool or make a new one */
394 entry = PL_hv_fetch_ent_mh;
396 PL_hv_fetch_ent_mh = HeNEXT(entry);
400 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
401 HeKEY_hek(entry) = (HEK*)k;
403 HeNEXT(entry) = Nullhe;
404 HeSVKEY_set(entry, keysv);
406 sv_upgrade(sv, SVt_PVLV);
408 LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
411 #ifdef ENV_IS_CASELESS
412 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
414 key = SvPV(keysv, klen);
415 for (i = 0; i < klen; ++i)
416 if (isLOWER(key[i])) {
417 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
418 (void)strupr(SvPVX(nkeysv));
419 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
421 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
428 keysave = key = SvPV(keysv, klen);
429 xhv = (XPVHV*)SvANY(hv);
430 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
432 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
433 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
436 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
437 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
443 is_utf8 = (SvUTF8(keysv)!=0);
446 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
450 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
454 PERL_HASH_INTERNAL(hash, key, klen);
456 if SvIsCOW_shared_hash(keysv) {
459 PERL_HASH(hash, key, klen);
463 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
464 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
465 for (; entry; entry = HeNEXT(entry)) {
466 if (HeHASH(entry) != hash) /* strings can't be equal */
468 if (HeKLEN(entry) != (I32)klen)
470 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
472 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
474 if (lval && HeKFLAGS(entry) != flags) {
475 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
476 But if entry was set previously with HVhek_WASUTF8 and key now
477 doesn't (or vice versa) then we should change the key's flag,
478 as this is assignment. */
479 if (HvSHAREKEYS(hv)) {
480 /* Need to swap the key we have for a key with the flags we
481 need. As keys are shared we can't just write to the flag,
482 so we share the new one, unshare the old one. */
483 int flags_nofree = flags & ~HVhek_FREEKEY;
484 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
485 unshare_hek (HeKEY_hek(entry));
486 HeKEY_hek(entry) = new_hek;
489 HeKFLAGS(entry) = flags;
495 /* if we find a placeholder, we pretend we haven't found anything */
496 if (HeVAL(entry) == &PL_sv_placeholder)
500 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
501 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
503 char *env = PerlEnv_ENVgetenv_len(key,&len);
505 sv = newSVpvn(env,len);
507 return hv_store_ent(hv,keysv,sv,hash);
511 if (!entry && SvREADONLY(hv)) {
512 S_hv_notallowed(aTHX_ flags, key, klen,
513 "access disallowed key '%"SVf"' in"
516 if (flags & HVhek_FREEKEY)
518 if (lval) { /* gonna assign to this, so it better be there */
520 return hv_store_ent(hv,keysv,sv,hash);
526 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
528 MAGIC *mg = SvMAGIC(hv);
532 if (isUPPER(mg->mg_type)) {
534 switch (mg->mg_type) {
535 case PERL_MAGIC_tied:
537 *needs_store = FALSE;
540 mg = mg->mg_moremagic;
547 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
548 the length of the key. The C<hash> parameter is the precomputed hash
549 value; if it is zero then Perl will compute it. The return value will be
550 NULL if the operation failed or if the value did not need to be actually
551 stored within the hash (as in the case of tied hashes). Otherwise it can
552 be dereferenced to get the original C<SV*>. Note that the caller is
553 responsible for suitably incrementing the reference count of C<val> before
554 the call, and decrementing it if the function returned NULL. Effectively
555 a successful hv_store takes ownership of one reference to C<val>. This is
556 usually what you want; a newly created SV has a reference count of one, so
557 if all your code does is create SVs then store them in a hash, hv_store
558 will own the only reference to the new SV, and your code doesn't need to do
559 anything further to tidy up. hv_store is not implemented as a call to
560 hv_store_ent, and does not create a temporary SV for the key, so if your
561 key data is not already in SV form then use hv_store in preference to
564 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
565 information on how to use this function on tied hashes.
571 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
573 bool is_utf8 = FALSE;
574 const char *keysave = key;
583 STRLEN tmplen = klen;
584 /* Just casting the &klen to (STRLEN) won't work well
585 * if STRLEN and I32 are of different widths. --jhi */
586 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
588 /* If we were able to downgrade here, then than means that we were
589 passed in a key which only had chars 0-255, but was utf8 encoded. */
592 /* If we found we were able to downgrade the string to bytes, then
593 we should flag that it needs upgrading on keys or each. */
595 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
598 return hv_store_flags (hv, key, klen, val, hash, flags);
602 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
603 register U32 hash, int flags)
608 register HE **oentry;
613 xhv = (XPVHV*)SvANY(hv);
617 hv_magic_check (hv, &needs_copy, &needs_store);
619 mg_copy((SV*)hv, val, key, klen);
620 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
621 if (flags & HVhek_FREEKEY)
625 #ifdef ENV_IS_CASELESS
626 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
627 key = savepvn(key,klen);
628 key = (const char*)strupr((char*)key);
636 HvHASKFLAGS_on((SV*)hv);
639 /* We don't have a pointer to the hv, so we have to replicate the
640 flag into every HEK, so that hv_iterkeysv can see it. */
641 flags |= HVhek_REHASH;
642 PERL_HASH_INTERNAL(hash, key, klen);
644 PERL_HASH(hash, key, klen);
646 if (!xhv->xhv_array /* !HvARRAY(hv) */)
647 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
648 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
651 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
652 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
655 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
656 if (HeHASH(entry) != hash) /* strings can't be equal */
658 if (HeKLEN(entry) != (I32)klen)
660 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
662 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
664 if (HeVAL(entry) == &PL_sv_placeholder)
665 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
667 SvREFCNT_dec(HeVAL(entry));
668 if (flags & HVhek_PLACEHOLD) {
669 /* We have been requested to insert a placeholder. Currently
670 only Storable is allowed to do this. */
671 xhv->xhv_placeholders++;
672 HeVAL(entry) = &PL_sv_placeholder;
676 if (HeKFLAGS(entry) != flags) {
677 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
678 But if entry was set previously with HVhek_WASUTF8 and key now
679 doesn't (or vice versa) then we should change the key's flag,
680 as this is assignment. */
681 if (HvSHAREKEYS(hv)) {
682 /* Need to swap the key we have for a key with the flags we
683 need. As keys are shared we can't just write to the flag,
684 so we share the new one, unshare the old one. */
685 int flags_nofree = flags & ~HVhek_FREEKEY;
686 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
687 unshare_hek (HeKEY_hek(entry));
688 HeKEY_hek(entry) = new_hek;
691 HeKFLAGS(entry) = flags;
693 if (flags & HVhek_FREEKEY)
695 return &HeVAL(entry);
698 if (SvREADONLY(hv)) {
699 S_hv_notallowed(aTHX_ flags, key, klen,
700 "access disallowed key '%"SVf"' to"
705 /* share_hek_flags will do the free for us. This might be considered
708 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
709 else /* gotta do the real thing */
710 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
711 if (flags & HVhek_PLACEHOLD) {
712 /* We have been requested to insert a placeholder. Currently
713 only Storable is allowed to do this. */
714 xhv->xhv_placeholders++;
715 HeVAL(entry) = &PL_sv_placeholder;
718 HeNEXT(entry) = *oentry;
721 xhv->xhv_keys++; /* HvKEYS(hv)++ */
722 if (i) { /* initial entry? */
723 xhv->xhv_fill++; /* HvFILL(hv)++ */
724 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
728 return &HeVAL(entry);
732 =for apidoc hv_store_ent
734 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
735 parameter is the precomputed hash value; if it is zero then Perl will
736 compute it. The return value is the new hash entry so created. It will be
737 NULL if the operation failed or if the value did not need to be actually
738 stored within the hash (as in the case of tied hashes). Otherwise the
739 contents of the return value can be accessed using the C<He?> macros
740 described here. Note that the caller is responsible for suitably
741 incrementing the reference count of C<val> before the call, and
742 decrementing it if the function returned NULL. Effectively a successful
743 hv_store_ent takes ownership of one reference to C<val>. This is
744 usually what you want; a newly created SV has a reference count of one, so
745 if all your code does is create SVs then store them in a hash, hv_store
746 will own the only reference to the new SV, and your code doesn't need to do
747 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
748 unlike C<val> it does not take ownership of it, so maintaining the correct
749 reference count on C<key> is entirely the caller's responsibility. hv_store
750 is not implemented as a call to hv_store_ent, and does not create a temporary
751 SV for the key, so if your key data is not already in SV form then use
752 hv_store in preference to hv_store_ent.
754 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
755 information on how to use this function on tied hashes.
761 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
776 xhv = (XPVHV*)SvANY(hv);
780 hv_magic_check (hv, &needs_copy, &needs_store);
782 bool save_taint = PL_tainted;
784 PL_tainted = SvTAINTED(keysv);
785 keysv = sv_2mortal(newSVsv(keysv));
786 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
787 TAINT_IF(save_taint);
788 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
790 #ifdef ENV_IS_CASELESS
791 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
792 key = SvPV(keysv, klen);
793 keysv = sv_2mortal(newSVpvn(key,klen));
794 (void)strupr(SvPVX(keysv));
801 keysave = key = SvPV(keysv, klen);
802 is_utf8 = (SvUTF8(keysv) != 0);
805 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
809 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
810 HvHASKFLAGS_on((SV*)hv);
814 /* We don't have a pointer to the hv, so we have to replicate the
815 flag into every HEK, so that hv_iterkeysv can see it. */
816 flags |= HVhek_REHASH;
817 PERL_HASH_INTERNAL(hash, key, klen);
819 if SvIsCOW_shared_hash(keysv) {
822 PERL_HASH(hash, key, klen);
826 if (!xhv->xhv_array /* !HvARRAY(hv) */)
827 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
828 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
831 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
832 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
835 for (; entry; i=0, entry = HeNEXT(entry)) {
836 if (HeHASH(entry) != hash) /* strings can't be equal */
838 if (HeKLEN(entry) != (I32)klen)
840 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
842 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
844 if (HeVAL(entry) == &PL_sv_placeholder)
845 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
847 SvREFCNT_dec(HeVAL(entry));
849 if (HeKFLAGS(entry) != flags) {
850 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
851 But if entry was set previously with HVhek_WASUTF8 and key now
852 doesn't (or vice versa) then we should change the key's flag,
853 as this is assignment. */
854 if (HvSHAREKEYS(hv)) {
855 /* Need to swap the key we have for a key with the flags we
856 need. As keys are shared we can't just write to the flag,
857 so we share the new one, unshare the old one. */
858 int flags_nofree = flags & ~HVhek_FREEKEY;
859 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
860 unshare_hek (HeKEY_hek(entry));
861 HeKEY_hek(entry) = new_hek;
864 HeKFLAGS(entry) = flags;
866 if (flags & HVhek_FREEKEY)
871 if (SvREADONLY(hv)) {
872 S_hv_notallowed(aTHX_ flags, key, klen,
873 "access disallowed key '%"SVf"' to"
878 /* share_hek_flags will do the free for us. This might be considered
881 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
882 else /* gotta do the real thing */
883 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
885 HeNEXT(entry) = *oentry;
888 xhv->xhv_keys++; /* HvKEYS(hv)++ */
889 if (i) { /* initial entry? */
890 xhv->xhv_fill++; /* HvFILL(hv)++ */
891 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
899 =for apidoc hv_delete
901 Deletes a key/value pair in the hash. The value SV is removed from the
902 hash and returned to the caller. The C<klen> is the length of the key.
903 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
910 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
916 register HE **oentry;
919 bool is_utf8 = FALSE;
921 const char *keysave = key;
929 if (SvRMAGICAL(hv)) {
932 hv_magic_check (hv, &needs_copy, &needs_store);
934 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
940 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
941 /* No longer an element */
942 sv_unmagic(sv, PERL_MAGIC_tiedelem);
945 return Nullsv; /* element cannot be deleted */
947 #ifdef ENV_IS_CASELESS
948 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
949 sv = sv_2mortal(newSVpvn(key,klen));
950 key = strupr(SvPVX(sv));
955 xhv = (XPVHV*)SvANY(hv);
956 if (!xhv->xhv_array /* !HvARRAY(hv) */)
960 STRLEN tmplen = klen;
961 /* See the note in hv_fetch(). --jhi */
962 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
965 k_flags = HVhek_UTF8;
967 k_flags |= HVhek_FREEKEY;
971 PERL_HASH_INTERNAL(hash, key, klen);
973 PERL_HASH(hash, key, klen);
976 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
977 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
980 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
981 if (HeHASH(entry) != hash) /* strings can't be equal */
983 if (HeKLEN(entry) != (I32)klen)
985 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
987 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
989 if (k_flags & HVhek_FREEKEY)
991 /* if placeholder is here, it's already been deleted.... */
992 if (HeVAL(entry) == &PL_sv_placeholder)
995 return Nullsv; /* if still SvREADONLY, leave it deleted. */
997 /* okay, really delete the placeholder... */
998 *oentry = HeNEXT(entry);
1000 xhv->xhv_fill--; /* HvFILL(hv)-- */
1001 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1004 hv_free_ent(hv, entry);
1005 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1006 if (xhv->xhv_keys == 0)
1007 HvHASKFLAGS_off(hv);
1008 xhv->xhv_placeholders--;
1012 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1013 S_hv_notallowed(aTHX_ k_flags, key, klen,
1014 "delete readonly key '%"SVf"' from"
1018 if (flags & G_DISCARD)
1021 sv = sv_2mortal(HeVAL(entry));
1022 HeVAL(entry) = &PL_sv_placeholder;
1026 * If a restricted hash, rather than really deleting the entry, put
1027 * a placeholder there. This marks the key as being "approved", so
1028 * we can still access via not-really-existing key without raising
1031 if (SvREADONLY(hv)) {
1032 HeVAL(entry) = &PL_sv_placeholder;
1033 /* We'll be saving this slot, so the number of allocated keys
1034 * doesn't go down, but the number placeholders goes up */
1035 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1037 *oentry = HeNEXT(entry);
1039 xhv->xhv_fill--; /* HvFILL(hv)-- */
1040 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1043 hv_free_ent(hv, entry);
1044 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1045 if (xhv->xhv_keys == 0)
1046 HvHASKFLAGS_off(hv);
1050 if (SvREADONLY(hv)) {
1051 S_hv_notallowed(aTHX_ k_flags, key, klen,
1052 "access disallowed key '%"SVf"' from"
1056 if (k_flags & HVhek_FREEKEY)
1062 =for apidoc hv_delete_ent
1064 Deletes a key/value pair in the hash. The value SV is removed from the
1065 hash and returned to the caller. The C<flags> value will normally be zero;
1066 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
1067 precomputed hash value, or 0 to ask for it to be computed.
1073 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
1075 register XPVHV* xhv;
1080 register HE **oentry;
1088 if (SvRMAGICAL(hv)) {
1091 hv_magic_check (hv, &needs_copy, &needs_store);
1093 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
1095 if (SvMAGICAL(sv)) {
1099 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
1100 /* No longer an element */
1101 sv_unmagic(sv, PERL_MAGIC_tiedelem);
1104 return Nullsv; /* element cannot be deleted */
1106 #ifdef ENV_IS_CASELESS
1107 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1108 key = SvPV(keysv, klen);
1109 keysv = sv_2mortal(newSVpvn(key,klen));
1110 (void)strupr(SvPVX(keysv));
1116 xhv = (XPVHV*)SvANY(hv);
1117 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1120 keysave = key = SvPV(keysv, klen);
1121 is_utf8 = (SvUTF8(keysv) != 0);
1124 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1126 k_flags = HVhek_UTF8;
1128 k_flags |= HVhek_FREEKEY;
1132 PERL_HASH_INTERNAL(hash, key, klen);
1134 PERL_HASH(hash, key, klen);
1137 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1138 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1141 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1142 if (HeHASH(entry) != hash) /* strings can't be equal */
1144 if (HeKLEN(entry) != (I32)klen)
1146 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1148 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1150 if (k_flags & HVhek_FREEKEY)
1153 /* if placeholder is here, it's already been deleted.... */
1154 if (HeVAL(entry) == &PL_sv_placeholder)
1157 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1159 /* okay, really delete the placeholder. */
1160 *oentry = HeNEXT(entry);
1162 xhv->xhv_fill--; /* HvFILL(hv)-- */
1163 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1166 hv_free_ent(hv, entry);
1167 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1168 if (xhv->xhv_keys == 0)
1169 HvHASKFLAGS_off(hv);
1170 xhv->xhv_placeholders--;
1173 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1174 S_hv_notallowed(aTHX_ k_flags, key, klen,
1175 "delete readonly key '%"SVf"' from"
1179 if (flags & G_DISCARD)
1182 sv = sv_2mortal(HeVAL(entry));
1183 HeVAL(entry) = &PL_sv_placeholder;
1187 * If a restricted hash, rather than really deleting the entry, put
1188 * a placeholder there. This marks the key as being "approved", so
1189 * we can still access via not-really-existing key without raising
1192 if (SvREADONLY(hv)) {
1193 HeVAL(entry) = &PL_sv_placeholder;
1194 /* We'll be saving this slot, so the number of allocated keys
1195 * doesn't go down, but the number placeholders goes up */
1196 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1198 *oentry = HeNEXT(entry);
1200 xhv->xhv_fill--; /* HvFILL(hv)-- */
1201 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1204 hv_free_ent(hv, entry);
1205 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1206 if (xhv->xhv_keys == 0)
1207 HvHASKFLAGS_off(hv);
1211 if (SvREADONLY(hv)) {
1212 S_hv_notallowed(aTHX_ k_flags, key, klen,
1213 "delete disallowed key '%"SVf"' from"
1217 if (k_flags & HVhek_FREEKEY)
1223 =for apidoc hv_exists
1225 Returns a boolean indicating whether the specified hash key exists. The
1226 C<klen> is the length of the key.
1232 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1234 register XPVHV* xhv;
1238 bool is_utf8 = FALSE;
1239 const char *keysave = key;
1250 if (SvRMAGICAL(hv)) {
1251 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1252 sv = sv_newmortal();
1253 mg_copy((SV*)hv, sv, key, klen);
1254 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1255 return (bool)SvTRUE(sv);
1257 #ifdef ENV_IS_CASELESS
1258 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1259 sv = sv_2mortal(newSVpvn(key,klen));
1260 key = strupr(SvPVX(sv));
1265 xhv = (XPVHV*)SvANY(hv);
1266 #ifndef DYNAMIC_ENV_FETCH
1267 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1272 STRLEN tmplen = klen;
1273 /* See the note in hv_fetch(). --jhi */
1274 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1277 k_flags = HVhek_UTF8;
1279 k_flags |= HVhek_FREEKEY;
1283 PERL_HASH_INTERNAL(hash, key, klen);
1285 PERL_HASH(hash, key, klen);
1288 #ifdef DYNAMIC_ENV_FETCH
1289 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1292 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1293 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1294 for (; entry; entry = HeNEXT(entry)) {
1295 if (HeHASH(entry) != hash) /* strings can't be equal */
1297 if (HeKLEN(entry) != klen)
1299 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1301 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1303 if (k_flags & HVhek_FREEKEY)
1305 /* If we find the key, but the value is a placeholder, return false. */
1306 if (HeVAL(entry) == &PL_sv_placeholder)
1311 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1312 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1314 char *env = PerlEnv_ENVgetenv_len(key,&len);
1316 sv = newSVpvn(env,len);
1318 (void)hv_store(hv,key,klen,sv,hash);
1319 if (k_flags & HVhek_FREEKEY)
1325 if (k_flags & HVhek_FREEKEY)
1332 =for apidoc hv_exists_ent
1334 Returns a boolean indicating whether the specified hash key exists. C<hash>
1335 can be a valid precomputed hash value, or 0 to ask for it to be
1342 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1344 register XPVHV* xhv;
1356 if (SvRMAGICAL(hv)) {
1357 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1358 SV* svret = sv_newmortal();
1359 sv = sv_newmortal();
1360 keysv = sv_2mortal(newSVsv(keysv));
1361 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1362 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1363 return (bool)SvTRUE(svret);
1365 #ifdef ENV_IS_CASELESS
1366 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1367 key = SvPV(keysv, klen);
1368 keysv = sv_2mortal(newSVpvn(key,klen));
1369 (void)strupr(SvPVX(keysv));
1375 xhv = (XPVHV*)SvANY(hv);
1376 #ifndef DYNAMIC_ENV_FETCH
1377 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1381 keysave = key = SvPV(keysv, klen);
1382 is_utf8 = (SvUTF8(keysv) != 0);
1384 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1386 k_flags = HVhek_UTF8;
1388 k_flags |= HVhek_FREEKEY;
1391 PERL_HASH_INTERNAL(hash, key, klen);
1393 PERL_HASH(hash, key, klen);
1395 #ifdef DYNAMIC_ENV_FETCH
1396 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1399 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1400 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1401 for (; entry; entry = HeNEXT(entry)) {
1402 if (HeHASH(entry) != hash) /* strings can't be equal */
1404 if (HeKLEN(entry) != (I32)klen)
1406 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1408 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1410 if (k_flags & HVhek_FREEKEY)
1412 /* If we find the key, but the value is a placeholder, return false. */
1413 if (HeVAL(entry) == &PL_sv_placeholder)
1417 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1418 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1420 char *env = PerlEnv_ENVgetenv_len(key,&len);
1422 sv = newSVpvn(env,len);
1424 (void)hv_store_ent(hv,keysv,sv,hash);
1425 if (k_flags & HVhek_FREEKEY)
1431 if (k_flags & HVhek_FREEKEY)
1437 S_hsplit(pTHX_ HV *hv)
1439 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1440 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1441 register I32 newsize = oldsize * 2;
1443 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1447 register HE **oentry;
1448 int longest_chain = 0;
1452 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1453 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1459 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1464 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1465 if (oldsize >= 64) {
1466 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1467 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1470 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1474 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1475 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1476 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1479 for (i=0; i<oldsize; i++,aep++) {
1480 int left_length = 0;
1481 int right_length = 0;
1483 if (!*aep) /* non-existent */
1486 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1487 if ((HeHASH(entry) & newsize) != (U32)i) {
1488 *oentry = HeNEXT(entry);
1489 HeNEXT(entry) = *bep;
1491 xhv->xhv_fill++; /* HvFILL(hv)++ */
1497 oentry = &HeNEXT(entry);
1501 if (!*aep) /* everything moved */
1502 xhv->xhv_fill--; /* HvFILL(hv)-- */
1503 /* I think we don't actually need to keep track of the longest length,
1504 merely flag if anything is too long. But for the moment while
1505 developing this code I'll track it. */
1506 if (left_length > longest_chain)
1507 longest_chain = left_length;
1508 if (right_length > longest_chain)
1509 longest_chain = right_length;
1513 /* Pick your policy for "hashing isn't working" here: */
1514 if (longest_chain < 8 || longest_chain * 2 < HvTOTALKEYS(hv)
1519 if (hv == PL_strtab) {
1520 /* Urg. Someone is doing something nasty to the string table.
1525 /* Awooga. Awooga. Pathological data. */
1526 /*PerlIO_printf(PerlIO_stderr(), "Awooga %d of %d with %d/%d buckets\n",
1527 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1530 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1531 was_shared = HvSHAREKEYS(hv);
1534 HvSHAREKEYS_off(hv);
1538 aep = (HE **) xhv->xhv_array;
1540 for (i=0; i<newsize; i++,aep++) {
1543 /* We're going to trash this HE's next pointer when we chain it
1544 into the new hash below, so store where we go next. */
1545 HE *next = HeNEXT(entry);
1549 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1554 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1555 hash, HeKFLAGS(entry));
1556 unshare_hek (HeKEY_hek(entry));
1557 HeKEY_hek(entry) = new_hek;
1559 /* Not shared, so simply write the new hash in. */
1560 HeHASH(entry) = hash;
1562 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1563 HEK_REHASH_on(HeKEY_hek(entry));
1564 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1566 /* Copy oentry to the correct new chain. */
1567 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1569 xhv->xhv_fill++; /* HvFILL(hv)++ */
1570 HeNEXT(entry) = *bep;
1576 Safefree (xhv->xhv_array);
1577 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1581 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1583 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1584 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1585 register I32 newsize;
1591 register HE **oentry;
1593 newsize = (I32) newmax; /* possible truncation here */
1594 if (newsize != newmax || newmax <= oldsize)
1596 while ((newsize & (1 + ~newsize)) != newsize) {
1597 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1599 if (newsize < newmax)
1601 if (newsize < newmax)
1602 return; /* overflow detection */
1604 a = xhv->xhv_array; /* HvARRAY(hv) */
1607 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1608 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1614 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1619 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1620 if (oldsize >= 64) {
1621 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1622 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1625 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1628 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1631 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1633 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1634 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1635 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1639 for (i=0; i<oldsize; i++,aep++) {
1640 if (!*aep) /* non-existent */
1642 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1643 if ((j = (HeHASH(entry) & newsize)) != i) {
1645 *oentry = HeNEXT(entry);
1646 if (!(HeNEXT(entry) = aep[j]))
1647 xhv->xhv_fill++; /* HvFILL(hv)++ */
1652 oentry = &HeNEXT(entry);
1654 if (!*aep) /* everything moved */
1655 xhv->xhv_fill--; /* HvFILL(hv)-- */
1662 Creates a new HV. The reference count is set to 1.
1671 register XPVHV* xhv;
1673 hv = (HV*)NEWSV(502,0);
1674 sv_upgrade((SV *)hv, SVt_PVHV);
1675 xhv = (XPVHV*)SvANY(hv);
1678 #ifndef NODEFAULT_SHAREKEYS
1679 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1682 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1683 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1684 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1685 (void)hv_iterinit(hv); /* so each() will start off right */
1690 Perl_newHVhv(pTHX_ HV *ohv)
1693 STRLEN hv_max, hv_fill;
1695 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1697 hv_max = HvMAX(ohv);
1699 if (!SvMAGICAL((SV *)ohv)) {
1700 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1702 bool shared = !!HvSHAREKEYS(ohv);
1703 HE **ents, **oents = (HE **)HvARRAY(ohv);
1705 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1708 /* In each bucket... */
1709 for (i = 0; i <= hv_max; i++) {
1710 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1717 /* Copy the linked list of entries. */
1718 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1719 U32 hash = HeHASH(oent);
1720 char *key = HeKEY(oent);
1721 STRLEN len = HeKLEN(oent);
1722 int flags = HeKFLAGS(oent);
1725 HeVAL(ent) = newSVsv(HeVAL(oent));
1727 = shared ? share_hek_flags(key, len, hash, flags)
1728 : save_hek_flags(key, len, hash, flags);
1739 HvFILL(hv) = hv_fill;
1740 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1744 /* Iterate over ohv, copying keys and values one at a time. */
1746 I32 riter = HvRITER(ohv);
1747 HE *eiter = HvEITER(ohv);
1749 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1750 while (hv_max && hv_max + 1 >= hv_fill * 2)
1751 hv_max = hv_max / 2;
1755 while ((entry = hv_iternext_flags(ohv, 0))) {
1756 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1757 newSVsv(HeVAL(entry)), HeHASH(entry),
1760 HvRITER(ohv) = riter;
1761 HvEITER(ohv) = eiter;
1768 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1775 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1776 PL_sub_generation++; /* may be deletion of method from stash */
1778 if (HeKLEN(entry) == HEf_SVKEY) {
1779 SvREFCNT_dec(HeKEY_sv(entry));
1780 Safefree(HeKEY_hek(entry));
1782 else if (HvSHAREKEYS(hv))
1783 unshare_hek(HeKEY_hek(entry));
1785 Safefree(HeKEY_hek(entry));
1790 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1794 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1795 PL_sub_generation++; /* may be deletion of method from stash */
1796 sv_2mortal(HeVAL(entry)); /* free between statements */
1797 if (HeKLEN(entry) == HEf_SVKEY) {
1798 sv_2mortal(HeKEY_sv(entry));
1799 Safefree(HeKEY_hek(entry));
1801 else if (HvSHAREKEYS(hv))
1802 unshare_hek(HeKEY_hek(entry));
1804 Safefree(HeKEY_hek(entry));
1809 =for apidoc hv_clear
1811 Clears a hash, making it empty.
1817 Perl_hv_clear(pTHX_ HV *hv)
1819 register XPVHV* xhv;
1823 xhv = (XPVHV*)SvANY(hv);
1825 if (SvREADONLY(hv)) {
1826 /* restricted hash: convert all keys to placeholders */
1829 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1830 entry = ((HE**)xhv->xhv_array)[i];
1831 for (; entry; entry = HeNEXT(entry)) {
1832 /* not already placeholder */
1833 if (HeVAL(entry) != &PL_sv_placeholder) {
1834 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1835 SV* keysv = hv_iterkeysv(entry);
1837 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1840 SvREFCNT_dec(HeVAL(entry));
1841 HeVAL(entry) = &PL_sv_placeholder;
1842 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1850 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1851 if (xhv->xhv_array /* HvARRAY(hv) */)
1852 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1853 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1858 HvHASKFLAGS_off(hv);
1863 S_hfreeentries(pTHX_ HV *hv)
1865 register HE **array;
1867 register HE *oentry = Null(HE*);
1878 array = HvARRAY(hv);
1879 /* make everyone else think the array is empty, so that the destructors
1880 * called for freed entries can't recusively mess with us */
1881 HvARRAY(hv) = Null(HE**);
1883 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1889 entry = HeNEXT(entry);
1890 hv_free_ent(hv, oentry);
1895 entry = array[riter];
1898 HvARRAY(hv) = array;
1899 (void)hv_iterinit(hv);
1903 =for apidoc hv_undef
1911 Perl_hv_undef(pTHX_ HV *hv)
1913 register XPVHV* xhv;
1916 xhv = (XPVHV*)SvANY(hv);
1918 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1921 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1922 Safefree(HvNAME(hv));
1925 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1926 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1927 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1934 =for apidoc hv_iterinit
1936 Prepares a starting point to traverse a hash table. Returns the number of
1937 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1938 currently only meaningful for hashes without tie magic.
1940 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1941 hash buckets that happen to be in use. If you still need that esoteric
1942 value, you can get it through the macro C<HvFILL(tb)>.
1949 Perl_hv_iterinit(pTHX_ HV *hv)
1951 register XPVHV* xhv;
1955 Perl_croak(aTHX_ "Bad hash");
1956 xhv = (XPVHV*)SvANY(hv);
1957 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1958 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1960 hv_free_ent(hv, entry);
1962 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1963 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1964 /* used to be xhv->xhv_fill before 5.004_65 */
1965 return XHvTOTALKEYS(xhv);
1968 =for apidoc hv_iternext
1970 Returns entries from a hash iterator. See C<hv_iterinit>.
1972 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1973 iterator currently points to, without losing your place or invalidating your
1974 iterator. Note that in this case the current entry is deleted from the hash
1975 with your iterator holding the last reference to it. Your iterator is flagged
1976 to free the entry on the next call to C<hv_iternext>, so you must not discard
1977 your iterator immediately else the entry will leak - call C<hv_iternext> to
1978 trigger the resource deallocation.
1984 Perl_hv_iternext(pTHX_ HV *hv)
1986 return hv_iternext_flags(hv, 0);
1990 =for apidoc hv_iternext_flags
1992 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1993 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1994 set the placeholders keys (for restricted hashes) will be returned in addition
1995 to normal keys. By default placeholders are automatically skipped over.
1996 Currently a placeholder is implemented with a value that is
1997 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1998 restricted hashes may change, and the implementation currently is
1999 insufficiently abstracted for any change to be tidy.
2005 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2007 register XPVHV* xhv;
2013 Perl_croak(aTHX_ "Bad hash");
2014 xhv = (XPVHV*)SvANY(hv);
2015 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
2017 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
2018 SV *key = sv_newmortal();
2020 sv_setsv(key, HeSVKEY_force(entry));
2021 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
2027 /* one HE per MAGICAL hash */
2028 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
2030 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
2032 HeKEY_hek(entry) = hek;
2033 HeKLEN(entry) = HEf_SVKEY;
2035 magic_nextpack((SV*) hv,mg,key);
2037 /* force key to stay around until next time */
2038 HeSVKEY_set(entry, SvREFCNT_inc(key));
2039 return entry; /* beware, hent_val is not set */
2042 SvREFCNT_dec(HeVAL(entry));
2043 Safefree(HeKEY_hek(entry));
2045 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
2048 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
2049 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
2053 if (!xhv->xhv_array /* !HvARRAY(hv) */)
2054 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
2055 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
2057 /* At start of hash, entry is NULL. */
2060 entry = HeNEXT(entry);
2061 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2063 * Skip past any placeholders -- don't want to include them in
2066 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
2067 entry = HeNEXT(entry);
2072 /* OK. Come to the end of the current list. Grab the next one. */
2074 xhv->xhv_riter++; /* HvRITER(hv)++ */
2075 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2076 /* There is no next one. End of the hash. */
2077 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
2080 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
2081 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
2083 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2084 /* If we have an entry, but it's a placeholder, don't count it.
2086 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2087 entry = HeNEXT(entry);
2089 /* Will loop again if this linked list starts NULL
2090 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2091 or if we run through it and find only placeholders. */
2094 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2096 hv_free_ent(hv, oldentry);
2099 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
2104 =for apidoc hv_iterkey
2106 Returns the key from the current position of the hash iterator. See
2113 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
2115 if (HeKLEN(entry) == HEf_SVKEY) {
2117 char *p = SvPV(HeKEY_sv(entry), len);
2122 *retlen = HeKLEN(entry);
2123 return HeKEY(entry);
2127 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2129 =for apidoc hv_iterkeysv
2131 Returns the key as an C<SV*> from the current position of the hash
2132 iterator. The return value will always be a mortal copy of the key. Also
2139 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2141 if (HeKLEN(entry) != HEf_SVKEY) {
2142 HEK *hek = HeKEY_hek(entry);
2143 int flags = HEK_FLAGS(hek);
2146 if (flags & HVhek_WASUTF8) {
2148 Andreas would like keys he put in as utf8 to come back as utf8
2150 STRLEN utf8_len = HEK_LEN(hek);
2151 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
2153 sv = newSVpvn ((char*)as_utf8, utf8_len);
2155 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
2156 } else if (flags & HVhek_REHASH) {
2157 /* We don't have a pointer to the hv, so we have to replicate the
2158 flag into every HEK. This hv is using custom a hasing
2159 algorithm. Hence we can't return a shared string scalar, as
2160 that would contain the (wrong) hash value, and might get passed
2161 into an hv routine with a regular hash */
2163 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
2167 sv = newSVpvn_share(HEK_KEY(hek),
2168 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
2171 return sv_2mortal(sv);
2173 return sv_mortalcopy(HeKEY_sv(entry));
2177 =for apidoc hv_iterval
2179 Returns the value from the current position of the hash iterator. See
2186 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2188 if (SvRMAGICAL(hv)) {
2189 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2190 SV* sv = sv_newmortal();
2191 if (HeKLEN(entry) == HEf_SVKEY)
2192 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2193 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2197 return HeVAL(entry);
2201 =for apidoc hv_iternextsv
2203 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2210 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2213 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
2215 *key = hv_iterkey(he, retlen);
2216 return hv_iterval(hv, he);
2220 =for apidoc hv_magic
2222 Adds magic to a hash. See C<sv_magic>.
2228 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2230 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2233 #if 0 /* use the macro from hv.h instead */
2236 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2238 return HEK_KEY(share_hek(sv, len, hash));
2243 /* possibly free a shared string if no one has access to it
2244 * len and hash must both be valid for str.
2247 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2249 unshare_hek_or_pvn (NULL, str, len, hash);
2254 Perl_unshare_hek(pTHX_ HEK *hek)
2256 unshare_hek_or_pvn(hek, NULL, 0, 0);
2259 /* possibly free a shared string if no one has access to it
2260 hek if non-NULL takes priority over the other 3, else str, len and hash
2261 are used. If so, len and hash must both be valid for str.
2264 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2266 register XPVHV* xhv;
2268 register HE **oentry;
2271 bool is_utf8 = FALSE;
2273 const char *save = str;
2276 hash = HEK_HASH(hek);
2277 } else if (len < 0) {
2278 STRLEN tmplen = -len;
2280 /* See the note in hv_fetch(). --jhi */
2281 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2284 k_flags = HVhek_UTF8;
2286 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2289 /* what follows is the moral equivalent of:
2290 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2291 if (--*Svp == Nullsv)
2292 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2294 xhv = (XPVHV*)SvANY(PL_strtab);
2295 /* assert(xhv_array != 0) */
2297 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2298 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2300 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2301 if (HeKEY_hek(entry) != hek)
2307 int flags_masked = k_flags & HVhek_MASK;
2308 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2309 if (HeHASH(entry) != hash) /* strings can't be equal */
2311 if (HeKLEN(entry) != len)
2313 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2315 if (HeKFLAGS(entry) != flags_masked)
2323 if (--HeVAL(entry) == Nullsv) {
2324 *oentry = HeNEXT(entry);
2326 xhv->xhv_fill--; /* HvFILL(hv)-- */
2327 Safefree(HeKEY_hek(entry));
2329 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2333 UNLOCK_STRTAB_MUTEX;
2334 if (!found && ckWARN_d(WARN_INTERNAL))
2335 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2336 "Attempt to free non-existent shared string '%s'%s",
2337 hek ? HEK_KEY(hek) : str,
2338 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2339 if (k_flags & HVhek_FREEKEY)
2343 /* get a (constant) string ptr from the global string table
2344 * string will get added if it is not already there.
2345 * len and hash must both be valid for str.
2348 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2350 bool is_utf8 = FALSE;
2352 const char *save = str;
2355 STRLEN tmplen = -len;
2357 /* See the note in hv_fetch(). --jhi */
2358 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2360 /* If we were able to downgrade here, then than means that we were passed
2361 in a key which only had chars 0-255, but was utf8 encoded. */
2364 /* If we found we were able to downgrade the string to bytes, then
2365 we should flag that it needs upgrading on keys or each. Also flag
2366 that we need share_hek_flags to free the string. */
2368 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2371 return share_hek_flags (str, len, hash, flags);
2375 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2377 register XPVHV* xhv;
2379 register HE **oentry;
2382 int flags_masked = flags & HVhek_MASK;
2384 /* what follows is the moral equivalent of:
2386 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2387 hv_store(PL_strtab, str, len, Nullsv, hash);
2389 xhv = (XPVHV*)SvANY(PL_strtab);
2390 /* assert(xhv_array != 0) */
2392 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2393 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2394 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2395 if (HeHASH(entry) != hash) /* strings can't be equal */
2397 if (HeKLEN(entry) != len)
2399 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2401 if (HeKFLAGS(entry) != flags_masked)
2408 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2409 HeVAL(entry) = Nullsv;
2410 HeNEXT(entry) = *oentry;
2412 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2413 if (i) { /* initial entry? */
2414 xhv->xhv_fill++; /* HvFILL(hv)++ */
2415 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2420 ++HeVAL(entry); /* use value slot as REFCNT */
2421 UNLOCK_STRTAB_MUTEX;
2423 if (flags & HVhek_FREEKEY)
2426 return HeKEY_hek(entry);