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