3 * Copyright (c) 1991-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "I sit beside the fire and think of all that I have seen." --Bilbo
15 =head1 Hash Manipulation Functions
30 PL_he_root = HeNEXT(he);
39 HeNEXT(p) = (HE*)PL_he_root;
50 New(54, ptr, 1008/sizeof(XPV), XPV);
51 ptr->xpv_pv = (char*)PL_he_arenaroot;
52 PL_he_arenaroot = ptr;
55 heend = &he[1008 / sizeof(HE) - 1];
58 HeNEXT(he) = (HE*)(he + 1);
66 #define new_HE() (HE*)safemalloc(sizeof(HE))
67 #define del_HE(p) safefree((char*)p)
71 #define new_HE() new_he()
72 #define del_HE(p) del_he(p)
77 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
82 New(54, k, HEK_BASESIZE + len + 2, char);
84 Copy(str, HEK_KEY(hek), len, char);
85 HEK_KEY(hek)[len] = 0;
88 HEK_FLAGS(hek) = (unsigned char)flags;
92 #if defined(USE_ITHREADS)
94 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
100 /* look for it in the table first */
101 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
105 /* create anew and remember what it is */
107 ptr_table_store(PL_ptr_table, e, ret);
109 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
110 if (HeKLEN(e) == HEf_SVKEY)
111 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
113 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
116 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
118 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
121 #endif /* USE_ITHREADS */
124 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
127 SV *sv = sv_newmortal(), *esv = sv_newmortal();
128 if (!(flags & HVhek_FREEKEY)) {
129 sv_setpvn(sv, key, klen);
132 /* Need to free saved eventually assign to mortal SV */
133 SV *sv = sv_newmortal();
134 sv_usepvn(sv, (char *) key, klen);
136 if (flags & HVhek_UTF8) {
139 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
140 Perl_croak(aTHX_ SvPVX(esv), sv);
143 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
149 Returns the SV which corresponds to the specified key in the hash. The
150 C<klen> is the length of the key. If C<lval> is set then the fetch will be
151 part of a store. Check that the return value is non-null before
152 dereferencing it to an C<SV*>.
154 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
155 information on how to use this function on tied hashes.
162 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
164 bool is_utf8 = FALSE;
165 const char *keysave = key;
174 STRLEN tmplen = klen;
175 /* Just casting the &klen to (STRLEN) won't work well
176 * if STRLEN and I32 are of different widths. --jhi */
177 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
179 /* If we were able to downgrade here, then than means that we were
180 passed in a key which only had chars 0-255, but was utf8 encoded. */
183 /* If we found we were able to downgrade the string to bytes, then
184 we should flag that it needs upgrading on keys or each. */
186 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
189 return hv_fetch_flags (hv, key, klen, lval, flags);
193 S_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen, I32 lval, int flags)
203 if (SvRMAGICAL(hv)) {
204 /* All this clause seems to be utf8 unaware.
205 By moving the utf8 stuff out to hv_fetch_flags I need to ensure
206 key doesn't leak. I've not tried solving the utf8-ness.
209 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
211 mg_copy((SV*)hv, sv, key, klen);
212 if (flags & HVhek_FREEKEY)
215 return &PL_hv_fetch_sv;
217 #ifdef ENV_IS_CASELESS
218 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
220 for (i = 0; i < klen; ++i)
221 if (isLOWER(key[i])) {
222 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
223 SV **ret = hv_fetch(hv, nkey, klen, 0);
225 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
227 } else if (flags & HVhek_FREEKEY)
235 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
236 avoid unnecessary pointer dereferencing. */
237 xhv = (XPVHV*)SvANY(hv);
238 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
240 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
241 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
244 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
245 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
248 if (flags & HVhek_FREEKEY)
254 PERL_HASH(hash, key, klen);
256 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
257 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
258 for (; entry; entry = HeNEXT(entry)) {
259 if (HeHASH(entry) != hash) /* strings can't be equal */
261 if (HeKLEN(entry) != klen)
263 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
265 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
266 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
267 xor is true if bits differ, in which case this isn't a match. */
268 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
270 if (lval && HeKFLAGS(entry) != flags) {
271 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
272 But if entry was set previously with HVhek_WASUTF8 and key now
273 doesn't (or vice versa) then we should change the key's flag,
274 as this is assignment. */
275 if (HvSHAREKEYS(hv)) {
276 /* Need to swap the key we have for a key with the flags we
277 need. As keys are shared we can't just write to the flag,
278 so we share the new one, unshare the old one. */
279 int flags_nofree = flags & ~HVhek_FREEKEY;
280 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
281 unshare_hek (HeKEY_hek(entry));
282 HeKEY_hek(entry) = new_hek;
285 HeKFLAGS(entry) = flags;
287 if (flags & HVhek_FREEKEY)
289 /* if we find a placeholder, we pretend we haven't found anything */
290 if (HeVAL(entry) == &PL_sv_undef)
292 return &HeVAL(entry);
295 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
296 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
298 char *env = PerlEnv_ENVgetenv_len(key,&len);
300 sv = newSVpvn(env,len);
302 if (flags & HVhek_FREEKEY)
304 return hv_store(hv,key,klen,sv,hash);
308 if (!entry && SvREADONLY(hv)) {
309 S_hv_notallowed(aTHX_ flags, key, klen,
310 "access disallowed key '%"SVf"' in"
313 if (lval) { /* gonna assign to this, so it better be there */
315 return hv_store_flags(hv,key,klen,sv,hash,flags);
317 if (flags & HVhek_FREEKEY)
322 /* returns an HE * structure with the all fields set */
323 /* note that hent_val will be a mortal sv for MAGICAL hashes */
325 =for apidoc hv_fetch_ent
327 Returns the hash entry which corresponds to the specified key in the hash.
328 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
329 if you want the function to compute it. IF C<lval> is set then the fetch
330 will be part of a store. Make sure the return value is non-null before
331 accessing it. The return value when C<tb> is a tied hash is a pointer to a
332 static location, so be sure to make a copy of the structure if you need to
335 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
336 information on how to use this function on tied hashes.
342 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
356 if (SvRMAGICAL(hv)) {
357 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
359 keysv = sv_2mortal(newSVsv(keysv));
360 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
361 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
363 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
364 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
366 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
367 HeVAL(&PL_hv_fetch_ent_mh) = sv;
368 return &PL_hv_fetch_ent_mh;
370 #ifdef ENV_IS_CASELESS
371 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
373 key = SvPV(keysv, klen);
374 for (i = 0; i < klen; ++i)
375 if (isLOWER(key[i])) {
376 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
377 (void)strupr(SvPVX(nkeysv));
378 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
380 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
387 xhv = (XPVHV*)SvANY(hv);
388 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
390 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
391 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
394 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
395 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
401 keysave = key = SvPV(keysv, klen);
402 is_utf8 = (SvUTF8(keysv)!=0);
405 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
409 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
413 PERL_HASH(hash, key, klen);
415 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
416 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
417 for (; entry; entry = HeNEXT(entry)) {
418 if (HeHASH(entry) != hash) /* strings can't be equal */
420 if (HeKLEN(entry) != klen)
422 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
424 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
426 if (lval && HeKFLAGS(entry) != flags) {
427 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
428 But if entry was set previously with HVhek_WASUTF8 and key now
429 doesn't (or vice versa) then we should change the key's flag,
430 as this is assignment. */
431 if (HvSHAREKEYS(hv)) {
432 /* Need to swap the key we have for a key with the flags we
433 need. As keys are shared we can't just write to the flag,
434 so we share the new one, unshare the old one. */
435 int flags_nofree = flags & ~HVhek_FREEKEY;
436 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
437 unshare_hek (HeKEY_hek(entry));
438 HeKEY_hek(entry) = new_hek;
441 HeKFLAGS(entry) = flags;
445 /* if we find a placeholder, we pretend we haven't found anything */
446 if (HeVAL(entry) == &PL_sv_undef)
450 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
451 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
453 char *env = PerlEnv_ENVgetenv_len(key,&len);
455 sv = newSVpvn(env,len);
457 return hv_store_ent(hv,keysv,sv,hash);
461 if (!entry && SvREADONLY(hv)) {
462 S_hv_notallowed(aTHX_ flags, key, klen,
463 "access disallowed key '%"SVf"' in"
466 if (flags & HVhek_FREEKEY)
468 if (lval) { /* gonna assign to this, so it better be there */
470 return hv_store_ent(hv,keysv,sv,hash);
476 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
478 MAGIC *mg = SvMAGIC(hv);
482 if (isUPPER(mg->mg_type)) {
484 switch (mg->mg_type) {
485 case PERL_MAGIC_tied:
487 *needs_store = FALSE;
490 mg = mg->mg_moremagic;
497 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
498 the length of the key. The C<hash> parameter is the precomputed hash
499 value; if it is zero then Perl will compute it. The return value will be
500 NULL if the operation failed or if the value did not need to be actually
501 stored within the hash (as in the case of tied hashes). Otherwise it can
502 be dereferenced to get the original C<SV*>. Note that the caller is
503 responsible for suitably incrementing the reference count of C<val> before
504 the call, and decrementing it if the function returned NULL.
506 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
507 information on how to use this function on tied hashes.
513 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
515 bool is_utf8 = FALSE;
516 const char *keysave = key;
520 STRLEN tmplen = klen;
521 /* Just casting the &klen to (STRLEN) won't work well
522 * if STRLEN and I32 are of different widths. --jhi */
523 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
525 /* If we were able to downgrade here, then than means that we were
526 passed in a key which only had chars 0-255, but was utf8 encoded. */
529 /* If we found we were able to downgrade the string to bytes, then
530 we should flag that it needs upgrading on keys or each. */
532 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
535 return hv_store_flags (hv, key, klen, val, hash, flags);
539 S_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
540 register U32 hash, int flags)
545 register HE **oentry;
550 xhv = (XPVHV*)SvANY(hv);
554 hv_magic_check (hv, &needs_copy, &needs_store);
556 mg_copy((SV*)hv, val, key, klen);
557 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
558 if (flags & HVhek_FREEKEY)
562 #ifdef ENV_IS_CASELESS
563 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
564 key = savepvn(key,klen);
565 key = (const char*)strupr((char*)key);
573 HvHASKFLAGS_on((SV*)hv);
576 PERL_HASH(hash, key, klen);
578 if (!xhv->xhv_array /* !HvARRAY(hv) */)
579 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
580 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
583 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
584 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
587 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
588 if (HeHASH(entry) != hash) /* strings can't be equal */
590 if (HeKLEN(entry) != klen)
592 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
594 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
596 if (HeVAL(entry) == &PL_sv_undef)
597 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
599 SvREFCNT_dec(HeVAL(entry));
602 if (HeKFLAGS(entry) != flags) {
603 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
604 But if entry was set previously with HVhek_WASUTF8 and key now
605 doesn't (or vice versa) then we should change the key's flag,
606 as this is assignment. */
607 if (HvSHAREKEYS(hv)) {
608 /* Need to swap the key we have for a key with the flags we
609 need. As keys are shared we can't just write to the flag,
610 so we share the new one, unshare the old one. */
611 int flags_nofree = flags & ~HVhek_FREEKEY;
612 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
613 unshare_hek (HeKEY_hek(entry));
614 HeKEY_hek(entry) = new_hek;
617 HeKFLAGS(entry) = flags;
619 if (flags & HVhek_FREEKEY)
621 return &HeVAL(entry);
624 if (SvREADONLY(hv)) {
625 S_hv_notallowed(aTHX_ flags, key, klen,
626 "access disallowed key '%"SVf"' to"
631 /* share_hek_flags will do the free for us. This might be considered
634 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
635 else /* gotta do the real thing */
636 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
638 HeNEXT(entry) = *oentry;
641 xhv->xhv_keys++; /* HvKEYS(hv)++ */
642 if (i) { /* initial entry? */
643 xhv->xhv_fill++; /* HvFILL(hv)++ */
644 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
648 return &HeVAL(entry);
652 =for apidoc hv_store_ent
654 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
655 parameter is the precomputed hash value; if it is zero then Perl will
656 compute it. The return value is the new hash entry so created. It will be
657 NULL if the operation failed or if the value did not need to be actually
658 stored within the hash (as in the case of tied hashes). Otherwise the
659 contents of the return value can be accessed using the C<He?> macros
660 described here. Note that the caller is responsible for suitably
661 incrementing the reference count of C<val> before the call, and
662 decrementing it if the function returned NULL.
664 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
665 information on how to use this function on tied hashes.
671 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
686 xhv = (XPVHV*)SvANY(hv);
690 hv_magic_check (hv, &needs_copy, &needs_store);
692 bool save_taint = PL_tainted;
694 PL_tainted = SvTAINTED(keysv);
695 keysv = sv_2mortal(newSVsv(keysv));
696 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
697 TAINT_IF(save_taint);
698 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
700 #ifdef ENV_IS_CASELESS
701 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
702 key = SvPV(keysv, klen);
703 keysv = sv_2mortal(newSVpvn(key,klen));
704 (void)strupr(SvPVX(keysv));
711 keysave = key = SvPV(keysv, klen);
712 is_utf8 = (SvUTF8(keysv) != 0);
715 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
719 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
720 HvHASKFLAGS_on((SV*)hv);
724 PERL_HASH(hash, key, klen);
726 if (!xhv->xhv_array /* !HvARRAY(hv) */)
727 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
728 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
731 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
732 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
735 for (; entry; i=0, entry = HeNEXT(entry)) {
736 if (HeHASH(entry) != hash) /* strings can't be equal */
738 if (HeKLEN(entry) != klen)
740 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
742 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
744 if (HeVAL(entry) == &PL_sv_undef)
745 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
747 SvREFCNT_dec(HeVAL(entry));
749 if (HeKFLAGS(entry) != flags) {
750 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
751 But if entry was set previously with HVhek_WASUTF8 and key now
752 doesn't (or vice versa) then we should change the key's flag,
753 as this is assignment. */
754 if (HvSHAREKEYS(hv)) {
755 /* Need to swap the key we have for a key with the flags we
756 need. As keys are shared we can't just write to the flag,
757 so we share the new one, unshare the old one. */
758 int flags_nofree = flags & ~HVhek_FREEKEY;
759 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
760 unshare_hek (HeKEY_hek(entry));
761 HeKEY_hek(entry) = new_hek;
764 HeKFLAGS(entry) = flags;
766 if (flags & HVhek_FREEKEY)
771 if (SvREADONLY(hv)) {
772 S_hv_notallowed(aTHX_ flags, key, klen,
773 "access disallowed key '%"SVf"' to"
778 /* share_hek_flags will do the free for us. This might be considered
781 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
782 else /* gotta do the real thing */
783 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
785 HeNEXT(entry) = *oentry;
788 xhv->xhv_keys++; /* HvKEYS(hv)++ */
789 if (i) { /* initial entry? */
790 xhv->xhv_fill++; /* HvFILL(hv)++ */
791 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
799 =for apidoc hv_delete
801 Deletes a key/value pair in the hash. The value SV is removed from the
802 hash and returned to the caller. The C<klen> is the length of the key.
803 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
810 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
816 register HE **oentry;
819 bool is_utf8 = FALSE;
821 const char *keysave = key;
829 if (SvRMAGICAL(hv)) {
832 hv_magic_check (hv, &needs_copy, &needs_store);
834 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
838 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
839 /* No longer an element */
840 sv_unmagic(sv, PERL_MAGIC_tiedelem);
843 return Nullsv; /* element cannot be deleted */
845 #ifdef ENV_IS_CASELESS
846 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
847 sv = sv_2mortal(newSVpvn(key,klen));
848 key = strupr(SvPVX(sv));
853 xhv = (XPVHV*)SvANY(hv);
854 if (!xhv->xhv_array /* !HvARRAY(hv) */)
858 STRLEN tmplen = klen;
859 /* See the note in hv_fetch(). --jhi */
860 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
863 k_flags = HVhek_UTF8;
865 k_flags |= HVhek_FREEKEY;
868 PERL_HASH(hash, key, klen);
870 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
871 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
874 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
875 if (HeHASH(entry) != hash) /* strings can't be equal */
877 if (HeKLEN(entry) != klen)
879 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
881 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
883 if (k_flags & HVhek_FREEKEY)
885 /* if placeholder is here, it's already been deleted.... */
886 if (HeVAL(entry) == &PL_sv_undef)
889 return Nullsv; /* if still SvREADONLY, leave it deleted. */
891 /* okay, really delete the placeholder... */
892 *oentry = HeNEXT(entry);
894 xhv->xhv_fill--; /* HvFILL(hv)-- */
895 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
898 hv_free_ent(hv, entry);
899 xhv->xhv_keys--; /* HvKEYS(hv)-- */
900 if (xhv->xhv_keys == 0)
902 xhv->xhv_placeholders--;
906 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
907 S_hv_notallowed(aTHX_ k_flags, key, klen,
908 "delete readonly key '%"SVf"' from"
912 if (flags & G_DISCARD)
915 sv = sv_2mortal(HeVAL(entry));
916 HeVAL(entry) = &PL_sv_undef;
920 * If a restricted hash, rather than really deleting the entry, put
921 * a placeholder there. This marks the key as being "approved", so
922 * we can still access via not-really-existing key without raising
925 if (SvREADONLY(hv)) {
926 HeVAL(entry) = &PL_sv_undef;
927 /* We'll be saving this slot, so the number of allocated keys
928 * doesn't go down, but the number placeholders goes up */
929 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
931 *oentry = HeNEXT(entry);
933 xhv->xhv_fill--; /* HvFILL(hv)-- */
934 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
937 hv_free_ent(hv, entry);
938 xhv->xhv_keys--; /* HvKEYS(hv)-- */
939 if (xhv->xhv_keys == 0)
944 if (SvREADONLY(hv)) {
945 S_hv_notallowed(aTHX_ k_flags, key, klen,
946 "access disallowed key '%"SVf"' from"
950 if (k_flags & HVhek_FREEKEY)
956 =for apidoc hv_delete_ent
958 Deletes a key/value pair in the hash. The value SV is removed from the
959 hash and returned to the caller. The C<flags> value will normally be zero;
960 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
961 precomputed hash value, or 0 to ask for it to be computed.
967 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
974 register HE **oentry;
982 if (SvRMAGICAL(hv)) {
985 hv_magic_check (hv, &needs_copy, &needs_store);
987 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
991 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
992 /* No longer an element */
993 sv_unmagic(sv, PERL_MAGIC_tiedelem);
996 return Nullsv; /* element cannot be deleted */
998 #ifdef ENV_IS_CASELESS
999 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1000 key = SvPV(keysv, klen);
1001 keysv = sv_2mortal(newSVpvn(key,klen));
1002 (void)strupr(SvPVX(keysv));
1008 xhv = (XPVHV*)SvANY(hv);
1009 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1012 keysave = key = SvPV(keysv, klen);
1013 is_utf8 = (SvUTF8(keysv) != 0);
1016 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1018 k_flags = HVhek_UTF8;
1020 k_flags |= HVhek_FREEKEY;
1024 PERL_HASH(hash, key, klen);
1026 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1027 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1030 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1031 if (HeHASH(entry) != hash) /* strings can't be equal */
1033 if (HeKLEN(entry) != klen)
1035 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1037 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1039 if (k_flags & HVhek_FREEKEY)
1042 /* if placeholder is here, it's already been deleted.... */
1043 if (HeVAL(entry) == &PL_sv_undef)
1046 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1048 /* okay, really delete the placeholder. */
1049 *oentry = HeNEXT(entry);
1051 xhv->xhv_fill--; /* HvFILL(hv)-- */
1052 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1055 hv_free_ent(hv, entry);
1056 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1057 if (xhv->xhv_keys == 0)
1058 HvHASKFLAGS_off(hv);
1059 xhv->xhv_placeholders--;
1062 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1063 S_hv_notallowed(aTHX_ k_flags, key, klen,
1064 "delete readonly key '%"SVf"' from"
1068 if (flags & G_DISCARD)
1071 sv = sv_2mortal(HeVAL(entry));
1072 HeVAL(entry) = &PL_sv_undef;
1076 * If a restricted hash, rather than really deleting the entry, put
1077 * a placeholder there. This marks the key as being "approved", so
1078 * we can still access via not-really-existing key without raising
1081 if (SvREADONLY(hv)) {
1082 HeVAL(entry) = &PL_sv_undef;
1083 /* We'll be saving this slot, so the number of allocated keys
1084 * doesn't go down, but the number placeholders goes up */
1085 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1087 *oentry = HeNEXT(entry);
1089 xhv->xhv_fill--; /* HvFILL(hv)-- */
1090 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1093 hv_free_ent(hv, entry);
1094 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1095 if (xhv->xhv_keys == 0)
1096 HvHASKFLAGS_off(hv);
1100 if (SvREADONLY(hv)) {
1101 S_hv_notallowed(aTHX_ k_flags, key, klen,
1102 "delete disallowed key '%"SVf"' from"
1106 if (k_flags & HVhek_FREEKEY)
1112 =for apidoc hv_exists
1114 Returns a boolean indicating whether the specified hash key exists. The
1115 C<klen> is the length of the key.
1121 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1123 register XPVHV* xhv;
1127 bool is_utf8 = FALSE;
1128 const char *keysave = key;
1139 if (SvRMAGICAL(hv)) {
1140 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1141 sv = sv_newmortal();
1142 mg_copy((SV*)hv, sv, key, klen);
1143 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1146 #ifdef ENV_IS_CASELESS
1147 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1148 sv = sv_2mortal(newSVpvn(key,klen));
1149 key = strupr(SvPVX(sv));
1154 xhv = (XPVHV*)SvANY(hv);
1155 #ifndef DYNAMIC_ENV_FETCH
1156 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1161 STRLEN tmplen = klen;
1162 /* See the note in hv_fetch(). --jhi */
1163 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1166 k_flags = HVhek_UTF8;
1168 k_flags |= HVhek_FREEKEY;
1171 PERL_HASH(hash, key, klen);
1173 #ifdef DYNAMIC_ENV_FETCH
1174 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1177 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1178 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1179 for (; entry; entry = HeNEXT(entry)) {
1180 if (HeHASH(entry) != hash) /* strings can't be equal */
1182 if (HeKLEN(entry) != klen)
1184 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1186 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1188 if (k_flags & HVhek_FREEKEY)
1190 /* If we find the key, but the value is a placeholder, return false. */
1191 if (HeVAL(entry) == &PL_sv_undef)
1196 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1197 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1199 char *env = PerlEnv_ENVgetenv_len(key,&len);
1201 sv = newSVpvn(env,len);
1203 (void)hv_store(hv,key,klen,sv,hash);
1204 if (k_flags & HVhek_FREEKEY)
1210 if (k_flags & HVhek_FREEKEY)
1217 =for apidoc hv_exists_ent
1219 Returns a boolean indicating whether the specified hash key exists. C<hash>
1220 can be a valid precomputed hash value, or 0 to ask for it to be
1227 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1229 register XPVHV* xhv;
1241 if (SvRMAGICAL(hv)) {
1242 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1243 SV* svret = sv_newmortal();
1244 sv = sv_newmortal();
1245 keysv = sv_2mortal(newSVsv(keysv));
1246 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1247 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1248 return SvTRUE(svret);
1250 #ifdef ENV_IS_CASELESS
1251 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1252 key = SvPV(keysv, klen);
1253 keysv = sv_2mortal(newSVpvn(key,klen));
1254 (void)strupr(SvPVX(keysv));
1260 xhv = (XPVHV*)SvANY(hv);
1261 #ifndef DYNAMIC_ENV_FETCH
1262 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1266 keysave = key = SvPV(keysv, klen);
1267 is_utf8 = (SvUTF8(keysv) != 0);
1269 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1271 k_flags = HVhek_UTF8;
1273 k_flags |= HVhek_FREEKEY;
1276 PERL_HASH(hash, key, klen);
1278 #ifdef DYNAMIC_ENV_FETCH
1279 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1282 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1283 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1284 for (; entry; entry = HeNEXT(entry)) {
1285 if (HeHASH(entry) != hash) /* strings can't be equal */
1287 if (HeKLEN(entry) != klen)
1289 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1291 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1293 if (k_flags & HVhek_FREEKEY)
1295 /* If we find the key, but the value is a placeholder, return false. */
1296 if (HeVAL(entry) == &PL_sv_undef)
1300 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1301 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1303 char *env = PerlEnv_ENVgetenv_len(key,&len);
1305 sv = newSVpvn(env,len);
1307 (void)hv_store_ent(hv,keysv,sv,hash);
1308 if (k_flags & HVhek_FREEKEY)
1314 if (k_flags & HVhek_FREEKEY)
1320 S_hsplit(pTHX_ HV *hv)
1322 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1323 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1324 register I32 newsize = oldsize * 2;
1326 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1330 register HE **oentry;
1333 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1334 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1340 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1345 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1346 if (oldsize >= 64) {
1347 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1348 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1351 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1355 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1356 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1357 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1360 for (i=0; i<oldsize; i++,aep++) {
1361 if (!*aep) /* non-existent */
1364 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1365 if ((HeHASH(entry) & newsize) != i) {
1366 *oentry = HeNEXT(entry);
1367 HeNEXT(entry) = *bep;
1369 xhv->xhv_fill++; /* HvFILL(hv)++ */
1374 oentry = &HeNEXT(entry);
1376 if (!*aep) /* everything moved */
1377 xhv->xhv_fill--; /* HvFILL(hv)-- */
1382 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1384 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1385 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1386 register I32 newsize;
1392 register HE **oentry;
1394 newsize = (I32) newmax; /* possible truncation here */
1395 if (newsize != newmax || newmax <= oldsize)
1397 while ((newsize & (1 + ~newsize)) != newsize) {
1398 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1400 if (newsize < newmax)
1402 if (newsize < newmax)
1403 return; /* overflow detection */
1405 a = xhv->xhv_array; /* HvARRAY(hv) */
1408 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1409 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1415 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1420 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1421 if (oldsize >= 64) {
1422 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1423 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1426 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1429 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1432 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1434 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1435 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1436 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1440 for (i=0; i<oldsize; i++,aep++) {
1441 if (!*aep) /* non-existent */
1443 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1444 if ((j = (HeHASH(entry) & newsize)) != i) {
1446 *oentry = HeNEXT(entry);
1447 if (!(HeNEXT(entry) = aep[j]))
1448 xhv->xhv_fill++; /* HvFILL(hv)++ */
1453 oentry = &HeNEXT(entry);
1455 if (!*aep) /* everything moved */
1456 xhv->xhv_fill--; /* HvFILL(hv)-- */
1463 Creates a new HV. The reference count is set to 1.
1472 register XPVHV* xhv;
1474 hv = (HV*)NEWSV(502,0);
1475 sv_upgrade((SV *)hv, SVt_PVHV);
1476 xhv = (XPVHV*)SvANY(hv);
1479 #ifndef NODEFAULT_SHAREKEYS
1480 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1482 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1483 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1484 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1485 (void)hv_iterinit(hv); /* so each() will start off right */
1490 Perl_newHVhv(pTHX_ HV *ohv)
1493 STRLEN hv_max, hv_fill;
1495 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1497 hv_max = HvMAX(ohv);
1499 if (!SvMAGICAL((SV *)ohv)) {
1500 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1501 int i, shared = !!HvSHAREKEYS(ohv);
1502 HE **ents, **oents = (HE **)HvARRAY(ohv);
1504 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1507 /* In each bucket... */
1508 for (i = 0; i <= hv_max; i++) {
1509 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1516 /* Copy the linked list of entries. */
1517 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1518 U32 hash = HeHASH(oent);
1519 char *key = HeKEY(oent);
1520 STRLEN len = HeKLEN(oent);
1521 int flags = HeKFLAGS(oent);
1524 HeVAL(ent) = newSVsv(HeVAL(oent));
1526 = shared ? share_hek_flags(key, len, hash, flags)
1527 : save_hek_flags(key, len, hash, flags);
1538 HvFILL(hv) = hv_fill;
1539 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1543 /* Iterate over ohv, copying keys and values one at a time. */
1545 I32 riter = HvRITER(ohv);
1546 HE *eiter = HvEITER(ohv);
1548 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1549 while (hv_max && hv_max + 1 >= hv_fill * 2)
1550 hv_max = hv_max / 2;
1554 while ((entry = hv_iternext(ohv))) {
1555 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1556 newSVsv(HeVAL(entry)), HeHASH(entry),
1559 HvRITER(ohv) = riter;
1560 HvEITER(ohv) = eiter;
1567 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1574 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1575 PL_sub_generation++; /* may be deletion of method from stash */
1577 if (HeKLEN(entry) == HEf_SVKEY) {
1578 SvREFCNT_dec(HeKEY_sv(entry));
1579 Safefree(HeKEY_hek(entry));
1581 else if (HvSHAREKEYS(hv))
1582 unshare_hek(HeKEY_hek(entry));
1584 Safefree(HeKEY_hek(entry));
1589 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1593 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1594 PL_sub_generation++; /* may be deletion of method from stash */
1595 sv_2mortal(HeVAL(entry)); /* free between statements */
1596 if (HeKLEN(entry) == HEf_SVKEY) {
1597 sv_2mortal(HeKEY_sv(entry));
1598 Safefree(HeKEY_hek(entry));
1600 else if (HvSHAREKEYS(hv))
1601 unshare_hek(HeKEY_hek(entry));
1603 Safefree(HeKEY_hek(entry));
1608 =for apidoc hv_clear
1610 Clears a hash, making it empty.
1616 Perl_hv_clear(pTHX_ HV *hv)
1618 register XPVHV* xhv;
1622 if(SvREADONLY(hv)) {
1623 Perl_croak(aTHX_ "Attempt to clear a restricted hash");
1626 xhv = (XPVHV*)SvANY(hv);
1628 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1629 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1630 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1631 if (xhv->xhv_array /* HvARRAY(hv) */)
1632 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1633 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1638 HvHASKFLAGS_off(hv);
1642 S_hfreeentries(pTHX_ HV *hv)
1644 register HE **array;
1646 register HE *oentry = Null(HE*);
1657 array = HvARRAY(hv);
1662 entry = HeNEXT(entry);
1663 hv_free_ent(hv, oentry);
1668 entry = array[riter];
1671 (void)hv_iterinit(hv);
1675 =for apidoc hv_undef
1683 Perl_hv_undef(pTHX_ HV *hv)
1685 register XPVHV* xhv;
1688 xhv = (XPVHV*)SvANY(hv);
1690 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1692 Safefree(HvNAME(hv));
1695 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1696 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1697 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1698 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1699 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1706 =for apidoc hv_iterinit
1708 Prepares a starting point to traverse a hash table. Returns the number of
1709 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1710 currently only meaningful for hashes without tie magic.
1712 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1713 hash buckets that happen to be in use. If you still need that esoteric
1714 value, you can get it through the macro C<HvFILL(tb)>.
1720 Perl_hv_iterinit(pTHX_ HV *hv)
1722 register XPVHV* xhv;
1726 Perl_croak(aTHX_ "Bad hash");
1727 xhv = (XPVHV*)SvANY(hv);
1728 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1729 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1731 hv_free_ent(hv, entry);
1733 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1734 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1735 /* used to be xhv->xhv_fill before 5.004_65 */
1736 return XHvTOTALKEYS(xhv);
1740 =for apidoc hv_iternext
1742 Returns entries from a hash iterator. See C<hv_iterinit>.
1748 Perl_hv_iternext(pTHX_ HV *hv)
1750 register XPVHV* xhv;
1756 Perl_croak(aTHX_ "Bad hash");
1757 xhv = (XPVHV*)SvANY(hv);
1758 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1760 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1761 SV *key = sv_newmortal();
1763 sv_setsv(key, HeSVKEY_force(entry));
1764 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1770 /* one HE per MAGICAL hash */
1771 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1773 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1775 HeKEY_hek(entry) = hek;
1776 HeKLEN(entry) = HEf_SVKEY;
1778 magic_nextpack((SV*) hv,mg,key);
1780 /* force key to stay around until next time */
1781 HeSVKEY_set(entry, SvREFCNT_inc(key));
1782 return entry; /* beware, hent_val is not set */
1785 SvREFCNT_dec(HeVAL(entry));
1786 Safefree(HeKEY_hek(entry));
1788 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1791 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1792 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1796 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1797 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1798 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1802 entry = HeNEXT(entry);
1804 * Skip past any placeholders -- don't want to include them in
1807 while (entry && HeVAL(entry) == &PL_sv_undef) {
1808 entry = HeNEXT(entry);
1812 xhv->xhv_riter++; /* HvRITER(hv)++ */
1813 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1814 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1817 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1818 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1820 /* if we have an entry, but it's a placeholder, don't count it */
1821 if (entry && HeVAL(entry) == &PL_sv_undef)
1826 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1828 hv_free_ent(hv, oldentry);
1831 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1836 =for apidoc hv_iterkey
1838 Returns the key from the current position of the hash iterator. See
1845 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1847 if (HeKLEN(entry) == HEf_SVKEY) {
1849 char *p = SvPV(HeKEY_sv(entry), len);
1854 *retlen = HeKLEN(entry);
1855 return HeKEY(entry);
1859 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1861 =for apidoc hv_iterkeysv
1863 Returns the key as an C<SV*> from the current position of the hash
1864 iterator. The return value will always be a mortal copy of the key. Also
1871 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1873 if (HeKLEN(entry) != HEf_SVKEY) {
1874 HEK *hek = HeKEY_hek(entry);
1875 int flags = HEK_FLAGS(hek);
1878 if (flags & HVhek_WASUTF8) {
1880 Andreas would like keys he put in as utf8 to come back as utf8
1882 STRLEN utf8_len = HEK_LEN(hek);
1883 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1885 sv = newSVpvn ((char*)as_utf8, utf8_len);
1888 sv = newSVpvn_share(HEK_KEY(hek),
1889 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1892 return sv_2mortal(sv);
1894 return sv_mortalcopy(HeKEY_sv(entry));
1898 =for apidoc hv_iterval
1900 Returns the value from the current position of the hash iterator. See
1907 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1909 if (SvRMAGICAL(hv)) {
1910 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1911 SV* sv = sv_newmortal();
1912 if (HeKLEN(entry) == HEf_SVKEY)
1913 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1914 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1918 return HeVAL(entry);
1922 =for apidoc hv_iternextsv
1924 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1931 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1934 if ( (he = hv_iternext(hv)) == NULL)
1936 *key = hv_iterkey(he, retlen);
1937 return hv_iterval(hv, he);
1941 =for apidoc hv_magic
1943 Adds magic to a hash. See C<sv_magic>.
1949 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1951 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1954 #if 0 /* use the macro from hv.h instead */
1957 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1959 return HEK_KEY(share_hek(sv, len, hash));
1964 /* possibly free a shared string if no one has access to it
1965 * len and hash must both be valid for str.
1968 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1970 unshare_hek_or_pvn (NULL, str, len, hash);
1975 Perl_unshare_hek(pTHX_ HEK *hek)
1977 unshare_hek_or_pvn(hek, NULL, 0, 0);
1980 /* possibly free a shared string if no one has access to it
1981 hek if non-NULL takes priority over the other 3, else str, len and hash
1982 are used. If so, len and hash must both be valid for str.
1985 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1987 register XPVHV* xhv;
1989 register HE **oentry;
1992 bool is_utf8 = FALSE;
1994 const char *save = str;
1997 hash = HEK_HASH(hek);
1998 } else if (len < 0) {
1999 STRLEN tmplen = -len;
2001 /* See the note in hv_fetch(). --jhi */
2002 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2005 k_flags = HVhek_UTF8;
2007 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2010 /* what follows is the moral equivalent of:
2011 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2012 if (--*Svp == Nullsv)
2013 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2015 xhv = (XPVHV*)SvANY(PL_strtab);
2016 /* assert(xhv_array != 0) */
2018 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2019 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2021 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2022 if (HeKEY_hek(entry) != hek)
2028 int flags_masked = k_flags & HVhek_MASK;
2029 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2030 if (HeHASH(entry) != hash) /* strings can't be equal */
2032 if (HeKLEN(entry) != len)
2034 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2036 if (HeKFLAGS(entry) != flags_masked)
2044 if (--HeVAL(entry) == Nullsv) {
2045 *oentry = HeNEXT(entry);
2047 xhv->xhv_fill--; /* HvFILL(hv)-- */
2048 Safefree(HeKEY_hek(entry));
2050 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2054 UNLOCK_STRTAB_MUTEX;
2055 if (!found && ckWARN_d(WARN_INTERNAL))
2056 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2057 "Attempt to free non-existent shared string '%s'%s",
2058 hek ? HEK_KEY(hek) : str,
2059 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2060 if (k_flags & HVhek_FREEKEY)
2064 /* get a (constant) string ptr from the global string table
2065 * string will get added if it is not already there.
2066 * len and hash must both be valid for str.
2069 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2071 bool is_utf8 = FALSE;
2073 const char *save = str;
2076 STRLEN tmplen = -len;
2078 /* See the note in hv_fetch(). --jhi */
2079 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2081 /* If we were able to downgrade here, then than means that we were passed
2082 in a key which only had chars 0-255, but was utf8 encoded. */
2085 /* If we found we were able to downgrade the string to bytes, then
2086 we should flag that it needs upgrading on keys or each. Also flag
2087 that we need share_hek_flags to free the string. */
2089 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2092 return share_hek_flags (str, len, hash, flags);
2096 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2098 register XPVHV* xhv;
2100 register HE **oentry;
2103 int flags_masked = flags & HVhek_MASK;
2105 /* what follows is the moral equivalent of:
2107 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2108 hv_store(PL_strtab, str, len, Nullsv, hash);
2110 xhv = (XPVHV*)SvANY(PL_strtab);
2111 /* assert(xhv_array != 0) */
2113 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2114 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2115 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2116 if (HeHASH(entry) != hash) /* strings can't be equal */
2118 if (HeKLEN(entry) != len)
2120 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2122 if (HeKFLAGS(entry) != flags_masked)
2129 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2130 HeVAL(entry) = Nullsv;
2131 HeNEXT(entry) = *oentry;
2133 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2134 if (i) { /* initial entry? */
2135 xhv->xhv_fill++; /* HvFILL(hv)++ */
2136 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2141 ++HeVAL(entry); /* use value slot as REFCNT */
2142 UNLOCK_STRTAB_MUTEX;
2144 if (flags & HVhek_FREEKEY)
2147 return HeKEY_hek(entry);