3 * Copyright (c) 1991-2000, 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
26 PL_he_root = HeNEXT(he);
35 HeNEXT(p) = (HE*)PL_he_root;
45 New(54, PL_he_root, 1008/sizeof(HE), HE);
47 heend = &he[1008 / sizeof(HE) - 1];
49 HeNEXT(he) = (HE*)(he + 1);
57 #define new_HE() (HE*)safemalloc(sizeof(HE))
58 #define del_HE(p) safefree((char*)p)
62 #define new_HE() new_he()
63 #define del_HE(p) del_he(p)
68 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
73 New(54, k, HEK_BASESIZE + len + 1, char);
75 Copy(str, HEK_KEY(hek), len, char);
76 *(HEK_KEY(hek) + len) = '\0';
83 Perl_unshare_hek(pTHX_ HEK *hek)
85 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
88 #if defined(USE_ITHREADS)
90 Perl_he_dup(pTHX_ HE *e, bool shared)
96 /* look for it in the table first */
97 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
101 /* create anew and remember what it is */
103 ptr_table_store(PL_ptr_table, e, ret);
105 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
106 if (HeKLEN(e) == HEf_SVKEY)
107 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
109 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
111 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
112 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
115 #endif /* USE_ITHREADS */
117 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
123 Returns the SV which corresponds to the specified key in the hash. The
124 C<klen> is the length of the key. If C<lval> is set then the fetch will be
125 part of a store. Check that the return value is non-null before
126 dereferencing it to a C<SV*>.
128 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
129 information on how to use this function on tied hashes.
135 Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
145 if (SvRMAGICAL(hv)) {
146 if (mg_find((SV*)hv,'P')) {
149 mg_copy((SV*)hv, sv, key, klen);
151 return &PL_hv_fetch_sv;
153 #ifdef ENV_IS_CASELESS
154 else if (mg_find((SV*)hv,'E')) {
156 for (i = 0; i < klen; ++i)
157 if (isLOWER(key[i])) {
158 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
159 SV **ret = hv_fetch(hv, nkey, klen, 0);
161 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
168 xhv = (XPVHV*)SvANY(hv);
169 if (!xhv->xhv_array) {
171 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
172 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
175 Newz(503, xhv->xhv_array,
176 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
181 PERL_HASH(hash, key, klen);
183 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
184 for (; entry; entry = HeNEXT(entry)) {
185 if (HeHASH(entry) != hash) /* strings can't be equal */
187 if (HeKLEN(entry) != klen)
189 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
191 return &HeVAL(entry);
193 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
194 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
196 char *env = PerlEnv_ENVgetenv_len(key,&len);
198 sv = newSVpvn(env,len);
200 return hv_store(hv,key,klen,sv,hash);
204 if (lval) { /* gonna assign to this, so it better be there */
206 return hv_store(hv,key,klen,sv,hash);
211 /* returns a HE * structure with the all fields set */
212 /* note that hent_val will be a mortal sv for MAGICAL hashes */
214 =for apidoc hv_fetch_ent
216 Returns the hash entry which corresponds to the specified key in the hash.
217 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
218 if you want the function to compute it. IF C<lval> is set then the fetch
219 will be part of a store. Make sure the return value is non-null before
220 accessing it. The return value when C<tb> is a tied hash is a pointer to a
221 static location, so be sure to make a copy of the structure if you need to
224 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
225 information on how to use this function on tied hashes.
231 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
242 if (SvRMAGICAL(hv)) {
243 if (mg_find((SV*)hv,'P')) {
246 keysv = sv_2mortal(newSVsv(keysv));
247 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
248 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
250 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
251 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
253 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
254 HeVAL(&PL_hv_fetch_ent_mh) = sv;
255 return &PL_hv_fetch_ent_mh;
257 #ifdef ENV_IS_CASELESS
258 else if (mg_find((SV*)hv,'E')) {
260 key = SvPV(keysv, klen);
261 for (i = 0; i < klen; ++i)
262 if (isLOWER(key[i])) {
263 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
264 (void)strupr(SvPVX(nkeysv));
265 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
267 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
274 xhv = (XPVHV*)SvANY(hv);
275 if (!xhv->xhv_array) {
277 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
278 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
281 Newz(503, xhv->xhv_array,
282 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
287 key = SvPV(keysv, klen);
290 PERL_HASH(hash, key, klen);
292 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
293 for (; entry; entry = HeNEXT(entry)) {
294 if (HeHASH(entry) != hash) /* strings can't be equal */
296 if (HeKLEN(entry) != klen)
298 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
302 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
303 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
305 char *env = PerlEnv_ENVgetenv_len(key,&len);
307 sv = newSVpvn(env,len);
309 return hv_store_ent(hv,keysv,sv,hash);
313 if (lval) { /* gonna assign to this, so it better be there */
315 return hv_store_ent(hv,keysv,sv,hash);
321 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
323 MAGIC *mg = SvMAGIC(hv);
327 if (isUPPER(mg->mg_type)) {
329 switch (mg->mg_type) {
332 *needs_store = FALSE;
335 mg = mg->mg_moremagic;
342 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
343 the length of the key. The C<hash> parameter is the precomputed hash
344 value; if it is zero then Perl will compute it. The return value will be
345 NULL if the operation failed or if the value did not need to be actually
346 stored within the hash (as in the case of tied hashes). Otherwise it can
347 be dereferenced to get the original C<SV*>. Note that the caller is
348 responsible for suitably incrementing the reference count of C<val> before
349 the call, and decrementing it if the function returned NULL.
351 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
352 information on how to use this function on tied hashes.
358 Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
363 register HE **oentry;
368 xhv = (XPVHV*)SvANY(hv);
372 hv_magic_check (hv, &needs_copy, &needs_store);
374 mg_copy((SV*)hv, val, key, klen);
375 if (!xhv->xhv_array && !needs_store)
377 #ifdef ENV_IS_CASELESS
378 else if (mg_find((SV*)hv,'E')) {
379 SV *sv = sv_2mortal(newSVpvn(key,klen));
380 key = strupr(SvPVX(sv));
387 PERL_HASH(hash, key, klen);
390 Newz(505, xhv->xhv_array,
391 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
393 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
396 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
397 if (HeHASH(entry) != hash) /* strings can't be equal */
399 if (HeKLEN(entry) != klen)
401 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
403 SvREFCNT_dec(HeVAL(entry));
405 return &HeVAL(entry);
410 HeKEY_hek(entry) = share_hek(key, klen, hash);
411 else /* gotta do the real thing */
412 HeKEY_hek(entry) = save_hek(key, klen, hash);
414 HeNEXT(entry) = *oentry;
418 if (i) { /* initial entry? */
420 if (xhv->xhv_keys > xhv->xhv_max)
424 return &HeVAL(entry);
428 =for apidoc hv_store_ent
430 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
431 parameter is the precomputed hash value; if it is zero then Perl will
432 compute it. The return value is the new hash entry so created. It will be
433 NULL if the operation failed or if the value did not need to be actually
434 stored within the hash (as in the case of tied hashes). Otherwise the
435 contents of the return value can be accessed using the C<He???> macros
436 described here. Note that the caller is responsible for suitably
437 incrementing the reference count of C<val> before the call, and
438 decrementing it if the function returned NULL.
440 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
441 information on how to use this function on tied hashes.
447 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
454 register HE **oentry;
459 xhv = (XPVHV*)SvANY(hv);
464 hv_magic_check (hv, &needs_copy, &needs_store);
466 bool save_taint = PL_tainted;
468 PL_tainted = SvTAINTED(keysv);
469 keysv = sv_2mortal(newSVsv(keysv));
470 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
471 TAINT_IF(save_taint);
472 if (!xhv->xhv_array && !needs_store)
474 #ifdef ENV_IS_CASELESS
475 else if (mg_find((SV*)hv,'E')) {
476 key = SvPV(keysv, klen);
477 keysv = sv_2mortal(newSVpvn(key,klen));
478 (void)strupr(SvPVX(keysv));
485 key = SvPV(keysv, klen);
488 PERL_HASH(hash, key, klen);
491 Newz(505, xhv->xhv_array,
492 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
494 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
497 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
498 if (HeHASH(entry) != hash) /* strings can't be equal */
500 if (HeKLEN(entry) != klen)
502 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
504 SvREFCNT_dec(HeVAL(entry));
511 HeKEY_hek(entry) = share_hek(key, klen, hash);
512 else /* gotta do the real thing */
513 HeKEY_hek(entry) = save_hek(key, klen, hash);
515 HeNEXT(entry) = *oentry;
519 if (i) { /* initial entry? */
521 if (xhv->xhv_keys > xhv->xhv_max)
529 =for apidoc hv_delete
531 Deletes a key/value pair in the hash. The value SV is removed from the
532 hash and returned to the caller. The C<klen> is the length of the key.
533 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
540 Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
546 register HE **oentry;
552 if (SvRMAGICAL(hv)) {
555 hv_magic_check (hv, &needs_copy, &needs_store);
557 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
561 if (mg_find(sv, 'p')) {
562 sv_unmagic(sv, 'p'); /* No longer an element */
565 return Nullsv; /* element cannot be deleted */
567 #ifdef ENV_IS_CASELESS
568 else if (mg_find((SV*)hv,'E')) {
569 sv = sv_2mortal(newSVpvn(key,klen));
570 key = strupr(SvPVX(sv));
575 xhv = (XPVHV*)SvANY(hv);
579 PERL_HASH(hash, key, klen);
581 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
584 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
585 if (HeHASH(entry) != hash) /* strings can't be equal */
587 if (HeKLEN(entry) != klen)
589 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
591 *oentry = HeNEXT(entry);
594 if (flags & G_DISCARD)
597 sv = sv_2mortal(HeVAL(entry));
598 HeVAL(entry) = &PL_sv_undef;
600 if (entry == xhv->xhv_eiter)
603 hv_free_ent(hv, entry);
611 =for apidoc hv_delete_ent
613 Deletes a key/value pair in the hash. The value SV is removed from the
614 hash and returned to the caller. The C<flags> value will normally be zero;
615 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
616 precomputed hash value, or 0 to ask for it to be computed.
622 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
629 register HE **oentry;
634 if (SvRMAGICAL(hv)) {
637 hv_magic_check (hv, &needs_copy, &needs_store);
639 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
643 if (mg_find(sv, 'p')) {
644 sv_unmagic(sv, 'p'); /* No longer an element */
647 return Nullsv; /* element cannot be deleted */
649 #ifdef ENV_IS_CASELESS
650 else if (mg_find((SV*)hv,'E')) {
651 key = SvPV(keysv, klen);
652 keysv = sv_2mortal(newSVpvn(key,klen));
653 (void)strupr(SvPVX(keysv));
659 xhv = (XPVHV*)SvANY(hv);
663 key = SvPV(keysv, klen);
666 PERL_HASH(hash, key, klen);
668 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
671 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
672 if (HeHASH(entry) != hash) /* strings can't be equal */
674 if (HeKLEN(entry) != klen)
676 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
678 *oentry = HeNEXT(entry);
681 if (flags & G_DISCARD)
684 sv = sv_2mortal(HeVAL(entry));
685 HeVAL(entry) = &PL_sv_undef;
687 if (entry == xhv->xhv_eiter)
690 hv_free_ent(hv, entry);
698 =for apidoc hv_exists
700 Returns a boolean indicating whether the specified hash key exists. The
701 C<klen> is the length of the key.
707 Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
717 if (SvRMAGICAL(hv)) {
718 if (mg_find((SV*)hv,'P')) {
721 mg_copy((SV*)hv, sv, key, klen);
722 magic_existspack(sv, mg_find(sv, 'p'));
725 #ifdef ENV_IS_CASELESS
726 else if (mg_find((SV*)hv,'E')) {
727 sv = sv_2mortal(newSVpvn(key,klen));
728 key = strupr(SvPVX(sv));
733 xhv = (XPVHV*)SvANY(hv);
734 #ifndef DYNAMIC_ENV_FETCH
739 PERL_HASH(hash, key, klen);
741 #ifdef DYNAMIC_ENV_FETCH
742 if (!xhv->xhv_array) entry = Null(HE*);
745 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
746 for (; entry; entry = HeNEXT(entry)) {
747 if (HeHASH(entry) != hash) /* strings can't be equal */
749 if (HeKLEN(entry) != klen)
751 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
755 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
756 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
758 char *env = PerlEnv_ENVgetenv_len(key,&len);
760 sv = newSVpvn(env,len);
762 (void)hv_store(hv,key,klen,sv,hash);
772 =for apidoc hv_exists_ent
774 Returns a boolean indicating whether the specified hash key exists. C<hash>
775 can be a valid precomputed hash value, or 0 to ask for it to be
782 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
793 if (SvRMAGICAL(hv)) {
794 if (mg_find((SV*)hv,'P')) {
795 dTHR; /* just for SvTRUE */
797 keysv = sv_2mortal(newSVsv(keysv));
798 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
799 magic_existspack(sv, mg_find(sv, 'p'));
802 #ifdef ENV_IS_CASELESS
803 else if (mg_find((SV*)hv,'E')) {
804 key = SvPV(keysv, klen);
805 keysv = sv_2mortal(newSVpvn(key,klen));
806 (void)strupr(SvPVX(keysv));
812 xhv = (XPVHV*)SvANY(hv);
813 #ifndef DYNAMIC_ENV_FETCH
818 key = SvPV(keysv, klen);
820 PERL_HASH(hash, key, klen);
822 #ifdef DYNAMIC_ENV_FETCH
823 if (!xhv->xhv_array) entry = Null(HE*);
826 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
827 for (; entry; entry = HeNEXT(entry)) {
828 if (HeHASH(entry) != hash) /* strings can't be equal */
830 if (HeKLEN(entry) != klen)
832 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
836 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
837 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
839 char *env = PerlEnv_ENVgetenv_len(key,&len);
841 sv = newSVpvn(env,len);
843 (void)hv_store_ent(hv,keysv,sv,hash);
852 S_hsplit(pTHX_ HV *hv)
854 register XPVHV* xhv = (XPVHV*)SvANY(hv);
855 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
856 register I32 newsize = oldsize * 2;
858 register char *a = xhv->xhv_array;
862 register HE **oentry;
865 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
866 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
872 #define MALLOC_OVERHEAD 16
873 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
878 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
880 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
883 Safefree(xhv->xhv_array);
887 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
888 xhv->xhv_max = --newsize;
892 for (i=0; i<oldsize; i++,aep++) {
893 if (!*aep) /* non-existent */
896 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
897 if ((HeHASH(entry) & newsize) != i) {
898 *oentry = HeNEXT(entry);
899 HeNEXT(entry) = *bep;
906 oentry = &HeNEXT(entry);
908 if (!*aep) /* everything moved */
914 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
916 register XPVHV* xhv = (XPVHV*)SvANY(hv);
917 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
918 register I32 newsize;
924 register HE **oentry;
926 newsize = (I32) newmax; /* possible truncation here */
927 if (newsize != newmax || newmax <= oldsize)
929 while ((newsize & (1 + ~newsize)) != newsize) {
930 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
932 if (newsize < newmax)
934 if (newsize < newmax)
935 return; /* overflow detection */
940 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
941 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
947 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
952 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
954 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
957 Safefree(xhv->xhv_array);
960 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
963 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
965 xhv->xhv_max = --newsize;
967 if (!xhv->xhv_fill) /* skip rest if no entries */
971 for (i=0; i<oldsize; i++,aep++) {
972 if (!*aep) /* non-existent */
974 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
975 if ((j = (HeHASH(entry) & newsize)) != i) {
977 *oentry = HeNEXT(entry);
978 if (!(HeNEXT(entry) = aep[j]))
984 oentry = &HeNEXT(entry);
986 if (!*aep) /* everything moved */
994 Creates a new HV. The reference count is set to 1.
1003 register XPVHV* xhv;
1005 hv = (HV*)NEWSV(502,0);
1006 sv_upgrade((SV *)hv, SVt_PVHV);
1007 xhv = (XPVHV*)SvANY(hv);
1010 #ifndef NODEFAULT_SHAREKEYS
1011 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1013 xhv->xhv_max = 7; /* start with 8 buckets */
1015 xhv->xhv_pmroot = 0;
1016 (void)hv_iterinit(hv); /* so each() will start off right */
1021 Perl_newHVhv(pTHX_ HV *ohv)
1024 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1025 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1028 while (hv_max && hv_max + 1 >= hv_fill * 2)
1029 hv_max = hv_max / 2; /* Is always 2^n-1 */
1035 if (! SvTIED_mg((SV*)ohv, 'P')) {
1042 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1043 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1047 while (entry = hv_iternext(ohv)) {
1048 hv_store(hv, HeKEY(entry), HeKLEN(entry),
1049 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
1051 HvRITER(ohv) = hv_riter;
1052 HvEITER(ohv) = hv_eiter;
1059 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1066 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1067 PL_sub_generation++; /* may be deletion of method from stash */
1069 if (HeKLEN(entry) == HEf_SVKEY) {
1070 SvREFCNT_dec(HeKEY_sv(entry));
1071 Safefree(HeKEY_hek(entry));
1073 else if (HvSHAREKEYS(hv))
1074 unshare_hek(HeKEY_hek(entry));
1076 Safefree(HeKEY_hek(entry));
1081 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1085 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1086 PL_sub_generation++; /* may be deletion of method from stash */
1087 sv_2mortal(HeVAL(entry)); /* free between statements */
1088 if (HeKLEN(entry) == HEf_SVKEY) {
1089 sv_2mortal(HeKEY_sv(entry));
1090 Safefree(HeKEY_hek(entry));
1092 else if (HvSHAREKEYS(hv))
1093 unshare_hek(HeKEY_hek(entry));
1095 Safefree(HeKEY_hek(entry));
1100 =for apidoc hv_clear
1102 Clears a hash, making it empty.
1108 Perl_hv_clear(pTHX_ HV *hv)
1110 register XPVHV* xhv;
1113 xhv = (XPVHV*)SvANY(hv);
1118 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
1125 S_hfreeentries(pTHX_ HV *hv)
1127 register HE **array;
1129 register HE *oentry = Null(HE*);
1140 array = HvARRAY(hv);
1145 entry = HeNEXT(entry);
1146 hv_free_ent(hv, oentry);
1151 entry = array[riter];
1154 (void)hv_iterinit(hv);
1158 =for apidoc hv_undef
1166 Perl_hv_undef(pTHX_ HV *hv)
1168 register XPVHV* xhv;
1171 xhv = (XPVHV*)SvANY(hv);
1173 Safefree(xhv->xhv_array);
1175 Safefree(HvNAME(hv));
1179 xhv->xhv_max = 7; /* it's a normal hash */
1188 =for apidoc hv_iterinit
1190 Prepares a starting point to traverse a hash table. Returns the number of
1191 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1192 currently only meaningful for hashes without tie magic.
1194 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1195 hash buckets that happen to be in use. If you still need that esoteric
1196 value, you can get it through the macro C<HvFILL(tb)>.
1202 Perl_hv_iterinit(pTHX_ HV *hv)
1204 register XPVHV* xhv;
1208 Perl_croak(aTHX_ "Bad hash");
1209 xhv = (XPVHV*)SvANY(hv);
1210 entry = xhv->xhv_eiter;
1211 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1213 hv_free_ent(hv, entry);
1215 xhv->xhv_riter = -1;
1216 xhv->xhv_eiter = Null(HE*);
1217 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1221 =for apidoc hv_iternext
1223 Returns entries from a hash iterator. See C<hv_iterinit>.
1229 Perl_hv_iternext(pTHX_ HV *hv)
1231 register XPVHV* xhv;
1237 Perl_croak(aTHX_ "Bad hash");
1238 xhv = (XPVHV*)SvANY(hv);
1239 oldentry = entry = xhv->xhv_eiter;
1241 if (mg = SvTIED_mg((SV*)hv, 'P')) {
1242 SV *key = sv_newmortal();
1244 sv_setsv(key, HeSVKEY_force(entry));
1245 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1251 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
1253 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1255 HeKEY_hek(entry) = hek;
1256 HeKLEN(entry) = HEf_SVKEY;
1258 magic_nextpack((SV*) hv,mg,key);
1260 /* force key to stay around until next time */
1261 HeSVKEY_set(entry, SvREFCNT_inc(key));
1262 return entry; /* beware, hent_val is not set */
1265 SvREFCNT_dec(HeVAL(entry));
1266 Safefree(HeKEY_hek(entry));
1268 xhv->xhv_eiter = Null(HE*);
1271 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1272 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1276 if (!xhv->xhv_array)
1277 Newz(506, xhv->xhv_array,
1278 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1280 entry = HeNEXT(entry);
1283 if (xhv->xhv_riter > xhv->xhv_max) {
1284 xhv->xhv_riter = -1;
1287 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1290 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1292 hv_free_ent(hv, oldentry);
1295 xhv->xhv_eiter = entry;
1300 =for apidoc hv_iterkey
1302 Returns the key from the current position of the hash iterator. See
1309 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1311 if (HeKLEN(entry) == HEf_SVKEY) {
1313 char *p = SvPV(HeKEY_sv(entry), len);
1318 *retlen = HeKLEN(entry);
1319 return HeKEY(entry);
1323 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1325 =for apidoc hv_iterkeysv
1327 Returns the key as an C<SV*> from the current position of the hash
1328 iterator. The return value will always be a mortal copy of the key. Also
1335 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1337 if (HeKLEN(entry) == HEf_SVKEY)
1338 return sv_mortalcopy(HeKEY_sv(entry));
1340 return sv_2mortal(newSVpvn((HeKLEN(entry) ? HeKEY(entry) : ""),
1345 =for apidoc hv_iterval
1347 Returns the value from the current position of the hash iterator. See
1354 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1356 if (SvRMAGICAL(hv)) {
1357 if (mg_find((SV*)hv,'P')) {
1358 SV* sv = sv_newmortal();
1359 if (HeKLEN(entry) == HEf_SVKEY)
1360 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1361 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1365 return HeVAL(entry);
1369 =for apidoc hv_iternextsv
1371 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1378 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1381 if ( (he = hv_iternext(hv)) == NULL)
1383 *key = hv_iterkey(he, retlen);
1384 return hv_iterval(hv, he);
1388 =for apidoc hv_magic
1390 Adds magic to a hash. See C<sv_magic>.
1396 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1398 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1402 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1404 return HEK_KEY(share_hek(sv, len, hash));
1407 /* possibly free a shared string if no one has access to it
1408 * len and hash must both be valid for str.
1411 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1413 register XPVHV* xhv;
1415 register HE **oentry;
1419 /* what follows is the moral equivalent of:
1420 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1421 if (--*Svp == Nullsv)
1422 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1424 xhv = (XPVHV*)SvANY(PL_strtab);
1425 /* assert(xhv_array != 0) */
1427 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1428 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1429 if (HeHASH(entry) != hash) /* strings can't be equal */
1431 if (HeKLEN(entry) != len)
1433 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1436 if (--HeVAL(entry) == Nullsv) {
1437 *oentry = HeNEXT(entry);
1440 Safefree(HeKEY_hek(entry));
1446 UNLOCK_STRTAB_MUTEX;
1450 if (!found && ckWARN_d(WARN_INTERNAL))
1451 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string");
1455 /* get a (constant) string ptr from the global string table
1456 * string will get added if it is not already there.
1457 * len and hash must both be valid for str.
1460 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1462 register XPVHV* xhv;
1464 register HE **oentry;
1468 /* what follows is the moral equivalent of:
1470 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1471 hv_store(PL_strtab, str, len, Nullsv, hash);
1473 xhv = (XPVHV*)SvANY(PL_strtab);
1474 /* assert(xhv_array != 0) */
1476 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1477 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1478 if (HeHASH(entry) != hash) /* strings can't be equal */
1480 if (HeKLEN(entry) != len)
1482 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1489 HeKEY_hek(entry) = save_hek(str, len, hash);
1490 HeVAL(entry) = Nullsv;
1491 HeNEXT(entry) = *oentry;
1494 if (i) { /* initial entry? */
1496 if (xhv->xhv_keys > xhv->xhv_max)
1501 ++HeVAL(entry); /* use value slot as REFCNT */
1502 UNLOCK_STRTAB_MUTEX;
1503 return HeKEY_hek(entry);