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