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) != (I32)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 if SvIsCOW_shared_hash(keysv) {
416 PERL_HASH(hash, key, klen);
420 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
421 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
422 for (; entry; entry = HeNEXT(entry)) {
423 if (HeHASH(entry) != hash) /* strings can't be equal */
425 if (HeKLEN(entry) != (I32)klen)
427 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
429 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
431 if (lval && HeKFLAGS(entry) != flags) {
432 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
433 But if entry was set previously with HVhek_WASUTF8 and key now
434 doesn't (or vice versa) then we should change the key's flag,
435 as this is assignment. */
436 if (HvSHAREKEYS(hv)) {
437 /* Need to swap the key we have for a key with the flags we
438 need. As keys are shared we can't just write to the flag,
439 so we share the new one, unshare the old one. */
440 int flags_nofree = flags & ~HVhek_FREEKEY;
441 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
442 unshare_hek (HeKEY_hek(entry));
443 HeKEY_hek(entry) = new_hek;
446 HeKFLAGS(entry) = flags;
450 /* if we find a placeholder, we pretend we haven't found anything */
451 if (HeVAL(entry) == &PL_sv_undef)
455 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
456 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
458 char *env = PerlEnv_ENVgetenv_len(key,&len);
460 sv = newSVpvn(env,len);
462 return hv_store_ent(hv,keysv,sv,hash);
466 if (!entry && SvREADONLY(hv)) {
467 S_hv_notallowed(aTHX_ flags, key, klen,
468 "access disallowed key '%"SVf"' in"
471 if (flags & HVhek_FREEKEY)
473 if (lval) { /* gonna assign to this, so it better be there */
475 return hv_store_ent(hv,keysv,sv,hash);
481 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
483 MAGIC *mg = SvMAGIC(hv);
487 if (isUPPER(mg->mg_type)) {
489 switch (mg->mg_type) {
490 case PERL_MAGIC_tied:
492 *needs_store = FALSE;
495 mg = mg->mg_moremagic;
502 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
503 the length of the key. The C<hash> parameter is the precomputed hash
504 value; if it is zero then Perl will compute it. The return value will be
505 NULL if the operation failed or if the value did not need to be actually
506 stored within the hash (as in the case of tied hashes). Otherwise it can
507 be dereferenced to get the original C<SV*>. Note that the caller is
508 responsible for suitably incrementing the reference count of C<val> before
509 the call, and decrementing it if the function returned NULL.
511 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
512 information on how to use this function on tied hashes.
518 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
520 bool is_utf8 = FALSE;
521 const char *keysave = key;
530 STRLEN tmplen = klen;
531 /* Just casting the &klen to (STRLEN) won't work well
532 * if STRLEN and I32 are of different widths. --jhi */
533 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
535 /* If we were able to downgrade here, then than means that we were
536 passed in a key which only had chars 0-255, but was utf8 encoded. */
539 /* If we found we were able to downgrade the string to bytes, then
540 we should flag that it needs upgrading on keys or each. */
542 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
545 return hv_store_flags (hv, key, klen, val, hash, flags);
549 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
550 register U32 hash, int flags)
555 register HE **oentry;
560 xhv = (XPVHV*)SvANY(hv);
564 hv_magic_check (hv, &needs_copy, &needs_store);
566 mg_copy((SV*)hv, val, key, klen);
567 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
568 if (flags & HVhek_FREEKEY)
572 #ifdef ENV_IS_CASELESS
573 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
574 key = savepvn(key,klen);
575 key = (const char*)strupr((char*)key);
583 HvHASKFLAGS_on((SV*)hv);
586 PERL_HASH(hash, key, klen);
588 if (!xhv->xhv_array /* !HvARRAY(hv) */)
589 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
590 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
593 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
594 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
597 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
598 if (HeHASH(entry) != hash) /* strings can't be equal */
600 if (HeKLEN(entry) != (I32)klen)
602 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
604 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
606 if (HeVAL(entry) == &PL_sv_undef)
607 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
609 SvREFCNT_dec(HeVAL(entry));
610 if (flags & HVhek_PLACEHOLD) {
611 /* We have been requested to insert a placeholder. Currently
612 only Storable is allowed to do this. */
613 xhv->xhv_placeholders++;
614 HeVAL(entry) = &PL_sv_undef;
618 if (HeKFLAGS(entry) != flags) {
619 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
620 But if entry was set previously with HVhek_WASUTF8 and key now
621 doesn't (or vice versa) then we should change the key's flag,
622 as this is assignment. */
623 if (HvSHAREKEYS(hv)) {
624 /* Need to swap the key we have for a key with the flags we
625 need. As keys are shared we can't just write to the flag,
626 so we share the new one, unshare the old one. */
627 int flags_nofree = flags & ~HVhek_FREEKEY;
628 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
629 unshare_hek (HeKEY_hek(entry));
630 HeKEY_hek(entry) = new_hek;
633 HeKFLAGS(entry) = flags;
635 if (flags & HVhek_FREEKEY)
637 return &HeVAL(entry);
640 if (SvREADONLY(hv)) {
641 S_hv_notallowed(aTHX_ flags, key, klen,
642 "access disallowed key '%"SVf"' to"
647 /* share_hek_flags will do the free for us. This might be considered
650 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
651 else /* gotta do the real thing */
652 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
653 if (flags & HVhek_PLACEHOLD) {
654 /* We have been requested to insert a placeholder. Currently
655 only Storable is allowed to do this. */
656 xhv->xhv_placeholders++;
657 HeVAL(entry) = &PL_sv_undef;
660 HeNEXT(entry) = *oentry;
663 xhv->xhv_keys++; /* HvKEYS(hv)++ */
664 if (i) { /* initial entry? */
665 xhv->xhv_fill++; /* HvFILL(hv)++ */
666 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
670 return &HeVAL(entry);
674 =for apidoc hv_store_ent
676 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
677 parameter is the precomputed hash value; if it is zero then Perl will
678 compute it. The return value is the new hash entry so created. It will be
679 NULL if the operation failed or if the value did not need to be actually
680 stored within the hash (as in the case of tied hashes). Otherwise the
681 contents of the return value can be accessed using the C<He?> macros
682 described here. Note that the caller is responsible for suitably
683 incrementing the reference count of C<val> before the call, and
684 decrementing it if the function returned NULL.
686 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
687 information on how to use this function on tied hashes.
693 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
708 xhv = (XPVHV*)SvANY(hv);
712 hv_magic_check (hv, &needs_copy, &needs_store);
714 bool save_taint = PL_tainted;
716 PL_tainted = SvTAINTED(keysv);
717 keysv = sv_2mortal(newSVsv(keysv));
718 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
719 TAINT_IF(save_taint);
720 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
722 #ifdef ENV_IS_CASELESS
723 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
724 key = SvPV(keysv, klen);
725 keysv = sv_2mortal(newSVpvn(key,klen));
726 (void)strupr(SvPVX(keysv));
733 keysave = key = SvPV(keysv, klen);
734 is_utf8 = (SvUTF8(keysv) != 0);
737 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
741 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
742 HvHASKFLAGS_on((SV*)hv);
746 if SvIsCOW_shared_hash(keysv) {
749 PERL_HASH(hash, key, klen);
753 if (!xhv->xhv_array /* !HvARRAY(hv) */)
754 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
755 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
758 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
759 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
762 for (; entry; i=0, entry = HeNEXT(entry)) {
763 if (HeHASH(entry) != hash) /* strings can't be equal */
765 if (HeKLEN(entry) != (I32)klen)
767 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
769 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
771 if (HeVAL(entry) == &PL_sv_undef)
772 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
774 SvREFCNT_dec(HeVAL(entry));
776 if (HeKFLAGS(entry) != flags) {
777 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
778 But if entry was set previously with HVhek_WASUTF8 and key now
779 doesn't (or vice versa) then we should change the key's flag,
780 as this is assignment. */
781 if (HvSHAREKEYS(hv)) {
782 /* Need to swap the key we have for a key with the flags we
783 need. As keys are shared we can't just write to the flag,
784 so we share the new one, unshare the old one. */
785 int flags_nofree = flags & ~HVhek_FREEKEY;
786 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
787 unshare_hek (HeKEY_hek(entry));
788 HeKEY_hek(entry) = new_hek;
791 HeKFLAGS(entry) = flags;
793 if (flags & HVhek_FREEKEY)
798 if (SvREADONLY(hv)) {
799 S_hv_notallowed(aTHX_ flags, key, klen,
800 "access disallowed key '%"SVf"' to"
805 /* share_hek_flags will do the free for us. This might be considered
808 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
809 else /* gotta do the real thing */
810 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
812 HeNEXT(entry) = *oentry;
815 xhv->xhv_keys++; /* HvKEYS(hv)++ */
816 if (i) { /* initial entry? */
817 xhv->xhv_fill++; /* HvFILL(hv)++ */
818 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
826 =for apidoc hv_delete
828 Deletes a key/value pair in the hash. The value SV is removed from the
829 hash and returned to the caller. The C<klen> is the length of the key.
830 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
837 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
843 register HE **oentry;
846 bool is_utf8 = FALSE;
848 const char *keysave = key;
856 if (SvRMAGICAL(hv)) {
859 hv_magic_check (hv, &needs_copy, &needs_store);
861 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
865 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
866 /* No longer an element */
867 sv_unmagic(sv, PERL_MAGIC_tiedelem);
870 return Nullsv; /* element cannot be deleted */
872 #ifdef ENV_IS_CASELESS
873 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
874 sv = sv_2mortal(newSVpvn(key,klen));
875 key = strupr(SvPVX(sv));
880 xhv = (XPVHV*)SvANY(hv);
881 if (!xhv->xhv_array /* !HvARRAY(hv) */)
885 STRLEN tmplen = klen;
886 /* See the note in hv_fetch(). --jhi */
887 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
890 k_flags = HVhek_UTF8;
892 k_flags |= HVhek_FREEKEY;
895 PERL_HASH(hash, key, klen);
897 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
898 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
901 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
902 if (HeHASH(entry) != hash) /* strings can't be equal */
904 if (HeKLEN(entry) != (I32)klen)
906 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
908 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
910 if (k_flags & HVhek_FREEKEY)
912 /* if placeholder is here, it's already been deleted.... */
913 if (HeVAL(entry) == &PL_sv_undef)
916 return Nullsv; /* if still SvREADONLY, leave it deleted. */
918 /* okay, really delete the placeholder... */
919 *oentry = HeNEXT(entry);
921 xhv->xhv_fill--; /* HvFILL(hv)-- */
922 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
925 hv_free_ent(hv, entry);
926 xhv->xhv_keys--; /* HvKEYS(hv)-- */
927 if (xhv->xhv_keys == 0)
929 xhv->xhv_placeholders--;
933 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
934 S_hv_notallowed(aTHX_ k_flags, key, klen,
935 "delete readonly key '%"SVf"' from"
939 if (flags & G_DISCARD)
942 sv = sv_2mortal(HeVAL(entry));
943 HeVAL(entry) = &PL_sv_undef;
947 * If a restricted hash, rather than really deleting the entry, put
948 * a placeholder there. This marks the key as being "approved", so
949 * we can still access via not-really-existing key without raising
952 if (SvREADONLY(hv)) {
953 HeVAL(entry) = &PL_sv_undef;
954 /* We'll be saving this slot, so the number of allocated keys
955 * doesn't go down, but the number placeholders goes up */
956 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
958 *oentry = HeNEXT(entry);
960 xhv->xhv_fill--; /* HvFILL(hv)-- */
961 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
964 hv_free_ent(hv, entry);
965 xhv->xhv_keys--; /* HvKEYS(hv)-- */
966 if (xhv->xhv_keys == 0)
971 if (SvREADONLY(hv)) {
972 S_hv_notallowed(aTHX_ k_flags, key, klen,
973 "access disallowed key '%"SVf"' from"
977 if (k_flags & HVhek_FREEKEY)
983 =for apidoc hv_delete_ent
985 Deletes a key/value pair in the hash. The value SV is removed from the
986 hash and returned to the caller. The C<flags> value will normally be zero;
987 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
988 precomputed hash value, or 0 to ask for it to be computed.
994 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
1001 register HE **oentry;
1009 if (SvRMAGICAL(hv)) {
1012 hv_magic_check (hv, &needs_copy, &needs_store);
1014 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
1018 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
1019 /* No longer an element */
1020 sv_unmagic(sv, PERL_MAGIC_tiedelem);
1023 return Nullsv; /* element cannot be deleted */
1025 #ifdef ENV_IS_CASELESS
1026 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1027 key = SvPV(keysv, klen);
1028 keysv = sv_2mortal(newSVpvn(key,klen));
1029 (void)strupr(SvPVX(keysv));
1035 xhv = (XPVHV*)SvANY(hv);
1036 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1039 keysave = key = SvPV(keysv, klen);
1040 is_utf8 = (SvUTF8(keysv) != 0);
1043 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1045 k_flags = HVhek_UTF8;
1047 k_flags |= HVhek_FREEKEY;
1051 PERL_HASH(hash, key, klen);
1053 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1054 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1057 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1058 if (HeHASH(entry) != hash) /* strings can't be equal */
1060 if (HeKLEN(entry) != (I32)klen)
1062 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1064 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1066 if (k_flags & HVhek_FREEKEY)
1069 /* if placeholder is here, it's already been deleted.... */
1070 if (HeVAL(entry) == &PL_sv_undef)
1073 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1075 /* okay, really delete the placeholder. */
1076 *oentry = HeNEXT(entry);
1078 xhv->xhv_fill--; /* HvFILL(hv)-- */
1079 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1082 hv_free_ent(hv, entry);
1083 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1084 if (xhv->xhv_keys == 0)
1085 HvHASKFLAGS_off(hv);
1086 xhv->xhv_placeholders--;
1089 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1090 S_hv_notallowed(aTHX_ k_flags, key, klen,
1091 "delete readonly key '%"SVf"' from"
1095 if (flags & G_DISCARD)
1098 sv = sv_2mortal(HeVAL(entry));
1099 HeVAL(entry) = &PL_sv_undef;
1103 * If a restricted hash, rather than really deleting the entry, put
1104 * a placeholder there. This marks the key as being "approved", so
1105 * we can still access via not-really-existing key without raising
1108 if (SvREADONLY(hv)) {
1109 HeVAL(entry) = &PL_sv_undef;
1110 /* We'll be saving this slot, so the number of allocated keys
1111 * doesn't go down, but the number placeholders goes up */
1112 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1114 *oentry = HeNEXT(entry);
1116 xhv->xhv_fill--; /* HvFILL(hv)-- */
1117 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1120 hv_free_ent(hv, entry);
1121 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1122 if (xhv->xhv_keys == 0)
1123 HvHASKFLAGS_off(hv);
1127 if (SvREADONLY(hv)) {
1128 S_hv_notallowed(aTHX_ k_flags, key, klen,
1129 "delete disallowed key '%"SVf"' from"
1133 if (k_flags & HVhek_FREEKEY)
1139 =for apidoc hv_exists
1141 Returns a boolean indicating whether the specified hash key exists. The
1142 C<klen> is the length of the key.
1148 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1150 register XPVHV* xhv;
1154 bool is_utf8 = FALSE;
1155 const char *keysave = key;
1166 if (SvRMAGICAL(hv)) {
1167 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1168 sv = sv_newmortal();
1169 mg_copy((SV*)hv, sv, key, klen);
1170 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1171 return (bool)SvTRUE(sv);
1173 #ifdef ENV_IS_CASELESS
1174 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1175 sv = sv_2mortal(newSVpvn(key,klen));
1176 key = strupr(SvPVX(sv));
1181 xhv = (XPVHV*)SvANY(hv);
1182 #ifndef DYNAMIC_ENV_FETCH
1183 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1188 STRLEN tmplen = klen;
1189 /* See the note in hv_fetch(). --jhi */
1190 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1193 k_flags = HVhek_UTF8;
1195 k_flags |= HVhek_FREEKEY;
1198 PERL_HASH(hash, key, klen);
1200 #ifdef DYNAMIC_ENV_FETCH
1201 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1204 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1205 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1206 for (; entry; entry = HeNEXT(entry)) {
1207 if (HeHASH(entry) != hash) /* strings can't be equal */
1209 if (HeKLEN(entry) != klen)
1211 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1213 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1215 if (k_flags & HVhek_FREEKEY)
1217 /* If we find the key, but the value is a placeholder, return false. */
1218 if (HeVAL(entry) == &PL_sv_undef)
1223 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1224 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1226 char *env = PerlEnv_ENVgetenv_len(key,&len);
1228 sv = newSVpvn(env,len);
1230 (void)hv_store(hv,key,klen,sv,hash);
1231 if (k_flags & HVhek_FREEKEY)
1237 if (k_flags & HVhek_FREEKEY)
1244 =for apidoc hv_exists_ent
1246 Returns a boolean indicating whether the specified hash key exists. C<hash>
1247 can be a valid precomputed hash value, or 0 to ask for it to be
1254 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1256 register XPVHV* xhv;
1268 if (SvRMAGICAL(hv)) {
1269 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1270 SV* svret = sv_newmortal();
1271 sv = sv_newmortal();
1272 keysv = sv_2mortal(newSVsv(keysv));
1273 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1274 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1275 return (bool)SvTRUE(svret);
1277 #ifdef ENV_IS_CASELESS
1278 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1279 key = SvPV(keysv, klen);
1280 keysv = sv_2mortal(newSVpvn(key,klen));
1281 (void)strupr(SvPVX(keysv));
1287 xhv = (XPVHV*)SvANY(hv);
1288 #ifndef DYNAMIC_ENV_FETCH
1289 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1293 keysave = key = SvPV(keysv, klen);
1294 is_utf8 = (SvUTF8(keysv) != 0);
1296 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1298 k_flags = HVhek_UTF8;
1300 k_flags |= HVhek_FREEKEY;
1303 PERL_HASH(hash, key, klen);
1305 #ifdef DYNAMIC_ENV_FETCH
1306 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1309 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1310 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1311 for (; entry; entry = HeNEXT(entry)) {
1312 if (HeHASH(entry) != hash) /* strings can't be equal */
1314 if (HeKLEN(entry) != (I32)klen)
1316 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1318 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1320 if (k_flags & HVhek_FREEKEY)
1322 /* If we find the key, but the value is a placeholder, return false. */
1323 if (HeVAL(entry) == &PL_sv_undef)
1327 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1328 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1330 char *env = PerlEnv_ENVgetenv_len(key,&len);
1332 sv = newSVpvn(env,len);
1334 (void)hv_store_ent(hv,keysv,sv,hash);
1335 if (k_flags & HVhek_FREEKEY)
1341 if (k_flags & HVhek_FREEKEY)
1347 S_hsplit(pTHX_ HV *hv)
1349 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1350 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1351 register I32 newsize = oldsize * 2;
1353 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1357 register HE **oentry;
1360 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1361 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1367 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1372 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1373 if (oldsize >= 64) {
1374 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1375 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1378 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1382 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1383 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1384 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1387 for (i=0; i<oldsize; i++,aep++) {
1388 if (!*aep) /* non-existent */
1391 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1392 if ((HeHASH(entry) & newsize) != (U32)i) {
1393 *oentry = HeNEXT(entry);
1394 HeNEXT(entry) = *bep;
1396 xhv->xhv_fill++; /* HvFILL(hv)++ */
1401 oentry = &HeNEXT(entry);
1403 if (!*aep) /* everything moved */
1404 xhv->xhv_fill--; /* HvFILL(hv)-- */
1409 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1411 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1412 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1413 register I32 newsize;
1419 register HE **oentry;
1421 newsize = (I32) newmax; /* possible truncation here */
1422 if (newsize != newmax || newmax <= oldsize)
1424 while ((newsize & (1 + ~newsize)) != newsize) {
1425 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1427 if (newsize < newmax)
1429 if (newsize < newmax)
1430 return; /* overflow detection */
1432 a = xhv->xhv_array; /* HvARRAY(hv) */
1435 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1436 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1442 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1447 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1448 if (oldsize >= 64) {
1449 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1450 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1453 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1456 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1459 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1461 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1462 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1463 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1467 for (i=0; i<oldsize; i++,aep++) {
1468 if (!*aep) /* non-existent */
1470 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1471 if ((j = (HeHASH(entry) & newsize)) != i) {
1473 *oentry = HeNEXT(entry);
1474 if (!(HeNEXT(entry) = aep[j]))
1475 xhv->xhv_fill++; /* HvFILL(hv)++ */
1480 oentry = &HeNEXT(entry);
1482 if (!*aep) /* everything moved */
1483 xhv->xhv_fill--; /* HvFILL(hv)-- */
1490 Creates a new HV. The reference count is set to 1.
1499 register XPVHV* xhv;
1501 hv = (HV*)NEWSV(502,0);
1502 sv_upgrade((SV *)hv, SVt_PVHV);
1503 xhv = (XPVHV*)SvANY(hv);
1506 #ifndef NODEFAULT_SHAREKEYS
1507 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1509 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1510 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1511 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1512 (void)hv_iterinit(hv); /* so each() will start off right */
1517 Perl_newHVhv(pTHX_ HV *ohv)
1520 STRLEN hv_max, hv_fill;
1522 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1524 hv_max = HvMAX(ohv);
1526 if (!SvMAGICAL((SV *)ohv)) {
1527 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1529 bool shared = !!HvSHAREKEYS(ohv);
1530 HE **ents, **oents = (HE **)HvARRAY(ohv);
1532 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1535 /* In each bucket... */
1536 for (i = 0; i <= hv_max; i++) {
1537 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1544 /* Copy the linked list of entries. */
1545 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1546 U32 hash = HeHASH(oent);
1547 char *key = HeKEY(oent);
1548 STRLEN len = HeKLEN(oent);
1549 int flags = HeKFLAGS(oent);
1552 HeVAL(ent) = newSVsv(HeVAL(oent));
1554 = shared ? share_hek_flags(key, len, hash, flags)
1555 : save_hek_flags(key, len, hash, flags);
1566 HvFILL(hv) = hv_fill;
1567 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1571 /* Iterate over ohv, copying keys and values one at a time. */
1573 I32 riter = HvRITER(ohv);
1574 HE *eiter = HvEITER(ohv);
1576 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1577 while (hv_max && hv_max + 1 >= hv_fill * 2)
1578 hv_max = hv_max / 2;
1582 while ((entry = hv_iternext_flags(ohv, 0))) {
1583 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1584 newSVsv(HeVAL(entry)), HeHASH(entry),
1587 HvRITER(ohv) = riter;
1588 HvEITER(ohv) = eiter;
1595 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1602 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1603 PL_sub_generation++; /* may be deletion of method from stash */
1605 if (HeKLEN(entry) == HEf_SVKEY) {
1606 SvREFCNT_dec(HeKEY_sv(entry));
1607 Safefree(HeKEY_hek(entry));
1609 else if (HvSHAREKEYS(hv))
1610 unshare_hek(HeKEY_hek(entry));
1612 Safefree(HeKEY_hek(entry));
1617 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1621 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1622 PL_sub_generation++; /* may be deletion of method from stash */
1623 sv_2mortal(HeVAL(entry)); /* free between statements */
1624 if (HeKLEN(entry) == HEf_SVKEY) {
1625 sv_2mortal(HeKEY_sv(entry));
1626 Safefree(HeKEY_hek(entry));
1628 else if (HvSHAREKEYS(hv))
1629 unshare_hek(HeKEY_hek(entry));
1631 Safefree(HeKEY_hek(entry));
1636 =for apidoc hv_clear
1638 Clears a hash, making it empty.
1644 Perl_hv_clear(pTHX_ HV *hv)
1646 register XPVHV* xhv;
1650 if(SvREADONLY(hv)) {
1651 Perl_croak(aTHX_ "Attempt to clear a restricted hash");
1654 xhv = (XPVHV*)SvANY(hv);
1656 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1657 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1658 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1659 if (xhv->xhv_array /* HvARRAY(hv) */)
1660 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1661 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1666 HvHASKFLAGS_off(hv);
1670 S_hfreeentries(pTHX_ HV *hv)
1672 register HE **array;
1674 register HE *oentry = Null(HE*);
1685 array = HvARRAY(hv);
1690 entry = HeNEXT(entry);
1691 hv_free_ent(hv, oentry);
1696 entry = array[riter];
1699 (void)hv_iterinit(hv);
1703 =for apidoc hv_undef
1711 Perl_hv_undef(pTHX_ HV *hv)
1713 register XPVHV* xhv;
1716 xhv = (XPVHV*)SvANY(hv);
1718 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1720 Safefree(HvNAME(hv));
1723 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1724 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1725 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1726 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1727 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1734 =for apidoc hv_iterinit
1736 Prepares a starting point to traverse a hash table. Returns the number of
1737 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1738 currently only meaningful for hashes without tie magic.
1740 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1741 hash buckets that happen to be in use. If you still need that esoteric
1742 value, you can get it through the macro C<HvFILL(tb)>.
1749 Perl_hv_iterinit(pTHX_ HV *hv)
1751 register XPVHV* xhv;
1755 Perl_croak(aTHX_ "Bad hash");
1756 xhv = (XPVHV*)SvANY(hv);
1757 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1758 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1760 hv_free_ent(hv, entry);
1762 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1763 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1764 /* used to be xhv->xhv_fill before 5.004_65 */
1765 return XHvTOTALKEYS(xhv);
1768 =for apidoc hv_iternext
1770 Returns entries from a hash iterator. See C<hv_iterinit>.
1772 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1773 iterator currently points to, without losing your place or invalidating your
1774 iterator. Note that in this case the current entry is deleted from the hash
1775 with your iterator holding the last reference to it. Your iterator is flagged
1776 to free the entry on the next call to C<hv_iternext>, so you must not discard
1777 your iterator immediately else the entry will leak - call C<hv_iternext> to
1778 trigger the resource deallocation.
1784 Perl_hv_iternext(pTHX_ HV *hv)
1786 return hv_iternext_flags(hv, 0);
1790 =for apidoc hv_iternext_flags
1792 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1793 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1794 set the placeholders keys (for restricted hashes) will be returned in addition
1795 to normal keys. By default placeholders are automatically skipped over.
1796 Currently a placeholder is implemented with a value that is literally
1797 <&Perl_sv_undef> (a regular C<undef> value is a normal read-write SV for which
1798 C<!SvOK> is false). Note that the implementation of placeholders and
1799 restricted hashes may change, and the implementation currently is
1800 insufficiently abstracted for any change to be tidy.
1806 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1808 register XPVHV* xhv;
1814 Perl_croak(aTHX_ "Bad hash");
1815 xhv = (XPVHV*)SvANY(hv);
1816 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1818 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1819 SV *key = sv_newmortal();
1821 sv_setsv(key, HeSVKEY_force(entry));
1822 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1828 /* one HE per MAGICAL hash */
1829 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1831 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1833 HeKEY_hek(entry) = hek;
1834 HeKLEN(entry) = HEf_SVKEY;
1836 magic_nextpack((SV*) hv,mg,key);
1838 /* force key to stay around until next time */
1839 HeSVKEY_set(entry, SvREFCNT_inc(key));
1840 return entry; /* beware, hent_val is not set */
1843 SvREFCNT_dec(HeVAL(entry));
1844 Safefree(HeKEY_hek(entry));
1846 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1849 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1850 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1854 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1855 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1856 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1860 entry = HeNEXT(entry);
1861 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1863 * Skip past any placeholders -- don't want to include them in
1866 while (entry && HeVAL(entry) == &PL_sv_undef) {
1867 entry = HeNEXT(entry);
1872 xhv->xhv_riter++; /* HvRITER(hv)++ */
1873 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1874 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1877 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1878 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1880 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1881 /* if we have an entry, but it's a placeholder, don't count it */
1882 if (entry && HeVAL(entry) == &PL_sv_undef)
1887 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1889 hv_free_ent(hv, oldentry);
1892 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1897 =for apidoc hv_iterkey
1899 Returns the key from the current position of the hash iterator. See
1906 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1908 if (HeKLEN(entry) == HEf_SVKEY) {
1910 char *p = SvPV(HeKEY_sv(entry), len);
1915 *retlen = HeKLEN(entry);
1916 return HeKEY(entry);
1920 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1922 =for apidoc hv_iterkeysv
1924 Returns the key as an C<SV*> from the current position of the hash
1925 iterator. The return value will always be a mortal copy of the key. Also
1932 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1934 if (HeKLEN(entry) != HEf_SVKEY) {
1935 HEK *hek = HeKEY_hek(entry);
1936 int flags = HEK_FLAGS(hek);
1939 if (flags & HVhek_WASUTF8) {
1941 Andreas would like keys he put in as utf8 to come back as utf8
1943 STRLEN utf8_len = HEK_LEN(hek);
1944 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1946 sv = newSVpvn ((char*)as_utf8, utf8_len);
1948 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1950 sv = newSVpvn_share(HEK_KEY(hek),
1951 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1954 return sv_2mortal(sv);
1956 return sv_mortalcopy(HeKEY_sv(entry));
1960 =for apidoc hv_iterval
1962 Returns the value from the current position of the hash iterator. See
1969 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1971 if (SvRMAGICAL(hv)) {
1972 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1973 SV* sv = sv_newmortal();
1974 if (HeKLEN(entry) == HEf_SVKEY)
1975 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1976 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1980 return HeVAL(entry);
1984 =for apidoc hv_iternextsv
1986 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1993 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1996 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1998 *key = hv_iterkey(he, retlen);
1999 return hv_iterval(hv, he);
2003 =for apidoc hv_magic
2005 Adds magic to a hash. See C<sv_magic>.
2011 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2013 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2016 #if 0 /* use the macro from hv.h instead */
2019 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2021 return HEK_KEY(share_hek(sv, len, hash));
2026 /* possibly free a shared string if no one has access to it
2027 * len and hash must both be valid for str.
2030 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2032 unshare_hek_or_pvn (NULL, str, len, hash);
2037 Perl_unshare_hek(pTHX_ HEK *hek)
2039 unshare_hek_or_pvn(hek, NULL, 0, 0);
2042 /* possibly free a shared string if no one has access to it
2043 hek if non-NULL takes priority over the other 3, else str, len and hash
2044 are used. If so, len and hash must both be valid for str.
2047 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2049 register XPVHV* xhv;
2051 register HE **oentry;
2054 bool is_utf8 = FALSE;
2056 const char *save = str;
2059 hash = HEK_HASH(hek);
2060 } else if (len < 0) {
2061 STRLEN tmplen = -len;
2063 /* See the note in hv_fetch(). --jhi */
2064 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2067 k_flags = HVhek_UTF8;
2069 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2072 /* what follows is the moral equivalent of:
2073 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2074 if (--*Svp == Nullsv)
2075 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2077 xhv = (XPVHV*)SvANY(PL_strtab);
2078 /* assert(xhv_array != 0) */
2080 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2081 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2083 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2084 if (HeKEY_hek(entry) != hek)
2090 int flags_masked = k_flags & HVhek_MASK;
2091 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2092 if (HeHASH(entry) != hash) /* strings can't be equal */
2094 if (HeKLEN(entry) != len)
2096 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2098 if (HeKFLAGS(entry) != flags_masked)
2106 if (--HeVAL(entry) == Nullsv) {
2107 *oentry = HeNEXT(entry);
2109 xhv->xhv_fill--; /* HvFILL(hv)-- */
2110 Safefree(HeKEY_hek(entry));
2112 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2116 UNLOCK_STRTAB_MUTEX;
2117 if (!found && ckWARN_d(WARN_INTERNAL))
2118 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2119 "Attempt to free non-existent shared string '%s'%s",
2120 hek ? HEK_KEY(hek) : str,
2121 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2122 if (k_flags & HVhek_FREEKEY)
2126 /* get a (constant) string ptr from the global string table
2127 * string will get added if it is not already there.
2128 * len and hash must both be valid for str.
2131 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2133 bool is_utf8 = FALSE;
2135 const char *save = str;
2138 STRLEN tmplen = -len;
2140 /* See the note in hv_fetch(). --jhi */
2141 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2143 /* If we were able to downgrade here, then than means that we were passed
2144 in a key which only had chars 0-255, but was utf8 encoded. */
2147 /* If we found we were able to downgrade the string to bytes, then
2148 we should flag that it needs upgrading on keys or each. Also flag
2149 that we need share_hek_flags to free the string. */
2151 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2154 return share_hek_flags (str, len, hash, flags);
2158 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2160 register XPVHV* xhv;
2162 register HE **oentry;
2165 int flags_masked = flags & HVhek_MASK;
2167 /* what follows is the moral equivalent of:
2169 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2170 hv_store(PL_strtab, str, len, Nullsv, hash);
2172 xhv = (XPVHV*)SvANY(PL_strtab);
2173 /* assert(xhv_array != 0) */
2175 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2176 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2177 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2178 if (HeHASH(entry) != hash) /* strings can't be equal */
2180 if (HeKLEN(entry) != len)
2182 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2184 if (HeKFLAGS(entry) != flags_masked)
2191 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2192 HeVAL(entry) = Nullsv;
2193 HeNEXT(entry) = *oentry;
2195 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2196 if (i) { /* initial entry? */
2197 xhv->xhv_fill++; /* HvFILL(hv)++ */
2198 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2203 ++HeVAL(entry); /* use value slot as REFCNT */
2204 UNLOCK_STRTAB_MUTEX;
2206 if (flags & HVhek_FREEKEY)
2209 return HeKEY_hek(entry);