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