3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, 2004, 2005, 2006, by Larry Wall and others
6 * You may distribute under the terms of either the GNU General Public
7 * License or the Artistic License, as specified in the README file.
12 * "I sit beside the fire and think of all that I have seen." --Bilbo
16 =head1 Hash Manipulation Functions
18 A HV structure represents a Perl hash. It consists mainly of an array
19 of pointers, each of which points to a linked list of HE structures. The
20 array is indexed by the hash function of the key, so each linked list
21 represents all the hash entries with the same hash value. Each HE contains
22 a pointer to the actual value, plus a pointer to a HEK structure which
23 holds the key and hash value.
31 #define PERL_HASH_INTERNAL_ACCESS
34 #define HV_MAX_LENGTH_BEFORE_SPLIT 14
42 Newx(ptr, PERL_ARENA_SIZE/sizeof(XPV), XPV);
43 ptr->xpv_pv = (char*)PL_he_arenaroot;
44 PL_he_arenaroot = ptr;
47 heend = &he[PERL_ARENA_SIZE / sizeof(HE) - 1];
50 HeNEXT(he) = (HE*)(he + 1);
64 PL_he_root = HeNEXT(he);
73 HeNEXT(p) = (HE*)PL_he_root;
80 #define new_HE() (HE*)safemalloc(sizeof(HE))
81 #define del_HE(p) safefree((char*)p)
85 #define new_HE() new_he()
86 #define del_HE(p) del_he(p)
91 S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
93 const int flags_masked = flags & HVhek_MASK;
97 Newx(k, HEK_BASESIZE + len + 2, char);
99 Copy(str, HEK_KEY(hek), len, char);
100 HEK_KEY(hek)[len] = 0;
102 HEK_HASH(hek) = hash;
103 HEK_FLAGS(hek) = (unsigned char)flags_masked;
105 if (flags & HVhek_FREEKEY)
110 /* free the pool of temporary HE/HEK pairs returned by hv_fetch_ent
114 Perl_free_tied_hv_pool(pTHX)
116 HE *he = PL_hv_fetch_ent_mh;
119 Safefree(HeKEY_hek(he));
123 PL_hv_fetch_ent_mh = Nullhe;
126 #if defined(USE_ITHREADS)
128 Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
134 /* look for it in the table first */
135 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
139 /* create anew and remember what it is */
141 ptr_table_store(PL_ptr_table, e, ret);
143 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
144 if (HeKLEN(e) == HEf_SVKEY) {
146 Newx(k, HEK_BASESIZE + sizeof(SV*), char);
147 HeKEY_hek(ret) = (HEK*)k;
148 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
151 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
154 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
156 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
159 #endif /* USE_ITHREADS */
162 S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
165 SV *sv = sv_newmortal();
166 if (!(flags & HVhek_FREEKEY)) {
167 sv_setpvn(sv, key, klen);
170 /* Need to free saved eventually assign to mortal SV */
171 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
172 sv_usepvn(sv, (char *) key, klen);
174 if (flags & HVhek_UTF8) {
177 Perl_croak(aTHX_ msg, sv);
180 /* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
183 #define HV_FETCH_ISSTORE 0x01
184 #define HV_FETCH_ISEXISTS 0x02
185 #define HV_FETCH_LVALUE 0x04
186 #define HV_FETCH_JUST_SV 0x08
191 Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
192 the length of the key. The C<hash> parameter is the precomputed hash
193 value; if it is zero then Perl will compute it. The return value will be
194 NULL if the operation failed or if the value did not need to be actually
195 stored within the hash (as in the case of tied hashes). Otherwise it can
196 be dereferenced to get the original C<SV*>. Note that the caller is
197 responsible for suitably incrementing the reference count of C<val> before
198 the call, and decrementing it if the function returned NULL. Effectively
199 a successful hv_store takes ownership of one reference to C<val>. This is
200 usually what you want; a newly created SV has a reference count of one, so
201 if all your code does is create SVs then store them in a hash, hv_store
202 will own the only reference to the new SV, and your code doesn't need to do
203 anything further to tidy up. hv_store is not implemented as a call to
204 hv_store_ent, and does not create a temporary SV for the key, so if your
205 key data is not already in SV form then use hv_store in preference to
208 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
209 information on how to use this function on tied hashes.
215 Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
228 hek = hv_fetch_common (hv, NULL, key, klen, flags,
229 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
230 return hek ? &HeVAL(hek) : NULL;
234 Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
235 register U32 hash, int flags)
237 HE * const hek = hv_fetch_common (hv, NULL, key, klen, flags,
238 (HV_FETCH_ISSTORE|HV_FETCH_JUST_SV), val, hash);
239 return hek ? &HeVAL(hek) : NULL;
243 =for apidoc hv_store_ent
245 Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
246 parameter is the precomputed hash value; if it is zero then Perl will
247 compute it. The return value is the new hash entry so created. It will be
248 NULL if the operation failed or if the value did not need to be actually
249 stored within the hash (as in the case of tied hashes). Otherwise the
250 contents of the return value can be accessed using the C<He?> macros
251 described here. Note that the caller is responsible for suitably
252 incrementing the reference count of C<val> before the call, and
253 decrementing it if the function returned NULL. Effectively a successful
254 hv_store_ent takes ownership of one reference to C<val>. This is
255 usually what you want; a newly created SV has a reference count of one, so
256 if all your code does is create SVs then store them in a hash, hv_store
257 will own the only reference to the new SV, and your code doesn't need to do
258 anything further to tidy up. Note that hv_store_ent only reads the C<key>;
259 unlike C<val> it does not take ownership of it, so maintaining the correct
260 reference count on C<key> is entirely the caller's responsibility. hv_store
261 is not implemented as a call to hv_store_ent, and does not create a temporary
262 SV for the key, so if your key data is not already in SV form then use
263 hv_store in preference to hv_store_ent.
265 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
266 information on how to use this function on tied hashes.
272 Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
274 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISSTORE, val, hash);
278 =for apidoc hv_exists
280 Returns a boolean indicating whether the specified hash key exists. The
281 C<klen> is the length of the key.
287 Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
299 return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0, 0)
306 Returns the SV which corresponds to the specified key in the hash. The
307 C<klen> is the length of the key. If C<lval> is set then the fetch will be
308 part of a store. Check that the return value is non-null before
309 dereferencing it to an C<SV*>.
311 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
312 information on how to use this function on tied hashes.
318 Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
331 hek = hv_fetch_common (hv, NULL, key, klen, flags,
332 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0),
334 return hek ? &HeVAL(hek) : NULL;
338 =for apidoc hv_exists_ent
340 Returns a boolean indicating whether the specified hash key exists. C<hash>
341 can be a valid precomputed hash value, or 0 to ask for it to be
348 Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
350 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, 0, hash)
354 /* returns an HE * structure with the all fields set */
355 /* note that hent_val will be a mortal sv for MAGICAL hashes */
357 =for apidoc hv_fetch_ent
359 Returns the hash entry which corresponds to the specified key in the hash.
360 C<hash> must be a valid precomputed hash number for the given C<key>, or 0
361 if you want the function to compute it. IF C<lval> is set then the fetch
362 will be part of a store. Make sure the return value is non-null before
363 accessing it. The return value when C<tb> is a tied hash is a pointer to a
364 static location, so be sure to make a copy of the structure if you need to
367 See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
368 information on how to use this function on tied hashes.
374 Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
376 return hv_fetch_common(hv, keysv, NULL, 0, 0,
377 (lval ? HV_FETCH_LVALUE : 0), Nullsv, hash);
381 S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
382 int flags, int action, SV *val, register U32 hash)
395 if (flags & HVhek_FREEKEY)
397 key = SvPV_const(keysv, klen);
399 is_utf8 = (SvUTF8(keysv) != 0);
401 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
404 xhv = (XPVHV*)SvANY(hv);
406 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS)))
408 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
411 /* XXX should be able to skimp on the HE/HEK here when
412 HV_FETCH_JUST_SV is true. */
415 keysv = newSVpvn(key, klen);
420 keysv = newSVsv(keysv);
422 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
424 /* grab a fake HE/HEK pair from the pool or make a new one */
425 entry = PL_hv_fetch_ent_mh;
427 PL_hv_fetch_ent_mh = HeNEXT(entry);
431 Newx(k, HEK_BASESIZE + sizeof(SV*), char);
432 HeKEY_hek(entry) = (HEK*)k;
434 HeNEXT(entry) = Nullhe;
435 HeSVKEY_set(entry, keysv);
437 sv_upgrade(sv, SVt_PVLV);
439 /* so we can free entry when freeing sv */
440 LvTARG(sv) = (SV*)entry;
442 /* XXX remove at some point? */
443 if (flags & HVhek_FREEKEY)
448 #ifdef ENV_IS_CASELESS
449 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
451 for (i = 0; i < klen; ++i)
452 if (isLOWER(key[i])) {
453 /* Would be nice if we had a routine to do the
454 copy and upercase in a single pass through. */
455 const char *nkey = strupr(savepvn(key,klen));
456 /* Note that this fetch is for nkey (the uppercased
457 key) whereas the store is for key (the original) */
458 entry = hv_fetch_common(hv, Nullsv, nkey, klen,
459 HVhek_FREEKEY, /* free nkey */
460 0 /* non-LVAL fetch */,
461 Nullsv /* no value */,
462 0 /* compute hash */);
463 if (!entry && (action & HV_FETCH_LVALUE)) {
464 /* This call will free key if necessary.
465 Do it this way to encourage compiler to tail
467 entry = hv_fetch_common(hv, keysv, key, klen,
468 flags, HV_FETCH_ISSTORE,
471 if (flags & HVhek_FREEKEY)
479 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
480 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
481 /* I don't understand why hv_exists_ent has svret and sv,
482 whereas hv_exists only had one. */
483 SV * const svret = sv_newmortal();
486 if (keysv || is_utf8) {
488 keysv = newSVpvn(key, klen);
491 keysv = newSVsv(keysv);
493 mg_copy((SV*)hv, sv, (char *)sv_2mortal(keysv), HEf_SVKEY);
495 mg_copy((SV*)hv, sv, key, klen);
497 if (flags & HVhek_FREEKEY)
499 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
500 /* This cast somewhat evil, but I'm merely using NULL/
501 not NULL to return the boolean exists.
502 And I know hv is not NULL. */
503 return SvTRUE(svret) ? (HE *)hv : NULL;
505 #ifdef ENV_IS_CASELESS
506 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
507 /* XXX This code isn't UTF8 clean. */
508 char * const keysave = (char * const)key;
509 /* Will need to free this, so set FREEKEY flag. */
510 key = savepvn(key,klen);
511 key = (const char*)strupr((char*)key);
516 if (flags & HVhek_FREEKEY) {
519 flags |= HVhek_FREEKEY;
523 else if (action & HV_FETCH_ISSTORE) {
526 hv_magic_check (hv, &needs_copy, &needs_store);
528 const bool save_taint = PL_tainted;
529 if (keysv || is_utf8) {
531 keysv = newSVpvn(key, klen);
535 PL_tainted = SvTAINTED(keysv);
536 keysv = sv_2mortal(newSVsv(keysv));
537 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
539 mg_copy((SV*)hv, val, key, klen);
542 TAINT_IF(save_taint);
543 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
544 if (flags & HVhek_FREEKEY)
548 #ifdef ENV_IS_CASELESS
549 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
550 /* XXX This code isn't UTF8 clean. */
551 const char *keysave = key;
552 /* Will need to free this, so set FREEKEY flag. */
553 key = savepvn(key,klen);
554 key = (const char*)strupr((char*)key);
559 if (flags & HVhek_FREEKEY) {
562 flags |= HVhek_FREEKEY;
569 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
570 if ((action & (HV_FETCH_LVALUE | HV_FETCH_ISSTORE))
571 #ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
572 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
577 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
579 HvARRAY(hv) = (HE**)array;
581 #ifdef DYNAMIC_ENV_FETCH
582 else if (action & HV_FETCH_ISEXISTS) {
583 /* for an %ENV exists, if we do an insert it's by a recursive
584 store call, so avoid creating HvARRAY(hv) right now. */
588 /* XXX remove at some point? */
589 if (flags & HVhek_FREEKEY)
597 char * const keysave = (char * const)key;
598 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
602 flags &= ~HVhek_UTF8;
603 if (key != keysave) {
604 if (flags & HVhek_FREEKEY)
606 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
611 PERL_HASH_INTERNAL(hash, key, klen);
612 /* We don't have a pointer to the hv, so we have to replicate the
613 flag into every HEK, so that hv_iterkeysv can see it. */
614 /* And yes, you do need this even though you are not "storing" because
615 you can flip the flags below if doing an lval lookup. (And that
616 was put in to give the semantics Andreas was expecting.) */
617 flags |= HVhek_REHASH;
619 /* Not enough shared hash key scalars around to make this worthwhile
620 (about 4% slowdown in perlbench with this in)
621 if (keysv && (SvIsCOW_shared_hash(keysv))) {
622 hash = SvSHARED_HASH(keysv);
626 PERL_HASH(hash, key, klen);
630 masked_flags = (flags & HVhek_MASK);
632 #ifdef DYNAMIC_ENV_FETCH
633 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
637 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
638 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
640 for (; entry; entry = HeNEXT(entry)) {
641 if (!HeKEY_hek(entry))
643 if (HeHASH(entry) != hash) /* strings can't be equal */
645 if (HeKLEN(entry) != (I32)klen)
647 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
649 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
652 if (action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE)) {
653 if (HeKFLAGS(entry) != masked_flags) {
654 /* We match if HVhek_UTF8 bit in our flags and hash key's
655 match. But if entry was set previously with HVhek_WASUTF8
656 and key now doesn't (or vice versa) then we should change
657 the key's flag, as this is assignment. */
658 if (HvSHAREKEYS(hv)) {
659 /* Need to swap the key we have for a key with the flags we
660 need. As keys are shared we can't just write to the
661 flag, so we share the new one, unshare the old one. */
662 HEK *new_hek = share_hek_flags(key, klen, hash,
664 unshare_hek (HeKEY_hek(entry));
665 HeKEY_hek(entry) = new_hek;
668 HeKFLAGS(entry) = masked_flags;
669 if (masked_flags & HVhek_ENABLEHVKFLAGS)
672 if (HeVAL(entry) == &PL_sv_placeholder) {
673 /* yes, can store into placeholder slot */
674 if (action & HV_FETCH_LVALUE) {
676 /* This preserves behaviour with the old hv_fetch
677 implementation which at this point would bail out
678 with a break; (at "if we find a placeholder, we
679 pretend we haven't found anything")
681 That break mean that if a placeholder were found, it
682 caused a call into hv_store, which in turn would
683 check magic, and if there is no magic end up pretty
684 much back at this point (in hv_store's code). */
687 /* LVAL fetch which actaully needs a store. */
689 xhv->xhv_placeholders--;
692 if (val != &PL_sv_placeholder)
693 xhv->xhv_placeholders--;
696 } else if (action & HV_FETCH_ISSTORE) {
697 SvREFCNT_dec(HeVAL(entry));
700 } else if (HeVAL(entry) == &PL_sv_placeholder) {
701 /* if we find a placeholder, we pretend we haven't found
705 if (flags & HVhek_FREEKEY)
709 #ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
710 if (!(action & HV_FETCH_ISSTORE)
711 && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
713 const char * const env = PerlEnv_ENVgetenv_len(key,&len);
715 sv = newSVpvn(env,len);
717 return hv_fetch_common(hv,keysv,key,klen,flags,HV_FETCH_ISSTORE,sv,
723 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
724 S_hv_notallowed(aTHX_ flags, key, klen,
725 "Attempt to access disallowed key '%"SVf"' in"
726 " a restricted hash");
728 if (!(action & (HV_FETCH_LVALUE|HV_FETCH_ISSTORE))) {
729 /* Not doing some form of store, so return failure. */
730 if (flags & HVhek_FREEKEY)
734 if (action & HV_FETCH_LVALUE) {
737 /* At this point the old hv_fetch code would call to hv_store,
738 which in turn might do some tied magic. So we need to make that
739 magic check happen. */
740 /* gonna assign to this, so it better be there */
741 return hv_fetch_common(hv, keysv, key, klen, flags,
742 HV_FETCH_ISSTORE, val, hash);
743 /* XXX Surely that could leak if the fetch-was-store fails?
744 Just like the hv_fetch. */
748 /* Welcome to hv_store... */
750 if (!xhv->xhv_array) {
751 /* Not sure if we can get here. I think the only case of oentry being
752 NULL is for %ENV with dynamic env fetch. But that should disappear
753 with magic in the previous code. */
756 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
758 HvARRAY(hv) = (HE**)array;
761 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
764 /* share_hek_flags will do the free for us. This might be considered
767 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
768 else /* gotta do the real thing */
769 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
771 HeNEXT(entry) = *oentry;
774 if (val == &PL_sv_placeholder)
775 xhv->xhv_placeholders++;
776 if (masked_flags & HVhek_ENABLEHVKFLAGS)
780 const HE *counter = HeNEXT(entry);
782 xhv->xhv_keys++; /* HvKEYS(hv)++ */
783 if (!counter) { /* initial entry? */
784 xhv->xhv_fill++; /* HvFILL(hv)++ */
785 } else if (xhv->xhv_keys > (IV)xhv->xhv_max) {
787 } else if(!HvREHASH(hv)) {
790 while ((counter = HeNEXT(counter)))
793 if (n_links > HV_MAX_LENGTH_BEFORE_SPLIT) {
794 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit
795 bucket splits on a rehashed hash, as we're not going to
796 split it again, and if someone is lucky (evil) enough to
797 get all the keys in one list they could exhaust our memory
798 as we repeatedly double the number of buckets on every
799 entry. Linear search feels a less worse thing to do. */
809 S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
811 const MAGIC *mg = SvMAGIC(hv);
815 if (isUPPER(mg->mg_type)) {
817 switch (mg->mg_type) {
818 case PERL_MAGIC_tied:
820 *needs_store = FALSE;
821 return; /* We've set all there is to set. */
824 mg = mg->mg_moremagic;
829 =for apidoc hv_scalar
831 Evaluates the hash in scalar context and returns the result. Handles magic when the hash is tied.
837 Perl_hv_scalar(pTHX_ HV *hv)
842 if ((SvRMAGICAL(hv) && (mg = mg_find((SV*)hv, PERL_MAGIC_tied)))) {
843 sv = magic_scalarpack(hv, mg);
849 Perl_sv_setpvf(aTHX_ sv, "%ld/%ld",
850 (long)HvFILL(hv), (long)HvMAX(hv) + 1);
858 =for apidoc hv_delete
860 Deletes a key/value pair in the hash. The value SV is removed from the
861 hash and returned to the caller. The C<klen> is the length of the key.
862 The C<flags> value will normally be zero; if set to G_DISCARD then NULL
869 Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
876 k_flags |= HVhek_UTF8;
880 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
884 =for apidoc hv_delete_ent
886 Deletes a key/value pair in the hash. The value SV is removed from the
887 hash and returned to the caller. The C<flags> value will normally be zero;
888 if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
889 precomputed hash value, or 0 to ask for it to be computed.
895 Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
897 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
901 S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
902 int k_flags, I32 d_flags, U32 hash)
906 register HE **oentry;
907 HE *const *first_entry;
916 if (k_flags & HVhek_FREEKEY)
918 key = SvPV_const(keysv, klen);
920 is_utf8 = (SvUTF8(keysv) != 0);
922 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
925 if (SvRMAGICAL(hv)) {
928 hv_magic_check (hv, &needs_copy, &needs_store);
931 entry = hv_fetch_common(hv, keysv, key, klen,
932 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
934 sv = entry ? HeVAL(entry) : NULL;
940 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
941 /* No longer an element */
942 sv_unmagic(sv, PERL_MAGIC_tiedelem);
945 return Nullsv; /* element cannot be deleted */
947 #ifdef ENV_IS_CASELESS
948 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
949 /* XXX This code isn't UTF8 clean. */
950 keysv = sv_2mortal(newSVpvn(key,klen));
951 if (k_flags & HVhek_FREEKEY) {
954 key = strupr(SvPVX(keysv));
963 xhv = (XPVHV*)SvANY(hv);
964 if (!xhv->xhv_array /* !HvARRAY(hv) */)
968 const char *keysave = key;
969 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
972 k_flags |= HVhek_UTF8;
974 k_flags &= ~HVhek_UTF8;
975 if (key != keysave) {
976 if (k_flags & HVhek_FREEKEY) {
977 /* This shouldn't happen if our caller does what we expect,
978 but strictly the API allows it. */
981 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
983 HvHASKFLAGS_on((SV*)hv);
987 PERL_HASH_INTERNAL(hash, key, klen);
989 /* Not enough shared hash key scalars around to make this worthwhile
990 (about 4% slowdown in perlbench with this in)
991 if (keysv && (SvIsCOW_shared_hash(keysv))) {
992 hash = SvSHARED_HASH(keysv);
996 PERL_HASH(hash, key, klen);
1000 masked_flags = (k_flags & HVhek_MASK);
1002 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1003 first_entry = oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1005 for (; entry; oentry = &HeNEXT(entry), entry = *oentry) {
1006 if (HeHASH(entry) != hash) /* strings can't be equal */
1008 if (HeKLEN(entry) != (I32)klen)
1010 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
1012 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
1015 /* if placeholder is here, it's already been deleted.... */
1016 if (HeVAL(entry) == &PL_sv_placeholder)
1018 if (k_flags & HVhek_FREEKEY)
1022 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1023 S_hv_notallowed(aTHX_ k_flags, key, klen,
1024 "Attempt to delete readonly key '%"SVf"' from"
1025 " a restricted hash");
1027 if (k_flags & HVhek_FREEKEY)
1030 if (d_flags & G_DISCARD)
1033 sv = sv_2mortal(HeVAL(entry));
1034 HeVAL(entry) = &PL_sv_placeholder;
1038 * If a restricted hash, rather than really deleting the entry, put
1039 * a placeholder there. This marks the key as being "approved", so
1040 * we can still access via not-really-existing key without raising
1043 if (SvREADONLY(hv)) {
1044 SvREFCNT_dec(HeVAL(entry));
1045 HeVAL(entry) = &PL_sv_placeholder;
1046 /* We'll be saving this slot, so the number of allocated keys
1047 * doesn't go down, but the number placeholders goes up */
1048 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1050 *oentry = HeNEXT(entry);
1052 xhv->xhv_fill--; /* HvFILL(hv)-- */
1054 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
1057 hv_free_ent(hv, entry);
1058 xhv->xhv_keys--; /* HvKEYS(hv)-- */
1059 if (xhv->xhv_keys == 0)
1060 HvHASKFLAGS_off(hv);
1064 if (SvREADONLY(hv)) {
1065 S_hv_notallowed(aTHX_ k_flags, key, klen,
1066 "Attempt to delete disallowed key '%"SVf"' from"
1067 " a restricted hash");
1070 if (k_flags & HVhek_FREEKEY)
1076 S_hsplit(pTHX_ HV *hv)
1078 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1079 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1080 register I32 newsize = oldsize * 2;
1082 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
1084 register HE **oentry;
1085 int longest_chain = 0;
1088 /*PerlIO_printf(PerlIO_stderr(), "hsplit called for %p which had %d\n",
1089 hv, (int) oldsize);*/
1091 if (HvPLACEHOLDERS_get(hv) && !SvREADONLY(hv)) {
1092 /* Can make this clear any placeholders first for non-restricted hashes,
1093 even though Storable rebuilds restricted hashes by putting in all the
1094 placeholders (first) before turning on the readonly flag, because
1095 Storable always pre-splits the hash. */
1096 hv_clear_placeholders(hv);
1100 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1101 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1107 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1112 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1113 if (oldsize >= 64) {
1114 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1115 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1118 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1122 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1123 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1124 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1127 for (i=0; i<oldsize; i++,aep++) {
1128 int left_length = 0;
1129 int right_length = 0;
1133 if (!*aep) /* non-existent */
1136 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1137 if ((HeHASH(entry) & newsize) != (U32)i) {
1138 *oentry = HeNEXT(entry);
1139 HeNEXT(entry) = *bep;
1141 xhv->xhv_fill++; /* HvFILL(hv)++ */
1147 oentry = &HeNEXT(entry);
1151 if (!*aep) /* everything moved */
1152 xhv->xhv_fill--; /* HvFILL(hv)-- */
1153 /* I think we don't actually need to keep track of the longest length,
1154 merely flag if anything is too long. But for the moment while
1155 developing this code I'll track it. */
1156 if (left_length > longest_chain)
1157 longest_chain = left_length;
1158 if (right_length > longest_chain)
1159 longest_chain = right_length;
1163 /* Pick your policy for "hashing isn't working" here: */
1164 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
1169 if (hv == PL_strtab) {
1170 /* Urg. Someone is doing something nasty to the string table.
1175 /* Awooga. Awooga. Pathological data. */
1176 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
1177 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1180 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1181 was_shared = HvSHAREKEYS(hv);
1184 HvSHAREKEYS_off(hv);
1187 aep = (HE **) xhv->xhv_array;
1189 for (i=0; i<newsize; i++,aep++) {
1190 register HE *entry = *aep;
1192 /* We're going to trash this HE's next pointer when we chain it
1193 into the new hash below, so store where we go next. */
1194 HE * const next = HeNEXT(entry);
1199 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1204 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1205 hash, HeKFLAGS(entry));
1206 unshare_hek (HeKEY_hek(entry));
1207 HeKEY_hek(entry) = new_hek;
1209 /* Not shared, so simply write the new hash in. */
1210 HeHASH(entry) = hash;
1212 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1213 HEK_REHASH_on(HeKEY_hek(entry));
1214 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1216 /* Copy oentry to the correct new chain. */
1217 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1219 xhv->xhv_fill++; /* HvFILL(hv)++ */
1220 HeNEXT(entry) = *bep;
1226 Safefree (xhv->xhv_array);
1227 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1231 Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
1233 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1234 const I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
1235 register I32 newsize;
1240 register HE **oentry;
1242 newsize = (I32) newmax; /* possible truncation here */
1243 if (newsize != newmax || newmax <= oldsize)
1245 while ((newsize & (1 + ~newsize)) != newsize) {
1246 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1248 if (newsize < newmax)
1250 if (newsize < newmax)
1251 return; /* overflow detection */
1253 a = xhv->xhv_array; /* HvARRAY(hv) */
1256 #if defined(STRANGE_MALLOC) || defined(MYMALLOC)
1257 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1263 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1268 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
1269 if (oldsize >= 64) {
1270 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1271 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
1274 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1277 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
1280 Newxz(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1282 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1283 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1284 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
1288 for (i=0; i<oldsize; i++,aep++) {
1289 if (!*aep) /* non-existent */
1291 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
1293 if ((j = (HeHASH(entry) & newsize)) != i) {
1295 *oentry = HeNEXT(entry);
1296 if (!(HeNEXT(entry) = aep[j]))
1297 xhv->xhv_fill++; /* HvFILL(hv)++ */
1302 oentry = &HeNEXT(entry);
1304 if (!*aep) /* everything moved */
1305 xhv->xhv_fill--; /* HvFILL(hv)-- */
1312 Creates a new HV. The reference count is set to 1.
1320 register XPVHV* xhv;
1321 HV * const hv = (HV*)NEWSV(502,0);
1323 sv_upgrade((SV *)hv, SVt_PVHV);
1324 xhv = (XPVHV*)SvANY(hv);
1327 #ifndef NODEFAULT_SHAREKEYS
1328 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1331 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1332 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1333 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
1334 (void)hv_iterinit(hv); /* so each() will start off right */
1339 Perl_newHVhv(pTHX_ HV *ohv)
1341 HV * const hv = newHV();
1342 STRLEN hv_max, hv_fill;
1344 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1346 hv_max = HvMAX(ohv);
1348 if (!SvMAGICAL((SV *)ohv)) {
1349 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
1351 const bool shared = !!HvSHAREKEYS(ohv);
1352 HE **ents, ** const oents = (HE **)HvARRAY(ohv);
1354 Newx(a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1357 /* In each bucket... */
1358 for (i = 0; i <= hv_max; i++) {
1359 HE *prev = NULL, *ent = NULL;
1360 HE *oent = oents[i];
1367 /* Copy the linked list of entries. */
1368 for (; oent; oent = HeNEXT(oent)) {
1369 const U32 hash = HeHASH(oent);
1370 const char * const key = HeKEY(oent);
1371 const STRLEN len = HeKLEN(oent);
1372 const int flags = HeKFLAGS(oent);
1375 HeVAL(ent) = newSVsv(HeVAL(oent));
1377 = shared ? share_hek_flags(key, len, hash, flags)
1378 : save_hek_flags(key, len, hash, flags);
1389 HvFILL(hv) = hv_fill;
1390 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
1394 /* Iterate over ohv, copying keys and values one at a time. */
1396 const I32 riter = HvRITER_get(ohv);
1397 HE * const eiter = HvEITER_get(ohv);
1399 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1400 while (hv_max && hv_max + 1 >= hv_fill * 2)
1401 hv_max = hv_max / 2;
1405 while ((entry = hv_iternext_flags(ohv, 0))) {
1406 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1407 newSVsv(HeVAL(entry)), HeHASH(entry),
1410 HvRITER_set(ohv, riter);
1411 HvEITER_set(ohv, eiter);
1418 Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
1425 if (val && isGV(val) && GvCVu(val) && HvNAME_get(hv))
1426 PL_sub_generation++; /* may be deletion of method from stash */
1428 if (HeKLEN(entry) == HEf_SVKEY) {
1429 SvREFCNT_dec(HeKEY_sv(entry));
1430 Safefree(HeKEY_hek(entry));
1432 else if (HvSHAREKEYS(hv))
1433 unshare_hek(HeKEY_hek(entry));
1435 Safefree(HeKEY_hek(entry));
1440 Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
1444 /* SvREFCNT_inc to counter the SvREFCNT_dec in hv_free_ent */
1445 sv_2mortal(SvREFCNT_inc(HeVAL(entry))); /* free between statements */
1446 if (HeKLEN(entry) == HEf_SVKEY) {
1447 sv_2mortal(SvREFCNT_inc(HeKEY_sv(entry)));
1449 hv_free_ent(hv, entry);
1453 =for apidoc hv_clear
1455 Clears a hash, making it empty.
1461 Perl_hv_clear(pTHX_ HV *hv)
1463 register XPVHV* xhv;
1467 xhv = (XPVHV*)SvANY(hv);
1469 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
1470 /* restricted hash: convert all keys to placeholders */
1472 for (i = 0; i <= xhv->xhv_max; i++) {
1473 HE *entry = ((HE**)xhv->xhv_array)[i];
1474 for (; entry; entry = HeNEXT(entry)) {
1475 /* not already placeholder */
1476 if (HeVAL(entry) != &PL_sv_placeholder) {
1477 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1478 SV* keysv = hv_iterkeysv(entry);
1480 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1483 SvREFCNT_dec(HeVAL(entry));
1484 HeVAL(entry) = &PL_sv_placeholder;
1485 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1493 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1494 if (xhv->xhv_array /* HvARRAY(hv) */)
1495 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1496 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
1501 HvHASKFLAGS_off(hv);
1504 HvEITER_set(hv, NULL);
1508 =for apidoc hv_clear_placeholders
1510 Clears any placeholders from a hash. If a restricted hash has any of its keys
1511 marked as readonly and the key is subsequently deleted, the key is not actually
1512 deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1513 it so it will be ignored by future operations such as iterating over the hash,
1514 but will still allow the hash to have a value reassigned to the key at some
1515 future point. This function clears any such placeholder keys from the hash.
1516 See Hash::Util::lock_keys() for an example of its use.
1522 Perl_hv_clear_placeholders(pTHX_ HV *hv)
1524 I32 items = (I32)HvPLACEHOLDERS_get(hv);
1532 /* Loop down the linked list heads */
1534 HE **oentry = &(HvARRAY(hv))[i];
1535 HE *entry = *oentry;
1540 for (; entry; entry = *oentry) {
1541 if (HeVAL(entry) == &PL_sv_placeholder) {
1542 *oentry = HeNEXT(entry);
1543 if (first && !*oentry)
1544 HvFILL(hv)--; /* This linked list is now empty. */
1545 if (HvEITER_get(hv))
1548 hv_free_ent(hv, entry);
1552 HvTOTALKEYS(hv) -= (IV)HvPLACEHOLDERS_get(hv);
1553 if (HvKEYS(hv) == 0)
1554 HvHASKFLAGS_off(hv);
1555 HvPLACEHOLDERS_set(hv, 0);
1559 oentry = &HeNEXT(entry);
1564 /* You can't get here, hence assertion should always fail. */
1565 assert (items == 0);
1570 S_hfreeentries(pTHX_ HV *hv)
1572 register HE **array;
1583 array = HvARRAY(hv);
1584 /* make everyone else think the array is empty, so that the destructors
1585 * called for freed entries can't recusively mess with us */
1586 HvARRAY(hv) = Null(HE**);
1588 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1593 register HE * const oentry = entry;
1594 entry = HeNEXT(entry);
1595 hv_free_ent(hv, oentry);
1600 entry = array[riter];
1603 HvARRAY(hv) = array;
1604 (void)hv_iterinit(hv);
1608 =for apidoc hv_undef
1616 Perl_hv_undef(pTHX_ HV *hv)
1618 register XPVHV* xhv;
1622 xhv = (XPVHV*)SvANY(hv);
1624 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
1625 if ((name = HvNAME_get(hv))) {
1626 /* FIXME - strlen HvNAME */
1628 hv_delete(PL_stashcache, name, strlen(name), G_DISCARD);
1629 hv_name_set(hv, Nullch, 0, 0);
1631 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1632 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
1633 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
1640 =for apidoc hv_iterinit
1642 Prepares a starting point to traverse a hash table. Returns the number of
1643 keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1644 currently only meaningful for hashes without tie magic.
1646 NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1647 hash buckets that happen to be in use. If you still need that esoteric
1648 value, you can get it through the macro C<HvFILL(tb)>.
1655 Perl_hv_iterinit(pTHX_ HV *hv)
1657 register XPVHV* xhv;
1661 Perl_croak(aTHX_ "Bad hash");
1662 xhv = (XPVHV*)SvANY(hv);
1663 entry = xhv->xhv_eiter; /* HvEITER(hv) */
1664 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1666 hv_free_ent(hv, entry);
1668 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1669 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1670 /* used to be xhv->xhv_fill before 5.004_65 */
1671 return HvTOTALKEYS(hv);
1674 =for apidoc hv_iternext
1676 Returns entries from a hash iterator. See C<hv_iterinit>.
1678 You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1679 iterator currently points to, without losing your place or invalidating your
1680 iterator. Note that in this case the current entry is deleted from the hash
1681 with your iterator holding the last reference to it. Your iterator is flagged
1682 to free the entry on the next call to C<hv_iternext>, so you must not discard
1683 your iterator immediately else the entry will leak - call C<hv_iternext> to
1684 trigger the resource deallocation.
1690 Perl_hv_iternext(pTHX_ HV *hv)
1692 return hv_iternext_flags(hv, 0);
1696 =for apidoc hv_iternext_flags
1698 Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1699 The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1700 set the placeholders keys (for restricted hashes) will be returned in addition
1701 to normal keys. By default placeholders are automatically skipped over.
1702 Currently a placeholder is implemented with a value that is
1703 C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
1704 restricted hashes may change, and the implementation currently is
1705 insufficiently abstracted for any change to be tidy.
1711 Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1713 register XPVHV* xhv;
1719 Perl_croak(aTHX_ "Bad hash");
1720 xhv = (XPVHV*)SvANY(hv);
1721 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
1723 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
1724 SV *key = sv_newmortal();
1726 sv_setsv(key, HeSVKEY_force(entry));
1727 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1733 /* one HE per MAGICAL hash */
1734 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
1736 Newxz(k, HEK_BASESIZE + sizeof(SV*), char);
1738 HeKEY_hek(entry) = hek;
1739 HeKLEN(entry) = HEf_SVKEY;
1741 magic_nextpack((SV*) hv,mg,key);
1743 /* force key to stay around until next time */
1744 HeSVKEY_set(entry, SvREFCNT_inc(key));
1745 return entry; /* beware, hent_val is not set */
1748 SvREFCNT_dec(HeVAL(entry));
1749 Safefree(HeKEY_hek(entry));
1751 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1754 #ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1755 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
1758 /* The prime_env_iter() on VMS just loaded up new hash values
1759 * so the iteration count needs to be reset back to the beginning
1763 oldentry = entry = iter->xhv_eiter; /* HvEITER(hv) */
1768 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1769 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1770 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1772 /* At start of hash, entry is NULL. */
1775 entry = HeNEXT(entry);
1776 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1778 * Skip past any placeholders -- don't want to include them in
1781 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
1782 entry = HeNEXT(entry);
1787 /* OK. Come to the end of the current list. Grab the next one. */
1789 xhv->xhv_riter++; /* HvRITER(hv)++ */
1790 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
1791 /* There is no next one. End of the hash. */
1792 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1795 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1796 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1798 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1799 /* If we have an entry, but it's a placeholder, don't count it.
1801 while (entry && HeVAL(entry) == &PL_sv_placeholder)
1802 entry = HeNEXT(entry);
1804 /* Will loop again if this linked list starts NULL
1805 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1806 or if we run through it and find only placeholders. */
1809 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1811 hv_free_ent(hv, oldentry);
1814 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1815 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1817 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
1822 =for apidoc hv_iterkey
1824 Returns the key from the current position of the hash iterator. See
1831 Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
1833 if (HeKLEN(entry) == HEf_SVKEY) {
1835 char *p = SvPV(HeKEY_sv(entry), len);
1840 *retlen = HeKLEN(entry);
1841 return HeKEY(entry);
1845 /* unlike hv_iterval(), this always returns a mortal copy of the key */
1847 =for apidoc hv_iterkeysv
1849 Returns the key as an C<SV*> from the current position of the hash
1850 iterator. The return value will always be a mortal copy of the key. Also
1857 Perl_hv_iterkeysv(pTHX_ register HE *entry)
1859 return sv_2mortal(newSVhek(HeKEY_hek(entry)));
1863 =for apidoc hv_iterval
1865 Returns the value from the current position of the hash iterator. See
1872 Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
1874 if (SvRMAGICAL(hv)) {
1875 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
1876 SV* sv = sv_newmortal();
1877 if (HeKLEN(entry) == HEf_SVKEY)
1878 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1880 mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
1884 return HeVAL(entry);
1888 =for apidoc hv_iternextsv
1890 Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1897 Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
1900 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
1902 *key = hv_iterkey(he, retlen);
1903 return hv_iterval(hv, he);
1907 =for apidoc hv_magic
1909 Adds magic to a hash. See C<sv_magic>.
1915 Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
1917 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
1920 #if 0 /* use the macro from hv.h instead */
1923 Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
1925 return HEK_KEY(share_hek(sv, len, hash));
1930 /* possibly free a shared string if no one has access to it
1931 * len and hash must both be valid for str.
1934 Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
1936 unshare_hek_or_pvn (NULL, str, len, hash);
1941 Perl_unshare_hek(pTHX_ HEK *hek)
1943 unshare_hek_or_pvn(hek, NULL, 0, 0);
1946 /* possibly free a shared string if no one has access to it
1947 hek if non-NULL takes priority over the other 3, else str, len and hash
1948 are used. If so, len and hash must both be valid for str.
1951 S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1953 register XPVHV* xhv;
1955 register HE **oentry;
1958 bool is_utf8 = FALSE;
1960 const char * const save = str;
1963 hash = HEK_HASH(hek);
1964 } else if (len < 0) {
1965 STRLEN tmplen = -len;
1967 /* See the note in hv_fetch(). --jhi */
1968 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1971 k_flags = HVhek_UTF8;
1973 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
1976 /* what follows is the moral equivalent of:
1977 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
1978 if (--*Svp == Nullsv)
1979 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
1981 xhv = (XPVHV*)SvANY(PL_strtab);
1982 /* assert(xhv_array != 0) */
1984 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1985 first = oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
1987 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
1988 if (HeKEY_hek(entry) != hek)
1994 const int flags_masked = k_flags & HVhek_MASK;
1995 for (entry = *oentry; entry; oentry = &HeNEXT(entry), entry = *oentry) {
1996 if (HeHASH(entry) != hash) /* strings can't be equal */
1998 if (HeKLEN(entry) != len)
2000 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2002 if (HeKFLAGS(entry) != flags_masked)
2010 if (--HeVAL(entry) == Nullsv) {
2011 *oentry = HeNEXT(entry);
2013 /* There are now no entries in our slot. */
2014 xhv->xhv_fill--; /* HvFILL(hv)-- */
2016 Safefree(HeKEY_hek(entry));
2018 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2022 UNLOCK_STRTAB_MUTEX;
2023 if (!found && ckWARN_d(WARN_INTERNAL))
2024 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2025 "Attempt to free non-existent shared string '%s'%s"
2027 hek ? HEK_KEY(hek) : str,
2028 ((k_flags & HVhek_UTF8) ? " (utf8)" : "") pTHX__VALUE);
2029 if (k_flags & HVhek_FREEKEY)
2033 /* get a (constant) string ptr from the global string table
2034 * string will get added if it is not already there.
2035 * len and hash must both be valid for str.
2038 Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
2040 bool is_utf8 = FALSE;
2042 const char * const save = str;
2045 STRLEN tmplen = -len;
2047 /* See the note in hv_fetch(). --jhi */
2048 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2050 /* If we were able to downgrade here, then than means that we were passed
2051 in a key which only had chars 0-255, but was utf8 encoded. */
2054 /* If we found we were able to downgrade the string to bytes, then
2055 we should flag that it needs upgrading on keys or each. Also flag
2056 that we need share_hek_flags to free the string. */
2058 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2061 return share_hek_flags (str, len, hash, flags);
2065 S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2067 register XPVHV* xhv;
2069 register HE **oentry;
2071 const int flags_masked = flags & HVhek_MASK;
2073 /* what follows is the moral equivalent of:
2075 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
2076 hv_store(PL_strtab, str, len, Nullsv, hash);
2078 Can't rehash the shared string table, so not sure if it's worth
2079 counting the number of entries in the linked list
2081 xhv = (XPVHV*)SvANY(PL_strtab);
2082 /* assert(xhv_array != 0) */
2084 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2085 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
2086 for (entry = *oentry; entry; entry = HeNEXT(entry)) {
2087 if (HeHASH(entry) != hash) /* strings can't be equal */
2089 if (HeKLEN(entry) != len)
2091 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
2093 if (HeKFLAGS(entry) != flags_masked)
2099 /* What used to be head of the list.
2100 If this is NULL, then we're the first entry for this slot, which
2101 means we need to increate fill. */
2102 const HE *old_first = *oentry;
2104 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags_masked);
2105 HeVAL(entry) = Nullsv;
2106 HeNEXT(entry) = *oentry;
2108 xhv->xhv_keys++; /* HvKEYS(hv)++ */
2109 if (!old_first) { /* initial entry? */
2110 xhv->xhv_fill++; /* HvFILL(hv)++ */
2111 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
2116 ++HeVAL(entry); /* use value slot as REFCNT */
2117 UNLOCK_STRTAB_MUTEX;
2119 if (flags & HVhek_FREEKEY)
2122 return HeKEY_hek(entry);
2127 * c-indentation-style: bsd
2129 * indent-tabs-mode: t
2132 * ex: set ts=8 sts=4 sw=4 noet: