3 * Copyright (c) 1991-2001, Larry Wall
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
11 * "I sit beside the fire and think of all that I have seen." --Bilbo
27 PL_he_root = HeNEXT(he);
36 HeNEXT(p) = (HE*)PL_he_root;
47 New(54, ptr, 1008/sizeof(XPV), XPV);
48 ptr->xpv_pv = (char*)PL_he_arenaroot;
49 PL_he_arenaroot = ptr;
52 heend = &he[1008 / sizeof(HE) - 1];
55 HeNEXT(he) = (HE*)(he + 1);
63 #define new_HE() (HE*)safemalloc(sizeof(HE))
64 #define del_HE(p) safefree((char*)p)
68 #define new_HE() new_he()
69 #define del_HE(p) del_he(p)
74 S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
85 New(54, k, HEK_BASESIZE + len + 1, char);
87 Copy(str, HEK_KEY(hek), len, char);
90 HEK_UTF8(hek) = (char)is_utf8;
95 Perl_unshare_hek(pTHX_ HEK *hek)
97 unsharepvn(HEK_KEY(hek),HEK_UTF8(hek)?-HEK_LEN(hek):HEK_LEN(hek),
101 #if defined(USE_ITHREADS)
103 Perl_he_dup(pTHX_ HE *e, bool shared)
109 /* look for it in the table first */
110 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
114 /* create anew and remember what it is */
116 ptr_table_store(PL_ptr_table, e, ret);
118 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
119 if (HeKLEN(e) == HEf_SVKEY)
120 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
122 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
124 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN_UTF8(e), HeHASH(e));
125 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
128 #endif /* USE_ITHREADS */
130 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
136 Returns the SV which corresponds to the specified key in the hash. The
137 C<klen> is the length of the key. If C<lval> is set then the fetch will be
138 part of a store. Check that the return value is non-null before
139 dereferencing it to a C<SV*>.
141 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
142 information on how to use this function on tied hashes.
148 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen, I32 lval)
154 bool is_utf8 = FALSE;
155 const char *keysave = key;
165 if (SvRMAGICAL(hv)) {
166 if (mg_find((SV*)hv,'P')) {
168 mg_copy((SV*)hv, sv, key, klen);
170 return &PL_hv_fetch_sv;
172 #ifdef ENV_IS_CASELESS
173 else if (mg_find((SV*)hv,'E')) {
175 for (i = 0; i < klen; ++i)
176 if (isLOWER(key[i])) {
177 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
178 SV **ret = hv_fetch(hv, nkey, klen, 0);
180 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
187 xhv = (XPVHV*)SvANY(hv);
188 if (!xhv->xhv_array) {
190 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
191 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
194 Newz(503, xhv->xhv_array,
195 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
200 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
201 key = (char*)bytes_from_utf8((U8*)key, (STRLEN*)&klen, &is_utf8);
203 PERL_HASH(hash, key, klen);
205 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
206 for (; entry; entry = HeNEXT(entry)) {
207 if (HeHASH(entry) != hash) /* strings can't be equal */
209 if (HeKLEN(entry) != klen)
211 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
213 if (HeKUTF8(entry) != (char)is_utf8)
217 return &HeVAL(entry);
219 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
220 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
222 char *env = PerlEnv_ENVgetenv_len(key,&len);
224 sv = newSVpvn(env,len);
228 return hv_store(hv,key,klen,sv,hash);
232 if (lval) { /* gonna assign to this, so it better be there */
234 if (key != keysave) { /* must be is_utf8 == 0 */
235 SV **ret = hv_store(hv,key,klen,sv,hash);
240 return hv_store(hv,key,is_utf8?-klen:klen,sv,hash);
247 /* returns a HE * structure with the all fields set */
248 /* note that hent_val will be a mortal sv for MAGICAL hashes */
250 =for apidoc hv_fetch_ent
252 Returns the hash entry which corresponds to the specified key in the hash.
253 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
254 if you want the function to compute it. IF C<lval> is set then the fetch
255 will be part of a store. Make sure the return value is non-null before
256 accessing it. The return value when C<tb> is a tied hash is a pointer to a
257 static location, so be sure to make a copy of the structure if you need to
260 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
261 information on how to use this function on tied hashes.
267 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
280 if (SvRMAGICAL(hv)) {
281 if (mg_find((SV*)hv,'P')) {
283 keysv = sv_2mortal(newSVsv(keysv));
284 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
285 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
287 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
288 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
290 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
291 HeVAL(&PL_hv_fetch_ent_mh) = sv;
292 return &PL_hv_fetch_ent_mh;
294 #ifdef ENV_IS_CASELESS
295 else if (mg_find((SV*)hv,'E')) {
297 key = SvPV(keysv, klen);
298 for (i = 0; i < klen; ++i)
299 if (isLOWER(key[i])) {
300 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
301 (void)strupr(SvPVX(nkeysv));
302 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
304 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
311 xhv = (XPVHV*)SvANY(hv);
312 if (!xhv->xhv_array) {
314 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
315 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
318 Newz(503, xhv->xhv_array,
319 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
324 keysave = key = SvPV(keysv, klen);
325 is_utf8 = (SvUTF8(keysv)!=0);
327 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
328 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
331 PERL_HASH(hash, key, klen);
333 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
334 for (; entry; entry = HeNEXT(entry)) {
335 if (HeHASH(entry) != hash) /* strings can't be equal */
337 if (HeKLEN(entry) != klen)
339 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
341 if (HeKUTF8(entry) != (char)is_utf8)
347 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
348 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
350 char *env = PerlEnv_ENVgetenv_len(key,&len);
352 sv = newSVpvn(env,len);
354 return hv_store_ent(hv,keysv,sv,hash);
360 if (lval) { /* gonna assign to this, so it better be there */
362 return hv_store_ent(hv,keysv,sv,hash);
368 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
370 MAGIC *mg = SvMAGIC(hv);
374 if (isUPPER(mg->mg_type)) {
376 switch (mg->mg_type) {
379 *needs_store = FALSE;
382 mg = mg->mg_moremagic;
389 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
390 the length of the key. The C<hash> parameter is the precomputed hash
391 value; if it is zero then Perl will compute it. The return value will be
392 NULL if the operation failed or if the value did not need to be actually
393 stored within the hash (as in the case of tied hashes). Otherwise it can
394 be dereferenced to get the original C<SV*>. Note that the caller is
395 responsible for suitably incrementing the reference count of C<val> before
396 the call, and decrementing it if the function returned NULL.
398 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
399 information on how to use this function on tied hashes.
405 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen, SV *val, register U32 hash)
410 register HE **oentry;
411 bool is_utf8 = FALSE;
412 const char *keysave = key;
422 xhv = (XPVHV*)SvANY(hv);
426 hv_magic_check (hv, &needs_copy, &needs_store);
428 mg_copy((SV*)hv, val, key, klen);
429 if (!xhv->xhv_array && !needs_store)
431 #ifdef ENV_IS_CASELESS
432 else if (mg_find((SV*)hv,'E')) {
433 SV *sv = sv_2mortal(newSVpvn(key,klen));
434 key = strupr(SvPVX(sv));
440 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
441 key = (char*)bytes_from_utf8((U8*)key, (STRLEN*)&klen, &is_utf8);
444 PERL_HASH(hash, key, klen);
447 Newz(505, xhv->xhv_array,
448 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
450 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
453 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
454 if (HeHASH(entry) != hash) /* strings can't be equal */
456 if (HeKLEN(entry) != klen)
458 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
460 if (HeKUTF8(entry) != (char)is_utf8)
462 SvREFCNT_dec(HeVAL(entry));
466 return &HeVAL(entry);
471 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
472 else /* gotta do the real thing */
473 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
477 HeNEXT(entry) = *oentry;
481 if (i) { /* initial entry? */
483 if (xhv->xhv_keys > xhv->xhv_max)
487 return &HeVAL(entry);
491 =for apidoc hv_store_ent
493 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
494 parameter is the precomputed hash value; if it is zero then Perl will
495 compute it. The return value is the new hash entry so created. It will be
496 NULL if the operation failed or if the value did not need to be actually
497 stored within the hash (as in the case of tied hashes). Otherwise the
498 contents of the return value can be accessed using the C<He???> macros
499 described here. Note that the caller is responsible for suitably
500 incrementing the reference count of C<val> before the call, and
501 decrementing it if the function returned NULL.
503 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
504 information on how to use this function on tied hashes.
510 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
517 register HE **oentry;
524 xhv = (XPVHV*)SvANY(hv);
528 hv_magic_check (hv, &needs_copy, &needs_store);
530 bool save_taint = PL_tainted;
532 PL_tainted = SvTAINTED(keysv);
533 keysv = sv_2mortal(newSVsv(keysv));
534 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
535 TAINT_IF(save_taint);
536 if (!xhv->xhv_array && !needs_store)
538 #ifdef ENV_IS_CASELESS
539 else if (mg_find((SV*)hv,'E')) {
540 key = SvPV(keysv, klen);
541 keysv = sv_2mortal(newSVpvn(key,klen));
542 (void)strupr(SvPVX(keysv));
549 keysave = key = SvPV(keysv, klen);
550 is_utf8 = (SvUTF8(keysv) != 0);
552 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
553 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
556 PERL_HASH(hash, key, klen);
559 Newz(505, xhv->xhv_array,
560 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
562 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
565 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
566 if (HeHASH(entry) != hash) /* strings can't be equal */
568 if (HeKLEN(entry) != klen)
570 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
572 if (HeKUTF8(entry) != (char)is_utf8)
574 SvREFCNT_dec(HeVAL(entry));
583 HeKEY_hek(entry) = share_hek(key, is_utf8?-klen:klen, hash);
584 else /* gotta do the real thing */
585 HeKEY_hek(entry) = save_hek(key, is_utf8?-klen:klen, hash);
589 HeNEXT(entry) = *oentry;
593 if (i) { /* initial entry? */
595 if (xhv->xhv_keys > xhv->xhv_max)
603 =for apidoc hv_delete
605 Deletes a key/value pair in the hash. The value SV is removed from the
606 hash and returned to the caller. The C<klen> is the length of the key.
607 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
614 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen, I32 flags)
620 register HE **oentry;
623 bool is_utf8 = FALSE;
624 const char *keysave = key;
632 if (SvRMAGICAL(hv)) {
635 hv_magic_check (hv, &needs_copy, &needs_store);
637 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
641 if (mg_find(sv, 'p')) {
642 sv_unmagic(sv, 'p'); /* No longer an element */
645 return Nullsv; /* element cannot be deleted */
647 #ifdef ENV_IS_CASELESS
648 else if (mg_find((SV*)hv,'E')) {
649 sv = sv_2mortal(newSVpvn(key,klen));
650 key = strupr(SvPVX(sv));
655 xhv = (XPVHV*)SvANY(hv);
659 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
660 key = (char*)bytes_from_utf8((U8*)key, (STRLEN*)&klen, &is_utf8);
662 PERL_HASH(hash, key, klen);
664 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
667 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
668 if (HeHASH(entry) != hash) /* strings can't be equal */
670 if (HeKLEN(entry) != klen)
672 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
674 if (HeKUTF8(entry) != (char)is_utf8)
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);
700 =for apidoc hv_delete_ent
702 Deletes a key/value pair in the hash. The value SV is removed from the
703 hash and returned to the caller. The C<flags> value will normally be zero;
704 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
705 precomputed hash value, or 0 to ask for it to be computed.
711 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
718 register HE **oentry;
725 if (SvRMAGICAL(hv)) {
728 hv_magic_check (hv, &needs_copy, &needs_store);
730 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
734 if (mg_find(sv, 'p')) {
735 sv_unmagic(sv, 'p'); /* No longer an element */
738 return Nullsv; /* element cannot be deleted */
740 #ifdef ENV_IS_CASELESS
741 else if (mg_find((SV*)hv,'E')) {
742 key = SvPV(keysv, klen);
743 keysv = sv_2mortal(newSVpvn(key,klen));
744 (void)strupr(SvPVX(keysv));
750 xhv = (XPVHV*)SvANY(hv);
754 keysave = key = SvPV(keysv, klen);
755 is_utf8 = (SvUTF8(keysv) != 0);
757 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
758 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
761 PERL_HASH(hash, key, klen);
763 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
766 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
767 if (HeHASH(entry) != hash) /* strings can't be equal */
769 if (HeKLEN(entry) != klen)
771 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
773 if (HeKUTF8(entry) != (char)is_utf8)
777 *oentry = HeNEXT(entry);
780 if (flags & G_DISCARD)
783 sv = sv_2mortal(HeVAL(entry));
784 HeVAL(entry) = &PL_sv_undef;
786 if (entry == xhv->xhv_eiter)
789 hv_free_ent(hv, entry);
799 =for apidoc hv_exists
801 Returns a boolean indicating whether the specified hash key exists. The
802 C<klen> is the length of the key.
808 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen)
814 bool is_utf8 = FALSE;
815 const char *keysave = key;
825 if (SvRMAGICAL(hv)) {
826 if (mg_find((SV*)hv,'P')) {
828 mg_copy((SV*)hv, sv, key, klen);
829 magic_existspack(sv, mg_find(sv, 'p'));
832 #ifdef ENV_IS_CASELESS
833 else if (mg_find((SV*)hv,'E')) {
834 sv = sv_2mortal(newSVpvn(key,klen));
835 key = strupr(SvPVX(sv));
840 xhv = (XPVHV*)SvANY(hv);
841 #ifndef DYNAMIC_ENV_FETCH
846 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
847 key = (char*)bytes_from_utf8((U8*)key, (STRLEN*)&klen, &is_utf8);
849 PERL_HASH(hash, key, klen);
851 #ifdef DYNAMIC_ENV_FETCH
852 if (!xhv->xhv_array) entry = Null(HE*);
855 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
856 for (; entry; entry = HeNEXT(entry)) {
857 if (HeHASH(entry) != hash) /* strings can't be equal */
859 if (HeKLEN(entry) != klen)
861 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
863 if (HeKUTF8(entry) != (char)is_utf8)
869 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
870 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
872 char *env = PerlEnv_ENVgetenv_len(key,&len);
874 sv = newSVpvn(env,len);
876 (void)hv_store(hv,key,klen,sv,hash);
888 =for apidoc hv_exists_ent
890 Returns a boolean indicating whether the specified hash key exists. C<hash>
891 can be a valid precomputed hash value, or 0 to ask for it to be
898 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
911 if (SvRMAGICAL(hv)) {
912 if (mg_find((SV*)hv,'P')) {
914 keysv = sv_2mortal(newSVsv(keysv));
915 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
916 magic_existspack(sv, mg_find(sv, 'p'));
919 #ifdef ENV_IS_CASELESS
920 else if (mg_find((SV*)hv,'E')) {
921 key = SvPV(keysv, klen);
922 keysv = sv_2mortal(newSVpvn(key,klen));
923 (void)strupr(SvPVX(keysv));
929 xhv = (XPVHV*)SvANY(hv);
930 #ifndef DYNAMIC_ENV_FETCH
935 keysave = key = SvPV(keysv, klen);
936 is_utf8 = (SvUTF8(keysv) != 0);
937 if (is_utf8 && !(PL_hints & HINT_UTF8_DISTINCT))
938 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
940 PERL_HASH(hash, key, klen);
942 #ifdef DYNAMIC_ENV_FETCH
943 if (!xhv->xhv_array) entry = Null(HE*);
946 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
947 for (; entry; entry = HeNEXT(entry)) {
948 if (HeHASH(entry) != hash) /* strings can't be equal */
950 if (HeKLEN(entry) != klen)
952 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
954 if (HeKUTF8(entry) != (char)is_utf8)
960 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
961 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
963 char *env = PerlEnv_ENVgetenv_len(key,&len);
965 sv = newSVpvn(env,len);
967 (void)hv_store_ent(hv,keysv,sv,hash);
978 S_hsplit(pTHX_ HV *hv)
980 register XPVHV* xhv = (XPVHV*)SvANY(hv);
981 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
982 register I32 newsize = oldsize * 2;
984 register char *a = xhv->xhv_array;
988 register HE **oentry;
991 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
992 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
998 #define MALLOC_OVERHEAD 16
999 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1004 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
1005 if (oldsize >= 64) {
1006 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1009 Safefree(xhv->xhv_array);
1013 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1014 xhv->xhv_max = --newsize;
1018 for (i=0; i<oldsize; i++,aep++) {
1019 if (!*aep) /* non-existent */
1022 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1023 if ((HeHASH(entry) & newsize) != i) {
1024 *oentry = HeNEXT(entry);
1025 HeNEXT(entry) = *bep;
1032 oentry = &HeNEXT(entry);
1034 if (!*aep) /* everything moved */
1040 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1042 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1043 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
1044 register I32 newsize;
1050 register HE **oentry;
1052 newsize = (I32) newmax; /* possible truncation here */
1053 if (newsize != newmax || newmax <= oldsize)
1055 while ((newsize & (1 + ~newsize)) != newsize) {
1056 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1058 if (newsize < newmax)
1060 if (newsize < newmax)
1061 return; /* overflow detection */
1066 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1067 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1073 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1078 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
1079 if (oldsize >= 64) {
1080 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1083 Safefree(xhv->xhv_array);
1086 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1089 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1091 xhv->xhv_max = --newsize;
1093 if (!xhv->xhv_fill) /* skip rest if no entries */
1097 for (i=0; i<oldsize; i++,aep++) {
1098 if (!*aep) /* non-existent */
1100 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1101 if ((j = (HeHASH(entry) & newsize)) != i) {
1103 *oentry = HeNEXT(entry);
1104 if (!(HeNEXT(entry) = aep[j]))
1110 oentry = &HeNEXT(entry);
1112 if (!*aep) /* everything moved */
1120 Creates a new HV. The reference count is set to 1.
1129 register XPVHV* xhv;
1131 hv = (HV*)NEWSV(502,0);
1132 sv_upgrade((SV *)hv, SVt_PVHV);
1133 xhv = (XPVHV*)SvANY(hv);
1136 #ifndef NODEFAULT_SHAREKEYS
1137 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1139 xhv->xhv_max = 7; /* start with 8 buckets */
1141 xhv->xhv_pmroot = 0;
1142 (void)hv_iterinit(hv); /* so each() will start off right */
1147 Perl_newHVhv(pTHX_ HV *ohv)
1150 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1151 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1154 while (hv_max && hv_max + 1 >= hv_fill * 2)
1155 hv_max = hv_max / 2; /* Is always 2^n-1 */
1161 if (! SvTIED_mg((SV*)ohv, 'P')) {
1168 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1169 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1173 while ((entry = hv_iternext(ohv))) {
1174 hv_store(hv, HeKEY(entry), HeKLEN_UTF8(entry),
1175 newSVsv(HeVAL(entry)), HeHASH(entry));
1177 HvRITER(ohv) = hv_riter;
1178 HvEITER(ohv) = hv_eiter;
1185 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1192 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1193 PL_sub_generation++; /* may be deletion of method from stash */
1195 if (HeKLEN(entry) == HEf_SVKEY) {
1196 SvREFCNT_dec(HeKEY_sv(entry));
1197 Safefree(HeKEY_hek(entry));
1199 else if (HvSHAREKEYS(hv))
1200 unshare_hek(HeKEY_hek(entry));
1202 Safefree(HeKEY_hek(entry));
1207 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1211 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1212 PL_sub_generation++; /* may be deletion of method from stash */
1213 sv_2mortal(HeVAL(entry)); /* free between statements */
1214 if (HeKLEN(entry) == HEf_SVKEY) {
1215 sv_2mortal(HeKEY_sv(entry));
1216 Safefree(HeKEY_hek(entry));
1218 else if (HvSHAREKEYS(hv))
1219 unshare_hek(HeKEY_hek(entry));
1221 Safefree(HeKEY_hek(entry));
1226 =for apidoc hv_clear
1228 Clears a hash, making it empty.
1234 Perl_hv_clear(pTHX_ HV *hv)
1236 register XPVHV* xhv;
1239 xhv = (XPVHV*)SvANY(hv);
1244 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
1251 S_hfreeentries(pTHX_ HV *hv)
1253 register HE **array;
1255 register HE *oentry = Null(HE*);
1266 array = HvARRAY(hv);
1271 entry = HeNEXT(entry);
1272 hv_free_ent(hv, oentry);
1277 entry = array[riter];
1280 (void)hv_iterinit(hv);
1284 =for apidoc hv_undef
1292 Perl_hv_undef(pTHX_ HV *hv)
1294 register XPVHV* xhv;
1297 xhv = (XPVHV*)SvANY(hv);
1299 Safefree(xhv->xhv_array);
1301 Safefree(HvNAME(hv));
1305 xhv->xhv_max = 7; /* it's a normal hash */
1314 =for apidoc hv_iterinit
1316 Prepares a starting point to traverse a hash table. Returns the number of
1317 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1318 currently only meaningful for hashes without tie magic.
1320 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1321 hash buckets that happen to be in use. If you still need that esoteric
1322 value, you can get it through the macro C<HvFILL(tb)>.
1328 Perl_hv_iterinit(pTHX_ HV *hv)
1330 register XPVHV* xhv;
1334 Perl_croak(aTHX_ "Bad hash");
1335 xhv = (XPVHV*)SvANY(hv);
1336 entry = xhv->xhv_eiter;
1337 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1339 hv_free_ent(hv, entry);
1341 xhv->xhv_riter = -1;
1342 xhv->xhv_eiter = Null(HE*);
1343 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1347 =for apidoc hv_iternext
1349 Returns entries from a hash iterator. See C<hv_iterinit>.
1355 Perl_hv_iternext(pTHX_ HV *hv)
1357 register XPVHV* xhv;
1363 Perl_croak(aTHX_ "Bad hash");
1364 xhv = (XPVHV*)SvANY(hv);
1365 oldentry = entry = xhv->xhv_eiter;
1367 if ((mg = SvTIED_mg((SV*)hv, 'P'))) {
1368 SV *key = sv_newmortal();
1370 sv_setsv(key, HeSVKEY_force(entry));
1371 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1377 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
1379 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1381 HeKEY_hek(entry) = hek;
1382 HeKLEN(entry) = HEf_SVKEY;
1384 magic_nextpack((SV*) hv,mg,key);
1386 /* force key to stay around until next time */
1387 HeSVKEY_set(entry, SvREFCNT_inc(key));
1388 return entry; /* beware, hent_val is not set */
1391 SvREFCNT_dec(HeVAL(entry));
1392 Safefree(HeKEY_hek(entry));
1394 xhv->xhv_eiter = Null(HE*);
1397 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1398 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1402 if (!xhv->xhv_array)
1403 Newz(506, xhv->xhv_array,
1404 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1406 entry = HeNEXT(entry);
1409 if (xhv->xhv_riter > xhv->xhv_max) {
1410 xhv->xhv_riter = -1;
1413 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1416 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1418 hv_free_ent(hv, oldentry);
1421 xhv->xhv_eiter = entry;
1426 =for apidoc hv_iterkey
1428 Returns the key from the current position of the hash iterator. See
1435 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1437 if (HeKLEN(entry) == HEf_SVKEY) {
1439 char *p = SvPV(HeKEY_sv(entry), len);
1444 *retlen = HeKLEN(entry);
1445 return HeKEY(entry);
1449 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1451 =for apidoc hv_iterkeysv
1453 Returns the key as an C<SV*> from the current position of the hash
1454 iterator. The return value will always be a mortal copy of the key. Also
1461 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1463 if (HeKLEN(entry) == HEf_SVKEY)
1464 return sv_mortalcopy(HeKEY_sv(entry));
1466 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1467 HeKLEN_UTF8(entry), HeHASH(entry)));
1471 =for apidoc hv_iterval
1473 Returns the value from the current position of the hash iterator. See
1480 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1482 if (SvRMAGICAL(hv)) {
1483 if (mg_find((SV*)hv,'P')) {
1484 SV* sv = sv_newmortal();
1485 if (HeKLEN(entry) == HEf_SVKEY)
1486 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1487 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1491 return HeVAL(entry);
1495 =for apidoc hv_iternextsv
1497 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1504 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1507 if ( (he = hv_iternext(hv)) == NULL)
1509 *key = hv_iterkey(he, retlen);
1510 return hv_iterval(hv, he);
1514 =for apidoc hv_magic
1516 Adds magic to a hash. See C<sv_magic>.
1522 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1524 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1528 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1530 return HEK_KEY(share_hek(sv, len, hash));
1533 /* possibly free a shared string if no one has access to it
1534 * len and hash must both be valid for str.
1537 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1539 register XPVHV* xhv;
1541 register HE **oentry;
1544 bool is_utf8 = FALSE;
1545 const char *save = str;
1550 if (!(PL_hints & HINT_UTF8_DISTINCT))
1551 str = (char*)bytes_from_utf8((U8*)str, (STRLEN*)&len, &is_utf8);
1554 /* what follows is the moral equivalent of:
1555 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1556 if (--*Svp == Nullsv)
1557 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1559 xhv = (XPVHV*)SvANY(PL_strtab);
1560 /* assert(xhv_array != 0) */
1562 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1563 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1564 if (HeHASH(entry) != hash) /* strings can't be equal */
1566 if (HeKLEN(entry) != len)
1568 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1570 if (HeKUTF8(entry) != (char)is_utf8)
1573 if (--HeVAL(entry) == Nullsv) {
1574 *oentry = HeNEXT(entry);
1577 Safefree(HeKEY_hek(entry));
1583 UNLOCK_STRTAB_MUTEX;
1586 if (!found && ckWARN_d(WARN_INTERNAL))
1587 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1590 /* get a (constant) string ptr from the global string table
1591 * string will get added if it is not already there.
1592 * len and hash must both be valid for str.
1595 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1597 register XPVHV* xhv;
1599 register HE **oentry;
1602 bool is_utf8 = FALSE;
1603 const char *save = str;
1608 if (!(PL_hints & HINT_UTF8_DISTINCT))
1609 str = (char*)bytes_from_utf8((U8*)str, (STRLEN*)&len, &is_utf8);
1612 /* what follows is the moral equivalent of:
1614 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1615 hv_store(PL_strtab, str, len, Nullsv, hash);
1617 xhv = (XPVHV*)SvANY(PL_strtab);
1618 /* assert(xhv_array != 0) */
1620 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1621 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1622 if (HeHASH(entry) != hash) /* strings can't be equal */
1624 if (HeKLEN(entry) != len)
1626 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1628 if (HeKUTF8(entry) != (char)is_utf8)
1635 HeKEY_hek(entry) = save_hek(str, is_utf8?-len:len, hash);
1636 HeVAL(entry) = Nullsv;
1637 HeNEXT(entry) = *oentry;
1640 if (i) { /* initial entry? */
1642 if (xhv->xhv_keys > xhv->xhv_max)
1647 ++HeVAL(entry); /* use value slot as REFCNT */
1648 UNLOCK_STRTAB_MUTEX;
1651 return HeKEY_hek(entry);