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
26 PL_he_root = HeNEXT(he);
35 HeNEXT(p) = (HE*)PL_he_root;
45 New(54, PL_he_root, 1008/sizeof(HE), HE);
47 heend = &he[1008 / sizeof(HE) - 1];
49 HeNEXT(he) = (HE*)(he + 1);
56 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
61 New(54, k, HEK_BASESIZE + len + 1, char);
63 Copy(str, HEK_KEY(hek), len, char);
64 *(HEK_KEY(hek) + len) = '\0';
71 Perl_unshare_hek(pTHX_ HEK *hek)
73 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
76 #if defined(USE_ITHREADS)
78 Perl_he_dup(pTHX_ HE *e, bool shared)
84 /* look for it in the table first */
85 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
89 /* create anew and remember what it is */
91 ptr_table_store(PL_ptr_table, e, ret);
93 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
94 if (HeKLEN(e) == HEf_SVKEY)
95 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
97 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
99 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
100 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
103 #endif /* USE_ITHREADS */
105 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
109 Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
119 if (SvRMAGICAL(hv)) {
120 if (mg_find((SV*)hv,'P')) {
123 mg_copy((SV*)hv, sv, key, klen);
125 return &PL_hv_fetch_sv;
127 #ifdef ENV_IS_CASELESS
128 else if (mg_find((SV*)hv,'E')) {
130 for (i = 0; i < klen; ++i)
131 if (isLOWER(key[i])) {
132 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
133 SV **ret = hv_fetch(hv, nkey, klen, 0);
135 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
142 xhv = (XPVHV*)SvANY(hv);
143 if (!xhv->xhv_array) {
145 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
146 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
149 Newz(503, xhv->xhv_array,
150 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
155 PERL_HASH(hash, key, klen);
157 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
158 for (; entry; entry = HeNEXT(entry)) {
159 if (HeHASH(entry) != hash) /* strings can't be equal */
161 if (HeKLEN(entry) != klen)
163 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
165 return &HeVAL(entry);
167 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
168 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
170 char *env = PerlEnv_ENVgetenv_len(key,&len);
172 sv = newSVpvn(env,len);
174 return hv_store(hv,key,klen,sv,hash);
178 if (lval) { /* gonna assign to this, so it better be there */
180 return hv_store(hv,key,klen,sv,hash);
185 /* returns a HE * structure with the all fields set */
186 /* note that hent_val will be a mortal sv for MAGICAL hashes */
188 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
199 if (SvRMAGICAL(hv)) {
200 if (mg_find((SV*)hv,'P')) {
203 keysv = sv_2mortal(newSVsv(keysv));
204 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
205 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
207 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
208 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
210 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
211 HeVAL(&PL_hv_fetch_ent_mh) = sv;
212 return &PL_hv_fetch_ent_mh;
214 #ifdef ENV_IS_CASELESS
215 else if (mg_find((SV*)hv,'E')) {
217 key = SvPV(keysv, klen);
218 for (i = 0; i < klen; ++i)
219 if (isLOWER(key[i])) {
220 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
221 (void)strupr(SvPVX(nkeysv));
222 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
224 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
231 xhv = (XPVHV*)SvANY(hv);
232 if (!xhv->xhv_array) {
234 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
235 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
238 Newz(503, xhv->xhv_array,
239 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
244 key = SvPV(keysv, klen);
247 PERL_HASH(hash, key, klen);
249 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
250 for (; entry; entry = HeNEXT(entry)) {
251 if (HeHASH(entry) != hash) /* strings can't be equal */
253 if (HeKLEN(entry) != klen)
255 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
259 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
260 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
262 char *env = PerlEnv_ENVgetenv_len(key,&len);
264 sv = newSVpvn(env,len);
266 return hv_store_ent(hv,keysv,sv,hash);
270 if (lval) { /* gonna assign to this, so it better be there */
272 return hv_store_ent(hv,keysv,sv,hash);
278 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
280 MAGIC *mg = SvMAGIC(hv);
284 if (isUPPER(mg->mg_type)) {
286 switch (mg->mg_type) {
289 *needs_store = FALSE;
292 mg = mg->mg_moremagic;
297 Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
302 register HE **oentry;
307 xhv = (XPVHV*)SvANY(hv);
311 hv_magic_check (hv, &needs_copy, &needs_store);
313 mg_copy((SV*)hv, val, key, klen);
314 if (!xhv->xhv_array && !needs_store)
316 #ifdef ENV_IS_CASELESS
317 else if (mg_find((SV*)hv,'E')) {
318 SV *sv = sv_2mortal(newSVpvn(key,klen));
319 key = strupr(SvPVX(sv));
326 PERL_HASH(hash, key, klen);
329 Newz(505, xhv->xhv_array,
330 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
332 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
335 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
336 if (HeHASH(entry) != hash) /* strings can't be equal */
338 if (HeKLEN(entry) != klen)
340 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
342 SvREFCNT_dec(HeVAL(entry));
344 return &HeVAL(entry);
349 HeKEY_hek(entry) = share_hek(key, klen, hash);
350 else /* gotta do the real thing */
351 HeKEY_hek(entry) = save_hek(key, klen, hash);
353 HeNEXT(entry) = *oentry;
357 if (i) { /* initial entry? */
359 if (xhv->xhv_keys > xhv->xhv_max)
363 return &HeVAL(entry);
367 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
374 register HE **oentry;
379 xhv = (XPVHV*)SvANY(hv);
384 hv_magic_check (hv, &needs_copy, &needs_store);
386 bool save_taint = PL_tainted;
388 PL_tainted = SvTAINTED(keysv);
389 keysv = sv_2mortal(newSVsv(keysv));
390 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
391 TAINT_IF(save_taint);
392 if (!xhv->xhv_array && !needs_store)
394 #ifdef ENV_IS_CASELESS
395 else if (mg_find((SV*)hv,'E')) {
396 key = SvPV(keysv, klen);
397 keysv = sv_2mortal(newSVpvn(key,klen));
398 (void)strupr(SvPVX(keysv));
405 key = SvPV(keysv, klen);
408 PERL_HASH(hash, key, klen);
411 Newz(505, xhv->xhv_array,
412 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
414 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
417 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
418 if (HeHASH(entry) != hash) /* strings can't be equal */
420 if (HeKLEN(entry) != klen)
422 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
424 SvREFCNT_dec(HeVAL(entry));
431 HeKEY_hek(entry) = share_hek(key, klen, hash);
432 else /* gotta do the real thing */
433 HeKEY_hek(entry) = save_hek(key, klen, hash);
435 HeNEXT(entry) = *oentry;
439 if (i) { /* initial entry? */
441 if (xhv->xhv_keys > xhv->xhv_max)
449 Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
455 register HE **oentry;
461 if (SvRMAGICAL(hv)) {
464 hv_magic_check (hv, &needs_copy, &needs_store);
466 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
470 if (mg_find(sv, 'p')) {
471 sv_unmagic(sv, 'p'); /* No longer an element */
474 return Nullsv; /* element cannot be deleted */
476 #ifdef ENV_IS_CASELESS
477 else if (mg_find((SV*)hv,'E')) {
478 sv = sv_2mortal(newSVpvn(key,klen));
479 key = strupr(SvPVX(sv));
484 xhv = (XPVHV*)SvANY(hv);
488 PERL_HASH(hash, key, klen);
490 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
493 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
494 if (HeHASH(entry) != hash) /* strings can't be equal */
496 if (HeKLEN(entry) != klen)
498 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
500 *oentry = HeNEXT(entry);
503 if (flags & G_DISCARD)
506 sv = sv_2mortal(HeVAL(entry));
507 HeVAL(entry) = &PL_sv_undef;
509 if (entry == xhv->xhv_eiter)
512 hv_free_ent(hv, entry);
520 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
527 register HE **oentry;
532 if (SvRMAGICAL(hv)) {
535 hv_magic_check (hv, &needs_copy, &needs_store);
537 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
541 if (mg_find(sv, 'p')) {
542 sv_unmagic(sv, 'p'); /* No longer an element */
545 return Nullsv; /* element cannot be deleted */
547 #ifdef ENV_IS_CASELESS
548 else if (mg_find((SV*)hv,'E')) {
549 key = SvPV(keysv, klen);
550 keysv = sv_2mortal(newSVpvn(key,klen));
551 (void)strupr(SvPVX(keysv));
557 xhv = (XPVHV*)SvANY(hv);
561 key = SvPV(keysv, klen);
564 PERL_HASH(hash, key, klen);
566 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
569 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
570 if (HeHASH(entry) != hash) /* strings can't be equal */
572 if (HeKLEN(entry) != klen)
574 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
576 *oentry = HeNEXT(entry);
579 if (flags & G_DISCARD)
582 sv = sv_2mortal(HeVAL(entry));
583 HeVAL(entry) = &PL_sv_undef;
585 if (entry == xhv->xhv_eiter)
588 hv_free_ent(hv, entry);
596 Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
606 if (SvRMAGICAL(hv)) {
607 if (mg_find((SV*)hv,'P')) {
610 mg_copy((SV*)hv, sv, key, klen);
611 magic_existspack(sv, mg_find(sv, 'p'));
614 #ifdef ENV_IS_CASELESS
615 else if (mg_find((SV*)hv,'E')) {
616 sv = sv_2mortal(newSVpvn(key,klen));
617 key = strupr(SvPVX(sv));
622 xhv = (XPVHV*)SvANY(hv);
623 #ifndef DYNAMIC_ENV_FETCH
628 PERL_HASH(hash, key, klen);
630 #ifdef DYNAMIC_ENV_FETCH
631 if (!xhv->xhv_array) entry = Null(HE*);
634 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
635 for (; entry; entry = HeNEXT(entry)) {
636 if (HeHASH(entry) != hash) /* strings can't be equal */
638 if (HeKLEN(entry) != klen)
640 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
644 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
645 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
647 char *env = PerlEnv_ENVgetenv_len(key,&len);
649 sv = newSVpvn(env,len);
651 (void)hv_store(hv,key,klen,sv,hash);
661 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
672 if (SvRMAGICAL(hv)) {
673 if (mg_find((SV*)hv,'P')) {
674 dTHR; /* just for SvTRUE */
676 keysv = sv_2mortal(newSVsv(keysv));
677 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
678 magic_existspack(sv, mg_find(sv, 'p'));
681 #ifdef ENV_IS_CASELESS
682 else if (mg_find((SV*)hv,'E')) {
683 key = SvPV(keysv, klen);
684 keysv = sv_2mortal(newSVpvn(key,klen));
685 (void)strupr(SvPVX(keysv));
691 xhv = (XPVHV*)SvANY(hv);
692 #ifndef DYNAMIC_ENV_FETCH
697 key = SvPV(keysv, klen);
699 PERL_HASH(hash, key, klen);
701 #ifdef DYNAMIC_ENV_FETCH
702 if (!xhv->xhv_array) entry = Null(HE*);
705 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
706 for (; entry; entry = HeNEXT(entry)) {
707 if (HeHASH(entry) != hash) /* strings can't be equal */
709 if (HeKLEN(entry) != klen)
711 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
715 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
716 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
718 char *env = PerlEnv_ENVgetenv_len(key,&len);
720 sv = newSVpvn(env,len);
722 (void)hv_store_ent(hv,keysv,sv,hash);
731 S_hsplit(pTHX_ HV *hv)
733 register XPVHV* xhv = (XPVHV*)SvANY(hv);
734 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
735 register I32 newsize = oldsize * 2;
737 register char *a = xhv->xhv_array;
741 register HE **oentry;
744 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
745 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
751 #define MALLOC_OVERHEAD 16
752 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
757 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
759 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
762 Safefree(xhv->xhv_array);
766 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
767 xhv->xhv_max = --newsize;
771 for (i=0; i<oldsize; i++,aep++) {
772 if (!*aep) /* non-existent */
775 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
776 if ((HeHASH(entry) & newsize) != i) {
777 *oentry = HeNEXT(entry);
778 HeNEXT(entry) = *bep;
785 oentry = &HeNEXT(entry);
787 if (!*aep) /* everything moved */
793 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
795 register XPVHV* xhv = (XPVHV*)SvANY(hv);
796 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
797 register I32 newsize;
803 register HE **oentry;
805 newsize = (I32) newmax; /* possible truncation here */
806 if (newsize != newmax || newmax <= oldsize)
808 while ((newsize & (1 + ~newsize)) != newsize) {
809 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
811 if (newsize < newmax)
813 if (newsize < newmax)
814 return; /* overflow detection */
819 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
820 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
826 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
831 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
833 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
836 Safefree(xhv->xhv_array);
839 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
842 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
844 xhv->xhv_max = --newsize;
846 if (!xhv->xhv_fill) /* skip rest if no entries */
850 for (i=0; i<oldsize; i++,aep++) {
851 if (!*aep) /* non-existent */
853 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
854 if ((j = (HeHASH(entry) & newsize)) != i) {
856 *oentry = HeNEXT(entry);
857 if (!(HeNEXT(entry) = aep[j]))
863 oentry = &HeNEXT(entry);
865 if (!*aep) /* everything moved */
876 hv = (HV*)NEWSV(502,0);
877 sv_upgrade((SV *)hv, SVt_PVHV);
878 xhv = (XPVHV*)SvANY(hv);
881 #ifndef NODEFAULT_SHAREKEYS
882 HvSHAREKEYS_on(hv); /* key-sharing on by default */
884 xhv->xhv_max = 7; /* start with 8 buckets */
887 (void)hv_iterinit(hv); /* so each() will start off right */
892 Perl_newHVhv(pTHX_ HV *ohv)
895 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
896 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
899 while (hv_max && hv_max + 1 >= hv_fill * 2)
900 hv_max = hv_max / 2; /* Is always 2^n-1 */
906 if (! SvTIED_mg((SV*)ohv, 'P')) {
913 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
914 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
918 while (entry = hv_iternext(ohv)) {
919 hv_store(hv, HeKEY(entry), HeKLEN(entry),
920 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
922 HvRITER(ohv) = hv_riter;
923 HvEITER(ohv) = hv_eiter;
930 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
937 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
938 PL_sub_generation++; /* may be deletion of method from stash */
940 if (HeKLEN(entry) == HEf_SVKEY) {
941 SvREFCNT_dec(HeKEY_sv(entry));
942 Safefree(HeKEY_hek(entry));
944 else if (HvSHAREKEYS(hv))
945 unshare_hek(HeKEY_hek(entry));
947 Safefree(HeKEY_hek(entry));
952 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
956 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
957 PL_sub_generation++; /* may be deletion of method from stash */
958 sv_2mortal(HeVAL(entry)); /* free between statements */
959 if (HeKLEN(entry) == HEf_SVKEY) {
960 sv_2mortal(HeKEY_sv(entry));
961 Safefree(HeKEY_hek(entry));
963 else if (HvSHAREKEYS(hv))
964 unshare_hek(HeKEY_hek(entry));
966 Safefree(HeKEY_hek(entry));
971 Perl_hv_clear(pTHX_ HV *hv)
976 xhv = (XPVHV*)SvANY(hv);
981 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
988 S_hfreeentries(pTHX_ HV *hv)
992 register HE *oentry = Null(HE*);
1003 array = HvARRAY(hv);
1008 entry = HeNEXT(entry);
1009 hv_free_ent(hv, oentry);
1014 entry = array[riter];
1017 (void)hv_iterinit(hv);
1021 Perl_hv_undef(pTHX_ HV *hv)
1023 register XPVHV* xhv;
1026 xhv = (XPVHV*)SvANY(hv);
1028 Safefree(xhv->xhv_array);
1030 Safefree(HvNAME(hv));
1034 xhv->xhv_max = 7; /* it's a normal hash */
1043 Perl_hv_iterinit(pTHX_ HV *hv)
1045 register XPVHV* xhv;
1049 Perl_croak(aTHX_ "Bad hash");
1050 xhv = (XPVHV*)SvANY(hv);
1051 entry = xhv->xhv_eiter;
1052 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1054 hv_free_ent(hv, entry);
1056 xhv->xhv_riter = -1;
1057 xhv->xhv_eiter = Null(HE*);
1058 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1062 Perl_hv_iternext(pTHX_ HV *hv)
1064 register XPVHV* xhv;
1070 Perl_croak(aTHX_ "Bad hash");
1071 xhv = (XPVHV*)SvANY(hv);
1072 oldentry = entry = xhv->xhv_eiter;
1074 if (mg = SvTIED_mg((SV*)hv, 'P')) {
1075 SV *key = sv_newmortal();
1077 sv_setsv(key, HeSVKEY_force(entry));
1078 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1084 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
1086 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1088 HeKEY_hek(entry) = hek;
1089 HeKLEN(entry) = HEf_SVKEY;
1091 magic_nextpack((SV*) hv,mg,key);
1093 /* force key to stay around until next time */
1094 HeSVKEY_set(entry, SvREFCNT_inc(key));
1095 return entry; /* beware, hent_val is not set */
1098 SvREFCNT_dec(HeVAL(entry));
1099 Safefree(HeKEY_hek(entry));
1101 xhv->xhv_eiter = Null(HE*);
1104 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1105 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1109 if (!xhv->xhv_array)
1110 Newz(506, xhv->xhv_array,
1111 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1113 entry = HeNEXT(entry);
1116 if (xhv->xhv_riter > xhv->xhv_max) {
1117 xhv->xhv_riter = -1;
1120 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1123 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1125 hv_free_ent(hv, oldentry);
1128 xhv->xhv_eiter = entry;
1133 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1135 if (HeKLEN(entry) == HEf_SVKEY) {
1137 char *p = SvPV(HeKEY_sv(entry), len);
1142 *retlen = HeKLEN(entry);
1143 return HeKEY(entry);
1147 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1149 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1151 if (HeKLEN(entry) == HEf_SVKEY)
1152 return sv_mortalcopy(HeKEY_sv(entry));
1154 return sv_2mortal(newSVpvn((HeKLEN(entry) ? HeKEY(entry) : ""),
1159 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1161 if (SvRMAGICAL(hv)) {
1162 if (mg_find((SV*)hv,'P')) {
1163 SV* sv = sv_newmortal();
1164 if (HeKLEN(entry) == HEf_SVKEY)
1165 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1166 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1170 return HeVAL(entry);
1174 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1177 if ( (he = hv_iternext(hv)) == NULL)
1179 *key = hv_iterkey(he, retlen);
1180 return hv_iterval(hv, he);
1184 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1186 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1190 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1192 return HEK_KEY(share_hek(sv, len, hash));
1195 /* possibly free a shared string if no one has access to it
1196 * len and hash must both be valid for str.
1199 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1201 register XPVHV* xhv;
1203 register HE **oentry;
1207 /* what follows is the moral equivalent of:
1208 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1209 if (--*Svp == Nullsv)
1210 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1212 xhv = (XPVHV*)SvANY(PL_strtab);
1213 /* assert(xhv_array != 0) */
1215 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1216 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1217 if (HeHASH(entry) != hash) /* strings can't be equal */
1219 if (HeKLEN(entry) != len)
1221 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1224 if (--HeVAL(entry) == Nullsv) {
1225 *oentry = HeNEXT(entry);
1228 Safefree(HeKEY_hek(entry));
1234 UNLOCK_STRTAB_MUTEX;
1238 if (!found && ckWARN_d(WARN_INTERNAL))
1239 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string");
1243 /* get a (constant) string ptr from the global string table
1244 * string will get added if it is not already there.
1245 * len and hash must both be valid for str.
1248 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1250 register XPVHV* xhv;
1252 register HE **oentry;
1256 /* what follows is the moral equivalent of:
1258 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1259 hv_store(PL_strtab, str, len, Nullsv, hash);
1261 xhv = (XPVHV*)SvANY(PL_strtab);
1262 /* assert(xhv_array != 0) */
1264 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1265 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1266 if (HeHASH(entry) != hash) /* strings can't be equal */
1268 if (HeKLEN(entry) != len)
1270 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1277 HeKEY_hek(entry) = save_hek(str, len, hash);
1278 HeVAL(entry) = Nullsv;
1279 HeNEXT(entry) = *oentry;
1282 if (i) { /* initial entry? */
1284 if (xhv->xhv_keys > xhv->xhv_max)
1289 ++HeVAL(entry); /* use value slot as REFCNT */
1290 UNLOCK_STRTAB_MUTEX;
1291 return HeKEY_hek(entry);