This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Farewell hv_exists_common - exists is now a call to fetch
[perl5.git] / hv.c
CommitLineData
a0d0e21e 1/* hv.c
79072805 2 *
4bb101f2
JH
3 * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
4 * 2000, 2001, 2002, 2003, by Larry Wall and others
79072805
LW
5 *
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.
8 *
a0d0e21e
LW
9 */
10
11/*
12 * "I sit beside the fire and think of all that I have seen." --Bilbo
79072805
LW
13 */
14
d5afce77
RB
15/*
16=head1 Hash Manipulation Functions
17*/
18
79072805 19#include "EXTERN.h"
864dbfa3 20#define PERL_IN_HV_C
3d78eb94 21#define PERL_HASH_INTERNAL_ACCESS
79072805
LW
22#include "perl.h"
23
d8012aaf 24#define HV_MAX_LENGTH_BEFORE_SPLIT 14
fdcd69b6 25
76e3520e 26STATIC HE*
cea2e8a9 27S_new_he(pTHX)
4633a7c4
LW
28{
29 HE* he;
333f433b
DG
30 LOCK_SV_MUTEX;
31 if (!PL_he_root)
8aacddc1 32 more_he();
333f433b
DG
33 he = PL_he_root;
34 PL_he_root = HeNEXT(he);
35 UNLOCK_SV_MUTEX;
36 return he;
4633a7c4
LW
37}
38
76e3520e 39STATIC void
cea2e8a9 40S_del_he(pTHX_ HE *p)
4633a7c4 41{
333f433b 42 LOCK_SV_MUTEX;
3280af22
NIS
43 HeNEXT(p) = (HE*)PL_he_root;
44 PL_he_root = p;
333f433b 45 UNLOCK_SV_MUTEX;
4633a7c4
LW
46}
47
333f433b 48STATIC void
cea2e8a9 49S_more_he(pTHX)
4633a7c4
LW
50{
51 register HE* he;
52 register HE* heend;
612f20c3
GS
53 XPV *ptr;
54 New(54, ptr, 1008/sizeof(XPV), XPV);
55 ptr->xpv_pv = (char*)PL_he_arenaroot;
56 PL_he_arenaroot = ptr;
57
58 he = (HE*)ptr;
4633a7c4 59 heend = &he[1008 / sizeof(HE) - 1];
612f20c3 60 PL_he_root = ++he;
4633a7c4 61 while (he < heend) {
8aacddc1
NIS
62 HeNEXT(he) = (HE*)(he + 1);
63 he++;
4633a7c4 64 }
fde52b5c 65 HeNEXT(he) = 0;
4633a7c4
LW
66}
67
d33b2eba
GS
68#ifdef PURIFY
69
70#define new_HE() (HE*)safemalloc(sizeof(HE))
71#define del_HE(p) safefree((char*)p)
72
73#else
74
75#define new_HE() new_he()
76#define del_HE(p) del_he(p)
77
78#endif
79
76e3520e 80STATIC HEK *
19692e8d 81S_save_hek_flags(pTHX_ const char *str, I32 len, U32 hash, int flags)
bbce6d69 82{
83 char *k;
84 register HEK *hek;
1c846c1f 85
e05949c7 86 New(54, k, HEK_BASESIZE + len + 2, char);
bbce6d69 87 hek = (HEK*)k;
ff68c719 88 Copy(str, HEK_KEY(hek), len, char);
e05949c7 89 HEK_KEY(hek)[len] = 0;
ff68c719 90 HEK_LEN(hek) = len;
91 HEK_HASH(hek) = hash;
19692e8d 92 HEK_FLAGS(hek) = (unsigned char)flags;
bbce6d69 93 return hek;
94}
95
dd28f7bb
DM
96/* free the pool of temporary HE/HEK pairs retunrned by hv_fetch_ent
97 * for tied hashes */
98
99void
100Perl_free_tied_hv_pool(pTHX)
101{
102 HE *ohe;
103 HE *he = PL_hv_fetch_ent_mh;
104 while (he) {
105 Safefree(HeKEY_hek(he));
106 ohe = he;
107 he = HeNEXT(he);
108 del_HE(ohe);
109 }
bf9cdc68 110 PL_hv_fetch_ent_mh = Nullhe;
dd28f7bb
DM
111}
112
d18c6117
GS
113#if defined(USE_ITHREADS)
114HE *
a8fc9800 115Perl_he_dup(pTHX_ HE *e, bool shared, CLONE_PARAMS* param)
d18c6117
GS
116{
117 HE *ret;
118
119 if (!e)
120 return Nullhe;
7766f137
GS
121 /* look for it in the table first */
122 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
123 if (ret)
124 return ret;
125
126 /* create anew and remember what it is */
d33b2eba 127 ret = new_HE();
7766f137
GS
128 ptr_table_store(PL_ptr_table, e, ret);
129
d2d73c3e 130 HeNEXT(ret) = he_dup(HeNEXT(e),shared, param);
dd28f7bb
DM
131 if (HeKLEN(e) == HEf_SVKEY) {
132 char *k;
133 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
134 HeKEY_hek(ret) = (HEK*)k;
d2d73c3e 135 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e), param));
dd28f7bb 136 }
d18c6117 137 else if (shared)
19692e8d
NC
138 HeKEY_hek(ret) = share_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
139 HeKFLAGS(e));
d18c6117 140 else
19692e8d
NC
141 HeKEY_hek(ret) = save_hek_flags(HeKEY(e), HeKLEN(e), HeHASH(e),
142 HeKFLAGS(e));
d2d73c3e 143 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e), param));
d18c6117
GS
144 return ret;
145}
146#endif /* USE_ITHREADS */
147
1b1f1335 148static void
2393f1b9
JH
149S_hv_notallowed(pTHX_ int flags, const char *key, I32 klen,
150 const char *msg)
1b1f1335 151{
2393f1b9 152 SV *sv = sv_newmortal(), *esv = sv_newmortal();
19692e8d 153 if (!(flags & HVhek_FREEKEY)) {
1b1f1335
NIS
154 sv_setpvn(sv, key, klen);
155 }
156 else {
157 /* Need to free saved eventually assign to mortal SV */
34c3c4e3 158 /* XXX is this line an error ???: SV *sv = sv_newmortal(); */
1b1f1335
NIS
159 sv_usepvn(sv, (char *) key, klen);
160 }
19692e8d 161 if (flags & HVhek_UTF8) {
1b1f1335
NIS
162 SvUTF8_on(sv);
163 }
2393f1b9
JH
164 Perl_sv_setpvf(aTHX_ esv, "Attempt to %s a restricted hash", msg);
165 Perl_croak(aTHX_ SvPVX(esv), sv);
1b1f1335
NIS
166}
167
fde52b5c 168/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
169 * contains an SV* */
170
954c1994
GS
171/*
172=for apidoc hv_fetch
173
174Returns the SV which corresponds to the specified key in the hash. The
175C<klen> is the length of the key. If C<lval> is set then the fetch will be
176part of a store. Check that the return value is non-null before
d1be9408 177dereferencing it to an C<SV*>.
954c1994 178
96f1132b 179See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
180information on how to use this function on tied hashes.
181
182=cut
183*/
184
7f66fda2
NC
185#define HV_FETCH_ISSTORE 0x01
186#define HV_FETCH_ISEXISTS 0x02
187#define HV_FETCH_LVALUE 0x04
188#define HV_FETCH_JUST_SV 0x08
19692e8d 189
79072805 190SV**
c1fe5510 191Perl_hv_fetch(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 lval)
79072805 192{
c1fe5510
NC
193 HE *hek;
194 STRLEN klen;
195 int flags;
196
197 if (klen_i32 < 0) {
198 klen = -klen_i32;
199 flags = HVhek_UTF8;
200 } else {
201 klen = klen_i32;
202 flags = 0;
203 }
204 hek = hv_fetch_common (hv, NULL, key, klen, flags,
205 HV_FETCH_JUST_SV | (lval ? HV_FETCH_LVALUE : 0), 0);
113738bb 206 return hek ? &HeVAL(hek) : NULL;
79072805
LW
207}
208
d1be9408 209/* returns an HE * structure with the all fields set */
fde52b5c 210/* note that hent_val will be a mortal sv for MAGICAL hashes */
954c1994
GS
211/*
212=for apidoc hv_fetch_ent
213
214Returns the hash entry which corresponds to the specified key in the hash.
215C<hash> must be a valid precomputed hash number for the given C<key>, or 0
216if you want the function to compute it. IF C<lval> is set then the fetch
217will be part of a store. Make sure the return value is non-null before
218accessing it. The return value when C<tb> is a tied hash is a pointer to a
219static location, so be sure to make a copy of the structure if you need to
1c846c1f 220store it somewhere.
954c1994 221
96f1132b 222See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
223information on how to use this function on tied hashes.
224
225=cut
226*/
227
fde52b5c 228HE *
864dbfa3 229Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
fde52b5c 230{
7f66fda2
NC
231 return hv_fetch_common(hv, keysv, NULL, 0, 0,
232 (lval ? HV_FETCH_LVALUE : 0), hash);
113738bb
NC
233}
234
235HE *
c1fe5510 236S_hv_fetch_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
113738bb
NC
237 int flags, int action, register U32 hash)
238{
cbec9347 239 register XPVHV* xhv;
fde52b5c 240 register HE *entry;
241 SV *sv;
da58a35d 242 bool is_utf8;
113738bb 243 int masked_flags;
fde52b5c 244
245 if (!hv)
246 return 0;
247
113738bb
NC
248 if (keysv) {
249 key = SvPV(keysv, klen);
c1fe5510 250 flags = 0;
113738bb
NC
251 is_utf8 = (SvUTF8(keysv) != 0);
252 } else {
c1fe5510 253 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
113738bb 254 }
113738bb 255
7f66fda2
NC
256 if (SvMAGICAL(hv)) {
257 if (SvRMAGICAL(hv) && !(action & (HV_FETCH_ISSTORE|HV_FETCH_ISEXISTS)))
258 {
259 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
260 sv = sv_newmortal();
113738bb 261
7f66fda2
NC
262 /* XXX should be able to skimp on the HE/HEK here when
263 HV_FETCH_JUST_SV is true. */
113738bb 264
7f66fda2
NC
265 if (!keysv) {
266 keysv = newSVpvn(key, klen);
267 if (is_utf8) {
268 SvUTF8_on(keysv);
269 }
270 } else {
271 keysv = newSVsv(keysv);
113738bb 272 }
7f66fda2
NC
273 mg_copy((SV*)hv, sv, (char *)keysv, HEf_SVKEY);
274
275 /* grab a fake HE/HEK pair from the pool or make a new one */
276 entry = PL_hv_fetch_ent_mh;
277 if (entry)
278 PL_hv_fetch_ent_mh = HeNEXT(entry);
279 else {
280 char *k;
281 entry = new_HE();
282 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
283 HeKEY_hek(entry) = (HEK*)k;
284 }
285 HeNEXT(entry) = Nullhe;
286 HeSVKEY_set(entry, keysv);
287 HeVAL(entry) = sv;
288 sv_upgrade(sv, SVt_PVLV);
289 LvTYPE(sv) = 'T';
290 /* so we can free entry when freeing sv */
291 LvTARG(sv) = (SV*)entry;
292
293 /* XXX remove at some point? */
294 if (flags & HVhek_FREEKEY)
295 Safefree(key);
296
297 return entry;
113738bb 298 }
7f66fda2
NC
299#ifdef ENV_IS_CASELESS
300 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
301 U32 i;
302 for (i = 0; i < klen; ++i)
303 if (isLOWER(key[i])) {
304 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
305 (void)strupr(SvPVX(nkeysv));
306 entry = hv_fetch_common(hv, nkeysv, NULL, 0, 0, 0);
307 if (!entry && (action & HV_FETCH_LVALUE))
308 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
309
310 /* XXX remove at some point? */
311 if (flags & HVhek_FREEKEY)
312 Safefree(key);
313
314 return entry;
315 }
902173a3 316 }
7f66fda2
NC
317#endif
318 } /* ISFETCH */
319 else if (SvRMAGICAL(hv) && (action & HV_FETCH_ISEXISTS)) {
320 if (mg_find((SV*)hv, PERL_MAGIC_tied) || SvGMAGICAL((SV*)hv)) {
321 SV* svret;
322
323 if (keysv || is_utf8) {
324 if (!keysv) {
325 keysv = newSVpvn(key, klen);
326 SvUTF8_on(keysv);
327 } else {
328 keysv = newSVsv(keysv);
329 }
330 key = (char *)sv_2mortal(keysv);
331 klen = HEf_SVKEY;
332 }
113738bb 333
7f66fda2
NC
334 /* I don't understand why hv_exists_ent has svret and sv,
335 whereas hv_exists only had one. */
336 svret = sv_newmortal();
337 sv = sv_newmortal();
338 mg_copy((SV*)hv, sv, key, klen);
339 magic_existspack(svret, mg_find(sv, PERL_MAGIC_tiedelem));
340 /* This cast somewhat evil, but I'm merely using NULL/
341 not NULL to return the boolean exists.
342 And I know hv is not NULL. */
343 return SvTRUE(svret) ? (HE *)hv : NULL;
e7152ba2 344 }
7f66fda2
NC
345#ifdef ENV_IS_CASELESS
346 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
347 /* XXX This code isn't UTF8 clean. */
348 keysv = sv_2mortal(newSVpvn(key,klen));
349 key = strupr(SvPVX(keysv));
350 is_utf8 = 0;
351 hash = 0;
352 }
902173a3 353#endif
7f66fda2
NC
354 } /* ISEXISTS */
355 } /* SvMAGICAL */
fde52b5c 356
cbec9347
JH
357 xhv = (XPVHV*)SvANY(hv);
358 if (!xhv->xhv_array /* !HvARRAY(hv) */) {
113738bb 359 if ((action & HV_FETCH_LVALUE)
fde52b5c 360#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
8aacddc1 361 || (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
fde52b5c 362#endif
8aacddc1 363 )
cbec9347
JH
364 Newz(503, xhv->xhv_array /* HvARRAY(hv) */,
365 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
366 char);
7f66fda2
NC
367#ifdef DYNAMIC_ENV_FETCH
368 else if (action & HV_FETCH_ISEXISTS) {
369 /* for an %ENV exists, if we do an insert it's by a recursive
370 store call, so avoid creating HvARRAY(hv) right now. */
371 }
372#endif
113738bb
NC
373 else {
374 /* XXX remove at some point? */
375 if (flags & HVhek_FREEKEY)
376 Safefree(key);
377
fde52b5c 378 return 0;
113738bb 379 }
fde52b5c 380 }
381
19692e8d 382 if (is_utf8) {
7f66fda2 383 const char *keysave = key;
f9a63242 384 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
19692e8d 385 if (is_utf8)
c1fe5510
NC
386 flags |= HVhek_UTF8;
387 else
388 flags &= ~HVhek_UTF8;
7f66fda2
NC
389 if (key != keysave) {
390 if (flags & HVhek_FREEKEY)
391 Safefree(keysave);
19692e8d 392 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
7f66fda2 393 }
19692e8d 394 }
f9a63242 395
4b5190b5
NC
396 if (HvREHASH(hv)) {
397 PERL_HASH_INTERNAL(hash, key, klen);
fdcd69b6
NC
398 /* Yes, you do need this even though you are not "storing" because
399 you can flip the flags below if doing an lval lookup. (And that
400 was put in to give the semantics Andreas was expecting.) */
401 flags |= HVhek_REHASH;
4b5190b5 402 } else if (!hash) {
113738bb 403 if (keysv && (SvIsCOW_shared_hash(keysv))) {
46187eeb
NC
404 hash = SvUVX(keysv);
405 } else {
406 PERL_HASH(hash, key, klen);
407 }
408 }
effa1e2d 409
113738bb
NC
410 masked_flags = (flags & HVhek_MASK);
411
7f66fda2
NC
412#ifdef DYNAMIC_ENV_FETCH
413 if (!xhv->xhv_array /* !HvARRAY(hv) */) entry = Null(HE*);
414 else
415#endif
cbec9347
JH
416 /* entry = (HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
417 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 418 for (; entry; entry = HeNEXT(entry)) {
419 if (HeHASH(entry) != hash) /* strings can't be equal */
420 continue;
eb160463 421 if (HeKLEN(entry) != (I32)klen)
fde52b5c 422 continue;
1c846c1f 423 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 424 continue;
113738bb 425 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
c3654f1a 426 continue;
113738bb 427 if ((action & HV_FETCH_LVALUE) && HeKFLAGS(entry) != masked_flags) {
19692e8d
NC
428 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
429 But if entry was set previously with HVhek_WASUTF8 and key now
430 doesn't (or vice versa) then we should change the key's flag,
431 as this is assignment. */
432 if (HvSHAREKEYS(hv)) {
433 /* Need to swap the key we have for a key with the flags we
434 need. As keys are shared we can't just write to the flag,
435 so we share the new one, unshare the old one. */
113738bb 436 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
19692e8d
NC
437 unshare_hek (HeKEY_hek(entry));
438 HeKEY_hek(entry) = new_hek;
439 }
440 else
113738bb
NC
441 HeKFLAGS(entry) = masked_flags;
442 if (masked_flags & HVhek_ENABLEHVKFLAGS)
27a3632d 443 HvHASKFLAGS_on(hv);
19692e8d 444 }
8aacddc1 445 /* if we find a placeholder, we pretend we haven't found anything */
7996736c 446 if (HeVAL(entry) == &PL_sv_placeholder)
8aacddc1 447 break;
113738bb
NC
448 if (flags & HVhek_FREEKEY)
449 Safefree(key);
fde52b5c 450 return entry;
451 }
452#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
cbec9347 453 if (SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env)) {
a6c40364
GS
454 unsigned long len;
455 char *env = PerlEnv_ENVgetenv_len(key,&len);
456 if (env) {
113738bb
NC
457 /* XXX remove once common API complete */
458 if (!keysv) {
459 nkeysv = sv_2mortal(newSVpvn(key,klen));
460 }
461
a6c40364
GS
462 sv = newSVpvn(env,len);
463 SvTAINTED_on(sv);
113738bb
NC
464 if (flags & HVhek_FREEKEY)
465 Safefree(key);
a6c40364
GS
466 return hv_store_ent(hv,keysv,sv,hash);
467 }
fde52b5c 468 }
469#endif
7f66fda2
NC
470
471 if (!entry && SvREADONLY(hv) && !(action & HV_FETCH_ISEXISTS)) {
2393f1b9
JH
472 S_hv_notallowed(aTHX_ flags, key, klen,
473 "access disallowed key '%"SVf"' in"
474 );
1b1f1335 475 }
113738bb
NC
476 if (action & HV_FETCH_LVALUE) {
477 /* XXX remove once common API complete */
478 if (!keysv) {
479 keysv = sv_2mortal(newSVpvn(key,klen));
480 }
481 }
482
19692e8d 483 if (flags & HVhek_FREEKEY)
f9a63242 484 Safefree(key);
113738bb
NC
485 if (action & HV_FETCH_LVALUE) {
486 /* gonna assign to this, so it better be there */
fde52b5c 487 sv = NEWSV(61,0);
e7152ba2 488 return hv_store_ent(hv,keysv,sv,hash);
fde52b5c 489 }
490 return 0;
491}
492
864dbfa3 493STATIC void
cea2e8a9 494S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
d0066dc7
OT
495{
496 MAGIC *mg = SvMAGIC(hv);
497 *needs_copy = FALSE;
498 *needs_store = TRUE;
499 while (mg) {
500 if (isUPPER(mg->mg_type)) {
501 *needs_copy = TRUE;
502 switch (mg->mg_type) {
14befaf4
DM
503 case PERL_MAGIC_tied:
504 case PERL_MAGIC_sig:
d0066dc7 505 *needs_store = FALSE;
d0066dc7
OT
506 }
507 }
508 mg = mg->mg_moremagic;
509 }
510}
511
954c1994
GS
512/*
513=for apidoc hv_store
514
515Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
516the length of the key. The C<hash> parameter is the precomputed hash
517value; if it is zero then Perl will compute it. The return value will be
518NULL if the operation failed or if the value did not need to be actually
519stored within the hash (as in the case of tied hashes). Otherwise it can
520be dereferenced to get the original C<SV*>. Note that the caller is
521responsible for suitably incrementing the reference count of C<val> before
4f646c4b
NC
522the call, and decrementing it if the function returned NULL. Effectively
523a successful hv_store takes ownership of one reference to C<val>. This is
524usually what you want; a newly created SV has a reference count of one, so
525if all your code does is create SVs then store them in a hash, hv_store
526will own the only reference to the new SV, and your code doesn't need to do
527anything further to tidy up. hv_store is not implemented as a call to
528hv_store_ent, and does not create a temporary SV for the key, so if your
529key data is not already in SV form then use hv_store in preference to
530hv_store_ent.
954c1994 531
96f1132b 532See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
533information on how to use this function on tied hashes.
534
535=cut
536*/
537
79072805 538SV**
a8adfdb3 539Perl_hv_store(pTHX_ HV *hv, const char *key, I32 klen_i32, SV *val, U32 hash)
19692e8d 540{
a8adfdb3
NC
541 HE *hek;
542 STRLEN klen;
543 int flags;
544
545 if (klen_i32 < 0) {
546 klen = -klen_i32;
547 flags = HVhek_UTF8;
548 } else {
549 klen = klen_i32;
550 flags = 0;
551 }
552 hek = hv_store_common (hv, NULL, key, klen, flags, val, 0);
570c4e91 553 return hek ? &HeVAL(hek) : NULL;
19692e8d
NC
554}
555
556SV**
e16e2ff8 557Perl_hv_store_flags(pTHX_ HV *hv, const char *key, I32 klen, SV *val,
19692e8d 558 register U32 hash, int flags)
79072805 559{
570c4e91
NC
560 HE *hek = hv_store_common (hv, NULL, key, klen, flags, val, hash);
561 return hek ? &HeVAL(hek) : NULL;
fde52b5c 562}
563
954c1994
GS
564/*
565=for apidoc hv_store_ent
566
567Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
568parameter is the precomputed hash value; if it is zero then Perl will
569compute it. The return value is the new hash entry so created. It will be
570NULL if the operation failed or if the value did not need to be actually
571stored within the hash (as in the case of tied hashes). Otherwise the
87324b0f 572contents of the return value can be accessed using the C<He?> macros
954c1994
GS
573described here. Note that the caller is responsible for suitably
574incrementing the reference count of C<val> before the call, and
4f646c4b
NC
575decrementing it if the function returned NULL. Effectively a successful
576hv_store_ent takes ownership of one reference to C<val>. This is
577usually what you want; a newly created SV has a reference count of one, so
578if all your code does is create SVs then store them in a hash, hv_store
579will own the only reference to the new SV, and your code doesn't need to do
580anything further to tidy up. Note that hv_store_ent only reads the C<key>;
581unlike C<val> it does not take ownership of it, so maintaining the correct
582reference count on C<key> is entirely the caller's responsibility. hv_store
583is not implemented as a call to hv_store_ent, and does not create a temporary
584SV for the key, so if your key data is not already in SV form then use
585hv_store in preference to hv_store_ent.
954c1994 586
96f1132b 587See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
588information on how to use this function on tied hashes.
589
590=cut
591*/
592
fde52b5c 593HE *
19692e8d 594Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, U32 hash)
fde52b5c 595{
570c4e91
NC
596 return hv_store_common(hv, keysv, NULL, 0, 0, val, hash);
597}
598
599HE *
a8adfdb3 600S_hv_store_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
570c4e91
NC
601 int flags, SV *val, U32 hash)
602{
19692e8d 603 XPVHV* xhv;
fdcd69b6 604 U32 n_links;
19692e8d
NC
605 HE *entry;
606 HE **oentry;
da58a35d 607 bool is_utf8;
a8adfdb3 608 int masked_flags;
fde52b5c 609
610 if (!hv)
611 return 0;
612
570c4e91
NC
613 if (keysv) {
614 key = SvPV(keysv, klen);
a8adfdb3 615 flags = 0;
570c4e91
NC
616 is_utf8 = (SvUTF8(keysv) != 0);
617 } else {
a8adfdb3 618 is_utf8 = ((flags & HVhek_UTF8) ? TRUE : FALSE);
570c4e91 619 }
570c4e91 620
cbec9347 621 xhv = (XPVHV*)SvANY(hv);
fde52b5c 622 if (SvMAGICAL(hv)) {
8aacddc1
NIS
623 bool needs_copy;
624 bool needs_store;
625 hv_magic_check (hv, &needs_copy, &needs_store);
626 if (needs_copy) {
570c4e91
NC
627 bool save_taint = PL_tainted;
628 if (keysv || is_utf8) {
629 if (!keysv) {
630 keysv = newSVpvn(key, klen);
631 SvUTF8_on(keysv);
632 }
633 if (PL_tainting)
634 PL_tainted = SvTAINTED(keysv);
635 keysv = sv_2mortal(newSVsv(keysv));
636 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
637 } else {
638 mg_copy((SV*)hv, val, key, klen);
639 }
640
8aacddc1 641 TAINT_IF(save_taint);
570c4e91
NC
642 if (!xhv->xhv_array /* !HvARRAY(hv) */ && !needs_store) {
643 if (flags & HVhek_FREEKEY)
644 Safefree(key);
8aacddc1 645 return Nullhe;
570c4e91 646 }
902173a3 647#ifdef ENV_IS_CASELESS
14befaf4 648 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
7f66fda2 649 const char *keysave = key;
570c4e91
NC
650 key = savepvn(key,klen);
651 key = (const char*)strupr((char*)key);
902173a3 652 hash = 0;
570c4e91 653
7f66fda2 654 if (flags & HVhek_FREEKEY) {
570c4e91 655 Safefree(keysave);
7f66fda2
NC
656 flags &= ~HVhek_FREEKEY;
657 }
902173a3
GS
658 }
659#endif
660 }
fde52b5c 661 }
662
570c4e91
NC
663
664 if (flags & HVhek_PLACEHOLD) {
665 /* We have been requested to insert a placeholder. Currently
666 only Storable is allowed to do this. */
667 val = &PL_sv_placeholder;
668 }
902173a3 669
574c8022 670 if (is_utf8) {
7f66fda2 671 const char *keysave = key;
f9a63242 672 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
570c4e91 673
19692e8d 674 if (is_utf8)
a8adfdb3
NC
675 flags |= HVhek_UTF8;
676 else
677 flags &= ~HVhek_UTF8;
7f66fda2
NC
678 if (key != keysave) {
679 if (flags & HVhek_FREEKEY) {
680 /* This shouldn't happen if our caller does what we expect,
681 but strictly the API allows it. */
682 Safefree(keysave);
683 }
19692e8d 684 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
7f66fda2 685 }
19692e8d 686 HvHASKFLAGS_on((SV*)hv);
574c8022 687 }
f9a63242 688
4b5190b5
NC
689 if (HvREHASH(hv)) {
690 /* We don't have a pointer to the hv, so we have to replicate the
691 flag into every HEK, so that hv_iterkeysv can see it. */
692 flags |= HVhek_REHASH;
693 PERL_HASH_INTERNAL(hash, key, klen);
694 } else if (!hash) {
570c4e91 695 if (keysv && SvIsCOW_shared_hash(keysv)) {
46187eeb
NC
696 hash = SvUVX(keysv);
697 } else {
698 PERL_HASH(hash, key, klen);
699 }
700 }
fde52b5c 701
a8adfdb3
NC
702 masked_flags = (flags & HVhek_MASK);
703
cbec9347
JH
704 if (!xhv->xhv_array /* !HvARRAY(hv) */)
705 Newz(505, xhv->xhv_array /* HvARRAY(hv) */,
706 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
707 char);
79072805 708
cbec9347
JH
709 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
710 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fdcd69b6 711 n_links = 0;
19692e8d 712 entry = *oentry;
fdcd69b6 713 for (; entry; ++n_links, entry = HeNEXT(entry)) {
fde52b5c 714 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 715 continue;
eb160463 716 if (HeKLEN(entry) != (I32)klen)
79072805 717 continue;
1c846c1f 718 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 719 continue;
a8adfdb3 720 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
c3654f1a 721 continue;
7996736c 722 if (HeVAL(entry) == &PL_sv_placeholder)
8aacddc1
NIS
723 xhv->xhv_placeholders--; /* yes, can store into placeholder slot */
724 else
725 SvREFCNT_dec(HeVAL(entry));
fde52b5c 726 HeVAL(entry) = val;
570c4e91
NC
727 if (val == &PL_sv_placeholder)
728 xhv->xhv_placeholders++;
729
a8adfdb3 730 if (HeKFLAGS(entry) != masked_flags) {
19692e8d
NC
731 /* We match if HVhek_UTF8 bit in our flags and hash key's match.
732 But if entry was set previously with HVhek_WASUTF8 and key now
733 doesn't (or vice versa) then we should change the key's flag,
734 as this is assignment. */
735 if (HvSHAREKEYS(hv)) {
736 /* Need to swap the key we have for a key with the flags we
737 need. As keys are shared we can't just write to the flag,
738 so we share the new one, unshare the old one. */
a8adfdb3 739 HEK *new_hek = share_hek_flags(key, klen, hash, masked_flags);
19692e8d
NC
740 unshare_hek (HeKEY_hek(entry));
741 HeKEY_hek(entry) = new_hek;
742 }
743 else
a8adfdb3 744 HeKFLAGS(entry) = masked_flags;
19692e8d
NC
745 }
746 if (flags & HVhek_FREEKEY)
f9a63242 747 Safefree(key);
fde52b5c 748 return entry;
79072805 749 }
79072805 750
1b1f1335 751 if (SvREADONLY(hv)) {
2393f1b9
JH
752 S_hv_notallowed(aTHX_ flags, key, klen,
753 "access disallowed key '%"SVf"' to"
754 );
1b1f1335
NIS
755 }
756
d33b2eba 757 entry = new_HE();
19692e8d
NC
758 /* share_hek_flags will do the free for us. This might be considered
759 bad API design. */
fde52b5c 760 if (HvSHAREKEYS(hv))
19692e8d 761 HeKEY_hek(entry) = share_hek_flags(key, klen, hash, flags);
fde52b5c 762 else /* gotta do the real thing */
19692e8d 763 HeKEY_hek(entry) = save_hek_flags(key, klen, hash, flags);
fde52b5c 764 HeVAL(entry) = val;
fde52b5c 765 HeNEXT(entry) = *oentry;
79072805
LW
766 *oentry = entry;
767
570c4e91
NC
768 if (val == &PL_sv_placeholder)
769 xhv->xhv_placeholders++;
770
cbec9347 771 xhv->xhv_keys++; /* HvKEYS(hv)++ */
fdcd69b6 772 if (!n_links) { /* initial entry? */
cbec9347 773 xhv->xhv_fill++; /* HvFILL(hv)++ */
fdcd69b6
NC
774 } else if ((xhv->xhv_keys > (IV)xhv->xhv_max)
775 || ((n_links > HV_MAX_LENGTH_BEFORE_SPLIT) && !HvREHASH(hv))) {
776 /* Use only the old HvKEYS(hv) > HvMAX(hv) condition to limit bucket
777 splits on a rehashed hash, as we're not going to split it again,
778 and if someone is lucky (evil) enough to get all the keys in one
779 list they could exhaust our memory as we repeatedly double the
780 number of buckets on every entry. Linear search feels a less worse
781 thing to do. */
782 hsplit(hv);
79072805 783 }
79072805 784
fde52b5c 785 return entry;
79072805
LW
786}
787
954c1994
GS
788/*
789=for apidoc hv_delete
790
791Deletes a key/value pair in the hash. The value SV is removed from the
1c846c1f 792hash and returned to the caller. The C<klen> is the length of the key.
954c1994
GS
793The C<flags> value will normally be zero; if set to G_DISCARD then NULL
794will be returned.
795
796=cut
797*/
798
79072805 799SV *
cd6d36ac 800Perl_hv_delete(pTHX_ HV *hv, const char *key, I32 klen_i32, I32 flags)
79072805 801{
cd6d36ac
NC
802 STRLEN klen;
803 int k_flags = 0;
804
805 if (klen_i32 < 0) {
806 klen = -klen_i32;
807 k_flags |= HVhek_UTF8;
808 } else {
809 klen = klen_i32;
810 }
811 return hv_delete_common(hv, NULL, key, klen, k_flags, flags, 0);
fde52b5c 812}
813
954c1994
GS
814/*
815=for apidoc hv_delete_ent
816
817Deletes a key/value pair in the hash. The value SV is removed from the
818hash and returned to the caller. The C<flags> value will normally be zero;
819if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
820precomputed hash value, or 0 to ask for it to be computed.
821
822=cut
823*/
824
fde52b5c 825SV *
864dbfa3 826Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
fde52b5c 827{
cd6d36ac 828 return hv_delete_common(hv, keysv, NULL, 0, 0, flags, hash);
f1317c8d
NC
829}
830
831SV *
cd6d36ac
NC
832S_hv_delete_common(pTHX_ HV *hv, SV *keysv, const char *key, STRLEN klen,
833 int k_flags, I32 d_flags, U32 hash)
f1317c8d 834{
cbec9347 835 register XPVHV* xhv;
fde52b5c 836 register I32 i;
fde52b5c 837 register HE *entry;
838 register HE **oentry;
839 SV *sv;
da58a35d 840 bool is_utf8;
7a9669ca 841 int masked_flags;
1c846c1f 842
fde52b5c 843 if (!hv)
844 return Nullsv;
f1317c8d
NC
845
846 if (keysv) {
847 key = SvPV(keysv, klen);
cd6d36ac 848 k_flags = 0;
f1317c8d
NC
849 is_utf8 = (SvUTF8(keysv) != 0);
850 } else {
cd6d36ac 851 is_utf8 = ((k_flags & HVhek_UTF8) ? TRUE : FALSE);
f1317c8d 852 }
f1317c8d 853
fde52b5c 854 if (SvRMAGICAL(hv)) {
0a0bb7c7
OT
855 bool needs_copy;
856 bool needs_store;
857 hv_magic_check (hv, &needs_copy, &needs_store);
858
f1317c8d 859 if (needs_copy) {
7a9669ca
NC
860 entry = hv_fetch_common(hv, keysv, key, klen,
861 k_flags & ~HVhek_FREEKEY, HV_FETCH_LVALUE,
862 hash);
863 sv = entry ? HeVAL(entry) : NULL;
f1317c8d
NC
864 if (sv) {
865 if (SvMAGICAL(sv)) {
866 mg_clear(sv);
867 }
868 if (!needs_store) {
869 if (mg_find(sv, PERL_MAGIC_tiedelem)) {
870 /* No longer an element */
871 sv_unmagic(sv, PERL_MAGIC_tiedelem);
872 return sv;
873 }
874 return Nullsv; /* element cannot be deleted */
875 }
0a0bb7c7 876 }
902173a3 877#ifdef ENV_IS_CASELESS
14befaf4 878 else if (mg_find((SV*)hv, PERL_MAGIC_env)) {
f1317c8d 879 /* XXX This code isn't UTF8 clean. */
79cb57f6 880 keysv = sv_2mortal(newSVpvn(key,klen));
7f66fda2
NC
881 key = strupr(SvPVX(keysv));
882
883 if (k_flags & HVhek_FREEKEY) {
884 Safefree(keysave);
885 }
886
f1317c8d 887 is_utf8 = 0;
cd6d36ac 888 k_flags = 0;
1c846c1f 889 hash = 0;
2fd1c6b8 890 }
902173a3 891#endif
2fd1c6b8 892 }
fde52b5c 893 }
cbec9347
JH
894 xhv = (XPVHV*)SvANY(hv);
895 if (!xhv->xhv_array /* !HvARRAY(hv) */)
fde52b5c 896 return Nullsv;
897
19692e8d 898 if (is_utf8) {
7f66fda2
NC
899 const char *keysave = key;
900 key = (char*)bytes_from_utf8((U8*)key, &klen, &is_utf8);
cd6d36ac 901
19692e8d 902 if (is_utf8)
cd6d36ac
NC
903 k_flags |= HVhek_UTF8;
904 else
905 k_flags &= ~HVhek_UTF8;
7f66fda2
NC
906 if (key != keysave) {
907 if (k_flags & HVhek_FREEKEY) {
908 /* This shouldn't happen if our caller does what we expect,
909 but strictly the API allows it. */
910 Safefree(keysave);
911 }
912 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
913 }
cd6d36ac 914 HvHASKFLAGS_on((SV*)hv);
19692e8d 915 }
f9a63242 916
4b5190b5
NC
917 if (HvREHASH(hv)) {
918 PERL_HASH_INTERNAL(hash, key, klen);
919 } else if (!hash) {
7a9669ca
NC
920 if (keysv && (SvIsCOW_shared_hash(keysv))) {
921 hash = SvUVX(keysv);
922 } else {
923 PERL_HASH(hash, key, klen);
924 }
5afd6d42 925 PERL_HASH(hash, key, klen);
4b5190b5 926 }
fde52b5c 927
7a9669ca
NC
928 masked_flags = (k_flags & HVhek_MASK);
929
cbec9347
JH
930 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
931 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 932 entry = *oentry;
933 i = 1;
934 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
935 if (HeHASH(entry) != hash) /* strings can't be equal */
936 continue;
eb160463 937 if (HeKLEN(entry) != (I32)klen)
fde52b5c 938 continue;
1c846c1f 939 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 940 continue;
7a9669ca 941 if ((HeKFLAGS(entry) ^ masked_flags) & HVhek_UTF8)
c3654f1a 942 continue;
19692e8d
NC
943 if (k_flags & HVhek_FREEKEY)
944 Safefree(key);
8aacddc1
NIS
945
946 /* if placeholder is here, it's already been deleted.... */
7996736c 947 if (HeVAL(entry) == &PL_sv_placeholder)
8aacddc1
NIS
948 {
949 if (SvREADONLY(hv))
950 return Nullsv; /* if still SvREADONLY, leave it deleted. */
03fed38d
MB
951
952 /* okay, really delete the placeholder. */
953 *oentry = HeNEXT(entry);
954 if (i && !*oentry)
955 xhv->xhv_fill--; /* HvFILL(hv)-- */
956 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
957 HvLAZYDEL_on(hv);
958 else
959 hv_free_ent(hv, entry);
960 xhv->xhv_keys--; /* HvKEYS(hv)-- */
574c8022 961 if (xhv->xhv_keys == 0)
19692e8d 962 HvHASKFLAGS_off(hv);
03fed38d
MB
963 xhv->xhv_placeholders--;
964 return Nullsv;
8aacddc1
NIS
965 }
966 else if (SvREADONLY(hv) && HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
2393f1b9
JH
967 S_hv_notallowed(aTHX_ k_flags, key, klen,
968 "delete readonly key '%"SVf"' from"
969 );
8aacddc1
NIS
970 }
971
cd6d36ac 972 if (d_flags & G_DISCARD)
fde52b5c 973 sv = Nullsv;
94f7643d 974 else {
79d01fbf 975 sv = sv_2mortal(HeVAL(entry));
7996736c 976 HeVAL(entry) = &PL_sv_placeholder;
94f7643d 977 }
8aacddc1
NIS
978
979 /*
980 * If a restricted hash, rather than really deleting the entry, put
981 * a placeholder there. This marks the key as being "approved", so
982 * we can still access via not-really-existing key without raising
983 * an error.
984 */
985 if (SvREADONLY(hv)) {
7996736c 986 HeVAL(entry) = &PL_sv_placeholder;
8aacddc1
NIS
987 /* We'll be saving this slot, so the number of allocated keys
988 * doesn't go down, but the number placeholders goes up */
989 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
990 } else {
a26e96df
NIS
991 *oentry = HeNEXT(entry);
992 if (i && !*oentry)
993 xhv->xhv_fill--; /* HvFILL(hv)-- */
8aacddc1
NIS
994 if (entry == xhv->xhv_eiter /* HvEITER(hv) */)
995 HvLAZYDEL_on(hv);
996 else
997 hv_free_ent(hv, entry);
998 xhv->xhv_keys--; /* HvKEYS(hv)-- */
574c8022 999 if (xhv->xhv_keys == 0)
19692e8d 1000 HvHASKFLAGS_off(hv);
8aacddc1 1001 }
79072805
LW
1002 return sv;
1003 }
8aacddc1 1004 if (SvREADONLY(hv)) {
2393f1b9
JH
1005 S_hv_notallowed(aTHX_ k_flags, key, klen,
1006 "delete disallowed key '%"SVf"' from"
1007 );
8aacddc1
NIS
1008 }
1009
19692e8d 1010 if (k_flags & HVhek_FREEKEY)
f9a63242 1011 Safefree(key);
79072805 1012 return Nullsv;
79072805
LW
1013}
1014
954c1994
GS
1015/*
1016=for apidoc hv_exists
1017
1018Returns a boolean indicating whether the specified hash key exists. The
1019C<klen> is the length of the key.
1020
1021=cut
1022*/
1023
a0d0e21e 1024bool
9b7c1c41 1025Perl_hv_exists(pTHX_ HV *hv, const char *key, I32 klen_i32)
a0d0e21e 1026{
9b7c1c41
NC
1027 STRLEN klen;
1028 int flags;
1029
1030 if (klen_i32 < 0) {
1031 klen = -klen_i32;
1032 flags = HVhek_UTF8;
1033 } else {
1034 klen = klen_i32;
1035 flags = 0;
1036 }
7f66fda2
NC
1037 return hv_fetch_common(hv, NULL, key, klen, flags, HV_FETCH_ISEXISTS, 0)
1038 ? TRUE : FALSE;
fde52b5c 1039}
1040
954c1994
GS
1041/*
1042=for apidoc hv_exists_ent
1043
1044Returns a boolean indicating whether the specified hash key exists. C<hash>
1045can be a valid precomputed hash value, or 0 to ask for it to be
1046computed.
1047
1048=cut
1049*/
1050
fde52b5c 1051bool
864dbfa3 1052Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
fde52b5c 1053{
7f66fda2
NC
1054 return hv_fetch_common(hv, keysv, NULL, 0, 0, HV_FETCH_ISEXISTS, hash)
1055 ? TRUE : FALSE;
71596152
NC
1056}
1057
76e3520e 1058STATIC void
cea2e8a9 1059S_hsplit(pTHX_ HV *hv)
79072805 1060{
cbec9347
JH
1061 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1062 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
79072805
LW
1063 register I32 newsize = oldsize * 2;
1064 register I32 i;
cbec9347 1065 register char *a = xhv->xhv_array; /* HvARRAY(hv) */
72311751
GS
1066 register HE **aep;
1067 register HE **bep;
79072805
LW
1068 register HE *entry;
1069 register HE **oentry;
4b5190b5
NC
1070 int longest_chain = 0;
1071 int was_shared;
79072805 1072
3280af22 1073 PL_nomemok = TRUE;
8d6dde3e 1074#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 1075 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 1076 if (!a) {
4a33f861 1077 PL_nomemok = FALSE;
422a93e5
GA
1078 return;
1079 }
4633a7c4 1080#else
d18c6117 1081 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 1082 if (!a) {
3280af22 1083 PL_nomemok = FALSE;
422a93e5
GA
1084 return;
1085 }
cbec9347 1086 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
fba3b22e 1087 if (oldsize >= 64) {
cbec9347
JH
1088 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1089 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
4633a7c4
LW
1090 }
1091 else
cbec9347 1092 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
4633a7c4
LW
1093#endif
1094
3280af22 1095 PL_nomemok = FALSE;
72311751 1096 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
cbec9347
JH
1097 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1098 xhv->xhv_array = a; /* HvARRAY(hv) = a */
72311751 1099 aep = (HE**)a;
79072805 1100
72311751 1101 for (i=0; i<oldsize; i++,aep++) {
4b5190b5
NC
1102 int left_length = 0;
1103 int right_length = 0;
1104
72311751 1105 if (!*aep) /* non-existent */
79072805 1106 continue;
72311751
GS
1107 bep = aep+oldsize;
1108 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
eb160463 1109 if ((HeHASH(entry) & newsize) != (U32)i) {
fde52b5c 1110 *oentry = HeNEXT(entry);
72311751
GS
1111 HeNEXT(entry) = *bep;
1112 if (!*bep)
cbec9347 1113 xhv->xhv_fill++; /* HvFILL(hv)++ */
72311751 1114 *bep = entry;
4b5190b5 1115 right_length++;
79072805
LW
1116 continue;
1117 }
4b5190b5 1118 else {
fde52b5c 1119 oentry = &HeNEXT(entry);
4b5190b5
NC
1120 left_length++;
1121 }
79072805 1122 }
72311751 1123 if (!*aep) /* everything moved */
cbec9347 1124 xhv->xhv_fill--; /* HvFILL(hv)-- */
4b5190b5
NC
1125 /* I think we don't actually need to keep track of the longest length,
1126 merely flag if anything is too long. But for the moment while
1127 developing this code I'll track it. */
1128 if (left_length > longest_chain)
1129 longest_chain = left_length;
1130 if (right_length > longest_chain)
1131 longest_chain = right_length;
1132 }
1133
1134
1135 /* Pick your policy for "hashing isn't working" here: */
fdcd69b6 1136 if (longest_chain <= HV_MAX_LENGTH_BEFORE_SPLIT /* split worked? */
4b5190b5
NC
1137 || HvREHASH(hv)) {
1138 return;
79072805 1139 }
4b5190b5
NC
1140
1141 if (hv == PL_strtab) {
1142 /* Urg. Someone is doing something nasty to the string table.
1143 Can't win. */
1144 return;
1145 }
1146
1147 /* Awooga. Awooga. Pathological data. */
fdcd69b6 1148 /*PerlIO_printf(PerlIO_stderr(), "%p %d of %d with %d/%d buckets\n", hv,
4b5190b5
NC
1149 longest_chain, HvTOTALKEYS(hv), HvFILL(hv), 1+HvMAX(hv));*/
1150
1151 ++newsize;
1152 Newz(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
1153 was_shared = HvSHAREKEYS(hv);
1154
1155 xhv->xhv_fill = 0;
1156 HvSHAREKEYS_off(hv);
1157 HvREHASH_on(hv);
1158
1159 aep = (HE **) xhv->xhv_array;
1160
1161 for (i=0; i<newsize; i++,aep++) {
1162 entry = *aep;
1163 while (entry) {
1164 /* We're going to trash this HE's next pointer when we chain it
1165 into the new hash below, so store where we go next. */
1166 HE *next = HeNEXT(entry);
1167 UV hash;
1168
1169 /* Rehash it */
1170 PERL_HASH_INTERNAL(hash, HeKEY(entry), HeKLEN(entry));
1171
1172 if (was_shared) {
1173 /* Unshare it. */
1174 HEK *new_hek
1175 = save_hek_flags(HeKEY(entry), HeKLEN(entry),
1176 hash, HeKFLAGS(entry));
1177 unshare_hek (HeKEY_hek(entry));
1178 HeKEY_hek(entry) = new_hek;
1179 } else {
1180 /* Not shared, so simply write the new hash in. */
1181 HeHASH(entry) = hash;
1182 }
1183 /*PerlIO_printf(PerlIO_stderr(), "%d ", HeKFLAGS(entry));*/
1184 HEK_REHASH_on(HeKEY_hek(entry));
1185 /*PerlIO_printf(PerlIO_stderr(), "%d\n", HeKFLAGS(entry));*/
1186
1187 /* Copy oentry to the correct new chain. */
1188 bep = ((HE**)a) + (hash & (I32) xhv->xhv_max);
1189 if (!*bep)
1190 xhv->xhv_fill++; /* HvFILL(hv)++ */
1191 HeNEXT(entry) = *bep;
1192 *bep = entry;
1193
1194 entry = next;
1195 }
1196 }
1197 Safefree (xhv->xhv_array);
1198 xhv->xhv_array = a; /* HvARRAY(hv) = a */
79072805
LW
1199}
1200
72940dca 1201void
864dbfa3 1202Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
72940dca 1203{
cbec9347
JH
1204 register XPVHV* xhv = (XPVHV*)SvANY(hv);
1205 I32 oldsize = (I32) xhv->xhv_max+1; /* HvMAX(hv)+1 (sick) */
72940dca 1206 register I32 newsize;
1207 register I32 i;
1208 register I32 j;
72311751
GS
1209 register char *a;
1210 register HE **aep;
72940dca 1211 register HE *entry;
1212 register HE **oentry;
1213
1214 newsize = (I32) newmax; /* possible truncation here */
1215 if (newsize != newmax || newmax <= oldsize)
1216 return;
1217 while ((newsize & (1 + ~newsize)) != newsize) {
1218 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
1219 }
1220 if (newsize < newmax)
1221 newsize *= 2;
1222 if (newsize < newmax)
1223 return; /* overflow detection */
1224
cbec9347 1225 a = xhv->xhv_array; /* HvARRAY(hv) */
72940dca 1226 if (a) {
3280af22 1227 PL_nomemok = TRUE;
8d6dde3e 1228#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 1229 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
8aacddc1 1230 if (!a) {
4a33f861 1231 PL_nomemok = FALSE;
422a93e5
GA
1232 return;
1233 }
72940dca 1234#else
d18c6117 1235 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
8aacddc1 1236 if (!a) {
3280af22 1237 PL_nomemok = FALSE;
422a93e5
GA
1238 return;
1239 }
cbec9347 1240 Copy(xhv->xhv_array /* HvARRAY(hv) */, a, oldsize * sizeof(HE*), char);
fba3b22e 1241 if (oldsize >= 64) {
cbec9347
JH
1242 offer_nice_chunk(xhv->xhv_array /* HvARRAY(hv) */,
1243 PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
72940dca 1244 }
1245 else
cbec9347 1246 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
72940dca 1247#endif
3280af22 1248 PL_nomemok = FALSE;
72311751 1249 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
72940dca 1250 }
1251 else {
d18c6117 1252 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
72940dca 1253 }
cbec9347
JH
1254 xhv->xhv_max = --newsize; /* HvMAX(hv) = --newsize */
1255 xhv->xhv_array = a; /* HvARRAY(hv) = a */
1256 if (!xhv->xhv_fill /* !HvFILL(hv) */) /* skip rest if no entries */
72940dca 1257 return;
1258
72311751
GS
1259 aep = (HE**)a;
1260 for (i=0; i<oldsize; i++,aep++) {
1261 if (!*aep) /* non-existent */
72940dca 1262 continue;
72311751 1263 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
72940dca 1264 if ((j = (HeHASH(entry) & newsize)) != i) {
1265 j -= i;
1266 *oentry = HeNEXT(entry);
72311751 1267 if (!(HeNEXT(entry) = aep[j]))
cbec9347 1268 xhv->xhv_fill++; /* HvFILL(hv)++ */
72311751 1269 aep[j] = entry;
72940dca 1270 continue;
1271 }
1272 else
1273 oentry = &HeNEXT(entry);
1274 }
72311751 1275 if (!*aep) /* everything moved */
cbec9347 1276 xhv->xhv_fill--; /* HvFILL(hv)-- */
72940dca 1277 }
1278}
1279
954c1994
GS
1280/*
1281=for apidoc newHV
1282
1283Creates a new HV. The reference count is set to 1.
1284
1285=cut
1286*/
1287
79072805 1288HV *
864dbfa3 1289Perl_newHV(pTHX)
79072805
LW
1290{
1291 register HV *hv;
cbec9347 1292 register XPVHV* xhv;
79072805 1293
a0d0e21e
LW
1294 hv = (HV*)NEWSV(502,0);
1295 sv_upgrade((SV *)hv, SVt_PVHV);
cbec9347 1296 xhv = (XPVHV*)SvANY(hv);
79072805
LW
1297 SvPOK_off(hv);
1298 SvNOK_off(hv);
1c846c1f 1299#ifndef NODEFAULT_SHAREKEYS
fde52b5c 1300 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1c846c1f 1301#endif
4b5190b5 1302
cbec9347
JH
1303 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (start with 8 buckets) */
1304 xhv->xhv_fill = 0; /* HvFILL(hv) = 0 */
1305 xhv->xhv_pmroot = 0; /* HvPMROOT(hv) = 0 */
79072805
LW
1306 (void)hv_iterinit(hv); /* so each() will start off right */
1307 return hv;
1308}
1309
b3ac6de7 1310HV *
864dbfa3 1311Perl_newHVhv(pTHX_ HV *ohv)
b3ac6de7 1312{
b56ba0bf 1313 HV *hv = newHV();
4beac62f 1314 STRLEN hv_max, hv_fill;
4beac62f
AMS
1315
1316 if (!ohv || (hv_fill = HvFILL(ohv)) == 0)
1317 return hv;
4beac62f 1318 hv_max = HvMAX(ohv);
b3ac6de7 1319
b56ba0bf
AMS
1320 if (!SvMAGICAL((SV *)ohv)) {
1321 /* It's an ordinary hash, so copy it fast. AMS 20010804 */
eb160463
GS
1322 STRLEN i;
1323 bool shared = !!HvSHAREKEYS(ohv);
b56ba0bf 1324 HE **ents, **oents = (HE **)HvARRAY(ohv);
ff875642
JH
1325 char *a;
1326 New(0, a, PERL_HV_ARRAY_ALLOC_BYTES(hv_max+1), char);
1327 ents = (HE**)a;
b56ba0bf
AMS
1328
1329 /* In each bucket... */
1330 for (i = 0; i <= hv_max; i++) {
1331 HE *prev = NULL, *ent = NULL, *oent = oents[i];
1332
1333 if (!oent) {
1334 ents[i] = NULL;
1335 continue;
1336 }
1337
1338 /* Copy the linked list of entries. */
1339 for (oent = oents[i]; oent; oent = HeNEXT(oent)) {
1340 U32 hash = HeHASH(oent);
1341 char *key = HeKEY(oent);
19692e8d
NC
1342 STRLEN len = HeKLEN(oent);
1343 int flags = HeKFLAGS(oent);
b56ba0bf
AMS
1344
1345 ent = new_HE();
45dea987 1346 HeVAL(ent) = newSVsv(HeVAL(oent));
19692e8d
NC
1347 HeKEY_hek(ent)
1348 = shared ? share_hek_flags(key, len, hash, flags)
1349 : save_hek_flags(key, len, hash, flags);
b56ba0bf
AMS
1350 if (prev)
1351 HeNEXT(prev) = ent;
1352 else
1353 ents[i] = ent;
1354 prev = ent;
1355 HeNEXT(ent) = NULL;
1356 }
1357 }
1358
1359 HvMAX(hv) = hv_max;
1360 HvFILL(hv) = hv_fill;
8aacddc1 1361 HvTOTALKEYS(hv) = HvTOTALKEYS(ohv);
b56ba0bf 1362 HvARRAY(hv) = ents;
1c846c1f 1363 }
b56ba0bf
AMS
1364 else {
1365 /* Iterate over ohv, copying keys and values one at a time. */
b3ac6de7 1366 HE *entry;
b56ba0bf
AMS
1367 I32 riter = HvRITER(ohv);
1368 HE *eiter = HvEITER(ohv);
1369
1370 /* Can we use fewer buckets? (hv_max is always 2^n-1) */
1371 while (hv_max && hv_max + 1 >= hv_fill * 2)
1372 hv_max = hv_max / 2;
1373 HvMAX(hv) = hv_max;
1374
4a76a316 1375 hv_iterinit(ohv);
e16e2ff8 1376 while ((entry = hv_iternext_flags(ohv, 0))) {
19692e8d
NC
1377 hv_store_flags(hv, HeKEY(entry), HeKLEN(entry),
1378 newSVsv(HeVAL(entry)), HeHASH(entry),
1379 HeKFLAGS(entry));
b3ac6de7 1380 }
b56ba0bf
AMS
1381 HvRITER(ohv) = riter;
1382 HvEITER(ohv) = eiter;
b3ac6de7 1383 }
1c846c1f 1384
b3ac6de7
IZ
1385 return hv;
1386}
1387
79072805 1388void
864dbfa3 1389Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
79072805 1390{
16bdeea2
GS
1391 SV *val;
1392
68dc0745 1393 if (!entry)
79072805 1394 return;
16bdeea2 1395 val = HeVAL(entry);
257c9e5b 1396 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
3280af22 1397 PL_sub_generation++; /* may be deletion of method from stash */
16bdeea2 1398 SvREFCNT_dec(val);
68dc0745 1399 if (HeKLEN(entry) == HEf_SVKEY) {
1400 SvREFCNT_dec(HeKEY_sv(entry));
8aacddc1 1401 Safefree(HeKEY_hek(entry));
44a8e56a 1402 }
1403 else if (HvSHAREKEYS(hv))
68dc0745 1404 unshare_hek(HeKEY_hek(entry));
fde52b5c 1405 else
68dc0745 1406 Safefree(HeKEY_hek(entry));
d33b2eba 1407 del_HE(entry);
79072805
LW
1408}
1409
1410void
864dbfa3 1411Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
79072805 1412{
68dc0745 1413 if (!entry)
79072805 1414 return;
68dc0745 1415 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
3280af22 1416 PL_sub_generation++; /* may be deletion of method from stash */
68dc0745 1417 sv_2mortal(HeVAL(entry)); /* free between statements */
1418 if (HeKLEN(entry) == HEf_SVKEY) {
1419 sv_2mortal(HeKEY_sv(entry));
1420 Safefree(HeKEY_hek(entry));
44a8e56a 1421 }
1422 else if (HvSHAREKEYS(hv))
68dc0745 1423 unshare_hek(HeKEY_hek(entry));
fde52b5c 1424 else
68dc0745 1425 Safefree(HeKEY_hek(entry));
d33b2eba 1426 del_HE(entry);
79072805
LW
1427}
1428
954c1994
GS
1429/*
1430=for apidoc hv_clear
1431
1432Clears a hash, making it empty.
1433
1434=cut
1435*/
1436
79072805 1437void
864dbfa3 1438Perl_hv_clear(pTHX_ HV *hv)
79072805 1439{
cbec9347 1440 register XPVHV* xhv;
79072805
LW
1441 if (!hv)
1442 return;
49293501 1443
ecae49c0
NC
1444 DEBUG_A(Perl_hv_assert(aTHX_ hv));
1445
34c3c4e3
DM
1446 xhv = (XPVHV*)SvANY(hv);
1447
5f099cb0 1448 if (SvREADONLY(hv) && xhv->xhv_array != NULL) {
34c3c4e3 1449 /* restricted hash: convert all keys to placeholders */
3a676441
JH
1450 I32 i;
1451 HE* entry;
1452 for (i = 0; i <= (I32) xhv->xhv_max; i++) {
1453 entry = ((HE**)xhv->xhv_array)[i];
1454 for (; entry; entry = HeNEXT(entry)) {
1455 /* not already placeholder */
7996736c 1456 if (HeVAL(entry) != &PL_sv_placeholder) {
3a676441
JH
1457 if (HeVAL(entry) && SvREADONLY(HeVAL(entry))) {
1458 SV* keysv = hv_iterkeysv(entry);
1459 Perl_croak(aTHX_
1460 "Attempt to delete readonly key '%"SVf"' from a restricted hash",
1461 keysv);
1462 }
1463 SvREFCNT_dec(HeVAL(entry));
7996736c 1464 HeVAL(entry) = &PL_sv_placeholder;
3a676441
JH
1465 xhv->xhv_placeholders++; /* HvPLACEHOLDERS(hv)++ */
1466 }
34c3c4e3
DM
1467 }
1468 }
1469 return;
49293501
MS
1470 }
1471
463ee0b2 1472 hfreeentries(hv);
8aacddc1 1473 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
cbec9347
JH
1474 if (xhv->xhv_array /* HvARRAY(hv) */)
1475 (void)memzero(xhv->xhv_array /* HvARRAY(hv) */,
1476 (xhv->xhv_max+1 /* HvMAX(hv)+1 */) * sizeof(HE*));
a0d0e21e
LW
1477
1478 if (SvRMAGICAL(hv))
1c846c1f 1479 mg_clear((SV*)hv);
574c8022 1480
19692e8d 1481 HvHASKFLAGS_off(hv);
bb443f97 1482 HvREHASH_off(hv);
79072805
LW
1483}
1484
3540d4ce
AB
1485/*
1486=for apidoc hv_clear_placeholders
1487
1488Clears any placeholders from a hash. If a restricted hash has any of its keys
1489marked as readonly and the key is subsequently deleted, the key is not actually
1490deleted but is marked by assigning it a value of &PL_sv_placeholder. This tags
1491it so it will be ignored by future operations such as iterating over the hash,
1492but will still allow the hash to have a value reaasigned to the key at some
1493future point. This function clears any such placeholder keys from the hash.
1494See Hash::Util::lock_keys() for an example of its use.
1495
1496=cut
1497*/
1498
1499void
1500Perl_hv_clear_placeholders(pTHX_ HV *hv)
1501{
1502 I32 items;
1503 items = (I32)HvPLACEHOLDERS(hv);
1504 if (items) {
1505 HE *entry;
1506 I32 riter = HvRITER(hv);
1507 HE *eiter = HvEITER(hv);
1508 hv_iterinit(hv);
1509 /* This may look suboptimal with the items *after* the iternext, but
1510 it's quite deliberate. We only get here with items==0 if we've
1511 just deleted the last placeholder in the hash. If we've just done
1512 that then it means that the hash is in lazy delete mode, and the
1513 HE is now only referenced in our iterator. If we just quit the loop
1514 and discarded our iterator then the HE leaks. So we do the && the
1515 other way to ensure iternext is called just one more time, which
1516 has the side effect of triggering the lazy delete. */
1517 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))
1518 && items) {
1519 SV *val = hv_iterval(hv, entry);
1520
1521 if (val == &PL_sv_placeholder) {
1522
1523 /* It seems that I have to go back in the front of the hash
1524 API to delete a hash, even though I have a HE structure
1525 pointing to the very entry I want to delete, and could hold
1526 onto the previous HE that points to it. And it's easier to
1527 go in with SVs as I can then specify the precomputed hash,
1528 and don't have fun and games with utf8 keys. */
1529 SV *key = hv_iterkeysv(entry);
1530
1531 hv_delete_ent (hv, key, G_DISCARD, HeHASH(entry));
1532 items--;
1533 }
1534 }
1535 HvRITER(hv) = riter;
1536 HvEITER(hv) = eiter;
1537 }
1538}
1539
76e3520e 1540STATIC void
cea2e8a9 1541S_hfreeentries(pTHX_ HV *hv)
79072805 1542{
a0d0e21e 1543 register HE **array;
68dc0745 1544 register HE *entry;
1545 register HE *oentry = Null(HE*);
a0d0e21e
LW
1546 I32 riter;
1547 I32 max;
79072805
LW
1548
1549 if (!hv)
1550 return;
a0d0e21e 1551 if (!HvARRAY(hv))
79072805 1552 return;
a0d0e21e
LW
1553
1554 riter = 0;
1555 max = HvMAX(hv);
1556 array = HvARRAY(hv);
2f86008e
DM
1557 /* make everyone else think the array is empty, so that the destructors
1558 * called for freed entries can't recusively mess with us */
1559 HvARRAY(hv) = Null(HE**);
1560 HvFILL(hv) = 0;
1561 ((XPVHV*) SvANY(hv))->xhv_keys = 0;
1562
68dc0745 1563 entry = array[0];
a0d0e21e 1564 for (;;) {
68dc0745 1565 if (entry) {
1566 oentry = entry;
1567 entry = HeNEXT(entry);
1568 hv_free_ent(hv, oentry);
a0d0e21e 1569 }
68dc0745 1570 if (!entry) {
a0d0e21e
LW
1571 if (++riter > max)
1572 break;
68dc0745 1573 entry = array[riter];
1c846c1f 1574 }
79072805 1575 }
2f86008e 1576 HvARRAY(hv) = array;
a0d0e21e 1577 (void)hv_iterinit(hv);
79072805
LW
1578}
1579
954c1994
GS
1580/*
1581=for apidoc hv_undef
1582
1583Undefines the hash.
1584
1585=cut
1586*/
1587
79072805 1588void
864dbfa3 1589Perl_hv_undef(pTHX_ HV *hv)
79072805 1590{
cbec9347 1591 register XPVHV* xhv;
79072805
LW
1592 if (!hv)
1593 return;
ecae49c0 1594 DEBUG_A(Perl_hv_assert(aTHX_ hv));
cbec9347 1595 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1596 hfreeentries(hv);
cbec9347 1597 Safefree(xhv->xhv_array /* HvARRAY(hv) */);
85e6fe83 1598 if (HvNAME(hv)) {
7e8961ec
AB
1599 if(PL_stashcache)
1600 hv_delete(PL_stashcache, HvNAME(hv), strlen(HvNAME(hv)), G_DISCARD);
85e6fe83
LW
1601 Safefree(HvNAME(hv));
1602 HvNAME(hv) = 0;
1603 }
cbec9347
JH
1604 xhv->xhv_max = 7; /* HvMAX(hv) = 7 (it's a normal hash) */
1605 xhv->xhv_array = 0; /* HvARRAY(hv) = 0 */
8aacddc1 1606 xhv->xhv_placeholders = 0; /* HvPLACEHOLDERS(hv) = 0 */
a0d0e21e
LW
1607
1608 if (SvRMAGICAL(hv))
1c846c1f 1609 mg_clear((SV*)hv);
79072805
LW
1610}
1611
954c1994
GS
1612/*
1613=for apidoc hv_iterinit
1614
1615Prepares a starting point to traverse a hash table. Returns the number of
1616keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1c846c1f 1617currently only meaningful for hashes without tie magic.
954c1994
GS
1618
1619NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1620hash buckets that happen to be in use. If you still need that esoteric
1621value, you can get it through the macro C<HvFILL(tb)>.
1622
e16e2ff8 1623
954c1994
GS
1624=cut
1625*/
1626
79072805 1627I32
864dbfa3 1628Perl_hv_iterinit(pTHX_ HV *hv)
79072805 1629{
cbec9347 1630 register XPVHV* xhv;
aa689395 1631 HE *entry;
1632
1633 if (!hv)
cea2e8a9 1634 Perl_croak(aTHX_ "Bad hash");
cbec9347
JH
1635 xhv = (XPVHV*)SvANY(hv);
1636 entry = xhv->xhv_eiter; /* HvEITER(hv) */
72940dca 1637 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1638 HvLAZYDEL_off(hv);
68dc0745 1639 hv_free_ent(hv, entry);
72940dca 1640 }
cbec9347
JH
1641 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
1642 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
1643 /* used to be xhv->xhv_fill before 5.004_65 */
8aacddc1 1644 return XHvTOTALKEYS(xhv);
79072805 1645}
954c1994
GS
1646/*
1647=for apidoc hv_iternext
1648
1649Returns entries from a hash iterator. See C<hv_iterinit>.
1650
fe7bca90
NC
1651You may call C<hv_delete> or C<hv_delete_ent> on the hash entry that the
1652iterator currently points to, without losing your place or invalidating your
1653iterator. Note that in this case the current entry is deleted from the hash
1654with your iterator holding the last reference to it. Your iterator is flagged
1655to free the entry on the next call to C<hv_iternext>, so you must not discard
1656your iterator immediately else the entry will leak - call C<hv_iternext> to
1657trigger the resource deallocation.
1658
954c1994
GS
1659=cut
1660*/
1661
79072805 1662HE *
864dbfa3 1663Perl_hv_iternext(pTHX_ HV *hv)
79072805 1664{
e16e2ff8
NC
1665 return hv_iternext_flags(hv, 0);
1666}
1667
1668/*
fe7bca90
NC
1669=for apidoc hv_iternext_flags
1670
1671Returns entries from a hash iterator. See C<hv_iterinit> and C<hv_iternext>.
1672The C<flags> value will normally be zero; if HV_ITERNEXT_WANTPLACEHOLDERS is
1673set the placeholders keys (for restricted hashes) will be returned in addition
1674to normal keys. By default placeholders are automatically skipped over.
7996736c
MHM
1675Currently a placeholder is implemented with a value that is
1676C<&Perl_sv_placeholder>. Note that the implementation of placeholders and
fe7bca90
NC
1677restricted hashes may change, and the implementation currently is
1678insufficiently abstracted for any change to be tidy.
e16e2ff8 1679
fe7bca90 1680=cut
e16e2ff8
NC
1681*/
1682
1683HE *
1684Perl_hv_iternext_flags(pTHX_ HV *hv, I32 flags)
1685{
cbec9347 1686 register XPVHV* xhv;
79072805 1687 register HE *entry;
a0d0e21e 1688 HE *oldentry;
463ee0b2 1689 MAGIC* mg;
79072805
LW
1690
1691 if (!hv)
cea2e8a9 1692 Perl_croak(aTHX_ "Bad hash");
cbec9347
JH
1693 xhv = (XPVHV*)SvANY(hv);
1694 oldentry = entry = xhv->xhv_eiter; /* HvEITER(hv) */
463ee0b2 1695
14befaf4 1696 if ((mg = SvTIED_mg((SV*)hv, PERL_MAGIC_tied))) {
8990e307 1697 SV *key = sv_newmortal();
cd1469e6 1698 if (entry) {
fde52b5c 1699 sv_setsv(key, HeSVKEY_force(entry));
cd1469e6 1700 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1701 }
a0d0e21e 1702 else {
ff68c719 1703 char *k;
bbce6d69 1704 HEK *hek;
ff68c719 1705
cbec9347
JH
1706 /* one HE per MAGICAL hash */
1707 xhv->xhv_eiter = entry = new_HE(); /* HvEITER(hv) = new_HE() */
4633a7c4 1708 Zero(entry, 1, HE);
ff68c719 1709 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1710 hek = (HEK*)k;
1711 HeKEY_hek(entry) = hek;
fde52b5c 1712 HeKLEN(entry) = HEf_SVKEY;
a0d0e21e
LW
1713 }
1714 magic_nextpack((SV*) hv,mg,key);
8aacddc1 1715 if (SvOK(key)) {
cd1469e6 1716 /* force key to stay around until next time */
bbce6d69 1717 HeSVKEY_set(entry, SvREFCNT_inc(key));
1718 return entry; /* beware, hent_val is not set */
8aacddc1 1719 }
fde52b5c 1720 if (HeVAL(entry))
1721 SvREFCNT_dec(HeVAL(entry));
ff68c719 1722 Safefree(HeKEY_hek(entry));
d33b2eba 1723 del_HE(entry);
cbec9347 1724 xhv->xhv_eiter = Null(HE*); /* HvEITER(hv) = Null(HE*) */
463ee0b2 1725 return Null(HE*);
79072805 1726 }
f675dbe5 1727#ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
cbec9347 1728 if (!entry && SvRMAGICAL((SV*)hv) && mg_find((SV*)hv, PERL_MAGIC_env))
f675dbe5
CB
1729 prime_env_iter();
1730#endif
463ee0b2 1731
cbec9347
JH
1732 if (!xhv->xhv_array /* !HvARRAY(hv) */)
1733 Newz(506, xhv->xhv_array /* HvARRAY(hv) */,
1734 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max+1 /* HvMAX(hv)+1 */),
1735 char);
015a5f36 1736 /* At start of hash, entry is NULL. */
fde52b5c 1737 if (entry)
8aacddc1 1738 {
fde52b5c 1739 entry = HeNEXT(entry);
e16e2ff8
NC
1740 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
1741 /*
1742 * Skip past any placeholders -- don't want to include them in
1743 * any iteration.
1744 */
7996736c 1745 while (entry && HeVAL(entry) == &PL_sv_placeholder) {
e16e2ff8
NC
1746 entry = HeNEXT(entry);
1747 }
8aacddc1
NIS
1748 }
1749 }
fde52b5c 1750 while (!entry) {
015a5f36
NC
1751 /* OK. Come to the end of the current list. Grab the next one. */
1752
cbec9347 1753 xhv->xhv_riter++; /* HvRITER(hv)++ */
eb160463 1754 if (xhv->xhv_riter > (I32)xhv->xhv_max /* HvRITER(hv) > HvMAX(hv) */) {
015a5f36 1755 /* There is no next one. End of the hash. */
cbec9347 1756 xhv->xhv_riter = -1; /* HvRITER(hv) = -1 */
fde52b5c 1757 break;
79072805 1758 }
cbec9347
JH
1759 /* entry = (HvARRAY(hv))[HvRITER(hv)]; */
1760 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
8aacddc1 1761
e16e2ff8 1762 if (!(flags & HV_ITERNEXT_WANTPLACEHOLDERS)) {
015a5f36
NC
1763 /* If we have an entry, but it's a placeholder, don't count it.
1764 Try the next. */
7996736c 1765 while (entry && HeVAL(entry) == &PL_sv_placeholder)
015a5f36
NC
1766 entry = HeNEXT(entry);
1767 }
1768 /* Will loop again if this linked list starts NULL
1769 (for HV_ITERNEXT_WANTPLACEHOLDERS)
1770 or if we run through it and find only placeholders. */
fde52b5c 1771 }
79072805 1772
72940dca 1773 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1774 HvLAZYDEL_off(hv);
68dc0745 1775 hv_free_ent(hv, oldentry);
72940dca 1776 }
a0d0e21e 1777
fdcd69b6
NC
1778 /*if (HvREHASH(hv) && entry && !HeKREHASH(entry))
1779 PerlIO_printf(PerlIO_stderr(), "Awooga %p %p\n", hv, entry);*/
1780
cbec9347 1781 xhv->xhv_eiter = entry; /* HvEITER(hv) = entry */
79072805
LW
1782 return entry;
1783}
1784
954c1994
GS
1785/*
1786=for apidoc hv_iterkey
1787
1788Returns the key from the current position of the hash iterator. See
1789C<hv_iterinit>.
1790
1791=cut
1792*/
1793
79072805 1794char *
864dbfa3 1795Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
79072805 1796{
fde52b5c 1797 if (HeKLEN(entry) == HEf_SVKEY) {
fb73857a 1798 STRLEN len;
1799 char *p = SvPV(HeKEY_sv(entry), len);
1800 *retlen = len;
1801 return p;
fde52b5c 1802 }
1803 else {
1804 *retlen = HeKLEN(entry);
1805 return HeKEY(entry);
1806 }
1807}
1808
1809/* unlike hv_iterval(), this always returns a mortal copy of the key */
954c1994
GS
1810/*
1811=for apidoc hv_iterkeysv
1812
1813Returns the key as an C<SV*> from the current position of the hash
1814iterator. The return value will always be a mortal copy of the key. Also
1815see C<hv_iterinit>.
1816
1817=cut
1818*/
1819
fde52b5c 1820SV *
864dbfa3 1821Perl_hv_iterkeysv(pTHX_ register HE *entry)
fde52b5c 1822{
19692e8d
NC
1823 if (HeKLEN(entry) != HEf_SVKEY) {
1824 HEK *hek = HeKEY_hek(entry);
1825 int flags = HEK_FLAGS(hek);
1826 SV *sv;
1827
1828 if (flags & HVhek_WASUTF8) {
1829 /* Trouble :-)
1830 Andreas would like keys he put in as utf8 to come back as utf8
1831 */
1832 STRLEN utf8_len = HEK_LEN(hek);
2e5dfef7 1833 U8 *as_utf8 = bytes_to_utf8 ((U8*)HEK_KEY(hek), &utf8_len);
19692e8d 1834
2e5dfef7 1835 sv = newSVpvn ((char*)as_utf8, utf8_len);
19692e8d 1836 SvUTF8_on (sv);
c193270f 1837 Safefree (as_utf8); /* bytes_to_utf8() allocates a new string */
4b5190b5
NC
1838 } else if (flags & HVhek_REHASH) {
1839 /* We don't have a pointer to the hv, so we have to replicate the
1840 flag into every HEK. This hv is using custom a hasing
1841 algorithm. Hence we can't return a shared string scalar, as
1842 that would contain the (wrong) hash value, and might get passed
1843 into an hv routine with a regular hash */
1844
1845 sv = newSVpvn (HEK_KEY(hek), HEK_LEN(hek));
1846 if (HEK_UTF8(hek))
1847 SvUTF8_on (sv);
1848 } else {
19692e8d
NC
1849 sv = newSVpvn_share(HEK_KEY(hek),
1850 (HEK_UTF8(hek) ? -HEK_LEN(hek) : HEK_LEN(hek)),
1851 HEK_HASH(hek));
1852 }
1853 return sv_2mortal(sv);
1854 }
1855 return sv_mortalcopy(HeKEY_sv(entry));
79072805
LW
1856}
1857
954c1994
GS
1858/*
1859=for apidoc hv_iterval
1860
1861Returns the value from the current position of the hash iterator. See
1862C<hv_iterkey>.
1863
1864=cut
1865*/
1866
79072805 1867SV *
864dbfa3 1868Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
79072805 1869{
8990e307 1870 if (SvRMAGICAL(hv)) {
14befaf4 1871 if (mg_find((SV*)hv, PERL_MAGIC_tied)) {
8990e307 1872 SV* sv = sv_newmortal();
bbce6d69 1873 if (HeKLEN(entry) == HEf_SVKEY)
1874 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1875 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
463ee0b2
LW
1876 return sv;
1877 }
79072805 1878 }
fde52b5c 1879 return HeVAL(entry);
79072805
LW
1880}
1881
954c1994
GS
1882/*
1883=for apidoc hv_iternextsv
1884
1885Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1886operation.
1887
1888=cut
1889*/
1890
a0d0e21e 1891SV *
864dbfa3 1892Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
a0d0e21e
LW
1893{
1894 HE *he;
e16e2ff8 1895 if ( (he = hv_iternext_flags(hv, 0)) == NULL)
a0d0e21e
LW
1896 return NULL;
1897 *key = hv_iterkey(he, retlen);
1898 return hv_iterval(hv, he);
1899}
1900
954c1994
GS
1901/*
1902=for apidoc hv_magic
1903
1904Adds magic to a hash. See C<sv_magic>.
1905
1906=cut
1907*/
1908
79072805 1909void
864dbfa3 1910Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
79072805 1911{
a0d0e21e 1912 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
79072805 1913}
fde52b5c 1914
37d85e3a
JH
1915#if 0 /* use the macro from hv.h instead */
1916
bbce6d69 1917char*
864dbfa3 1918Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
bbce6d69 1919{
ff68c719 1920 return HEK_KEY(share_hek(sv, len, hash));
bbce6d69 1921}
1922
37d85e3a
JH
1923#endif
1924
bbce6d69 1925/* possibly free a shared string if no one has access to it
fde52b5c 1926 * len and hash must both be valid for str.
1927 */
bbce6d69 1928void
864dbfa3 1929Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
fde52b5c 1930{
19692e8d
NC
1931 unshare_hek_or_pvn (NULL, str, len, hash);
1932}
1933
1934
1935void
1936Perl_unshare_hek(pTHX_ HEK *hek)
1937{
1938 unshare_hek_or_pvn(hek, NULL, 0, 0);
1939}
1940
1941/* possibly free a shared string if no one has access to it
1942 hek if non-NULL takes priority over the other 3, else str, len and hash
1943 are used. If so, len and hash must both be valid for str.
1944 */
df132699 1945STATIC void
19692e8d
NC
1946S_unshare_hek_or_pvn(pTHX_ HEK *hek, const char *str, I32 len, U32 hash)
1947{
cbec9347 1948 register XPVHV* xhv;
fde52b5c 1949 register HE *entry;
1950 register HE **oentry;
1951 register I32 i = 1;
1952 I32 found = 0;
c3654f1a 1953 bool is_utf8 = FALSE;
19692e8d 1954 int k_flags = 0;
f9a63242 1955 const char *save = str;
c3654f1a 1956
19692e8d
NC
1957 if (hek) {
1958 hash = HEK_HASH(hek);
1959 } else if (len < 0) {
1960 STRLEN tmplen = -len;
1961 is_utf8 = TRUE;
1962 /* See the note in hv_fetch(). --jhi */
1963 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
1964 len = tmplen;
1965 if (is_utf8)
1966 k_flags = HVhek_UTF8;
1967 if (str != save)
1968 k_flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
c3654f1a 1969 }
1c846c1f 1970
fde52b5c 1971 /* what follows is the moral equivalent of:
6b88bc9c 1972 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
bbce6d69 1973 if (--*Svp == Nullsv)
6b88bc9c 1974 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
bbce6d69 1975 } */
cbec9347 1976 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1977 /* assert(xhv_array != 0) */
5f08fbcd 1978 LOCK_STRTAB_MUTEX;
cbec9347
JH
1979 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
1980 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
19692e8d
NC
1981 if (hek) {
1982 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1983 if (HeKEY_hek(entry) != hek)
1984 continue;
1985 found = 1;
1986 break;
1987 }
1988 } else {
1989 int flags_masked = k_flags & HVhek_MASK;
1990 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
1991 if (HeHASH(entry) != hash) /* strings can't be equal */
1992 continue;
1993 if (HeKLEN(entry) != len)
1994 continue;
1995 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
1996 continue;
1997 if (HeKFLAGS(entry) != flags_masked)
1998 continue;
1999 found = 1;
2000 break;
2001 }
2002 }
2003
2004 if (found) {
2005 if (--HeVAL(entry) == Nullsv) {
2006 *oentry = HeNEXT(entry);
2007 if (i && !*oentry)
2008 xhv->xhv_fill--; /* HvFILL(hv)-- */
2009 Safefree(HeKEY_hek(entry));
2010 del_HE(entry);
2011 xhv->xhv_keys--; /* HvKEYS(hv)-- */
2012 }
fde52b5c 2013 }
19692e8d 2014
333f433b 2015 UNLOCK_STRTAB_MUTEX;
411caa50 2016 if (!found && ckWARN_d(WARN_INTERNAL))
19692e8d
NC
2017 Perl_warner(aTHX_ packWARN(WARN_INTERNAL),
2018 "Attempt to free non-existent shared string '%s'%s",
2019 hek ? HEK_KEY(hek) : str,
2020 (k_flags & HVhek_UTF8) ? " (utf8)" : "");
2021 if (k_flags & HVhek_FREEKEY)
2022 Safefree(str);
fde52b5c 2023}
2024
bbce6d69 2025/* get a (constant) string ptr from the global string table
2026 * string will get added if it is not already there.
fde52b5c 2027 * len and hash must both be valid for str.
2028 */
bbce6d69 2029HEK *
864dbfa3 2030Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
fde52b5c 2031{
da58a35d 2032 bool is_utf8 = FALSE;
19692e8d 2033 int flags = 0;
f9a63242 2034 const char *save = str;
da58a35d
JH
2035
2036 if (len < 0) {
77caf834 2037 STRLEN tmplen = -len;
da58a35d 2038 is_utf8 = TRUE;
77caf834
JH
2039 /* See the note in hv_fetch(). --jhi */
2040 str = (char*)bytes_from_utf8((U8*)str, &tmplen, &is_utf8);
2041 len = tmplen;
19692e8d
NC
2042 /* If we were able to downgrade here, then than means that we were passed
2043 in a key which only had chars 0-255, but was utf8 encoded. */
2044 if (is_utf8)
2045 flags = HVhek_UTF8;
2046 /* If we found we were able to downgrade the string to bytes, then
2047 we should flag that it needs upgrading on keys or each. Also flag
2048 that we need share_hek_flags to free the string. */
2049 if (str != save)
2050 flags |= HVhek_WASUTF8 | HVhek_FREEKEY;
2051 }
2052
2053 return share_hek_flags (str, len, hash, flags);
2054}
2055
df132699 2056STATIC HEK *
19692e8d
NC
2057S_share_hek_flags(pTHX_ const char *str, I32 len, register U32 hash, int flags)
2058{
2059 register XPVHV* xhv;
2060 register HE *entry;
2061 register HE **oentry;
2062 register I32 i = 1;
2063 I32 found = 0;
2064 int flags_masked = flags & HVhek_MASK;
bbce6d69 2065
fde52b5c 2066 /* what follows is the moral equivalent of:
1c846c1f 2067
6b88bc9c 2068 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
8aacddc1 2069 hv_store(PL_strtab, str, len, Nullsv, hash);
fdcd69b6
NC
2070
2071 Can't rehash the shared string table, so not sure if it's worth
2072 counting the number of entries in the linked list
bbce6d69 2073 */
cbec9347 2074 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 2075 /* assert(xhv_array != 0) */
5f08fbcd 2076 LOCK_STRTAB_MUTEX;
cbec9347
JH
2077 /* oentry = &(HvARRAY(hv))[hash & (I32) HvMAX(hv)]; */
2078 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 2079 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
fde52b5c 2080 if (HeHASH(entry) != hash) /* strings can't be equal */
2081 continue;
2082 if (HeKLEN(entry) != len)
2083 continue;
1c846c1f 2084 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 2085 continue;
19692e8d 2086 if (HeKFLAGS(entry) != flags_masked)
c3654f1a 2087 continue;
fde52b5c 2088 found = 1;
fde52b5c 2089 break;
2090 }
bbce6d69 2091 if (!found) {
d33b2eba 2092 entry = new_HE();
19692e8d 2093 HeKEY_hek(entry) = save_hek_flags(str, len, hash, flags);
bbce6d69 2094 HeVAL(entry) = Nullsv;
2095 HeNEXT(entry) = *oentry;
2096 *oentry = entry;
cbec9347 2097 xhv->xhv_keys++; /* HvKEYS(hv)++ */
bbce6d69 2098 if (i) { /* initial entry? */
cbec9347 2099 xhv->xhv_fill++; /* HvFILL(hv)++ */
4c9cc595 2100 } else if (xhv->xhv_keys > (IV)xhv->xhv_max /* HvKEYS(hv) > HvMAX(hv) */) {
cbec9347 2101 hsplit(PL_strtab);
bbce6d69 2102 }
2103 }
2104
2105 ++HeVAL(entry); /* use value slot as REFCNT */
5f08fbcd 2106 UNLOCK_STRTAB_MUTEX;
19692e8d
NC
2107
2108 if (flags & HVhek_FREEKEY)
f9a63242 2109 Safefree(str);
19692e8d 2110
ff68c719 2111 return HeKEY_hek(entry);
fde52b5c 2112}
ecae49c0
NC
2113
2114
2115/*
2116=for apidoc hv_assert
2117
2118Check that a hash is in an internally consistent state.
2119
2120=cut
2121*/
2122
2123void
2124Perl_hv_assert(pTHX_ HV *hv)
2125{
2126 HE* entry;
2127 int withflags = 0;
2128 int placeholders = 0;
2129 int real = 0;
2130 int bad = 0;
2131 I32 riter = HvRITER(hv);
2132 HE *eiter = HvEITER(hv);
2133
2134 (void)hv_iterinit(hv);
2135
2136 while ((entry = hv_iternext_flags(hv, HV_ITERNEXT_WANTPLACEHOLDERS))) {
2137 /* sanity check the values */
2138 if (HeVAL(entry) == &PL_sv_placeholder) {
2139 placeholders++;
2140 } else {
2141 real++;
2142 }
2143 /* sanity check the keys */
2144 if (HeSVKEY(entry)) {
2145 /* Don't know what to check on SV keys. */
2146 } else if (HeKUTF8(entry)) {
2147 withflags++;
2148 if (HeKWASUTF8(entry)) {
2149 PerlIO_printf(Perl_debug_log,
2150 "hash key has both WASUFT8 and UTF8: '%.*s'\n",
2151 (int) HeKLEN(entry), HeKEY(entry));
2152 bad = 1;
2153 }
2154 } else if (HeKWASUTF8(entry)) {
2155 withflags++;
2156 }
2157 }
2158 if (!SvTIED_mg((SV*)hv, PERL_MAGIC_tied)) {
2159 if (HvUSEDKEYS(hv) != real) {
2160 PerlIO_printf(Perl_debug_log, "Count %d key(s), but hash reports %d\n",
2161 (int) real, (int) HvUSEDKEYS(hv));
2162 bad = 1;
2163 }
2164 if (HvPLACEHOLDERS(hv) != placeholders) {
2165 PerlIO_printf(Perl_debug_log,
2166 "Count %d placeholder(s), but hash reports %d\n",
2167 (int) placeholders, (int) HvPLACEHOLDERS(hv));
2168 bad = 1;
2169 }
2170 }
2171 if (withflags && ! HvHASKFLAGS(hv)) {
2172 PerlIO_printf(Perl_debug_log,
2173 "Hash has HASKFLAGS off but I count %d key(s) with flags\n",
2174 withflags);
2175 bad = 1;
2176 }
2177 if (bad) {
2178 sv_dump((SV *)hv);
2179 }
2180 HvRITER(hv) = riter; /* Restore hash iterator state */
2181 HvEITER(hv) = eiter;
2182}