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