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 he_root = (HE*)safemalloc(1008);
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;
430 if (SvRMAGICAL(hv)) {
433 hv_magic_check (hv, &needs_copy, &needs_store);
436 sv = *hv_fetch(hv, key, klen, TRUE);
439 if (mg_find(sv, 'p')) {
440 sv_unmagic(sv, 'p'); /* No longer an element */
443 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));
453 xhv = (XPVHV*)SvANY(hv);
457 PERL_HASH(hash, key, klen);
459 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
462 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
463 if (HeHASH(entry) != hash) /* strings can't be equal */
465 if (HeKLEN(entry) != klen)
467 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
469 *oentry = HeNEXT(entry);
472 if (flags & G_DISCARD)
475 sv = sv_mortalcopy(HeVAL(entry));
476 if (entry == xhv->xhv_eiter)
479 hv_free_ent(hv, entry);
487 hv_delete_ent(HV *hv, SV *keysv, I32 flags, U32 hash)
494 register HE **oentry;
499 if (SvRMAGICAL(hv)) {
502 hv_magic_check (hv, &needs_copy, &needs_store);
505 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 */
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));
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*);
680 i = newsize * sizeof(HE*);
681 #define MALLOC_OVERHEAD 16
682 tmp = MALLOC_OVERHEAD;
683 while (tmp - MALLOC_OVERHEAD < i)
685 tmp -= MALLOC_OVERHEAD;
687 assert(tmp >= newsize);
689 Copy(xhv->xhv_array, a, oldsize, HE*);
691 offer_nice_chunk(xhv->xhv_array,
692 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
695 Safefree(xhv->xhv_array);
699 Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/
700 xhv->xhv_max = --newsize;
701 xhv->xhv_array = (char*)a;
703 for (i=0; i<oldsize; i++,a++) {
704 if (!*a) /* non-existent */
707 for (oentry = a, entry = *a; entry; entry = *oentry) {
708 if ((HeHASH(entry) & newsize) != i) {
709 *oentry = HeNEXT(entry);
717 oentry = &HeNEXT(entry);
719 if (!*a) /* everything moved */
725 hv_ksplit(HV *hv, IV newmax)
727 register XPVHV* xhv = (XPVHV*)SvANY(hv);
728 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
729 register I32 newsize;
734 register HE **oentry;
736 newsize = (I32) newmax; /* possible truncation here */
737 if (newsize != newmax || newmax <= oldsize)
739 while ((newsize & (1 + ~newsize)) != newsize) {
740 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
742 if (newsize < newmax)
744 if (newsize < newmax)
745 return; /* overflow detection */
747 a = (HE**)xhv->xhv_array;
750 #ifdef STRANGE_MALLOC
751 Renew(a, newsize, HE*);
753 i = newsize * sizeof(HE*);
755 while (j - MALLOC_OVERHEAD < i)
757 j -= MALLOC_OVERHEAD;
759 assert(j >= newsize);
761 Copy(xhv->xhv_array, a, oldsize, HE*);
763 offer_nice_chunk(xhv->xhv_array,
764 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
767 Safefree(xhv->xhv_array);
770 Zero(&a[oldsize], newsize-oldsize, HE*); /* zero 2nd half*/
773 Newz(0, a, newsize, HE*);
775 xhv->xhv_max = --newsize;
776 xhv->xhv_array = (char*)a;
777 if (!xhv->xhv_fill) /* skip rest if no entries */
780 for (i=0; i<oldsize; i++,a++) {
781 if (!*a) /* non-existent */
783 for (oentry = a, entry = *a; entry; entry = *oentry) {
784 if ((j = (HeHASH(entry) & newsize)) != i) {
786 *oentry = HeNEXT(entry);
787 if (!(HeNEXT(entry) = a[j]))
793 oentry = &HeNEXT(entry);
795 if (!*a) /* everything moved */
806 hv = (HV*)NEWSV(502,0);
807 sv_upgrade((SV *)hv, SVt_PVHV);
808 xhv = (XPVHV*)SvANY(hv);
811 #ifndef NODEFAULT_SHAREKEYS
812 HvSHAREKEYS_on(hv); /* key-sharing on by default */
814 xhv->xhv_max = 7; /* start with 8 buckets */
817 (void)hv_iterinit(hv); /* so each() will start off right */
822 hv_free_ent(HV *hv, register HE *entry)
826 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
827 sub_generation++; /* may be deletion of method from stash */
828 SvREFCNT_dec(HeVAL(entry));
829 if (HeKLEN(entry) == HEf_SVKEY) {
830 SvREFCNT_dec(HeKEY_sv(entry));
831 Safefree(HeKEY_hek(entry));
833 else if (HvSHAREKEYS(hv))
834 unshare_hek(HeKEY_hek(entry));
836 Safefree(HeKEY_hek(entry));
841 hv_delayfree_ent(HV *hv, register HE *entry)
845 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
846 sub_generation++; /* may be deletion of method from stash */
847 sv_2mortal(HeVAL(entry)); /* free between statements */
848 if (HeKLEN(entry) == HEf_SVKEY) {
849 sv_2mortal(HeKEY_sv(entry));
850 Safefree(HeKEY_hek(entry));
852 else if (HvSHAREKEYS(hv))
853 unshare_hek(HeKEY_hek(entry));
855 Safefree(HeKEY_hek(entry));
865 xhv = (XPVHV*)SvANY(hv);
870 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
881 register HE *oentry = Null(HE*);
897 entry = HeNEXT(entry);
898 hv_free_ent(hv, oentry);
903 entry = array[riter];
906 (void)hv_iterinit(hv);
915 xhv = (XPVHV*)SvANY(hv);
917 Safefree(xhv->xhv_array);
919 Safefree(HvNAME(hv));
923 xhv->xhv_max = 7; /* it's a normal hash */
939 xhv = (XPVHV*)SvANY(hv);
940 entry = xhv->xhv_eiter;
941 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
942 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
945 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
947 hv_free_ent(hv, entry);
950 xhv->xhv_eiter = Null(HE*);
951 return xhv->xhv_fill; /* should be xhv->xhv_keys? May change later */
964 xhv = (XPVHV*)SvANY(hv);
965 oldentry = entry = xhv->xhv_eiter;
967 if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) {
968 SV *key = sv_newmortal();
970 sv_setsv(key, HeSVKEY_force(entry));
971 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
977 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
979 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
981 HeKEY_hek(entry) = hek;
982 HeKLEN(entry) = HEf_SVKEY;
984 magic_nextpack((SV*) hv,mg,key);
986 /* force key to stay around until next time */
987 HeSVKEY_set(entry, SvREFCNT_inc(key));
988 return entry; /* beware, hent_val is not set */
991 SvREFCNT_dec(HeVAL(entry));
992 Safefree(HeKEY_hek(entry));
994 xhv->xhv_eiter = Null(HE*);
999 Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
1001 entry = HeNEXT(entry);
1004 if (xhv->xhv_riter > xhv->xhv_max) {
1005 xhv->xhv_riter = -1;
1008 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1011 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1013 hv_free_ent(hv, oldentry);
1016 xhv->xhv_eiter = entry;
1021 hv_iterkey(register HE *entry, I32 *retlen)
1023 if (HeKLEN(entry) == HEf_SVKEY) {
1025 char *p = SvPV(HeKEY_sv(entry), len);
1030 *retlen = HeKLEN(entry);
1031 return HeKEY(entry);
1035 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1037 hv_iterkeysv(register HE *entry)
1039 if (HeKLEN(entry) == HEf_SVKEY)
1040 return sv_mortalcopy(HeKEY_sv(entry));
1042 return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""),
1047 hv_iterval(HV *hv, register HE *entry)
1049 if (SvRMAGICAL(hv)) {
1050 if (mg_find((SV*)hv,'P')) {
1051 SV* sv = sv_newmortal();
1052 if (HeKLEN(entry) == HEf_SVKEY)
1053 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1054 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1058 return HeVAL(entry);
1062 hv_iternextsv(HV *hv, char **key, I32 *retlen)
1065 if ( (he = hv_iternext(hv)) == NULL)
1067 *key = hv_iterkey(he, retlen);
1068 return hv_iterval(hv, he);
1072 hv_magic(HV *hv, GV *gv, int how)
1074 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1078 sharepvn(char *sv, I32 len, U32 hash)
1080 return HEK_KEY(share_hek(sv, len, hash));
1083 /* possibly free a shared string if no one has access to it
1084 * len and hash must both be valid for str.
1087 unsharepvn(char *str, I32 len, U32 hash)
1089 register XPVHV* xhv;
1091 register HE **oentry;
1095 /* what follows is the moral equivalent of:
1096 if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) {
1097 if (--*Svp == Nullsv)
1098 hv_delete(strtab, str, len, G_DISCARD, hash);
1100 xhv = (XPVHV*)SvANY(strtab);
1101 /* assert(xhv_array != 0) */
1102 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1103 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1104 if (HeHASH(entry) != hash) /* strings can't be equal */
1106 if (HeKLEN(entry) != len)
1108 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1111 if (--HeVAL(entry) == Nullsv) {
1112 *oentry = HeNEXT(entry);
1115 Safefree(HeKEY_hek(entry));
1123 warn("Attempt to free non-existent shared string");
1126 /* get a (constant) string ptr from the global string table
1127 * string will get added if it is not already there.
1128 * len and hash must both be valid for str.
1131 share_hek(char *str, I32 len, register U32 hash)
1133 register XPVHV* xhv;
1135 register HE **oentry;
1139 /* what follows is the moral equivalent of:
1141 if (!(Svp = hv_fetch(strtab, str, len, FALSE)))
1142 hv_store(strtab, str, len, Nullsv, hash);
1144 xhv = (XPVHV*)SvANY(strtab);
1145 /* assert(xhv_array != 0) */
1146 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1147 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1148 if (HeHASH(entry) != hash) /* strings can't be equal */
1150 if (HeKLEN(entry) != len)
1152 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1159 HeKEY_hek(entry) = save_hek(str, len, hash);
1160 HeVAL(entry) = Nullsv;
1161 HeNEXT(entry) = *oentry;
1164 if (i) { /* initial entry? */
1166 if (xhv->xhv_keys > xhv->xhv_max)
1171 ++HeVAL(entry); /* use value slot as REFCNT */
1172 return HeKEY_hek(entry);