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 DO_HSPLIT(xhv) ((xhv)->xhv_keys > (xhv)->xhv_max) /* HvTOTALKEYS(hv) > HvMAX(hv) */
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, 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)
797 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
798 if ( DO_HSPLIT(xhv) ) {
803 return entry ? (void *) &HeVAL(entry) : NULL;
805 return (void *) entry;
809 S_hv_magic_check(HV *hv, bool *needs_copy, bool *needs_store)
811 const MAGIC *mg = SvMAGIC(hv);
813 PERL_ARGS_ASSERT_HV_MAGIC_CHECK;
818 if (isUPPER(mg->mg_type)) {
820 if (mg->mg_type == PERL_MAGIC_tied) {
821 *needs_store = FALSE;
822 return; /* We've set all there is to set. */
825 mg = mg->mg_moremagic;
830 =for apidoc hv_scalar
832 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
838 Perl_hv_scalar(pTHX_ HV *hv)
842 PERL_ARGS_ASSERT_HV_SCALAR;
844 if (SvRMAGICAL(hv)) {
845 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_tied);
847 return magic_scalarpack(hv, mg);
851 if (HvTOTALKEYS((const HV *)hv))
852 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
853 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
861 =for apidoc hv_delete
863 Deletes a key/value pair in the hash. The value's SV is removed from
864 the hash, made mortal, and returned to the caller. The absolute
865 value of C<klen> is the length of the key. If C<klen> is negative the
866 key is assumed to be in UTF-8-encoded Unicode. The C<flags> value
867 will normally be zero; if set to G_DISCARD then NULL will be returned.
868 NULL will also be returned if the key is not found.
870 =for apidoc hv_delete_ent
872 Deletes a key/value pair in the hash. The value SV is removed from the hash,
873 made mortal, and returned to the caller. The C<flags> value will normally be
874 zero; if set to G_DISCARD then NULL will be returned. NULL will also be
875 returned if the key is not found. C<hash> can be a valid precomputed hash
876 value, or 0 to ask for it to be computed.
882 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
883 int k_flags, I32 d_flags, U32 hash)
889 bool is_utf8 = (k_flags & HVhek_UTF8) ? TRUE : FALSE;
892 if (SvRMAGICAL(hv)) {
895 hv_magic_check (hv, &needs_copy, &needs_store);
899 entry = (HE *) hv_common(hv, keysv, key, klen,
900 k_flags & ~HVhek_FREEKEY,
901 HV_FETCH_LVALUE|HV_DISABLE_UVAR_XKEY,
903 sv = entry ? HeVAL(entry) : NULL;
909 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
910 /* No longer an element */
911 sv_unmagic(sv, PERL_MAGIC_tiedelem);
914 return NULL; /* element cannot be deleted */
916 #ifdef ENV_IS_CASELESS
917 else if (mg_find((const SV *)hv, PERL_MAGIC_env)) {
918 /* XXX This code isn't UTF8 clean. */
919 keysv = newSVpvn_flags(key, klen, SVs_TEMP);
920 if (k_flags & HVhek_FREEKEY) {
923 key = strupr(SvPVX(keysv));
932 xhv = (XPVHV*)SvANY(hv);
936 if (is_utf8 && !(k_flags & HVhek_KEYCANONICAL)) {
937 const char * const keysave = key;
938 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
941 k_flags |= HVhek_UTF8;
943 k_flags &= ~HVhek_UTF8;
944 if (key != keysave) {
945 if (k_flags & HVhek_FREEKEY) {
946 /* This shouldn't happen if our caller does what we expect,
947 but strictly the API allows it. */
950 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
952 HvHASKFLAGS_on(MUTABLE_SV(hv));
956 if (keysv && (SvIsCOW_shared_hash(keysv)))
957 hash = SvSHARED_HASH(keysv);
959 PERL_HASH(hash, key, klen);
962 masked_flags = (k_flags & HVhek_MASK);
964 oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)];
966 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) {
968 U8 mro_changes = 0; /* 1 = isa; 2 = package moved */
972 if (HeHASH(entry) != hash) /* strings can't be equal */
974 if (HeKLEN(entry) != (I32)klen)
976 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
978 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
981 if (hv == PL_strtab) {
982 if (k_flags & HVhek_FREEKEY)
984 Perl_croak(aTHX_ S_strtab_error, "delete");
987 /* if placeholder is here, it's already been deleted.... */
988 if (HeVAL(entry) == &PL_sv_placeholder) {
989 if (k_flags & HVhek_FREEKEY)
993 if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))
994 && !SvIsCOW(HeVAL(entry))) {
995 hv_notallowed(k_flags, key, klen,
996 "Attempt to delete readonly key '%"SVf"' from"
997 " a restricted hash");
999 if (k_flags & HVhek_FREEKEY)
1002 /* If this is a stash and the key ends with ::, then someone is
1003 * deleting a package.
1005 if (HeVAL(entry) && HvENAME_get(hv)) {
1006 gv = (GV *)HeVAL(entry);
1007 if (keysv) key = SvPV(keysv, klen);
1009 (klen > 1 && key[klen-2] == ':' && key[klen-1] == ':')
1011 (klen == 1 && key[0] == ':')
1013 && (klen != 6 || hv!=PL_defstash || memNE(key,"main::",6))
1014 && SvTYPE(gv) == SVt_PVGV && (stash = GvHV((GV *)gv))
1015 && HvENAME_get(stash)) {
1016 /* A previous version of this code checked that the
1017 * GV was still in the symbol table by fetching the
1018 * GV with its name. That is not necessary (and
1019 * sometimes incorrect), as HvENAME cannot be set
1020 * on hv if it is not in the symtab. */
1022 /* Hang on to it for a bit. */
1023 SvREFCNT_inc_simple_void_NN(
1024 sv_2mortal((SV *)gv)
1027 else if (klen == 3 && strnEQ(key, "ISA", 3))
1031 sv = d_flags & G_DISCARD ? HeVAL(entry) : sv_2mortal(HeVAL(entry));
1032 HeVAL(entry) = &PL_sv_placeholder;
1034 /* deletion of method from stash */
1035 if (isGV(sv) && isGV_with_GP(sv) && GvCVu(sv)
1037 mro_method_changed_in(hv);
1041 * If a restricted hash, rather than really deleting the entry, put
1042 * a placeholder there. This marks the key as being "approved", so
1043 * we can still access via not-really-existing key without raising
1047 /* We'll be saving this slot, so the number of allocated keys
1048 * doesn't go down, but the number placeholders goes up */
1049 HvPLACEHOLDERS(hv)++;
1051 *oentry = HeNEXT(entry);
1052 if (SvOOK(hv) && entry == HvAUX(hv)->xhv_eiter /* HvEITER(hv) */)
1055 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1056 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1057 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
1058 hv_free_ent(hv, entry);
1060 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
1061 if (xhv->xhv_keys == 0)
1062 HvHASKFLAGS_off(hv);
1065 if (d_flags & G_DISCARD) {
1070 if (mro_changes == 1) mro_isa_changed_in(hv);
1071 else if (mro_changes == 2)
1072 mro_package_moved(NULL, stash, gv, 1);
1076 if (SvREADONLY(hv)) {
1077 hv_notallowed(k_flags, key, klen,
1078 "Attempt to delete disallowed key '%"SVf"' from"
1079 " a restricted hash");
1082 if (k_flags & HVhek_FREEKEY)
1088 S_hsplit(pTHX_ HV *hv)
1091 XPVHV* const xhv = (XPVHV*)SvANY(hv);
1092 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1093 I32 newsize = oldsize * 2;
1095 char *a = (char*) HvARRAY(hv);
1098 PERL_ARGS_ASSERT_HSPLIT;
1100 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1101 (void*)hv, (int) oldsize);*/
1103 if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) {
1104 /* Can make this clear any placeholders first for non-restricted hashes,
1105 even though Storable rebuilds restricted hashes by putting in all the
1106 placeholders (first) before turning on the readonly flag, because
1107 Storable always pre-splits the hash. */
1108 hv_clear_placeholders(hv);
1112 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1113 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1114 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1120 Move(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1123 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1124 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1129 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1131 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1133 Safefree(HvARRAY(hv));
1137 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1138 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1139 HvARRAY(hv) = (HE**) a;
1142 for (i=0; i<oldsize; i++,aep++) {
1147 if (!entry) /* non-existent */
1151 if ((HeHASH(entry) & newsize) != (U32)i) {
1152 *oentry = HeNEXT(entry);
1153 HeNEXT(entry) = *bep;
1157 oentry = &HeNEXT(entry);
1161 /* I think we don't actually need to keep track of the longest length,
1162 merely flag if anything is too long. But for the moment while
1163 developing this code I'll track it. */
1168 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1171 XPVHV* xhv = (XPVHV*)SvANY(hv);
1172 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1178 PERL_ARGS_ASSERT_HV_KSPLIT;
1180 newsize = (I32) newmax; /* possible truncation here */
1181 if (newsize != newmax || newmax <= oldsize)
1183 while ((newsize & (1 + ~newsize)) != newsize) {
1184 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1186 if (newsize < newmax)
1188 if (newsize < newmax)
1189 return; /* overflow detection */
1191 a = (char *) HvARRAY(hv);
1194 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1195 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1196 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1202 Copy(&a[oldsize * sizeof(HE*)], &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1205 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize)
1206 + (SvOOK(hv) ? sizeof(struct xpvhv_aux) : 0), char);
1211 Copy(HvARRAY(hv), a, oldsize * sizeof(HE*), char);
1213 Copy(HvAUX(hv), &a[newsize * sizeof(HE*)], 1, struct xpvhv_aux);
1215 Safefree(HvARRAY(hv));
1218 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1221 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1223 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1224 HvARRAY(hv) = (HE **) a;
1225 if (!xhv->xhv_keys /* !HvTOTALKEYS(hv) */) /* skip rest if no entries */
1229 for (i=0; i<oldsize; i++,aep++) {
1233 if (!entry) /* non-existent */
1236 I32 j = (HeHASH(entry) & newsize);
1240 *oentry = HeNEXT(entry);
1241 HeNEXT(entry) = aep[j];
1245 oentry = &HeNEXT(entry);
1252 Perl_newHVhv(pTHX_ HV *ohv)
1255 HV * const hv = newHV();
1258 if (!ohv || (!HvTOTALKEYS(ohv) && !SvMAGICAL((const SV *)ohv)))
1260 hv_max = HvMAX(ohv);
1262 if (!SvMAGICAL((const SV *)ohv)) {
1263 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1265 const bool shared = !!HvSHAREKEYS(ohv);
1266 HE **ents, ** const oents = (HE **)HvARRAY(ohv);
1268 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1271 /* In each bucket... */
1272 for (i = 0; i <= hv_max; i++) {
1274 HE *oent = oents[i];
1281 /* Copy the linked list of entries. */
1282 for (; oent; oent = HeNEXT(oent)) {
1283 const U32 hash = HeHASH(oent);
1284 const char * const key = HeKEY(oent);
1285 const STRLEN len = HeKLEN(oent);
1286 const int flags = HeKFLAGS(oent);
1287 HE * const ent = new_HE();
1288 SV *const val = HeVAL(oent);
1290 HeVAL(ent) = SvIMMORTAL(val) ? val : newSVsv(val);
1292 = shared ? share_hek_flags(key, len, hash, flags)
1293 : save_hek_flags(key, len, hash, flags);
1304 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1308 /* Iterate over ohv, copying keys and values one at a time. */
1310 const I32 riter = HvRITER_get(ohv);
1311 HE * const eiter = HvEITER_get(ohv);
1312 STRLEN hv_fill = HvFILL(ohv);
1314 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1315 while (hv_max && hv_max + 1 >= hv_fill * 2)
1316 hv_max = hv_max / 2;
1320 while ((entry = hv_iternext_flags(ohv, 0))) {
1321 SV *val = hv_iterval(ohv,entry);
1322 SV * const keysv = HeSVKEY(entry);
1323 val = SvIMMORTAL(val) ? val : newSVsv(val);
1325 (void)hv_store_ent(hv, keysv, val, 0);
1327 (void)hv_store_flags(hv, HeKEY(entry), HeKLEN(entry), val,
1328 HeHASH(entry), HeKFLAGS(entry));
1330 HvRITER_set(ohv, riter);
1331 HvEITER_set(ohv, eiter);
1338 =for apidoc Am|HV *|hv_copy_hints_hv|HV *ohv
1340 A specialised version of L</newHVhv> for copying C<%^H>. I<ohv> must be
1341 a pointer to a hash (which may have C<%^H> magic, but should be generally
1342 non-magical), or C<NULL> (interpreted as an empty hash). The content
1343 of I<ohv> is copied to a new hash, which has the C<%^H>-specific magic
1344 added to it. A pointer to the new hash is returned.
1350 Perl_hv_copy_hints_hv(pTHX_ HV *const ohv)
1352 HV * const hv = newHV();
1355 STRLEN hv_max = HvMAX(ohv);
1356 STRLEN hv_fill = HvFILL(ohv);
1358 const I32 riter = HvRITER_get(ohv);
1359 HE * const eiter = HvEITER_get(ohv);
1364 while (hv_max && hv_max + 1 >= hv_fill * 2)
1365 hv_max = hv_max / 2;
1369 while ((entry = hv_iternext_flags(ohv, 0))) {
1370 SV *const sv = newSVsv(hv_iterval(ohv,entry));
1371 SV *heksv = HeSVKEY(entry);
1372 if (!heksv && sv) heksv = newSVhek(HeKEY_hek(entry));
1373 if (sv) sv_magic(sv, NULL, PERL_MAGIC_hintselem,
1374 (char *)heksv, HEf_SVKEY);
1375 if (heksv == HeSVKEY(entry))
1376 (void)hv_store_ent(hv, heksv, sv, 0);
1378 (void)hv_common(hv, heksv, HeKEY(entry), HeKLEN(entry),
1379 HeKFLAGS(entry), HV_FETCH_ISSTORE|HV_FETCH_JUST_SV, sv, HeHASH(entry));
1380 SvREFCNT_dec(heksv);
1383 HvRITER_set(ohv, riter);
1384 HvEITER_set(ohv, eiter);
1386 SvREFCNT_inc_simple_void_NN(hv);
1389 hv_magic(hv, NULL, PERL_MAGIC_hints);
1393 /* like hv_free_ent, but returns the SV rather than freeing it */
1395 S_hv_free_ent_ret(pTHX_ HV *hv, HE *entry)
1400 PERL_ARGS_ASSERT_HV_FREE_ENT_RET;
1405 if (HeKLEN(entry) == HEf_SVKEY) {
1406 SvREFCNT_dec(HeKEY_sv(entry));
1407 Safefree(HeKEY_hek(entry));
1409 else if (HvSHAREKEYS(hv))
1410 unshare_hek(HeKEY_hek(entry));
1412 Safefree(HeKEY_hek(entry));
1419 Perl_hv_free_ent(pTHX_ HV *hv, HE *entry)
1424 PERL_ARGS_ASSERT_HV_FREE_ENT;
1428 val = hv_free_ent_ret(hv, entry);
1434 Perl_hv_delayfree_ent(pTHX_ HV *hv, HE *entry)
1438 PERL_ARGS_ASSERT_HV_DELAYFREE_ENT;
1442 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */
1443 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */
1444 if (HeKLEN(entry) == HEf_SVKEY) {
1445 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry)));
1447 hv_free_ent(hv, entry);
1451 =for apidoc hv_clear
1453 Frees the all the elements of a hash, leaving it empty.
1454 The XS equivalent of C<%hash = ()>. See also L</hv_undef>.
1456 If any destructors are triggered as a result, the hv itself may
1463 Perl_hv_clear(pTHX_ HV *hv)
1470 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1472 xhv = (XPVHV*)SvANY(hv);
1475 SAVEFREESV(SvREFCNT_inc_simple_NN(hv));
1476 if (SvREADONLY(hv) && HvARRAY(hv) != NULL) {
1477 /* restricted hash: convert all keys to placeholders */
1479 for (i = 0; i <= xhv->xhv_max; i++) {
1480 HE *entry = (HvARRAY(hv))[i];
1481 for (; entry; entry = HeNEXT(entry)) {
1482 /* not already placeholder */
1483 if (HeVAL(entry) != &PL_sv_placeholder) {
1484 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))
1485 && !SvIsCOW(HeVAL(entry))) {
1486 SV* const keysv = hv_iterkeysv(entry);
1488 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1491 SvREFCNT_dec(HeVAL(entry));
1492 HeVAL(entry) = &PL_sv_placeholder;
1493 HvPLACEHOLDERS(hv)++;
1500 HvPLACEHOLDERS_set(hv, 0);
1503 mg_clear(MUTABLE_SV(hv));
1505 HvHASKFLAGS_off(hv);
1509 mro_isa_changed_in(hv);
1510 HvEITER_set(hv, NULL);
1516 =for apidoc hv_clear_placeholders
1518 Clears any placeholders from a hash. If a restricted hash has any of its keys
1519 marked as readonly and the key is subsequently deleted, the key is not actually
1520 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1521 it so it will be ignored by future operations such as iterating over the hash,
1522 but will still allow the hash to have a value reassigned to the key at some
1523 future point. This function clears any such placeholder keys from the hash.
1524 See Hash::Util::lock_keys() for an example of its use.
1530 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1533 const U32 items = (U32)HvPLACEHOLDERS_get(hv);
1535 PERL_ARGS_ASSERT_HV_CLEAR_PLACEHOLDERS;
1538 clear_placeholders(hv, items);
1542 S_clear_placeholders(pTHX_ HV *hv, U32 items)
1547 PERL_ARGS_ASSERT_CLEAR_PLACEHOLDERS;
1554 /* Loop down the linked list heads */
1555 HE **oentry = &(HvARRAY(hv))[i];
1558 while ((entry = *oentry)) {
1559 if (HeVAL(entry) == &PL_sv_placeholder) {
1560 *oentry = HeNEXT(entry);
1561 if (entry == HvEITER_get(hv))
1564 if (SvOOK(hv) && HvLAZYDEL(hv) &&
1565 entry == HeNEXT(HvAUX(hv)->xhv_eiter))
1566 HeNEXT(HvAUX(hv)->xhv_eiter) = HeNEXT(entry);
1567 hv_free_ent(hv, entry);
1572 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv);
1573 if (HvUSEDKEYS(hv) == 0)
1574 HvHASKFLAGS_off(hv);
1575 HvPLACEHOLDERS_set(hv, 0);
1579 oentry = &HeNEXT(entry);
1583 /* You can't get here, hence assertion should always fail. */
1584 assert (items == 0);
1589 S_hfreeentries(pTHX_ HV *hv)
1592 XPVHV * const xhv = (XPVHV*)SvANY(hv);
1595 PERL_ARGS_ASSERT_HFREEENTRIES;
1597 while ((sv = Perl_hfree_next_entry(aTHX_ hv, &index))||xhv->xhv_keys) {
1603 /* hfree_next_entry()
1604 * For use only by S_hfreeentries() and sv_clear().
1605 * Delete the next available HE from hv and return the associated SV.
1606 * Returns null on empty hash. Nevertheless null is not a reliable
1607 * indicator that the hash is empty, as the deleted entry may have a
1609 * indexp is a pointer to the current index into HvARRAY. The index should
1610 * initially be set to 0. hfree_next_entry() may update it. */
1613 Perl_hfree_next_entry(pTHX_ HV *hv, STRLEN *indexp)
1615 struct xpvhv_aux *iter;
1619 STRLEN orig_index = *indexp;
1622 PERL_ARGS_ASSERT_HFREE_NEXT_ENTRY;
1624 if (SvOOK(hv) && ((iter = HvAUX(hv)))
1625 && ((entry = iter->xhv_eiter)) )
1627 /* the iterator may get resurrected after each
1628 * destructor call, so check each time */
1629 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1631 hv_free_ent(hv, entry);
1632 /* warning: at this point HvARRAY may have been
1633 * re-allocated, HvMAX changed etc */
1635 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1636 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1639 if (!((XPVHV*)SvANY(hv))->xhv_keys)
1642 array = HvARRAY(hv);
1644 while ( ! ((entry = array[*indexp])) ) {
1645 if ((*indexp)++ >= HvMAX(hv))
1647 assert(*indexp != orig_index);
1649 array[*indexp] = HeNEXT(entry);
1650 ((XPVHV*) SvANY(hv))->xhv_keys--;
1652 if ( PL_phase != PERL_PHASE_DESTRUCT && HvENAME(hv)
1653 && HeVAL(entry) && isGV(HeVAL(entry))
1654 && GvHV(HeVAL(entry)) && HvENAME(GvHV(HeVAL(entry)))
1657 const char * const key = HePV(entry,klen);
1658 if ((klen > 1 && key[klen-1]==':' && key[klen-2]==':')
1659 || (klen == 1 && key[0] == ':')) {
1661 NULL, GvHV(HeVAL(entry)),
1662 (GV *)HeVAL(entry), 0
1666 return hv_free_ent_ret(hv, entry);
1671 =for apidoc hv_undef
1673 Undefines the hash. The XS equivalent of C<undef(%hash)>.
1675 As well as freeing all the elements of the hash (like hv_clear()), this
1676 also frees any auxiliary data and storage associated with the hash.
1678 If any destructors are triggered as a result, the hv itself may
1681 See also L</hv_clear>.
1687 Perl_hv_undef_flags(pTHX_ HV *hv, U32 flags)
1692 const bool save = !!SvREFCNT(hv);
1696 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1697 xhv = (XPVHV*)SvANY(hv);
1699 /* The name must be deleted before the call to hfreeeeentries so that
1700 CVs are anonymised properly. But the effective name must be pre-
1701 served until after that call (and only deleted afterwards if the
1702 call originated from sv_clear). For stashes with one name that is
1703 both the canonical name and the effective name, hv_name_set has to
1704 allocate an array for storing the effective name. We can skip that
1705 during global destruction, as it does not matter where the CVs point
1706 if they will be freed anyway. */
1707 /* note that the code following prior to hfreeentries is duplicated
1708 * in sv_clear(), and changes here should be done there too */
1709 if (PL_phase != PERL_PHASE_DESTRUCT && (name = HvNAME(hv))) {
1710 if (PL_stashcache) {
1711 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for '%"
1712 HEKf"'\n", HvNAME_HEK(hv)));
1713 (void)hv_delete(PL_stashcache, name,
1714 HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv),
1718 hv_name_set(hv, NULL, 0, 0);
1722 SAVEFREESV(SvREFCNT_inc_simple_NN(hv));
1726 struct xpvhv_aux * const aux = HvAUX(hv);
1727 struct mro_meta *meta;
1729 if ((name = HvENAME_get(hv))) {
1730 if (PL_phase != PERL_PHASE_DESTRUCT)
1731 mro_isa_changed_in(hv);
1732 if (PL_stashcache) {
1733 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for effective name '%"
1734 HEKf"'\n", HvENAME_HEK(hv)));
1736 PL_stashcache, name,
1737 HEK_UTF8(HvENAME_HEK(hv)) ? -HvENAMELEN_get(hv) : HvENAMELEN_get(hv),
1743 /* If this call originated from sv_clear, then we must check for
1744 * effective names that need freeing, as well as the usual name. */
1746 if (flags & HV_NAME_SETALL ? !!aux->xhv_name_u.xhvnameu_name : !!name) {
1747 if (name && PL_stashcache) {
1748 DEBUG_o(Perl_deb(aTHX_ "hv_undef_flags clearing PL_stashcache for name '%"
1749 HEKf"'\n", HvNAME_HEK(hv)));
1750 (void)hv_delete(PL_stashcache, name, (HEK_UTF8(HvNAME_HEK(hv)) ? -HvNAMELEN_get(hv) : HvNAMELEN_get(hv)), G_DISCARD);
1752 hv_name_set(hv, NULL, 0, flags);
1754 if((meta = aux->xhv_mro_meta)) {
1755 if (meta->mro_linear_all) {
1756 SvREFCNT_dec_NN(meta->mro_linear_all);
1757 /* mro_linear_current is just acting as a shortcut pointer,
1761 /* Only the current MRO is stored, so this owns the data.
1763 SvREFCNT_dec(meta->mro_linear_current);
1764 SvREFCNT_dec(meta->mro_nextmethod);
1765 SvREFCNT_dec(meta->isa);
1767 aux->xhv_mro_meta = NULL;
1769 SvREFCNT_dec(aux->xhv_super);
1770 if (!aux->xhv_name_u.xhvnameu_name && ! aux->xhv_backreferences)
1771 SvFLAGS(hv) &= ~SVf_OOK;
1774 Safefree(HvARRAY(hv));
1775 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1778 /* if we're freeing the HV, the SvMAGIC field has been reused for
1779 * other purposes, and so there can't be any placeholder magic */
1781 HvPLACEHOLDERS_set(hv, 0);
1784 mg_clear(MUTABLE_SV(hv));
1791 Returns the number of hash buckets that happen to be in use. This function is
1792 wrapped by the macro C<HvFILL>.
1794 Previously this value was stored in the HV structure, rather than being
1795 calculated on demand.
1801 Perl_hv_fill(pTHX_ HV const *const hv)
1804 HE **ents = HvARRAY(hv);
1806 PERL_ARGS_ASSERT_HV_FILL;
1809 HE *const *const last = ents + HvMAX(hv);
1810 count = last + 1 - ents;
1815 } while (++ents <= last);
1820 static struct xpvhv_aux*
1821 S_hv_auxinit(HV *hv) {
1822 struct xpvhv_aux *iter;
1825 PERL_ARGS_ASSERT_HV_AUXINIT;
1828 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1829 + sizeof(struct xpvhv_aux), char);
1831 array = (char *) HvARRAY(hv);
1832 Renew(array, PERL_HV_ARRAY_ALLOC_BYTES(HvMAX(hv) + 1)
1833 + sizeof(struct xpvhv_aux), char);
1835 HvARRAY(hv) = (HE**) array;
1839 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1840 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1841 iter->xhv_name_u.xhvnameu_name = 0;
1842 iter->xhv_name_count = 0;
1843 iter->xhv_backreferences = 0;
1844 iter->xhv_mro_meta = NULL;
1845 iter->xhv_super = NULL;
1850 =for apidoc hv_iterinit
1852 Prepares a starting point to traverse a hash table. Returns the number of
1853 keys in the hash (i.e. the same as C<HvUSEDKEYS(hv)>). The return value is
1854 currently only meaningful for hashes without tie magic.
1856 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1857 hash buckets that happen to be in use. If you still need that esoteric
1858 value, you can get it through the macro C<HvFILL(hv)>.
1865 Perl_hv_iterinit(pTHX_ HV *hv)
1867 PERL_ARGS_ASSERT_HV_ITERINIT;
1869 /* FIXME: Are we not NULL, or do we croak? Place bets now! */
1872 Perl_croak(aTHX_ "Bad hash");
1875 struct xpvhv_aux * const iter = HvAUX(hv);
1876 HE * const entry = iter->xhv_eiter; /* HvEITER(hv) */
1877 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1879 hv_free_ent(hv, entry);
1881 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
1882 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
1887 /* used to be xhv->xhv_fill before 5.004_65 */
1888 return HvTOTALKEYS(hv);
1892 Perl_hv_riter_p(pTHX_ HV *hv) {
1893 struct xpvhv_aux *iter;
1895 PERL_ARGS_ASSERT_HV_RITER_P;
1898 Perl_croak(aTHX_ "Bad hash");
1900 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1901 return &(iter->xhv_riter);
1905 Perl_hv_eiter_p(pTHX_ HV *hv) {
1906 struct xpvhv_aux *iter;
1908 PERL_ARGS_ASSERT_HV_EITER_P;
1911 Perl_croak(aTHX_ "Bad hash");
1913 iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
1914 return &(iter->xhv_eiter);
1918 Perl_hv_riter_set(pTHX_ HV *hv, I32 riter) {
1919 struct xpvhv_aux *iter;
1921 PERL_ARGS_ASSERT_HV_RITER_SET;
1924 Perl_croak(aTHX_ "Bad hash");
1932 iter = hv_auxinit(hv);
1934 iter->xhv_riter = riter;
1938 Perl_hv_eiter_set(pTHX_ HV *hv, HE *eiter) {
1939 struct xpvhv_aux *iter;
1941 PERL_ARGS_ASSERT_HV_EITER_SET;
1944 Perl_croak(aTHX_ "Bad hash");
1949 /* 0 is the default so don't go malloc()ing a new structure just to
1954 iter = hv_auxinit(hv);
1956 iter->xhv_eiter = eiter;
1960 Perl_hv_name_set(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
1963 struct xpvhv_aux *iter;
1967 PERL_ARGS_ASSERT_HV_NAME_SET;
1970 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
1974 if (iter->xhv_name_u.xhvnameu_name) {
1975 if(iter->xhv_name_count) {
1976 if(flags & HV_NAME_SETALL) {
1977 HEK ** const name = HvAUX(hv)->xhv_name_u.xhvnameu_names;
1978 HEK **hekp = name + (
1979 iter->xhv_name_count < 0
1980 ? -iter->xhv_name_count
1981 : iter->xhv_name_count
1983 while(hekp-- > name+1)
1984 unshare_hek_or_pvn(*hekp, 0, 0, 0);
1985 /* The first elem may be null. */
1986 if(*name) unshare_hek_or_pvn(*name, 0, 0, 0);
1988 spot = &iter->xhv_name_u.xhvnameu_name;
1989 iter->xhv_name_count = 0;
1992 if(iter->xhv_name_count > 0) {
1993 /* shift some things over */
1995 iter->xhv_name_u.xhvnameu_names, iter->xhv_name_count + 1, HEK *
1997 spot = iter->xhv_name_u.xhvnameu_names;
1998 spot[iter->xhv_name_count] = spot[1];
2000 iter->xhv_name_count = -(iter->xhv_name_count + 1);
2002 else if(*(spot = iter->xhv_name_u.xhvnameu_names)) {
2003 unshare_hek_or_pvn(*spot, 0, 0, 0);
2007 else if (flags & HV_NAME_SETALL) {
2008 unshare_hek_or_pvn(iter->xhv_name_u.xhvnameu_name, 0, 0, 0);
2009 spot = &iter->xhv_name_u.xhvnameu_name;
2012 HEK * const existing_name = iter->xhv_name_u.xhvnameu_name;
2013 Newx(iter->xhv_name_u.xhvnameu_names, 2, HEK *);
2014 iter->xhv_name_count = -2;
2015 spot = iter->xhv_name_u.xhvnameu_names;
2016 spot[1] = existing_name;
2019 else { spot = &iter->xhv_name_u.xhvnameu_name; iter->xhv_name_count = 0; }
2024 iter = hv_auxinit(hv);
2025 spot = &iter->xhv_name_u.xhvnameu_name;
2027 PERL_HASH(hash, name, len);
2028 *spot = name ? share_hek(name, flags & SVf_UTF8 ? -(I32)len : (I32)len, hash) : NULL;
2032 This is basically sv_eq_flags() in sv.c, but we avoid the magic
2037 hek_eq_pvn_flags(pTHX_ const HEK *hek, const char* pv, const I32 pvlen, const U32 flags) {
2038 if ( (HEK_UTF8(hek) ? 1 : 0) != (flags & SVf_UTF8 ? 1 : 0) ) {
2039 if (flags & SVf_UTF8)
2040 return (bytes_cmp_utf8(
2041 (const U8*)HEK_KEY(hek), HEK_LEN(hek),
2042 (const U8*)pv, pvlen) == 0);
2044 return (bytes_cmp_utf8(
2045 (const U8*)pv, pvlen,
2046 (const U8*)HEK_KEY(hek), HEK_LEN(hek)) == 0);
2049 return HEK_LEN(hek) == pvlen && ((HEK_KEY(hek) == pv)
2050 || memEQ(HEK_KEY(hek), pv, pvlen));
2054 =for apidoc hv_ename_add
2056 Adds a name to a stash's internal list of effective names. See
2059 This is called when a stash is assigned to a new location in the symbol
2066 Perl_hv_ename_add(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2069 struct xpvhv_aux *aux = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2072 PERL_ARGS_ASSERT_HV_ENAME_ADD;
2075 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2077 PERL_HASH(hash, name, len);
2079 if (aux->xhv_name_count) {
2080 HEK ** const xhv_name = aux->xhv_name_u.xhvnameu_names;
2081 I32 count = aux->xhv_name_count;
2082 HEK **hekp = xhv_name + (count < 0 ? -count : count);
2083 while (hekp-- > xhv_name)
2085 (HEK_UTF8(*hekp) || (flags & SVf_UTF8))
2086 ? hek_eq_pvn_flags(aTHX_ *hekp, name, (I32)len, flags)
2087 : (HEK_LEN(*hekp) == (I32)len && memEQ(HEK_KEY(*hekp), name, len))
2089 if (hekp == xhv_name && count < 0)
2090 aux->xhv_name_count = -count;
2093 if (count < 0) aux->xhv_name_count--, count = -count;
2094 else aux->xhv_name_count++;
2095 Renew(aux->xhv_name_u.xhvnameu_names, count + 1, HEK *);
2096 (aux->xhv_name_u.xhvnameu_names)[count] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
2099 HEK *existing_name = aux->xhv_name_u.xhvnameu_name;
2102 (HEK_UTF8(existing_name) || (flags & SVf_UTF8))
2103 ? hek_eq_pvn_flags(aTHX_ existing_name, name, (I32)len, flags)
2104 : (HEK_LEN(existing_name) == (I32)len && memEQ(HEK_KEY(existing_name), name, len))
2107 Newx(aux->xhv_name_u.xhvnameu_names, 2, HEK *);
2108 aux->xhv_name_count = existing_name ? 2 : -2;
2109 *aux->xhv_name_u.xhvnameu_names = existing_name;
2110 (aux->xhv_name_u.xhvnameu_names)[1] = share_hek(name, (flags & SVf_UTF8 ? -(I32)len : (I32)len), hash);
2115 =for apidoc hv_ename_delete
2117 Removes a name from a stash's internal list of effective names. If this is
2118 the name returned by C<HvENAME>, then another name in the list will take
2119 its place (C<HvENAME> will use it).
2121 This is called when a stash is deleted from the symbol table.
2127 Perl_hv_ename_delete(pTHX_ HV *hv, const char *name, U32 len, U32 flags)
2130 struct xpvhv_aux *aux;
2132 PERL_ARGS_ASSERT_HV_ENAME_DELETE;
2135 Perl_croak(aTHX_ "panic: hv name too long (%"UVuf")", (UV) len);
2137 if (!SvOOK(hv)) return;
2140 if (!aux->xhv_name_u.xhvnameu_name) return;
2142 if (aux->xhv_name_count) {
2143 HEK ** const namep = aux->xhv_name_u.xhvnameu_names;
2144 I32 const count = aux->xhv_name_count;
2145 HEK **victim = namep + (count < 0 ? -count : count);
2146 while (victim-- > namep + 1)
2148 (HEK_UTF8(*victim) || (flags & SVf_UTF8))
2149 ? hek_eq_pvn_flags(aTHX_ *victim, name, (I32)len, flags)
2150 : (HEK_LEN(*victim) == (I32)len && memEQ(HEK_KEY(*victim), name, len))
2152 unshare_hek_or_pvn(*victim, 0, 0, 0);
2153 if (count < 0) ++aux->xhv_name_count;
2154 else --aux->xhv_name_count;
2156 (aux->xhv_name_count == 1 || aux->xhv_name_count == -1)
2158 ) { /* if there are none left */
2160 aux->xhv_name_u.xhvnameu_names = NULL;
2161 aux->xhv_name_count = 0;
2164 /* Move the last one back to fill the empty slot. It
2165 does not matter what order they are in. */
2166 *victim = *(namep + (count < 0 ? -count : count) - 1);
2171 count > 0 && (HEK_UTF8(*namep) || (flags & SVf_UTF8))
2172 ? hek_eq_pvn_flags(aTHX_ *namep, name, (I32)len, flags)
2173 : (HEK_LEN(*namep) == (I32)len && memEQ(HEK_KEY(*namep), name, len))
2175 aux->xhv_name_count = -count;
2179 (HEK_UTF8(aux->xhv_name_u.xhvnameu_name) || (flags & SVf_UTF8))
2180 ? hek_eq_pvn_flags(aTHX_ aux->xhv_name_u.xhvnameu_name, name, (I32)len, flags)
2181 : (HEK_LEN(aux->xhv_name_u.xhvnameu_name) == (I32)len &&
2182 memEQ(HEK_KEY(aux->xhv_name_u.xhvnameu_name), name, len))
2184 HEK * const namehek = aux->xhv_name_u.xhvnameu_name;
2185 Newx(aux->xhv_name_u.xhvnameu_names, 1, HEK *);
2186 *aux->xhv_name_u.xhvnameu_names = namehek;
2187 aux->xhv_name_count = -1;
2192 Perl_hv_backreferences_p(pTHX_ HV *hv) {
2193 struct xpvhv_aux * const iter = SvOOK(hv) ? HvAUX(hv) : hv_auxinit(hv);
2195 PERL_ARGS_ASSERT_HV_BACKREFERENCES_P;
2196 PERL_UNUSED_CONTEXT;
2198 return &(iter->xhv_backreferences);
2202 Perl_hv_kill_backrefs(pTHX_ HV *hv) {
2205 PERL_ARGS_ASSERT_HV_KILL_BACKREFS;
2210 av = HvAUX(hv)->xhv_backreferences;
2213 HvAUX(hv)->xhv_backreferences = 0;
2214 Perl_sv_kill_backrefs(aTHX_ MUTABLE_SV(hv), av);
2215 if (SvTYPE(av) == SVt_PVAV)
2216 SvREFCNT_dec_NN(av);
2221 hv_iternext is implemented as a macro in hv.h
2223 =for apidoc hv_iternext
2225 Returns entries from a hash iterator. See C<hv_iterinit>.
2227 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
2228 iterator currently points to, without losing your place or invalidating your
2229 iterator. Note that in this case the current entry is deleted from the hash
2230 with your iterator holding the last reference to it. Your iterator is flagged
2231 to free the entry on the next call to C<hv_iternext>, so you must not discard
2232 your iterator immediately else the entry will leak - call C<hv_iternext> to
2233 trigger the resource deallocation.
2235 =for apidoc hv_iternext_flags
2237 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
2238 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
2239 set the placeholders keys (for restricted hashes) will be returned in addition
2240 to normal keys. By default placeholders are automatically skipped over.
2241 Currently a placeholder is implemented with a value that is
2242 C<&PL_sv_placeholder>. Note that the implementation of placeholders and
2243 restricted hashes may change, and the implementation currently is
2244 insufficiently abstracted for any change to be tidy.
2250 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
2257 struct xpvhv_aux *iter;
2259 PERL_ARGS_ASSERT_HV_ITERNEXT_FLAGS;
2262 Perl_croak(aTHX_ "Bad hash");
2264 xhv = (XPVHV*)SvANY(hv);
2267 /* Too many things (well, pp_each at least) merrily assume that you can
2268 call hv_iternext without calling hv_iterinit, so we'll have to deal
2274 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2275 if (SvMAGICAL(hv) && SvRMAGICAL(hv)) {
2276 if ( ( mg = mg_find((const SV *)hv, PERL_MAGIC_tied) ) ) {
2277 SV * const key = sv_newmortal();
2279 sv_setsv(key, HeSVKEY_force(entry));
2280 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
2281 HeSVKEY_set(entry, NULL);
2287 /* one HE per MAGICAL hash */
2288 iter->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
2289 HvLAZYDEL_on(hv); /* make sure entry gets freed */
2291 Newxz(k, HEK_BASESIZE + sizeof(const SV *), char);
2293 HeKEY_hek(entry) = hek;
2294 HeKLEN(entry) = HEf_SVKEY;
2296 magic_nextpack(MUTABLE_SV(hv),mg,key);
2298 /* force key to stay around until next time */
2299 HeSVKEY_set(entry, SvREFCNT_inc_simple_NN(key));
2300 return entry; /* beware, hent_val is not set */
2302 SvREFCNT_dec(HeVAL(entry));
2303 Safefree(HeKEY_hek(entry));
2305 iter->xhv_eiter = NULL; /* HvEITER(hv) = NULL */
2310 #if defined(DYNAMIC_ENV_FETCH) && !defined(__riscos__) /* set up %ENV for iteration */
2311 if (!entry && SvRMAGICAL((const SV *)hv)
2312 && mg_find((const SV *)hv, PERL_MAGIC_env)) {
2315 /* The prime_env_iter() on VMS just loaded up new hash values
2316 * so the iteration count needs to be reset back to the beginning
2320 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
2325 /* hv_iterinit now ensures this. */
2326 assert (HvARRAY(hv));
2328 /* At start of hash, entry is NULL. */
2331 entry = HeNEXT(entry);
2332 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2334 * Skip past any placeholders -- don't want to include them in
2337 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
2338 entry = HeNEXT(entry);
2343 /* Skip the entire loop if the hash is empty. */
2344 if ((flags & HV_ITERNEXT_WANTPLACEHOLDERS)
2345 ? HvTOTALKEYS(hv) : HvUSEDKEYS(hv)) {
2347 /* OK. Come to the end of the current list. Grab the next one. */
2349 iter->xhv_riter++; /* HvRITER(hv)++ */
2350 if (iter->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
2351 /* There is no next one. End of the hash. */
2352 iter->xhv_riter = -1; /* HvRITER(hv) = -1 */
2355 entry = (HvARRAY(hv))[iter->xhv_riter];
2357 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
2358 /* If we have an entry, but it's a placeholder, don't count it.
2360 while (entry && HeVAL(entry) == &PL_sv_placeholder)
2361 entry = HeNEXT(entry);
2363 /* Will loop again if this linked list starts NULL
2364 (for HV_ITERNEXT_WANTPLACEHOLDERS)
2365 or if we run through it and find only placeholders. */
2368 else iter->xhv_riter = -1;
2370 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
2372 hv_free_ent(hv, oldentry);
2375 iter->xhv_eiter = entry; /* HvEITER(hv) = entry */
2380 =for apidoc hv_iterkey
2382 Returns the key from the current position of the hash iterator. See
2389 Perl_hv_iterkey(pTHX_ HE *entry, I32 *retlen)
2391 PERL_ARGS_ASSERT_HV_ITERKEY;
2393 if (HeKLEN(entry) == HEf_SVKEY) {
2395 char * const p = SvPV(HeKEY_sv(entry), len);
2400 *retlen = HeKLEN(entry);
2401 return HeKEY(entry);
2405 /* unlike hv_iterval(), this always returns a mortal copy of the key */
2407 =for apidoc hv_iterkeysv
2409 Returns the key as an C<SV*> from the current position of the hash
2410 iterator. The return value will always be a mortal copy of the key. Also
2417 Perl_hv_iterkeysv(pTHX_ HE *entry)
2419 PERL_ARGS_ASSERT_HV_ITERKEYSV;
2421 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
2425 =for apidoc hv_iterval
2427 Returns the value from the current position of the hash iterator. See
2434 Perl_hv_iterval(pTHX_ HV *hv, HE *entry)
2436 PERL_ARGS_ASSERT_HV_ITERVAL;
2438 if (SvRMAGICAL(hv)) {
2439 if (mg_find((const SV *)hv, PERL_MAGIC_tied)) {
2440 SV* const sv = sv_newmortal();
2441 if (HeKLEN(entry) == HEf_SVKEY)
2442 mg_copy(MUTABLE_SV(hv), sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
2444 mg_copy(MUTABLE_SV(hv), sv, HeKEY(entry), HeKLEN(entry));
2448 return HeVAL(entry);
2452 =for apidoc hv_iternextsv
2454 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
2461 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
2463 HE * const he = hv_iternext_flags(hv, 0);
2465 PERL_ARGS_ASSERT_HV_ITERNEXTSV;
2469 *key = hv_iterkey(he, retlen);
2470 return hv_iterval(hv, he);
2477 =for apidoc hv_magic
2479 Adds magic to a hash. See C<sv_magic>.
2484 /* possibly free a shared string if no one has access to it
2485 * len and hash must both be valid for str.
2488 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
2490 unshare_hek_or_pvn (NULL, str, len, hash);
2495 Perl_unshare_hek(pTHX_ HEK *hek)
2498 unshare_hek_or_pvn(hek, NULL, 0, 0);
2501 /* possibly free a shared string if no one has access to it
2502 hek if non-NULL takes priority over the other 3, else str, len and hash
2503 are used. If so, len and hash must both be valid for str.
2506 S_unshare_hek_or_pvn(pTHX_ const HEK *hek, const char *str, I32 len, U32 hash)
2512 bool is_utf8 = FALSE;
2514 const char * const save = str;
2515 struct shared_he *he = NULL;
2518 /* Find the shared he which is just before us in memory. */
2519 he = (struct shared_he *)(((char *)hek)
2520 - STRUCT_OFFSET(struct shared_he,
2523 /* Assert that the caller passed us a genuine (or at least consistent)
2525 assert (he->shared_he_he.hent_hek == hek);
2527 if (he->shared_he_he.he_valu.hent_refcount - 1) {
2528 --he->shared_he_he.he_valu.hent_refcount;
2532 hash = HEK_HASH(hek);
2533 } else if (len < 0) {
2534 STRLEN tmplen = -len;
2536 /* See the note in hv_fetch(). --jhi */
2537 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2540 k_flags = HVhek_UTF8;
2542 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2545 /* what follows was the moral equivalent of:
2546 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
2548 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
2550 xhv = (XPVHV*)SvANY(PL_strtab);
2551 /* assert(xhv_array != 0) */
2552 oentry = &(HvARRAY(PL_strtab))[hash & (I32) HvMAX(PL_strtab)];
2554 const HE *const he_he = &(he->shared_he_he);
2555 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2560 const int flags_masked = k_flags & HVhek_MASK;
2561 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
2562 if (HeHASH(entry) != hash) /* strings can't be equal */
2564 if (HeKLEN(entry) != len)
2566 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2568 if (HeKFLAGS(entry) != flags_masked)
2575 if (--entry->he_valu.hent_refcount == 0) {
2576 *oentry = HeNEXT(entry);
2578 xhv->xhv_keys--; /* HvTOTALKEYS(hv)-- */
2583 Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL),
2584 "Attempt to free nonexistent shared string '%s'%s"
2586 hek ? HEK_KEY(hek) : str,
2587 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2588 if (k_flags & HVhek_FREEKEY)
2592 /* get a (constant) string ptr from the global string table
2593 * string will get added if it is not already there.
2594 * len and hash must both be valid for str.
2597 Perl_share_hek(pTHX_ const char *str, I32 len, U32 hash)
2599 bool is_utf8 = FALSE;
2601 const char * const save = str;
2603 PERL_ARGS_ASSERT_SHARE_HEK;
2606 STRLEN tmplen = -len;
2608 /* See the note in hv_fetch(). --jhi */
2609 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2611 /* If we were able to downgrade here, then than means that we were passed
2612 in a key which only had chars 0-255, but was utf8 encoded. */
2615 /* If we found we were able to downgrade the string to bytes, then
2616 we should flag that it needs upgrading on keys or each. Also flag
2617 that we need share_hek_flags to free the string. */
2620 PERL_HASH(hash, str, len);
2621 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2625 return share_hek_flags (str, len, hash, flags);
2629 S_share_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
2633 const int flags_masked = flags & HVhek_MASK;
2634 const U32 hindex = hash & (I32) HvMAX(PL_strtab);
2635 XPVHV * const xhv = (XPVHV*)SvANY(PL_strtab);
2637 PERL_ARGS_ASSERT_SHARE_HEK_FLAGS;
2639 /* what follows is the moral equivalent of:
2641 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2642 hv_store(PL_strtab, str, len, NULL, hash);
2644 Can't rehash the shared string table, so not sure if it's worth
2645 counting the number of entries in the linked list
2648 /* assert(xhv_array != 0) */
2649 entry = (HvARRAY(PL_strtab))[hindex];
2650 for (;entry; entry = HeNEXT(entry)) {
2651 if (HeHASH(entry) != hash) /* strings can't be equal */
2653 if (HeKLEN(entry) != len)
2655 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2657 if (HeKFLAGS(entry) != flags_masked)
2663 /* What used to be head of the list.
2664 If this is NULL, then we're the first entry for this slot, which
2665 means we need to increate fill. */
2666 struct shared_he *new_entry;
2669 HE **const head = &HvARRAY(PL_strtab)[hindex];
2670 HE *const next = *head;
2672 /* We don't actually store a HE from the arena and a regular HEK.
2673 Instead we allocate one chunk of memory big enough for both,
2674 and put the HEK straight after the HE. This way we can find the
2675 HE directly from the HEK.
2678 Newx(k, STRUCT_OFFSET(struct shared_he,
2679 shared_he_hek.hek_key[0]) + len + 2, char);
2680 new_entry = (struct shared_he *)k;
2681 entry = &(new_entry->shared_he_he);
2682 hek = &(new_entry->shared_he_hek);
2684 Copy(str, HEK_KEY(hek), len, char);
2685 HEK_KEY(hek)[len] = 0;
2687 HEK_HASH(hek) = hash;
2688 HEK_FLAGS(hek) = (unsigned char)flags_masked;
2690 /* Still "point" to the HEK, so that other code need not know what
2692 HeKEY_hek(entry) = hek;
2693 entry->he_valu.hent_refcount = 0;
2694 HeNEXT(entry) = next;
2697 xhv->xhv_keys++; /* HvTOTALKEYS(hv)++ */
2698 if (!next) { /* initial entry? */
2699 } else if ( DO_HSPLIT(xhv) ) {
2704 ++entry->he_valu.hent_refcount;
2706 if (flags & HVhek_FREEKEY)
2709 return HeKEY_hek(entry);
2713 Perl_hv_placeholders_p(pTHX_ HV *hv)
2716 MAGIC *mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2718 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_P;
2721 mg = sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, 0);
2724 Perl_die(aTHX_ "panic: hv_placeholders_p");
2727 return &(mg->mg_len);
2732 Perl_hv_placeholders_get(pTHX_ const HV *hv)
2735 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2737 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_GET;
2739 return mg ? mg->mg_len : 0;
2743 Perl_hv_placeholders_set(pTHX_ HV *hv, I32 ph)
2746 MAGIC * const mg = mg_find((const SV *)hv, PERL_MAGIC_rhash);
2748 PERL_ARGS_ASSERT_HV_PLACEHOLDERS_SET;
2753 if (!sv_magicext(MUTABLE_SV(hv), 0, PERL_MAGIC_rhash, 0, 0, ph))
2754 Perl_die(aTHX_ "panic: hv_placeholders_set");
2756 /* else we don't need to add magic to record 0 placeholders. */
2760 S_refcounted_he_value(pTHX_ const struct refcounted_he *he)
2765 PERL_ARGS_ASSERT_REFCOUNTED_HE_VALUE;
2767 switch(he->refcounted_he_data[0] & HVrhek_typemask) {
2772 value = &PL_sv_placeholder;
2775 value = newSViv(he->refcounted_he_val.refcounted_he_u_iv);
2778 value = newSVuv(he->refcounted_he_val.refcounted_he_u_uv);
2781 case HVrhek_PV_UTF8:
2782 /* Create a string SV that directly points to the bytes in our
2784 value = newSV_type(SVt_PV);
2785 SvPV_set(value, (char *) he->refcounted_he_data + 1);
2786 SvCUR_set(value, he->refcounted_he_val.refcounted_he_u_len);
2787 /* This stops anything trying to free it */
2788 SvLEN_set(value, 0);
2790 SvREADONLY_on(value);
2791 if ((he->refcounted_he_data[0] & HVrhek_typemask) == HVrhek_PV_UTF8)
2795 Perl_croak(aTHX_ "panic: refcounted_he_value bad flags %"UVxf,
2796 (UV)he->refcounted_he_data[0]);
2802 =for apidoc m|HV *|refcounted_he_chain_2hv|const struct refcounted_he *c|U32 flags
2804 Generates and returns a C<HV *> representing the content of a
2805 C<refcounted_he> chain.
2806 I<flags> is currently unused and must be zero.
2811 Perl_refcounted_he_chain_2hv(pTHX_ const struct refcounted_he *chain, U32 flags)
2815 U32 placeholders, max;
2818 Perl_croak(aTHX_ "panic: refcounted_he_chain_2hv bad flags %"UVxf,
2821 /* We could chase the chain once to get an idea of the number of keys,
2822 and call ksplit. But for now we'll make a potentially inefficient
2823 hash with only 8 entries in its array. */
2828 Newxz(array, PERL_HV_ARRAY_ALLOC_BYTES(max + 1), char);
2829 HvARRAY(hv) = (HE**)array;
2835 U32 hash = chain->refcounted_he_hash;
2837 U32 hash = HEK_HASH(chain->refcounted_he_hek);
2839 HE **oentry = &((HvARRAY(hv))[hash & max]);
2840 HE *entry = *oentry;
2843 for (; entry; entry = HeNEXT(entry)) {
2844 if (HeHASH(entry) == hash) {
2845 /* We might have a duplicate key here. If so, entry is older
2846 than the key we've already put in the hash, so if they are
2847 the same, skip adding entry. */
2849 const STRLEN klen = HeKLEN(entry);
2850 const char *const key = HeKEY(entry);
2851 if (klen == chain->refcounted_he_keylen
2852 && (!!HeKUTF8(entry)
2853 == !!(chain->refcounted_he_data[0] & HVhek_UTF8))
2854 && memEQ(key, REF_HE_KEY(chain), klen))
2857 if (HeKEY_hek(entry) == chain->refcounted_he_hek)
2859 if (HeKLEN(entry) == HEK_LEN(chain->refcounted_he_hek)
2860 && HeKUTF8(entry) == HEK_UTF8(chain->refcounted_he_hek)
2861 && memEQ(HeKEY(entry), HEK_KEY(chain->refcounted_he_hek),
2872 = share_hek_flags(REF_HE_KEY(chain),
2873 chain->refcounted_he_keylen,
2874 chain->refcounted_he_hash,
2875 (chain->refcounted_he_data[0]
2876 & (HVhek_UTF8|HVhek_WASUTF8)));
2878 HeKEY_hek(entry) = share_hek_hek(chain->refcounted_he_hek);
2880 value = refcounted_he_value(chain);
2881 if (value == &PL_sv_placeholder)
2883 HeVAL(entry) = value;
2885 /* Link it into the chain. */
2886 HeNEXT(entry) = *oentry;
2892 chain = chain->refcounted_he_next;
2896 clear_placeholders(hv, placeholders);
2897 HvTOTALKEYS(hv) -= placeholders;
2900 /* We could check in the loop to see if we encounter any keys with key
2901 flags, but it's probably not worth it, as this per-hash flag is only
2902 really meant as an optimisation for things like Storable. */
2904 DEBUG_A(Perl_hv_assert(aTHX_ hv));
2910 =for apidoc m|SV *|refcounted_he_fetch_pvn|const struct refcounted_he *chain|const char *keypv|STRLEN keylen|U32 hash|U32 flags
2912 Search along a C<refcounted_he> chain for an entry with the key specified
2913 by I<keypv> and I<keylen>. If I<flags> has the C<REFCOUNTED_HE_KEY_UTF8>
2914 bit set, the key octets are interpreted as UTF-8, otherwise they
2915 are interpreted as Latin-1. I<hash> is a precomputed hash of the key
2916 string, or zero if it has not been precomputed. Returns a mortal scalar
2917 representing the value associated with the key, or C<&PL_sv_placeholder>
2918 if there is no value associated with the key.
2924 Perl_refcounted_he_fetch_pvn(pTHX_ const struct refcounted_he *chain,
2925 const char *keypv, STRLEN keylen, U32 hash, U32 flags)
2929 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PVN;
2931 if (flags & ~(REFCOUNTED_HE_KEY_UTF8|REFCOUNTED_HE_EXISTS))
2932 Perl_croak(aTHX_ "panic: refcounted_he_fetch_pvn bad flags %"UVxf,
2935 return &PL_sv_placeholder;
2936 if (flags & REFCOUNTED_HE_KEY_UTF8) {
2937 /* For searching purposes, canonicalise to Latin-1 where possible. */
2938 const char *keyend = keypv + keylen, *p;
2939 STRLEN nonascii_count = 0;
2940 for (p = keypv; p != keyend; p++) {
2943 if (!((c & 0xfe) == 0xc2 && ++p != keyend &&
2944 (((U8)*p) & 0xc0) == 0x80))
2945 goto canonicalised_key;
2949 if (nonascii_count) {
2951 const char *p = keypv, *keyend = keypv + keylen;
2952 keylen -= nonascii_count;
2953 Newx(q, keylen, char);
2956 for (; p != keyend; p++, q++) {
2959 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c);
2962 flags &= ~REFCOUNTED_HE_KEY_UTF8;
2963 canonicalised_key: ;
2965 utf8_flag = (flags & REFCOUNTED_HE_KEY_UTF8) ? HVhek_UTF8 : 0;
2967 PERL_HASH(hash, keypv, keylen);
2969 for (; chain; chain = chain->refcounted_he_next) {
2972 hash == chain->refcounted_he_hash &&
2973 keylen == chain->refcounted_he_keylen &&
2974 memEQ(REF_HE_KEY(chain), keypv, keylen) &&
2975 utf8_flag == (chain->refcounted_he_data[0] & HVhek_UTF8)
2977 hash == HEK_HASH(chain->refcounted_he_hek) &&
2978 keylen == (STRLEN)HEK_LEN(chain->refcounted_he_hek) &&
2979 memEQ(HEK_KEY(chain->refcounted_he_hek), keypv, keylen) &&
2980 utf8_flag == (HEK_FLAGS(chain->refcounted_he_hek) & HVhek_UTF8)
2983 if (flags & REFCOUNTED_HE_EXISTS)
2984 return (chain->refcounted_he_data[0] & HVrhek_typemask)
2986 ? NULL : &PL_sv_yes;
2987 return sv_2mortal(refcounted_he_value(chain));
2990 return flags & REFCOUNTED_HE_EXISTS ? NULL : &PL_sv_placeholder;
2994 =for apidoc m|SV *|refcounted_he_fetch_pv|const struct refcounted_he *chain|const char *key|U32 hash|U32 flags
2996 Like L</refcounted_he_fetch_pvn>, but takes a nul-terminated string
2997 instead of a string/length pair.
3003 Perl_refcounted_he_fetch_pv(pTHX_ const struct refcounted_he *chain,
3004 const char *key, U32 hash, U32 flags)
3006 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_PV;
3007 return refcounted_he_fetch_pvn(chain, key, strlen(key), hash, flags);
3011 =for apidoc m|SV *|refcounted_he_fetch_sv|const struct refcounted_he *chain|SV *key|U32 hash|U32 flags
3013 Like L</refcounted_he_fetch_pvn>, but takes a Perl scalar instead of a
3020 Perl_refcounted_he_fetch_sv(pTHX_ const struct refcounted_he *chain,
3021 SV *key, U32 hash, U32 flags)
3025 PERL_ARGS_ASSERT_REFCOUNTED_HE_FETCH_SV;
3026 if (flags & REFCOUNTED_HE_KEY_UTF8)
3027 Perl_croak(aTHX_ "panic: refcounted_he_fetch_sv bad flags %"UVxf,
3029 keypv = SvPV_const(key, keylen);
3031 flags |= REFCOUNTED_HE_KEY_UTF8;
3032 if (!hash && SvIsCOW_shared_hash(key))
3033 hash = SvSHARED_HASH(key);
3034 return refcounted_he_fetch_pvn(chain, keypv, keylen, hash, flags);
3038 =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
3040 Creates a new C<refcounted_he>. This consists of a single key/value
3041 pair and a reference to an existing C<refcounted_he> chain (which may
3042 be empty), and thus forms a longer chain. When using the longer chain,
3043 the new key/value pair takes precedence over any entry for the same key
3044 further along the chain.
3046 The new key is specified by I<keypv> and I<keylen>. If I<flags> has
3047 the C<REFCOUNTED_HE_KEY_UTF8> bit set, the key octets are interpreted
3048 as UTF-8, otherwise they are interpreted as Latin-1. I<hash> is
3049 a precomputed hash of the key string, or zero if it has not been
3052 I<value> is the scalar value to store for this key. I<value> is copied
3053 by this function, which thus does not take ownership of any reference
3054 to it, and later changes to the scalar will not be reflected in the
3055 value visible in the C<refcounted_he>. Complex types of scalar will not
3056 be stored with referential integrity, but will be coerced to strings.
3057 I<value> may be either null or C<&PL_sv_placeholder> to indicate that no
3058 value is to be associated with the key; this, as with any non-null value,
3059 takes precedence over the existence of a value for the key further along
3062 I<parent> points to the rest of the C<refcounted_he> chain to be
3063 attached to the new C<refcounted_he>. This function takes ownership
3064 of one reference to I<parent>, and returns one reference to the new
3070 struct refcounted_he *
3071 Perl_refcounted_he_new_pvn(pTHX_ struct refcounted_he *parent,
3072 const char *keypv, STRLEN keylen, U32 hash, SV *value, U32 flags)
3075 STRLEN value_len = 0;
3076 const char *value_p = NULL;
3080 STRLEN key_offset = 1;
3081 struct refcounted_he *he;
3082 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PVN;
3084 if (!value || value == &PL_sv_placeholder) {
3085 value_type = HVrhek_delete;
3086 } else if (SvPOK(value)) {
3087 value_type = HVrhek_PV;
3088 } else if (SvIOK(value)) {
3089 value_type = SvUOK((const SV *)value) ? HVrhek_UV : HVrhek_IV;
3090 } else if (!SvOK(value)) {
3091 value_type = HVrhek_undef;
3093 value_type = HVrhek_PV;
3095 is_pv = value_type == HVrhek_PV;
3097 /* Do it this way so that the SvUTF8() test is after the SvPV, in case
3098 the value is overloaded, and doesn't yet have the UTF-8flag set. */
3099 value_p = SvPV_const(value, value_len);
3101 value_type = HVrhek_PV_UTF8;
3102 key_offset = value_len + 2;
3104 hekflags = value_type;
3106 if (flags & REFCOUNTED_HE_KEY_UTF8) {
3107 /* Canonicalise to Latin-1 where possible. */
3108 const char *keyend = keypv + keylen, *p;
3109 STRLEN nonascii_count = 0;
3110 for (p = keypv; p != keyend; p++) {
3113 if (!((c & 0xfe) == 0xc2 && ++p != keyend &&
3114 (((U8)*p) & 0xc0) == 0x80))
3115 goto canonicalised_key;
3119 if (nonascii_count) {
3121 const char *p = keypv, *keyend = keypv + keylen;
3122 keylen -= nonascii_count;
3123 Newx(q, keylen, char);
3126 for (; p != keyend; p++, q++) {
3129 ((c & 0x80) ? ((c & 0x03) << 6) | (((U8)*++p) & 0x3f) : c);
3132 flags &= ~REFCOUNTED_HE_KEY_UTF8;
3133 canonicalised_key: ;
3135 if (flags & REFCOUNTED_HE_KEY_UTF8)
3136 hekflags |= HVhek_UTF8;
3138 PERL_HASH(hash, keypv, keylen);
3141 he = (struct refcounted_he*)
3142 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3146 he = (struct refcounted_he*)
3147 PerlMemShared_malloc(sizeof(struct refcounted_he) - 1
3151 he->refcounted_he_next = parent;
3154 Copy(value_p, he->refcounted_he_data + 1, value_len + 1, char);
3155 he->refcounted_he_val.refcounted_he_u_len = value_len;
3156 } else if (value_type == HVrhek_IV) {
3157 he->refcounted_he_val.refcounted_he_u_iv = SvIVX(value);
3158 } else if (value_type == HVrhek_UV) {
3159 he->refcounted_he_val.refcounted_he_u_uv = SvUVX(value);
3163 he->refcounted_he_hash = hash;
3164 he->refcounted_he_keylen = keylen;
3165 Copy(keypv, he->refcounted_he_data + key_offset, keylen, char);
3167 he->refcounted_he_hek = share_hek_flags(keypv, keylen, hash, hekflags);
3170 he->refcounted_he_data[0] = hekflags;
3171 he->refcounted_he_refcnt = 1;
3177 =for apidoc m|struct refcounted_he *|refcounted_he_new_pv|struct refcounted_he *parent|const char *key|U32 hash|SV *value|U32 flags
3179 Like L</refcounted_he_new_pvn>, but takes a nul-terminated string instead
3180 of a string/length pair.
3185 struct refcounted_he *
3186 Perl_refcounted_he_new_pv(pTHX_ struct refcounted_he *parent,
3187 const char *key, U32 hash, SV *value, U32 flags)
3189 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_PV;
3190 return refcounted_he_new_pvn(parent, key, strlen(key), hash, value, flags);
3194 =for apidoc m|struct refcounted_he *|refcounted_he_new_sv|struct refcounted_he *parent|SV *key|U32 hash|SV *value|U32 flags
3196 Like L</refcounted_he_new_pvn>, but takes a Perl scalar instead of a
3202 struct refcounted_he *
3203 Perl_refcounted_he_new_sv(pTHX_ struct refcounted_he *parent,
3204 SV *key, U32 hash, SV *value, U32 flags)
3208 PERL_ARGS_ASSERT_REFCOUNTED_HE_NEW_SV;
3209 if (flags & REFCOUNTED_HE_KEY_UTF8)
3210 Perl_croak(aTHX_ "panic: refcounted_he_new_sv bad flags %"UVxf,
3212 keypv = SvPV_const(key, keylen);
3214 flags |= REFCOUNTED_HE_KEY_UTF8;
3215 if (!hash && SvIsCOW_shared_hash(key))
3216 hash = SvSHARED_HASH(key);
3217 return refcounted_he_new_pvn(parent, keypv, keylen, hash, value, flags);
3221 =for apidoc m|void|refcounted_he_free|struct refcounted_he *he
3223 Decrements the reference count of a C<refcounted_he> by one. If the
3224 reference count reaches zero the structure's memory is freed, which
3225 (recursively) causes a reduction of its parent C<refcounted_he>'s
3226 reference count. It is safe to pass a null pointer to this function:
3227 no action occurs in this case.
3233 Perl_refcounted_he_free(pTHX_ struct refcounted_he *he) {
3235 PERL_UNUSED_CONTEXT;
3238 struct refcounted_he *copy;
3242 new_count = --he->refcounted_he_refcnt;
3243 HINTS_REFCNT_UNLOCK;
3249 #ifndef USE_ITHREADS
3250 unshare_hek_or_pvn (he->refcounted_he_hek, 0, 0, 0);
3253 he = he->refcounted_he_next;
3254 PerlMemShared_free(copy);
3259 =for apidoc m|struct refcounted_he *|refcounted_he_inc|struct refcounted_he *he
3261 Increment the reference count of a C<refcounted_he>. The pointer to the
3262 C<refcounted_he> is also returned. It is safe to pass a null pointer
3263 to this function: no action occurs and a null pointer is returned.
3268 struct refcounted_he *
3269 Perl_refcounted_he_inc(pTHX_ struct refcounted_he *he)
3274 he->refcounted_he_refcnt++;
3275 HINTS_REFCNT_UNLOCK;
3281 =for apidoc cop_fetch_label
3283 Returns the label attached to a cop.
3284 The flags pointer may be set to C<SVf_UTF8> or 0.
3289 /* pp_entereval is aware that labels are stored with a key ':' at the top of
3292 Perl_cop_fetch_label(pTHX_ COP *const cop, STRLEN *len, U32 *flags) {
3293 struct refcounted_he *const chain = cop->cop_hints_hash;
3295 PERL_ARGS_ASSERT_COP_FETCH_LABEL;
3300 if (chain->refcounted_he_keylen != 1)
3302 if (*REF_HE_KEY(chain) != ':')
3305 if ((STRLEN)HEK_LEN(chain->refcounted_he_hek) != 1)
3307 if (*HEK_KEY(chain->refcounted_he_hek) != ':')
3310 /* Stop anyone trying to really mess us up by adding their own value for
3312 if ((chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV
3313 && (chain->refcounted_he_data[0] & HVrhek_typemask) != HVrhek_PV_UTF8)
3317 *len = chain->refcounted_he_val.refcounted_he_u_len;
3319 *flags = ((chain->refcounted_he_data[0] & HVrhek_typemask)
3320 == HVrhek_PV_UTF8) ? SVf_UTF8 : 0;
3322 return chain->refcounted_he_data + 1;
3326 =for apidoc cop_store_label
3328 Save a label into a C<cop_hints_hash>. You need to set flags to C<SVf_UTF8>
3335 Perl_cop_store_label(pTHX_ COP *const cop, const char *label, STRLEN len,
3339 PERL_ARGS_ASSERT_COP_STORE_LABEL;
3341 if (flags & ~(SVf_UTF8))
3342 Perl_croak(aTHX_ "panic: cop_store_label illegal flag bits 0x%" UVxf,
3344 labelsv = newSVpvn_flags(label, len, SVs_TEMP);
3345 if (flags & SVf_UTF8)
3348 = refcounted_he_new_pvs(cop->cop_hints_hash, ":", labelsv, 0);
3352 =for apidoc hv_assert
3354 Check that a hash is in an internally consistent state.
3362 Perl_hv_assert(pTHX_ HV *hv)
3367 int placeholders = 0;
3370 const I32 riter = HvRITER_get(hv);
3371 HE *eiter = HvEITER_get(hv);
3373 PERL_ARGS_ASSERT_HV_ASSERT;
3375 (void)hv_iterinit(hv);
3377 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
3378 /* sanity check the values */
3379 if (HeVAL(entry) == &PL_sv_placeholder)
3383 /* sanity check the keys */
3384 if (HeSVKEY(entry)) {
3385 NOOP; /* Don't know what to check on SV keys. */
3386 } else if (HeKUTF8(entry)) {
3388 if (HeKWASUTF8(entry)) {
3389 PerlIO_printf(Perl_debug_log,
3390 "hash key has both WASUTF8 and UTF8: '%.*s'\n",
3391 (int) HeKLEN(entry), HeKEY(entry));
3394 } else if (HeKWASUTF8(entry))
3397 if (!SvTIED_mg((const SV *)hv, PERL_MAGIC_tied)) {
3398 static const char bad_count[] = "Count %d %s(s), but hash reports %d\n";
3399 const int nhashkeys = HvUSEDKEYS(hv);
3400 const int nhashplaceholders = HvPLACEHOLDERS_get(hv);
3402 if (nhashkeys != real) {
3403 PerlIO_printf(Perl_debug_log, bad_count, real, "keys", nhashkeys );
3406 if (nhashplaceholders != placeholders) {
3407 PerlIO_printf(Perl_debug_log, bad_count, placeholders, "placeholder", nhashplaceholders );
3411 if (withflags && ! HvHASKFLAGS(hv)) {
3412 PerlIO_printf(Perl_debug_log,
3413 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
3418 sv_dump(MUTABLE_SV(hv));
3420 HvRITER_set(hv, riter); /* Restore hash iterator state */
3421 HvEITER_set(hv, eiter);
3428 * c-indentation-style: bsd
3430 * indent-tabs-mode: nil
3433 * ex: set ts=8 sts=4 sw=4 et: