3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "I sit beside the fire and think of all that I have seen." --Bilbo
16 =head1 Hash Manipulation Functions
31 PL_he_root = HeNEXT(he);
40 HeNEXT(p) = (HE*)PL_he_root;
51 New(54, ptr, 1008/sizeof(XPV), XPV);
52 ptr->xpv_pv = (char*)PL_he_arenaroot;
53 PL_he_arenaroot = ptr;
56 heend = &he[1008 / sizeof(HE) - 1];
59 HeNEXT(he) = (HE*)(he + 1);
67 #define new_HE() (HE*)safemalloc(sizeof(HE))
68 #define del_HE(p) safefree((char*)p)
72 #define new_HE() new_he()
73 #define del_HE(p) del_he(p)
78 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
83 New(54, k, HEK_BASESIZE + len + 2, char);
85 Copy(str, HEK_KEY(hek), len, char);
86 HEK_KEY(hek)[len] = 0;
89 HEK_FLAGS(hek) = (unsigned char)flags;
93 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
97 Perl_free_tied_hv_pool(pTHX)
100 HE *he = PL_hv_fetch_ent_mh;
102 Safefree(HeKEY_hek(he));
109 #if defined(USE_ITHREADS)
111 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
117 /* look for it in the table first */
118 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
122 /* create anew and remember what it is */
124 ptr_table_store(PL_ptr_table, e, ret);
126 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
127 if (HeKLEN(e) == HEf_SVKEY) {
129 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
130 HeKEY_hek(ret) = (HEK*)k;
131 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
134 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
137 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
139 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
142 #endif /* USE_ITHREADS */
145 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
148 SV *sv = sv_newmortal(), *esv = sv_newmortal();
149 if (!(flags & HVhek_FREEKEY)) {
150 sv_setpvn(sv, key, klen);
153 /* Need to free saved eventually assign to mortal SV */
154 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
155 sv_usepvn(sv, (char *) key, klen);
157 if (flags & HVhek_UTF8) {
160 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
161 Perl_croak(aTHX_ SvPVX(esv), sv);
164 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
170 Returns the SV which corresponds to the specified key in the hash. The
171 C<klen> is the length of the key. If C<lval> is set then the fetch will be
172 part of a store. Check that the return value is non-null before
173 dereferencing it to an C<SV*>.
175 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
176 information on how to use this function on tied hashes.
183 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
185 bool is_utf8 = FALSE;
186 const char *keysave = key;
195 STRLEN tmplen = klen;
196 /* Just casting the &klen to (STRLEN) won't work well
197 * if STRLEN and I32 are of different widths. --jhi */
198 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
200 /* If we were able to downgrade here, then than means that we were
201 passed in a key which only had chars 0-255, but was utf8 encoded. */
204 /* If we found we were able to downgrade the string to bytes, then
205 we should flag that it needs upgrading on keys or each. */
207 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
210 return hv_fetch_flags (hv, key, klen, lval, flags);
214 S_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen, I32 lval, int flags)
224 if (SvRMAGICAL(hv)) {
225 /* All this clause seems to be utf8 unaware.
226 By moving the utf8 stuff out to hv_fetch_flags I need to ensure
227 key doesn't leak. I've not tried solving the utf8-ness.
230 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
232 sv_upgrade(sv, SVt_PVLV);
233 mg_copy((SV*)hv, sv, key, klen);
234 if (flags & HVhek_FREEKEY)
237 LvTARG(sv) = sv; /* fake (SV**) */
238 return &(LvTARG(sv));
240 #ifdef ENV_IS_CASELESS
241 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
243 for (i = 0; i < klen; ++i)
244 if (isLOWER(key[i])) {
245 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
246 SV **ret = hv_fetch(hv, nkey, klen, 0);
248 ret = hv_store_flags(hv, key, klen, NEWSV(61,0), 0,
250 } else if (flags & HVhek_FREEKEY)
258 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
259 avoid unnecessary pointer dereferencing. */
260 xhv = (XPVHV*)SvANY(hv);
261 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
263 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
264 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
267 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
268 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
271 if (flags & HVhek_FREEKEY)
277 PERL_HASH(hash, key, klen);
279 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
280 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
281 for (; entry; entry = HeNEXT(entry)) {
282 if (!HeKEY_hek(entry))
284 if (HeHASH(entry) != hash) /* strings can't be equal */
286 if (HeKLEN(entry) != (I32)klen)
288 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
290 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
291 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
292 xor is true if bits differ, in which case this isn't a match. */
293 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
295 if (lval && HeKFLAGS(entry) != flags) {
296 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
297 But if entry was set previously with HVhek_WASUTF8 and key now
298 doesn't (or vice versa) then we should change the key's flag,
299 as this is assignment. */
300 if (HvSHAREKEYS(hv)) {
301 /* Need to swap the key we have for a key with the flags we
302 need. As keys are shared we can't just write to the flag,
303 so we share the new one, unshare the old one. */
304 int flags_nofree = flags & ~HVhek_FREEKEY;
305 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
306 unshare_hek (HeKEY_hek(entry));
307 HeKEY_hek(entry) = new_hek;
310 HeKFLAGS(entry) = flags;
312 if (flags & HVhek_FREEKEY)
314 /* if we find a placeholder, we pretend we haven't found anything */
315 if (HeVAL(entry) == &PL_sv_placeholder)
317 return &HeVAL(entry);
320 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
321 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
323 char *env = PerlEnv_ENVgetenv_len(key,&len);
325 sv = newSVpvn(env,len);
327 if (flags & HVhek_FREEKEY)
329 return hv_store(hv,key,klen,sv,hash);
333 if (!entry && SvREADONLY(hv)) {
334 S_hv_notallowed(aTHX_ flags, key, klen,
335 "access disallowed key '%"SVf"' in"
338 if (lval) { /* gonna assign to this, so it better be there */
340 return hv_store_flags(hv,key,klen,sv,hash,flags);
342 if (flags & HVhek_FREEKEY)
347 /* returns an HE * structure with the all fields set */
348 /* note that hent_val will be a mortal sv for MAGICAL hashes */
350 =for apidoc hv_fetch_ent
352 Returns the hash entry which corresponds to the specified key in the hash.
353 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
354 if you want the function to compute it. IF C<lval> is set then the fetch
355 will be part of a store. Make sure the return value is non-null before
356 accessing it. The return value when C<tb> is a tied hash is a pointer to a
357 static location, so be sure to make a copy of the structure if you need to
360 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
361 information on how to use this function on tied hashes.
367 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
381 if (SvRMAGICAL(hv)) {
382 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
384 keysv = newSVsv(keysv);
385 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
386 /* grab a fake HE/HEK pair from the pool or make a new one */
387 entry = PL_hv_fetch_ent_mh;
389 PL_hv_fetch_ent_mh = HeNEXT(entry);
393 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
394 HeKEY_hek(entry) = (HEK*)k;
396 HeNEXT(entry) = Nullhe;
397 HeSVKEY_set(entry, keysv);
399 sv_upgrade(sv, SVt_PVLV);
401 LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
404 #ifdef ENV_IS_CASELESS
405 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
407 key = SvPV(keysv, klen);
408 for (i = 0; i < klen; ++i)
409 if (isLOWER(key[i])) {
410 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
411 (void)strupr(SvPVX(nkeysv));
412 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
414 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
421 keysave = key = SvPV(keysv, klen);
422 xhv = (XPVHV*)SvANY(hv);
423 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
425 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
426 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
429 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
430 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
436 is_utf8 = (SvUTF8(keysv)!=0);
439 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
443 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
447 if SvIsCOW_shared_hash(keysv) {
450 PERL_HASH(hash, key, klen);
454 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
455 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
456 for (; entry; entry = HeNEXT(entry)) {
457 if (HeHASH(entry) != hash) /* strings can't be equal */
459 if (HeKLEN(entry) != (I32)klen)
461 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
463 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
465 if (lval && HeKFLAGS(entry) != flags) {
466 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
467 But if entry was set previously with HVhek_WASUTF8 and key now
468 doesn't (or vice versa) then we should change the key's flag,
469 as this is assignment. */
470 if (HvSHAREKEYS(hv)) {
471 /* Need to swap the key we have for a key with the flags we
472 need. As keys are shared we can't just write to the flag,
473 so we share the new one, unshare the old one. */
474 int flags_nofree = flags & ~HVhek_FREEKEY;
475 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
476 unshare_hek (HeKEY_hek(entry));
477 HeKEY_hek(entry) = new_hek;
480 HeKFLAGS(entry) = flags;
484 /* if we find a placeholder, we pretend we haven't found anything */
485 if (HeVAL(entry) == &PL_sv_placeholder)
489 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
490 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
492 char *env = PerlEnv_ENVgetenv_len(key,&len);
494 sv = newSVpvn(env,len);
496 return hv_store_ent(hv,keysv,sv,hash);
500 if (!entry && SvREADONLY(hv)) {
501 S_hv_notallowed(aTHX_ flags, key, klen,
502 "access disallowed key '%"SVf"' in"
505 if (flags & HVhek_FREEKEY)
507 if (lval) { /* gonna assign to this, so it better be there */
509 return hv_store_ent(hv,keysv,sv,hash);
515 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
517 MAGIC *mg = SvMAGIC(hv);
521 if (isUPPER(mg->mg_type)) {
523 switch (mg->mg_type) {
524 case PERL_MAGIC_tied:
526 *needs_store = FALSE;
529 mg = mg->mg_moremagic;
536 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
537 the length of the key. The C<hash> parameter is the precomputed hash
538 value; if it is zero then Perl will compute it. The return value will be
539 NULL if the operation failed or if the value did not need to be actually
540 stored within the hash (as in the case of tied hashes). Otherwise it can
541 be dereferenced to get the original C<SV*>. Note that the caller is
542 responsible for suitably incrementing the reference count of C<val> before
543 the call, and decrementing it if the function returned NULL. Effectively
544 a successful hv_store takes ownership of one reference to C<val>. This is
545 usually what you want; a newly created SV has a reference count of one, so
546 if all your code does is create SVs then store them in a hash, hv_store
547 will own the only reference to the new SV, and your code doesn't need to do
548 anything further to tidy up. hv_store is not implemented as a call to
549 hv_store_ent, and does not create a temporary SV for the key, so if your
550 key data is not already in SV form then use hv_store in preference to
553 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
554 information on how to use this function on tied hashes.
560 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
562 bool is_utf8 = FALSE;
563 const char *keysave = key;
572 STRLEN tmplen = klen;
573 /* Just casting the &klen to (STRLEN) won't work well
574 * if STRLEN and I32 are of different widths. --jhi */
575 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
577 /* If we were able to downgrade here, then than means that we were
578 passed in a key which only had chars 0-255, but was utf8 encoded. */
581 /* If we found we were able to downgrade the string to bytes, then
582 we should flag that it needs upgrading on keys or each. */
584 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
587 return hv_store_flags (hv, key, klen, val, hash, flags);
591 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
592 register U32 hash, int flags)
597 register HE **oentry;
602 xhv = (XPVHV*)SvANY(hv);
606 hv_magic_check (hv, &needs_copy, &needs_store);
608 mg_copy((SV*)hv, val, key, klen);
609 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
610 if (flags & HVhek_FREEKEY)
614 #ifdef ENV_IS_CASELESS
615 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
616 key = savepvn(key,klen);
617 key = (const char*)strupr((char*)key);
625 HvHASKFLAGS_on((SV*)hv);
628 PERL_HASH(hash, key, klen);
630 if (!xhv->xhv_array /* !HvARRAY(hv) */)
631 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
632 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
635 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
636 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
639 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
640 if (HeHASH(entry) != hash) /* strings can't be equal */
642 if (HeKLEN(entry) != (I32)klen)
644 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
646 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
648 if (HeVAL(entry) == &PL_sv_placeholder)
649 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
651 SvREFCNT_dec(HeVAL(entry));
652 if (flags & HVhek_PLACEHOLD) {
653 /* We have been requested to insert a placeholder. Currently
654 only Storable is allowed to do this. */
655 xhv->xhv_placeholders++;
656 HeVAL(entry) = &PL_sv_placeholder;
660 if (HeKFLAGS(entry) != flags) {
661 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
662 But if entry was set previously with HVhek_WASUTF8 and key now
663 doesn't (or vice versa) then we should change the key's flag,
664 as this is assignment. */
665 if (HvSHAREKEYS(hv)) {
666 /* Need to swap the key we have for a key with the flags we
667 need. As keys are shared we can't just write to the flag,
668 so we share the new one, unshare the old one. */
669 int flags_nofree = flags & ~HVhek_FREEKEY;
670 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
671 unshare_hek (HeKEY_hek(entry));
672 HeKEY_hek(entry) = new_hek;
675 HeKFLAGS(entry) = flags;
677 if (flags & HVhek_FREEKEY)
679 return &HeVAL(entry);
682 if (SvREADONLY(hv)) {
683 S_hv_notallowed(aTHX_ flags, key, klen,
684 "access disallowed key '%"SVf"' to"
689 /* share_hek_flags will do the free for us. This might be considered
692 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
693 else /* gotta do the real thing */
694 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
695 if (flags & HVhek_PLACEHOLD) {
696 /* We have been requested to insert a placeholder. Currently
697 only Storable is allowed to do this. */
698 xhv->xhv_placeholders++;
699 HeVAL(entry) = &PL_sv_placeholder;
702 HeNEXT(entry) = *oentry;
705 xhv->xhv_keys++; /* HvKEYS(hv)++ */
706 if (i) { /* initial entry? */
707 xhv->xhv_fill++; /* HvFILL(hv)++ */
708 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
712 return &HeVAL(entry);
716 =for apidoc hv_store_ent
718 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
719 parameter is the precomputed hash value; if it is zero then Perl will
720 compute it. The return value is the new hash entry so created. It will be
721 NULL if the operation failed or if the value did not need to be actually
722 stored within the hash (as in the case of tied hashes). Otherwise the
723 contents of the return value can be accessed using the C<He?> macros
724 described here. Note that the caller is responsible for suitably
725 incrementing the reference count of C<val> before the call, and
726 decrementing it if the function returned NULL. Effectively a successful
727 hv_store_ent takes ownership of one reference to C<val>. This is
728 usually what you want; a newly created SV has a reference count of one, so
729 if all your code does is create SVs then store them in a hash, hv_store
730 will own the only reference to the new SV, and your code doesn't need to do
731 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
732 unlike C<val> it does not take ownership of it, so maintaining the correct
733 reference count on C<key> is entirely the caller's responsibility. hv_store
734 is not implemented as a call to hv_store_ent, and does not create a temporary
735 SV for the key, so if your key data is not already in SV form then use
736 hv_store in preference to hv_store_ent.
738 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
739 information on how to use this function on tied hashes.
745 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
760 xhv = (XPVHV*)SvANY(hv);
764 hv_magic_check (hv, &needs_copy, &needs_store);
766 bool save_taint = PL_tainted;
768 PL_tainted = SvTAINTED(keysv);
769 keysv = sv_2mortal(newSVsv(keysv));
770 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
771 TAINT_IF(save_taint);
772 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
774 #ifdef ENV_IS_CASELESS
775 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
776 key = SvPV(keysv, klen);
777 keysv = sv_2mortal(newSVpvn(key,klen));
778 (void)strupr(SvPVX(keysv));
785 keysave = key = SvPV(keysv, klen);
786 is_utf8 = (SvUTF8(keysv) != 0);
789 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
793 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
794 HvHASKFLAGS_on((SV*)hv);
798 if SvIsCOW_shared_hash(keysv) {
801 PERL_HASH(hash, key, klen);
805 if (!xhv->xhv_array /* !HvARRAY(hv) */)
806 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
807 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
810 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
811 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
814 for (; entry; i=0, entry = HeNEXT(entry)) {
815 if (HeHASH(entry) != hash) /* strings can't be equal */
817 if (HeKLEN(entry) != (I32)klen)
819 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
821 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
823 if (HeVAL(entry) == &PL_sv_placeholder)
824 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
826 SvREFCNT_dec(HeVAL(entry));
828 if (HeKFLAGS(entry) != flags) {
829 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
830 But if entry was set previously with HVhek_WASUTF8 and key now
831 doesn't (or vice versa) then we should change the key's flag,
832 as this is assignment. */
833 if (HvSHAREKEYS(hv)) {
834 /* Need to swap the key we have for a key with the flags we
835 need. As keys are shared we can't just write to the flag,
836 so we share the new one, unshare the old one. */
837 int flags_nofree = flags & ~HVhek_FREEKEY;
838 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
839 unshare_hek (HeKEY_hek(entry));
840 HeKEY_hek(entry) = new_hek;
843 HeKFLAGS(entry) = flags;
845 if (flags & HVhek_FREEKEY)
850 if (SvREADONLY(hv)) {
851 S_hv_notallowed(aTHX_ flags, key, klen,
852 "access disallowed key '%"SVf"' to"
857 /* share_hek_flags will do the free for us. This might be considered
860 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
861 else /* gotta do the real thing */
862 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
864 HeNEXT(entry) = *oentry;
867 xhv->xhv_keys++; /* HvKEYS(hv)++ */
868 if (i) { /* initial entry? */
869 xhv->xhv_fill++; /* HvFILL(hv)++ */
870 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
878 =for apidoc hv_delete
880 Deletes a key/value pair in the hash. The value SV is removed from the
881 hash and returned to the caller. The C<klen> is the length of the key.
882 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
889 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
895 register HE **oentry;
898 bool is_utf8 = FALSE;
900 const char *keysave = key;
908 if (SvRMAGICAL(hv)) {
911 hv_magic_check (hv, &needs_copy, &needs_store);
913 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
919 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
920 /* No longer an element */
921 sv_unmagic(sv, PERL_MAGIC_tiedelem);
924 return Nullsv; /* element cannot be deleted */
926 #ifdef ENV_IS_CASELESS
927 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
928 sv = sv_2mortal(newSVpvn(key,klen));
929 key = strupr(SvPVX(sv));
934 xhv = (XPVHV*)SvANY(hv);
935 if (!xhv->xhv_array /* !HvARRAY(hv) */)
939 STRLEN tmplen = klen;
940 /* See the note in hv_fetch(). --jhi */
941 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
944 k_flags = HVhek_UTF8;
946 k_flags |= HVhek_FREEKEY;
949 PERL_HASH(hash, key, klen);
951 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
952 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
955 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
956 if (HeHASH(entry) != hash) /* strings can't be equal */
958 if (HeKLEN(entry) != (I32)klen)
960 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
962 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
964 if (k_flags & HVhek_FREEKEY)
966 /* if placeholder is here, it's already been deleted.... */
967 if (HeVAL(entry) == &PL_sv_placeholder)
970 return Nullsv; /* if still SvREADONLY, leave it deleted. */
972 /* okay, really delete the placeholder... */
973 *oentry = HeNEXT(entry);
975 xhv->xhv_fill--; /* HvFILL(hv)-- */
976 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
979 hv_free_ent(hv, entry);
980 xhv->xhv_keys--; /* HvKEYS(hv)-- */
981 if (xhv->xhv_keys == 0)
983 xhv->xhv_placeholders--;
987 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
988 S_hv_notallowed(aTHX_ k_flags, key, klen,
989 "delete readonly key '%"SVf"' from"
993 if (flags & G_DISCARD)
996 sv = sv_2mortal(HeVAL(entry));
997 HeVAL(entry) = &PL_sv_placeholder;
1001 * If a restricted hash, rather than really deleting the entry, put
1002 * a placeholder there. This marks the key as being "approved", so
1003 * we can still access via not-really-existing key without raising
1006 if (SvREADONLY(hv)) {
1007 HeVAL(entry) = &PL_sv_placeholder;
1008 /* We'll be saving this slot, so the number of allocated keys
1009 * doesn't go down, but the number placeholders goes up */
1010 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1012 *oentry = HeNEXT(entry);
1014 xhv->xhv_fill--; /* HvFILL(hv)-- */
1015 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1018 hv_free_ent(hv, entry);
1019 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1020 if (xhv->xhv_keys == 0)
1021 HvHASKFLAGS_off(hv);
1025 if (SvREADONLY(hv)) {
1026 S_hv_notallowed(aTHX_ k_flags, key, klen,
1027 "access disallowed key '%"SVf"' from"
1031 if (k_flags & HVhek_FREEKEY)
1037 =for apidoc hv_delete_ent
1039 Deletes a key/value pair in the hash. The value SV is removed from the
1040 hash and returned to the caller. The C<flags> value will normally be zero;
1041 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
1042 precomputed hash value, or 0 to ask for it to be computed.
1048 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
1050 register XPVHV* xhv;
1055 register HE **oentry;
1063 if (SvRMAGICAL(hv)) {
1066 hv_magic_check (hv, &needs_copy, &needs_store);
1068 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
1070 if (SvMAGICAL(sv)) {
1074 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
1075 /* No longer an element */
1076 sv_unmagic(sv, PERL_MAGIC_tiedelem);
1079 return Nullsv; /* element cannot be deleted */
1081 #ifdef ENV_IS_CASELESS
1082 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1083 key = SvPV(keysv, klen);
1084 keysv = sv_2mortal(newSVpvn(key,klen));
1085 (void)strupr(SvPVX(keysv));
1091 xhv = (XPVHV*)SvANY(hv);
1092 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1095 keysave = key = SvPV(keysv, klen);
1096 is_utf8 = (SvUTF8(keysv) != 0);
1099 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1101 k_flags = HVhek_UTF8;
1103 k_flags |= HVhek_FREEKEY;
1107 PERL_HASH(hash, key, klen);
1109 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1110 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1113 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1114 if (HeHASH(entry) != hash) /* strings can't be equal */
1116 if (HeKLEN(entry) != (I32)klen)
1118 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1120 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1122 if (k_flags & HVhek_FREEKEY)
1125 /* if placeholder is here, it's already been deleted.... */
1126 if (HeVAL(entry) == &PL_sv_placeholder)
1129 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1131 /* okay, really delete the placeholder. */
1132 *oentry = HeNEXT(entry);
1134 xhv->xhv_fill--; /* HvFILL(hv)-- */
1135 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1138 hv_free_ent(hv, entry);
1139 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1140 if (xhv->xhv_keys == 0)
1141 HvHASKFLAGS_off(hv);
1142 xhv->xhv_placeholders--;
1145 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1146 S_hv_notallowed(aTHX_ k_flags, key, klen,
1147 "delete readonly key '%"SVf"' from"
1151 if (flags & G_DISCARD)
1154 sv = sv_2mortal(HeVAL(entry));
1155 HeVAL(entry) = &PL_sv_placeholder;
1159 * If a restricted hash, rather than really deleting the entry, put
1160 * a placeholder there. This marks the key as being "approved", so
1161 * we can still access via not-really-existing key without raising
1164 if (SvREADONLY(hv)) {
1165 HeVAL(entry) = &PL_sv_placeholder;
1166 /* We'll be saving this slot, so the number of allocated keys
1167 * doesn't go down, but the number placeholders goes up */
1168 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1170 *oentry = HeNEXT(entry);
1172 xhv->xhv_fill--; /* HvFILL(hv)-- */
1173 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1176 hv_free_ent(hv, entry);
1177 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1178 if (xhv->xhv_keys == 0)
1179 HvHASKFLAGS_off(hv);
1183 if (SvREADONLY(hv)) {
1184 S_hv_notallowed(aTHX_ k_flags, key, klen,
1185 "delete disallowed key '%"SVf"' from"
1189 if (k_flags & HVhek_FREEKEY)
1195 =for apidoc hv_exists
1197 Returns a boolean indicating whether the specified hash key exists. The
1198 C<klen> is the length of the key.
1204 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1206 register XPVHV* xhv;
1210 bool is_utf8 = FALSE;
1211 const char *keysave = key;
1222 if (SvRMAGICAL(hv)) {
1223 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1224 sv = sv_newmortal();
1225 mg_copy((SV*)hv, sv, key, klen);
1226 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1227 return (bool)SvTRUE(sv);
1229 #ifdef ENV_IS_CASELESS
1230 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1231 sv = sv_2mortal(newSVpvn(key,klen));
1232 key = strupr(SvPVX(sv));
1237 xhv = (XPVHV*)SvANY(hv);
1238 #ifndef DYNAMIC_ENV_FETCH
1239 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1244 STRLEN tmplen = klen;
1245 /* See the note in hv_fetch(). --jhi */
1246 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1249 k_flags = HVhek_UTF8;
1251 k_flags |= HVhek_FREEKEY;
1254 PERL_HASH(hash, key, klen);
1256 #ifdef DYNAMIC_ENV_FETCH
1257 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1260 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1261 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1262 for (; entry; entry = HeNEXT(entry)) {
1263 if (HeHASH(entry) != hash) /* strings can't be equal */
1265 if (HeKLEN(entry) != klen)
1267 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1269 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1271 if (k_flags & HVhek_FREEKEY)
1273 /* If we find the key, but the value is a placeholder, return false. */
1274 if (HeVAL(entry) == &PL_sv_placeholder)
1279 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1280 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1282 char *env = PerlEnv_ENVgetenv_len(key,&len);
1284 sv = newSVpvn(env,len);
1286 (void)hv_store(hv,key,klen,sv,hash);
1287 if (k_flags & HVhek_FREEKEY)
1293 if (k_flags & HVhek_FREEKEY)
1300 =for apidoc hv_exists_ent
1302 Returns a boolean indicating whether the specified hash key exists. C<hash>
1303 can be a valid precomputed hash value, or 0 to ask for it to be
1310 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1312 register XPVHV* xhv;
1324 if (SvRMAGICAL(hv)) {
1325 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1326 SV* svret = sv_newmortal();
1327 sv = sv_newmortal();
1328 keysv = sv_2mortal(newSVsv(keysv));
1329 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1330 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1331 return (bool)SvTRUE(svret);
1333 #ifdef ENV_IS_CASELESS
1334 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1335 key = SvPV(keysv, klen);
1336 keysv = sv_2mortal(newSVpvn(key,klen));
1337 (void)strupr(SvPVX(keysv));
1343 xhv = (XPVHV*)SvANY(hv);
1344 #ifndef DYNAMIC_ENV_FETCH
1345 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1349 keysave = key = SvPV(keysv, klen);
1350 is_utf8 = (SvUTF8(keysv) != 0);
1352 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1354 k_flags = HVhek_UTF8;
1356 k_flags |= HVhek_FREEKEY;
1359 PERL_HASH(hash, key, klen);
1361 #ifdef DYNAMIC_ENV_FETCH
1362 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1365 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1366 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1367 for (; entry; entry = HeNEXT(entry)) {
1368 if (HeHASH(entry) != hash) /* strings can't be equal */
1370 if (HeKLEN(entry) != (I32)klen)
1372 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1374 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1376 if (k_flags & HVhek_FREEKEY)
1378 /* If we find the key, but the value is a placeholder, return false. */
1379 if (HeVAL(entry) == &PL_sv_placeholder)
1383 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1384 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1386 char *env = PerlEnv_ENVgetenv_len(key,&len);
1388 sv = newSVpvn(env,len);
1390 (void)hv_store_ent(hv,keysv,sv,hash);
1391 if (k_flags & HVhek_FREEKEY)
1397 if (k_flags & HVhek_FREEKEY)
1403 S_hsplit(pTHX_ HV *hv)
1405 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1406 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1407 register I32 newsize = oldsize * 2;
1409 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1413 register HE **oentry;
1416 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1417 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1423 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1428 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1429 if (oldsize >= 64) {
1430 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1431 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1434 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1438 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1439 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1440 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1443 for (i=0; i<oldsize; i++,aep++) {
1444 if (!*aep) /* non-existent */
1447 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1448 if ((HeHASH(entry) & newsize) != (U32)i) {
1449 *oentry = HeNEXT(entry);
1450 HeNEXT(entry) = *bep;
1452 xhv->xhv_fill++; /* HvFILL(hv)++ */
1457 oentry = &HeNEXT(entry);
1459 if (!*aep) /* everything moved */
1460 xhv->xhv_fill--; /* HvFILL(hv)-- */
1465 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1467 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1468 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1469 register I32 newsize;
1475 register HE **oentry;
1477 newsize = (I32) newmax; /* possible truncation here */
1478 if (newsize != newmax || newmax <= oldsize)
1480 while ((newsize & (1 + ~newsize)) != newsize) {
1481 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1483 if (newsize < newmax)
1485 if (newsize < newmax)
1486 return; /* overflow detection */
1488 a = xhv->xhv_array; /* HvARRAY(hv) */
1491 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1492 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1498 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1503 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1504 if (oldsize >= 64) {
1505 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1506 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1509 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1512 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1515 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1517 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1518 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1519 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1523 for (i=0; i<oldsize; i++,aep++) {
1524 if (!*aep) /* non-existent */
1526 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1527 if ((j = (HeHASH(entry) & newsize)) != i) {
1529 *oentry = HeNEXT(entry);
1530 if (!(HeNEXT(entry) = aep[j]))
1531 xhv->xhv_fill++; /* HvFILL(hv)++ */
1536 oentry = &HeNEXT(entry);
1538 if (!*aep) /* everything moved */
1539 xhv->xhv_fill--; /* HvFILL(hv)-- */
1546 Creates a new HV. The reference count is set to 1.
1555 register XPVHV* xhv;
1557 hv = (HV*)NEWSV(502,0);
1558 sv_upgrade((SV *)hv, SVt_PVHV);
1559 xhv = (XPVHV*)SvANY(hv);
1562 #ifndef NODEFAULT_SHAREKEYS
1563 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1565 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1566 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1567 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1568 (void)hv_iterinit(hv); /* so each() will start off right */
1573 Perl_newHVhv(pTHX_ HV *ohv)
1576 STRLEN hv_max, hv_fill;
1578 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1580 hv_max = HvMAX(ohv);
1582 if (!SvMAGICAL((SV *)ohv)) {
1583 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1585 bool shared = !!HvSHAREKEYS(ohv);
1586 HE **ents, **oents = (HE **)HvARRAY(ohv);
1588 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1591 /* In each bucket... */
1592 for (i = 0; i <= hv_max; i++) {
1593 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1600 /* Copy the linked list of entries. */
1601 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1602 U32 hash = HeHASH(oent);
1603 char *key = HeKEY(oent);
1604 STRLEN len = HeKLEN(oent);
1605 int flags = HeKFLAGS(oent);
1608 HeVAL(ent) = newSVsv(HeVAL(oent));
1610 = shared ? share_hek_flags(key, len, hash, flags)
1611 : save_hek_flags(key, len, hash, flags);
1622 HvFILL(hv) = hv_fill;
1623 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1627 /* Iterate over ohv, copying keys and values one at a time. */
1629 I32 riter = HvRITER(ohv);
1630 HE *eiter = HvEITER(ohv);
1632 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1633 while (hv_max && hv_max + 1 >= hv_fill * 2)
1634 hv_max = hv_max / 2;
1638 while ((entry = hv_iternext_flags(ohv, 0))) {
1639 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1640 newSVsv(HeVAL(entry)), HeHASH(entry),
1643 HvRITER(ohv) = riter;
1644 HvEITER(ohv) = eiter;
1651 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1658 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1659 PL_sub_generation++; /* may be deletion of method from stash */
1661 if (HeKLEN(entry) == HEf_SVKEY) {
1662 SvREFCNT_dec(HeKEY_sv(entry));
1663 Safefree(HeKEY_hek(entry));
1665 else if (HvSHAREKEYS(hv))
1666 unshare_hek(HeKEY_hek(entry));
1668 Safefree(HeKEY_hek(entry));
1673 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1677 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1678 PL_sub_generation++; /* may be deletion of method from stash */
1679 sv_2mortal(HeVAL(entry)); /* free between statements */
1680 if (HeKLEN(entry) == HEf_SVKEY) {
1681 sv_2mortal(HeKEY_sv(entry));
1682 Safefree(HeKEY_hek(entry));
1684 else if (HvSHAREKEYS(hv))
1685 unshare_hek(HeKEY_hek(entry));
1687 Safefree(HeKEY_hek(entry));
1692 =for apidoc hv_clear
1694 Clears a hash, making it empty.
1700 Perl_hv_clear(pTHX_ HV *hv)
1702 register XPVHV* xhv;
1706 xhv = (XPVHV*)SvANY(hv);
1708 if (SvREADONLY(hv)) {
1709 /* restricted hash: convert all keys to placeholders */
1712 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1713 entry = ((HE**)xhv->xhv_array)[i];
1714 for (; entry; entry = HeNEXT(entry)) {
1715 /* not already placeholder */
1716 if (HeVAL(entry) != &PL_sv_placeholder) {
1717 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1718 SV* keysv = hv_iterkeysv(entry);
1720 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1723 SvREFCNT_dec(HeVAL(entry));
1724 HeVAL(entry) = &PL_sv_placeholder;
1725 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1733 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1734 if (xhv->xhv_array /* HvARRAY(hv) */)
1735 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1736 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1741 HvHASKFLAGS_off(hv);
1745 S_hfreeentries(pTHX_ HV *hv)
1747 register HE **array;
1749 register HE *oentry = Null(HE*);
1760 array = HvARRAY(hv);
1761 /* make everyone else think the array is empty, so that the destructors
1762 * called for freed entries can't recusively mess with us */
1763 HvARRAY(hv) = Null(HE**);
1765 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1771 entry = HeNEXT(entry);
1772 hv_free_ent(hv, oentry);
1777 entry = array[riter];
1780 HvARRAY(hv) = array;
1781 (void)hv_iterinit(hv);
1785 =for apidoc hv_undef
1793 Perl_hv_undef(pTHX_ HV *hv)
1795 register XPVHV* xhv;
1798 xhv = (XPVHV*)SvANY(hv);
1800 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1803 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1804 Safefree(HvNAME(hv));
1807 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1808 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1809 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1816 =for apidoc hv_iterinit
1818 Prepares a starting point to traverse a hash table. Returns the number of
1819 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1820 currently only meaningful for hashes without tie magic.
1822 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1823 hash buckets that happen to be in use. If you still need that esoteric
1824 value, you can get it through the macro C<HvFILL(tb)>.
1831 Perl_hv_iterinit(pTHX_ HV *hv)
1833 register XPVHV* xhv;
1837 Perl_croak(aTHX_ "Bad hash");
1838 xhv = (XPVHV*)SvANY(hv);
1839 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1840 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1842 hv_free_ent(hv, entry);
1844 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1845 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1846 /* used to be xhv->xhv_fill before 5.004_65 */
1847 return XHvTOTALKEYS(xhv);
1850 =for apidoc hv_iternext
1852 Returns entries from a hash iterator. See C<hv_iterinit>.
1854 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1855 iterator currently points to, without losing your place or invalidating your
1856 iterator. Note that in this case the current entry is deleted from the hash
1857 with your iterator holding the last reference to it. Your iterator is flagged
1858 to free the entry on the next call to C<hv_iternext>, so you must not discard
1859 your iterator immediately else the entry will leak - call C<hv_iternext> to
1860 trigger the resource deallocation.
1866 Perl_hv_iternext(pTHX_ HV *hv)
1868 return hv_iternext_flags(hv, 0);
1872 =for apidoc hv_iternext_flags
1874 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1875 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1876 set the placeholders keys (for restricted hashes) will be returned in addition
1877 to normal keys. By default placeholders are automatically skipped over.
1878 Currently a placeholder is implemented with a value that is
1879 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1880 restricted hashes may change, and the implementation currently is
1881 insufficiently abstracted for any change to be tidy.
1887 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1889 register XPVHV* xhv;
1895 Perl_croak(aTHX_ "Bad hash");
1896 xhv = (XPVHV*)SvANY(hv);
1897 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1899 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1900 SV *key = sv_newmortal();
1902 sv_setsv(key, HeSVKEY_force(entry));
1903 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1909 /* one HE per MAGICAL hash */
1910 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1912 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1914 HeKEY_hek(entry) = hek;
1915 HeKLEN(entry) = HEf_SVKEY;
1917 magic_nextpack((SV*) hv,mg,key);
1919 /* force key to stay around until next time */
1920 HeSVKEY_set(entry, SvREFCNT_inc(key));
1921 return entry; /* beware, hent_val is not set */
1924 SvREFCNT_dec(HeVAL(entry));
1925 Safefree(HeKEY_hek(entry));
1927 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1930 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1931 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1935 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1936 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1937 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1939 /* At start of hash, entry is NULL. */
1942 entry = HeNEXT(entry);
1943 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1945 * Skip past any placeholders -- don't want to include them in
1948 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1949 entry = HeNEXT(entry);
1954 /* OK. Come to the end of the current list. Grab the next one. */
1956 xhv->xhv_riter++; /* HvRITER(hv)++ */
1957 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1958 /* There is no next one. End of the hash. */
1959 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1962 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1963 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1965 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1966 /* If we have an entry, but it's a placeholder, don't count it.
1968 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1969 entry = HeNEXT(entry);
1971 /* Will loop again if this linked list starts NULL
1972 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1973 or if we run through it and find only placeholders. */
1976 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1978 hv_free_ent(hv, oldentry);
1981 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1986 =for apidoc hv_iterkey
1988 Returns the key from the current position of the hash iterator. See
1995 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1997 if (HeKLEN(entry) == HEf_SVKEY) {
1999 char *p = SvPV(HeKEY_sv(entry), len);
2004 *retlen = HeKLEN(entry);
2005 return HeKEY(entry);
2009 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2011 =for apidoc hv_iterkeysv
2013 Returns the key as an C<SV*> from the current position of the hash
2014 iterator. The return value will always be a mortal copy of the key. Also
2021 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2023 if (HeKLEN(entry) != HEf_SVKEY) {
2024 HEK *hek = HeKEY_hek(entry);
2025 int flags = HEK_FLAGS(hek);
2028 if (flags & HVhek_WASUTF8) {
2030 Andreas would like keys he put in as utf8 to come back as utf8
2032 STRLEN utf8_len = HEK_LEN(hek);
2033 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
2035 sv = newSVpvn ((char*)as_utf8, utf8_len);
2037 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
2039 sv = newSVpvn_share(HEK_KEY(hek),
2040 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
2043 return sv_2mortal(sv);
2045 return sv_mortalcopy(HeKEY_sv(entry));
2049 =for apidoc hv_iterval
2051 Returns the value from the current position of the hash iterator. See
2058 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2060 if (SvRMAGICAL(hv)) {
2061 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2062 SV* sv = sv_newmortal();
2063 if (HeKLEN(entry) == HEf_SVKEY)
2064 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2065 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2069 return HeVAL(entry);
2073 =for apidoc hv_iternextsv
2075 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2082 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2085 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
2087 *key = hv_iterkey(he, retlen);
2088 return hv_iterval(hv, he);
2092 =for apidoc hv_magic
2094 Adds magic to a hash. See C<sv_magic>.
2100 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2102 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2105 #if 0 /* use the macro from hv.h instead */
2108 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2110 return HEK_KEY(share_hek(sv, len, hash));
2115 /* possibly free a shared string if no one has access to it
2116 * len and hash must both be valid for str.
2119 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2121 unshare_hek_or_pvn (NULL, str, len, hash);
2126 Perl_unshare_hek(pTHX_ HEK *hek)
2128 unshare_hek_or_pvn(hek, NULL, 0, 0);
2131 /* possibly free a shared string if no one has access to it
2132 hek if non-NULL takes priority over the other 3, else str, len and hash
2133 are used. If so, len and hash must both be valid for str.
2136 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2138 register XPVHV* xhv;
2140 register HE **oentry;
2143 bool is_utf8 = FALSE;
2145 const char *save = str;
2148 hash = HEK_HASH(hek);
2149 } else if (len < 0) {
2150 STRLEN tmplen = -len;
2152 /* See the note in hv_fetch(). --jhi */
2153 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2156 k_flags = HVhek_UTF8;
2158 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2161 /* what follows is the moral equivalent of:
2162 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2163 if (--*Svp == Nullsv)
2164 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2166 xhv = (XPVHV*)SvANY(PL_strtab);
2167 /* assert(xhv_array != 0) */
2169 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2170 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2172 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2173 if (HeKEY_hek(entry) != hek)
2179 int flags_masked = k_flags & HVhek_MASK;
2180 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2181 if (HeHASH(entry) != hash) /* strings can't be equal */
2183 if (HeKLEN(entry) != len)
2185 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2187 if (HeKFLAGS(entry) != flags_masked)
2195 if (--HeVAL(entry) == Nullsv) {
2196 *oentry = HeNEXT(entry);
2198 xhv->xhv_fill--; /* HvFILL(hv)-- */
2199 Safefree(HeKEY_hek(entry));
2201 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2205 UNLOCK_STRTAB_MUTEX;
2206 if (!found && ckWARN_d(WARN_INTERNAL))
2207 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2208 "Attempt to free non-existent shared string '%s'%s",
2209 hek ? HEK_KEY(hek) : str,
2210 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2211 if (k_flags & HVhek_FREEKEY)
2215 /* get a (constant) string ptr from the global string table
2216 * string will get added if it is not already there.
2217 * len and hash must both be valid for str.
2220 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2222 bool is_utf8 = FALSE;
2224 const char *save = str;
2227 STRLEN tmplen = -len;
2229 /* See the note in hv_fetch(). --jhi */
2230 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2232 /* If we were able to downgrade here, then than means that we were passed
2233 in a key which only had chars 0-255, but was utf8 encoded. */
2236 /* If we found we were able to downgrade the string to bytes, then
2237 we should flag that it needs upgrading on keys or each. Also flag
2238 that we need share_hek_flags to free the string. */
2240 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2243 return share_hek_flags (str, len, hash, flags);
2247 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2249 register XPVHV* xhv;
2251 register HE **oentry;
2254 int flags_masked = flags & HVhek_MASK;
2256 /* what follows is the moral equivalent of:
2258 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2259 hv_store(PL_strtab, str, len, Nullsv, hash);
2261 xhv = (XPVHV*)SvANY(PL_strtab);
2262 /* assert(xhv_array != 0) */
2264 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2265 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2266 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2267 if (HeHASH(entry) != hash) /* strings can't be equal */
2269 if (HeKLEN(entry) != len)
2271 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2273 if (HeKFLAGS(entry) != flags_masked)
2280 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2281 HeVAL(entry) = Nullsv;
2282 HeNEXT(entry) = *oentry;
2284 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2285 if (i) { /* initial entry? */
2286 xhv->xhv_fill++; /* HvFILL(hv)++ */
2287 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2292 ++HeVAL(entry); /* use value slot as REFCNT */
2293 UNLOCK_STRTAB_MUTEX;
2295 if (flags & HVhek_FREEKEY)
2298 return HeKEY_hek(entry);