3 * Copyright (c) 1991-2002, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "I sit beside the fire and think of all that I have seen." --Bilbo
15 =head1 Hash Manipulation Functions
30 PL_he_root = HeNEXT(he);
39 HeNEXT(p) = (HE*)PL_he_root;
50 New(54, ptr, 1008/sizeof(XPV), XPV);
51 ptr->xpv_pv = (char*)PL_he_arenaroot;
52 PL_he_arenaroot = ptr;
55 heend = &he[1008 / sizeof(HE) - 1];
58 HeNEXT(he) = (HE*)(he + 1);
66 #define new_HE() (HE*)safemalloc(sizeof(HE))
67 #define del_HE(p) safefree((char*)p)
71 #define new_HE() new_he()
72 #define del_HE(p) del_he(p)
77 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
88 New(54, k, HEK_BASESIZE + len + 2, char);
90 Copy(str, HEK_KEY(hek), len, char);
91 HEK_KEY(hek)[len] = 0;
94 HEK_UTF8(hek) = (char)is_utf8;
99 Perl_unshare_hek(pTHX_ HEK *hek)
101 unsharepvn(HEK_KEY(hek),HEK_UTF8(hek)?-HEK_LEN(hek):HEK_LEN(hek),
105 #if defined(USE_ITHREADS)
107 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
113 /* look for it in the table first */
114 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
118 /* create anew and remember what it is */
120 ptr_table_store(PL_ptr_table, e, ret);
122 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
123 if (HeKLEN(e) == HEf_SVKEY)
124 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
126 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
128 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
129 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
132 #endif /* USE_ITHREADS */
135 Perl_hv_notallowed(pTHX_ bool is_utf8, const char *key, I32 klen,
138 SV *sv = sv_newmortal();
139 if (key == keysave) {
140 sv_setpvn(sv, key, klen);
143 /* Need to free saved eventually assign to mortal SV */
144 SV *sv = sv_newmortal();
145 sv_usepvn(sv, (char *) key, klen);
150 Perl_croak(aTHX_ "Attempt to access key '%"SVf"' in fixed hash",sv);
153 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
159 Returns the SV which corresponds to the specified key in the hash. The
160 C<klen> is the length of the key. If C<lval> is set then the fetch will be
161 part of a store. Check that the return value is non-null before
162 dereferencing it to an C<SV*>.
164 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
165 information on how to use this function on tied hashes.
171 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
177 bool is_utf8 = FALSE;
178 const char *keysave = key;
188 if (SvRMAGICAL(hv)) {
189 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
191 mg_copy((SV*)hv, sv, key, klen);
193 return &PL_hv_fetch_sv;
195 #ifdef ENV_IS_CASELESS
196 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
198 for (i = 0; i < klen; ++i)
199 if (isLOWER(key[i])) {
200 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
201 SV **ret = hv_fetch(hv, nkey, klen, 0);
203 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
210 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
211 avoid unnecessary pointer dereferencing. */
212 xhv = (XPVHV*)SvANY(hv);
213 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
215 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
216 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
219 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
220 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
227 STRLEN tmplen = klen;
228 /* Just casting the &klen to (STRLEN) won't work well
229 * if STRLEN and I32 are of different widths. --jhi */
230 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
234 PERL_HASH(hash, key, klen);
236 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
237 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
238 for (; entry; entry = HeNEXT(entry)) {
239 if (HeHASH(entry) != hash) /* strings can't be equal */
241 if (HeKLEN(entry) != klen)
243 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
245 if (HeKUTF8(entry) != (char)is_utf8)
249 /* if we find a placeholder, we pretend we haven't found anything */
250 if (HeVAL(entry) == &PL_sv_undef)
252 return &HeVAL(entry);
255 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
256 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
258 char *env = PerlEnv_ENVgetenv_len(key,&len);
260 sv = newSVpvn(env,len);
264 return hv_store(hv,key,klen,sv,hash);
268 if (!entry && SvREADONLY(hv)) {
269 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
271 if (lval) { /* gonna assign to this, so it better be there */
273 if (key != keysave) { /* must be is_utf8 == 0 */
274 SV **ret = hv_store(hv,key,klen,sv,hash);
279 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
286 /* returns an HE * structure with the all fields set */
287 /* note that hent_val will be a mortal sv for MAGICAL hashes */
289 =for apidoc hv_fetch_ent
291 Returns the hash entry which corresponds to the specified key in the hash.
292 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
293 if you want the function to compute it. IF C<lval> is set then the fetch
294 will be part of a store. Make sure the return value is non-null before
295 accessing it. The return value when C<tb> is a tied hash is a pointer to a
296 static location, so be sure to make a copy of the structure if you need to
299 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
300 information on how to use this function on tied hashes.
306 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
319 if (SvRMAGICAL(hv)) {
320 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
322 keysv = sv_2mortal(newSVsv(keysv));
323 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
324 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
326 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
327 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
329 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
330 HeVAL(&PL_hv_fetch_ent_mh) = sv;
331 return &PL_hv_fetch_ent_mh;
333 #ifdef ENV_IS_CASELESS
334 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
336 key = SvPV(keysv, klen);
337 for (i = 0; i < klen; ++i)
338 if (isLOWER(key[i])) {
339 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
340 (void)strupr(SvPVX(nkeysv));
341 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
343 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
350 xhv = (XPVHV*)SvANY(hv);
351 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
353 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
354 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
357 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
358 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
364 keysave = key = SvPV(keysv, klen);
365 is_utf8 = (SvUTF8(keysv)!=0);
368 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
371 PERL_HASH(hash, key, klen);
373 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
374 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
375 for (; entry; entry = HeNEXT(entry)) {
376 if (HeHASH(entry) != hash) /* strings can't be equal */
378 if (HeKLEN(entry) != klen)
380 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
382 if (HeKUTF8(entry) != (char)is_utf8)
386 /* if we find a placeholder, we pretend we haven't found anything */
387 if (HeVAL(entry) == &PL_sv_undef)
391 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
392 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
394 char *env = PerlEnv_ENVgetenv_len(key,&len);
396 sv = newSVpvn(env,len);
398 return hv_store_ent(hv,keysv,sv,hash);
402 if (!entry && SvREADONLY(hv)) {
403 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
407 if (lval) { /* gonna assign to this, so it better be there */
409 return hv_store_ent(hv,keysv,sv,hash);
415 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
417 MAGIC *mg = SvMAGIC(hv);
421 if (isUPPER(mg->mg_type)) {
423 switch (mg->mg_type) {
424 case PERL_MAGIC_tied:
426 *needs_store = FALSE;
429 mg = mg->mg_moremagic;
436 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
437 the length of the key. The C<hash> parameter is the precomputed hash
438 value; if it is zero then Perl will compute it. The return value will be
439 NULL if the operation failed or if the value did not need to be actually
440 stored within the hash (as in the case of tied hashes). Otherwise it can
441 be dereferenced to get the original C<SV*>. Note that the caller is
442 responsible for suitably incrementing the reference count of C<val> before
443 the call, and decrementing it if the function returned NULL.
445 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
446 information on how to use this function on tied hashes.
452 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
457 register HE **oentry;
458 bool is_utf8 = FALSE;
459 const char *keysave = key;
469 xhv = (XPVHV*)SvANY(hv);
473 hv_magic_check (hv, &needs_copy, &needs_store);
475 mg_copy((SV*)hv, val, key, klen);
476 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store)
478 #ifdef ENV_IS_CASELESS
479 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
480 key = savepvn(key,klen);
481 key = (const char*)strupr((char*)key);
488 STRLEN tmplen = klen;
489 /* See the note in hv_fetch(). --jhi */
490 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
495 PERL_HASH(hash, key, klen);
497 if (!xhv->xhv_array /* !HvARRAY(hv) */)
498 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
499 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
502 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
503 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
506 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
507 if (HeHASH(entry) != hash) /* strings can't be equal */
509 if (HeKLEN(entry) != klen)
511 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
513 if (HeKUTF8(entry) != (char)is_utf8)
515 if (HeVAL(entry) == &PL_sv_undef)
516 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
518 SvREFCNT_dec(HeVAL(entry));
522 return &HeVAL(entry);
525 if (SvREADONLY(hv)) {
526 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
531 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
532 else /* gotta do the real thing */
533 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
537 HeNEXT(entry) = *oentry;
540 xhv->xhv_keys++; /* HvKEYS(hv)++ */
541 if (i) { /* initial entry? */
542 xhv->xhv_fill++; /* HvFILL(hv)++ */
543 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
547 return &HeVAL(entry);
551 =for apidoc hv_store_ent
553 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
554 parameter is the precomputed hash value; if it is zero then Perl will
555 compute it. The return value is the new hash entry so created. It will be
556 NULL if the operation failed or if the value did not need to be actually
557 stored within the hash (as in the case of tied hashes). Otherwise the
558 contents of the return value can be accessed using the C<He?> macros
559 described here. Note that the caller is responsible for suitably
560 incrementing the reference count of C<val> before the call, and
561 decrementing it if the function returned NULL.
563 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
564 information on how to use this function on tied hashes.
570 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
577 register HE **oentry;
584 xhv = (XPVHV*)SvANY(hv);
588 hv_magic_check (hv, &needs_copy, &needs_store);
590 bool save_taint = PL_tainted;
592 PL_tainted = SvTAINTED(keysv);
593 keysv = sv_2mortal(newSVsv(keysv));
594 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
595 TAINT_IF(save_taint);
596 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
598 #ifdef ENV_IS_CASELESS
599 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
600 key = SvPV(keysv, klen);
601 keysv = sv_2mortal(newSVpvn(key,klen));
602 (void)strupr(SvPVX(keysv));
609 keysave = key = SvPV(keysv, klen);
610 is_utf8 = (SvUTF8(keysv) != 0);
613 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
616 PERL_HASH(hash, key, klen);
618 if (!xhv->xhv_array /* !HvARRAY(hv) */)
619 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
620 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
623 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
624 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
627 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
628 if (HeHASH(entry) != hash) /* strings can't be equal */
630 if (HeKLEN(entry) != klen)
632 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
634 if (HeKUTF8(entry) != (char)is_utf8)
636 if (HeVAL(entry) == &PL_sv_undef)
637 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
639 SvREFCNT_dec(HeVAL(entry));
646 if (SvREADONLY(hv)) {
647 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
652 HeKEY_hek(entry) = share_hek(key, is_utf8?-(I32)klen:klen, hash);
653 else /* gotta do the real thing */
654 HeKEY_hek(entry) = save_hek(key, is_utf8?-(I32)klen:klen, hash);
658 HeNEXT(entry) = *oentry;
661 xhv->xhv_keys++; /* HvKEYS(hv)++ */
662 if (i) { /* initial entry? */
663 xhv->xhv_fill++; /* HvFILL(hv)++ */
664 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
672 =for apidoc hv_delete
674 Deletes a key/value pair in the hash. The value SV is removed from the
675 hash and returned to the caller. The C<klen> is the length of the key.
676 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
683 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
689 register HE **oentry;
692 bool is_utf8 = FALSE;
693 const char *keysave = key;
701 if (SvRMAGICAL(hv)) {
704 hv_magic_check (hv, &needs_copy, &needs_store);
706 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
710 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
711 /* No longer an element */
712 sv_unmagic(sv, PERL_MAGIC_tiedelem);
715 return Nullsv; /* element cannot be deleted */
717 #ifdef ENV_IS_CASELESS
718 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
719 sv = sv_2mortal(newSVpvn(key,klen));
720 key = strupr(SvPVX(sv));
725 xhv = (XPVHV*)SvANY(hv);
726 if (!xhv->xhv_array /* !HvARRAY(hv) */)
730 STRLEN tmplen = klen;
731 /* See the note in hv_fetch(). --jhi */
732 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
736 PERL_HASH(hash, key, klen);
738 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
739 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
742 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
743 if (HeHASH(entry) != hash) /* strings can't be equal */
745 if (HeKLEN(entry) != klen)
747 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
749 if (HeKUTF8(entry) != (char)is_utf8)
753 /* if placeholder is here, it's already been deleted.... */
754 if (HeVAL(entry) == &PL_sv_undef)
757 return Nullsv; /* if still SvREADONLY, leave it deleted. */
759 /* okay, really delete the placeholder... */
760 *oentry = HeNEXT(entry);
762 xhv->xhv_fill--; /* HvFILL(hv)-- */
763 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
766 hv_free_ent(hv, entry);
767 xhv->xhv_keys--; /* HvKEYS(hv)-- */
768 xhv->xhv_placeholders--;
772 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
773 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
776 if (flags & G_DISCARD)
779 sv = sv_2mortal(HeVAL(entry));
780 HeVAL(entry) = &PL_sv_undef;
784 * If a restricted hash, rather than really deleting the entry, put
785 * a placeholder there. This marks the key as being "approved", so
786 * we can still access via not-really-existing key without raising
789 if (SvREADONLY(hv)) {
790 HeVAL(entry) = &PL_sv_undef;
791 /* We'll be saving this slot, so the number of allocated keys
792 * doesn't go down, but the number placeholders goes up */
793 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
795 *oentry = HeNEXT(entry);
797 xhv->xhv_fill--; /* HvFILL(hv)-- */
798 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
801 hv_free_ent(hv, entry);
802 xhv->xhv_keys--; /* HvKEYS(hv)-- */
806 if (SvREADONLY(hv)) {
807 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
816 =for apidoc hv_delete_ent
818 Deletes a key/value pair in the hash. The value SV is removed from the
819 hash and returned to the caller. The C<flags> value will normally be zero;
820 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
821 precomputed hash value, or 0 to ask for it to be computed.
827 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
834 register HE **oentry;
841 if (SvRMAGICAL(hv)) {
844 hv_magic_check (hv, &needs_copy, &needs_store);
846 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
850 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
851 /* No longer an element */
852 sv_unmagic(sv, PERL_MAGIC_tiedelem);
855 return Nullsv; /* element cannot be deleted */
857 #ifdef ENV_IS_CASELESS
858 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
859 key = SvPV(keysv, klen);
860 keysv = sv_2mortal(newSVpvn(key,klen));
861 (void)strupr(SvPVX(keysv));
867 xhv = (XPVHV*)SvANY(hv);
868 if (!xhv->xhv_array /* !HvARRAY(hv) */)
871 keysave = key = SvPV(keysv, klen);
872 is_utf8 = (SvUTF8(keysv) != 0);
875 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
878 PERL_HASH(hash, key, klen);
880 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
881 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
884 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
885 if (HeHASH(entry) != hash) /* strings can't be equal */
887 if (HeKLEN(entry) != klen)
889 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
891 if (HeKUTF8(entry) != (char)is_utf8)
896 /* if placeholder is here, it's already been deleted.... */
897 if (HeVAL(entry) == &PL_sv_undef)
900 return Nullsv; /* if still SvREADONLY, leave it deleted. */
902 /* okay, really delete the placeholder. */
903 *oentry = HeNEXT(entry);
905 xhv->xhv_fill--; /* HvFILL(hv)-- */
906 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
909 hv_free_ent(hv, entry);
910 xhv->xhv_keys--; /* HvKEYS(hv)-- */
911 xhv->xhv_placeholders--;
914 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
915 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
918 if (flags & G_DISCARD)
921 sv = sv_2mortal(HeVAL(entry));
922 HeVAL(entry) = &PL_sv_undef;
926 * If a restricted hash, rather than really deleting the entry, put
927 * a placeholder there. This marks the key as being "approved", so
928 * we can still access via not-really-existing key without raising
931 if (SvREADONLY(hv)) {
932 HeVAL(entry) = &PL_sv_undef;
933 /* We'll be saving this slot, so the number of allocated keys
934 * doesn't go down, but the number placeholders goes up */
935 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
937 *oentry = HeNEXT(entry);
939 xhv->xhv_fill--; /* HvFILL(hv)-- */
940 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
943 hv_free_ent(hv, entry);
944 xhv->xhv_keys--; /* HvKEYS(hv)-- */
948 if (SvREADONLY(hv)) {
949 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
958 =for apidoc hv_exists
960 Returns a boolean indicating whether the specified hash key exists. The
961 C<klen> is the length of the key.
967 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
973 bool is_utf8 = FALSE;
974 const char *keysave = key;
984 if (SvRMAGICAL(hv)) {
985 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
987 mg_copy((SV*)hv, sv, key, klen);
988 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
991 #ifdef ENV_IS_CASELESS
992 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
993 sv = sv_2mortal(newSVpvn(key,klen));
994 key = strupr(SvPVX(sv));
999 xhv = (XPVHV*)SvANY(hv);
1000 #ifndef DYNAMIC_ENV_FETCH
1001 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1006 STRLEN tmplen = klen;
1007 /* See the note in hv_fetch(). --jhi */
1008 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1012 PERL_HASH(hash, key, klen);
1014 #ifdef DYNAMIC_ENV_FETCH
1015 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1018 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1019 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1020 for (; entry; entry = HeNEXT(entry)) {
1021 if (HeHASH(entry) != hash) /* strings can't be equal */
1023 if (HeKLEN(entry) != klen)
1025 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1027 if (HeKUTF8(entry) != (char)is_utf8)
1031 /* If we find the key, but the value is a placeholder, return false. */
1032 if (HeVAL(entry) == &PL_sv_undef)
1037 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1038 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1040 char *env = PerlEnv_ENVgetenv_len(key,&len);
1042 sv = newSVpvn(env,len);
1044 (void)hv_store(hv,key,klen,sv,hash);
1056 =for apidoc hv_exists_ent
1058 Returns a boolean indicating whether the specified hash key exists. C<hash>
1059 can be a valid precomputed hash value, or 0 to ask for it to be
1066 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1068 register XPVHV* xhv;
1079 if (SvRMAGICAL(hv)) {
1080 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1081 SV* svret = sv_newmortal();
1082 sv = sv_newmortal();
1083 keysv = sv_2mortal(newSVsv(keysv));
1084 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1085 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1086 return SvTRUE(svret);
1088 #ifdef ENV_IS_CASELESS
1089 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1090 key = SvPV(keysv, klen);
1091 keysv = sv_2mortal(newSVpvn(key,klen));
1092 (void)strupr(SvPVX(keysv));
1098 xhv = (XPVHV*)SvANY(hv);
1099 #ifndef DYNAMIC_ENV_FETCH
1100 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1104 keysave = key = SvPV(keysv, klen);
1105 is_utf8 = (SvUTF8(keysv) != 0);
1107 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1109 PERL_HASH(hash, key, klen);
1111 #ifdef DYNAMIC_ENV_FETCH
1112 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1115 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1116 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1117 for (; entry; entry = HeNEXT(entry)) {
1118 if (HeHASH(entry) != hash) /* strings can't be equal */
1120 if (HeKLEN(entry) != klen)
1122 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1124 if (HeKUTF8(entry) != (char)is_utf8)
1128 /* If we find the key, but the value is a placeholder, return false. */
1129 if (HeVAL(entry) == &PL_sv_undef)
1133 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1134 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1136 char *env = PerlEnv_ENVgetenv_len(key,&len);
1138 sv = newSVpvn(env,len);
1140 (void)hv_store_ent(hv,keysv,sv,hash);
1151 S_hsplit(pTHX_ HV *hv)
1153 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1154 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1155 register I32 newsize = oldsize * 2;
1157 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1161 register HE **oentry;
1164 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1165 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1171 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1176 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1177 if (oldsize >= 64) {
1178 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1179 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1182 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1186 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1187 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1188 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1191 for (i=0; i<oldsize; i++,aep++) {
1192 if (!*aep) /* non-existent */
1195 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1196 if ((HeHASH(entry) & newsize) != i) {
1197 *oentry = HeNEXT(entry);
1198 HeNEXT(entry) = *bep;
1200 xhv->xhv_fill++; /* HvFILL(hv)++ */
1205 oentry = &HeNEXT(entry);
1207 if (!*aep) /* everything moved */
1208 xhv->xhv_fill--; /* HvFILL(hv)-- */
1213 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1215 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1216 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1217 register I32 newsize;
1223 register HE **oentry;
1225 newsize = (I32) newmax; /* possible truncation here */
1226 if (newsize != newmax || newmax <= oldsize)
1228 while ((newsize & (1 + ~newsize)) != newsize) {
1229 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1231 if (newsize < newmax)
1233 if (newsize < newmax)
1234 return; /* overflow detection */
1236 a = xhv->xhv_array; /* HvARRAY(hv) */
1239 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1240 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1246 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1251 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1252 if (oldsize >= 64) {
1253 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1254 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1257 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1260 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1263 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1265 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1266 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1267 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1271 for (i=0; i<oldsize; i++,aep++) {
1272 if (!*aep) /* non-existent */
1274 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1275 if ((j = (HeHASH(entry) & newsize)) != i) {
1277 *oentry = HeNEXT(entry);
1278 if (!(HeNEXT(entry) = aep[j]))
1279 xhv->xhv_fill++; /* HvFILL(hv)++ */
1284 oentry = &HeNEXT(entry);
1286 if (!*aep) /* everything moved */
1287 xhv->xhv_fill--; /* HvFILL(hv)-- */
1294 Creates a new HV. The reference count is set to 1.
1303 register XPVHV* xhv;
1305 hv = (HV*)NEWSV(502,0);
1306 sv_upgrade((SV *)hv, SVt_PVHV);
1307 xhv = (XPVHV*)SvANY(hv);
1310 #ifndef NODEFAULT_SHAREKEYS
1311 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1313 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1314 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1315 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1316 (void)hv_iterinit(hv); /* so each() will start off right */
1321 Perl_newHVhv(pTHX_ HV *ohv)
1324 STRLEN hv_max, hv_fill;
1326 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1328 hv_max = HvMAX(ohv);
1330 if (!SvMAGICAL((SV *)ohv)) {
1331 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1332 int i, shared = !!HvSHAREKEYS(ohv);
1333 HE **ents, **oents = (HE **)HvARRAY(ohv);
1335 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1338 /* In each bucket... */
1339 for (i = 0; i <= hv_max; i++) {
1340 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1347 /* Copy the linked list of entries. */
1348 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1349 U32 hash = HeHASH(oent);
1350 char *key = HeKEY(oent);
1351 STRLEN len = HeKLEN_UTF8(oent);
1354 HeVAL(ent) = newSVsv(HeVAL(oent));
1355 HeKEY_hek(ent) = shared ? share_hek(key, len, hash)
1356 : save_hek(key, len, hash);
1367 HvFILL(hv) = hv_fill;
1368 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1372 /* Iterate over ohv, copying keys and values one at a time. */
1374 I32 riter = HvRITER(ohv);
1375 HE *eiter = HvEITER(ohv);
1377 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1378 while (hv_max && hv_max + 1 >= hv_fill * 2)
1379 hv_max = hv_max / 2;
1383 while ((entry = hv_iternext(ohv))) {
1384 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1385 newSVsv(HeVAL(entry)), HeHASH(entry));
1387 HvRITER(ohv) = riter;
1388 HvEITER(ohv) = eiter;
1395 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1402 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1403 PL_sub_generation++; /* may be deletion of method from stash */
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));
1417 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1421 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1422 PL_sub_generation++; /* may be deletion of method from stash */
1423 sv_2mortal(HeVAL(entry)); /* free between statements */
1424 if (HeKLEN(entry) == HEf_SVKEY) {
1425 sv_2mortal(HeKEY_sv(entry));
1426 Safefree(HeKEY_hek(entry));
1428 else if (HvSHAREKEYS(hv))
1429 unshare_hek(HeKEY_hek(entry));
1431 Safefree(HeKEY_hek(entry));
1436 =for apidoc hv_clear
1438 Clears a hash, making it empty.
1444 Perl_hv_clear(pTHX_ HV *hv)
1446 register XPVHV* xhv;
1449 xhv = (XPVHV*)SvANY(hv);
1451 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1452 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1453 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1454 if (xhv->xhv_array /* HvARRAY(hv) */)
1455 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1456 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1463 S_hfreeentries(pTHX_ HV *hv)
1465 register HE **array;
1467 register HE *oentry = Null(HE*);
1478 array = HvARRAY(hv);
1483 entry = HeNEXT(entry);
1484 hv_free_ent(hv, oentry);
1489 entry = array[riter];
1492 (void)hv_iterinit(hv);
1496 =for apidoc hv_undef
1504 Perl_hv_undef(pTHX_ HV *hv)
1506 register XPVHV* xhv;
1509 xhv = (XPVHV*)SvANY(hv);
1511 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1513 Safefree(HvNAME(hv));
1516 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1517 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1518 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1519 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1520 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1527 =for apidoc hv_iterinit
1529 Prepares a starting point to traverse a hash table. Returns the number of
1530 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1531 currently only meaningful for hashes without tie magic.
1533 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1534 hash buckets that happen to be in use. If you still need that esoteric
1535 value, you can get it through the macro C<HvFILL(tb)>.
1541 Perl_hv_iterinit(pTHX_ HV *hv)
1543 register XPVHV* xhv;
1547 Perl_croak(aTHX_ "Bad hash");
1548 xhv = (XPVHV*)SvANY(hv);
1549 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1550 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1552 hv_free_ent(hv, entry);
1554 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1555 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1556 /* used to be xhv->xhv_fill before 5.004_65 */
1557 return XHvTOTALKEYS(xhv);
1561 =for apidoc hv_iternext
1563 Returns entries from a hash iterator. See C<hv_iterinit>.
1569 Perl_hv_iternext(pTHX_ HV *hv)
1571 register XPVHV* xhv;
1577 Perl_croak(aTHX_ "Bad hash");
1578 xhv = (XPVHV*)SvANY(hv);
1579 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1581 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1582 SV *key = sv_newmortal();
1584 sv_setsv(key, HeSVKEY_force(entry));
1585 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1591 /* one HE per MAGICAL hash */
1592 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1594 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1596 HeKEY_hek(entry) = hek;
1597 HeKLEN(entry) = HEf_SVKEY;
1599 magic_nextpack((SV*) hv,mg,key);
1601 /* force key to stay around until next time */
1602 HeSVKEY_set(entry, SvREFCNT_inc(key));
1603 return entry; /* beware, hent_val is not set */
1606 SvREFCNT_dec(HeVAL(entry));
1607 Safefree(HeKEY_hek(entry));
1609 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1612 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1613 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1617 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1618 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1619 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1623 entry = HeNEXT(entry);
1625 * Skip past any placeholders -- don't want to include them in
1628 while (entry && HeVAL(entry) == &PL_sv_undef) {
1629 entry = HeNEXT(entry);
1633 xhv->xhv_riter++; /* HvRITER(hv)++ */
1634 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1635 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1638 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1639 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1641 /* if we have an entry, but it's a placeholder, don't count it */
1642 if (entry && HeVAL(entry) == &PL_sv_undef)
1647 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1649 hv_free_ent(hv, oldentry);
1652 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1657 =for apidoc hv_iterkey
1659 Returns the key from the current position of the hash iterator. See
1666 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1668 if (HeKLEN(entry) == HEf_SVKEY) {
1670 char *p = SvPV(HeKEY_sv(entry), len);
1675 *retlen = HeKLEN(entry);
1676 return HeKEY(entry);
1680 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1682 =for apidoc hv_iterkeysv
1684 Returns the key as an C<SV*> from the current position of the hash
1685 iterator. The return value will always be a mortal copy of the key. Also
1692 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1694 if (HeKLEN(entry) == HEf_SVKEY)
1695 return sv_mortalcopy(HeKEY_sv(entry));
1697 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1698 HeKLEN_UTF8(entry), HeHASH(entry)));
1702 =for apidoc hv_iterval
1704 Returns the value from the current position of the hash iterator. See
1711 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1713 if (SvRMAGICAL(hv)) {
1714 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1715 SV* sv = sv_newmortal();
1716 if (HeKLEN(entry) == HEf_SVKEY)
1717 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1718 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1722 return HeVAL(entry);
1726 =for apidoc hv_iternextsv
1728 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1735 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1738 if ( (he = hv_iternext(hv)) == NULL)
1740 *key = hv_iterkey(he, retlen);
1741 return hv_iterval(hv, he);
1745 =for apidoc hv_magic
1747 Adds magic to a hash. See C<sv_magic>.
1753 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1755 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1758 #if 0 /* use the macro from hv.h instead */
1761 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1763 return HEK_KEY(share_hek(sv, len, hash));
1768 /* possibly free a shared string if no one has access to it
1769 * len and hash must both be valid for str.
1772 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1774 register XPVHV* xhv;
1776 register HE **oentry;
1779 bool is_utf8 = FALSE;
1780 const char *save = str;
1783 STRLEN tmplen = -len;
1785 /* See the note in hv_fetch(). --jhi */
1786 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1790 /* what follows is the moral equivalent of:
1791 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1792 if (--*Svp == Nullsv)
1793 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1795 xhv = (XPVHV*)SvANY(PL_strtab);
1796 /* assert(xhv_array != 0) */
1798 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1799 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1800 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1801 if (HeHASH(entry) != hash) /* strings can't be equal */
1803 if (HeKLEN(entry) != len)
1805 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1807 if (HeKUTF8(entry) != (char)is_utf8)
1810 if (--HeVAL(entry) == Nullsv) {
1811 *oentry = HeNEXT(entry);
1813 xhv->xhv_fill--; /* HvFILL(hv)-- */
1814 Safefree(HeKEY_hek(entry));
1816 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1820 UNLOCK_STRTAB_MUTEX;
1823 if (!found && ckWARN_d(WARN_INTERNAL))
1824 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1827 /* get a (constant) string ptr from the global string table
1828 * string will get added if it is not already there.
1829 * len and hash must both be valid for str.
1832 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1834 register XPVHV* xhv;
1836 register HE **oentry;
1839 bool is_utf8 = FALSE;
1840 const char *save = str;
1843 STRLEN tmplen = -len;
1845 /* See the note in hv_fetch(). --jhi */
1846 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1850 /* what follows is the moral equivalent of:
1852 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1853 hv_store(PL_strtab, str, len, Nullsv, hash);
1855 xhv = (XPVHV*)SvANY(PL_strtab);
1856 /* assert(xhv_array != 0) */
1858 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1859 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1860 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1861 if (HeHASH(entry) != hash) /* strings can't be equal */
1863 if (HeKLEN(entry) != len)
1865 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1867 if (HeKUTF8(entry) != (char)is_utf8)
1874 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1875 HeVAL(entry) = Nullsv;
1876 HeNEXT(entry) = *oentry;
1878 xhv->xhv_keys++; /* HvKEYS(hv)++ */
1879 if (i) { /* initial entry? */
1880 xhv->xhv_fill++; /* HvFILL(hv)++ */
1881 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
1886 ++HeVAL(entry); /* use value slot as REFCNT */
1887 UNLOCK_STRTAB_MUTEX;
1890 return HeKEY_hek(entry);