3 * Copyright (c) 1991-1994, 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);
58 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
62 hv_fetch(hv,key,klen,lval)
77 if (mg_find((SV*)hv,'P')) {
79 mg_copy((SV*)hv, sv, key, klen);
85 xhv = (XPVHV*)SvANY(hv);
86 if (!xhv->xhv_array) {
88 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
89 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
92 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
97 PERL_HASH(hash, key, klen);
99 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
100 for (; entry; entry = HeNEXT(entry)) {
101 if (HeHASH(entry) != hash) /* strings can't be equal */
103 if (HeKLEN(entry) != klen)
105 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
107 return &HeVAL(entry);
109 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
110 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
113 gotenv = my_getenv(key);
114 if (gotenv != NULL) {
115 sv = newSVpv(gotenv,strlen(gotenv));
116 return hv_store(hv,key,klen,sv,hash);
120 if (lval) { /* gonna assign to this, so it better be there */
122 return hv_store(hv,key,klen,sv,hash);
127 /* returns a HE * structure with the all fields set */
128 /* note that hent_val will be a mortal sv for MAGICAL hashes */
130 hv_fetch_ent(hv,keysv,lval,hash)
145 xhv = (XPVHV*)SvANY(hv);
147 if (SvRMAGICAL(hv) && mg_find((SV*)hv,'P')) {
148 if (!(entry = xhv->xhv_eiter)) {
149 xhv->xhv_eiter = entry = new_he(); /* only one HE per MAGICAL hash */
151 HeKLEN(entry) = HEf_SVKEY; /* hent_key is holding an SV* */
153 else if ((sv = HeSVKEY(entry)))
156 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
158 HeKEY(entry) = (char*)SvREFCNT_inc(keysv);
162 key = SvPV(keysv, klen);
165 PERL_HASH(hash, key, klen);
167 if (!xhv->xhv_array) {
169 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
170 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
173 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
178 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
179 for (; entry; entry = HeNEXT(entry)) {
180 if (HeHASH(entry) != hash) /* strings can't be equal */
182 if (HeKLEN(entry) != klen)
184 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
188 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
189 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
192 gotenv = my_getenv(key);
193 if (gotenv != NULL) {
194 sv = newSVpv(gotenv,strlen(gotenv));
195 return hv_store_ent(hv,keysv,sv,hash);
199 if (lval) { /* gonna assign to this, so it better be there */
201 return hv_store_ent(hv,keysv,sv,hash);
207 hv_store(hv,key,klen,val,hash)
217 register HE **oentry;
222 xhv = (XPVHV*)SvANY(hv);
224 mg_copy((SV*)hv, val, key, klen);
229 if (!xhv->xhv_array && (SvMAGIC(hv)->mg_type != 'A'
230 || SvMAGIC(hv)->mg_moremagic))
232 #endif /* OVERLOAD */
235 PERL_HASH(hash, key, klen);
238 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
240 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
243 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
244 if (HeHASH(entry) != hash) /* strings can't be equal */
246 if (HeKLEN(entry) != klen)
248 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
250 SvREFCNT_dec(HeVAL(entry));
252 return &HeVAL(entry);
256 HeKLEN(entry) = klen;
258 HeKEY(entry) = sharepvn(key, klen, hash);
259 else /* gotta do the real thing */
260 HeKEY(entry) = savepvn(key,klen);
262 HeHASH(entry) = hash;
263 HeNEXT(entry) = *oentry;
267 if (i) { /* initial entry? */
269 if (xhv->xhv_keys > xhv->xhv_max)
273 return &HeVAL(entry);
277 hv_store_ent(hv,keysv,val,hash)
288 register HE **oentry;
293 xhv = (XPVHV*)SvANY(hv);
295 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
300 if (!xhv->xhv_array && (SvMAGIC(hv)->mg_type != 'A'
301 || SvMAGIC(hv)->mg_moremagic))
303 #endif /* OVERLOAD */
306 key = SvPV(keysv, klen);
309 PERL_HASH(hash, key, klen);
312 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
314 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
317 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
318 if (HeHASH(entry) != hash) /* strings can't be equal */
320 if (HeKLEN(entry) != klen)
322 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
324 SvREFCNT_dec(HeVAL(entry));
330 HeKLEN(entry) = klen;
332 HeKEY(entry) = sharepvn(key, klen, hash);
333 else /* gotta do the real thing */
334 HeKEY(entry) = savepvn(key,klen);
336 HeHASH(entry) = hash;
337 HeNEXT(entry) = *oentry;
341 if (i) { /* initial entry? */
343 if (xhv->xhv_keys > xhv->xhv_max)
351 hv_delete(hv,key,klen,flags)
361 register HE **oentry;
366 if (SvRMAGICAL(hv)) {
367 sv = *hv_fetch(hv, key, klen, TRUE);
369 if (mg_find(sv, 's')) {
370 return Nullsv; /* %SIG elements cannot be deleted */
372 if (mg_find(sv, 'p')) {
373 sv_unmagic(sv, 'p'); /* No longer an element */
377 xhv = (XPVHV*)SvANY(hv);
381 PERL_HASH(hash, key, klen);
383 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
386 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
387 if (HeHASH(entry) != hash) /* strings can't be equal */
389 if (HeKLEN(entry) != klen)
391 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
393 *oentry = HeNEXT(entry);
396 if (flags & G_DISCARD)
399 sv = sv_mortalcopy(HeVAL(entry));
400 if (entry == xhv->xhv_eiter)
401 HeKLEN(entry) = HEf_LAZYDEL;
403 he_free(entry, HvSHAREKEYS(hv));
411 hv_delete_ent(hv,keysv,flags,hash)
422 register HE **oentry;
427 if (SvRMAGICAL(hv)) {
428 entry = hv_fetch_ent(hv, keysv, TRUE, hash);
431 if (mg_find(sv, 'p')) {
432 sv_unmagic(sv, 'p'); /* No longer an element */
436 xhv = (XPVHV*)SvANY(hv);
440 key = SvPV(keysv, klen);
443 PERL_HASH(hash, key, klen);
445 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
448 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
449 if (HeHASH(entry) != hash) /* strings can't be equal */
451 if (HeKLEN(entry) != klen)
453 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
455 *oentry = HeNEXT(entry);
458 if (flags & G_DISCARD)
461 sv = sv_mortalcopy(HeVAL(entry));
462 if (entry == xhv->xhv_eiter)
463 HeKLEN(entry) = HEf_LAZYDEL;
465 he_free(entry, HvSHAREKEYS(hv));
473 hv_exists(hv,key,klen)
486 if (SvRMAGICAL(hv)) {
487 if (mg_find((SV*)hv,'P')) {
489 mg_copy((SV*)hv, sv, key, klen);
490 magic_existspack(sv, mg_find(sv, 'p'));
495 xhv = (XPVHV*)SvANY(hv);
499 PERL_HASH(hash, key, klen);
501 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
502 for (; entry; entry = HeNEXT(entry)) {
503 if (HeHASH(entry) != hash) /* strings can't be equal */
505 if (HeKLEN(entry) != klen)
507 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
516 hv_exists_ent(hv,keysv,hash)
530 if (SvRMAGICAL(hv)) {
531 if (mg_find((SV*)hv,'P')) {
533 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
534 magic_existspack(sv, mg_find(sv, 'p'));
539 xhv = (XPVHV*)SvANY(hv);
543 key = SvPV(keysv, klen);
545 PERL_HASH(hash, key, klen);
547 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
548 for (; entry; entry = HeNEXT(entry)) {
549 if (HeHASH(entry) != hash) /* strings can't be equal */
551 if (HeKLEN(entry) != klen)
553 if (bcmp(HeKEY(entry),key,klen)) /* is this it? */
564 register XPVHV* xhv = (XPVHV*)SvANY(hv);
565 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
566 register I32 newsize = oldsize * 2;
571 register HE **oentry;
572 #ifndef STRANGE_MALLOC
576 a = (HE**)xhv->xhv_array;
578 #ifdef STRANGE_MALLOC
579 Renew(a, newsize, HE*);
581 i = newsize * sizeof(HE*);
582 #define MALLOC_OVERHEAD 16
583 tmp = MALLOC_OVERHEAD;
584 while (tmp - MALLOC_OVERHEAD < i)
586 tmp -= MALLOC_OVERHEAD;
588 assert(tmp >= newsize);
590 Copy(xhv->xhv_array, a, oldsize, HE*);
591 if (oldsize >= 64 && !nice_chunk) {
592 nice_chunk = (char*)xhv->xhv_array;
593 nice_chunk_size = oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD;
596 Safefree(xhv->xhv_array);
600 Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/
601 xhv->xhv_max = --newsize;
602 xhv->xhv_array = (char*)a;
604 for (i=0; i<oldsize; i++,a++) {
605 if (!*a) /* non-existent */
608 for (oentry = a, entry = *a; entry; entry = *oentry) {
609 if ((HeHASH(entry) & newsize) != i) {
610 *oentry = HeNEXT(entry);
618 oentry = &HeNEXT(entry);
620 if (!*a) /* everything moved */
631 hv = (HV*)NEWSV(502,0);
632 sv_upgrade((SV *)hv, SVt_PVHV);
633 xhv = (XPVHV*)SvANY(hv);
636 #ifndef NODEFAULT_SHAREKEYS
637 HvSHAREKEYS_on(hv); /* key-sharing on by default */
639 xhv->xhv_max = 7; /* start with 8 buckets */
642 (void)hv_iterinit(hv); /* so each() will start off right */
647 he_free(hent, shared)
653 SvREFCNT_dec(HeVAL(hent));
654 if (HeKLEN(hent) == HEf_SVKEY)
655 SvREFCNT_dec((SV*)HeKEY(hent));
657 unsharepvn(HeKEY(hent), HeKLEN(hent), HeHASH(hent));
659 Safefree(HeKEY(hent));
664 he_delayfree(hent, shared)
670 sv_2mortal(HeVAL(hent)); /* free between statements */
671 if (HeKLEN(hent) == HEf_SVKEY)
672 sv_2mortal((SV*)HeKEY(hent));
674 unsharepvn(HeKEY(hent), HeKLEN(hent), HeHASH(hent));
676 Safefree(HeKEY(hent));
687 xhv = (XPVHV*)SvANY(hv);
692 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
704 register HE *ohent = Null(HE*);
718 shared = HvSHAREKEYS(hv);
723 he_free(ohent, shared);
731 (void)hv_iterinit(hv);
741 xhv = (XPVHV*)SvANY(hv);
743 Safefree(xhv->xhv_array);
745 Safefree(HvNAME(hv));
749 xhv->xhv_max = 7; /* it's a normal associative array */
761 register XPVHV* xhv = (XPVHV*)SvANY(hv);
762 HE *entry = xhv->xhv_eiter;
763 if (entry && HeKLEN(entry) == HEf_LAZYDEL) /* was deleted earlier? */
764 he_free(entry, HvSHAREKEYS(hv));
766 xhv->xhv_eiter = Null(HE*);
767 return xhv->xhv_fill;
780 croak("Bad associative array");
781 xhv = (XPVHV*)SvANY(hv);
782 oldentry = entry = xhv->xhv_eiter;
784 if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) {
785 SV *key = sv_newmortal();
787 sv_setsv(key, HeSVKEY_force(entry));
789 xhv->xhv_eiter = entry = new_he(); /* only one HE per MAGICAL hash */
791 HeKLEN(entry) = HEf_SVKEY;
793 magic_nextpack((SV*) hv,mg,key);
795 SvREFCNT_dec(HeSVKEY(entry));
796 HeKEY(entry) = (char*)SvREFCNT_inc(key);
797 return entry; /* beware, hent_val is not set */
800 SvREFCNT_dec(HeVAL(entry));
802 xhv->xhv_eiter = Null(HE*);
807 Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
809 entry = HeNEXT(entry);
812 if (xhv->xhv_riter > xhv->xhv_max) {
816 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
819 if (oldentry && HeKLEN(oldentry) == HEf_LAZYDEL) /* was deleted earlier? */
820 he_free(oldentry, HvSHAREKEYS(hv));
822 xhv->xhv_eiter = entry;
827 hv_iterkey(entry,retlen)
831 if (HeKLEN(entry) == HEf_SVKEY) {
832 return SvPV((SV*)HeKEY(entry), *(STRLEN*)retlen);
835 *retlen = HeKLEN(entry);
840 /* unlike hv_iterval(), this always returns a mortal copy of the key */
845 if (HeKLEN(entry) == HEf_SVKEY)
846 return sv_mortalcopy((SV*)HeKEY(entry));
848 return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""),
857 if (SvRMAGICAL(hv)) {
858 if (mg_find((SV*)hv,'P')) {
859 SV* sv = sv_newmortal();
860 mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
868 hv_iternextsv(hv, key, retlen)
874 if ( (he = hv_iternext(hv)) == NULL)
876 *key = hv_iterkey(he, retlen);
877 return hv_iterval(hv, he);
881 hv_magic(hv, gv, how)
886 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
889 /* get a (constant) string ptr from the global string table
890 * string will get added if it is not already there.
891 * len and hash must both be valid for str.
894 sharepvn(str, len, hash)
901 register HE **oentry;
905 /* what follows is the moral equivalent of:
907 if (!(Svp = hv_fetch(strtab, str, len, FALSE)))
908 hv_store(strtab, str, len, Nullsv, hash);
910 xhv = (XPVHV*)SvANY(strtab);
911 /* assert(xhv_array != 0) */
912 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
913 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
914 if (HeHASH(entry) != hash) /* strings can't be equal */
916 if (HeKLEN(entry) != len)
918 if (bcmp(HeKEY(entry),str,len)) /* is this it? */
926 HeKEY(entry) = savepvn(str,len);
927 HeVAL(entry) = Nullsv;
928 HeHASH(entry) = hash;
929 HeNEXT(entry) = *oentry;
932 if (i) { /* initial entry? */
934 if (xhv->xhv_keys > xhv->xhv_max)
939 ++HeVAL(entry); /* use value slot as REFCNT */
943 /* possibly free a shared string if no one has access to it
944 * len and hash must both be valid for str.
947 unsharepvn(str, len, hash)
954 register HE **oentry;
958 /* what follows is the moral equivalent of:
959 if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) {
960 if (--*Svp == Nullsv)
961 hv_delete(strtab, str, len, G_DISCARD, hash);
963 xhv = (XPVHV*)SvANY(strtab);
964 /* assert(xhv_array != 0) */
965 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
966 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
967 if (HeHASH(entry) != hash) /* strings can't be equal */
969 if (HeKLEN(entry) != len)
971 if (bcmp(HeKEY(entry),str,len)) /* is this it? */
974 if (--HeVAL(entry) == Nullsv) {
975 *oentry = HeNEXT(entry);
978 Safefree(HeKEY(entry));
986 warn("Attempt to free non-existent shared string");