This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
More #undefing of stdio.
[perl5.git] / hv.c
CommitLineData
a0d0e21e 1/* hv.c
79072805 2 *
3818b22b 3 * Copyright (c) 1991-2000, 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
1c846c1f 18
76e3520e 19STATIC HE*
cea2e8a9 20S_new_he(pTHX)
4633a7c4
LW
21{
22 HE* he;
333f433b
DG
23 LOCK_SV_MUTEX;
24 if (!PL_he_root)
25 more_he();
26 he = PL_he_root;
27 PL_he_root = HeNEXT(he);
28 UNLOCK_SV_MUTEX;
29 return he;
4633a7c4
LW
30}
31
76e3520e 32STATIC void
cea2e8a9 33S_del_he(pTHX_ HE *p)
4633a7c4 34{
333f433b 35 LOCK_SV_MUTEX;
3280af22
NIS
36 HeNEXT(p) = (HE*)PL_he_root;
37 PL_he_root = p;
333f433b 38 UNLOCK_SV_MUTEX;
4633a7c4
LW
39}
40
333f433b 41STATIC void
cea2e8a9 42S_more_he(pTHX)
4633a7c4
LW
43{
44 register HE* he;
45 register HE* heend;
612f20c3
GS
46 XPV *ptr;
47 New(54, ptr, 1008/sizeof(XPV), XPV);
48 ptr->xpv_pv = (char*)PL_he_arenaroot;
49 PL_he_arenaroot = ptr;
50
51 he = (HE*)ptr;
4633a7c4 52 heend = &he[1008 / sizeof(HE) - 1];
612f20c3 53 PL_he_root = ++he;
4633a7c4 54 while (he < heend) {
fde52b5c 55 HeNEXT(he) = (HE*)(he + 1);
4633a7c4
LW
56 he++;
57 }
fde52b5c 58 HeNEXT(he) = 0;
4633a7c4
LW
59}
60
d33b2eba
GS
61#ifdef PURIFY
62
63#define new_HE() (HE*)safemalloc(sizeof(HE))
64#define del_HE(p) safefree((char*)p)
65
66#else
67
68#define new_HE() new_he()
69#define del_HE(p) del_he(p)
70
71#endif
72
76e3520e 73STATIC HEK *
cea2e8a9 74S_save_hek(pTHX_ const char *str, I32 len, U32 hash)
bbce6d69 75{
76 char *k;
77 register HEK *hek;
1c846c1f 78
ff68c719 79 New(54, k, HEK_BASESIZE + len + 1, char);
bbce6d69 80 hek = (HEK*)k;
ff68c719 81 Copy(str, HEK_KEY(hek), len, char);
82 *(HEK_KEY(hek) + len) = '\0';
83 HEK_LEN(hek) = len;
84 HEK_HASH(hek) = hash;
bbce6d69 85 return hek;
86}
87
88void
864dbfa3 89Perl_unshare_hek(pTHX_ HEK *hek)
bbce6d69 90{
ff68c719 91 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
bbce6d69 92}
93
d18c6117
GS
94#if defined(USE_ITHREADS)
95HE *
96Perl_he_dup(pTHX_ HE *e, bool shared)
97{
98 HE *ret;
99
100 if (!e)
101 return Nullhe;
7766f137
GS
102 /* look for it in the table first */
103 ret = (HE*)ptr_table_fetch(PL_ptr_table, e);
104 if (ret)
105 return ret;
106
107 /* create anew and remember what it is */
d33b2eba 108 ret = new_HE();
7766f137
GS
109 ptr_table_store(PL_ptr_table, e, ret);
110
111 HeNEXT(ret) = he_dup(HeNEXT(e),shared);
d18c6117
GS
112 if (HeKLEN(e) == HEf_SVKEY)
113 HeKEY_sv(ret) = SvREFCNT_inc(sv_dup(HeKEY_sv(e)));
114 else if (shared)
115 HeKEY_hek(ret) = share_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
116 else
117 HeKEY_hek(ret) = save_hek(HeKEY(e), HeKLEN(e), HeHASH(e));
118 HeVAL(ret) = SvREFCNT_inc(sv_dup(HeVAL(e)));
119 return ret;
120}
121#endif /* USE_ITHREADS */
122
fde52b5c 123/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
124 * contains an SV* */
125
954c1994
GS
126/*
127=for apidoc hv_fetch
128
129Returns the SV which corresponds to the specified key in the hash. The
130C<klen> is the length of the key. If C<lval> is set then the fetch will be
131part of a store. Check that the return value is non-null before
1c846c1f 132dereferencing it to a C<SV*>.
954c1994 133
96f1132b 134See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
135information on how to use this function on tied hashes.
136
137=cut
138*/
139
79072805 140SV**
864dbfa3 141Perl_hv_fetch(pTHX_ HV *hv, const char *key, U32 klen, I32 lval)
79072805
LW
142{
143 register XPVHV* xhv;
fde52b5c 144 register U32 hash;
79072805 145 register HE *entry;
79072805 146 SV *sv;
79072805
LW
147
148 if (!hv)
149 return 0;
463ee0b2 150
8990e307 151 if (SvRMAGICAL(hv)) {
463ee0b2 152 if (mg_find((SV*)hv,'P')) {
11343788 153 dTHR;
8990e307 154 sv = sv_newmortal();
463ee0b2 155 mg_copy((SV*)hv, sv, key, klen);
3280af22
NIS
156 PL_hv_fetch_sv = sv;
157 return &PL_hv_fetch_sv;
463ee0b2 158 }
902173a3
GS
159#ifdef ENV_IS_CASELESS
160 else if (mg_find((SV*)hv,'E')) {
e7152ba2
GS
161 U32 i;
162 for (i = 0; i < klen; ++i)
163 if (isLOWER(key[i])) {
79cb57f6 164 char *nkey = strupr(SvPVX(sv_2mortal(newSVpvn(key,klen))));
e7152ba2
GS
165 SV **ret = hv_fetch(hv, nkey, klen, 0);
166 if (!ret && lval)
167 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
168 return ret;
169 }
902173a3
GS
170 }
171#endif
463ee0b2
LW
172 }
173
79072805
LW
174 xhv = (XPVHV*)SvANY(hv);
175 if (!xhv->xhv_array) {
1c846c1f 176 if (lval
a0d0e21e
LW
177#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
178 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
179#endif
180 )
d18c6117
GS
181 Newz(503, xhv->xhv_array,
182 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
79072805
LW
183 else
184 return 0;
185 }
186
fde52b5c 187 PERL_HASH(hash, key, klen);
79072805 188
a0d0e21e 189 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 190 for (; entry; entry = HeNEXT(entry)) {
191 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 192 continue;
fde52b5c 193 if (HeKLEN(entry) != klen)
79072805 194 continue;
1c846c1f 195 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 196 continue;
fde52b5c 197 return &HeVAL(entry);
79072805 198 }
a0d0e21e
LW
199#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
200 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
a6c40364
GS
201 unsigned long len;
202 char *env = PerlEnv_ENVgetenv_len(key,&len);
203 if (env) {
204 sv = newSVpvn(env,len);
205 SvTAINTED_on(sv);
206 return hv_store(hv,key,klen,sv,hash);
207 }
a0d0e21e
LW
208 }
209#endif
79072805
LW
210 if (lval) { /* gonna assign to this, so it better be there */
211 sv = NEWSV(61,0);
e7152ba2 212 return hv_store(hv,key,klen,sv,hash);
79072805
LW
213 }
214 return 0;
215}
216
fde52b5c 217/* returns a HE * structure with the all fields set */
218/* note that hent_val will be a mortal sv for MAGICAL hashes */
954c1994
GS
219/*
220=for apidoc hv_fetch_ent
221
222Returns the hash entry which corresponds to the specified key in the hash.
223C<hash> must be a valid precomputed hash number for the given C<key>, or 0
224if you want the function to compute it. IF C<lval> is set then the fetch
225will be part of a store. Make sure the return value is non-null before
226accessing it. The return value when C<tb> is a tied hash is a pointer to a
227static location, so be sure to make a copy of the structure if you need to
1c846c1f 228store it somewhere.
954c1994 229
96f1132b 230See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
231information on how to use this function on tied hashes.
232
233=cut
234*/
235
fde52b5c 236HE *
864dbfa3 237Perl_hv_fetch_ent(pTHX_ HV *hv, SV *keysv, I32 lval, register U32 hash)
fde52b5c 238{
239 register XPVHV* xhv;
240 register char *key;
241 STRLEN klen;
242 register HE *entry;
243 SV *sv;
244
245 if (!hv)
246 return 0;
247
902173a3
GS
248 if (SvRMAGICAL(hv)) {
249 if (mg_find((SV*)hv,'P')) {
6ff68fdd 250 dTHR;
902173a3
GS
251 sv = sv_newmortal();
252 keysv = sv_2mortal(newSVsv(keysv));
253 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
3280af22 254 if (!HeKEY_hek(&PL_hv_fetch_ent_mh)) {
902173a3
GS
255 char *k;
256 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
3280af22 257 HeKEY_hek(&PL_hv_fetch_ent_mh) = (HEK*)k;
902173a3 258 }
3280af22
NIS
259 HeSVKEY_set(&PL_hv_fetch_ent_mh, keysv);
260 HeVAL(&PL_hv_fetch_ent_mh) = sv;
261 return &PL_hv_fetch_ent_mh;
1cf368ac 262 }
902173a3
GS
263#ifdef ENV_IS_CASELESS
264 else if (mg_find((SV*)hv,'E')) {
e7152ba2 265 U32 i;
902173a3 266 key = SvPV(keysv, klen);
e7152ba2
GS
267 for (i = 0; i < klen; ++i)
268 if (isLOWER(key[i])) {
79cb57f6 269 SV *nkeysv = sv_2mortal(newSVpvn(key,klen));
e7152ba2
GS
270 (void)strupr(SvPVX(nkeysv));
271 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
272 if (!entry && lval)
273 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
274 return entry;
275 }
902173a3
GS
276 }
277#endif
fde52b5c 278 }
279
effa1e2d 280 xhv = (XPVHV*)SvANY(hv);
fde52b5c 281 if (!xhv->xhv_array) {
1c846c1f 282 if (lval
fde52b5c 283#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
284 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
285#endif
286 )
d18c6117
GS
287 Newz(503, xhv->xhv_array,
288 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
fde52b5c 289 else
290 return 0;
291 }
292
effa1e2d 293 key = SvPV(keysv, klen);
1c846c1f 294
effa1e2d 295 if (!hash)
296 PERL_HASH(hash, key, klen);
297
fde52b5c 298 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
299 for (; entry; entry = HeNEXT(entry)) {
300 if (HeHASH(entry) != hash) /* strings can't be equal */
301 continue;
302 if (HeKLEN(entry) != klen)
303 continue;
1c846c1f 304 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 305 continue;
306 return entry;
307 }
308#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
309 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
a6c40364
GS
310 unsigned long len;
311 char *env = PerlEnv_ENVgetenv_len(key,&len);
312 if (env) {
313 sv = newSVpvn(env,len);
314 SvTAINTED_on(sv);
315 return hv_store_ent(hv,keysv,sv,hash);
316 }
fde52b5c 317 }
318#endif
319 if (lval) { /* gonna assign to this, so it better be there */
320 sv = NEWSV(61,0);
e7152ba2 321 return hv_store_ent(hv,keysv,sv,hash);
fde52b5c 322 }
323 return 0;
324}
325
864dbfa3 326STATIC void
cea2e8a9 327S_hv_magic_check(pTHX_ HV *hv, bool *needs_copy, bool *needs_store)
d0066dc7
OT
328{
329 MAGIC *mg = SvMAGIC(hv);
330 *needs_copy = FALSE;
331 *needs_store = TRUE;
332 while (mg) {
333 if (isUPPER(mg->mg_type)) {
334 *needs_copy = TRUE;
335 switch (mg->mg_type) {
336 case 'P':
d0066dc7
OT
337 case 'S':
338 *needs_store = FALSE;
d0066dc7
OT
339 }
340 }
341 mg = mg->mg_moremagic;
342 }
343}
344
954c1994
GS
345/*
346=for apidoc hv_store
347
348Stores an SV in a hash. The hash key is specified as C<key> and C<klen> is
349the length of the key. The C<hash> parameter is the precomputed hash
350value; if it is zero then Perl will compute it. The return value will be
351NULL if the operation failed or if the value did not need to be actually
352stored within the hash (as in the case of tied hashes). Otherwise it can
353be dereferenced to get the original C<SV*>. Note that the caller is
354responsible for suitably incrementing the reference count of C<val> before
1c846c1f 355the call, and decrementing it if the function returned NULL.
954c1994 356
96f1132b 357See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
358information on how to use this function on tied hashes.
359
360=cut
361*/
362
79072805 363SV**
864dbfa3 364Perl_hv_store(pTHX_ HV *hv, const char *key, U32 klen, SV *val, register U32 hash)
79072805
LW
365{
366 register XPVHV* xhv;
79072805
LW
367 register I32 i;
368 register HE *entry;
369 register HE **oentry;
79072805
LW
370
371 if (!hv)
372 return 0;
373
374 xhv = (XPVHV*)SvANY(hv);
463ee0b2 375 if (SvMAGICAL(hv)) {
d0066dc7
OT
376 bool needs_copy;
377 bool needs_store;
378 hv_magic_check (hv, &needs_copy, &needs_store);
379 if (needs_copy) {
380 mg_copy((SV*)hv, val, key, klen);
381 if (!xhv->xhv_array && !needs_store)
382 return 0;
902173a3
GS
383#ifdef ENV_IS_CASELESS
384 else if (mg_find((SV*)hv,'E')) {
79cb57f6 385 SV *sv = sv_2mortal(newSVpvn(key,klen));
902173a3
GS
386 key = strupr(SvPVX(sv));
387 hash = 0;
388 }
389#endif
d0066dc7 390 }
463ee0b2 391 }
fde52b5c 392 if (!hash)
393 PERL_HASH(hash, key, klen);
394
395 if (!xhv->xhv_array)
d18c6117
GS
396 Newz(505, xhv->xhv_array,
397 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
fde52b5c 398
399 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
400 i = 1;
401
402 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
403 if (HeHASH(entry) != hash) /* strings can't be equal */
404 continue;
405 if (HeKLEN(entry) != klen)
406 continue;
1c846c1f 407 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 408 continue;
409 SvREFCNT_dec(HeVAL(entry));
410 HeVAL(entry) = val;
411 return &HeVAL(entry);
412 }
413
d33b2eba 414 entry = new_HE();
fde52b5c 415 if (HvSHAREKEYS(hv))
ff68c719 416 HeKEY_hek(entry) = share_hek(key, klen, hash);
fde52b5c 417 else /* gotta do the real thing */
ff68c719 418 HeKEY_hek(entry) = save_hek(key, klen, hash);
fde52b5c 419 HeVAL(entry) = val;
fde52b5c 420 HeNEXT(entry) = *oentry;
421 *oentry = entry;
422
423 xhv->xhv_keys++;
424 if (i) { /* initial entry? */
425 ++xhv->xhv_fill;
426 if (xhv->xhv_keys > xhv->xhv_max)
427 hsplit(hv);
79072805
LW
428 }
429
fde52b5c 430 return &HeVAL(entry);
431}
432
954c1994
GS
433/*
434=for apidoc hv_store_ent
435
436Stores C<val> in a hash. The hash key is specified as C<key>. The C<hash>
437parameter is the precomputed hash value; if it is zero then Perl will
438compute it. The return value is the new hash entry so created. It 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 the
441contents of the return value can be accessed using the C<He???> macros
442described here. Note that the caller is responsible for suitably
443incrementing the reference count of C<val> before the call, and
1c846c1f 444decrementing it if the function returned NULL.
954c1994 445
96f1132b 446See L<perlguts/"Understanding the Magic of Tied Hashes and Arrays"> for more
954c1994
GS
447information on how to use this function on tied hashes.
448
449=cut
450*/
451
fde52b5c 452HE *
864dbfa3 453Perl_hv_store_ent(pTHX_ HV *hv, SV *keysv, SV *val, register U32 hash)
fde52b5c 454{
455 register XPVHV* xhv;
456 register char *key;
457 STRLEN klen;
458 register I32 i;
459 register HE *entry;
460 register HE **oentry;
461
462 if (!hv)
463 return 0;
464
465 xhv = (XPVHV*)SvANY(hv);
466 if (SvMAGICAL(hv)) {
aeea060c 467 dTHR;
d0066dc7
OT
468 bool needs_copy;
469 bool needs_store;
470 hv_magic_check (hv, &needs_copy, &needs_store);
471 if (needs_copy) {
3280af22
NIS
472 bool save_taint = PL_tainted;
473 if (PL_tainting)
474 PL_tainted = SvTAINTED(keysv);
d0066dc7
OT
475 keysv = sv_2mortal(newSVsv(keysv));
476 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
477 TAINT_IF(save_taint);
478 if (!xhv->xhv_array && !needs_store)
479 return Nullhe;
902173a3
GS
480#ifdef ENV_IS_CASELESS
481 else if (mg_find((SV*)hv,'E')) {
482 key = SvPV(keysv, klen);
79cb57f6 483 keysv = sv_2mortal(newSVpvn(key,klen));
902173a3
GS
484 (void)strupr(SvPVX(keysv));
485 hash = 0;
486 }
487#endif
488 }
fde52b5c 489 }
490
491 key = SvPV(keysv, klen);
902173a3 492
fde52b5c 493 if (!hash)
494 PERL_HASH(hash, key, klen);
495
79072805 496 if (!xhv->xhv_array)
d18c6117
GS
497 Newz(505, xhv->xhv_array,
498 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
79072805 499
a0d0e21e 500 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805
LW
501 i = 1;
502
fde52b5c 503 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
504 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 505 continue;
fde52b5c 506 if (HeKLEN(entry) != klen)
79072805 507 continue;
1c846c1f 508 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 509 continue;
fde52b5c 510 SvREFCNT_dec(HeVAL(entry));
511 HeVAL(entry) = val;
512 return entry;
79072805 513 }
79072805 514
d33b2eba 515 entry = new_HE();
fde52b5c 516 if (HvSHAREKEYS(hv))
ff68c719 517 HeKEY_hek(entry) = share_hek(key, klen, hash);
fde52b5c 518 else /* gotta do the real thing */
ff68c719 519 HeKEY_hek(entry) = save_hek(key, klen, hash);
fde52b5c 520 HeVAL(entry) = val;
fde52b5c 521 HeNEXT(entry) = *oentry;
79072805
LW
522 *oentry = entry;
523
463ee0b2 524 xhv->xhv_keys++;
79072805 525 if (i) { /* initial entry? */
463ee0b2
LW
526 ++xhv->xhv_fill;
527 if (xhv->xhv_keys > xhv->xhv_max)
79072805
LW
528 hsplit(hv);
529 }
79072805 530
fde52b5c 531 return entry;
79072805
LW
532}
533
954c1994
GS
534/*
535=for apidoc hv_delete
536
537Deletes a key/value pair in the hash. The value SV is removed from the
1c846c1f 538hash and returned to the caller. The C<klen> is the length of the key.
954c1994
GS
539The C<flags> value will normally be zero; if set to G_DISCARD then NULL
540will be returned.
541
542=cut
543*/
544
79072805 545SV *
864dbfa3 546Perl_hv_delete(pTHX_ HV *hv, const char *key, U32 klen, I32 flags)
79072805
LW
547{
548 register XPVHV* xhv;
79072805 549 register I32 i;
fde52b5c 550 register U32 hash;
79072805
LW
551 register HE *entry;
552 register HE **oentry;
67a38de0 553 SV **svp;
79072805 554 SV *sv;
79072805
LW
555
556 if (!hv)
557 return Nullsv;
8990e307 558 if (SvRMAGICAL(hv)) {
0a0bb7c7
OT
559 bool needs_copy;
560 bool needs_store;
561 hv_magic_check (hv, &needs_copy, &needs_store);
562
67a38de0
NIS
563 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
564 sv = *svp;
0a0bb7c7
OT
565 mg_clear(sv);
566 if (!needs_store) {
567 if (mg_find(sv, 'p')) {
568 sv_unmagic(sv, 'p'); /* No longer an element */
569 return sv;
570 }
571 return Nullsv; /* element cannot be deleted */
572 }
902173a3 573#ifdef ENV_IS_CASELESS
2fd1c6b8 574 else if (mg_find((SV*)hv,'E')) {
79cb57f6 575 sv = sv_2mortal(newSVpvn(key,klen));
2fd1c6b8
GS
576 key = strupr(SvPVX(sv));
577 }
902173a3 578#endif
2fd1c6b8 579 }
463ee0b2 580 }
79072805
LW
581 xhv = (XPVHV*)SvANY(hv);
582 if (!xhv->xhv_array)
583 return Nullsv;
fde52b5c 584
585 PERL_HASH(hash, key, klen);
79072805 586
a0d0e21e 587 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805
LW
588 entry = *oentry;
589 i = 1;
fde52b5c 590 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
591 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 592 continue;
fde52b5c 593 if (HeKLEN(entry) != klen)
79072805 594 continue;
1c846c1f 595 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 596 continue;
fde52b5c 597 *oentry = HeNEXT(entry);
79072805
LW
598 if (i && !*oentry)
599 xhv->xhv_fill--;
748a9306
LW
600 if (flags & G_DISCARD)
601 sv = Nullsv;
94f7643d 602 else {
79d01fbf 603 sv = sv_2mortal(HeVAL(entry));
94f7643d
GS
604 HeVAL(entry) = &PL_sv_undef;
605 }
a0d0e21e 606 if (entry == xhv->xhv_eiter)
72940dca 607 HvLAZYDEL_on(hv);
a0d0e21e 608 else
68dc0745 609 hv_free_ent(hv, entry);
fde52b5c 610 --xhv->xhv_keys;
611 return sv;
612 }
613 return Nullsv;
614}
615
954c1994
GS
616/*
617=for apidoc hv_delete_ent
618
619Deletes a key/value pair in the hash. The value SV is removed from the
620hash and returned to the caller. The C<flags> value will normally be zero;
621if set to G_DISCARD then NULL will be returned. C<hash> can be a valid
622precomputed hash value, or 0 to ask for it to be computed.
623
624=cut
625*/
626
fde52b5c 627SV *
864dbfa3 628Perl_hv_delete_ent(pTHX_ HV *hv, SV *keysv, I32 flags, U32 hash)
fde52b5c 629{
630 register XPVHV* xhv;
631 register I32 i;
632 register char *key;
633 STRLEN klen;
634 register HE *entry;
635 register HE **oentry;
636 SV *sv;
1c846c1f 637
fde52b5c 638 if (!hv)
639 return Nullsv;
640 if (SvRMAGICAL(hv)) {
0a0bb7c7
OT
641 bool needs_copy;
642 bool needs_store;
643 hv_magic_check (hv, &needs_copy, &needs_store);
644
67a38de0 645 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
0a0bb7c7
OT
646 sv = HeVAL(entry);
647 mg_clear(sv);
648 if (!needs_store) {
649 if (mg_find(sv, 'p')) {
650 sv_unmagic(sv, 'p'); /* No longer an element */
651 return sv;
652 }
653 return Nullsv; /* element cannot be deleted */
654 }
902173a3 655#ifdef ENV_IS_CASELESS
2fd1c6b8
GS
656 else if (mg_find((SV*)hv,'E')) {
657 key = SvPV(keysv, klen);
79cb57f6 658 keysv = sv_2mortal(newSVpvn(key,klen));
2fd1c6b8 659 (void)strupr(SvPVX(keysv));
1c846c1f 660 hash = 0;
2fd1c6b8 661 }
902173a3 662#endif
2fd1c6b8 663 }
fde52b5c 664 }
665 xhv = (XPVHV*)SvANY(hv);
666 if (!xhv->xhv_array)
667 return Nullsv;
668
669 key = SvPV(keysv, klen);
1c846c1f 670
fde52b5c 671 if (!hash)
672 PERL_HASH(hash, key, klen);
673
674 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
675 entry = *oentry;
676 i = 1;
677 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
678 if (HeHASH(entry) != hash) /* strings can't be equal */
679 continue;
680 if (HeKLEN(entry) != klen)
681 continue;
1c846c1f 682 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 683 continue;
684 *oentry = HeNEXT(entry);
685 if (i && !*oentry)
686 xhv->xhv_fill--;
687 if (flags & G_DISCARD)
688 sv = Nullsv;
94f7643d 689 else {
79d01fbf 690 sv = sv_2mortal(HeVAL(entry));
94f7643d
GS
691 HeVAL(entry) = &PL_sv_undef;
692 }
fde52b5c 693 if (entry == xhv->xhv_eiter)
72940dca 694 HvLAZYDEL_on(hv);
fde52b5c 695 else
68dc0745 696 hv_free_ent(hv, entry);
463ee0b2 697 --xhv->xhv_keys;
79072805
LW
698 return sv;
699 }
79072805 700 return Nullsv;
79072805
LW
701}
702
954c1994
GS
703/*
704=for apidoc hv_exists
705
706Returns a boolean indicating whether the specified hash key exists. The
707C<klen> is the length of the key.
708
709=cut
710*/
711
a0d0e21e 712bool
864dbfa3 713Perl_hv_exists(pTHX_ HV *hv, const char *key, U32 klen)
a0d0e21e
LW
714{
715 register XPVHV* xhv;
fde52b5c 716 register U32 hash;
a0d0e21e
LW
717 register HE *entry;
718 SV *sv;
719
720 if (!hv)
721 return 0;
722
723 if (SvRMAGICAL(hv)) {
724 if (mg_find((SV*)hv,'P')) {
11343788 725 dTHR;
a0d0e21e 726 sv = sv_newmortal();
1c846c1f 727 mg_copy((SV*)hv, sv, key, klen);
a0d0e21e
LW
728 magic_existspack(sv, mg_find(sv, 'p'));
729 return SvTRUE(sv);
730 }
902173a3
GS
731#ifdef ENV_IS_CASELESS
732 else if (mg_find((SV*)hv,'E')) {
79cb57f6 733 sv = sv_2mortal(newSVpvn(key,klen));
902173a3
GS
734 key = strupr(SvPVX(sv));
735 }
736#endif
a0d0e21e
LW
737 }
738
739 xhv = (XPVHV*)SvANY(hv);
f675dbe5 740#ifndef DYNAMIC_ENV_FETCH
a0d0e21e 741 if (!xhv->xhv_array)
1c846c1f 742 return 0;
f675dbe5 743#endif
a0d0e21e 744
fde52b5c 745 PERL_HASH(hash, key, klen);
a0d0e21e 746
f675dbe5
CB
747#ifdef DYNAMIC_ENV_FETCH
748 if (!xhv->xhv_array) entry = Null(HE*);
749 else
750#endif
a0d0e21e 751 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 752 for (; entry; entry = HeNEXT(entry)) {
753 if (HeHASH(entry) != hash) /* strings can't be equal */
a0d0e21e 754 continue;
fde52b5c 755 if (HeKLEN(entry) != klen)
a0d0e21e 756 continue;
1c846c1f 757 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 758 continue;
759 return TRUE;
760 }
f675dbe5 761#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
a6c40364
GS
762 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
763 unsigned long len;
764 char *env = PerlEnv_ENVgetenv_len(key,&len);
765 if (env) {
766 sv = newSVpvn(env,len);
767 SvTAINTED_on(sv);
768 (void)hv_store(hv,key,klen,sv,hash);
769 return TRUE;
770 }
f675dbe5
CB
771 }
772#endif
fde52b5c 773 return FALSE;
774}
775
776
954c1994
GS
777/*
778=for apidoc hv_exists_ent
779
780Returns a boolean indicating whether the specified hash key exists. C<hash>
781can be a valid precomputed hash value, or 0 to ask for it to be
782computed.
783
784=cut
785*/
786
fde52b5c 787bool
864dbfa3 788Perl_hv_exists_ent(pTHX_ HV *hv, SV *keysv, U32 hash)
fde52b5c 789{
790 register XPVHV* xhv;
791 register char *key;
792 STRLEN klen;
793 register HE *entry;
794 SV *sv;
795
796 if (!hv)
797 return 0;
798
799 if (SvRMAGICAL(hv)) {
800 if (mg_find((SV*)hv,'P')) {
e858de61 801 dTHR; /* just for SvTRUE */
fde52b5c 802 sv = sv_newmortal();
effa1e2d 803 keysv = sv_2mortal(newSVsv(keysv));
1c846c1f 804 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
fde52b5c 805 magic_existspack(sv, mg_find(sv, 'p'));
806 return SvTRUE(sv);
807 }
902173a3
GS
808#ifdef ENV_IS_CASELESS
809 else if (mg_find((SV*)hv,'E')) {
810 key = SvPV(keysv, klen);
79cb57f6 811 keysv = sv_2mortal(newSVpvn(key,klen));
902173a3 812 (void)strupr(SvPVX(keysv));
1c846c1f 813 hash = 0;
902173a3
GS
814 }
815#endif
fde52b5c 816 }
817
818 xhv = (XPVHV*)SvANY(hv);
f675dbe5 819#ifndef DYNAMIC_ENV_FETCH
fde52b5c 820 if (!xhv->xhv_array)
1c846c1f 821 return 0;
f675dbe5 822#endif
fde52b5c 823
824 key = SvPV(keysv, klen);
825 if (!hash)
826 PERL_HASH(hash, key, klen);
827
f675dbe5
CB
828#ifdef DYNAMIC_ENV_FETCH
829 if (!xhv->xhv_array) entry = Null(HE*);
830 else
831#endif
fde52b5c 832 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
833 for (; entry; entry = HeNEXT(entry)) {
834 if (HeHASH(entry) != hash) /* strings can't be equal */
835 continue;
836 if (HeKLEN(entry) != klen)
837 continue;
1c846c1f 838 if (HeKEY(entry) != key && memNE(HeKEY(entry),key,klen)) /* is this it? */
a0d0e21e
LW
839 continue;
840 return TRUE;
841 }
f675dbe5 842#ifdef DYNAMIC_ENV_FETCH /* is it out there? */
a6c40364
GS
843 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME)) {
844 unsigned long len;
845 char *env = PerlEnv_ENVgetenv_len(key,&len);
846 if (env) {
847 sv = newSVpvn(env,len);
848 SvTAINTED_on(sv);
849 (void)hv_store_ent(hv,keysv,sv,hash);
850 return TRUE;
851 }
f675dbe5
CB
852 }
853#endif
a0d0e21e
LW
854 return FALSE;
855}
856
76e3520e 857STATIC void
cea2e8a9 858S_hsplit(pTHX_ HV *hv)
79072805
LW
859{
860 register XPVHV* xhv = (XPVHV*)SvANY(hv);
a0d0e21e 861 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
79072805
LW
862 register I32 newsize = oldsize * 2;
863 register I32 i;
72311751
GS
864 register char *a = xhv->xhv_array;
865 register HE **aep;
866 register HE **bep;
79072805
LW
867 register HE *entry;
868 register HE **oentry;
869
3280af22 870 PL_nomemok = TRUE;
8d6dde3e 871#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 872 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 873 if (!a) {
4a33f861 874 PL_nomemok = FALSE;
422a93e5
GA
875 return;
876 }
4633a7c4 877#else
4633a7c4 878#define MALLOC_OVERHEAD 16
d18c6117 879 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 880 if (!a) {
3280af22 881 PL_nomemok = FALSE;
422a93e5
GA
882 return;
883 }
72311751 884 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
fba3b22e 885 if (oldsize >= 64) {
d18c6117 886 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
4633a7c4
LW
887 }
888 else
889 Safefree(xhv->xhv_array);
890#endif
891
3280af22 892 PL_nomemok = FALSE;
72311751 893 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
79072805 894 xhv->xhv_max = --newsize;
72311751
GS
895 xhv->xhv_array = a;
896 aep = (HE**)a;
79072805 897
72311751
GS
898 for (i=0; i<oldsize; i++,aep++) {
899 if (!*aep) /* non-existent */
79072805 900 continue;
72311751
GS
901 bep = aep+oldsize;
902 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
fde52b5c 903 if ((HeHASH(entry) & newsize) != i) {
904 *oentry = HeNEXT(entry);
72311751
GS
905 HeNEXT(entry) = *bep;
906 if (!*bep)
79072805 907 xhv->xhv_fill++;
72311751 908 *bep = entry;
79072805
LW
909 continue;
910 }
911 else
fde52b5c 912 oentry = &HeNEXT(entry);
79072805 913 }
72311751 914 if (!*aep) /* everything moved */
79072805
LW
915 xhv->xhv_fill--;
916 }
917}
918
72940dca 919void
864dbfa3 920Perl_hv_ksplit(pTHX_ HV *hv, IV newmax)
72940dca 921{
922 register XPVHV* xhv = (XPVHV*)SvANY(hv);
923 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
924 register I32 newsize;
925 register I32 i;
926 register I32 j;
72311751
GS
927 register char *a;
928 register HE **aep;
72940dca 929 register HE *entry;
930 register HE **oentry;
931
932 newsize = (I32) newmax; /* possible truncation here */
933 if (newsize != newmax || newmax <= oldsize)
934 return;
935 while ((newsize & (1 + ~newsize)) != newsize) {
936 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
937 }
938 if (newsize < newmax)
939 newsize *= 2;
940 if (newsize < newmax)
941 return; /* overflow detection */
942
72311751 943 a = xhv->xhv_array;
72940dca 944 if (a) {
3280af22 945 PL_nomemok = TRUE;
8d6dde3e 946#if defined(STRANGE_MALLOC) || defined(MYMALLOC)
d18c6117 947 Renew(a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 948 if (!a) {
4a33f861 949 PL_nomemok = FALSE;
422a93e5
GA
950 return;
951 }
72940dca 952#else
d18c6117 953 New(2, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
422a93e5 954 if (!a) {
3280af22 955 PL_nomemok = FALSE;
422a93e5
GA
956 return;
957 }
72311751 958 Copy(xhv->xhv_array, a, oldsize * sizeof(HE*), char);
fba3b22e 959 if (oldsize >= 64) {
d18c6117 960 offer_nice_chunk(xhv->xhv_array, PERL_HV_ARRAY_ALLOC_BYTES(oldsize));
72940dca 961 }
962 else
963 Safefree(xhv->xhv_array);
964#endif
3280af22 965 PL_nomemok = FALSE;
72311751 966 Zero(&a[oldsize * sizeof(HE*)], (newsize-oldsize) * sizeof(HE*), char); /* zero 2nd half*/
72940dca 967 }
968 else {
d18c6117 969 Newz(0, a, PERL_HV_ARRAY_ALLOC_BYTES(newsize), char);
72940dca 970 }
971 xhv->xhv_max = --newsize;
72311751 972 xhv->xhv_array = a;
72940dca 973 if (!xhv->xhv_fill) /* skip rest if no entries */
974 return;
975
72311751
GS
976 aep = (HE**)a;
977 for (i=0; i<oldsize; i++,aep++) {
978 if (!*aep) /* non-existent */
72940dca 979 continue;
72311751 980 for (oentry = aep, entry = *aep; entry; entry = *oentry) {
72940dca 981 if ((j = (HeHASH(entry) & newsize)) != i) {
982 j -= i;
983 *oentry = HeNEXT(entry);
72311751 984 if (!(HeNEXT(entry) = aep[j]))
72940dca 985 xhv->xhv_fill++;
72311751 986 aep[j] = entry;
72940dca 987 continue;
988 }
989 else
990 oentry = &HeNEXT(entry);
991 }
72311751 992 if (!*aep) /* everything moved */
72940dca 993 xhv->xhv_fill--;
994 }
995}
996
954c1994
GS
997/*
998=for apidoc newHV
999
1000Creates a new HV. The reference count is set to 1.
1001
1002=cut
1003*/
1004
79072805 1005HV *
864dbfa3 1006Perl_newHV(pTHX)
79072805
LW
1007{
1008 register HV *hv;
1009 register XPVHV* xhv;
1010
a0d0e21e
LW
1011 hv = (HV*)NEWSV(502,0);
1012 sv_upgrade((SV *)hv, SVt_PVHV);
79072805
LW
1013 xhv = (XPVHV*)SvANY(hv);
1014 SvPOK_off(hv);
1015 SvNOK_off(hv);
1c846c1f 1016#ifndef NODEFAULT_SHAREKEYS
fde52b5c 1017 HvSHAREKEYS_on(hv); /* key-sharing on by default */
1c846c1f 1018#endif
463ee0b2 1019 xhv->xhv_max = 7; /* start with 8 buckets */
79072805
LW
1020 xhv->xhv_fill = 0;
1021 xhv->xhv_pmroot = 0;
79072805
LW
1022 (void)hv_iterinit(hv); /* so each() will start off right */
1023 return hv;
1024}
1025
b3ac6de7 1026HV *
864dbfa3 1027Perl_newHVhv(pTHX_ HV *ohv)
b3ac6de7
IZ
1028{
1029 register HV *hv;
b3ac6de7
IZ
1030 STRLEN hv_max = ohv ? HvMAX(ohv) : 0;
1031 STRLEN hv_fill = ohv ? HvFILL(ohv) : 0;
1032
1033 hv = newHV();
1034 while (hv_max && hv_max + 1 >= hv_fill * 2)
1035 hv_max = hv_max / 2; /* Is always 2^n-1 */
4a76a316 1036 HvMAX(hv) = hv_max;
b3ac6de7
IZ
1037 if (!hv_fill)
1038 return hv;
1039
1040#if 0
33c27489 1041 if (! SvTIED_mg((SV*)ohv, 'P')) {
b3ac6de7 1042 /* Quick way ???*/
1c846c1f
NIS
1043 }
1044 else
b3ac6de7
IZ
1045#endif
1046 {
1047 HE *entry;
1048 I32 hv_riter = HvRITER(ohv); /* current root of iterator */
1049 HE *hv_eiter = HvEITER(ohv); /* current entry of iterator */
1050
1051 /* Slow way */
4a76a316 1052 hv_iterinit(ohv);
155aba94 1053 while ((entry = hv_iternext(ohv))) {
1c846c1f 1054 hv_store(hv, HeKEY(entry), HeKLEN(entry),
b3ac6de7
IZ
1055 SvREFCNT_inc(HeVAL(entry)), HeHASH(entry));
1056 }
1057 HvRITER(ohv) = hv_riter;
1058 HvEITER(ohv) = hv_eiter;
1059 }
1c846c1f 1060
b3ac6de7
IZ
1061 return hv;
1062}
1063
79072805 1064void
864dbfa3 1065Perl_hv_free_ent(pTHX_ HV *hv, register HE *entry)
79072805 1066{
16bdeea2
GS
1067 SV *val;
1068
68dc0745 1069 if (!entry)
79072805 1070 return;
16bdeea2 1071 val = HeVAL(entry);
257c9e5b 1072 if (val && isGV(val) && GvCVu(val) && HvNAME(hv))
3280af22 1073 PL_sub_generation++; /* may be deletion of method from stash */
16bdeea2 1074 SvREFCNT_dec(val);
68dc0745 1075 if (HeKLEN(entry) == HEf_SVKEY) {
1076 SvREFCNT_dec(HeKEY_sv(entry));
1077 Safefree(HeKEY_hek(entry));
44a8e56a 1078 }
1079 else if (HvSHAREKEYS(hv))
68dc0745 1080 unshare_hek(HeKEY_hek(entry));
fde52b5c 1081 else
68dc0745 1082 Safefree(HeKEY_hek(entry));
d33b2eba 1083 del_HE(entry);
79072805
LW
1084}
1085
1086void
864dbfa3 1087Perl_hv_delayfree_ent(pTHX_ HV *hv, register HE *entry)
79072805 1088{
68dc0745 1089 if (!entry)
79072805 1090 return;
68dc0745 1091 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
3280af22 1092 PL_sub_generation++; /* may be deletion of method from stash */
68dc0745 1093 sv_2mortal(HeVAL(entry)); /* free between statements */
1094 if (HeKLEN(entry) == HEf_SVKEY) {
1095 sv_2mortal(HeKEY_sv(entry));
1096 Safefree(HeKEY_hek(entry));
44a8e56a 1097 }
1098 else if (HvSHAREKEYS(hv))
68dc0745 1099 unshare_hek(HeKEY_hek(entry));
fde52b5c 1100 else
68dc0745 1101 Safefree(HeKEY_hek(entry));
d33b2eba 1102 del_HE(entry);
79072805
LW
1103}
1104
954c1994
GS
1105/*
1106=for apidoc hv_clear
1107
1108Clears a hash, making it empty.
1109
1110=cut
1111*/
1112
79072805 1113void
864dbfa3 1114Perl_hv_clear(pTHX_ HV *hv)
79072805
LW
1115{
1116 register XPVHV* xhv;
1117 if (!hv)
1118 return;
1119 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1120 hfreeentries(hv);
79072805 1121 xhv->xhv_fill = 0;
a0d0e21e 1122 xhv->xhv_keys = 0;
79072805 1123 if (xhv->xhv_array)
463ee0b2 1124 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
a0d0e21e
LW
1125
1126 if (SvRMAGICAL(hv))
1c846c1f 1127 mg_clear((SV*)hv);
79072805
LW
1128}
1129
76e3520e 1130STATIC void
cea2e8a9 1131S_hfreeentries(pTHX_ HV *hv)
79072805 1132{
a0d0e21e 1133 register HE **array;
68dc0745 1134 register HE *entry;
1135 register HE *oentry = Null(HE*);
a0d0e21e
LW
1136 I32 riter;
1137 I32 max;
79072805
LW
1138
1139 if (!hv)
1140 return;
a0d0e21e 1141 if (!HvARRAY(hv))
79072805 1142 return;
a0d0e21e
LW
1143
1144 riter = 0;
1145 max = HvMAX(hv);
1146 array = HvARRAY(hv);
68dc0745 1147 entry = array[0];
a0d0e21e 1148 for (;;) {
68dc0745 1149 if (entry) {
1150 oentry = entry;
1151 entry = HeNEXT(entry);
1152 hv_free_ent(hv, oentry);
a0d0e21e 1153 }
68dc0745 1154 if (!entry) {
a0d0e21e
LW
1155 if (++riter > max)
1156 break;
68dc0745 1157 entry = array[riter];
1c846c1f 1158 }
79072805 1159 }
a0d0e21e 1160 (void)hv_iterinit(hv);
79072805
LW
1161}
1162
954c1994
GS
1163/*
1164=for apidoc hv_undef
1165
1166Undefines the hash.
1167
1168=cut
1169*/
1170
79072805 1171void
864dbfa3 1172Perl_hv_undef(pTHX_ HV *hv)
79072805
LW
1173{
1174 register XPVHV* xhv;
1175 if (!hv)
1176 return;
1177 xhv = (XPVHV*)SvANY(hv);
463ee0b2 1178 hfreeentries(hv);
79072805 1179 Safefree(xhv->xhv_array);
85e6fe83
LW
1180 if (HvNAME(hv)) {
1181 Safefree(HvNAME(hv));
1182 HvNAME(hv) = 0;
1183 }
79072805 1184 xhv->xhv_array = 0;
aa689395 1185 xhv->xhv_max = 7; /* it's a normal hash */
79072805 1186 xhv->xhv_fill = 0;
a0d0e21e
LW
1187 xhv->xhv_keys = 0;
1188
1189 if (SvRMAGICAL(hv))
1c846c1f 1190 mg_clear((SV*)hv);
79072805
LW
1191}
1192
954c1994
GS
1193/*
1194=for apidoc hv_iterinit
1195
1196Prepares a starting point to traverse a hash table. Returns the number of
1197keys in the hash (i.e. the same as C<HvKEYS(tb)>). The return value is
1c846c1f 1198currently only meaningful for hashes without tie magic.
954c1994
GS
1199
1200NOTE: Before version 5.004_65, C<hv_iterinit> used to return the number of
1201hash buckets that happen to be in use. If you still need that esoteric
1202value, you can get it through the macro C<HvFILL(tb)>.
1203
1204=cut
1205*/
1206
79072805 1207I32
864dbfa3 1208Perl_hv_iterinit(pTHX_ HV *hv)
79072805 1209{
aa689395 1210 register XPVHV* xhv;
1211 HE *entry;
1212
1213 if (!hv)
cea2e8a9 1214 Perl_croak(aTHX_ "Bad hash");
aa689395 1215 xhv = (XPVHV*)SvANY(hv);
1216 entry = xhv->xhv_eiter;
72940dca 1217 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1218 HvLAZYDEL_off(hv);
68dc0745 1219 hv_free_ent(hv, entry);
72940dca 1220 }
79072805
LW
1221 xhv->xhv_riter = -1;
1222 xhv->xhv_eiter = Null(HE*);
c6601927 1223 return xhv->xhv_keys; /* used to be xhv->xhv_fill before 5.004_65 */
79072805
LW
1224}
1225
954c1994
GS
1226/*
1227=for apidoc hv_iternext
1228
1229Returns entries from a hash iterator. See C<hv_iterinit>.
1230
1231=cut
1232*/
1233
79072805 1234HE *
864dbfa3 1235Perl_hv_iternext(pTHX_ HV *hv)
79072805
LW
1236{
1237 register XPVHV* xhv;
1238 register HE *entry;
a0d0e21e 1239 HE *oldentry;
463ee0b2 1240 MAGIC* mg;
79072805
LW
1241
1242 if (!hv)
cea2e8a9 1243 Perl_croak(aTHX_ "Bad hash");
79072805 1244 xhv = (XPVHV*)SvANY(hv);
a0d0e21e 1245 oldentry = entry = xhv->xhv_eiter;
463ee0b2 1246
155aba94 1247 if ((mg = SvTIED_mg((SV*)hv, 'P'))) {
8990e307 1248 SV *key = sv_newmortal();
cd1469e6 1249 if (entry) {
fde52b5c 1250 sv_setsv(key, HeSVKEY_force(entry));
cd1469e6 1251 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
1252 }
a0d0e21e 1253 else {
ff68c719 1254 char *k;
bbce6d69 1255 HEK *hek;
ff68c719 1256
d33b2eba 1257 xhv->xhv_eiter = entry = new_HE(); /* one HE per MAGICAL hash */
4633a7c4 1258 Zero(entry, 1, HE);
ff68c719 1259 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
1260 hek = (HEK*)k;
1261 HeKEY_hek(entry) = hek;
fde52b5c 1262 HeKLEN(entry) = HEf_SVKEY;
a0d0e21e
LW
1263 }
1264 magic_nextpack((SV*) hv,mg,key);
463ee0b2 1265 if (SvOK(key)) {
cd1469e6 1266 /* force key to stay around until next time */
bbce6d69 1267 HeSVKEY_set(entry, SvREFCNT_inc(key));
1268 return entry; /* beware, hent_val is not set */
463ee0b2 1269 }
fde52b5c 1270 if (HeVAL(entry))
1271 SvREFCNT_dec(HeVAL(entry));
ff68c719 1272 Safefree(HeKEY_hek(entry));
d33b2eba 1273 del_HE(entry);
463ee0b2
LW
1274 xhv->xhv_eiter = Null(HE*);
1275 return Null(HE*);
79072805 1276 }
f675dbe5
CB
1277#ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
1278 if (!entry && HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
1279 prime_env_iter();
1280#endif
463ee0b2 1281
79072805 1282 if (!xhv->xhv_array)
d18c6117
GS
1283 Newz(506, xhv->xhv_array,
1284 PERL_HV_ARRAY_ALLOC_BYTES(xhv->xhv_max + 1), char);
fde52b5c 1285 if (entry)
1286 entry = HeNEXT(entry);
1287 while (!entry) {
1288 ++xhv->xhv_riter;
1289 if (xhv->xhv_riter > xhv->xhv_max) {
1290 xhv->xhv_riter = -1;
1291 break;
79072805 1292 }
fde52b5c 1293 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1294 }
79072805 1295
72940dca 1296 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1297 HvLAZYDEL_off(hv);
68dc0745 1298 hv_free_ent(hv, oldentry);
72940dca 1299 }
a0d0e21e 1300
79072805
LW
1301 xhv->xhv_eiter = entry;
1302 return entry;
1303}
1304
954c1994
GS
1305/*
1306=for apidoc hv_iterkey
1307
1308Returns the key from the current position of the hash iterator. See
1309C<hv_iterinit>.
1310
1311=cut
1312*/
1313
79072805 1314char *
864dbfa3 1315Perl_hv_iterkey(pTHX_ register HE *entry, I32 *retlen)
79072805 1316{
fde52b5c 1317 if (HeKLEN(entry) == HEf_SVKEY) {
fb73857a 1318 STRLEN len;
1319 char *p = SvPV(HeKEY_sv(entry), len);
1320 *retlen = len;
1321 return p;
fde52b5c 1322 }
1323 else {
1324 *retlen = HeKLEN(entry);
1325 return HeKEY(entry);
1326 }
1327}
1328
1329/* unlike hv_iterval(), this always returns a mortal copy of the key */
954c1994
GS
1330/*
1331=for apidoc hv_iterkeysv
1332
1333Returns the key as an C<SV*> from the current position of the hash
1334iterator. The return value will always be a mortal copy of the key. Also
1335see C<hv_iterinit>.
1336
1337=cut
1338*/
1339
fde52b5c 1340SV *
864dbfa3 1341Perl_hv_iterkeysv(pTHX_ register HE *entry)
fde52b5c 1342{
1343 if (HeKLEN(entry) == HEf_SVKEY)
bbce6d69 1344 return sv_mortalcopy(HeKEY_sv(entry));
1c846c1f
NIS
1345 else {
1346 return sv_2mortal(newSVpvn_share((HeKLEN(entry) ? HeKEY(entry) : ""),
1347 HeKLEN(entry), HeHASH(entry)));
1348 }
79072805
LW
1349}
1350
954c1994
GS
1351/*
1352=for apidoc hv_iterval
1353
1354Returns the value from the current position of the hash iterator. See
1355C<hv_iterkey>.
1356
1357=cut
1358*/
1359
79072805 1360SV *
864dbfa3 1361Perl_hv_iterval(pTHX_ HV *hv, register HE *entry)
79072805 1362{
8990e307 1363 if (SvRMAGICAL(hv)) {
463ee0b2 1364 if (mg_find((SV*)hv,'P')) {
8990e307 1365 SV* sv = sv_newmortal();
bbce6d69 1366 if (HeKLEN(entry) == HEf_SVKEY)
1367 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1368 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
463ee0b2
LW
1369 return sv;
1370 }
79072805 1371 }
fde52b5c 1372 return HeVAL(entry);
79072805
LW
1373}
1374
954c1994
GS
1375/*
1376=for apidoc hv_iternextsv
1377
1378Performs an C<hv_iternext>, C<hv_iterkey>, and C<hv_iterval> in one
1379operation.
1380
1381=cut
1382*/
1383
a0d0e21e 1384SV *
864dbfa3 1385Perl_hv_iternextsv(pTHX_ HV *hv, char **key, I32 *retlen)
a0d0e21e
LW
1386{
1387 HE *he;
1388 if ( (he = hv_iternext(hv)) == NULL)
1389 return NULL;
1390 *key = hv_iterkey(he, retlen);
1391 return hv_iterval(hv, he);
1392}
1393
954c1994
GS
1394/*
1395=for apidoc hv_magic
1396
1397Adds magic to a hash. See C<sv_magic>.
1398
1399=cut
1400*/
1401
79072805 1402void
864dbfa3 1403Perl_hv_magic(pTHX_ HV *hv, GV *gv, int how)
79072805 1404{
a0d0e21e 1405 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
79072805 1406}
fde52b5c 1407
bbce6d69 1408char*
864dbfa3 1409Perl_sharepvn(pTHX_ const char *sv, I32 len, U32 hash)
bbce6d69 1410{
ff68c719 1411 return HEK_KEY(share_hek(sv, len, hash));
bbce6d69 1412}
1413
1414/* possibly free a shared string if no one has access to it
fde52b5c 1415 * len and hash must both be valid for str.
1416 */
bbce6d69 1417void
864dbfa3 1418Perl_unsharepvn(pTHX_ const char *str, I32 len, U32 hash)
fde52b5c 1419{
1420 register XPVHV* xhv;
1421 register HE *entry;
1422 register HE **oentry;
1423 register I32 i = 1;
1424 I32 found = 0;
1c846c1f 1425
fde52b5c 1426 /* what follows is the moral equivalent of:
6b88bc9c 1427 if ((Svp = hv_fetch(PL_strtab, tmpsv, FALSE, hash))) {
bbce6d69 1428 if (--*Svp == Nullsv)
6b88bc9c 1429 hv_delete(PL_strtab, str, len, G_DISCARD, hash);
bbce6d69 1430 } */
3280af22 1431 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1432 /* assert(xhv_array != 0) */
5f08fbcd 1433 LOCK_STRTAB_MUTEX;
fde52b5c 1434 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1435 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
fde52b5c 1436 if (HeHASH(entry) != hash) /* strings can't be equal */
1437 continue;
1438 if (HeKLEN(entry) != len)
1439 continue;
1c846c1f 1440 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1441 continue;
1442 found = 1;
bbce6d69 1443 if (--HeVAL(entry) == Nullsv) {
1444 *oentry = HeNEXT(entry);
1445 if (i && !*oentry)
1446 xhv->xhv_fill--;
ff68c719 1447 Safefree(HeKEY_hek(entry));
d33b2eba 1448 del_HE(entry);
bbce6d69 1449 --xhv->xhv_keys;
fde52b5c 1450 }
bbce6d69 1451 break;
fde52b5c 1452 }
333f433b 1453 UNLOCK_STRTAB_MUTEX;
1c846c1f 1454
0453d815
PM
1455 {
1456 dTHR;
1457 if (!found && ckWARN_d(WARN_INTERNAL))
1c846c1f 1458 Perl_warner(aTHX_ WARN_INTERNAL, "Attempt to free non-existent shared string '%s'",str);
0453d815 1459 }
fde52b5c 1460}
1461
bbce6d69 1462/* get a (constant) string ptr from the global string table
1463 * string will get added if it is not already there.
fde52b5c 1464 * len and hash must both be valid for str.
1465 */
bbce6d69 1466HEK *
864dbfa3 1467Perl_share_hek(pTHX_ const char *str, I32 len, register U32 hash)
fde52b5c 1468{
1469 register XPVHV* xhv;
1470 register HE *entry;
1471 register HE **oentry;
1472 register I32 i = 1;
1473 I32 found = 0;
bbce6d69 1474
fde52b5c 1475 /* what follows is the moral equivalent of:
1c846c1f 1476
6b88bc9c
GS
1477 if (!(Svp = hv_fetch(PL_strtab, str, len, FALSE)))
1478 hv_store(PL_strtab, str, len, Nullsv, hash);
bbce6d69 1479 */
3280af22 1480 xhv = (XPVHV*)SvANY(PL_strtab);
fde52b5c 1481 /* assert(xhv_array != 0) */
5f08fbcd 1482 LOCK_STRTAB_MUTEX;
fde52b5c 1483 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1484 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
fde52b5c 1485 if (HeHASH(entry) != hash) /* strings can't be equal */
1486 continue;
1487 if (HeKLEN(entry) != len)
1488 continue;
1c846c1f 1489 if (HeKEY(entry) != str && memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1490 continue;
1491 found = 1;
fde52b5c 1492 break;
1493 }
bbce6d69 1494 if (!found) {
d33b2eba 1495 entry = new_HE();
ff68c719 1496 HeKEY_hek(entry) = save_hek(str, len, hash);
bbce6d69 1497 HeVAL(entry) = Nullsv;
1498 HeNEXT(entry) = *oentry;
1499 *oentry = entry;
1500 xhv->xhv_keys++;
1501 if (i) { /* initial entry? */
1502 ++xhv->xhv_fill;
1503 if (xhv->xhv_keys > xhv->xhv_max)
3280af22 1504 hsplit(PL_strtab);
bbce6d69 1505 }
1506 }
1507
1508 ++HeVAL(entry); /* use value slot as REFCNT */
5f08fbcd 1509 UNLOCK_STRTAB_MUTEX;
ff68c719 1510 return HeKEY_hek(entry);
fde52b5c 1511}
1512
bbce6d69 1513
61c8b479 1514