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 *oentry = HeNEXT(entry);
773 xhv->xhv_fill--; /* HvFILL(hv)-- */
774 if (flags & G_DISCARD)
777 sv = sv_2mortal(HeVAL(entry));
778 HeVAL(entry) = &PL_sv_undef;
782 * If a restricted hash, rather than really deleting the entry, put
783 * a placeholder there. This marks the key as being "approved", so
784 * we can still access via not-really-existing key without raising
787 if (SvREADONLY(hv)) {
788 HeVAL(entry) = &PL_sv_undef;
789 /* We'll be saving this slot, so the number of allocated keys
790 * doesn't go down, but the number placeholders goes up */
791 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(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--;
910 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
911 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
914 *oentry = HeNEXT(entry);
916 xhv->xhv_fill--; /* HvFILL(hv)-- */
917 if (flags & G_DISCARD)
920 sv = sv_2mortal(HeVAL(entry));
921 HeVAL(entry) = &PL_sv_undef;
925 * If a restricted hash, rather than really deleting the entry, put
926 * a placeholder there. This marks the key as being "approved", so
927 * we can still access via not-really-existing key without raising
930 if (SvREADONLY(hv)) {
931 HeVAL(entry) = &PL_sv_undef;
932 /* We'll be saving this slot, so the number of allocated keys
933 * doesn't go down, but the number placeholders goes up */
934 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
936 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
939 hv_free_ent(hv, entry);
940 xhv->xhv_keys--; /* HvKEYS(hv)-- */
944 if (SvREADONLY(hv)) {
945 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
954 =for apidoc hv_exists
956 Returns a boolean indicating whether the specified hash key exists. The
957 C<klen> is the length of the key.
963 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
969 bool is_utf8 = FALSE;
970 const char *keysave = key;
980 if (SvRMAGICAL(hv)) {
981 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
983 mg_copy((SV*)hv, sv, key, klen);
984 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
987 #ifdef ENV_IS_CASELESS
988 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
989 sv = sv_2mortal(newSVpvn(key,klen));
990 key = strupr(SvPVX(sv));
995 xhv = (XPVHV*)SvANY(hv);
996 #ifndef DYNAMIC_ENV_FETCH
997 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1002 STRLEN tmplen = klen;
1003 /* See the note in hv_fetch(). --jhi */
1004 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1008 PERL_HASH(hash, key, klen);
1010 #ifdef DYNAMIC_ENV_FETCH
1011 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1014 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1015 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1016 for (; entry; entry = HeNEXT(entry)) {
1017 if (HeHASH(entry) != hash) /* strings can't be equal */
1019 if (HeKLEN(entry) != klen)
1021 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1023 if (HeKUTF8(entry) != (char)is_utf8)
1027 /* If we find the key, but the value is a placeholder, return false. */
1028 if (HeVAL(entry) == &PL_sv_undef)
1033 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1034 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1036 char *env = PerlEnv_ENVgetenv_len(key,&len);
1038 sv = newSVpvn(env,len);
1040 (void)hv_store(hv,key,klen,sv,hash);
1052 =for apidoc hv_exists_ent
1054 Returns a boolean indicating whether the specified hash key exists. C<hash>
1055 can be a valid precomputed hash value, or 0 to ask for it to be
1062 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1064 register XPVHV* xhv;
1075 if (SvRMAGICAL(hv)) {
1076 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1077 SV* svret = sv_newmortal();
1078 sv = sv_newmortal();
1079 keysv = sv_2mortal(newSVsv(keysv));
1080 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1081 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1082 return SvTRUE(svret);
1084 #ifdef ENV_IS_CASELESS
1085 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1086 key = SvPV(keysv, klen);
1087 keysv = sv_2mortal(newSVpvn(key,klen));
1088 (void)strupr(SvPVX(keysv));
1094 xhv = (XPVHV*)SvANY(hv);
1095 #ifndef DYNAMIC_ENV_FETCH
1096 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1100 keysave = key = SvPV(keysv, klen);
1101 is_utf8 = (SvUTF8(keysv) != 0);
1103 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1105 PERL_HASH(hash, key, klen);
1107 #ifdef DYNAMIC_ENV_FETCH
1108 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1111 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1112 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1113 for (; entry; entry = HeNEXT(entry)) {
1114 if (HeHASH(entry) != hash) /* strings can't be equal */
1116 if (HeKLEN(entry) != klen)
1118 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1120 if (HeKUTF8(entry) != (char)is_utf8)
1124 /* If we find the key, but the value is a placeholder, return false. */
1125 if (HeVAL(entry) == &PL_sv_undef)
1129 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1130 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1132 char *env = PerlEnv_ENVgetenv_len(key,&len);
1134 sv = newSVpvn(env,len);
1136 (void)hv_store_ent(hv,keysv,sv,hash);
1147 S_hsplit(pTHX_ HV *hv)
1149 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1150 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1151 register I32 newsize = oldsize * 2;
1153 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1157 register HE **oentry;
1160 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1161 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1167 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1172 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1173 if (oldsize >= 64) {
1174 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1175 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1178 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1182 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1183 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1184 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1187 for (i=0; i<oldsize; i++,aep++) {
1188 if (!*aep) /* non-existent */
1191 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1192 if ((HeHASH(entry) & newsize) != i) {
1193 *oentry = HeNEXT(entry);
1194 HeNEXT(entry) = *bep;
1196 xhv->xhv_fill++; /* HvFILL(hv)++ */
1201 oentry = &HeNEXT(entry);
1203 if (!*aep) /* everything moved */
1204 xhv->xhv_fill--; /* HvFILL(hv)-- */
1209 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1211 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1212 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1213 register I32 newsize;
1219 register HE **oentry;
1221 newsize = (I32) newmax; /* possible truncation here */
1222 if (newsize != newmax || newmax <= oldsize)
1224 while ((newsize & (1 + ~newsize)) != newsize) {
1225 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1227 if (newsize < newmax)
1229 if (newsize < newmax)
1230 return; /* overflow detection */
1232 a = xhv->xhv_array; /* HvARRAY(hv) */
1235 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1236 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1242 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1247 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1248 if (oldsize >= 64) {
1249 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1250 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1253 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1256 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1259 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1261 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1262 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1263 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1267 for (i=0; i<oldsize; i++,aep++) {
1268 if (!*aep) /* non-existent */
1270 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1271 if ((j = (HeHASH(entry) & newsize)) != i) {
1273 *oentry = HeNEXT(entry);
1274 if (!(HeNEXT(entry) = aep[j]))
1275 xhv->xhv_fill++; /* HvFILL(hv)++ */
1280 oentry = &HeNEXT(entry);
1282 if (!*aep) /* everything moved */
1283 xhv->xhv_fill--; /* HvFILL(hv)-- */
1290 Creates a new HV. The reference count is set to 1.
1299 register XPVHV* xhv;
1301 hv = (HV*)NEWSV(502,0);
1302 sv_upgrade((SV *)hv, SVt_PVHV);
1303 xhv = (XPVHV*)SvANY(hv);
1306 #ifndef NODEFAULT_SHAREKEYS
1307 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1309 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1310 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1311 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1312 (void)hv_iterinit(hv); /* so each() will start off right */
1317 Perl_newHVhv(pTHX_ HV *ohv)
1320 STRLEN hv_max, hv_fill;
1322 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1324 hv_max = HvMAX(ohv);
1326 if (!SvMAGICAL((SV *)ohv)) {
1327 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1328 int i, shared = !!HvSHAREKEYS(ohv);
1329 HE **ents, **oents = (HE **)HvARRAY(ohv);
1331 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1334 /* In each bucket... */
1335 for (i = 0; i <= hv_max; i++) {
1336 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1343 /* Copy the linked list of entries. */
1344 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1345 U32 hash = HeHASH(oent);
1346 char *key = HeKEY(oent);
1347 STRLEN len = HeKLEN_UTF8(oent);
1350 HeVAL(ent) = newSVsv(HeVAL(oent));
1351 HeKEY_hek(ent) = shared ? share_hek(key, len, hash)
1352 : save_hek(key, len, hash);
1363 HvFILL(hv) = hv_fill;
1364 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1368 /* Iterate over ohv, copying keys and values one at a time. */
1370 I32 riter = HvRITER(ohv);
1371 HE *eiter = HvEITER(ohv);
1373 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1374 while (hv_max && hv_max + 1 >= hv_fill * 2)
1375 hv_max = hv_max / 2;
1379 while ((entry = hv_iternext(ohv))) {
1380 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1381 newSVsv(HeVAL(entry)), HeHASH(entry));
1383 HvRITER(ohv) = riter;
1384 HvEITER(ohv) = eiter;
1391 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1398 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1399 PL_sub_generation++; /* may be deletion of method from stash */
1401 if (HeKLEN(entry) == HEf_SVKEY) {
1402 SvREFCNT_dec(HeKEY_sv(entry));
1403 Safefree(HeKEY_hek(entry));
1405 else if (HvSHAREKEYS(hv))
1406 unshare_hek(HeKEY_hek(entry));
1408 Safefree(HeKEY_hek(entry));
1413 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1417 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1418 PL_sub_generation++; /* may be deletion of method from stash */
1419 sv_2mortal(HeVAL(entry)); /* free between statements */
1420 if (HeKLEN(entry) == HEf_SVKEY) {
1421 sv_2mortal(HeKEY_sv(entry));
1422 Safefree(HeKEY_hek(entry));
1424 else if (HvSHAREKEYS(hv))
1425 unshare_hek(HeKEY_hek(entry));
1427 Safefree(HeKEY_hek(entry));
1432 =for apidoc hv_clear
1434 Clears a hash, making it empty.
1440 Perl_hv_clear(pTHX_ HV *hv)
1442 register XPVHV* xhv;
1445 xhv = (XPVHV*)SvANY(hv);
1447 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1448 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1449 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1450 if (xhv->xhv_array /* HvARRAY(hv) */)
1451 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1452 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1459 S_hfreeentries(pTHX_ HV *hv)
1461 register HE **array;
1463 register HE *oentry = Null(HE*);
1474 array = HvARRAY(hv);
1479 entry = HeNEXT(entry);
1480 hv_free_ent(hv, oentry);
1485 entry = array[riter];
1488 (void)hv_iterinit(hv);
1492 =for apidoc hv_undef
1500 Perl_hv_undef(pTHX_ HV *hv)
1502 register XPVHV* xhv;
1505 xhv = (XPVHV*)SvANY(hv);
1507 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1509 Safefree(HvNAME(hv));
1512 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1513 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1514 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1515 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1516 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1523 =for apidoc hv_iterinit
1525 Prepares a starting point to traverse a hash table. Returns the number of
1526 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1527 currently only meaningful for hashes without tie magic.
1529 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1530 hash buckets that happen to be in use. If you still need that esoteric
1531 value, you can get it through the macro C<HvFILL(tb)>.
1537 Perl_hv_iterinit(pTHX_ HV *hv)
1539 register XPVHV* xhv;
1543 Perl_croak(aTHX_ "Bad hash");
1544 xhv = (XPVHV*)SvANY(hv);
1545 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1546 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1548 hv_free_ent(hv, entry);
1550 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1551 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1552 /* used to be xhv->xhv_fill before 5.004_65 */
1553 return XHvTOTALKEYS(xhv);
1557 =for apidoc hv_iternext
1559 Returns entries from a hash iterator. See C<hv_iterinit>.
1565 Perl_hv_iternext(pTHX_ HV *hv)
1567 register XPVHV* xhv;
1573 Perl_croak(aTHX_ "Bad hash");
1574 xhv = (XPVHV*)SvANY(hv);
1575 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1577 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1578 SV *key = sv_newmortal();
1580 sv_setsv(key, HeSVKEY_force(entry));
1581 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1587 /* one HE per MAGICAL hash */
1588 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1590 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1592 HeKEY_hek(entry) = hek;
1593 HeKLEN(entry) = HEf_SVKEY;
1595 magic_nextpack((SV*) hv,mg,key);
1597 /* force key to stay around until next time */
1598 HeSVKEY_set(entry, SvREFCNT_inc(key));
1599 return entry; /* beware, hent_val is not set */
1602 SvREFCNT_dec(HeVAL(entry));
1603 Safefree(HeKEY_hek(entry));
1605 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1608 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1609 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1613 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1614 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1615 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1619 entry = HeNEXT(entry);
1621 * Skip past any placeholders -- don't want to include them in
1624 while (entry && HeVAL(entry) == &PL_sv_undef) {
1625 entry = HeNEXT(entry);
1629 xhv->xhv_riter++; /* HvRITER(hv)++ */
1630 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1631 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1634 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1635 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1637 /* if we have an entry, but it's a placeholder, don't count it */
1638 if (entry && HeVAL(entry) == &PL_sv_undef)
1643 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1645 hv_free_ent(hv, oldentry);
1648 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1653 =for apidoc hv_iterkey
1655 Returns the key from the current position of the hash iterator. See
1662 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1664 if (HeKLEN(entry) == HEf_SVKEY) {
1666 char *p = SvPV(HeKEY_sv(entry), len);
1671 *retlen = HeKLEN(entry);
1672 return HeKEY(entry);
1676 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1678 =for apidoc hv_iterkeysv
1680 Returns the key as an C<SV*> from the current position of the hash
1681 iterator. The return value will always be a mortal copy of the key. Also
1688 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1690 if (HeKLEN(entry) == HEf_SVKEY)
1691 return sv_mortalcopy(HeKEY_sv(entry));
1693 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1694 HeKLEN_UTF8(entry), HeHASH(entry)));
1698 =for apidoc hv_iterval
1700 Returns the value from the current position of the hash iterator. See
1707 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1709 if (SvRMAGICAL(hv)) {
1710 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1711 SV* sv = sv_newmortal();
1712 if (HeKLEN(entry) == HEf_SVKEY)
1713 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1714 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1718 return HeVAL(entry);
1722 =for apidoc hv_iternextsv
1724 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1731 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1734 if ( (he = hv_iternext(hv)) == NULL)
1736 *key = hv_iterkey(he, retlen);
1737 return hv_iterval(hv, he);
1741 =for apidoc hv_magic
1743 Adds magic to a hash. See C<sv_magic>.
1749 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1751 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1754 #if 0 /* use the macro from hv.h instead */
1757 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1759 return HEK_KEY(share_hek(sv, len, hash));
1764 /* possibly free a shared string if no one has access to it
1765 * len and hash must both be valid for str.
1768 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1770 register XPVHV* xhv;
1772 register HE **oentry;
1775 bool is_utf8 = FALSE;
1776 const char *save = str;
1779 STRLEN tmplen = -len;
1781 /* See the note in hv_fetch(). --jhi */
1782 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1786 /* what follows is the moral equivalent of:
1787 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1788 if (--*Svp == Nullsv)
1789 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1791 xhv = (XPVHV*)SvANY(PL_strtab);
1792 /* assert(xhv_array != 0) */
1794 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1795 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1796 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1797 if (HeHASH(entry) != hash) /* strings can't be equal */
1799 if (HeKLEN(entry) != len)
1801 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1803 if (HeKUTF8(entry) != (char)is_utf8)
1806 if (--HeVAL(entry) == Nullsv) {
1807 *oentry = HeNEXT(entry);
1809 xhv->xhv_fill--; /* HvFILL(hv)-- */
1810 Safefree(HeKEY_hek(entry));
1812 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1816 UNLOCK_STRTAB_MUTEX;
1819 if (!found && ckWARN_d(WARN_INTERNAL))
1820 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1823 /* get a (constant) string ptr from the global string table
1824 * string will get added if it is not already there.
1825 * len and hash must both be valid for str.
1828 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1830 register XPVHV* xhv;
1832 register HE **oentry;
1835 bool is_utf8 = FALSE;
1836 const char *save = str;
1839 STRLEN tmplen = -len;
1841 /* See the note in hv_fetch(). --jhi */
1842 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1846 /* what follows is the moral equivalent of:
1848 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1849 hv_store(PL_strtab, str, len, Nullsv, hash);
1851 xhv = (XPVHV*)SvANY(PL_strtab);
1852 /* assert(xhv_array != 0) */
1854 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1855 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1856 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1857 if (HeHASH(entry) != hash) /* strings can't be equal */
1859 if (HeKLEN(entry) != len)
1861 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1863 if (HeKUTF8(entry) != (char)is_utf8)
1870 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1871 HeVAL(entry) = Nullsv;
1872 HeNEXT(entry) = *oentry;
1874 xhv->xhv_keys++; /* HvKEYS(hv)++ */
1875 if (i) { /* initial entry? */
1876 xhv->xhv_fill++; /* HvFILL(hv)++ */
1877 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
1882 ++HeVAL(entry); /* use value slot as REFCNT */
1883 UNLOCK_STRTAB_MUTEX;
1886 return HeKEY_hek(entry);