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