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