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)
198 const char* keysave = key;
204 if (SvRMAGICAL(hv)) {
205 /* All this clause seems to be utf8 unaware.
206 By moving the utf8 stuff out to hv_fetch_flags I need to ensure
207 key doesn't leak. I've not tried solving the utf8-ness.
210 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
212 mg_copy((SV*)hv, sv, key, klen);
213 if (flags & HVhek_FREEKEY)
216 return &PL_hv_fetch_sv;
218 #ifdef ENV_IS_CASELESS
219 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
221 for (i = 0; i < klen; ++i)
222 if (isLOWER(key[i])) {
223 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
224 SV **ret = hv_fetch(hv, nkey, klen, 0);
226 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
228 } else if (flags & HVhek_FREEKEY)
236 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
237 avoid unnecessary pointer dereferencing. */
238 xhv = (XPVHV*)SvANY(hv);
239 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
241 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
242 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
245 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
246 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
249 if (flags & HVhek_FREEKEY)
255 PERL_HASH(hash, key, klen);
257 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
258 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
259 for (; entry; entry = HeNEXT(entry)) {
260 if (HeHASH(entry) != hash) /* strings can't be equal */
262 if (HeKLEN(entry) != klen)
264 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
266 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
267 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
268 xor is true if bits differ, in which case this isn't a match. */
269 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
271 if (lval && HeKFLAGS(entry) != flags) {
272 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
273 But if entry was set previously with HVhek_WASUTF8 and key now
274 doesn't (or vice versa) then we should change the key's flag,
275 as this is assignment. */
276 if (HvSHAREKEYS(hv)) {
277 /* Need to swap the key we have for a key with the flags we
278 need. As keys are shared we can't just write to the flag,
279 so we share the new one, unshare the old one. */
280 int flags_nofree = flags & ~HVhek_FREEKEY;
281 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
282 unshare_hek (HeKEY_hek(entry));
283 HeKEY_hek(entry) = new_hek;
286 HeKFLAGS(entry) = flags;
288 if (flags & HVhek_FREEKEY)
290 /* if we find a placeholder, we pretend we haven't found anything */
291 if (HeVAL(entry) == &PL_sv_undef)
293 return &HeVAL(entry);
296 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
297 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
299 char *env = PerlEnv_ENVgetenv_len(key,&len);
301 sv = newSVpvn(env,len);
303 if (flags & HVhek_FREEKEY)
305 return hv_store(hv,key,klen,sv,hash);
309 if (!entry && SvREADONLY(hv)) {
310 S_hv_notallowed(aTHX_ flags, key, klen,
311 "access disallowed key '%"SVf"' in"
314 if (lval) { /* gonna assign to this, so it better be there */
316 return hv_store_flags(hv,key,klen,sv,hash,flags);
318 if (flags & HVhek_FREEKEY)
323 /* returns an HE * structure with the all fields set */
324 /* note that hent_val will be a mortal sv for MAGICAL hashes */
326 =for apidoc hv_fetch_ent
328 Returns the hash entry which corresponds to the specified key in the hash.
329 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
330 if you want the function to compute it. IF C<lval> is set then the fetch
331 will be part of a store. Make sure the return value is non-null before
332 accessing it. The return value when C<tb> is a tied hash is a pointer to a
333 static location, so be sure to make a copy of the structure if you need to
336 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
337 information on how to use this function on tied hashes.
343 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
357 if (SvRMAGICAL(hv)) {
358 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
360 keysv = sv_2mortal(newSVsv(keysv));
361 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
362 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
364 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
365 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
367 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
368 HeVAL(&PL_hv_fetch_ent_mh) = sv;
369 return &PL_hv_fetch_ent_mh;
371 #ifdef ENV_IS_CASELESS
372 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
374 key = SvPV(keysv, klen);
375 for (i = 0; i < klen; ++i)
376 if (isLOWER(key[i])) {
377 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
378 (void)strupr(SvPVX(nkeysv));
379 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
381 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
388 xhv = (XPVHV*)SvANY(hv);
389 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
391 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
392 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
395 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
396 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
402 keysave = key = SvPV(keysv, klen);
403 is_utf8 = (SvUTF8(keysv)!=0);
406 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
410 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
414 PERL_HASH(hash, key, klen);
416 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
417 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
418 for (; entry; entry = HeNEXT(entry)) {
419 if (HeHASH(entry) != hash) /* strings can't be equal */
421 if (HeKLEN(entry) != klen)
423 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
425 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
427 if (lval && HeKFLAGS(entry) != flags) {
428 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
429 But if entry was set previously with HVhek_WASUTF8 and key now
430 doesn't (or vice versa) then we should change the key's flag,
431 as this is assignment. */
432 if (HvSHAREKEYS(hv)) {
433 /* Need to swap the key we have for a key with the flags we
434 need. As keys are shared we can't just write to the flag,
435 so we share the new one, unshare the old one. */
436 int flags_nofree = flags & ~HVhek_FREEKEY;
437 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
438 unshare_hek (HeKEY_hek(entry));
439 HeKEY_hek(entry) = new_hek;
442 HeKFLAGS(entry) = flags;
446 /* if we find a placeholder, we pretend we haven't found anything */
447 if (HeVAL(entry) == &PL_sv_undef)
451 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
452 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
454 char *env = PerlEnv_ENVgetenv_len(key,&len);
456 sv = newSVpvn(env,len);
458 return hv_store_ent(hv,keysv,sv,hash);
462 if (!entry && SvREADONLY(hv)) {
463 S_hv_notallowed(aTHX_ flags, key, klen,
464 "access disallowed key '%"SVf"' in"
467 if (flags & HVhek_FREEKEY)
469 if (lval) { /* gonna assign to this, so it better be there */
471 return hv_store_ent(hv,keysv,sv,hash);
477 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
479 MAGIC *mg = SvMAGIC(hv);
483 if (isUPPER(mg->mg_type)) {
485 switch (mg->mg_type) {
486 case PERL_MAGIC_tied:
488 *needs_store = FALSE;
491 mg = mg->mg_moremagic;
498 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
499 the length of the key. The C<hash> parameter is the precomputed hash
500 value; if it is zero then Perl will compute it. The return value will be
501 NULL if the operation failed or if the value did not need to be actually
502 stored within the hash (as in the case of tied hashes). Otherwise it can
503 be dereferenced to get the original C<SV*>. Note that the caller is
504 responsible for suitably incrementing the reference count of C<val> before
505 the call, and decrementing it if the function returned NULL.
507 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
508 information on how to use this function on tied hashes.
514 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
516 bool is_utf8 = FALSE;
517 const char *keysave = key;
521 STRLEN tmplen = klen;
522 /* Just casting the &klen to (STRLEN) won't work well
523 * if STRLEN and I32 are of different widths. --jhi */
524 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
526 /* If we were able to downgrade here, then than means that we were
527 passed in a key which only had chars 0-255, but was utf8 encoded. */
530 /* If we found we were able to downgrade the string to bytes, then
531 we should flag that it needs upgrading on keys or each. */
533 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
536 return hv_store_flags (hv, key, klen, val, hash, flags);
540 S_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
541 register U32 hash, int flags)
546 register HE **oentry;
551 xhv = (XPVHV*)SvANY(hv);
555 hv_magic_check (hv, &needs_copy, &needs_store);
557 mg_copy((SV*)hv, val, key, klen);
558 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
559 if (flags & HVhek_FREEKEY)
563 #ifdef ENV_IS_CASELESS
564 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
565 key = savepvn(key,klen);
566 key = (const char*)strupr((char*)key);
574 HvHASKFLAGS_on((SV*)hv);
577 PERL_HASH(hash, key, klen);
579 if (!xhv->xhv_array /* !HvARRAY(hv) */)
580 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
581 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
584 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
585 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
588 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
589 if (HeHASH(entry) != hash) /* strings can't be equal */
591 if (HeKLEN(entry) != klen)
593 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
595 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
597 if (HeVAL(entry) == &PL_sv_undef)
598 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
600 SvREFCNT_dec(HeVAL(entry));
603 if (HeKFLAGS(entry) != flags) {
604 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
605 But if entry was set previously with HVhek_WASUTF8 and key now
606 doesn't (or vice versa) then we should change the key's flag,
607 as this is assignment. */
608 if (HvSHAREKEYS(hv)) {
609 /* Need to swap the key we have for a key with the flags we
610 need. As keys are shared we can't just write to the flag,
611 so we share the new one, unshare the old one. */
612 int flags_nofree = flags & ~HVhek_FREEKEY;
613 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
614 unshare_hek (HeKEY_hek(entry));
615 HeKEY_hek(entry) = new_hek;
618 HeKFLAGS(entry) = flags;
620 if (flags & HVhek_FREEKEY)
622 return &HeVAL(entry);
625 if (SvREADONLY(hv)) {
626 S_hv_notallowed(aTHX_ flags, key, klen,
627 "access disallowed key '%"SVf"' to"
632 /* share_hek_flags will do the free for us. This might be considered
635 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
636 else /* gotta do the real thing */
637 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
639 HeNEXT(entry) = *oentry;
642 xhv->xhv_keys++; /* HvKEYS(hv)++ */
643 if (i) { /* initial entry? */
644 xhv->xhv_fill++; /* HvFILL(hv)++ */
645 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
649 return &HeVAL(entry);
653 =for apidoc hv_store_ent
655 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
656 parameter is the precomputed hash value; if it is zero then Perl will
657 compute it. The return value is the new hash entry so created. It will be
658 NULL if the operation failed or if the value did not need to be actually
659 stored within the hash (as in the case of tied hashes). Otherwise the
660 contents of the return value can be accessed using the C<He?> macros
661 described here. Note that the caller is responsible for suitably
662 incrementing the reference count of C<val> before the call, and
663 decrementing it if the function returned NULL.
665 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
666 information on how to use this function on tied hashes.
672 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
687 xhv = (XPVHV*)SvANY(hv);
691 hv_magic_check (hv, &needs_copy, &needs_store);
693 bool save_taint = PL_tainted;
695 PL_tainted = SvTAINTED(keysv);
696 keysv = sv_2mortal(newSVsv(keysv));
697 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
698 TAINT_IF(save_taint);
699 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
701 #ifdef ENV_IS_CASELESS
702 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
703 key = SvPV(keysv, klen);
704 keysv = sv_2mortal(newSVpvn(key,klen));
705 (void)strupr(SvPVX(keysv));
712 keysave = key = SvPV(keysv, klen);
713 is_utf8 = (SvUTF8(keysv) != 0);
716 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
720 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
721 HvHASKFLAGS_on((SV*)hv);
725 PERL_HASH(hash, key, klen);
727 if (!xhv->xhv_array /* !HvARRAY(hv) */)
728 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
729 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
732 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
733 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
736 for (; entry; i=0, entry = HeNEXT(entry)) {
737 if (HeHASH(entry) != hash) /* strings can't be equal */
739 if (HeKLEN(entry) != klen)
741 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
743 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
745 if (HeVAL(entry) == &PL_sv_undef)
746 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
748 SvREFCNT_dec(HeVAL(entry));
750 if (HeKFLAGS(entry) != flags) {
751 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
752 But if entry was set previously with HVhek_WASUTF8 and key now
753 doesn't (or vice versa) then we should change the key's flag,
754 as this is assignment. */
755 if (HvSHAREKEYS(hv)) {
756 /* Need to swap the key we have for a key with the flags we
757 need. As keys are shared we can't just write to the flag,
758 so we share the new one, unshare the old one. */
759 int flags_nofree = flags & ~HVhek_FREEKEY;
760 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
761 unshare_hek (HeKEY_hek(entry));
762 HeKEY_hek(entry) = new_hek;
765 HeKFLAGS(entry) = flags;
767 if (flags & HVhek_FREEKEY)
772 if (SvREADONLY(hv)) {
773 S_hv_notallowed(aTHX_ flags, key, klen,
774 "access disallowed key '%"SVf"' to"
779 /* share_hek_flags will do the free for us. This might be considered
782 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
783 else /* gotta do the real thing */
784 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
786 HeNEXT(entry) = *oentry;
789 xhv->xhv_keys++; /* HvKEYS(hv)++ */
790 if (i) { /* initial entry? */
791 xhv->xhv_fill++; /* HvFILL(hv)++ */
792 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
800 =for apidoc hv_delete
802 Deletes a key/value pair in the hash. The value SV is removed from the
803 hash and returned to the caller. The C<klen> is the length of the key.
804 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
811 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
817 register HE **oentry;
820 bool is_utf8 = FALSE;
822 const char *keysave = key;
830 if (SvRMAGICAL(hv)) {
833 hv_magic_check (hv, &needs_copy, &needs_store);
835 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
839 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
840 /* No longer an element */
841 sv_unmagic(sv, PERL_MAGIC_tiedelem);
844 return Nullsv; /* element cannot be deleted */
846 #ifdef ENV_IS_CASELESS
847 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
848 sv = sv_2mortal(newSVpvn(key,klen));
849 key = strupr(SvPVX(sv));
854 xhv = (XPVHV*)SvANY(hv);
855 if (!xhv->xhv_array /* !HvARRAY(hv) */)
859 STRLEN tmplen = klen;
860 /* See the note in hv_fetch(). --jhi */
861 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
864 k_flags = HVhek_UTF8;
866 k_flags |= HVhek_FREEKEY;
869 PERL_HASH(hash, key, klen);
871 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
872 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
875 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
876 if (HeHASH(entry) != hash) /* strings can't be equal */
878 if (HeKLEN(entry) != klen)
880 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
882 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
884 if (k_flags & HVhek_FREEKEY)
886 /* if placeholder is here, it's already been deleted.... */
887 if (HeVAL(entry) == &PL_sv_undef)
890 return Nullsv; /* if still SvREADONLY, leave it deleted. */
892 /* okay, really delete the placeholder... */
893 *oentry = HeNEXT(entry);
895 xhv->xhv_fill--; /* HvFILL(hv)-- */
896 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
899 hv_free_ent(hv, entry);
900 xhv->xhv_keys--; /* HvKEYS(hv)-- */
901 if (xhv->xhv_keys == 0)
903 xhv->xhv_placeholders--;
907 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
908 S_hv_notallowed(aTHX_ k_flags, key, klen,
909 "delete readonly key '%"SVf"' from"
913 if (flags & G_DISCARD)
916 sv = sv_2mortal(HeVAL(entry));
917 HeVAL(entry) = &PL_sv_undef;
921 * If a restricted hash, rather than really deleting the entry, put
922 * a placeholder there. This marks the key as being "approved", so
923 * we can still access via not-really-existing key without raising
926 if (SvREADONLY(hv)) {
927 HeVAL(entry) = &PL_sv_undef;
928 /* We'll be saving this slot, so the number of allocated keys
929 * doesn't go down, but the number placeholders goes up */
930 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
932 *oentry = HeNEXT(entry);
934 xhv->xhv_fill--; /* HvFILL(hv)-- */
935 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
938 hv_free_ent(hv, entry);
939 xhv->xhv_keys--; /* HvKEYS(hv)-- */
940 if (xhv->xhv_keys == 0)
945 if (SvREADONLY(hv)) {
946 S_hv_notallowed(aTHX_ k_flags, key, klen,
947 "access disallowed key '%"SVf"' from"
951 if (k_flags & HVhek_FREEKEY)
957 =for apidoc hv_delete_ent
959 Deletes a key/value pair in the hash. The value SV is removed from the
960 hash and returned to the caller. The C<flags> value will normally be zero;
961 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
962 precomputed hash value, or 0 to ask for it to be computed.
968 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
975 register HE **oentry;
983 if (SvRMAGICAL(hv)) {
986 hv_magic_check (hv, &needs_copy, &needs_store);
988 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
992 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
993 /* No longer an element */
994 sv_unmagic(sv, PERL_MAGIC_tiedelem);
997 return Nullsv; /* element cannot be deleted */
999 #ifdef ENV_IS_CASELESS
1000 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1001 key = SvPV(keysv, klen);
1002 keysv = sv_2mortal(newSVpvn(key,klen));
1003 (void)strupr(SvPVX(keysv));
1009 xhv = (XPVHV*)SvANY(hv);
1010 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1013 keysave = key = SvPV(keysv, klen);
1014 is_utf8 = (SvUTF8(keysv) != 0);
1017 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1019 k_flags = HVhek_UTF8;
1021 k_flags |= HVhek_FREEKEY;
1025 PERL_HASH(hash, key, klen);
1027 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1028 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1031 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1032 if (HeHASH(entry) != hash) /* strings can't be equal */
1034 if (HeKLEN(entry) != klen)
1036 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1038 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1040 if (k_flags & HVhek_FREEKEY)
1043 /* if placeholder is here, it's already been deleted.... */
1044 if (HeVAL(entry) == &PL_sv_undef)
1047 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1049 /* okay, really delete the placeholder. */
1050 *oentry = HeNEXT(entry);
1052 xhv->xhv_fill--; /* HvFILL(hv)-- */
1053 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1056 hv_free_ent(hv, entry);
1057 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1058 if (xhv->xhv_keys == 0)
1059 HvHASKFLAGS_off(hv);
1060 xhv->xhv_placeholders--;
1063 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1064 S_hv_notallowed(aTHX_ k_flags, key, klen,
1065 "delete readonly key '%"SVf"' from"
1069 if (flags & G_DISCARD)
1072 sv = sv_2mortal(HeVAL(entry));
1073 HeVAL(entry) = &PL_sv_undef;
1077 * If a restricted hash, rather than really deleting the entry, put
1078 * a placeholder there. This marks the key as being "approved", so
1079 * we can still access via not-really-existing key without raising
1082 if (SvREADONLY(hv)) {
1083 HeVAL(entry) = &PL_sv_undef;
1084 /* We'll be saving this slot, so the number of allocated keys
1085 * doesn't go down, but the number placeholders goes up */
1086 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1088 *oentry = HeNEXT(entry);
1090 xhv->xhv_fill--; /* HvFILL(hv)-- */
1091 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1094 hv_free_ent(hv, entry);
1095 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1096 if (xhv->xhv_keys == 0)
1097 HvHASKFLAGS_off(hv);
1101 if (SvREADONLY(hv)) {
1102 S_hv_notallowed(aTHX_ k_flags, key, klen,
1103 "delete disallowed key '%"SVf"' from"
1107 if (k_flags & HVhek_FREEKEY)
1113 =for apidoc hv_exists
1115 Returns a boolean indicating whether the specified hash key exists. The
1116 C<klen> is the length of the key.
1122 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1124 register XPVHV* xhv;
1128 bool is_utf8 = FALSE;
1129 const char *keysave = key;
1140 if (SvRMAGICAL(hv)) {
1141 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1142 sv = sv_newmortal();
1143 mg_copy((SV*)hv, sv, key, klen);
1144 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1147 #ifdef ENV_IS_CASELESS
1148 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1149 sv = sv_2mortal(newSVpvn(key,klen));
1150 key = strupr(SvPVX(sv));
1155 xhv = (XPVHV*)SvANY(hv);
1156 #ifndef DYNAMIC_ENV_FETCH
1157 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1162 STRLEN tmplen = klen;
1163 /* See the note in hv_fetch(). --jhi */
1164 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1167 k_flags = HVhek_UTF8;
1169 k_flags |= HVhek_FREEKEY;
1172 PERL_HASH(hash, key, klen);
1174 #ifdef DYNAMIC_ENV_FETCH
1175 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1178 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1179 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1180 for (; entry; entry = HeNEXT(entry)) {
1181 if (HeHASH(entry) != hash) /* strings can't be equal */
1183 if (HeKLEN(entry) != klen)
1185 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1187 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1189 if (k_flags & HVhek_FREEKEY)
1191 /* If we find the key, but the value is a placeholder, return false. */
1192 if (HeVAL(entry) == &PL_sv_undef)
1197 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1198 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1200 char *env = PerlEnv_ENVgetenv_len(key,&len);
1202 sv = newSVpvn(env,len);
1204 (void)hv_store(hv,key,klen,sv,hash);
1205 if (k_flags & HVhek_FREEKEY)
1211 if (k_flags & HVhek_FREEKEY)
1218 =for apidoc hv_exists_ent
1220 Returns a boolean indicating whether the specified hash key exists. C<hash>
1221 can be a valid precomputed hash value, or 0 to ask for it to be
1228 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1230 register XPVHV* xhv;
1242 if (SvRMAGICAL(hv)) {
1243 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1244 SV* svret = sv_newmortal();
1245 sv = sv_newmortal();
1246 keysv = sv_2mortal(newSVsv(keysv));
1247 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1248 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1249 return SvTRUE(svret);
1251 #ifdef ENV_IS_CASELESS
1252 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1253 key = SvPV(keysv, klen);
1254 keysv = sv_2mortal(newSVpvn(key,klen));
1255 (void)strupr(SvPVX(keysv));
1261 xhv = (XPVHV*)SvANY(hv);
1262 #ifndef DYNAMIC_ENV_FETCH
1263 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1267 keysave = key = SvPV(keysv, klen);
1268 is_utf8 = (SvUTF8(keysv) != 0);
1270 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1272 k_flags = HVhek_UTF8;
1274 k_flags |= HVhek_FREEKEY;
1277 PERL_HASH(hash, key, klen);
1279 #ifdef DYNAMIC_ENV_FETCH
1280 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1283 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1284 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1285 for (; entry; entry = HeNEXT(entry)) {
1286 if (HeHASH(entry) != hash) /* strings can't be equal */
1288 if (HeKLEN(entry) != klen)
1290 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1292 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1294 if (k_flags & HVhek_FREEKEY)
1296 /* If we find the key, but the value is a placeholder, return false. */
1297 if (HeVAL(entry) == &PL_sv_undef)
1301 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1302 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1304 char *env = PerlEnv_ENVgetenv_len(key,&len);
1306 sv = newSVpvn(env,len);
1308 (void)hv_store_ent(hv,keysv,sv,hash);
1309 if (k_flags & HVhek_FREEKEY)
1315 if (k_flags & HVhek_FREEKEY)
1321 S_hsplit(pTHX_ HV *hv)
1323 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1324 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1325 register I32 newsize = oldsize * 2;
1327 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1331 register HE **oentry;
1334 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1335 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1341 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1346 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1347 if (oldsize >= 64) {
1348 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1349 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1352 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1356 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1357 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1358 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1361 for (i=0; i<oldsize; i++,aep++) {
1362 if (!*aep) /* non-existent */
1365 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1366 if ((HeHASH(entry) & newsize) != i) {
1367 *oentry = HeNEXT(entry);
1368 HeNEXT(entry) = *bep;
1370 xhv->xhv_fill++; /* HvFILL(hv)++ */
1375 oentry = &HeNEXT(entry);
1377 if (!*aep) /* everything moved */
1378 xhv->xhv_fill--; /* HvFILL(hv)-- */
1383 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1385 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1386 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1387 register I32 newsize;
1393 register HE **oentry;
1395 newsize = (I32) newmax; /* possible truncation here */
1396 if (newsize != newmax || newmax <= oldsize)
1398 while ((newsize & (1 + ~newsize)) != newsize) {
1399 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1401 if (newsize < newmax)
1403 if (newsize < newmax)
1404 return; /* overflow detection */
1406 a = xhv->xhv_array; /* HvARRAY(hv) */
1409 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1410 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1416 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1421 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1422 if (oldsize >= 64) {
1423 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1424 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1427 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1430 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1433 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1435 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1436 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1437 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1441 for (i=0; i<oldsize; i++,aep++) {
1442 if (!*aep) /* non-existent */
1444 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1445 if ((j = (HeHASH(entry) & newsize)) != i) {
1447 *oentry = HeNEXT(entry);
1448 if (!(HeNEXT(entry) = aep[j]))
1449 xhv->xhv_fill++; /* HvFILL(hv)++ */
1454 oentry = &HeNEXT(entry);
1456 if (!*aep) /* everything moved */
1457 xhv->xhv_fill--; /* HvFILL(hv)-- */
1464 Creates a new HV. The reference count is set to 1.
1473 register XPVHV* xhv;
1475 hv = (HV*)NEWSV(502,0);
1476 sv_upgrade((SV *)hv, SVt_PVHV);
1477 xhv = (XPVHV*)SvANY(hv);
1480 #ifndef NODEFAULT_SHAREKEYS
1481 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1483 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1484 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1485 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1486 (void)hv_iterinit(hv); /* so each() will start off right */
1491 Perl_newHVhv(pTHX_ HV *ohv)
1494 STRLEN hv_max, hv_fill;
1496 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1498 hv_max = HvMAX(ohv);
1500 if (!SvMAGICAL((SV *)ohv)) {
1501 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1502 int i, shared = !!HvSHAREKEYS(ohv);
1503 HE **ents, **oents = (HE **)HvARRAY(ohv);
1505 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1508 /* In each bucket... */
1509 for (i = 0; i <= hv_max; i++) {
1510 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1517 /* Copy the linked list of entries. */
1518 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1519 U32 hash = HeHASH(oent);
1520 char *key = HeKEY(oent);
1521 STRLEN len = HeKLEN(oent);
1522 int flags = HeKFLAGS(oent);
1525 HeVAL(ent) = newSVsv(HeVAL(oent));
1527 = shared ? share_hek_flags(key, len, hash, flags)
1528 : save_hek_flags(key, len, hash, flags);
1539 HvFILL(hv) = hv_fill;
1540 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1544 /* Iterate over ohv, copying keys and values one at a time. */
1546 I32 riter = HvRITER(ohv);
1547 HE *eiter = HvEITER(ohv);
1549 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1550 while (hv_max && hv_max + 1 >= hv_fill * 2)
1551 hv_max = hv_max / 2;
1555 while ((entry = hv_iternext(ohv))) {
1556 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1557 newSVsv(HeVAL(entry)), HeHASH(entry),
1560 HvRITER(ohv) = riter;
1561 HvEITER(ohv) = eiter;
1568 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1575 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1576 PL_sub_generation++; /* may be deletion of method from stash */
1578 if (HeKLEN(entry) == HEf_SVKEY) {
1579 SvREFCNT_dec(HeKEY_sv(entry));
1580 Safefree(HeKEY_hek(entry));
1582 else if (HvSHAREKEYS(hv))
1583 unshare_hek(HeKEY_hek(entry));
1585 Safefree(HeKEY_hek(entry));
1590 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1594 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1595 PL_sub_generation++; /* may be deletion of method from stash */
1596 sv_2mortal(HeVAL(entry)); /* free between statements */
1597 if (HeKLEN(entry) == HEf_SVKEY) {
1598 sv_2mortal(HeKEY_sv(entry));
1599 Safefree(HeKEY_hek(entry));
1601 else if (HvSHAREKEYS(hv))
1602 unshare_hek(HeKEY_hek(entry));
1604 Safefree(HeKEY_hek(entry));
1609 =for apidoc hv_clear
1611 Clears a hash, making it empty.
1617 Perl_hv_clear(pTHX_ HV *hv)
1619 register XPVHV* xhv;
1623 if(SvREADONLY(hv)) {
1624 Perl_croak(aTHX_ "Attempt to clear a restricted hash");
1627 xhv = (XPVHV*)SvANY(hv);
1629 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1630 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1631 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1632 if (xhv->xhv_array /* HvARRAY(hv) */)
1633 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1634 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1639 HvHASKFLAGS_off(hv);
1643 S_hfreeentries(pTHX_ HV *hv)
1645 register HE **array;
1647 register HE *oentry = Null(HE*);
1658 array = HvARRAY(hv);
1663 entry = HeNEXT(entry);
1664 hv_free_ent(hv, oentry);
1669 entry = array[riter];
1672 (void)hv_iterinit(hv);
1676 =for apidoc hv_undef
1684 Perl_hv_undef(pTHX_ HV *hv)
1686 register XPVHV* xhv;
1689 xhv = (XPVHV*)SvANY(hv);
1691 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1693 Safefree(HvNAME(hv));
1696 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1697 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1698 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1699 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1700 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1707 =for apidoc hv_iterinit
1709 Prepares a starting point to traverse a hash table. Returns the number of
1710 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1711 currently only meaningful for hashes without tie magic.
1713 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1714 hash buckets that happen to be in use. If you still need that esoteric
1715 value, you can get it through the macro C<HvFILL(tb)>.
1721 Perl_hv_iterinit(pTHX_ HV *hv)
1723 register XPVHV* xhv;
1727 Perl_croak(aTHX_ "Bad hash");
1728 xhv = (XPVHV*)SvANY(hv);
1729 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1730 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1732 hv_free_ent(hv, entry);
1734 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1735 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1736 /* used to be xhv->xhv_fill before 5.004_65 */
1737 return XHvTOTALKEYS(xhv);
1741 =for apidoc hv_iternext
1743 Returns entries from a hash iterator. See C<hv_iterinit>.
1749 Perl_hv_iternext(pTHX_ HV *hv)
1751 register XPVHV* xhv;
1757 Perl_croak(aTHX_ "Bad hash");
1758 xhv = (XPVHV*)SvANY(hv);
1759 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1761 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1762 SV *key = sv_newmortal();
1764 sv_setsv(key, HeSVKEY_force(entry));
1765 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1771 /* one HE per MAGICAL hash */
1772 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1774 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1776 HeKEY_hek(entry) = hek;
1777 HeKLEN(entry) = HEf_SVKEY;
1779 magic_nextpack((SV*) hv,mg,key);
1781 /* force key to stay around until next time */
1782 HeSVKEY_set(entry, SvREFCNT_inc(key));
1783 return entry; /* beware, hent_val is not set */
1786 SvREFCNT_dec(HeVAL(entry));
1787 Safefree(HeKEY_hek(entry));
1789 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1792 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1793 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1797 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1798 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1799 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1803 entry = HeNEXT(entry);
1805 * Skip past any placeholders -- don't want to include them in
1808 while (entry && HeVAL(entry) == &PL_sv_undef) {
1809 entry = HeNEXT(entry);
1813 xhv->xhv_riter++; /* HvRITER(hv)++ */
1814 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1815 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1818 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1819 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1821 /* if we have an entry, but it's a placeholder, don't count it */
1822 if (entry && HeVAL(entry) == &PL_sv_undef)
1827 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1829 hv_free_ent(hv, oldentry);
1832 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1837 =for apidoc hv_iterkey
1839 Returns the key from the current position of the hash iterator. See
1846 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1848 if (HeKLEN(entry) == HEf_SVKEY) {
1850 char *p = SvPV(HeKEY_sv(entry), len);
1855 *retlen = HeKLEN(entry);
1856 return HeKEY(entry);
1860 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1862 =for apidoc hv_iterkeysv
1864 Returns the key as an C<SV*> from the current position of the hash
1865 iterator. The return value will always be a mortal copy of the key. Also
1872 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1874 if (HeKLEN(entry) != HEf_SVKEY) {
1875 HEK *hek = HeKEY_hek(entry);
1876 int flags = HEK_FLAGS(hek);
1879 if (flags & HVhek_WASUTF8) {
1881 Andreas would like keys he put in as utf8 to come back as utf8
1883 STRLEN utf8_len = HEK_LEN(hek);
1884 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1886 sv = newSVpvn ((char*)as_utf8, utf8_len);
1889 sv = newSVpvn_share(HEK_KEY(hek),
1890 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1893 return sv_2mortal(sv);
1895 return sv_mortalcopy(HeKEY_sv(entry));
1899 =for apidoc hv_iterval
1901 Returns the value from the current position of the hash iterator. See
1908 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1910 if (SvRMAGICAL(hv)) {
1911 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1912 SV* sv = sv_newmortal();
1913 if (HeKLEN(entry) == HEf_SVKEY)
1914 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1915 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1919 return HeVAL(entry);
1923 =for apidoc hv_iternextsv
1925 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1932 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1935 if ( (he = hv_iternext(hv)) == NULL)
1937 *key = hv_iterkey(he, retlen);
1938 return hv_iterval(hv, he);
1942 =for apidoc hv_magic
1944 Adds magic to a hash. See C<sv_magic>.
1950 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1952 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1955 #if 0 /* use the macro from hv.h instead */
1958 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1960 return HEK_KEY(share_hek(sv, len, hash));
1965 /* possibly free a shared string if no one has access to it
1966 * len and hash must both be valid for str.
1969 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1971 unshare_hek_or_pvn (NULL, str, len, hash);
1976 Perl_unshare_hek(pTHX_ HEK *hek)
1978 unshare_hek_or_pvn(hek, NULL, 0, 0);
1981 /* possibly free a shared string if no one has access to it
1982 hek if non-NULL takes priority over the other 3, else str, len and hash
1983 are used. If so, len and hash must both be valid for str.
1986 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1988 register XPVHV* xhv;
1990 register HE **oentry;
1993 bool is_utf8 = FALSE;
1995 const char *save = str;
1998 hash = HEK_HASH(hek);
1999 } else if (len < 0) {
2000 STRLEN tmplen = -len;
2002 /* See the note in hv_fetch(). --jhi */
2003 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2006 k_flags = HVhek_UTF8;
2008 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2011 /* what follows is the moral equivalent of:
2012 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2013 if (--*Svp == Nullsv)
2014 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2016 xhv = (XPVHV*)SvANY(PL_strtab);
2017 /* assert(xhv_array != 0) */
2019 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2020 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2022 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2023 if (HeKEY_hek(entry) != hek)
2029 int flags_masked = k_flags & HVhek_MASK;
2030 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2031 if (HeHASH(entry) != hash) /* strings can't be equal */
2033 if (HeKLEN(entry) != len)
2035 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2037 if (HeKFLAGS(entry) != flags_masked)
2045 if (--HeVAL(entry) == Nullsv) {
2046 *oentry = HeNEXT(entry);
2048 xhv->xhv_fill--; /* HvFILL(hv)-- */
2049 Safefree(HeKEY_hek(entry));
2051 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2055 UNLOCK_STRTAB_MUTEX;
2056 if (!found && ckWARN_d(WARN_INTERNAL))
2057 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2058 "Attempt to free non-existent shared string '%s'%s",
2059 hek ? HEK_KEY(hek) : str,
2060 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2061 if (k_flags & HVhek_FREEKEY)
2065 /* get a (constant) string ptr from the global string table
2066 * string will get added if it is not already there.
2067 * len and hash must both be valid for str.
2070 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2072 bool is_utf8 = FALSE;
2074 const char *save = str;
2077 STRLEN tmplen = -len;
2079 /* See the note in hv_fetch(). --jhi */
2080 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2082 /* If we were able to downgrade here, then than means that we were passed
2083 in a key which only had chars 0-255, but was utf8 encoded. */
2086 /* If we found we were able to downgrade the string to bytes, then
2087 we should flag that it needs upgrading on keys or each. Also flag
2088 that we need share_hek_flags to free the string. */
2090 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2093 return share_hek_flags (str, len, hash, flags);
2097 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2099 register XPVHV* xhv;
2101 register HE **oentry;
2104 int flags_masked = flags & HVhek_MASK;
2106 /* what follows is the moral equivalent of:
2108 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2109 hv_store(PL_strtab, str, len, Nullsv, hash);
2111 xhv = (XPVHV*)SvANY(PL_strtab);
2112 /* assert(xhv_array != 0) */
2114 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2115 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2116 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2117 if (HeHASH(entry) != hash) /* strings can't be equal */
2119 if (HeKLEN(entry) != len)
2121 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2123 if (HeKFLAGS(entry) != flags_masked)
2130 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2131 HeVAL(entry) = Nullsv;
2132 HeNEXT(entry) = *oentry;
2134 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2135 if (i) { /* initial entry? */
2136 xhv->xhv_fill++; /* HvFILL(hv)++ */
2137 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2142 ++HeVAL(entry); /* use value slot as REFCNT */
2143 UNLOCK_STRTAB_MUTEX;
2145 if (flags & HVhek_FREEKEY)
2148 return HeKEY_hek(entry);