3 * Copyright (c) 1991-1999, 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
18 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
19 # define ARRAY_ALLOC_BYTES(size) ( (size)*sizeof(HE*) )
21 # define MALLOC_OVERHEAD 16
22 # define ARRAY_ALLOC_BYTES(size) ( ((size) < 64) \
23 ? (size)*sizeof(HE*) \
24 : (size)*sizeof(HE*)*2 - MALLOC_OVERHEAD )
35 PL_he_root = HeNEXT(he);
44 HeNEXT(p) = (HE*)PL_he_root;
54 New(54, PL_he_root, 1008/sizeof(HE), HE);
56 heend = &he[1008 / sizeof(HE) - 1];
58 HeNEXT(he) = (HE*)(he + 1);
65 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
70 New(54, k, HEK_BASESIZE + len + 1, char);
72 Copy(str, HEK_KEY(hek), len, char);
73 *(HEK_KEY(hek) + len) = '\0';
80 Perl_unshare_hek(pTHX_ HEK *hek)
82 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
85 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
89 Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
100 if (mg_find((SV*)hv,'P')) {
103 mg_copy((SV*)hv, sv, key, klen);
105 return &PL_hv_fetch_sv;
107 #ifdef ENV_IS_CASELESS
108 else if (mg_find((SV*)hv,'E')) {
110 for (i = 0; i < klen; ++i)
111 if (isLOWER(key[i])) {
112 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
113 SV **ret = hv_fetch(hv, nkey, klen, 0);
115 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
122 xhv = (XPVHV*)SvANY(hv);
123 if (!xhv->xhv_array) {
125 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
126 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
129 Newz(503,xhv->xhv_array, ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
134 PERL_HASH(hash, key, klen);
136 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
137 for (; entry; entry = HeNEXT(entry)) {
138 if (HeHASH(entry) != hash) /* strings can't be equal */
140 if (HeKLEN(entry) != klen)
142 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
144 return &HeVAL(entry);
146 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
147 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
149 char *env = PerlEnv_ENVgetenv_len(key,&len);
151 sv = newSVpvn(env,len);
153 return hv_store(hv,key,klen,sv,hash);
157 if (lval) { /* gonna assign to this, so it better be there */
159 return hv_store(hv,key,klen,sv,hash);
164 /* returns a HE * structure with the all fields set */
165 /* note that hent_val will be a mortal sv for MAGICAL hashes */
167 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
178 if (SvRMAGICAL(hv)) {
179 if (mg_find((SV*)hv,'P')) {
182 keysv = sv_2mortal(newSVsv(keysv));
183 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
184 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
186 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
187 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
189 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
190 HeVAL(&PL_hv_fetch_ent_mh) = sv;
191 return &PL_hv_fetch_ent_mh;
193 #ifdef ENV_IS_CASELESS
194 else if (mg_find((SV*)hv,'E')) {
196 key = SvPV(keysv, klen);
197 for (i = 0; i < klen; ++i)
198 if (isLOWER(key[i])) {
199 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
200 (void)strupr(SvPVX(nkeysv));
201 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
203 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
210 xhv = (XPVHV*)SvANY(hv);
211 if (!xhv->xhv_array) {
213 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
214 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
217 Newz(503,xhv->xhv_array, ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
222 key = SvPV(keysv, klen);
225 PERL_HASH(hash, key, klen);
227 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
228 for (; entry; entry = HeNEXT(entry)) {
229 if (HeHASH(entry) != hash) /* strings can't be equal */
231 if (HeKLEN(entry) != klen)
233 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
237 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
238 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
240 char *env = PerlEnv_ENVgetenv_len(key,&len);
242 sv = newSVpvn(env,len);
244 return hv_store_ent(hv,keysv,sv,hash);
248 if (lval) { /* gonna assign to this, so it better be there */
250 return hv_store_ent(hv,keysv,sv,hash);
256 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
258 MAGIC *mg = SvMAGIC(hv);
262 if (isUPPER(mg->mg_type)) {
264 switch (mg->mg_type) {
267 *needs_store = FALSE;
270 mg = mg->mg_moremagic;
275 Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
280 register HE **oentry;
285 xhv = (XPVHV*)SvANY(hv);
289 hv_magic_check (hv, &needs_copy, &needs_store);
291 mg_copy((SV*)hv, val, key, klen);
292 if (!xhv->xhv_array && !needs_store)
294 #ifdef ENV_IS_CASELESS
295 else if (mg_find((SV*)hv,'E')) {
296 SV *sv = sv_2mortal(newSVpvn(key,klen));
297 key = strupr(SvPVX(sv));
304 PERL_HASH(hash, key, klen);
307 Newz(505, xhv->xhv_array, ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
309 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
312 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
313 if (HeHASH(entry) != hash) /* strings can't be equal */
315 if (HeKLEN(entry) != klen)
317 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
319 SvREFCNT_dec(HeVAL(entry));
321 return &HeVAL(entry);
326 HeKEY_hek(entry) = share_hek(key, klen, hash);
327 else /* gotta do the real thing */
328 HeKEY_hek(entry) = save_hek(key, klen, hash);
330 HeNEXT(entry) = *oentry;
334 if (i) { /* initial entry? */
336 if (xhv->xhv_keys > xhv->xhv_max)
340 return &HeVAL(entry);
344 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
351 register HE **oentry;
356 xhv = (XPVHV*)SvANY(hv);
361 hv_magic_check (hv, &needs_copy, &needs_store);
363 bool save_taint = PL_tainted;
365 PL_tainted = SvTAINTED(keysv);
366 keysv = sv_2mortal(newSVsv(keysv));
367 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
368 TAINT_IF(save_taint);
369 if (!xhv->xhv_array && !needs_store)
371 #ifdef ENV_IS_CASELESS
372 else if (mg_find((SV*)hv,'E')) {
373 key = SvPV(keysv, klen);
374 keysv = sv_2mortal(newSVpvn(key,klen));
375 (void)strupr(SvPVX(keysv));
382 key = SvPV(keysv, klen);
385 PERL_HASH(hash, key, klen);
388 Newz(505, xhv->xhv_array, ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
390 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
393 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
394 if (HeHASH(entry) != hash) /* strings can't be equal */
396 if (HeKLEN(entry) != klen)
398 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
400 SvREFCNT_dec(HeVAL(entry));
407 HeKEY_hek(entry) = share_hek(key, klen, hash);
408 else /* gotta do the real thing */
409 HeKEY_hek(entry) = save_hek(key, klen, hash);
411 HeNEXT(entry) = *oentry;
415 if (i) { /* initial entry? */
417 if (xhv->xhv_keys > xhv->xhv_max)
425 Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
431 register HE **oentry;
437 if (SvRMAGICAL(hv)) {
440 hv_magic_check (hv, &needs_copy, &needs_store);
442 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
446 if (mg_find(sv, 'p')) {
447 sv_unmagic(sv, 'p'); /* No longer an element */
450 return Nullsv; /* element cannot be deleted */
452 #ifdef ENV_IS_CASELESS
453 else if (mg_find((SV*)hv,'E')) {
454 sv = sv_2mortal(newSVpvn(key,klen));
455 key = strupr(SvPVX(sv));
460 xhv = (XPVHV*)SvANY(hv);
464 PERL_HASH(hash, key, klen);
466 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
469 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
470 if (HeHASH(entry) != hash) /* strings can't be equal */
472 if (HeKLEN(entry) != klen)
474 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
476 *oentry = HeNEXT(entry);
479 if (flags & G_DISCARD)
482 sv = sv_mortalcopy(HeVAL(entry));
483 if (entry == xhv->xhv_eiter)
486 hv_free_ent(hv, entry);
494 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
501 register HE **oentry;
506 if (SvRMAGICAL(hv)) {
509 hv_magic_check (hv, &needs_copy, &needs_store);
511 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
515 if (mg_find(sv, 'p')) {
516 sv_unmagic(sv, 'p'); /* No longer an element */
519 return Nullsv; /* element cannot be deleted */
521 #ifdef ENV_IS_CASELESS
522 else if (mg_find((SV*)hv,'E')) {
523 key = SvPV(keysv, klen);
524 keysv = sv_2mortal(newSVpvn(key,klen));
525 (void)strupr(SvPVX(keysv));
531 xhv = (XPVHV*)SvANY(hv);
535 key = SvPV(keysv, klen);
538 PERL_HASH(hash, key, klen);
540 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
543 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
544 if (HeHASH(entry) != hash) /* strings can't be equal */
546 if (HeKLEN(entry) != klen)
548 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
550 *oentry = HeNEXT(entry);
553 if (flags & G_DISCARD)
556 sv = sv_mortalcopy(HeVAL(entry));
557 if (entry == xhv->xhv_eiter)
560 hv_free_ent(hv, entry);
568 Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
578 if (SvRMAGICAL(hv)) {
579 if (mg_find((SV*)hv,'P')) {
582 mg_copy((SV*)hv, sv, key, klen);
583 magic_existspack(sv, mg_find(sv, 'p'));
586 #ifdef ENV_IS_CASELESS
587 else if (mg_find((SV*)hv,'E')) {
588 sv = sv_2mortal(newSVpvn(key,klen));
589 key = strupr(SvPVX(sv));
594 xhv = (XPVHV*)SvANY(hv);
595 #ifndef DYNAMIC_ENV_FETCH
600 PERL_HASH(hash, key, klen);
602 #ifdef DYNAMIC_ENV_FETCH
603 if (!xhv->xhv_array) entry = Null(HE*);
606 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
607 for (; entry; entry = HeNEXT(entry)) {
608 if (HeHASH(entry) != hash) /* strings can't be equal */
610 if (HeKLEN(entry) != klen)
612 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
616 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
617 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
619 char *env = PerlEnv_ENVgetenv_len(key,&len);
621 sv = newSVpvn(env,len);
623 (void)hv_store(hv,key,klen,sv,hash);
633 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
644 if (SvRMAGICAL(hv)) {
645 if (mg_find((SV*)hv,'P')) {
646 dTHR; /* just for SvTRUE */
648 keysv = sv_2mortal(newSVsv(keysv));
649 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
650 magic_existspack(sv, mg_find(sv, 'p'));
653 #ifdef ENV_IS_CASELESS
654 else if (mg_find((SV*)hv,'E')) {
655 key = SvPV(keysv, klen);
656 keysv = sv_2mortal(newSVpvn(key,klen));
657 (void)strupr(SvPVX(keysv));
663 xhv = (XPVHV*)SvANY(hv);
664 #ifndef DYNAMIC_ENV_FETCH
669 key = SvPV(keysv, klen);
671 PERL_HASH(hash, key, klen);
673 #ifdef DYNAMIC_ENV_FETCH
674 if (!xhv->xhv_array) entry = Null(HE*);
677 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
678 for (; entry; entry = HeNEXT(entry)) {
679 if (HeHASH(entry) != hash) /* strings can't be equal */
681 if (HeKLEN(entry) != klen)
683 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
687 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
688 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
690 char *env = PerlEnv_ENVgetenv_len(key,&len);
692 sv = newSVpvn(env,len);
694 (void)hv_store_ent(hv,keysv,sv,hash);
703 S_hsplit(pTHX_ HV *hv)
705 register XPVHV* xhv = (XPVHV*)SvANY(hv);
706 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
707 register I32 newsize = oldsize * 2;
709 register char *a = xhv->xhv_array;
713 register HE **oentry;
716 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
717 Renew(a, ARRAY_ALLOC_BYTES(newsize), char);
723 #define MALLOC_OVERHEAD 16
724 New(2, a, ARRAY_ALLOC_BYTES(newsize), char);
729 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
731 offer_nice_chunk(xhv->xhv_array, ARRAY_ALLOC_BYTES(oldsize));
734 Safefree(xhv->xhv_array);
738 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
739 xhv->xhv_max = --newsize;
743 for (i=0; i<oldsize; i++,aep++) {
744 if (!*aep) /* non-existent */
747 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
748 if ((HeHASH(entry) & newsize) != i) {
749 *oentry = HeNEXT(entry);
750 HeNEXT(entry) = *bep;
757 oentry = &HeNEXT(entry);
759 if (!*aep) /* everything moved */
765 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
767 register XPVHV* xhv = (XPVHV*)SvANY(hv);
768 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
769 register I32 newsize;
775 register HE **oentry;
777 newsize = (I32) newmax; /* possible truncation here */
778 if (newsize != newmax || newmax <= oldsize)
780 while ((newsize & (1 + ~newsize)) != newsize) {
781 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
783 if (newsize < newmax)
785 if (newsize < newmax)
786 return; /* overflow detection */
791 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
792 Renew(a, ARRAY_ALLOC_BYTES(newsize), char);
798 New(2, a, ARRAY_ALLOC_BYTES(newsize), char);
803 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
805 offer_nice_chunk(xhv->xhv_array, ARRAY_ALLOC_BYTES(oldsize));
808 Safefree(xhv->xhv_array);
811 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
814 Newz(0, a, ARRAY_ALLOC_BYTES(newsize), char);
816 xhv->xhv_max = --newsize;
818 if (!xhv->xhv_fill) /* skip rest if no entries */
822 for (i=0; i<oldsize; i++,aep++) {
823 if (!*aep) /* non-existent */
825 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
826 if ((j = (HeHASH(entry) & newsize)) != i) {
828 *oentry = HeNEXT(entry);
829 if (!(HeNEXT(entry) = aep[j]))
835 oentry = &HeNEXT(entry);
837 if (!*aep) /* everything moved */
848 hv = (HV*)NEWSV(502,0);
849 sv_upgrade((SV *)hv, SVt_PVHV);
850 xhv = (XPVHV*)SvANY(hv);
853 #ifndef NODEFAULT_SHAREKEYS
854 HvSHAREKEYS_on(hv); /* key-sharing on by default */
856 xhv->xhv_max = 7; /* start with 8 buckets */
859 (void)hv_iterinit(hv); /* so each() will start off right */
864 Perl_newHVhv(pTHX_ HV *ohv)
867 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
868 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
871 while (hv_max && hv_max + 1 >= hv_fill * 2)
872 hv_max = hv_max / 2; /* Is always 2^n-1 */
878 if (! SvTIED_mg((SV*)ohv, 'P')) {
885 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
886 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
890 while (entry = hv_iternext(ohv)) {
891 hv_store(hv, HeKEY(entry), HeKLEN(entry),
892 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
894 HvRITER(ohv) = hv_riter;
895 HvEITER(ohv) = hv_eiter;
902 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
909 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
910 PL_sub_generation++; /* may be deletion of method from stash */
912 if (HeKLEN(entry) == HEf_SVKEY) {
913 SvREFCNT_dec(HeKEY_sv(entry));
914 Safefree(HeKEY_hek(entry));
916 else if (HvSHAREKEYS(hv))
917 unshare_hek(HeKEY_hek(entry));
919 Safefree(HeKEY_hek(entry));
924 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
928 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
929 PL_sub_generation++; /* may be deletion of method from stash */
930 sv_2mortal(HeVAL(entry)); /* free between statements */
931 if (HeKLEN(entry) == HEf_SVKEY) {
932 sv_2mortal(HeKEY_sv(entry));
933 Safefree(HeKEY_hek(entry));
935 else if (HvSHAREKEYS(hv))
936 unshare_hek(HeKEY_hek(entry));
938 Safefree(HeKEY_hek(entry));
943 Perl_hv_clear(pTHX_ HV *hv)
948 xhv = (XPVHV*)SvANY(hv);
953 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
960 S_hfreeentries(pTHX_ HV *hv)
964 register HE *oentry = Null(HE*);
980 entry = HeNEXT(entry);
981 hv_free_ent(hv, oentry);
986 entry = array[riter];
989 (void)hv_iterinit(hv);
993 Perl_hv_undef(pTHX_ HV *hv)
998 xhv = (XPVHV*)SvANY(hv);
1000 Safefree(xhv->xhv_array);
1002 Safefree(HvNAME(hv));
1006 xhv->xhv_max = 7; /* it's a normal hash */
1015 Perl_hv_iterinit(pTHX_ HV *hv)
1017 register XPVHV* xhv;
1021 Perl_croak(aTHX_ "Bad hash");
1022 xhv = (XPVHV*)SvANY(hv);
1023 entry = xhv->xhv_eiter;
1024 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1026 hv_free_ent(hv, entry);
1028 xhv->xhv_riter = -1;
1029 xhv->xhv_eiter = Null(HE*);
1030 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1034 Perl_hv_iternext(pTHX_ HV *hv)
1036 register XPVHV* xhv;
1042 Perl_croak(aTHX_ "Bad hash");
1043 xhv = (XPVHV*)SvANY(hv);
1044 oldentry = entry = xhv->xhv_eiter;
1046 if (mg = SvTIED_mg((SV*)hv, 'P')) {
1047 SV *key = sv_newmortal();
1049 sv_setsv(key, HeSVKEY_force(entry));
1050 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1056 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
1058 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1060 HeKEY_hek(entry) = hek;
1061 HeKLEN(entry) = HEf_SVKEY;
1063 magic_nextpack((SV*) hv,mg,key);
1065 /* force key to stay around until next time */
1066 HeSVKEY_set(entry, SvREFCNT_inc(key));
1067 return entry; /* beware, hent_val is not set */
1070 SvREFCNT_dec(HeVAL(entry));
1071 Safefree(HeKEY_hek(entry));
1073 xhv->xhv_eiter = Null(HE*);
1076 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1077 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1081 if (!xhv->xhv_array)
1082 Newz(506,xhv->xhv_array, ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1084 entry = HeNEXT(entry);
1087 if (xhv->xhv_riter > xhv->xhv_max) {
1088 xhv->xhv_riter = -1;
1091 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1094 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1096 hv_free_ent(hv, oldentry);
1099 xhv->xhv_eiter = entry;
1104 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1106 if (HeKLEN(entry) == HEf_SVKEY) {
1108 char *p = SvPV(HeKEY_sv(entry), len);
1113 *retlen = HeKLEN(entry);
1114 return HeKEY(entry);
1118 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1120 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1122 if (HeKLEN(entry) == HEf_SVKEY)
1123 return sv_mortalcopy(HeKEY_sv(entry));
1125 return sv_2mortal(newSVpvn((HeKLEN(entry) ? HeKEY(entry) : ""),
1130 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1132 if (SvRMAGICAL(hv)) {
1133 if (mg_find((SV*)hv,'P')) {
1134 SV* sv = sv_newmortal();
1135 if (HeKLEN(entry) == HEf_SVKEY)
1136 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1137 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1141 return HeVAL(entry);
1145 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1148 if ( (he = hv_iternext(hv)) == NULL)
1150 *key = hv_iterkey(he, retlen);
1151 return hv_iterval(hv, he);
1155 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1157 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1161 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1163 return HEK_KEY(share_hek(sv, len, hash));
1166 /* possibly free a shared string if no one has access to it
1167 * len and hash must both be valid for str.
1170 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1172 register XPVHV* xhv;
1174 register HE **oentry;
1178 /* what follows is the moral equivalent of:
1179 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1180 if (--*Svp == Nullsv)
1181 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1183 xhv = (XPVHV*)SvANY(PL_strtab);
1184 /* assert(xhv_array != 0) */
1186 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1187 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1188 if (HeHASH(entry) != hash) /* strings can't be equal */
1190 if (HeKLEN(entry) != len)
1192 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1195 if (--HeVAL(entry) == Nullsv) {
1196 *oentry = HeNEXT(entry);
1199 Safefree(HeKEY_hek(entry));
1205 UNLOCK_STRTAB_MUTEX;
1209 if (!found && ckWARN_d(WARN_INTERNAL))
1210 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string");
1214 /* get a (constant) string ptr from the global string table
1215 * string will get added if it is not already there.
1216 * len and hash must both be valid for str.
1219 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1221 register XPVHV* xhv;
1223 register HE **oentry;
1227 /* what follows is the moral equivalent of:
1229 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1230 hv_store(PL_strtab, str, len, Nullsv, hash);
1232 xhv = (XPVHV*)SvANY(PL_strtab);
1233 /* assert(xhv_array != 0) */
1235 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1236 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1237 if (HeHASH(entry) != hash) /* strings can't be equal */
1239 if (HeKLEN(entry) != len)
1241 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1248 HeKEY_hek(entry) = save_hek(str, len, hash);
1249 HeVAL(entry) = Nullsv;
1250 HeNEXT(entry) = *oentry;
1253 if (i) { /* initial entry? */
1255 if (xhv->xhv_keys > xhv->xhv_max)
1260 ++HeVAL(entry); /* use value slot as REFCNT */
1261 UNLOCK_STRTAB_MUTEX;
1262 return HeKEY_hek(entry);