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
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)
84 New(54, k, HEK_BASESIZE + len + 1, char);
86 Copy(str, HEK_KEY(hek), len, char);
89 HEK_UTF8(hek) = (char)is_utf8;
94 Perl_unshare_hek(pTHX_ HEK *hek)
96 unsharepvn(HEK_KEY(hek),HEK_UTF8(hek)?-HEK_LEN(hek):HEK_LEN(hek),
100 #if defined(USE_ITHREADS)
102 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
108 /* look for it in the table first */
109 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
113 /* create anew and remember what it is */
115 ptr_table_store(PL_ptr_table, e, ret);
117 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
118 if (HeKLEN(e) == HEf_SVKEY)
119 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
121 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
123 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
124 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
127 #endif /* USE_ITHREADS */
130 Perl_hv_notallowed(pTHX_ bool is_utf8, const char *key, I32 klen,
133 SV *sv = sv_newmortal();
134 if (key == keysave) {
135 sv_setpvn(sv, key, klen);
138 /* Need to free saved eventually assign to mortal SV */
139 SV *sv = sv_newmortal();
140 sv_usepvn(sv, (char *) key, klen);
145 Perl_croak(aTHX_ "Attempt to access to key '%_' in fixed hash",sv);
148 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
154 Returns the SV which corresponds to the specified key in the hash. The
155 C<klen> is the length of the key. If C<lval> is set then the fetch will be
156 part of a store. Check that the return value is non-null before
157 dereferencing it to an C<SV*>.
159 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
160 information on how to use this function on tied hashes.
166 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
172 bool is_utf8 = FALSE;
173 const char *keysave = key;
183 if (SvRMAGICAL(hv)) {
184 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
186 mg_copy((SV*)hv, sv, key, klen);
188 return &PL_hv_fetch_sv;
190 #ifdef ENV_IS_CASELESS
191 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
193 for (i = 0; i < klen; ++i)
194 if (isLOWER(key[i])) {
195 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
196 SV **ret = hv_fetch(hv, nkey, klen, 0);
198 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
205 /* We use xhv->xhv_foo fields directly instead of HvFOO(hv) to
206 avoid unnecessary pointer dereferencing. */
207 xhv = (XPVHV*)SvANY(hv);
208 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
210 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
211 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
214 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
215 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
222 STRLEN tmplen = klen;
223 /* Just casting the &klen to (STRLEN) won't work well
224 * if STRLEN and I32 are of different widths. --jhi */
225 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
229 PERL_HASH(hash, key, klen);
231 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
232 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
233 for (; entry; entry = HeNEXT(entry)) {
234 if (HeHASH(entry) != hash) /* strings can't be equal */
236 if (HeKLEN(entry) != klen)
238 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
240 if (HeKUTF8(entry) != (char)is_utf8)
244 return &HeVAL(entry);
246 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
247 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
249 char *env = PerlEnv_ENVgetenv_len(key,&len);
251 sv = newSVpvn(env,len);
255 return hv_store(hv,key,klen,sv,hash);
259 if (SvREADONLY(hv)) {
260 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
262 if (lval) { /* gonna assign to this, so it better be there */
264 if (key != keysave) { /* must be is_utf8 == 0 */
265 SV **ret = hv_store(hv,key,klen,sv,hash);
270 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
277 /* returns an HE * structure with the all fields set */
278 /* note that hent_val will be a mortal sv for MAGICAL hashes */
280 =for apidoc hv_fetch_ent
282 Returns the hash entry which corresponds to the specified key in the hash.
283 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
284 if you want the function to compute it. IF C<lval> is set then the fetch
285 will be part of a store. Make sure the return value is non-null before
286 accessing it. The return value when C<tb> is a tied hash is a pointer to a
287 static location, so be sure to make a copy of the structure if you need to
290 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
291 information on how to use this function on tied hashes.
297 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
310 if (SvRMAGICAL(hv)) {
311 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
313 keysv = sv_2mortal(newSVsv(keysv));
314 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
315 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
317 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
318 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
320 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
321 HeVAL(&PL_hv_fetch_ent_mh) = sv;
322 return &PL_hv_fetch_ent_mh;
324 #ifdef ENV_IS_CASELESS
325 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
327 key = SvPV(keysv, klen);
328 for (i = 0; i < klen; ++i)
329 if (isLOWER(key[i])) {
330 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
331 (void)strupr(SvPVX(nkeysv));
332 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
334 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
341 xhv = (XPVHV*)SvANY(hv);
342 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
344 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
345 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
348 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
349 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
355 keysave = key = SvPV(keysv, klen);
356 is_utf8 = (SvUTF8(keysv)!=0);
359 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
362 PERL_HASH(hash, key, klen);
364 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
365 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
366 for (; entry; entry = HeNEXT(entry)) {
367 if (HeHASH(entry) != hash) /* strings can't be equal */
369 if (HeKLEN(entry) != klen)
371 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
373 if (HeKUTF8(entry) != (char)is_utf8)
379 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
380 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
382 char *env = PerlEnv_ENVgetenv_len(key,&len);
384 sv = newSVpvn(env,len);
386 return hv_store_ent(hv,keysv,sv,hash);
390 if (SvREADONLY(hv)) {
391 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
395 if (lval) { /* gonna assign to this, so it better be there */
397 return hv_store_ent(hv,keysv,sv,hash);
403 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
405 MAGIC *mg = SvMAGIC(hv);
409 if (isUPPER(mg->mg_type)) {
411 switch (mg->mg_type) {
412 case PERL_MAGIC_tied:
414 *needs_store = FALSE;
417 mg = mg->mg_moremagic;
424 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
425 the length of the key. The C<hash> parameter is the precomputed hash
426 value; if it is zero then Perl will compute it. The return value will be
427 NULL if the operation failed or if the value did not need to be actually
428 stored within the hash (as in the case of tied hashes). Otherwise it can
429 be dereferenced to get the original C<SV*>. Note that the caller is
430 responsible for suitably incrementing the reference count of C<val> before
431 the call, and decrementing it if the function returned NULL.
433 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
434 information on how to use this function on tied hashes.
440 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
445 register HE **oentry;
446 bool is_utf8 = FALSE;
447 const char *keysave = key;
457 xhv = (XPVHV*)SvANY(hv);
461 hv_magic_check (hv, &needs_copy, &needs_store);
463 mg_copy((SV*)hv, val, key, klen);
464 if (!xhv->xhv_array /* !HvARRAY */ && !needs_store)
466 #ifdef ENV_IS_CASELESS
467 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
468 key = savepvn(key,klen);
469 key = (const char*)strupr((char*)key);
476 STRLEN tmplen = klen;
477 /* See the note in hv_fetch(). --jhi */
478 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
483 PERL_HASH(hash, key, klen);
485 if (!xhv->xhv_array /* !HvARRAY(hv) */)
486 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
487 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
490 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
491 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
494 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
495 if (HeHASH(entry) != hash) /* strings can't be equal */
497 if (HeKLEN(entry) != klen)
499 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
501 if (HeKUTF8(entry) != (char)is_utf8)
503 SvREFCNT_dec(HeVAL(entry));
507 return &HeVAL(entry);
510 if (SvREADONLY(hv)) {
511 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
516 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
517 else /* gotta do the real thing */
518 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
522 HeNEXT(entry) = *oentry;
525 xhv->xhv_keys++; /* HvKEYS(hv)++ */
526 if (i) { /* initial entry? */
527 xhv->xhv_fill++; /* HvFILL(hv)++ */
528 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
532 return &HeVAL(entry);
536 =for apidoc hv_store_ent
538 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
539 parameter is the precomputed hash value; if it is zero then Perl will
540 compute it. The return value is the new hash entry so created. It will be
541 NULL if the operation failed or if the value did not need to be actually
542 stored within the hash (as in the case of tied hashes). Otherwise the
543 contents of the return value can be accessed using the C<He?> macros
544 described here. Note that the caller is responsible for suitably
545 incrementing the reference count of C<val> before the call, and
546 decrementing it if the function returned NULL.
548 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
549 information on how to use this function on tied hashes.
555 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
562 register HE **oentry;
569 xhv = (XPVHV*)SvANY(hv);
573 hv_magic_check (hv, &needs_copy, &needs_store);
575 bool save_taint = PL_tainted;
577 PL_tainted = SvTAINTED(keysv);
578 keysv = sv_2mortal(newSVsv(keysv));
579 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
580 TAINT_IF(save_taint);
581 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store)
583 #ifdef ENV_IS_CASELESS
584 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
585 key = SvPV(keysv, klen);
586 keysv = sv_2mortal(newSVpvn(key,klen));
587 (void)strupr(SvPVX(keysv));
594 keysave = key = SvPV(keysv, klen);
595 is_utf8 = (SvUTF8(keysv) != 0);
598 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
601 PERL_HASH(hash, key, klen);
603 if (!xhv->xhv_array /* !HvARRAY(hv) */)
604 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
605 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
608 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
609 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
612 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
613 if (HeHASH(entry) != hash) /* strings can't be equal */
615 if (HeKLEN(entry) != klen)
617 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
619 if (HeKUTF8(entry) != (char)is_utf8)
621 SvREFCNT_dec(HeVAL(entry));
628 if (SvREADONLY(hv)) {
629 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
634 HeKEY_hek(entry) = share_hek(key, is_utf8?-(I32)klen:klen, hash);
635 else /* gotta do the real thing */
636 HeKEY_hek(entry) = save_hek(key, is_utf8?-(I32)klen:klen, hash);
640 HeNEXT(entry) = *oentry;
643 xhv->xhv_keys++; /* HvKEYS(hv)++ */
644 if (i) { /* initial entry? */
645 xhv->xhv_fill++; /* HvFILL(hv)++ */
646 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
654 =for apidoc hv_delete
656 Deletes a key/value pair in the hash. The value SV is removed from the
657 hash and returned to the caller. The C<klen> is the length of the key.
658 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
665 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
671 register HE **oentry;
674 bool is_utf8 = FALSE;
675 const char *keysave = key;
683 if (SvRMAGICAL(hv)) {
686 hv_magic_check (hv, &needs_copy, &needs_store);
688 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
692 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
693 /* No longer an element */
694 sv_unmagic(sv, PERL_MAGIC_tiedelem);
697 return Nullsv; /* element cannot be deleted */
699 #ifdef ENV_IS_CASELESS
700 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
701 sv = sv_2mortal(newSVpvn(key,klen));
702 key = strupr(SvPVX(sv));
707 xhv = (XPVHV*)SvANY(hv);
708 if (!xhv->xhv_array /* !HvARRAY(hv) */)
712 STRLEN tmplen = klen;
713 /* See the note in hv_fetch(). --jhi */
714 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
718 if (SvREADONLY(hv)) {
719 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
722 PERL_HASH(hash, key, klen);
724 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
725 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
728 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
729 if (HeHASH(entry) != hash) /* strings can't be equal */
731 if (HeKLEN(entry) != klen)
733 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
735 if (HeKUTF8(entry) != (char)is_utf8)
739 *oentry = HeNEXT(entry);
741 xhv->xhv_fill--; /* HvFILL(hv)-- */
742 if (flags & G_DISCARD)
745 sv = sv_2mortal(HeVAL(entry));
746 HeVAL(entry) = &PL_sv_undef;
748 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
751 hv_free_ent(hv, entry);
752 xhv->xhv_keys--; /* HvKEYS(hv)-- */
761 =for apidoc hv_delete_ent
763 Deletes a key/value pair in the hash. The value SV is removed from the
764 hash and returned to the caller. The C<flags> value will normally be zero;
765 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
766 precomputed hash value, or 0 to ask for it to be computed.
772 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
779 register HE **oentry;
786 if (SvRMAGICAL(hv)) {
789 hv_magic_check (hv, &needs_copy, &needs_store);
791 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
795 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
796 /* No longer an element */
797 sv_unmagic(sv, PERL_MAGIC_tiedelem);
800 return Nullsv; /* element cannot be deleted */
802 #ifdef ENV_IS_CASELESS
803 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
804 key = SvPV(keysv, klen);
805 keysv = sv_2mortal(newSVpvn(key,klen));
806 (void)strupr(SvPVX(keysv));
812 xhv = (XPVHV*)SvANY(hv);
813 if (!xhv->xhv_array /* !HvARRAY(hv) */)
816 keysave = key = SvPV(keysv, klen);
817 is_utf8 = (SvUTF8(keysv) != 0);
820 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
822 if (SvREADONLY(hv)) {
823 Perl_hv_notallowed(aTHX_ is_utf8, key, klen, keysave);
827 PERL_HASH(hash, key, klen);
829 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
830 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
833 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
834 if (HeHASH(entry) != hash) /* strings can't be equal */
836 if (HeKLEN(entry) != klen)
838 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
840 if (HeKUTF8(entry) != (char)is_utf8)
844 *oentry = HeNEXT(entry);
846 xhv->xhv_fill--; /* HvFILL(hv)-- */
847 if (flags & G_DISCARD)
850 sv = sv_2mortal(HeVAL(entry));
851 HeVAL(entry) = &PL_sv_undef;
853 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
856 hv_free_ent(hv, entry);
857 xhv->xhv_keys--; /* HvKEYS(hv)-- */
866 =for apidoc hv_exists
868 Returns a boolean indicating whether the specified hash key exists. The
869 C<klen> is the length of the key.
875 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
881 bool is_utf8 = FALSE;
882 const char *keysave = key;
892 if (SvRMAGICAL(hv)) {
893 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
895 mg_copy((SV*)hv, sv, key, klen);
896 magic_existspack(sv, mg_find(sv, PERL_MAGIC_tiedelem));
899 #ifdef ENV_IS_CASELESS
900 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
901 sv = sv_2mortal(newSVpvn(key,klen));
902 key = strupr(SvPVX(sv));
907 xhv = (XPVHV*)SvANY(hv);
908 #ifndef DYNAMIC_ENV_FETCH
909 if (!xhv->xhv_array /* !HvARRAY(hv) */)
914 STRLEN tmplen = klen;
915 /* See the note in hv_fetch(). --jhi */
916 key = (char*)bytes_from_utf8((U8*)key, &tmplen, &is_utf8);
920 PERL_HASH(hash, key, klen);
922 #ifdef DYNAMIC_ENV_FETCH
923 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
926 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
927 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
928 for (; entry; entry = HeNEXT(entry)) {
929 if (HeHASH(entry) != hash) /* strings can't be equal */
931 if (HeKLEN(entry) != klen)
933 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
935 if (HeKUTF8(entry) != (char)is_utf8)
941 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
942 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
944 char *env = PerlEnv_ENVgetenv_len(key,&len);
946 sv = newSVpvn(env,len);
948 (void)hv_store(hv,key,klen,sv,hash);
960 =for apidoc hv_exists_ent
962 Returns a boolean indicating whether the specified hash key exists. C<hash>
963 can be a valid precomputed hash value, or 0 to ask for it to be
970 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
983 if (SvRMAGICAL(hv)) {
984 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
985 SV* svret = sv_newmortal();
987 keysv = sv_2mortal(newSVsv(keysv));
988 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
989 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
990 return SvTRUE(svret);
992 #ifdef ENV_IS_CASELESS
993 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
994 key = SvPV(keysv, klen);
995 keysv = sv_2mortal(newSVpvn(key,klen));
996 (void)strupr(SvPVX(keysv));
1002 xhv = (XPVHV*)SvANY(hv);
1003 #ifndef DYNAMIC_ENV_FETCH
1004 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1008 keysave = key = SvPV(keysv, klen);
1009 is_utf8 = (SvUTF8(keysv) != 0);
1011 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
1013 PERL_HASH(hash, key, klen);
1015 #ifdef DYNAMIC_ENV_FETCH
1016 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
1019 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1020 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1021 for (; entry; entry = HeNEXT(entry)) {
1022 if (HeHASH(entry) != hash) /* strings can't be equal */
1024 if (HeKLEN(entry) != klen)
1026 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1028 if (HeKUTF8(entry) != (char)is_utf8)
1034 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
1035 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1037 char *env = PerlEnv_ENVgetenv_len(key,&len);
1039 sv = newSVpvn(env,len);
1041 (void)hv_store_ent(hv,keysv,sv,hash);
1052 S_hsplit(pTHX_ HV *hv)
1054 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1055 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1056 register I32 newsize = oldsize * 2;
1058 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1062 register HE **oentry;
1065 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1066 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1072 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1077 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1078 if (oldsize >= 64) {
1079 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1080 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1083 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1087 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1088 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1089 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1092 for (i=0; i<oldsize; i++,aep++) {
1093 if (!*aep) /* non-existent */
1096 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1097 if ((HeHASH(entry) & newsize) != i) {
1098 *oentry = HeNEXT(entry);
1099 HeNEXT(entry) = *bep;
1101 xhv->xhv_fill++; /* HvFILL(hv)++ */
1106 oentry = &HeNEXT(entry);
1108 if (!*aep) /* everything moved */
1109 xhv->xhv_fill--; /* HvFILL(hv)-- */
1114 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1116 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1117 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1118 register I32 newsize;
1124 register HE **oentry;
1126 newsize = (I32) newmax; /* possible truncation here */
1127 if (newsize != newmax || newmax <= oldsize)
1129 while ((newsize & (1 + ~newsize)) != newsize) {
1130 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1132 if (newsize < newmax)
1134 if (newsize < newmax)
1135 return; /* overflow detection */
1137 a = xhv->xhv_array; /* HvARRAY(hv) */
1140 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1141 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1147 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1152 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1153 if (oldsize >= 64) {
1154 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1155 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1158 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1161 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1164 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1166 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1167 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1168 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1172 for (i=0; i<oldsize; i++,aep++) {
1173 if (!*aep) /* non-existent */
1175 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1176 if ((j = (HeHASH(entry) & newsize)) != i) {
1178 *oentry = HeNEXT(entry);
1179 if (!(HeNEXT(entry) = aep[j]))
1180 xhv->xhv_fill++; /* HvFILL(hv)++ */
1185 oentry = &HeNEXT(entry);
1187 if (!*aep) /* everything moved */
1188 xhv->xhv_fill--; /* HvFILL(hv)-- */
1195 Creates a new HV. The reference count is set to 1.
1204 register XPVHV* xhv;
1206 hv = (HV*)NEWSV(502,0);
1207 sv_upgrade((SV *)hv, SVt_PVHV);
1208 xhv = (XPVHV*)SvANY(hv);
1211 #ifndef NODEFAULT_SHAREKEYS
1212 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1214 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1215 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1216 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1217 (void)hv_iterinit(hv); /* so each() will start off right */
1222 Perl_newHVhv(pTHX_ HV *ohv)
1225 STRLEN hv_max, hv_fill;
1227 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1229 hv_max = HvMAX(ohv);
1231 if (!SvMAGICAL((SV *)ohv)) {
1232 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1233 int i, shared = !!HvSHAREKEYS(ohv);
1234 HE **ents, **oents = (HE **)HvARRAY(ohv);
1236 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1239 /* In each bucket... */
1240 for (i = 0; i <= hv_max; i++) {
1241 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1248 /* Copy the linked list of entries. */
1249 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1250 U32 hash = HeHASH(oent);
1251 char *key = HeKEY(oent);
1252 STRLEN len = HeKLEN_UTF8(oent);
1255 HeVAL(ent) = newSVsv(HeVAL(oent));
1256 HeKEY_hek(ent) = shared ? share_hek(key, len, hash)
1257 : save_hek(key, len, hash);
1268 HvFILL(hv) = hv_fill;
1269 HvKEYS(hv) = HvKEYS(ohv);
1273 /* Iterate over ohv, copying keys and values one at a time. */
1275 I32 riter = HvRITER(ohv);
1276 HE *eiter = HvEITER(ohv);
1278 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1279 while (hv_max && hv_max + 1 >= hv_fill * 2)
1280 hv_max = hv_max / 2;
1284 while ((entry = hv_iternext(ohv))) {
1285 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1286 newSVsv(HeVAL(entry)), HeHASH(entry));
1288 HvRITER(ohv) = riter;
1289 HvEITER(ohv) = eiter;
1296 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1303 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1304 PL_sub_generation++; /* may be deletion of method from stash */
1306 if (HeKLEN(entry) == HEf_SVKEY) {
1307 SvREFCNT_dec(HeKEY_sv(entry));
1308 Safefree(HeKEY_hek(entry));
1310 else if (HvSHAREKEYS(hv))
1311 unshare_hek(HeKEY_hek(entry));
1313 Safefree(HeKEY_hek(entry));
1318 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1322 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1323 PL_sub_generation++; /* may be deletion of method from stash */
1324 sv_2mortal(HeVAL(entry)); /* free between statements */
1325 if (HeKLEN(entry) == HEf_SVKEY) {
1326 sv_2mortal(HeKEY_sv(entry));
1327 Safefree(HeKEY_hek(entry));
1329 else if (HvSHAREKEYS(hv))
1330 unshare_hek(HeKEY_hek(entry));
1332 Safefree(HeKEY_hek(entry));
1337 =for apidoc hv_clear
1339 Clears a hash, making it empty.
1345 Perl_hv_clear(pTHX_ HV *hv)
1347 register XPVHV* xhv;
1350 xhv = (XPVHV*)SvANY(hv);
1352 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1353 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1354 if (xhv->xhv_array /* HvARRAY(hv) */)
1355 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1356 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1363 S_hfreeentries(pTHX_ HV *hv)
1365 register HE **array;
1367 register HE *oentry = Null(HE*);
1378 array = HvARRAY(hv);
1383 entry = HeNEXT(entry);
1384 hv_free_ent(hv, oentry);
1389 entry = array[riter];
1392 (void)hv_iterinit(hv);
1396 =for apidoc hv_undef
1404 Perl_hv_undef(pTHX_ HV *hv)
1406 register XPVHV* xhv;
1409 xhv = (XPVHV*)SvANY(hv);
1411 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1413 Safefree(HvNAME(hv));
1416 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1417 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1418 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1419 xhv->xhv_keys = 0; /* HvKEYS(hv) = 0 */
1426 =for apidoc hv_iterinit
1428 Prepares a starting point to traverse a hash table. Returns the number of
1429 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1430 currently only meaningful for hashes without tie magic.
1432 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1433 hash buckets that happen to be in use. If you still need that esoteric
1434 value, you can get it through the macro C<HvFILL(tb)>.
1440 Perl_hv_iterinit(pTHX_ HV *hv)
1442 register XPVHV* xhv;
1446 Perl_croak(aTHX_ "Bad hash");
1447 xhv = (XPVHV*)SvANY(hv);
1448 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1449 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1451 hv_free_ent(hv, entry);
1453 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1454 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1455 /* used to be xhv->xhv_fill before 5.004_65 */
1456 return xhv->xhv_keys; /* HvKEYS(hv) */
1460 =for apidoc hv_iternext
1462 Returns entries from a hash iterator. See C<hv_iterinit>.
1468 Perl_hv_iternext(pTHX_ HV *hv)
1470 register XPVHV* xhv;
1476 Perl_croak(aTHX_ "Bad hash");
1477 xhv = (XPVHV*)SvANY(hv);
1478 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1480 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1481 SV *key = sv_newmortal();
1483 sv_setsv(key, HeSVKEY_force(entry));
1484 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1490 /* one HE per MAGICAL hash */
1491 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1493 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1495 HeKEY_hek(entry) = hek;
1496 HeKLEN(entry) = HEf_SVKEY;
1498 magic_nextpack((SV*) hv,mg,key);
1500 /* force key to stay around until next time */
1501 HeSVKEY_set(entry, SvREFCNT_inc(key));
1502 return entry; /* beware, hent_val is not set */
1505 SvREFCNT_dec(HeVAL(entry));
1506 Safefree(HeKEY_hek(entry));
1508 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1511 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1512 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
1516 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1517 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1518 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1521 entry = HeNEXT(entry);
1523 xhv->xhv_riter++; /* HvRITER(hv)++ */
1524 if (xhv->xhv_riter > xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1525 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1528 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1529 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1532 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1534 hv_free_ent(hv, oldentry);
1537 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1542 =for apidoc hv_iterkey
1544 Returns the key from the current position of the hash iterator. See
1551 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1553 if (HeKLEN(entry) == HEf_SVKEY) {
1555 char *p = SvPV(HeKEY_sv(entry), len);
1560 *retlen = HeKLEN(entry);
1561 return HeKEY(entry);
1565 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1567 =for apidoc hv_iterkeysv
1569 Returns the key as an C<SV*> from the current position of the hash
1570 iterator. The return value will always be a mortal copy of the key. Also
1577 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1579 if (HeKLEN(entry) == HEf_SVKEY)
1580 return sv_mortalcopy(HeKEY_sv(entry));
1582 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1583 HeKLEN_UTF8(entry), HeHASH(entry)));
1587 =for apidoc hv_iterval
1589 Returns the value from the current position of the hash iterator. See
1596 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1598 if (SvRMAGICAL(hv)) {
1599 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1600 SV* sv = sv_newmortal();
1601 if (HeKLEN(entry) == HEf_SVKEY)
1602 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1603 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1607 return HeVAL(entry);
1611 =for apidoc hv_iternextsv
1613 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1620 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1623 if ( (he = hv_iternext(hv)) == NULL)
1625 *key = hv_iterkey(he, retlen);
1626 return hv_iterval(hv, he);
1630 =for apidoc hv_magic
1632 Adds magic to a hash. See C<sv_magic>.
1638 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1640 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1644 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1646 return HEK_KEY(share_hek(sv, len, hash));
1649 /* possibly free a shared string if no one has access to it
1650 * len and hash must both be valid for str.
1653 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1655 register XPVHV* xhv;
1657 register HE **oentry;
1660 bool is_utf8 = FALSE;
1661 const char *save = str;
1664 STRLEN tmplen = -len;
1666 /* See the note in hv_fetch(). --jhi */
1667 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1671 /* what follows is the moral equivalent of:
1672 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1673 if (--*Svp == Nullsv)
1674 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1676 xhv = (XPVHV*)SvANY(PL_strtab);
1677 /* assert(xhv_array != 0) */
1679 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1680 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1681 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1682 if (HeHASH(entry) != hash) /* strings can't be equal */
1684 if (HeKLEN(entry) != len)
1686 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1688 if (HeKUTF8(entry) != (char)is_utf8)
1691 if (--HeVAL(entry) == Nullsv) {
1692 *oentry = HeNEXT(entry);
1694 xhv->xhv_fill--; /* HvFILL(hv)-- */
1695 Safefree(HeKEY_hek(entry));
1697 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1701 UNLOCK_STRTAB_MUTEX;
1704 if (!found && ckWARN_d(WARN_INTERNAL))
1705 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1708 /* get a (constant) string ptr from the global string table
1709 * string will get added if it is not already there.
1710 * len and hash must both be valid for str.
1713 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1715 register XPVHV* xhv;
1717 register HE **oentry;
1720 bool is_utf8 = FALSE;
1721 const char *save = str;
1724 STRLEN tmplen = -len;
1726 /* See the note in hv_fetch(). --jhi */
1727 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1731 /* what follows is the moral equivalent of:
1733 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1734 hv_store(PL_strtab, str, len, Nullsv, hash);
1736 xhv = (XPVHV*)SvANY(PL_strtab);
1737 /* assert(xhv_array != 0) */
1739 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1740 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1741 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1742 if (HeHASH(entry) != hash) /* strings can't be equal */
1744 if (HeKLEN(entry) != len)
1746 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1748 if (HeKUTF8(entry) != (char)is_utf8)
1755 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1756 HeVAL(entry) = Nullsv;
1757 HeNEXT(entry) = *oentry;
1759 xhv->xhv_keys++; /* HvKEYS(hv)++ */
1760 if (i) { /* initial entry? */
1761 xhv->xhv_fill++; /* HvFILL(hv)++ */
1762 if (xhv->xhv_keys > xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */)
1767 ++HeVAL(entry); /* use value slot as REFCNT */
1768 UNLOCK_STRTAB_MUTEX;
1771 return HeKEY_hek(entry);