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
21 #define PERL_HASH_INTERNAL_ACCESS
24 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
34 PL_he_root = HeNEXT(he);
43 HeNEXT(p) = (HE*)PL_he_root;
54 New(54, ptr, 1008/sizeof(XPV), XPV);
55 ptr->xpv_pv = (char*)PL_he_arenaroot;
56 PL_he_arenaroot = ptr;
59 heend = &he[1008 / sizeof(HE) - 1];
62 HeNEXT(he) = (HE*)(he + 1);
70 #define new_HE() (HE*)safemalloc(sizeof(HE))
71 #define del_HE(p) safefree((char*)p)
75 #define new_HE() new_he()
76 #define del_HE(p) del_he(p)
81 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
86 New(54, k, HEK_BASESIZE + len + 2, char);
88 Copy(str, HEK_KEY(hek), len, char);
89 HEK_KEY(hek)[len] = 0;
92 HEK_FLAGS(hek) = (unsigned char)flags;
96 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
100 Perl_free_tied_hv_pool(pTHX)
103 HE *he = PL_hv_fetch_ent_mh;
105 Safefree(HeKEY_hek(he));
110 PL_hv_fetch_ent_mh = Nullhe;
113 #if defined(USE_ITHREADS)
115 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
121 /* look for it in the table first */
122 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
126 /* create anew and remember what it is */
128 ptr_table_store(PL_ptr_table, e, ret);
130 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
131 if (HeKLEN(e) == HEf_SVKEY) {
133 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
134 HeKEY_hek(ret) = (HEK*)k;
135 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
138 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
141 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
143 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
146 #endif /* USE_ITHREADS */
149 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
152 SV *sv = sv_newmortal(), *esv = sv_newmortal();
153 if (!(flags & HVhek_FREEKEY)) {
154 sv_setpvn(sv, key, klen);
157 /* Need to free saved eventually assign to mortal SV */
158 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
159 sv_usepvn(sv, (char *) key, klen);
161 if (flags & HVhek_UTF8) {
164 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
165 Perl_croak(aTHX_ SvPVX(esv), sv);
168 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
171 #define HV_FETCH_ISSTORE 0x01
172 #define HV_FETCH_ISEXISTS 0x02
173 #define HV_FETCH_LVALUE 0x04
174 #define HV_FETCH_JUST_SV 0x08
179 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
180 the length of the key. The C<hash> parameter is the precomputed hash
181 value; if it is zero then Perl will compute it. The return value will be
182 NULL if the operation failed or if the value did not need to be actually
183 stored within the hash (as in the case of tied hashes). Otherwise it can
184 be dereferenced to get the original C<SV*>. Note that the caller is
185 responsible for suitably incrementing the reference count of C<val> before
186 the call, and decrementing it if the function returned NULL. Effectively
187 a successful hv_store takes ownership of one reference to C<val>. This is
188 usually what you want; a newly created SV has a reference count of one, so
189 if all your code does is create SVs then store them in a hash, hv_store
190 will own the only reference to the new SV, and your code doesn't need to do
191 anything further to tidy up. hv_store is not implemented as a call to
192 hv_store_ent, and does not create a temporary SV for the key, so if your
193 key data is not already in SV form then use hv_store in preference to
196 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
197 information on how to use this function on tied hashes.
203 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
216 hek = hv_fetch_common (hv, NULL, key, klen, flags,
217 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, 0);
218 return hek ? &HeVAL(hek) : NULL;
222 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
223 register U32 hash, int flags)
225 HE *hek = hv_fetch_common (hv, NULL, key, klen, flags,
226 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
227 return hek ? &HeVAL(hek) : NULL;
231 =for apidoc hv_store_ent
233 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
234 parameter is the precomputed hash value; if it is zero then Perl will
235 compute it. The return value is the new hash entry so created. It will be
236 NULL if the operation failed or if the value did not need to be actually
237 stored within the hash (as in the case of tied hashes). Otherwise the
238 contents of the return value can be accessed using the C<He?> macros
239 described here. Note that the caller is responsible for suitably
240 incrementing the reference count of C<val> before the call, and
241 decrementing it if the function returned NULL. Effectively a successful
242 hv_store_ent takes ownership of one reference to C<val>. This is
243 usually what you want; a newly created SV has a reference count of one, so
244 if all your code does is create SVs then store them in a hash, hv_store
245 will own the only reference to the new SV, and your code doesn't need to do
246 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
247 unlike C<val> it does not take ownership of it, so maintaining the correct
248 reference count on C<key> is entirely the caller's responsibility. hv_store
249 is not implemented as a call to hv_store_ent, and does not create a temporary
250 SV for the key, so if your key data is not already in SV form then use
251 hv_store in preference to hv_store_ent.
253 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
254 information on how to use this function on tied hashes.
260 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
262 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash);
266 =for apidoc hv_exists
268 Returns a boolean indicating whether the specified hash key exists. The
269 C<klen> is the length of the key.
275 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
287 return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0)
294 Returns the SV which corresponds to the specified key in the hash. The
295 C<klen> is the length of the key. If C<lval> is set then the fetch will be
296 part of a store. Check that the return value is non-null before
297 dereferencing it to an C<SV*>.
299 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
300 information on how to use this function on tied hashes.
306 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
319 hek = hv_fetch_common (hv, NULL, key, klen, flags,
320 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0),
322 return hek ? &HeVAL(hek) : NULL;
326 =for apidoc hv_exists_ent
328 Returns a boolean indicating whether the specified hash key exists. C<hash>
329 can be a valid precomputed hash value, or 0 to ask for it to be
336 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
338 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash)
342 /* returns an HE * structure with the all fields set */
343 /* note that hent_val will be a mortal sv for MAGICAL hashes */
345 =for apidoc hv_fetch_ent
347 Returns the hash entry which corresponds to the specified key in the hash.
348 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
349 if you want the function to compute it. IF C<lval> is set then the fetch
350 will be part of a store. Make sure the return value is non-null before
351 accessing it. The return value when C<tb> is a tied hash is a pointer to a
352 static location, so be sure to make a copy of the structure if you need to
355 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
356 information on how to use this function on tied hashes.
362 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
364 return hv_fetch_common(hv, keysv, NULL, 0, 0,
365 (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash);
369 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
370 int flags, int action, SV *val, register U32 hash)
384 key = SvPV(keysv, klen);
386 is_utf8 = (SvUTF8(keysv) != 0);
388 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
391 xhv = (XPVHV*)SvANY(hv);
393 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS)))
395 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
398 /* XXX should be able to skimp on the HE/HEK here when
399 HV_FETCH_JUST_SV is true. */
402 keysv = newSVpvn(key, klen);
407 keysv = newSVsv(keysv);
409 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
411 /* grab a fake HE/HEK pair from the pool or make a new one */
412 entry = PL_hv_fetch_ent_mh;
414 PL_hv_fetch_ent_mh = HeNEXT(entry);
418 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
419 HeKEY_hek(entry) = (HEK*)k;
421 HeNEXT(entry) = Nullhe;
422 HeSVKEY_set(entry, keysv);
424 sv_upgrade(sv, SVt_PVLV);
426 /* so we can free entry when freeing sv */
427 LvTARG(sv) = (SV*)entry;
429 /* XXX remove at some point? */
430 if (flags & HVhek_FREEKEY)
435 #ifdef ENV_IS_CASELESS
436 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
438 for (i = 0; i < klen; ++i)
439 if (isLOWER(key[i])) {
440 const char *keysave = key;
441 /* Will need to free this, so set FREEKEY flag
442 on call to hv_fetch_common. */
443 key = savepvn(key,klen);
444 key = (const char*)strupr((char*)key);
446 if (flags & HVhek_FREEKEY)
449 /* This isn't strictly the same as the old hv_fetch
450 magic, which made a call to hv_fetch, followed
451 by a call to hv_store if that failed and lvalue
453 Which I believe could have been done by simply
454 passing the lvalue through to the first hv_fetch.
455 So I will do that here. */
456 return hv_fetch_common(hv, Nullsv, key, klen,
463 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
464 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
466 /* I don't understand why hv_exists_ent has svret and sv,
467 whereas hv_exists only had one. */
468 svret = sv_newmortal();
471 if (keysv || is_utf8) {
473 keysv = newSVpvn(key, klen);
476 keysv = newSVsv(keysv);
478 mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
480 mg_copy((SV*)hv, sv, key, klen);
482 if (flags & HVhek_FREEKEY)
484 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
485 /* This cast somewhat evil, but I'm merely using NULL/
486 not NULL to return the boolean exists.
487 And I know hv is not NULL. */
488 return SvTRUE(svret) ? (HE *)hv : NULL;
490 #ifdef ENV_IS_CASELESS
491 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
492 /* XXX This code isn't UTF8 clean. */
493 const char *keysave = key;
494 /* Will need to free this, so set FREEKEY flag. */
495 key = savepvn(key,klen);
496 key = (const char*)strupr((char*)key);
500 if (flags & HVhek_FREEKEY) {
503 flags |= HVhek_FREEKEY;
507 else if (action & HV_FETCH_ISSTORE) {
510 hv_magic_check (hv, &needs_copy, &needs_store);
512 bool save_taint = PL_tainted;
513 if (keysv || is_utf8) {
515 keysv = newSVpvn(key, klen);
519 PL_tainted = SvTAINTED(keysv);
520 keysv = sv_2mortal(newSVsv(keysv));
521 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
523 mg_copy((SV*)hv, val, key, klen);
526 TAINT_IF(save_taint);
527 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
528 if (flags & HVhek_FREEKEY)
532 #ifdef ENV_IS_CASELESS
533 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
534 /* XXX This code isn't UTF8 clean. */
535 const char *keysave = key;
536 /* Will need to free this, so set FREEKEY flag. */
537 key = savepvn(key,klen);
538 key = (const char*)strupr((char*)key);
542 if (flags & HVhek_FREEKEY) {
545 flags |= HVhek_FREEKEY;
552 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
553 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
554 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
555 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
558 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
559 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
561 #ifdef DYNAMIC_ENV_FETCH
562 else if (action & HV_FETCH_ISEXISTS) {
563 /* for an %ENV exists, if we do an insert it's by a recursive
564 store call, so avoid creating HvARRAY(hv) right now. */
568 /* XXX remove at some point? */
569 if (flags & HVhek_FREEKEY)
577 const char *keysave = key;
578 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
582 flags &= ~HVhek_UTF8;
583 if (key != keysave) {
584 if (flags & HVhek_FREEKEY)
586 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
591 PERL_HASH_INTERNAL(hash, key, klen);
592 /* We don't have a pointer to the hv, so we have to replicate the
593 flag into every HEK, so that hv_iterkeysv can see it. */
594 /* And yes, you do need this even though you are not "storing" because
595 you can flip the flags below if doing an lval lookup. (And that
596 was put in to give the semantics Andreas was expecting.) */
597 flags |= HVhek_REHASH;
599 if (keysv && (SvIsCOW_shared_hash(keysv))) {
602 PERL_HASH(hash, key, klen);
606 masked_flags = (flags & HVhek_MASK);
609 #ifdef DYNAMIC_ENV_FETCH
610 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
614 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
615 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
617 for (; entry; ++n_links, entry = HeNEXT(entry)) {
618 if (HeHASH(entry) != hash) /* strings can't be equal */
620 if (HeKLEN(entry) != (I32)klen)
622 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
624 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
627 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
628 if (HeKFLAGS(entry) != masked_flags) {
629 /* We match if HVhek_UTF8 bit in our flags and hash key's
630 match. But if entry was set previously with HVhek_WASUTF8
631 and key now doesn't (or vice versa) then we should change
632 the key's flag, as this is assignment. */
633 if (HvSHAREKEYS(hv)) {
634 /* Need to swap the key we have for a key with the flags we
635 need. As keys are shared we can't just write to the
636 flag, so we share the new one, unshare the old one. */
637 HEK *new_hek = share_hek_flags(key, klen, hash,
639 unshare_hek (HeKEY_hek(entry));
640 HeKEY_hek(entry) = new_hek;
643 HeKFLAGS(entry) = masked_flags;
644 if (masked_flags & HVhek_ENABLEHVKFLAGS)
647 if (HeVAL(entry) == &PL_sv_placeholder) {
648 /* yes, can store into placeholder slot */
649 if (action & HV_FETCH_LVALUE) {
651 /* This preserves behaviour with the old hv_fetch
652 implementation which at this point would bail out
653 with a break; (at "if we find a placeholder, we
654 pretend we haven't found anything")
656 That break mean that if a placeholder were found, it
657 caused a call into hv_store, which in turn would
658 check magic, and if there is no magic end up pretty
659 much back at this point (in hv_store's code). */
662 /* LVAL fetch which actaully needs a store. */
664 xhv->xhv_placeholders--;
667 if (val != &PL_sv_placeholder)
668 xhv->xhv_placeholders--;
671 } else if (action & HV_FETCH_ISSTORE) {
672 SvREFCNT_dec(HeVAL(entry));
675 } else if (HeVAL(entry) == &PL_sv_placeholder) {
676 /* if we find a placeholder, we pretend we haven't found
680 if (flags & HVhek_FREEKEY)
684 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
685 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
687 char *env = PerlEnv_ENVgetenv_len(key,&len);
689 sv = newSVpvn(env,len);
691 return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv,
697 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
698 S_hv_notallowed(aTHX_ flags, key, klen,
699 "access disallowed key '%"SVf"' in"
702 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
703 /* Not doing some form of store, so return failure. */
704 if (flags & HVhek_FREEKEY)
708 if (action & HV_FETCH_LVALUE) {
711 /* At this point the old hv_fetch code would call to hv_store,
712 which in turn might do some tied magic. So we need to make that
713 magic check happen. */
714 /* gonna assign to this, so it better be there */
715 return hv_fetch_common(hv, keysv, key, klen, flags,
716 HV_FETCH_ISSTORE, val, hash);
717 /* XXX Surely that could leak if the fetch-was-store fails?
718 Just like the hv_fetch. */
722 /* Welcome to hv_store... */
724 if (!xhv->xhv_array) {
725 /* Not sure if we can get here. I think the only case of oentry being
726 NULL is for %ENV with dynamic env fetch. But that should disappear
727 with magic in the previous code. */
728 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
729 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
733 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
736 /* share_hek_flags will do the free for us. This might be considered
739 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
740 else /* gotta do the real thing */
741 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
743 HeNEXT(entry) = *oentry;
746 if (val == &PL_sv_placeholder)
747 xhv->xhv_placeholders++;
748 if (masked_flags & HVhek_ENABLEHVKFLAGS)
751 xhv->xhv_keys++; /* HvKEYS(hv)++ */
752 if (!n_links) { /* initial entry? */
753 xhv->xhv_fill++; /* HvFILL(hv)++ */
754 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
755 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
756 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
757 splits on a rehashed hash, as we're not going to split it again,
758 and if someone is lucky (evil) enough to get all the keys in one
759 list they could exhaust our memory as we repeatedly double the
760 number of buckets on every entry. Linear search feels a less worse
769 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
771 MAGIC *mg = SvMAGIC(hv);
775 if (isUPPER(mg->mg_type)) {
777 switch (mg->mg_type) {
778 case PERL_MAGIC_tied:
780 *needs_store = FALSE;
783 mg = mg->mg_moremagic;
788 =for apidoc hv_delete
790 Deletes a key/value pair in the hash. The value SV is removed from the
791 hash and returned to the caller. The C<klen> is the length of the key.
792 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
799 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
806 k_flags |= HVhek_UTF8;
810 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
814 =for apidoc hv_delete_ent
816 Deletes a key/value pair in the hash. The value SV is removed from the
817 hash and returned to the caller. The C<flags> value will normally be zero;
818 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
819 precomputed hash value, or 0 to ask for it to be computed.
825 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
827 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
831 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
832 int k_flags, I32 d_flags, U32 hash)
837 register HE **oentry;
846 key = SvPV(keysv, klen);
848 is_utf8 = (SvUTF8(keysv) != 0);
850 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
853 if (SvRMAGICAL(hv)) {
856 hv_magic_check (hv, &needs_copy, &needs_store);
859 entry = hv_fetch_common(hv, keysv, key, klen,
860 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
862 sv = entry ? HeVAL(entry) : NULL;
868 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
869 /* No longer an element */
870 sv_unmagic(sv, PERL_MAGIC_tiedelem);
873 return Nullsv; /* element cannot be deleted */
876 #ifdef ENV_IS_CASELESS
877 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
878 /* XXX This code isn't UTF8 clean. */
879 keysv = sv_2mortal(newSVpvn(key,klen));
880 key = strupr(SvPVX(keysv));
882 if (k_flags & HVhek_FREEKEY) {
893 xhv = (XPVHV*)SvANY(hv);
894 if (!xhv->xhv_array /* !HvARRAY(hv) */)
898 const char *keysave = key;
899 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
902 k_flags |= HVhek_UTF8;
904 k_flags &= ~HVhek_UTF8;
905 if (key != keysave) {
906 if (k_flags & HVhek_FREEKEY) {
907 /* This shouldn't happen if our caller does what we expect,
908 but strictly the API allows it. */
911 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
913 HvHASKFLAGS_on((SV*)hv);
917 PERL_HASH_INTERNAL(hash, key, klen);
919 if (keysv && (SvIsCOW_shared_hash(keysv))) {
922 PERL_HASH(hash, key, klen);
924 PERL_HASH(hash, key, klen);
927 masked_flags = (k_flags & HVhek_MASK);
929 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
930 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
933 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
934 if (HeHASH(entry) != hash) /* strings can't be equal */
936 if (HeKLEN(entry) != (I32)klen)
938 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
940 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
942 if (k_flags & HVhek_FREEKEY)
945 /* if placeholder is here, it's already been deleted.... */
946 if (HeVAL(entry) == &PL_sv_placeholder)
949 return Nullsv; /* if still SvREADONLY, leave it deleted. */
951 /* okay, really delete the placeholder. */
952 *oentry = HeNEXT(entry);
954 xhv->xhv_fill--; /* HvFILL(hv)-- */
955 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
958 hv_free_ent(hv, entry);
959 xhv->xhv_keys--; /* HvKEYS(hv)-- */
960 if (xhv->xhv_keys == 0)
962 xhv->xhv_placeholders--;
965 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
966 S_hv_notallowed(aTHX_ k_flags, key, klen,
967 "delete readonly key '%"SVf"' from"
971 if (d_flags & G_DISCARD)
974 sv = sv_2mortal(HeVAL(entry));
975 HeVAL(entry) = &PL_sv_placeholder;
979 * If a restricted hash, rather than really deleting the entry, put
980 * a placeholder there. This marks the key as being "approved", so
981 * we can still access via not-really-existing key without raising
984 if (SvREADONLY(hv)) {
985 HeVAL(entry) = &PL_sv_placeholder;
986 /* We'll be saving this slot, so the number of allocated keys
987 * doesn't go down, but the number placeholders goes up */
988 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
990 *oentry = HeNEXT(entry);
992 xhv->xhv_fill--; /* HvFILL(hv)-- */
993 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
996 hv_free_ent(hv, entry);
997 xhv->xhv_keys--; /* HvKEYS(hv)-- */
998 if (xhv->xhv_keys == 0)
1003 if (SvREADONLY(hv)) {
1004 S_hv_notallowed(aTHX_ k_flags, key, klen,
1005 "delete disallowed key '%"SVf"' from"
1009 if (k_flags & HVhek_FREEKEY)
1015 S_hsplit(pTHX_ HV *hv)
1017 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1018 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1019 register I32 newsize = oldsize * 2;
1021 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1025 register HE **oentry;
1026 int longest_chain = 0;
1030 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1031 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1037 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1042 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1043 if (oldsize >= 64) {
1044 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1045 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1048 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1052 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1053 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1054 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1057 for (i=0; i<oldsize; i++,aep++) {
1058 int left_length = 0;
1059 int right_length = 0;
1061 if (!*aep) /* non-existent */
1064 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1065 if ((HeHASH(entry) & newsize) != (U32)i) {
1066 *oentry = HeNEXT(entry);
1067 HeNEXT(entry) = *bep;
1069 xhv->xhv_fill++; /* HvFILL(hv)++ */
1075 oentry = &HeNEXT(entry);
1079 if (!*aep) /* everything moved */
1080 xhv->xhv_fill--; /* HvFILL(hv)-- */
1081 /* I think we don't actually need to keep track of the longest length,
1082 merely flag if anything is too long. But for the moment while
1083 developing this code I'll track it. */
1084 if (left_length > longest_chain)
1085 longest_chain = left_length;
1086 if (right_length > longest_chain)
1087 longest_chain = right_length;
1091 /* Pick your policy for "hashing isn't working" here: */
1092 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1097 if (hv == PL_strtab) {
1098 /* Urg. Someone is doing something nasty to the string table.
1103 /* Awooga. Awooga. Pathological data. */
1104 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1105 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1108 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1109 was_shared = HvSHAREKEYS(hv);
1112 HvSHAREKEYS_off(hv);
1115 aep = (HE **) xhv->xhv_array;
1117 for (i=0; i<newsize; i++,aep++) {
1120 /* We're going to trash this HE's next pointer when we chain it
1121 into the new hash below, so store where we go next. */
1122 HE *next = HeNEXT(entry);
1126 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1131 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1132 hash, HeKFLAGS(entry));
1133 unshare_hek (HeKEY_hek(entry));
1134 HeKEY_hek(entry) = new_hek;
1136 /* Not shared, so simply write the new hash in. */
1137 HeHASH(entry) = hash;
1139 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1140 HEK_REHASH_on(HeKEY_hek(entry));
1141 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1143 /* Copy oentry to the correct new chain. */
1144 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1146 xhv->xhv_fill++; /* HvFILL(hv)++ */
1147 HeNEXT(entry) = *bep;
1153 Safefree (xhv->xhv_array);
1154 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1158 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1160 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1161 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1162 register I32 newsize;
1168 register HE **oentry;
1170 newsize = (I32) newmax; /* possible truncation here */
1171 if (newsize != newmax || newmax <= oldsize)
1173 while ((newsize & (1 + ~newsize)) != newsize) {
1174 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1176 if (newsize < newmax)
1178 if (newsize < newmax)
1179 return; /* overflow detection */
1181 a = xhv->xhv_array; /* HvARRAY(hv) */
1184 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1185 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1191 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1196 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1197 if (oldsize >= 64) {
1198 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1199 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1202 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1205 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1208 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1210 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1211 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1212 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1216 for (i=0; i<oldsize; i++,aep++) {
1217 if (!*aep) /* non-existent */
1219 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1220 if ((j = (HeHASH(entry) & newsize)) != i) {
1222 *oentry = HeNEXT(entry);
1223 if (!(HeNEXT(entry) = aep[j]))
1224 xhv->xhv_fill++; /* HvFILL(hv)++ */
1229 oentry = &HeNEXT(entry);
1231 if (!*aep) /* everything moved */
1232 xhv->xhv_fill--; /* HvFILL(hv)-- */
1239 Creates a new HV. The reference count is set to 1.
1248 register XPVHV* xhv;
1250 hv = (HV*)NEWSV(502,0);
1251 sv_upgrade((SV *)hv, SVt_PVHV);
1252 xhv = (XPVHV*)SvANY(hv);
1255 #ifndef NODEFAULT_SHAREKEYS
1256 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1259 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1260 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1261 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1262 (void)hv_iterinit(hv); /* so each() will start off right */
1267 Perl_newHVhv(pTHX_ HV *ohv)
1270 STRLEN hv_max, hv_fill;
1272 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1274 hv_max = HvMAX(ohv);
1276 if (!SvMAGICAL((SV *)ohv)) {
1277 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1279 bool shared = !!HvSHAREKEYS(ohv);
1280 HE **ents, **oents = (HE **)HvARRAY(ohv);
1282 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1285 /* In each bucket... */
1286 for (i = 0; i <= hv_max; i++) {
1287 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1294 /* Copy the linked list of entries. */
1295 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1296 U32 hash = HeHASH(oent);
1297 char *key = HeKEY(oent);
1298 STRLEN len = HeKLEN(oent);
1299 int flags = HeKFLAGS(oent);
1302 HeVAL(ent) = newSVsv(HeVAL(oent));
1304 = shared ? share_hek_flags(key, len, hash, flags)
1305 : save_hek_flags(key, len, hash, flags);
1316 HvFILL(hv) = hv_fill;
1317 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1321 /* Iterate over ohv, copying keys and values one at a time. */
1323 I32 riter = HvRITER(ohv);
1324 HE *eiter = HvEITER(ohv);
1326 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1327 while (hv_max && hv_max + 1 >= hv_fill * 2)
1328 hv_max = hv_max / 2;
1332 while ((entry = hv_iternext_flags(ohv, 0))) {
1333 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1334 newSVsv(HeVAL(entry)), HeHASH(entry),
1337 HvRITER(ohv) = riter;
1338 HvEITER(ohv) = eiter;
1345 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1352 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1353 PL_sub_generation++; /* may be deletion of method from stash */
1355 if (HeKLEN(entry) == HEf_SVKEY) {
1356 SvREFCNT_dec(HeKEY_sv(entry));
1357 Safefree(HeKEY_hek(entry));
1359 else if (HvSHAREKEYS(hv))
1360 unshare_hek(HeKEY_hek(entry));
1362 Safefree(HeKEY_hek(entry));
1367 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1371 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1372 PL_sub_generation++; /* may be deletion of method from stash */
1373 sv_2mortal(HeVAL(entry)); /* free between statements */
1374 if (HeKLEN(entry) == HEf_SVKEY) {
1375 sv_2mortal(HeKEY_sv(entry));
1376 Safefree(HeKEY_hek(entry));
1378 else if (HvSHAREKEYS(hv))
1379 unshare_hek(HeKEY_hek(entry));
1381 Safefree(HeKEY_hek(entry));
1386 =for apidoc hv_clear
1388 Clears a hash, making it empty.
1394 Perl_hv_clear(pTHX_ HV *hv)
1396 register XPVHV* xhv;
1400 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1402 xhv = (XPVHV*)SvANY(hv);
1404 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1405 /* restricted hash: convert all keys to placeholders */
1408 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1409 entry = ((HE**)xhv->xhv_array)[i];
1410 for (; entry; entry = HeNEXT(entry)) {
1411 /* not already placeholder */
1412 if (HeVAL(entry) != &PL_sv_placeholder) {
1413 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1414 SV* keysv = hv_iterkeysv(entry);
1416 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1419 SvREFCNT_dec(HeVAL(entry));
1420 HeVAL(entry) = &PL_sv_placeholder;
1421 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1429 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1430 if (xhv->xhv_array /* HvARRAY(hv) */)
1431 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1432 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1437 HvHASKFLAGS_off(hv);
1442 =for apidoc hv_clear_placeholders
1444 Clears any placeholders from a hash. If a restricted hash has any of its keys
1445 marked as readonly and the key is subsequently deleted, the key is not actually
1446 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1447 it so it will be ignored by future operations such as iterating over the hash,
1448 but will still allow the hash to have a value reaasigned to the key at some
1449 future point. This function clears any such placeholder keys from the hash.
1450 See Hash::Util::lock_keys() for an example of its use.
1456 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1459 items = (I32)HvPLACEHOLDERS(hv);
1462 I32 riter = HvRITER(hv);
1463 HE *eiter = HvEITER(hv);
1465 /* This may look suboptimal with the items *after* the iternext, but
1466 it's quite deliberate. We only get here with items==0 if we've
1467 just deleted the last placeholder in the hash. If we've just done
1468 that then it means that the hash is in lazy delete mode, and the
1469 HE is now only referenced in our iterator. If we just quit the loop
1470 and discarded our iterator then the HE leaks. So we do the && the
1471 other way to ensure iternext is called just one more time, which
1472 has the side effect of triggering the lazy delete. */
1473 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
1475 SV *val = hv_iterval(hv, entry);
1477 if (val == &PL_sv_placeholder) {
1479 /* It seems that I have to go back in the front of the hash
1480 API to delete a hash, even though I have a HE structure
1481 pointing to the very entry I want to delete, and could hold
1482 onto the previous HE that points to it. And it's easier to
1483 go in with SVs as I can then specify the precomputed hash,
1484 and don't have fun and games with utf8 keys. */
1485 SV *key = hv_iterkeysv(entry);
1487 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
1491 HvRITER(hv) = riter;
1492 HvEITER(hv) = eiter;
1497 S_hfreeentries(pTHX_ HV *hv)
1499 register HE **array;
1501 register HE *oentry = Null(HE*);
1512 array = HvARRAY(hv);
1513 /* make everyone else think the array is empty, so that the destructors
1514 * called for freed entries can't recusively mess with us */
1515 HvARRAY(hv) = Null(HE**);
1517 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1523 entry = HeNEXT(entry);
1524 hv_free_ent(hv, oentry);
1529 entry = array[riter];
1532 HvARRAY(hv) = array;
1533 (void)hv_iterinit(hv);
1537 =for apidoc hv_undef
1545 Perl_hv_undef(pTHX_ HV *hv)
1547 register XPVHV* xhv;
1550 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1551 xhv = (XPVHV*)SvANY(hv);
1553 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1556 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1557 Safefree(HvNAME(hv));
1560 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1561 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1562 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1569 =for apidoc hv_iterinit
1571 Prepares a starting point to traverse a hash table. Returns the number of
1572 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1573 currently only meaningful for hashes without tie magic.
1575 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1576 hash buckets that happen to be in use. If you still need that esoteric
1577 value, you can get it through the macro C<HvFILL(tb)>.
1584 Perl_hv_iterinit(pTHX_ HV *hv)
1586 register XPVHV* xhv;
1590 Perl_croak(aTHX_ "Bad hash");
1591 xhv = (XPVHV*)SvANY(hv);
1592 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1593 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1595 hv_free_ent(hv, entry);
1597 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1598 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1599 /* used to be xhv->xhv_fill before 5.004_65 */
1600 return XHvTOTALKEYS(xhv);
1603 =for apidoc hv_iternext
1605 Returns entries from a hash iterator. See C<hv_iterinit>.
1607 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1608 iterator currently points to, without losing your place or invalidating your
1609 iterator. Note that in this case the current entry is deleted from the hash
1610 with your iterator holding the last reference to it. Your iterator is flagged
1611 to free the entry on the next call to C<hv_iternext>, so you must not discard
1612 your iterator immediately else the entry will leak - call C<hv_iternext> to
1613 trigger the resource deallocation.
1619 Perl_hv_iternext(pTHX_ HV *hv)
1621 return hv_iternext_flags(hv, 0);
1625 =for apidoc hv_iternext_flags
1627 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1628 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1629 set the placeholders keys (for restricted hashes) will be returned in addition
1630 to normal keys. By default placeholders are automatically skipped over.
1631 Currently a placeholder is implemented with a value that is
1632 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1633 restricted hashes may change, and the implementation currently is
1634 insufficiently abstracted for any change to be tidy.
1640 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1642 register XPVHV* xhv;
1648 Perl_croak(aTHX_ "Bad hash");
1649 xhv = (XPVHV*)SvANY(hv);
1650 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1652 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1653 SV *key = sv_newmortal();
1655 sv_setsv(key, HeSVKEY_force(entry));
1656 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1662 /* one HE per MAGICAL hash */
1663 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1665 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1667 HeKEY_hek(entry) = hek;
1668 HeKLEN(entry) = HEf_SVKEY;
1670 magic_nextpack((SV*) hv,mg,key);
1672 /* force key to stay around until next time */
1673 HeSVKEY_set(entry, SvREFCNT_inc(key));
1674 return entry; /* beware, hent_val is not set */
1677 SvREFCNT_dec(HeVAL(entry));
1678 Safefree(HeKEY_hek(entry));
1680 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1683 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1684 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1688 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1689 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1690 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1692 /* At start of hash, entry is NULL. */
1695 entry = HeNEXT(entry);
1696 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1698 * Skip past any placeholders -- don't want to include them in
1701 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1702 entry = HeNEXT(entry);
1707 /* OK. Come to the end of the current list. Grab the next one. */
1709 xhv->xhv_riter++; /* HvRITER(hv)++ */
1710 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1711 /* There is no next one. End of the hash. */
1712 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1715 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1716 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1718 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1719 /* If we have an entry, but it's a placeholder, don't count it.
1721 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1722 entry = HeNEXT(entry);
1724 /* Will loop again if this linked list starts NULL
1725 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1726 or if we run through it and find only placeholders. */
1729 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1731 hv_free_ent(hv, oldentry);
1734 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1735 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1737 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1742 =for apidoc hv_iterkey
1744 Returns the key from the current position of the hash iterator. See
1751 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1753 if (HeKLEN(entry) == HEf_SVKEY) {
1755 char *p = SvPV(HeKEY_sv(entry), len);
1760 *retlen = HeKLEN(entry);
1761 return HeKEY(entry);
1765 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1767 =for apidoc hv_iterkeysv
1769 Returns the key as an C<SV*> from the current position of the hash
1770 iterator. The return value will always be a mortal copy of the key. Also
1777 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1779 if (HeKLEN(entry) != HEf_SVKEY) {
1780 HEK *hek = HeKEY_hek(entry);
1781 int flags = HEK_FLAGS(hek);
1784 if (flags & HVhek_WASUTF8) {
1786 Andreas would like keys he put in as utf8 to come back as utf8
1788 STRLEN utf8_len = HEK_LEN(hek);
1789 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1791 sv = newSVpvn ((char*)as_utf8, utf8_len);
1793 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1794 } else if (flags & HVhek_REHASH) {
1795 /* We don't have a pointer to the hv, so we have to replicate the
1796 flag into every HEK. This hv is using custom a hasing
1797 algorithm. Hence we can't return a shared string scalar, as
1798 that would contain the (wrong) hash value, and might get passed
1799 into an hv routine with a regular hash */
1801 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1805 sv = newSVpvn_share(HEK_KEY(hek),
1806 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1809 return sv_2mortal(sv);
1811 return sv_mortalcopy(HeKEY_sv(entry));
1815 =for apidoc hv_iterval
1817 Returns the value from the current position of the hash iterator. See
1824 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1826 if (SvRMAGICAL(hv)) {
1827 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1828 SV* sv = sv_newmortal();
1829 if (HeKLEN(entry) == HEf_SVKEY)
1830 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1831 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1835 return HeVAL(entry);
1839 =for apidoc hv_iternextsv
1841 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1848 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1851 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1853 *key = hv_iterkey(he, retlen);
1854 return hv_iterval(hv, he);
1858 =for apidoc hv_magic
1860 Adds magic to a hash. See C<sv_magic>.
1866 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1868 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1871 #if 0 /* use the macro from hv.h instead */
1874 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1876 return HEK_KEY(share_hek(sv, len, hash));
1881 /* possibly free a shared string if no one has access to it
1882 * len and hash must both be valid for str.
1885 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1887 unshare_hek_or_pvn (NULL, str, len, hash);
1892 Perl_unshare_hek(pTHX_ HEK *hek)
1894 unshare_hek_or_pvn(hek, NULL, 0, 0);
1897 /* possibly free a shared string if no one has access to it
1898 hek if non-NULL takes priority over the other 3, else str, len and hash
1899 are used. If so, len and hash must both be valid for str.
1902 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1904 register XPVHV* xhv;
1906 register HE **oentry;
1909 bool is_utf8 = FALSE;
1911 const char *save = str;
1914 hash = HEK_HASH(hek);
1915 } else if (len < 0) {
1916 STRLEN tmplen = -len;
1918 /* See the note in hv_fetch(). --jhi */
1919 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1922 k_flags = HVhek_UTF8;
1924 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1927 /* what follows is the moral equivalent of:
1928 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1929 if (--*Svp == Nullsv)
1930 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1932 xhv = (XPVHV*)SvANY(PL_strtab);
1933 /* assert(xhv_array != 0) */
1935 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1936 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1938 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1939 if (HeKEY_hek(entry) != hek)
1945 int flags_masked = k_flags & HVhek_MASK;
1946 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1947 if (HeHASH(entry) != hash) /* strings can't be equal */
1949 if (HeKLEN(entry) != len)
1951 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1953 if (HeKFLAGS(entry) != flags_masked)
1961 if (--HeVAL(entry) == Nullsv) {
1962 *oentry = HeNEXT(entry);
1964 xhv->xhv_fill--; /* HvFILL(hv)-- */
1965 Safefree(HeKEY_hek(entry));
1967 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1971 UNLOCK_STRTAB_MUTEX;
1972 if (!found && ckWARN_d(WARN_INTERNAL))
1973 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
1974 "Attempt to free non-existent shared string '%s'%s",
1975 hek ? HEK_KEY(hek) : str,
1976 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
1977 if (k_flags & HVhek_FREEKEY)
1981 /* get a (constant) string ptr from the global string table
1982 * string will get added if it is not already there.
1983 * len and hash must both be valid for str.
1986 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1988 bool is_utf8 = FALSE;
1990 const char *save = str;
1993 STRLEN tmplen = -len;
1995 /* See the note in hv_fetch(). --jhi */
1996 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1998 /* If we were able to downgrade here, then than means that we were passed
1999 in a key which only had chars 0-255, but was utf8 encoded. */
2002 /* If we found we were able to downgrade the string to bytes, then
2003 we should flag that it needs upgrading on keys or each. Also flag
2004 that we need share_hek_flags to free the string. */
2006 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2009 return share_hek_flags (str, len, hash, flags);
2013 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2015 register XPVHV* xhv;
2017 register HE **oentry;
2020 int flags_masked = flags & HVhek_MASK;
2022 /* what follows is the moral equivalent of:
2024 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2025 hv_store(PL_strtab, str, len, Nullsv, hash);
2027 Can't rehash the shared string table, so not sure if it's worth
2028 counting the number of entries in the linked list
2030 xhv = (XPVHV*)SvANY(PL_strtab);
2031 /* assert(xhv_array != 0) */
2033 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2034 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2035 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2036 if (HeHASH(entry) != hash) /* strings can't be equal */
2038 if (HeKLEN(entry) != len)
2040 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2042 if (HeKFLAGS(entry) != flags_masked)
2049 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
2050 HeVAL(entry) = Nullsv;
2051 HeNEXT(entry) = *oentry;
2053 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2054 if (i) { /* initial entry? */
2055 xhv->xhv_fill++; /* HvFILL(hv)++ */
2056 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2061 ++HeVAL(entry); /* use value slot as REFCNT */
2062 UNLOCK_STRTAB_MUTEX;
2064 if (flags & HVhek_FREEKEY)
2067 return HeKEY_hek(entry);
2072 =for apidoc hv_assert
2074 Check that a hash is in an internally consistent state.
2080 Perl_hv_assert(pTHX_ HV *hv)
2084 int placeholders = 0;
2087 I32 riter = HvRITER(hv);
2088 HE *eiter = HvEITER(hv);
2090 (void)hv_iterinit(hv);
2092 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2093 /* sanity check the values */
2094 if (HeVAL(entry) == &PL_sv_placeholder) {
2099 /* sanity check the keys */
2100 if (HeSVKEY(entry)) {
2101 /* Don't know what to check on SV keys. */
2102 } else if (HeKUTF8(entry)) {
2104 if (HeKWASUTF8(entry)) {
2105 PerlIO_printf(Perl_debug_log,
2106 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2107 (int) HeKLEN(entry), HeKEY(entry));
2110 } else if (HeKWASUTF8(entry)) {
2114 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2115 if (HvUSEDKEYS(hv) != real) {
2116 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2117 (int) real, (int) HvUSEDKEYS(hv));
2120 if (HvPLACEHOLDERS(hv) != placeholders) {
2121 PerlIO_printf(Perl_debug_log,
2122 "Count %d placeholder(s), but hash reports %d\n",
2123 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2127 if (withflags && ! HvHASKFLAGS(hv)) {
2128 PerlIO_printf(Perl_debug_log,
2129 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2136 HvRITER(hv) = riter; /* Restore hash iterator state */
2137 HvEITER(hv) = eiter;