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));
38 HeNEXT(p) = (HE*)he_root;
47 he_root = (HE*)safemalloc(1008);
49 heend = &he[1008 / sizeof(HE) - 1];
51 HeNEXT(he) = (HE*)(he + 1);
59 save_hek(str, len, hash)
67 New(54, k, HEK_BASESIZE + len + 1, char);
69 Copy(str, HEK_KEY(hek), len, char);
70 *(HEK_KEY(hek) + len) = '\0';
80 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
83 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
87 hv_fetch(hv,key,klen,lval)
101 if (SvRMAGICAL(hv)) {
102 if (mg_find((SV*)hv,'P')) {
104 mg_copy((SV*)hv, sv, key, klen);
110 xhv = (XPVHV*)SvANY(hv);
111 if (!xhv->xhv_array) {
113 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
114 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
117 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
122 PERL_HASH(hash, key, klen);
124 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
125 for (; entry; entry = HeNEXT(entry)) {
126 if (HeHASH(entry) != hash) /* strings can't be equal */
128 if (HeKLEN(entry) != klen)
130 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
132 return &HeVAL(entry);
134 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
135 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
138 if ((gotenv = ENV_getenv(key)) != Nullch) {
139 sv = newSVpv(gotenv,strlen(gotenv));
141 return hv_store(hv,key,klen,sv,hash);
145 if (lval) { /* gonna assign to this, so it better be there */
147 return hv_store(hv,key,klen,sv,hash);
152 /* returns a HE * structure with the all fields set */
153 /* note that hent_val will be a mortal sv for MAGICAL hashes */
155 hv_fetch_ent(hv,keysv,lval,hash)
170 if (SvRMAGICAL(hv) && mg_find((SV*)hv,'P')) {
174 keysv = sv_2mortal(newSVsv(keysv));
175 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
176 if (!HeKEY_hek(&mh)) {
178 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
179 HeKEY_hek(&mh) = (HEK*)k;
181 HeSVKEY_set(&mh, keysv);
186 xhv = (XPVHV*)SvANY(hv);
187 if (!xhv->xhv_array) {
189 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
190 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
193 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
198 key = SvPV(keysv, klen);
201 PERL_HASH(hash, key, klen);
203 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
204 for (; entry; entry = HeNEXT(entry)) {
205 if (HeHASH(entry) != hash) /* strings can't be equal */
207 if (HeKLEN(entry) != klen)
209 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
213 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
214 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
217 if ((gotenv = ENV_getenv(key)) != Nullch) {
218 sv = newSVpv(gotenv,strlen(gotenv));
220 return hv_store_ent(hv,keysv,sv,hash);
224 if (lval) { /* gonna assign to this, so it better be there */
226 return hv_store_ent(hv,keysv,sv,hash);
232 hv_store(hv,key,klen,val,hash)
242 register HE **oentry;
247 xhv = (XPVHV*)SvANY(hv);
249 mg_copy((SV*)hv, val, key, klen);
251 && (SvMAGIC(hv)->mg_moremagic
252 || (SvMAGIC(hv)->mg_type != 'E'
254 && SvMAGIC(hv)->mg_type != 'A'
255 #endif /* OVERLOAD */
260 PERL_HASH(hash, key, klen);
263 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
265 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
268 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
269 if (HeHASH(entry) != hash) /* strings can't be equal */
271 if (HeKLEN(entry) != klen)
273 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
275 SvREFCNT_dec(HeVAL(entry));
277 return &HeVAL(entry);
282 HeKEY_hek(entry) = share_hek(key, klen, hash);
283 else /* gotta do the real thing */
284 HeKEY_hek(entry) = save_hek(key, klen, hash);
286 HeNEXT(entry) = *oentry;
290 if (i) { /* initial entry? */
292 if (xhv->xhv_keys > xhv->xhv_max)
296 return &HeVAL(entry);
300 hv_store_ent(hv,keysv,val,hash)
311 register HE **oentry;
316 xhv = (XPVHV*)SvANY(hv);
318 bool save_taint = tainted;
320 tainted = SvTAINTED(keysv);
321 keysv = sv_2mortal(newSVsv(keysv));
322 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
323 TAINT_IF(save_taint);
325 && (SvMAGIC(hv)->mg_moremagic
326 || (SvMAGIC(hv)->mg_type != 'E'
328 && SvMAGIC(hv)->mg_type != 'A'
329 #endif /* OVERLOAD */
334 key = SvPV(keysv, klen);
337 PERL_HASH(hash, key, klen);
340 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
342 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
345 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
346 if (HeHASH(entry) != hash) /* strings can't be equal */
348 if (HeKLEN(entry) != klen)
350 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
352 SvREFCNT_dec(HeVAL(entry));
359 HeKEY_hek(entry) = share_hek(key, klen, hash);
360 else /* gotta do the real thing */
361 HeKEY_hek(entry) = save_hek(key, klen, hash);
363 HeNEXT(entry) = *oentry;
367 if (i) { /* initial entry? */
369 if (xhv->xhv_keys > xhv->xhv_max)
377 hv_delete(hv,key,klen,flags)
387 register HE **oentry;
392 if (SvRMAGICAL(hv)) {
393 sv = *hv_fetch(hv, key, klen, TRUE);
395 if (mg_find(sv, 's')) {
396 return Nullsv; /* %SIG elements cannot be deleted */
398 if (mg_find(sv, 'p')) {
399 sv_unmagic(sv, 'p'); /* No longer an element */
403 xhv = (XPVHV*)SvANY(hv);
407 PERL_HASH(hash, key, klen);
409 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
412 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
413 if (HeHASH(entry) != hash) /* strings can't be equal */
415 if (HeKLEN(entry) != klen)
417 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
419 *oentry = HeNEXT(entry);
422 if (flags & G_DISCARD)
425 sv = sv_mortalcopy(HeVAL(entry));
426 if (entry == xhv->xhv_eiter)
429 hv_free_ent(hv, entry);
437 hv_delete_ent(hv,keysv,flags,hash)
448 register HE **oentry;
453 if (SvRMAGICAL(hv)) {
454 entry = hv_fetch_ent(hv, keysv, TRUE, hash);
457 if (mg_find(sv, 'p')) {
458 sv_unmagic(sv, 'p'); /* No longer an element */
462 xhv = (XPVHV*)SvANY(hv);
466 key = SvPV(keysv, klen);
469 PERL_HASH(hash, key, klen);
471 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
474 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
475 if (HeHASH(entry) != hash) /* strings can't be equal */
477 if (HeKLEN(entry) != klen)
479 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
481 *oentry = HeNEXT(entry);
484 if (flags & G_DISCARD)
487 sv = sv_mortalcopy(HeVAL(entry));
488 if (entry == xhv->xhv_eiter)
491 hv_free_ent(hv, entry);
499 hv_exists(hv,key,klen)
512 if (SvRMAGICAL(hv)) {
513 if (mg_find((SV*)hv,'P')) {
515 mg_copy((SV*)hv, sv, key, klen);
516 magic_existspack(sv, mg_find(sv, 'p'));
521 xhv = (XPVHV*)SvANY(hv);
525 PERL_HASH(hash, key, klen);
527 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
528 for (; entry; entry = HeNEXT(entry)) {
529 if (HeHASH(entry) != hash) /* strings can't be equal */
531 if (HeKLEN(entry) != klen)
533 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
542 hv_exists_ent(hv,keysv,hash)
556 if (SvRMAGICAL(hv)) {
557 if (mg_find((SV*)hv,'P')) {
559 keysv = sv_2mortal(newSVsv(keysv));
560 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
561 magic_existspack(sv, mg_find(sv, 'p'));
566 xhv = (XPVHV*)SvANY(hv);
570 key = SvPV(keysv, klen);
572 PERL_HASH(hash, key, klen);
574 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
575 for (; entry; entry = HeNEXT(entry)) {
576 if (HeHASH(entry) != hash) /* strings can't be equal */
578 if (HeKLEN(entry) != klen)
580 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
591 register XPVHV* xhv = (XPVHV*)SvANY(hv);
592 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
593 register I32 newsize = oldsize * 2;
598 register HE **oentry;
599 #ifndef STRANGE_MALLOC
603 a = (HE**)xhv->xhv_array;
605 #ifdef STRANGE_MALLOC
606 Renew(a, newsize, HE*);
608 i = newsize * sizeof(HE*);
609 #define MALLOC_OVERHEAD 16
610 tmp = MALLOC_OVERHEAD;
611 while (tmp - MALLOC_OVERHEAD < i)
613 tmp -= MALLOC_OVERHEAD;
615 assert(tmp >= newsize);
617 Copy(xhv->xhv_array, a, oldsize, HE*);
618 if (oldsize >= 64 && !nice_chunk) {
619 nice_chunk = (char*)xhv->xhv_array;
620 nice_chunk_size = oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD;
623 Safefree(xhv->xhv_array);
627 Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/
628 xhv->xhv_max = --newsize;
629 xhv->xhv_array = (char*)a;
631 for (i=0; i<oldsize; i++,a++) {
632 if (!*a) /* non-existent */
635 for (oentry = a, entry = *a; entry; entry = *oentry) {
636 if ((HeHASH(entry) & newsize) != i) {
637 *oentry = HeNEXT(entry);
645 oentry = &HeNEXT(entry);
647 if (!*a) /* everything moved */
653 hv_ksplit(hv, newmax)
657 register XPVHV* xhv = (XPVHV*)SvANY(hv);
658 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
659 register I32 newsize;
664 register HE **oentry;
666 newsize = (I32) newmax; /* possible truncation here */
667 if (newsize != newmax || newmax <= oldsize)
669 while ((newsize & (1 + ~newsize)) != newsize) {
670 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
672 if (newsize < newmax)
674 if (newsize < newmax)
675 return; /* overflow detection */
677 a = (HE**)xhv->xhv_array;
680 #ifdef STRANGE_MALLOC
681 Renew(a, newsize, HE*);
683 i = newsize * sizeof(HE*);
685 while (j - MALLOC_OVERHEAD < i)
687 j -= MALLOC_OVERHEAD;
689 assert(j >= newsize);
691 Copy(xhv->xhv_array, a, oldsize, HE*);
692 if (oldsize >= 64 && !nice_chunk) {
693 nice_chunk = (char*)xhv->xhv_array;
694 nice_chunk_size = oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD;
697 Safefree(xhv->xhv_array);
700 Zero(&a[oldsize], newsize-oldsize, HE*); /* zero 2nd half*/
703 Newz(0, a, newsize, HE*);
705 xhv->xhv_max = --newsize;
706 xhv->xhv_array = (char*)a;
707 if (!xhv->xhv_fill) /* skip rest if no entries */
710 for (i=0; i<oldsize; i++,a++) {
711 if (!*a) /* non-existent */
713 for (oentry = a, entry = *a; entry; entry = *oentry) {
714 if ((j = (HeHASH(entry) & newsize)) != i) {
716 *oentry = HeNEXT(entry);
717 if (!(HeNEXT(entry) = a[j]))
723 oentry = &HeNEXT(entry);
725 if (!*a) /* everything moved */
736 hv = (HV*)NEWSV(502,0);
737 sv_upgrade((SV *)hv, SVt_PVHV);
738 xhv = (XPVHV*)SvANY(hv);
741 #ifndef NODEFAULT_SHAREKEYS
742 HvSHAREKEYS_on(hv); /* key-sharing on by default */
744 xhv->xhv_max = 7; /* start with 8 buckets */
747 (void)hv_iterinit(hv); /* so each() will start off right */
752 hv_free_ent(hv, entry)
758 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
759 sub_generation++; /* may be deletion of method from stash */
760 SvREFCNT_dec(HeVAL(entry));
761 if (HeKLEN(entry) == HEf_SVKEY) {
762 SvREFCNT_dec(HeKEY_sv(entry));
763 Safefree(HeKEY_hek(entry));
765 else if (HvSHAREKEYS(hv))
766 unshare_hek(HeKEY_hek(entry));
768 Safefree(HeKEY_hek(entry));
773 hv_delayfree_ent(hv, entry)
779 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
780 sub_generation++; /* may be deletion of method from stash */
781 sv_2mortal(HeVAL(entry)); /* free between statements */
782 if (HeKLEN(entry) == HEf_SVKEY) {
783 sv_2mortal(HeKEY_sv(entry));
784 Safefree(HeKEY_hek(entry));
786 else if (HvSHAREKEYS(hv))
787 unshare_hek(HeKEY_hek(entry));
789 Safefree(HeKEY_hek(entry));
800 xhv = (XPVHV*)SvANY(hv);
805 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
817 register HE *oentry = Null(HE*);
833 entry = HeNEXT(entry);
834 hv_free_ent(hv, oentry);
839 entry = array[riter];
842 (void)hv_iterinit(hv);
852 xhv = (XPVHV*)SvANY(hv);
854 Safefree(xhv->xhv_array);
856 Safefree(HvNAME(hv));
860 xhv->xhv_max = 7; /* it's a normal hash */
877 xhv = (XPVHV*)SvANY(hv);
878 entry = xhv->xhv_eiter;
879 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
880 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
883 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
885 hv_free_ent(hv, entry);
888 xhv->xhv_eiter = Null(HE*);
889 return xhv->xhv_fill; /* should be xhv->xhv_keys? May change later */
903 xhv = (XPVHV*)SvANY(hv);
904 oldentry = entry = xhv->xhv_eiter;
906 if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) {
907 SV *key = sv_newmortal();
909 sv_setsv(key, HeSVKEY_force(entry));
910 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
916 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
918 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
920 HeKEY_hek(entry) = hek;
921 HeKLEN(entry) = HEf_SVKEY;
923 magic_nextpack((SV*) hv,mg,key);
925 /* force key to stay around until next time */
926 HeSVKEY_set(entry, SvREFCNT_inc(key));
927 return entry; /* beware, hent_val is not set */
930 SvREFCNT_dec(HeVAL(entry));
931 Safefree(HeKEY_hek(entry));
933 xhv->xhv_eiter = Null(HE*);
938 Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
940 entry = HeNEXT(entry);
943 if (xhv->xhv_riter > xhv->xhv_max) {
947 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
950 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
952 hv_free_ent(hv, oldentry);
955 xhv->xhv_eiter = entry;
960 hv_iterkey(entry,retlen)
964 if (HeKLEN(entry) == HEf_SVKEY) {
966 char *p = SvPV(HeKEY_sv(entry), len);
971 *retlen = HeKLEN(entry);
976 /* unlike hv_iterval(), this always returns a mortal copy of the key */
981 if (HeKLEN(entry) == HEf_SVKEY)
982 return sv_mortalcopy(HeKEY_sv(entry));
984 return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""),
993 if (SvRMAGICAL(hv)) {
994 if (mg_find((SV*)hv,'P')) {
995 SV* sv = sv_newmortal();
996 if (HeKLEN(entry) == HEf_SVKEY)
997 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
998 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1002 return HeVAL(entry);
1006 hv_iternextsv(hv, key, retlen)
1012 if ( (he = hv_iternext(hv)) == NULL)
1014 *key = hv_iterkey(he, retlen);
1015 return hv_iterval(hv, he);
1019 hv_magic(hv, gv, how)
1024 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1028 sharepvn(sv, len, hash)
1033 return HEK_KEY(share_hek(sv, len, hash));
1036 /* possibly free a shared string if no one has access to it
1037 * len and hash must both be valid for str.
1040 unsharepvn(str, len, hash)
1045 register XPVHV* xhv;
1047 register HE **oentry;
1051 /* what follows is the moral equivalent of:
1052 if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) {
1053 if (--*Svp == Nullsv)
1054 hv_delete(strtab, str, len, G_DISCARD, hash);
1056 xhv = (XPVHV*)SvANY(strtab);
1057 /* assert(xhv_array != 0) */
1058 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1059 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1060 if (HeHASH(entry) != hash) /* strings can't be equal */
1062 if (HeKLEN(entry) != len)
1064 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1067 if (--HeVAL(entry) == Nullsv) {
1068 *oentry = HeNEXT(entry);
1071 Safefree(HeKEY_hek(entry));
1079 warn("Attempt to free non-existent shared string");
1082 /* get a (constant) string ptr from the global string table
1083 * string will get added if it is not already there.
1084 * len and hash must both be valid for str.
1087 share_hek(str, len, hash)
1092 register XPVHV* xhv;
1094 register HE **oentry;
1098 /* what follows is the moral equivalent of:
1100 if (!(Svp = hv_fetch(strtab, str, len, FALSE)))
1101 hv_store(strtab, str, len, Nullsv, hash);
1103 xhv = (XPVHV*)SvANY(strtab);
1104 /* assert(xhv_array != 0) */
1105 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1106 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1107 if (HeHASH(entry) != hash) /* strings can't be equal */
1109 if (HeKLEN(entry) != len)
1111 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1118 HeKEY_hek(entry) = save_hek(str, len, hash);
1119 HeVAL(entry) = Nullsv;
1120 HeNEXT(entry) = *oentry;
1123 if (i) { /* initial entry? */
1125 if (xhv->xhv_keys > xhv->xhv_max)
1130 ++HeVAL(entry); /* use value slot as REFCNT */
1131 return HeKEY_hek(entry);