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