3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 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
41 New(54, he, PERL_ARENA_SIZE/sizeof(HE), HE);
42 HeNEXT(he) = PL_he_arenaroot;
45 heend = &he[PERL_ARENA_SIZE / sizeof(HE) - 1];
48 HeNEXT(he) = (HE*)(he + 1);
62 PL_he_root = HeNEXT(he);
71 HeNEXT(p) = (HE*)PL_he_root;
78 #define new_HE() (HE*)safemalloc(sizeof(HE))
79 #define del_HE(p) safefree((char*)p)
83 #define new_HE() new_he()
84 #define del_HE(p) del_he(p)
89 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
91 const int flags_masked = flags & HVhek_MASK;
95 New(54, k, HEK_BASESIZE + len + 2, char);
97 Copy(str, HEK_KEY(hek), len, char);
98 HEK_KEY(hek)[len] = 0;
100 HEK_HASH(hek) = hash;
101 HEK_FLAGS(hek) = (unsigned char)flags_masked;
103 if (flags & HVhek_FREEKEY)
108 /* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
112 Perl_free_tied_hv_pool(pTHX)
115 HE *he = PL_hv_fetch_ent_mh;
117 Safefree(HeKEY_hek(he));
122 PL_hv_fetch_ent_mh = Nullhe;
125 #if defined(USE_ITHREADS)
127 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
133 /* look for it in the table first */
134 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
138 /* create anew and remember what it is */
140 ptr_table_store(PL_ptr_table, e, ret);
142 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
143 if (HeKLEN(e) == HEf_SVKEY) {
145 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
146 HeKEY_hek(ret) = (HEK*)k;
147 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
150 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
153 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
155 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
158 #endif /* USE_ITHREADS */
161 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
164 SV *sv = sv_newmortal();
165 if (!(flags & HVhek_FREEKEY)) {
166 sv_setpvn(sv, key, klen);
169 /* Need to free saved eventually assign to mortal SV */
170 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
171 sv_usepvn(sv, (char *) key, klen);
173 if (flags & HVhek_UTF8) {
176 Perl_croak(aTHX_ msg, sv);
179 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
182 #define HV_FETCH_ISSTORE 0x01
183 #define HV_FETCH_ISEXISTS 0x02
184 #define HV_FETCH_LVALUE 0x04
185 #define HV_FETCH_JUST_SV 0x08
190 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
191 the length of the key. The C<hash> parameter is the precomputed hash
192 value; if it is zero then Perl will compute it. The return value will be
193 NULL if the operation failed or if the value did not need to be actually
194 stored within the hash (as in the case of tied hashes). Otherwise it can
195 be dereferenced to get the original C<SV*>. Note that the caller is
196 responsible for suitably incrementing the reference count of C<val> before
197 the call, and decrementing it if the function returned NULL. Effectively
198 a successful hv_store takes ownership of one reference to C<val>. This is
199 usually what you want; a newly created SV has a reference count of one, so
200 if all your code does is create SVs then store them in a hash, hv_store
201 will own the only reference to the new SV, and your code doesn't need to do
202 anything further to tidy up. hv_store is not implemented as a call to
203 hv_store_ent, and does not create a temporary SV for the key, so if your
204 key data is not already in SV form then use hv_store in preference to
207 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
208 information on how to use this function on tied hashes.
214 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
227 hek = hv_fetch_common (hv, NULL, key, klen, flags,
228 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
229 return hek ? &HeVAL(hek) : NULL;
233 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
234 register U32 hash, int flags)
236 HE *hek = hv_fetch_common (hv, NULL, key, klen, flags,
237 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
238 return hek ? &HeVAL(hek) : NULL;
242 =for apidoc hv_store_ent
244 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
245 parameter is the precomputed hash value; if it is zero then Perl will
246 compute it. The return value is the new hash entry so created. It will be
247 NULL if the operation failed or if the value did not need to be actually
248 stored within the hash (as in the case of tied hashes). Otherwise the
249 contents of the return value can be accessed using the C<He?> macros
250 described here. Note that the caller is responsible for suitably
251 incrementing the reference count of C<val> before the call, and
252 decrementing it if the function returned NULL. Effectively a successful
253 hv_store_ent takes ownership of one reference to C<val>. This is
254 usually what you want; a newly created SV has a reference count of one, so
255 if all your code does is create SVs then store them in a hash, hv_store
256 will own the only reference to the new SV, and your code doesn't need to do
257 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
258 unlike C<val> it does not take ownership of it, so maintaining the correct
259 reference count on C<key> is entirely the caller's responsibility. hv_store
260 is not implemented as a call to hv_store_ent, and does not create a temporary
261 SV for the key, so if your key data is not already in SV form then use
262 hv_store in preference to hv_store_ent.
264 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
265 information on how to use this function on tied hashes.
271 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
273 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash);
277 =for apidoc hv_exists
279 Returns a boolean indicating whether the specified hash key exists. The
280 C<klen> is the length of the key.
286 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
298 return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0)
305 Returns the SV which corresponds to the specified key in the hash. The
306 C<klen> is the length of the key. If C<lval> is set then the fetch will be
307 part of a store. Check that the return value is non-null before
308 dereferencing it to an C<SV*>.
310 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
311 information on how to use this function on tied hashes.
317 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
330 hek = hv_fetch_common (hv, NULL, key, klen, flags,
331 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0),
333 return hek ? &HeVAL(hek) : NULL;
337 =for apidoc hv_exists_ent
339 Returns a boolean indicating whether the specified hash key exists. C<hash>
340 can be a valid precomputed hash value, or 0 to ask for it to be
347 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
349 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash)
353 /* returns an HE * structure with the all fields set */
354 /* note that hent_val will be a mortal sv for MAGICAL hashes */
356 =for apidoc hv_fetch_ent
358 Returns the hash entry which corresponds to the specified key in the hash.
359 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
360 if you want the function to compute it. IF C<lval> is set then the fetch
361 will be part of a store. Make sure the return value is non-null before
362 accessing it. The return value when C<tb> is a tied hash is a pointer to a
363 static location, so be sure to make a copy of the structure if you need to
366 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
367 information on how to use this function on tied hashes.
373 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
375 return hv_fetch_common(hv, keysv, NULL, 0, 0,
376 (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash);
380 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
381 int flags, int action, SV *val, register U32 hash)
396 if (flags & HVhek_FREEKEY)
398 key = SvPV(keysv, klen);
400 is_utf8 = (SvUTF8(keysv) != 0);
402 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
405 xhv = (XPVHV*)SvANY(hv);
407 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS)))
409 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
412 /* XXX should be able to skimp on the HE/HEK here when
413 HV_FETCH_JUST_SV is true. */
416 keysv = newSVpvn(key, klen);
421 keysv = newSVsv(keysv);
423 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
425 /* grab a fake HE/HEK pair from the pool or make a new one */
426 entry = PL_hv_fetch_ent_mh;
428 PL_hv_fetch_ent_mh = HeNEXT(entry);
432 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
433 HeKEY_hek(entry) = (HEK*)k;
435 HeNEXT(entry) = Nullhe;
436 HeSVKEY_set(entry, keysv);
438 sv_upgrade(sv, SVt_PVLV);
440 /* so we can free entry when freeing sv */
441 LvTARG(sv) = (SV*)entry;
443 /* XXX remove at some point? */
444 if (flags & HVhek_FREEKEY)
449 #ifdef ENV_IS_CASELESS
450 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
452 for (i = 0; i < klen; ++i)
453 if (isLOWER(key[i])) {
454 /* Would be nice if we had a routine to do the
455 copy and upercase in a single pass through. */
456 const char *nkey = strupr(savepvn(key,klen));
457 /* Note that this fetch is for nkey (the uppercased
458 key) whereas the store is for key (the original) */
459 entry = hv_fetch_common(hv, Nullsv, nkey, klen,
460 HVhek_FREEKEY, /* free nkey */
461 0 /* non-LVAL fetch */,
462 Nullsv /* no value */,
463 0 /* compute hash */);
464 if (!entry && (action & HV_FETCH_LVALUE)) {
465 /* This call will free key if necessary.
466 Do it this way to encourage compiler to tail
468 entry = hv_fetch_common(hv, keysv, key, klen,
469 flags, HV_FETCH_ISSTORE,
472 if (flags & HVhek_FREEKEY)
480 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
481 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
483 /* I don't understand why hv_exists_ent has svret and sv,
484 whereas hv_exists only had one. */
485 svret = sv_newmortal();
488 if (keysv || is_utf8) {
490 keysv = newSVpvn(key, klen);
493 keysv = newSVsv(keysv);
495 mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
497 mg_copy((SV*)hv, sv, key, klen);
499 if (flags & HVhek_FREEKEY)
501 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
502 /* This cast somewhat evil, but I'm merely using NULL/
503 not NULL to return the boolean exists.
504 And I know hv is not NULL. */
505 return SvTRUE(svret) ? (HE *)hv : NULL;
507 #ifdef ENV_IS_CASELESS
508 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
509 /* XXX This code isn't UTF8 clean. */
510 const char *keysave = key;
511 /* Will need to free this, so set FREEKEY flag. */
512 key = savepvn(key,klen);
513 key = (const char*)strupr((char*)key);
518 if (flags & HVhek_FREEKEY) {
521 flags |= HVhek_FREEKEY;
525 else if (action & HV_FETCH_ISSTORE) {
528 hv_magic_check (hv, &needs_copy, &needs_store);
530 const bool save_taint = PL_tainted;
531 if (keysv || is_utf8) {
533 keysv = newSVpvn(key, klen);
537 PL_tainted = SvTAINTED(keysv);
538 keysv = sv_2mortal(newSVsv(keysv));
539 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
541 mg_copy((SV*)hv, val, key, klen);
544 TAINT_IF(save_taint);
545 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
546 if (flags & HVhek_FREEKEY)
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;
571 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
572 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
573 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
574 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
577 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
578 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
580 #ifdef DYNAMIC_ENV_FETCH
581 else if (action & HV_FETCH_ISEXISTS) {
582 /* for an %ENV exists, if we do an insert it's by a recursive
583 store call, so avoid creating HvARRAY(hv) right now. */
587 /* XXX remove at some point? */
588 if (flags & HVhek_FREEKEY)
596 const char *keysave = key;
597 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
601 flags &= ~HVhek_UTF8;
602 if (key != keysave) {
603 if (flags & HVhek_FREEKEY)
605 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
610 PERL_HASH_INTERNAL(hash, key, klen);
611 /* We don't have a pointer to the hv, so we have to replicate the
612 flag into every HEK, so that hv_iterkeysv can see it. */
613 /* And yes, you do need this even though you are not "storing" because
614 you can flip the flags below if doing an lval lookup. (And that
615 was put in to give the semantics Andreas was expecting.) */
616 flags |= HVhek_REHASH;
618 if (keysv && (SvIsCOW_shared_hash(keysv))) {
621 PERL_HASH(hash, key, klen);
625 masked_flags = (flags & HVhek_MASK);
628 #ifdef DYNAMIC_ENV_FETCH
629 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
633 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
634 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
636 for (; entry; ++n_links, entry = HeNEXT(entry)) {
637 if (HeHASH(entry) != hash) /* strings can't be equal */
639 if (HeKLEN(entry) != (I32)klen)
641 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
643 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
646 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
647 if (HeKFLAGS(entry) != masked_flags) {
648 /* We match if HVhek_UTF8 bit in our flags and hash key's
649 match. But if entry was set previously with HVhek_WASUTF8
650 and key now doesn't (or vice versa) then we should change
651 the key's flag, as this is assignment. */
652 if (HvSHAREKEYS(hv)) {
653 /* Need to swap the key we have for a key with the flags we
654 need. As keys are shared we can't just write to the
655 flag, so we share the new one, unshare the old one. */
656 HEK *new_hek = share_hek_flags(key, klen, hash,
658 unshare_hek (HeKEY_hek(entry));
659 HeKEY_hek(entry) = new_hek;
662 HeKFLAGS(entry) = masked_flags;
663 if (masked_flags & HVhek_ENABLEHVKFLAGS)
666 if (HeVAL(entry) == &PL_sv_placeholder) {
667 /* yes, can store into placeholder slot */
668 if (action & HV_FETCH_LVALUE) {
670 /* This preserves behaviour with the old hv_fetch
671 implementation which at this point would bail out
672 with a break; (at "if we find a placeholder, we
673 pretend we haven't found anything")
675 That break mean that if a placeholder were found, it
676 caused a call into hv_store, which in turn would
677 check magic, and if there is no magic end up pretty
678 much back at this point (in hv_store's code). */
681 /* LVAL fetch which actaully needs a store. */
683 HvPLACEHOLDERS(hv)--;
686 if (val != &PL_sv_placeholder)
687 HvPLACEHOLDERS(hv)--;
690 } else if (action & HV_FETCH_ISSTORE) {
691 SvREFCNT_dec(HeVAL(entry));
694 } else if (HeVAL(entry) == &PL_sv_placeholder) {
695 /* if we find a placeholder, we pretend we haven't found
699 if (flags & HVhek_FREEKEY)
703 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
704 if (!(action & HV_FETCH_ISSTORE)
705 && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
707 char *env = PerlEnv_ENVgetenv_len(key,&len);
709 sv = newSVpvn(env,len);
711 return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv,
717 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
718 S_hv_notallowed(aTHX_ flags, key, klen,
719 "Attempt to access disallowed key '%"SVf"' in"
720 " a restricted hash");
722 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
723 /* Not doing some form of store, so return failure. */
724 if (flags & HVhek_FREEKEY)
728 if (action & HV_FETCH_LVALUE) {
731 /* At this point the old hv_fetch code would call to hv_store,
732 which in turn might do some tied magic. So we need to make that
733 magic check happen. */
734 /* gonna assign to this, so it better be there */
735 return hv_fetch_common(hv, keysv, key, klen, flags,
736 HV_FETCH_ISSTORE, val, hash);
737 /* XXX Surely that could leak if the fetch-was-store fails?
738 Just like the hv_fetch. */
742 /* Welcome to hv_store... */
744 if (!xhv->xhv_array) {
745 /* Not sure if we can get here. I think the only case of oentry being
746 NULL is for %ENV with dynamic env fetch. But that should disappear
747 with magic in the previous code. */
748 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
749 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
753 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
756 /* share_hek_flags will do the free for us. This might be considered
759 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
760 else /* gotta do the real thing */
761 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
763 HeNEXT(entry) = *oentry;
766 if (val == &PL_sv_placeholder)
767 HvPLACEHOLDERS(hv)++;
768 if (masked_flags & HVhek_ENABLEHVKFLAGS)
771 xhv->xhv_keys++; /* HvKEYS(hv)++ */
772 if (!n_links) { /* initial entry? */
773 xhv->xhv_fill++; /* HvFILL(hv)++ */
774 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
775 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
776 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
777 splits on a rehashed hash, as we're not going to split it again,
778 and if someone is lucky (evil) enough to get all the keys in one
779 list they could exhaust our memory as we repeatedly double the
780 number of buckets on every entry. Linear search feels a less worse
789 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
791 const MAGIC *mg = SvMAGIC(hv);
795 if (isUPPER(mg->mg_type)) {
797 switch (mg->mg_type) {
798 case PERL_MAGIC_tied:
800 *needs_store = FALSE;
803 mg = mg->mg_moremagic;
808 =for apidoc hv_scalar
810 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
816 Perl_hv_scalar(pTHX_ HV *hv)
821 if ((SvRMAGICAL(hv) && (mg = mg_find((SV*)hv, PERL_MAGIC_tied)))) {
822 sv = magic_scalarpack(hv, mg);
828 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
829 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
837 =for apidoc hv_delete
839 Deletes a key/value pair in the hash. The value SV is removed from the
840 hash and returned to the caller. The C<klen> is the length of the key.
841 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
848 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
855 k_flags |= HVhek_UTF8;
859 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
863 =for apidoc hv_delete_ent
865 Deletes a key/value pair in the hash. The value SV is removed from the
866 hash and returned to the caller. The C<flags> value will normally be zero;
867 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
868 precomputed hash value, or 0 to ask for it to be computed.
874 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
876 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
880 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
881 int k_flags, I32 d_flags, U32 hash)
887 register HE **oentry;
896 if (k_flags & HVhek_FREEKEY)
898 key = SvPV(keysv, klen);
900 is_utf8 = (SvUTF8(keysv) != 0);
902 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
905 if (SvRMAGICAL(hv)) {
908 hv_magic_check (hv, &needs_copy, &needs_store);
911 entry = hv_fetch_common(hv, keysv, key, klen,
912 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
914 sv = entry ? HeVAL(entry) : NULL;
920 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
921 /* No longer an element */
922 sv_unmagic(sv, PERL_MAGIC_tiedelem);
925 return Nullsv; /* element cannot be deleted */
927 #ifdef ENV_IS_CASELESS
928 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
929 /* XXX This code isn't UTF8 clean. */
930 keysv = sv_2mortal(newSVpvn(key,klen));
931 if (k_flags & HVhek_FREEKEY) {
934 key = strupr(SvPVX(keysv));
943 xhv = (XPVHV*)SvANY(hv);
944 if (!xhv->xhv_array /* !HvARRAY(hv) */)
948 const char *keysave = key;
949 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
952 k_flags |= HVhek_UTF8;
954 k_flags &= ~HVhek_UTF8;
955 if (key != keysave) {
956 if (k_flags & HVhek_FREEKEY) {
957 /* This shouldn't happen if our caller does what we expect,
958 but strictly the API allows it. */
961 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
963 HvHASKFLAGS_on((SV*)hv);
967 PERL_HASH_INTERNAL(hash, key, klen);
969 if (keysv && (SvIsCOW_shared_hash(keysv))) {
972 PERL_HASH(hash, key, klen);
976 masked_flags = (k_flags & HVhek_MASK);
978 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
979 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
982 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
983 if (HeHASH(entry) != hash) /* strings can't be equal */
985 if (HeKLEN(entry) != (I32)klen)
987 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
989 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
992 /* if placeholder is here, it's already been deleted.... */
993 if (HeVAL(entry) == &PL_sv_placeholder)
995 if (k_flags & HVhek_FREEKEY)
999 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1000 S_hv_notallowed(aTHX_ k_flags, key, klen,
1001 "Attempt to delete readonly key '%"SVf"' from"
1002 " a restricted hash");
1004 if (k_flags & HVhek_FREEKEY)
1007 if (d_flags & G_DISCARD)
1010 sv = sv_2mortal(HeVAL(entry));
1011 HeVAL(entry) = &PL_sv_placeholder;
1015 * If a restricted hash, rather than really deleting the entry, put
1016 * a placeholder there. This marks the key as being "approved", so
1017 * we can still access via not-really-existing key without raising
1020 if (SvREADONLY(hv)) {
1021 SvREFCNT_dec(HeVAL(entry));
1022 HeVAL(entry) = &PL_sv_placeholder;
1023 /* We'll be saving this slot, so the number of allocated keys
1024 * doesn't go down, but the number placeholders goes up */
1025 HvPLACEHOLDERS(hv)++;
1027 *oentry = HeNEXT(entry);
1029 xhv->xhv_fill--; /* HvFILL(hv)-- */
1030 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1033 hv_free_ent(hv, entry);
1034 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1035 if (xhv->xhv_keys == 0)
1036 HvHASKFLAGS_off(hv);
1040 if (SvREADONLY(hv)) {
1041 S_hv_notallowed(aTHX_ k_flags, key, klen,
1042 "Attempt to delete disallowed key '%"SVf"' from"
1043 " a restricted hash");
1046 if (k_flags & HVhek_FREEKEY)
1052 S_hsplit(pTHX_ HV *hv)
1054 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1055 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1056 register I32 newsize = oldsize * 2;
1058 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1060 register HE **oentry;
1061 int longest_chain = 0;
1064 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1065 hv, (int) oldsize);*/
1067 if (HvPLACEHOLDERS(hv) && !SvREADONLY(hv)) {
1068 /* Can make this clear any placeholders first for non-restricted hashes,
1069 even though Storable rebuilds restricted hashes by putting in all the
1070 placeholders (first) before turning on the readonly flag, because
1071 Storable always pre-splits the hash. */
1072 hv_clear_placeholders(hv);
1076 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1077 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1083 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1088 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1089 if (oldsize >= 64) {
1090 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1091 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1094 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1098 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1099 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1100 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1103 for (i=0; i<oldsize; i++,aep++) {
1104 int left_length = 0;
1105 int right_length = 0;
1109 if (!*aep) /* non-existent */
1112 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1113 if ((HeHASH(entry) & newsize) != (U32)i) {
1114 *oentry = HeNEXT(entry);
1115 HeNEXT(entry) = *bep;
1117 xhv->xhv_fill++; /* HvFILL(hv)++ */
1123 oentry = &HeNEXT(entry);
1127 if (!*aep) /* everything moved */
1128 xhv->xhv_fill--; /* HvFILL(hv)-- */
1129 /* I think we don't actually need to keep track of the longest length,
1130 merely flag if anything is too long. But for the moment while
1131 developing this code I'll track it. */
1132 if (left_length > longest_chain)
1133 longest_chain = left_length;
1134 if (right_length > longest_chain)
1135 longest_chain = right_length;
1139 /* Pick your policy for "hashing isn't working" here: */
1140 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1145 if (hv == PL_strtab) {
1146 /* Urg. Someone is doing something nasty to the string table.
1151 /* Awooga. Awooga. Pathological data. */
1152 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1153 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1156 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1157 was_shared = HvSHAREKEYS(hv);
1160 HvSHAREKEYS_off(hv);
1163 aep = (HE **) xhv->xhv_array;
1165 for (i=0; i<newsize; i++,aep++) {
1166 register HE *entry = *aep;
1168 /* We're going to trash this HE's next pointer when we chain it
1169 into the new hash below, so store where we go next. */
1170 HE *next = HeNEXT(entry);
1175 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1180 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1181 hash, HeKFLAGS(entry));
1182 unshare_hek (HeKEY_hek(entry));
1183 HeKEY_hek(entry) = new_hek;
1185 /* Not shared, so simply write the new hash in. */
1186 HeHASH(entry) = hash;
1188 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1189 HEK_REHASH_on(HeKEY_hek(entry));
1190 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1192 /* Copy oentry to the correct new chain. */
1193 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1195 xhv->xhv_fill++; /* HvFILL(hv)++ */
1196 HeNEXT(entry) = *bep;
1202 Safefree (xhv->xhv_array);
1203 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1207 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1209 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1210 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1211 register I32 newsize;
1216 register HE **oentry;
1218 newsize = (I32) newmax; /* possible truncation here */
1219 if (newsize != newmax || newmax <= oldsize)
1221 while ((newsize & (1 + ~newsize)) != newsize) {
1222 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1224 if (newsize < newmax)
1226 if (newsize < newmax)
1227 return; /* overflow detection */
1229 a = xhv->xhv_array; /* HvARRAY(hv) */
1232 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1233 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1239 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1244 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1245 if (oldsize >= 64) {
1246 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1247 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1250 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1253 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1256 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1258 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1259 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1260 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1264 for (i=0; i<oldsize; i++,aep++) {
1265 if (!*aep) /* non-existent */
1267 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1269 if ((j = (HeHASH(entry) & newsize)) != i) {
1271 *oentry = HeNEXT(entry);
1272 if (!(HeNEXT(entry) = aep[j]))
1273 xhv->xhv_fill++; /* HvFILL(hv)++ */
1278 oentry = &HeNEXT(entry);
1280 if (!*aep) /* everything moved */
1281 xhv->xhv_fill--; /* HvFILL(hv)-- */
1288 Creates a new HV. The reference count is set to 1.
1297 register XPVHV* xhv;
1299 hv = (HV*)NEWSV(502,0);
1300 sv_upgrade((SV *)hv, SVt_PVHV);
1301 xhv = (XPVHV*)SvANY(hv);
1304 #ifndef NODEFAULT_SHAREKEYS
1305 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1308 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1309 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1310 (void)hv_iterinit(hv); /* so each() will start off right */
1315 Perl_newHVhv(pTHX_ HV *ohv)
1318 STRLEN hv_max, hv_fill;
1320 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1322 hv_max = HvMAX(ohv);
1324 if (!SvMAGICAL((SV *)ohv)) {
1325 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1327 const bool shared = !!HvSHAREKEYS(ohv);
1328 HE **ents, **oents = (HE **)HvARRAY(ohv);
1330 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1333 /* In each bucket... */
1334 for (i = 0; i <= hv_max; i++) {
1335 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1342 /* Copy the linked list of entries. */
1343 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1344 const U32 hash = HeHASH(oent);
1345 const char * const key = HeKEY(oent);
1346 const STRLEN len = HeKLEN(oent);
1347 const int flags = HeKFLAGS(oent);
1350 HeVAL(ent) = newSVsv(HeVAL(oent));
1352 = shared ? share_hek_flags(key, len, hash, flags)
1353 : save_hek_flags(key, len, hash, flags);
1364 HvFILL(hv) = hv_fill;
1365 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1369 /* Iterate over ohv, copying keys and values one at a time. */
1371 const I32 riter = HvRITER(ohv);
1372 HE * const eiter = HvEITER(ohv);
1374 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1375 while (hv_max && hv_max + 1 >= hv_fill * 2)
1376 hv_max = hv_max / 2;
1380 while ((entry = hv_iternext_flags(ohv, 0))) {
1381 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1382 newSVsv(HeVAL(entry)), HeHASH(entry),
1385 HvRITER(ohv) = riter;
1386 HvEITER(ohv) = eiter;
1393 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1400 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1401 PL_sub_generation++; /* may be deletion of method from stash */
1403 if (HeKLEN(entry) == HEf_SVKEY) {
1404 SvREFCNT_dec(HeKEY_sv(entry));
1405 Safefree(HeKEY_hek(entry));
1407 else if (HvSHAREKEYS(hv))
1408 unshare_hek(HeKEY_hek(entry));
1410 Safefree(HeKEY_hek(entry));
1415 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1419 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1420 PL_sub_generation++; /* may be deletion of method from stash */
1421 sv_2mortal(HeVAL(entry)); /* free between statements */
1422 if (HeKLEN(entry) == HEf_SVKEY) {
1423 sv_2mortal(HeKEY_sv(entry));
1424 Safefree(HeKEY_hek(entry));
1426 else if (HvSHAREKEYS(hv))
1427 unshare_hek(HeKEY_hek(entry));
1429 Safefree(HeKEY_hek(entry));
1434 =for apidoc hv_clear
1436 Clears a hash, making it empty.
1442 Perl_hv_clear(pTHX_ HV *hv)
1445 register XPVHV* xhv;
1449 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1451 xhv = (XPVHV*)SvANY(hv);
1453 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1454 /* restricted hash: convert all keys to placeholders */
1456 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1457 HE *entry = ((HE**)xhv->xhv_array)[i];
1458 for (; entry; entry = HeNEXT(entry)) {
1459 /* not already placeholder */
1460 if (HeVAL(entry) != &PL_sv_placeholder) {
1461 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1462 SV* keysv = hv_iterkeysv(entry);
1464 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1467 SvREFCNT_dec(HeVAL(entry));
1468 HeVAL(entry) = &PL_sv_placeholder;
1469 HvPLACEHOLDERS(hv)++;
1477 HvPLACEHOLDERS_set(hv, 0);
1478 if (xhv->xhv_array /* HvARRAY(hv) */)
1479 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1480 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1485 HvHASKFLAGS_off(hv);
1492 =for apidoc hv_clear_placeholders
1494 Clears any placeholders from a hash. If a restricted hash has any of its keys
1495 marked as readonly and the key is subsequently deleted, the key is not actually
1496 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1497 it so it will be ignored by future operations such as iterating over the hash,
1498 but will still allow the hash to have a value reassigned to the key at some
1499 future point. This function clears any such placeholder keys from the hash.
1500 See Hash::Util::lock_keys() for an example of its use.
1506 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1509 I32 items = (I32)HvPLACEHOLDERS(hv);
1516 /* Loop down the linked list heads */
1518 HE **oentry = &(HvARRAY(hv))[i];
1519 HE *entry = *oentry;
1524 for (; entry; entry = *oentry) {
1525 if (HeVAL(entry) == &PL_sv_placeholder) {
1526 *oentry = HeNEXT(entry);
1527 if (first && !*oentry)
1528 HvFILL(hv)--; /* This linked list is now empty. */
1532 hv_free_ent(hv, entry);
1536 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS(hv);
1537 if (HvKEYS(hv) == 0)
1538 HvHASKFLAGS_off(hv);
1539 HvPLACEHOLDERS(hv) = 0;
1543 oentry = &HeNEXT(entry);
1548 /* You can't get here, hence assertion should always fail. */
1549 assert (items == 0);
1554 S_hfreeentries(pTHX_ HV *hv)
1556 register HE **array;
1568 array = HvARRAY(hv);
1569 /* make everyone else think the array is empty, so that the destructors
1570 * called for freed entries can't recusively mess with us */
1571 HvARRAY(hv) = Null(HE**);
1573 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1578 register HE *oentry = entry;
1579 entry = HeNEXT(entry);
1580 hv_free_ent(hv, oentry);
1585 entry = array[riter];
1588 HvARRAY(hv) = array;
1589 (void)hv_iterinit(hv);
1593 =for apidoc hv_undef
1601 Perl_hv_undef(pTHX_ HV *hv)
1603 register XPVHV* xhv;
1606 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1607 xhv = (XPVHV*)SvANY(hv);
1609 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1612 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
1613 Safefree(HvNAME(hv));
1616 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1617 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1618 HvPLACEHOLDERS_set(hv, 0);
1625 =for apidoc hv_iterinit
1627 Prepares a starting point to traverse a hash table. Returns the number of
1628 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1629 currently only meaningful for hashes without tie magic.
1631 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1632 hash buckets that happen to be in use. If you still need that esoteric
1633 value, you can get it through the macro C<HvFILL(tb)>.
1640 Perl_hv_iterinit(pTHX_ HV *hv)
1642 register XPVHV* xhv;
1646 Perl_croak(aTHX_ "Bad hash");
1647 xhv = (XPVHV*)SvANY(hv);
1648 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1649 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1651 hv_free_ent(hv, entry);
1653 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1654 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1655 /* used to be xhv->xhv_fill before 5.004_65 */
1656 return XHvTOTALKEYS(xhv);
1659 =for apidoc hv_iternext
1661 Returns entries from a hash iterator. See C<hv_iterinit>.
1663 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1664 iterator currently points to, without losing your place or invalidating your
1665 iterator. Note that in this case the current entry is deleted from the hash
1666 with your iterator holding the last reference to it. Your iterator is flagged
1667 to free the entry on the next call to C<hv_iternext>, so you must not discard
1668 your iterator immediately else the entry will leak - call C<hv_iternext> to
1669 trigger the resource deallocation.
1675 Perl_hv_iternext(pTHX_ HV *hv)
1677 return hv_iternext_flags(hv, 0);
1681 =for apidoc hv_iternext_flags
1683 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1684 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1685 set the placeholders keys (for restricted hashes) will be returned in addition
1686 to normal keys. By default placeholders are automatically skipped over.
1687 Currently a placeholder is implemented with a value that is
1688 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1689 restricted hashes may change, and the implementation currently is
1690 insufficiently abstracted for any change to be tidy.
1696 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1699 register XPVHV* xhv;
1705 Perl_croak(aTHX_ "Bad hash");
1706 xhv = (XPVHV*)SvANY(hv);
1707 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1709 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1710 SV *key = sv_newmortal();
1712 sv_setsv(key, HeSVKEY_force(entry));
1713 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1719 /* one HE per MAGICAL hash */
1720 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1722 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1724 HeKEY_hek(entry) = hek;
1725 HeKLEN(entry) = HEf_SVKEY;
1727 magic_nextpack((SV*) hv,mg,key);
1729 /* force key to stay around until next time */
1730 HeSVKEY_set(entry, SvREFCNT_inc(key));
1731 return entry; /* beware, hent_val is not set */
1734 SvREFCNT_dec(HeVAL(entry));
1735 Safefree(HeKEY_hek(entry));
1737 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1740 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1741 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1745 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1746 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1747 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1749 /* At start of hash, entry is NULL. */
1752 entry = HeNEXT(entry);
1753 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1755 * Skip past any placeholders -- don't want to include them in
1758 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1759 entry = HeNEXT(entry);
1764 /* OK. Come to the end of the current list. Grab the next one. */
1766 xhv->xhv_riter++; /* HvRITER(hv)++ */
1767 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1768 /* There is no next one. End of the hash. */
1769 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1772 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1773 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1775 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1776 /* If we have an entry, but it's a placeholder, don't count it.
1778 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1779 entry = HeNEXT(entry);
1781 /* Will loop again if this linked list starts NULL
1782 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1783 or if we run through it and find only placeholders. */
1786 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1788 hv_free_ent(hv, oldentry);
1791 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1792 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1794 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1799 =for apidoc hv_iterkey
1801 Returns the key from the current position of the hash iterator. See
1808 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1810 if (HeKLEN(entry) == HEf_SVKEY) {
1812 char *p = SvPV(HeKEY_sv(entry), len);
1817 *retlen = HeKLEN(entry);
1818 return HeKEY(entry);
1822 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1824 =for apidoc hv_iterkeysv
1826 Returns the key as an C<SV*> from the current position of the hash
1827 iterator. The return value will always be a mortal copy of the key. Also
1834 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1836 if (HeKLEN(entry) != HEf_SVKEY) {
1837 HEK *hek = HeKEY_hek(entry);
1838 const int flags = HEK_FLAGS(hek);
1841 if (flags & HVhek_WASUTF8) {
1843 Andreas would like keys he put in as utf8 to come back as utf8
1845 STRLEN utf8_len = HEK_LEN(hek);
1846 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
1848 sv = newSVpvn ((char*)as_utf8, utf8_len);
1850 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
1851 } else if (flags & HVhek_REHASH) {
1852 /* We don't have a pointer to the hv, so we have to replicate the
1853 flag into every HEK. This hv is using custom a hasing
1854 algorithm. Hence we can't return a shared string scalar, as
1855 that would contain the (wrong) hash value, and might get passed
1856 into an hv routine with a regular hash */
1858 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1862 sv = newSVpvn_share(HEK_KEY(hek),
1863 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1866 return sv_2mortal(sv);
1868 return sv_mortalcopy(HeKEY_sv(entry));
1872 =for apidoc hv_iterval
1874 Returns the value from the current position of the hash iterator. See
1881 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1883 if (SvRMAGICAL(hv)) {
1884 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1885 SV* sv = sv_newmortal();
1886 if (HeKLEN(entry) == HEf_SVKEY)
1887 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1889 mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1893 return HeVAL(entry);
1897 =for apidoc hv_iternextsv
1899 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1906 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1909 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1911 *key = hv_iterkey(he, retlen);
1912 return hv_iterval(hv, he);
1916 =for apidoc hv_magic
1918 Adds magic to a hash. See C<sv_magic>.
1924 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1926 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1929 #if 0 /* use the macro from hv.h instead */
1932 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1934 return HEK_KEY(share_hek(sv, len, hash));
1939 /* possibly free a shared string if no one has access to it
1940 * len and hash must both be valid for str.
1943 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1945 unshare_hek_or_pvn (NULL, str, len, hash);
1950 Perl_unshare_hek(pTHX_ HEK *hek)
1952 unshare_hek_or_pvn(hek, NULL, 0, 0);
1955 /* possibly free a shared string if no one has access to it
1956 hek if non-NULL takes priority over the other 3, else str, len and hash
1957 are used. If so, len and hash must both be valid for str.
1960 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1962 register XPVHV* xhv;
1964 register HE **oentry;
1967 bool is_utf8 = FALSE;
1969 const char *save = str;
1972 hash = HEK_HASH(hek);
1973 } else if (len < 0) {
1974 STRLEN tmplen = -len;
1976 /* See the note in hv_fetch(). --jhi */
1977 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1980 k_flags = HVhek_UTF8;
1982 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1985 /* what follows is the moral equivalent of:
1986 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1987 if (--*Svp == Nullsv)
1988 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1990 xhv = (XPVHV*)SvANY(PL_strtab);
1991 /* assert(xhv_array != 0) */
1993 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1994 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1996 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1997 if (HeKEY_hek(entry) != hek)
2003 const int flags_masked = k_flags & HVhek_MASK;
2004 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
2005 if (HeHASH(entry) != hash) /* strings can't be equal */
2007 if (HeKLEN(entry) != len)
2009 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2011 if (HeKFLAGS(entry) != flags_masked)
2019 if (--HeVAL(entry) == Nullsv) {
2020 *oentry = HeNEXT(entry);
2022 xhv->xhv_fill--; /* HvFILL(hv)-- */
2023 Safefree(HeKEY_hek(entry));
2025 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2029 UNLOCK_STRTAB_MUTEX;
2030 if (!found && ckWARN_d(WARN_INTERNAL))
2031 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2032 "Attempt to free non-existent shared string '%s'%s"
2034 hek ? HEK_KEY(hek) : str,
2035 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2036 if (k_flags & HVhek_FREEKEY)
2040 /* get a (constant) string ptr from the global string table
2041 * string will get added if it is not already there.
2042 * len and hash must both be valid for str.
2045 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2047 bool is_utf8 = FALSE;
2049 const char *save = str;
2052 STRLEN tmplen = -len;
2054 /* See the note in hv_fetch(). --jhi */
2055 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2057 /* If we were able to downgrade here, then than means that we were passed
2058 in a key which only had chars 0-255, but was utf8 encoded. */
2061 /* If we found we were able to downgrade the string to bytes, then
2062 we should flag that it needs upgrading on keys or each. Also flag
2063 that we need share_hek_flags to free the string. */
2065 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2068 return share_hek_flags (str, len, hash, flags);
2072 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2074 register XPVHV* xhv;
2076 register HE **oentry;
2079 const int flags_masked = flags & HVhek_MASK;
2081 /* what follows is the moral equivalent of:
2083 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2084 hv_store(PL_strtab, str, len, Nullsv, hash);
2086 Can't rehash the shared string table, so not sure if it's worth
2087 counting the number of entries in the linked list
2089 xhv = (XPVHV*)SvANY(PL_strtab);
2090 /* assert(xhv_array != 0) */
2092 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2093 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2094 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
2095 if (HeHASH(entry) != hash) /* strings can't be equal */
2097 if (HeKLEN(entry) != len)
2099 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2101 if (HeKFLAGS(entry) != flags_masked)
2108 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags_masked);
2109 HeVAL(entry) = Nullsv;
2110 HeNEXT(entry) = *oentry;
2112 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2113 if (i) { /* initial entry? */
2114 xhv->xhv_fill++; /* HvFILL(hv)++ */
2115 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2120 ++HeVAL(entry); /* use value slot as REFCNT */
2121 UNLOCK_STRTAB_MUTEX;
2123 if (flags & HVhek_FREEKEY)
2126 return HeKEY_hek(entry);
2130 Perl_hv_placeholders_p(pTHX_ HV *hv)
2133 MAGIC *mg = mg_find((SV*)hv, PERL_MAGIC_rhash);
2136 mg = sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, 0);
2139 Perl_die(aTHX_ "panic: hv_placeholders_p");
2142 return &(mg->mg_len);
2147 Perl_hv_placeholders_get(pTHX_ HV *hv)
2150 MAGIC *mg = mg_find((SV*)hv, PERL_MAGIC_rhash);
2152 return mg ? mg->mg_len : 0;
2156 Perl_hv_placeholders_set(pTHX_ HV *hv, IV ph)
2159 MAGIC *mg = mg_find((SV*)hv, PERL_MAGIC_rhash);
2164 if (!sv_magicext((SV*)hv, 0, PERL_MAGIC_rhash, 0, 0, ph))
2165 Perl_die(aTHX_ "panic: hv_placeholders_set");
2167 /* else we don't need to add magic to record 0 placeholders. */
2171 =for apidoc hv_assert
2173 Check that a hash is in an internally consistent state.
2179 Perl_hv_assert(pTHX_ HV *hv)
2184 int placeholders = 0;
2187 const I32 riter = HvRITER(hv);
2188 HE *eiter = HvEITER(hv);
2190 (void)hv_iterinit(hv);
2192 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2193 /* sanity check the values */
2194 if (HeVAL(entry) == &PL_sv_placeholder) {
2199 /* sanity check the keys */
2200 if (HeSVKEY(entry)) {
2201 /* Don't know what to check on SV keys. */
2202 } else if (HeKUTF8(entry)) {
2204 if (HeKWASUTF8(entry)) {
2205 PerlIO_printf(Perl_debug_log,
2206 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2207 (int) HeKLEN(entry), HeKEY(entry));
2210 } else if (HeKWASUTF8(entry)) {
2214 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2215 if (HvUSEDKEYS(hv) != real) {
2216 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2217 (int) real, (int) HvUSEDKEYS(hv));
2220 if (HvPLACEHOLDERS(hv) != placeholders) {
2221 PerlIO_printf(Perl_debug_log,
2222 "Count %d placeholder(s), but hash reports %d\n",
2223 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2227 if (withflags && ! HvHASKFLAGS(hv)) {
2228 PerlIO_printf(Perl_debug_log,
2229 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2236 HvRITER(hv) = riter; /* Restore hash iterator state */
2237 HvEITER(hv) = eiter;
2242 * c-indentation-style: bsd
2244 * indent-tabs-mode: t
2247 * ex: set ts=8 sts=4 sw=4 noet: