3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 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
18 A HV structure represents a Perl hash. It consists mainly of an array
19 of pointers, each of which points to a linked list of HE structures. The
20 array is indexed by the hash function of the key, so each linked list
21 represents all the hash entries with the same hash value. Each HE contains
22 a pointer to the actual value, plus a pointer to a HEK structure which
23 holds the key and hash value.
31 #define PERL_HASH_INTERNAL_ACCESS
34 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
44 PL_he_root = HeNEXT(he);
53 HeNEXT(p) = (HE*)PL_he_root;
64 New(54, ptr, 1008/sizeof(XPV), XPV);
65 ptr->xpv_pv = (char*)PL_he_arenaroot;
66 PL_he_arenaroot = ptr;
69 heend = &he[1008 / sizeof(HE) - 1];
72 HeNEXT(he) = (HE*)(he + 1);
80 #define new_HE() (HE*)safemalloc(sizeof(HE))
81 #define del_HE(p) safefree((char*)p)
85 #define new_HE() new_he()
86 #define del_HE(p) del_he(p)
91 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
93 int flags_masked = flags & HVhek_MASK;
97 New(54, k, HEK_BASESIZE + len + 2, char);
99 Copy(str, HEK_KEY(hek), len, char);
100 HEK_KEY(hek)[len] = 0;
102 HEK_HASH(hek) = hash;
103 HEK_FLAGS(hek) = (unsigned char)flags_masked;
105 if (flags & HVhek_FREEKEY)
110 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
114 Perl_free_tied_hv_pool(pTHX)
117 HE *he = PL_hv_fetch_ent_mh;
119 Safefree(HeKEY_hek(he));
124 PL_hv_fetch_ent_mh = Nullhe;
127 #if defined(USE_ITHREADS)
129 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
135 /* look for it in the table first */
136 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
140 /* create anew and remember what it is */
142 ptr_table_store(PL_ptr_table, e, ret);
144 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
145 if (HeKLEN(e) == HEf_SVKEY) {
147 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
148 HeKEY_hek(ret) = (HEK*)k;
149 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
152 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
155 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
157 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
160 #endif /* USE_ITHREADS */
163 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
166 SV *sv = sv_newmortal(), *esv = sv_newmortal();
167 if (!(flags & HVhek_FREEKEY)) {
168 sv_setpvn(sv, key, klen);
171 /* Need to free saved eventually assign to mortal SV */
172 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
173 sv_usepvn(sv, (char *) key, klen);
175 if (flags & HVhek_UTF8) {
178 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
179 Perl_croak(aTHX_ SvPVX(esv), sv);
182 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
185 #define HV_FETCH_ISSTORE 0x01
186 #define HV_FETCH_ISEXISTS 0x02
187 #define HV_FETCH_LVALUE 0x04
188 #define HV_FETCH_JUST_SV 0x08
189 #define HV_FETCH_PLACEHOLDER 0x10
194 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
195 the length of the key. The C<hash> parameter is the precomputed hash
196 value; if it is zero then Perl will compute it. The return value will be
197 NULL if the operation failed or if the value did not need to be actually
198 stored within the hash (as in the case of tied hashes). Otherwise it can
199 be dereferenced to get the original C<SV*>. Note that the caller is
200 responsible for suitably incrementing the reference count of C<val> before
201 the call, and decrementing it if the function returned NULL. Effectively
202 a successful hv_store takes ownership of one reference to C<val>. This is
203 usually what you want; a newly created SV has a reference count of one, so
204 if all your code does is create SVs then store them in a hash, hv_store
205 will own the only reference to the new SV, and your code doesn't need to do
206 anything further to tidy up. hv_store is not implemented as a call to
207 hv_store_ent, and does not create a temporary SV for the key, so if your
208 key data is not already in SV form then use hv_store in preference to
211 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
212 information on how to use this function on tied hashes.
218 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
231 hek = hv_fetch_common (hv, NULL, key, klen, flags,
232 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
233 return hek ? &HeVAL(hek) : NULL;
237 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
238 register U32 hash, int flags)
240 HE *hek = hv_fetch_common (hv, NULL, key, klen, flags,
241 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
242 return hek ? &HeVAL(hek) : NULL;
246 =for apidoc hv_store_ent
248 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
249 parameter is the precomputed hash value; if it is zero then Perl will
250 compute it. The return value is the new hash entry so created. It will be
251 NULL if the operation failed or if the value did not need to be actually
252 stored within the hash (as in the case of tied hashes). Otherwise the
253 contents of the return value can be accessed using the C<He?> macros
254 described here. Note that the caller is responsible for suitably
255 incrementing the reference count of C<val> before the call, and
256 decrementing it if the function returned NULL. Effectively a successful
257 hv_store_ent takes ownership of one reference to C<val>. This is
258 usually what you want; a newly created SV has a reference count of one, so
259 if all your code does is create SVs then store them in a hash, hv_store
260 will own the only reference to the new SV, and your code doesn't need to do
261 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
262 unlike C<val> it does not take ownership of it, so maintaining the correct
263 reference count on C<key> is entirely the caller's responsibility. hv_store
264 is not implemented as a call to hv_store_ent, and does not create a temporary
265 SV for the key, so if your key data is not already in SV form then use
266 hv_store in preference to hv_store_ent.
268 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
269 information on how to use this function on tied hashes.
275 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
277 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash);
281 =for apidoc hv_exists
283 Returns a boolean indicating whether the specified hash key exists. The
284 C<klen> is the length of the key.
290 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
302 return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0)
309 Returns the SV which corresponds to the specified key in the hash. The
310 C<klen> is the length of the key. If C<lval> is set then the fetch will be
311 part of a store. Check that the return value is non-null before
312 dereferencing it to an C<SV*>.
314 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
315 information on how to use this function on tied hashes.
321 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
334 hek = hv_fetch_common (hv, NULL, key, klen, flags,
335 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0),
337 return hek ? &HeVAL(hek) : NULL;
341 =for apidoc hv_fetch_flags
343 Returns the SV which corresponds to the specified key in the hash.
345 The C<flags> value will normally be zero; if HV_FETCH_WANTPLACEHOLDERS is
346 set then placeholders keys (for restricted hashes) will be returned in addition
347 to normal keys. By default placeholders are automatically skipped over.
348 Currently a placeholder is implemented with a value that is
349 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
350 restricted hashes may change.
356 Perl_hv_fetch_flags(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval,
365 common_flags = HVhek_UTF8;
370 hek = hv_fetch_common (hv, NULL, key, klen, common_flags,
371 ((flags & HV_FETCH_WANTPLACEHOLDERS)
372 ? HV_FETCH_PLACEHOLDER
375 | (lval ? HV_FETCH_LVALUE : 0),
377 return hek ? &HeVAL(hek) : NULL;
381 =for apidoc hv_exists_ent
383 Returns a boolean indicating whether the specified hash key exists. C<hash>
384 can be a valid precomputed hash value, or 0 to ask for it to be
391 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
393 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash)
397 /* returns an HE * structure with the all fields set */
398 /* note that hent_val will be a mortal sv for MAGICAL hashes */
400 =for apidoc hv_fetch_ent
402 Returns the hash entry which corresponds to the specified key in the hash.
403 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
404 if you want the function to compute it. IF C<lval> is set then the fetch
405 will be part of a store. Make sure the return value is non-null before
406 accessing it. The return value when C<tb> is a tied hash is a pointer to a
407 static location, so be sure to make a copy of the structure if you need to
410 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
411 information on how to use this function on tied hashes.
417 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
419 return hv_fetch_common(hv, keysv, NULL, 0, 0,
420 (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash);
424 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
425 int flags, int action, SV *val, register U32 hash)
439 if (flags & HVhek_FREEKEY)
441 key = SvPV(keysv, klen);
443 is_utf8 = (SvUTF8(keysv) != 0);
445 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
448 xhv = (XPVHV*)SvANY(hv);
450 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS)))
452 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
455 /* XXX should be able to skimp on the HE/HEK here when
456 HV_FETCH_JUST_SV is true. */
459 keysv = newSVpvn(key, klen);
464 keysv = newSVsv(keysv);
466 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
468 /* grab a fake HE/HEK pair from the pool or make a new one */
469 entry = PL_hv_fetch_ent_mh;
471 PL_hv_fetch_ent_mh = HeNEXT(entry);
475 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
476 HeKEY_hek(entry) = (HEK*)k;
478 HeNEXT(entry) = Nullhe;
479 HeSVKEY_set(entry, keysv);
481 sv_upgrade(sv, SVt_PVLV);
483 /* so we can free entry when freeing sv */
484 LvTARG(sv) = (SV*)entry;
486 /* XXX remove at some point? */
487 if (flags & HVhek_FREEKEY)
492 #ifdef ENV_IS_CASELESS
493 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
495 for (i = 0; i < klen; ++i)
496 if (isLOWER(key[i])) {
497 /* Would be nice if we had a routine to do the
498 copy and upercase in a single pass through. */
499 char *nkey = strupr(savepvn(key,klen));
500 /* Note that this fetch is for nkey (the uppercased
501 key) whereas the store is for key (the original) */
502 entry = hv_fetch_common(hv, Nullsv, nkey, klen,
503 HVhek_FREEKEY, /* free nkey */
504 0 /* non-LVAL fetch */,
505 Nullsv /* no value */,
506 0 /* compute hash */);
507 if (!entry && (action & HV_FETCH_LVALUE)) {
508 /* This call will free key if necessary.
509 Do it this way to encourage compiler to tail
511 entry = hv_fetch_common(hv, keysv, key, klen,
512 flags, HV_FETCH_ISSTORE,
515 if (flags & HVhek_FREEKEY)
523 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
524 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
526 /* I don't understand why hv_exists_ent has svret and sv,
527 whereas hv_exists only had one. */
528 svret = sv_newmortal();
531 if (keysv || is_utf8) {
533 keysv = newSVpvn(key, klen);
536 keysv = newSVsv(keysv);
538 mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
540 mg_copy((SV*)hv, sv, key, klen);
542 if (flags & HVhek_FREEKEY)
544 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
545 /* This cast somewhat evil, but I'm merely using NULL/
546 not NULL to return the boolean exists.
547 And I know hv is not NULL. */
548 return SvTRUE(svret) ? (HE *)hv : NULL;
550 #ifdef ENV_IS_CASELESS
551 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
552 /* XXX This code isn't UTF8 clean. */
553 const char *keysave = key;
554 /* Will need to free this, so set FREEKEY flag. */
555 key = savepvn(key,klen);
556 key = (const char*)strupr((char*)key);
561 if (flags & HVhek_FREEKEY) {
564 flags |= HVhek_FREEKEY;
568 else if (action & HV_FETCH_ISSTORE) {
571 hv_magic_check (hv, &needs_copy, &needs_store);
573 bool save_taint = PL_tainted;
574 if (keysv || is_utf8) {
576 keysv = newSVpvn(key, klen);
580 PL_tainted = SvTAINTED(keysv);
581 keysv = sv_2mortal(newSVsv(keysv));
582 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
584 mg_copy((SV*)hv, val, key, klen);
587 TAINT_IF(save_taint);
588 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
589 if (flags & HVhek_FREEKEY)
593 #ifdef ENV_IS_CASELESS
594 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
595 /* XXX This code isn't UTF8 clean. */
596 const char *keysave = key;
597 /* Will need to free this, so set FREEKEY flag. */
598 key = savepvn(key,klen);
599 key = (const char*)strupr((char*)key);
604 if (flags & HVhek_FREEKEY) {
607 flags |= HVhek_FREEKEY;
614 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
615 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
616 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
617 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
620 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
621 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
623 #ifdef DYNAMIC_ENV_FETCH
624 else if (action & HV_FETCH_ISEXISTS) {
625 /* for an %ENV exists, if we do an insert it's by a recursive
626 store call, so avoid creating HvARRAY(hv) right now. */
630 /* XXX remove at some point? */
631 if (flags & HVhek_FREEKEY)
639 const char *keysave = key;
640 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
644 flags &= ~HVhek_UTF8;
645 if (key != keysave) {
646 if (flags & HVhek_FREEKEY)
648 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
653 PERL_HASH_INTERNAL(hash, key, klen);
654 /* We don't have a pointer to the hv, so we have to replicate the
655 flag into every HEK, so that hv_iterkeysv can see it. */
656 /* And yes, you do need this even though you are not "storing" because
657 you can flip the flags below if doing an lval lookup. (And that
658 was put in to give the semantics Andreas was expecting.) */
659 flags |= HVhek_REHASH;
661 if (keysv && (SvIsCOW_shared_hash(keysv))) {
664 PERL_HASH(hash, key, klen);
668 masked_flags = (flags & HVhek_MASK);
671 #ifdef DYNAMIC_ENV_FETCH
672 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
676 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
677 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
679 for (; entry; ++n_links, entry = HeNEXT(entry)) {
680 if (HeHASH(entry) != hash) /* strings can't be equal */
682 if (HeKLEN(entry) != (I32)klen)
684 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
686 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
689 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
690 if (HeKFLAGS(entry) != masked_flags) {
691 /* We match if HVhek_UTF8 bit in our flags and hash key's
692 match. But if entry was set previously with HVhek_WASUTF8
693 and key now doesn't (or vice versa) then we should change
694 the key's flag, as this is assignment. */
695 if (HvSHAREKEYS(hv)) {
696 /* Need to swap the key we have for a key with the flags we
697 need. As keys are shared we can't just write to the
698 flag, so we share the new one, unshare the old one. */
699 HEK *new_hek = share_hek_flags(key, klen, hash,
701 unshare_hek (HeKEY_hek(entry));
702 HeKEY_hek(entry) = new_hek;
705 HeKFLAGS(entry) = masked_flags;
706 if (masked_flags & HVhek_ENABLEHVKFLAGS)
709 if (HeVAL(entry) == &PL_sv_placeholder) {
710 /* yes, can store into placeholder slot */
711 if (action & HV_FETCH_LVALUE) {
713 /* This preserves behaviour with the old hv_fetch
714 implementation which at this point would bail out
715 with a break; (at "if we find a placeholder, we
716 pretend we haven't found anything")
718 That break mean that if a placeholder were found, it
719 caused a call into hv_store, which in turn would
720 check magic, and if there is no magic end up pretty
721 much back at this point (in hv_store's code). */
724 /* LVAL fetch which actaully needs a store. */
726 xhv->xhv_placeholders--;
729 if (val != &PL_sv_placeholder)
730 xhv->xhv_placeholders--;
733 } else if (action & HV_FETCH_ISSTORE) {
734 SvREFCNT_dec(HeVAL(entry));
737 } else if (HeVAL(entry) == &PL_sv_placeholder
738 && !(action & HV_FETCH_PLACEHOLDER))
740 /* if we find a placeholder, we pretend we haven't found
744 if (flags & HVhek_FREEKEY)
748 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
749 if (!(action & HV_FETCH_ISSTORE)
750 && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
752 char *env = PerlEnv_ENVgetenv_len(key,&len);
754 sv = newSVpvn(env,len);
756 return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv,
762 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
763 S_hv_notallowed(aTHX_ flags, key, klen,
764 "access disallowed key '%"SVf"' in"
767 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
768 /* Not doing some form of store, so return failure. */
769 if (flags & HVhek_FREEKEY)
773 if (action & HV_FETCH_LVALUE) {
776 /* At this point the old hv_fetch code would call to hv_store,
777 which in turn might do some tied magic. So we need to make that
778 magic check happen. */
779 /* gonna assign to this, so it better be there */
780 return hv_fetch_common(hv, keysv, key, klen, flags,
781 HV_FETCH_ISSTORE, val, hash);
782 /* XXX Surely that could leak if the fetch-was-store fails?
783 Just like the hv_fetch. */
787 /* Welcome to hv_store... */
789 if (!xhv->xhv_array) {
790 /* Not sure if we can get here. I think the only case of oentry being
791 NULL is for %ENV with dynamic env fetch. But that should disappear
792 with magic in the previous code. */
793 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
794 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
798 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
801 /* share_hek_flags will do the free for us. This might be considered
804 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
805 else /* gotta do the real thing */
806 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
808 HeNEXT(entry) = *oentry;
811 if (val == &PL_sv_placeholder)
812 xhv->xhv_placeholders++;
813 if (masked_flags & HVhek_ENABLEHVKFLAGS)
816 xhv->xhv_keys++; /* HvKEYS(hv)++ */
817 if (!n_links) { /* initial entry? */
818 xhv->xhv_fill++; /* HvFILL(hv)++ */
819 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
820 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
821 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
822 splits on a rehashed hash, as we're not going to split it again,
823 and if someone is lucky (evil) enough to get all the keys in one
824 list they could exhaust our memory as we repeatedly double the
825 number of buckets on every entry. Linear search feels a less worse
834 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
836 MAGIC *mg = SvMAGIC(hv);
840 if (isUPPER(mg->mg_type)) {
842 switch (mg->mg_type) {
843 case PERL_MAGIC_tied:
845 *needs_store = FALSE;
848 mg = mg->mg_moremagic;
853 =for apidoc hv_scalar
855 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
861 Perl_hv_scalar(pTHX_ HV *hv)
866 if ((SvRMAGICAL(hv) && (mg = mg_find((SV*)hv, PERL_MAGIC_tied)))) {
867 sv = magic_scalarpack(hv, mg);
873 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
874 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
882 =for apidoc hv_delete
884 Deletes a key/value pair in the hash. The value SV is removed from the
885 hash and returned to the caller. The C<klen> is the length of the key.
886 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
893 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
900 k_flags |= HVhek_UTF8;
904 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
908 =for apidoc hv_delete_ent
910 Deletes a key/value pair in the hash. The value SV is removed from the
911 hash and returned to the caller. The C<flags> value will normally be zero;
912 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
913 precomputed hash value, or 0 to ask for it to be computed.
919 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
921 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
925 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
926 int k_flags, I32 d_flags, U32 hash)
931 register HE **oentry;
940 if (k_flags & HVhek_FREEKEY)
942 key = SvPV(keysv, klen);
944 is_utf8 = (SvUTF8(keysv) != 0);
946 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
949 if (SvRMAGICAL(hv)) {
952 hv_magic_check (hv, &needs_copy, &needs_store);
955 entry = hv_fetch_common(hv, keysv, key, klen,
956 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
958 sv = entry ? HeVAL(entry) : NULL;
964 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
965 /* No longer an element */
966 sv_unmagic(sv, PERL_MAGIC_tiedelem);
969 return Nullsv; /* element cannot be deleted */
971 #ifdef ENV_IS_CASELESS
972 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
973 /* XXX This code isn't UTF8 clean. */
974 keysv = sv_2mortal(newSVpvn(key,klen));
975 if (k_flags & HVhek_FREEKEY) {
978 key = strupr(SvPVX(keysv));
987 xhv = (XPVHV*)SvANY(hv);
988 if (!xhv->xhv_array /* !HvARRAY(hv) */)
992 const char *keysave = key;
993 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
996 k_flags |= HVhek_UTF8;
998 k_flags &= ~HVhek_UTF8;
999 if (key != keysave) {
1000 if (k_flags & HVhek_FREEKEY) {
1001 /* This shouldn't happen if our caller does what we expect,
1002 but strictly the API allows it. */
1005 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1007 HvHASKFLAGS_on((SV*)hv);
1011 PERL_HASH_INTERNAL(hash, key, klen);
1013 if (keysv && (SvIsCOW_shared_hash(keysv))) {
1014 hash = SvUVX(keysv);
1016 PERL_HASH(hash, key, klen);
1020 masked_flags = (k_flags & HVhek_MASK);
1022 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1023 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1026 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1027 if (HeHASH(entry) != hash) /* strings can't be equal */
1029 if (HeKLEN(entry) != (I32)klen)
1031 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1033 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1036 /* if placeholder is here, it's already been deleted.... */
1037 if (HeVAL(entry) == &PL_sv_placeholder)
1039 if (k_flags & HVhek_FREEKEY)
1043 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1044 S_hv_notallowed(aTHX_ k_flags, key, klen,
1045 "delete readonly key '%"SVf"' from"
1048 if (k_flags & HVhek_FREEKEY)
1051 if (d_flags & G_DISCARD)
1054 sv = sv_2mortal(HeVAL(entry));
1055 HeVAL(entry) = &PL_sv_placeholder;
1059 * If a restricted hash, rather than really deleting the entry, put
1060 * a placeholder there. This marks the key as being "approved", so
1061 * we can still access via not-really-existing key without raising
1064 if (SvREADONLY(hv)) {
1065 SvREFCNT_dec(HeVAL(entry));
1066 HeVAL(entry) = &PL_sv_placeholder;
1067 /* We'll be saving this slot, so the number of allocated keys
1068 * doesn't go down, but the number placeholders goes up */
1069 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1071 *oentry = HeNEXT(entry);
1073 xhv->xhv_fill--; /* HvFILL(hv)-- */
1074 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1077 hv_free_ent(hv, entry);
1078 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1079 if (xhv->xhv_keys == 0)
1080 HvHASKFLAGS_off(hv);
1084 if (SvREADONLY(hv)) {
1085 S_hv_notallowed(aTHX_ k_flags, key, klen,
1086 "delete disallowed key '%"SVf"' from"
1090 if (k_flags & HVhek_FREEKEY)
1096 S_hsplit(pTHX_ HV *hv)
1098 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1099 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1100 register I32 newsize = oldsize * 2;
1102 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1106 register HE **oentry;
1107 int longest_chain = 0;
1110 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1111 hv, (int) oldsize);*/
1113 if (HvPLACEHOLDERS(hv) && !SvREADONLY(hv)) {
1114 /* Can make this clear any placeholders first for non-restricted hashes,
1115 even though Storable rebuilds restricted hashes by putting in all the
1116 placeholders (first) before turning on the readonly flag, because
1117 Storable always pre-splits the hash. */
1118 hv_clear_placeholders(hv);
1122 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1123 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1129 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1134 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1135 if (oldsize >= 64) {
1136 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1137 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1140 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1144 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1145 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1146 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1149 for (i=0; i<oldsize; i++,aep++) {
1150 int left_length = 0;
1151 int right_length = 0;
1153 if (!*aep) /* non-existent */
1156 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1157 if ((HeHASH(entry) & newsize) != (U32)i) {
1158 *oentry = HeNEXT(entry);
1159 HeNEXT(entry) = *bep;
1161 xhv->xhv_fill++; /* HvFILL(hv)++ */
1167 oentry = &HeNEXT(entry);
1171 if (!*aep) /* everything moved */
1172 xhv->xhv_fill--; /* HvFILL(hv)-- */
1173 /* I think we don't actually need to keep track of the longest length,
1174 merely flag if anything is too long. But for the moment while
1175 developing this code I'll track it. */
1176 if (left_length > longest_chain)
1177 longest_chain = left_length;
1178 if (right_length > longest_chain)
1179 longest_chain = right_length;
1183 /* Pick your policy for "hashing isn't working" here: */
1184 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1189 if (hv == PL_strtab) {
1190 /* Urg. Someone is doing something nasty to the string table.
1195 /* Awooga. Awooga. Pathological data. */
1196 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1197 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1200 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1201 was_shared = HvSHAREKEYS(hv);
1204 HvSHAREKEYS_off(hv);
1207 aep = (HE **) xhv->xhv_array;
1209 for (i=0; i<newsize; i++,aep++) {
1212 /* We're going to trash this HE's next pointer when we chain it
1213 into the new hash below, so store where we go next. */
1214 HE *next = HeNEXT(entry);
1218 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1223 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1224 hash, HeKFLAGS(entry));
1225 unshare_hek (HeKEY_hek(entry));
1226 HeKEY_hek(entry) = new_hek;
1228 /* Not shared, so simply write the new hash in. */
1229 HeHASH(entry) = hash;
1231 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1232 HEK_REHASH_on(HeKEY_hek(entry));
1233 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1235 /* Copy oentry to the correct new chain. */
1236 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1238 xhv->xhv_fill++; /* HvFILL(hv)++ */
1239 HeNEXT(entry) = *bep;
1245 Safefree (xhv->xhv_array);
1246 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1250 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1252 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1253 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1254 register I32 newsize;
1260 register HE **oentry;
1262 newsize = (I32) newmax; /* possible truncation here */
1263 if (newsize != newmax || newmax <= oldsize)
1265 while ((newsize & (1 + ~newsize)) != newsize) {
1266 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1268 if (newsize < newmax)
1270 if (newsize < newmax)
1271 return; /* overflow detection */
1273 a = xhv->xhv_array; /* HvARRAY(hv) */
1276 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1277 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1283 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1288 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1289 if (oldsize >= 64) {
1290 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1291 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1294 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1297 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1300 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1302 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1303 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1304 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1308 for (i=0; i<oldsize; i++,aep++) {
1309 if (!*aep) /* non-existent */
1311 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1312 if ((j = (HeHASH(entry) & newsize)) != i) {
1314 *oentry = HeNEXT(entry);
1315 if (!(HeNEXT(entry) = aep[j]))
1316 xhv->xhv_fill++; /* HvFILL(hv)++ */
1321 oentry = &HeNEXT(entry);
1323 if (!*aep) /* everything moved */
1324 xhv->xhv_fill--; /* HvFILL(hv)-- */
1331 Creates a new HV. The reference count is set to 1.
1340 register XPVHV* xhv;
1342 hv = (HV*)NEWSV(502,0);
1343 sv_upgrade((SV *)hv, SVt_PVHV);
1344 xhv = (XPVHV*)SvANY(hv);
1347 #ifndef NODEFAULT_SHAREKEYS
1348 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1351 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1352 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1353 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1354 (void)hv_iterinit(hv); /* so each() will start off right */
1359 Perl_newHVhv(pTHX_ HV *ohv)
1362 STRLEN hv_max, hv_fill;
1364 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1366 hv_max = HvMAX(ohv);
1368 if (!SvMAGICAL((SV *)ohv)) {
1369 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1371 bool shared = !!HvSHAREKEYS(ohv);
1372 HE **ents, **oents = (HE **)HvARRAY(ohv);
1374 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1377 /* In each bucket... */
1378 for (i = 0; i <= hv_max; i++) {
1379 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1386 /* Copy the linked list of entries. */
1387 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1388 U32 hash = HeHASH(oent);
1389 char *key = HeKEY(oent);
1390 STRLEN len = HeKLEN(oent);
1391 int flags = HeKFLAGS(oent);
1394 HeVAL(ent) = newSVsv(HeVAL(oent));
1396 = shared ? share_hek_flags(key, len, hash, flags)
1397 : save_hek_flags(key, len, hash, flags);
1408 HvFILL(hv) = hv_fill;
1409 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1413 /* Iterate over ohv, copying keys and values one at a time. */
1415 I32 riter = HvRITER(ohv);
1416 HE *eiter = HvEITER(ohv);
1418 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1419 while (hv_max && hv_max + 1 >= hv_fill * 2)
1420 hv_max = hv_max / 2;
1424 while ((entry = hv_iternext_flags(ohv, 0))) {
1425 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1426 newSVsv(HeVAL(entry)), HeHASH(entry),
1429 HvRITER(ohv) = riter;
1430 HvEITER(ohv) = eiter;
1437 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1444 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1445 PL_sub_generation++; /* may be deletion of method from stash */
1447 if (HeKLEN(entry) == HEf_SVKEY) {
1448 SvREFCNT_dec(HeKEY_sv(entry));
1449 Safefree(HeKEY_hek(entry));
1451 else if (HvSHAREKEYS(hv))
1452 unshare_hek(HeKEY_hek(entry));
1454 Safefree(HeKEY_hek(entry));
1459 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1463 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1464 PL_sub_generation++; /* may be deletion of method from stash */
1465 sv_2mortal(HeVAL(entry)); /* free between statements */
1466 if (HeKLEN(entry) == HEf_SVKEY) {
1467 sv_2mortal(HeKEY_sv(entry));
1468 Safefree(HeKEY_hek(entry));
1470 else if (HvSHAREKEYS(hv))
1471 unshare_hek(HeKEY_hek(entry));
1473 Safefree(HeKEY_hek(entry));
1478 =for apidoc hv_clear
1480 Clears a hash, making it empty.
1486 Perl_hv_clear(pTHX_ HV *hv)
1488 register XPVHV* xhv;
1492 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1494 xhv = (XPVHV*)SvANY(hv);
1496 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1497 /* restricted hash: convert all keys to placeholders */
1500 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1501 entry = ((HE**)xhv->xhv_array)[i];
1502 for (; entry; entry = HeNEXT(entry)) {
1503 /* not already placeholder */
1504 if (HeVAL(entry) != &PL_sv_placeholder) {
1505 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1506 SV* keysv = hv_iterkeysv(entry);
1508 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1511 SvREFCNT_dec(HeVAL(entry));
1512 HeVAL(entry) = &PL_sv_placeholder;
1513 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1521 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1522 if (xhv->xhv_array /* HvARRAY(hv) */)
1523 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1524 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1529 HvHASKFLAGS_off(hv);
1536 =for apidoc hv_clear_placeholders
1538 Clears any placeholders from a hash. If a restricted hash has any of its keys
1539 marked as readonly and the key is subsequently deleted, the key is not actually
1540 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1541 it so it will be ignored by future operations such as iterating over the hash,
1542 but will still allow the hash to have a value reassigned to the key at some
1543 future point. This function clears any such placeholder keys from the hash.
1544 See Hash::Util::lock_keys() for an example of its use.
1550 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1552 I32 items = (I32)HvPLACEHOLDERS(hv);
1559 /* Loop down the linked list heads */
1561 HE **oentry = &(HvARRAY(hv))[i];
1562 HE *entry = *oentry;
1567 for (; entry; entry = *oentry) {
1568 if (HeVAL(entry) == &PL_sv_placeholder) {
1569 *oentry = HeNEXT(entry);
1570 if (first && !*oentry)
1571 HvFILL(hv)--; /* This linked list is now empty. */
1575 hv_free_ent(hv, entry);
1579 HvTOTALKEYS(hv) -= HvPLACEHOLDERS(hv);
1580 if (HvKEYS(hv) == 0)
1581 HvHASKFLAGS_off(hv);
1582 HvPLACEHOLDERS(hv) = 0;
1586 oentry = &HeNEXT(entry);
1591 /* You can't get here, hence assertion should always fail. */
1592 assert (items == 0);
1597 S_hfreeentries(pTHX_ HV *hv)
1599 register HE **array;
1601 register HE *oentry = Null(HE*);
1612 array = HvARRAY(hv);
1613 /* make everyone else think the array is empty, so that the destructors
1614 * called for freed entries can't recusively mess with us */
1615 HvARRAY(hv) = Null(HE**);
1617 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1623 entry = HeNEXT(entry);
1624 hv_free_ent(hv, oentry);
1629 entry = array[riter];
1632 HvARRAY(hv) = array;
1633 (void)hv_iterinit(hv);
1637 =for apidoc hv_undef
1645 Perl_hv_undef(pTHX_ HV *hv)
1647 register XPVHV* xhv;
1650 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1651 xhv = (XPVHV*)SvANY(hv);
1653 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1656 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1657 Safefree(HvNAME(hv));
1660 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1661 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1662 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1669 =for apidoc hv_iterinit
1671 Prepares a starting point to traverse a hash table. Returns the number of
1672 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1673 currently only meaningful for hashes without tie magic.
1675 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1676 hash buckets that happen to be in use. If you still need that esoteric
1677 value, you can get it through the macro C<HvFILL(tb)>.
1684 Perl_hv_iterinit(pTHX_ HV *hv)
1686 register XPVHV* xhv;
1690 Perl_croak(aTHX_ "Bad hash");
1691 xhv = (XPVHV*)SvANY(hv);
1692 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1693 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1695 hv_free_ent(hv, entry);
1697 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1698 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1699 /* used to be xhv->xhv_fill before 5.004_65 */
1700 return XHvTOTALKEYS(xhv);
1703 =for apidoc hv_iternext
1705 Returns entries from a hash iterator. See C<hv_iterinit>.
1707 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1708 iterator currently points to, without losing your place or invalidating your
1709 iterator. Note that in this case the current entry is deleted from the hash
1710 with your iterator holding the last reference to it. Your iterator is flagged
1711 to free the entry on the next call to C<hv_iternext>, so you must not discard
1712 your iterator immediately else the entry will leak - call C<hv_iternext> to
1713 trigger the resource deallocation.
1719 Perl_hv_iternext(pTHX_ HV *hv)
1721 return hv_iternext_flags(hv, 0);
1725 =for apidoc hv_iternext_flags
1727 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1728 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1729 set the placeholders keys (for restricted hashes) will be returned in addition
1730 to normal keys. By default placeholders are automatically skipped over.
1731 Currently a placeholder is implemented with a value that is
1732 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1733 restricted hashes may change, and the implementation currently is
1734 insufficiently abstracted for any change to be tidy.
1740 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1742 register XPVHV* xhv;
1748 Perl_croak(aTHX_ "Bad hash");
1749 xhv = (XPVHV*)SvANY(hv);
1750 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1752 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1753 SV *key = sv_newmortal();
1755 sv_setsv(key, HeSVKEY_force(entry));
1756 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1762 /* one HE per MAGICAL hash */
1763 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1765 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1767 HeKEY_hek(entry) = hek;
1768 HeKLEN(entry) = HEf_SVKEY;
1770 magic_nextpack((SV*) hv,mg,key);
1772 /* force key to stay around until next time */
1773 HeSVKEY_set(entry, SvREFCNT_inc(key));
1774 return entry; /* beware, hent_val is not set */
1777 SvREFCNT_dec(HeVAL(entry));
1778 Safefree(HeKEY_hek(entry));
1780 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1783 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1784 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1788 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1789 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1790 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1792 /* At start of hash, entry is NULL. */
1795 entry = HeNEXT(entry);
1796 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1798 * Skip past any placeholders -- don't want to include them in
1801 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1802 entry = HeNEXT(entry);
1807 /* OK. Come to the end of the current list. Grab the next one. */
1809 xhv->xhv_riter++; /* HvRITER(hv)++ */
1810 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1811 /* There is no next one. End of the hash. */
1812 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1815 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1816 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1818 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1819 /* If we have an entry, but it's a placeholder, don't count it.
1821 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1822 entry = HeNEXT(entry);
1824 /* Will loop again if this linked list starts NULL
1825 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1826 or if we run through it and find only placeholders. */
1829 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1831 hv_free_ent(hv, oldentry);
1834 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1835 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1837 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1842 =for apidoc hv_iterkey
1844 Returns the key from the current position of the hash iterator. See
1851 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1853 if (HeKLEN(entry) == HEf_SVKEY) {
1855 char *p = SvPV(HeKEY_sv(entry), len);
1860 *retlen = HeKLEN(entry);
1861 return HeKEY(entry);
1865 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1867 =for apidoc hv_iterkeysv
1869 Returns the key as an C<SV*> from the current position of the hash
1870 iterator. The return value will always be a mortal copy of the key. Also
1877 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1879 if (HeKLEN(entry) != HEf_SVKEY) {
1880 HEK *hek = HeKEY_hek(entry);
1881 int flags = HEK_FLAGS(hek);
1884 if (flags & HVhek_WASUTF8) {
1886 Andreas would like keys he put in as utf8 to come back as utf8
1888 STRLEN utf8_len = HEK_LEN(hek);
1889 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1891 sv = newSVpvn ((char*)as_utf8, utf8_len);
1893 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1894 } else if (flags & HVhek_REHASH) {
1895 /* We don't have a pointer to the hv, so we have to replicate the
1896 flag into every HEK. This hv is using custom a hasing
1897 algorithm. Hence we can't return a shared string scalar, as
1898 that would contain the (wrong) hash value, and might get passed
1899 into an hv routine with a regular hash */
1901 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1905 sv = newSVpvn_share(HEK_KEY(hek),
1906 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1909 return sv_2mortal(sv);
1911 return sv_mortalcopy(HeKEY_sv(entry));
1915 =for apidoc hv_iterval
1917 Returns the value from the current position of the hash iterator. See
1924 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1926 if (SvRMAGICAL(hv)) {
1927 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1928 SV* sv = sv_newmortal();
1929 if (HeKLEN(entry) == HEf_SVKEY)
1930 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1931 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1935 return HeVAL(entry);
1939 =for apidoc hv_iternextsv
1941 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1948 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1951 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1953 *key = hv_iterkey(he, retlen);
1954 return hv_iterval(hv, he);
1958 =for apidoc hv_magic
1960 Adds magic to a hash. See C<sv_magic>.
1966 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1968 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1971 #if 0 /* use the macro from hv.h instead */
1974 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1976 return HEK_KEY(share_hek(sv, len, hash));
1981 /* possibly free a shared string if no one has access to it
1982 * len and hash must both be valid for str.
1985 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1987 unshare_hek_or_pvn (NULL, str, len, hash);
1992 Perl_unshare_hek(pTHX_ HEK *hek)
1994 unshare_hek_or_pvn(hek, NULL, 0, 0);
1997 /* possibly free a shared string if no one has access to it
1998 hek if non-NULL takes priority over the other 3, else str, len and hash
1999 are used. If so, len and hash must both be valid for str.
2002 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
2004 register XPVHV* xhv;
2006 register HE **oentry;
2009 bool is_utf8 = FALSE;
2011 const char *save = str;
2014 hash = HEK_HASH(hek);
2015 } else if (len < 0) {
2016 STRLEN tmplen = -len;
2018 /* See the note in hv_fetch(). --jhi */
2019 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2022 k_flags = HVhek_UTF8;
2024 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2027 /* what follows is the moral equivalent of:
2028 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2029 if (--*Svp == Nullsv)
2030 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2032 xhv = (XPVHV*)SvANY(PL_strtab);
2033 /* assert(xhv_array != 0) */
2035 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2036 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2038 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2039 if (HeKEY_hek(entry) != hek)
2045 int flags_masked = k_flags & HVhek_MASK;
2046 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2047 if (HeHASH(entry) != hash) /* strings can't be equal */
2049 if (HeKLEN(entry) != len)
2051 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2053 if (HeKFLAGS(entry) != flags_masked)
2061 if (--HeVAL(entry) == Nullsv) {
2062 *oentry = HeNEXT(entry);
2064 xhv->xhv_fill--; /* HvFILL(hv)-- */
2065 Safefree(HeKEY_hek(entry));
2067 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2071 UNLOCK_STRTAB_MUTEX;
2072 if (!found && ckWARN_d(WARN_INTERNAL))
2073 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2074 "Attempt to free non-existent shared string '%s'%s"
2076 hek ? HEK_KEY(hek) : str,
2077 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2078 if (k_flags & HVhek_FREEKEY)
2082 /* get a (constant) string ptr from the global string table
2083 * string will get added if it is not already there.
2084 * len and hash must both be valid for str.
2087 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2089 bool is_utf8 = FALSE;
2091 const char *save = str;
2094 STRLEN tmplen = -len;
2096 /* See the note in hv_fetch(). --jhi */
2097 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2099 /* If we were able to downgrade here, then than means that we were passed
2100 in a key which only had chars 0-255, but was utf8 encoded. */
2103 /* If we found we were able to downgrade the string to bytes, then
2104 we should flag that it needs upgrading on keys or each. Also flag
2105 that we need share_hek_flags to free the string. */
2107 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2110 return share_hek_flags (str, len, hash, flags);
2114 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2116 register XPVHV* xhv;
2118 register HE **oentry;
2121 int flags_masked = flags & HVhek_MASK;
2123 /* what follows is the moral equivalent of:
2125 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2126 hv_store(PL_strtab, str, len, Nullsv, hash);
2128 Can't rehash the shared string table, so not sure if it's worth
2129 counting the number of entries in the linked list
2131 xhv = (XPVHV*)SvANY(PL_strtab);
2132 /* assert(xhv_array != 0) */
2134 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2135 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2136 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2137 if (HeHASH(entry) != hash) /* strings can't be equal */
2139 if (HeKLEN(entry) != len)
2141 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2143 if (HeKFLAGS(entry) != flags_masked)
2150 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags_masked);
2151 HeVAL(entry) = Nullsv;
2152 HeNEXT(entry) = *oentry;
2154 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2155 if (i) { /* initial entry? */
2156 xhv->xhv_fill++; /* HvFILL(hv)++ */
2157 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2162 ++HeVAL(entry); /* use value slot as REFCNT */
2163 UNLOCK_STRTAB_MUTEX;
2165 if (flags & HVhek_FREEKEY)
2168 return HeKEY_hek(entry);
2173 =for apidoc hv_assert
2175 Check that a hash is in an internally consistent state.
2181 Perl_hv_assert(pTHX_ HV *hv)
2185 int placeholders = 0;
2188 I32 riter = HvRITER(hv);
2189 HE *eiter = HvEITER(hv);
2191 (void)hv_iterinit(hv);
2193 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2194 /* sanity check the values */
2195 if (HeVAL(entry) == &PL_sv_placeholder) {
2200 /* sanity check the keys */
2201 if (HeSVKEY(entry)) {
2202 /* Don't know what to check on SV keys. */
2203 } else if (HeKUTF8(entry)) {
2205 if (HeKWASUTF8(entry)) {
2206 PerlIO_printf(Perl_debug_log,
2207 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2208 (int) HeKLEN(entry), HeKEY(entry));
2211 } else if (HeKWASUTF8(entry)) {
2215 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2216 if (HvUSEDKEYS(hv) != real) {
2217 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2218 (int) real, (int) HvUSEDKEYS(hv));
2221 if (HvPLACEHOLDERS(hv) != placeholders) {
2222 PerlIO_printf(Perl_debug_log,
2223 "Count %d placeholder(s), but hash reports %d\n",
2224 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2228 if (withflags && ! HvHASKFLAGS(hv)) {
2229 PerlIO_printf(Perl_debug_log,
2230 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2237 HvRITER(hv) = riter; /* Restore hash iterator state */
2238 HvEITER(hv) = eiter;