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
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)
79 New(54, k, HEK_BASESIZE + len + 1, char);
81 Copy(str, HEK_KEY(hek), len, char);
82 *(HEK_KEY(hek) + len) = '\0';
89 Perl_unshare_hek(pTHX_ HEK *hek)
91 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
94 #if defined(USE_ITHREADS)
96 Perl_he_dup(pTHX_ HE *e, bool shared)
102 /* look for it in the table first */
103 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
107 /* create anew and remember what it is */
109 ptr_table_store(PL_ptr_table, e, ret);
111 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
112 if (HeKLEN(e) == HEf_SVKEY)
113 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
115 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
117 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
118 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
121 #endif /* USE_ITHREADS */
123 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
129 Returns the SV which corresponds to the specified key in the hash. The
130 C<klen> is the length of the key. If C<lval> is set then the fetch will be
131 part of a store. Check that the return value is non-null before
132 dereferencing it to a C<SV*>.
134 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
135 information on how to use this function on tied hashes.
141 Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
151 if (SvRMAGICAL(hv)) {
152 if (mg_find((SV*)hv,'P')) {
155 mg_copy((SV*)hv, sv, key, klen);
157 return &PL_hv_fetch_sv;
159 #ifdef ENV_IS_CASELESS
160 else if (mg_find((SV*)hv,'E')) {
162 for (i = 0; i < klen; ++i)
163 if (isLOWER(key[i])) {
164 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
165 SV **ret = hv_fetch(hv, nkey, klen, 0);
167 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
174 xhv = (XPVHV*)SvANY(hv);
175 if (!xhv->xhv_array) {
177 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
178 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
181 Newz(503, xhv->xhv_array,
182 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
187 PERL_HASH(hash, key, klen);
189 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
190 for (; entry; entry = HeNEXT(entry)) {
191 if (HeHASH(entry) != hash) /* strings can't be equal */
193 if (HeKLEN(entry) != klen)
195 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
197 return &HeVAL(entry);
199 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
200 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
202 char *env = PerlEnv_ENVgetenv_len(key,&len);
204 sv = newSVpvn(env,len);
206 return hv_store(hv,key,klen,sv,hash);
210 if (lval) { /* gonna assign to this, so it better be there */
212 return hv_store(hv,key,klen,sv,hash);
217 /* returns a HE * structure with the all fields set */
218 /* note that hent_val will be a mortal sv for MAGICAL hashes */
220 =for apidoc hv_fetch_ent
222 Returns the hash entry which corresponds to the specified key in the hash.
223 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
224 if you want the function to compute it. IF C<lval> is set then the fetch
225 will be part of a store. Make sure the return value is non-null before
226 accessing it. The return value when C<tb> is a tied hash is a pointer to a
227 static location, so be sure to make a copy of the structure if you need to
230 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
231 information on how to use this function on tied hashes.
237 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
248 if (SvUTF8((SV*)hv) && !SvUTF8(keysv))
249 sv_utf8_upgrade(keysv);
251 if (SvRMAGICAL(hv)) {
252 if (mg_find((SV*)hv,'P')) {
255 keysv = sv_2mortal(newSVsv(keysv));
256 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
257 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
259 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
260 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
262 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
263 HeVAL(&PL_hv_fetch_ent_mh) = sv;
264 return &PL_hv_fetch_ent_mh;
266 #ifdef ENV_IS_CASELESS
267 else if (mg_find((SV*)hv,'E')) {
269 key = SvPV(keysv, klen);
270 for (i = 0; i < klen; ++i)
271 if (isLOWER(key[i])) {
272 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
273 (void)strupr(SvPVX(nkeysv));
274 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
276 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
283 xhv = (XPVHV*)SvANY(hv);
284 if (!xhv->xhv_array) {
286 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
287 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
290 Newz(503, xhv->xhv_array,
291 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
296 key = SvPV(keysv, klen);
299 PERL_HASH(hash, key, klen);
301 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
302 for (; entry; entry = HeNEXT(entry)) {
303 if (HeHASH(entry) != hash) /* strings can't be equal */
305 if (HeKLEN(entry) != klen)
307 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
311 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
312 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
314 char *env = PerlEnv_ENVgetenv_len(key,&len);
316 sv = newSVpvn(env,len);
318 return hv_store_ent(hv,keysv,sv,hash);
322 if (lval) { /* gonna assign to this, so it better be there */
324 return hv_store_ent(hv,keysv,sv,hash);
330 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
332 MAGIC *mg = SvMAGIC(hv);
336 if (isUPPER(mg->mg_type)) {
338 switch (mg->mg_type) {
341 *needs_store = FALSE;
344 mg = mg->mg_moremagic;
351 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
352 the length of the key. The C<hash> parameter is the precomputed hash
353 value; if it is zero then Perl will compute it. The return value will be
354 NULL if the operation failed or if the value did not need to be actually
355 stored within the hash (as in the case of tied hashes). Otherwise it can
356 be dereferenced to get the original C<SV*>. Note that the caller is
357 responsible for suitably incrementing the reference count of C<val> before
358 the call, and decrementing it if the function returned NULL.
360 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
361 information on how to use this function on tied hashes.
367 Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
372 register HE **oentry;
377 xhv = (XPVHV*)SvANY(hv);
381 hv_magic_check (hv, &needs_copy, &needs_store);
383 mg_copy((SV*)hv, val, key, klen);
384 if (!xhv->xhv_array && !needs_store)
386 #ifdef ENV_IS_CASELESS
387 else if (mg_find((SV*)hv,'E')) {
388 SV *sv = sv_2mortal(newSVpvn(key,klen));
389 key = strupr(SvPVX(sv));
396 PERL_HASH(hash, key, klen);
399 Newz(505, xhv->xhv_array,
400 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
402 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
405 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
406 if (HeHASH(entry) != hash) /* strings can't be equal */
408 if (HeKLEN(entry) != klen)
410 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
412 SvREFCNT_dec(HeVAL(entry));
414 return &HeVAL(entry);
419 HeKEY_hek(entry) = share_hek(key, klen, hash);
420 else /* gotta do the real thing */
421 HeKEY_hek(entry) = save_hek(key, klen, hash);
423 HeNEXT(entry) = *oentry;
427 if (i) { /* initial entry? */
429 if (xhv->xhv_keys > xhv->xhv_max)
433 return &HeVAL(entry);
437 =for apidoc hv_store_ent
439 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
440 parameter is the precomputed hash value; if it is zero then Perl will
441 compute it. The return value is the new hash entry so created. It will be
442 NULL if the operation failed or if the value did not need to be actually
443 stored within the hash (as in the case of tied hashes). Otherwise the
444 contents of the return value can be accessed using the C<He???> macros
445 described here. Note that the caller is responsible for suitably
446 incrementing the reference count of C<val> before the call, and
447 decrementing it if the function returned NULL.
449 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
450 information on how to use this function on tied hashes.
456 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
463 register HE **oentry;
468 xhv = (XPVHV*)SvANY(hv);
470 if (SvUTF8((SV*)hv) && !SvUTF8(keysv))
471 sv_utf8_upgrade(keysv);
472 else if (SvUTF8(keysv) && !SvUTF8((SV*)hv)) { /* Upgrade hash */
474 /* XXX Need to save iterator to prevent weird things during "each" */
475 (void)hv_iterinit(hv);
476 while (entry = hv_iternext(hv)) {
477 if (HeKLEN(entry) != HEf_SVKEY) /* Upgrade to SV key */
478 HeSVKEY_set(entry, newSVpvn(HeKEY(entry), HeKLEN(entry)));
479 sv_utf8_upgrade(HeKEY_sv(entry));
487 hv_magic_check (hv, &needs_copy, &needs_store);
489 bool save_taint = PL_tainted;
491 PL_tainted = SvTAINTED(keysv);
492 keysv = sv_2mortal(newSVsv(keysv));
493 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
494 TAINT_IF(save_taint);
495 if (!xhv->xhv_array && !needs_store)
497 #ifdef ENV_IS_CASELESS
498 else if (mg_find((SV*)hv,'E')) {
499 key = SvPV(keysv, klen);
500 keysv = sv_2mortal(newSVpvn(key,klen));
501 (void)strupr(SvPVX(keysv));
508 key = SvPV(keysv, klen);
511 PERL_HASH(hash, key, klen);
514 Newz(505, xhv->xhv_array,
515 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
517 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
520 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
521 if (HeHASH(entry) != hash) /* strings can't be equal */
523 if (HeKLEN(entry) != klen)
525 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
527 SvREFCNT_dec(HeVAL(entry));
534 HeKEY_hek(entry) = share_hek(key, klen, hash);
535 else /* gotta do the real thing */
536 HeKEY_hek(entry) = save_hek(key, klen, hash);
538 HeNEXT(entry) = *oentry;
542 if (i) { /* initial entry? */
544 if (xhv->xhv_keys > xhv->xhv_max)
552 =for apidoc hv_delete
554 Deletes a key/value pair in the hash. The value SV is removed from the
555 hash and returned to the caller. The C<klen> is the length of the key.
556 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
563 Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
569 register HE **oentry;
575 if (SvRMAGICAL(hv)) {
578 hv_magic_check (hv, &needs_copy, &needs_store);
580 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
584 if (mg_find(sv, 'p')) {
585 sv_unmagic(sv, 'p'); /* No longer an element */
588 return Nullsv; /* element cannot be deleted */
590 #ifdef ENV_IS_CASELESS
591 else if (mg_find((SV*)hv,'E')) {
592 sv = sv_2mortal(newSVpvn(key,klen));
593 key = strupr(SvPVX(sv));
598 xhv = (XPVHV*)SvANY(hv);
602 PERL_HASH(hash, key, klen);
604 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
607 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
608 if (HeHASH(entry) != hash) /* strings can't be equal */
610 if (HeKLEN(entry) != klen)
612 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
614 *oentry = HeNEXT(entry);
617 if (flags & G_DISCARD)
620 sv = sv_2mortal(HeVAL(entry));
621 HeVAL(entry) = &PL_sv_undef;
623 if (entry == xhv->xhv_eiter)
626 hv_free_ent(hv, entry);
634 =for apidoc hv_delete_ent
636 Deletes a key/value pair in the hash. The value SV is removed from the
637 hash and returned to the caller. The C<flags> value will normally be zero;
638 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
639 precomputed hash value, or 0 to ask for it to be computed.
645 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
652 register HE **oentry;
657 if (SvRMAGICAL(hv)) {
660 hv_magic_check (hv, &needs_copy, &needs_store);
662 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
666 if (mg_find(sv, 'p')) {
667 sv_unmagic(sv, 'p'); /* No longer an element */
670 return Nullsv; /* element cannot be deleted */
672 #ifdef ENV_IS_CASELESS
673 else if (mg_find((SV*)hv,'E')) {
674 key = SvPV(keysv, klen);
675 keysv = sv_2mortal(newSVpvn(key,klen));
676 (void)strupr(SvPVX(keysv));
682 xhv = (XPVHV*)SvANY(hv);
686 key = SvPV(keysv, klen);
689 PERL_HASH(hash, key, klen);
691 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
694 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
695 if (HeHASH(entry) != hash) /* strings can't be equal */
697 if (HeKLEN(entry) != klen)
699 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
701 *oentry = HeNEXT(entry);
704 if (flags & G_DISCARD)
707 sv = sv_2mortal(HeVAL(entry));
708 HeVAL(entry) = &PL_sv_undef;
710 if (entry == xhv->xhv_eiter)
713 hv_free_ent(hv, entry);
721 =for apidoc hv_exists
723 Returns a boolean indicating whether the specified hash key exists. The
724 C<klen> is the length of the key.
730 Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
740 if (SvRMAGICAL(hv)) {
741 if (mg_find((SV*)hv,'P')) {
744 mg_copy((SV*)hv, sv, key, klen);
745 magic_existspack(sv, mg_find(sv, 'p'));
748 #ifdef ENV_IS_CASELESS
749 else if (mg_find((SV*)hv,'E')) {
750 sv = sv_2mortal(newSVpvn(key,klen));
751 key = strupr(SvPVX(sv));
756 xhv = (XPVHV*)SvANY(hv);
757 #ifndef DYNAMIC_ENV_FETCH
762 PERL_HASH(hash, key, klen);
764 #ifdef DYNAMIC_ENV_FETCH
765 if (!xhv->xhv_array) entry = Null(HE*);
768 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
769 for (; entry; entry = HeNEXT(entry)) {
770 if (HeHASH(entry) != hash) /* strings can't be equal */
772 if (HeKLEN(entry) != klen)
774 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
778 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
779 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
781 char *env = PerlEnv_ENVgetenv_len(key,&len);
783 sv = newSVpvn(env,len);
785 (void)hv_store(hv,key,klen,sv,hash);
795 =for apidoc hv_exists_ent
797 Returns a boolean indicating whether the specified hash key exists. C<hash>
798 can be a valid precomputed hash value, or 0 to ask for it to be
805 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
816 if (SvRMAGICAL(hv)) {
817 if (mg_find((SV*)hv,'P')) {
818 dTHR; /* just for SvTRUE */
820 keysv = sv_2mortal(newSVsv(keysv));
821 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
822 magic_existspack(sv, mg_find(sv, 'p'));
825 #ifdef ENV_IS_CASELESS
826 else if (mg_find((SV*)hv,'E')) {
827 key = SvPV(keysv, klen);
828 keysv = sv_2mortal(newSVpvn(key,klen));
829 (void)strupr(SvPVX(keysv));
835 xhv = (XPVHV*)SvANY(hv);
836 #ifndef DYNAMIC_ENV_FETCH
841 key = SvPV(keysv, klen);
843 PERL_HASH(hash, key, klen);
845 #ifdef DYNAMIC_ENV_FETCH
846 if (!xhv->xhv_array) entry = Null(HE*);
849 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
850 for (; entry; entry = HeNEXT(entry)) {
851 if (HeHASH(entry) != hash) /* strings can't be equal */
853 if (HeKLEN(entry) != klen)
855 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
859 #ifdef DYNAMIC_ENV_FETCH /* is it out there? */
860 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
862 char *env = PerlEnv_ENVgetenv_len(key,&len);
864 sv = newSVpvn(env,len);
866 (void)hv_store_ent(hv,keysv,sv,hash);
875 S_hsplit(pTHX_ HV *hv)
877 register XPVHV* xhv = (XPVHV*)SvANY(hv);
878 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
879 register I32 newsize = oldsize * 2;
881 register char *a = xhv->xhv_array;
885 register HE **oentry;
888 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
889 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
895 #define MALLOC_OVERHEAD 16
896 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
901 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
903 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
906 Safefree(xhv->xhv_array);
910 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
911 xhv->xhv_max = --newsize;
915 for (i=0; i<oldsize; i++,aep++) {
916 if (!*aep) /* non-existent */
919 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
920 if ((HeHASH(entry) & newsize) != i) {
921 *oentry = HeNEXT(entry);
922 HeNEXT(entry) = *bep;
929 oentry = &HeNEXT(entry);
931 if (!*aep) /* everything moved */
937 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
939 register XPVHV* xhv = (XPVHV*)SvANY(hv);
940 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
941 register I32 newsize;
947 register HE **oentry;
949 newsize = (I32) newmax; /* possible truncation here */
950 if (newsize != newmax || newmax <= oldsize)
952 while ((newsize & (1 + ~newsize)) != newsize) {
953 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
955 if (newsize < newmax)
957 if (newsize < newmax)
958 return; /* overflow detection */
963 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
964 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
970 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
975 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
977 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
980 Safefree(xhv->xhv_array);
983 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
986 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
988 xhv->xhv_max = --newsize;
990 if (!xhv->xhv_fill) /* skip rest if no entries */
994 for (i=0; i<oldsize; i++,aep++) {
995 if (!*aep) /* non-existent */
997 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
998 if ((j = (HeHASH(entry) & newsize)) != i) {
1000 *oentry = HeNEXT(entry);
1001 if (!(HeNEXT(entry) = aep[j]))
1007 oentry = &HeNEXT(entry);
1009 if (!*aep) /* everything moved */
1017 Creates a new HV. The reference count is set to 1.
1026 register XPVHV* xhv;
1028 hv = (HV*)NEWSV(502,0);
1029 sv_upgrade((SV *)hv, SVt_PVHV);
1030 xhv = (XPVHV*)SvANY(hv);
1033 #ifndef NODEFAULT_SHAREKEYS
1034 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1036 xhv->xhv_max = 7; /* start with 8 buckets */
1038 xhv->xhv_pmroot = 0;
1039 (void)hv_iterinit(hv); /* so each() will start off right */
1044 Perl_newHVhv(pTHX_ HV *ohv)
1047 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1048 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1051 while (hv_max && hv_max + 1 >= hv_fill * 2)
1052 hv_max = hv_max / 2; /* Is always 2^n-1 */
1058 if (! SvTIED_mg((SV*)ohv, 'P')) {
1065 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1066 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1070 while ((entry = hv_iternext(ohv))) {
1071 hv_store(hv, HeKEY(entry), HeKLEN(entry),
1072 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
1074 HvRITER(ohv) = hv_riter;
1075 HvEITER(ohv) = hv_eiter;
1082 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1089 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
1090 PL_sub_generation++; /* may be deletion of method from stash */
1092 if (HeKLEN(entry) == HEf_SVKEY) {
1093 SvREFCNT_dec(HeKEY_sv(entry));
1094 Safefree(HeKEY_hek(entry));
1096 else if (HvSHAREKEYS(hv))
1097 unshare_hek(HeKEY_hek(entry));
1099 Safefree(HeKEY_hek(entry));
1104 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1108 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
1109 PL_sub_generation++; /* may be deletion of method from stash */
1110 sv_2mortal(HeVAL(entry)); /* free between statements */
1111 if (HeKLEN(entry) == HEf_SVKEY) {
1112 sv_2mortal(HeKEY_sv(entry));
1113 Safefree(HeKEY_hek(entry));
1115 else if (HvSHAREKEYS(hv))
1116 unshare_hek(HeKEY_hek(entry));
1118 Safefree(HeKEY_hek(entry));
1123 =for apidoc hv_clear
1125 Clears a hash, making it empty.
1131 Perl_hv_clear(pTHX_ HV *hv)
1133 register XPVHV* xhv;
1136 xhv = (XPVHV*)SvANY(hv);
1141 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
1148 S_hfreeentries(pTHX_ HV *hv)
1150 register HE **array;
1152 register HE *oentry = Null(HE*);
1163 array = HvARRAY(hv);
1168 entry = HeNEXT(entry);
1169 hv_free_ent(hv, oentry);
1174 entry = array[riter];
1177 (void)hv_iterinit(hv);
1181 =for apidoc hv_undef
1189 Perl_hv_undef(pTHX_ HV *hv)
1191 register XPVHV* xhv;
1194 xhv = (XPVHV*)SvANY(hv);
1196 Safefree(xhv->xhv_array);
1198 Safefree(HvNAME(hv));
1202 xhv->xhv_max = 7; /* it's a normal hash */
1211 =for apidoc hv_iterinit
1213 Prepares a starting point to traverse a hash table. Returns the number of
1214 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1215 currently only meaningful for hashes without tie magic.
1217 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1218 hash buckets that happen to be in use. If you still need that esoteric
1219 value, you can get it through the macro C<HvFILL(tb)>.
1225 Perl_hv_iterinit(pTHX_ HV *hv)
1227 register XPVHV* xhv;
1231 Perl_croak(aTHX_ "Bad hash");
1232 xhv = (XPVHV*)SvANY(hv);
1233 entry = xhv->xhv_eiter;
1234 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1236 hv_free_ent(hv, entry);
1238 xhv->xhv_riter = -1;
1239 xhv->xhv_eiter = Null(HE*);
1240 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
1244 =for apidoc hv_iternext
1246 Returns entries from a hash iterator. See C<hv_iterinit>.
1252 Perl_hv_iternext(pTHX_ HV *hv)
1254 register XPVHV* xhv;
1260 Perl_croak(aTHX_ "Bad hash");
1261 xhv = (XPVHV*)SvANY(hv);
1262 oldentry = entry = xhv->xhv_eiter;
1264 if ((mg = SvTIED_mg((SV*)hv, 'P'))) {
1265 SV *key = sv_newmortal();
1267 sv_setsv(key, HeSVKEY_force(entry));
1268 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1274 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
1276 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1278 HeKEY_hek(entry) = hek;
1279 HeKLEN(entry) = HEf_SVKEY;
1281 magic_nextpack((SV*) hv,mg,key);
1283 /* force key to stay around until next time */
1284 HeSVKEY_set(entry, SvREFCNT_inc(key));
1285 return entry; /* beware, hent_val is not set */
1288 SvREFCNT_dec(HeVAL(entry));
1289 Safefree(HeKEY_hek(entry));
1291 xhv->xhv_eiter = Null(HE*);
1294 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1295 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1299 if (!xhv->xhv_array)
1300 Newz(506, xhv->xhv_array,
1301 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
1303 entry = HeNEXT(entry);
1306 if (xhv->xhv_riter > xhv->xhv_max) {
1307 xhv->xhv_riter = -1;
1310 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1313 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1315 hv_free_ent(hv, oldentry);
1318 xhv->xhv_eiter = entry;
1323 =for apidoc hv_iterkey
1325 Returns the key from the current position of the hash iterator. See
1332 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1334 if (HeKLEN(entry) == HEf_SVKEY) {
1336 char *p = SvPV(HeKEY_sv(entry), len);
1341 *retlen = HeKLEN(entry);
1342 return HeKEY(entry);
1346 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1348 =for apidoc hv_iterkeysv
1350 Returns the key as an C<SV*> from the current position of the hash
1351 iterator. The return value will always be a mortal copy of the key. Also
1358 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1360 if (HeKLEN(entry) == HEf_SVKEY)
1361 return sv_mortalcopy(HeKEY_sv(entry));
1363 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1364 HeKLEN(entry), HeHASH(entry)));
1369 =for apidoc hv_iterval
1371 Returns the value from the current position of the hash iterator. See
1378 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1380 if (SvRMAGICAL(hv)) {
1381 if (mg_find((SV*)hv,'P')) {
1382 SV* sv = sv_newmortal();
1383 if (HeKLEN(entry) == HEf_SVKEY)
1384 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1385 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1389 return HeVAL(entry);
1393 =for apidoc hv_iternextsv
1395 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1402 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1405 if ( (he = hv_iternext(hv)) == NULL)
1407 *key = hv_iterkey(he, retlen);
1408 return hv_iterval(hv, he);
1412 =for apidoc hv_magic
1414 Adds magic to a hash. See C<sv_magic>.
1420 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1422 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1426 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1428 return HEK_KEY(share_hek(sv, len, hash));
1431 /* possibly free a shared string if no one has access to it
1432 * len and hash must both be valid for str.
1435 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1437 register XPVHV* xhv;
1439 register HE **oentry;
1443 /* what follows is the moral equivalent of:
1444 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1445 if (--*Svp == Nullsv)
1446 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1448 xhv = (XPVHV*)SvANY(PL_strtab);
1449 /* assert(xhv_array != 0) */
1451 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1452 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1453 if (HeHASH(entry) != hash) /* strings can't be equal */
1455 if (HeKLEN(entry) != len)
1457 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1460 if (--HeVAL(entry) == Nullsv) {
1461 *oentry = HeNEXT(entry);
1464 Safefree(HeKEY_hek(entry));
1470 UNLOCK_STRTAB_MUTEX;
1474 if (!found && ckWARN_d(WARN_INTERNAL))
1475 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
1479 /* get a (constant) string ptr from the global string table
1480 * string will get added if it is not already there.
1481 * len and hash must both be valid for str.
1484 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
1486 register XPVHV* xhv;
1488 register HE **oentry;
1492 /* what follows is the moral equivalent of:
1494 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1495 hv_store(PL_strtab, str, len, Nullsv, hash);
1497 xhv = (XPVHV*)SvANY(PL_strtab);
1498 /* assert(xhv_array != 0) */
1500 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1501 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
1502 if (HeHASH(entry) != hash) /* strings can't be equal */
1504 if (HeKLEN(entry) != len)
1506 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1513 HeKEY_hek(entry) = save_hek(str, len, hash);
1514 HeVAL(entry) = Nullsv;
1515 HeNEXT(entry) = *oentry;
1518 if (i) { /* initial entry? */
1520 if (xhv->xhv_keys > xhv->xhv_max)
1525 ++HeVAL(entry); /* use value slot as REFCNT */
1526 UNLOCK_STRTAB_MUTEX;
1527 return HeKEY_hek(entry);