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 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 (HeHASH(entry) != hash) /* strings can't be equal */
284 if (HeKLEN(entry) != (I32)klen)
286 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
288 /* flags is 0 if not utf8. need HeKFLAGS(entry) also 0.
289 flags is 1 if utf8. need HeKFLAGS(entry) also 1.
290 xor is true if bits differ, in which case this isn't a match. */
291 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
293 if (lval && HeKFLAGS(entry) != flags) {
294 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
295 But if entry was set previously with HVhek_WASUTF8 and key now
296 doesn't (or vice versa) then we should change the key's flag,
297 as this is assignment. */
298 if (HvSHAREKEYS(hv)) {
299 /* Need to swap the key we have for a key with the flags we
300 need. As keys are shared we can't just write to the flag,
301 so we share the new one, unshare the old one. */
302 int flags_nofree = flags & ~HVhek_FREEKEY;
303 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
304 unshare_hek (HeKEY_hek(entry));
305 HeKEY_hek(entry) = new_hek;
308 HeKFLAGS(entry) = flags;
310 if (flags & HVhek_FREEKEY)
312 /* if we find a placeholder, we pretend we haven't found anything */
313 if (HeVAL(entry) == &PL_sv_undef)
315 return &HeVAL(entry);
318 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
319 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
321 char *env = PerlEnv_ENVgetenv_len(key,&len);
323 sv = newSVpvn(env,len);
325 if (flags & HVhek_FREEKEY)
327 return hv_store(hv,key,klen,sv,hash);
331 if (!entry && SvREADONLY(hv)) {
332 S_hv_notallowed(aTHX_ flags, key, klen,
333 "access disallowed key '%"SVf"' in"
336 if (lval) { /* gonna assign to this, so it better be there */
338 return hv_store_flags(hv,key,klen,sv,hash,flags);
340 if (flags & HVhek_FREEKEY)
345 /* returns an HE * structure with the all fields set */
346 /* note that hent_val will be a mortal sv for MAGICAL hashes */
348 =for apidoc hv_fetch_ent
350 Returns the hash entry which corresponds to the specified key in the hash.
351 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
352 if you want the function to compute it. IF C<lval> is set then the fetch
353 will be part of a store. Make sure the return value is non-null before
354 accessing it. The return value when C<tb> is a tied hash is a pointer to a
355 static location, so be sure to make a copy of the structure if you need to
358 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
359 information on how to use this function on tied hashes.
365 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
379 if (SvRMAGICAL(hv)) {
380 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
382 keysv = newSVsv(keysv);
383 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
384 /* grab a fake HE/HEK pair from the pool or make a new one */
385 entry = PL_hv_fetch_ent_mh;
387 PL_hv_fetch_ent_mh = HeNEXT(entry);
391 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
392 HeKEY_hek(entry) = (HEK*)k;
394 HeNEXT(entry) = Nullhe;
395 HeSVKEY_set(entry, keysv);
397 sv_upgrade(sv, SVt_PVLV);
399 LvTARG(sv) = (SV*)entry; /* so we can free entry when freeing sv */
402 #ifdef ENV_IS_CASELESS
403 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
405 key = SvPV(keysv, klen);
406 for (i = 0; i < klen; ++i)
407 if (isLOWER(key[i])) {
408 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
409 (void)strupr(SvPVX(nkeysv));
410 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
412 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
419 xhv = (XPVHV*)SvANY(hv);
420 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
422 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
423 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
426 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
427 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
433 keysave = key = SvPV(keysv, klen);
434 is_utf8 = (SvUTF8(keysv)!=0);
437 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
441 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
445 if SvIsCOW_shared_hash(keysv) {
448 PERL_HASH(hash, key, klen);
452 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
453 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
454 for (; entry; entry = HeNEXT(entry)) {
455 if (HeHASH(entry) != hash) /* strings can't be equal */
457 if (HeKLEN(entry) != (I32)klen)
459 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
461 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
463 if (lval && HeKFLAGS(entry) != flags) {
464 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
465 But if entry was set previously with HVhek_WASUTF8 and key now
466 doesn't (or vice versa) then we should change the key's flag,
467 as this is assignment. */
468 if (HvSHAREKEYS(hv)) {
469 /* Need to swap the key we have for a key with the flags we
470 need. As keys are shared we can't just write to the flag,
471 so we share the new one, unshare the old one. */
472 int flags_nofree = flags & ~HVhek_FREEKEY;
473 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
474 unshare_hek (HeKEY_hek(entry));
475 HeKEY_hek(entry) = new_hek;
478 HeKFLAGS(entry) = flags;
482 /* if we find a placeholder, we pretend we haven't found anything */
483 if (HeVAL(entry) == &PL_sv_undef)
487 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
488 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
490 char *env = PerlEnv_ENVgetenv_len(key,&len);
492 sv = newSVpvn(env,len);
494 return hv_store_ent(hv,keysv,sv,hash);
498 if (!entry && SvREADONLY(hv)) {
499 S_hv_notallowed(aTHX_ flags, key, klen,
500 "access disallowed key '%"SVf"' in"
503 if (flags & HVhek_FREEKEY)
505 if (lval) { /* gonna assign to this, so it better be there */
507 return hv_store_ent(hv,keysv,sv,hash);
513 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
515 MAGIC *mg = SvMAGIC(hv);
519 if (isUPPER(mg->mg_type)) {
521 switch (mg->mg_type) {
522 case PERL_MAGIC_tied:
524 *needs_store = FALSE;
527 mg = mg->mg_moremagic;
534 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
535 the length of the key. The C<hash> parameter is the precomputed hash
536 value; if it is zero then Perl will compute it. The return value will be
537 NULL if the operation failed or if the value did not need to be actually
538 stored within the hash (as in the case of tied hashes). Otherwise it can
539 be dereferenced to get the original C<SV*>. Note that the caller is
540 responsible for suitably incrementing the reference count of C<val> before
541 the call, and decrementing it if the function returned NULL. Effectively
542 a successful hv_store takes ownership of one reference to C<val>. This is
543 usually what you want; a newly created SV has a reference count of one, so
544 if all your code does is create SVs then store them in a hash, hv_store
545 will own the only reference to the new SV, and your code doesn't need to do
546 anything further to tidy up. hv_store is not implemented as a call to
547 hv_store_ent, and does not create a temporary SV for the key, so if your
548 key data is not already in SV form then use hv_store in preference to
551 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
552 information on how to use this function on tied hashes.
558 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, U32 hash)
560 bool is_utf8 = FALSE;
561 const char *keysave = key;
570 STRLEN tmplen = klen;
571 /* Just casting the &klen to (STRLEN) won't work well
572 * if STRLEN and I32 are of different widths. --jhi */
573 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
575 /* If we were able to downgrade here, then than means that we were
576 passed in a key which only had chars 0-255, but was utf8 encoded. */
579 /* If we found we were able to downgrade the string to bytes, then
580 we should flag that it needs upgrading on keys or each. */
582 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
585 return hv_store_flags (hv, key, klen, val, hash, flags);
589 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
590 register U32 hash, int flags)
595 register HE **oentry;
600 xhv = (XPVHV*)SvANY(hv);
604 hv_magic_check (hv, &needs_copy, &needs_store);
606 mg_copy((SV*)hv, val, key, klen);
607 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store) {
608 if (flags & HVhek_FREEKEY)
612 #ifdef ENV_IS_CASELESS
613 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
614 key = savepvn(key,klen);
615 key = (const char*)strupr((char*)key);
623 HvHASKFLAGS_on((SV*)hv);
626 PERL_HASH(hash, key, klen);
628 if (!xhv->xhv_array /* !HvARRAY(hv) */)
629 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
630 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
633 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
634 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
637 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
638 if (HeHASH(entry) != hash) /* strings can't be equal */
640 if (HeKLEN(entry) != (I32)klen)
642 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
644 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
646 if (HeVAL(entry) == &PL_sv_undef)
647 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
649 SvREFCNT_dec(HeVAL(entry));
650 if (flags & HVhek_PLACEHOLD) {
651 /* We have been requested to insert a placeholder. Currently
652 only Storable is allowed to do this. */
653 xhv->xhv_placeholders++;
654 HeVAL(entry) = &PL_sv_undef;
658 if (HeKFLAGS(entry) != flags) {
659 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
660 But if entry was set previously with HVhek_WASUTF8 and key now
661 doesn't (or vice versa) then we should change the key's flag,
662 as this is assignment. */
663 if (HvSHAREKEYS(hv)) {
664 /* Need to swap the key we have for a key with the flags we
665 need. As keys are shared we can't just write to the flag,
666 so we share the new one, unshare the old one. */
667 int flags_nofree = flags & ~HVhek_FREEKEY;
668 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
669 unshare_hek (HeKEY_hek(entry));
670 HeKEY_hek(entry) = new_hek;
673 HeKFLAGS(entry) = flags;
675 if (flags & HVhek_FREEKEY)
677 return &HeVAL(entry);
680 if (SvREADONLY(hv)) {
681 S_hv_notallowed(aTHX_ flags, key, klen,
682 "access disallowed key '%"SVf"' to"
687 /* share_hek_flags will do the free for us. This might be considered
690 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
691 else /* gotta do the real thing */
692 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
693 if (flags & HVhek_PLACEHOLD) {
694 /* We have been requested to insert a placeholder. Currently
695 only Storable is allowed to do this. */
696 xhv->xhv_placeholders++;
697 HeVAL(entry) = &PL_sv_undef;
700 HeNEXT(entry) = *oentry;
703 xhv->xhv_keys++; /* HvKEYS(hv)++ */
704 if (i) { /* initial entry? */
705 xhv->xhv_fill++; /* HvFILL(hv)++ */
706 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
710 return &HeVAL(entry);
714 =for apidoc hv_store_ent
716 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
717 parameter is the precomputed hash value; if it is zero then Perl will
718 compute it. The return value is the new hash entry so created. It will be
719 NULL if the operation failed or if the value did not need to be actually
720 stored within the hash (as in the case of tied hashes). Otherwise the
721 contents of the return value can be accessed using the C<He?> macros
722 described here. Note that the caller is responsible for suitably
723 incrementing the reference count of C<val> before the call, and
724 decrementing it if the function returned NULL. Effectively a successful
725 hv_store_ent takes ownership of one reference to C<val>. This is
726 usually what you want; a newly created SV has a reference count of one, so
727 if all your code does is create SVs then store them in a hash, hv_store
728 will own the only reference to the new SV, and your code doesn't need to do
729 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
730 unlike C<val> it does not take ownership of it, so maintaining the correct
731 reference count on C<key> is entirely the caller's responsibility. hv_store
732 is not implemented as a call to hv_store_ent, and does not create a temporary
733 SV for the key, so if your key data is not already in SV form then use
734 hv_store in preference to hv_store_ent.
736 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
737 information on how to use this function on tied hashes.
743 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
758 xhv = (XPVHV*)SvANY(hv);
762 hv_magic_check (hv, &needs_copy, &needs_store);
764 bool save_taint = PL_tainted;
766 PL_tainted = SvTAINTED(keysv);
767 keysv = sv_2mortal(newSVsv(keysv));
768 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
769 TAINT_IF(save_taint);
770 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
772 #ifdef ENV_IS_CASELESS
773 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
774 key = SvPV(keysv, klen);
775 keysv = sv_2mortal(newSVpvn(key,klen));
776 (void)strupr(SvPVX(keysv));
783 keysave = key = SvPV(keysv, klen);
784 is_utf8 = (SvUTF8(keysv) != 0);
787 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
791 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
792 HvHASKFLAGS_on((SV*)hv);
796 if SvIsCOW_shared_hash(keysv) {
799 PERL_HASH(hash, key, klen);
803 if (!xhv->xhv_array /* !HvARRAY(hv) */)
804 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
805 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
808 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
809 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
812 for (; entry; i=0, entry = HeNEXT(entry)) {
813 if (HeHASH(entry) != hash) /* strings can't be equal */
815 if (HeKLEN(entry) != (I32)klen)
817 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
819 if ((HeKFLAGS(entry) ^ flags) & HVhek_UTF8)
821 if (HeVAL(entry) == &PL_sv_undef)
822 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
824 SvREFCNT_dec(HeVAL(entry));
826 if (HeKFLAGS(entry) != flags) {
827 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
828 But if entry was set previously with HVhek_WASUTF8 and key now
829 doesn't (or vice versa) then we should change the key's flag,
830 as this is assignment. */
831 if (HvSHAREKEYS(hv)) {
832 /* Need to swap the key we have for a key with the flags we
833 need. As keys are shared we can't just write to the flag,
834 so we share the new one, unshare the old one. */
835 int flags_nofree = flags & ~HVhek_FREEKEY;
836 HEK *new_hek = share_hek_flags(key, klen, hash, flags_nofree);
837 unshare_hek (HeKEY_hek(entry));
838 HeKEY_hek(entry) = new_hek;
841 HeKFLAGS(entry) = flags;
843 if (flags & HVhek_FREEKEY)
848 if (SvREADONLY(hv)) {
849 S_hv_notallowed(aTHX_ flags, key, klen,
850 "access disallowed key '%"SVf"' to"
855 /* share_hek_flags will do the free for us. This might be considered
858 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
859 else /* gotta do the real thing */
860 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
862 HeNEXT(entry) = *oentry;
865 xhv->xhv_keys++; /* HvKEYS(hv)++ */
866 if (i) { /* initial entry? */
867 xhv->xhv_fill++; /* HvFILL(hv)++ */
868 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
876 =for apidoc hv_delete
878 Deletes a key/value pair in the hash. The value SV is removed from the
879 hash and returned to the caller. The C<klen> is the length of the key.
880 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
887 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
893 register HE **oentry;
896 bool is_utf8 = FALSE;
898 const char *keysave = key;
906 if (SvRMAGICAL(hv)) {
909 hv_magic_check (hv, &needs_copy, &needs_store);
911 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
917 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
918 /* No longer an element */
919 sv_unmagic(sv, PERL_MAGIC_tiedelem);
922 return Nullsv; /* element cannot be deleted */
924 #ifdef ENV_IS_CASELESS
925 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
926 sv = sv_2mortal(newSVpvn(key,klen));
927 key = strupr(SvPVX(sv));
932 xhv = (XPVHV*)SvANY(hv);
933 if (!xhv->xhv_array /* !HvARRAY(hv) */)
937 STRLEN tmplen = klen;
938 /* See the note in hv_fetch(). --jhi */
939 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
942 k_flags = HVhek_UTF8;
944 k_flags |= HVhek_FREEKEY;
947 PERL_HASH(hash, key, klen);
949 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
950 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
953 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
954 if (HeHASH(entry) != hash) /* strings can't be equal */
956 if (HeKLEN(entry) != (I32)klen)
958 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
960 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
962 if (k_flags & HVhek_FREEKEY)
964 /* if placeholder is here, it's already been deleted.... */
965 if (HeVAL(entry) == &PL_sv_undef)
968 return Nullsv; /* if still SvREADONLY, leave it deleted. */
970 /* okay, really delete the placeholder... */
971 *oentry = HeNEXT(entry);
973 xhv->xhv_fill--; /* HvFILL(hv)-- */
974 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
977 hv_free_ent(hv, entry);
978 xhv->xhv_keys--; /* HvKEYS(hv)-- */
979 if (xhv->xhv_keys == 0)
981 xhv->xhv_placeholders--;
985 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
986 S_hv_notallowed(aTHX_ k_flags, key, klen,
987 "delete readonly key '%"SVf"' from"
991 if (flags & G_DISCARD)
994 sv = sv_2mortal(HeVAL(entry));
995 HeVAL(entry) = &PL_sv_undef;
999 * If a restricted hash, rather than really deleting the entry, put
1000 * a placeholder there. This marks the key as being "approved", so
1001 * we can still access via not-really-existing key without raising
1004 if (SvREADONLY(hv)) {
1005 HeVAL(entry) = &PL_sv_undef;
1006 /* We'll be saving this slot, so the number of allocated keys
1007 * doesn't go down, but the number placeholders goes up */
1008 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1010 *oentry = HeNEXT(entry);
1012 xhv->xhv_fill--; /* HvFILL(hv)-- */
1013 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1016 hv_free_ent(hv, entry);
1017 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1018 if (xhv->xhv_keys == 0)
1019 HvHASKFLAGS_off(hv);
1023 if (SvREADONLY(hv)) {
1024 S_hv_notallowed(aTHX_ k_flags, key, klen,
1025 "access disallowed key '%"SVf"' from"
1029 if (k_flags & HVhek_FREEKEY)
1035 =for apidoc hv_delete_ent
1037 Deletes a key/value pair in the hash. The value SV is removed from the
1038 hash and returned to the caller. The C<flags> value will normally be zero;
1039 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
1040 precomputed hash value, or 0 to ask for it to be computed.
1046 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
1048 register XPVHV* xhv;
1053 register HE **oentry;
1061 if (SvRMAGICAL(hv)) {
1064 hv_magic_check (hv, &needs_copy, &needs_store);
1066 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
1068 if (SvMAGICAL(sv)) {
1072 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
1073 /* No longer an element */
1074 sv_unmagic(sv, PERL_MAGIC_tiedelem);
1077 return Nullsv; /* element cannot be deleted */
1079 #ifdef ENV_IS_CASELESS
1080 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1081 key = SvPV(keysv, klen);
1082 keysv = sv_2mortal(newSVpvn(key,klen));
1083 (void)strupr(SvPVX(keysv));
1089 xhv = (XPVHV*)SvANY(hv);
1090 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1093 keysave = key = SvPV(keysv, klen);
1094 is_utf8 = (SvUTF8(keysv) != 0);
1097 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1099 k_flags = HVhek_UTF8;
1101 k_flags |= HVhek_FREEKEY;
1105 PERL_HASH(hash, key, klen);
1107 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1108 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1111 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1112 if (HeHASH(entry) != hash) /* strings can't be equal */
1114 if (HeKLEN(entry) != (I32)klen)
1116 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1118 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1120 if (k_flags & HVhek_FREEKEY)
1123 /* if placeholder is here, it's already been deleted.... */
1124 if (HeVAL(entry) == &PL_sv_undef)
1127 return Nullsv; /* if still SvREADONLY, leave it deleted. */
1129 /* okay, really delete the placeholder. */
1130 *oentry = HeNEXT(entry);
1132 xhv->xhv_fill--; /* HvFILL(hv)-- */
1133 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1136 hv_free_ent(hv, entry);
1137 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1138 if (xhv->xhv_keys == 0)
1139 HvHASKFLAGS_off(hv);
1140 xhv->xhv_placeholders--;
1143 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1144 S_hv_notallowed(aTHX_ k_flags, key, klen,
1145 "delete readonly key '%"SVf"' from"
1149 if (flags & G_DISCARD)
1152 sv = sv_2mortal(HeVAL(entry));
1153 HeVAL(entry) = &PL_sv_undef;
1157 * If a restricted hash, rather than really deleting the entry, put
1158 * a placeholder there. This marks the key as being "approved", so
1159 * we can still access via not-really-existing key without raising
1162 if (SvREADONLY(hv)) {
1163 HeVAL(entry) = &PL_sv_undef;
1164 /* We'll be saving this slot, so the number of allocated keys
1165 * doesn't go down, but the number placeholders goes up */
1166 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1168 *oentry = HeNEXT(entry);
1170 xhv->xhv_fill--; /* HvFILL(hv)-- */
1171 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1174 hv_free_ent(hv, entry);
1175 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1176 if (xhv->xhv_keys == 0)
1177 HvHASKFLAGS_off(hv);
1181 if (SvREADONLY(hv)) {
1182 S_hv_notallowed(aTHX_ k_flags, key, klen,
1183 "delete disallowed key '%"SVf"' from"
1187 if (k_flags & HVhek_FREEKEY)
1193 =for apidoc hv_exists
1195 Returns a boolean indicating whether the specified hash key exists. The
1196 C<klen> is the length of the key.
1202 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
1204 register XPVHV* xhv;
1208 bool is_utf8 = FALSE;
1209 const char *keysave = key;
1220 if (SvRMAGICAL(hv)) {
1221 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1222 sv = sv_newmortal();
1223 mg_copy((SV*)hv, sv, key, klen);
1224 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1225 return (bool)SvTRUE(sv);
1227 #ifdef ENV_IS_CASELESS
1228 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1229 sv = sv_2mortal(newSVpvn(key,klen));
1230 key = strupr(SvPVX(sv));
1235 xhv = (XPVHV*)SvANY(hv);
1236 #ifndef DYNAMIC_ENV_FETCH
1237 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1242 STRLEN tmplen = klen;
1243 /* See the note in hv_fetch(). --jhi */
1244 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1247 k_flags = HVhek_UTF8;
1249 k_flags |= HVhek_FREEKEY;
1252 PERL_HASH(hash, key, klen);
1254 #ifdef DYNAMIC_ENV_FETCH
1255 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1258 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1259 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1260 for (; entry; entry = HeNEXT(entry)) {
1261 if (HeHASH(entry) != hash) /* strings can't be equal */
1263 if (HeKLEN(entry) != klen)
1265 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1267 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1269 if (k_flags & HVhek_FREEKEY)
1271 /* If we find the key, but the value is a placeholder, return false. */
1272 if (HeVAL(entry) == &PL_sv_undef)
1277 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1278 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1280 char *env = PerlEnv_ENVgetenv_len(key,&len);
1282 sv = newSVpvn(env,len);
1284 (void)hv_store(hv,key,klen,sv,hash);
1285 if (k_flags & HVhek_FREEKEY)
1291 if (k_flags & HVhek_FREEKEY)
1298 =for apidoc hv_exists_ent
1300 Returns a boolean indicating whether the specified hash key exists. C<hash>
1301 can be a valid precomputed hash value, or 0 to ask for it to be
1308 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1310 register XPVHV* xhv;
1322 if (SvRMAGICAL(hv)) {
1323 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1324 SV* svret = sv_newmortal();
1325 sv = sv_newmortal();
1326 keysv = sv_2mortal(newSVsv(keysv));
1327 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1328 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1329 return (bool)SvTRUE(svret);
1331 #ifdef ENV_IS_CASELESS
1332 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1333 key = SvPV(keysv, klen);
1334 keysv = sv_2mortal(newSVpvn(key,klen));
1335 (void)strupr(SvPVX(keysv));
1341 xhv = (XPVHV*)SvANY(hv);
1342 #ifndef DYNAMIC_ENV_FETCH
1343 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1347 keysave = key = SvPV(keysv, klen);
1348 is_utf8 = (SvUTF8(keysv) != 0);
1350 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1352 k_flags = HVhek_UTF8;
1354 k_flags |= HVhek_FREEKEY;
1357 PERL_HASH(hash, key, klen);
1359 #ifdef DYNAMIC_ENV_FETCH
1360 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1363 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1364 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1365 for (; entry; entry = HeNEXT(entry)) {
1366 if (HeHASH(entry) != hash) /* strings can't be equal */
1368 if (HeKLEN(entry) != (I32)klen)
1370 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1372 if ((HeKFLAGS(entry) ^ k_flags) & HVhek_UTF8)
1374 if (k_flags & HVhek_FREEKEY)
1376 /* If we find the key, but the value is a placeholder, return false. */
1377 if (HeVAL(entry) == &PL_sv_undef)
1381 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1382 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1384 char *env = PerlEnv_ENVgetenv_len(key,&len);
1386 sv = newSVpvn(env,len);
1388 (void)hv_store_ent(hv,keysv,sv,hash);
1389 if (k_flags & HVhek_FREEKEY)
1395 if (k_flags & HVhek_FREEKEY)
1401 S_hsplit(pTHX_ HV *hv)
1403 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1404 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1405 register I32 newsize = oldsize * 2;
1407 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1411 register HE **oentry;
1414 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1415 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1421 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1426 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1427 if (oldsize >= 64) {
1428 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1429 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1432 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1436 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1437 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1438 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1441 for (i=0; i<oldsize; i++,aep++) {
1442 if (!*aep) /* non-existent */
1445 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1446 if ((HeHASH(entry) & newsize) != (U32)i) {
1447 *oentry = HeNEXT(entry);
1448 HeNEXT(entry) = *bep;
1450 xhv->xhv_fill++; /* HvFILL(hv)++ */
1455 oentry = &HeNEXT(entry);
1457 if (!*aep) /* everything moved */
1458 xhv->xhv_fill--; /* HvFILL(hv)-- */
1463 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1465 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1466 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1467 register I32 newsize;
1473 register HE **oentry;
1475 newsize = (I32) newmax; /* possible truncation here */
1476 if (newsize != newmax || newmax <= oldsize)
1478 while ((newsize & (1 + ~newsize)) != newsize) {
1479 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1481 if (newsize < newmax)
1483 if (newsize < newmax)
1484 return; /* overflow detection */
1486 a = xhv->xhv_array; /* HvARRAY(hv) */
1489 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1490 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1496 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1501 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1502 if (oldsize >= 64) {
1503 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1504 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1507 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1510 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1513 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1515 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1516 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1517 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1521 for (i=0; i<oldsize; i++,aep++) {
1522 if (!*aep) /* non-existent */
1524 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1525 if ((j = (HeHASH(entry) & newsize)) != i) {
1527 *oentry = HeNEXT(entry);
1528 if (!(HeNEXT(entry) = aep[j]))
1529 xhv->xhv_fill++; /* HvFILL(hv)++ */
1534 oentry = &HeNEXT(entry);
1536 if (!*aep) /* everything moved */
1537 xhv->xhv_fill--; /* HvFILL(hv)-- */
1544 Creates a new HV. The reference count is set to 1.
1553 register XPVHV* xhv;
1555 hv = (HV*)NEWSV(502,0);
1556 sv_upgrade((SV *)hv, SVt_PVHV);
1557 xhv = (XPVHV*)SvANY(hv);
1560 #ifndef NODEFAULT_SHAREKEYS
1561 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1563 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1564 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1565 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1566 (void)hv_iterinit(hv); /* so each() will start off right */
1571 Perl_newHVhv(pTHX_ HV *ohv)
1574 STRLEN hv_max, hv_fill;
1576 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1578 hv_max = HvMAX(ohv);
1580 if (!SvMAGICAL((SV *)ohv)) {
1581 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1583 bool shared = !!HvSHAREKEYS(ohv);
1584 HE **ents, **oents = (HE **)HvARRAY(ohv);
1586 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1589 /* In each bucket... */
1590 for (i = 0; i <= hv_max; i++) {
1591 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1598 /* Copy the linked list of entries. */
1599 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1600 U32 hash = HeHASH(oent);
1601 char *key = HeKEY(oent);
1602 STRLEN len = HeKLEN(oent);
1603 int flags = HeKFLAGS(oent);
1606 HeVAL(ent) = newSVsv(HeVAL(oent));
1608 = shared ? share_hek_flags(key, len, hash, flags)
1609 : save_hek_flags(key, len, hash, flags);
1620 HvFILL(hv) = hv_fill;
1621 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1625 /* Iterate over ohv, copying keys and values one at a time. */
1627 I32 riter = HvRITER(ohv);
1628 HE *eiter = HvEITER(ohv);
1630 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1631 while (hv_max && hv_max + 1 >= hv_fill * 2)
1632 hv_max = hv_max / 2;
1636 while ((entry = hv_iternext_flags(ohv, 0))) {
1637 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1638 newSVsv(HeVAL(entry)), HeHASH(entry),
1641 HvRITER(ohv) = riter;
1642 HvEITER(ohv) = eiter;
1649 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1656 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1657 PL_sub_generation++; /* may be deletion of method from stash */
1659 if (HeKLEN(entry) == HEf_SVKEY) {
1660 SvREFCNT_dec(HeKEY_sv(entry));
1661 Safefree(HeKEY_hek(entry));
1663 else if (HvSHAREKEYS(hv))
1664 unshare_hek(HeKEY_hek(entry));
1666 Safefree(HeKEY_hek(entry));
1671 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1675 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1676 PL_sub_generation++; /* may be deletion of method from stash */
1677 sv_2mortal(HeVAL(entry)); /* free between statements */
1678 if (HeKLEN(entry) == HEf_SVKEY) {
1679 sv_2mortal(HeKEY_sv(entry));
1680 Safefree(HeKEY_hek(entry));
1682 else if (HvSHAREKEYS(hv))
1683 unshare_hek(HeKEY_hek(entry));
1685 Safefree(HeKEY_hek(entry));
1690 =for apidoc hv_clear
1692 Clears a hash, making it empty.
1698 Perl_hv_clear(pTHX_ HV *hv)
1700 register XPVHV* xhv;
1704 if(SvREADONLY(hv)) {
1705 Perl_croak(aTHX_ "Attempt to clear a restricted hash");
1708 xhv = (XPVHV*)SvANY(hv);
1710 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1711 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1712 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1713 if (xhv->xhv_array /* HvARRAY(hv) */)
1714 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1715 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1720 HvHASKFLAGS_off(hv);
1724 S_hfreeentries(pTHX_ HV *hv)
1726 register HE **array;
1728 register HE *oentry = Null(HE*);
1739 array = HvARRAY(hv);
1744 entry = HeNEXT(entry);
1745 hv_free_ent(hv, oentry);
1750 entry = array[riter];
1753 (void)hv_iterinit(hv);
1757 =for apidoc hv_undef
1765 Perl_hv_undef(pTHX_ HV *hv)
1767 register XPVHV* xhv;
1770 xhv = (XPVHV*)SvANY(hv);
1772 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1775 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1776 Safefree(HvNAME(hv));
1779 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1780 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1781 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1782 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1783 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1790 =for apidoc hv_iterinit
1792 Prepares a starting point to traverse a hash table. Returns the number of
1793 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1794 currently only meaningful for hashes without tie magic.
1796 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1797 hash buckets that happen to be in use. If you still need that esoteric
1798 value, you can get it through the macro C<HvFILL(tb)>.
1805 Perl_hv_iterinit(pTHX_ HV *hv)
1807 register XPVHV* xhv;
1811 Perl_croak(aTHX_ "Bad hash");
1812 xhv = (XPVHV*)SvANY(hv);
1813 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1814 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1816 hv_free_ent(hv, entry);
1818 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1819 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1820 /* used to be xhv->xhv_fill before 5.004_65 */
1821 return XHvTOTALKEYS(xhv);
1824 =for apidoc hv_iternext
1826 Returns entries from a hash iterator. See C<hv_iterinit>.
1828 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1829 iterator currently points to, without losing your place or invalidating your
1830 iterator. Note that in this case the current entry is deleted from the hash
1831 with your iterator holding the last reference to it. Your iterator is flagged
1832 to free the entry on the next call to C<hv_iternext>, so you must not discard
1833 your iterator immediately else the entry will leak - call C<hv_iternext> to
1834 trigger the resource deallocation.
1840 Perl_hv_iternext(pTHX_ HV *hv)
1842 return hv_iternext_flags(hv, 0);
1846 =for apidoc hv_iternext_flags
1848 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1849 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1850 set the placeholders keys (for restricted hashes) will be returned in addition
1851 to normal keys. By default placeholders are automatically skipped over.
1852 Currently a placeholder is implemented with a value that is literally
1853 <&Perl_sv_undef> (a regular C<undef> value is a normal read-write SV for which
1854 C<!SvOK> is false). Note that the implementation of placeholders and
1855 restricted hashes may change, and the implementation currently is
1856 insufficiently abstracted for any change to be tidy.
1862 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1864 register XPVHV* xhv;
1870 Perl_croak(aTHX_ "Bad hash");
1871 xhv = (XPVHV*)SvANY(hv);
1872 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1874 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1875 SV *key = sv_newmortal();
1877 sv_setsv(key, HeSVKEY_force(entry));
1878 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1884 /* one HE per MAGICAL hash */
1885 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1887 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1889 HeKEY_hek(entry) = hek;
1890 HeKLEN(entry) = HEf_SVKEY;
1892 magic_nextpack((SV*) hv,mg,key);
1894 /* force key to stay around until next time */
1895 HeSVKEY_set(entry, SvREFCNT_inc(key));
1896 return entry; /* beware, hent_val is not set */
1899 SvREFCNT_dec(HeVAL(entry));
1900 Safefree(HeKEY_hek(entry));
1902 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1905 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1906 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1910 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1911 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1912 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1914 /* At start of hash, entry is NULL. */
1917 entry = HeNEXT(entry);
1918 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1920 * Skip past any placeholders -- don't want to include them in
1923 while (entry && HeVAL(entry) == &PL_sv_undef) {
1924 entry = HeNEXT(entry);
1929 /* OK. Come to the end of the current list. Grab the next one. */
1931 xhv->xhv_riter++; /* HvRITER(hv)++ */
1932 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1933 /* There is no next one. End of the hash. */
1934 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1937 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1938 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1940 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1941 /* If we have an entry, but it's a placeholder, don't count it.
1943 while (entry && HeVAL(entry) == &PL_sv_undef)
1944 entry = HeNEXT(entry);
1946 /* Will loop again if this linked list starts NULL
1947 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1948 or if we run through it and find only placeholders. */
1951 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1953 hv_free_ent(hv, oldentry);
1956 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1961 =for apidoc hv_iterkey
1963 Returns the key from the current position of the hash iterator. See
1970 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1972 if (HeKLEN(entry) == HEf_SVKEY) {
1974 char *p = SvPV(HeKEY_sv(entry), len);
1979 *retlen = HeKLEN(entry);
1980 return HeKEY(entry);
1984 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1986 =for apidoc hv_iterkeysv
1988 Returns the key as an C<SV*> from the current position of the hash
1989 iterator. The return value will always be a mortal copy of the key. Also
1996 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1998 if (HeKLEN(entry) != HEf_SVKEY) {
1999 HEK *hek = HeKEY_hek(entry);
2000 int flags = HEK_FLAGS(hek);
2003 if (flags & HVhek_WASUTF8) {
2005 Andreas would like keys he put in as utf8 to come back as utf8
2007 STRLEN utf8_len = HEK_LEN(hek);
2008 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
2010 sv = newSVpvn ((char*)as_utf8, utf8_len);
2012 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
2014 sv = newSVpvn_share(HEK_KEY(hek),
2015 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
2018 return sv_2mortal(sv);
2020 return sv_mortalcopy(HeKEY_sv(entry));
2024 =for apidoc hv_iterval
2026 Returns the value from the current position of the hash iterator. See
2033 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2035 if (SvRMAGICAL(hv)) {
2036 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2037 SV* sv = sv_newmortal();
2038 if (HeKLEN(entry) == HEf_SVKEY)
2039 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2040 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2044 return HeVAL(entry);
2048 =for apidoc hv_iternextsv
2050 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2057 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2060 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
2062 *key = hv_iterkey(he, retlen);
2063 return hv_iterval(hv, he);
2067 =for apidoc hv_magic
2069 Adds magic to a hash. See C<sv_magic>.
2075 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2077 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2080 #if 0 /* use the macro from hv.h instead */
2083 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2085 return HEK_KEY(share_hek(sv, len, hash));
2090 /* possibly free a shared string if no one has access to it
2091 * len and hash must both be valid for str.
2094 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2096 unshare_hek_or_pvn (NULL, str, len, hash);
2101 Perl_unshare_hek(pTHX_ HEK *hek)
2103 unshare_hek_or_pvn(hek, NULL, 0, 0);
2106 /* possibly free a shared string if no one has access to it
2107 hek if non-NULL takes priority over the other 3, else str, len and hash
2108 are used. If so, len and hash must both be valid for str.
2111 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2113 register XPVHV* xhv;
2115 register HE **oentry;
2118 bool is_utf8 = FALSE;
2120 const char *save = str;
2123 hash = HEK_HASH(hek);
2124 } else if (len < 0) {
2125 STRLEN tmplen = -len;
2127 /* See the note in hv_fetch(). --jhi */
2128 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2131 k_flags = HVhek_UTF8;
2133 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2136 /* what follows is the moral equivalent of:
2137 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2138 if (--*Svp == Nullsv)
2139 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2141 xhv = (XPVHV*)SvANY(PL_strtab);
2142 /* assert(xhv_array != 0) */
2144 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2145 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2147 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2148 if (HeKEY_hek(entry) != hek)
2154 int flags_masked = k_flags & HVhek_MASK;
2155 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2156 if (HeHASH(entry) != hash) /* strings can't be equal */
2158 if (HeKLEN(entry) != len)
2160 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2162 if (HeKFLAGS(entry) != flags_masked)
2170 if (--HeVAL(entry) == Nullsv) {
2171 *oentry = HeNEXT(entry);
2173 xhv->xhv_fill--; /* HvFILL(hv)-- */
2174 Safefree(HeKEY_hek(entry));
2176 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2180 UNLOCK_STRTAB_MUTEX;
2181 if (!found && ckWARN_d(WARN_INTERNAL))
2182 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2183 "Attempt to free non-existent shared string '%s'%s",
2184 hek ? HEK_KEY(hek) : str,
2185 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2186 if (k_flags & HVhek_FREEKEY)
2190 /* get a (constant) string ptr from the global string table
2191 * string will get added if it is not already there.
2192 * len and hash must both be valid for str.
2195 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2197 bool is_utf8 = FALSE;
2199 const char *save = str;
2202 STRLEN tmplen = -len;
2204 /* See the note in hv_fetch(). --jhi */
2205 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2207 /* If we were able to downgrade here, then than means that we were passed
2208 in a key which only had chars 0-255, but was utf8 encoded. */
2211 /* If we found we were able to downgrade the string to bytes, then
2212 we should flag that it needs upgrading on keys or each. Also flag
2213 that we need share_hek_flags to free the string. */
2215 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2218 return share_hek_flags (str, len, hash, flags);
2222 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2224 register XPVHV* xhv;
2226 register HE **oentry;
2229 int flags_masked = flags & HVhek_MASK;
2231 /* what follows is the moral equivalent of:
2233 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2234 hv_store(PL_strtab, str, len, Nullsv, hash);
2236 xhv = (XPVHV*)SvANY(PL_strtab);
2237 /* assert(xhv_array != 0) */
2239 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2240 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2241 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2242 if (HeHASH(entry) != hash) /* strings can't be equal */
2244 if (HeKLEN(entry) != len)
2246 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2248 if (HeKFLAGS(entry) != flags_masked)
2255 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2256 HeVAL(entry) = Nullsv;
2257 HeNEXT(entry) = *oentry;
2259 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2260 if (i) { /* initial entry? */
2261 xhv->xhv_fill++; /* HvFILL(hv)++ */
2262 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2267 ++HeVAL(entry); /* use value slot as REFCNT */
2268 UNLOCK_STRTAB_MUTEX;
2270 if (flags & HVhek_FREEKEY)
2273 return HeKEY_hek(entry);