3 * Copyright (c) 1991-1999, 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);
56 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
61 New(54, k, HEK_BASESIZE + len + 1, char);
63 Copy(str, HEK_KEY(hek), len, char);
64 *(HEK_KEY(hek) + len) = '\0';
71 Perl_unshare_hek(pTHX_ HEK *hek)
73 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
76 #if defined(USE_ITHREADS)
78 Perl_he_dup(pTHX_ HE *e, bool shared)
84 /* look for it in the table first */
85 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
89 /* create anew and remember what it is */
91 ptr_table_store(PL_ptr_table, e, ret);
93 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
94 if (HeKLEN(e) == HEf_SVKEY)
95 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
97 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
99 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
100 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
103 #endif /* USE_ITHREADS */
105 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
111 Returns the SV which corresponds to the specified key in the hash. The
112 C<klen> is the length of the key. If C<lval> is set then the fetch will be
113 part of a store. Check that the return value is non-null before
114 dereferencing it to a C<SV*>.
116 See L<Understanding the Magic of Tied Hashes and Arrays> for more
117 information on how to use this function on tied hashes.
123 Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
133 if (SvRMAGICAL(hv)) {
134 if (mg_find((SV*)hv,'P')) {
137 mg_copy((SV*)hv, sv, key, klen);
139 return &PL_hv_fetch_sv;
141 #ifdef ENV_IS_CASELESS
142 else if (mg_find((SV*)hv,'E')) {
144 for (i = 0; i < klen; ++i)
145 if (isLOWER(key[i])) {
146 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
147 SV **ret = hv_fetch(hv, nkey, klen, 0);
149 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
156 xhv = (XPVHV*)SvANY(hv);
157 if (!xhv->xhv_array) {
159 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
160 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
163 Newz(503, xhv->xhv_array,
164 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
169 PERL_HASH(hash, key, klen);
171 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
172 for (; entry; entry = HeNEXT(entry)) {
173 if (HeHASH(entry) != hash) /* strings can't be equal */
175 if (HeKLEN(entry) != klen)
177 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
179 return &HeVAL(entry);
181 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
182 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
184 char *env = PerlEnv_ENVgetenv_len(key,&len);
186 sv = newSVpvn(env,len);
188 return hv_store(hv,key,klen,sv,hash);
192 if (lval) { /* gonna assign to this, so it better be there */
194 return hv_store(hv,key,klen,sv,hash);
199 /* returns a HE * structure with the all fields set */
200 /* note that hent_val will be a mortal sv for MAGICAL hashes */
202 =for apidoc hv_fetch_ent
204 Returns the hash entry which corresponds to the specified key in the hash.
205 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
206 if you want the function to compute it. IF C<lval> is set then the fetch
207 will be part of a store. Make sure the return value is non-null before
208 accessing it. The return value when C<tb> is a tied hash is a pointer to a
209 static location, so be sure to make a copy of the structure if you need to
212 See L<Understanding the Magic of Tied Hashes and Arrays> for more
213 information on how to use this function on tied hashes.
219 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
230 if (SvRMAGICAL(hv)) {
231 if (mg_find((SV*)hv,'P')) {
234 keysv = sv_2mortal(newSVsv(keysv));
235 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
236 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
238 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
239 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
241 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
242 HeVAL(&PL_hv_fetch_ent_mh) = sv;
243 return &PL_hv_fetch_ent_mh;
245 #ifdef ENV_IS_CASELESS
246 else if (mg_find((SV*)hv,'E')) {
248 key = SvPV(keysv, klen);
249 for (i = 0; i < klen; ++i)
250 if (isLOWER(key[i])) {
251 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
252 (void)strupr(SvPVX(nkeysv));
253 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
255 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
262 xhv = (XPVHV*)SvANY(hv);
263 if (!xhv->xhv_array) {
265 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
266 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
269 Newz(503, xhv->xhv_array,
270 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
275 key = SvPV(keysv, klen);
278 PERL_HASH(hash, key, klen);
280 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
281 for (; entry; entry = HeNEXT(entry)) {
282 if (HeHASH(entry) != hash) /* strings can't be equal */
284 if (HeKLEN(entry) != klen)
286 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
290 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
291 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
293 char *env = PerlEnv_ENVgetenv_len(key,&len);
295 sv = newSVpvn(env,len);
297 return hv_store_ent(hv,keysv,sv,hash);
301 if (lval) { /* gonna assign to this, so it better be there */
303 return hv_store_ent(hv,keysv,sv,hash);
309 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
311 MAGIC *mg = SvMAGIC(hv);
315 if (isUPPER(mg->mg_type)) {
317 switch (mg->mg_type) {
320 *needs_store = FALSE;
323 mg = mg->mg_moremagic;
330 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
331 the length of the key. The C<hash> parameter is the precomputed hash
332 value; if it is zero then Perl will compute it. The return value will be
333 NULL if the operation failed or if the value did not need to be actually
334 stored within the hash (as in the case of tied hashes). Otherwise it can
335 be dereferenced to get the original C<SV*>. Note that the caller is
336 responsible for suitably incrementing the reference count of C<val> before
337 the call, and decrementing it if the function returned NULL.
339 See L<Understanding the Magic of Tied Hashes and Arrays> for more
340 information on how to use this function on tied hashes.
346 Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
351 register HE **oentry;
356 xhv = (XPVHV*)SvANY(hv);
360 hv_magic_check (hv, &needs_copy, &needs_store);
362 mg_copy((SV*)hv, val, key, klen);
363 if (!xhv->xhv_array && !needs_store)
365 #ifdef ENV_IS_CASELESS
366 else if (mg_find((SV*)hv,'E')) {
367 SV *sv = sv_2mortal(newSVpvn(key,klen));
368 key = strupr(SvPVX(sv));
375 PERL_HASH(hash, key, klen);
378 Newz(505, xhv->xhv_array,
379 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
381 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
384 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
385 if (HeHASH(entry) != hash) /* strings can't be equal */
387 if (HeKLEN(entry) != klen)
389 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
391 SvREFCNT_dec(HeVAL(entry));
393 return &HeVAL(entry);
398 HeKEY_hek(entry) = share_hek(key, klen, hash);
399 else /* gotta do the real thing */
400 HeKEY_hek(entry) = save_hek(key, klen, hash);
402 HeNEXT(entry) = *oentry;
406 if (i) { /* initial entry? */
408 if (xhv->xhv_keys > xhv->xhv_max)
412 return &HeVAL(entry);
416 =for apidoc hv_store_ent
418 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
419 parameter is the precomputed hash value; if it is zero then Perl will
420 compute it. The return value is the new hash entry so created. It will be
421 NULL if the operation failed or if the value did not need to be actually
422 stored within the hash (as in the case of tied hashes). Otherwise the
423 contents of the return value can be accessed using the C<He???> macros
424 described here. Note that the caller is responsible for suitably
425 incrementing the reference count of C<val> before the call, and
426 decrementing it if the function returned NULL.
428 See L<Understanding the Magic of Tied Hashes and Arrays> for more
429 information on how to use this function on tied hashes.
435 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
442 register HE **oentry;
447 xhv = (XPVHV*)SvANY(hv);
452 hv_magic_check (hv, &needs_copy, &needs_store);
454 bool save_taint = PL_tainted;
456 PL_tainted = SvTAINTED(keysv);
457 keysv = sv_2mortal(newSVsv(keysv));
458 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
459 TAINT_IF(save_taint);
460 if (!xhv->xhv_array && !needs_store)
462 #ifdef ENV_IS_CASELESS
463 else if (mg_find((SV*)hv,'E')) {
464 key = SvPV(keysv, klen);
465 keysv = sv_2mortal(newSVpvn(key,klen));
466 (void)strupr(SvPVX(keysv));
473 key = SvPV(keysv, klen);
476 PERL_HASH(hash, key, klen);
479 Newz(505, xhv->xhv_array,
480 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
482 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
485 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
486 if (HeHASH(entry) != hash) /* strings can't be equal */
488 if (HeKLEN(entry) != klen)
490 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
492 SvREFCNT_dec(HeVAL(entry));
499 HeKEY_hek(entry) = share_hek(key, klen, hash);
500 else /* gotta do the real thing */
501 HeKEY_hek(entry) = save_hek(key, klen, hash);
503 HeNEXT(entry) = *oentry;
507 if (i) { /* initial entry? */
509 if (xhv->xhv_keys > xhv->xhv_max)
517 =for apidoc hv_delete
519 Deletes a key/value pair in the hash. The value SV is removed from the
520 hash and returned to the caller. The C<klen> is the length of the key.
521 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
528 Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
534 register HE **oentry;
540 if (SvRMAGICAL(hv)) {
543 hv_magic_check (hv, &needs_copy, &needs_store);
545 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
549 if (mg_find(sv, 'p')) {
550 sv_unmagic(sv, 'p'); /* No longer an element */
553 return Nullsv; /* element cannot be deleted */
555 #ifdef ENV_IS_CASELESS
556 else if (mg_find((SV*)hv,'E')) {
557 sv = sv_2mortal(newSVpvn(key,klen));
558 key = strupr(SvPVX(sv));
563 xhv = (XPVHV*)SvANY(hv);
567 PERL_HASH(hash, key, klen);
569 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
572 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
573 if (HeHASH(entry) != hash) /* strings can't be equal */
575 if (HeKLEN(entry) != klen)
577 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
579 *oentry = HeNEXT(entry);
582 if (flags & G_DISCARD)
585 sv = sv_2mortal(HeVAL(entry));
586 HeVAL(entry) = &PL_sv_undef;
588 if (entry == xhv->xhv_eiter)
591 hv_free_ent(hv, entry);
599 =for apidoc hv_delete_ent
601 Deletes a key/value pair in the hash. The value SV is removed from the
602 hash and returned to the caller. The C<flags> value will normally be zero;
603 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
604 precomputed hash value, or 0 to ask for it to be computed.
610 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
617 register HE **oentry;
622 if (SvRMAGICAL(hv)) {
625 hv_magic_check (hv, &needs_copy, &needs_store);
627 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
631 if (mg_find(sv, 'p')) {
632 sv_unmagic(sv, 'p'); /* No longer an element */
635 return Nullsv; /* element cannot be deleted */
637 #ifdef ENV_IS_CASELESS
638 else if (mg_find((SV*)hv,'E')) {
639 key = SvPV(keysv, klen);
640 keysv = sv_2mortal(newSVpvn(key,klen));
641 (void)strupr(SvPVX(keysv));
647 xhv = (XPVHV*)SvANY(hv);
651 key = SvPV(keysv, klen);
654 PERL_HASH(hash, key, klen);
656 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
659 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
660 if (HeHASH(entry) != hash) /* strings can't be equal */
662 if (HeKLEN(entry) != klen)
664 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
666 *oentry = HeNEXT(entry);
669 if (flags & G_DISCARD)
672 sv = sv_2mortal(HeVAL(entry));
673 HeVAL(entry) = &PL_sv_undef;
675 if (entry == xhv->xhv_eiter)
678 hv_free_ent(hv, entry);
686 =for apidoc hv_exists
688 Returns a boolean indicating whether the specified hash key exists. The
689 C<klen> is the length of the key.
695 Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
705 if (SvRMAGICAL(hv)) {
706 if (mg_find((SV*)hv,'P')) {
709 mg_copy((SV*)hv, sv, key, klen);
710 magic_existspack(sv, mg_find(sv, 'p'));
713 #ifdef ENV_IS_CASELESS
714 else if (mg_find((SV*)hv,'E')) {
715 sv = sv_2mortal(newSVpvn(key,klen));
716 key = strupr(SvPVX(sv));
721 xhv = (XPVHV*)SvANY(hv);
722 #ifndef DYNAMIC_ENV_FETCH
727 PERL_HASH(hash, key, klen);
729 #ifdef DYNAMIC_ENV_FETCH
730 if (!xhv->xhv_array) entry = Null(HE*);
733 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
734 for (; entry; entry = HeNEXT(entry)) {
735 if (HeHASH(entry) != hash) /* strings can't be equal */
737 if (HeKLEN(entry) != klen)
739 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
743 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
744 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
746 char *env = PerlEnv_ENVgetenv_len(key,&len);
748 sv = newSVpvn(env,len);
750 (void)hv_store(hv,key,klen,sv,hash);
760 =for apidoc hv_exists_ent
762 Returns a boolean indicating whether the specified hash key exists. C<hash>
763 can be a valid precomputed hash value, or 0 to ask for it to be
770 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
781 if (SvRMAGICAL(hv)) {
782 if (mg_find((SV*)hv,'P')) {
783 dTHR; /* just for SvTRUE */
785 keysv = sv_2mortal(newSVsv(keysv));
786 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
787 magic_existspack(sv, mg_find(sv, 'p'));
790 #ifdef ENV_IS_CASELESS
791 else if (mg_find((SV*)hv,'E')) {
792 key = SvPV(keysv, klen);
793 keysv = sv_2mortal(newSVpvn(key,klen));
794 (void)strupr(SvPVX(keysv));
800 xhv = (XPVHV*)SvANY(hv);
801 #ifndef DYNAMIC_ENV_FETCH
806 key = SvPV(keysv, klen);
808 PERL_HASH(hash, key, klen);
810 #ifdef DYNAMIC_ENV_FETCH
811 if (!xhv->xhv_array) entry = Null(HE*);
814 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
815 for (; entry; entry = HeNEXT(entry)) {
816 if (HeHASH(entry) != hash) /* strings can't be equal */
818 if (HeKLEN(entry) != klen)
820 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
824 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
825 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
827 char *env = PerlEnv_ENVgetenv_len(key,&len);
829 sv = newSVpvn(env,len);
831 (void)hv_store_ent(hv,keysv,sv,hash);
840 S_hsplit(pTHX_ HV *hv)
842 register XPVHV* xhv = (XPVHV*)SvANY(hv);
843 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
844 register I32 newsize = oldsize * 2;
846 register char *a = xhv->xhv_array;
850 register HE **oentry;
853 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
854 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
860 #define MALLOC_OVERHEAD 16
861 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
866 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
868 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
871 Safefree(xhv->xhv_array);
875 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
876 xhv->xhv_max = --newsize;
880 for (i=0; i<oldsize; i++,aep++) {
881 if (!*aep) /* non-existent */
884 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
885 if ((HeHASH(entry) & newsize) != i) {
886 *oentry = HeNEXT(entry);
887 HeNEXT(entry) = *bep;
894 oentry = &HeNEXT(entry);
896 if (!*aep) /* everything moved */
902 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
904 register XPVHV* xhv = (XPVHV*)SvANY(hv);
905 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
906 register I32 newsize;
912 register HE **oentry;
914 newsize = (I32) newmax; /* possible truncation here */
915 if (newsize != newmax || newmax <= oldsize)
917 while ((newsize & (1 + ~newsize)) != newsize) {
918 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
920 if (newsize < newmax)
922 if (newsize < newmax)
923 return; /* overflow detection */
928 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
929 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
935 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
940 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
942 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
945 Safefree(xhv->xhv_array);
948 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
951 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
953 xhv->xhv_max = --newsize;
955 if (!xhv->xhv_fill) /* skip rest if no entries */
959 for (i=0; i<oldsize; i++,aep++) {
960 if (!*aep) /* non-existent */
962 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
963 if ((j = (HeHASH(entry) & newsize)) != i) {
965 *oentry = HeNEXT(entry);
966 if (!(HeNEXT(entry) = aep[j]))
972 oentry = &HeNEXT(entry);
974 if (!*aep) /* everything moved */
982 Creates a new HV. The reference count is set to 1.
993 hv = (HV*)NEWSV(502,0);
994 sv_upgrade((SV *)hv, SVt_PVHV);
995 xhv = (XPVHV*)SvANY(hv);
998 #ifndef NODEFAULT_SHAREKEYS
999 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1001 xhv->xhv_max = 7; /* start with 8 buckets */
1003 xhv->xhv_pmroot = 0;
1004 (void)hv_iterinit(hv); /* so each() will start off right */
1009 Perl_newHVhv(pTHX_ HV *ohv)
1012 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1013 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1016 while (hv_max && hv_max + 1 >= hv_fill * 2)
1017 hv_max = hv_max / 2; /* Is always 2^n-1 */
1023 if (! SvTIED_mg((SV*)ohv, 'P')) {
1030 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1031 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1035 while (entry = hv_iternext(ohv)) {
1036 hv_store(hv, HeKEY(entry), HeKLEN(entry),
1037 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
1039 HvRITER(ohv) = hv_riter;
1040 HvEITER(ohv) = hv_eiter;
1047 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1054 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1055 PL_sub_generation++; /* may be deletion of method from stash */
1057 if (HeKLEN(entry) == HEf_SVKEY) {
1058 SvREFCNT_dec(HeKEY_sv(entry));
1059 Safefree(HeKEY_hek(entry));
1061 else if (HvSHAREKEYS(hv))
1062 unshare_hek(HeKEY_hek(entry));
1064 Safefree(HeKEY_hek(entry));
1069 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1073 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1074 PL_sub_generation++; /* may be deletion of method from stash */
1075 sv_2mortal(HeVAL(entry)); /* free between statements */
1076 if (HeKLEN(entry) == HEf_SVKEY) {
1077 sv_2mortal(HeKEY_sv(entry));
1078 Safefree(HeKEY_hek(entry));
1080 else if (HvSHAREKEYS(hv))
1081 unshare_hek(HeKEY_hek(entry));
1083 Safefree(HeKEY_hek(entry));
1088 =for apidoc hv_clear
1090 Clears a hash, making it empty.
1096 Perl_hv_clear(pTHX_ HV *hv)
1098 register XPVHV* xhv;
1101 xhv = (XPVHV*)SvANY(hv);
1106 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
1113 S_hfreeentries(pTHX_ HV *hv)
1115 register HE **array;
1117 register HE *oentry = Null(HE*);
1128 array = HvARRAY(hv);
1133 entry = HeNEXT(entry);
1134 hv_free_ent(hv, oentry);
1139 entry = array[riter];
1142 (void)hv_iterinit(hv);
1146 =for apidoc hv_undef
1154 Perl_hv_undef(pTHX_ HV *hv)
1156 register XPVHV* xhv;
1159 xhv = (XPVHV*)SvANY(hv);
1161 Safefree(xhv->xhv_array);
1163 Safefree(HvNAME(hv));
1167 xhv->xhv_max = 7; /* it's a normal hash */
1176 =for apidoc hv_iterinit
1178 Prepares a starting point to traverse a hash table. Returns the number of
1179 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1180 currently only meaningful for hashes without tie magic.
1182 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1183 hash buckets that happen to be in use. If you still need that esoteric
1184 value, you can get it through the macro C<HvFILL(tb)>.
1190 Perl_hv_iterinit(pTHX_ HV *hv)
1192 register XPVHV* xhv;
1196 Perl_croak(aTHX_ "Bad hash");
1197 xhv = (XPVHV*)SvANY(hv);
1198 entry = xhv->xhv_eiter;
1199 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1201 hv_free_ent(hv, entry);
1203 xhv->xhv_riter = -1;
1204 xhv->xhv_eiter = Null(HE*);
1205 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1209 =for apidoc hv_iternext
1211 Returns entries from a hash iterator. See C<hv_iterinit>.
1217 Perl_hv_iternext(pTHX_ HV *hv)
1219 register XPVHV* xhv;
1225 Perl_croak(aTHX_ "Bad hash");
1226 xhv = (XPVHV*)SvANY(hv);
1227 oldentry = entry = xhv->xhv_eiter;
1229 if (mg = SvTIED_mg((SV*)hv, 'P')) {
1230 SV *key = sv_newmortal();
1232 sv_setsv(key, HeSVKEY_force(entry));
1233 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1239 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
1241 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1243 HeKEY_hek(entry) = hek;
1244 HeKLEN(entry) = HEf_SVKEY;
1246 magic_nextpack((SV*) hv,mg,key);
1248 /* force key to stay around until next time */
1249 HeSVKEY_set(entry, SvREFCNT_inc(key));
1250 return entry; /* beware, hent_val is not set */
1253 SvREFCNT_dec(HeVAL(entry));
1254 Safefree(HeKEY_hek(entry));
1256 xhv->xhv_eiter = Null(HE*);
1259 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1260 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1264 if (!xhv->xhv_array)
1265 Newz(506, xhv->xhv_array,
1266 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1268 entry = HeNEXT(entry);
1271 if (xhv->xhv_riter > xhv->xhv_max) {
1272 xhv->xhv_riter = -1;
1275 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1278 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1280 hv_free_ent(hv, oldentry);
1283 xhv->xhv_eiter = entry;
1288 =for apidoc hv_iterkey
1290 Returns the key from the current position of the hash iterator. See
1297 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1299 if (HeKLEN(entry) == HEf_SVKEY) {
1301 char *p = SvPV(HeKEY_sv(entry), len);
1306 *retlen = HeKLEN(entry);
1307 return HeKEY(entry);
1311 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1313 =for apidoc hv_iterkeysv
1315 Returns the key as an C<SV*> from the current position of the hash
1316 iterator. The return value will always be a mortal copy of the key. Also
1323 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1325 if (HeKLEN(entry) == HEf_SVKEY)
1326 return sv_mortalcopy(HeKEY_sv(entry));
1328 return sv_2mortal(newSVpvn((HeKLEN(entry) ? HeKEY(entry) : ""),
1333 =for apidoc hv_iterval
1335 Returns the value from the current position of the hash iterator. See
1342 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1344 if (SvRMAGICAL(hv)) {
1345 if (mg_find((SV*)hv,'P')) {
1346 SV* sv = sv_newmortal();
1347 if (HeKLEN(entry) == HEf_SVKEY)
1348 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1349 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1353 return HeVAL(entry);
1357 =for apidoc hv_iternextsv
1359 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1366 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1369 if ( (he = hv_iternext(hv)) == NULL)
1371 *key = hv_iterkey(he, retlen);
1372 return hv_iterval(hv, he);
1376 =for apidoc hv_magic
1378 Adds magic to a hash. See C<sv_magic>.
1384 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1386 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1390 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1392 return HEK_KEY(share_hek(sv, len, hash));
1395 /* possibly free a shared string if no one has access to it
1396 * len and hash must both be valid for str.
1399 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1401 register XPVHV* xhv;
1403 register HE **oentry;
1407 /* what follows is the moral equivalent of:
1408 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1409 if (--*Svp == Nullsv)
1410 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1412 xhv = (XPVHV*)SvANY(PL_strtab);
1413 /* assert(xhv_array != 0) */
1415 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1416 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1417 if (HeHASH(entry) != hash) /* strings can't be equal */
1419 if (HeKLEN(entry) != len)
1421 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1424 if (--HeVAL(entry) == Nullsv) {
1425 *oentry = HeNEXT(entry);
1428 Safefree(HeKEY_hek(entry));
1434 UNLOCK_STRTAB_MUTEX;
1438 if (!found && ckWARN_d(WARN_INTERNAL))
1439 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string");
1443 /* get a (constant) string ptr from the global string table
1444 * string will get added if it is not already there.
1445 * len and hash must both be valid for str.
1448 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1450 register XPVHV* xhv;
1452 register HE **oentry;
1456 /* what follows is the moral equivalent of:
1458 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1459 hv_store(PL_strtab, str, len, Nullsv, hash);
1461 xhv = (XPVHV*)SvANY(PL_strtab);
1462 /* assert(xhv_array != 0) */
1464 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1465 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1466 if (HeHASH(entry) != hash) /* strings can't be equal */
1468 if (HeKLEN(entry) != len)
1470 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1477 HeKEY_hek(entry) = save_hek(str, len, hash);
1478 HeVAL(entry) = Nullsv;
1479 HeNEXT(entry) = *oentry;
1482 if (i) { /* initial entry? */
1484 if (xhv->xhv_keys > xhv->xhv_max)
1489 ++HeVAL(entry); /* use value slot as REFCNT */
1490 UNLOCK_STRTAB_MUTEX;
1491 return HeKEY_hek(entry);