This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[win32] maintpatch
[perl5.git] / hv.c
CommitLineData
a0d0e21e 1/* hv.c
79072805 2 *
9607fc9c 3 * Copyright (c) 1991-1997, 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"
15#include "perl.h"
16
a0d0e21e
LW
17static void hsplit _((HV *hv));
18static void hfreeentries _((HV *hv));
c31fac66
GS
19static void hv_magic_check _((HV *hv, bool *needs_copy, bool *needs_store));
20static HE* more_he _((void));
4633a7c4
LW
21
22static HE*
8ac85365 23new_he(void)
4633a7c4
LW
24{
25 HE* he;
26 if (he_root) {
27 he = he_root;
fde52b5c 28 he_root = HeNEXT(he);
4633a7c4
LW
29 return he;
30 }
31 return more_he();
32}
33
34static void
8ac85365 35del_he(HE *p)
4633a7c4 36{
fde52b5c 37 HeNEXT(p) = (HE*)he_root;
4633a7c4
LW
38 he_root = p;
39}
40
41static HE*
8ac85365 42more_he(void)
4633a7c4
LW
43{
44 register HE* he;
45 register HE* heend;
8c52afec 46 New(54, he_root, 1008/sizeof(HE), HE);
4633a7c4
LW
47 he = he_root;
48 heend = &he[1008 / sizeof(HE) - 1];
49 while (he < heend) {
fde52b5c 50 HeNEXT(he) = (HE*)(he + 1);
4633a7c4
LW
51 he++;
52 }
fde52b5c 53 HeNEXT(he) = 0;
4633a7c4
LW
54 return new_he();
55}
56
bbce6d69 57static HEK *
8ac85365 58save_hek(char *str, I32 len, U32 hash)
bbce6d69 59{
60 char *k;
61 register HEK *hek;
62
ff68c719 63 New(54, k, HEK_BASESIZE + len + 1, char);
bbce6d69 64 hek = (HEK*)k;
ff68c719 65 Copy(str, HEK_KEY(hek), len, char);
66 *(HEK_KEY(hek) + len) = '\0';
67 HEK_LEN(hek) = len;
68 HEK_HASH(hek) = hash;
bbce6d69 69 return hek;
70}
71
72void
8ac85365 73unshare_hek(HEK *hek)
bbce6d69 74{
ff68c719 75 unsharepvn(HEK_KEY(hek),HEK_LEN(hek),HEK_HASH(hek));
bbce6d69 76}
77
fde52b5c 78/* (klen == HEf_SVKEY) is special for MAGICAL hv entries, meaning key slot
79 * contains an SV* */
80
79072805 81SV**
8ac85365 82hv_fetch(HV *hv, char *key, U32 klen, I32 lval)
79072805
LW
83{
84 register XPVHV* xhv;
fde52b5c 85 register U32 hash;
79072805 86 register HE *entry;
79072805 87 SV *sv;
79072805
LW
88
89 if (!hv)
90 return 0;
463ee0b2 91
8990e307 92 if (SvRMAGICAL(hv)) {
463ee0b2 93 if (mg_find((SV*)hv,'P')) {
11343788 94 dTHR;
8990e307 95 sv = sv_newmortal();
463ee0b2 96 mg_copy((SV*)hv, sv, key, klen);
463ee0b2
LW
97 Sv = sv;
98 return &Sv;
99 }
902173a3
GS
100#ifdef ENV_IS_CASELESS
101 else if (mg_find((SV*)hv,'E')) {
e7152ba2
GS
102 U32 i;
103 for (i = 0; i < klen; ++i)
104 if (isLOWER(key[i])) {
105 char *nkey = strupr(SvPVX(sv_2mortal(newSVpv(key,klen))));
106 SV **ret = hv_fetch(hv, nkey, klen, 0);
107 if (!ret && lval)
108 ret = hv_store(hv, key, klen, NEWSV(61,0), 0);
109 return ret;
110 }
902173a3
GS
111 }
112#endif
463ee0b2
LW
113 }
114
79072805
LW
115 xhv = (XPVHV*)SvANY(hv);
116 if (!xhv->xhv_array) {
a0d0e21e
LW
117 if (lval
118#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
119 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
120#endif
121 )
463ee0b2 122 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
79072805
LW
123 else
124 return 0;
125 }
126
fde52b5c 127 PERL_HASH(hash, key, klen);
79072805 128
a0d0e21e 129 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 130 for (; entry; entry = HeNEXT(entry)) {
131 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 132 continue;
fde52b5c 133 if (HeKLEN(entry) != klen)
79072805 134 continue;
36477c24 135 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 136 continue;
fde52b5c 137 return &HeVAL(entry);
79072805 138 }
a0d0e21e
LW
139#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
140 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
141 char *gotenv;
142
1e422769 143 if ((gotenv = ENV_getenv(key)) != Nullch) {
a0d0e21e 144 sv = newSVpv(gotenv,strlen(gotenv));
1e422769 145 SvTAINTED_on(sv);
e7152ba2 146 return hv_store(hv,key,klen,sv,hash);
a0d0e21e
LW
147 }
148 }
149#endif
79072805
LW
150 if (lval) { /* gonna assign to this, so it better be there */
151 sv = NEWSV(61,0);
e7152ba2 152 return hv_store(hv,key,klen,sv,hash);
79072805
LW
153 }
154 return 0;
155}
156
fde52b5c 157/* returns a HE * structure with the all fields set */
158/* note that hent_val will be a mortal sv for MAGICAL hashes */
159HE *
8ac85365 160hv_fetch_ent(HV *hv, SV *keysv, I32 lval, register U32 hash)
fde52b5c 161{
162 register XPVHV* xhv;
163 register char *key;
164 STRLEN klen;
165 register HE *entry;
166 SV *sv;
167
168 if (!hv)
169 return 0;
170
902173a3
GS
171 if (SvRMAGICAL(hv)) {
172 if (mg_find((SV*)hv,'P')) {
173 static HE mh;
ff68c719 174
902173a3
GS
175 sv = sv_newmortal();
176 keysv = sv_2mortal(newSVsv(keysv));
177 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
178 if (!HeKEY_hek(&mh)) {
179 char *k;
180 New(54, k, HEK_BASESIZE + sizeof(SV*), char);
181 HeKEY_hek(&mh) = (HEK*)k;
182 }
183 HeSVKEY_set(&mh, keysv);
184 HeVAL(&mh) = sv;
185 return &mh;
1cf368ac 186 }
902173a3
GS
187#ifdef ENV_IS_CASELESS
188 else if (mg_find((SV*)hv,'E')) {
e7152ba2 189 U32 i;
902173a3 190 key = SvPV(keysv, klen);
e7152ba2
GS
191 for (i = 0; i < klen; ++i)
192 if (isLOWER(key[i])) {
193 SV *nkeysv = sv_2mortal(newSVpv(key,klen));
194 (void)strupr(SvPVX(nkeysv));
195 entry = hv_fetch_ent(hv, nkeysv, 0, 0);
196 if (!entry && lval)
197 entry = hv_store_ent(hv, keysv, NEWSV(61,0), hash);
198 return entry;
199 }
902173a3
GS
200 }
201#endif
fde52b5c 202 }
203
effa1e2d 204 xhv = (XPVHV*)SvANY(hv);
fde52b5c 205 if (!xhv->xhv_array) {
206 if (lval
207#ifdef DYNAMIC_ENV_FETCH /* if it's an %ENV lookup, we may get it on the fly */
208 || (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME))
209#endif
210 )
211 Newz(503,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
212 else
213 return 0;
214 }
215
effa1e2d 216 key = SvPV(keysv, klen);
217
218 if (!hash)
219 PERL_HASH(hash, key, klen);
220
fde52b5c 221 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
222 for (; entry; entry = HeNEXT(entry)) {
223 if (HeHASH(entry) != hash) /* strings can't be equal */
224 continue;
225 if (HeKLEN(entry) != klen)
226 continue;
36477c24 227 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 228 continue;
229 return entry;
230 }
231#ifdef DYNAMIC_ENV_FETCH /* %ENV lookup? If so, try to fetch the value now */
232 if (HvNAME(hv) && strEQ(HvNAME(hv),ENV_HV_NAME)) {
233 char *gotenv;
234
1e422769 235 if ((gotenv = ENV_getenv(key)) != Nullch) {
fde52b5c 236 sv = newSVpv(gotenv,strlen(gotenv));
1e422769 237 SvTAINTED_on(sv);
e7152ba2 238 return hv_store_ent(hv,keysv,sv,hash);
fde52b5c 239 }
240 }
241#endif
242 if (lval) { /* gonna assign to this, so it better be there */
243 sv = NEWSV(61,0);
e7152ba2 244 return hv_store_ent(hv,keysv,sv,hash);
fde52b5c 245 }
246 return 0;
247}
248
d0066dc7 249static void
61c8b479 250hv_magic_check (HV *hv, bool *needs_copy, bool *needs_store)
d0066dc7
OT
251{
252 MAGIC *mg = SvMAGIC(hv);
253 *needs_copy = FALSE;
254 *needs_store = TRUE;
255 while (mg) {
256 if (isUPPER(mg->mg_type)) {
257 *needs_copy = TRUE;
258 switch (mg->mg_type) {
259 case 'P':
d0066dc7
OT
260 case 'S':
261 *needs_store = FALSE;
d0066dc7
OT
262 }
263 }
264 mg = mg->mg_moremagic;
265 }
266}
267
79072805 268SV**
8ac85365 269hv_store(HV *hv, char *key, U32 klen, SV *val, register U32 hash)
79072805
LW
270{
271 register XPVHV* xhv;
79072805
LW
272 register I32 i;
273 register HE *entry;
274 register HE **oentry;
79072805
LW
275
276 if (!hv)
277 return 0;
278
279 xhv = (XPVHV*)SvANY(hv);
463ee0b2 280 if (SvMAGICAL(hv)) {
d0066dc7
OT
281 bool needs_copy;
282 bool needs_store;
283 hv_magic_check (hv, &needs_copy, &needs_store);
284 if (needs_copy) {
285 mg_copy((SV*)hv, val, key, klen);
286 if (!xhv->xhv_array && !needs_store)
287 return 0;
902173a3
GS
288#ifdef ENV_IS_CASELESS
289 else if (mg_find((SV*)hv,'E')) {
290 SV *sv = sv_2mortal(newSVpv(key,klen));
291 key = strupr(SvPVX(sv));
292 hash = 0;
293 }
294#endif
d0066dc7 295 }
463ee0b2 296 }
fde52b5c 297 if (!hash)
298 PERL_HASH(hash, key, klen);
299
300 if (!xhv->xhv_array)
301 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
302
303 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
304 i = 1;
305
306 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
307 if (HeHASH(entry) != hash) /* strings can't be equal */
308 continue;
309 if (HeKLEN(entry) != klen)
310 continue;
36477c24 311 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 312 continue;
313 SvREFCNT_dec(HeVAL(entry));
314 HeVAL(entry) = val;
315 return &HeVAL(entry);
316 }
317
318 entry = new_he();
fde52b5c 319 if (HvSHAREKEYS(hv))
ff68c719 320 HeKEY_hek(entry) = share_hek(key, klen, hash);
fde52b5c 321 else /* gotta do the real thing */
ff68c719 322 HeKEY_hek(entry) = save_hek(key, klen, hash);
fde52b5c 323 HeVAL(entry) = val;
fde52b5c 324 HeNEXT(entry) = *oentry;
325 *oentry = entry;
326
327 xhv->xhv_keys++;
328 if (i) { /* initial entry? */
329 ++xhv->xhv_fill;
330 if (xhv->xhv_keys > xhv->xhv_max)
331 hsplit(hv);
79072805
LW
332 }
333
fde52b5c 334 return &HeVAL(entry);
335}
336
337HE *
8ac85365 338hv_store_ent(HV *hv, SV *keysv, SV *val, register U32 hash)
fde52b5c 339{
340 register XPVHV* xhv;
341 register char *key;
342 STRLEN klen;
343 register I32 i;
344 register HE *entry;
345 register HE **oentry;
346
347 if (!hv)
348 return 0;
349
350 xhv = (XPVHV*)SvANY(hv);
351 if (SvMAGICAL(hv)) {
aeea060c 352 dTHR;
d0066dc7
OT
353 bool needs_copy;
354 bool needs_store;
355 hv_magic_check (hv, &needs_copy, &needs_store);
356 if (needs_copy) {
357 bool save_taint = tainted;
358 if (tainting)
359 tainted = SvTAINTED(keysv);
360 keysv = sv_2mortal(newSVsv(keysv));
361 mg_copy((SV*)hv, val, (char*)keysv, HEf_SVKEY);
362 TAINT_IF(save_taint);
363 if (!xhv->xhv_array && !needs_store)
364 return Nullhe;
902173a3
GS
365#ifdef ENV_IS_CASELESS
366 else if (mg_find((SV*)hv,'E')) {
367 key = SvPV(keysv, klen);
368 keysv = sv_2mortal(newSVpv(key,klen));
369 (void)strupr(SvPVX(keysv));
370 hash = 0;
371 }
372#endif
373 }
fde52b5c 374 }
375
376 key = SvPV(keysv, klen);
902173a3 377
fde52b5c 378 if (!hash)
379 PERL_HASH(hash, key, klen);
380
79072805 381 if (!xhv->xhv_array)
463ee0b2 382 Newz(505, xhv->xhv_array, sizeof(HE**) * (xhv->xhv_max + 1), char);
79072805 383
a0d0e21e 384 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805
LW
385 i = 1;
386
fde52b5c 387 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
388 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 389 continue;
fde52b5c 390 if (HeKLEN(entry) != klen)
79072805 391 continue;
36477c24 392 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 393 continue;
fde52b5c 394 SvREFCNT_dec(HeVAL(entry));
395 HeVAL(entry) = val;
396 return entry;
79072805 397 }
79072805 398
4633a7c4 399 entry = new_he();
fde52b5c 400 if (HvSHAREKEYS(hv))
ff68c719 401 HeKEY_hek(entry) = share_hek(key, klen, hash);
fde52b5c 402 else /* gotta do the real thing */
ff68c719 403 HeKEY_hek(entry) = save_hek(key, klen, hash);
fde52b5c 404 HeVAL(entry) = val;
fde52b5c 405 HeNEXT(entry) = *oentry;
79072805
LW
406 *oentry = entry;
407
463ee0b2 408 xhv->xhv_keys++;
79072805 409 if (i) { /* initial entry? */
463ee0b2
LW
410 ++xhv->xhv_fill;
411 if (xhv->xhv_keys > xhv->xhv_max)
79072805
LW
412 hsplit(hv);
413 }
79072805 414
fde52b5c 415 return entry;
79072805
LW
416}
417
418SV *
8ac85365 419hv_delete(HV *hv, char *key, U32 klen, I32 flags)
79072805
LW
420{
421 register XPVHV* xhv;
79072805 422 register I32 i;
fde52b5c 423 register U32 hash;
79072805
LW
424 register HE *entry;
425 register HE **oentry;
67a38de0 426 SV **svp;
79072805 427 SV *sv;
79072805
LW
428
429 if (!hv)
430 return Nullsv;
8990e307 431 if (SvRMAGICAL(hv)) {
0a0bb7c7
OT
432 bool needs_copy;
433 bool needs_store;
434 hv_magic_check (hv, &needs_copy, &needs_store);
435
67a38de0
NIS
436 if (needs_copy && (svp = hv_fetch(hv, key, klen, TRUE))) {
437 sv = *svp;
0a0bb7c7
OT
438 mg_clear(sv);
439 if (!needs_store) {
440 if (mg_find(sv, 'p')) {
441 sv_unmagic(sv, 'p'); /* No longer an element */
442 return sv;
443 }
444 return Nullsv; /* element cannot be deleted */
445 }
902173a3 446#ifdef ENV_IS_CASELESS
2fd1c6b8
GS
447 else if (mg_find((SV*)hv,'E')) {
448 sv = sv_2mortal(newSVpv(key,klen));
449 key = strupr(SvPVX(sv));
450 }
902173a3 451#endif
2fd1c6b8 452 }
463ee0b2 453 }
79072805
LW
454 xhv = (XPVHV*)SvANY(hv);
455 if (!xhv->xhv_array)
456 return Nullsv;
fde52b5c 457
458 PERL_HASH(hash, key, klen);
79072805 459
a0d0e21e 460 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
79072805
LW
461 entry = *oentry;
462 i = 1;
fde52b5c 463 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
464 if (HeHASH(entry) != hash) /* strings can't be equal */
79072805 465 continue;
fde52b5c 466 if (HeKLEN(entry) != klen)
79072805 467 continue;
36477c24 468 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
79072805 469 continue;
fde52b5c 470 *oentry = HeNEXT(entry);
79072805
LW
471 if (i && !*oentry)
472 xhv->xhv_fill--;
748a9306
LW
473 if (flags & G_DISCARD)
474 sv = Nullsv;
475 else
fde52b5c 476 sv = sv_mortalcopy(HeVAL(entry));
a0d0e21e 477 if (entry == xhv->xhv_eiter)
72940dca 478 HvLAZYDEL_on(hv);
a0d0e21e 479 else
68dc0745 480 hv_free_ent(hv, entry);
fde52b5c 481 --xhv->xhv_keys;
482 return sv;
483 }
484 return Nullsv;
485}
486
487SV *
8ac85365 488hv_delete_ent(HV *hv, SV *keysv, I32 flags, U32 hash)
fde52b5c 489{
490 register XPVHV* xhv;
491 register I32 i;
492 register char *key;
493 STRLEN klen;
494 register HE *entry;
495 register HE **oentry;
496 SV *sv;
497
498 if (!hv)
499 return Nullsv;
500 if (SvRMAGICAL(hv)) {
0a0bb7c7
OT
501 bool needs_copy;
502 bool needs_store;
503 hv_magic_check (hv, &needs_copy, &needs_store);
504
67a38de0 505 if (needs_copy && (entry = hv_fetch_ent(hv, keysv, TRUE, hash))) {
0a0bb7c7
OT
506 sv = HeVAL(entry);
507 mg_clear(sv);
508 if (!needs_store) {
509 if (mg_find(sv, 'p')) {
510 sv_unmagic(sv, 'p'); /* No longer an element */
511 return sv;
512 }
513 return Nullsv; /* element cannot be deleted */
514 }
902173a3 515#ifdef ENV_IS_CASELESS
2fd1c6b8
GS
516 else if (mg_find((SV*)hv,'E')) {
517 key = SvPV(keysv, klen);
518 keysv = sv_2mortal(newSVpv(key,klen));
519 (void)strupr(SvPVX(keysv));
520 hash = 0;
521 }
902173a3 522#endif
2fd1c6b8 523 }
fde52b5c 524 }
525 xhv = (XPVHV*)SvANY(hv);
526 if (!xhv->xhv_array)
527 return Nullsv;
528
529 key = SvPV(keysv, klen);
530
531 if (!hash)
532 PERL_HASH(hash, key, klen);
533
534 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
535 entry = *oentry;
536 i = 1;
537 for (; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
538 if (HeHASH(entry) != hash) /* strings can't be equal */
539 continue;
540 if (HeKLEN(entry) != klen)
541 continue;
36477c24 542 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 543 continue;
544 *oentry = HeNEXT(entry);
545 if (i && !*oentry)
546 xhv->xhv_fill--;
547 if (flags & G_DISCARD)
548 sv = Nullsv;
549 else
550 sv = sv_mortalcopy(HeVAL(entry));
551 if (entry == xhv->xhv_eiter)
72940dca 552 HvLAZYDEL_on(hv);
fde52b5c 553 else
68dc0745 554 hv_free_ent(hv, entry);
463ee0b2 555 --xhv->xhv_keys;
79072805
LW
556 return sv;
557 }
79072805 558 return Nullsv;
79072805
LW
559}
560
a0d0e21e 561bool
8ac85365 562hv_exists(HV *hv, char *key, U32 klen)
a0d0e21e
LW
563{
564 register XPVHV* xhv;
fde52b5c 565 register U32 hash;
a0d0e21e
LW
566 register HE *entry;
567 SV *sv;
568
569 if (!hv)
570 return 0;
571
572 if (SvRMAGICAL(hv)) {
573 if (mg_find((SV*)hv,'P')) {
11343788 574 dTHR;
a0d0e21e
LW
575 sv = sv_newmortal();
576 mg_copy((SV*)hv, sv, key, klen);
577 magic_existspack(sv, mg_find(sv, 'p'));
578 return SvTRUE(sv);
579 }
902173a3
GS
580#ifdef ENV_IS_CASELESS
581 else if (mg_find((SV*)hv,'E')) {
582 sv = sv_2mortal(newSVpv(key,klen));
583 key = strupr(SvPVX(sv));
584 }
585#endif
a0d0e21e
LW
586 }
587
588 xhv = (XPVHV*)SvANY(hv);
589 if (!xhv->xhv_array)
590 return 0;
591
fde52b5c 592 PERL_HASH(hash, key, klen);
a0d0e21e
LW
593
594 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
fde52b5c 595 for (; entry; entry = HeNEXT(entry)) {
596 if (HeHASH(entry) != hash) /* strings can't be equal */
a0d0e21e 597 continue;
fde52b5c 598 if (HeKLEN(entry) != klen)
a0d0e21e 599 continue;
36477c24 600 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
fde52b5c 601 continue;
602 return TRUE;
603 }
604 return FALSE;
605}
606
607
608bool
8ac85365 609hv_exists_ent(HV *hv, SV *keysv, U32 hash)
fde52b5c 610{
611 register XPVHV* xhv;
612 register char *key;
613 STRLEN klen;
614 register HE *entry;
615 SV *sv;
616
617 if (!hv)
618 return 0;
619
620 if (SvRMAGICAL(hv)) {
621 if (mg_find((SV*)hv,'P')) {
e858de61 622 dTHR; /* just for SvTRUE */
fde52b5c 623 sv = sv_newmortal();
effa1e2d 624 keysv = sv_2mortal(newSVsv(keysv));
fde52b5c 625 mg_copy((SV*)hv, sv, (char*)keysv, HEf_SVKEY);
626 magic_existspack(sv, mg_find(sv, 'p'));
627 return SvTRUE(sv);
628 }
902173a3
GS
629#ifdef ENV_IS_CASELESS
630 else if (mg_find((SV*)hv,'E')) {
631 key = SvPV(keysv, klen);
632 keysv = sv_2mortal(newSVpv(key,klen));
633 (void)strupr(SvPVX(keysv));
634 hash = 0;
635 }
636#endif
fde52b5c 637 }
638
639 xhv = (XPVHV*)SvANY(hv);
640 if (!xhv->xhv_array)
641 return 0;
642
643 key = SvPV(keysv, klen);
644 if (!hash)
645 PERL_HASH(hash, key, klen);
646
647 entry = ((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
648 for (; entry; entry = HeNEXT(entry)) {
649 if (HeHASH(entry) != hash) /* strings can't be equal */
650 continue;
651 if (HeKLEN(entry) != klen)
652 continue;
36477c24 653 if (memNE(HeKEY(entry),key,klen)) /* is this it? */
a0d0e21e
LW
654 continue;
655 return TRUE;
656 }
657 return FALSE;
658}
659
79072805 660static void
8ac85365 661hsplit(HV *hv)
79072805
LW
662{
663 register XPVHV* xhv = (XPVHV*)SvANY(hv);
a0d0e21e 664 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
79072805
LW
665 register I32 newsize = oldsize * 2;
666 register I32 i;
667 register HE **a;
668 register HE **b;
669 register HE *entry;
670 register HE **oentry;
c07a80fd 671#ifndef STRANGE_MALLOC
4633a7c4 672 I32 tmp;
c07a80fd 673#endif
79072805 674
463ee0b2 675 a = (HE**)xhv->xhv_array;
79072805 676 nomemok = TRUE;
4633a7c4 677#ifdef STRANGE_MALLOC
79072805 678 Renew(a, newsize, HE*);
422a93e5
GA
679 if (!a) {
680 nomemok = FALSE;
681 return;
682 }
4633a7c4
LW
683#else
684 i = newsize * sizeof(HE*);
685#define MALLOC_OVERHEAD 16
686 tmp = MALLOC_OVERHEAD;
687 while (tmp - MALLOC_OVERHEAD < i)
688 tmp += tmp;
689 tmp -= MALLOC_OVERHEAD;
690 tmp /= sizeof(HE*);
691 assert(tmp >= newsize);
692 New(2,a, tmp, HE*);
422a93e5
GA
693 if (!a) {
694 nomemok = FALSE;
695 return;
696 }
4633a7c4 697 Copy(xhv->xhv_array, a, oldsize, HE*);
fba3b22e
MB
698 if (oldsize >= 64) {
699 offer_nice_chunk(xhv->xhv_array,
700 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
4633a7c4
LW
701 }
702 else
703 Safefree(xhv->xhv_array);
704#endif
705
79072805 706 nomemok = FALSE;
79072805
LW
707 Zero(&a[oldsize], oldsize, HE*); /* zero 2nd half*/
708 xhv->xhv_max = --newsize;
463ee0b2 709 xhv->xhv_array = (char*)a;
79072805
LW
710
711 for (i=0; i<oldsize; i++,a++) {
712 if (!*a) /* non-existent */
713 continue;
714 b = a+oldsize;
715 for (oentry = a, entry = *a; entry; entry = *oentry) {
fde52b5c 716 if ((HeHASH(entry) & newsize) != i) {
717 *oentry = HeNEXT(entry);
718 HeNEXT(entry) = *b;
79072805
LW
719 if (!*b)
720 xhv->xhv_fill++;
721 *b = entry;
722 continue;
723 }
724 else
fde52b5c 725 oentry = &HeNEXT(entry);
79072805
LW
726 }
727 if (!*a) /* everything moved */
728 xhv->xhv_fill--;
729 }
730}
731
72940dca 732void
8ac85365 733hv_ksplit(HV *hv, IV newmax)
72940dca 734{
735 register XPVHV* xhv = (XPVHV*)SvANY(hv);
736 I32 oldsize = (I32) xhv->xhv_max + 1; /* sic(k) */
737 register I32 newsize;
738 register I32 i;
739 register I32 j;
740 register HE **a;
741 register HE *entry;
742 register HE **oentry;
743
744 newsize = (I32) newmax; /* possible truncation here */
745 if (newsize != newmax || newmax <= oldsize)
746 return;
747 while ((newsize & (1 + ~newsize)) != newsize) {
748 newsize &= ~(newsize & (1 + ~newsize)); /* get proper power of 2 */
749 }
750 if (newsize < newmax)
751 newsize *= 2;
752 if (newsize < newmax)
753 return; /* overflow detection */
754
755 a = (HE**)xhv->xhv_array;
756 if (a) {
757 nomemok = TRUE;
758#ifdef STRANGE_MALLOC
759 Renew(a, newsize, HE*);
422a93e5
GA
760 if (!a) {
761 nomemok = FALSE;
762 return;
763 }
72940dca 764#else
765 i = newsize * sizeof(HE*);
766 j = MALLOC_OVERHEAD;
767 while (j - MALLOC_OVERHEAD < i)
768 j += j;
769 j -= MALLOC_OVERHEAD;
770 j /= sizeof(HE*);
771 assert(j >= newsize);
772 New(2, a, j, HE*);
422a93e5
GA
773 if (!a) {
774 nomemok = FALSE;
775 return;
776 }
72940dca 777 Copy(xhv->xhv_array, a, oldsize, HE*);
fba3b22e
MB
778 if (oldsize >= 64) {
779 offer_nice_chunk(xhv->xhv_array,
780 oldsize * sizeof(HE*) * 2 - MALLOC_OVERHEAD);
72940dca 781 }
782 else
783 Safefree(xhv->xhv_array);
784#endif
785 nomemok = FALSE;
786 Zero(&a[oldsize], newsize-oldsize, HE*); /* zero 2nd half*/
787 }
788 else {
789 Newz(0, a, newsize, HE*);
790 }
791 xhv->xhv_max = --newsize;
792 xhv->xhv_array = (char*)a;
793 if (!xhv->xhv_fill) /* skip rest if no entries */
794 return;
795
796 for (i=0; i<oldsize; i++,a++) {
797 if (!*a) /* non-existent */
798 continue;
799 for (oentry = a, entry = *a; entry; entry = *oentry) {
800 if ((j = (HeHASH(entry) & newsize)) != i) {
801 j -= i;
802 *oentry = HeNEXT(entry);
803 if (!(HeNEXT(entry) = a[j]))
804 xhv->xhv_fill++;
805 a[j] = entry;
806 continue;
807 }
808 else
809 oentry = &HeNEXT(entry);
810 }
811 if (!*a) /* everything moved */
812 xhv->xhv_fill--;
813 }
814}
815
79072805 816HV *
8ac85365 817newHV(void)
79072805
LW
818{
819 register HV *hv;
820 register XPVHV* xhv;
821
a0d0e21e
LW
822 hv = (HV*)NEWSV(502,0);
823 sv_upgrade((SV *)hv, SVt_PVHV);
79072805
LW
824 xhv = (XPVHV*)SvANY(hv);
825 SvPOK_off(hv);
826 SvNOK_off(hv);
fde52b5c 827#ifndef NODEFAULT_SHAREKEYS
828 HvSHAREKEYS_on(hv); /* key-sharing on by default */
829#endif
463ee0b2 830 xhv->xhv_max = 7; /* start with 8 buckets */
79072805
LW
831 xhv->xhv_fill = 0;
832 xhv->xhv_pmroot = 0;
79072805
LW
833 (void)hv_iterinit(hv); /* so each() will start off right */
834 return hv;
835}
836
837void
8ac85365 838hv_free_ent(HV *hv, register HE *entry)
79072805 839{
68dc0745 840 if (!entry)
79072805 841 return;
68dc0745 842 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
44a8e56a 843 sub_generation++; /* may be deletion of method from stash */
68dc0745 844 SvREFCNT_dec(HeVAL(entry));
845 if (HeKLEN(entry) == HEf_SVKEY) {
846 SvREFCNT_dec(HeKEY_sv(entry));
847 Safefree(HeKEY_hek(entry));
44a8e56a 848 }
849 else if (HvSHAREKEYS(hv))
68dc0745 850 unshare_hek(HeKEY_hek(entry));
fde52b5c 851 else
68dc0745 852 Safefree(HeKEY_hek(entry));
853 del_he(entry);
79072805
LW
854}
855
856void
8ac85365 857hv_delayfree_ent(HV *hv, register HE *entry)
79072805 858{
68dc0745 859 if (!entry)
79072805 860 return;
68dc0745 861 if (isGV(HeVAL(entry)) && GvCVu(HeVAL(entry)) && HvNAME(hv))
44a8e56a 862 sub_generation++; /* may be deletion of method from stash */
68dc0745 863 sv_2mortal(HeVAL(entry)); /* free between statements */
864 if (HeKLEN(entry) == HEf_SVKEY) {
865 sv_2mortal(HeKEY_sv(entry));
866 Safefree(HeKEY_hek(entry));
44a8e56a 867 }
868 else if (HvSHAREKEYS(hv))
68dc0745 869 unshare_hek(HeKEY_hek(entry));
fde52b5c 870 else
68dc0745 871 Safefree(HeKEY_hek(entry));
872 del_he(entry);
79072805
LW
873}
874
875void
8ac85365 876hv_clear(HV *hv)
79072805
LW
877{
878 register XPVHV* xhv;
879 if (!hv)
880 return;
881 xhv = (XPVHV*)SvANY(hv);
463ee0b2 882 hfreeentries(hv);
79072805 883 xhv->xhv_fill = 0;
a0d0e21e 884 xhv->xhv_keys = 0;
79072805 885 if (xhv->xhv_array)
463ee0b2 886 (void)memzero(xhv->xhv_array, (xhv->xhv_max + 1) * sizeof(HE*));
a0d0e21e
LW
887
888 if (SvRMAGICAL(hv))
889 mg_clear((SV*)hv);
79072805
LW
890}
891
892static void
8ac85365 893hfreeentries(HV *hv)
79072805 894{
a0d0e21e 895 register HE **array;
68dc0745 896 register HE *entry;
897 register HE *oentry = Null(HE*);
a0d0e21e
LW
898 I32 riter;
899 I32 max;
79072805
LW
900
901 if (!hv)
902 return;
a0d0e21e 903 if (!HvARRAY(hv))
79072805 904 return;
a0d0e21e
LW
905
906 riter = 0;
907 max = HvMAX(hv);
908 array = HvARRAY(hv);
68dc0745 909 entry = array[0];
a0d0e21e 910 for (;;) {
68dc0745 911 if (entry) {
912 oentry = entry;
913 entry = HeNEXT(entry);
914 hv_free_ent(hv, oentry);
a0d0e21e 915 }
68dc0745 916 if (!entry) {
a0d0e21e
LW
917 if (++riter > max)
918 break;
68dc0745 919 entry = array[riter];
a0d0e21e 920 }
79072805 921 }
a0d0e21e 922 (void)hv_iterinit(hv);
79072805
LW
923}
924
925void
8ac85365 926hv_undef(HV *hv)
79072805
LW
927{
928 register XPVHV* xhv;
929 if (!hv)
930 return;
931 xhv = (XPVHV*)SvANY(hv);
463ee0b2 932 hfreeentries(hv);
79072805 933 Safefree(xhv->xhv_array);
85e6fe83
LW
934 if (HvNAME(hv)) {
935 Safefree(HvNAME(hv));
936 HvNAME(hv) = 0;
937 }
79072805 938 xhv->xhv_array = 0;
aa689395 939 xhv->xhv_max = 7; /* it's a normal hash */
79072805 940 xhv->xhv_fill = 0;
a0d0e21e
LW
941 xhv->xhv_keys = 0;
942
943 if (SvRMAGICAL(hv))
944 mg_clear((SV*)hv);
79072805
LW
945}
946
79072805 947I32
8ac85365 948hv_iterinit(HV *hv)
79072805 949{
aa689395 950 register XPVHV* xhv;
951 HE *entry;
952
953 if (!hv)
954 croak("Bad hash");
955 xhv = (XPVHV*)SvANY(hv);
956 entry = xhv->xhv_eiter;
effa1e2d 957#ifdef DYNAMIC_ENV_FETCH /* set up %ENV for iteration */
aa689395 958 if (HvNAME(hv) && strEQ(HvNAME(hv), ENV_HV_NAME))
959 prime_env_iter();
effa1e2d 960#endif
72940dca 961 if (entry && HvLAZYDEL(hv)) { /* was deleted earlier? */
962 HvLAZYDEL_off(hv);
68dc0745 963 hv_free_ent(hv, entry);
72940dca 964 }
79072805
LW
965 xhv->xhv_riter = -1;
966 xhv->xhv_eiter = Null(HE*);
fb73857a 967 return xhv->xhv_fill; /* should be xhv->xhv_keys? May change later */
79072805
LW
968}
969
970HE *
8ac85365 971hv_iternext(HV *hv)
79072805
LW
972{
973 register XPVHV* xhv;
974 register HE *entry;
a0d0e21e 975 HE *oldentry;
463ee0b2 976 MAGIC* mg;
79072805
LW
977
978 if (!hv)
aa689395 979 croak("Bad hash");
79072805 980 xhv = (XPVHV*)SvANY(hv);
a0d0e21e 981 oldentry = entry = xhv->xhv_eiter;
463ee0b2 982
8990e307
LW
983 if (SvRMAGICAL(hv) && (mg = mg_find((SV*)hv,'P'))) {
984 SV *key = sv_newmortal();
cd1469e6 985 if (entry) {
fde52b5c 986 sv_setsv(key, HeSVKEY_force(entry));
cd1469e6 987 SvREFCNT_dec(HeSVKEY(entry)); /* get rid of previous key */
988 }
a0d0e21e 989 else {
ff68c719 990 char *k;
bbce6d69 991 HEK *hek;
ff68c719 992
993 xhv->xhv_eiter = entry = new_he(); /* one HE per MAGICAL hash */
4633a7c4 994 Zero(entry, 1, HE);
ff68c719 995 Newz(54, k, HEK_BASESIZE + sizeof(SV*), char);
996 hek = (HEK*)k;
997 HeKEY_hek(entry) = hek;
fde52b5c 998 HeKLEN(entry) = HEf_SVKEY;
a0d0e21e
LW
999 }
1000 magic_nextpack((SV*) hv,mg,key);
463ee0b2 1001 if (SvOK(key)) {
cd1469e6 1002 /* force key to stay around until next time */
bbce6d69 1003 HeSVKEY_set(entry, SvREFCNT_inc(key));
1004 return entry; /* beware, hent_val is not set */
463ee0b2 1005 }
fde52b5c 1006 if (HeVAL(entry))
1007 SvREFCNT_dec(HeVAL(entry));
ff68c719 1008 Safefree(HeKEY_hek(entry));
4633a7c4 1009 del_he(entry);
463ee0b2
LW
1010 xhv->xhv_eiter = Null(HE*);
1011 return Null(HE*);
79072805 1012 }
463ee0b2 1013
79072805 1014 if (!xhv->xhv_array)
4633a7c4 1015 Newz(506,xhv->xhv_array, sizeof(HE*) * (xhv->xhv_max + 1), char);
fde52b5c 1016 if (entry)
1017 entry = HeNEXT(entry);
1018 while (!entry) {
1019 ++xhv->xhv_riter;
1020 if (xhv->xhv_riter > xhv->xhv_max) {
1021 xhv->xhv_riter = -1;
1022 break;
79072805 1023 }
fde52b5c 1024 entry = ((HE**)xhv->xhv_array)[xhv->xhv_riter];
1025 }
79072805 1026
72940dca 1027 if (oldentry && HvLAZYDEL(hv)) { /* was deleted earlier? */
1028 HvLAZYDEL_off(hv);
68dc0745 1029 hv_free_ent(hv, oldentry);
72940dca 1030 }
a0d0e21e 1031
79072805
LW
1032 xhv->xhv_eiter = entry;
1033 return entry;
1034}
1035
1036char *
8ac85365 1037hv_iterkey(register HE *entry, I32 *retlen)
79072805 1038{
fde52b5c 1039 if (HeKLEN(entry) == HEf_SVKEY) {
fb73857a 1040 STRLEN len;
1041 char *p = SvPV(HeKEY_sv(entry), len);
1042 *retlen = len;
1043 return p;
fde52b5c 1044 }
1045 else {
1046 *retlen = HeKLEN(entry);
1047 return HeKEY(entry);
1048 }
1049}
1050
1051/* unlike hv_iterval(), this always returns a mortal copy of the key */
1052SV *
8ac85365 1053hv_iterkeysv(register HE *entry)
fde52b5c 1054{
1055 if (HeKLEN(entry) == HEf_SVKEY)
bbce6d69 1056 return sv_mortalcopy(HeKEY_sv(entry));
fde52b5c 1057 else
1058 return sv_2mortal(newSVpv((HeKLEN(entry) ? HeKEY(entry) : ""),
1059 HeKLEN(entry)));
79072805
LW
1060}
1061
1062SV *
8ac85365 1063hv_iterval(HV *hv, register HE *entry)
79072805 1064{
8990e307 1065 if (SvRMAGICAL(hv)) {
463ee0b2 1066 if (mg_find((SV*)hv,'P')) {
8990e307 1067 SV* sv = sv_newmortal();
bbce6d69 1068 if (HeKLEN(entry) == HEf_SVKEY)
1069 mg_copy((SV*)hv, sv, (char*)HeKEY_sv(entry), HEf_SVKEY);
1070 else mg_copy((SV*)hv, sv, HeKEY(entry), HeKLEN(entry));
463ee0b2
LW
1071 return sv;
1072 }
79072805 1073 }
fde52b5c 1074 return HeVAL(entry);
79072805
LW
1075}
1076
a0d0e21e 1077SV *
8ac85365 1078hv_iternextsv(HV *hv, char **key, I32 *retlen)
a0d0e21e
LW
1079{
1080 HE *he;
1081 if ( (he = hv_iternext(hv)) == NULL)
1082 return NULL;
1083 *key = hv_iterkey(he, retlen);
1084 return hv_iterval(hv, he);
1085}
1086
79072805 1087void
8ac85365 1088hv_magic(HV *hv, GV *gv, int how)
79072805 1089{
a0d0e21e 1090 sv_magic((SV*)hv, (SV*)gv, how, Nullch, 0);
79072805 1091}
fde52b5c 1092
bbce6d69 1093char*
8ac85365 1094sharepvn(char *sv, I32 len, U32 hash)
bbce6d69 1095{
ff68c719 1096 return HEK_KEY(share_hek(sv, len, hash));
bbce6d69 1097}
1098
1099/* possibly free a shared string if no one has access to it
fde52b5c 1100 * len and hash must both be valid for str.
1101 */
bbce6d69 1102void
8ac85365 1103unsharepvn(char *str, I32 len, U32 hash)
fde52b5c 1104{
1105 register XPVHV* xhv;
1106 register HE *entry;
1107 register HE **oentry;
1108 register I32 i = 1;
1109 I32 found = 0;
bbce6d69 1110
fde52b5c 1111 /* what follows is the moral equivalent of:
bbce6d69 1112 if ((Svp = hv_fetch(strtab, tmpsv, FALSE, hash))) {
1113 if (--*Svp == Nullsv)
1114 hv_delete(strtab, str, len, G_DISCARD, hash);
1115 } */
fde52b5c 1116 xhv = (XPVHV*)SvANY(strtab);
1117 /* assert(xhv_array != 0) */
1118 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1119 for (entry = *oentry; entry; i=0, oentry = &HeNEXT(entry), entry = *oentry) {
fde52b5c 1120 if (HeHASH(entry) != hash) /* strings can't be equal */
1121 continue;
1122 if (HeKLEN(entry) != len)
1123 continue;
36477c24 1124 if (memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1125 continue;
1126 found = 1;
bbce6d69 1127 if (--HeVAL(entry) == Nullsv) {
1128 *oentry = HeNEXT(entry);
1129 if (i && !*oentry)
1130 xhv->xhv_fill--;
ff68c719 1131 Safefree(HeKEY_hek(entry));
bbce6d69 1132 del_he(entry);
1133 --xhv->xhv_keys;
fde52b5c 1134 }
bbce6d69 1135 break;
fde52b5c 1136 }
bbce6d69 1137
1138 if (!found)
1139 warn("Attempt to free non-existent shared string");
fde52b5c 1140}
1141
bbce6d69 1142/* get a (constant) string ptr from the global string table
1143 * string will get added if it is not already there.
fde52b5c 1144 * len and hash must both be valid for str.
1145 */
bbce6d69 1146HEK *
8ac85365 1147share_hek(char *str, I32 len, register U32 hash)
fde52b5c 1148{
1149 register XPVHV* xhv;
1150 register HE *entry;
1151 register HE **oentry;
1152 register I32 i = 1;
1153 I32 found = 0;
bbce6d69 1154
fde52b5c 1155 /* what follows is the moral equivalent of:
bbce6d69 1156
1157 if (!(Svp = hv_fetch(strtab, str, len, FALSE)))
1158 hv_store(strtab, str, len, Nullsv, hash);
1159 */
fde52b5c 1160 xhv = (XPVHV*)SvANY(strtab);
1161 /* assert(xhv_array != 0) */
1162 oentry = &((HE**)xhv->xhv_array)[hash & (I32) xhv->xhv_max];
bbce6d69 1163 for (entry = *oentry; entry; i=0, entry = HeNEXT(entry)) {
fde52b5c 1164 if (HeHASH(entry) != hash) /* strings can't be equal */
1165 continue;
1166 if (HeKLEN(entry) != len)
1167 continue;
36477c24 1168 if (memNE(HeKEY(entry),str,len)) /* is this it? */
fde52b5c 1169 continue;
1170 found = 1;
fde52b5c 1171 break;
1172 }
bbce6d69 1173 if (!found) {
1174 entry = new_he();
ff68c719 1175 HeKEY_hek(entry) = save_hek(str, len, hash);
bbce6d69 1176 HeVAL(entry) = Nullsv;
1177 HeNEXT(entry) = *oentry;
1178 *oentry = entry;
1179 xhv->xhv_keys++;
1180 if (i) { /* initial entry? */
1181 ++xhv->xhv_fill;
1182 if (xhv->xhv_keys > xhv->xhv_max)
1183 hsplit(strtab);
1184 }
1185 }
1186
1187 ++HeVAL(entry); /* use value slot as REFCNT */
ff68c719 1188 return HeKEY_hek(entry);
fde52b5c 1189}
1190
bbce6d69 1191
61c8b479 1192