3 * Copyright (c) 1991-1997, 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
17 static void hv_magic_check _((HV *hv, bool *needs_copy, bool *needs_store));
19 static void hsplit _((HV *hv));
20 static void hfreeentries _((HV *hv));
21 static HE* more_he _((void));
39 HeNEXT(p) = (HE*)he_root;
48 New(54, he_root, 1008/sizeof(HE), HE);
50 heend = &he[1008 / sizeof(HE) - 1];
52 HeNEXT(he) = (HE*)(he + 1);
60 save_hek(char *str, I32 len, U32 hash)
65 New(54, k, HEK_BASESIZE + len + 1, char);
67 Copy(str, HEK_KEY(hek), len, char);
68 *(HEK_KEY(hek) + len) = '\0';
77 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
80 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
84 hv_fetch(HV *hv, char *key, U32 klen, I32 lval)
95 if (mg_find((SV*)hv,'P')) {
98 mg_copy((SV*)hv, sv, key, klen);
102 #ifdef ENV_IS_CASELESS
103 else if (mg_find((SV*)hv,'E')) {
105 for (i = 0; i < klen; ++i)
106 if (isLOWER(key[i])) {
107 char *nkey = strupr(SvPVX(sv_2mortal(newSVpv(key,klen))));
108 SV **ret = hv_fetch(hv, nkey, klen, 0);
110 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
117 xhv = (XPVHV*)SvANY(hv);
118 if (!xhv->xhv_array) {
120 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
121 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
124 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
129 PERL_HASH(hash, key, klen);
131 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
132 for (; entry; entry = HeNEXT(entry)) {
133 if (HeHASH(entry) != hash) /* strings can't be equal */
135 if (HeKLEN(entry) != klen)
137 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
139 return &HeVAL(entry);
141 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
142 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
145 if ((gotenv = PerlEnv_getenv(key)) != Nullch) {
146 sv = newSVpv(gotenv,strlen(gotenv));
148 return hv_store(hv,key,klen,sv,hash);
152 if (lval) { /* gonna assign to this, so it better be there */
154 return hv_store(hv,key,klen,sv,hash);
159 /* returns a HE * structure with the all fields set */
160 /* note that hent_val will be a mortal sv for MAGICAL hashes */
162 hv_fetch_ent(HV *hv, SV *keysv, I32 lval, register U32 hash)
173 if (SvRMAGICAL(hv)) {
174 if (mg_find((SV*)hv,'P')) {
177 keysv = sv_2mortal(newSVsv(keysv));
178 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
179 if (!HeKEY_hek(&hv_fetch_ent_mh)) {
181 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
182 HeKEY_hek(&hv_fetch_ent_mh) = (HEK*)k;
184 HeSVKEY_set(&hv_fetch_ent_mh, keysv);
185 HeVAL(&hv_fetch_ent_mh) = sv;
186 return &hv_fetch_ent_mh;
188 #ifdef ENV_IS_CASELESS
189 else if (mg_find((SV*)hv,'E')) {
191 key = SvPV(keysv, klen);
192 for (i = 0; i < klen; ++i)
193 if (isLOWER(key[i])) {
194 SV *nkeysv = sv_2mortal(newSVpv(key,klen));
195 (void)strupr(SvPVX(nkeysv));
196 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
198 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
205 xhv = (XPVHV*)SvANY(hv);
206 if (!xhv->xhv_array) {
208 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
209 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
212 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
217 key = SvPV(keysv, klen);
220 PERL_HASH(hash, key, klen);
222 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
223 for (; entry; entry = HeNEXT(entry)) {
224 if (HeHASH(entry) != hash) /* strings can't be equal */
226 if (HeKLEN(entry) != klen)
228 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
232 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
233 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
236 if ((gotenv = PerlEnv_getenv(key)) != Nullch) {
237 sv = newSVpv(gotenv,strlen(gotenv));
239 return hv_store_ent(hv,keysv,sv,hash);
243 if (lval) { /* gonna assign to this, so it better be there */
245 return hv_store_ent(hv,keysv,sv,hash);
251 hv_magic_check (HV *hv, bool *needs_copy, bool *needs_store)
253 MAGIC *mg = SvMAGIC(hv);
257 if (isUPPER(mg->mg_type)) {
259 switch (mg->mg_type) {
262 *needs_store = FALSE;
265 mg = mg->mg_moremagic;
270 hv_store(HV *hv, char *key, U32 klen, SV *val, register U32 hash)
275 register HE **oentry;
280 xhv = (XPVHV*)SvANY(hv);
284 hv_magic_check (hv, &needs_copy, &needs_store);
286 mg_copy((SV*)hv, val, key, klen);
287 if (!xhv->xhv_array && !needs_store)
289 #ifdef ENV_IS_CASELESS
290 else if (mg_find((SV*)hv,'E')) {
291 SV *sv = sv_2mortal(newSVpv(key,klen));
292 key = strupr(SvPVX(sv));
299 PERL_HASH(hash, key, klen);
302 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
304 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
307 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
308 if (HeHASH(entry) != hash) /* strings can't be equal */
310 if (HeKLEN(entry) != klen)
312 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
314 SvREFCNT_dec(HeVAL(entry));
316 return &HeVAL(entry);
321 HeKEY_hek(entry) = share_hek(key, klen, hash);
322 else /* gotta do the real thing */
323 HeKEY_hek(entry) = save_hek(key, klen, hash);
325 HeNEXT(entry) = *oentry;
329 if (i) { /* initial entry? */
331 if (xhv->xhv_keys > xhv->xhv_max)
335 return &HeVAL(entry);
339 hv_store_ent(HV *hv, SV *keysv, SV *val, register U32 hash)
346 register HE **oentry;
351 xhv = (XPVHV*)SvANY(hv);
356 hv_magic_check (hv, &needs_copy, &needs_store);
358 bool save_taint = tainted;
360 tainted = SvTAINTED(keysv);
361 keysv = sv_2mortal(newSVsv(keysv));
362 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
363 TAINT_IF(save_taint);
364 if (!xhv->xhv_array && !needs_store)
366 #ifdef ENV_IS_CASELESS
367 else if (mg_find((SV*)hv,'E')) {
368 key = SvPV(keysv, klen);
369 keysv = sv_2mortal(newSVpv(key,klen));
370 (void)strupr(SvPVX(keysv));
377 key = SvPV(keysv, klen);
380 PERL_HASH(hash, key, klen);
383 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
385 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
388 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
389 if (HeHASH(entry) != hash) /* strings can't be equal */
391 if (HeKLEN(entry) != klen)
393 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
395 SvREFCNT_dec(HeVAL(entry));
402 HeKEY_hek(entry) = share_hek(key, klen, hash);
403 else /* gotta do the real thing */
404 HeKEY_hek(entry) = save_hek(key, klen, hash);
406 HeNEXT(entry) = *oentry;
410 if (i) { /* initial entry? */
412 if (xhv->xhv_keys > xhv->xhv_max)
420 hv_delete(HV *hv, char *key, U32 klen, I32 flags)
426 register HE **oentry;
432 if (SvRMAGICAL(hv)) {
435 hv_magic_check (hv, &needs_copy, &needs_store);
437 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
441 if (mg_find(sv, 'p')) {
442 sv_unmagic(sv, 'p'); /* No longer an element */
445 return Nullsv; /* element cannot be deleted */
447 #ifdef ENV_IS_CASELESS
448 else if (mg_find((SV*)hv,'E')) {
449 sv = sv_2mortal(newSVpv(key,klen));
450 key = strupr(SvPVX(sv));
455 xhv = (XPVHV*)SvANY(hv);
459 PERL_HASH(hash, key, klen);
461 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
464 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
465 if (HeHASH(entry) != hash) /* strings can't be equal */
467 if (HeKLEN(entry) != klen)
469 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
471 *oentry = HeNEXT(entry);
474 if (flags & G_DISCARD)
477 sv = sv_mortalcopy(HeVAL(entry));
478 if (entry == xhv->xhv_eiter)
481 hv_free_ent(hv, entry);
489 hv_delete_ent(HV *hv, SV *keysv, I32 flags, U32 hash)
496 register HE **oentry;
501 if (SvRMAGICAL(hv)) {
504 hv_magic_check (hv, &needs_copy, &needs_store);
506 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
510 if (mg_find(sv, 'p')) {
511 sv_unmagic(sv, 'p'); /* No longer an element */
514 return Nullsv; /* element cannot be deleted */
516 #ifdef ENV_IS_CASELESS
517 else if (mg_find((SV*)hv,'E')) {
518 key = SvPV(keysv, klen);
519 keysv = sv_2mortal(newSVpv(key,klen));
520 (void)strupr(SvPVX(keysv));
526 xhv = (XPVHV*)SvANY(hv);
530 key = SvPV(keysv, klen);
533 PERL_HASH(hash, key, klen);
535 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
538 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
539 if (HeHASH(entry) != hash) /* strings can't be equal */
541 if (HeKLEN(entry) != klen)
543 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
545 *oentry = HeNEXT(entry);
548 if (flags & G_DISCARD)
551 sv = sv_mortalcopy(HeVAL(entry));
552 if (entry == xhv->xhv_eiter)
555 hv_free_ent(hv, entry);
563 hv_exists(HV *hv, char *key, U32 klen)
573 if (SvRMAGICAL(hv)) {
574 if (mg_find((SV*)hv,'P')) {
577 mg_copy((SV*)hv, sv, key, klen);
578 magic_existspack(sv, mg_find(sv, 'p'));
581 #ifdef ENV_IS_CASELESS
582 else if (mg_find((SV*)hv,'E')) {
583 sv = sv_2mortal(newSVpv(key,klen));
584 key = strupr(SvPVX(sv));
589 xhv = (XPVHV*)SvANY(hv);
593 PERL_HASH(hash, key, klen);
595 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
596 for (; entry; entry = HeNEXT(entry)) {
597 if (HeHASH(entry) != hash) /* strings can't be equal */
599 if (HeKLEN(entry) != klen)
601 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
610 hv_exists_ent(HV *hv, SV *keysv, U32 hash)
621 if (SvRMAGICAL(hv)) {
622 if (mg_find((SV*)hv,'P')) {
623 dTHR; /* just for SvTRUE */
625 keysv = sv_2mortal(newSVsv(keysv));
626 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
627 magic_existspack(sv, mg_find(sv, 'p'));
630 #ifdef ENV_IS_CASELESS
631 else if (mg_find((SV*)hv,'E')) {
632 key = SvPV(keysv, klen);
633 keysv = sv_2mortal(newSVpv(key,klen));
634 (void)strupr(SvPVX(keysv));
640 xhv = (XPVHV*)SvANY(hv);
644 key = SvPV(keysv, klen);
646 PERL_HASH(hash, key, klen);
648 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
649 for (; entry; entry = HeNEXT(entry)) {
650 if (HeHASH(entry) != hash) /* strings can't be equal */
652 if (HeKLEN(entry) != klen)
654 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
664 register XPVHV* xhv = (XPVHV*)SvANY(hv);
665 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
666 register I32 newsize = oldsize * 2;
671 register HE **oentry;
672 #ifndef STRANGE_MALLOC
676 a = (HE**)xhv->xhv_array;
678 #ifdef STRANGE_MALLOC
679 Renew(a, newsize, HE*);
685 i = newsize * sizeof(HE*);
686 #define MALLOC_OVERHEAD 16
687 tmp = MALLOC_OVERHEAD;
688 while (tmp - MALLOC_OVERHEAD < i)
690 tmp -= MALLOC_OVERHEAD;
692 assert(tmp >= newsize);
698 Copy(xhv->xhv_array, a, oldsize, HE*);
700 offer_nice_chunk(xhv->xhv_array,
701 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
704 Safefree(xhv->xhv_array);
708 Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/
709 xhv->xhv_max = --newsize;
710 xhv->xhv_array = (char*)a;
712 for (i=0; i<oldsize; i++,a++) {
713 if (!*a) /* non-existent */
716 for (oentry = a, entry = *a; entry; entry = *oentry) {
717 if ((HeHASH(entry) & newsize) != i) {
718 *oentry = HeNEXT(entry);
726 oentry = &HeNEXT(entry);
728 if (!*a) /* everything moved */
734 hv_ksplit(HV *hv, IV newmax)
736 register XPVHV* xhv = (XPVHV*)SvANY(hv);
737 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
738 register I32 newsize;
743 register HE **oentry;
745 newsize = (I32) newmax; /* possible truncation here */
746 if (newsize != newmax || newmax <= oldsize)
748 while ((newsize & (1 + ~newsize)) != newsize) {
749 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
751 if (newsize < newmax)
753 if (newsize < newmax)
754 return; /* overflow detection */
756 a = (HE**)xhv->xhv_array;
759 #ifdef STRANGE_MALLOC
760 Renew(a, newsize, HE*);
766 i = newsize * sizeof(HE*);
768 while (j - MALLOC_OVERHEAD < i)
770 j -= MALLOC_OVERHEAD;
772 assert(j >= newsize);
778 Copy(xhv->xhv_array, a, oldsize, HE*);
780 offer_nice_chunk(xhv->xhv_array,
781 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
784 Safefree(xhv->xhv_array);
787 Zero(&a[oldsize], newsize-oldsize, HE*); /* zero 2nd half*/
790 Newz(0, a, newsize, HE*);
792 xhv->xhv_max = --newsize;
793 xhv->xhv_array = (char*)a;
794 if (!xhv->xhv_fill) /* skip rest if no entries */
797 for (i=0; i<oldsize; i++,a++) {
798 if (!*a) /* non-existent */
800 for (oentry = a, entry = *a; entry; entry = *oentry) {
801 if ((j = (HeHASH(entry) & newsize)) != i) {
803 *oentry = HeNEXT(entry);
804 if (!(HeNEXT(entry) = a[j]))
810 oentry = &HeNEXT(entry);
812 if (!*a) /* everything moved */
823 hv = (HV*)NEWSV(502,0);
824 sv_upgrade((SV *)hv, SVt_PVHV);
825 xhv = (XPVHV*)SvANY(hv);
828 #ifndef NODEFAULT_SHAREKEYS
829 HvSHAREKEYS_on(hv); /* key-sharing on by default */
831 xhv->xhv_max = 7; /* start with 8 buckets */
834 (void)hv_iterinit(hv); /* so each() will start off right */
839 hv_free_ent(HV *hv, register HE *entry)
846 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
847 sub_generation++; /* may be deletion of method from stash */
849 if (HeKLEN(entry) == HEf_SVKEY) {
850 SvREFCNT_dec(HeKEY_sv(entry));
851 Safefree(HeKEY_hek(entry));
853 else if (HvSHAREKEYS(hv))
854 unshare_hek(HeKEY_hek(entry));
856 Safefree(HeKEY_hek(entry));
861 hv_delayfree_ent(HV *hv, register HE *entry)
865 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
866 sub_generation++; /* may be deletion of method from stash */
867 sv_2mortal(HeVAL(entry)); /* free between statements */
868 if (HeKLEN(entry) == HEf_SVKEY) {
869 sv_2mortal(HeKEY_sv(entry));
870 Safefree(HeKEY_hek(entry));
872 else if (HvSHAREKEYS(hv))
873 unshare_hek(HeKEY_hek(entry));
875 Safefree(HeKEY_hek(entry));
885 xhv = (XPVHV*)SvANY(hv);
890 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
901 register HE *oentry = Null(HE*);
917 entry = HeNEXT(entry);
918 hv_free_ent(hv, oentry);
923 entry = array[riter];
926 (void)hv_iterinit(hv);
935 xhv = (XPVHV*)SvANY(hv);
937 Safefree(xhv->xhv_array);
939 Safefree(HvNAME(hv));
943 xhv->xhv_max = 7; /* it's a normal hash */
959 xhv = (XPVHV*)SvANY(hv);
960 entry = xhv->xhv_eiter;
961 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
962 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
965 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
967 hv_free_ent(hv, entry);
970 xhv->xhv_eiter = Null(HE*);
971 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
984 xhv = (XPVHV*)SvANY(hv);
985 oldentry = entry = xhv->xhv_eiter;
987 if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) {
988 SV *key = sv_newmortal();
990 sv_setsv(key, HeSVKEY_force(entry));
991 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
997 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
999 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1001 HeKEY_hek(entry) = hek;
1002 HeKLEN(entry) = HEf_SVKEY;
1004 magic_nextpack((SV*) hv,mg,key);
1006 /* force key to stay around until next time */
1007 HeSVKEY_set(entry, SvREFCNT_inc(key));
1008 return entry; /* beware, hent_val is not set */
1011 SvREFCNT_dec(HeVAL(entry));
1012 Safefree(HeKEY_hek(entry));
1014 xhv->xhv_eiter = Null(HE*);
1018 if (!xhv->xhv_array)
1019 Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
1021 entry = HeNEXT(entry);
1024 if (xhv->xhv_riter > xhv->xhv_max) {
1025 xhv->xhv_riter = -1;
1028 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1031 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1033 hv_free_ent(hv, oldentry);
1036 xhv->xhv_eiter = entry;
1041 hv_iterkey(register HE *entry, I32 *retlen)
1043 if (HeKLEN(entry) == HEf_SVKEY) {
1045 char *p = SvPV(HeKEY_sv(entry), len);
1050 *retlen = HeKLEN(entry);
1051 return HeKEY(entry);
1055 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1057 hv_iterkeysv(register HE *entry)
1059 if (HeKLEN(entry) == HEf_SVKEY)
1060 return sv_mortalcopy(HeKEY_sv(entry));
1062 return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""),
1067 hv_iterval(HV *hv, register HE *entry)
1069 if (SvRMAGICAL(hv)) {
1070 if (mg_find((SV*)hv,'P')) {
1071 SV* sv = sv_newmortal();
1072 if (HeKLEN(entry) == HEf_SVKEY)
1073 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1074 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1078 return HeVAL(entry);
1082 hv_iternextsv(HV *hv, char **key, I32 *retlen)
1085 if ( (he = hv_iternext(hv)) == NULL)
1087 *key = hv_iterkey(he, retlen);
1088 return hv_iterval(hv, he);
1092 hv_magic(HV *hv, GV *gv, int how)
1094 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1098 sharepvn(char *sv, I32 len, U32 hash)
1100 return HEK_KEY(share_hek(sv, len, hash));
1103 /* possibly free a shared string if no one has access to it
1104 * len and hash must both be valid for str.
1107 unsharepvn(char *str, I32 len, U32 hash)
1109 register XPVHV* xhv;
1111 register HE **oentry;
1115 /* what follows is the moral equivalent of:
1116 if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) {
1117 if (--*Svp == Nullsv)
1118 hv_delete(strtab, str, len, G_DISCARD, hash);
1120 xhv = (XPVHV*)SvANY(strtab);
1121 /* assert(xhv_array != 0) */
1122 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1123 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1124 if (HeHASH(entry) != hash) /* strings can't be equal */
1126 if (HeKLEN(entry) != len)
1128 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1131 if (--HeVAL(entry) == Nullsv) {
1132 *oentry = HeNEXT(entry);
1135 Safefree(HeKEY_hek(entry));
1143 warn("Attempt to free non-existent shared string");
1146 /* get a (constant) string ptr from the global string table
1147 * string will get added if it is not already there.
1148 * len and hash must both be valid for str.
1151 share_hek(char *str, I32 len, register U32 hash)
1153 register XPVHV* xhv;
1155 register HE **oentry;
1159 /* what follows is the moral equivalent of:
1161 if (!(Svp = hv_fetch(strtab, str, len, FALSE)))
1162 hv_store(strtab, str, len, Nullsv, hash);
1164 xhv = (XPVHV*)SvANY(strtab);
1165 /* assert(xhv_array != 0) */
1166 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1167 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1168 if (HeHASH(entry) != hash) /* strings can't be equal */
1170 if (HeKLEN(entry) != len)
1172 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1179 HeKEY_hek(entry) = save_hek(str, len, hash);
1180 HeVAL(entry) = Nullsv;
1181 HeNEXT(entry) = *oentry;
1184 if (i) { /* initial entry? */
1186 if (xhv->xhv_keys > xhv->xhv_max)
1191 ++HeVAL(entry); /* use value slot as REFCNT */
1192 return HeKEY_hek(entry);