1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073
|
/*
* Garbage collection with weak references in CLISP
* Bruno Haible 2004-2008
*/
/* An array that contains the addresses of those objects whose mark bit is
being watched, plus room for the gc_mark queue. */
typedef struct markwatch_t {
uintP address;
gcv_object_t weakobj;
uintL weakindex;
struct markwatch_t * q_next;
} markwatch_t;
local markwatch_t* markwatchset;
/* Its allocated size. */
local uintM markwatchset_allocated;
/* Its public size. */
local uintM markwatchset_size;
/* Number of entries currently used. */
local uintL markwatchset_count;
/* Tail of the queue. */
local markwatch_t** markwatch_queue_tail;
#ifdef MULTITHREAD
/* mutex for guarding globals and O(all_weeakpointers)*/
global xmutex_t all_weakpointers_lock;
#endif
/* The markwatchset is sorted according to the address. */
#define SORTID markwatch
#define SORT_ELEMENT markwatch_t
#define SORT_KEY uintP
#define SORT_KEYOF(element) (element).address
#define SORT_COMPARE(key1,key2) \
((key1) > (key2) ? 1 : (key1) < (key2) ? -1 : 0)
#define SORT_LESS(key1,key2) ((key1) < (key2))
#include "sort.c"
#undef SORT_LESS
#undef SORT_COMPARE
#undef SORT_KEYOF
#undef SORT_KEY
#undef SORT_ELEMENT
#undef SORTID
/* Lookup the markwatchset entry of an address. Returns NULL if there is
none. */
local markwatch_t* markwatchset_lookup (uintP addr) {
/* Binary search of addr. */
if (markwatchset_count == 0)
return NULL;
var uintL i1 = 0;
var uintL i2 = markwatchset_count;
for (;;) {
var uintL i = (i1+i2)/2;
if (markwatchset[i].address == addr)
return &markwatchset[i];
else if (markwatchset[i].address < addr) {
if (i1 == i) return NULL;
/* Note here: i1 < i < i2. */
i1 = i;
} else if (markwatchset[i].address > addr) {
if (i2 == i) return NULL;
/* Note here: i1 <= i < i2. */
i2 = i;
} else
abort();
}
}
/* Add the markwatchset entries with the given address to the queue, unless
they are already enqueued. */
local void markwatch_enqueue (markwatch_t* entry) {
/* If there is no entry, i.e. the object being marked is not being watched,
there is nothing to do. */
if (entry != NULL)
/* If the object is already enqueued, nothing to do. */
if (entry->q_next == NULL) {
/* The same address can be mentioned by multiple adjacent entries;
enqueue them all. */
while (entry > &markwatchset[0] && entry[-1].address == entry[0].address)
entry--;
for (;; entry++) {
entry->q_next = *markwatch_queue_tail;
*markwatch_queue_tail = entry;
markwatch_queue_tail = &entry->q_next;
if (!(entry < &markwatchset[markwatchset_count-1]
&& entry[1].address == entry[0].address))
break;
}
}
}
/* gc_mark_with_watchset(obj) marks the object recursively. When it marks
an object, it also removes it from markwatchset.
We use this separate marking routine, thus duplicating the gc_mark code,
because updating the markwatchset is quite expensive, compared to just
setting a bit at given address, and it is only needed for those few objects
that are held in memory only through weak mappings. */
#define gc_mark gc_mark_with_watchset
#define MARK(obj) \
{ mark(obj); \
markwatch_enqueue(markwatchset_lookup((aint)(obj))); \
}
#include "spvw_gcmark.c"
#undef MARK
#undef gc_mark
/* Given an object of Rectype_Weak* type, returns the maximum number of
pointers contained in it, whose liveness needs to be watched, because some
action needs to be done depending on it. Replacing a pointer with unbound
doesn't count here, as it's done afterwards and has no consequences. */
local uintL max_watchset_count (object obj) {
if_recordp(obj, ; , abort(); );
switch (Record_type(obj)) {
case Rectype_Weakpointer:
return 0;
case Rectype_Weakmapping:
return 1;
case Rectype_WeakAnd:
case Rectype_WeakOr: {
var uintL n = Lrecord_length(obj)-2;
return n;
}
case Rectype_WeakList:
return 0;
case Rectype_WeakAndMapping:
case Rectype_WeakOrMapping: {
var uintL n = Lrecord_length(obj)-3;
return n;
}
case Rectype_WeakAlist_Key:
case Rectype_WeakAlist_Value: {
var uintL n = (Lrecord_length(obj)-2) / 2;
return n;
}
case Rectype_WeakAlist_Either:
return 0;
case Rectype_WeakAlist_Both: {
var uintL n = (Lrecord_length(obj)-2) / 2;
return 2*n;
}
case Rectype_WeakHashedAlist_Key:
case Rectype_WeakHashedAlist_Value: {
var uintL n = (Lrecord_length(obj)-4) / 3;
return n;
}
case Rectype_WeakHashedAlist_Either:
return 0;
case Rectype_WeakHashedAlist_Both: {
var uintL n = (Lrecord_length(obj)-4) / 3;
return 2*n;
}
default: abort();
}
}
/* Adds a pointer to a markwatch_t array, unless it is GC-invariant. */
local inline void add_watchable (markwatch_t** accumulatorp, object obj, uintL index,
object ptr) {
/* NB: For ptr = unbound, nothing is done, because unbound is gcinvariant. */
if (!gcinvariant_object_p(ptr))
if (!marked(ThePointer(ptr))) {
(*accumulatorp)->address = (aint)ThePointer(ptr);
(*accumulatorp)->weakobj = obj;
(*accumulatorp)->weakindex = index;
(*accumulatorp)->q_next = NULL;
(*accumulatorp)++;
}
}
/* Given an object of Rectype_Weak* type, pushes into a markwatch_t array
all the pointers in it, whose liveness needs to be watched, because some
action needs to be done depending on it. Replacing a pointer with unbound
doesn't count here, as it's done afterwards and has no consequences. */
local markwatch_t* get_watchset (object obj, markwatch_t* accumulator) {
if_recordp(obj, ; , abort(); );
switch (Record_type(obj)) {
case Rectype_Weakpointer:
break;
case Rectype_Weakmapping:
add_watchable(&accumulator,obj,0,TheWeakmapping(obj)->wm_key);
break;
case Rectype_WeakAnd: {
var uintL n = Lrecord_length(obj)-2;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,0,TheWeakAnd(obj)->war_keys[i]);
break;
}
case Rectype_WeakOr: {
var uintL n = Lrecord_length(obj)-2;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,0,TheWeakOr(obj)->wor_keys[i]);
break;
}
case Rectype_WeakList:
break;
case Rectype_WeakAndMapping: {
var uintL n = Lrecord_length(obj)-3;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,0,TheWeakAndMapping(obj)->wam_keys[i]);
break;
}
case Rectype_WeakOrMapping: {
var uintL n = Lrecord_length(obj)-3;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,0,TheWeakOrMapping(obj)->wom_keys[i]);
break;
}
case Rectype_WeakAlist_Key: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,i,TheWeakAlist(obj)->wal_data[2*i+0]);
break;
}
case Rectype_WeakAlist_Value: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,i,TheWeakAlist(obj)->wal_data[2*i+1]);
break;
}
case Rectype_WeakAlist_Either:
break;
case Rectype_WeakAlist_Both: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var uintL i;
for (i = 0; i < n; i++) {
add_watchable(&accumulator,obj,i,TheWeakAlist(obj)->wal_data[2*i+0]);
add_watchable(&accumulator,obj,i,TheWeakAlist(obj)->wal_data[2*i+1]);
}
break;
}
case Rectype_WeakHashedAlist_Key: {
var uintL n = (Lrecord_length(obj)-4) / 3;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,i,TheWeakHashedAlist(obj)->whal_data[3*i+0]);
break;
}
case Rectype_WeakHashedAlist_Value: {
var uintL n = (Lrecord_length(obj)-4) / 3;
var uintL i;
for (i = 0; i < n; i++)
add_watchable(&accumulator,obj,i,TheWeakHashedAlist(obj)->whal_data[3*i+1]);
break;
}
case Rectype_WeakHashedAlist_Either:
break;
case Rectype_WeakHashedAlist_Both: {
var uintL n = (Lrecord_length(obj)-4) / 3;
var uintL i;
for (i = 0; i < n; i++) {
add_watchable(&accumulator,obj,i,TheWeakHashedAlist(obj)->whal_data[3*i+0]);
add_watchable(&accumulator,obj,i,TheWeakHashedAlist(obj)->whal_data[3*i+1]);
}
break;
}
default: abort();
}
return accumulator;
}
/* Given an object of Rectype_Weak* type, propagates the liveness from
the keys to the values and similar.
If the index is (uintL)~(uintL)0, operate on the entire object,
otherwise only on the part with the given index. */
local void propagate_through_weak (object obj, uintL index) {
if_recordp(obj, ; , abort(); );
switch (Record_type(obj)) {
case Rectype_Weakpointer:
break;
case Rectype_Weakmapping: {
var object key = TheWeakmapping(obj)->wm_key;
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
gc_mark_with_watchset(TheWeakmapping(obj)->wm_value);
break;
}
case Rectype_WeakAnd: {
var uintL n = Lrecord_length(obj)-2;
var bool all_alive = true;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAnd(obj)->war_keys[i];
if (!(!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))) {
all_alive = false;
break;
}
}
if (all_alive)
gc_mark_with_watchset(TheWeakAnd(obj)->war_keys_list);
break;
}
case Rectype_WeakOr: {
var uintL n = Lrecord_length(obj)-2;
var bool some_alive = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakOr(obj)->wor_keys[i];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key))) {
some_alive = true;
break;
}
}
if (some_alive)
gc_mark_with_watchset(TheWeakOr(obj)->wor_keys_list);
break;
}
case Rectype_WeakList:
break;
case Rectype_WeakAndMapping: {
var uintL n = Lrecord_length(obj)-3;
var bool all_alive = true;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAndMapping(obj)->wam_keys[i];
if (!(!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))) {
all_alive = false;
break;
}
}
if (all_alive) {
gc_mark_with_watchset(TheWeakAndMapping(obj)->wam_keys_list);
gc_mark_with_watchset(TheWeakAndMapping(obj)->wam_value);
}
break;
}
case Rectype_WeakOrMapping: {
var uintL n = Lrecord_length(obj)-3;
var bool some_alive = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakOrMapping(obj)->wom_keys[i];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key))) {
some_alive = true;
break;
}
}
if (some_alive) {
gc_mark_with_watchset(TheWeakOrMapping(obj)->wom_keys_list);
gc_mark_with_watchset(TheWeakOrMapping(obj)->wom_value);
}
break;
}
case Rectype_WeakAlist_Key: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var uintL i;
if (index == (uintL)~(uintL)0)
for (i = 0; i < n; i++) {
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
gc_mark_with_watchset(TheWeakAlist(obj)->wal_data[2*i+1]);
}
else {
i = index;
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
gc_mark_with_watchset(TheWeakAlist(obj)->wal_data[2*i+1]);
}
break;
}
case Rectype_WeakAlist_Value: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var uintL i;
if (index == (uintL)~(uintL)0)
for (i = 0; i < n; i++) {
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value)))
gc_mark_with_watchset(TheWeakAlist(obj)->wal_data[2*i+0]);
}
else {
i = index;
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value)))
gc_mark_with_watchset(TheWeakAlist(obj)->wal_data[2*i+0]);
}
break;
}
case Rectype_WeakAlist_Either:
break;
case Rectype_WeakAlist_Both: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var uintL i;
if (index == (uintL)~(uintL)0)
for (i = 0; i < n; i++) {
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (!eq(key,unbound) && !eq(value,unbound)) {
var bool key_alive = (gcinvariant_object_p(key) || alive(key));
var bool value_alive = (gcinvariant_object_p(value) || alive(value));
if (key_alive && !value_alive)
gc_mark_with_watchset(value);
if (!key_alive && value_alive)
gc_mark_with_watchset(key);
}
}
else {
i = index;
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (!eq(key,unbound) && !eq(value,unbound)) {
var bool key_alive = (gcinvariant_object_p(key) || alive(key));
var bool value_alive = (gcinvariant_object_p(value) || alive(value));
if (key_alive && !value_alive)
gc_mark_with_watchset(value);
if (!key_alive && value_alive)
gc_mark_with_watchset(key);
}
}
break;
}
case Rectype_WeakHashedAlist_Key: {
var uintL n = (Lrecord_length(obj)-4) / 3;
var uintL i;
if (index == (uintL)~(uintL)0) {
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_itable);
for (i = 0; i < n; i++) {
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_data[3*i+1]);
}
} else {
i = index;
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_data[3*i+1]);
}
break;
}
case Rectype_WeakHashedAlist_Value: {
var uintL n = (Lrecord_length(obj)-4) / 3;
var uintL i;
if (index == (uintL)~(uintL)0) {
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_itable);
for (i = 0; i < n; i++) {
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value)))
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_data[3*i+0]);
}
} else {
i = index;
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value)))
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_data[3*i+0]);
}
break;
}
case Rectype_WeakHashedAlist_Either:
if (index == (uintL)~(uintL)0) {
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_itable);
}
break;
case Rectype_WeakHashedAlist_Both: {
var uintL n = (Lrecord_length(obj)-4) / 3;
var uintL i;
if (index == (uintL)~(uintL)0) {
gc_mark_with_watchset(TheWeakHashedAlist(obj)->whal_itable);
for (i = 0; i < n; i++) {
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (!eq(key,unbound) && !eq(value,unbound)) {
var bool key_alive = (gcinvariant_object_p(key) || alive(key));
var bool value_alive = (gcinvariant_object_p(value) || alive(value));
if (key_alive && !value_alive)
gc_mark_with_watchset(value);
if (!key_alive && value_alive)
gc_mark_with_watchset(key);
}
}
} else {
i = index;
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (!eq(key,unbound) && !eq(value,unbound)) {
var bool key_alive = (gcinvariant_object_p(key) || alive(key));
var bool value_alive = (gcinvariant_object_p(value) || alive(value));
if (key_alive && !value_alive)
gc_mark_with_watchset(value);
if (!key_alive && value_alive)
gc_mark_with_watchset(key);
}
}
break;
}
default: abort();
}
}
/* Straightens one pointer into a WeakHashedAlist. */
local inline object weak_hashed_alist_update_one (object kvtable, uintL n,
object x) {
while (1) {
if (!(posfixnump(x) && posfixnum_to_V(x) < n)) abort();
var gcv_object_t* KVptr =
&TheWeakHashedAlist(kvtable)->whal_data[3*posfixnum_to_V(x)];
if (!eq(KVptr[0],unbound))
/* Found an alive key/value pair at index x. */
break;
/* The key/value pair at index x is dead, continue following the chain. */
var object y = KVptr[2];
/* Move the cell at index x to the freelist. */
KVptr[2] = TheWeakHashedAlist(kvtable)->whal_freelist;
TheWeakHashedAlist(kvtable)->whal_freelist = x;
x = y;
/* Test whether the last element of the list was removed. */
if (eq(x,unbound))
break;
}
return x;
}
/* Update the list structure inside a WeakHashedAlist after one or more
(key, value) pairs have been removed.
This pass doesn't assume anything about the hash code function or about
whether the hash table's list structure is invalid and needs rehashing or
not. All it does is to straighten pointers from itable to kvtable or
inside kvtable, by removing list elements which correspond to removed
(key, value) pairs. This pass _does_ assume that the itable and the
kvtable's next fields contain indices that realize a list structure of
pairwise disjoint lists of finite length. */
local void weak_hashed_alist_update (object kvtable) {
var uintL n = (Lrecord_length(kvtable)-4) / 3;
{
var object itable = TheWeakHashedAlist(kvtable)->whal_itable;
if (!eq(itable,unbound)) {
if (!simple_vector_p(itable)) abort();
var uintL m = Svector_length(itable);
var uintL i;
for (i = 0; i < m; i++) {
var object x = TheSvector(itable)->data[i];
if (!eq(x,unbound))
TheSvector(itable)->data[i] =
weak_hashed_alist_update_one(kvtable,n,x);
}
}
}
{
var uintL i;
for (i = 0; i < n; i++) {
var object x = TheWeakHashedAlist(kvtable)->whal_data[3*i+2];
if (!eq(x,unbound))
TheWeakHashedAlist(kvtable)->whal_data[3*i+2] =
weak_hashed_alist_update_one(kvtable,n,x);
}
}
}
/* Given an object of Rectype_Weak* type, clean the dead parts.
Return a flag indicating whether the object should still be kept active. */
local bool weak_clean_dead (object obj) {
if_recordp(obj, ; , abort(); );
switch (Record_type(obj)) {
case Rectype_Weakpointer: {
var object value = TheWeakpointer(obj)->wp_value;
if (gcinvariant_object_p(value))
return false;
if (alive(value))
return true;
else {
TheWeakpointer(obj)->wp_value = unbound;
return false;
}
}
case Rectype_Weakmapping: {
var object key = TheWeakmapping(obj)->wm_key;
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
return true;
else {
TheWeakmapping(obj)->wm_key = unbound;
TheWeakmapping(obj)->wm_value = unbound;
return false;
}
}
case Rectype_WeakAnd: {
var uintL n = Lrecord_length(obj)-2;
var bool all_alive = true;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAnd(obj)->war_keys[i];
if (!(!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))) {
all_alive = false;
break;
}
}
if (all_alive)
return true;
else {
for (i = 0; i < n; i++)
TheWeakAnd(obj)->war_keys[i] = unbound;
TheWeakAnd(obj)->war_keys_list = unbound;
return false;
}
}
case Rectype_WeakOr: {
var uintL n = Lrecord_length(obj)-2;
var bool some_alive = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakOr(obj)->wor_keys[i];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key))) {
some_alive = true;
break;
}
}
if (some_alive)
return true;
else {
for (i = 0; i < n; i++)
TheWeakOr(obj)->wor_keys[i] = unbound;
TheWeakOr(obj)->wor_keys_list = unbound;
return false;
}
}
case Rectype_WeakList: {
var uintL n = Lrecord_length(obj)-2;
var bool keep = false;
var uintL i;
for (i = 0; i < n; i++) {
var object value = TheWeakList(obj)->wl_elements[i];
if (gcinvariant_object_p(value))
;
else if (alive(value))
keep = true;
else {
TheWeakList(obj)->wl_elements[i] = unbound;
TheWeakList(obj)->wl_count = fixnum_inc(TheWeakList(obj)->wl_count,-1);
}
}
return keep;
}
case Rectype_WeakAndMapping: {
var uintL n = Lrecord_length(obj)-3;
var bool all_alive = true;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAndMapping(obj)->wam_keys[i];
if (!(!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))) {
all_alive = false;
break;
}
}
if (all_alive)
return true;
else {
for (i = 0; i < n; i++)
TheWeakAndMapping(obj)->wam_keys[i] = unbound;
TheWeakAndMapping(obj)->wam_keys_list = unbound;
TheWeakAndMapping(obj)->wam_value = unbound;
return false;
}
}
case Rectype_WeakOrMapping: {
var uintL n = Lrecord_length(obj)-3;
var bool some_alive = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakOrMapping(obj)->wom_keys[i];
if (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key))) {
some_alive = true;
break;
}
}
if (some_alive)
return true;
else {
for (i = 0; i < n; i++)
TheWeakOrMapping(obj)->wom_keys[i] = unbound;
TheWeakOrMapping(obj)->wom_keys_list = unbound;
TheWeakOrMapping(obj)->wom_value = unbound;
return false;
}
}
case Rectype_WeakAlist_Key: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var bool keep = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
if (eq(key,unbound))
;
else if (gcinvariant_object_p(key) || alive(key))
keep = true;
else {
TheWeakAlist(obj)->wal_data[2*i+0] = unbound;
TheWeakAlist(obj)->wal_data[2*i+1] = unbound;
TheWeakAlist(obj)->wal_count = fixnum_inc(TheWeakAlist(obj)->wal_count,-1);
}
}
return keep;
}
case Rectype_WeakAlist_Value: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var bool keep = false;
var uintL i;
for (i = 0; i < n; i++) {
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (eq(value,unbound))
;
else if (gcinvariant_object_p(value) || alive(value))
keep = true;
else {
TheWeakAlist(obj)->wal_data[2*i+0] = unbound;
TheWeakAlist(obj)->wal_data[2*i+1] = unbound;
TheWeakAlist(obj)->wal_count = fixnum_inc(TheWeakAlist(obj)->wal_count,-1);
}
}
return keep;
}
case Rectype_WeakAlist_Either: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var bool keep = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (eq(key,unbound) && eq(value,unbound))
;
else if ((!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
&& (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value))))
/* both alive */
keep = true;
else {
TheWeakAlist(obj)->wal_data[2*i+0] = unbound;
TheWeakAlist(obj)->wal_data[2*i+1] = unbound;
TheWeakAlist(obj)->wal_count = fixnum_inc(TheWeakAlist(obj)->wal_count,-1);
}
}
return keep;
}
case Rectype_WeakAlist_Both: {
var uintL n = (Lrecord_length(obj)-2) / 2;
var bool keep = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakAlist(obj)->wal_data[2*i+0];
var object value = TheWeakAlist(obj)->wal_data[2*i+1];
if (eq(key,unbound) && eq(value,unbound))
;
else {
bool key_alive = (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)));
bool value_alive = (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value)));
if (key_alive || value_alive) {
/* key or value alive; the other one must be kept alive too. */
if (!(key_alive && value_alive)) abort();
keep = true;
} else {
TheWeakAlist(obj)->wal_data[2*i+0] = unbound;
TheWeakAlist(obj)->wal_data[2*i+1] = unbound;
TheWeakAlist(obj)->wal_count = fixnum_inc(TheWeakAlist(obj)->wal_count,-1);
}
}
}
return keep;
}
case Rectype_WeakHashedAlist_Key: {
var uintL n = (Lrecord_length(obj)-4) / 3;
{
var object itable = TheWeakHashedAlist(obj)->whal_itable;
if (!eq(itable,unbound) && !alive(itable))
abort();
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_freelist))
abort();
var bool any_died = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
if (eq(key,unbound))
;
else if (gcinvariant_object_p(key) || alive(key))
;
else {
TheWeakHashedAlist(obj)->whal_data[3*i+0] = unbound;
TheWeakHashedAlist(obj)->whal_data[3*i+1] = unbound;
TheWeakHashedAlist(obj)->whal_count = fixnum_inc(TheWeakHashedAlist(obj)->whal_count,-1);
any_died = true;
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_data[3*i+2]))
abort();
}
if (any_died)
weak_hashed_alist_update(obj);
/* Must keep obj active, even if its whal_count is 0: the whal_itable is
not seen by spvw_gcmark (due to Lrecord_nonweak_length(obj) = 0) and
must nevertheless be updated during the next GCs. */
return true;
}
case Rectype_WeakHashedAlist_Value: {
var uintL n = (Lrecord_length(obj)-4) / 3;
{
var object itable = TheWeakHashedAlist(obj)->whal_itable;
if (!eq(itable,unbound) && !alive(itable))
abort();
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_freelist))
abort();
var bool any_died = false;
var uintL i;
for (i = 0; i < n; i++) {
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (eq(value,unbound))
;
else if (gcinvariant_object_p(value) || alive(value))
;
else {
TheWeakHashedAlist(obj)->whal_data[3*i+0] = unbound;
TheWeakHashedAlist(obj)->whal_data[3*i+1] = unbound;
TheWeakHashedAlist(obj)->whal_count = fixnum_inc(TheWeakHashedAlist(obj)->whal_count,-1);
any_died = true;
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_data[3*i+2]))
abort();
}
if (any_died)
weak_hashed_alist_update(obj);
/* Must keep obj active, even if its whal_count is 0: the whal_itable is
not seen by spvw_gcmark (due to Lrecord_nonweak_length(obj) = 0) and
must nevertheless be updated during the next GCs. */
return true;
}
case Rectype_WeakHashedAlist_Either: {
var uintL n = (Lrecord_length(obj)-4) / 3;
{
var object itable = TheWeakHashedAlist(obj)->whal_itable;
if (!eq(itable,unbound) && !alive(itable))
abort();
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_freelist))
abort();
var bool any_died = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (eq(key,unbound) && eq(value,unbound))
;
else if ((!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)))
&& (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value))))
/* both alive */
;
else {
TheWeakHashedAlist(obj)->whal_data[3*i+0] = unbound;
TheWeakHashedAlist(obj)->whal_data[3*i+1] = unbound;
TheWeakHashedAlist(obj)->whal_count = fixnum_inc(TheWeakHashedAlist(obj)->whal_count,-1);
any_died = true;
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_data[3*i+2]))
abort();
}
if (any_died)
weak_hashed_alist_update(obj);
/* Must keep obj active, even if its whal_count is 0: the whal_itable is
not seen by spvw_gcmark (due to Lrecord_nonweak_length(obj) = 0) and
must nevertheless be updated during the next GCs. */
return true;
}
case Rectype_WeakHashedAlist_Both: {
var uintL n = (Lrecord_length(obj)-4) / 3;
{
var object itable = TheWeakHashedAlist(obj)->whal_itable;
if (!eq(itable,unbound) && !alive(itable))
abort();
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_freelist))
abort();
var bool any_died = false;
var uintL i;
for (i = 0; i < n; i++) {
var object key = TheWeakHashedAlist(obj)->whal_data[3*i+0];
var object value = TheWeakHashedAlist(obj)->whal_data[3*i+1];
if (eq(key,unbound) && eq(value,unbound))
;
else {
bool key_alive = (!eq(key,unbound) && (gcinvariant_object_p(key) || alive(key)));
bool value_alive = (!eq(value,unbound) && (gcinvariant_object_p(value) || alive(value)));
if (key_alive || value_alive) {
/* key or value alive; the other one must be kept alive too. */
if (!(key_alive && value_alive)) abort();
} else {
TheWeakHashedAlist(obj)->whal_data[3*i+0] = unbound;
TheWeakHashedAlist(obj)->whal_data[3*i+1] = unbound;
TheWeakHashedAlist(obj)->whal_count = fixnum_inc(TheWeakHashedAlist(obj)->whal_count,-1);
any_died = true;
}
}
if (!gcinvariant_object_p(TheWeakHashedAlist(obj)->whal_data[3*i+2]))
abort();
}
if (any_died)
weak_hashed_alist_update(obj);
/* Must keep obj active, even if its whal_count is 0: the whal_itable is
not seen by spvw_gcmark (due to Lrecord_nonweak_length(obj) = 0) and
must nevertheless be updated during the next GCs. */
return true;
}
default: abort();
}
}
/* Handle the weak pointers at the end of the mark phase. */
local void gc_mark_weakpointers (object all_weakpointers) {
/* 1. Build up the markwatchset. */
{ /* Gather all watchable objects. */
var markwatch_t* accumulator = &markwatchset[0];
var object L;
for (L = all_weakpointers;
!eq(L,Fixnum_0);
L = ((Weakpointer)TheRecord(L))->wp_cdr) {
var object obj = L;
/* Watch the mark bit of the weak pointer itself. */
add_watchable(&accumulator,obj,(uintL)~(uintL)0,obj);
/* Watch the mark bits of the weak references that it contains. */
accumulator = get_watchset(obj,accumulator);
}
markwatchset_count = accumulator - &markwatchset[0];
}
{ /* Sort the markwatchset. */
SORT(markwatch,sort)(markwatchset,markwatchset_count);
}
/* 2. Initialize the queue to empty. */
var markwatch_t markwatch_queue_end;
var markwatch_t* markwatch_queue_head = &markwatch_queue_end;
markwatch_queue_tail = &markwatch_queue_head;
/* 3. Propagate through the initially marked weak objects.
(This is equivalent to initializing the queue with the initially
marked weak objects. It is because of this step that add_watchable
can ignore objects that are initially already marked.) */
{
var object L;
for (L = all_weakpointers;
!eq(L,Fixnum_0);
L = ((Weakpointer)TheRecord(L))->wp_cdr) {
var object obj = L;
if (alive(obj))
propagate_through_weak(obj,(uintL)~(uintL)0);
}
}
/* 4. Execute the queued propagations. Each propagation can enqueue new
requests, but since the markwatchset is finite, the queue eventually
stops growing. */
{
var markwatch_t* queue_rest;
for (queue_rest = markwatch_queue_head;
queue_rest != &markwatch_queue_end;
queue_rest = queue_rest->q_next) {
var object obj = queue_rest->weakobj;
if (alive(obj))
propagate_through_weak(obj,queue_rest->weakindex);
}
}
}
/* Clean the dead parts of all_weakpointers and store the rest in
O(all_weakpointers). */
local void clean_weakpointers (object all_weakpointers) {
/* 5. Clean the dead parts. */
var object Lu = all_weakpointers;
var gcv_object_t* L1 = &O(all_weakpointers);
while (!eq(Lu,Fixnum_0)) {
if (!alive(Lu)) {
/* The weak-pointer itself is dead. Remove it from the list. */
markwatchset_size -= 1 + max_watchset_count(Lu);
Lu = ((Weakpointer)TheRecord(Lu))->wp_cdr;
} else {
/* Clean dead parts inside the weak-pointer. */
if (weak_clean_dead(Lu)) {
/* Keep it in the list. */
*L1 = Lu; L1 = &((Weakpointer)TheRecord(Lu))->wp_cdr; Lu = *L1;
} else {
var object next = ((Weakpointer)TheRecord(Lu))->wp_cdr;
((Weakpointer)TheRecord(Lu))->wp_cdr = unbound;
Lu = next;
}
}
}
*L1 = Fixnum_0;
}
/* --------------------- Code executed outside the GC --------------------- */
/* We keep all WEAK-POINTER objects on the O(all_weakpointers) list unless the
value that the WEAK-POINTER points to is GC-invariant. This requires that
we add the WEAK-POINTER to O(all_weakpointers) when the value is changed
to a non-GC-invariant one, and GC removes the WEAK-POINTERs with GC-invariant
values from O(all_weakpointers).
The alternative is to keep all the WEAK-POINTERs on the list.
We do not do that because we assume that the lifetime of a WEAK-POINTER is
relatively high compared to GC timeout, so there will be several GCs while
the given WEAK-POINTER is alive (why would one use a WEAK-POINTER otherwise?)
and therefore it is worth the effort to keep O(all_weakpointers) as short
as possible. */
/* Given a fresh object of Rectype_Weak* type, tells whether it should be
added to the O(all_weakpointers). */
local bool weak_must_activate (object obj) {
if_recordp(obj, ; , abort(); );
switch (Record_type(obj)) {
case Rectype_Weakpointer: {
var object value = TheWeakpointer(obj)->wp_value;
return (!gcinvariant_object_p(value));
}
case Rectype_WeakList: {
var uintL n = Lrecord_length(obj)-2;
var uintL i;
for (i = 0; i < n; i++) {
var object value = TheWeakList(obj)->wl_elements[i];
if (!gcinvariant_object_p(value))
return true;
}
return false;
}
case Rectype_Weakmapping:
case Rectype_WeakAnd:
case Rectype_WeakOr:
case Rectype_WeakAndMapping:
case Rectype_WeakOrMapping:
case Rectype_WeakAlist_Key:
case Rectype_WeakAlist_Value:
case Rectype_WeakAlist_Either:
case Rectype_WeakAlist_Both:
case Rectype_WeakHashedAlist_Key:
case Rectype_WeakHashedAlist_Value:
case Rectype_WeakHashedAlist_Either:
case Rectype_WeakHashedAlist_Both:
return true;
default: abort();
}
}
/* Adds a freshly allocated object to the list of weak pointers.
activate_weak(obj);
> obj: A fresh but filled object of type Rectype_Weak* */
global void activate_weak (object obj) {
if (weak_must_activate(obj)) {
#ifdef MULTITHREAD
/* this is the only place where all_weakpointers_lock is used.
we do not allow GC here (no GC safe region) !*/
begin_system_call();
xmutex_lock(&all_weakpointers_lock);
end_system_call();
#endif
/* Ensure that markwatchset has enough room for the next GC. */
var uintL need = 1 + max_watchset_count(obj);
var uintM new_markwatchset_size = markwatchset_size + need;
if (new_markwatchset_size > markwatchset_allocated) {
/* Increment markwatchset_allocated by a proportional factor 1.5. */
var uintM new_markwatchset_allocated = markwatchset_allocated + markwatchset_allocated/2;
if (new_markwatchset_allocated < new_markwatchset_size)
new_markwatchset_allocated = new_markwatchset_size;
var markwatch_t* new_markwatchset = (markwatch_t*)clisp_malloc(new_markwatchset_allocated*sizeof(markwatch_t));
/* Now that malloc() succeeded, we can free the old markwatchset and
update the variables. */
var markwatch_t* old_markwatchset = markwatchset;
markwatchset = new_markwatchset;
markwatchset_allocated = new_markwatchset_allocated;
if (old_markwatchset != NULL)
free(old_markwatchset);
}
markwatchset_size = new_markwatchset_size;
/* Now that markwatchset is large enough, add obj to O(all_weakpointers). */
((Weakpointer)TheRecord(obj))->wp_cdr = O(all_weakpointers);
O(all_weakpointers) = obj;
#ifdef MULTITHREAD
begin_system_call();
xmutex_unlock(&all_weakpointers_lock);
end_system_call();
#endif
}
}
|