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