3 * Copyright (c) 1991-2001, 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
26 PL_he_root = HeNEXT(he);
35 HeNEXT(p) = (HE*)PL_he_root;
46 New(54, ptr, 1008/sizeof(XPV), XPV);
47 ptr->xpv_pv = (char*)PL_he_arenaroot;
48 PL_he_arenaroot = ptr;
51 heend = &he[1008 / sizeof(HE) - 1];
54 HeNEXT(he) = (HE*)(he + 1);
62 #define new_HE() (HE*)safemalloc(sizeof(HE))
63 #define del_HE(p) safefree((char*)p)
67 #define new_HE() new_he()
68 #define del_HE(p) del_he(p)
73 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
84 New(54, k, HEK_BASESIZE + len + 1, char);
86 Copy(str, HEK_KEY(hek), len, char);
89 HEK_UTF8(hek) = (char)is_utf8;
94 Perl_unshare_hek(pTHX_ HEK *hek)
96 unsharepvn(HEK_KEY(hek),HEK_UTF8(hek)?-HEK_LEN(hek):HEK_LEN(hek),
100 #if defined(USE_ITHREADS)
102 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
108 /* look for it in the table first */
109 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
113 /* create anew and remember what it is */
115 ptr_table_store(PL_ptr_table, e, ret);
117 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
118 if (HeKLEN(e) == HEf_SVKEY)
119 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
121 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
123 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
124 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
127 #endif /* USE_ITHREADS */
130 Perl_hv_notallowed(pTHX_ bool is_utf8, const char *key, I32 klen,
133 SV *sv = sv_newmortal();
134 if (key == keysave) {
135 sv_setpvn(sv, key, klen);
138 /* Need to free saved eventually assign to mortal SV */
139 SV *sv = sv_newmortal();
140 sv_usepvn(sv, (char *) key, klen);
145 Perl_croak(aTHX_ "Attempt to access to key '%_' in fixed hash",sv);
148 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
154 Returns the SV which corresponds to the specified key in the hash. The
155 C<klen> is the length of the key. If C<lval> is set then the fetch will be
156 part of a store. Check that the return value is non-null before
157 dereferencing it to an C<SV*>.
159 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
160 information on how to use this function on tied hashes.
166 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
172 bool is_utf8 = FALSE;
173 const char *keysave = key;
183 if (SvRMAGICAL(hv)) {
184 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
186 mg_copy((SV*)hv, sv, key, klen);
188 return &PL_hv_fetch_sv;
190 #ifdef ENV_IS_CASELESS
191 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
193 for (i = 0; i < klen; ++i)
194 if (isLOWER(key[i])) {
195 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
196 SV **ret = hv_fetch(hv, nkey, klen, 0);
198 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
205 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
206 avoid unnecessary pointer dereferencing. */
207 xhv = (XPVHV*)SvANY(hv);
208 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
210 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
211 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
214 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
215 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
222 STRLEN tmplen = klen;
223 /* Just casting the &klen to (STRLEN) won't work well
224 * if STRLEN and I32 are of different widths. --jhi */
225 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
229 PERL_HASH(hash, key, klen);
231 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
232 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
233 for (; entry; entry = HeNEXT(entry)) {
234 if (HeHASH(entry) != hash) /* strings can't be equal */
236 if (HeKLEN(entry) != klen)
238 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
240 if (HeKUTF8(entry) != (char)is_utf8)
244 /* if we find a placeholder, we pretend we haven't found anything */
245 if (HeVAL(entry) == &PL_sv_undef)
247 return &HeVAL(entry);
250 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
251 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
253 char *env = PerlEnv_ENVgetenv_len(key,&len);
255 sv = newSVpvn(env,len);
259 return hv_store(hv,key,klen,sv,hash);
263 if (!entry && SvREADONLY(hv)) {
264 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
266 if (lval) { /* gonna assign to this, so it better be there */
268 if (key != keysave) { /* must be is_utf8 == 0 */
269 SV **ret = hv_store(hv,key,klen,sv,hash);
274 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
281 /* returns an HE * structure with the all fields set */
282 /* note that hent_val will be a mortal sv for MAGICAL hashes */
284 =for apidoc hv_fetch_ent
286 Returns the hash entry which corresponds to the specified key in the hash.
287 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
288 if you want the function to compute it. IF C<lval> is set then the fetch
289 will be part of a store. Make sure the return value is non-null before
290 accessing it. The return value when C<tb> is a tied hash is a pointer to a
291 static location, so be sure to make a copy of the structure if you need to
294 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
295 information on how to use this function on tied hashes.
301 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
314 if (SvRMAGICAL(hv)) {
315 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
317 keysv = sv_2mortal(newSVsv(keysv));
318 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
319 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
321 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
322 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
324 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
325 HeVAL(&PL_hv_fetch_ent_mh) = sv;
326 return &PL_hv_fetch_ent_mh;
328 #ifdef ENV_IS_CASELESS
329 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
331 key = SvPV(keysv, klen);
332 for (i = 0; i < klen; ++i)
333 if (isLOWER(key[i])) {
334 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
335 (void)strupr(SvPVX(nkeysv));
336 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
338 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
345 xhv = (XPVHV*)SvANY(hv);
346 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
348 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
349 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
352 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
353 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
359 keysave = key = SvPV(keysv, klen);
360 is_utf8 = (SvUTF8(keysv)!=0);
363 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
366 PERL_HASH(hash, key, klen);
368 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
369 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
370 for (; entry; entry = HeNEXT(entry)) {
371 if (HeHASH(entry) != hash) /* strings can't be equal */
373 if (HeKLEN(entry) != klen)
375 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
377 if (HeKUTF8(entry) != (char)is_utf8)
381 /* if we find a placeholder, we pretend we haven't found anything */
382 if (HeVAL(entry) == &PL_sv_undef)
386 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
387 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
389 char *env = PerlEnv_ENVgetenv_len(key,&len);
391 sv = newSVpvn(env,len);
393 return hv_store_ent(hv,keysv,sv,hash);
397 if (!entry && SvREADONLY(hv)) {
398 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
402 if (lval) { /* gonna assign to this, so it better be there */
404 return hv_store_ent(hv,keysv,sv,hash);
410 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
412 MAGIC *mg = SvMAGIC(hv);
416 if (isUPPER(mg->mg_type)) {
418 switch (mg->mg_type) {
419 case PERL_MAGIC_tied:
421 *needs_store = FALSE;
424 mg = mg->mg_moremagic;
431 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
432 the length of the key. The C<hash> parameter is the precomputed hash
433 value; if it is zero then Perl will compute it. The return value will be
434 NULL if the operation failed or if the value did not need to be actually
435 stored within the hash (as in the case of tied hashes). Otherwise it can
436 be dereferenced to get the original C<SV*>. Note that the caller is
437 responsible for suitably incrementing the reference count of C<val> before
438 the call, and decrementing it if the function returned NULL.
440 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
441 information on how to use this function on tied hashes.
447 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
452 register HE **oentry;
453 bool is_utf8 = FALSE;
454 const char *keysave = key;
464 xhv = (XPVHV*)SvANY(hv);
468 hv_magic_check (hv, &needs_copy, &needs_store);
470 mg_copy((SV*)hv, val, key, klen);
471 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store)
473 #ifdef ENV_IS_CASELESS
474 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
475 key = savepvn(key,klen);
476 key = (const char*)strupr((char*)key);
483 STRLEN tmplen = klen;
484 /* See the note in hv_fetch(). --jhi */
485 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
490 PERL_HASH(hash, key, klen);
492 if (!xhv->xhv_array /* !HvARRAY(hv) */)
493 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
494 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
497 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
498 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
501 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
502 if (HeHASH(entry) != hash) /* strings can't be equal */
504 if (HeKLEN(entry) != klen)
506 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
508 if (HeKUTF8(entry) != (char)is_utf8)
510 if (HeVAL(entry) == &PL_sv_undef)
511 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
513 SvREFCNT_dec(HeVAL(entry));
517 return &HeVAL(entry);
520 if (SvREADONLY(hv)) {
521 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
526 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
527 else /* gotta do the real thing */
528 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
532 HeNEXT(entry) = *oentry;
535 xhv->xhv_keys++; /* HvKEYS(hv)++ */
536 if (i) { /* initial entry? */
537 xhv->xhv_fill++; /* HvFILL(hv)++ */
538 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
542 return &HeVAL(entry);
546 =for apidoc hv_store_ent
548 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
549 parameter is the precomputed hash value; if it is zero then Perl will
550 compute it. The return value is the new hash entry so created. It will be
551 NULL if the operation failed or if the value did not need to be actually
552 stored within the hash (as in the case of tied hashes). Otherwise the
553 contents of the return value can be accessed using the C<He?> macros
554 described here. Note that the caller is responsible for suitably
555 incrementing the reference count of C<val> before the call, and
556 decrementing it if the function returned NULL.
558 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
559 information on how to use this function on tied hashes.
565 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
572 register HE **oentry;
579 xhv = (XPVHV*)SvANY(hv);
583 hv_magic_check (hv, &needs_copy, &needs_store);
585 bool save_taint = PL_tainted;
587 PL_tainted = SvTAINTED(keysv);
588 keysv = sv_2mortal(newSVsv(keysv));
589 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
590 TAINT_IF(save_taint);
591 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
593 #ifdef ENV_IS_CASELESS
594 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
595 key = SvPV(keysv, klen);
596 keysv = sv_2mortal(newSVpvn(key,klen));
597 (void)strupr(SvPVX(keysv));
604 keysave = key = SvPV(keysv, klen);
605 is_utf8 = (SvUTF8(keysv) != 0);
608 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
611 PERL_HASH(hash, key, klen);
613 if (!xhv->xhv_array /* !HvARRAY(hv) */)
614 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
615 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
618 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
619 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
622 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
623 if (HeHASH(entry) != hash) /* strings can't be equal */
625 if (HeKLEN(entry) != klen)
627 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
629 if (HeKUTF8(entry) != (char)is_utf8)
631 if (HeVAL(entry) == &PL_sv_undef)
632 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
634 SvREFCNT_dec(HeVAL(entry));
641 if (SvREADONLY(hv)) {
642 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
647 HeKEY_hek(entry) = share_hek(key, is_utf8?-(I32)klen:klen, hash);
648 else /* gotta do the real thing */
649 HeKEY_hek(entry) = save_hek(key, is_utf8?-(I32)klen:klen, hash);
653 HeNEXT(entry) = *oentry;
656 xhv->xhv_keys++; /* HvKEYS(hv)++ */
657 if (i) { /* initial entry? */
658 xhv->xhv_fill++; /* HvFILL(hv)++ */
659 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
667 =for apidoc hv_delete
669 Deletes a key/value pair in the hash. The value SV is removed from the
670 hash and returned to the caller. The C<klen> is the length of the key.
671 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
678 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
684 register HE **oentry;
687 bool is_utf8 = FALSE;
688 const char *keysave = key;
696 if (SvRMAGICAL(hv)) {
699 hv_magic_check (hv, &needs_copy, &needs_store);
701 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
705 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
706 /* No longer an element */
707 sv_unmagic(sv, PERL_MAGIC_tiedelem);
710 return Nullsv; /* element cannot be deleted */
712 #ifdef ENV_IS_CASELESS
713 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
714 sv = sv_2mortal(newSVpvn(key,klen));
715 key = strupr(SvPVX(sv));
720 xhv = (XPVHV*)SvANY(hv);
721 if (!xhv->xhv_array /* !HvARRAY(hv) */)
725 STRLEN tmplen = klen;
726 /* See the note in hv_fetch(). --jhi */
727 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
731 PERL_HASH(hash, key, klen);
733 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
734 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
737 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
738 if (HeHASH(entry) != hash) /* strings can't be equal */
740 if (HeKLEN(entry) != klen)
742 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
744 if (HeKUTF8(entry) != (char)is_utf8)
748 /* if placeholder is here, it's already been deleted.... */
749 if (HeVAL(entry) == &PL_sv_undef)
752 return Nullsv; /* if still SvREADONLY, leave it deleted. */
754 /* okay, really delete the placeholder... */
755 *oentry = HeNEXT(entry);
757 xhv->xhv_fill--; /* HvFILL(hv)-- */
758 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
761 hv_free_ent(hv, entry);
762 xhv->xhv_keys--; /* HvKEYS(hv)-- */
763 xhv->xhv_placeholders--;
767 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
768 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
771 if (flags & G_DISCARD)
774 sv = sv_2mortal(HeVAL(entry));
775 HeVAL(entry) = &PL_sv_undef;
779 * If a restricted hash, rather than really deleting the entry, put
780 * a placeholder there. This marks the key as being "approved", so
781 * we can still access via not-really-existing key without raising
784 if (SvREADONLY(hv)) {
785 HeVAL(entry) = &PL_sv_undef;
786 /* We'll be saving this slot, so the number of allocated keys
787 * doesn't go down, but the number placeholders goes up */
788 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
790 *oentry = HeNEXT(entry);
792 xhv->xhv_fill--; /* HvFILL(hv)-- */
793 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
796 hv_free_ent(hv, entry);
797 xhv->xhv_keys--; /* HvKEYS(hv)-- */
801 if (SvREADONLY(hv)) {
802 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
811 =for apidoc hv_delete_ent
813 Deletes a key/value pair in the hash. The value SV is removed from the
814 hash and returned to the caller. The C<flags> value will normally be zero;
815 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
816 precomputed hash value, or 0 to ask for it to be computed.
822 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
829 register HE **oentry;
836 if (SvRMAGICAL(hv)) {
839 hv_magic_check (hv, &needs_copy, &needs_store);
841 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
845 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
846 /* No longer an element */
847 sv_unmagic(sv, PERL_MAGIC_tiedelem);
850 return Nullsv; /* element cannot be deleted */
852 #ifdef ENV_IS_CASELESS
853 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
854 key = SvPV(keysv, klen);
855 keysv = sv_2mortal(newSVpvn(key,klen));
856 (void)strupr(SvPVX(keysv));
862 xhv = (XPVHV*)SvANY(hv);
863 if (!xhv->xhv_array /* !HvARRAY(hv) */)
866 keysave = key = SvPV(keysv, klen);
867 is_utf8 = (SvUTF8(keysv) != 0);
870 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
873 PERL_HASH(hash, key, klen);
875 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
876 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
879 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
880 if (HeHASH(entry) != hash) /* strings can't be equal */
882 if (HeKLEN(entry) != klen)
884 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
886 if (HeKUTF8(entry) != (char)is_utf8)
891 /* if placeholder is here, it's already been deleted.... */
892 if (HeVAL(entry) == &PL_sv_undef)
895 return Nullsv; /* if still SvREADONLY, leave it deleted. */
897 /* okay, really delete the placeholder. */
898 *oentry = HeNEXT(entry);
900 xhv->xhv_fill--; /* HvFILL(hv)-- */
901 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
904 hv_free_ent(hv, entry);
905 xhv->xhv_keys--; /* HvKEYS(hv)-- */
906 xhv->xhv_placeholders--;
909 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
910 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
913 if (flags & G_DISCARD)
916 sv = sv_2mortal(HeVAL(entry));
917 HeVAL(entry) = &PL_sv_undef;
921 * If a restricted hash, rather than really deleting the entry, put
922 * a placeholder there. This marks the key as being "approved", so
923 * we can still access via not-really-existing key without raising
926 if (SvREADONLY(hv)) {
927 HeVAL(entry) = &PL_sv_undef;
928 /* We'll be saving this slot, so the number of allocated keys
929 * doesn't go down, but the number placeholders goes up */
930 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
932 *oentry = HeNEXT(entry);
934 xhv->xhv_fill--; /* HvFILL(hv)-- */
935 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
938 hv_free_ent(hv, entry);
939 xhv->xhv_keys--; /* HvKEYS(hv)-- */
943 if (SvREADONLY(hv)) {
944 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
953 =for apidoc hv_exists
955 Returns a boolean indicating whether the specified hash key exists. The
956 C<klen> is the length of the key.
962 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
968 bool is_utf8 = FALSE;
969 const char *keysave = key;
979 if (SvRMAGICAL(hv)) {
980 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
982 mg_copy((SV*)hv, sv, key, klen);
983 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
986 #ifdef ENV_IS_CASELESS
987 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
988 sv = sv_2mortal(newSVpvn(key,klen));
989 key = strupr(SvPVX(sv));
994 xhv = (XPVHV*)SvANY(hv);
995 #ifndef DYNAMIC_ENV_FETCH
996 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1001 STRLEN tmplen = klen;
1002 /* See the note in hv_fetch(). --jhi */
1003 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1007 PERL_HASH(hash, key, klen);
1009 #ifdef DYNAMIC_ENV_FETCH
1010 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1013 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1014 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1015 for (; entry; entry = HeNEXT(entry)) {
1016 if (HeHASH(entry) != hash) /* strings can't be equal */
1018 if (HeKLEN(entry) != klen)
1020 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1022 if (HeKUTF8(entry) != (char)is_utf8)
1026 /* If we find the key, but the value is a placeholder, return false. */
1027 if (HeVAL(entry) == &PL_sv_undef)
1032 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1033 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1035 char *env = PerlEnv_ENVgetenv_len(key,&len);
1037 sv = newSVpvn(env,len);
1039 (void)hv_store(hv,key,klen,sv,hash);
1051 =for apidoc hv_exists_ent
1053 Returns a boolean indicating whether the specified hash key exists. C<hash>
1054 can be a valid precomputed hash value, or 0 to ask for it to be
1061 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1063 register XPVHV* xhv;
1074 if (SvRMAGICAL(hv)) {
1075 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1076 SV* svret = sv_newmortal();
1077 sv = sv_newmortal();
1078 keysv = sv_2mortal(newSVsv(keysv));
1079 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1080 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1081 return SvTRUE(svret);
1083 #ifdef ENV_IS_CASELESS
1084 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1085 key = SvPV(keysv, klen);
1086 keysv = sv_2mortal(newSVpvn(key,klen));
1087 (void)strupr(SvPVX(keysv));
1093 xhv = (XPVHV*)SvANY(hv);
1094 #ifndef DYNAMIC_ENV_FETCH
1095 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1099 keysave = key = SvPV(keysv, klen);
1100 is_utf8 = (SvUTF8(keysv) != 0);
1102 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1104 PERL_HASH(hash, key, klen);
1106 #ifdef DYNAMIC_ENV_FETCH
1107 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1110 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1111 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1112 for (; entry; entry = HeNEXT(entry)) {
1113 if (HeHASH(entry) != hash) /* strings can't be equal */
1115 if (HeKLEN(entry) != klen)
1117 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1119 if (HeKUTF8(entry) != (char)is_utf8)
1123 /* If we find the key, but the value is a placeholder, return false. */
1124 if (HeVAL(entry) == &PL_sv_undef)
1128 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1129 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1131 char *env = PerlEnv_ENVgetenv_len(key,&len);
1133 sv = newSVpvn(env,len);
1135 (void)hv_store_ent(hv,keysv,sv,hash);
1146 S_hsplit(pTHX_ HV *hv)
1148 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1149 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1150 register I32 newsize = oldsize * 2;
1152 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1156 register HE **oentry;
1159 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1160 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1166 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1171 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1172 if (oldsize >= 64) {
1173 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1174 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1177 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1181 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1182 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1183 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1186 for (i=0; i<oldsize; i++,aep++) {
1187 if (!*aep) /* non-existent */
1190 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1191 if ((HeHASH(entry) & newsize) != i) {
1192 *oentry = HeNEXT(entry);
1193 HeNEXT(entry) = *bep;
1195 xhv->xhv_fill++; /* HvFILL(hv)++ */
1200 oentry = &HeNEXT(entry);
1202 if (!*aep) /* everything moved */
1203 xhv->xhv_fill--; /* HvFILL(hv)-- */
1208 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1210 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1211 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1212 register I32 newsize;
1218 register HE **oentry;
1220 newsize = (I32) newmax; /* possible truncation here */
1221 if (newsize != newmax || newmax <= oldsize)
1223 while ((newsize & (1 + ~newsize)) != newsize) {
1224 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1226 if (newsize < newmax)
1228 if (newsize < newmax)
1229 return; /* overflow detection */
1231 a = xhv->xhv_array; /* HvARRAY(hv) */
1234 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1235 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1241 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1246 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1247 if (oldsize >= 64) {
1248 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1249 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1252 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1255 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1258 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1260 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1261 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1262 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1266 for (i=0; i<oldsize; i++,aep++) {
1267 if (!*aep) /* non-existent */
1269 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1270 if ((j = (HeHASH(entry) & newsize)) != i) {
1272 *oentry = HeNEXT(entry);
1273 if (!(HeNEXT(entry) = aep[j]))
1274 xhv->xhv_fill++; /* HvFILL(hv)++ */
1279 oentry = &HeNEXT(entry);
1281 if (!*aep) /* everything moved */
1282 xhv->xhv_fill--; /* HvFILL(hv)-- */
1289 Creates a new HV. The reference count is set to 1.
1298 register XPVHV* xhv;
1300 hv = (HV*)NEWSV(502,0);
1301 sv_upgrade((SV *)hv, SVt_PVHV);
1302 xhv = (XPVHV*)SvANY(hv);
1305 #ifndef NODEFAULT_SHAREKEYS
1306 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1308 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1309 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1310 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1311 (void)hv_iterinit(hv); /* so each() will start off right */
1316 Perl_newHVhv(pTHX_ HV *ohv)
1319 STRLEN hv_max, hv_fill;
1321 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1323 hv_max = HvMAX(ohv);
1325 if (!SvMAGICAL((SV *)ohv)) {
1326 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1327 int i, shared = !!HvSHAREKEYS(ohv);
1328 HE **ents, **oents = (HE **)HvARRAY(ohv);
1330 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1333 /* In each bucket... */
1334 for (i = 0; i <= hv_max; i++) {
1335 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1342 /* Copy the linked list of entries. */
1343 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1344 U32 hash = HeHASH(oent);
1345 char *key = HeKEY(oent);
1346 STRLEN len = HeKLEN_UTF8(oent);
1349 HeVAL(ent) = newSVsv(HeVAL(oent));
1350 HeKEY_hek(ent) = shared ? share_hek(key, len, hash)
1351 : save_hek(key, len, hash);
1362 HvFILL(hv) = hv_fill;
1363 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1367 /* Iterate over ohv, copying keys and values one at a time. */
1369 I32 riter = HvRITER(ohv);
1370 HE *eiter = HvEITER(ohv);
1372 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1373 while (hv_max && hv_max + 1 >= hv_fill * 2)
1374 hv_max = hv_max / 2;
1378 while ((entry = hv_iternext(ohv))) {
1379 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1380 newSVsv(HeVAL(entry)), HeHASH(entry));
1382 HvRITER(ohv) = riter;
1383 HvEITER(ohv) = eiter;
1390 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1397 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1398 PL_sub_generation++; /* may be deletion of method from stash */
1400 if (HeKLEN(entry) == HEf_SVKEY) {
1401 SvREFCNT_dec(HeKEY_sv(entry));
1402 Safefree(HeKEY_hek(entry));
1404 else if (HvSHAREKEYS(hv))
1405 unshare_hek(HeKEY_hek(entry));
1407 Safefree(HeKEY_hek(entry));
1412 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1416 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1417 PL_sub_generation++; /* may be deletion of method from stash */
1418 sv_2mortal(HeVAL(entry)); /* free between statements */
1419 if (HeKLEN(entry) == HEf_SVKEY) {
1420 sv_2mortal(HeKEY_sv(entry));
1421 Safefree(HeKEY_hek(entry));
1423 else if (HvSHAREKEYS(hv))
1424 unshare_hek(HeKEY_hek(entry));
1426 Safefree(HeKEY_hek(entry));
1431 =for apidoc hv_clear
1433 Clears a hash, making it empty.
1439 Perl_hv_clear(pTHX_ HV *hv)
1441 register XPVHV* xhv;
1444 xhv = (XPVHV*)SvANY(hv);
1446 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1447 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1448 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1449 if (xhv->xhv_array /* HvARRAY(hv) */)
1450 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1451 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1458 S_hfreeentries(pTHX_ HV *hv)
1460 register HE **array;
1462 register HE *oentry = Null(HE*);
1473 array = HvARRAY(hv);
1478 entry = HeNEXT(entry);
1479 hv_free_ent(hv, oentry);
1484 entry = array[riter];
1487 (void)hv_iterinit(hv);
1491 =for apidoc hv_undef
1499 Perl_hv_undef(pTHX_ HV *hv)
1501 register XPVHV* xhv;
1504 xhv = (XPVHV*)SvANY(hv);
1506 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1508 Safefree(HvNAME(hv));
1511 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1512 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1513 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1514 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1515 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1522 =for apidoc hv_iterinit
1524 Prepares a starting point to traverse a hash table. Returns the number of
1525 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1526 currently only meaningful for hashes without tie magic.
1528 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1529 hash buckets that happen to be in use. If you still need that esoteric
1530 value, you can get it through the macro C<HvFILL(tb)>.
1536 Perl_hv_iterinit(pTHX_ HV *hv)
1538 register XPVHV* xhv;
1542 Perl_croak(aTHX_ "Bad hash");
1543 xhv = (XPVHV*)SvANY(hv);
1544 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1545 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1547 hv_free_ent(hv, entry);
1549 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1550 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1551 /* used to be xhv->xhv_fill before 5.004_65 */
1552 return XHvTOTALKEYS(xhv);
1556 =for apidoc hv_iternext
1558 Returns entries from a hash iterator. See C<hv_iterinit>.
1564 Perl_hv_iternext(pTHX_ HV *hv)
1566 register XPVHV* xhv;
1572 Perl_croak(aTHX_ "Bad hash");
1573 xhv = (XPVHV*)SvANY(hv);
1574 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1576 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1577 SV *key = sv_newmortal();
1579 sv_setsv(key, HeSVKEY_force(entry));
1580 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1586 /* one HE per MAGICAL hash */
1587 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1589 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1591 HeKEY_hek(entry) = hek;
1592 HeKLEN(entry) = HEf_SVKEY;
1594 magic_nextpack((SV*) hv,mg,key);
1596 /* force key to stay around until next time */
1597 HeSVKEY_set(entry, SvREFCNT_inc(key));
1598 return entry; /* beware, hent_val is not set */
1601 SvREFCNT_dec(HeVAL(entry));
1602 Safefree(HeKEY_hek(entry));
1604 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1607 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1608 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1612 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1613 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1614 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1618 entry = HeNEXT(entry);
1620 * Skip past any placeholders -- don't want to include them in
1623 while (entry && HeVAL(entry) == &PL_sv_undef) {
1624 entry = HeNEXT(entry);
1628 xhv->xhv_riter++; /* HvRITER(hv)++ */
1629 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1630 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1633 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1634 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1636 /* if we have an entry, but it's a placeholder, don't count it */
1637 if (entry && HeVAL(entry) == &PL_sv_undef)
1642 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1644 hv_free_ent(hv, oldentry);
1647 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1652 =for apidoc hv_iterkey
1654 Returns the key from the current position of the hash iterator. See
1661 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1663 if (HeKLEN(entry) == HEf_SVKEY) {
1665 char *p = SvPV(HeKEY_sv(entry), len);
1670 *retlen = HeKLEN(entry);
1671 return HeKEY(entry);
1675 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1677 =for apidoc hv_iterkeysv
1679 Returns the key as an C<SV*> from the current position of the hash
1680 iterator. The return value will always be a mortal copy of the key. Also
1687 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1689 if (HeKLEN(entry) == HEf_SVKEY)
1690 return sv_mortalcopy(HeKEY_sv(entry));
1692 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1693 HeKLEN_UTF8(entry), HeHASH(entry)));
1697 =for apidoc hv_iterval
1699 Returns the value from the current position of the hash iterator. See
1706 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1708 if (SvRMAGICAL(hv)) {
1709 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1710 SV* sv = sv_newmortal();
1711 if (HeKLEN(entry) == HEf_SVKEY)
1712 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1713 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1717 return HeVAL(entry);
1721 =for apidoc hv_iternextsv
1723 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1730 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1733 if ( (he = hv_iternext(hv)) == NULL)
1735 *key = hv_iterkey(he, retlen);
1736 return hv_iterval(hv, he);
1740 =for apidoc hv_magic
1742 Adds magic to a hash. See C<sv_magic>.
1748 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1750 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1753 #if 0 /* use the macro from hv.h instead */
1756 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1758 return HEK_KEY(share_hek(sv, len, hash));
1763 /* possibly free a shared string if no one has access to it
1764 * len and hash must both be valid for str.
1767 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1769 register XPVHV* xhv;
1771 register HE **oentry;
1774 bool is_utf8 = FALSE;
1775 const char *save = str;
1778 STRLEN tmplen = -len;
1780 /* See the note in hv_fetch(). --jhi */
1781 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1785 /* what follows is the moral equivalent of:
1786 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1787 if (--*Svp == Nullsv)
1788 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1790 xhv = (XPVHV*)SvANY(PL_strtab);
1791 /* assert(xhv_array != 0) */
1793 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1794 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1795 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1796 if (HeHASH(entry) != hash) /* strings can't be equal */
1798 if (HeKLEN(entry) != len)
1800 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1802 if (HeKUTF8(entry) != (char)is_utf8)
1805 if (--HeVAL(entry) == Nullsv) {
1806 *oentry = HeNEXT(entry);
1808 xhv->xhv_fill--; /* HvFILL(hv)-- */
1809 Safefree(HeKEY_hek(entry));
1811 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1815 UNLOCK_STRTAB_MUTEX;
1818 if (!found && ckWARN_d(WARN_INTERNAL))
1819 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1822 /* get a (constant) string ptr from the global string table
1823 * string will get added if it is not already there.
1824 * len and hash must both be valid for str.
1827 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1829 register XPVHV* xhv;
1831 register HE **oentry;
1834 bool is_utf8 = FALSE;
1835 const char *save = str;
1838 STRLEN tmplen = -len;
1840 /* See the note in hv_fetch(). --jhi */
1841 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1845 /* what follows is the moral equivalent of:
1847 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1848 hv_store(PL_strtab, str, len, Nullsv, hash);
1850 xhv = (XPVHV*)SvANY(PL_strtab);
1851 /* assert(xhv_array != 0) */
1853 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1854 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1855 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1856 if (HeHASH(entry) != hash) /* strings can't be equal */
1858 if (HeKLEN(entry) != len)
1860 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1862 if (HeKUTF8(entry) != (char)is_utf8)
1869 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1870 HeVAL(entry) = Nullsv;
1871 HeNEXT(entry) = *oentry;
1873 xhv->xhv_keys++; /* HvKEYS(hv)++ */
1874 if (i) { /* initial entry? */
1875 xhv->xhv_fill++; /* HvFILL(hv)++ */
1876 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
1881 ++HeVAL(entry); /* use value slot as REFCNT */
1882 UNLOCK_STRTAB_MUTEX;
1885 return HeKEY_hek(entry);