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 hsplit _((HV *hv));
18 static void hfreeentries _((HV *hv));
19 static void hv_magic_check _((HV *hv, bool *needs_copy, bool *needs_store));
20 static HE* more_he _((void));
37 HeNEXT(p) = (HE*)he_root;
46 New(54, he_root, 1008/sizeof(HE), HE);
48 heend = &he[1008 / sizeof(HE) - 1];
50 HeNEXT(he) = (HE*)(he + 1);
58 save_hek(char *str, I32 len, U32 hash)
63 New(54, k, HEK_BASESIZE + len + 1, char);
65 Copy(str, HEK_KEY(hek), len, char);
66 *(HEK_KEY(hek) + len) = '\0';
75 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
78 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
82 hv_fetch(HV *hv, char *key, U32 klen, I32 lval)
93 if (mg_find((SV*)hv,'P')) {
96 mg_copy((SV*)hv, sv, key, klen);
100 #ifdef ENV_IS_CASELESS
101 else if (mg_find((SV*)hv,'E')) {
103 for (i = 0; i < klen; ++i)
104 if (isLOWER(key[i])) {
105 char *nkey = strupr(SvPVX(sv_2mortal(newSVpv(key,klen))));
106 SV **ret = hv_fetch(hv, nkey, klen, 0);
108 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
115 xhv = (XPVHV*)SvANY(hv);
116 if (!xhv->xhv_array) {
118 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
119 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
122 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
127 PERL_HASH(hash, key, klen);
129 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
130 for (; entry; entry = HeNEXT(entry)) {
131 if (HeHASH(entry) != hash) /* strings can't be equal */
133 if (HeKLEN(entry) != klen)
135 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
137 return &HeVAL(entry);
139 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
140 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
143 if ((gotenv = ENV_getenv(key)) != Nullch) {
144 sv = newSVpv(gotenv,strlen(gotenv));
146 return hv_store(hv,key,klen,sv,hash);
150 if (lval) { /* gonna assign to this, so it better be there */
152 return hv_store(hv,key,klen,sv,hash);
157 /* returns a HE * structure with the all fields set */
158 /* note that hent_val will be a mortal sv for MAGICAL hashes */
160 hv_fetch_ent(HV *hv, SV *keysv, I32 lval, register U32 hash)
171 if (SvRMAGICAL(hv)) {
172 if (mg_find((SV*)hv,'P')) {
176 keysv = sv_2mortal(newSVsv(keysv));
177 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
178 if (!HeKEY_hek(&mh)) {
180 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
181 HeKEY_hek(&mh) = (HEK*)k;
183 HeSVKEY_set(&mh, keysv);
187 #ifdef ENV_IS_CASELESS
188 else if (mg_find((SV*)hv,'E')) {
190 key = SvPV(keysv, klen);
191 for (i = 0; i < klen; ++i)
192 if (isLOWER(key[i])) {
193 SV *nkeysv = sv_2mortal(newSVpv(key,klen));
194 (void)strupr(SvPVX(nkeysv));
195 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
197 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
204 xhv = (XPVHV*)SvANY(hv);
205 if (!xhv->xhv_array) {
207 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
208 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
211 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
216 key = SvPV(keysv, klen);
219 PERL_HASH(hash, key, klen);
221 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
222 for (; entry; entry = HeNEXT(entry)) {
223 if (HeHASH(entry) != hash) /* strings can't be equal */
225 if (HeKLEN(entry) != klen)
227 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
231 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
232 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
235 if ((gotenv = ENV_getenv(key)) != Nullch) {
236 sv = newSVpv(gotenv,strlen(gotenv));
238 return hv_store_ent(hv,keysv,sv,hash);
242 if (lval) { /* gonna assign to this, so it better be there */
244 return hv_store_ent(hv,keysv,sv,hash);
250 hv_magic_check (HV *hv, bool *needs_copy, bool *needs_store)
252 MAGIC *mg = SvMAGIC(hv);
256 if (isUPPER(mg->mg_type)) {
258 switch (mg->mg_type) {
261 *needs_store = FALSE;
264 mg = mg->mg_moremagic;
269 hv_store(HV *hv, char *key, U32 klen, SV *val, register U32 hash)
274 register HE **oentry;
279 xhv = (XPVHV*)SvANY(hv);
283 hv_magic_check (hv, &needs_copy, &needs_store);
285 mg_copy((SV*)hv, val, key, klen);
286 if (!xhv->xhv_array && !needs_store)
288 #ifdef ENV_IS_CASELESS
289 else if (mg_find((SV*)hv,'E')) {
290 SV *sv = sv_2mortal(newSVpv(key,klen));
291 key = strupr(SvPVX(sv));
298 PERL_HASH(hash, key, klen);
301 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
303 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
306 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
307 if (HeHASH(entry) != hash) /* strings can't be equal */
309 if (HeKLEN(entry) != klen)
311 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
313 SvREFCNT_dec(HeVAL(entry));
315 return &HeVAL(entry);
320 HeKEY_hek(entry) = share_hek(key, klen, hash);
321 else /* gotta do the real thing */
322 HeKEY_hek(entry) = save_hek(key, klen, hash);
324 HeNEXT(entry) = *oentry;
328 if (i) { /* initial entry? */
330 if (xhv->xhv_keys > xhv->xhv_max)
334 return &HeVAL(entry);
338 hv_store_ent(HV *hv, SV *keysv, SV *val, register U32 hash)
345 register HE **oentry;
350 xhv = (XPVHV*)SvANY(hv);
355 hv_magic_check (hv, &needs_copy, &needs_store);
357 bool save_taint = tainted;
359 tainted = SvTAINTED(keysv);
360 keysv = sv_2mortal(newSVsv(keysv));
361 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
362 TAINT_IF(save_taint);
363 if (!xhv->xhv_array && !needs_store)
365 #ifdef ENV_IS_CASELESS
366 else if (mg_find((SV*)hv,'E')) {
367 key = SvPV(keysv, klen);
368 keysv = sv_2mortal(newSVpv(key,klen));
369 (void)strupr(SvPVX(keysv));
376 key = SvPV(keysv, klen);
379 PERL_HASH(hash, key, klen);
382 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
384 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
387 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
388 if (HeHASH(entry) != hash) /* strings can't be equal */
390 if (HeKLEN(entry) != klen)
392 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
394 SvREFCNT_dec(HeVAL(entry));
401 HeKEY_hek(entry) = share_hek(key, klen, hash);
402 else /* gotta do the real thing */
403 HeKEY_hek(entry) = save_hek(key, klen, hash);
405 HeNEXT(entry) = *oentry;
409 if (i) { /* initial entry? */
411 if (xhv->xhv_keys > xhv->xhv_max)
419 hv_delete(HV *hv, char *key, U32 klen, I32 flags)
425 register HE **oentry;
431 if (SvRMAGICAL(hv)) {
434 hv_magic_check (hv, &needs_copy, &needs_store);
436 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
440 if (mg_find(sv, 'p')) {
441 sv_unmagic(sv, 'p'); /* No longer an element */
444 return Nullsv; /* element cannot be deleted */
446 #ifdef ENV_IS_CASELESS
447 else if (mg_find((SV*)hv,'E')) {
448 sv = sv_2mortal(newSVpv(key,klen));
449 key = strupr(SvPVX(sv));
454 xhv = (XPVHV*)SvANY(hv);
458 PERL_HASH(hash, key, klen);
460 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
463 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
464 if (HeHASH(entry) != hash) /* strings can't be equal */
466 if (HeKLEN(entry) != klen)
468 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
470 *oentry = HeNEXT(entry);
473 if (flags & G_DISCARD)
476 sv = sv_mortalcopy(HeVAL(entry));
477 if (entry == xhv->xhv_eiter)
480 hv_free_ent(hv, entry);
488 hv_delete_ent(HV *hv, SV *keysv, I32 flags, U32 hash)
495 register HE **oentry;
500 if (SvRMAGICAL(hv)) {
503 hv_magic_check (hv, &needs_copy, &needs_store);
505 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
509 if (mg_find(sv, 'p')) {
510 sv_unmagic(sv, 'p'); /* No longer an element */
513 return Nullsv; /* element cannot be deleted */
515 #ifdef ENV_IS_CASELESS
516 else if (mg_find((SV*)hv,'E')) {
517 key = SvPV(keysv, klen);
518 keysv = sv_2mortal(newSVpv(key,klen));
519 (void)strupr(SvPVX(keysv));
525 xhv = (XPVHV*)SvANY(hv);
529 key = SvPV(keysv, klen);
532 PERL_HASH(hash, key, klen);
534 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
537 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
538 if (HeHASH(entry) != hash) /* strings can't be equal */
540 if (HeKLEN(entry) != klen)
542 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
544 *oentry = HeNEXT(entry);
547 if (flags & G_DISCARD)
550 sv = sv_mortalcopy(HeVAL(entry));
551 if (entry == xhv->xhv_eiter)
554 hv_free_ent(hv, entry);
562 hv_exists(HV *hv, char *key, U32 klen)
572 if (SvRMAGICAL(hv)) {
573 if (mg_find((SV*)hv,'P')) {
576 mg_copy((SV*)hv, sv, key, klen);
577 magic_existspack(sv, mg_find(sv, 'p'));
580 #ifdef ENV_IS_CASELESS
581 else if (mg_find((SV*)hv,'E')) {
582 sv = sv_2mortal(newSVpv(key,klen));
583 key = strupr(SvPVX(sv));
588 xhv = (XPVHV*)SvANY(hv);
592 PERL_HASH(hash, key, klen);
594 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
595 for (; entry; entry = HeNEXT(entry)) {
596 if (HeHASH(entry) != hash) /* strings can't be equal */
598 if (HeKLEN(entry) != klen)
600 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
609 hv_exists_ent(HV *hv, SV *keysv, U32 hash)
620 if (SvRMAGICAL(hv)) {
621 if (mg_find((SV*)hv,'P')) {
622 dTHR; /* just for SvTRUE */
624 keysv = sv_2mortal(newSVsv(keysv));
625 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
626 magic_existspack(sv, mg_find(sv, 'p'));
629 #ifdef ENV_IS_CASELESS
630 else if (mg_find((SV*)hv,'E')) {
631 key = SvPV(keysv, klen);
632 keysv = sv_2mortal(newSVpv(key,klen));
633 (void)strupr(SvPVX(keysv));
639 xhv = (XPVHV*)SvANY(hv);
643 key = SvPV(keysv, klen);
645 PERL_HASH(hash, key, klen);
647 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
648 for (; entry; entry = HeNEXT(entry)) {
649 if (HeHASH(entry) != hash) /* strings can't be equal */
651 if (HeKLEN(entry) != klen)
653 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
663 register XPVHV* xhv = (XPVHV*)SvANY(hv);
664 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
665 register I32 newsize = oldsize * 2;
670 register HE **oentry;
671 #ifndef STRANGE_MALLOC
675 a = (HE**)xhv->xhv_array;
677 #ifdef STRANGE_MALLOC
678 Renew(a, newsize, HE*);
684 i = newsize * sizeof(HE*);
685 #define MALLOC_OVERHEAD 16
686 tmp = MALLOC_OVERHEAD;
687 while (tmp - MALLOC_OVERHEAD < i)
689 tmp -= MALLOC_OVERHEAD;
691 assert(tmp >= newsize);
697 Copy(xhv->xhv_array, a, oldsize, HE*);
699 offer_nice_chunk(xhv->xhv_array,
700 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
703 Safefree(xhv->xhv_array);
707 Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/
708 xhv->xhv_max = --newsize;
709 xhv->xhv_array = (char*)a;
711 for (i=0; i<oldsize; i++,a++) {
712 if (!*a) /* non-existent */
715 for (oentry = a, entry = *a; entry; entry = *oentry) {
716 if ((HeHASH(entry) & newsize) != i) {
717 *oentry = HeNEXT(entry);
725 oentry = &HeNEXT(entry);
727 if (!*a) /* everything moved */
733 hv_ksplit(HV *hv, IV newmax)
735 register XPVHV* xhv = (XPVHV*)SvANY(hv);
736 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
737 register I32 newsize;
742 register HE **oentry;
744 newsize = (I32) newmax; /* possible truncation here */
745 if (newsize != newmax || newmax <= oldsize)
747 while ((newsize & (1 + ~newsize)) != newsize) {
748 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
750 if (newsize < newmax)
752 if (newsize < newmax)
753 return; /* overflow detection */
755 a = (HE**)xhv->xhv_array;
758 #ifdef STRANGE_MALLOC
759 Renew(a, newsize, HE*);
765 i = newsize * sizeof(HE*);
767 while (j - MALLOC_OVERHEAD < i)
769 j -= MALLOC_OVERHEAD;
771 assert(j >= newsize);
777 Copy(xhv->xhv_array, a, oldsize, HE*);
779 offer_nice_chunk(xhv->xhv_array,
780 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
783 Safefree(xhv->xhv_array);
786 Zero(&a[oldsize], newsize-oldsize, HE*); /* zero 2nd half*/
789 Newz(0, a, newsize, HE*);
791 xhv->xhv_max = --newsize;
792 xhv->xhv_array = (char*)a;
793 if (!xhv->xhv_fill) /* skip rest if no entries */
796 for (i=0; i<oldsize; i++,a++) {
797 if (!*a) /* non-existent */
799 for (oentry = a, entry = *a; entry; entry = *oentry) {
800 if ((j = (HeHASH(entry) & newsize)) != i) {
802 *oentry = HeNEXT(entry);
803 if (!(HeNEXT(entry) = a[j]))
809 oentry = &HeNEXT(entry);
811 if (!*a) /* everything moved */
822 hv = (HV*)NEWSV(502,0);
823 sv_upgrade((SV *)hv, SVt_PVHV);
824 xhv = (XPVHV*)SvANY(hv);
827 #ifndef NODEFAULT_SHAREKEYS
828 HvSHAREKEYS_on(hv); /* key-sharing on by default */
830 xhv->xhv_max = 7; /* start with 8 buckets */
833 (void)hv_iterinit(hv); /* so each() will start off right */
838 hv_free_ent(HV *hv, register HE *entry)
842 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
843 sub_generation++; /* may be deletion of method from stash */
844 SvREFCNT_dec(HeVAL(entry));
845 if (HeKLEN(entry) == HEf_SVKEY) {
846 SvREFCNT_dec(HeKEY_sv(entry));
847 Safefree(HeKEY_hek(entry));
849 else if (HvSHAREKEYS(hv))
850 unshare_hek(HeKEY_hek(entry));
852 Safefree(HeKEY_hek(entry));
857 hv_delayfree_ent(HV *hv, register HE *entry)
861 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
862 sub_generation++; /* may be deletion of method from stash */
863 sv_2mortal(HeVAL(entry)); /* free between statements */
864 if (HeKLEN(entry) == HEf_SVKEY) {
865 sv_2mortal(HeKEY_sv(entry));
866 Safefree(HeKEY_hek(entry));
868 else if (HvSHAREKEYS(hv))
869 unshare_hek(HeKEY_hek(entry));
871 Safefree(HeKEY_hek(entry));
881 xhv = (XPVHV*)SvANY(hv);
886 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
897 register HE *oentry = Null(HE*);
913 entry = HeNEXT(entry);
914 hv_free_ent(hv, oentry);
919 entry = array[riter];
922 (void)hv_iterinit(hv);
931 xhv = (XPVHV*)SvANY(hv);
933 Safefree(xhv->xhv_array);
935 Safefree(HvNAME(hv));
939 xhv->xhv_max = 7; /* it's a normal hash */
955 xhv = (XPVHV*)SvANY(hv);
956 entry = xhv->xhv_eiter;
957 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
958 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
961 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
963 hv_free_ent(hv, entry);
966 xhv->xhv_eiter = Null(HE*);
967 return xhv->xhv_fill; /* should be xhv->xhv_keys? May change later */
980 xhv = (XPVHV*)SvANY(hv);
981 oldentry = entry = xhv->xhv_eiter;
983 if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) {
984 SV *key = sv_newmortal();
986 sv_setsv(key, HeSVKEY_force(entry));
987 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
993 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
995 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
997 HeKEY_hek(entry) = hek;
998 HeKLEN(entry) = HEf_SVKEY;
1000 magic_nextpack((SV*) hv,mg,key);
1002 /* force key to stay around until next time */
1003 HeSVKEY_set(entry, SvREFCNT_inc(key));
1004 return entry; /* beware, hent_val is not set */
1007 SvREFCNT_dec(HeVAL(entry));
1008 Safefree(HeKEY_hek(entry));
1010 xhv->xhv_eiter = Null(HE*);
1014 if (!xhv->xhv_array)
1015 Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
1017 entry = HeNEXT(entry);
1020 if (xhv->xhv_riter > xhv->xhv_max) {
1021 xhv->xhv_riter = -1;
1024 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1027 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1029 hv_free_ent(hv, oldentry);
1032 xhv->xhv_eiter = entry;
1037 hv_iterkey(register HE *entry, I32 *retlen)
1039 if (HeKLEN(entry) == HEf_SVKEY) {
1041 char *p = SvPV(HeKEY_sv(entry), len);
1046 *retlen = HeKLEN(entry);
1047 return HeKEY(entry);
1051 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1053 hv_iterkeysv(register HE *entry)
1055 if (HeKLEN(entry) == HEf_SVKEY)
1056 return sv_mortalcopy(HeKEY_sv(entry));
1058 return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""),
1063 hv_iterval(HV *hv, register HE *entry)
1065 if (SvRMAGICAL(hv)) {
1066 if (mg_find((SV*)hv,'P')) {
1067 SV* sv = sv_newmortal();
1068 if (HeKLEN(entry) == HEf_SVKEY)
1069 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1070 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1074 return HeVAL(entry);
1078 hv_iternextsv(HV *hv, char **key, I32 *retlen)
1081 if ( (he = hv_iternext(hv)) == NULL)
1083 *key = hv_iterkey(he, retlen);
1084 return hv_iterval(hv, he);
1088 hv_magic(HV *hv, GV *gv, int how)
1090 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1094 sharepvn(char *sv, I32 len, U32 hash)
1096 return HEK_KEY(share_hek(sv, len, hash));
1099 /* possibly free a shared string if no one has access to it
1100 * len and hash must both be valid for str.
1103 unsharepvn(char *str, I32 len, U32 hash)
1105 register XPVHV* xhv;
1107 register HE **oentry;
1111 /* what follows is the moral equivalent of:
1112 if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) {
1113 if (--*Svp == Nullsv)
1114 hv_delete(strtab, str, len, G_DISCARD, hash);
1116 xhv = (XPVHV*)SvANY(strtab);
1117 /* assert(xhv_array != 0) */
1118 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1119 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1120 if (HeHASH(entry) != hash) /* strings can't be equal */
1122 if (HeKLEN(entry) != len)
1124 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1127 if (--HeVAL(entry) == Nullsv) {
1128 *oentry = HeNEXT(entry);
1131 Safefree(HeKEY_hek(entry));
1139 warn("Attempt to free non-existent shared string");
1142 /* get a (constant) string ptr from the global string table
1143 * string will get added if it is not already there.
1144 * len and hash must both be valid for str.
1147 share_hek(char *str, I32 len, register U32 hash)
1149 register XPVHV* xhv;
1151 register HE **oentry;
1155 /* what follows is the moral equivalent of:
1157 if (!(Svp = hv_fetch(strtab, str, len, FALSE)))
1158 hv_store(strtab, str, len, Nullsv, hash);
1160 xhv = (XPVHV*)SvANY(strtab);
1161 /* assert(xhv_array != 0) */
1162 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1163 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1164 if (HeHASH(entry) != hash) /* strings can't be equal */
1166 if (HeKLEN(entry) != len)
1168 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1175 HeKEY_hek(entry) = save_hek(str, len, hash);
1176 HeVAL(entry) = Nullsv;
1177 HeNEXT(entry) = *oentry;
1180 if (i) { /* initial entry? */
1182 if (xhv->xhv_keys > xhv->xhv_max)
1187 ++HeVAL(entry); /* use value slot as REFCNT */
1188 return HeKEY_hek(entry);