3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000,
4 * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008 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
13 * of all that I have seen.
16 * [p.278 of _The Lord of the Rings_, II/iii: "The Ring Goes South"]
20 =head1 Hash Manipulation Functions
22 A HV structure represents a Perl hash. It consists mainly of an array
23 of pointers, each of which points to a linked list of HE structures. The
24 array is indexed by the hash function of the key, so each linked list
25 represents all the hash entries with the same hash value. Each HE contains
26 a pointer to the actual value, plus a pointer to a HEK structure which
27 holds the key and hash value.
35 #define PERL_HASH_INTERNAL_ACCESS
38 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
40 static const char S_strtab_error[]
41 = "Cannot modify shared string table in hv_%s";
45 #define new_HE() (HE*)safemalloc(sizeof(HE))
46 #define del_HE(p) safefree((char*)p)
55 void ** const root = &PL_body_roots[HE_SVSLOT];
58 Perl_more_bodies(aTHX_ HE_SVSLOT, sizeof(HE), PERL_ARENA_SIZE);
65 #define new_HE() new_he()
68 HeNEXT(p) = (HE*)(PL_body_roots[HE_SVSLOT]); \
69 PL_body_roots[HE_SVSLOT] = p; \
77 S_save_hek_flags(const char *str, I32 len, U32 hash, int flags)
79 const int flags_masked = flags & HVhek_MASK;
83 PERL_ARGS_ASSERT_SAVE_HEK_FLAGS;
85 Newx(k, HEK_BASESIZE + len + 2, char);
87 Copy(str, HEK_KEY(hek), len, char);
88 HEK_KEY(hek)[len] = 0;
91 HEK_FLAGS(hek) = (unsigned char)flags_masked | HVhek_UNSHARED;
93 if (flags & HVhek_FREEKEY)
98 /* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent
102 Perl_free_tied_hv_pool(pTHX)
105 HE *he = PL_hv_fetch_ent_mh;
108 Safefree(HeKEY_hek(he));
112 PL_hv_fetch_ent_mh = NULL;
115 #if defined(USE_ITHREADS)
117 Perl_hek_dup(pTHX_ HEK *source, CLONE_PARAMS* param)
121 PERL_ARGS_ASSERT_HEK_DUP;
122 PERL_UNUSED_ARG(param);
127 shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
129 /* We already shared this hash key. */
130 (void)share_hek_hek(shared);
134 = share_hek_flags(HEK_KEY(source), HEK_LEN(source),
135 HEK_HASH(source), HEK_FLAGS(source));
136 ptr_table_store(PL_ptr_table, source, shared);
142 Perl_he_dup(pTHX_ const HE *e, bool shared, CLONE_PARAMS* param)
146 PERL_ARGS_ASSERT_HE_DUP;
150 /* look for it in the table first */
151 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
155 /* create anew and remember what it is */
157 ptr_table_store(PL_ptr_table, e, ret);
159 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
160 if (HeKLEN(e) == HEf_SVKEY) {
162 Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
163 HeKEY_hek(ret) = (HEK*)k;
164 HeKEY_sv(ret) = sv_dup_inc(HeKEY_sv(e), param);
167 /* This is hek_dup inlined, which seems to be important for speed
169 HEK * const source = HeKEY_hek(e);
170 HEK *shared = (HEK*)ptr_table_fetch(PL_ptr_table, source);
173 /* We already shared this hash key. */
174 (void)share_hek_hek(shared);
178 = share_hek_flags(HEK_KEY(source), HEK_LEN(source),
179 HEK_HASH(source), HEK_FLAGS(source));
180 ptr_table_store(PL_ptr_table, source, shared);
182 HeKEY_hek(ret) = shared;
185 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
187 HeVAL(ret) = sv_dup_inc(HeVAL(e), param);
190 #endif /* USE_ITHREADS */
193 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
196 SV * const sv = sv_newmortal();
198 PERL_ARGS_ASSERT_HV_NOTALLOWED;
200 if (!(flags & HVhek_FREEKEY)) {
201 sv_setpvn(sv, key, klen);
204 /* Need to free saved eventually assign to mortal SV */
205 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
206 sv_usepvn(sv, (char *) key, klen);
208 if (flags & HVhek_UTF8) {
211 Perl_croak(aTHX_ msg, SVfARG(sv));
214 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
220 Stores an SV in a hash. The hash key is specified as C<key> and the
221 absolute value of C<klen> is the length of the key. If C<klen> is
222 negative the key is assumed to be in UTF-8-encoded Unicode. The
223 C<hash> parameter is the precomputed hash value; if it is zero then
224 Perl will compute it.
226 The return value will be
227 NULL if the operation failed or if the value did not need to be actually
228 stored within the hash (as in the case of tied hashes). Otherwise it can
229 be dereferenced to get the original C<SV*>. Note that the caller is
230 responsible for suitably incrementing the reference count of C<val> before
231 the call, and decrementing it if the function returned NULL. Effectively
232 a successful hv_store takes ownership of one reference to C<val>. This is
233 usually what you want; a newly created SV has a reference count of one, so
234 if all your code does is create SVs then store them in a hash, hv_store
235 will own the only reference to the new SV, and your code doesn't need to do
236 anything further to tidy up. hv_store is not implemented as a call to
237 hv_store_ent, and does not create a temporary SV for the key, so if your
238 key data is not already in SV form then use hv_store in preference to
241 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
242 information on how to use this function on tied hashes.
244 =for apidoc hv_store_ent
246 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
247 parameter is the precomputed hash value; if it is zero then Perl will
248 compute it. The return value is the new hash entry so created. It will be
249 NULL if the operation failed or if the value did not need to be actually
250 stored within the hash (as in the case of tied hashes). Otherwise the
251 contents of the return value can be accessed using the C<He?> macros
252 described here. Note that the caller is responsible for suitably
253 incrementing the reference count of C<val> before the call, and
254 decrementing it if the function returned NULL. Effectively a successful
255 hv_store_ent takes ownership of one reference to C<val>. This is
256 usually what you want; a newly created SV has a reference count of one, so
257 if all your code does is create SVs then store them in a hash, hv_store
258 will own the only reference to the new SV, and your code doesn't need to do
259 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
260 unlike C<val> it does not take ownership of it, so maintaining the correct
261 reference count on C<key> is entirely the caller's responsibility. hv_store
262 is not implemented as a call to hv_store_ent, and does not create a temporary
263 SV for the key, so if your key data is not already in SV form then use
264 hv_store in preference to hv_store_ent.
266 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
267 information on how to use this function on tied hashes.
269 =for apidoc hv_exists
271 Returns a boolean indicating whether the specified hash key exists. The
272 absolute value of C<klen> is the length of the key. If C<klen> is
273 negative the key is assumed to be in UTF-8-encoded Unicode.
277 Returns the SV which corresponds to the specified key in the hash.
278 The absolute value of C<klen> is the length of the key. If C<klen> is
279 negative the key is assumed to be in UTF-8-encoded Unicode. If
280 C<lval> is set then the fetch will be part of a store. Check that the
281 return value is non-null before dereferencing it to an C<SV*>.
283 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
284 information on how to use this function on tied hashes.
286 =for apidoc hv_exists_ent
288 Returns a boolean indicating whether the specified hash key exists. C<hash>
289 can be a valid precomputed hash value, or 0 to ask for it to be
295 /* returns an HE * structure with the all fields set */
296 /* note that hent_val will be a mortal sv for MAGICAL hashes */
298 =for apidoc hv_fetch_ent
300 Returns the hash entry which corresponds to the specified key in the hash.
301 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
302 if you want the function to compute it. IF C<lval> is set then the fetch
303 will be part of a store. Make sure the return value is non-null before
304 accessing it. The return value when C<hv> is a tied hash is a pointer to a
305 static location, so be sure to make a copy of the structure if you need to
308 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
309 information on how to use this function on tied hashes.
314 /* Common code for hv_delete()/hv_exists()/hv_fetch()/hv_store() */
316 Perl_hv_common_key_len(pTHX_ HV *hv, const char *key, I32 klen_i32,
317 const int action, SV *val, const U32 hash)
322 PERL_ARGS_ASSERT_HV_COMMON_KEY_LEN;
331 return hv_common(hv, NULL, key, klen, flags, action, val, hash);
335 Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
336 int flags, int action, SV *val, register U32 hash)
345 const int return_svp = action & HV_FETCH_JUST_SV;
349 if (SvTYPE(hv) == (svtype)SVTYPEMASK)
352 assert(SvTYPE(hv) == SVt_PVHV);
354 if (SvSMAGICAL(hv) && SvGMAGICAL(hv) && !(action & HV_DISABLE_UVAR_XKEY)) {
356 if ((mg = mg_find((const SV *)hv, PERL_MAGIC_uvar))) {
357 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
358 if (uf->uf_set == NULL) {
359 SV* obj = mg->mg_obj;
362 keysv = newSVpvn_flags(key, klen, SVs_TEMP |
363 ((flags & HVhek_UTF8)
367 mg->mg_obj = keysv; /* pass key */
368 uf->uf_index = action; /* pass action */
369 magic_getuvar(MUTABLE_SV(hv), mg);
370 keysv = mg->mg_obj; /* may have changed */
373 /* If the key may have changed, then we need to invalidate
374 any passed-in computed hash value. */
380 if (flags & HVhek_FREEKEY)
382 key = SvPV_const(keysv, klen);
383 is_utf8 = (SvUTF8(keysv) != 0);
384 if (SvIsCOW_shared_hash(keysv)) {
385 flags = HVhek_KEYCANONICAL | (is_utf8 ? HVhek_UTF8 : 0);
390 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
393 if (action & HV_DELETE) {
394 return (void *) hv_delete_common(hv, keysv, key, klen,
395 flags | (is_utf8 ? HVhek_UTF8 : 0),
399 xhv = (XPVHV*)SvANY(hv);
401 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) {
402 if (mg_find((const SV *)hv, PERL_MAGIC_tied)
403 || SvGMAGICAL((const SV *)hv))
405 /* FIXME should be able to skimp on the HE/HEK here when
406 HV_FETCH_JUST_SV is true. */
408 keysv = newSVpvn_utf8(key, klen, is_utf8);
410 keysv = newSVsv(keysv);
413 mg_copy(MUTABLE_SV(hv), sv, (char *)keysv, HEf_SVKEY);
415 /* grab a fake HE/HEK pair from the pool or make a new one */
416 entry = PL_hv_fetch_ent_mh;
418 PL_hv_fetch_ent_mh = HeNEXT(entry);
422 Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
423 HeKEY_hek(entry) = (HEK*)k;
425 HeNEXT(entry) = NULL;
426 HeSVKEY_set(entry, keysv);
428 sv_upgrade(sv, SVt_PVLV);
430 /* so we can free entry when freeing sv */
431 LvTARG(sv) = MUTABLE_SV(entry);
433 /* XXX remove at some point? */
434 if (flags & HVhek_FREEKEY)
438 return entry ? (void *) &HeVAL(entry) : NULL;
440 return (void *) entry;
442 #ifdef ENV_IS_CASELESS
443 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
445 for (i = 0; i < klen; ++i)
446 if (isLOWER(key[i])) {
447 /* Would be nice if we had a routine to do the
448 copy and upercase in a single pass through. */
449 const char * const nkey = strupr(savepvn(key,klen));
450 /* Note that this fetch is for nkey (the uppercased
451 key) whereas the store is for key (the original) */
452 void *result = hv_common(hv, NULL, nkey, klen,
453 HVhek_FREEKEY, /* free nkey */
454 0 /* non-LVAL fetch */
455 | HV_DISABLE_UVAR_XKEY
458 0 /* compute hash */);
459 if (!result && (action & HV_FETCH_LVALUE)) {
460 /* This call will free key if necessary.
461 Do it this way to encourage compiler to tail
463 result = hv_common(hv, keysv, key, klen, flags,
465 | HV_DISABLE_UVAR_XKEY
469 if (flags & HVhek_FREEKEY)
477 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
478 if (mg_find((const SV *)hv, PERL_MAGIC_tied)
479 || SvGMAGICAL((const SV *)hv)) {
480 /* I don't understand why hv_exists_ent has svret and sv,
481 whereas hv_exists only had one. */
482 SV * const svret = sv_newmortal();
485 if (keysv || is_utf8) {
487 keysv = newSVpvn_utf8(key, klen, TRUE);
489 keysv = newSVsv(keysv);
491 mg_copy(MUTABLE_SV(hv), sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
493 mg_copy(MUTABLE_SV(hv), sv, key, klen);
495 if (flags & HVhek_FREEKEY)
497 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
498 /* This cast somewhat evil, but I'm merely using NULL/
499 not NULL to return the boolean exists.
500 And I know hv is not NULL. */
501 return SvTRUE(svret) ? (void *)hv : NULL;
503 #ifdef ENV_IS_CASELESS
504 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
505 /* XXX This code isn't UTF8 clean. */
506 char * const keysave = (char * const)key;
507 /* Will need to free this, so set FREEKEY flag. */
508 key = savepvn(key,klen);
509 key = (const char*)strupr((char*)key);
514 if (flags & HVhek_FREEKEY) {
517 flags |= HVhek_FREEKEY;
521 else if (action & HV_FETCH_ISSTORE) {
524 hv_magic_check (hv, &needs_copy, &needs_store);
526 const bool save_taint = PL_tainted;
527 if (keysv || is_utf8) {
529 keysv = newSVpvn_utf8(key, klen, TRUE);
532 PL_tainted = SvTAINTED(keysv);
533 keysv = sv_2mortal(newSVsv(keysv));
534 mg_copy(MUTABLE_SV(hv), val, (char*)keysv, HEf_SVKEY);
536 mg_copy(MUTABLE_SV(hv), val, key, klen);
539 TAINT_IF(save_taint);
541 if (flags & HVhek_FREEKEY)
545 #ifdef ENV_IS_CASELESS
546 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
547 /* XXX This code isn't UTF8 clean. */
548 const char *keysave = key;
549 /* Will need to free this, so set FREEKEY flag. */
550 key = savepvn(key,klen);
551 key = (const char*)strupr((char*)key);
556 if (flags & HVhek_FREEKEY) {
559 flags |= HVhek_FREEKEY;
567 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
568 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
569 || (SvRMAGICAL((const SV *)hv)
570 && mg_find((const SV *)hv, PERL_MAGIC_env))
575 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
577 HvARRAY(hv) = (HE**)array;
579 #ifdef DYNAMIC_ENV_FETCH
580 else if (action & HV_FETCH_ISEXISTS) {
581 /* for an %ENV exists, if we do an insert it's by a recursive
582 store call, so avoid creating HvARRAY(hv) right now. */
586 /* XXX remove at some point? */
587 if (flags & HVhek_FREEKEY)
594 if (is_utf8 & !(flags & HVhek_KEYCANONICAL)) {
595 char * const keysave = (char *)key;
596 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
600 flags &= ~HVhek_UTF8;
601 if (key != keysave) {
602 if (flags & HVhek_FREEKEY)
604 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
605 /* If the caller calculated a hash, it was on the sequence of
606 octets that are the UTF-8 form. We've now changed the sequence
607 of octets stored to that of the equivalent byte representation,
608 so the hash we need is different. */
613 if (HvREHASH(hv) || (!hash && !(keysv && (SvIsCOW_shared_hash(keysv)))))
614 PERL_HASH_INTERNAL_(hash, key, klen, HvREHASH(hv));
616 hash = SvSHARED_HASH(keysv);
618 /* We don't have a pointer to the hv, so we have to replicate the
619 flag into every HEK, so that hv_iterkeysv can see it.
620 And yes, you do need this even though you are not "storing" because
621 you can flip the flags below if doing an lval lookup. (And that
622 was put in to give the semantics Andreas was expecting.) */
624 flags |= HVhek_REHASH;
626 masked_flags = (flags & HVhek_MASK);
628 #ifdef DYNAMIC_ENV_FETCH
629 if (!HvARRAY(hv)) entry = NULL;
633 entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)];
635 for (; entry; entry = HeNEXT(entry)) {
636 if (HeHASH(entry) != hash) /* strings can't be equal */
638 if (HeKLEN(entry) != (I32)klen)
640 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
642 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
645 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
646 if (HeKFLAGS(entry) != masked_flags) {
647 /* We match if HVhek_UTF8 bit in our flags and hash key's
648 match. But if entry was set previously with HVhek_WASUTF8
649 and key now doesn't (or vice versa) then we should change
650 the key's flag, as this is assignment. */
651 if (HvSHAREKEYS(hv)) {
652 /* Need to swap the key we have for a key with the flags we
653 need. As keys are shared we can't just write to the
654 flag, so we share the new one, unshare the old one. */
655 HEK * const new_hek = share_hek_flags(key, klen, hash,
657 unshare_hek (HeKEY_hek(entry));
658 HeKEY_hek(entry) = new_hek;
660 else if (hv == PL_strtab) {
661 /* PL_strtab is usually the only hash without HvSHAREKEYS,
662 so putting this test here is cheap */
663 if (flags & HVhek_FREEKEY)
665 Perl_croak(aTHX_ S_strtab_error,
666 action & HV_FETCH_LVALUE ? "fetch" : "store");
669 HeKFLAGS(entry) = masked_flags;
670 if (masked_flags & HVhek_ENABLEHVKFLAGS)
673 if (HeVAL(entry) == &PL_sv_placeholder) {
674 /* yes, can store into placeholder slot */
675 if (action & HV_FETCH_LVALUE) {
677 /* This preserves behaviour with the old hv_fetch
678 implementation which at this point would bail out
679 with a break; (at "if we find a placeholder, we
680 pretend we haven't found anything")
682 That break mean that if a placeholder were found, it
683 caused a call into hv_store, which in turn would
684 check magic, and if there is no magic end up pretty
685 much back at this point (in hv_store's code). */
688 /* LVAL fetch which actually needs a store. */
690 HvPLACEHOLDERS(hv)--;
693 if (val != &PL_sv_placeholder)
694 HvPLACEHOLDERS(hv)--;
697 } else if (action & HV_FETCH_ISSTORE) {
698 SvREFCNT_dec(HeVAL(entry));
701 } else if (HeVAL(entry) == &PL_sv_placeholder) {
702 /* if we find a placeholder, we pretend we haven't found
706 if (flags & HVhek_FREEKEY)
709 return entry ? (void *) &HeVAL(entry) : NULL;
713 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
714 if (!(action & HV_FETCH_ISSTORE)
715 && SvRMAGICAL((const SV *)hv)
716 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
718 const char * const env = PerlEnv_ENVgetenv_len(key,&len);
720 sv = newSVpvn(env,len);
722 return hv_common(hv, keysv, key, klen, flags,
723 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
729 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
730 hv_notallowed(flags, key, klen,
731 "Attempt to access disallowed key '%"SVf"' in"
732 " a restricted hash");
734 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
735 /* Not doing some form of store, so return failure. */
736 if (flags & HVhek_FREEKEY)
740 if (action & HV_FETCH_LVALUE) {
741 val = action & HV_FETCH_EMPTY_HE ? NULL : newSV(0);
743 /* At this point the old hv_fetch code would call to hv_store,
744 which in turn might do some tied magic. So we need to make that
745 magic check happen. */
746 /* gonna assign to this, so it better be there */
747 /* If a fetch-as-store fails on the fetch, then the action is to
748 recurse once into "hv_store". If we didn't do this, then that
749 recursive call would call the key conversion routine again.
750 However, as we replace the original key with the converted
751 key, this would result in a double conversion, which would show
752 up as a bug if the conversion routine is not idempotent. */
753 return hv_common(hv, keysv, key, klen, flags,
754 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
756 /* XXX Surely that could leak if the fetch-was-store fails?
757 Just like the hv_fetch. */
761 /* Welcome to hv_store... */
764 /* Not sure if we can get here. I think the only case of oentry being
765 NULL is for %ENV with dynamic env fetch. But that should disappear
766 with magic in the previous code. */
769 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
771 HvARRAY(hv) = (HE**)array;
774 oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max];
777 /* share_hek_flags will do the free for us. This might be considered
780 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
781 else if (hv == PL_strtab) {
782 /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting
783 this test here is cheap */
784 if (flags & HVhek_FREEKEY)
786 Perl_croak(aTHX_ S_strtab_error,
787 action & HV_FETCH_LVALUE ? "fetch" : "store");
789 else /* gotta do the real thing */
790 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
792 HeNEXT(entry) = *oentry;
795 if (val == &PL_sv_placeholder)
796 HvPLACEHOLDERS(hv)++;
797 if (masked_flags & HVhek_ENABLEHVKFLAGS)
801 const HE *counter = HeNEXT(entry);
803 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
804 if (!counter) { /* initial entry? */
805 } else if (xhv->xhv_keys > xhv->xhv_max) {
806 /* Use only the old HvUSEDKEYS(hv) > HvMAX(hv) condition to limit
807 bucket splits on a rehashed hash, as we're not going to
808 split it again, and if someone is lucky (evil) enough to
809 get all the keys in one list they could exhaust our memory
810 as we repeatedly double the number of buckets on every
811 entry. Linear search feels a less worse thing to do. */
813 } else if(!HvREHASH(hv)) {
816 while ((counter = HeNEXT(counter)))
819 if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) {
826 return entry ? (void *) &HeVAL(entry) : NULL;
828 return (void *) entry;
832 S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store)
834 const MAGIC *mg = SvMAGIC(hv);
836 PERL_ARGS_ASSERT_HV_MAGIC_CHECK;
841 if (isUPPER(mg->mg_type)) {
843 if (mg->mg_type == PERL_MAGIC_tied) {
844 *needs_store = FALSE;
845 return; /* We've set all there is to set. */
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)
865 PERL_ARGS_ASSERT_HV_SCALAR;
867 if (SvRMAGICAL(hv)) {
868 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_tied);
870 return magic_scalarpack(hv, mg);
874 if (HvTOTALKEYS((const HV *)hv))
875 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
876 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
884 =for apidoc hv_delete
886 Deletes a key/value pair in the hash. The value's SV is removed from
887 the hash, made mortal, and returned to the caller. The absolute
888 value of C<klen> is the length of the key. If C<klen> is negative the
889 key is assumed to be in UTF-8-encoded Unicode. The C<flags> value
890 will normally be zero; if set to G_DISCARD then NULL will be returned.
891 NULL will also be returned if the key is not found.
893 =for apidoc hv_delete_ent
895 Deletes a key/value pair in the hash. The value SV is removed from the hash,
896 made mortal, and returned to the caller. The C<flags> value will normally be
897 zero; if set to G_DISCARD then NULL will be returned. NULL will also be
898 returned if the key is not found. C<hash> can be a valid precomputed hash
899 value, or 0 to ask for it to be computed.
905 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
906 int k_flags, I32 d_flags, U32 hash)
911 register HE **oentry;
912 bool is_utf8 = (k_flags & HVhek_UTF8) ? TRUE : FALSE;
915 if (SvRMAGICAL(hv)) {
918 hv_magic_check (hv, &needs_copy, &needs_store);
922 entry = (HE *) hv_common(hv, keysv, key, klen,
923 k_flags & ~HVhek_FREEKEY,
924 HV_FETCH_LVALUE|HV_DISABLE_UVAR_XKEY,
926 sv = entry ? HeVAL(entry) : NULL;
932 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
933 /* No longer an element */
934 sv_unmagic(sv, PERL_MAGIC_tiedelem);
937 return NULL; /* element cannot be deleted */
939 #ifdef ENV_IS_CASELESS
940 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
941 /* XXX This code isn't UTF8 clean. */
942 keysv = newSVpvn_flags(key, klen, SVs_TEMP);
943 if (k_flags & HVhek_FREEKEY) {
946 key = strupr(SvPVX(keysv));
955 xhv = (XPVHV*)SvANY(hv);
960 const char * const keysave = key;
961 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
964 k_flags |= HVhek_UTF8;
966 k_flags &= ~HVhek_UTF8;
967 if (key != keysave) {
968 if (k_flags & HVhek_FREEKEY) {
969 /* This shouldn't happen if our caller does what we expect,
970 but strictly the API allows it. */
973 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
975 HvHASKFLAGS_on(MUTABLE_SV(hv));
978 if (HvREHASH(hv) || (!hash && !(keysv && (SvIsCOW_shared_hash(keysv)))))
979 PERL_HASH_INTERNAL_(hash, key, klen, HvREHASH(hv));
981 hash = SvSHARED_HASH(keysv);
983 masked_flags = (k_flags & HVhek_MASK);
985 oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)];
987 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) {
989 U8 mro_changes = 0; /* 1 = isa; 2 = package moved */
993 if (HeHASH(entry) != hash) /* strings can't be equal */
995 if (HeKLEN(entry) != (I32)klen)
997 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
999 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1002 if (hv == PL_strtab) {
1003 if (k_flags & HVhek_FREEKEY)
1005 Perl_croak(aTHX_ S_strtab_error, "delete");
1008 /* if placeholder is here, it's already been deleted.... */
1009 if (HeVAL(entry) == &PL_sv_placeholder) {
1010 if (k_flags & HVhek_FREEKEY)
1014 if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))
1015 && !SvIsCOW(HeVAL(entry))) {
1016 hv_notallowed(k_flags, key, klen,
1017 "Attempt to delete readonly key '%"SVf"' from"
1018 " a restricted hash");
1020 if (k_flags & HVhek_FREEKEY)
1023 /* If this is a stash and the key ends with ::, then someone is
1024 * deleting a package.
1026 if (HeVAL(entry) && HvENAME_get(hv)) {
1027 gv = (GV *)HeVAL(entry);
1028 if (keysv) key = SvPV(keysv, klen);
1030 (klen > 1 && key[klen-2] == ':' && key[klen-1] == ':')
1032 (klen == 1 && key[0] == ':')
1034 && (klen != 6 || hv!=PL_defstash || memNE(key,"main::",6))
1035 && SvTYPE(gv) == SVt_PVGV && (stash = GvHV((GV *)gv))
1036 && HvENAME_get(stash)) {
1037 /* A previous version of this code checked that the
1038 * GV was still in the symbol table by fetching the
1039 * GV with its name. That is not necessary (and
1040 * sometimes incorrect), as HvENAME cannot be set
1041 * on hv if it is not in the symtab. */
1043 /* Hang on to it for a bit. */
1044 SvREFCNT_inc_simple_void_NN(
1045 sv_2mortal((SV *)gv)
1048 else if (klen == 3 && strnEQ(key, "ISA", 3))
1052 if (d_flags & G_DISCARD) {
1054 HeVAL(entry) = &PL_sv_placeholder;
1056 /* deletion of method from stash */
1057 if (isGV(sv) && isGV_with_GP(sv) && GvCVu(sv)
1059 mro_method_changed_in(hv);
1065 sv = sv_2mortal(HeVAL(entry));
1066 HeVAL(entry) = &PL_sv_placeholder;
1070 * If a restricted hash, rather than really deleting the entry, put
1071 * a placeholder there. This marks the key as being "approved", so
1072 * we can still access via not-really-existing key without raising
1076 /* We'll be saving this slot, so the number of allocated keys
1077 * doesn't go down, but the number placeholders goes up */
1078 HvPLACEHOLDERS(hv)++;
1080 *oentry = HeNEXT(entry);
1081 if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */)
1084 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1085 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1086 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
1087 hv_free_ent(hv, entry);
1089 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
1090 if (xhv->xhv_keys == 0)
1091 HvHASKFLAGS_off(hv);
1094 if (mro_changes == 1) mro_isa_changed_in(hv);
1095 else if (mro_changes == 2)
1096 mro_package_moved(NULL, stash, gv, 1);
1100 if (SvREADONLY(hv)) {
1101 hv_notallowed(k_flags, key, klen,
1102 "Attempt to delete disallowed key '%"SVf"' from"
1103 " a restricted hash");
1106 if (k_flags & HVhek_FREEKEY)
1112 S_hsplit(pTHX_ HV *hv)
1115 register XPVHV* const xhv = (XPVHV*)SvANY(hv);
1116 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1117 register I32 newsize = oldsize * 2;
1119 char *a = (char*) HvARRAY(hv);
1121 int longest_chain = 0;
1124 PERL_ARGS_ASSERT_HSPLIT;
1126 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1127 (void*)hv, (int) oldsize);*/
1129 if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) {
1130 /* Can make this clear any placeholders first for non-restricted hashes,
1131 even though Storable rebuilds restricted hashes by putting in all the
1132 placeholders (first) before turning on the readonly flag, because
1133 Storable always pre-splits the hash. */
1134 hv_clear_placeholders(hv);
1138 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1139 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1140 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1146 Move(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1149 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1150 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1155 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1157 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1159 Safefree(HvARRAY(hv));
1163 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1164 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1165 HvARRAY(hv) = (HE**) a;
1168 for (i=0; i<oldsize; i++,aep++) {
1169 int left_length = 0;
1170 int right_length = 0;
1175 if (!entry) /* non-existent */
1179 if ((HeHASH(entry) & newsize) != (U32)i) {
1180 *oentry = HeNEXT(entry);
1181 HeNEXT(entry) = *bep;
1186 oentry = &HeNEXT(entry);
1191 /* I think we don't actually need to keep track of the longest length,
1192 merely flag if anything is too long. But for the moment while
1193 developing this code I'll track it. */
1194 if (left_length > longest_chain)
1195 longest_chain = left_length;
1196 if (right_length > longest_chain)
1197 longest_chain = right_length;
1201 /* Pick your policy for "hashing isn't working" here: */
1202 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1207 if (hv == PL_strtab) {
1208 /* Urg. Someone is doing something nasty to the string table.
1213 /* Awooga. Awooga. Pathological data. */
1214 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", (void*)hv,
1215 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1218 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1219 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1221 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1224 was_shared = HvSHAREKEYS(hv);
1226 HvSHAREKEYS_off(hv);
1231 for (i=0; i<newsize; i++,aep++) {
1232 register HE *entry = *aep;
1234 /* We're going to trash this HE's next pointer when we chain it
1235 into the new hash below, so store where we go next. */
1236 HE * const next = HeNEXT(entry);
1241 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1246 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1247 hash, HeKFLAGS(entry));
1248 unshare_hek (HeKEY_hek(entry));
1249 HeKEY_hek(entry) = new_hek;
1251 /* Not shared, so simply write the new hash in. */
1252 HeHASH(entry) = hash;
1254 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1255 HEK_REHASH_on(HeKEY_hek(entry));
1256 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1258 /* Copy oentry to the correct new chain. */
1259 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1260 HeNEXT(entry) = *bep;
1266 Safefree (HvARRAY(hv));
1267 HvARRAY(hv) = (HE **)a;
1271 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1274 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1275 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1276 register I32 newsize;
1281 PERL_ARGS_ASSERT_HV_KSPLIT;
1283 newsize = (I32) newmax; /* possible truncation here */
1284 if (newsize != newmax || newmax <= oldsize)
1286 while ((newsize & (1 + ~newsize)) != newsize) {
1287 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1289 if (newsize < newmax)
1291 if (newsize < newmax)
1292 return; /* overflow detection */
1294 a = (char *) HvARRAY(hv);
1297 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1298 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1299 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1305 Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1308 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1309 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1314 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1316 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1318 Safefree(HvARRAY(hv));
1321 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1324 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1326 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1327 HvARRAY(hv) = (HE **) a;
1328 if (!xhv->xhv_keys /* !HvTOTALKEYS(hv) */) /* skip rest if no entries */
1332 for (i=0; i<oldsize; i++,aep++) {
1336 if (!entry) /* non-existent */
1339 register I32 j = (HeHASH(entry) & newsize);
1343 *oentry = HeNEXT(entry);
1344 HeNEXT(entry) = aep[j];
1348 oentry = &HeNEXT(entry);
1355 Perl_newHVhv(pTHX_ HV *ohv)
1358 HV * const hv = newHV();
1361 if (!ohv || !HvTOTALKEYS(ohv))
1363 hv_max = HvMAX(ohv);
1365 if (!SvMAGICAL((const SV *)ohv)) {
1366 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1368 const bool shared = !!HvSHAREKEYS(ohv);
1369 HE **ents, ** const oents = (HE **)HvARRAY(ohv);
1371 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1374 /* In each bucket... */
1375 for (i = 0; i <= hv_max; i++) {
1377 HE *oent = oents[i];
1384 /* Copy the linked list of entries. */
1385 for (; oent; oent = HeNEXT(oent)) {
1386 const U32 hash = HeHASH(oent);
1387 const char * const key = HeKEY(oent);
1388 const STRLEN len = HeKLEN(oent);
1389 const int flags = HeKFLAGS(oent);
1390 HE * const ent = new_HE();
1391 SV *const val = HeVAL(oent);
1393 HeVAL(ent) = SvIMMORTAL(val) ? val : newSVsv(val);
1395 = shared ? share_hek_flags(key, len, hash, flags)
1396 : save_hek_flags(key, len, hash, flags);
1407 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1411 /* Iterate over ohv, copying keys and values one at a time. */
1413 const I32 riter = HvRITER_get(ohv);
1414 HE * const eiter = HvEITER_get(ohv);
1415 STRLEN hv_fill = HvFILL(ohv);
1417 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1418 while (hv_max && hv_max + 1 >= hv_fill * 2)
1419 hv_max = hv_max / 2;
1423 while ((entry = hv_iternext_flags(ohv, 0))) {
1424 SV *const val = HeVAL(entry);
1425 (void)hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1426 SvIMMORTAL(val) ? val : newSVsv(val),
1427 HeHASH(entry), HeKFLAGS(entry));
1429 HvRITER_set(ohv, riter);
1430 HvEITER_set(ohv, eiter);
1437 =for apidoc Am|HV *|hv_copy_hints_hv|HV *ohv
1439 A specialised version of L</newHVhv> for copying C<%^H>. I<ohv> must be
1440 a pointer to a hash (which may have C<%^H> magic, but should be generally
1441 non-magical), or C<NULL> (interpreted as an empty hash). The content
1442 of I<ohv> is copied to a new hash, which has the C<%^H>-specific magic
1443 added to it. A pointer to the new hash is returned.
1449 Perl_hv_copy_hints_hv(pTHX_ HV *const ohv)
1451 HV * const hv = newHV();
1454 STRLEN hv_max = HvMAX(ohv);
1455 STRLEN hv_fill = HvFILL(ohv);
1457 const I32 riter = HvRITER_get(ohv);
1458 HE * const eiter = HvEITER_get(ohv);
1460 while (hv_max && hv_max + 1 >= hv_fill * 2)
1461 hv_max = hv_max / 2;
1465 while ((entry = hv_iternext_flags(ohv, 0))) {
1466 SV *const sv = newSVsv(hv_iterval(ohv,entry));
1467 SV *heksv = HeSVKEY(entry);
1468 if (!heksv && sv) heksv = newSVhek(HeKEY_hek(entry));
1469 if (sv) sv_magic(sv, NULL, PERL_MAGIC_hintselem,
1470 (char *)heksv, HEf_SVKEY);
1471 if (heksv == HeSVKEY(entry))
1472 (void)hv_store_ent(hv, heksv, sv, 0);
1474 (void)hv_common(hv, heksv, HeKEY(entry), HeKLEN(entry),
1475 HeKFLAGS(entry), HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv, HeHASH(entry));
1476 SvREFCNT_dec(heksv);
1479 HvRITER_set(ohv, riter);
1480 HvEITER_set(ohv, eiter);
1482 hv_magic(hv, NULL, PERL_MAGIC_hints);
1486 /* like hv_free_ent, but returns the SV rather than freeing it */
1488 S_hv_free_ent_ret(pTHX_ HV *hv, register HE *entry)
1493 PERL_ARGS_ASSERT_HV_FREE_ENT_RET;
1498 if (val && isGV(val) && isGV_with_GP(val) && GvCVu(val) && HvENAME(hv))
1499 mro_method_changed_in(hv); /* deletion of method from stash */
1500 if (HeKLEN(entry) == HEf_SVKEY) {
1501 SvREFCNT_dec(HeKEY_sv(entry));
1502 Safefree(HeKEY_hek(entry));
1504 else if (HvSHAREKEYS(hv))
1505 unshare_hek(HeKEY_hek(entry));
1507 Safefree(HeKEY_hek(entry));
1514 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1519 PERL_ARGS_ASSERT_HV_FREE_ENT;
1523 val = hv_free_ent_ret(hv, entry);
1529 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1533 PERL_ARGS_ASSERT_HV_DELAYFREE_ENT;
1537 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */
1538 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */
1539 if (HeKLEN(entry) == HEf_SVKEY) {
1540 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry)));
1542 hv_free_ent(hv, entry);
1546 =for apidoc hv_clear
1548 Frees the all the elements of a hash, leaving it empty.
1549 The XS equivalent of %hash = (). See also L</hv_undef>.
1555 Perl_hv_clear(pTHX_ HV *hv)
1558 register XPVHV* xhv;
1562 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1564 xhv = (XPVHV*)SvANY(hv);
1566 if (SvREADONLY(hv) && HvARRAY(hv) != NULL) {
1567 /* restricted hash: convert all keys to placeholders */
1569 for (i = 0; i <= xhv->xhv_max; i++) {
1570 HE *entry = (HvARRAY(hv))[i];
1571 for (; entry; entry = HeNEXT(entry)) {
1572 /* not already placeholder */
1573 if (HeVAL(entry) != &PL_sv_placeholder) {
1574 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))
1575 && !SvIsCOW(HeVAL(entry))) {
1576 SV* const keysv = hv_iterkeysv(entry);
1578 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1581 SvREFCNT_dec(HeVAL(entry));
1582 HeVAL(entry) = &PL_sv_placeholder;
1583 HvPLACEHOLDERS(hv)++;
1590 HvPLACEHOLDERS_set(hv, 0);
1593 mg_clear(MUTABLE_SV(hv));
1595 HvHASKFLAGS_off(hv);
1600 mro_isa_changed_in(hv);
1601 HvEITER_set(hv, NULL);
1606 =for apidoc hv_clear_placeholders
1608 Clears any placeholders from a hash. If a restricted hash has any of its keys
1609 marked as readonly and the key is subsequently deleted, the key is not actually
1610 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1611 it so it will be ignored by future operations such as iterating over the hash,
1612 but will still allow the hash to have a value reassigned to the key at some
1613 future point. This function clears any such placeholder keys from the hash.
1614 See Hash::Util::lock_keys() for an example of its use.
1620 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1623 const U32 items = (U32)HvPLACEHOLDERS_get(hv);
1625 PERL_ARGS_ASSERT_HV_CLEAR_PLACEHOLDERS;
1628 clear_placeholders(hv, items);
1632 S_clear_placeholders(pTHX_ HV *hv, U32 items)
1637 PERL_ARGS_ASSERT_CLEAR_PLACEHOLDERS;
1644 /* Loop down the linked list heads */
1645 HE **oentry = &(HvARRAY(hv))[i];
1648 while ((entry = *oentry)) {
1649 if (HeVAL(entry) == &PL_sv_placeholder) {
1650 *oentry = HeNEXT(entry);
1651 if (entry == HvEITER_get(hv))
1654 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1655 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1656 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
1657 hv_free_ent(hv, entry);
1662 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv);
1663 if (HvUSEDKEYS(hv) == 0)
1664 HvHASKFLAGS_off(hv);
1665 HvPLACEHOLDERS_set(hv, 0);
1669 oentry = &HeNEXT(entry);
1673 /* You can't get here, hence assertion should always fail. */
1674 assert (items == 0);
1679 S_hfreeentries(pTHX_ HV *hv)
1682 XPVHV * const xhv = (XPVHV*)SvANY(hv);
1685 PERL_ARGS_ASSERT_HFREEENTRIES;
1687 while ((sv = Perl_hfree_next_entry(aTHX_ hv, &index))||xhv->xhv_keys) {
1693 /* hfree_next_entry()
1694 * For use only by S_hfreeentries() and sv_clear().
1695 * Delete the next available HE from hv and return the associated SV.
1696 * Returns null on empty hash. Nevertheless null is not a reliable
1697 * indicator that the hash is empty, as the deleted entry may have a
1699 * indexp is a pointer to the current index into HvARRAY. The index should
1700 * initially be set to 0. hfree_next_entry() may update it. */
1703 Perl_hfree_next_entry(pTHX_ HV *hv, STRLEN *indexp)
1705 struct xpvhv_aux *iter;
1709 STRLEN orig_index = *indexp;
1712 PERL_ARGS_ASSERT_HFREE_NEXT_ENTRY;
1714 if (SvOOK(hv) && ((iter = HvAUX(hv)))
1715 && ((entry = iter->xhv_eiter)) )
1717 /* the iterator may get resurrected after each
1718 * destructor call, so check each time */
1719 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1721 hv_free_ent(hv, entry);
1722 /* warning: at this point HvARRAY may have been
1723 * re-allocated, HvMAX changed etc */
1725 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1726 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1729 if (!((XPVHV*)SvANY(hv))->xhv_keys)
1732 array = HvARRAY(hv);
1734 while ( ! ((entry = array[*indexp])) ) {
1735 if ((*indexp)++ >= HvMAX(hv))
1737 assert(*indexp != orig_index);
1739 array[*indexp] = HeNEXT(entry);
1740 ((XPVHV*) SvANY(hv))->xhv_keys--;
1742 if ( PL_phase != PERL_PHASE_DESTRUCT && HvENAME(hv)
1743 && HeVAL(entry) && isGV(HeVAL(entry))
1744 && GvHV(HeVAL(entry)) && HvENAME(GvHV(HeVAL(entry)))
1747 const char * const key = HePV(entry,klen);
1748 if ((klen > 1 && key[klen-1]==':' && key[klen-2]==':')
1749 || (klen == 1 && key[0] == ':')) {
1751 NULL, GvHV(HeVAL(entry)),
1752 (GV *)HeVAL(entry), 0
1756 return hv_free_ent_ret(hv, entry);
1761 =for apidoc hv_undef
1763 Undefines the hash. The XS equivalent of undef(%hash).
1765 As well as freeing all the elements of the hash (like hv_clear()), this
1766 also frees any auxiliary data and storage associated with the hash.
1767 See also L</hv_clear>.
1773 Perl_hv_undef_flags(pTHX_ HV *hv, U32 flags)
1776 register XPVHV* xhv;
1781 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1782 xhv = (XPVHV*)SvANY(hv);
1784 /* The name must be deleted before the call to hfreeeeentries so that
1785 CVs are anonymised properly. But the effective name must be pre-
1786 served until after that call (and only deleted afterwards if the
1787 call originated from sv_clear). For stashes with one name that is
1788 both the canonical name and the effective name, hv_name_set has to
1789 allocate an array for storing the effective name. We can skip that
1790 during global destruction, as it does not matter where the CVs point
1791 if they will be freed anyway. */
1792 /* note that the code following prior to hfreeentries is duplicated
1793 * in sv_clear(), and changes here should be done there too */
1794 if (PL_phase != PERL_PHASE_DESTRUCT && (name = HvNAME(hv))) {
1796 (void)hv_delete(PL_stashcache, name,
1797 HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv),
1800 hv_name_set(hv, NULL, 0, 0);
1804 struct xpvhv_aux * const aux = HvAUX(hv);
1805 struct mro_meta *meta;
1807 if ((name = HvENAME_get(hv))) {
1808 if (PL_phase != PERL_PHASE_DESTRUCT)
1809 mro_isa_changed_in(hv);
1812 PL_stashcache, name,
1813 HEK_UTF8(HvENAME_HEK(hv)) ? -HvENAMELEN_get(hv) : HvENAMELEN_get(hv),
1818 /* If this call originated from sv_clear, then we must check for
1819 * effective names that need freeing, as well as the usual name. */
1821 if (flags & HV_NAME_SETALL ? !!aux->xhv_name_u.xhvnameu_name : !!name) {
1822 if (name && PL_stashcache)
1823 (void)hv_delete(PL_stashcache, name, (HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv)), G_DISCARD);
1824 hv_name_set(hv, NULL, 0, flags);
1826 if((meta = aux->xhv_mro_meta)) {
1827 if (meta->mro_linear_all) {
1828 SvREFCNT_dec(MUTABLE_SV(meta->mro_linear_all));
1829 meta->mro_linear_all = NULL;
1830 /* This is just acting as a shortcut pointer. */
1831 meta->mro_linear_current = NULL;
1832 } else if (meta->mro_linear_current) {
1833 /* Only the current MRO is stored, so this owns the data.
1835 SvREFCNT_dec(meta->mro_linear_current);
1836 meta->mro_linear_current = NULL;
1838 SvREFCNT_dec(meta->mro_nextmethod);
1839 SvREFCNT_dec(meta->isa);
1841 aux->xhv_mro_meta = NULL;
1843 if (!aux->xhv_name_u.xhvnameu_name && ! aux->xhv_backreferences)
1844 SvFLAGS(hv) &= ~SVf_OOK;
1847 Safefree(HvARRAY(hv));
1848 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1851 HvPLACEHOLDERS_set(hv, 0);
1854 mg_clear(MUTABLE_SV(hv));
1860 Returns the number of hash buckets that happen to be in use. This function is
1861 wrapped by the macro C<HvFILL>.
1863 Previously this value was stored in the HV structure, rather than being
1864 calculated on demand.
1870 Perl_hv_fill(pTHX_ HV const *const hv)
1873 HE **ents = HvARRAY(hv);
1875 PERL_ARGS_ASSERT_HV_FILL;
1878 HE *const *const last = ents + HvMAX(hv);
1879 count = last + 1 - ents;
1884 } while (++ents <= last);
1889 static struct xpvhv_aux*
1890 S_hv_auxinit(HV *hv) {
1891 struct xpvhv_aux *iter;
1894 PERL_ARGS_ASSERT_HV_AUXINIT;
1897 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1898 + sizeof(struct xpvhv_aux), char);
1900 array = (char *) HvARRAY(hv);
1901 Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1902 + sizeof(struct xpvhv_aux), char);
1904 HvARRAY(hv) = (HE**) array;
1908 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1909 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1910 iter->xhv_name_u.xhvnameu_name = 0;
1911 iter->xhv_name_count = 0;
1912 iter->xhv_backreferences = 0;
1913 iter->xhv_mro_meta = NULL;
1918 =for apidoc hv_iterinit
1920 Prepares a starting point to traverse a hash table. Returns the number of
1921 keys in the hash (i.e. the same as C<HvUSEDKEYS(hv)>). The return value is
1922 currently only meaningful for hashes without tie magic.
1924 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1925 hash buckets that happen to be in use. If you still need that esoteric
1926 value, you can get it through the macro C<HvFILL(hv)>.
1933 Perl_hv_iterinit(pTHX_ HV *hv)
1935 PERL_ARGS_ASSERT_HV_ITERINIT;
1937 /* FIXME: Are we not NULL, or do we croak? Place bets now! */
1940 Perl_croak(aTHX_ "Bad hash");
1943 struct xpvhv_aux * const iter = HvAUX(hv);
1944 HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */
1945 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1947 hv_free_ent(hv, entry);
1949 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1950 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1955 /* used to be xhv->xhv_fill before 5.004_65 */
1956 return HvTOTALKEYS(hv);
1960 Perl_hv_riter_p(pTHX_ HV *hv) {
1961 struct xpvhv_aux *iter;
1963 PERL_ARGS_ASSERT_HV_RITER_P;
1966 Perl_croak(aTHX_ "Bad hash");
1968 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1969 return &(iter->xhv_riter);
1973 Perl_hv_eiter_p(pTHX_ HV *hv) {
1974 struct xpvhv_aux *iter;
1976 PERL_ARGS_ASSERT_HV_EITER_P;
1979 Perl_croak(aTHX_ "Bad hash");
1981 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1982 return &(iter->xhv_eiter);
1986 Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) {
1987 struct xpvhv_aux *iter;
1989 PERL_ARGS_ASSERT_HV_RITER_SET;
1992 Perl_croak(aTHX_ "Bad hash");
2000 iter = hv_auxinit(hv);
2002 iter->xhv_riter = riter;
2006 Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) {
2007 struct xpvhv_aux *iter;
2009 PERL_ARGS_ASSERT_HV_EITER_SET;
2012 Perl_croak(aTHX_ "Bad hash");
2017 /* 0 is the default so don't go malloc()ing a new structure just to
2022 iter = hv_auxinit(hv);
2024 iter->xhv_eiter = eiter;
2028 Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2031 struct xpvhv_aux *iter;
2035 PERL_ARGS_ASSERT_HV_NAME_SET;
2038 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2042 if (iter->xhv_name_u.xhvnameu_name) {
2043 if(iter->xhv_name_count) {
2044 if(flags & HV_NAME_SETALL) {
2045 HEK ** const name = HvAUX(hv)->xhv_name_u.xhvnameu_names;
2046 HEK **hekp = name + (
2047 iter->xhv_name_count < 0
2048 ? -iter->xhv_name_count
2049 : iter->xhv_name_count
2051 while(hekp-- > name+1)
2052 unshare_hek_or_pvn(*hekp, 0, 0, 0);
2053 /* The first elem may be null. */
2054 if(*name) unshare_hek_or_pvn(*name, 0, 0, 0);
2056 spot = &iter->xhv_name_u.xhvnameu_name;
2057 iter->xhv_name_count = 0;
2060 if(iter->xhv_name_count > 0) {
2061 /* shift some things over */
2063 iter->xhv_name_u.xhvnameu_names, iter->xhv_name_count + 1, HEK *
2065 spot = iter->xhv_name_u.xhvnameu_names;
2066 spot[iter->xhv_name_count] = spot[1];
2068 iter->xhv_name_count = -(iter->xhv_name_count + 1);
2070 else if(*(spot = iter->xhv_name_u.xhvnameu_names)) {
2071 unshare_hek_or_pvn(*spot, 0, 0, 0);
2075 else if (flags & HV_NAME_SETALL) {
2076 unshare_hek_or_pvn(iter->xhv_name_u.xhvnameu_name, 0, 0, 0);
2077 spot = &iter->xhv_name_u.xhvnameu_name;
2080 HEK * const existing_name = iter->xhv_name_u.xhvnameu_name;
2081 Newx(iter->xhv_name_u.xhvnameu_names, 2, HEK *);
2082 iter->xhv_name_count = -2;
2083 spot = iter->xhv_name_u.xhvnameu_names;
2084 spot[1] = existing_name;
2087 else { spot = &iter->xhv_name_u.xhvnameu_name; iter->xhv_name_count = 0; }
2092 iter = hv_auxinit(hv);
2093 spot = &iter->xhv_name_u.xhvnameu_name;
2095 PERL_HASH(hash, name, len);
2096 *spot = name ? share_hek(name, flags & SVf_UTF8 ? -(I32)len : (I32)len, hash) : NULL;
2100 This is basically sv_eq_flags() in sv.c, but we avoid the magic
2105 hek_eq_pvn_flags(pTHX_ const HEK *hek, const char* pv, const I32 pvlen, const U32 flags) {
2106 if ( (HEK_UTF8(hek) ? 1 : 0) != (flags & SVf_UTF8 ? 1 : 0) ) {
2107 if (flags & SVf_UTF8)
2108 return (bytes_cmp_utf8(
2109 (const U8*)HEK_KEY(hek), HEK_LEN(hek),
2110 (const U8*)pv, pvlen) == 0);
2112 return (bytes_cmp_utf8(
2113 (const U8*)pv, pvlen,
2114 (const U8*)HEK_KEY(hek), HEK_LEN(hek)) == 0);
2117 return HEK_LEN(hek) == pvlen && ((HEK_KEY(hek) == pv)
2118 || memEQ(HEK_KEY(hek), pv, pvlen));
2122 =for apidoc hv_ename_add
2124 Adds a name to a stash's internal list of effective names. See
2127 This is called when a stash is assigned to a new location in the symbol
2134 Perl_hv_ename_add(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2137 struct xpvhv_aux *aux = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2140 PERL_ARGS_ASSERT_HV_ENAME_ADD;
2143 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2145 PERL_HASH(hash, name, len);
2147 if (aux->xhv_name_count) {
2148 HEK ** const xhv_name = aux->xhv_name_u.xhvnameu_names;
2149 I32 count = aux->xhv_name_count;
2150 HEK **hekp = xhv_name + (count < 0 ? -count : count);
2151 while (hekp-- > xhv_name)
2153 (HEK_UTF8(*hekp) || (flags & SVf_UTF8))
2154 ? hek_eq_pvn_flags(aTHX_ *hekp, name, (I32)len, flags)
2155 : (HEK_LEN(*hekp) == (I32)len && memEQ(HEK_KEY(*hekp), name, len))
2157 if (hekp == xhv_name && count < 0)
2158 aux->xhv_name_count = -count;
2161 if (count < 0) aux->xhv_name_count--, count = -count;
2162 else aux->xhv_name_count++;
2163 Renew(aux->xhv_name_u.xhvnameu_names, count + 1, HEK *);
2164 (aux->xhv_name_u.xhvnameu_names)[count] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
2167 HEK *existing_name = aux->xhv_name_u.xhvnameu_name;
2170 (HEK_UTF8(existing_name) || (flags & SVf_UTF8))
2171 ? hek_eq_pvn_flags(aTHX_ existing_name, name, (I32)len, flags)
2172 : (HEK_LEN(existing_name) == (I32)len && memEQ(HEK_KEY(existing_name), name, len))
2175 Newx(aux->xhv_name_u.xhvnameu_names, 2, HEK *);
2176 aux->xhv_name_count = existing_name ? 2 : -2;
2177 *aux->xhv_name_u.xhvnameu_names = existing_name;
2178 (aux->xhv_name_u.xhvnameu_names)[1] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
2183 =for apidoc hv_ename_delete
2185 Removes a name from a stash's internal list of effective names. If this is
2186 the name returned by C<HvENAME>, then another name in the list will take
2187 its place (C<HvENAME> will use it).
2189 This is called when a stash is deleted from the symbol table.
2195 Perl_hv_ename_delete(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2198 struct xpvhv_aux *aux;
2200 PERL_ARGS_ASSERT_HV_ENAME_DELETE;
2203 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2205 if (!SvOOK(hv)) return;
2208 if (!aux->xhv_name_u.xhvnameu_name) return;
2210 if (aux->xhv_name_count) {
2211 HEK ** const namep = aux->xhv_name_u.xhvnameu_names;
2212 I32 const count = aux->xhv_name_count;
2213 HEK **victim = namep + (count < 0 ? -count : count);
2214 while (victim-- > namep + 1)
2216 (HEK_UTF8(*victim) || (flags & SVf_UTF8))
2217 ? hek_eq_pvn_flags(aTHX_ *victim, name, (I32)len, flags)
2218 : (HEK_LEN(*victim) == (I32)len && memEQ(HEK_KEY(*victim), name, len))
2220 unshare_hek_or_pvn(*victim, 0, 0, 0);
2221 if (count < 0) ++aux->xhv_name_count;
2222 else --aux->xhv_name_count;
2224 (aux->xhv_name_count == 1 || aux->xhv_name_count == -1)
2226 ) { /* if there are none left */
2228 aux->xhv_name_u.xhvnameu_names = NULL;
2229 aux->xhv_name_count = 0;
2232 /* Move the last one back to fill the empty slot. It
2233 does not matter what order they are in. */
2234 *victim = *(namep + (count < 0 ? -count : count) - 1);
2239 count > 0 && (HEK_UTF8(*namep) || (flags & SVf_UTF8))
2240 ? hek_eq_pvn_flags(aTHX_ *namep, name, (I32)len, flags)
2241 : (HEK_LEN(*namep) == (I32)len && memEQ(HEK_KEY(*namep), name, len))
2243 aux->xhv_name_count = -count;
2247 (HEK_UTF8(aux->xhv_name_u.xhvnameu_name) || (flags & SVf_UTF8))
2248 ? hek_eq_pvn_flags(aTHX_ aux->xhv_name_u.xhvnameu_name, name, (I32)len, flags)
2249 : (HEK_LEN(aux->xhv_name_u.xhvnameu_name) == (I32)len &&
2250 memEQ(HEK_KEY(aux->xhv_name_u.xhvnameu_name), name, len))
2252 HEK * const namehek = aux->xhv_name_u.xhvnameu_name;
2253 Newx(aux->xhv_name_u.xhvnameu_names, 1, HEK *);
2254 *aux->xhv_name_u.xhvnameu_names = namehek;
2255 aux->xhv_name_count = -1;
2260 Perl_hv_backreferences_p(pTHX_ HV *hv) {
2261 struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2263 PERL_ARGS_ASSERT_HV_BACKREFERENCES_P;
2264 PERL_UNUSED_CONTEXT;
2266 return &(iter->xhv_backreferences);
2270 Perl_hv_kill_backrefs(pTHX_ HV *hv) {
2273 PERL_ARGS_ASSERT_HV_KILL_BACKREFS;
2278 av = HvAUX(hv)->xhv_backreferences;
2281 HvAUX(hv)->xhv_backreferences = 0;
2282 Perl_sv_kill_backrefs(aTHX_ MUTABLE_SV(hv), av);
2283 if (SvTYPE(av) == SVt_PVAV)
2289 hv_iternext is implemented as a macro in hv.h
2291 =for apidoc hv_iternext
2293 Returns entries from a hash iterator. See C<hv_iterinit>.
2295 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
2296 iterator currently points to, without losing your place or invalidating your
2297 iterator. Note that in this case the current entry is deleted from the hash
2298 with your iterator holding the last reference to it. Your iterator is flagged
2299 to free the entry on the next call to C<hv_iternext>, so you must not discard
2300 your iterator immediately else the entry will leak - call C<hv_iternext> to
2301 trigger the resource deallocation.
2303 =for apidoc hv_iternext_flags
2305 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
2306 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
2307 set the placeholders keys (for restricted hashes) will be returned in addition
2308 to normal keys. By default placeholders are automatically skipped over.
2309 Currently a placeholder is implemented with a value that is
2310 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
2311 restricted hashes may change, and the implementation currently is
2312 insufficiently abstracted for any change to be tidy.
2318 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2321 register XPVHV* xhv;
2325 struct xpvhv_aux *iter;
2327 PERL_ARGS_ASSERT_HV_ITERNEXT_FLAGS;
2330 Perl_croak(aTHX_ "Bad hash");
2332 xhv = (XPVHV*)SvANY(hv);
2335 /* Too many things (well, pp_each at least) merrily assume that you can
2336 call iv_iternext without calling hv_iterinit, so we'll have to deal
2342 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2343 if (SvMAGICAL(hv) && SvRMAGICAL(hv)) {
2344 if ( ( mg = mg_find((const SV *)hv, PERL_MAGIC_tied) ) ) {
2345 SV * const key = sv_newmortal();
2347 sv_setsv(key, HeSVKEY_force(entry));
2348 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
2354 /* one HE per MAGICAL hash */
2355 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
2357 Newxz(k, HEK_BASESIZE + sizeof(const SV *), char);
2359 HeKEY_hek(entry) = hek;
2360 HeKLEN(entry) = HEf_SVKEY;
2362 magic_nextpack(MUTABLE_SV(hv),mg,key);
2364 /* force key to stay around until next time */
2365 HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key));
2366 return entry; /* beware, hent_val is not set */
2368 SvREFCNT_dec(HeVAL(entry));
2369 Safefree(HeKEY_hek(entry));
2371 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
2375 #if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */
2376 if (!entry && SvRMAGICAL((const SV *)hv)
2377 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
2380 /* The prime_env_iter() on VMS just loaded up new hash values
2381 * so the iteration count needs to be reset back to the beginning
2385 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2390 /* hv_iterinit now ensures this. */
2391 assert (HvARRAY(hv));
2393 /* At start of hash, entry is NULL. */
2396 entry = HeNEXT(entry);
2397 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2399 * Skip past any placeholders -- don't want to include them in
2402 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
2403 entry = HeNEXT(entry);
2408 /* Skip the entire loop if the hash is empty. */
2409 if ((flags & HV_ITERNEXT_WANTPLACEHOLDERS)
2410 ? HvTOTALKEYS(hv) : HvUSEDKEYS(hv)) {
2412 /* OK. Come to the end of the current list. Grab the next one. */
2414 iter->xhv_riter++; /* HvRITER(hv)++ */
2415 if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2416 /* There is no next one. End of the hash. */
2417 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
2420 entry = (HvARRAY(hv))[iter->xhv_riter];
2422 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2423 /* If we have an entry, but it's a placeholder, don't count it.
2425 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2426 entry = HeNEXT(entry);
2428 /* Will loop again if this linked list starts NULL
2429 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2430 or if we run through it and find only placeholders. */
2433 else iter->xhv_riter = -1;
2435 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2437 hv_free_ent(hv, oldentry);
2440 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
2441 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", (void*)hv, (void*)entry);*/
2443 iter->xhv_eiter = entry; /* HvEITER(hv) = entry */
2448 =for apidoc hv_iterkey
2450 Returns the key from the current position of the hash iterator. See
2457 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
2459 PERL_ARGS_ASSERT_HV_ITERKEY;
2461 if (HeKLEN(entry) == HEf_SVKEY) {
2463 char * const p = SvPV(HeKEY_sv(entry), len);
2468 *retlen = HeKLEN(entry);
2469 return HeKEY(entry);
2473 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2475 =for apidoc hv_iterkeysv
2477 Returns the key as an C<SV*> from the current position of the hash
2478 iterator. The return value will always be a mortal copy of the key. Also
2485 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2487 PERL_ARGS_ASSERT_HV_ITERKEYSV;
2489 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
2493 =for apidoc hv_iterval
2495 Returns the value from the current position of the hash iterator. See
2502 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2504 PERL_ARGS_ASSERT_HV_ITERVAL;
2506 if (SvRMAGICAL(hv)) {
2507 if (mg_find((const SV *)hv, PERL_MAGIC_tied)) {
2508 SV* const sv = sv_newmortal();
2509 if (HeKLEN(entry) == HEf_SVKEY)
2510 mg_copy(MUTABLE_SV(hv), sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2512 mg_copy(MUTABLE_SV(hv), sv, HeKEY(entry), HeKLEN(entry));
2516 return HeVAL(entry);
2520 =for apidoc hv_iternextsv
2522 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2529 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2531 HE * const he = hv_iternext_flags(hv, 0);
2533 PERL_ARGS_ASSERT_HV_ITERNEXTSV;
2537 *key = hv_iterkey(he, retlen);
2538 return hv_iterval(hv, he);
2545 =for apidoc hv_magic
2547 Adds magic to a hash. See C<sv_magic>.
2552 /* possibly free a shared string if no one has access to it
2553 * len and hash must both be valid for str.
2556 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2558 unshare_hek_or_pvn (NULL, str, len, hash);
2563 Perl_unshare_hek(pTHX_ HEK *hek)
2566 unshare_hek_or_pvn(hek, NULL, 0, 0);
2569 /* possibly free a shared string if no one has access to it
2570 hek if non-NULL takes priority over the other 3, else str, len and hash
2571 are used. If so, len and hash must both be valid for str.
2574 S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash)
2577 register XPVHV* xhv;
2579 register HE **oentry;
2580 bool is_utf8 = FALSE;
2582 const char * const save = str;
2583 struct shared_he *he = NULL;
2586 /* Find the shared he which is just before us in memory. */
2587 he = (struct shared_he *)(((char *)hek)
2588 - STRUCT_OFFSET(struct shared_he,
2591 /* Assert that the caller passed us a genuine (or at least consistent)
2593 assert (he->shared_he_he.hent_hek == hek);
2595 if (he->shared_he_he.he_valu.hent_refcount - 1) {
2596 --he->shared_he_he.he_valu.hent_refcount;
2600 hash = HEK_HASH(hek);
2601 } else if (len < 0) {
2602 STRLEN tmplen = -len;
2604 /* See the note in hv_fetch(). --jhi */
2605 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2608 k_flags = HVhek_UTF8;
2610 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2613 /* what follows was the moral equivalent of:
2614 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2616 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2618 xhv = (XPVHV*)SvANY(PL_strtab);
2619 /* assert(xhv_array != 0) */
2620 oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)];
2622 const HE *const he_he = &(he->shared_he_he);
2623 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2628 const int flags_masked = k_flags & HVhek_MASK;
2629 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2630 if (HeHASH(entry) != hash) /* strings can't be equal */
2632 if (HeKLEN(entry) != len)
2634 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2636 if (HeKFLAGS(entry) != flags_masked)
2643 if (--entry->he_valu.hent_refcount == 0) {
2644 *oentry = HeNEXT(entry);
2646 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
2651 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
2652 "Attempt to free nonexistent shared string '%s'%s"
2654 hek ? HEK_KEY(hek) : str,
2655 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2656 if (k_flags & HVhek_FREEKEY)
2660 /* get a (constant) string ptr from the global string table
2661 * string will get added if it is not already there.
2662 * len and hash must both be valid for str.
2665 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2667 bool is_utf8 = FALSE;
2669 const char * const save = str;
2671 PERL_ARGS_ASSERT_SHARE_HEK;
2674 STRLEN tmplen = -len;
2676 /* See the note in hv_fetch(). --jhi */
2677 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2679 /* If we were able to downgrade here, then than means that we were passed
2680 in a key which only had chars 0-255, but was utf8 encoded. */
2683 /* If we found we were able to downgrade the string to bytes, then
2684 we should flag that it needs upgrading on keys or each. Also flag
2685 that we need share_hek_flags to free the string. */
2687 PERL_HASH(hash, str, len);
2688 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2692 return share_hek_flags (str, len, hash, flags);
2696 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2700 const int flags_masked = flags & HVhek_MASK;
2701 const U32 hindex = hash & (I32) HvMAX(PL_strtab);
2702 register XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab);
2704 PERL_ARGS_ASSERT_SHARE_HEK_FLAGS;
2706 /* what follows is the moral equivalent of:
2708 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2709 hv_store(PL_strtab, str, len, NULL, hash);
2711 Can't rehash the shared string table, so not sure if it's worth
2712 counting the number of entries in the linked list
2715 /* assert(xhv_array != 0) */
2716 entry = (HvARRAY(PL_strtab))[hindex];
2717 for (;entry; entry = HeNEXT(entry)) {
2718 if (HeHASH(entry) != hash) /* strings can't be equal */
2720 if (HeKLEN(entry) != len)
2722 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2724 if (HeKFLAGS(entry) != flags_masked)
2730 /* What used to be head of the list.
2731 If this is NULL, then we're the first entry for this slot, which
2732 means we need to increate fill. */
2733 struct shared_he *new_entry;
2736 HE **const head = &HvARRAY(PL_strtab)[hindex];
2737 HE *const next = *head;
2739 /* We don't actually store a HE from the arena and a regular HEK.
2740 Instead we allocate one chunk of memory big enough for both,
2741 and put the HEK straight after the HE. This way we can find the
2742 HE directly from the HEK.
2745 Newx(k, STRUCT_OFFSET(struct shared_he,
2746 shared_he_hek.hek_key[0]) + len + 2, char);
2747 new_entry = (struct shared_he *)k;
2748 entry = &(new_entry->shared_he_he);
2749 hek = &(new_entry->shared_he_hek);
2751 Copy(str, HEK_KEY(hek), len, char);
2752 HEK_KEY(hek)[len] = 0;
2754 HEK_HASH(hek) = hash;
2755 HEK_FLAGS(hek) = (unsigned char)flags_masked;
2757 /* Still "point" to the HEK, so that other code need not know what
2759 HeKEY_hek(entry) = hek;
2760 entry->he_valu.hent_refcount = 0;
2761 HeNEXT(entry) = next;
2764 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
2765 if (!next) { /* initial entry? */
2766 } else if (xhv->xhv_keys > xhv->xhv_max /* HvUSEDKEYS(hv) > HvMAX(hv) */) {
2771 ++entry->he_valu.hent_refcount;
2773 if (flags & HVhek_FREEKEY)
2776 return HeKEY_hek(entry);
2780 Perl_hv_placeholders_p(pTHX_ HV *hv)
2783 MAGIC *mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2785 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_P;
2788 mg = sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, 0);
2791 Perl_die(aTHX_ "panic: hv_placeholders_p");
2794 return &(mg->mg_len);
2799 Perl_hv_placeholders_get(pTHX_ const HV *hv)
2802 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2804 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_GET;
2806 return mg ? mg->mg_len : 0;
2810 Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph)
2813 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2815 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_SET;
2820 if (!sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, ph))
2821 Perl_die(aTHX_ "panic: hv_placeholders_set");
2823 /* else we don't need to add magic to record 0 placeholders. */
2827 S_refcounted_he_value(pTHX_ const struct refcounted_he *he)
2832 PERL_ARGS_ASSERT_REFCOUNTED_HE_VALUE;
2834 switch(he->refcounted_he_data[0] & HVrhek_typemask) {
2839 value = &PL_sv_placeholder;
2842 value = newSViv(he->refcounted_he_val.refcounted_he_u_iv);
2845 value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv);
2848 case HVrhek_PV_UTF8:
2849 /* Create a string SV that directly points to the bytes in our
2851 value = newSV_type(SVt_PV);
2852 SvPV_set(value, (char *) he->refcounted_he_data + 1);
2853 SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len);
2854 /* This stops anything trying to free it */
2855 SvLEN_set(value, 0);
2857 SvREADONLY_on(value);
2858 if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8)
2862 Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %"UVxf,
2863 (UV)he->refcounted_he_data[0]);
2869 =for apidoc m|HV *|refcounted_he_chain_2hv|const struct refcounted_he *c|U32 flags
2871 Generates and returns a C<HV *> representing the content of a
2872 C<refcounted_he> chain.
2873 I<flags> is currently unused and must be zero.
2878 Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain, U32 flags)
2882 U32 placeholders, max;
2885 Perl_croak(aTHX_ "panic: refcounted_he_chain_2hv bad flags %"UVxf,
2888 /* We could chase the chain once to get an idea of the number of keys,
2889 and call ksplit. But for now we'll make a potentially inefficient
2890 hash with only 8 entries in its array. */
2895 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char);
2896 HvARRAY(hv) = (HE**)array;
2902 U32 hash = chain->refcounted_he_hash;
2904 U32 hash = HEK_HASH(chain->refcounted_he_hek);
2906 HE **oentry = &((HvARRAY(hv))[hash & max]);
2907 HE *entry = *oentry;
2910 for (; entry; entry = HeNEXT(entry)) {
2911 if (HeHASH(entry) == hash) {
2912 /* We might have a duplicate key here. If so, entry is older
2913 than the key we've already put in the hash, so if they are
2914 the same, skip adding entry. */
2916 const STRLEN klen = HeKLEN(entry);
2917 const char *const key = HeKEY(entry);
2918 if (klen == chain->refcounted_he_keylen
2919 && (!!HeKUTF8(entry)
2920 == !!(chain->refcounted_he_data[0] & HVhek_UTF8))
2921 && memEQ(key, REF_HE_KEY(chain), klen))
2924 if (HeKEY_hek(entry) == chain->refcounted_he_hek)
2926 if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek)
2927 && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek)
2928 && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek),
2939 = share_hek_flags(REF_HE_KEY(chain),
2940 chain->refcounted_he_keylen,
2941 chain->refcounted_he_hash,
2942 (chain->refcounted_he_data[0]
2943 & (HVhek_UTF8|HVhek_WASUTF8)));
2945 HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek);
2947 value = refcounted_he_value(chain);
2948 if (value == &PL_sv_placeholder)
2950 HeVAL(entry) = value;
2952 /* Link it into the chain. */
2953 HeNEXT(entry) = *oentry;
2959 chain = chain->refcounted_he_next;
2963 clear_placeholders(hv, placeholders);
2964 HvTOTALKEYS(hv) -= placeholders;
2967 /* We could check in the loop to see if we encounter any keys with key
2968 flags, but it's probably not worth it, as this per-hash flag is only
2969 really meant as an optimisation for things like Storable. */
2971 DEBUG_A(Perl_hv_assert(aTHX_ hv));
2977 =for apidoc m|SV *|refcounted_he_fetch_pvn|const struct refcounted_he *chain|const char *keypv|STRLEN keylen|U32 hash|U32 flags
2979 Search along a C<refcounted_he> chain for an entry with the key specified
2980 by I<keypv> and I<keylen>. If I<flags> has the C<REFCOUNTED_HE_KEY_UTF8>
2981 bit set, the key octets are interpreted as UTF-8, otherwise they
2982 are interpreted as Latin-1. I<hash> is a precomputed hash of the key
2983 string, or zero if it has not been precomputed. Returns a mortal scalar
2984 representing the value associated with the key, or C<&PL_sv_placeholder>
2985 if there is no value associated with the key.
2991 Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain,
2992 const char *keypv, STRLEN keylen, U32 hash, U32 flags)
2996 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PVN;
2998 if (flags & ~(REFCOUNTED_HE_KEY_UTF8|REFCOUNTED_HE_EXISTS))
2999 Perl_croak(aTHX_ "panic: refcounted_he_fetch_pvn bad flags %"UVxf,
3002 return &PL_sv_placeholder;
3003 if (flags & REFCOUNTED_HE_KEY_UTF8) {
3004 /* For searching purposes, canonicalise to Latin-1 where possible. */
3005 const char *keyend = keypv + keylen, *p;
3006 STRLEN nonascii_count = 0;
3007 for (p = keypv; p != keyend; p++) {
3010 if (!((c & 0xfe) == 0xc2 && ++p != keyend &&
3011 (((U8)*p) & 0xc0) == 0x80))
3012 goto canonicalised_key;
3016 if (nonascii_count) {
3018 const char *p = keypv, *keyend = keypv + keylen;
3019 keylen -= nonascii_count;
3020 Newx(q, keylen, char);
3023 for (; p != keyend; p++, q++) {
3026 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c);
3029 flags &= ~REFCOUNTED_HE_KEY_UTF8;
3030 canonicalised_key: ;
3032 utf8_flag = (flags & REFCOUNTED_HE_KEY_UTF8) ? HVhek_UTF8 : 0;
3034 PERL_HASH(hash, keypv, keylen);
3036 for (; chain; chain = chain->refcounted_he_next) {
3039 hash == chain->refcounted_he_hash &&
3040 keylen == chain->refcounted_he_keylen &&
3041 memEQ(REF_HE_KEY(chain), keypv, keylen) &&
3042 utf8_flag == (chain->refcounted_he_data[0] & HVhek_UTF8)
3044 hash == HEK_HASH(chain->refcounted_he_hek) &&
3045 keylen == (STRLEN)HEK_LEN(chain->refcounted_he_hek) &&
3046 memEQ(HEK_KEY(chain->refcounted_he_hek), keypv, keylen) &&
3047 utf8_flag == (HEK_FLAGS(chain->refcounted_he_hek) & HVhek_UTF8)
3051 flags & REFCOUNTED_HE_EXISTS
3052 ? (chain->refcounted_he_data[0] & HVrhek_typemask)
3056 : sv_2mortal(refcounted_he_value(chain));
3058 return flags & REFCOUNTED_HE_EXISTS ? NULL : &PL_sv_placeholder;
3062 =for apidoc m|SV *|refcounted_he_fetch_pv|const struct refcounted_he *chain|const char *key|U32 hash|U32 flags
3064 Like L</refcounted_he_fetch_pvn>, but takes a nul-terminated string
3065 instead of a string/length pair.
3071 Perl_refcounted_he_fetch_pv(pTHX_ const struct refcounted_he *chain,
3072 const char *key, U32 hash, U32 flags)
3074 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PV;
3075 return refcounted_he_fetch_pvn(chain, key, strlen(key), hash, flags);
3079 =for apidoc m|SV *|refcounted_he_fetch_sv|const struct refcounted_he *chain|SV *key|U32 hash|U32 flags
3081 Like L</refcounted_he_fetch_pvn>, but takes a Perl scalar instead of a
3088 Perl_refcounted_he_fetch_sv(pTHX_ const struct refcounted_he *chain,
3089 SV *key, U32 hash, U32 flags)
3093 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_SV;
3094 if (flags & REFCOUNTED_HE_KEY_UTF8)
3095 Perl_croak(aTHX_ "panic: refcounted_he_fetch_sv bad flags %"UVxf,
3097 keypv = SvPV_const(key, keylen);
3099 flags |= REFCOUNTED_HE_KEY_UTF8;
3100 if (!hash && SvIsCOW_shared_hash(key))
3101 hash = SvSHARED_HASH(key);
3102 return refcounted_he_fetch_pvn(chain, keypv, keylen, hash, flags);
3106 =for apidoc m|struct refcounted_he *|refcounted_he_new_pvn|struct refcounted_he *parent|const char *keypv|STRLEN keylen|U32 hash|SV *value|U32 flags
3108 Creates a new C<refcounted_he>. This consists of a single key/value
3109 pair and a reference to an existing C<refcounted_he> chain (which may
3110 be empty), and thus forms a longer chain. When using the longer chain,
3111 the new key/value pair takes precedence over any entry for the same key
3112 further along the chain.
3114 The new key is specified by I<keypv> and I<keylen>. If I<flags> has
3115 the C<REFCOUNTED_HE_KEY_UTF8> bit set, the key octets are interpreted
3116 as UTF-8, otherwise they are interpreted as Latin-1. I<hash> is
3117 a precomputed hash of the key string, or zero if it has not been
3120 I<value> is the scalar value to store for this key. I<value> is copied
3121 by this function, which thus does not take ownership of any reference
3122 to it, and later changes to the scalar will not be reflected in the
3123 value visible in the C<refcounted_he>. Complex types of scalar will not
3124 be stored with referential integrity, but will be coerced to strings.
3125 I<value> may be either null or C<&PL_sv_placeholder> to indicate that no
3126 value is to be associated with the key; this, as with any non-null value,
3127 takes precedence over the existence of a value for the key further along
3130 I<parent> points to the rest of the C<refcounted_he> chain to be
3131 attached to the new C<refcounted_he>. This function takes ownership
3132 of one reference to I<parent>, and returns one reference to the new
3138 struct refcounted_he *
3139 Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent,
3140 const char *keypv, STRLEN keylen, U32 hash, SV *value, U32 flags)
3143 STRLEN value_len = 0;
3144 const char *value_p = NULL;
3148 STRLEN key_offset = 1;
3149 struct refcounted_he *he;
3150 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PVN;
3152 if (!value || value == &PL_sv_placeholder) {
3153 value_type = HVrhek_delete;
3154 } else if (SvPOK(value)) {
3155 value_type = HVrhek_PV;
3156 } else if (SvIOK(value)) {
3157 value_type = SvUOK((const SV *)value) ? HVrhek_UV : HVrhek_IV;
3158 } else if (!SvOK(value)) {
3159 value_type = HVrhek_undef;
3161 value_type = HVrhek_PV;
3163 is_pv = value_type == HVrhek_PV;
3165 /* Do it this way so that the SvUTF8() test is after the SvPV, in case
3166 the value is overloaded, and doesn't yet have the UTF-8flag set. */
3167 value_p = SvPV_const(value, value_len);
3169 value_type = HVrhek_PV_UTF8;
3170 key_offset = value_len + 2;
3172 hekflags = value_type;
3174 if (flags & REFCOUNTED_HE_KEY_UTF8) {
3175 /* Canonicalise to Latin-1 where possible. */
3176 const char *keyend = keypv + keylen, *p;
3177 STRLEN nonascii_count = 0;
3178 for (p = keypv; p != keyend; p++) {
3181 if (!((c & 0xfe) == 0xc2 && ++p != keyend &&
3182 (((U8)*p) & 0xc0) == 0x80))
3183 goto canonicalised_key;
3187 if (nonascii_count) {
3189 const char *p = keypv, *keyend = keypv + keylen;
3190 keylen -= nonascii_count;
3191 Newx(q, keylen, char);
3194 for (; p != keyend; p++, q++) {
3197 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c);
3200 flags &= ~REFCOUNTED_HE_KEY_UTF8;
3201 canonicalised_key: ;
3203 if (flags & REFCOUNTED_HE_KEY_UTF8)
3204 hekflags |= HVhek_UTF8;
3206 PERL_HASH(hash, keypv, keylen);
3209 he = (struct refcounted_he*)
3210 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3214 he = (struct refcounted_he*)
3215 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3219 he->refcounted_he_next = parent;
3222 Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char);
3223 he->refcounted_he_val.refcounted_he_u_len = value_len;
3224 } else if (value_type == HVrhek_IV) {
3225 he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value);
3226 } else if (value_type == HVrhek_UV) {
3227 he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value);
3231 he->refcounted_he_hash = hash;
3232 he->refcounted_he_keylen = keylen;
3233 Copy(keypv, he->refcounted_he_data + key_offset, keylen, char);
3235 he->refcounted_he_hek = share_hek_flags(keypv, keylen, hash, hekflags);
3238 he->refcounted_he_data[0] = hekflags;
3239 he->refcounted_he_refcnt = 1;
3245 =for apidoc m|struct refcounted_he *|refcounted_he_new_pv|struct refcounted_he *parent|const char *key|U32 hash|SV *value|U32 flags
3247 Like L</refcounted_he_new_pvn>, but takes a nul-terminated string instead
3248 of a string/length pair.
3253 struct refcounted_he *
3254 Perl_refcounted_he_new_pv(pTHX_ struct refcounted_he *parent,
3255 const char *key, U32 hash, SV *value, U32 flags)
3257 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PV;
3258 return refcounted_he_new_pvn(parent, key, strlen(key), hash, value, flags);
3262 =for apidoc m|struct refcounted_he *|refcounted_he_new_sv|struct refcounted_he *parent|SV *key|U32 hash|SV *value|U32 flags
3264 Like L</refcounted_he_new_pvn>, but takes a Perl scalar instead of a
3270 struct refcounted_he *
3271 Perl_refcounted_he_new_sv(pTHX_ struct refcounted_he *parent,
3272 SV *key, U32 hash, SV *value, U32 flags)
3276 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_SV;
3277 if (flags & REFCOUNTED_HE_KEY_UTF8)
3278 Perl_croak(aTHX_ "panic: refcounted_he_new_sv bad flags %"UVxf,
3280 keypv = SvPV_const(key, keylen);
3282 flags |= REFCOUNTED_HE_KEY_UTF8;
3283 if (!hash && SvIsCOW_shared_hash(key))
3284 hash = SvSHARED_HASH(key);
3285 return refcounted_he_new_pvn(parent, keypv, keylen, hash, value, flags);
3289 =for apidoc m|void|refcounted_he_free|struct refcounted_he *he
3291 Decrements the reference count of a C<refcounted_he> by one. If the
3292 reference count reaches zero the structure's memory is freed, which
3293 (recursively) causes a reduction of its parent C<refcounted_he>'s
3294 reference count. It is safe to pass a null pointer to this function:
3295 no action occurs in this case.
3301 Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) {
3303 PERL_UNUSED_CONTEXT;
3306 struct refcounted_he *copy;
3310 new_count = --he->refcounted_he_refcnt;
3311 HINTS_REFCNT_UNLOCK;
3317 #ifndef USE_ITHREADS
3318 unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0);
3321 he = he->refcounted_he_next;
3322 PerlMemShared_free(copy);
3327 =for apidoc m|struct refcounted_he *|refcounted_he_inc|struct refcounted_he *he
3329 Increment the reference count of a C<refcounted_he>. The pointer to the
3330 C<refcounted_he> is also returned. It is safe to pass a null pointer
3331 to this function: no action occurs and a null pointer is returned.
3336 struct refcounted_he *
3337 Perl_refcounted_he_inc(pTHX_ struct refcounted_he *he)
3342 he->refcounted_he_refcnt++;
3343 HINTS_REFCNT_UNLOCK;
3349 =for apidoc cop_fetch_label
3351 Returns the label attached to a cop.
3352 The flags pointer may be set to C<SVf_UTF8> or 0.
3357 /* pp_entereval is aware that labels are stored with a key ':' at the top of
3360 Perl_cop_fetch_label(pTHX_ COP *const cop, STRLEN *len, U32 *flags) {
3361 struct refcounted_he *const chain = cop->cop_hints_hash;
3363 PERL_ARGS_ASSERT_COP_FETCH_LABEL;
3368 if (chain->refcounted_he_keylen != 1)
3370 if (*REF_HE_KEY(chain) != ':')
3373 if ((STRLEN)HEK_LEN(chain->refcounted_he_hek) != 1)
3375 if (*HEK_KEY(chain->refcounted_he_hek) != ':')
3378 /* Stop anyone trying to really mess us up by adding their own value for
3380 if ((chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV
3381 && (chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV_UTF8)
3385 *len = chain->refcounted_he_val.refcounted_he_u_len;
3387 *flags = ((chain->refcounted_he_data[0] & HVrhek_typemask)
3388 == HVrhek_PV_UTF8) ? SVf_UTF8 : 0;
3390 return chain->refcounted_he_data + 1;
3394 =for apidoc cop_store_label
3396 Save a label into a C<cop_hints_hash>. You need to set flags to C<SVf_UTF8>
3403 Perl_cop_store_label(pTHX_ COP *const cop, const char *label, STRLEN len,
3407 PERL_ARGS_ASSERT_COP_STORE_LABEL;
3409 if (flags & ~(SVf_UTF8))
3410 Perl_croak(aTHX_ "panic: cop_store_label illegal flag bits 0x%" UVxf,
3412 labelsv = newSVpvn_flags(label, len, SVs_TEMP);
3413 if (flags & SVf_UTF8)
3416 = refcounted_he_new_pvs(cop->cop_hints_hash, ":", labelsv, 0);
3420 =for apidoc hv_assert
3422 Check that a hash is in an internally consistent state.
3430 Perl_hv_assert(pTHX_ HV *hv)
3435 int placeholders = 0;
3438 const I32 riter = HvRITER_get(hv);
3439 HE *eiter = HvEITER_get(hv);
3441 PERL_ARGS_ASSERT_HV_ASSERT;
3443 (void)hv_iterinit(hv);
3445 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
3446 /* sanity check the values */
3447 if (HeVAL(entry) == &PL_sv_placeholder)
3451 /* sanity check the keys */
3452 if (HeSVKEY(entry)) {
3453 NOOP; /* Don't know what to check on SV keys. */
3454 } else if (HeKUTF8(entry)) {
3456 if (HeKWASUTF8(entry)) {
3457 PerlIO_printf(Perl_debug_log,
3458 "hash key has both WASUTF8 and UTF8: '%.*s'\n",
3459 (int) HeKLEN(entry), HeKEY(entry));
3462 } else if (HeKWASUTF8(entry))
3465 if (!SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) {
3466 static const char bad_count[] = "Count %d %s(s), but hash reports %d\n";
3467 const int nhashkeys = HvUSEDKEYS(hv);
3468 const int nhashplaceholders = HvPLACEHOLDERS_get(hv);
3470 if (nhashkeys != real) {
3471 PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys );
3474 if (nhashplaceholders != placeholders) {
3475 PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders );
3479 if (withflags && ! HvHASKFLAGS(hv)) {
3480 PerlIO_printf(Perl_debug_log,
3481 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
3486 sv_dump(MUTABLE_SV(hv));
3488 HvRITER_set(hv, riter); /* Restore hash iterator state */
3489 HvEITER_set(hv, eiter);
3496 * c-indentation-style: bsd
3498 * indent-tabs-mode: t
3501 * ex: set ts=8 sts=4 sw=4 noet: