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