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,
136 const char *keysave, const char *msg)
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_ msg, 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,
270 "Attempt to access disallowed key '%"SVf"' in a fixed hash"
273 if (lval) { /* gonna assign to this, so it better be there */
275 if (key != keysave) { /* must be is_utf8 == 0 */
276 SV **ret = hv_store(hv,key,klen,sv,hash);
281 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
288 /* returns an HE * structure with the all fields set */
289 /* note that hent_val will be a mortal sv for MAGICAL hashes */
291 =for apidoc hv_fetch_ent
293 Returns the hash entry which corresponds to the specified key in the hash.
294 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
295 if you want the function to compute it. IF C<lval> is set then the fetch
296 will be part of a store. Make sure the return value is non-null before
297 accessing it. The return value when C<tb> is a tied hash is a pointer to a
298 static location, so be sure to make a copy of the structure if you need to
301 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
302 information on how to use this function on tied hashes.
308 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
321 if (SvRMAGICAL(hv)) {
322 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
324 keysv = sv_2mortal(newSVsv(keysv));
325 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
326 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
328 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
329 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
331 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
332 HeVAL(&PL_hv_fetch_ent_mh) = sv;
333 return &PL_hv_fetch_ent_mh;
335 #ifdef ENV_IS_CASELESS
336 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
338 key = SvPV(keysv, klen);
339 for (i = 0; i < klen; ++i)
340 if (isLOWER(key[i])) {
341 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
342 (void)strupr(SvPVX(nkeysv));
343 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
345 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
352 xhv = (XPVHV*)SvANY(hv);
353 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
355 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
356 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
359 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
360 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
366 keysave = key = SvPV(keysv, klen);
367 is_utf8 = (SvUTF8(keysv)!=0);
370 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
373 PERL_HASH(hash, key, klen);
375 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
376 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
377 for (; entry; entry = HeNEXT(entry)) {
378 if (HeHASH(entry) != hash) /* strings can't be equal */
380 if (HeKLEN(entry) != klen)
382 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
384 if (HeKUTF8(entry) != (char)is_utf8)
388 /* if we find a placeholder, we pretend we haven't found anything */
389 if (HeVAL(entry) == &PL_sv_undef)
393 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
394 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
396 char *env = PerlEnv_ENVgetenv_len(key,&len);
398 sv = newSVpvn(env,len);
400 return hv_store_ent(hv,keysv,sv,hash);
404 if (!entry && SvREADONLY(hv)) {
405 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
406 "Attempt to access disallowed key '%"SVf"' in a fixed hash"
411 if (lval) { /* gonna assign to this, so it better be there */
413 return hv_store_ent(hv,keysv,sv,hash);
419 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
421 MAGIC *mg = SvMAGIC(hv);
425 if (isUPPER(mg->mg_type)) {
427 switch (mg->mg_type) {
428 case PERL_MAGIC_tied:
430 *needs_store = FALSE;
433 mg = mg->mg_moremagic;
440 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
441 the length of the key. The C<hash> parameter is the precomputed hash
442 value; if it is zero then Perl will compute it. The return value will be
443 NULL if the operation failed or if the value did not need to be actually
444 stored within the hash (as in the case of tied hashes). Otherwise it can
445 be dereferenced to get the original C<SV*>. Note that the caller is
446 responsible for suitably incrementing the reference count of C<val> before
447 the call, and decrementing it if the function returned NULL.
449 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
450 information on how to use this function on tied hashes.
456 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
461 register HE **oentry;
462 bool is_utf8 = FALSE;
463 const char *keysave = key;
473 xhv = (XPVHV*)SvANY(hv);
477 hv_magic_check (hv, &needs_copy, &needs_store);
479 mg_copy((SV*)hv, val, key, klen);
480 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store)
482 #ifdef ENV_IS_CASELESS
483 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
484 key = savepvn(key,klen);
485 key = (const char*)strupr((char*)key);
492 STRLEN tmplen = klen;
493 /* See the note in hv_fetch(). --jhi */
494 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
499 PERL_HASH(hash, key, klen);
501 if (!xhv->xhv_array /* !HvARRAY(hv) */)
502 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
503 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
506 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
507 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
510 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
511 if (HeHASH(entry) != hash) /* strings can't be equal */
513 if (HeKLEN(entry) != klen)
515 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
517 if (HeKUTF8(entry) != (char)is_utf8)
519 if (HeVAL(entry) == &PL_sv_undef)
520 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
522 SvREFCNT_dec(HeVAL(entry));
526 return &HeVAL(entry);
529 if (SvREADONLY(hv)) {
530 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
531 "Attempt to access disallowed key '%"SVf"' to a fixed hash"
537 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
538 else /* gotta do the real thing */
539 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
543 HeNEXT(entry) = *oentry;
546 xhv->xhv_keys++; /* HvKEYS(hv)++ */
547 if (i) { /* initial entry? */
548 xhv->xhv_fill++; /* HvFILL(hv)++ */
549 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
553 return &HeVAL(entry);
557 =for apidoc hv_store_ent
559 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
560 parameter is the precomputed hash value; if it is zero then Perl will
561 compute it. The return value is the new hash entry so created. It will be
562 NULL if the operation failed or if the value did not need to be actually
563 stored within the hash (as in the case of tied hashes). Otherwise the
564 contents of the return value can be accessed using the C<He?> macros
565 described here. Note that the caller is responsible for suitably
566 incrementing the reference count of C<val> before the call, and
567 decrementing it if the function returned NULL.
569 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
570 information on how to use this function on tied hashes.
576 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
583 register HE **oentry;
590 xhv = (XPVHV*)SvANY(hv);
594 hv_magic_check (hv, &needs_copy, &needs_store);
596 bool save_taint = PL_tainted;
598 PL_tainted = SvTAINTED(keysv);
599 keysv = sv_2mortal(newSVsv(keysv));
600 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
601 TAINT_IF(save_taint);
602 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
604 #ifdef ENV_IS_CASELESS
605 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
606 key = SvPV(keysv, klen);
607 keysv = sv_2mortal(newSVpvn(key,klen));
608 (void)strupr(SvPVX(keysv));
615 keysave = key = SvPV(keysv, klen);
616 is_utf8 = (SvUTF8(keysv) != 0);
619 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
622 PERL_HASH(hash, key, klen);
624 if (!xhv->xhv_array /* !HvARRAY(hv) */)
625 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
626 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
629 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
630 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
633 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
634 if (HeHASH(entry) != hash) /* strings can't be equal */
636 if (HeKLEN(entry) != klen)
638 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
640 if (HeKUTF8(entry) != (char)is_utf8)
642 if (HeVAL(entry) == &PL_sv_undef)
643 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
645 SvREFCNT_dec(HeVAL(entry));
652 if (SvREADONLY(hv)) {
653 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
654 "Attempt to access disallowed key '%"SVf"' to a fixed hash"
660 HeKEY_hek(entry) = share_hek(key, is_utf8?-(I32)klen:klen, hash);
661 else /* gotta do the real thing */
662 HeKEY_hek(entry) = save_hek(key, is_utf8?-(I32)klen:klen, hash);
666 HeNEXT(entry) = *oentry;
669 xhv->xhv_keys++; /* HvKEYS(hv)++ */
670 if (i) { /* initial entry? */
671 xhv->xhv_fill++; /* HvFILL(hv)++ */
672 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
680 =for apidoc hv_delete
682 Deletes a key/value pair in the hash. The value SV is removed from the
683 hash and returned to the caller. The C<klen> is the length of the key.
684 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
691 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
697 register HE **oentry;
700 bool is_utf8 = FALSE;
701 const char *keysave = key;
709 if (SvRMAGICAL(hv)) {
712 hv_magic_check (hv, &needs_copy, &needs_store);
714 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
718 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
719 /* No longer an element */
720 sv_unmagic(sv, PERL_MAGIC_tiedelem);
723 return Nullsv; /* element cannot be deleted */
725 #ifdef ENV_IS_CASELESS
726 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
727 sv = sv_2mortal(newSVpvn(key,klen));
728 key = strupr(SvPVX(sv));
733 xhv = (XPVHV*)SvANY(hv);
734 if (!xhv->xhv_array /* !HvARRAY(hv) */)
738 STRLEN tmplen = klen;
739 /* See the note in hv_fetch(). --jhi */
740 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
744 PERL_HASH(hash, key, klen);
746 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
747 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
750 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
751 if (HeHASH(entry) != hash) /* strings can't be equal */
753 if (HeKLEN(entry) != klen)
755 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
757 if (HeKUTF8(entry) != (char)is_utf8)
761 /* if placeholder is here, it's already been deleted.... */
762 if (HeVAL(entry) == &PL_sv_undef)
765 return Nullsv; /* if still SvREADONLY, leave it deleted. */
767 /* okay, really delete the placeholder... */
768 *oentry = HeNEXT(entry);
770 xhv->xhv_fill--; /* HvFILL(hv)-- */
771 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
774 hv_free_ent(hv, entry);
775 xhv->xhv_keys--; /* HvKEYS(hv)-- */
776 xhv->xhv_placeholders--;
780 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
781 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
782 "Attempt to delete readonly key '%"SVf"' from a fixed hash"
786 if (flags & G_DISCARD)
789 sv = sv_2mortal(HeVAL(entry));
790 HeVAL(entry) = &PL_sv_undef;
794 * If a restricted hash, rather than really deleting the entry, put
795 * a placeholder there. This marks the key as being "approved", so
796 * we can still access via not-really-existing key without raising
799 if (SvREADONLY(hv)) {
800 HeVAL(entry) = &PL_sv_undef;
801 /* We'll be saving this slot, so the number of allocated keys
802 * doesn't go down, but the number placeholders goes up */
803 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
805 *oentry = HeNEXT(entry);
807 xhv->xhv_fill--; /* HvFILL(hv)-- */
808 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
811 hv_free_ent(hv, entry);
812 xhv->xhv_keys--; /* HvKEYS(hv)-- */
816 if (SvREADONLY(hv)) {
817 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
818 "Attempt to access disallowed key '%"SVf"' from a fixed hash"
828 =for apidoc hv_delete_ent
830 Deletes a key/value pair in the hash. The value SV is removed from the
831 hash and returned to the caller. The C<flags> value will normally be zero;
832 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
833 precomputed hash value, or 0 to ask for it to be computed.
839 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
846 register HE **oentry;
853 if (SvRMAGICAL(hv)) {
856 hv_magic_check (hv, &needs_copy, &needs_store);
858 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
862 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
863 /* No longer an element */
864 sv_unmagic(sv, PERL_MAGIC_tiedelem);
867 return Nullsv; /* element cannot be deleted */
869 #ifdef ENV_IS_CASELESS
870 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
871 key = SvPV(keysv, klen);
872 keysv = sv_2mortal(newSVpvn(key,klen));
873 (void)strupr(SvPVX(keysv));
879 xhv = (XPVHV*)SvANY(hv);
880 if (!xhv->xhv_array /* !HvARRAY(hv) */)
883 keysave = key = SvPV(keysv, klen);
884 is_utf8 = (SvUTF8(keysv) != 0);
887 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
890 PERL_HASH(hash, key, klen);
892 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
893 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
896 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
897 if (HeHASH(entry) != hash) /* strings can't be equal */
899 if (HeKLEN(entry) != klen)
901 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
903 if (HeKUTF8(entry) != (char)is_utf8)
908 /* if placeholder is here, it's already been deleted.... */
909 if (HeVAL(entry) == &PL_sv_undef)
912 return Nullsv; /* if still SvREADONLY, leave it deleted. */
914 /* okay, really delete the placeholder. */
915 *oentry = HeNEXT(entry);
917 xhv->xhv_fill--; /* HvFILL(hv)-- */
918 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
921 hv_free_ent(hv, entry);
922 xhv->xhv_keys--; /* HvKEYS(hv)-- */
923 xhv->xhv_placeholders--;
926 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
927 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
928 "Attempt to delete readonly key '%"SVf"' from a fixed hash"
932 if (flags & G_DISCARD)
935 sv = sv_2mortal(HeVAL(entry));
936 HeVAL(entry) = &PL_sv_undef;
940 * If a restricted hash, rather than really deleting the entry, put
941 * a placeholder there. This marks the key as being "approved", so
942 * we can still access via not-really-existing key without raising
945 if (SvREADONLY(hv)) {
946 HeVAL(entry) = &PL_sv_undef;
947 /* We'll be saving this slot, so the number of allocated keys
948 * doesn't go down, but the number placeholders goes up */
949 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
951 *oentry = HeNEXT(entry);
953 xhv->xhv_fill--; /* HvFILL(hv)-- */
954 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
957 hv_free_ent(hv, entry);
958 xhv->xhv_keys--; /* HvKEYS(hv)-- */
962 if (SvREADONLY(hv)) {
963 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave,
964 "Attempt to delete disallowed key '%"SVf"' from a fixed hash"
974 =for apidoc hv_exists
976 Returns a boolean indicating whether the specified hash key exists. The
977 C<klen> is the length of the key.
983 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
989 bool is_utf8 = FALSE;
990 const char *keysave = key;
1000 if (SvRMAGICAL(hv)) {
1001 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1002 sv = sv_newmortal();
1003 mg_copy((SV*)hv, sv, key, klen);
1004 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
1007 #ifdef ENV_IS_CASELESS
1008 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1009 sv = sv_2mortal(newSVpvn(key,klen));
1010 key = strupr(SvPVX(sv));
1015 xhv = (XPVHV*)SvANY(hv);
1016 #ifndef DYNAMIC_ENV_FETCH
1017 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1022 STRLEN tmplen = klen;
1023 /* See the note in hv_fetch(). --jhi */
1024 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
1028 PERL_HASH(hash, key, klen);
1030 #ifdef DYNAMIC_ENV_FETCH
1031 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1034 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1035 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1036 for (; entry; entry = HeNEXT(entry)) {
1037 if (HeHASH(entry) != hash) /* strings can't be equal */
1039 if (HeKLEN(entry) != klen)
1041 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1043 if (HeKUTF8(entry) != (char)is_utf8)
1047 /* If we find the key, but the value is a placeholder, return false. */
1048 if (HeVAL(entry) == &PL_sv_undef)
1053 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1054 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1056 char *env = PerlEnv_ENVgetenv_len(key,&len);
1058 sv = newSVpvn(env,len);
1060 (void)hv_store(hv,key,klen,sv,hash);
1072 =for apidoc hv_exists_ent
1074 Returns a boolean indicating whether the specified hash key exists. C<hash>
1075 can be a valid precomputed hash value, or 0 to ask for it to be
1082 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
1084 register XPVHV* xhv;
1095 if (SvRMAGICAL(hv)) {
1096 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
1097 SV* svret = sv_newmortal();
1098 sv = sv_newmortal();
1099 keysv = sv_2mortal(newSVsv(keysv));
1100 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
1101 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
1102 return SvTRUE(svret);
1104 #ifdef ENV_IS_CASELESS
1105 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
1106 key = SvPV(keysv, klen);
1107 keysv = sv_2mortal(newSVpvn(key,klen));
1108 (void)strupr(SvPVX(keysv));
1114 xhv = (XPVHV*)SvANY(hv);
1115 #ifndef DYNAMIC_ENV_FETCH
1116 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1120 keysave = key = SvPV(keysv, klen);
1121 is_utf8 = (SvUTF8(keysv) != 0);
1123 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1125 PERL_HASH(hash, key, klen);
1127 #ifdef DYNAMIC_ENV_FETCH
1128 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1131 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1132 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1133 for (; entry; entry = HeNEXT(entry)) {
1134 if (HeHASH(entry) != hash) /* strings can't be equal */
1136 if (HeKLEN(entry) != klen)
1138 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1140 if (HeKUTF8(entry) != (char)is_utf8)
1144 /* If we find the key, but the value is a placeholder, return false. */
1145 if (HeVAL(entry) == &PL_sv_undef)
1149 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1150 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1152 char *env = PerlEnv_ENVgetenv_len(key,&len);
1154 sv = newSVpvn(env,len);
1156 (void)hv_store_ent(hv,keysv,sv,hash);
1167 S_hsplit(pTHX_ HV *hv)
1169 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1170 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1171 register I32 newsize = oldsize * 2;
1173 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1177 register HE **oentry;
1180 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1181 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1187 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1192 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1193 if (oldsize >= 64) {
1194 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1195 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1198 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1202 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1203 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1204 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1207 for (i=0; i<oldsize; i++,aep++) {
1208 if (!*aep) /* non-existent */
1211 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1212 if ((HeHASH(entry) & newsize) != i) {
1213 *oentry = HeNEXT(entry);
1214 HeNEXT(entry) = *bep;
1216 xhv->xhv_fill++; /* HvFILL(hv)++ */
1221 oentry = &HeNEXT(entry);
1223 if (!*aep) /* everything moved */
1224 xhv->xhv_fill--; /* HvFILL(hv)-- */
1229 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1231 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1232 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1233 register I32 newsize;
1239 register HE **oentry;
1241 newsize = (I32) newmax; /* possible truncation here */
1242 if (newsize != newmax || newmax <= oldsize)
1244 while ((newsize & (1 + ~newsize)) != newsize) {
1245 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1247 if (newsize < newmax)
1249 if (newsize < newmax)
1250 return; /* overflow detection */
1252 a = xhv->xhv_array; /* HvARRAY(hv) */
1255 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1256 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1262 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1267 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1268 if (oldsize >= 64) {
1269 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1270 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1273 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1276 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1279 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1281 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1282 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1283 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1287 for (i=0; i<oldsize; i++,aep++) {
1288 if (!*aep) /* non-existent */
1290 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1291 if ((j = (HeHASH(entry) & newsize)) != i) {
1293 *oentry = HeNEXT(entry);
1294 if (!(HeNEXT(entry) = aep[j]))
1295 xhv->xhv_fill++; /* HvFILL(hv)++ */
1300 oentry = &HeNEXT(entry);
1302 if (!*aep) /* everything moved */
1303 xhv->xhv_fill--; /* HvFILL(hv)-- */
1310 Creates a new HV. The reference count is set to 1.
1319 register XPVHV* xhv;
1321 hv = (HV*)NEWSV(502,0);
1322 sv_upgrade((SV *)hv, SVt_PVHV);
1323 xhv = (XPVHV*)SvANY(hv);
1326 #ifndef NODEFAULT_SHAREKEYS
1327 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1329 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1330 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1331 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1332 (void)hv_iterinit(hv); /* so each() will start off right */
1337 Perl_newHVhv(pTHX_ HV *ohv)
1340 STRLEN hv_max, hv_fill;
1342 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1344 hv_max = HvMAX(ohv);
1346 if (!SvMAGICAL((SV *)ohv)) {
1347 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1348 int i, shared = !!HvSHAREKEYS(ohv);
1349 HE **ents, **oents = (HE **)HvARRAY(ohv);
1351 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1354 /* In each bucket... */
1355 for (i = 0; i <= hv_max; i++) {
1356 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1363 /* Copy the linked list of entries. */
1364 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1365 U32 hash = HeHASH(oent);
1366 char *key = HeKEY(oent);
1367 STRLEN len = HeKLEN_UTF8(oent);
1370 HeVAL(ent) = newSVsv(HeVAL(oent));
1371 HeKEY_hek(ent) = shared ? share_hek(key, len, hash)
1372 : save_hek(key, len, hash);
1383 HvFILL(hv) = hv_fill;
1384 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1388 /* Iterate over ohv, copying keys and values one at a time. */
1390 I32 riter = HvRITER(ohv);
1391 HE *eiter = HvEITER(ohv);
1393 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1394 while (hv_max && hv_max + 1 >= hv_fill * 2)
1395 hv_max = hv_max / 2;
1399 while ((entry = hv_iternext(ohv))) {
1400 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1401 newSVsv(HeVAL(entry)), HeHASH(entry));
1403 HvRITER(ohv) = riter;
1404 HvEITER(ohv) = eiter;
1411 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1418 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1419 PL_sub_generation++; /* may be deletion of method from stash */
1421 if (HeKLEN(entry) == HEf_SVKEY) {
1422 SvREFCNT_dec(HeKEY_sv(entry));
1423 Safefree(HeKEY_hek(entry));
1425 else if (HvSHAREKEYS(hv))
1426 unshare_hek(HeKEY_hek(entry));
1428 Safefree(HeKEY_hek(entry));
1433 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1437 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1438 PL_sub_generation++; /* may be deletion of method from stash */
1439 sv_2mortal(HeVAL(entry)); /* free between statements */
1440 if (HeKLEN(entry) == HEf_SVKEY) {
1441 sv_2mortal(HeKEY_sv(entry));
1442 Safefree(HeKEY_hek(entry));
1444 else if (HvSHAREKEYS(hv))
1445 unshare_hek(HeKEY_hek(entry));
1447 Safefree(HeKEY_hek(entry));
1452 =for apidoc hv_clear
1454 Clears a hash, making it empty.
1460 Perl_hv_clear(pTHX_ HV *hv)
1462 register XPVHV* xhv;
1466 if(SvREADONLY(hv)) {
1467 Perl_croak(aTHX_ "Attempt to clear a fixed hash");
1470 xhv = (XPVHV*)SvANY(hv);
1472 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1473 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1474 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1475 if (xhv->xhv_array /* HvARRAY(hv) */)
1476 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1477 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1484 S_hfreeentries(pTHX_ HV *hv)
1486 register HE **array;
1488 register HE *oentry = Null(HE*);
1499 array = HvARRAY(hv);
1504 entry = HeNEXT(entry);
1505 hv_free_ent(hv, oentry);
1510 entry = array[riter];
1513 (void)hv_iterinit(hv);
1517 =for apidoc hv_undef
1525 Perl_hv_undef(pTHX_ HV *hv)
1527 register XPVHV* xhv;
1530 xhv = (XPVHV*)SvANY(hv);
1532 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1534 Safefree(HvNAME(hv));
1537 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1538 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1539 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1540 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1541 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1548 =for apidoc hv_iterinit
1550 Prepares a starting point to traverse a hash table. Returns the number of
1551 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1552 currently only meaningful for hashes without tie magic.
1554 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1555 hash buckets that happen to be in use. If you still need that esoteric
1556 value, you can get it through the macro C<HvFILL(tb)>.
1562 Perl_hv_iterinit(pTHX_ HV *hv)
1564 register XPVHV* xhv;
1568 Perl_croak(aTHX_ "Bad hash");
1569 xhv = (XPVHV*)SvANY(hv);
1570 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1571 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1573 hv_free_ent(hv, entry);
1575 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1576 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1577 /* used to be xhv->xhv_fill before 5.004_65 */
1578 return XHvTOTALKEYS(xhv);
1582 =for apidoc hv_iternext
1584 Returns entries from a hash iterator. See C<hv_iterinit>.
1590 Perl_hv_iternext(pTHX_ HV *hv)
1592 register XPVHV* xhv;
1598 Perl_croak(aTHX_ "Bad hash");
1599 xhv = (XPVHV*)SvANY(hv);
1600 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1602 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1603 SV *key = sv_newmortal();
1605 sv_setsv(key, HeSVKEY_force(entry));
1606 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1612 /* one HE per MAGICAL hash */
1613 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1615 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1617 HeKEY_hek(entry) = hek;
1618 HeKLEN(entry) = HEf_SVKEY;
1620 magic_nextpack((SV*) hv,mg,key);
1622 /* force key to stay around until next time */
1623 HeSVKEY_set(entry, SvREFCNT_inc(key));
1624 return entry; /* beware, hent_val is not set */
1627 SvREFCNT_dec(HeVAL(entry));
1628 Safefree(HeKEY_hek(entry));
1630 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1633 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1634 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1638 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1639 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1640 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1644 entry = HeNEXT(entry);
1646 * Skip past any placeholders -- don't want to include them in
1649 while (entry && HeVAL(entry) == &PL_sv_undef) {
1650 entry = HeNEXT(entry);
1654 xhv->xhv_riter++; /* HvRITER(hv)++ */
1655 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1656 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1659 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1660 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1662 /* if we have an entry, but it's a placeholder, don't count it */
1663 if (entry && HeVAL(entry) == &PL_sv_undef)
1668 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1670 hv_free_ent(hv, oldentry);
1673 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1678 =for apidoc hv_iterkey
1680 Returns the key from the current position of the hash iterator. See
1687 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1689 if (HeKLEN(entry) == HEf_SVKEY) {
1691 char *p = SvPV(HeKEY_sv(entry), len);
1696 *retlen = HeKLEN(entry);
1697 return HeKEY(entry);
1701 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1703 =for apidoc hv_iterkeysv
1705 Returns the key as an C<SV*> from the current position of the hash
1706 iterator. The return value will always be a mortal copy of the key. Also
1713 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1715 if (HeKLEN(entry) == HEf_SVKEY)
1716 return sv_mortalcopy(HeKEY_sv(entry));
1718 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1719 HeKLEN_UTF8(entry), HeHASH(entry)));
1723 =for apidoc hv_iterval
1725 Returns the value from the current position of the hash iterator. See
1732 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1734 if (SvRMAGICAL(hv)) {
1735 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1736 SV* sv = sv_newmortal();
1737 if (HeKLEN(entry) == HEf_SVKEY)
1738 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1739 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1743 return HeVAL(entry);
1747 =for apidoc hv_iternextsv
1749 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1756 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1759 if ( (he = hv_iternext(hv)) == NULL)
1761 *key = hv_iterkey(he, retlen);
1762 return hv_iterval(hv, he);
1766 =for apidoc hv_magic
1768 Adds magic to a hash. See C<sv_magic>.
1774 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1776 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1779 #if 0 /* use the macro from hv.h instead */
1782 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1784 return HEK_KEY(share_hek(sv, len, hash));
1789 /* possibly free a shared string if no one has access to it
1790 * len and hash must both be valid for str.
1793 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1795 register XPVHV* xhv;
1797 register HE **oentry;
1800 bool is_utf8 = FALSE;
1801 const char *save = str;
1804 STRLEN tmplen = -len;
1806 /* See the note in hv_fetch(). --jhi */
1807 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1811 /* what follows is the moral equivalent of:
1812 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1813 if (--*Svp == Nullsv)
1814 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1816 xhv = (XPVHV*)SvANY(PL_strtab);
1817 /* assert(xhv_array != 0) */
1819 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1820 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1821 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1822 if (HeHASH(entry) != hash) /* strings can't be equal */
1824 if (HeKLEN(entry) != len)
1826 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1828 if (HeKUTF8(entry) != (char)is_utf8)
1831 if (--HeVAL(entry) == Nullsv) {
1832 *oentry = HeNEXT(entry);
1834 xhv->xhv_fill--; /* HvFILL(hv)-- */
1835 Safefree(HeKEY_hek(entry));
1837 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1841 UNLOCK_STRTAB_MUTEX;
1844 if (!found && ckWARN_d(WARN_INTERNAL))
1845 Perl_warner(aTHX_ packWARN(WARN_INTERNAL), "Attempt to free non-existent shared string '%s'",str);
1848 /* get a (constant) string ptr from the global string table
1849 * string will get added if it is not already there.
1850 * len and hash must both be valid for str.
1853 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1855 register XPVHV* xhv;
1857 register HE **oentry;
1860 bool is_utf8 = FALSE;
1861 const char *save = str;
1864 STRLEN tmplen = -len;
1866 /* See the note in hv_fetch(). --jhi */
1867 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1871 /* what follows is the moral equivalent of:
1873 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1874 hv_store(PL_strtab, str, len, Nullsv, hash);
1876 xhv = (XPVHV*)SvANY(PL_strtab);
1877 /* assert(xhv_array != 0) */
1879 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1880 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1881 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1882 if (HeHASH(entry) != hash) /* strings can't be equal */
1884 if (HeKLEN(entry) != len)
1886 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1888 if (HeKUTF8(entry) != (char)is_utf8)
1895 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1896 HeVAL(entry) = Nullsv;
1897 HeNEXT(entry) = *oentry;
1899 xhv->xhv_keys++; /* HvKEYS(hv)++ */
1900 if (i) { /* initial entry? */
1901 xhv->xhv_fill++; /* HvFILL(hv)++ */
1902 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
1907 ++HeVAL(entry); /* use value slot as REFCNT */
1908 UNLOCK_STRTAB_MUTEX;
1911 return HeKEY_hek(entry);