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