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. This means that if
281 there is no value in the hash associated with the given key, then one is
282 created and a pointer to it is returned. The C<SV*> it points to can be
283 assigned to. But always check that the
284 return value is non-null before dereferencing it to an C<SV*>.
286 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
287 information on how to use this function on tied hashes.
289 =for apidoc hv_exists_ent
291 Returns a boolean indicating whether
292 the specified hash key exists. C<hash>
293 can be a valid precomputed hash value, or 0 to ask for it to be
299 /* returns an HE * structure with the all fields set */
300 /* note that hent_val will be a mortal sv for MAGICAL hashes */
302 =for apidoc hv_fetch_ent
304 Returns the hash entry which corresponds to the specified key in the hash.
305 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
306 if you want the function to compute it. IF C<lval> is set then the fetch
307 will be part of a store. Make sure the return value is non-null before
308 accessing it. The return value when C<hv> is a tied hash is a pointer to a
309 static location, so be sure to make a copy of the structure if you need to
312 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
313 information on how to use this function on tied hashes.
318 /* Common code for hv_delete()/hv_exists()/hv_fetch()/hv_store() */
320 Perl_hv_common_key_len(pTHX_ HV *hv, const char *key, I32 klen_i32,
321 const int action, SV *val, const U32 hash)
326 PERL_ARGS_ASSERT_HV_COMMON_KEY_LEN;
335 return hv_common(hv, NULL, key, klen, flags, action, val, hash);
339 Perl_hv_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
340 int flags, int action, SV *val, register U32 hash)
349 const int return_svp = action & HV_FETCH_JUST_SV;
353 if (SvTYPE(hv) == (svtype)SVTYPEMASK)
356 assert(SvTYPE(hv) == SVt_PVHV);
358 if (SvSMAGICAL(hv) && SvGMAGICAL(hv) && !(action & HV_DISABLE_UVAR_XKEY)) {
360 if ((mg = mg_find((const SV *)hv, PERL_MAGIC_uvar))) {
361 struct ufuncs * const uf = (struct ufuncs *)mg->mg_ptr;
362 if (uf->uf_set == NULL) {
363 SV* obj = mg->mg_obj;
366 keysv = newSVpvn_flags(key, klen, SVs_TEMP |
367 ((flags & HVhek_UTF8)
371 mg->mg_obj = keysv; /* pass key */
372 uf->uf_index = action; /* pass action */
373 magic_getuvar(MUTABLE_SV(hv), mg);
374 keysv = mg->mg_obj; /* may have changed */
377 /* If the key may have changed, then we need to invalidate
378 any passed-in computed hash value. */
384 if (flags & HVhek_FREEKEY)
386 key = SvPV_const(keysv, klen);
387 is_utf8 = (SvUTF8(keysv) != 0);
388 if (SvIsCOW_shared_hash(keysv)) {
389 flags = HVhek_KEYCANONICAL | (is_utf8 ? HVhek_UTF8 : 0);
391 flags = is_utf8 ? HVhek_UTF8 : 0;
394 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
397 if (action & HV_DELETE) {
398 return (void *) hv_delete_common(hv, keysv, key, klen,
399 flags, action, hash);
402 xhv = (XPVHV*)SvANY(hv);
404 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS))) {
405 if (mg_find((const SV *)hv, PERL_MAGIC_tied)
406 || SvGMAGICAL((const SV *)hv))
408 /* FIXME should be able to skimp on the HE/HEK here when
409 HV_FETCH_JUST_SV is true. */
411 keysv = newSVpvn_utf8(key, klen, is_utf8);
413 keysv = newSVsv(keysv);
416 mg_copy(MUTABLE_SV(hv), sv, (char *)keysv, HEf_SVKEY);
418 /* grab a fake HE/HEK pair from the pool or make a new one */
419 entry = PL_hv_fetch_ent_mh;
421 PL_hv_fetch_ent_mh = HeNEXT(entry);
425 Newx(k, HEK_BASESIZE + sizeof(const SV *), char);
426 HeKEY_hek(entry) = (HEK*)k;
428 HeNEXT(entry) = NULL;
429 HeSVKEY_set(entry, keysv);
431 sv_upgrade(sv, SVt_PVLV);
433 /* so we can free entry when freeing sv */
434 LvTARG(sv) = MUTABLE_SV(entry);
436 /* XXX remove at some point? */
437 if (flags & HVhek_FREEKEY)
441 return entry ? (void *) &HeVAL(entry) : NULL;
443 return (void *) entry;
445 #ifdef ENV_IS_CASELESS
446 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
448 for (i = 0; i < klen; ++i)
449 if (isLOWER(key[i])) {
450 /* Would be nice if we had a routine to do the
451 copy and upercase in a single pass through. */
452 const char * const nkey = strupr(savepvn(key,klen));
453 /* Note that this fetch is for nkey (the uppercased
454 key) whereas the store is for key (the original) */
455 void *result = hv_common(hv, NULL, nkey, klen,
456 HVhek_FREEKEY, /* free nkey */
457 0 /* non-LVAL fetch */
458 | HV_DISABLE_UVAR_XKEY
461 0 /* compute hash */);
462 if (!result && (action & HV_FETCH_LVALUE)) {
463 /* This call will free key if necessary.
464 Do it this way to encourage compiler to tail
466 result = hv_common(hv, keysv, key, klen, flags,
468 | HV_DISABLE_UVAR_XKEY
472 if (flags & HVhek_FREEKEY)
480 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
481 if (mg_find((const SV *)hv, PERL_MAGIC_tied)
482 || SvGMAGICAL((const SV *)hv)) {
483 /* I don't understand why hv_exists_ent has svret and sv,
484 whereas hv_exists only had one. */
485 SV * const svret = sv_newmortal();
488 if (keysv || is_utf8) {
490 keysv = newSVpvn_utf8(key, klen, TRUE);
492 keysv = newSVsv(keysv);
494 mg_copy(MUTABLE_SV(hv), sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
496 mg_copy(MUTABLE_SV(hv), sv, key, klen);
498 if (flags & HVhek_FREEKEY)
500 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
501 /* This cast somewhat evil, but I'm merely using NULL/
502 not NULL to return the boolean exists.
503 And I know hv is not NULL. */
504 return SvTRUE(svret) ? (void *)hv : NULL;
506 #ifdef ENV_IS_CASELESS
507 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
508 /* XXX This code isn't UTF8 clean. */
509 char * const keysave = (char * const)key;
510 /* Will need to free this, so set FREEKEY flag. */
511 key = savepvn(key,klen);
512 key = (const char*)strupr((char*)key);
517 if (flags & HVhek_FREEKEY) {
520 flags |= HVhek_FREEKEY;
524 else if (action & HV_FETCH_ISSTORE) {
527 hv_magic_check (hv, &needs_copy, &needs_store);
529 const bool save_taint = TAINT_get; /* Unused var warning under NO_TAINT_SUPPORT */
530 if (keysv || is_utf8) {
532 keysv = newSVpvn_utf8(key, klen, TRUE);
535 TAINT_set(SvTAINTED(keysv));
536 keysv = sv_2mortal(newSVsv(keysv));
537 mg_copy(MUTABLE_SV(hv), val, (char*)keysv, HEf_SVKEY);
539 mg_copy(MUTABLE_SV(hv), val, key, klen);
542 TAINT_IF(save_taint);
544 if (flags & HVhek_FREEKEY)
548 #ifdef ENV_IS_CASELESS
549 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
550 /* XXX This code isn't UTF8 clean. */
551 const char *keysave = key;
552 /* Will need to free this, so set FREEKEY flag. */
553 key = savepvn(key,klen);
554 key = (const char*)strupr((char*)key);
559 if (flags & HVhek_FREEKEY) {
562 flags |= HVhek_FREEKEY;
570 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
571 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
572 || (SvRMAGICAL((const SV *)hv)
573 && mg_find((const SV *)hv, PERL_MAGIC_env))
578 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
580 HvARRAY(hv) = (HE**)array;
582 #ifdef DYNAMIC_ENV_FETCH
583 else if (action & HV_FETCH_ISEXISTS) {
584 /* for an %ENV exists, if we do an insert it's by a recursive
585 store call, so avoid creating HvARRAY(hv) right now. */
589 /* XXX remove at some point? */
590 if (flags & HVhek_FREEKEY)
597 if (is_utf8 && !(flags & HVhek_KEYCANONICAL)) {
598 char * const keysave = (char *)key;
599 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
603 flags &= ~HVhek_UTF8;
604 if (key != keysave) {
605 if (flags & HVhek_FREEKEY)
607 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
608 /* If the caller calculated a hash, it was on the sequence of
609 octets that are the UTF-8 form. We've now changed the sequence
610 of octets stored to that of the equivalent byte representation,
611 so the hash we need is different. */
617 if (keysv && (SvIsCOW_shared_hash(keysv)))
618 hash = SvSHARED_HASH(keysv);
620 PERL_HASH(hash, key, klen);
623 masked_flags = (flags & HVhek_MASK);
625 #ifdef DYNAMIC_ENV_FETCH
626 if (!HvARRAY(hv)) entry = NULL;
630 entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)];
632 for (; entry; entry = HeNEXT(entry)) {
633 if (HeHASH(entry) != hash) /* strings can't be equal */
635 if (HeKLEN(entry) != (I32)klen)
637 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
639 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
642 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
643 if (HeKFLAGS(entry) != masked_flags) {
644 /* We match if HVhek_UTF8 bit in our flags and hash key's
645 match. But if entry was set previously with HVhek_WASUTF8
646 and key now doesn't (or vice versa) then we should change
647 the key's flag, as this is assignment. */
648 if (HvSHAREKEYS(hv)) {
649 /* Need to swap the key we have for a key with the flags we
650 need. As keys are shared we can't just write to the
651 flag, so we share the new one, unshare the old one. */
652 HEK * const new_hek = share_hek_flags(key, klen, hash,
654 unshare_hek (HeKEY_hek(entry));
655 HeKEY_hek(entry) = new_hek;
657 else if (hv == PL_strtab) {
658 /* PL_strtab is usually the only hash without HvSHAREKEYS,
659 so putting this test here is cheap */
660 if (flags & HVhek_FREEKEY)
662 Perl_croak(aTHX_ S_strtab_error,
663 action & HV_FETCH_LVALUE ? "fetch" : "store");
666 HeKFLAGS(entry) = masked_flags;
667 if (masked_flags & HVhek_ENABLEHVKFLAGS)
670 if (HeVAL(entry) == &PL_sv_placeholder) {
671 /* yes, can store into placeholder slot */
672 if (action & HV_FETCH_LVALUE) {
674 /* This preserves behaviour with the old hv_fetch
675 implementation which at this point would bail out
676 with a break; (at "if we find a placeholder, we
677 pretend we haven't found anything")
679 That break mean that if a placeholder were found, it
680 caused a call into hv_store, which in turn would
681 check magic, and if there is no magic end up pretty
682 much back at this point (in hv_store's code). */
685 /* LVAL fetch which actually needs a store. */
687 HvPLACEHOLDERS(hv)--;
690 if (val != &PL_sv_placeholder)
691 HvPLACEHOLDERS(hv)--;
694 } else if (action & HV_FETCH_ISSTORE) {
695 SvREFCNT_dec(HeVAL(entry));
698 } else if (HeVAL(entry) == &PL_sv_placeholder) {
699 /* if we find a placeholder, we pretend we haven't found
703 if (flags & HVhek_FREEKEY)
706 return entry ? (void *) &HeVAL(entry) : NULL;
710 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
711 if (!(action & HV_FETCH_ISSTORE)
712 && SvRMAGICAL((const SV *)hv)
713 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
715 const char * const env = PerlEnv_ENVgetenv_len(key,&len);
717 sv = newSVpvn(env,len);
719 return hv_common(hv, keysv, key, klen, flags,
720 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
726 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
727 hv_notallowed(flags, key, klen,
728 "Attempt to access disallowed key '%"SVf"' in"
729 " a restricted hash");
731 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
732 /* Not doing some form of store, so return failure. */
733 if (flags & HVhek_FREEKEY)
737 if (action & HV_FETCH_LVALUE) {
738 val = action & HV_FETCH_EMPTY_HE ? NULL : newSV(0);
740 /* At this point the old hv_fetch code would call to hv_store,
741 which in turn might do some tied magic. So we need to make that
742 magic check happen. */
743 /* gonna assign to this, so it better be there */
744 /* If a fetch-as-store fails on the fetch, then the action is to
745 recurse once into "hv_store". If we didn't do this, then that
746 recursive call would call the key conversion routine again.
747 However, as we replace the original key with the converted
748 key, this would result in a double conversion, which would show
749 up as a bug if the conversion routine is not idempotent. */
750 return hv_common(hv, keysv, key, klen, flags,
751 HV_FETCH_ISSTORE|HV_DISABLE_UVAR_XKEY|return_svp,
753 /* XXX Surely that could leak if the fetch-was-store fails?
754 Just like the hv_fetch. */
758 /* Welcome to hv_store... */
761 /* Not sure if we can get here. I think the only case of oentry being
762 NULL is for %ENV with dynamic env fetch. But that should disappear
763 with magic in the previous code. */
766 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
768 HvARRAY(hv) = (HE**)array;
771 oentry = &(HvARRAY(hv))[hash & (I32) xhv->xhv_max];
774 /* share_hek_flags will do the free for us. This might be considered
777 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
778 else if (hv == PL_strtab) {
779 /* PL_strtab is usually the only hash without HvSHAREKEYS, so putting
780 this test here is cheap */
781 if (flags & HVhek_FREEKEY)
783 Perl_croak(aTHX_ S_strtab_error,
784 action & HV_FETCH_LVALUE ? "fetch" : "store");
786 else /* gotta do the real thing */
787 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
789 HeNEXT(entry) = *oentry;
792 if (val == &PL_sv_placeholder)
793 HvPLACEHOLDERS(hv)++;
794 if (masked_flags & HVhek_ENABLEHVKFLAGS)
798 const HE *counter = HeNEXT(entry);
800 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
801 if (!counter) { /* initial entry? */
802 } else if (xhv->xhv_keys > xhv->xhv_max) {
803 /* Use only the old HvUSEDKEYS(hv) > HvMAX(hv) condition to limit
804 bucket splits on a rehashed hash, as we're not going to
805 split it again, and if someone is lucky (evil) enough to
806 get all the keys in one list they could exhaust our memory
807 as we repeatedly double the number of buckets on every
808 entry. Linear search feels a less worse thing to do. */
813 while ((counter = HeNEXT(counter)))
816 if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) {
823 return entry ? (void *) &HeVAL(entry) : NULL;
825 return (void *) entry;
829 S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store)
831 const MAGIC *mg = SvMAGIC(hv);
833 PERL_ARGS_ASSERT_HV_MAGIC_CHECK;
838 if (isUPPER(mg->mg_type)) {
840 if (mg->mg_type == PERL_MAGIC_tied) {
841 *needs_store = FALSE;
842 return; /* We've set all there is to set. */
845 mg = mg->mg_moremagic;
850 =for apidoc hv_scalar
852 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
858 Perl_hv_scalar(pTHX_ HV *hv)
862 PERL_ARGS_ASSERT_HV_SCALAR;
864 if (SvRMAGICAL(hv)) {
865 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_tied);
867 return magic_scalarpack(hv, mg);
871 if (HvTOTALKEYS((const HV *)hv))
872 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
873 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
881 =for apidoc hv_delete
883 Deletes a key/value pair in the hash. The value's SV is removed from
884 the hash, made mortal, and returned to the caller. The absolute
885 value of C<klen> is the length of the key. If C<klen> is negative the
886 key is assumed to be in UTF-8-encoded Unicode. The C<flags> value
887 will normally be zero; if set to G_DISCARD then NULL will be returned.
888 NULL will also be returned if the key is not found.
890 =for apidoc hv_delete_ent
892 Deletes a key/value pair in the hash. The value SV is removed from the hash,
893 made mortal, and returned to the caller. The C<flags> value will normally be
894 zero; if set to G_DISCARD then NULL will be returned. NULL will also be
895 returned if the key is not found. C<hash> can be a valid precomputed hash
896 value, or 0 to ask for it to be computed.
902 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
903 int k_flags, I32 d_flags, U32 hash)
909 bool is_utf8 = (k_flags & HVhek_UTF8) ? TRUE : FALSE;
912 if (SvRMAGICAL(hv)) {
915 hv_magic_check (hv, &needs_copy, &needs_store);
919 entry = (HE *) hv_common(hv, keysv, key, klen,
920 k_flags & ~HVhek_FREEKEY,
921 HV_FETCH_LVALUE|HV_DISABLE_UVAR_XKEY,
923 sv = entry ? HeVAL(entry) : NULL;
929 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
930 /* No longer an element */
931 sv_unmagic(sv, PERL_MAGIC_tiedelem);
934 return NULL; /* element cannot be deleted */
936 #ifdef ENV_IS_CASELESS
937 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
938 /* XXX This code isn't UTF8 clean. */
939 keysv = newSVpvn_flags(key, klen, SVs_TEMP);
940 if (k_flags & HVhek_FREEKEY) {
943 key = strupr(SvPVX(keysv));
952 xhv = (XPVHV*)SvANY(hv);
956 if (is_utf8 && !(k_flags & HVhek_KEYCANONICAL)) {
957 const char * const keysave = key;
958 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
961 k_flags |= HVhek_UTF8;
963 k_flags &= ~HVhek_UTF8;
964 if (key != keysave) {
965 if (k_flags & HVhek_FREEKEY) {
966 /* This shouldn't happen if our caller does what we expect,
967 but strictly the API allows it. */
970 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
972 HvHASKFLAGS_on(MUTABLE_SV(hv));
976 if (keysv && (SvIsCOW_shared_hash(keysv)))
977 hash = SvSHARED_HASH(keysv);
979 PERL_HASH(hash, key, klen);
982 masked_flags = (k_flags & HVhek_MASK);
984 oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)];
986 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) {
988 U8 mro_changes = 0; /* 1 = isa; 2 = package moved */
992 if (HeHASH(entry) != hash) /* strings can't be equal */
994 if (HeKLEN(entry) != (I32)klen)
996 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
998 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1001 if (hv == PL_strtab) {
1002 if (k_flags & HVhek_FREEKEY)
1004 Perl_croak(aTHX_ S_strtab_error, "delete");
1007 /* if placeholder is here, it's already been deleted.... */
1008 if (HeVAL(entry) == &PL_sv_placeholder) {
1009 if (k_flags & HVhek_FREEKEY)
1013 if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))
1014 && !SvIsCOW(HeVAL(entry))) {
1015 hv_notallowed(k_flags, key, klen,
1016 "Attempt to delete readonly key '%"SVf"' from"
1017 " a restricted hash");
1019 if (k_flags & HVhek_FREEKEY)
1022 /* If this is a stash and the key ends with ::, then someone is
1023 * deleting a package.
1025 if (HeVAL(entry) && HvENAME_get(hv)) {
1026 gv = (GV *)HeVAL(entry);
1027 if (keysv) key = SvPV(keysv, klen);
1029 (klen > 1 && key[klen-2] == ':' && key[klen-1] == ':')
1031 (klen == 1 && key[0] == ':')
1033 && (klen != 6 || hv!=PL_defstash || memNE(key,"main::",6))
1034 && SvTYPE(gv) == SVt_PVGV && (stash = GvHV((GV *)gv))
1035 && HvENAME_get(stash)) {
1036 /* A previous version of this code checked that the
1037 * GV was still in the symbol table by fetching the
1038 * GV with its name. That is not necessary (and
1039 * sometimes incorrect), as HvENAME cannot be set
1040 * on hv if it is not in the symtab. */
1042 /* Hang on to it for a bit. */
1043 SvREFCNT_inc_simple_void_NN(
1044 sv_2mortal((SV *)gv)
1047 else if (klen == 3 && strnEQ(key, "ISA", 3))
1051 sv = d_flags & G_DISCARD ? HeVAL(entry) : sv_2mortal(HeVAL(entry));
1052 HeVAL(entry) = &PL_sv_placeholder;
1054 /* deletion of method from stash */
1055 if (isGV(sv) && isGV_with_GP(sv) && GvCVu(sv)
1057 mro_method_changed_in(hv);
1061 * If a restricted hash, rather than really deleting the entry, put
1062 * a placeholder there. This marks the key as being "approved", so
1063 * we can still access via not-really-existing key without raising
1067 /* We'll be saving this slot, so the number of allocated keys
1068 * doesn't go down, but the number placeholders goes up */
1069 HvPLACEHOLDERS(hv)++;
1071 *oentry = HeNEXT(entry);
1072 if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */)
1075 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1076 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1077 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
1078 hv_free_ent(hv, entry);
1080 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
1081 if (xhv->xhv_keys == 0)
1082 HvHASKFLAGS_off(hv);
1085 if (d_flags & G_DISCARD) {
1090 if (mro_changes == 1) mro_isa_changed_in(hv);
1091 else if (mro_changes == 2)
1092 mro_package_moved(NULL, stash, gv, 1);
1096 if (SvREADONLY(hv)) {
1097 hv_notallowed(k_flags, key, klen,
1098 "Attempt to delete disallowed key '%"SVf"' from"
1099 " a restricted hash");
1102 if (k_flags & HVhek_FREEKEY)
1108 S_hsplit(pTHX_ HV *hv)
1111 XPVHV* const xhv = (XPVHV*)SvANY(hv);
1112 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1113 I32 newsize = oldsize * 2;
1115 char *a = (char*) HvARRAY(hv);
1118 PERL_ARGS_ASSERT_HSPLIT;
1120 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1121 (void*)hv, (int) oldsize);*/
1123 if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) {
1124 /* Can make this clear any placeholders first for non-restricted hashes,
1125 even though Storable rebuilds restricted hashes by putting in all the
1126 placeholders (first) before turning on the readonly flag, because
1127 Storable always pre-splits the hash. */
1128 hv_clear_placeholders(hv);
1132 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1133 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1134 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1140 Move(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1143 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1144 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1149 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1151 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1153 Safefree(HvARRAY(hv));
1157 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1158 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1159 HvARRAY(hv) = (HE**) a;
1162 for (i=0; i<oldsize; i++,aep++) {
1167 if (!entry) /* non-existent */
1171 if ((HeHASH(entry) & newsize) != (U32)i) {
1172 *oentry = HeNEXT(entry);
1173 HeNEXT(entry) = *bep;
1177 oentry = &HeNEXT(entry);
1181 /* I think we don't actually need to keep track of the longest length,
1182 merely flag if anything is too long. But for the moment while
1183 developing this code I'll track it. */
1188 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1191 XPVHV* xhv = (XPVHV*)SvANY(hv);
1192 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1198 PERL_ARGS_ASSERT_HV_KSPLIT;
1200 newsize = (I32) newmax; /* possible truncation here */
1201 if (newsize != newmax || newmax <= oldsize)
1203 while ((newsize & (1 + ~newsize)) != newsize) {
1204 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1206 if (newsize < newmax)
1208 if (newsize < newmax)
1209 return; /* overflow detection */
1211 a = (char *) HvARRAY(hv);
1214 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1215 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1216 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1222 Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1225 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1226 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1231 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1233 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1235 Safefree(HvARRAY(hv));
1238 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1241 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1243 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1244 HvARRAY(hv) = (HE **) a;
1245 if (!xhv->xhv_keys /* !HvTOTALKEYS(hv) */) /* skip rest if no entries */
1249 for (i=0; i<oldsize; i++,aep++) {
1253 if (!entry) /* non-existent */
1256 I32 j = (HeHASH(entry) & newsize);
1260 *oentry = HeNEXT(entry);
1261 HeNEXT(entry) = aep[j];
1265 oentry = &HeNEXT(entry);
1272 Perl_newHVhv(pTHX_ HV *ohv)
1275 HV * const hv = newHV();
1278 if (!ohv || (!HvTOTALKEYS(ohv) && !SvMAGICAL((const SV *)ohv)))
1280 hv_max = HvMAX(ohv);
1282 if (!SvMAGICAL((const SV *)ohv)) {
1283 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1285 const bool shared = !!HvSHAREKEYS(ohv);
1286 HE **ents, ** const oents = (HE **)HvARRAY(ohv);
1288 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1291 /* In each bucket... */
1292 for (i = 0; i <= hv_max; i++) {
1294 HE *oent = oents[i];
1301 /* Copy the linked list of entries. */
1302 for (; oent; oent = HeNEXT(oent)) {
1303 const U32 hash = HeHASH(oent);
1304 const char * const key = HeKEY(oent);
1305 const STRLEN len = HeKLEN(oent);
1306 const int flags = HeKFLAGS(oent);
1307 HE * const ent = new_HE();
1308 SV *const val = HeVAL(oent);
1310 HeVAL(ent) = SvIMMORTAL(val) ? val : newSVsv(val);
1312 = shared ? share_hek_flags(key, len, hash, flags)
1313 : save_hek_flags(key, len, hash, flags);
1324 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1328 /* Iterate over ohv, copying keys and values one at a time. */
1330 const I32 riter = HvRITER_get(ohv);
1331 HE * const eiter = HvEITER_get(ohv);
1332 STRLEN hv_fill = HvFILL(ohv);
1334 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1335 while (hv_max && hv_max + 1 >= hv_fill * 2)
1336 hv_max = hv_max / 2;
1340 while ((entry = hv_iternext_flags(ohv, 0))) {
1341 SV *val = hv_iterval(ohv,entry);
1342 SV * const keysv = HeSVKEY(entry);
1343 val = SvIMMORTAL(val) ? val : newSVsv(val);
1345 (void)hv_store_ent(hv, keysv, val, 0);
1347 (void)hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), val,
1348 HeHASH(entry), HeKFLAGS(entry));
1350 HvRITER_set(ohv, riter);
1351 HvEITER_set(ohv, eiter);
1358 =for apidoc Am|HV *|hv_copy_hints_hv|HV *ohv
1360 A specialised version of L</newHVhv> for copying C<%^H>. I<ohv> must be
1361 a pointer to a hash (which may have C<%^H> magic, but should be generally
1362 non-magical), or C<NULL> (interpreted as an empty hash). The content
1363 of I<ohv> is copied to a new hash, which has the C<%^H>-specific magic
1364 added to it. A pointer to the new hash is returned.
1370 Perl_hv_copy_hints_hv(pTHX_ HV *const ohv)
1372 HV * const hv = newHV();
1375 STRLEN hv_max = HvMAX(ohv);
1376 STRLEN hv_fill = HvFILL(ohv);
1378 const I32 riter = HvRITER_get(ohv);
1379 HE * const eiter = HvEITER_get(ohv);
1384 while (hv_max && hv_max + 1 >= hv_fill * 2)
1385 hv_max = hv_max / 2;
1389 while ((entry = hv_iternext_flags(ohv, 0))) {
1390 SV *const sv = newSVsv(hv_iterval(ohv,entry));
1391 SV *heksv = HeSVKEY(entry);
1392 if (!heksv && sv) heksv = newSVhek(HeKEY_hek(entry));
1393 if (sv) sv_magic(sv, NULL, PERL_MAGIC_hintselem,
1394 (char *)heksv, HEf_SVKEY);
1395 if (heksv == HeSVKEY(entry))
1396 (void)hv_store_ent(hv, heksv, sv, 0);
1398 (void)hv_common(hv, heksv, HeKEY(entry), HeKLEN(entry),
1399 HeKFLAGS(entry), HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv, HeHASH(entry));
1400 SvREFCNT_dec(heksv);
1403 HvRITER_set(ohv, riter);
1404 HvEITER_set(ohv, eiter);
1406 SvREFCNT_inc_simple_void_NN(hv);
1409 hv_magic(hv, NULL, PERL_MAGIC_hints);
1413 /* like hv_free_ent, but returns the SV rather than freeing it */
1415 S_hv_free_ent_ret(pTHX_ HV *hv, register HE *entry)
1420 PERL_ARGS_ASSERT_HV_FREE_ENT_RET;
1425 if (HeKLEN(entry) == HEf_SVKEY) {
1426 SvREFCNT_dec(HeKEY_sv(entry));
1427 Safefree(HeKEY_hek(entry));
1429 else if (HvSHAREKEYS(hv))
1430 unshare_hek(HeKEY_hek(entry));
1432 Safefree(HeKEY_hek(entry));
1439 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1444 PERL_ARGS_ASSERT_HV_FREE_ENT;
1448 val = hv_free_ent_ret(hv, entry);
1454 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1458 PERL_ARGS_ASSERT_HV_DELAYFREE_ENT;
1462 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */
1463 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */
1464 if (HeKLEN(entry) == HEf_SVKEY) {
1465 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry)));
1467 hv_free_ent(hv, entry);
1471 =for apidoc hv_clear
1473 Frees the all the elements of a hash, leaving it empty.
1474 The XS equivalent of C<%hash = ()>. See also L</hv_undef>.
1476 If any destructors are triggered as a result, the hv itself may
1483 Perl_hv_clear(pTHX_ HV *hv)
1490 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1492 xhv = (XPVHV*)SvANY(hv);
1495 SAVEFREESV(SvREFCNT_inc_simple_NN(hv));
1496 if (SvREADONLY(hv) && HvARRAY(hv) != NULL) {
1497 /* restricted hash: convert all keys to placeholders */
1499 for (i = 0; i <= xhv->xhv_max; i++) {
1500 HE *entry = (HvARRAY(hv))[i];
1501 for (; entry; entry = HeNEXT(entry)) {
1502 /* not already placeholder */
1503 if (HeVAL(entry) != &PL_sv_placeholder) {
1504 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))
1505 && !SvIsCOW(HeVAL(entry))) {
1506 SV* const keysv = hv_iterkeysv(entry);
1508 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1511 SvREFCNT_dec(HeVAL(entry));
1512 HeVAL(entry) = &PL_sv_placeholder;
1513 HvPLACEHOLDERS(hv)++;
1520 HvPLACEHOLDERS_set(hv, 0);
1523 mg_clear(MUTABLE_SV(hv));
1525 HvHASKFLAGS_off(hv);
1529 mro_isa_changed_in(hv);
1530 HvEITER_set(hv, NULL);
1536 =for apidoc hv_clear_placeholders
1538 Clears any placeholders from a hash. If a restricted hash has any of its keys
1539 marked as readonly and the key is subsequently deleted, the key is not actually
1540 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1541 it so it will be ignored by future operations such as iterating over the hash,
1542 but will still allow the hash to have a value reassigned to the key at some
1543 future point. This function clears any such placeholder keys from the hash.
1544 See Hash::Util::lock_keys() for an example of its use.
1550 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1553 const U32 items = (U32)HvPLACEHOLDERS_get(hv);
1555 PERL_ARGS_ASSERT_HV_CLEAR_PLACEHOLDERS;
1558 clear_placeholders(hv, items);
1562 S_clear_placeholders(pTHX_ HV *hv, U32 items)
1567 PERL_ARGS_ASSERT_CLEAR_PLACEHOLDERS;
1574 /* Loop down the linked list heads */
1575 HE **oentry = &(HvARRAY(hv))[i];
1578 while ((entry = *oentry)) {
1579 if (HeVAL(entry) == &PL_sv_placeholder) {
1580 *oentry = HeNEXT(entry);
1581 if (entry == HvEITER_get(hv))
1584 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1585 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1586 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
1587 hv_free_ent(hv, entry);
1592 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv);
1593 if (HvUSEDKEYS(hv) == 0)
1594 HvHASKFLAGS_off(hv);
1595 HvPLACEHOLDERS_set(hv, 0);
1599 oentry = &HeNEXT(entry);
1603 /* You can't get here, hence assertion should always fail. */
1604 assert (items == 0);
1609 S_hfreeentries(pTHX_ HV *hv)
1612 XPVHV * const xhv = (XPVHV*)SvANY(hv);
1615 PERL_ARGS_ASSERT_HFREEENTRIES;
1617 while ((sv = Perl_hfree_next_entry(aTHX_ hv, &index))||xhv->xhv_keys) {
1623 /* hfree_next_entry()
1624 * For use only by S_hfreeentries() and sv_clear().
1625 * Delete the next available HE from hv and return the associated SV.
1626 * Returns null on empty hash. Nevertheless null is not a reliable
1627 * indicator that the hash is empty, as the deleted entry may have a
1629 * indexp is a pointer to the current index into HvARRAY. The index should
1630 * initially be set to 0. hfree_next_entry() may update it. */
1633 Perl_hfree_next_entry(pTHX_ HV *hv, STRLEN *indexp)
1635 struct xpvhv_aux *iter;
1639 STRLEN orig_index = *indexp;
1642 PERL_ARGS_ASSERT_HFREE_NEXT_ENTRY;
1644 if (SvOOK(hv) && ((iter = HvAUX(hv)))
1645 && ((entry = iter->xhv_eiter)) )
1647 /* the iterator may get resurrected after each
1648 * destructor call, so check each time */
1649 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1651 hv_free_ent(hv, entry);
1652 /* warning: at this point HvARRAY may have been
1653 * re-allocated, HvMAX changed etc */
1655 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1656 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1659 if (!((XPVHV*)SvANY(hv))->xhv_keys)
1662 array = HvARRAY(hv);
1664 while ( ! ((entry = array[*indexp])) ) {
1665 if ((*indexp)++ >= HvMAX(hv))
1667 assert(*indexp != orig_index);
1669 array[*indexp] = HeNEXT(entry);
1670 ((XPVHV*) SvANY(hv))->xhv_keys--;
1672 if ( PL_phase != PERL_PHASE_DESTRUCT && HvENAME(hv)
1673 && HeVAL(entry) && isGV(HeVAL(entry))
1674 && GvHV(HeVAL(entry)) && HvENAME(GvHV(HeVAL(entry)))
1677 const char * const key = HePV(entry,klen);
1678 if ((klen > 1 && key[klen-1]==':' && key[klen-2]==':')
1679 || (klen == 1 && key[0] == ':')) {
1681 NULL, GvHV(HeVAL(entry)),
1682 (GV *)HeVAL(entry), 0
1686 return hv_free_ent_ret(hv, entry);
1691 =for apidoc hv_undef
1693 Undefines the hash. The XS equivalent of C<undef(%hash)>.
1695 As well as freeing all the elements of the hash (like hv_clear()), this
1696 also frees any auxiliary data and storage associated with the hash.
1698 If any destructors are triggered as a result, the hv itself may
1701 See also L</hv_clear>.
1707 Perl_hv_undef_flags(pTHX_ HV *hv, U32 flags)
1712 const bool save = !!SvREFCNT(hv);
1716 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1717 xhv = (XPVHV*)SvANY(hv);
1719 /* The name must be deleted before the call to hfreeeeentries so that
1720 CVs are anonymised properly. But the effective name must be pre-
1721 served until after that call (and only deleted afterwards if the
1722 call originated from sv_clear). For stashes with one name that is
1723 both the canonical name and the effective name, hv_name_set has to
1724 allocate an array for storing the effective name. We can skip that
1725 during global destruction, as it does not matter where the CVs point
1726 if they will be freed anyway. */
1727 /* note that the code following prior to hfreeentries is duplicated
1728 * in sv_clear(), and changes here should be done there too */
1729 if (PL_phase != PERL_PHASE_DESTRUCT && (name = HvNAME(hv))) {
1730 if (PL_stashcache) {
1731 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for '%"
1732 HEKf"'\n", HvNAME_HEK(hv)));
1733 (void)hv_delete(PL_stashcache, name,
1734 HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv),
1738 hv_name_set(hv, NULL, 0, 0);
1742 SAVEFREESV(SvREFCNT_inc_simple_NN(hv));
1746 struct xpvhv_aux * const aux = HvAUX(hv);
1747 struct mro_meta *meta;
1749 if ((name = HvENAME_get(hv))) {
1750 if (PL_phase != PERL_PHASE_DESTRUCT)
1751 mro_isa_changed_in(hv);
1752 if (PL_stashcache) {
1753 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for effective name '%"
1754 HEKf"'\n", HvENAME_HEK(hv)));
1756 PL_stashcache, name,
1757 HEK_UTF8(HvENAME_HEK(hv)) ? -HvENAMELEN_get(hv) : HvENAMELEN_get(hv),
1763 /* If this call originated from sv_clear, then we must check for
1764 * effective names that need freeing, as well as the usual name. */
1766 if (flags & HV_NAME_SETALL ? !!aux->xhv_name_u.xhvnameu_name : !!name) {
1767 if (name && PL_stashcache) {
1768 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for name '%"
1769 HEKf"'\n", HvNAME_HEK(hv)));
1770 (void)hv_delete(PL_stashcache, name, (HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv)), G_DISCARD);
1772 hv_name_set(hv, NULL, 0, flags);
1774 if((meta = aux->xhv_mro_meta)) {
1775 if (meta->mro_linear_all) {
1776 SvREFCNT_dec(MUTABLE_SV(meta->mro_linear_all));
1777 meta->mro_linear_all = NULL;
1778 /* This is just acting as a shortcut pointer. */
1779 meta->mro_linear_current = NULL;
1780 } else if (meta->mro_linear_current) {
1781 /* Only the current MRO is stored, so this owns the data.
1783 SvREFCNT_dec(meta->mro_linear_current);
1784 meta->mro_linear_current = NULL;
1786 SvREFCNT_dec(meta->mro_nextmethod);
1787 SvREFCNT_dec(meta->isa);
1789 aux->xhv_mro_meta = NULL;
1791 SvREFCNT_dec(aux->xhv_super);
1792 if (!aux->xhv_name_u.xhvnameu_name && ! aux->xhv_backreferences)
1793 SvFLAGS(hv) &= ~SVf_OOK;
1796 Safefree(HvARRAY(hv));
1797 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1800 /* if we're freeing the HV, the SvMAGIC field has been reused for
1801 * other purposes, and so there can't be any placeholder magic */
1803 HvPLACEHOLDERS_set(hv, 0);
1806 mg_clear(MUTABLE_SV(hv));
1813 Returns the number of hash buckets that happen to be in use. This function is
1814 wrapped by the macro C<HvFILL>.
1816 Previously this value was stored in the HV structure, rather than being
1817 calculated on demand.
1823 Perl_hv_fill(pTHX_ HV const *const hv)
1826 HE **ents = HvARRAY(hv);
1828 PERL_ARGS_ASSERT_HV_FILL;
1831 HE *const *const last = ents + HvMAX(hv);
1832 count = last + 1 - ents;
1837 } while (++ents <= last);
1842 static struct xpvhv_aux*
1843 S_hv_auxinit(HV *hv) {
1844 struct xpvhv_aux *iter;
1847 PERL_ARGS_ASSERT_HV_AUXINIT;
1850 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1851 + sizeof(struct xpvhv_aux), char);
1853 array = (char *) HvARRAY(hv);
1854 Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1855 + sizeof(struct xpvhv_aux), char);
1857 HvARRAY(hv) = (HE**) array;
1861 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1862 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1863 iter->xhv_name_u.xhvnameu_name = 0;
1864 iter->xhv_name_count = 0;
1865 iter->xhv_backreferences = 0;
1866 iter->xhv_mro_meta = NULL;
1867 iter->xhv_super = NULL;
1872 =for apidoc hv_iterinit
1874 Prepares a starting point to traverse a hash table. Returns the number of
1875 keys in the hash (i.e. the same as C<HvUSEDKEYS(hv)>). The return value is
1876 currently only meaningful for hashes without tie magic.
1878 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1879 hash buckets that happen to be in use. If you still need that esoteric
1880 value, you can get it through the macro C<HvFILL(hv)>.
1887 Perl_hv_iterinit(pTHX_ HV *hv)
1889 PERL_ARGS_ASSERT_HV_ITERINIT;
1891 /* FIXME: Are we not NULL, or do we croak? Place bets now! */
1894 Perl_croak(aTHX_ "Bad hash");
1897 struct xpvhv_aux * const iter = HvAUX(hv);
1898 HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */
1899 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1901 hv_free_ent(hv, entry);
1903 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1904 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1909 /* used to be xhv->xhv_fill before 5.004_65 */
1910 return HvTOTALKEYS(hv);
1914 Perl_hv_riter_p(pTHX_ HV *hv) {
1915 struct xpvhv_aux *iter;
1917 PERL_ARGS_ASSERT_HV_RITER_P;
1920 Perl_croak(aTHX_ "Bad hash");
1922 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1923 return &(iter->xhv_riter);
1927 Perl_hv_eiter_p(pTHX_ HV *hv) {
1928 struct xpvhv_aux *iter;
1930 PERL_ARGS_ASSERT_HV_EITER_P;
1933 Perl_croak(aTHX_ "Bad hash");
1935 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1936 return &(iter->xhv_eiter);
1940 Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) {
1941 struct xpvhv_aux *iter;
1943 PERL_ARGS_ASSERT_HV_RITER_SET;
1946 Perl_croak(aTHX_ "Bad hash");
1954 iter = hv_auxinit(hv);
1956 iter->xhv_riter = riter;
1960 Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) {
1961 struct xpvhv_aux *iter;
1963 PERL_ARGS_ASSERT_HV_EITER_SET;
1966 Perl_croak(aTHX_ "Bad hash");
1971 /* 0 is the default so don't go malloc()ing a new structure just to
1976 iter = hv_auxinit(hv);
1978 iter->xhv_eiter = eiter;
1982 Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
1985 struct xpvhv_aux *iter;
1989 PERL_ARGS_ASSERT_HV_NAME_SET;
1992 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
1996 if (iter->xhv_name_u.xhvnameu_name) {
1997 if(iter->xhv_name_count) {
1998 if(flags & HV_NAME_SETALL) {
1999 HEK ** const name = HvAUX(hv)->xhv_name_u.xhvnameu_names;
2000 HEK **hekp = name + (
2001 iter->xhv_name_count < 0
2002 ? -iter->xhv_name_count
2003 : iter->xhv_name_count
2005 while(hekp-- > name+1)
2006 unshare_hek_or_pvn(*hekp, 0, 0, 0);
2007 /* The first elem may be null. */
2008 if(*name) unshare_hek_or_pvn(*name, 0, 0, 0);
2010 spot = &iter->xhv_name_u.xhvnameu_name;
2011 iter->xhv_name_count = 0;
2014 if(iter->xhv_name_count > 0) {
2015 /* shift some things over */
2017 iter->xhv_name_u.xhvnameu_names, iter->xhv_name_count + 1, HEK *
2019 spot = iter->xhv_name_u.xhvnameu_names;
2020 spot[iter->xhv_name_count] = spot[1];
2022 iter->xhv_name_count = -(iter->xhv_name_count + 1);
2024 else if(*(spot = iter->xhv_name_u.xhvnameu_names)) {
2025 unshare_hek_or_pvn(*spot, 0, 0, 0);
2029 else if (flags & HV_NAME_SETALL) {
2030 unshare_hek_or_pvn(iter->xhv_name_u.xhvnameu_name, 0, 0, 0);
2031 spot = &iter->xhv_name_u.xhvnameu_name;
2034 HEK * const existing_name = iter->xhv_name_u.xhvnameu_name;
2035 Newx(iter->xhv_name_u.xhvnameu_names, 2, HEK *);
2036 iter->xhv_name_count = -2;
2037 spot = iter->xhv_name_u.xhvnameu_names;
2038 spot[1] = existing_name;
2041 else { spot = &iter->xhv_name_u.xhvnameu_name; iter->xhv_name_count = 0; }
2046 iter = hv_auxinit(hv);
2047 spot = &iter->xhv_name_u.xhvnameu_name;
2049 PERL_HASH(hash, name, len);
2050 *spot = name ? share_hek(name, flags & SVf_UTF8 ? -(I32)len : (I32)len, hash) : NULL;
2054 This is basically sv_eq_flags() in sv.c, but we avoid the magic
2059 hek_eq_pvn_flags(pTHX_ const HEK *hek, const char* pv, const I32 pvlen, const U32 flags) {
2060 if ( (HEK_UTF8(hek) ? 1 : 0) != (flags & SVf_UTF8 ? 1 : 0) ) {
2061 if (flags & SVf_UTF8)
2062 return (bytes_cmp_utf8(
2063 (const U8*)HEK_KEY(hek), HEK_LEN(hek),
2064 (const U8*)pv, pvlen) == 0);
2066 return (bytes_cmp_utf8(
2067 (const U8*)pv, pvlen,
2068 (const U8*)HEK_KEY(hek), HEK_LEN(hek)) == 0);
2071 return HEK_LEN(hek) == pvlen && ((HEK_KEY(hek) == pv)
2072 || memEQ(HEK_KEY(hek), pv, pvlen));
2076 =for apidoc hv_ename_add
2078 Adds a name to a stash's internal list of effective names. See
2081 This is called when a stash is assigned to a new location in the symbol
2088 Perl_hv_ename_add(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2091 struct xpvhv_aux *aux = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2094 PERL_ARGS_ASSERT_HV_ENAME_ADD;
2097 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2099 PERL_HASH(hash, name, len);
2101 if (aux->xhv_name_count) {
2102 HEK ** const xhv_name = aux->xhv_name_u.xhvnameu_names;
2103 I32 count = aux->xhv_name_count;
2104 HEK **hekp = xhv_name + (count < 0 ? -count : count);
2105 while (hekp-- > xhv_name)
2107 (HEK_UTF8(*hekp) || (flags & SVf_UTF8))
2108 ? hek_eq_pvn_flags(aTHX_ *hekp, name, (I32)len, flags)
2109 : (HEK_LEN(*hekp) == (I32)len && memEQ(HEK_KEY(*hekp), name, len))
2111 if (hekp == xhv_name && count < 0)
2112 aux->xhv_name_count = -count;
2115 if (count < 0) aux->xhv_name_count--, count = -count;
2116 else aux->xhv_name_count++;
2117 Renew(aux->xhv_name_u.xhvnameu_names, count + 1, HEK *);
2118 (aux->xhv_name_u.xhvnameu_names)[count] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
2121 HEK *existing_name = aux->xhv_name_u.xhvnameu_name;
2124 (HEK_UTF8(existing_name) || (flags & SVf_UTF8))
2125 ? hek_eq_pvn_flags(aTHX_ existing_name, name, (I32)len, flags)
2126 : (HEK_LEN(existing_name) == (I32)len && memEQ(HEK_KEY(existing_name), name, len))
2129 Newx(aux->xhv_name_u.xhvnameu_names, 2, HEK *);
2130 aux->xhv_name_count = existing_name ? 2 : -2;
2131 *aux->xhv_name_u.xhvnameu_names = existing_name;
2132 (aux->xhv_name_u.xhvnameu_names)[1] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
2137 =for apidoc hv_ename_delete
2139 Removes a name from a stash's internal list of effective names. If this is
2140 the name returned by C<HvENAME>, then another name in the list will take
2141 its place (C<HvENAME> will use it).
2143 This is called when a stash is deleted from the symbol table.
2149 Perl_hv_ename_delete(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2152 struct xpvhv_aux *aux;
2154 PERL_ARGS_ASSERT_HV_ENAME_DELETE;
2157 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2159 if (!SvOOK(hv)) return;
2162 if (!aux->xhv_name_u.xhvnameu_name) return;
2164 if (aux->xhv_name_count) {
2165 HEK ** const namep = aux->xhv_name_u.xhvnameu_names;
2166 I32 const count = aux->xhv_name_count;
2167 HEK **victim = namep + (count < 0 ? -count : count);
2168 while (victim-- > namep + 1)
2170 (HEK_UTF8(*victim) || (flags & SVf_UTF8))
2171 ? hek_eq_pvn_flags(aTHX_ *victim, name, (I32)len, flags)
2172 : (HEK_LEN(*victim) == (I32)len && memEQ(HEK_KEY(*victim), name, len))
2174 unshare_hek_or_pvn(*victim, 0, 0, 0);
2175 if (count < 0) ++aux->xhv_name_count;
2176 else --aux->xhv_name_count;
2178 (aux->xhv_name_count == 1 || aux->xhv_name_count == -1)
2180 ) { /* if there are none left */
2182 aux->xhv_name_u.xhvnameu_names = NULL;
2183 aux->xhv_name_count = 0;
2186 /* Move the last one back to fill the empty slot. It
2187 does not matter what order they are in. */
2188 *victim = *(namep + (count < 0 ? -count : count) - 1);
2193 count > 0 && (HEK_UTF8(*namep) || (flags & SVf_UTF8))
2194 ? hek_eq_pvn_flags(aTHX_ *namep, name, (I32)len, flags)
2195 : (HEK_LEN(*namep) == (I32)len && memEQ(HEK_KEY(*namep), name, len))
2197 aux->xhv_name_count = -count;
2201 (HEK_UTF8(aux->xhv_name_u.xhvnameu_name) || (flags & SVf_UTF8))
2202 ? hek_eq_pvn_flags(aTHX_ aux->xhv_name_u.xhvnameu_name, name, (I32)len, flags)
2203 : (HEK_LEN(aux->xhv_name_u.xhvnameu_name) == (I32)len &&
2204 memEQ(HEK_KEY(aux->xhv_name_u.xhvnameu_name), name, len))
2206 HEK * const namehek = aux->xhv_name_u.xhvnameu_name;
2207 Newx(aux->xhv_name_u.xhvnameu_names, 1, HEK *);
2208 *aux->xhv_name_u.xhvnameu_names = namehek;
2209 aux->xhv_name_count = -1;
2214 Perl_hv_backreferences_p(pTHX_ HV *hv) {
2215 struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2217 PERL_ARGS_ASSERT_HV_BACKREFERENCES_P;
2218 PERL_UNUSED_CONTEXT;
2220 return &(iter->xhv_backreferences);
2224 Perl_hv_kill_backrefs(pTHX_ HV *hv) {
2227 PERL_ARGS_ASSERT_HV_KILL_BACKREFS;
2232 av = HvAUX(hv)->xhv_backreferences;
2235 HvAUX(hv)->xhv_backreferences = 0;
2236 Perl_sv_kill_backrefs(aTHX_ MUTABLE_SV(hv), av);
2237 if (SvTYPE(av) == SVt_PVAV)
2243 hv_iternext is implemented as a macro in hv.h
2245 =for apidoc hv_iternext
2247 Returns entries from a hash iterator. See C<hv_iterinit>.
2249 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
2250 iterator currently points to, without losing your place or invalidating your
2251 iterator. Note that in this case the current entry is deleted from the hash
2252 with your iterator holding the last reference to it. Your iterator is flagged
2253 to free the entry on the next call to C<hv_iternext>, so you must not discard
2254 your iterator immediately else the entry will leak - call C<hv_iternext> to
2255 trigger the resource deallocation.
2257 =for apidoc hv_iternext_flags
2259 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
2260 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
2261 set the placeholders keys (for restricted hashes) will be returned in addition
2262 to normal keys. By default placeholders are automatically skipped over.
2263 Currently a placeholder is implemented with a value that is
2264 C<&PL_sv_placeholder>. Note that the implementation of placeholders and
2265 restricted hashes may change, and the implementation currently is
2266 insufficiently abstracted for any change to be tidy.
2272 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2279 struct xpvhv_aux *iter;
2281 PERL_ARGS_ASSERT_HV_ITERNEXT_FLAGS;
2284 Perl_croak(aTHX_ "Bad hash");
2286 xhv = (XPVHV*)SvANY(hv);
2289 /* Too many things (well, pp_each at least) merrily assume that you can
2290 call hv_iternext without calling hv_iterinit, so we'll have to deal
2296 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2297 if (SvMAGICAL(hv) && SvRMAGICAL(hv)) {
2298 if ( ( mg = mg_find((const SV *)hv, PERL_MAGIC_tied) ) ) {
2299 SV * const key = sv_newmortal();
2301 sv_setsv(key, HeSVKEY_force(entry));
2302 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
2303 HeSVKEY_set(entry, NULL);
2309 /* one HE per MAGICAL hash */
2310 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
2311 HvLAZYDEL_on(hv); /* make sure entry gets freed */
2313 Newxz(k, HEK_BASESIZE + sizeof(const SV *), char);
2315 HeKEY_hek(entry) = hek;
2316 HeKLEN(entry) = HEf_SVKEY;
2318 magic_nextpack(MUTABLE_SV(hv),mg,key);
2320 /* force key to stay around until next time */
2321 HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key));
2322 return entry; /* beware, hent_val is not set */
2324 SvREFCNT_dec(HeVAL(entry));
2325 Safefree(HeKEY_hek(entry));
2327 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
2332 #if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */
2333 if (!entry && SvRMAGICAL((const SV *)hv)
2334 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
2337 /* The prime_env_iter() on VMS just loaded up new hash values
2338 * so the iteration count needs to be reset back to the beginning
2342 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2347 /* hv_iterinit now ensures this. */
2348 assert (HvARRAY(hv));
2350 /* At start of hash, entry is NULL. */
2353 entry = HeNEXT(entry);
2354 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2356 * Skip past any placeholders -- don't want to include them in
2359 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
2360 entry = HeNEXT(entry);
2365 /* Skip the entire loop if the hash is empty. */
2366 if ((flags & HV_ITERNEXT_WANTPLACEHOLDERS)
2367 ? HvTOTALKEYS(hv) : HvUSEDKEYS(hv)) {
2369 /* OK. Come to the end of the current list. Grab the next one. */
2371 iter->xhv_riter++; /* HvRITER(hv)++ */
2372 if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2373 /* There is no next one. End of the hash. */
2374 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
2377 entry = (HvARRAY(hv))[iter->xhv_riter];
2379 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2380 /* If we have an entry, but it's a placeholder, don't count it.
2382 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2383 entry = HeNEXT(entry);
2385 /* Will loop again if this linked list starts NULL
2386 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2387 or if we run through it and find only placeholders. */
2390 else iter->xhv_riter = -1;
2392 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2394 hv_free_ent(hv, oldentry);
2397 iter->xhv_eiter = entry; /* HvEITER(hv) = entry */
2402 =for apidoc hv_iterkey
2404 Returns the key from the current position of the hash iterator. See
2411 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
2413 PERL_ARGS_ASSERT_HV_ITERKEY;
2415 if (HeKLEN(entry) == HEf_SVKEY) {
2417 char * const p = SvPV(HeKEY_sv(entry), len);
2422 *retlen = HeKLEN(entry);
2423 return HeKEY(entry);
2427 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2429 =for apidoc hv_iterkeysv
2431 Returns the key as an C<SV*> from the current position of the hash
2432 iterator. The return value will always be a mortal copy of the key. Also
2439 Perl_hv_iterkeysv(pTHX_ register HE *entry)
2441 PERL_ARGS_ASSERT_HV_ITERKEYSV;
2443 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
2447 =for apidoc hv_iterval
2449 Returns the value from the current position of the hash iterator. See
2456 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
2458 PERL_ARGS_ASSERT_HV_ITERVAL;
2460 if (SvRMAGICAL(hv)) {
2461 if (mg_find((const SV *)hv, PERL_MAGIC_tied)) {
2462 SV* const sv = sv_newmortal();
2463 if (HeKLEN(entry) == HEf_SVKEY)
2464 mg_copy(MUTABLE_SV(hv), sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2466 mg_copy(MUTABLE_SV(hv), sv, HeKEY(entry), HeKLEN(entry));
2470 return HeVAL(entry);
2474 =for apidoc hv_iternextsv
2476 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2483 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2485 HE * const he = hv_iternext_flags(hv, 0);
2487 PERL_ARGS_ASSERT_HV_ITERNEXTSV;
2491 *key = hv_iterkey(he, retlen);
2492 return hv_iterval(hv, he);
2499 =for apidoc hv_magic
2501 Adds magic to a hash. See C<sv_magic>.
2506 /* possibly free a shared string if no one has access to it
2507 * len and hash must both be valid for str.
2510 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2512 unshare_hek_or_pvn (NULL, str, len, hash);
2517 Perl_unshare_hek(pTHX_ HEK *hek)
2520 unshare_hek_or_pvn(hek, NULL, 0, 0);
2523 /* possibly free a shared string if no one has access to it
2524 hek if non-NULL takes priority over the other 3, else str, len and hash
2525 are used. If so, len and hash must both be valid for str.
2528 S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash)
2534 bool is_utf8 = FALSE;
2536 const char * const save = str;
2537 struct shared_he *he = NULL;
2540 /* Find the shared he which is just before us in memory. */
2541 he = (struct shared_he *)(((char *)hek)
2542 - STRUCT_OFFSET(struct shared_he,
2545 /* Assert that the caller passed us a genuine (or at least consistent)
2547 assert (he->shared_he_he.hent_hek == hek);
2549 if (he->shared_he_he.he_valu.hent_refcount - 1) {
2550 --he->shared_he_he.he_valu.hent_refcount;
2554 hash = HEK_HASH(hek);
2555 } else if (len < 0) {
2556 STRLEN tmplen = -len;
2558 /* See the note in hv_fetch(). --jhi */
2559 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2562 k_flags = HVhek_UTF8;
2564 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2567 /* what follows was the moral equivalent of:
2568 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2570 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2572 xhv = (XPVHV*)SvANY(PL_strtab);
2573 /* assert(xhv_array != 0) */
2574 oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)];
2576 const HE *const he_he = &(he->shared_he_he);
2577 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2582 const int flags_masked = k_flags & HVhek_MASK;
2583 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2584 if (HeHASH(entry) != hash) /* strings can't be equal */
2586 if (HeKLEN(entry) != len)
2588 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2590 if (HeKFLAGS(entry) != flags_masked)
2597 if (--entry->he_valu.hent_refcount == 0) {
2598 *oentry = HeNEXT(entry);
2600 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
2605 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
2606 "Attempt to free nonexistent shared string '%s'%s"
2608 hek ? HEK_KEY(hek) : str,
2609 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2610 if (k_flags & HVhek_FREEKEY)
2614 /* get a (constant) string ptr from the global string table
2615 * string will get added if it is not already there.
2616 * len and hash must both be valid for str.
2619 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2621 bool is_utf8 = FALSE;
2623 const char * const save = str;
2625 PERL_ARGS_ASSERT_SHARE_HEK;
2628 STRLEN tmplen = -len;
2630 /* See the note in hv_fetch(). --jhi */
2631 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2633 /* If we were able to downgrade here, then than means that we were passed
2634 in a key which only had chars 0-255, but was utf8 encoded. */
2637 /* If we found we were able to downgrade the string to bytes, then
2638 we should flag that it needs upgrading on keys or each. Also flag
2639 that we need share_hek_flags to free the string. */
2641 PERL_HASH(hash, str, len);
2642 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2646 return share_hek_flags (str, len, hash, flags);
2650 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2654 const int flags_masked = flags & HVhek_MASK;
2655 const U32 hindex = hash & (I32) HvMAX(PL_strtab);
2656 XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab);
2658 PERL_ARGS_ASSERT_SHARE_HEK_FLAGS;
2660 /* what follows is the moral equivalent of:
2662 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2663 hv_store(PL_strtab, str, len, NULL, hash);
2665 Can't rehash the shared string table, so not sure if it's worth
2666 counting the number of entries in the linked list
2669 /* assert(xhv_array != 0) */
2670 entry = (HvARRAY(PL_strtab))[hindex];
2671 for (;entry; entry = HeNEXT(entry)) {
2672 if (HeHASH(entry) != hash) /* strings can't be equal */
2674 if (HeKLEN(entry) != len)
2676 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2678 if (HeKFLAGS(entry) != flags_masked)
2684 /* What used to be head of the list.
2685 If this is NULL, then we're the first entry for this slot, which
2686 means we need to increate fill. */
2687 struct shared_he *new_entry;
2690 HE **const head = &HvARRAY(PL_strtab)[hindex];
2691 HE *const next = *head;
2693 /* We don't actually store a HE from the arena and a regular HEK.
2694 Instead we allocate one chunk of memory big enough for both,
2695 and put the HEK straight after the HE. This way we can find the
2696 HE directly from the HEK.
2699 Newx(k, STRUCT_OFFSET(struct shared_he,
2700 shared_he_hek.hek_key[0]) + len + 2, char);
2701 new_entry = (struct shared_he *)k;
2702 entry = &(new_entry->shared_he_he);
2703 hek = &(new_entry->shared_he_hek);
2705 Copy(str, HEK_KEY(hek), len, char);
2706 HEK_KEY(hek)[len] = 0;
2708 HEK_HASH(hek) = hash;
2709 HEK_FLAGS(hek) = (unsigned char)flags_masked;
2711 /* Still "point" to the HEK, so that other code need not know what
2713 HeKEY_hek(entry) = hek;
2714 entry->he_valu.hent_refcount = 0;
2715 HeNEXT(entry) = next;
2718 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
2719 if (!next) { /* initial entry? */
2720 } else if (xhv->xhv_keys > xhv->xhv_max /* HvUSEDKEYS(hv) > HvMAX(hv) */) {
2725 ++entry->he_valu.hent_refcount;
2727 if (flags & HVhek_FREEKEY)
2730 return HeKEY_hek(entry);
2734 Perl_hv_placeholders_p(pTHX_ HV *hv)
2737 MAGIC *mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2739 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_P;
2742 mg = sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, 0);
2745 Perl_die(aTHX_ "panic: hv_placeholders_p");
2748 return &(mg->mg_len);
2753 Perl_hv_placeholders_get(pTHX_ const HV *hv)
2756 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2758 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_GET;
2760 return mg ? mg->mg_len : 0;
2764 Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph)
2767 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2769 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_SET;
2774 if (!sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, ph))
2775 Perl_die(aTHX_ "panic: hv_placeholders_set");
2777 /* else we don't need to add magic to record 0 placeholders. */
2781 S_refcounted_he_value(pTHX_ const struct refcounted_he *he)
2786 PERL_ARGS_ASSERT_REFCOUNTED_HE_VALUE;
2788 switch(he->refcounted_he_data[0] & HVrhek_typemask) {
2793 value = &PL_sv_placeholder;
2796 value = newSViv(he->refcounted_he_val.refcounted_he_u_iv);
2799 value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv);
2802 case HVrhek_PV_UTF8:
2803 /* Create a string SV that directly points to the bytes in our
2805 value = newSV_type(SVt_PV);
2806 SvPV_set(value, (char *) he->refcounted_he_data + 1);
2807 SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len);
2808 /* This stops anything trying to free it */
2809 SvLEN_set(value, 0);
2811 SvREADONLY_on(value);
2812 if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8)
2816 Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %"UVxf,
2817 (UV)he->refcounted_he_data[0]);
2823 =for apidoc m|HV *|refcounted_he_chain_2hv|const struct refcounted_he *c|U32 flags
2825 Generates and returns a C<HV *> representing the content of a
2826 C<refcounted_he> chain.
2827 I<flags> is currently unused and must be zero.
2832 Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain, U32 flags)
2836 U32 placeholders, max;
2839 Perl_croak(aTHX_ "panic: refcounted_he_chain_2hv bad flags %"UVxf,
2842 /* We could chase the chain once to get an idea of the number of keys,
2843 and call ksplit. But for now we'll make a potentially inefficient
2844 hash with only 8 entries in its array. */
2849 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char);
2850 HvARRAY(hv) = (HE**)array;
2856 U32 hash = chain->refcounted_he_hash;
2858 U32 hash = HEK_HASH(chain->refcounted_he_hek);
2860 HE **oentry = &((HvARRAY(hv))[hash & max]);
2861 HE *entry = *oentry;
2864 for (; entry; entry = HeNEXT(entry)) {
2865 if (HeHASH(entry) == hash) {
2866 /* We might have a duplicate key here. If so, entry is older
2867 than the key we've already put in the hash, so if they are
2868 the same, skip adding entry. */
2870 const STRLEN klen = HeKLEN(entry);
2871 const char *const key = HeKEY(entry);
2872 if (klen == chain->refcounted_he_keylen
2873 && (!!HeKUTF8(entry)
2874 == !!(chain->refcounted_he_data[0] & HVhek_UTF8))
2875 && memEQ(key, REF_HE_KEY(chain), klen))
2878 if (HeKEY_hek(entry) == chain->refcounted_he_hek)
2880 if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek)
2881 && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek)
2882 && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek),
2893 = share_hek_flags(REF_HE_KEY(chain),
2894 chain->refcounted_he_keylen,
2895 chain->refcounted_he_hash,
2896 (chain->refcounted_he_data[0]
2897 & (HVhek_UTF8|HVhek_WASUTF8)));
2899 HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek);
2901 value = refcounted_he_value(chain);
2902 if (value == &PL_sv_placeholder)
2904 HeVAL(entry) = value;
2906 /* Link it into the chain. */
2907 HeNEXT(entry) = *oentry;
2913 chain = chain->refcounted_he_next;
2917 clear_placeholders(hv, placeholders);
2918 HvTOTALKEYS(hv) -= placeholders;
2921 /* We could check in the loop to see if we encounter any keys with key
2922 flags, but it's probably not worth it, as this per-hash flag is only
2923 really meant as an optimisation for things like Storable. */
2925 DEBUG_A(Perl_hv_assert(aTHX_ hv));
2931 =for apidoc m|SV *|refcounted_he_fetch_pvn|const struct refcounted_he *chain|const char *keypv|STRLEN keylen|U32 hash|U32 flags
2933 Search along a C<refcounted_he> chain for an entry with the key specified
2934 by I<keypv> and I<keylen>. If I<flags> has the C<REFCOUNTED_HE_KEY_UTF8>
2935 bit set, the key octets are interpreted as UTF-8, otherwise they
2936 are interpreted as Latin-1. I<hash> is a precomputed hash of the key
2937 string, or zero if it has not been precomputed. Returns a mortal scalar
2938 representing the value associated with the key, or C<&PL_sv_placeholder>
2939 if there is no value associated with the key.
2945 Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain,
2946 const char *keypv, STRLEN keylen, U32 hash, U32 flags)
2950 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PVN;
2952 if (flags & ~(REFCOUNTED_HE_KEY_UTF8|REFCOUNTED_HE_EXISTS))
2953 Perl_croak(aTHX_ "panic: refcounted_he_fetch_pvn bad flags %"UVxf,
2956 return &PL_sv_placeholder;
2957 if (flags & REFCOUNTED_HE_KEY_UTF8) {
2958 /* For searching purposes, canonicalise to Latin-1 where possible. */
2959 const char *keyend = keypv + keylen, *p;
2960 STRLEN nonascii_count = 0;
2961 for (p = keypv; p != keyend; p++) {
2964 if (!((c & 0xfe) == 0xc2 && ++p != keyend &&
2965 (((U8)*p) & 0xc0) == 0x80))
2966 goto canonicalised_key;
2970 if (nonascii_count) {
2972 const char *p = keypv, *keyend = keypv + keylen;
2973 keylen -= nonascii_count;
2974 Newx(q, keylen, char);
2977 for (; p != keyend; p++, q++) {
2980 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c);
2983 flags &= ~REFCOUNTED_HE_KEY_UTF8;
2984 canonicalised_key: ;
2986 utf8_flag = (flags & REFCOUNTED_HE_KEY_UTF8) ? HVhek_UTF8 : 0;
2988 PERL_HASH(hash, keypv, keylen);
2990 for (; chain; chain = chain->refcounted_he_next) {
2993 hash == chain->refcounted_he_hash &&
2994 keylen == chain->refcounted_he_keylen &&
2995 memEQ(REF_HE_KEY(chain), keypv, keylen) &&
2996 utf8_flag == (chain->refcounted_he_data[0] & HVhek_UTF8)
2998 hash == HEK_HASH(chain->refcounted_he_hek) &&
2999 keylen == (STRLEN)HEK_LEN(chain->refcounted_he_hek) &&
3000 memEQ(HEK_KEY(chain->refcounted_he_hek), keypv, keylen) &&
3001 utf8_flag == (HEK_FLAGS(chain->refcounted_he_hek) & HVhek_UTF8)
3004 if (flags & REFCOUNTED_HE_EXISTS)
3005 return (chain->refcounted_he_data[0] & HVrhek_typemask)
3007 ? NULL : &PL_sv_yes;
3008 return sv_2mortal(refcounted_he_value(chain));
3011 return flags & REFCOUNTED_HE_EXISTS ? NULL : &PL_sv_placeholder;
3015 =for apidoc m|SV *|refcounted_he_fetch_pv|const struct refcounted_he *chain|const char *key|U32 hash|U32 flags
3017 Like L</refcounted_he_fetch_pvn>, but takes a nul-terminated string
3018 instead of a string/length pair.
3024 Perl_refcounted_he_fetch_pv(pTHX_ const struct refcounted_he *chain,
3025 const char *key, U32 hash, U32 flags)
3027 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PV;
3028 return refcounted_he_fetch_pvn(chain, key, strlen(key), hash, flags);
3032 =for apidoc m|SV *|refcounted_he_fetch_sv|const struct refcounted_he *chain|SV *key|U32 hash|U32 flags
3034 Like L</refcounted_he_fetch_pvn>, but takes a Perl scalar instead of a
3041 Perl_refcounted_he_fetch_sv(pTHX_ const struct refcounted_he *chain,
3042 SV *key, U32 hash, U32 flags)
3046 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_SV;
3047 if (flags & REFCOUNTED_HE_KEY_UTF8)
3048 Perl_croak(aTHX_ "panic: refcounted_he_fetch_sv bad flags %"UVxf,
3050 keypv = SvPV_const(key, keylen);
3052 flags |= REFCOUNTED_HE_KEY_UTF8;
3053 if (!hash && SvIsCOW_shared_hash(key))
3054 hash = SvSHARED_HASH(key);
3055 return refcounted_he_fetch_pvn(chain, keypv, keylen, hash, flags);
3059 =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
3061 Creates a new C<refcounted_he>. This consists of a single key/value
3062 pair and a reference to an existing C<refcounted_he> chain (which may
3063 be empty), and thus forms a longer chain. When using the longer chain,
3064 the new key/value pair takes precedence over any entry for the same key
3065 further along the chain.
3067 The new key is specified by I<keypv> and I<keylen>. If I<flags> has
3068 the C<REFCOUNTED_HE_KEY_UTF8> bit set, the key octets are interpreted
3069 as UTF-8, otherwise they are interpreted as Latin-1. I<hash> is
3070 a precomputed hash of the key string, or zero if it has not been
3073 I<value> is the scalar value to store for this key. I<value> is copied
3074 by this function, which thus does not take ownership of any reference
3075 to it, and later changes to the scalar will not be reflected in the
3076 value visible in the C<refcounted_he>. Complex types of scalar will not
3077 be stored with referential integrity, but will be coerced to strings.
3078 I<value> may be either null or C<&PL_sv_placeholder> to indicate that no
3079 value is to be associated with the key; this, as with any non-null value,
3080 takes precedence over the existence of a value for the key further along
3083 I<parent> points to the rest of the C<refcounted_he> chain to be
3084 attached to the new C<refcounted_he>. This function takes ownership
3085 of one reference to I<parent>, and returns one reference to the new
3091 struct refcounted_he *
3092 Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent,
3093 const char *keypv, STRLEN keylen, U32 hash, SV *value, U32 flags)
3096 STRLEN value_len = 0;
3097 const char *value_p = NULL;
3101 STRLEN key_offset = 1;
3102 struct refcounted_he *he;
3103 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PVN;
3105 if (!value || value == &PL_sv_placeholder) {
3106 value_type = HVrhek_delete;
3107 } else if (SvPOK(value)) {
3108 value_type = HVrhek_PV;
3109 } else if (SvIOK(value)) {
3110 value_type = SvUOK((const SV *)value) ? HVrhek_UV : HVrhek_IV;
3111 } else if (!SvOK(value)) {
3112 value_type = HVrhek_undef;
3114 value_type = HVrhek_PV;
3116 is_pv = value_type == HVrhek_PV;
3118 /* Do it this way so that the SvUTF8() test is after the SvPV, in case
3119 the value is overloaded, and doesn't yet have the UTF-8flag set. */
3120 value_p = SvPV_const(value, value_len);
3122 value_type = HVrhek_PV_UTF8;
3123 key_offset = value_len + 2;
3125 hekflags = value_type;
3127 if (flags & REFCOUNTED_HE_KEY_UTF8) {
3128 /* Canonicalise to Latin-1 where possible. */
3129 const char *keyend = keypv + keylen, *p;
3130 STRLEN nonascii_count = 0;
3131 for (p = keypv; p != keyend; p++) {
3134 if (!((c & 0xfe) == 0xc2 && ++p != keyend &&
3135 (((U8)*p) & 0xc0) == 0x80))
3136 goto canonicalised_key;
3140 if (nonascii_count) {
3142 const char *p = keypv, *keyend = keypv + keylen;
3143 keylen -= nonascii_count;
3144 Newx(q, keylen, char);
3147 for (; p != keyend; p++, q++) {
3150 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c);
3153 flags &= ~REFCOUNTED_HE_KEY_UTF8;
3154 canonicalised_key: ;
3156 if (flags & REFCOUNTED_HE_KEY_UTF8)
3157 hekflags |= HVhek_UTF8;
3159 PERL_HASH(hash, keypv, keylen);
3162 he = (struct refcounted_he*)
3163 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3167 he = (struct refcounted_he*)
3168 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3172 he->refcounted_he_next = parent;
3175 Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char);
3176 he->refcounted_he_val.refcounted_he_u_len = value_len;
3177 } else if (value_type == HVrhek_IV) {
3178 he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value);
3179 } else if (value_type == HVrhek_UV) {
3180 he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value);
3184 he->refcounted_he_hash = hash;
3185 he->refcounted_he_keylen = keylen;
3186 Copy(keypv, he->refcounted_he_data + key_offset, keylen, char);
3188 he->refcounted_he_hek = share_hek_flags(keypv, keylen, hash, hekflags);
3191 he->refcounted_he_data[0] = hekflags;
3192 he->refcounted_he_refcnt = 1;
3198 =for apidoc m|struct refcounted_he *|refcounted_he_new_pv|struct refcounted_he *parent|const char *key|U32 hash|SV *value|U32 flags
3200 Like L</refcounted_he_new_pvn>, but takes a nul-terminated string instead
3201 of a string/length pair.
3206 struct refcounted_he *
3207 Perl_refcounted_he_new_pv(pTHX_ struct refcounted_he *parent,
3208 const char *key, U32 hash, SV *value, U32 flags)
3210 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PV;
3211 return refcounted_he_new_pvn(parent, key, strlen(key), hash, value, flags);
3215 =for apidoc m|struct refcounted_he *|refcounted_he_new_sv|struct refcounted_he *parent|SV *key|U32 hash|SV *value|U32 flags
3217 Like L</refcounted_he_new_pvn>, but takes a Perl scalar instead of a
3223 struct refcounted_he *
3224 Perl_refcounted_he_new_sv(pTHX_ struct refcounted_he *parent,
3225 SV *key, U32 hash, SV *value, U32 flags)
3229 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_SV;
3230 if (flags & REFCOUNTED_HE_KEY_UTF8)
3231 Perl_croak(aTHX_ "panic: refcounted_he_new_sv bad flags %"UVxf,
3233 keypv = SvPV_const(key, keylen);
3235 flags |= REFCOUNTED_HE_KEY_UTF8;
3236 if (!hash && SvIsCOW_shared_hash(key))
3237 hash = SvSHARED_HASH(key);
3238 return refcounted_he_new_pvn(parent, keypv, keylen, hash, value, flags);
3242 =for apidoc m|void|refcounted_he_free|struct refcounted_he *he
3244 Decrements the reference count of a C<refcounted_he> by one. If the
3245 reference count reaches zero the structure's memory is freed, which
3246 (recursively) causes a reduction of its parent C<refcounted_he>'s
3247 reference count. It is safe to pass a null pointer to this function:
3248 no action occurs in this case.
3254 Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) {
3256 PERL_UNUSED_CONTEXT;
3259 struct refcounted_he *copy;
3263 new_count = --he->refcounted_he_refcnt;
3264 HINTS_REFCNT_UNLOCK;
3270 #ifndef USE_ITHREADS
3271 unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0);
3274 he = he->refcounted_he_next;
3275 PerlMemShared_free(copy);
3280 =for apidoc m|struct refcounted_he *|refcounted_he_inc|struct refcounted_he *he
3282 Increment the reference count of a C<refcounted_he>. The pointer to the
3283 C<refcounted_he> is also returned. It is safe to pass a null pointer
3284 to this function: no action occurs and a null pointer is returned.
3289 struct refcounted_he *
3290 Perl_refcounted_he_inc(pTHX_ struct refcounted_he *he)
3295 he->refcounted_he_refcnt++;
3296 HINTS_REFCNT_UNLOCK;
3302 =for apidoc cop_fetch_label
3304 Returns the label attached to a cop.
3305 The flags pointer may be set to C<SVf_UTF8> or 0.
3310 /* pp_entereval is aware that labels are stored with a key ':' at the top of
3313 Perl_cop_fetch_label(pTHX_ COP *const cop, STRLEN *len, U32 *flags) {
3314 struct refcounted_he *const chain = cop->cop_hints_hash;
3316 PERL_ARGS_ASSERT_COP_FETCH_LABEL;
3321 if (chain->refcounted_he_keylen != 1)
3323 if (*REF_HE_KEY(chain) != ':')
3326 if ((STRLEN)HEK_LEN(chain->refcounted_he_hek) != 1)
3328 if (*HEK_KEY(chain->refcounted_he_hek) != ':')
3331 /* Stop anyone trying to really mess us up by adding their own value for
3333 if ((chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV
3334 && (chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV_UTF8)
3338 *len = chain->refcounted_he_val.refcounted_he_u_len;
3340 *flags = ((chain->refcounted_he_data[0] & HVrhek_typemask)
3341 == HVrhek_PV_UTF8) ? SVf_UTF8 : 0;
3343 return chain->refcounted_he_data + 1;
3347 =for apidoc cop_store_label
3349 Save a label into a C<cop_hints_hash>. You need to set flags to C<SVf_UTF8>
3356 Perl_cop_store_label(pTHX_ COP *const cop, const char *label, STRLEN len,
3360 PERL_ARGS_ASSERT_COP_STORE_LABEL;
3362 if (flags & ~(SVf_UTF8))
3363 Perl_croak(aTHX_ "panic: cop_store_label illegal flag bits 0x%" UVxf,
3365 labelsv = newSVpvn_flags(label, len, SVs_TEMP);
3366 if (flags & SVf_UTF8)
3369 = refcounted_he_new_pvs(cop->cop_hints_hash, ":", labelsv, 0);
3373 =for apidoc hv_assert
3375 Check that a hash is in an internally consistent state.
3383 Perl_hv_assert(pTHX_ HV *hv)
3388 int placeholders = 0;
3391 const I32 riter = HvRITER_get(hv);
3392 HE *eiter = HvEITER_get(hv);
3394 PERL_ARGS_ASSERT_HV_ASSERT;
3396 (void)hv_iterinit(hv);
3398 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
3399 /* sanity check the values */
3400 if (HeVAL(entry) == &PL_sv_placeholder)
3404 /* sanity check the keys */
3405 if (HeSVKEY(entry)) {
3406 NOOP; /* Don't know what to check on SV keys. */
3407 } else if (HeKUTF8(entry)) {
3409 if (HeKWASUTF8(entry)) {
3410 PerlIO_printf(Perl_debug_log,
3411 "hash key has both WASUTF8 and UTF8: '%.*s'\n",
3412 (int) HeKLEN(entry), HeKEY(entry));
3415 } else if (HeKWASUTF8(entry))
3418 if (!SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) {
3419 static const char bad_count[] = "Count %d %s(s), but hash reports %d\n";
3420 const int nhashkeys = HvUSEDKEYS(hv);
3421 const int nhashplaceholders = HvPLACEHOLDERS_get(hv);
3423 if (nhashkeys != real) {
3424 PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys );
3427 if (nhashplaceholders != placeholders) {
3428 PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders );
3432 if (withflags && ! HvHASKFLAGS(hv)) {
3433 PerlIO_printf(Perl_debug_log,
3434 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
3439 sv_dump(MUTABLE_SV(hv));
3441 HvRITER_set(hv, riter); /* Restore hash iterator state */
3442 HvEITER_set(hv, eiter);
3449 * c-indentation-style: bsd
3451 * indent-tabs-mode: nil
3454 * ex: set ts=8 sts=4 sw=4 et: