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 Perl_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
127 SV *sv = 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_croak(aTHX_ msg, sv);
142 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
148 Returns the SV which corresponds to the specified key in the hash. The
149 C<klen> is the length of the key. If C<lval> is set then the fetch will be
150 part of a store. Check that the return value is non-null before
151 dereferencing it to an C<SV*>.
153 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
154 information on how to use this function on tied hashes.
161 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
163 bool is_utf8 = FALSE;
164 const char *keysave = key;
173 STRLEN tmplen = klen;
174 /* Just casting the &klen to (STRLEN) won't work well
175 * if STRLEN and I32 are of different widths. --jhi */
176 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
178 /* If we were able to downgrade here, then than means that we were
179 passed in a key which only had chars 0-255, but was utf8 encoded. */
182 /* If we found we were able to downgrade the string to bytes, then
183 we should flag that it needs upgrading on keys or each. */
185 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
188 return hv_fetch_flags (hv, key, klen, lval, flags);
192 S_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen, I32 lval, int flags)
202 if (SvRMAGICAL(hv)) {
203 /* All this clause seems to be utf8 unaware.
204 By moving the utf8 stuff out to hv_fetch_flags I need to ensure
205 key doesn't leak. I've not tried solving the utf8-ness.
208 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
210 mg_copy((SV*)hv, sv, key, klen);
211 if (flags & HVhek_FREEKEY)
214 return &PL_hv_fetch_sv;
216 #ifdef ENV_IS_CASELESS
217 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
219 for (i = 0; i < klen; ++i)
220 if (isLOWER(key[i])) {
221 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
222 SV **ret = hv_fetch(hv, nkey, klen, 0);
224 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
226 } else if (flags & HVhek_FREEKEY)
234 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
235 avoid unnecessary pointer dereferencing. */
236 xhv = (XPVHV*)SvANY(hv);
237 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
239 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
240 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
243 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
244 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
247 if (flags & HVhek_FREEKEY)
253 PERL_HASH(hash, key, klen);
255 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
256 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
257 for (; entry; entry = HeNEXT(entry)) {
258 if (HeHASH(entry) != hash) /* strings can't be equal */
260 if (HeKLEN(entry) != klen)
262 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
264 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
265 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
266 xor is true if bits differ, in which case this isn't a match. */
267 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
269 if (lval && HeKFLAGS(entry) != flags) {
270 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
271 But if entry was set previously with HVhek_WASUTF8 and key now
272 doesn't (or vice versa) then we should change the key's flag,
273 as this is assignment. */
274 if (HvSHAREKEYS(hv)) {
275 /* Need to swap the key we have for a key with the flags we
276 need. As keys are shared we can't just write to the flag,
277 so we share the new one, unshare the old one. */
278 int flags_nofree = flags & ~HVhek_FREEKEY;
279 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
280 unshare_hek (HeKEY_hek(entry));
281 HeKEY_hek(entry) = new_hek;
284 HeKFLAGS(entry) = flags;
286 if (flags & HVhek_FREEKEY)
288 /* if we find a placeholder, we pretend we haven't found anything */
289 if (HeVAL(entry) == &PL_sv_undef)
291 return &HeVAL(entry);
294 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
295 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
297 char *env = PerlEnv_ENVgetenv_len(key,&len);
299 sv = newSVpvn(env,len);
303 return hv_store(hv,key,klen,sv,hash);
307 if (!entry && SvREADONLY(hv)) {
308 Perl_hv_notallowed(aTHX_ flags, key, klen,
309 "Attempt to access disallowed key '%"SVf"' in a fixed hash"
312 if (lval) { /* gonna assign to this, so it better be there */
314 return hv_store_flags(hv,key,klen,sv,hash,flags);
316 if (flags & HVhek_FREEKEY)
321 /* returns an HE * structure with the all fields set */
322 /* note that hent_val will be a mortal sv for MAGICAL hashes */
324 =for apidoc hv_fetch_ent
326 Returns the hash entry which corresponds to the specified key in the hash.
327 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
328 if you want the function to compute it. IF C<lval> is set then the fetch
329 will be part of a store. Make sure the return value is non-null before
330 accessing it. The return value when C<tb> is a tied hash is a pointer to a
331 static location, so be sure to make a copy of the structure if you need to
334 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
335 information on how to use this function on tied hashes.
341 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
355 if (SvRMAGICAL(hv)) {
356 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
358 keysv = sv_2mortal(newSVsv(keysv));
359 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
360 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
362 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
363 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
365 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
366 HeVAL(&PL_hv_fetch_ent_mh) = sv;
367 return &PL_hv_fetch_ent_mh;
369 #ifdef ENV_IS_CASELESS
370 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
372 key = SvPV(keysv, klen);
373 for (i = 0; i < klen; ++i)
374 if (isLOWER(key[i])) {
375 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
376 (void)strupr(SvPVX(nkeysv));
377 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
379 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
386 xhv = (XPVHV*)SvANY(hv);
387 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
389 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
390 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
393 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
394 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
400 keysave = key = SvPV(keysv, klen);
401 is_utf8 = (SvUTF8(keysv)!=0);
404 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
408 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
412 PERL_HASH(hash, key, klen);
414 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
415 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
416 for (; entry; entry = HeNEXT(entry)) {
417 if (HeHASH(entry) != hash) /* strings can't be equal */
419 if (HeKLEN(entry) != klen)
421 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
423 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
425 if (lval && HeKFLAGS(entry) != flags) {
426 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
427 But if entry was set previously with HVhek_WASUTF8 and key now
428 doesn't (or vice versa) then we should change the key's flag,
429 as this is assignment. */
430 if (HvSHAREKEYS(hv)) {
431 /* Need to swap the key we have for a key with the flags we
432 need. As keys are shared we can't just write to the flag,
433 so we share the new one, unshare the old one. */
434 int flags_nofree = flags & ~HVhek_FREEKEY;
435 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
436 unshare_hek (HeKEY_hek(entry));
437 HeKEY_hek(entry) = new_hek;
440 HeKFLAGS(entry) = flags;
444 /* if we find a placeholder, we pretend we haven't found anything */
445 if (HeVAL(entry) == &PL_sv_undef)
449 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
450 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
452 char *env = PerlEnv_ENVgetenv_len(key,&len);
454 sv = newSVpvn(env,len);
456 return hv_store_ent(hv,keysv,sv,hash);
460 if (!entry && SvREADONLY(hv)) {
461 Perl_hv_notallowed(aTHX_ flags, key, klen,
462 "Attempt to access disallowed key '%"SVf"' in a fixed hash"
465 if (flags & HVhek_FREEKEY)
467 if (lval) { /* gonna assign to this, so it better be there */
469 return hv_store_ent(hv,keysv,sv,hash);
475 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
477 MAGIC *mg = SvMAGIC(hv);
481 if (isUPPER(mg->mg_type)) {
483 switch (mg->mg_type) {
484 case PERL_MAGIC_tied:
486 *needs_store = FALSE;
489 mg = mg->mg_moremagic;
496 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
497 the length of the key. The C<hash> parameter is the precomputed hash
498 value; if it is zero then Perl will compute it. The return value will be
499 NULL if the operation failed or if the value did not need to be actually
500 stored within the hash (as in the case of tied hashes). Otherwise it can
501 be dereferenced to get the original C<SV*>. Note that the caller is
502 responsible for suitably incrementing the reference count of C<val> before
503 the call, and decrementing it if the function returned NULL.
505 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
506 information on how to use this function on tied hashes.
512 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
514 bool is_utf8 = FALSE;
515 const char *keysave = key;
519 STRLEN tmplen = klen;
520 /* Just casting the &klen to (STRLEN) won't work well
521 * if STRLEN and I32 are of different widths. --jhi */
522 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
524 /* If we were able to downgrade here, then than means that we were
525 passed in a key which only had chars 0-255, but was utf8 encoded. */
528 /* If we found we were able to downgrade the string to bytes, then
529 we should flag that it needs upgrading on keys or each. */
531 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
534 return hv_store_flags (hv, key, klen, val, hash, flags);
538 S_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
539 register U32 hash, int flags)
544 register HE **oentry;
549 xhv = (XPVHV*)SvANY(hv);
553 hv_magic_check (hv, &needs_copy, &needs_store);
555 mg_copy((SV*)hv, val, key, klen);
556 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
557 if (flags & HVhek_FREEKEY)
561 #ifdef ENV_IS_CASELESS
562 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
563 key = savepvn(key,klen);
564 key = (const char*)strupr((char*)key);
572 HvHASKFLAGS_on((SV*)hv);
575 PERL_HASH(hash, key, klen);
577 if (!xhv->xhv_array /* !HvARRAY(hv) */)
578 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
579 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
582 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
583 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
586 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
587 if (HeHASH(entry) != hash) /* strings can't be equal */
589 if (HeKLEN(entry) != klen)
591 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
593 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
595 if (HeVAL(entry) == &PL_sv_undef)
596 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
598 SvREFCNT_dec(HeVAL(entry));
601 if (HeKFLAGS(entry) != flags) {
602 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
603 But if entry was set previously with HVhek_WASUTF8 and key now
604 doesn't (or vice versa) then we should change the key's flag,
605 as this is assignment. */
606 if (HvSHAREKEYS(hv)) {
607 /* Need to swap the key we have for a key with the flags we
608 need. As keys are shared we can't just write to the flag,
609 so we share the new one, unshare the old one. */
610 int flags_nofree = flags & ~HVhek_FREEKEY;
611 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
612 unshare_hek (HeKEY_hek(entry));
613 HeKEY_hek(entry) = new_hek;
616 HeKFLAGS(entry) = flags;
618 if (flags & HVhek_FREEKEY)
620 return &HeVAL(entry);
623 if (SvREADONLY(hv)) {
624 Perl_hv_notallowed(aTHX_ flags, key, klen,
625 "Attempt to access disallowed key '%"SVf"' to a fixed hash"
630 /* share_hek_flags will do the free for us. This might be considered
633 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
634 else /* gotta do the real thing */
635 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
637 HeNEXT(entry) = *oentry;
640 xhv->xhv_keys++; /* HvKEYS(hv)++ */
641 if (i) { /* initial entry? */
642 xhv->xhv_fill++; /* HvFILL(hv)++ */
643 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
647 return &HeVAL(entry);
651 =for apidoc hv_store_ent
653 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
654 parameter is the precomputed hash value; if it is zero then Perl will
655 compute it. The return value is the new hash entry so created. It will be
656 NULL if the operation failed or if the value did not need to be actually
657 stored within the hash (as in the case of tied hashes). Otherwise the
658 contents of the return value can be accessed using the C<He?> macros
659 described here. Note that the caller is responsible for suitably
660 incrementing the reference count of C<val> before the call, and
661 decrementing it if the function returned NULL.
663 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
664 information on how to use this function on tied hashes.
670 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
685 xhv = (XPVHV*)SvANY(hv);
689 hv_magic_check (hv, &needs_copy, &needs_store);
691 bool save_taint = PL_tainted;
693 PL_tainted = SvTAINTED(keysv);
694 keysv = sv_2mortal(newSVsv(keysv));
695 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
696 TAINT_IF(save_taint);
697 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
699 #ifdef ENV_IS_CASELESS
700 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
701 key = SvPV(keysv, klen);
702 keysv = sv_2mortal(newSVpvn(key,klen));
703 (void)strupr(SvPVX(keysv));
710 keysave = key = SvPV(keysv, klen);
711 is_utf8 = (SvUTF8(keysv) != 0);
714 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
718 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
719 HvHASKFLAGS_on((SV*)hv);
723 PERL_HASH(hash, key, klen);
725 if (!xhv->xhv_array /* !HvARRAY(hv) */)
726 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
727 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
730 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
731 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
734 for (; entry; i=0, entry = HeNEXT(entry)) {
735 if (HeHASH(entry) != hash) /* strings can't be equal */
737 if (HeKLEN(entry) != klen)
739 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
741 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
743 if (HeVAL(entry) == &PL_sv_undef)
744 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
746 SvREFCNT_dec(HeVAL(entry));
748 if (HeKFLAGS(entry) != flags) {
749 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
750 But if entry was set previously with HVhek_WASUTF8 and key now
751 doesn't (or vice versa) then we should change the key's flag,
752 as this is assignment. */
753 if (HvSHAREKEYS(hv)) {
754 /* Need to swap the key we have for a key with the flags we
755 need. As keys are shared we can't just write to the flag,
756 so we share the new one, unshare the old one. */
757 int flags_nofree = flags & ~HVhek_FREEKEY;
758 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
759 unshare_hek (HeKEY_hek(entry));
760 HeKEY_hek(entry) = new_hek;
763 HeKFLAGS(entry) = flags;
765 if (flags & HVhek_FREEKEY)
770 if (SvREADONLY(hv)) {
771 Perl_hv_notallowed(aTHX_ flags, key, klen,
772 "Attempt to access disallowed key '%"SVf"' to a fixed hash"
777 /* share_hek_flags will do the free for us. This might be considered
780 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
781 else /* gotta do the real thing */
782 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
784 HeNEXT(entry) = *oentry;
787 xhv->xhv_keys++; /* HvKEYS(hv)++ */
788 if (i) { /* initial entry? */
789 xhv->xhv_fill++; /* HvFILL(hv)++ */
790 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
798 =for apidoc hv_delete
800 Deletes a key/value pair in the hash. The value SV is removed from the
801 hash and returned to the caller. The C<klen> is the length of the key.
802 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
809 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
815 register HE **oentry;
818 bool is_utf8 = FALSE;
820 const char *keysave = key;
828 if (SvRMAGICAL(hv)) {
831 hv_magic_check (hv, &needs_copy, &needs_store);
833 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
837 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
838 /* No longer an element */
839 sv_unmagic(sv, PERL_MAGIC_tiedelem);
842 return Nullsv; /* element cannot be deleted */
844 #ifdef ENV_IS_CASELESS
845 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
846 sv = sv_2mortal(newSVpvn(key,klen));
847 key = strupr(SvPVX(sv));
852 xhv = (XPVHV*)SvANY(hv);
853 if (!xhv->xhv_array /* !HvARRAY(hv) */)
857 STRLEN tmplen = klen;
858 /* See the note in hv_fetch(). --jhi */
859 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
862 k_flags = HVhek_UTF8;
864 k_flags |= HVhek_FREEKEY;
867 PERL_HASH(hash, key, klen);
869 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
870 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
873 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
874 if (HeHASH(entry) != hash) /* strings can't be equal */
876 if (HeKLEN(entry) != klen)
878 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
880 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
882 if (k_flags & HVhek_FREEKEY)
884 /* if placeholder is here, it's already been deleted.... */
885 if (HeVAL(entry) == &PL_sv_undef)
888 return Nullsv; /* if still SvREADONLY, leave it deleted. */
890 /* okay, really delete the placeholder... */
891 *oentry = HeNEXT(entry);
893 xhv->xhv_fill--; /* HvFILL(hv)-- */
894 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
897 hv_free_ent(hv, entry);
898 xhv->xhv_keys--; /* HvKEYS(hv)-- */
899 if (xhv->xhv_keys == 0)
901 xhv->xhv_placeholders--;
905 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
906 Perl_hv_notallowed(aTHX_ k_flags, key, klen,
907 "Attempt to delete readonly key '%"SVf"' from a fixed hash"
911 if (flags & G_DISCARD)
914 sv = sv_2mortal(HeVAL(entry));
915 HeVAL(entry) = &PL_sv_undef;
919 * If a restricted hash, rather than really deleting the entry, put
920 * a placeholder there. This marks the key as being "approved", so
921 * we can still access via not-really-existing key without raising
924 if (SvREADONLY(hv)) {
925 HeVAL(entry) = &PL_sv_undef;
926 /* We'll be saving this slot, so the number of allocated keys
927 * doesn't go down, but the number placeholders goes up */
928 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
930 *oentry = HeNEXT(entry);
932 xhv->xhv_fill--; /* HvFILL(hv)-- */
933 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
936 hv_free_ent(hv, entry);
937 xhv->xhv_keys--; /* HvKEYS(hv)-- */
938 if (xhv->xhv_keys == 0)
943 if (SvREADONLY(hv)) {
944 Perl_hv_notallowed(aTHX_ k_flags, key, klen,
945 "Attempt to access disallowed key '%"SVf"' from a fixed hash"
949 if (k_flags & HVhek_FREEKEY)
955 =for apidoc hv_delete_ent
957 Deletes a key/value pair in the hash. The value SV is removed from the
958 hash and returned to the caller. The C<flags> value will normally be zero;
959 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
960 precomputed hash value, or 0 to ask for it to be computed.
966 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
973 register HE **oentry;
981 if (SvRMAGICAL(hv)) {
984 hv_magic_check (hv, &needs_copy, &needs_store);
986 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
990 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
991 /* No longer an element */
992 sv_unmagic(sv, PERL_MAGIC_tiedelem);
995 return Nullsv; /* element cannot be deleted */
997 #ifdef ENV_IS_CASELESS
998 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
999 key = SvPV(keysv, klen);
1000 keysv = sv_2mortal(newSVpvn(key,klen));
1001 (void)strupr(SvPVX(keysv));
1007 xhv = (XPVHV*)SvANY(hv);
1008 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1011 keysave = key = SvPV(keysv, klen);
1012 is_utf8 = (SvUTF8(keysv) != 0);
1015 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1017 k_flags = HVhek_UTF8;
1019 k_flags |= HVhek_FREEKEY;
1023 PERL_HASH(hash, key, klen);
1025 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1026 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1029 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1030 if (HeHASH(entry) != hash) /* strings can't be equal */
1032 if (HeKLEN(entry) != klen)
1034 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1036 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1038 if (k_flags & HVhek_FREEKEY)
1041 /* if placeholder is here, it's already been deleted.... */
1042 if (HeVAL(entry) == &PL_sv_undef)
1045 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1047 /* okay, really delete the placeholder. */
1048 *oentry = HeNEXT(entry);
1050 xhv->xhv_fill--; /* HvFILL(hv)-- */
1051 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1054 hv_free_ent(hv, entry);
1055 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1056 if (xhv->xhv_keys == 0)
1057 HvHASKFLAGS_off(hv);
1058 xhv->xhv_placeholders--;
1061 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1062 Perl_hv_notallowed(aTHX_ k_flags, key, klen,
1063 "Attempt to delete readonly key '%"SVf"' from a fixed hash"
1067 if (flags & G_DISCARD)
1070 sv = sv_2mortal(HeVAL(entry));
1071 HeVAL(entry) = &PL_sv_undef;
1075 * If a restricted hash, rather than really deleting the entry, put
1076 * a placeholder there. This marks the key as being "approved", so
1077 * we can still access via not-really-existing key without raising
1080 if (SvREADONLY(hv)) {
1081 HeVAL(entry) = &PL_sv_undef;
1082 /* We'll be saving this slot, so the number of allocated keys
1083 * doesn't go down, but the number placeholders goes up */
1084 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1086 *oentry = HeNEXT(entry);
1088 xhv->xhv_fill--; /* HvFILL(hv)-- */
1089 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1092 hv_free_ent(hv, entry);
1093 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1094 if (xhv->xhv_keys == 0)
1095 HvHASKFLAGS_off(hv);
1099 if (SvREADONLY(hv)) {
1100 Perl_hv_notallowed(aTHX_ k_flags, key, klen,
1101 "Attempt to delete disallowed key '%"SVf"' from a fixed hash"
1105 if (k_flags & HVhek_FREEKEY)
1111 =for apidoc hv_exists
1113 Returns a boolean indicating whether the specified hash key exists. The
1114 C<klen> is the length of the key.
1120 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1122 register XPVHV* xhv;
1126 bool is_utf8 = FALSE;
1127 const char *keysave = key;
1138 if (SvRMAGICAL(hv)) {
1139 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1140 sv = sv_newmortal();
1141 mg_copy((SV*)hv, sv, key, klen);
1142 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1145 #ifdef ENV_IS_CASELESS
1146 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1147 sv = sv_2mortal(newSVpvn(key,klen));
1148 key = strupr(SvPVX(sv));
1153 xhv = (XPVHV*)SvANY(hv);
1154 #ifndef DYNAMIC_ENV_FETCH
1155 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1160 STRLEN tmplen = klen;
1161 /* See the note in hv_fetch(). --jhi */
1162 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1165 k_flags = HVhek_UTF8;
1167 k_flags |= HVhek_FREEKEY;
1170 PERL_HASH(hash, key, klen);
1172 #ifdef DYNAMIC_ENV_FETCH
1173 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1176 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1177 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1178 for (; entry; entry = HeNEXT(entry)) {
1179 if (HeHASH(entry) != hash) /* strings can't be equal */
1181 if (HeKLEN(entry) != klen)
1183 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1185 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1187 if (k_flags & HVhek_FREEKEY)
1189 /* If we find the key, but the value is a placeholder, return false. */
1190 if (HeVAL(entry) == &PL_sv_undef)
1195 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1196 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1198 char *env = PerlEnv_ENVgetenv_len(key,&len);
1200 sv = newSVpvn(env,len);
1202 (void)hv_store(hv,key,klen,sv,hash);
1203 if (k_flags & HVhek_FREEKEY)
1209 if (k_flags & HVhek_FREEKEY)
1216 =for apidoc hv_exists_ent
1218 Returns a boolean indicating whether the specified hash key exists. C<hash>
1219 can be a valid precomputed hash value, or 0 to ask for it to be
1226 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1228 register XPVHV* xhv;
1240 if (SvRMAGICAL(hv)) {
1241 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1242 SV* svret = sv_newmortal();
1243 sv = sv_newmortal();
1244 keysv = sv_2mortal(newSVsv(keysv));
1245 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1246 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1247 return SvTRUE(svret);
1249 #ifdef ENV_IS_CASELESS
1250 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1251 key = SvPV(keysv, klen);
1252 keysv = sv_2mortal(newSVpvn(key,klen));
1253 (void)strupr(SvPVX(keysv));
1259 xhv = (XPVHV*)SvANY(hv);
1260 #ifndef DYNAMIC_ENV_FETCH
1261 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1265 keysave = key = SvPV(keysv, klen);
1266 is_utf8 = (SvUTF8(keysv) != 0);
1268 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1270 k_flags = HVhek_UTF8;
1272 k_flags |= HVhek_FREEKEY;
1275 PERL_HASH(hash, key, klen);
1277 #ifdef DYNAMIC_ENV_FETCH
1278 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1281 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1282 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1283 for (; entry; entry = HeNEXT(entry)) {
1284 if (HeHASH(entry) != hash) /* strings can't be equal */
1286 if (HeKLEN(entry) != klen)
1288 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1290 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1292 if (k_flags & HVhek_FREEKEY)
1294 /* If we find the key, but the value is a placeholder, return false. */
1295 if (HeVAL(entry) == &PL_sv_undef)
1299 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1300 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1302 char *env = PerlEnv_ENVgetenv_len(key,&len);
1304 sv = newSVpvn(env,len);
1306 (void)hv_store_ent(hv,keysv,sv,hash);
1307 if (k_flags & HVhek_FREEKEY)
1313 if (k_flags & HVhek_FREEKEY)
1319 S_hsplit(pTHX_ HV *hv)
1321 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1322 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1323 register I32 newsize = oldsize * 2;
1325 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1329 register HE **oentry;
1332 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1333 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1339 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1344 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1345 if (oldsize >= 64) {
1346 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1347 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1350 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1354 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1355 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1356 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1359 for (i=0; i<oldsize; i++,aep++) {
1360 if (!*aep) /* non-existent */
1363 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1364 if ((HeHASH(entry) & newsize) != i) {
1365 *oentry = HeNEXT(entry);
1366 HeNEXT(entry) = *bep;
1368 xhv->xhv_fill++; /* HvFILL(hv)++ */
1373 oentry = &HeNEXT(entry);
1375 if (!*aep) /* everything moved */
1376 xhv->xhv_fill--; /* HvFILL(hv)-- */
1381 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1383 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1384 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1385 register I32 newsize;
1391 register HE **oentry;
1393 newsize = (I32) newmax; /* possible truncation here */
1394 if (newsize != newmax || newmax <= oldsize)
1396 while ((newsize & (1 + ~newsize)) != newsize) {
1397 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1399 if (newsize < newmax)
1401 if (newsize < newmax)
1402 return; /* overflow detection */
1404 a = xhv->xhv_array; /* HvARRAY(hv) */
1407 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1408 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1414 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1419 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1420 if (oldsize >= 64) {
1421 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1422 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1425 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1428 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1431 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1433 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1434 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1435 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1439 for (i=0; i<oldsize; i++,aep++) {
1440 if (!*aep) /* non-existent */
1442 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1443 if ((j = (HeHASH(entry) & newsize)) != i) {
1445 *oentry = HeNEXT(entry);
1446 if (!(HeNEXT(entry) = aep[j]))
1447 xhv->xhv_fill++; /* HvFILL(hv)++ */
1452 oentry = &HeNEXT(entry);
1454 if (!*aep) /* everything moved */
1455 xhv->xhv_fill--; /* HvFILL(hv)-- */
1462 Creates a new HV. The reference count is set to 1.
1471 register XPVHV* xhv;
1473 hv = (HV*)NEWSV(502,0);
1474 sv_upgrade((SV *)hv, SVt_PVHV);
1475 xhv = (XPVHV*)SvANY(hv);
1478 #ifndef NODEFAULT_SHAREKEYS
1479 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1481 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1482 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1483 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1484 (void)hv_iterinit(hv); /* so each() will start off right */
1489 Perl_newHVhv(pTHX_ HV *ohv)
1492 STRLEN hv_max, hv_fill;
1494 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1496 hv_max = HvMAX(ohv);
1498 if (!SvMAGICAL((SV *)ohv)) {
1499 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1500 int i, shared = !!HvSHAREKEYS(ohv);
1501 HE **ents, **oents = (HE **)HvARRAY(ohv);
1503 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1506 /* In each bucket... */
1507 for (i = 0; i <= hv_max; i++) {
1508 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1515 /* Copy the linked list of entries. */
1516 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1517 U32 hash = HeHASH(oent);
1518 char *key = HeKEY(oent);
1519 STRLEN len = HeKLEN(oent);
1520 int flags = HeKFLAGS(oent);
1523 HeVAL(ent) = newSVsv(HeVAL(oent));
1525 = shared ? share_hek_flags(key, len, hash, flags)
1526 : save_hek_flags(key, len, hash, flags);
1537 HvFILL(hv) = hv_fill;
1538 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1542 /* Iterate over ohv, copying keys and values one at a time. */
1544 I32 riter = HvRITER(ohv);
1545 HE *eiter = HvEITER(ohv);
1547 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1548 while (hv_max && hv_max + 1 >= hv_fill * 2)
1549 hv_max = hv_max / 2;
1553 while ((entry = hv_iternext(ohv))) {
1554 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1555 newSVsv(HeVAL(entry)), HeHASH(entry),
1558 HvRITER(ohv) = riter;
1559 HvEITER(ohv) = eiter;
1566 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1573 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1574 PL_sub_generation++; /* may be deletion of method from stash */
1576 if (HeKLEN(entry) == HEf_SVKEY) {
1577 SvREFCNT_dec(HeKEY_sv(entry));
1578 Safefree(HeKEY_hek(entry));
1580 else if (HvSHAREKEYS(hv))
1581 unshare_hek(HeKEY_hek(entry));
1583 Safefree(HeKEY_hek(entry));
1588 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1592 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1593 PL_sub_generation++; /* may be deletion of method from stash */
1594 sv_2mortal(HeVAL(entry)); /* free between statements */
1595 if (HeKLEN(entry) == HEf_SVKEY) {
1596 sv_2mortal(HeKEY_sv(entry));
1597 Safefree(HeKEY_hek(entry));
1599 else if (HvSHAREKEYS(hv))
1600 unshare_hek(HeKEY_hek(entry));
1602 Safefree(HeKEY_hek(entry));
1607 =for apidoc hv_clear
1609 Clears a hash, making it empty.
1615 Perl_hv_clear(pTHX_ HV *hv)
1617 register XPVHV* xhv;
1621 if(SvREADONLY(hv)) {
1622 Perl_croak(aTHX_ "Attempt to clear a fixed hash");
1625 xhv = (XPVHV*)SvANY(hv);
1627 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1628 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1629 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1630 if (xhv->xhv_array /* HvARRAY(hv) */)
1631 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1632 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1637 HvHASKFLAGS_off(hv);
1641 S_hfreeentries(pTHX_ HV *hv)
1643 register HE **array;
1645 register HE *oentry = Null(HE*);
1656 array = HvARRAY(hv);
1661 entry = HeNEXT(entry);
1662 hv_free_ent(hv, oentry);
1667 entry = array[riter];
1670 (void)hv_iterinit(hv);
1674 =for apidoc hv_undef
1682 Perl_hv_undef(pTHX_ HV *hv)
1684 register XPVHV* xhv;
1687 xhv = (XPVHV*)SvANY(hv);
1689 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1691 Safefree(HvNAME(hv));
1694 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1695 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1696 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1697 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1698 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1705 =for apidoc hv_iterinit
1707 Prepares a starting point to traverse a hash table. Returns the number of
1708 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1709 currently only meaningful for hashes without tie magic.
1711 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1712 hash buckets that happen to be in use. If you still need that esoteric
1713 value, you can get it through the macro C<HvFILL(tb)>.
1719 Perl_hv_iterinit(pTHX_ HV *hv)
1721 register XPVHV* xhv;
1725 Perl_croak(aTHX_ "Bad hash");
1726 xhv = (XPVHV*)SvANY(hv);
1727 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1728 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1730 hv_free_ent(hv, entry);
1732 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1733 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1734 /* used to be xhv->xhv_fill before 5.004_65 */
1735 return XHvTOTALKEYS(xhv);
1739 =for apidoc hv_iternext
1741 Returns entries from a hash iterator. See C<hv_iterinit>.
1747 Perl_hv_iternext(pTHX_ HV *hv)
1749 register XPVHV* xhv;
1755 Perl_croak(aTHX_ "Bad hash");
1756 xhv = (XPVHV*)SvANY(hv);
1757 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1759 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1760 SV *key = sv_newmortal();
1762 sv_setsv(key, HeSVKEY_force(entry));
1763 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1769 /* one HE per MAGICAL hash */
1770 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1772 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1774 HeKEY_hek(entry) = hek;
1775 HeKLEN(entry) = HEf_SVKEY;
1777 magic_nextpack((SV*) hv,mg,key);
1779 /* force key to stay around until next time */
1780 HeSVKEY_set(entry, SvREFCNT_inc(key));
1781 return entry; /* beware, hent_val is not set */
1784 SvREFCNT_dec(HeVAL(entry));
1785 Safefree(HeKEY_hek(entry));
1787 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1790 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1791 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1795 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1796 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1797 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1801 entry = HeNEXT(entry);
1803 * Skip past any placeholders -- don't want to include them in
1806 while (entry && HeVAL(entry) == &PL_sv_undef) {
1807 entry = HeNEXT(entry);
1811 xhv->xhv_riter++; /* HvRITER(hv)++ */
1812 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1813 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1816 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1817 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1819 /* if we have an entry, but it's a placeholder, don't count it */
1820 if (entry && HeVAL(entry) == &PL_sv_undef)
1825 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1827 hv_free_ent(hv, oldentry);
1830 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1835 =for apidoc hv_iterkey
1837 Returns the key from the current position of the hash iterator. See
1844 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1846 if (HeKLEN(entry) == HEf_SVKEY) {
1848 char *p = SvPV(HeKEY_sv(entry), len);
1853 *retlen = HeKLEN(entry);
1854 return HeKEY(entry);
1858 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1860 =for apidoc hv_iterkeysv
1862 Returns the key as an C<SV*> from the current position of the hash
1863 iterator. The return value will always be a mortal copy of the key. Also
1870 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1872 if (HeKLEN(entry) != HEf_SVKEY) {
1873 HEK *hek = HeKEY_hek(entry);
1874 int flags = HEK_FLAGS(hek);
1877 if (flags & HVhek_WASUTF8) {
1879 Andreas would like keys he put in as utf8 to come back as utf8
1881 STRLEN utf8_len = HEK_LEN(hek);
1882 U8 *as_utf8 = bytes_to_utf8 (HEK_KEY(hek), &utf8_len);
1884 sv = newSVpvn (as_utf8, utf8_len);
1887 sv = newSVpvn_share(HEK_KEY(hek),
1888 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1891 return sv_2mortal(sv);
1893 return sv_mortalcopy(HeKEY_sv(entry));
1897 =for apidoc hv_iterval
1899 Returns the value from the current position of the hash iterator. See
1906 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1908 if (SvRMAGICAL(hv)) {
1909 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1910 SV* sv = sv_newmortal();
1911 if (HeKLEN(entry) == HEf_SVKEY)
1912 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1913 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1917 return HeVAL(entry);
1921 =for apidoc hv_iternextsv
1923 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1930 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1933 if ( (he = hv_iternext(hv)) == NULL)
1935 *key = hv_iterkey(he, retlen);
1936 return hv_iterval(hv, he);
1940 =for apidoc hv_magic
1942 Adds magic to a hash. See C<sv_magic>.
1948 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1950 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1953 #if 0 /* use the macro from hv.h instead */
1956 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1958 return HEK_KEY(share_hek(sv, len, hash));
1963 /* possibly free a shared string if no one has access to it
1964 * len and hash must both be valid for str.
1967 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1969 unshare_hek_or_pvn (NULL, str, len, hash);
1974 Perl_unshare_hek(pTHX_ HEK *hek)
1976 unshare_hek_or_pvn(hek, NULL, 0, 0);
1979 /* possibly free a shared string if no one has access to it
1980 hek if non-NULL takes priority over the other 3, else str, len and hash
1981 are used. If so, len and hash must both be valid for str.
1984 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1986 register XPVHV* xhv;
1988 register HE **oentry;
1991 bool is_utf8 = FALSE;
1993 const char *save = str;
1996 hash = HEK_HASH(hek);
1997 } else if (len < 0) {
1998 STRLEN tmplen = -len;
2000 /* See the note in hv_fetch(). --jhi */
2001 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2004 k_flags = HVhek_UTF8;
2006 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2009 /* what follows is the moral equivalent of:
2010 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2011 if (--*Svp == Nullsv)
2012 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2014 xhv = (XPVHV*)SvANY(PL_strtab);
2015 /* assert(xhv_array != 0) */
2017 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2018 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2020 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2021 if (HeKEY_hek(entry) != hek)
2027 int flags_masked = k_flags & HVhek_MASK;
2028 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2029 if (HeHASH(entry) != hash) /* strings can't be equal */
2031 if (HeKLEN(entry) != len)
2033 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2035 if (HeKFLAGS(entry) != flags_masked)
2043 if (--HeVAL(entry) == Nullsv) {
2044 *oentry = HeNEXT(entry);
2046 xhv->xhv_fill--; /* HvFILL(hv)-- */
2047 Safefree(HeKEY_hek(entry));
2049 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2053 UNLOCK_STRTAB_MUTEX;
2054 if (!found && ckWARN_d(WARN_INTERNAL))
2055 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2056 "Attempt to free non-existent shared string '%s'%s",
2057 hek ? HEK_KEY(hek) : str,
2058 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2059 if (k_flags & HVhek_FREEKEY)
2063 /* get a (constant) string ptr from the global string table
2064 * string will get added if it is not already there.
2065 * len and hash must both be valid for str.
2068 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2070 bool is_utf8 = FALSE;
2072 const char *save = str;
2075 STRLEN tmplen = -len;
2077 /* See the note in hv_fetch(). --jhi */
2078 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2080 /* If we were able to downgrade here, then than means that we were passed
2081 in a key which only had chars 0-255, but was utf8 encoded. */
2084 /* If we found we were able to downgrade the string to bytes, then
2085 we should flag that it needs upgrading on keys or each. Also flag
2086 that we need share_hek_flags to free the string. */
2088 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2091 return share_hek_flags (str, len, hash, flags);
2095 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2097 register XPVHV* xhv;
2099 register HE **oentry;
2102 int flags_masked = flags & HVhek_MASK;
2104 /* what follows is the moral equivalent of:
2106 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2107 hv_store(PL_strtab, str, len, Nullsv, hash);
2109 xhv = (XPVHV*)SvANY(PL_strtab);
2110 /* assert(xhv_array != 0) */
2112 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2113 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2114 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2115 if (HeHASH(entry) != hash) /* strings can't be equal */
2117 if (HeKLEN(entry) != len)
2119 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2121 if (HeKFLAGS(entry) != flags_masked)
2128 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2129 HeVAL(entry) = Nullsv;
2130 HeNEXT(entry) = *oentry;
2132 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2133 if (i) { /* initial entry? */
2134 xhv->xhv_fill++; /* HvFILL(hv)++ */
2135 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2140 ++HeVAL(entry); /* use value slot as REFCNT */
2141 UNLOCK_STRTAB_MUTEX;
2143 if (flags & HVhek_FREEKEY)
2146 return HeKEY_hek(entry);