3 * Copyright (c) 1991-2001, 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
27 PL_he_root = HeNEXT(he);
36 HeNEXT(p) = (HE*)PL_he_root;
47 New(54, ptr, 1008/sizeof(XPV), XPV);
48 ptr->xpv_pv = (char*)PL_he_arenaroot;
49 PL_he_arenaroot = ptr;
52 heend = &he[1008 / sizeof(HE) - 1];
55 HeNEXT(he) = (HE*)(he + 1);
63 #define new_HE() (HE*)safemalloc(sizeof(HE))
64 #define del_HE(p) safefree((char*)p)
68 #define new_HE() new_he()
69 #define del_HE(p) del_he(p)
74 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
85 New(54, k, HEK_BASESIZE + len + 1, char);
87 Copy(str, HEK_KEY(hek), len, char);
90 HEK_UTF8(hek) = (char)is_utf8;
95 Perl_unshare_hek(pTHX_ HEK *hek)
97 unsharepvn(HEK_KEY(hek),HEK_UTF8(hek)?-HEK_LEN(hek):HEK_LEN(hek),
101 #if defined(USE_ITHREADS)
103 Perl_he_dup(pTHX_ HE *e, bool shared)
109 /* look for it in the table first */
110 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
114 /* create anew and remember what it is */
116 ptr_table_store(PL_ptr_table, e, ret);
118 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
119 if (HeKLEN(e) == HEf_SVKEY)
120 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
122 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
124 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
125 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
128 #endif /* USE_ITHREADS */
130 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
136 Returns the SV which corresponds to the specified key in the hash. The
137 C<klen> is the length of the key. If C<lval> is set then the fetch will be
138 part of a store. Check that the return value is non-null before
139 dereferencing it to a C<SV*>.
141 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
142 information on how to use this function on tied hashes.
148 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
154 bool is_utf8 = FALSE;
164 if (SvRMAGICAL(hv)) {
165 if (mg_find((SV*)hv,'P')) {
167 mg_copy((SV*)hv, sv, key, klen);
169 return &PL_hv_fetch_sv;
171 #ifdef ENV_IS_CASELESS
172 else if (mg_find((SV*)hv,'E')) {
174 for (i = 0; i < klen; ++i)
175 if (isLOWER(key[i])) {
176 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
177 SV **ret = hv_fetch(hv, nkey, klen, 0);
179 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
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,
194 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
199 PERL_HASH(hash, key, klen);
201 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
202 for (; entry; entry = HeNEXT(entry)) {
203 if (HeHASH(entry) != hash) /* strings can't be equal */
205 if (HeKLEN(entry) != klen)
207 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
209 if (HeKUTF8(entry) != (char)is_utf8)
211 return &HeVAL(entry);
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)) {
216 char *env = PerlEnv_ENVgetenv_len(key,&len);
218 sv = newSVpvn(env,len);
220 return hv_store(hv,key,klen,sv,hash);
224 if (lval) { /* gonna assign to this, so it better be there */
226 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
231 /* returns a HE * structure with the all fields set */
232 /* note that hent_val will be a mortal sv for MAGICAL hashes */
234 =for apidoc hv_fetch_ent
236 Returns the hash entry which corresponds to the specified key in the hash.
237 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
238 if you want the function to compute it. IF C<lval> is set then the fetch
239 will be part of a store. Make sure the return value is non-null before
240 accessing it. The return value when C<tb> is a tied hash is a pointer to a
241 static location, so be sure to make a copy of the structure if you need to
244 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
245 information on how to use this function on tied hashes.
251 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
263 if (SvRMAGICAL(hv)) {
264 if (mg_find((SV*)hv,'P')) {
266 keysv = sv_2mortal(newSVsv(keysv));
267 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
268 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
270 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
271 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
273 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
274 HeVAL(&PL_hv_fetch_ent_mh) = sv;
275 return &PL_hv_fetch_ent_mh;
277 #ifdef ENV_IS_CASELESS
278 else if (mg_find((SV*)hv,'E')) {
280 key = SvPV(keysv, klen);
281 for (i = 0; i < klen; ++i)
282 if (isLOWER(key[i])) {
283 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
284 (void)strupr(SvPVX(nkeysv));
285 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
287 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
294 xhv = (XPVHV*)SvANY(hv);
295 if (!xhv->xhv_array) {
297 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
298 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
301 Newz(503, xhv->xhv_array,
302 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
307 key = SvPV(keysv, klen);
308 is_utf8 = (SvUTF8(keysv)!=0);
311 PERL_HASH(hash, key, klen);
313 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
314 for (; entry; entry = HeNEXT(entry)) {
315 if (HeHASH(entry) != hash) /* strings can't be equal */
317 if (HeKLEN(entry) != klen)
319 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
321 if (HeKUTF8(entry) != (char)is_utf8)
325 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
326 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
328 char *env = PerlEnv_ENVgetenv_len(key,&len);
330 sv = newSVpvn(env,len);
332 return hv_store_ent(hv,keysv,sv,hash);
336 if (lval) { /* gonna assign to this, so it better be there */
338 return hv_store_ent(hv,keysv,sv,hash);
344 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
346 MAGIC *mg = SvMAGIC(hv);
350 if (isUPPER(mg->mg_type)) {
352 switch (mg->mg_type) {
355 *needs_store = FALSE;
358 mg = mg->mg_moremagic;
365 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
366 the length of the key. The C<hash> parameter is the precomputed hash
367 value; if it is zero then Perl will compute it. The return value will be
368 NULL if the operation failed or if the value did not need to be actually
369 stored within the hash (as in the case of tied hashes). Otherwise it can
370 be dereferenced to get the original C<SV*>. Note that the caller is
371 responsible for suitably incrementing the reference count of C<val> before
372 the call, and decrementing it if the function returned NULL.
374 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
375 information on how to use this function on tied hashes.
381 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
386 register HE **oentry;
387 bool is_utf8 = FALSE;
397 xhv = (XPVHV*)SvANY(hv);
401 hv_magic_check (hv, &needs_copy, &needs_store);
403 mg_copy((SV*)hv, val, key, klen);
404 if (!xhv->xhv_array && !needs_store)
406 #ifdef ENV_IS_CASELESS
407 else if (mg_find((SV*)hv,'E')) {
408 SV *sv = sv_2mortal(newSVpvn(key,klen));
409 key = strupr(SvPVX(sv));
416 PERL_HASH(hash, key, klen);
419 Newz(505, xhv->xhv_array,
420 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
422 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
425 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
426 if (HeHASH(entry) != hash) /* strings can't be equal */
428 if (HeKLEN(entry) != klen)
430 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
432 if (HeKUTF8(entry) != (char)is_utf8)
434 SvREFCNT_dec(HeVAL(entry));
436 return &HeVAL(entry);
441 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
442 else /* gotta do the real thing */
443 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
445 HeNEXT(entry) = *oentry;
449 if (i) { /* initial entry? */
451 if (xhv->xhv_keys > xhv->xhv_max)
455 return &HeVAL(entry);
459 =for apidoc hv_store_ent
461 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
462 parameter is the precomputed hash value; if it is zero then Perl will
463 compute it. The return value is the new hash entry so created. It will be
464 NULL if the operation failed or if the value did not need to be actually
465 stored within the hash (as in the case of tied hashes). Otherwise the
466 contents of the return value can be accessed using the C<He???> macros
467 described here. Note that the caller is responsible for suitably
468 incrementing the reference count of C<val> before the call, and
469 decrementing it if the function returned NULL.
471 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
472 information on how to use this function on tied hashes.
478 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
485 register HE **oentry;
491 xhv = (XPVHV*)SvANY(hv);
495 hv_magic_check (hv, &needs_copy, &needs_store);
497 bool save_taint = PL_tainted;
499 PL_tainted = SvTAINTED(keysv);
500 keysv = sv_2mortal(newSVsv(keysv));
501 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
502 TAINT_IF(save_taint);
503 if (!xhv->xhv_array && !needs_store)
505 #ifdef ENV_IS_CASELESS
506 else if (mg_find((SV*)hv,'E')) {
507 key = SvPV(keysv, klen);
508 keysv = sv_2mortal(newSVpvn(key,klen));
509 (void)strupr(SvPVX(keysv));
516 key = SvPV(keysv, klen);
517 is_utf8 = (SvUTF8(keysv) != 0);
520 PERL_HASH(hash, key, klen);
523 Newz(505, xhv->xhv_array,
524 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
526 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
529 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
530 if (HeHASH(entry) != hash) /* strings can't be equal */
532 if (HeKLEN(entry) != klen)
534 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
536 if (HeKUTF8(entry) != (char)is_utf8)
538 SvREFCNT_dec(HeVAL(entry));
545 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
546 else /* gotta do the real thing */
547 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
549 HeNEXT(entry) = *oentry;
553 if (i) { /* initial entry? */
555 if (xhv->xhv_keys > xhv->xhv_max)
563 =for apidoc hv_delete
565 Deletes a key/value pair in the hash. The value SV is removed from the
566 hash and returned to the caller. The C<klen> is the length of the key.
567 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
574 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
580 register HE **oentry;
583 bool is_utf8 = FALSE;
591 if (SvRMAGICAL(hv)) {
594 hv_magic_check (hv, &needs_copy, &needs_store);
596 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
600 if (mg_find(sv, 'p')) {
601 sv_unmagic(sv, 'p'); /* No longer an element */
604 return Nullsv; /* element cannot be deleted */
606 #ifdef ENV_IS_CASELESS
607 else if (mg_find((SV*)hv,'E')) {
608 sv = sv_2mortal(newSVpvn(key,klen));
609 key = strupr(SvPVX(sv));
614 xhv = (XPVHV*)SvANY(hv);
618 PERL_HASH(hash, key, klen);
620 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
623 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
624 if (HeHASH(entry) != hash) /* strings can't be equal */
626 if (HeKLEN(entry) != klen)
628 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
630 if (HeKUTF8(entry) != (char)is_utf8)
632 *oentry = HeNEXT(entry);
635 if (flags & G_DISCARD)
638 sv = sv_2mortal(HeVAL(entry));
639 HeVAL(entry) = &PL_sv_undef;
641 if (entry == xhv->xhv_eiter)
644 hv_free_ent(hv, entry);
652 =for apidoc hv_delete_ent
654 Deletes a key/value pair in the hash. The value SV is removed from the
655 hash and returned to the caller. The C<flags> value will normally be zero;
656 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
657 precomputed hash value, or 0 to ask for it to be computed.
663 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
670 register HE **oentry;
676 if (SvRMAGICAL(hv)) {
679 hv_magic_check (hv, &needs_copy, &needs_store);
681 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
685 if (mg_find(sv, 'p')) {
686 sv_unmagic(sv, 'p'); /* No longer an element */
689 return Nullsv; /* element cannot be deleted */
691 #ifdef ENV_IS_CASELESS
692 else if (mg_find((SV*)hv,'E')) {
693 key = SvPV(keysv, klen);
694 keysv = sv_2mortal(newSVpvn(key,klen));
695 (void)strupr(SvPVX(keysv));
701 xhv = (XPVHV*)SvANY(hv);
705 key = SvPV(keysv, klen);
706 is_utf8 = (SvUTF8(keysv) != 0);
709 PERL_HASH(hash, key, klen);
711 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
714 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
715 if (HeHASH(entry) != hash) /* strings can't be equal */
717 if (HeKLEN(entry) != klen)
719 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
721 if (HeKUTF8(entry) != (char)is_utf8)
723 *oentry = HeNEXT(entry);
726 if (flags & G_DISCARD)
729 sv = sv_2mortal(HeVAL(entry));
730 HeVAL(entry) = &PL_sv_undef;
732 if (entry == xhv->xhv_eiter)
735 hv_free_ent(hv, entry);
743 =for apidoc hv_exists
745 Returns a boolean indicating whether the specified hash key exists. The
746 C<klen> is the length of the key.
752 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
758 bool is_utf8 = FALSE;
768 if (SvRMAGICAL(hv)) {
769 if (mg_find((SV*)hv,'P')) {
771 mg_copy((SV*)hv, sv, key, klen);
772 magic_existspack(sv, mg_find(sv, 'p'));
775 #ifdef ENV_IS_CASELESS
776 else if (mg_find((SV*)hv,'E')) {
777 sv = sv_2mortal(newSVpvn(key,klen));
778 key = strupr(SvPVX(sv));
783 xhv = (XPVHV*)SvANY(hv);
784 #ifndef DYNAMIC_ENV_FETCH
789 PERL_HASH(hash, key, klen);
791 #ifdef DYNAMIC_ENV_FETCH
792 if (!xhv->xhv_array) entry = Null(HE*);
795 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
796 for (; entry; entry = HeNEXT(entry)) {
797 if (HeHASH(entry) != hash) /* strings can't be equal */
799 if (HeKLEN(entry) != klen)
801 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
803 if (HeKUTF8(entry) != (char)is_utf8)
807 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
808 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
810 char *env = PerlEnv_ENVgetenv_len(key,&len);
812 sv = newSVpvn(env,len);
814 (void)hv_store(hv,key,klen,sv,hash);
824 =for apidoc hv_exists_ent
826 Returns a boolean indicating whether the specified hash key exists. C<hash>
827 can be a valid precomputed hash value, or 0 to ask for it to be
834 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
846 if (SvRMAGICAL(hv)) {
847 if (mg_find((SV*)hv,'P')) {
849 keysv = sv_2mortal(newSVsv(keysv));
850 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
851 magic_existspack(sv, mg_find(sv, 'p'));
854 #ifdef ENV_IS_CASELESS
855 else if (mg_find((SV*)hv,'E')) {
856 key = SvPV(keysv, klen);
857 keysv = sv_2mortal(newSVpvn(key,klen));
858 (void)strupr(SvPVX(keysv));
864 xhv = (XPVHV*)SvANY(hv);
865 #ifndef DYNAMIC_ENV_FETCH
870 key = SvPV(keysv, klen);
871 is_utf8 = (SvUTF8(keysv) != 0);
873 PERL_HASH(hash, key, klen);
875 #ifdef DYNAMIC_ENV_FETCH
876 if (!xhv->xhv_array) entry = Null(HE*);
879 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
880 for (; entry; entry = HeNEXT(entry)) {
881 if (HeHASH(entry) != hash) /* strings can't be equal */
883 if (HeKLEN(entry) != klen)
885 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
887 if (HeKUTF8(entry) != (char)is_utf8)
891 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
892 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
894 char *env = PerlEnv_ENVgetenv_len(key,&len);
896 sv = newSVpvn(env,len);
898 (void)hv_store_ent(hv,keysv,sv,hash);
907 S_hsplit(pTHX_ HV *hv)
909 register XPVHV* xhv = (XPVHV*)SvANY(hv);
910 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
911 register I32 newsize = oldsize * 2;
913 register char *a = xhv->xhv_array;
917 register HE **oentry;
920 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
921 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
927 #define MALLOC_OVERHEAD 16
928 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
933 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
935 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
938 Safefree(xhv->xhv_array);
942 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
943 xhv->xhv_max = --newsize;
947 for (i=0; i<oldsize; i++,aep++) {
948 if (!*aep) /* non-existent */
951 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
952 if ((HeHASH(entry) & newsize) != i) {
953 *oentry = HeNEXT(entry);
954 HeNEXT(entry) = *bep;
961 oentry = &HeNEXT(entry);
963 if (!*aep) /* everything moved */
969 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
971 register XPVHV* xhv = (XPVHV*)SvANY(hv);
972 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
973 register I32 newsize;
979 register HE **oentry;
981 newsize = (I32) newmax; /* possible truncation here */
982 if (newsize != newmax || newmax <= oldsize)
984 while ((newsize & (1 + ~newsize)) != newsize) {
985 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
987 if (newsize < newmax)
989 if (newsize < newmax)
990 return; /* overflow detection */
995 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
996 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1002 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1007 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
1008 if (oldsize >= 64) {
1009 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1012 Safefree(xhv->xhv_array);
1015 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1018 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1020 xhv->xhv_max = --newsize;
1022 if (!xhv->xhv_fill) /* skip rest if no entries */
1026 for (i=0; i<oldsize; i++,aep++) {
1027 if (!*aep) /* non-existent */
1029 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1030 if ((j = (HeHASH(entry) & newsize)) != i) {
1032 *oentry = HeNEXT(entry);
1033 if (!(HeNEXT(entry) = aep[j]))
1039 oentry = &HeNEXT(entry);
1041 if (!*aep) /* everything moved */
1049 Creates a new HV. The reference count is set to 1.
1058 register XPVHV* xhv;
1060 hv = (HV*)NEWSV(502,0);
1061 sv_upgrade((SV *)hv, SVt_PVHV);
1062 xhv = (XPVHV*)SvANY(hv);
1065 #ifndef NODEFAULT_SHAREKEYS
1066 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1068 xhv->xhv_max = 7; /* start with 8 buckets */
1070 xhv->xhv_pmroot = 0;
1071 (void)hv_iterinit(hv); /* so each() will start off right */
1076 Perl_newHVhv(pTHX_ HV *ohv)
1079 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1080 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1083 while (hv_max && hv_max + 1 >= hv_fill * 2)
1084 hv_max = hv_max / 2; /* Is always 2^n-1 */
1090 if (! SvTIED_mg((SV*)ohv, 'P')) {
1097 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1098 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1102 while ((entry = hv_iternext(ohv))) {
1103 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1104 newSVsv(HeVAL(entry)), HeHASH(entry));
1106 HvRITER(ohv) = hv_riter;
1107 HvEITER(ohv) = hv_eiter;
1114 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1121 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1122 PL_sub_generation++; /* may be deletion of method from stash */
1124 if (HeKLEN(entry) == HEf_SVKEY) {
1125 SvREFCNT_dec(HeKEY_sv(entry));
1126 Safefree(HeKEY_hek(entry));
1128 else if (HvSHAREKEYS(hv))
1129 unshare_hek(HeKEY_hek(entry));
1131 Safefree(HeKEY_hek(entry));
1136 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1140 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1141 PL_sub_generation++; /* may be deletion of method from stash */
1142 sv_2mortal(HeVAL(entry)); /* free between statements */
1143 if (HeKLEN(entry) == HEf_SVKEY) {
1144 sv_2mortal(HeKEY_sv(entry));
1145 Safefree(HeKEY_hek(entry));
1147 else if (HvSHAREKEYS(hv))
1148 unshare_hek(HeKEY_hek(entry));
1150 Safefree(HeKEY_hek(entry));
1155 =for apidoc hv_clear
1157 Clears a hash, making it empty.
1163 Perl_hv_clear(pTHX_ HV *hv)
1165 register XPVHV* xhv;
1168 xhv = (XPVHV*)SvANY(hv);
1173 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
1180 S_hfreeentries(pTHX_ HV *hv)
1182 register HE **array;
1184 register HE *oentry = Null(HE*);
1195 array = HvARRAY(hv);
1200 entry = HeNEXT(entry);
1201 hv_free_ent(hv, oentry);
1206 entry = array[riter];
1209 (void)hv_iterinit(hv);
1213 =for apidoc hv_undef
1221 Perl_hv_undef(pTHX_ HV *hv)
1223 register XPVHV* xhv;
1226 xhv = (XPVHV*)SvANY(hv);
1228 Safefree(xhv->xhv_array);
1230 Safefree(HvNAME(hv));
1234 xhv->xhv_max = 7; /* it's a normal hash */
1243 =for apidoc hv_iterinit
1245 Prepares a starting point to traverse a hash table. Returns the number of
1246 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1247 currently only meaningful for hashes without tie magic.
1249 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1250 hash buckets that happen to be in use. If you still need that esoteric
1251 value, you can get it through the macro C<HvFILL(tb)>.
1257 Perl_hv_iterinit(pTHX_ HV *hv)
1259 register XPVHV* xhv;
1263 Perl_croak(aTHX_ "Bad hash");
1264 xhv = (XPVHV*)SvANY(hv);
1265 entry = xhv->xhv_eiter;
1266 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1268 hv_free_ent(hv, entry);
1270 xhv->xhv_riter = -1;
1271 xhv->xhv_eiter = Null(HE*);
1272 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1276 =for apidoc hv_iternext
1278 Returns entries from a hash iterator. See C<hv_iterinit>.
1284 Perl_hv_iternext(pTHX_ HV *hv)
1286 register XPVHV* xhv;
1292 Perl_croak(aTHX_ "Bad hash");
1293 xhv = (XPVHV*)SvANY(hv);
1294 oldentry = entry = xhv->xhv_eiter;
1296 if ((mg = SvTIED_mg((SV*)hv, 'P'))) {
1297 SV *key = sv_newmortal();
1299 sv_setsv(key, HeSVKEY_force(entry));
1300 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1306 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
1308 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1310 HeKEY_hek(entry) = hek;
1311 HeKLEN(entry) = HEf_SVKEY;
1313 magic_nextpack((SV*) hv,mg,key);
1315 /* force key to stay around until next time */
1316 HeSVKEY_set(entry, SvREFCNT_inc(key));
1317 return entry; /* beware, hent_val is not set */
1320 SvREFCNT_dec(HeVAL(entry));
1321 Safefree(HeKEY_hek(entry));
1323 xhv->xhv_eiter = Null(HE*);
1326 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1327 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1331 if (!xhv->xhv_array)
1332 Newz(506, xhv->xhv_array,
1333 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1335 entry = HeNEXT(entry);
1338 if (xhv->xhv_riter > xhv->xhv_max) {
1339 xhv->xhv_riter = -1;
1342 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1345 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1347 hv_free_ent(hv, oldentry);
1350 xhv->xhv_eiter = entry;
1355 =for apidoc hv_iterkey
1357 Returns the key from the current position of the hash iterator. See
1364 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1366 if (HeKLEN(entry) == HEf_SVKEY) {
1368 char *p = SvPV(HeKEY_sv(entry), len);
1373 *retlen = HeKLEN(entry);
1374 return HeKEY(entry);
1378 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1380 =for apidoc hv_iterkeysv
1382 Returns the key as an C<SV*> from the current position of the hash
1383 iterator. The return value will always be a mortal copy of the key. Also
1390 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1392 if (HeKLEN(entry) == HEf_SVKEY)
1393 return sv_mortalcopy(HeKEY_sv(entry));
1395 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1396 HeKLEN_UTF8(entry), HeHASH(entry)));
1400 =for apidoc hv_iterval
1402 Returns the value from the current position of the hash iterator. See
1409 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1411 if (SvRMAGICAL(hv)) {
1412 if (mg_find((SV*)hv,'P')) {
1413 SV* sv = sv_newmortal();
1414 if (HeKLEN(entry) == HEf_SVKEY)
1415 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1416 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1420 return HeVAL(entry);
1424 =for apidoc hv_iternextsv
1426 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1433 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1436 if ( (he = hv_iternext(hv)) == NULL)
1438 *key = hv_iterkey(he, retlen);
1439 return hv_iterval(hv, he);
1443 =for apidoc hv_magic
1445 Adds magic to a hash. See C<sv_magic>.
1451 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1453 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1457 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1459 return HEK_KEY(share_hek(sv, len, hash));
1462 /* possibly free a shared string if no one has access to it
1463 * len and hash must both be valid for str.
1466 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1468 register XPVHV* xhv;
1470 register HE **oentry;
1473 bool is_utf8 = FALSE;
1480 /* what follows is the moral equivalent of:
1481 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1482 if (--*Svp == Nullsv)
1483 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1485 xhv = (XPVHV*)SvANY(PL_strtab);
1486 /* assert(xhv_array != 0) */
1488 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1489 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1490 if (HeHASH(entry) != hash) /* strings can't be equal */
1492 if (HeKLEN(entry) != len)
1494 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1496 if (HeKUTF8(entry) != (char)is_utf8)
1499 if (--HeVAL(entry) == Nullsv) {
1500 *oentry = HeNEXT(entry);
1503 Safefree(HeKEY_hek(entry));
1509 UNLOCK_STRTAB_MUTEX;
1511 if (!found && ckWARN_d(WARN_INTERNAL))
1512 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1515 /* get a (constant) string ptr from the global string table
1516 * string will get added if it is not already there.
1517 * len and hash must both be valid for str.
1520 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1522 register XPVHV* xhv;
1524 register HE **oentry;
1527 bool is_utf8 = FALSE;
1534 /* what follows is the moral equivalent of:
1536 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1537 hv_store(PL_strtab, str, len, Nullsv, hash);
1539 xhv = (XPVHV*)SvANY(PL_strtab);
1540 /* assert(xhv_array != 0) */
1542 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1543 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1544 if (HeHASH(entry) != hash) /* strings can't be equal */
1546 if (HeKLEN(entry) != len)
1548 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1550 if (HeKUTF8(entry) != (char)is_utf8)
1557 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1558 HeVAL(entry) = Nullsv;
1559 HeNEXT(entry) = *oentry;
1562 if (i) { /* initial entry? */
1564 if (xhv->xhv_keys > xhv->xhv_max)
1569 ++HeVAL(entry); /* use value slot as REFCNT */
1570 UNLOCK_STRTAB_MUTEX;
1571 return HeKEY_hek(entry);