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