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;
46 New(54, ptr, 1008/sizeof(XPV), XPV);
47 ptr->xpv_pv = (char*)PL_he_arenaroot;
48 PL_he_arenaroot = ptr;
51 heend = &he[1008 / sizeof(HE) - 1];
54 HeNEXT(he) = (HE*)(he + 1);
62 #define new_HE() (HE*)safemalloc(sizeof(HE))
63 #define del_HE(p) safefree((char*)p)
67 #define new_HE() new_he()
68 #define del_HE(p) del_he(p)
73 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
78 New(54, k, HEK_BASESIZE + len + 1, char);
80 Copy(str, HEK_KEY(hek), len, char);
81 *(HEK_KEY(hek) + len) = '\0';
88 Perl_unshare_hek(pTHX_ HEK *hek)
90 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
93 #if defined(USE_ITHREADS)
95 Perl_he_dup(pTHX_ HE *e, bool shared)
101 /* look for it in the table first */
102 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
106 /* create anew and remember what it is */
108 ptr_table_store(PL_ptr_table, e, ret);
110 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
111 if (HeKLEN(e) == HEf_SVKEY)
112 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
114 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
116 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
117 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
120 #endif /* USE_ITHREADS */
122 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
128 Returns the SV which corresponds to the specified key in the hash. The
129 C<klen> is the length of the key. If C<lval> is set then the fetch will be
130 part of a store. Check that the return value is non-null before
131 dereferencing it to a C<SV*>.
133 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
134 information on how to use this function on tied hashes.
140 Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
150 if (SvRMAGICAL(hv)) {
151 if (mg_find((SV*)hv,'P')) {
154 mg_copy((SV*)hv, sv, key, klen);
156 return &PL_hv_fetch_sv;
158 #ifdef ENV_IS_CASELESS
159 else if (mg_find((SV*)hv,'E')) {
161 for (i = 0; i < klen; ++i)
162 if (isLOWER(key[i])) {
163 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
164 SV **ret = hv_fetch(hv, nkey, klen, 0);
166 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
173 xhv = (XPVHV*)SvANY(hv);
174 if (!xhv->xhv_array) {
176 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
177 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
180 Newz(503, xhv->xhv_array,
181 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
186 PERL_HASH(hash, key, klen);
188 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
189 for (; entry; entry = HeNEXT(entry)) {
190 if (HeHASH(entry) != hash) /* strings can't be equal */
192 if (HeKLEN(entry) != klen)
194 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
196 return &HeVAL(entry);
198 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
199 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
201 char *env = PerlEnv_ENVgetenv_len(key,&len);
203 sv = newSVpvn(env,len);
205 return hv_store(hv,key,klen,sv,hash);
209 if (lval) { /* gonna assign to this, so it better be there */
211 return hv_store(hv,key,klen,sv,hash);
216 /* returns a HE * structure with the all fields set */
217 /* note that hent_val will be a mortal sv for MAGICAL hashes */
219 =for apidoc hv_fetch_ent
221 Returns the hash entry which corresponds to the specified key in the hash.
222 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
223 if you want the function to compute it. IF C<lval> is set then the fetch
224 will be part of a store. Make sure the return value is non-null before
225 accessing it. The return value when C<tb> is a tied hash is a pointer to a
226 static location, so be sure to make a copy of the structure if you need to
229 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
230 information on how to use this function on tied hashes.
236 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
247 if (SvRMAGICAL(hv)) {
248 if (mg_find((SV*)hv,'P')) {
251 keysv = sv_2mortal(newSVsv(keysv));
252 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
253 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
255 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
256 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
258 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
259 HeVAL(&PL_hv_fetch_ent_mh) = sv;
260 return &PL_hv_fetch_ent_mh;
262 #ifdef ENV_IS_CASELESS
263 else if (mg_find((SV*)hv,'E')) {
265 key = SvPV(keysv, klen);
266 for (i = 0; i < klen; ++i)
267 if (isLOWER(key[i])) {
268 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
269 (void)strupr(SvPVX(nkeysv));
270 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
272 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
279 xhv = (XPVHV*)SvANY(hv);
280 if (!xhv->xhv_array) {
282 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
283 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
286 Newz(503, xhv->xhv_array,
287 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
292 key = SvPV(keysv, klen);
295 PERL_HASH(hash, key, klen);
297 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
298 for (; entry; entry = HeNEXT(entry)) {
299 if (HeHASH(entry) != hash) /* strings can't be equal */
301 if (HeKLEN(entry) != klen)
303 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
307 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
308 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
310 char *env = PerlEnv_ENVgetenv_len(key,&len);
312 sv = newSVpvn(env,len);
314 return hv_store_ent(hv,keysv,sv,hash);
318 if (lval) { /* gonna assign to this, so it better be there */
320 return hv_store_ent(hv,keysv,sv,hash);
326 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
328 MAGIC *mg = SvMAGIC(hv);
332 if (isUPPER(mg->mg_type)) {
334 switch (mg->mg_type) {
337 *needs_store = FALSE;
340 mg = mg->mg_moremagic;
347 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
348 the length of the key. The C<hash> parameter is the precomputed hash
349 value; if it is zero then Perl will compute it. The return value will be
350 NULL if the operation failed or if the value did not need to be actually
351 stored within the hash (as in the case of tied hashes). Otherwise it can
352 be dereferenced to get the original C<SV*>. Note that the caller is
353 responsible for suitably incrementing the reference count of C<val> before
354 the call, and decrementing it if the function returned NULL.
356 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
357 information on how to use this function on tied hashes.
363 Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
368 register HE **oentry;
373 xhv = (XPVHV*)SvANY(hv);
377 hv_magic_check (hv, &needs_copy, &needs_store);
379 mg_copy((SV*)hv, val, key, klen);
380 if (!xhv->xhv_array && !needs_store)
382 #ifdef ENV_IS_CASELESS
383 else if (mg_find((SV*)hv,'E')) {
384 SV *sv = sv_2mortal(newSVpvn(key,klen));
385 key = strupr(SvPVX(sv));
392 PERL_HASH(hash, key, klen);
395 Newz(505, xhv->xhv_array,
396 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
398 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
401 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
402 if (HeHASH(entry) != hash) /* strings can't be equal */
404 if (HeKLEN(entry) != klen)
406 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
408 SvREFCNT_dec(HeVAL(entry));
410 return &HeVAL(entry);
415 HeKEY_hek(entry) = share_hek(key, klen, hash);
416 else /* gotta do the real thing */
417 HeKEY_hek(entry) = save_hek(key, klen, hash);
419 HeNEXT(entry) = *oentry;
423 if (i) { /* initial entry? */
425 if (xhv->xhv_keys > xhv->xhv_max)
429 return &HeVAL(entry);
433 =for apidoc hv_store_ent
435 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
436 parameter is the precomputed hash value; if it is zero then Perl will
437 compute it. The return value is the new hash entry so created. It will be
438 NULL if the operation failed or if the value did not need to be actually
439 stored within the hash (as in the case of tied hashes). Otherwise the
440 contents of the return value can be accessed using the C<He???> macros
441 described here. Note that the caller is responsible for suitably
442 incrementing the reference count of C<val> before the call, and
443 decrementing it if the function returned NULL.
445 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
446 information on how to use this function on tied hashes.
452 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
459 register HE **oentry;
464 xhv = (XPVHV*)SvANY(hv);
469 hv_magic_check (hv, &needs_copy, &needs_store);
471 bool save_taint = PL_tainted;
473 PL_tainted = SvTAINTED(keysv);
474 keysv = sv_2mortal(newSVsv(keysv));
475 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
476 TAINT_IF(save_taint);
477 if (!xhv->xhv_array && !needs_store)
479 #ifdef ENV_IS_CASELESS
480 else if (mg_find((SV*)hv,'E')) {
481 key = SvPV(keysv, klen);
482 keysv = sv_2mortal(newSVpvn(key,klen));
483 (void)strupr(SvPVX(keysv));
490 key = SvPV(keysv, klen);
493 PERL_HASH(hash, key, klen);
496 Newz(505, xhv->xhv_array,
497 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
499 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
502 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
503 if (HeHASH(entry) != hash) /* strings can't be equal */
505 if (HeKLEN(entry) != klen)
507 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
509 SvREFCNT_dec(HeVAL(entry));
516 HeKEY_hek(entry) = share_hek(key, klen, hash);
517 else /* gotta do the real thing */
518 HeKEY_hek(entry) = save_hek(key, klen, hash);
520 HeNEXT(entry) = *oentry;
524 if (i) { /* initial entry? */
526 if (xhv->xhv_keys > xhv->xhv_max)
534 =for apidoc hv_delete
536 Deletes a key/value pair in the hash. The value SV is removed from the
537 hash and returned to the caller. The C<klen> is the length of the key.
538 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
545 Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
551 register HE **oentry;
557 if (SvRMAGICAL(hv)) {
560 hv_magic_check (hv, &needs_copy, &needs_store);
562 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
566 if (mg_find(sv, 'p')) {
567 sv_unmagic(sv, 'p'); /* No longer an element */
570 return Nullsv; /* element cannot be deleted */
572 #ifdef ENV_IS_CASELESS
573 else if (mg_find((SV*)hv,'E')) {
574 sv = sv_2mortal(newSVpvn(key,klen));
575 key = strupr(SvPVX(sv));
580 xhv = (XPVHV*)SvANY(hv);
584 PERL_HASH(hash, key, klen);
586 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
589 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
590 if (HeHASH(entry) != hash) /* strings can't be equal */
592 if (HeKLEN(entry) != klen)
594 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
596 *oentry = HeNEXT(entry);
599 if (flags & G_DISCARD)
602 sv = sv_2mortal(HeVAL(entry));
603 HeVAL(entry) = &PL_sv_undef;
605 if (entry == xhv->xhv_eiter)
608 hv_free_ent(hv, entry);
616 =for apidoc hv_delete_ent
618 Deletes a key/value pair in the hash. The value SV is removed from the
619 hash and returned to the caller. The C<flags> value will normally be zero;
620 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
621 precomputed hash value, or 0 to ask for it to be computed.
627 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
634 register HE **oentry;
639 if (SvRMAGICAL(hv)) {
642 hv_magic_check (hv, &needs_copy, &needs_store);
644 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
648 if (mg_find(sv, 'p')) {
649 sv_unmagic(sv, 'p'); /* No longer an element */
652 return Nullsv; /* element cannot be deleted */
654 #ifdef ENV_IS_CASELESS
655 else if (mg_find((SV*)hv,'E')) {
656 key = SvPV(keysv, klen);
657 keysv = sv_2mortal(newSVpvn(key,klen));
658 (void)strupr(SvPVX(keysv));
664 xhv = (XPVHV*)SvANY(hv);
668 key = SvPV(keysv, klen);
671 PERL_HASH(hash, key, klen);
673 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
676 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
677 if (HeHASH(entry) != hash) /* strings can't be equal */
679 if (HeKLEN(entry) != klen)
681 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
683 *oentry = HeNEXT(entry);
686 if (flags & G_DISCARD)
689 sv = sv_2mortal(HeVAL(entry));
690 HeVAL(entry) = &PL_sv_undef;
692 if (entry == xhv->xhv_eiter)
695 hv_free_ent(hv, entry);
703 =for apidoc hv_exists
705 Returns a boolean indicating whether the specified hash key exists. The
706 C<klen> is the length of the key.
712 Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
722 if (SvRMAGICAL(hv)) {
723 if (mg_find((SV*)hv,'P')) {
726 mg_copy((SV*)hv, sv, key, klen);
727 magic_existspack(sv, mg_find(sv, 'p'));
730 #ifdef ENV_IS_CASELESS
731 else if (mg_find((SV*)hv,'E')) {
732 sv = sv_2mortal(newSVpvn(key,klen));
733 key = strupr(SvPVX(sv));
738 xhv = (XPVHV*)SvANY(hv);
739 #ifndef DYNAMIC_ENV_FETCH
744 PERL_HASH(hash, key, klen);
746 #ifdef DYNAMIC_ENV_FETCH
747 if (!xhv->xhv_array) entry = Null(HE*);
750 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
751 for (; entry; entry = HeNEXT(entry)) {
752 if (HeHASH(entry) != hash) /* strings can't be equal */
754 if (HeKLEN(entry) != klen)
756 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
760 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
761 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
763 char *env = PerlEnv_ENVgetenv_len(key,&len);
765 sv = newSVpvn(env,len);
767 (void)hv_store(hv,key,klen,sv,hash);
777 =for apidoc hv_exists_ent
779 Returns a boolean indicating whether the specified hash key exists. C<hash>
780 can be a valid precomputed hash value, or 0 to ask for it to be
787 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
798 if (SvRMAGICAL(hv)) {
799 if (mg_find((SV*)hv,'P')) {
800 dTHR; /* just for SvTRUE */
802 keysv = sv_2mortal(newSVsv(keysv));
803 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
804 magic_existspack(sv, mg_find(sv, 'p'));
807 #ifdef ENV_IS_CASELESS
808 else if (mg_find((SV*)hv,'E')) {
809 key = SvPV(keysv, klen);
810 keysv = sv_2mortal(newSVpvn(key,klen));
811 (void)strupr(SvPVX(keysv));
817 xhv = (XPVHV*)SvANY(hv);
818 #ifndef DYNAMIC_ENV_FETCH
823 key = SvPV(keysv, klen);
825 PERL_HASH(hash, key, klen);
827 #ifdef DYNAMIC_ENV_FETCH
828 if (!xhv->xhv_array) entry = Null(HE*);
831 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
832 for (; entry; entry = HeNEXT(entry)) {
833 if (HeHASH(entry) != hash) /* strings can't be equal */
835 if (HeKLEN(entry) != klen)
837 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
841 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
842 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
844 char *env = PerlEnv_ENVgetenv_len(key,&len);
846 sv = newSVpvn(env,len);
848 (void)hv_store_ent(hv,keysv,sv,hash);
857 S_hsplit(pTHX_ HV *hv)
859 register XPVHV* xhv = (XPVHV*)SvANY(hv);
860 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
861 register I32 newsize = oldsize * 2;
863 register char *a = xhv->xhv_array;
867 register HE **oentry;
870 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
871 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
877 #define MALLOC_OVERHEAD 16
878 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
883 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
885 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
888 Safefree(xhv->xhv_array);
892 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
893 xhv->xhv_max = --newsize;
897 for (i=0; i<oldsize; i++,aep++) {
898 if (!*aep) /* non-existent */
901 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
902 if ((HeHASH(entry) & newsize) != i) {
903 *oentry = HeNEXT(entry);
904 HeNEXT(entry) = *bep;
911 oentry = &HeNEXT(entry);
913 if (!*aep) /* everything moved */
919 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
921 register XPVHV* xhv = (XPVHV*)SvANY(hv);
922 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
923 register I32 newsize;
929 register HE **oentry;
931 newsize = (I32) newmax; /* possible truncation here */
932 if (newsize != newmax || newmax <= oldsize)
934 while ((newsize & (1 + ~newsize)) != newsize) {
935 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
937 if (newsize < newmax)
939 if (newsize < newmax)
940 return; /* overflow detection */
945 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
946 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
952 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
957 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
959 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
962 Safefree(xhv->xhv_array);
965 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
968 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
970 xhv->xhv_max = --newsize;
972 if (!xhv->xhv_fill) /* skip rest if no entries */
976 for (i=0; i<oldsize; i++,aep++) {
977 if (!*aep) /* non-existent */
979 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
980 if ((j = (HeHASH(entry) & newsize)) != i) {
982 *oentry = HeNEXT(entry);
983 if (!(HeNEXT(entry) = aep[j]))
989 oentry = &HeNEXT(entry);
991 if (!*aep) /* everything moved */
999 Creates a new HV. The reference count is set to 1.
1008 register XPVHV* xhv;
1010 hv = (HV*)NEWSV(502,0);
1011 sv_upgrade((SV *)hv, SVt_PVHV);
1012 xhv = (XPVHV*)SvANY(hv);
1015 #ifndef NODEFAULT_SHAREKEYS
1016 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1018 xhv->xhv_max = 7; /* start with 8 buckets */
1020 xhv->xhv_pmroot = 0;
1021 (void)hv_iterinit(hv); /* so each() will start off right */
1026 Perl_newHVhv(pTHX_ HV *ohv)
1029 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1030 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1033 while (hv_max && hv_max + 1 >= hv_fill * 2)
1034 hv_max = hv_max / 2; /* Is always 2^n-1 */
1040 if (! SvTIED_mg((SV*)ohv, 'P')) {
1047 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1048 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1052 while ((entry = hv_iternext(ohv))) {
1053 hv_store(hv, HeKEY(entry), HeKLEN(entry),
1054 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
1056 HvRITER(ohv) = hv_riter;
1057 HvEITER(ohv) = hv_eiter;
1064 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1071 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1072 PL_sub_generation++; /* may be deletion of method from stash */
1074 if (HeKLEN(entry) == HEf_SVKEY) {
1075 SvREFCNT_dec(HeKEY_sv(entry));
1076 Safefree(HeKEY_hek(entry));
1078 else if (HvSHAREKEYS(hv))
1079 unshare_hek(HeKEY_hek(entry));
1081 Safefree(HeKEY_hek(entry));
1086 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1090 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1091 PL_sub_generation++; /* may be deletion of method from stash */
1092 sv_2mortal(HeVAL(entry)); /* free between statements */
1093 if (HeKLEN(entry) == HEf_SVKEY) {
1094 sv_2mortal(HeKEY_sv(entry));
1095 Safefree(HeKEY_hek(entry));
1097 else if (HvSHAREKEYS(hv))
1098 unshare_hek(HeKEY_hek(entry));
1100 Safefree(HeKEY_hek(entry));
1105 =for apidoc hv_clear
1107 Clears a hash, making it empty.
1113 Perl_hv_clear(pTHX_ HV *hv)
1115 register XPVHV* xhv;
1118 xhv = (XPVHV*)SvANY(hv);
1123 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
1130 S_hfreeentries(pTHX_ HV *hv)
1132 register HE **array;
1134 register HE *oentry = Null(HE*);
1145 array = HvARRAY(hv);
1150 entry = HeNEXT(entry);
1151 hv_free_ent(hv, oentry);
1156 entry = array[riter];
1159 (void)hv_iterinit(hv);
1163 =for apidoc hv_undef
1171 Perl_hv_undef(pTHX_ HV *hv)
1173 register XPVHV* xhv;
1176 xhv = (XPVHV*)SvANY(hv);
1178 Safefree(xhv->xhv_array);
1180 Safefree(HvNAME(hv));
1184 xhv->xhv_max = 7; /* it's a normal hash */
1193 =for apidoc hv_iterinit
1195 Prepares a starting point to traverse a hash table. Returns the number of
1196 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1197 currently only meaningful for hashes without tie magic.
1199 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1200 hash buckets that happen to be in use. If you still need that esoteric
1201 value, you can get it through the macro C<HvFILL(tb)>.
1207 Perl_hv_iterinit(pTHX_ HV *hv)
1209 register XPVHV* xhv;
1213 Perl_croak(aTHX_ "Bad hash");
1214 xhv = (XPVHV*)SvANY(hv);
1215 entry = xhv->xhv_eiter;
1216 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1218 hv_free_ent(hv, entry);
1220 xhv->xhv_riter = -1;
1221 xhv->xhv_eiter = Null(HE*);
1222 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1226 =for apidoc hv_iternext
1228 Returns entries from a hash iterator. See C<hv_iterinit>.
1234 Perl_hv_iternext(pTHX_ HV *hv)
1236 register XPVHV* xhv;
1242 Perl_croak(aTHX_ "Bad hash");
1243 xhv = (XPVHV*)SvANY(hv);
1244 oldentry = entry = xhv->xhv_eiter;
1246 if ((mg = SvTIED_mg((SV*)hv, 'P'))) {
1247 SV *key = sv_newmortal();
1249 sv_setsv(key, HeSVKEY_force(entry));
1250 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1256 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
1258 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1260 HeKEY_hek(entry) = hek;
1261 HeKLEN(entry) = HEf_SVKEY;
1263 magic_nextpack((SV*) hv,mg,key);
1265 /* force key to stay around until next time */
1266 HeSVKEY_set(entry, SvREFCNT_inc(key));
1267 return entry; /* beware, hent_val is not set */
1270 SvREFCNT_dec(HeVAL(entry));
1271 Safefree(HeKEY_hek(entry));
1273 xhv->xhv_eiter = Null(HE*);
1276 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1277 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1281 if (!xhv->xhv_array)
1282 Newz(506, xhv->xhv_array,
1283 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1285 entry = HeNEXT(entry);
1288 if (xhv->xhv_riter > xhv->xhv_max) {
1289 xhv->xhv_riter = -1;
1292 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1295 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1297 hv_free_ent(hv, oldentry);
1300 xhv->xhv_eiter = entry;
1305 =for apidoc hv_iterkey
1307 Returns the key from the current position of the hash iterator. See
1314 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1316 if (HeKLEN(entry) == HEf_SVKEY) {
1318 char *p = SvPV(HeKEY_sv(entry), len);
1323 *retlen = HeKLEN(entry);
1324 return HeKEY(entry);
1328 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1330 =for apidoc hv_iterkeysv
1332 Returns the key as an C<SV*> from the current position of the hash
1333 iterator. The return value will always be a mortal copy of the key. Also
1340 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1342 if (HeKLEN(entry) == HEf_SVKEY)
1343 return sv_mortalcopy(HeKEY_sv(entry));
1345 return sv_2mortal(newSVpvn((HeKLEN(entry) ? HeKEY(entry) : ""),
1350 =for apidoc hv_iterval
1352 Returns the value from the current position of the hash iterator. See
1359 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1361 if (SvRMAGICAL(hv)) {
1362 if (mg_find((SV*)hv,'P')) {
1363 SV* sv = sv_newmortal();
1364 if (HeKLEN(entry) == HEf_SVKEY)
1365 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1366 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1370 return HeVAL(entry);
1374 =for apidoc hv_iternextsv
1376 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1383 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1386 if ( (he = hv_iternext(hv)) == NULL)
1388 *key = hv_iterkey(he, retlen);
1389 return hv_iterval(hv, he);
1393 =for apidoc hv_magic
1395 Adds magic to a hash. See C<sv_magic>.
1401 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1403 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1407 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1409 return HEK_KEY(share_hek(sv, len, hash));
1412 /* possibly free a shared string if no one has access to it
1413 * len and hash must both be valid for str.
1416 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1418 register XPVHV* xhv;
1420 register HE **oentry;
1424 /* what follows is the moral equivalent of:
1425 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1426 if (--*Svp == Nullsv)
1427 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1429 xhv = (XPVHV*)SvANY(PL_strtab);
1430 /* assert(xhv_array != 0) */
1432 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1433 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1434 if (HeHASH(entry) != hash) /* strings can't be equal */
1436 if (HeKLEN(entry) != len)
1438 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1441 if (--HeVAL(entry) == Nullsv) {
1442 *oentry = HeNEXT(entry);
1445 Safefree(HeKEY_hek(entry));
1451 UNLOCK_STRTAB_MUTEX;
1455 if (!found && ckWARN_d(WARN_INTERNAL))
1456 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string");
1460 /* get a (constant) string ptr from the global string table
1461 * string will get added if it is not already there.
1462 * len and hash must both be valid for str.
1465 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1467 register XPVHV* xhv;
1469 register HE **oentry;
1473 /* what follows is the moral equivalent of:
1475 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1476 hv_store(PL_strtab, str, len, Nullsv, hash);
1478 xhv = (XPVHV*)SvANY(PL_strtab);
1479 /* assert(xhv_array != 0) */
1481 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1482 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1483 if (HeHASH(entry) != hash) /* strings can't be equal */
1485 if (HeKLEN(entry) != len)
1487 if (memNE(HeKEY(entry),str,len)) /* is this it? */
1494 HeKEY_hek(entry) = save_hek(str, len, hash);
1495 HeVAL(entry) = Nullsv;
1496 HeNEXT(entry) = *oentry;
1499 if (i) { /* initial entry? */
1501 if (xhv->xhv_keys > xhv->xhv_max)
1506 ++HeVAL(entry); /* use value slot as REFCNT */
1507 UNLOCK_STRTAB_MUTEX;
1508 return HeKEY_hek(entry);