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