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 (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 xhv = (XPVHV*)SvANY(hv);
1706 if(SvREADONLY(hv)) {
1707 /* restricted hash: convert all keys to placeholders */
1710 for (i=0; i< (I32) xhv->xhv_max; i++) {
1711 entry = ((HE**)xhv->xhv_array)[i];
1712 for (; entry; entry = HeNEXT(entry)) {
1713 /* not already placeholder */
1714 if (HeVAL(entry) != &PL_sv_undef) {
1715 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1716 SV* keysv = hv_iterkeysv(entry);
1718 "Attempt to delete readonly key '%_' from a restricted hash",
1721 SvREFCNT_dec(HeVAL(entry));
1722 HeVAL(entry) = &PL_sv_undef;
1723 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1731 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1732 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
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);
1765 entry = HeNEXT(entry);
1766 hv_free_ent(hv, oentry);
1771 entry = array[riter];
1774 (void)hv_iterinit(hv);
1778 =for apidoc hv_undef
1786 Perl_hv_undef(pTHX_ HV *hv)
1788 register XPVHV* xhv;
1791 xhv = (XPVHV*)SvANY(hv);
1793 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1796 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1797 Safefree(HvNAME(hv));
1800 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1801 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1802 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1803 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1804 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1811 =for apidoc hv_iterinit
1813 Prepares a starting point to traverse a hash table. Returns the number of
1814 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1815 currently only meaningful for hashes without tie magic.
1817 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1818 hash buckets that happen to be in use. If you still need that esoteric
1819 value, you can get it through the macro C<HvFILL(tb)>.
1826 Perl_hv_iterinit(pTHX_ HV *hv)
1828 register XPVHV* xhv;
1832 Perl_croak(aTHX_ "Bad hash");
1833 xhv = (XPVHV*)SvANY(hv);
1834 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1835 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1837 hv_free_ent(hv, entry);
1839 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1840 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1841 /* used to be xhv->xhv_fill before 5.004_65 */
1842 return XHvTOTALKEYS(xhv);
1845 =for apidoc hv_iternext
1847 Returns entries from a hash iterator. See C<hv_iterinit>.
1849 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1850 iterator currently points to, without losing your place or invalidating your
1851 iterator. Note that in this case the current entry is deleted from the hash
1852 with your iterator holding the last reference to it. Your iterator is flagged
1853 to free the entry on the next call to C<hv_iternext>, so you must not discard
1854 your iterator immediately else the entry will leak - call C<hv_iternext> to
1855 trigger the resource deallocation.
1861 Perl_hv_iternext(pTHX_ HV *hv)
1863 return hv_iternext_flags(hv, 0);
1867 =for apidoc hv_iternext_flags
1869 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1870 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1871 set the placeholders keys (for restricted hashes) will be returned in addition
1872 to normal keys. By default placeholders are automatically skipped over.
1873 Currently a placeholder is implemented with a value that is literally
1874 <&Perl_sv_undef> (a regular C<undef> value is a normal read-write SV for which
1875 C<!SvOK> is false). Note that the implementation of placeholders and
1876 restricted hashes may change, and the implementation currently is
1877 insufficiently abstracted for any change to be tidy.
1883 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1885 register XPVHV* xhv;
1891 Perl_croak(aTHX_ "Bad hash");
1892 xhv = (XPVHV*)SvANY(hv);
1893 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1895 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1896 SV *key = sv_newmortal();
1898 sv_setsv(key, HeSVKEY_force(entry));
1899 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1905 /* one HE per MAGICAL hash */
1906 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1908 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1910 HeKEY_hek(entry) = hek;
1911 HeKLEN(entry) = HEf_SVKEY;
1913 magic_nextpack((SV*) hv,mg,key);
1915 /* force key to stay around until next time */
1916 HeSVKEY_set(entry, SvREFCNT_inc(key));
1917 return entry; /* beware, hent_val is not set */
1920 SvREFCNT_dec(HeVAL(entry));
1921 Safefree(HeKEY_hek(entry));
1923 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1926 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1927 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1931 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1932 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1933 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1935 /* At start of hash, entry is NULL. */
1938 entry = HeNEXT(entry);
1939 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1941 * Skip past any placeholders -- don't want to include them in
1944 while (entry && HeVAL(entry) == &PL_sv_undef) {
1945 entry = HeNEXT(entry);
1950 /* OK. Come to the end of the current list. Grab the next one. */
1952 xhv->xhv_riter++; /* HvRITER(hv)++ */
1953 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1954 /* There is no next one. End of the hash. */
1955 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1958 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1959 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1961 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1962 /* If we have an entry, but it's a placeholder, don't count it.
1964 while (entry && HeVAL(entry) == &PL_sv_undef)
1965 entry = HeNEXT(entry);
1967 /* Will loop again if this linked list starts NULL
1968 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1969 or if we run through it and find only placeholders. */
1972 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1974 hv_free_ent(hv, oldentry);
1977 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1982 =for apidoc hv_iterkey
1984 Returns the key from the current position of the hash iterator. See
1991 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1993 if (HeKLEN(entry) == HEf_SVKEY) {
1995 char *p = SvPV(HeKEY_sv(entry), len);
2000 *retlen = HeKLEN(entry);
2001 return HeKEY(entry);
2005 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2007 =for apidoc hv_iterkeysv
2009 Returns the key as an C<SV*> from the current position of the hash
2010 iterator. The return value will always be a mortal copy of the key. Also
2017 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2019 if (HeKLEN(entry) != HEf_SVKEY) {
2020 HEK *hek = HeKEY_hek(entry);
2021 int flags = HEK_FLAGS(hek);
2024 if (flags & HVhek_WASUTF8) {
2026 Andreas would like keys he put in as utf8 to come back as utf8
2028 STRLEN utf8_len = HEK_LEN(hek);
2029 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
2031 sv = newSVpvn ((char*)as_utf8, utf8_len);
2033 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
2035 sv = newSVpvn_share(HEK_KEY(hek),
2036 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
2039 return sv_2mortal(sv);
2041 return sv_mortalcopy(HeKEY_sv(entry));
2045 =for apidoc hv_iterval
2047 Returns the value from the current position of the hash iterator. See
2054 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2056 if (SvRMAGICAL(hv)) {
2057 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
2058 SV* sv = sv_newmortal();
2059 if (HeKLEN(entry) == HEf_SVKEY)
2060 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2061 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
2065 return HeVAL(entry);
2069 =for apidoc hv_iternextsv
2071 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2078 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2081 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
2083 *key = hv_iterkey(he, retlen);
2084 return hv_iterval(hv, he);
2088 =for apidoc hv_magic
2090 Adds magic to a hash. See C<sv_magic>.
2096 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
2098 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
2101 #if 0 /* use the macro from hv.h instead */
2104 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
2106 return HEK_KEY(share_hek(sv, len, hash));
2111 /* possibly free a shared string if no one has access to it
2112 * len and hash must both be valid for str.
2115 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2117 unshare_hek_or_pvn (NULL, str, len, hash);
2122 Perl_unshare_hek(pTHX_ HEK *hek)
2124 unshare_hek_or_pvn(hek, NULL, 0, 0);
2127 /* possibly free a shared string if no one has access to it
2128 hek if non-NULL takes priority over the other 3, else str, len and hash
2129 are used. If so, len and hash must both be valid for str.
2132 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2134 register XPVHV* xhv;
2136 register HE **oentry;
2139 bool is_utf8 = FALSE;
2141 const char *save = str;
2144 hash = HEK_HASH(hek);
2145 } else if (len < 0) {
2146 STRLEN tmplen = -len;
2148 /* See the note in hv_fetch(). --jhi */
2149 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2152 k_flags = HVhek_UTF8;
2154 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2157 /* what follows is the moral equivalent of:
2158 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2159 if (--*Svp == Nullsv)
2160 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2162 xhv = (XPVHV*)SvANY(PL_strtab);
2163 /* assert(xhv_array != 0) */
2165 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2166 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2168 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2169 if (HeKEY_hek(entry) != hek)
2175 int flags_masked = k_flags & HVhek_MASK;
2176 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2177 if (HeHASH(entry) != hash) /* strings can't be equal */
2179 if (HeKLEN(entry) != len)
2181 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2183 if (HeKFLAGS(entry) != flags_masked)
2191 if (--HeVAL(entry) == Nullsv) {
2192 *oentry = HeNEXT(entry);
2194 xhv->xhv_fill--; /* HvFILL(hv)-- */
2195 Safefree(HeKEY_hek(entry));
2197 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2201 UNLOCK_STRTAB_MUTEX;
2202 if (!found && ckWARN_d(WARN_INTERNAL))
2203 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2204 "Attempt to free non-existent shared string '%s'%s",
2205 hek ? HEK_KEY(hek) : str,
2206 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2207 if (k_flags & HVhek_FREEKEY)
2211 /* get a (constant) string ptr from the global string table
2212 * string will get added if it is not already there.
2213 * len and hash must both be valid for str.
2216 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2218 bool is_utf8 = FALSE;
2220 const char *save = str;
2223 STRLEN tmplen = -len;
2225 /* See the note in hv_fetch(). --jhi */
2226 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2228 /* If we were able to downgrade here, then than means that we were passed
2229 in a key which only had chars 0-255, but was utf8 encoded. */
2232 /* If we found we were able to downgrade the string to bytes, then
2233 we should flag that it needs upgrading on keys or each. Also flag
2234 that we need share_hek_flags to free the string. */
2236 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2239 return share_hek_flags (str, len, hash, flags);
2243 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2245 register XPVHV* xhv;
2247 register HE **oentry;
2250 int flags_masked = flags & HVhek_MASK;
2252 /* what follows is the moral equivalent of:
2254 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2255 hv_store(PL_strtab, str, len, Nullsv, hash);
2257 xhv = (XPVHV*)SvANY(PL_strtab);
2258 /* assert(xhv_array != 0) */
2260 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2261 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2262 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2263 if (HeHASH(entry) != hash) /* strings can't be equal */
2265 if (HeKLEN(entry) != len)
2267 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2269 if (HeKFLAGS(entry) != flags_masked)
2276 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2277 HeVAL(entry) = Nullsv;
2278 HeNEXT(entry) = *oentry;
2280 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2281 if (i) { /* initial entry? */
2282 xhv->xhv_fill++; /* HvFILL(hv)++ */
2283 if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
2288 ++HeVAL(entry); /* use value slot as REFCNT */
2289 UNLOCK_STRTAB_MUTEX;
2291 if (flags & HVhek_FREEKEY)
2294 return HeKEY_hek(entry);