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