3 * Copyright (c) 1991-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "I sit beside the fire and think of all that I have seen." --Bilbo
15 =head1 Hash Manipulation Functions
30 PL_he_root = HeNEXT(he);
39 HeNEXT(p) = (HE*)PL_he_root;
50 New(54, ptr, 1008/sizeof(XPV), XPV);
51 ptr->xpv_pv = (char*)PL_he_arenaroot;
52 PL_he_arenaroot = ptr;
55 heend = &he[1008 / sizeof(HE) - 1];
58 HeNEXT(he) = (HE*)(he + 1);
66 #define new_HE() (HE*)safemalloc(sizeof(HE))
67 #define del_HE(p) safefree((char*)p)
71 #define new_HE() new_he()
72 #define del_HE(p) del_he(p)
77 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
82 New(54, k, HEK_BASESIZE + len + 2, char);
84 Copy(str, HEK_KEY(hek), len, char);
85 HEK_KEY(hek)[len] = 0;
88 HEK_FLAGS(hek) = (unsigned char)flags;
92 #if defined(USE_ITHREADS)
94 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
100 /* look for it in the table first */
101 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
105 /* create anew and remember what it is */
107 ptr_table_store(PL_ptr_table, e, ret);
109 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
110 if (HeKLEN(e) == HEf_SVKEY)
111 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
113 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
116 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
118 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
121 #endif /* USE_ITHREADS */
124 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
127 SV *sv = sv_newmortal(), *esv = sv_newmortal();
128 if (!(flags & HVhek_FREEKEY)) {
129 sv_setpvn(sv, key, klen);
132 /* Need to free saved eventually assign to mortal SV */
133 SV *sv = sv_newmortal();
134 sv_usepvn(sv, (char *) key, klen);
136 if (flags & HVhek_UTF8) {
139 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
140 Perl_croak(aTHX_ SvPVX(esv), sv);
143 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
149 Returns the SV which corresponds to the specified key in the hash. The
150 C<klen> is the length of the key. If C<lval> is set then the fetch will be
151 part of a store. Check that the return value is non-null before
152 dereferencing it to an C<SV*>.
154 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
155 information on how to use this function on tied hashes.
162 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
164 bool is_utf8 = FALSE;
165 const char *keysave = key;
174 STRLEN tmplen = klen;
175 /* Just casting the &klen to (STRLEN) won't work well
176 * if STRLEN and I32 are of different widths. --jhi */
177 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
179 /* If we were able to downgrade here, then than means that we were
180 passed in a key which only had chars 0-255, but was utf8 encoded. */
183 /* If we found we were able to downgrade the string to bytes, then
184 we should flag that it needs upgrading on keys or each. */
186 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
189 return hv_fetch_flags (hv, key, klen, lval, flags);
193 S_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen, I32 lval, int flags)
203 if (SvRMAGICAL(hv)) {
204 /* All this clause seems to be utf8 unaware.
205 By moving the utf8 stuff out to hv_fetch_flags I need to ensure
206 key doesn't leak. I've not tried solving the utf8-ness.
209 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
211 mg_copy((SV*)hv, sv, key, klen);
212 if (flags & HVhek_FREEKEY)
215 return &PL_hv_fetch_sv;
217 #ifdef ENV_IS_CASELESS
218 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
220 for (i = 0; i < klen; ++i)
221 if (isLOWER(key[i])) {
222 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
223 SV **ret = hv_fetch(hv, nkey, klen, 0);
225 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
227 } else if (flags & HVhek_FREEKEY)
235 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
236 avoid unnecessary pointer dereferencing. */
237 xhv = (XPVHV*)SvANY(hv);
238 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
240 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
241 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
244 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
245 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
248 if (flags & HVhek_FREEKEY)
254 PERL_HASH(hash, key, klen);
256 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
257 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
258 for (; entry; entry = HeNEXT(entry)) {
259 if (HeHASH(entry) != hash) /* strings can't be equal */
261 if (HeKLEN(entry) != klen)
263 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
265 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
266 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
267 xor is true if bits differ, in which case this isn't a match. */
268 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
270 if (lval && HeKFLAGS(entry) != flags) {
271 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
272 But if entry was set previously with HVhek_WASUTF8 and key now
273 doesn't (or vice versa) then we should change the key's flag,
274 as this is assignment. */
275 if (HvSHAREKEYS(hv)) {
276 /* Need to swap the key we have for a key with the flags we
277 need. As keys are shared we can't just write to the flag,
278 so we share the new one, unshare the old one. */
279 int flags_nofree = flags & ~HVhek_FREEKEY;
280 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
281 unshare_hek (HeKEY_hek(entry));
282 HeKEY_hek(entry) = new_hek;
285 HeKFLAGS(entry) = flags;
287 if (flags & HVhek_FREEKEY)
289 /* if we find a placeholder, we pretend we haven't found anything */
290 if (HeVAL(entry) == &PL_sv_undef)
292 return &HeVAL(entry);
295 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
296 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
298 char *env = PerlEnv_ENVgetenv_len(key,&len);
300 sv = newSVpvn(env,len);
302 if (flags & HVhek_FREEKEY)
304 return hv_store(hv,key,klen,sv,hash);
308 if (!entry && SvREADONLY(hv)) {
309 S_hv_notallowed(aTHX_ flags, key, klen,
310 "access disallowed key '%"SVf"' in"
313 if (lval) { /* gonna assign to this, so it better be there */
315 return hv_store_flags(hv,key,klen,sv,hash,flags);
317 if (flags & HVhek_FREEKEY)
322 /* returns an HE * structure with the all fields set */
323 /* note that hent_val will be a mortal sv for MAGICAL hashes */
325 =for apidoc hv_fetch_ent
327 Returns the hash entry which corresponds to the specified key in the hash.
328 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
329 if you want the function to compute it. IF C<lval> is set then the fetch
330 will be part of a store. Make sure the return value is non-null before
331 accessing it. The return value when C<tb> is a tied hash is a pointer to a
332 static location, so be sure to make a copy of the structure if you need to
335 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
336 information on how to use this function on tied hashes.
342 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
356 if (SvRMAGICAL(hv)) {
357 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
359 keysv = sv_2mortal(newSVsv(keysv));
360 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
361 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
363 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
364 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
366 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
367 HeVAL(&PL_hv_fetch_ent_mh) = sv;
368 return &PL_hv_fetch_ent_mh;
370 #ifdef ENV_IS_CASELESS
371 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
373 key = SvPV(keysv, klen);
374 for (i = 0; i < klen; ++i)
375 if (isLOWER(key[i])) {
376 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
377 (void)strupr(SvPVX(nkeysv));
378 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
380 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
387 xhv = (XPVHV*)SvANY(hv);
388 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
390 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
391 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
394 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
395 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
401 keysave = key = SvPV(keysv, klen);
402 is_utf8 = (SvUTF8(keysv)!=0);
405 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
409 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
413 PERL_HASH(hash, key, klen);
415 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
416 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
417 for (; entry; entry = HeNEXT(entry)) {
418 if (HeHASH(entry) != hash) /* strings can't be equal */
420 if (HeKLEN(entry) != klen)
422 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
424 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
426 if (lval && HeKFLAGS(entry) != flags) {
427 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
428 But if entry was set previously with HVhek_WASUTF8 and key now
429 doesn't (or vice versa) then we should change the key's flag,
430 as this is assignment. */
431 if (HvSHAREKEYS(hv)) {
432 /* Need to swap the key we have for a key with the flags we
433 need. As keys are shared we can't just write to the flag,
434 so we share the new one, unshare the old one. */
435 int flags_nofree = flags & ~HVhek_FREEKEY;
436 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
437 unshare_hek (HeKEY_hek(entry));
438 HeKEY_hek(entry) = new_hek;
441 HeKFLAGS(entry) = flags;
445 /* if we find a placeholder, we pretend we haven't found anything */
446 if (HeVAL(entry) == &PL_sv_undef)
450 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
451 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
453 char *env = PerlEnv_ENVgetenv_len(key,&len);
455 sv = newSVpvn(env,len);
457 return hv_store_ent(hv,keysv,sv,hash);
461 if (!entry && SvREADONLY(hv)) {
462 S_hv_notallowed(aTHX_ flags, key, klen,
463 "access disallowed key '%"SVf"' in"
466 if (flags & HVhek_FREEKEY)
468 if (lval) { /* gonna assign to this, so it better be there */
470 return hv_store_ent(hv,keysv,sv,hash);
476 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
478 MAGIC *mg = SvMAGIC(hv);
482 if (isUPPER(mg->mg_type)) {
484 switch (mg->mg_type) {
485 case PERL_MAGIC_tied:
487 *needs_store = FALSE;
490 mg = mg->mg_moremagic;
497 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
498 the length of the key. The C<hash> parameter is the precomputed hash
499 value; if it is zero then Perl will compute it. The return value will be
500 NULL if the operation failed or if the value did not need to be actually
501 stored within the hash (as in the case of tied hashes). Otherwise it can
502 be dereferenced to get the original C<SV*>. Note that the caller is
503 responsible for suitably incrementing the reference count of C<val> before
504 the call, and decrementing it if the function returned NULL.
506 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
507 information on how to use this function on tied hashes.
513 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
515 bool is_utf8 = FALSE;
516 const char *keysave = key;
525 STRLEN tmplen = klen;
526 /* Just casting the &klen to (STRLEN) won't work well
527 * if STRLEN and I32 are of different widths. --jhi */
528 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
530 /* If we were able to downgrade here, then than means that we were
531 passed in a key which only had chars 0-255, but was utf8 encoded. */
534 /* If we found we were able to downgrade the string to bytes, then
535 we should flag that it needs upgrading on keys or each. */
537 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
540 return hv_store_flags (hv, key, klen, val, hash, flags);
544 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
545 register U32 hash, int flags)
550 register HE **oentry;
555 xhv = (XPVHV*)SvANY(hv);
559 hv_magic_check (hv, &needs_copy, &needs_store);
561 mg_copy((SV*)hv, val, key, klen);
562 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
563 if (flags & HVhek_FREEKEY)
567 #ifdef ENV_IS_CASELESS
568 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
569 key = savepvn(key,klen);
570 key = (const char*)strupr((char*)key);
578 HvHASKFLAGS_on((SV*)hv);
581 PERL_HASH(hash, key, klen);
583 if (!xhv->xhv_array /* !HvARRAY(hv) */)
584 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
585 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
588 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
589 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
592 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
593 if (HeHASH(entry) != hash) /* strings can't be equal */
595 if (HeKLEN(entry) != klen)
597 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
599 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
601 if (HeVAL(entry) == &PL_sv_undef)
602 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
604 SvREFCNT_dec(HeVAL(entry));
605 if (flags & HVhek_PLACEHOLD) {
606 /* We have been requested to insert a placeholder. Currently
607 only Storable is allowed to do this. */
608 xhv->xhv_placeholders++;
609 HeVAL(entry) = &PL_sv_undef;
613 if (HeKFLAGS(entry) != flags) {
614 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
615 But if entry was set previously with HVhek_WASUTF8 and key now
616 doesn't (or vice versa) then we should change the key's flag,
617 as this is assignment. */
618 if (HvSHAREKEYS(hv)) {
619 /* Need to swap the key we have for a key with the flags we
620 need. As keys are shared we can't just write to the flag,
621 so we share the new one, unshare the old one. */
622 int flags_nofree = flags & ~HVhek_FREEKEY;
623 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
624 unshare_hek (HeKEY_hek(entry));
625 HeKEY_hek(entry) = new_hek;
628 HeKFLAGS(entry) = flags;
630 if (flags & HVhek_FREEKEY)
632 return &HeVAL(entry);
635 if (SvREADONLY(hv)) {
636 S_hv_notallowed(aTHX_ flags, key, klen,
637 "access disallowed key '%"SVf"' to"
642 /* share_hek_flags will do the free for us. This might be considered
645 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
646 else /* gotta do the real thing */
647 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
648 if (flags & HVhek_PLACEHOLD) {
649 /* We have been requested to insert a placeholder. Currently
650 only Storable is allowed to do this. */
651 xhv->xhv_placeholders++;
652 HeVAL(entry) = &PL_sv_undef;
655 HeNEXT(entry) = *oentry;
658 xhv->xhv_keys++; /* HvKEYS(hv)++ */
659 if (i) { /* initial entry? */
660 xhv->xhv_fill++; /* HvFILL(hv)++ */
661 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
665 return &HeVAL(entry);
669 =for apidoc hv_store_ent
671 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
672 parameter is the precomputed hash value; if it is zero then Perl will
673 compute it. The return value is the new hash entry so created. It will be
674 NULL if the operation failed or if the value did not need to be actually
675 stored within the hash (as in the case of tied hashes). Otherwise the
676 contents of the return value can be accessed using the C<He?> macros
677 described here. Note that the caller is responsible for suitably
678 incrementing the reference count of C<val> before the call, and
679 decrementing it if the function returned NULL.
681 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
682 information on how to use this function on tied hashes.
688 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
703 xhv = (XPVHV*)SvANY(hv);
707 hv_magic_check (hv, &needs_copy, &needs_store);
709 bool save_taint = PL_tainted;
711 PL_tainted = SvTAINTED(keysv);
712 keysv = sv_2mortal(newSVsv(keysv));
713 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
714 TAINT_IF(save_taint);
715 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
717 #ifdef ENV_IS_CASELESS
718 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
719 key = SvPV(keysv, klen);
720 keysv = sv_2mortal(newSVpvn(key,klen));
721 (void)strupr(SvPVX(keysv));
728 keysave = key = SvPV(keysv, klen);
729 is_utf8 = (SvUTF8(keysv) != 0);
732 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
736 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
737 HvHASKFLAGS_on((SV*)hv);
741 PERL_HASH(hash, key, klen);
743 if (!xhv->xhv_array /* !HvARRAY(hv) */)
744 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
745 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
748 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
749 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
752 for (; entry; i=0, entry = HeNEXT(entry)) {
753 if (HeHASH(entry) != hash) /* strings can't be equal */
755 if (HeKLEN(entry) != klen)
757 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
759 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
761 if (HeVAL(entry) == &PL_sv_undef)
762 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
764 SvREFCNT_dec(HeVAL(entry));
766 if (HeKFLAGS(entry) != flags) {
767 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
768 But if entry was set previously with HVhek_WASUTF8 and key now
769 doesn't (or vice versa) then we should change the key's flag,
770 as this is assignment. */
771 if (HvSHAREKEYS(hv)) {
772 /* Need to swap the key we have for a key with the flags we
773 need. As keys are shared we can't just write to the flag,
774 so we share the new one, unshare the old one. */
775 int flags_nofree = flags & ~HVhek_FREEKEY;
776 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
777 unshare_hek (HeKEY_hek(entry));
778 HeKEY_hek(entry) = new_hek;
781 HeKFLAGS(entry) = flags;
783 if (flags & HVhek_FREEKEY)
788 if (SvREADONLY(hv)) {
789 S_hv_notallowed(aTHX_ flags, key, klen,
790 "access disallowed key '%"SVf"' to"
795 /* share_hek_flags will do the free for us. This might be considered
798 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
799 else /* gotta do the real thing */
800 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
802 HeNEXT(entry) = *oentry;
805 xhv->xhv_keys++; /* HvKEYS(hv)++ */
806 if (i) { /* initial entry? */
807 xhv->xhv_fill++; /* HvFILL(hv)++ */
808 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
816 =for apidoc hv_delete
818 Deletes a key/value pair in the hash. The value SV is removed from the
819 hash and returned to the caller. The C<klen> is the length of the key.
820 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
827 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
833 register HE **oentry;
836 bool is_utf8 = FALSE;
838 const char *keysave = key;
846 if (SvRMAGICAL(hv)) {
849 hv_magic_check (hv, &needs_copy, &needs_store);
851 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
855 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
856 /* No longer an element */
857 sv_unmagic(sv, PERL_MAGIC_tiedelem);
860 return Nullsv; /* element cannot be deleted */
862 #ifdef ENV_IS_CASELESS
863 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
864 sv = sv_2mortal(newSVpvn(key,klen));
865 key = strupr(SvPVX(sv));
870 xhv = (XPVHV*)SvANY(hv);
871 if (!xhv->xhv_array /* !HvARRAY(hv) */)
875 STRLEN tmplen = klen;
876 /* See the note in hv_fetch(). --jhi */
877 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
880 k_flags = HVhek_UTF8;
882 k_flags |= HVhek_FREEKEY;
885 PERL_HASH(hash, key, klen);
887 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
888 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
891 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
892 if (HeHASH(entry) != hash) /* strings can't be equal */
894 if (HeKLEN(entry) != klen)
896 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
898 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
900 if (k_flags & HVhek_FREEKEY)
902 /* if placeholder is here, it's already been deleted.... */
903 if (HeVAL(entry) == &PL_sv_undef)
906 return Nullsv; /* if still SvREADONLY, leave it deleted. */
908 /* okay, really delete the placeholder... */
909 *oentry = HeNEXT(entry);
911 xhv->xhv_fill--; /* HvFILL(hv)-- */
912 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
915 hv_free_ent(hv, entry);
916 xhv->xhv_keys--; /* HvKEYS(hv)-- */
917 if (xhv->xhv_keys == 0)
919 xhv->xhv_placeholders--;
923 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
924 S_hv_notallowed(aTHX_ k_flags, key, klen,
925 "delete readonly key '%"SVf"' from"
929 if (flags & G_DISCARD)
932 sv = sv_2mortal(HeVAL(entry));
933 HeVAL(entry) = &PL_sv_undef;
937 * If a restricted hash, rather than really deleting the entry, put
938 * a placeholder there. This marks the key as being "approved", so
939 * we can still access via not-really-existing key without raising
942 if (SvREADONLY(hv)) {
943 HeVAL(entry) = &PL_sv_undef;
944 /* We'll be saving this slot, so the number of allocated keys
945 * doesn't go down, but the number placeholders goes up */
946 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
948 *oentry = HeNEXT(entry);
950 xhv->xhv_fill--; /* HvFILL(hv)-- */
951 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
954 hv_free_ent(hv, entry);
955 xhv->xhv_keys--; /* HvKEYS(hv)-- */
956 if (xhv->xhv_keys == 0)
961 if (SvREADONLY(hv)) {
962 S_hv_notallowed(aTHX_ k_flags, key, klen,
963 "access disallowed key '%"SVf"' from"
967 if (k_flags & HVhek_FREEKEY)
973 =for apidoc hv_delete_ent
975 Deletes a key/value pair in the hash. The value SV is removed from the
976 hash and returned to the caller. The C<flags> value will normally be zero;
977 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
978 precomputed hash value, or 0 to ask for it to be computed.
984 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
991 register HE **oentry;
999 if (SvRMAGICAL(hv)) {
1002 hv_magic_check (hv, &needs_copy, &needs_store);
1004 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
1008 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
1009 /* No longer an element */
1010 sv_unmagic(sv, PERL_MAGIC_tiedelem);
1013 return Nullsv; /* element cannot be deleted */
1015 #ifdef ENV_IS_CASELESS
1016 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1017 key = SvPV(keysv, klen);
1018 keysv = sv_2mortal(newSVpvn(key,klen));
1019 (void)strupr(SvPVX(keysv));
1025 xhv = (XPVHV*)SvANY(hv);
1026 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1029 keysave = key = SvPV(keysv, klen);
1030 is_utf8 = (SvUTF8(keysv) != 0);
1033 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1035 k_flags = HVhek_UTF8;
1037 k_flags |= HVhek_FREEKEY;
1041 PERL_HASH(hash, key, klen);
1043 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1044 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1047 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1048 if (HeHASH(entry) != hash) /* strings can't be equal */
1050 if (HeKLEN(entry) != klen)
1052 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1054 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1056 if (k_flags & HVhek_FREEKEY)
1059 /* if placeholder is here, it's already been deleted.... */
1060 if (HeVAL(entry) == &PL_sv_undef)
1063 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1065 /* okay, really delete the placeholder. */
1066 *oentry = HeNEXT(entry);
1068 xhv->xhv_fill--; /* HvFILL(hv)-- */
1069 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1072 hv_free_ent(hv, entry);
1073 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1074 if (xhv->xhv_keys == 0)
1075 HvHASKFLAGS_off(hv);
1076 xhv->xhv_placeholders--;
1079 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1080 S_hv_notallowed(aTHX_ k_flags, key, klen,
1081 "delete readonly key '%"SVf"' from"
1085 if (flags & G_DISCARD)
1088 sv = sv_2mortal(HeVAL(entry));
1089 HeVAL(entry) = &PL_sv_undef;
1093 * If a restricted hash, rather than really deleting the entry, put
1094 * a placeholder there. This marks the key as being "approved", so
1095 * we can still access via not-really-existing key without raising
1098 if (SvREADONLY(hv)) {
1099 HeVAL(entry) = &PL_sv_undef;
1100 /* We'll be saving this slot, so the number of allocated keys
1101 * doesn't go down, but the number placeholders goes up */
1102 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1104 *oentry = HeNEXT(entry);
1106 xhv->xhv_fill--; /* HvFILL(hv)-- */
1107 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1110 hv_free_ent(hv, entry);
1111 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1112 if (xhv->xhv_keys == 0)
1113 HvHASKFLAGS_off(hv);
1117 if (SvREADONLY(hv)) {
1118 S_hv_notallowed(aTHX_ k_flags, key, klen,
1119 "delete disallowed key '%"SVf"' from"
1123 if (k_flags & HVhek_FREEKEY)
1129 =for apidoc hv_exists
1131 Returns a boolean indicating whether the specified hash key exists. The
1132 C<klen> is the length of the key.
1138 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1140 register XPVHV* xhv;
1144 bool is_utf8 = FALSE;
1145 const char *keysave = key;
1156 if (SvRMAGICAL(hv)) {
1157 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1158 sv = sv_newmortal();
1159 mg_copy((SV*)hv, sv, key, klen);
1160 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1163 #ifdef ENV_IS_CASELESS
1164 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1165 sv = sv_2mortal(newSVpvn(key,klen));
1166 key = strupr(SvPVX(sv));
1171 xhv = (XPVHV*)SvANY(hv);
1172 #ifndef DYNAMIC_ENV_FETCH
1173 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1178 STRLEN tmplen = klen;
1179 /* See the note in hv_fetch(). --jhi */
1180 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1183 k_flags = HVhek_UTF8;
1185 k_flags |= HVhek_FREEKEY;
1188 PERL_HASH(hash, key, klen);
1190 #ifdef DYNAMIC_ENV_FETCH
1191 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1194 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1195 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1196 for (; entry; entry = HeNEXT(entry)) {
1197 if (HeHASH(entry) != hash) /* strings can't be equal */
1199 if (HeKLEN(entry) != klen)
1201 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1203 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1205 if (k_flags & HVhek_FREEKEY)
1207 /* If we find the key, but the value is a placeholder, return false. */
1208 if (HeVAL(entry) == &PL_sv_undef)
1213 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1214 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1216 char *env = PerlEnv_ENVgetenv_len(key,&len);
1218 sv = newSVpvn(env,len);
1220 (void)hv_store(hv,key,klen,sv,hash);
1221 if (k_flags & HVhek_FREEKEY)
1227 if (k_flags & HVhek_FREEKEY)
1234 =for apidoc hv_exists_ent
1236 Returns a boolean indicating whether the specified hash key exists. C<hash>
1237 can be a valid precomputed hash value, or 0 to ask for it to be
1244 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1246 register XPVHV* xhv;
1258 if (SvRMAGICAL(hv)) {
1259 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1260 SV* svret = sv_newmortal();
1261 sv = sv_newmortal();
1262 keysv = sv_2mortal(newSVsv(keysv));
1263 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1264 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1265 return SvTRUE(svret);
1267 #ifdef ENV_IS_CASELESS
1268 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1269 key = SvPV(keysv, klen);
1270 keysv = sv_2mortal(newSVpvn(key,klen));
1271 (void)strupr(SvPVX(keysv));
1277 xhv = (XPVHV*)SvANY(hv);
1278 #ifndef DYNAMIC_ENV_FETCH
1279 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1283 keysave = key = SvPV(keysv, klen);
1284 is_utf8 = (SvUTF8(keysv) != 0);
1286 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1288 k_flags = HVhek_UTF8;
1290 k_flags |= HVhek_FREEKEY;
1293 PERL_HASH(hash, key, klen);
1295 #ifdef DYNAMIC_ENV_FETCH
1296 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1299 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1300 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1301 for (; entry; entry = HeNEXT(entry)) {
1302 if (HeHASH(entry) != hash) /* strings can't be equal */
1304 if (HeKLEN(entry) != klen)
1306 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1308 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1310 if (k_flags & HVhek_FREEKEY)
1312 /* If we find the key, but the value is a placeholder, return false. */
1313 if (HeVAL(entry) == &PL_sv_undef)
1317 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1318 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1320 char *env = PerlEnv_ENVgetenv_len(key,&len);
1322 sv = newSVpvn(env,len);
1324 (void)hv_store_ent(hv,keysv,sv,hash);
1325 if (k_flags & HVhek_FREEKEY)
1331 if (k_flags & HVhek_FREEKEY)
1337 S_hsplit(pTHX_ HV *hv)
1339 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1340 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1341 register I32 newsize = oldsize * 2;
1343 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1347 register HE **oentry;
1350 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1351 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1357 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1362 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1363 if (oldsize >= 64) {
1364 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1365 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1368 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1372 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1373 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1374 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1377 for (i=0; i<oldsize; i++,aep++) {
1378 if (!*aep) /* non-existent */
1381 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1382 if ((HeHASH(entry) & newsize) != i) {
1383 *oentry = HeNEXT(entry);
1384 HeNEXT(entry) = *bep;
1386 xhv->xhv_fill++; /* HvFILL(hv)++ */
1391 oentry = &HeNEXT(entry);
1393 if (!*aep) /* everything moved */
1394 xhv->xhv_fill--; /* HvFILL(hv)-- */
1399 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1401 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1402 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1403 register I32 newsize;
1409 register HE **oentry;
1411 newsize = (I32) newmax; /* possible truncation here */
1412 if (newsize != newmax || newmax <= oldsize)
1414 while ((newsize & (1 + ~newsize)) != newsize) {
1415 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1417 if (newsize < newmax)
1419 if (newsize < newmax)
1420 return; /* overflow detection */
1422 a = xhv->xhv_array; /* HvARRAY(hv) */
1425 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1426 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1432 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1437 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1438 if (oldsize >= 64) {
1439 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1440 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1443 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1446 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1449 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1451 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1452 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1453 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1457 for (i=0; i<oldsize; i++,aep++) {
1458 if (!*aep) /* non-existent */
1460 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1461 if ((j = (HeHASH(entry) & newsize)) != i) {
1463 *oentry = HeNEXT(entry);
1464 if (!(HeNEXT(entry) = aep[j]))
1465 xhv->xhv_fill++; /* HvFILL(hv)++ */
1470 oentry = &HeNEXT(entry);
1472 if (!*aep) /* everything moved */
1473 xhv->xhv_fill--; /* HvFILL(hv)-- */
1480 Creates a new HV. The reference count is set to 1.
1489 register XPVHV* xhv;
1491 hv = (HV*)NEWSV(502,0);
1492 sv_upgrade((SV *)hv, SVt_PVHV);
1493 xhv = (XPVHV*)SvANY(hv);
1496 #ifndef NODEFAULT_SHAREKEYS
1497 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1499 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1500 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1501 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1502 (void)hv_iterinit(hv); /* so each() will start off right */
1507 Perl_newHVhv(pTHX_ HV *ohv)
1510 STRLEN hv_max, hv_fill;
1512 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1514 hv_max = HvMAX(ohv);
1516 if (!SvMAGICAL((SV *)ohv)) {
1517 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1518 int i, shared = !!HvSHAREKEYS(ohv);
1519 HE **ents, **oents = (HE **)HvARRAY(ohv);
1521 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1524 /* In each bucket... */
1525 for (i = 0; i <= hv_max; i++) {
1526 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1533 /* Copy the linked list of entries. */
1534 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1535 U32 hash = HeHASH(oent);
1536 char *key = HeKEY(oent);
1537 STRLEN len = HeKLEN(oent);
1538 int flags = HeKFLAGS(oent);
1541 HeVAL(ent) = newSVsv(HeVAL(oent));
1543 = shared ? share_hek_flags(key, len, hash, flags)
1544 : save_hek_flags(key, len, hash, flags);
1555 HvFILL(hv) = hv_fill;
1556 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1560 /* Iterate over ohv, copying keys and values one at a time. */
1562 I32 riter = HvRITER(ohv);
1563 HE *eiter = HvEITER(ohv);
1565 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1566 while (hv_max && hv_max + 1 >= hv_fill * 2)
1567 hv_max = hv_max / 2;
1571 while ((entry = hv_iternext_flags(ohv, 0))) {
1572 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1573 newSVsv(HeVAL(entry)), HeHASH(entry),
1576 HvRITER(ohv) = riter;
1577 HvEITER(ohv) = eiter;
1584 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1591 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1592 PL_sub_generation++; /* may be deletion of method from stash */
1594 if (HeKLEN(entry) == HEf_SVKEY) {
1595 SvREFCNT_dec(HeKEY_sv(entry));
1596 Safefree(HeKEY_hek(entry));
1598 else if (HvSHAREKEYS(hv))
1599 unshare_hek(HeKEY_hek(entry));
1601 Safefree(HeKEY_hek(entry));
1606 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1610 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1611 PL_sub_generation++; /* may be deletion of method from stash */
1612 sv_2mortal(HeVAL(entry)); /* free between statements */
1613 if (HeKLEN(entry) == HEf_SVKEY) {
1614 sv_2mortal(HeKEY_sv(entry));
1615 Safefree(HeKEY_hek(entry));
1617 else if (HvSHAREKEYS(hv))
1618 unshare_hek(HeKEY_hek(entry));
1620 Safefree(HeKEY_hek(entry));
1625 =for apidoc hv_clear
1627 Clears a hash, making it empty.
1633 Perl_hv_clear(pTHX_ HV *hv)
1635 register XPVHV* xhv;
1639 if(SvREADONLY(hv)) {
1640 Perl_croak(aTHX_ "Attempt to clear a restricted hash");
1643 xhv = (XPVHV*)SvANY(hv);
1645 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1646 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1647 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1648 if (xhv->xhv_array /* HvARRAY(hv) */)
1649 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1650 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1655 HvHASKFLAGS_off(hv);
1659 S_hfreeentries(pTHX_ HV *hv)
1661 register HE **array;
1663 register HE *oentry = Null(HE*);
1674 array = HvARRAY(hv);
1679 entry = HeNEXT(entry);
1680 hv_free_ent(hv, oentry);
1685 entry = array[riter];
1688 (void)hv_iterinit(hv);
1692 =for apidoc hv_undef
1700 Perl_hv_undef(pTHX_ HV *hv)
1702 register XPVHV* xhv;
1705 xhv = (XPVHV*)SvANY(hv);
1707 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1709 Safefree(HvNAME(hv));
1712 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1713 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1714 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1715 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1716 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1723 =for apidoc hv_iterinit
1725 Prepares a starting point to traverse a hash table. Returns the number of
1726 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1727 currently only meaningful for hashes without tie magic.
1729 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1730 hash buckets that happen to be in use. If you still need that esoteric
1731 value, you can get it through the macro C<HvFILL(tb)>.
1738 Perl_hv_iterinit(pTHX_ HV *hv)
1740 register XPVHV* xhv;
1744 Perl_croak(aTHX_ "Bad hash");
1745 xhv = (XPVHV*)SvANY(hv);
1746 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1747 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1749 hv_free_ent(hv, entry);
1751 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1752 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1753 /* used to be xhv->xhv_fill before 5.004_65 */
1754 return XHvTOTALKEYS(xhv);
1757 =for apidoc hv_iternext
1759 Returns entries from a hash iterator. See C<hv_iterinit>.
1761 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1762 iterator currently points to, without losing your place or invalidating your
1763 iterator. Note that in this case the current entry is deleted from the hash
1764 with your iterator holding the last reference to it. Your iterator is flagged
1765 to free the entry on the next call to C<hv_iternext>, so you must not discard
1766 your iterator immediately else the entry will leak - call C<hv_iternext> to
1767 trigger the resource deallocation.
1773 Perl_hv_iternext(pTHX_ HV *hv)
1775 return hv_iternext_flags(hv, 0);
1779 =for apidoc hv_iternext_flags
1781 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1782 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1783 set the placeholders keys (for restricted hashes) will be returned in addition
1784 to normal keys. By default placeholders are automatically skipped over.
1785 Currently a placeholder is implemented with a value that is literally
1786 <&Perl_sv_undef> (a regular C<undef> value is a normal read-write SV for which
1787 C<!SvOK> is false). Note that the implementation of placeholders and
1788 restricted hashes may change, and the implementation currently is
1789 insufficiently abstracted for any change to be tidy.
1795 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1797 register XPVHV* xhv;
1803 Perl_croak(aTHX_ "Bad hash");
1804 xhv = (XPVHV*)SvANY(hv);
1805 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1807 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1808 SV *key = sv_newmortal();
1810 sv_setsv(key, HeSVKEY_force(entry));
1811 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1817 /* one HE per MAGICAL hash */
1818 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1820 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1822 HeKEY_hek(entry) = hek;
1823 HeKLEN(entry) = HEf_SVKEY;
1825 magic_nextpack((SV*) hv,mg,key);
1827 /* force key to stay around until next time */
1828 HeSVKEY_set(entry, SvREFCNT_inc(key));
1829 return entry; /* beware, hent_val is not set */
1832 SvREFCNT_dec(HeVAL(entry));
1833 Safefree(HeKEY_hek(entry));
1835 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1838 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1839 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1843 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1844 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1845 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1849 entry = HeNEXT(entry);
1850 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1852 * Skip past any placeholders -- don't want to include them in
1855 while (entry && HeVAL(entry) == &PL_sv_undef) {
1856 entry = HeNEXT(entry);
1861 xhv->xhv_riter++; /* HvRITER(hv)++ */
1862 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1863 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1866 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1867 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1869 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1870 /* if we have an entry, but it's a placeholder, don't count it */
1871 if (entry && HeVAL(entry) == &PL_sv_undef)
1876 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1878 hv_free_ent(hv, oldentry);
1881 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1886 =for apidoc hv_iterkey
1888 Returns the key from the current position of the hash iterator. See
1895 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1897 if (HeKLEN(entry) == HEf_SVKEY) {
1899 char *p = SvPV(HeKEY_sv(entry), len);
1904 *retlen = HeKLEN(entry);
1905 return HeKEY(entry);
1909 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1911 =for apidoc hv_iterkeysv
1913 Returns the key as an C<SV*> from the current position of the hash
1914 iterator. The return value will always be a mortal copy of the key. Also
1921 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1923 if (HeKLEN(entry) != HEf_SVKEY) {
1924 HEK *hek = HeKEY_hek(entry);
1925 int flags = HEK_FLAGS(hek);
1928 if (flags & HVhek_WASUTF8) {
1930 Andreas would like keys he put in as utf8 to come back as utf8
1932 STRLEN utf8_len = HEK_LEN(hek);
1933 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1935 sv = newSVpvn ((char*)as_utf8, utf8_len);
1938 sv = newSVpvn_share(HEK_KEY(hek),
1939 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1942 return sv_2mortal(sv);
1944 return sv_mortalcopy(HeKEY_sv(entry));
1948 =for apidoc hv_iterval
1950 Returns the value from the current position of the hash iterator. See
1957 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1959 if (SvRMAGICAL(hv)) {
1960 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1961 SV* sv = sv_newmortal();
1962 if (HeKLEN(entry) == HEf_SVKEY)
1963 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1964 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1968 return HeVAL(entry);
1972 =for apidoc hv_iternextsv
1974 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1981 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1984 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1986 *key = hv_iterkey(he, retlen);
1987 return hv_iterval(hv, he);
1991 =for apidoc hv_magic
1993 Adds magic to a hash. See C<sv_magic>.
1999 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2001 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2004 #if 0 /* use the macro from hv.h instead */
2007 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2009 return HEK_KEY(share_hek(sv, len, hash));
2014 /* possibly free a shared string if no one has access to it
2015 * len and hash must both be valid for str.
2018 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2020 unshare_hek_or_pvn (NULL, str, len, hash);
2025 Perl_unshare_hek(pTHX_ HEK *hek)
2027 unshare_hek_or_pvn(hek, NULL, 0, 0);
2030 /* possibly free a shared string if no one has access to it
2031 hek if non-NULL takes priority over the other 3, else str, len and hash
2032 are used. If so, len and hash must both be valid for str.
2035 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2037 register XPVHV* xhv;
2039 register HE **oentry;
2042 bool is_utf8 = FALSE;
2044 const char *save = str;
2047 hash = HEK_HASH(hek);
2048 } else if (len < 0) {
2049 STRLEN tmplen = -len;
2051 /* See the note in hv_fetch(). --jhi */
2052 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2055 k_flags = HVhek_UTF8;
2057 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2060 /* what follows is the moral equivalent of:
2061 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2062 if (--*Svp == Nullsv)
2063 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2065 xhv = (XPVHV*)SvANY(PL_strtab);
2066 /* assert(xhv_array != 0) */
2068 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2069 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2071 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2072 if (HeKEY_hek(entry) != hek)
2078 int flags_masked = k_flags & HVhek_MASK;
2079 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2080 if (HeHASH(entry) != hash) /* strings can't be equal */
2082 if (HeKLEN(entry) != len)
2084 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2086 if (HeKFLAGS(entry) != flags_masked)
2094 if (--HeVAL(entry) == Nullsv) {
2095 *oentry = HeNEXT(entry);
2097 xhv->xhv_fill--; /* HvFILL(hv)-- */
2098 Safefree(HeKEY_hek(entry));
2100 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2104 UNLOCK_STRTAB_MUTEX;
2105 if (!found && ckWARN_d(WARN_INTERNAL))
2106 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2107 "Attempt to free non-existent shared string '%s'%s",
2108 hek ? HEK_KEY(hek) : str,
2109 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2110 if (k_flags & HVhek_FREEKEY)
2114 /* get a (constant) string ptr from the global string table
2115 * string will get added if it is not already there.
2116 * len and hash must both be valid for str.
2119 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2121 bool is_utf8 = FALSE;
2123 const char *save = str;
2126 STRLEN tmplen = -len;
2128 /* See the note in hv_fetch(). --jhi */
2129 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2131 /* If we were able to downgrade here, then than means that we were passed
2132 in a key which only had chars 0-255, but was utf8 encoded. */
2135 /* If we found we were able to downgrade the string to bytes, then
2136 we should flag that it needs upgrading on keys or each. Also flag
2137 that we need share_hek_flags to free the string. */
2139 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2142 return share_hek_flags (str, len, hash, flags);
2146 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2148 register XPVHV* xhv;
2150 register HE **oentry;
2153 int flags_masked = flags & HVhek_MASK;
2155 /* what follows is the moral equivalent of:
2157 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2158 hv_store(PL_strtab, str, len, Nullsv, hash);
2160 xhv = (XPVHV*)SvANY(PL_strtab);
2161 /* assert(xhv_array != 0) */
2163 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2164 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2165 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2166 if (HeHASH(entry) != hash) /* strings can't be equal */
2168 if (HeKLEN(entry) != len)
2170 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2172 if (HeKFLAGS(entry) != flags_masked)
2179 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2180 HeVAL(entry) = Nullsv;
2181 HeNEXT(entry) = *oentry;
2183 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2184 if (i) { /* initial entry? */
2185 xhv->xhv_fill++; /* HvFILL(hv)++ */
2186 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2191 ++HeVAL(entry); /* use value slot as REFCNT */
2192 UNLOCK_STRTAB_MUTEX;
2194 if (flags & HVhek_FREEKEY)
2197 return HeKEY_hek(entry);