2 * GENerational Conservative Garbage Collector for SBCL x86
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 * For a review of garbage collection techniques (e.g. generational
18 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20 * had been accepted for _ACM Computing Surveys_ and was available
21 * as a PostScript preprint through
22 * <http://www.cs.utexas.edu/users/oops/papers.html>
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
28 * FIXME: GC :FULL T seems to be unable to recover a lot of unused
29 * space. After cold init is complete, GC :FULL T gets us down to
30 * about 44 Mb total used, but PURIFY gets us down to about 17 Mb
41 #include "interrupt.h"
48 /* a function defined externally in assembly language, called from
50 void do_pending_interrupt(void);
56 /* the number of actual generations. (The number of 'struct
57 * generation' objects is one more than this, because one serves as
58 * scratch when GC'ing.) */
59 #define NUM_GENERATIONS 6
61 /* Should we use page protection to help avoid the scavenging of pages
62 * that don't have pointers to younger generations? */
63 boolean enable_page_protection = 1;
65 /* Should we unmap a page and re-mmap it to have it zero filled? */
66 #if defined(__FreeBSD__) || defined(__OpenBSD__)
67 /* comment from cmucl-2.4.8: This can waste a lot of swap on FreeBSD
68 * so don't unmap there.
70 * The CMU CL comment didn't specify a version, but was probably an
71 * old version of FreeBSD (pre-4.0), so this might no longer be true.
72 * OTOH, if it is true, this behavior might exist on OpenBSD too, so
73 * for now we don't unmap there either. -- WHN 2001-04-07 */
74 boolean gencgc_unmap_zero = 0;
76 boolean gencgc_unmap_zero = 1;
79 /* the minimum size (in bytes) for a large object*/
80 unsigned large_object_size = 4 * 4096;
82 /* Should we filter stack/register pointers? This could reduce the
83 * number of invalid pointers accepted. KLUDGE: It will probably
84 * degrades interrupt safety during object initialization. */
85 boolean enable_pointer_filter = 1;
91 #define gc_abort() lose("GC invariant lost, file \"%s\", line %d", \
94 /* FIXME: In CMU CL, this was "#if 0" with no explanation. Find out
95 * how much it costs to make it "#if 1". If it's not too expensive,
98 #define gc_assert(ex) do { \
99 if (!(ex)) gc_abort(); \
102 #define gc_assert(ex)
105 /* the verbosity level. All non-error messages are disabled at level 0;
106 * and only a few rare messages are printed at level 1. */
107 unsigned gencgc_verbose = (QSHOW ? 1 : 0);
109 /* FIXME: At some point enable the various error-checking things below
110 * and see what they say. */
112 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
113 * Set verify_gens to NUM_GENERATIONS to disable this kind of check. */
114 int verify_gens = NUM_GENERATIONS;
116 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
117 boolean pre_verify_gen_0 = 0;
119 /* Should we check for bad pointers after gc_free_heap is called
120 * from Lisp PURIFY? */
121 boolean verify_after_free_heap = 0;
123 /* Should we print a note when code objects are found in the dynamic space
124 * during a heap verify? */
125 boolean verify_dynamic_code_check = 0;
127 /* Should we check code objects for fixup errors after they are transported? */
128 boolean check_code_fixups = 0;
130 /* Should we check that newly allocated regions are zero filled? */
131 boolean gencgc_zero_check = 0;
133 /* Should we check that the free space is zero filled? */
134 boolean gencgc_enable_verify_zero_fill = 0;
136 /* Should we check that free pages are zero filled during gc_free_heap
137 * called after Lisp PURIFY? */
138 boolean gencgc_zero_check_during_free_heap = 0;
141 * GC structures and variables
144 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
145 unsigned long bytes_allocated = 0;
146 static unsigned long auto_gc_trigger = 0;
148 /* the source and destination generations. These are set before a GC starts
150 static int from_space;
151 static int new_space;
153 /* FIXME: It would be nice to use this symbolic constant instead of
154 * bare 4096 almost everywhere. We could also use an assertion that
155 * it's equal to getpagesize(). */
156 #define PAGE_BYTES 4096
158 /* An array of page structures is statically allocated.
159 * This helps quickly map between an address its page structure.
160 * NUM_PAGES is set from the size of the dynamic space. */
161 struct page page_table[NUM_PAGES];
163 /* To map addresses to page structures the address of the first page
165 static void *heap_base = NULL;
167 /* Calculate the start address for the given page number. */
169 *page_address(int page_num)
171 return (heap_base + (page_num * 4096));
174 /* Find the page index within the page_table for the given
175 * address. Return -1 on failure. */
177 find_page_index(void *addr)
179 int index = addr-heap_base;
182 index = ((unsigned int)index)/4096;
183 if (index < NUM_PAGES)
190 /* a structure to hold the state of a generation */
193 /* the first page that gc_alloc checks on its next call */
194 int alloc_start_page;
196 /* the first page that gc_alloc_unboxed checks on its next call */
197 int alloc_unboxed_start_page;
199 /* the first page that gc_alloc_large (boxed) considers on its next
200 * call. (Although it always allocates after the boxed_region.) */
201 int alloc_large_start_page;
203 /* the first page that gc_alloc_large (unboxed) considers on its
204 * next call. (Although it always allocates after the
205 * current_unboxed_region.) */
206 int alloc_large_unboxed_start_page;
208 /* the bytes allocated to this generation */
211 /* the number of bytes at which to trigger a GC */
214 /* to calculate a new level for gc_trigger */
215 int bytes_consed_between_gc;
217 /* the number of GCs since the last raise */
220 /* the average age after which a GC will raise objects to the
224 /* the cumulative sum of the bytes allocated to this generation. It is
225 * cleared after a GC on this generations, and update before new
226 * objects are added from a GC of a younger generation. Dividing by
227 * the bytes_allocated will give the average age of the memory in
228 * this generation since its last GC. */
229 int cum_sum_bytes_allocated;
231 /* a minimum average memory age before a GC will occur helps
232 * prevent a GC when a large number of new live objects have been
233 * added, in which case a GC could be a waste of time */
234 double min_av_mem_age;
237 /* an array of generation structures. There needs to be one more
238 * generation structure than actual generations as the oldest
239 * generation is temporarily raised then lowered. */
240 static struct generation generations[NUM_GENERATIONS+1];
242 /* the oldest generation that is will currently be GCed by default.
243 * Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
245 * The default of (NUM_GENERATIONS-1) enables GC on all generations.
247 * Setting this to 0 effectively disables the generational nature of
248 * the GC. In some applications generational GC may not be useful
249 * because there are no long-lived objects.
251 * An intermediate value could be handy after moving long-lived data
252 * into an older generation so an unnecessary GC of this long-lived
253 * data can be avoided. */
254 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
256 /* The maximum free page in the heap is maintained and used to update
257 * ALLOCATION_POINTER which is used by the room function to limit its
258 * search of the heap. XX Gencgc obviously needs to be better
259 * integrated with the Lisp code. */
260 static int last_free_page;
261 static int last_used_page = 0;
264 * miscellaneous heap functions
267 /* Count the number of pages which are write-protected within the
268 * given generation. */
270 count_write_protect_generation_pages(int generation)
275 for (i = 0; i < last_free_page; i++)
276 if ((page_table[i].allocated != FREE_PAGE)
277 && (page_table[i].gen == generation)
278 && (page_table[i].write_protected == 1))
283 /* Count the number of pages within the given generation */
285 count_generation_pages(int generation)
290 for (i = 0; i < last_free_page; i++)
291 if ((page_table[i].allocated != 0)
292 && (page_table[i].gen == generation))
297 /* Count the number of dont_move pages. */
299 count_dont_move_pages(void)
304 for (i = 0; i < last_free_page; i++)
305 if ((page_table[i].allocated != 0)
306 && (page_table[i].dont_move != 0))
311 /* Work through the pages and add up the number of bytes used for the
312 * given generation. */
314 generation_bytes_allocated (int gen)
319 for (i = 0; i < last_free_page; i++) {
320 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
321 result += page_table[i].bytes_used;
326 /* Return the average age of the memory in a generation. */
328 gen_av_mem_age(int gen)
330 if (generations[gen].bytes_allocated == 0)
334 ((double)generations[gen].cum_sum_bytes_allocated)
335 / ((double)generations[gen].bytes_allocated);
338 /* The verbose argument controls how much to print: 0 for normal
339 * level of detail; 1 for debugging. */
341 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
346 /* This code uses the FP instructions which may be set up for Lisp
347 * so they need to be saved and reset for C. */
350 /* number of generations to print */
352 gens = NUM_GENERATIONS+1;
354 gens = NUM_GENERATIONS;
356 /* Print the heap stats. */
358 " Generation Boxed Unboxed LB LUB Alloc Waste Trig WP GCs Mem-age\n");
360 for (i = 0; i < gens; i++) {
364 int large_boxed_cnt = 0;
365 int large_unboxed_cnt = 0;
367 for (j = 0; j < last_free_page; j++)
368 if (page_table[j].gen == i) {
370 /* Count the number of boxed pages within the given
372 if (page_table[j].allocated == BOXED_PAGE) {
373 if (page_table[j].large_object)
379 /* Count the number of unboxed pages within the given
381 if (page_table[j].allocated == UNBOXED_PAGE) {
382 if (page_table[j].large_object)
389 gc_assert(generations[i].bytes_allocated
390 == generation_bytes_allocated(i));
392 " %8d: %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4f\n",
394 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
395 generations[i].bytes_allocated,
396 (count_generation_pages(i)*4096
397 - generations[i].bytes_allocated),
398 generations[i].gc_trigger,
399 count_write_protect_generation_pages(i),
400 generations[i].num_gc,
403 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
405 fpu_restore(fpu_state);
409 * allocation routines
413 * To support quick and inline allocation, regions of memory can be
414 * allocated and then allocated from with just a free pointer and a
415 * check against an end address.
417 * Since objects can be allocated to spaces with different properties
418 * e.g. boxed/unboxed, generation, ages; there may need to be many
419 * allocation regions.
421 * Each allocation region may be start within a partly used page. Many
422 * features of memory use are noted on a page wise basis, e.g. the
423 * generation; so if a region starts within an existing allocated page
424 * it must be consistent with this page.
426 * During the scavenging of the newspace, objects will be transported
427 * into an allocation region, and pointers updated to point to this
428 * allocation region. It is possible that these pointers will be
429 * scavenged again before the allocation region is closed, e.g. due to
430 * trans_list which jumps all over the place to cleanup the list. It
431 * is important to be able to determine properties of all objects
432 * pointed to when scavenging, e.g to detect pointers to the oldspace.
433 * Thus it's important that the allocation regions have the correct
434 * properties set when allocated, and not just set when closed. The
435 * region allocation routines return regions with the specified
436 * properties, and grab all the pages, setting their properties
437 * appropriately, except that the amount used is not known.
439 * These regions are used to support quicker allocation using just a
440 * free pointer. The actual space used by the region is not reflected
441 * in the pages tables until it is closed. It can't be scavenged until
444 * When finished with the region it should be closed, which will
445 * update the page tables for the actual space used returning unused
446 * space. Further it may be noted in the new regions which is
447 * necessary when scavenging the newspace.
449 * Large objects may be allocated directly without an allocation
450 * region, the page tables are updated immediately.
452 * Unboxed objects don't contain pointers to other objects and so
453 * don't need scavenging. Further they can't contain pointers to
454 * younger generations so WP is not needed. By allocating pages to
455 * unboxed objects the whole page never needs scavenging or
456 * write-protecting. */
458 /* We are only using two regions at present. Both are for the current
459 * newspace generation. */
460 struct alloc_region boxed_region;
461 struct alloc_region unboxed_region;
463 /* XX hack. Current Lisp code uses the following. Need copying in/out. */
464 void *current_region_free_pointer;
465 void *current_region_end_addr;
467 /* The generation currently being allocated to. */
468 static int gc_alloc_generation;
470 /* Find a new region with room for at least the given number of bytes.
472 * It starts looking at the current generation's alloc_start_page. So
473 * may pick up from the previous region if there is enough space. This
474 * keeps the allocation contiguous when scavenging the newspace.
476 * The alloc_region should have been closed by a call to
477 * gc_alloc_update_page_tables, and will thus be in an empty state.
479 * To assist the scavenging functions write-protected pages are not
480 * used. Free pages should not be write-protected.
482 * It is critical to the conservative GC that the start of regions be
483 * known. To help achieve this only small regions are allocated at a
486 * During scavenging, pointers may be found to within the current
487 * region and the page generation must be set so that pointers to the
488 * from space can be recognized. Therefore the generation of pages in
489 * the region are set to gc_alloc_generation. To prevent another
490 * allocation call using the same pages, all the pages in the region
491 * are allocated, although they will initially be empty.
494 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
506 "/alloc_new_region for %d bytes from gen %d\n",
507 nbytes, gc_alloc_generation));
510 /* Check that the region is in a reset state. */
511 gc_assert((alloc_region->first_page == 0)
512 && (alloc_region->last_page == -1)
513 && (alloc_region->free_pointer == alloc_region->end_addr));
517 generations[gc_alloc_generation].alloc_unboxed_start_page;
520 generations[gc_alloc_generation].alloc_start_page;
523 /* Search for a contiguous free region of at least nbytes with the
524 * given properties: boxed/unboxed, generation. */
526 first_page = restart_page;
528 /* First search for a page with at least 32 bytes free, which is
529 * not write-protected, and which is not marked dont_move. */
530 while ((first_page < NUM_PAGES)
531 && (page_table[first_page].allocated != FREE_PAGE) /* not free page */
533 (page_table[first_page].allocated != UNBOXED_PAGE))
535 (page_table[first_page].allocated != BOXED_PAGE))
536 || (page_table[first_page].large_object != 0)
537 || (page_table[first_page].gen != gc_alloc_generation)
538 || (page_table[first_page].bytes_used >= (4096-32))
539 || (page_table[first_page].write_protected != 0)
540 || (page_table[first_page].dont_move != 0)))
542 /* Check for a failure. */
543 if (first_page >= NUM_PAGES) {
545 "Argh! gc_alloc_new_region failed on first_page, nbytes=%d.\n",
547 print_generation_stats(1);
551 gc_assert(page_table[first_page].write_protected == 0);
555 "/first_page=%d bytes_used=%d\n",
556 first_page, page_table[first_page].bytes_used));
559 /* Now search forward to calculate the available region size. It
560 * tries to keeps going until nbytes are found and the number of
561 * pages is greater than some level. This helps keep down the
562 * number of pages in a region. */
563 last_page = first_page;
564 bytes_found = 4096 - page_table[first_page].bytes_used;
566 while (((bytes_found < nbytes) || (num_pages < 2))
567 && (last_page < (NUM_PAGES-1))
568 && (page_table[last_page+1].allocated == FREE_PAGE)) {
572 gc_assert(page_table[last_page].write_protected == 0);
575 region_size = (4096 - page_table[first_page].bytes_used)
576 + 4096*(last_page-first_page);
578 gc_assert(bytes_found == region_size);
582 "/last_page=%d bytes_found=%d num_pages=%d\n",
583 last_page, bytes_found, num_pages));
586 restart_page = last_page + 1;
587 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
589 /* Check for a failure. */
590 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
592 "Argh! gc_alloc_new_region failed on restart_page, nbytes=%d.\n",
594 print_generation_stats(1);
600 "/gc_alloc_new_region gen %d: %d bytes: pages %d to %d: addr=%x\n",
605 page_address(first_page)));
608 /* Set up the alloc_region. */
609 alloc_region->first_page = first_page;
610 alloc_region->last_page = last_page;
611 alloc_region->start_addr = page_table[first_page].bytes_used
612 + page_address(first_page);
613 alloc_region->free_pointer = alloc_region->start_addr;
614 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
616 if (gencgc_zero_check) {
618 for (p = (int *)alloc_region->start_addr;
619 p < (int *)alloc_region->end_addr; p++) {
621 /* KLUDGE: It would be nice to use %lx and explicit casts
622 * (long) in code like this, so that it is less likely to
623 * break randomly when running on a machine with different
624 * word sizes. -- WHN 19991129 */
625 lose("The new region at %x is not zero.", p);
630 /* Set up the pages. */
632 /* The first page may have already been in use. */
633 if (page_table[first_page].bytes_used == 0) {
635 page_table[first_page].allocated = UNBOXED_PAGE;
637 page_table[first_page].allocated = BOXED_PAGE;
638 page_table[first_page].gen = gc_alloc_generation;
639 page_table[first_page].large_object = 0;
640 page_table[first_page].first_object_offset = 0;
644 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
646 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
647 gc_assert(page_table[first_page].gen == gc_alloc_generation);
648 gc_assert(page_table[first_page].large_object == 0);
650 for (i = first_page+1; i <= last_page; i++) {
652 page_table[i].allocated = UNBOXED_PAGE;
654 page_table[i].allocated = BOXED_PAGE;
655 page_table[i].gen = gc_alloc_generation;
656 page_table[i].large_object = 0;
657 /* This may not be necessary for unboxed regions (think it was
659 page_table[i].first_object_offset =
660 alloc_region->start_addr - page_address(i);
663 /* Bump up last_free_page. */
664 if (last_page+1 > last_free_page) {
665 last_free_page = last_page+1;
666 SetSymbolValue(ALLOCATION_POINTER,
667 (lispobj)(((char *)heap_base) + last_free_page*4096));
668 if (last_page+1 > last_used_page)
669 last_used_page = last_page+1;
673 /* If the record_new_objects flag is 2 then all new regions created
676 * If it's 1 then then it is only recorded if the first page of the
677 * current region is <= new_areas_ignore_page. This helps avoid
678 * unnecessary recording when doing full scavenge pass.
680 * The new_object structure holds the page, byte offset, and size of
681 * new regions of objects. Each new area is placed in the array of
682 * these structures pointer to by new_areas. new_areas_index holds the
683 * offset into new_areas.
685 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
686 * later code must detect this and handle it, probably by doing a full
687 * scavenge of a generation. */
688 #define NUM_NEW_AREAS 512
689 static int record_new_objects = 0;
690 static int new_areas_ignore_page;
696 static struct new_area (*new_areas)[];
697 static int new_areas_index;
700 /* Add a new area to new_areas. */
702 add_new_area(int first_page, int offset, int size)
704 unsigned new_area_start,c;
707 /* Ignore if full. */
708 if (new_areas_index >= NUM_NEW_AREAS)
711 switch (record_new_objects) {
715 if (first_page > new_areas_ignore_page)
724 new_area_start = 4096*first_page + offset;
726 /* Search backwards for a prior area that this follows from. If
727 found this will save adding a new area. */
728 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
730 4096*((*new_areas)[i].page)
731 + (*new_areas)[i].offset
732 + (*new_areas)[i].size;
734 "/add_new_area S1 %d %d %d %d\n",
735 i, c, new_area_start, area_end));*/
736 if (new_area_start == area_end) {
738 "/adding to [%d] %d %d %d with %d %d %d:\n",
740 (*new_areas)[i].page,
741 (*new_areas)[i].offset,
742 (*new_areas)[i].size,
746 (*new_areas)[i].size += size;
750 /*FSHOW((stderr, "/add_new_area S1 %d %d %d\n", i, c, new_area_start));*/
752 (*new_areas)[new_areas_index].page = first_page;
753 (*new_areas)[new_areas_index].offset = offset;
754 (*new_areas)[new_areas_index].size = size;
756 "/new_area %d page %d offset %d size %d\n",
757 new_areas_index, first_page, offset, size));*/
760 /* Note the max new_areas used. */
761 if (new_areas_index > max_new_areas)
762 max_new_areas = new_areas_index;
765 /* Update the tables for the alloc_region. The region maybe added to
768 * When done the alloc_region is set up so that the next quick alloc
769 * will fail safely and thus a new region will be allocated. Further
770 * it is safe to try to re-update the page table of this reset
773 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
779 int orig_first_page_bytes_used;
785 "/gc_alloc_update_page_tables to gen %d:\n",
786 gc_alloc_generation));
789 first_page = alloc_region->first_page;
791 /* Catch an unused alloc_region. */
792 if ((first_page == 0) && (alloc_region->last_page == -1))
795 next_page = first_page+1;
797 /* Skip if no bytes were allocated */
798 if (alloc_region->free_pointer != alloc_region->start_addr) {
799 orig_first_page_bytes_used = page_table[first_page].bytes_used;
801 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
803 /* All the pages used need to be updated */
805 /* Update the first page. */
807 /* If the page was free then set up the gen, and
808 first_object_offset. */
809 if (page_table[first_page].bytes_used == 0)
810 gc_assert(page_table[first_page].first_object_offset == 0);
813 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
815 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
816 gc_assert(page_table[first_page].gen == gc_alloc_generation);
817 gc_assert(page_table[first_page].large_object == 0);
821 /* Calc. the number of bytes used in this page. This is not always
822 the number of new bytes, unless it was free. */
824 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>4096) {
828 page_table[first_page].bytes_used = bytes_used;
829 byte_cnt += bytes_used;
832 /* All the rest of the pages should be free. Need to set their
833 first_object_offset pointer to the start of the region, and set
837 gc_assert(page_table[next_page].allocated == UNBOXED_PAGE);
839 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
840 gc_assert(page_table[next_page].bytes_used == 0);
841 gc_assert(page_table[next_page].gen == gc_alloc_generation);
842 gc_assert(page_table[next_page].large_object == 0);
844 gc_assert(page_table[next_page].first_object_offset ==
845 alloc_region->start_addr - page_address(next_page));
847 /* Calculate the number of bytes used in this page. */
849 if ((bytes_used = (alloc_region->free_pointer
850 - page_address(next_page)))>4096) {
854 page_table[next_page].bytes_used = bytes_used;
855 byte_cnt += bytes_used;
860 region_size = alloc_region->free_pointer - alloc_region->start_addr;
861 bytes_allocated += region_size;
862 generations[gc_alloc_generation].bytes_allocated += region_size;
864 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
866 /* Set the generations alloc restart page to the last page of
869 generations[gc_alloc_generation].alloc_unboxed_start_page =
872 generations[gc_alloc_generation].alloc_start_page = next_page-1;
874 /* Add the region to the new_areas if requested. */
876 add_new_area(first_page,orig_first_page_bytes_used, region_size);
880 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
882 gc_alloc_generation));
886 /* No bytes allocated. Unallocate the first_page if there are 0
888 if (page_table[first_page].bytes_used == 0)
889 page_table[first_page].allocated = FREE_PAGE;
891 /* Unallocate any unused pages. */
892 while (next_page <= alloc_region->last_page) {
893 gc_assert(page_table[next_page].bytes_used == 0);
894 page_table[next_page].allocated = FREE_PAGE;
898 /* Reset the alloc_region. */
899 alloc_region->first_page = 0;
900 alloc_region->last_page = -1;
901 alloc_region->start_addr = page_address(0);
902 alloc_region->free_pointer = page_address(0);
903 alloc_region->end_addr = page_address(0);
906 static inline void *gc_quick_alloc(int nbytes);
908 /* Allocate a possibly large object. */
910 *gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
918 int orig_first_page_bytes_used;
923 int large = (nbytes >= large_object_size);
927 FSHOW((stderr, "/alloc_large %d\n", nbytes));
932 "/gc_alloc_large for %d bytes from gen %d\n",
933 nbytes, gc_alloc_generation));
936 /* If the object is small, and there is room in the current region
937 then allocation it in the current region. */
939 && ((alloc_region->end_addr-alloc_region->free_pointer) >= nbytes))
940 return gc_quick_alloc(nbytes);
942 /* Search for a contiguous free region of at least nbytes. If it's a
943 large object then align it on a page boundary by searching for a
946 /* To allow the allocation of small objects without the danger of
947 using a page in the current boxed region, the search starts after
948 the current boxed free region. XX could probably keep a page
949 index ahead of the current region and bumped up here to save a
950 lot of re-scanning. */
952 restart_page = generations[gc_alloc_generation].alloc_large_unboxed_start_page;
954 restart_page = generations[gc_alloc_generation].alloc_large_start_page;
955 if (restart_page <= alloc_region->last_page)
956 restart_page = alloc_region->last_page+1;
959 first_page = restart_page;
962 while ((first_page < NUM_PAGES)
963 && (page_table[first_page].allocated != FREE_PAGE))
966 while ((first_page < NUM_PAGES)
967 && (page_table[first_page].allocated != FREE_PAGE)
969 (page_table[first_page].allocated != UNBOXED_PAGE))
971 (page_table[first_page].allocated != BOXED_PAGE))
972 || (page_table[first_page].large_object != 0)
973 || (page_table[first_page].gen != gc_alloc_generation)
974 || (page_table[first_page].bytes_used >= (4096-32))
975 || (page_table[first_page].write_protected != 0)
976 || (page_table[first_page].dont_move != 0)))
979 if (first_page >= NUM_PAGES) {
981 "Argh! gc_alloc_large failed (first_page), nbytes=%d.\n",
983 print_generation_stats(1);
987 gc_assert(page_table[first_page].write_protected == 0);
991 "/first_page=%d bytes_used=%d\n",
992 first_page, page_table[first_page].bytes_used));
995 last_page = first_page;
996 bytes_found = 4096 - page_table[first_page].bytes_used;
998 while ((bytes_found < nbytes)
999 && (last_page < (NUM_PAGES-1))
1000 && (page_table[last_page+1].allocated == FREE_PAGE)) {
1003 bytes_found += 4096;
1004 gc_assert(page_table[last_page].write_protected == 0);
1007 region_size = (4096 - page_table[first_page].bytes_used)
1008 + 4096*(last_page-first_page);
1010 gc_assert(bytes_found == region_size);
1014 "/last_page=%d bytes_found=%d num_pages=%d\n",
1015 last_page, bytes_found, num_pages));
1018 restart_page = last_page + 1;
1019 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1021 /* Check for a failure */
1022 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1024 "Argh! gc_alloc_large failed (restart_page), nbytes=%d.\n",
1026 print_generation_stats(1);
1033 "/gc_alloc_large gen %d: %d of %d bytes: from pages %d to %d: addr=%x\n",
1034 gc_alloc_generation,
1039 page_address(first_page)));
1042 gc_assert(first_page > alloc_region->last_page);
1044 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1047 generations[gc_alloc_generation].alloc_large_start_page = last_page;
1049 /* Set up the pages. */
1050 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1052 /* If the first page was free then set up the gen, and
1053 * first_object_offset. */
1054 if (page_table[first_page].bytes_used == 0) {
1056 page_table[first_page].allocated = UNBOXED_PAGE;
1058 page_table[first_page].allocated = BOXED_PAGE;
1059 page_table[first_page].gen = gc_alloc_generation;
1060 page_table[first_page].first_object_offset = 0;
1061 page_table[first_page].large_object = large;
1065 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
1067 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
1068 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1069 gc_assert(page_table[first_page].large_object == large);
1073 /* Calc. the number of bytes used in this page. This is not
1074 * always the number of new bytes, unless it was free. */
1076 if ((bytes_used = nbytes+orig_first_page_bytes_used) > 4096) {
1080 page_table[first_page].bytes_used = bytes_used;
1081 byte_cnt += bytes_used;
1083 next_page = first_page+1;
1085 /* All the rest of the pages should be free. We need to set their
1086 * first_object_offset pointer to the start of the region, and
1087 * set the bytes_used. */
1089 gc_assert(page_table[next_page].allocated == FREE_PAGE);
1090 gc_assert(page_table[next_page].bytes_used == 0);
1092 page_table[next_page].allocated = UNBOXED_PAGE;
1094 page_table[next_page].allocated = BOXED_PAGE;
1095 page_table[next_page].gen = gc_alloc_generation;
1096 page_table[next_page].large_object = large;
1098 page_table[next_page].first_object_offset =
1099 orig_first_page_bytes_used - 4096*(next_page-first_page);
1101 /* Calculate the number of bytes used in this page. */
1103 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > 4096) {
1107 page_table[next_page].bytes_used = bytes_used;
1108 byte_cnt += bytes_used;
1113 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1115 bytes_allocated += nbytes;
1116 generations[gc_alloc_generation].bytes_allocated += nbytes;
1118 /* Add the region to the new_areas if requested. */
1120 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1122 /* Bump up last_free_page */
1123 if (last_page+1 > last_free_page) {
1124 last_free_page = last_page+1;
1125 SetSymbolValue(ALLOCATION_POINTER,
1126 (lispobj)(((char *)heap_base) + last_free_page*4096));
1127 if (last_page+1 > last_used_page)
1128 last_used_page = last_page+1;
1131 return((void *)(page_address(first_page)+orig_first_page_bytes_used));
1134 /* Allocate bytes from the boxed_region. It first checks if there is
1135 * room, if not then it calls gc_alloc_new_region to find a new region
1136 * with enough space. A pointer to the start of the region is returned. */
1138 *gc_alloc(int nbytes)
1140 void *new_free_pointer;
1142 /* FSHOW((stderr, "/gc_alloc %d\n", nbytes)); */
1144 /* Check whether there is room in the current alloc region. */
1145 new_free_pointer = boxed_region.free_pointer + nbytes;
1147 if (new_free_pointer <= boxed_region.end_addr) {
1148 /* If so then allocate from the current alloc region. */
1149 void *new_obj = boxed_region.free_pointer;
1150 boxed_region.free_pointer = new_free_pointer;
1152 /* Check whether the alloc region is almost empty. */
1153 if ((boxed_region.end_addr - boxed_region.free_pointer) <= 32) {
1154 /* If so finished with the current region. */
1155 gc_alloc_update_page_tables(0, &boxed_region);
1156 /* Set up a new region. */
1157 gc_alloc_new_region(32, 0, &boxed_region);
1159 return((void *)new_obj);
1162 /* Else not enough free space in the current region. */
1164 /* If there some room left in the current region, enough to be worth
1165 * saving, then allocate a large object. */
1166 /* FIXME: "32" should be a named parameter. */
1167 if ((boxed_region.end_addr-boxed_region.free_pointer) > 32)
1168 return gc_alloc_large(nbytes, 0, &boxed_region);
1170 /* Else find a new region. */
1172 /* Finished with the current region. */
1173 gc_alloc_update_page_tables(0, &boxed_region);
1175 /* Set up a new region. */
1176 gc_alloc_new_region(nbytes, 0, &boxed_region);
1178 /* Should now be enough room. */
1180 /* Check whether there is room in the current region. */
1181 new_free_pointer = boxed_region.free_pointer + nbytes;
1183 if (new_free_pointer <= boxed_region.end_addr) {
1184 /* If so then allocate from the current region. */
1185 void *new_obj = boxed_region.free_pointer;
1186 boxed_region.free_pointer = new_free_pointer;
1188 /* Check whether the current region is almost empty. */
1189 if ((boxed_region.end_addr - boxed_region.free_pointer) <= 32) {
1190 /* If so find, finished with the current region. */
1191 gc_alloc_update_page_tables(0, &boxed_region);
1193 /* Set up a new region. */
1194 gc_alloc_new_region(32, 0, &boxed_region);
1197 return((void *)new_obj);
1200 /* shouldn't happen */
1202 return((void *) NIL); /* dummy value: return something ... */
1205 /* Allocate space from the boxed_region. If there is not enough free
1206 * space then call gc_alloc to do the job. A pointer to the start of
1207 * the region is returned. */
1209 *gc_quick_alloc(int nbytes)
1211 void *new_free_pointer;
1213 /* Check whether there is room in the current region. */
1214 new_free_pointer = boxed_region.free_pointer + nbytes;
1216 if (new_free_pointer <= boxed_region.end_addr) {
1217 /* If so then allocate from the current region. */
1218 void *new_obj = boxed_region.free_pointer;
1219 boxed_region.free_pointer = new_free_pointer;
1220 return((void *)new_obj);
1223 /* Else call gc_alloc */
1224 return (gc_alloc(nbytes));
1227 /* Allocate space for the boxed object. If it is a large object then
1228 * do a large alloc else allocate from the current region. If there is
1229 * not enough free space then call gc_alloc to do the job. A pointer
1230 * to the start of the region is returned. */
1232 *gc_quick_alloc_large(int nbytes)
1234 void *new_free_pointer;
1236 if (nbytes >= large_object_size)
1237 return gc_alloc_large(nbytes, 0, &boxed_region);
1239 /* Check whether there is room in the current region. */
1240 new_free_pointer = boxed_region.free_pointer + nbytes;
1242 if (new_free_pointer <= boxed_region.end_addr) {
1243 /* If so then allocate from the current region. */
1244 void *new_obj = boxed_region.free_pointer;
1245 boxed_region.free_pointer = new_free_pointer;
1246 return((void *)new_obj);
1249 /* Else call gc_alloc */
1250 return (gc_alloc(nbytes));
1254 *gc_alloc_unboxed(int nbytes)
1256 void *new_free_pointer;
1259 FSHOW((stderr, "/gc_alloc_unboxed %d\n", nbytes));
1262 /* Check whether there is room in the current region. */
1263 new_free_pointer = unboxed_region.free_pointer + nbytes;
1265 if (new_free_pointer <= unboxed_region.end_addr) {
1266 /* If so then allocate from the current region. */
1267 void *new_obj = unboxed_region.free_pointer;
1268 unboxed_region.free_pointer = new_free_pointer;
1270 /* Check whether the current region is almost empty. */
1271 if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1272 /* If so finished with the current region. */
1273 gc_alloc_update_page_tables(1, &unboxed_region);
1275 /* Set up a new region. */
1276 gc_alloc_new_region(32, 1, &unboxed_region);
1279 return((void *)new_obj);
1282 /* Else not enough free space in the current region. */
1284 /* If there is a bit of room left in the current region then
1285 allocate a large object. */
1286 if ((unboxed_region.end_addr-unboxed_region.free_pointer) > 32)
1287 return gc_alloc_large(nbytes,1,&unboxed_region);
1289 /* Else find a new region. */
1291 /* Finished with the current region. */
1292 gc_alloc_update_page_tables(1, &unboxed_region);
1294 /* Set up a new region. */
1295 gc_alloc_new_region(nbytes, 1, &unboxed_region);
1297 /* Should now be enough room. */
1299 /* Check whether there is room in the current region. */
1300 new_free_pointer = unboxed_region.free_pointer + nbytes;
1302 if (new_free_pointer <= unboxed_region.end_addr) {
1303 /* If so then allocate from the current region. */
1304 void *new_obj = unboxed_region.free_pointer;
1305 unboxed_region.free_pointer = new_free_pointer;
1307 /* Check whether the current region is almost empty. */
1308 if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1309 /* If so find, finished with the current region. */
1310 gc_alloc_update_page_tables(1, &unboxed_region);
1312 /* Set up a new region. */
1313 gc_alloc_new_region(32, 1, &unboxed_region);
1316 return((void *)new_obj);
1319 /* shouldn't happen? */
1321 return((void *) NIL); /* dummy value: return something ... */
1325 *gc_quick_alloc_unboxed(int nbytes)
1327 void *new_free_pointer;
1329 /* Check whether there is room in the current region. */
1330 new_free_pointer = unboxed_region.free_pointer + nbytes;
1332 if (new_free_pointer <= unboxed_region.end_addr) {
1333 /* If so then allocate from the current region. */
1334 void *new_obj = unboxed_region.free_pointer;
1335 unboxed_region.free_pointer = new_free_pointer;
1337 return((void *)new_obj);
1340 /* Else call gc_alloc */
1341 return (gc_alloc_unboxed(nbytes));
1344 /* Allocate space for the object. If it is a large object then do a
1345 * large alloc else allocate from the current region. If there is not
1346 * enough free space then call gc_alloc to do the job.
1348 * A pointer to the start of the region is returned. */
1350 *gc_quick_alloc_large_unboxed(int nbytes)
1352 void *new_free_pointer;
1354 if (nbytes >= large_object_size)
1355 return gc_alloc_large(nbytes,1,&unboxed_region);
1357 /* Check whether there is room in the current region. */
1358 new_free_pointer = unboxed_region.free_pointer + nbytes;
1360 if (new_free_pointer <= unboxed_region.end_addr) {
1361 /* If so then allocate from the current region. */
1362 void *new_obj = unboxed_region.free_pointer;
1363 unboxed_region.free_pointer = new_free_pointer;
1365 return((void *)new_obj);
1368 /* Else call gc_alloc. */
1369 return (gc_alloc_unboxed(nbytes));
1373 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1376 static int (*scavtab[256])(lispobj *where, lispobj object);
1377 static lispobj (*transother[256])(lispobj object);
1378 static int (*sizetab[256])(lispobj *where);
1380 static struct weak_pointer *weak_pointers;
1382 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
1388 static inline boolean
1389 from_space_p(lispobj obj)
1391 int page_index=(void*)obj - heap_base;
1392 return ((page_index >= 0)
1393 && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
1394 && (page_table[page_index].gen == from_space));
1397 static inline boolean
1398 new_space_p(lispobj obj)
1400 int page_index = (void*)obj - heap_base;
1401 return ((page_index >= 0)
1402 && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
1403 && (page_table[page_index].gen == new_space));
1410 /* to copy a boxed object */
1411 static inline lispobj
1412 copy_object(lispobj object, int nwords)
1416 lispobj *source, *dest;
1418 gc_assert(Pointerp(object));
1419 gc_assert(from_space_p(object));
1420 gc_assert((nwords & 0x01) == 0);
1422 /* Get tag of object. */
1423 tag = LowtagOf(object);
1425 /* Allocate space. */
1426 new = gc_quick_alloc(nwords*4);
1429 source = (lispobj *) PTR(object);
1431 /* Copy the object. */
1432 while (nwords > 0) {
1433 dest[0] = source[0];
1434 dest[1] = source[1];
1440 /* Return Lisp pointer of new object. */
1441 return ((lispobj) new) | tag;
1444 /* to copy a large boxed object. If the object is in a large object
1445 * region then it is simply promoted, else it is copied. If it's large
1446 * enough then it's copied to a large object region.
1448 * Vectors may have shrunk. If the object is not copied the space
1449 * needs to be reclaimed, and the page_tables corrected. */
1451 copy_large_object(lispobj object, int nwords)
1455 lispobj *source, *dest;
1458 gc_assert(Pointerp(object));
1459 gc_assert(from_space_p(object));
1460 gc_assert((nwords & 0x01) == 0);
1462 if ((nwords > 1024*1024) && gencgc_verbose) {
1463 FSHOW((stderr, "/copy_large_object: %d bytes\n", nwords*4));
1466 /* Check whether it's a large object. */
1467 first_page = find_page_index((void *)object);
1468 gc_assert(first_page >= 0);
1470 if (page_table[first_page].large_object) {
1472 /* Promote the object. */
1474 int remaining_bytes;
1479 /* Note: Any page write-protection must be removed, else a
1480 * later scavenge_newspace may incorrectly not scavenge these
1481 * pages. This would not be necessary if they are added to the
1482 * new areas, but let's do it for them all (they'll probably
1483 * be written anyway?). */
1485 gc_assert(page_table[first_page].first_object_offset == 0);
1487 next_page = first_page;
1488 remaining_bytes = nwords*4;
1489 while (remaining_bytes > 4096) {
1490 gc_assert(page_table[next_page].gen == from_space);
1491 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
1492 gc_assert(page_table[next_page].large_object);
1493 gc_assert(page_table[next_page].first_object_offset==
1494 -4096*(next_page-first_page));
1495 gc_assert(page_table[next_page].bytes_used == 4096);
1497 page_table[next_page].gen = new_space;
1499 /* Remove any write-protection. We should be able to rely
1500 * on the write-protect flag to avoid redundant calls. */
1501 if (page_table[next_page].write_protected) {
1502 os_protect(page_address(next_page), 4096, OS_VM_PROT_ALL);
1503 page_table[next_page].write_protected = 0;
1505 remaining_bytes -= 4096;
1509 /* Now only one page remains, but the object may have shrunk
1510 * so there may be more unused pages which will be freed. */
1512 /* The object may have shrunk but shouldn't have grown. */
1513 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1515 page_table[next_page].gen = new_space;
1516 gc_assert(page_table[next_page].allocated = BOXED_PAGE);
1518 /* Adjust the bytes_used. */
1519 old_bytes_used = page_table[next_page].bytes_used;
1520 page_table[next_page].bytes_used = remaining_bytes;
1522 bytes_freed = old_bytes_used - remaining_bytes;
1524 /* Free any remaining pages; needs care. */
1526 while ((old_bytes_used == 4096) &&
1527 (page_table[next_page].gen == from_space) &&
1528 (page_table[next_page].allocated == BOXED_PAGE) &&
1529 page_table[next_page].large_object &&
1530 (page_table[next_page].first_object_offset ==
1531 -(next_page - first_page)*4096)) {
1532 /* Checks out OK, free the page. Don't need to both zeroing
1533 * pages as this should have been done before shrinking the
1534 * object. These pages shouldn't be write-protected as they
1535 * should be zero filled. */
1536 gc_assert(page_table[next_page].write_protected == 0);
1538 old_bytes_used = page_table[next_page].bytes_used;
1539 page_table[next_page].allocated = FREE_PAGE;
1540 page_table[next_page].bytes_used = 0;
1541 bytes_freed += old_bytes_used;
1545 if ((bytes_freed > 0) && gencgc_verbose)
1546 FSHOW((stderr, "/copy_large_boxed bytes_freed=%d\n", bytes_freed));
1548 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1549 generations[new_space].bytes_allocated += 4*nwords;
1550 bytes_allocated -= bytes_freed;
1552 /* Add the region to the new_areas if requested. */
1553 add_new_area(first_page,0,nwords*4);
1557 /* Get tag of object. */
1558 tag = LowtagOf(object);
1560 /* Allocate space. */
1561 new = gc_quick_alloc_large(nwords*4);
1564 source = (lispobj *) PTR(object);
1566 /* Copy the object. */
1567 while (nwords > 0) {
1568 dest[0] = source[0];
1569 dest[1] = source[1];
1575 /* Return Lisp pointer of new object. */
1576 return ((lispobj) new) | tag;
1580 /* to copy unboxed objects */
1581 static inline lispobj
1582 copy_unboxed_object(lispobj object, int nwords)
1586 lispobj *source, *dest;
1588 gc_assert(Pointerp(object));
1589 gc_assert(from_space_p(object));
1590 gc_assert((nwords & 0x01) == 0);
1592 /* Get tag of object. */
1593 tag = LowtagOf(object);
1595 /* Allocate space. */
1596 new = gc_quick_alloc_unboxed(nwords*4);
1599 source = (lispobj *) PTR(object);
1601 /* Copy the object. */
1602 while (nwords > 0) {
1603 dest[0] = source[0];
1604 dest[1] = source[1];
1610 /* Return Lisp pointer of new object. */
1611 return ((lispobj) new) | tag;
1614 /* to copy large unboxed objects
1616 * If the object is in a large object region then it is simply
1617 * promoted, else it is copied. If it's large enough then it's copied
1618 * to a large object region.
1620 * Bignums and vectors may have shrunk. If the object is not copied
1621 * the space needs to be reclaimed, and the page_tables corrected.
1623 * KLUDGE: There's a lot of cut-and-paste duplication between this
1624 * function and copy_large_object(..). -- WHN 20000619 */
1626 copy_large_unboxed_object(lispobj object, int nwords)
1630 lispobj *source, *dest;
1633 gc_assert(Pointerp(object));
1634 gc_assert(from_space_p(object));
1635 gc_assert((nwords & 0x01) == 0);
1637 if ((nwords > 1024*1024) && gencgc_verbose)
1638 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*4));
1640 /* Check whether it's a large object. */
1641 first_page = find_page_index((void *)object);
1642 gc_assert(first_page >= 0);
1644 if (page_table[first_page].large_object) {
1645 /* Promote the object. Note: Unboxed objects may have been
1646 * allocated to a BOXED region so it may be necessary to
1647 * change the region to UNBOXED. */
1648 int remaining_bytes;
1653 gc_assert(page_table[first_page].first_object_offset == 0);
1655 next_page = first_page;
1656 remaining_bytes = nwords*4;
1657 while (remaining_bytes > 4096) {
1658 gc_assert(page_table[next_page].gen == from_space);
1659 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE)
1660 || (page_table[next_page].allocated == BOXED_PAGE));
1661 gc_assert(page_table[next_page].large_object);
1662 gc_assert(page_table[next_page].first_object_offset==
1663 -4096*(next_page-first_page));
1664 gc_assert(page_table[next_page].bytes_used == 4096);
1666 page_table[next_page].gen = new_space;
1667 page_table[next_page].allocated = UNBOXED_PAGE;
1668 remaining_bytes -= 4096;
1672 /* Now only one page remains, but the object may have shrunk so
1673 * there may be more unused pages which will be freed. */
1675 /* Object may have shrunk but shouldn't have grown - check. */
1676 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1678 page_table[next_page].gen = new_space;
1679 page_table[next_page].allocated = UNBOXED_PAGE;
1681 /* Adjust the bytes_used. */
1682 old_bytes_used = page_table[next_page].bytes_used;
1683 page_table[next_page].bytes_used = remaining_bytes;
1685 bytes_freed = old_bytes_used - remaining_bytes;
1687 /* Free any remaining pages; needs care. */
1689 while ((old_bytes_used == 4096) &&
1690 (page_table[next_page].gen == from_space) &&
1691 ((page_table[next_page].allocated == UNBOXED_PAGE)
1692 || (page_table[next_page].allocated == BOXED_PAGE)) &&
1693 page_table[next_page].large_object &&
1694 (page_table[next_page].first_object_offset ==
1695 -(next_page - first_page)*4096)) {
1696 /* Checks out OK, free the page. Don't need to both zeroing
1697 * pages as this should have been done before shrinking the
1698 * object. These pages shouldn't be write-protected, even if
1699 * boxed they should be zero filled. */
1700 gc_assert(page_table[next_page].write_protected == 0);
1702 old_bytes_used = page_table[next_page].bytes_used;
1703 page_table[next_page].allocated = FREE_PAGE;
1704 page_table[next_page].bytes_used = 0;
1705 bytes_freed += old_bytes_used;
1709 if ((bytes_freed > 0) && gencgc_verbose)
1711 "/copy_large_unboxed bytes_freed=%d\n",
1714 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1715 generations[new_space].bytes_allocated += 4*nwords;
1716 bytes_allocated -= bytes_freed;
1721 /* Get tag of object. */
1722 tag = LowtagOf(object);
1724 /* Allocate space. */
1725 new = gc_quick_alloc_large_unboxed(nwords*4);
1728 source = (lispobj *) PTR(object);
1730 /* Copy the object. */
1731 while (nwords > 0) {
1732 dest[0] = source[0];
1733 dest[1] = source[1];
1739 /* Return Lisp pointer of new object. */
1740 return ((lispobj) new) | tag;
1748 #define DIRECT_SCAV 0
1750 /* FIXME: Most calls end up going to a little trouble to compute an
1751 * 'nwords' value. The system might be a little simpler if this
1752 * function used an 'end' parameter instead. */
1754 scavenge(lispobj *start, long nwords)
1756 while (nwords > 0) {
1761 int words_scavenged;
1765 /* FSHOW((stderr, "Scavenge: %p, %ld\n", start, nwords)); */
1767 gc_assert(object != 0x01); /* not a forwarding pointer */
1770 type = TypeOf(object);
1771 words_scavenged = (scavtab[type])(start, object);
1773 if (Pointerp(object)) {
1774 /* It's a pointer. */
1775 if (from_space_p(object)) {
1776 /* It currently points to old space. Check for a forwarding
1778 lispobj *ptr = (lispobj *)PTR(object);
1779 lispobj first_word = *ptr;
1781 if (first_word == 0x01) {
1782 /* Yes, there's a forwarding pointer. */
1784 words_scavenged = 1;
1787 /* Scavenge that pointer. */
1788 words_scavenged = (scavtab[TypeOf(object)])(start, object);
1790 /* It points somewhere other than oldspace. Leave it alone. */
1791 words_scavenged = 1;
1794 if ((object & 3) == 0) {
1795 /* It's a fixnum: really easy.. */
1796 words_scavenged = 1;
1798 /* It's some sort of header object or another. */
1799 words_scavenged = (scavtab[TypeOf(object)])(start, object);
1804 start += words_scavenged;
1805 nwords -= words_scavenged;
1807 gc_assert(nwords == 0);
1812 * code and code-related objects
1815 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
1817 static lispobj trans_function_header(lispobj object);
1818 static lispobj trans_boxed(lispobj object);
1822 scav_function_pointer(lispobj *where, lispobj object)
1824 gc_assert(Pointerp(object));
1826 if (from_space_p(object)) {
1827 lispobj first, *first_pointer;
1829 /* object is a pointer into from space. Check to see whether
1830 * it has been forwarded. */
1831 first_pointer = (lispobj *) PTR(object);
1832 first = *first_pointer;
1834 if (first == 0x01) {
1836 *where = first_pointer[1];
1843 /* must transport object -- object may point to either a
1844 * function header, a closure function header, or to a
1845 * closure header. */
1847 type = TypeOf(first);
1849 case type_FunctionHeader:
1850 case type_ClosureFunctionHeader:
1851 copy = trans_function_header(object);
1854 copy = trans_boxed(object);
1858 if (copy != object) {
1859 /* Set forwarding pointer. */
1860 first_pointer[0] = 0x01;
1861 first_pointer[1] = copy;
1867 gc_assert(Pointerp(first));
1868 gc_assert(!from_space_p(first));
1876 scav_function_pointer(lispobj *where, lispobj object)
1878 lispobj *first_pointer;
1881 gc_assert(Pointerp(object));
1883 /* Object is a pointer into from space - no a FP. */
1884 first_pointer = (lispobj *) PTR(object);
1886 /* must transport object -- object may point to either a function
1887 * header, a closure function header, or to a closure header. */
1889 switch (TypeOf(*first_pointer)) {
1890 case type_FunctionHeader:
1891 case type_ClosureFunctionHeader:
1892 copy = trans_function_header(object);
1895 copy = trans_boxed(object);
1899 if (copy != object) {
1900 /* Set forwarding pointer */
1901 first_pointer[0] = 0x01;
1902 first_pointer[1] = copy;
1905 gc_assert(Pointerp(copy));
1906 gc_assert(!from_space_p(copy));
1914 /* Scan a x86 compiled code object, looking for possible fixups that
1915 * have been missed after a move.
1917 * Two types of fixups are needed:
1918 * 1. Absolute fixups to within the code object.
1919 * 2. Relative fixups to outside the code object.
1921 * Currently only absolute fixups to the constant vector, or to the
1922 * code area are checked. */
1924 sniff_code_object(struct code *code, unsigned displacement)
1926 int nheader_words, ncode_words, nwords;
1928 void *constants_start_addr, *constants_end_addr;
1929 void *code_start_addr, *code_end_addr;
1930 int fixup_found = 0;
1932 if (!check_code_fixups)
1935 /* It's ok if it's byte compiled code. The trace table offset will
1936 * be a fixnum if it's x86 compiled code - check. */
1937 if (code->trace_table_offset & 0x3) {
1938 FSHOW((stderr, "/Sniffing byte compiled code object at %x.\n", code));
1942 /* Else it's x86 machine code. */
1944 ncode_words = fixnum_value(code->code_size);
1945 nheader_words = HeaderValue(*(lispobj *)code);
1946 nwords = ncode_words + nheader_words;
1948 constants_start_addr = (void *)code + 5*4;
1949 constants_end_addr = (void *)code + nheader_words*4;
1950 code_start_addr = (void *)code + nheader_words*4;
1951 code_end_addr = (void *)code + nwords*4;
1953 /* Work through the unboxed code. */
1954 for (p = code_start_addr; p < code_end_addr; p++) {
1955 void *data = *(void **)p;
1956 unsigned d1 = *((unsigned char *)p - 1);
1957 unsigned d2 = *((unsigned char *)p - 2);
1958 unsigned d3 = *((unsigned char *)p - 3);
1959 unsigned d4 = *((unsigned char *)p - 4);
1960 unsigned d5 = *((unsigned char *)p - 5);
1961 unsigned d6 = *((unsigned char *)p - 6);
1963 /* Check for code references. */
1964 /* Check for a 32 bit word that looks like an absolute
1965 reference to within the code adea of the code object. */
1966 if ((data >= (code_start_addr-displacement))
1967 && (data < (code_end_addr-displacement))) {
1968 /* function header */
1970 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1971 /* Skip the function header */
1975 /* the case of PUSH imm32 */
1979 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1980 p, d6, d5, d4, d3, d2, d1, data));
1981 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1983 /* the case of MOV [reg-8],imm32 */
1985 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1986 || d2==0x45 || d2==0x46 || d2==0x47)
1990 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1991 p, d6, d5, d4, d3, d2, d1, data));
1992 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1994 /* the case of LEA reg,[disp32] */
1995 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1998 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1999 p, d6, d5, d4, d3, d2, d1, data));
2000 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
2004 /* Check for constant references. */
2005 /* Check for a 32 bit word that looks like an absolute
2006 reference to within the constant vector. Constant references
2008 if ((data >= (constants_start_addr-displacement))
2009 && (data < (constants_end_addr-displacement))
2010 && (((unsigned)data & 0x3) == 0)) {
2015 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2016 p, d6, d5, d4, d3, d2, d1, data));
2017 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
2020 /* the case of MOV m32,EAX */
2024 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2025 p, d6, d5, d4, d3, d2, d1, data));
2026 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
2029 /* the case of CMP m32,imm32 */
2030 if ((d1 == 0x3d) && (d2 == 0x81)) {
2033 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2034 p, d6, d5, d4, d3, d2, d1, data));
2036 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
2039 /* Check for a mod=00, r/m=101 byte. */
2040 if ((d1 & 0xc7) == 5) {
2045 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2046 p, d6, d5, d4, d3, d2, d1, data));
2047 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
2049 /* the case of CMP reg32,m32 */
2053 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2054 p, d6, d5, d4, d3, d2, d1, data));
2055 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
2057 /* the case of MOV m32,reg32 */
2061 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2062 p, d6, d5, d4, d3, d2, d1, data));
2063 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
2065 /* the case of MOV reg32,m32 */
2069 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2070 p, d6, d5, d4, d3, d2, d1, data));
2071 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
2073 /* the case of LEA reg32,m32 */
2077 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2078 p, d6, d5, d4, d3, d2, d1, data));
2079 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
2085 /* If anything was found, print some information on the code
2089 "/compiled code object at %x: header words = %d, code words = %d\n",
2090 code, nheader_words, ncode_words));
2092 "/const start = %x, end = %x\n",
2093 constants_start_addr, constants_end_addr));
2095 "/code start = %x, end = %x\n",
2096 code_start_addr, code_end_addr));
2101 apply_code_fixups(struct code *old_code, struct code *new_code)
2103 int nheader_words, ncode_words, nwords;
2104 void *constants_start_addr, *constants_end_addr;
2105 void *code_start_addr, *code_end_addr;
2106 lispobj fixups = NIL;
2107 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
2108 struct vector *fixups_vector;
2110 /* It's OK if it's byte compiled code. The trace table offset will
2111 * be a fixnum if it's x86 compiled code - check. */
2112 if (new_code->trace_table_offset & 0x3) {
2113 /* FSHOW((stderr, "/byte compiled code object at %x\n", new_code)); */
2117 /* Else it's x86 machine code. */
2118 ncode_words = fixnum_value(new_code->code_size);
2119 nheader_words = HeaderValue(*(lispobj *)new_code);
2120 nwords = ncode_words + nheader_words;
2122 "/compiled code object at %x: header words = %d, code words = %d\n",
2123 new_code, nheader_words, ncode_words)); */
2124 constants_start_addr = (void *)new_code + 5*4;
2125 constants_end_addr = (void *)new_code + nheader_words*4;
2126 code_start_addr = (void *)new_code + nheader_words*4;
2127 code_end_addr = (void *)new_code + nwords*4;
2130 "/const start = %x, end = %x\n",
2131 constants_start_addr,constants_end_addr));
2133 "/code start = %x; end = %x\n",
2134 code_start_addr,code_end_addr));
2137 /* The first constant should be a pointer to the fixups for this
2138 code objects. Check. */
2139 fixups = new_code->constants[0];
2141 /* It will be 0 or the unbound-marker if there are no fixups, and
2142 * will be an other pointer if it is valid. */
2143 if ((fixups == 0) || (fixups == type_UnboundMarker) || !Pointerp(fixups)) {
2144 /* Check for possible errors. */
2145 if (check_code_fixups)
2146 sniff_code_object(new_code, displacement);
2148 /*fprintf(stderr,"Fixups for code object not found!?\n");
2149 fprintf(stderr,"*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2150 new_code, nheader_words, ncode_words);
2151 fprintf(stderr,"*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2152 constants_start_addr,constants_end_addr,
2153 code_start_addr,code_end_addr);*/
2157 fixups_vector = (struct vector *)PTR(fixups);
2159 /* Could be pointing to a forwarding pointer. */
2160 if (Pointerp(fixups) && (find_page_index((void*)fixups_vector) != -1)
2161 && (fixups_vector->header == 0x01)) {
2162 /* If so, then follow it. */
2163 /*SHOW("following pointer to a forwarding pointer");*/
2164 fixups_vector = (struct vector *)PTR((lispobj)fixups_vector->length);
2167 /*SHOW("got fixups");*/
2169 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
2170 /* Got the fixups for the code block. Now work through the vector,
2171 and apply a fixup at each address. */
2172 int length = fixnum_value(fixups_vector->length);
2174 for (i = 0; i < length; i++) {
2175 unsigned offset = fixups_vector->data[i];
2176 /* Now check the current value of offset. */
2177 unsigned old_value =
2178 *(unsigned *)((unsigned)code_start_addr + offset);
2180 /* If it's within the old_code object then it must be an
2181 * absolute fixup (relative ones are not saved) */
2182 if ((old_value >= (unsigned)old_code)
2183 && (old_value < ((unsigned)old_code + nwords*4)))
2184 /* So add the dispacement. */
2185 *(unsigned *)((unsigned)code_start_addr + offset) =
2186 old_value + displacement;
2188 /* It is outside the old code object so it must be a
2189 * relative fixup (absolute fixups are not saved). So
2190 * subtract the displacement. */
2191 *(unsigned *)((unsigned)code_start_addr + offset) =
2192 old_value - displacement;
2196 /* Check for possible errors. */
2197 if (check_code_fixups) {
2198 sniff_code_object(new_code,displacement);
2202 static struct code *
2203 trans_code(struct code *code)
2205 struct code *new_code;
2206 lispobj l_code, l_new_code;
2207 int nheader_words, ncode_words, nwords;
2208 unsigned long displacement;
2209 lispobj fheaderl, *prev_pointer;
2212 "\n/transporting code object located at 0x%08x\n",
2213 (unsigned long) code)); */
2215 /* If object has already been transported, just return pointer. */
2216 if (*((lispobj *)code) == 0x01)
2217 return (struct code*)(((lispobj *)code)[1]);
2219 gc_assert(TypeOf(code->header) == type_CodeHeader);
2221 /* Prepare to transport the code vector. */
2222 l_code = (lispobj) code | type_OtherPointer;
2224 ncode_words = fixnum_value(code->code_size);
2225 nheader_words = HeaderValue(code->header);
2226 nwords = ncode_words + nheader_words;
2227 nwords = CEILING(nwords, 2);
2229 l_new_code = copy_large_object(l_code, nwords);
2230 new_code = (struct code *) PTR(l_new_code);
2232 /* may not have been moved.. */
2233 if (new_code == code)
2236 displacement = l_new_code - l_code;
2240 "/old code object at 0x%08x, new code object at 0x%08x\n",
2241 (unsigned long) code,
2242 (unsigned long) new_code));
2243 FSHOW((stderr, "/Code object is %d words long.\n", nwords));
2246 /* Set forwarding pointer. */
2247 ((lispobj *)code)[0] = 0x01;
2248 ((lispobj *)code)[1] = l_new_code;
2250 /* Set forwarding pointers for all the function headers in the
2251 * code object. Also fix all self pointers. */
2253 fheaderl = code->entry_points;
2254 prev_pointer = &new_code->entry_points;
2256 while (fheaderl != NIL) {
2257 struct function *fheaderp, *nfheaderp;
2260 fheaderp = (struct function *) PTR(fheaderl);
2261 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
2263 /* Calculate the new function pointer and the new */
2264 /* function header. */
2265 nfheaderl = fheaderl + displacement;
2266 nfheaderp = (struct function *) PTR(nfheaderl);
2268 /* Set forwarding pointer. */
2269 ((lispobj *)fheaderp)[0] = 0x01;
2270 ((lispobj *)fheaderp)[1] = nfheaderl;
2272 /* Fix self pointer. */
2273 nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
2275 *prev_pointer = nfheaderl;
2277 fheaderl = fheaderp->next;
2278 prev_pointer = &nfheaderp->next;
2281 /* sniff_code_object(new_code,displacement);*/
2282 apply_code_fixups(code,new_code);
2288 scav_code_header(lispobj *where, lispobj object)
2291 int nheader_words, ncode_words, nwords;
2293 struct function *fheaderp;
2295 code = (struct code *) where;
2296 ncode_words = fixnum_value(code->code_size);
2297 nheader_words = HeaderValue(object);
2298 nwords = ncode_words + nheader_words;
2299 nwords = CEILING(nwords, 2);
2301 /* Scavenge the boxed section of the code data block. */
2302 scavenge(where + 1, nheader_words - 1);
2304 /* Scavenge the boxed section of each function object in the */
2305 /* code data block. */
2306 fheaderl = code->entry_points;
2307 while (fheaderl != NIL) {
2308 fheaderp = (struct function *) PTR(fheaderl);
2309 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
2311 scavenge(&fheaderp->name, 1);
2312 scavenge(&fheaderp->arglist, 1);
2313 scavenge(&fheaderp->type, 1);
2315 fheaderl = fheaderp->next;
2322 trans_code_header(lispobj object)
2326 ncode = trans_code((struct code *) PTR(object));
2327 return (lispobj) ncode | type_OtherPointer;
2331 size_code_header(lispobj *where)
2334 int nheader_words, ncode_words, nwords;
2336 code = (struct code *) where;
2338 ncode_words = fixnum_value(code->code_size);
2339 nheader_words = HeaderValue(code->header);
2340 nwords = ncode_words + nheader_words;
2341 nwords = CEILING(nwords, 2);
2347 scav_return_pc_header(lispobj *where, lispobj object)
2349 lose("attempted to scavenge a return PC header where=0x%08x object=0x%08x",
2350 (unsigned long) where,
2351 (unsigned long) object);
2352 return 0; /* bogus return value to satisfy static type checking */
2356 trans_return_pc_header(lispobj object)
2358 struct function *return_pc;
2359 unsigned long offset;
2360 struct code *code, *ncode;
2362 SHOW("/trans_return_pc_header: Will this work?");
2364 return_pc = (struct function *) PTR(object);
2365 offset = HeaderValue(return_pc->header) * 4;
2367 /* Transport the whole code object. */
2368 code = (struct code *) ((unsigned long) return_pc - offset);
2369 ncode = trans_code(code);
2371 return ((lispobj) ncode + offset) | type_OtherPointer;
2374 /* On the 386, closures hold a pointer to the raw address instead of the
2375 * function object. */
2378 scav_closure_header(lispobj *where, lispobj object)
2380 struct closure *closure;
2383 closure = (struct closure *)where;
2384 fun = closure->function - RAW_ADDR_OFFSET;
2386 /* The function may have moved so update the raw address. But
2387 * don't write unnecessarily. */
2388 if (closure->function != fun + RAW_ADDR_OFFSET)
2389 closure->function = fun + RAW_ADDR_OFFSET;
2396 scav_function_header(lispobj *where, lispobj object)
2398 lose("attempted to scavenge a function header where=0x%08x object=0x%08x",
2399 (unsigned long) where,
2400 (unsigned long) object);
2401 return 0; /* bogus return value to satisfy static type checking */
2405 trans_function_header(lispobj object)
2407 struct function *fheader;
2408 unsigned long offset;
2409 struct code *code, *ncode;
2411 fheader = (struct function *) PTR(object);
2412 offset = HeaderValue(fheader->header) * 4;
2414 /* Transport the whole code object. */
2415 code = (struct code *) ((unsigned long) fheader - offset);
2416 ncode = trans_code(code);
2418 return ((lispobj) ncode + offset) | type_FunctionPointer;
2427 scav_instance_pointer(lispobj *where, lispobj object)
2429 if (from_space_p(object)) {
2430 lispobj first, *first_pointer;
2432 /* Object is a pointer into from space. Check to see */
2433 /* whether it has been forwarded. */
2434 first_pointer = (lispobj *) PTR(object);
2435 first = *first_pointer;
2437 if (first == 0x01) {
2439 first = first_pointer[1];
2441 first = trans_boxed(object);
2442 gc_assert(first != object);
2443 /* Set forwarding pointer. */
2444 first_pointer[0] = 0x01;
2445 first_pointer[1] = first;
2453 scav_instance_pointer(lispobj *where, lispobj object)
2455 lispobj copy, *first_pointer;
2457 /* Object is a pointer into from space - not a FP. */
2458 copy = trans_boxed(object);
2460 gc_assert(copy != object);
2462 first_pointer = (lispobj *) PTR(object);
2464 /* Set forwarding pointer. */
2465 first_pointer[0] = 0x01;
2466 first_pointer[1] = copy;
2477 static lispobj trans_list(lispobj object);
2481 scav_list_pointer(lispobj *where, lispobj object)
2483 /* KLUDGE: There's lots of cut-and-paste duplication between this
2484 * and scav_instance_pointer(..), scav_other_pointer(..), and
2485 * perhaps other functions too. -- WHN 20000620 */
2487 gc_assert(Pointerp(object));
2489 if (from_space_p(object)) {
2490 lispobj first, *first_pointer;
2492 /* Object is a pointer into from space. Check to see whether it has
2493 * been forwarded. */
2494 first_pointer = (lispobj *) PTR(object);
2495 first = *first_pointer;
2497 if (first == 0x01) {
2499 first = first_pointer[1];
2501 first = trans_list(object);
2503 /* Set forwarding pointer */
2504 first_pointer[0] = 0x01;
2505 first_pointer[1] = first;
2508 gc_assert(Pointerp(first));
2509 gc_assert(!from_space_p(first));
2516 scav_list_pointer(lispobj *where, lispobj object)
2518 lispobj first, *first_pointer;
2520 gc_assert(Pointerp(object));
2522 /* Object is a pointer into from space - not FP. */
2524 first = trans_list(object);
2525 gc_assert(first != object);
2527 first_pointer = (lispobj *) PTR(object);
2529 /* Set forwarding pointer */
2530 first_pointer[0] = 0x01;
2531 first_pointer[1] = first;
2533 gc_assert(Pointerp(first));
2534 gc_assert(!from_space_p(first));
2541 trans_list(lispobj object)
2543 lispobj new_list_pointer;
2544 struct cons *cons, *new_cons;
2547 gc_assert(from_space_p(object));
2549 cons = (struct cons *) PTR(object);
2551 /* Copy 'object'. */
2552 new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
2553 new_cons->car = cons->car;
2554 new_cons->cdr = cons->cdr; /* updated later */
2555 new_list_pointer = (lispobj)new_cons | LowtagOf(object);
2557 /* Grab the cdr before it is clobbered. */
2560 /* Set forwarding pointer (clobbers start of list). */
2562 cons->cdr = new_list_pointer;
2564 /* Try to linearize the list in the cdr direction to help reduce
2568 struct cons *cdr_cons, *new_cdr_cons;
2570 if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
2571 || (*((lispobj *)PTR(cdr)) == 0x01))
2574 cdr_cons = (struct cons *) PTR(cdr);
2577 new_cdr_cons = (struct cons*) gc_quick_alloc(sizeof(struct cons));
2578 new_cdr_cons->car = cdr_cons->car;
2579 new_cdr_cons->cdr = cdr_cons->cdr;
2580 new_cdr = (lispobj)new_cdr_cons | LowtagOf(cdr);
2582 /* Grab the cdr before it is clobbered. */
2583 cdr = cdr_cons->cdr;
2585 /* Set forwarding pointer. */
2586 cdr_cons->car = 0x01;
2587 cdr_cons->cdr = new_cdr;
2589 /* Update the cdr of the last cons copied into new space to
2590 * keep the newspace scavenge from having to do it. */
2591 new_cons->cdr = new_cdr;
2593 new_cons = new_cdr_cons;
2596 return new_list_pointer;
2601 * scavenging and transporting other pointers
2606 scav_other_pointer(lispobj *where, lispobj object)
2608 gc_assert(Pointerp(object));
2610 if (from_space_p(object)) {
2611 lispobj first, *first_pointer;
2613 /* Object is a pointer into from space. Check to see */
2614 /* whether it has been forwarded. */
2615 first_pointer = (lispobj *) PTR(object);
2616 first = *first_pointer;
2618 if (first == 0x01) {
2620 first = first_pointer[1];
2623 first = (transother[TypeOf(first)])(object);
2625 if (first != object) {
2626 /* Set forwarding pointer */
2627 first_pointer[0] = 0x01;
2628 first_pointer[1] = first;
2633 gc_assert(Pointerp(first));
2634 gc_assert(!from_space_p(first));
2640 scav_other_pointer(lispobj *where, lispobj object)
2642 lispobj first, *first_pointer;
2644 gc_assert(Pointerp(object));
2646 /* Object is a pointer into from space - not FP. */
2647 first_pointer = (lispobj *) PTR(object);
2649 first = (transother[TypeOf(*first_pointer)])(object);
2651 if (first != object) {
2652 /* Set forwarding pointer. */
2653 first_pointer[0] = 0x01;
2654 first_pointer[1] = first;
2658 gc_assert(Pointerp(first));
2659 gc_assert(!from_space_p(first));
2667 * immediate, boxed, and unboxed objects
2671 size_pointer(lispobj *where)
2677 scav_immediate(lispobj *where, lispobj object)
2683 trans_immediate(lispobj object)
2685 lose("trying to transport an immediate");
2686 return NIL; /* bogus return value to satisfy static type checking */
2690 size_immediate(lispobj *where)
2697 scav_boxed(lispobj *where, lispobj object)
2703 trans_boxed(lispobj object)
2706 unsigned long length;
2708 gc_assert(Pointerp(object));
2710 header = *((lispobj *) PTR(object));
2711 length = HeaderValue(header) + 1;
2712 length = CEILING(length, 2);
2714 return copy_object(object, length);
2718 trans_boxed_large(lispobj object)
2721 unsigned long length;
2723 gc_assert(Pointerp(object));
2725 header = *((lispobj *) PTR(object));
2726 length = HeaderValue(header) + 1;
2727 length = CEILING(length, 2);
2729 return copy_large_object(object, length);
2733 size_boxed(lispobj *where)
2736 unsigned long length;
2739 length = HeaderValue(header) + 1;
2740 length = CEILING(length, 2);
2746 scav_fdefn(lispobj *where, lispobj object)
2748 struct fdefn *fdefn;
2750 fdefn = (struct fdefn *)where;
2752 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
2753 fdefn->function, fdefn->raw_addr)); */
2755 if ((char *)(fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
2756 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
2758 /* Don't write unnecessarily. */
2759 if (fdefn->raw_addr != (char *)(fdefn->function + RAW_ADDR_OFFSET))
2760 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
2762 return sizeof(struct fdefn) / sizeof(lispobj);
2769 scav_unboxed(lispobj *where, lispobj object)
2771 unsigned long length;
2773 length = HeaderValue(object) + 1;
2774 length = CEILING(length, 2);
2780 trans_unboxed(lispobj object)
2783 unsigned long length;
2786 gc_assert(Pointerp(object));
2788 header = *((lispobj *) PTR(object));
2789 length = HeaderValue(header) + 1;
2790 length = CEILING(length, 2);
2792 return copy_unboxed_object(object, length);
2796 trans_unboxed_large(lispobj object)
2799 unsigned long length;
2802 gc_assert(Pointerp(object));
2804 header = *((lispobj *) PTR(object));
2805 length = HeaderValue(header) + 1;
2806 length = CEILING(length, 2);
2808 return copy_large_unboxed_object(object, length);
2812 size_unboxed(lispobj *where)
2815 unsigned long length;
2818 length = HeaderValue(header) + 1;
2819 length = CEILING(length, 2);
2825 * vector-like objects
2828 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
2831 scav_string(lispobj *where, lispobj object)
2833 struct vector *vector;
2836 /* NOTE: Strings contain one more byte of data than the length */
2837 /* slot indicates. */
2839 vector = (struct vector *) where;
2840 length = fixnum_value(vector->length) + 1;
2841 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2847 trans_string(lispobj object)
2849 struct vector *vector;
2852 gc_assert(Pointerp(object));
2854 /* NOTE: A string contains one more byte of data (a terminating
2855 * '\0' to help when interfacing with C functions) than indicated
2856 * by the length slot. */
2858 vector = (struct vector *) PTR(object);
2859 length = fixnum_value(vector->length) + 1;
2860 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2862 return copy_large_unboxed_object(object, nwords);
2866 size_string(lispobj *where)
2868 struct vector *vector;
2871 /* NOTE: A string contains one more byte of data (a terminating
2872 * '\0' to help when interfacing with C functions) than indicated
2873 * by the length slot. */
2875 vector = (struct vector *) where;
2876 length = fixnum_value(vector->length) + 1;
2877 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2882 /* FIXME: What does this mean? */
2883 int gencgc_hash = 1;
2886 scav_vector(lispobj *where, lispobj object)
2888 unsigned int kv_length;
2890 unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
2891 lispobj *hash_table;
2892 lispobj empty_symbol;
2893 unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2894 unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2895 unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2897 unsigned next_vector_length = 0;
2899 /* FIXME: A comment explaining this would be nice. It looks as
2900 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
2901 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
2902 if (HeaderValue(object) != subtype_VectorValidHashing)
2906 /* This is set for backward compatibility. FIXME: Do we need
2908 *where = (subtype_VectorMustRehash << type_Bits) | type_SimpleVector;
2912 kv_length = fixnum_value(where[1]);
2913 kv_vector = where + 2; /* Skip the header and length. */
2914 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
2916 /* Scavenge element 0, which may be a hash-table structure. */
2917 scavenge(where+2, 1);
2918 if (!Pointerp(where[2])) {
2919 lose("no pointer at %x in hash table", where[2]);
2921 hash_table = (lispobj *)PTR(where[2]);
2922 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
2923 if (TypeOf(hash_table[0]) != type_InstanceHeader) {
2924 lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
2927 /* Scavenge element 1, which should be some internal symbol that
2928 * the hash table code reserves for marking empty slots. */
2929 scavenge(where+3, 1);
2930 if (!Pointerp(where[3])) {
2931 lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
2933 empty_symbol = where[3];
2934 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
2935 if (TypeOf(*(lispobj *)PTR(empty_symbol)) != type_SymbolHeader) {
2936 lose("not a symbol where empty-hash-table-slot symbol expected: %x",
2937 *(lispobj *)PTR(empty_symbol));
2940 /* Scavenge hash table, which will fix the positions of the other
2941 * needed objects. */
2942 scavenge(hash_table, 16);
2944 /* Cross-check the kv_vector. */
2945 if (where != (lispobj *)PTR(hash_table[9])) {
2946 lose("hash_table table!=this table %x", hash_table[9]);
2950 weak_p_obj = hash_table[10];
2954 lispobj index_vector_obj = hash_table[13];
2956 if (Pointerp(index_vector_obj) &&
2957 (TypeOf(*(lispobj *)PTR(index_vector_obj)) == type_SimpleArrayUnsignedByte32)) {
2958 index_vector = ((unsigned int *)PTR(index_vector_obj)) + 2;
2959 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
2960 length = fixnum_value(((unsigned int *)PTR(index_vector_obj))[1]);
2961 /*FSHOW((stderr, "/length = %d\n", length));*/
2963 lose("invalid index_vector %x", index_vector_obj);
2969 lispobj next_vector_obj = hash_table[14];
2971 if (Pointerp(next_vector_obj) &&
2972 (TypeOf(*(lispobj *)PTR(next_vector_obj)) == type_SimpleArrayUnsignedByte32)) {
2973 next_vector = ((unsigned int *)PTR(next_vector_obj)) + 2;
2974 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
2975 next_vector_length = fixnum_value(((unsigned int *)PTR(next_vector_obj))[1]);
2976 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
2978 lose("invalid next_vector %x", next_vector_obj);
2982 /* maybe hash vector */
2984 /* FIXME: This bare "15" offset should become a symbolic
2985 * expression of some sort. And all the other bare offsets
2986 * too. And the bare "16" in scavenge(hash_table, 16). And
2987 * probably other stuff too. Ugh.. */
2988 lispobj hash_vector_obj = hash_table[15];
2990 if (Pointerp(hash_vector_obj) &&
2991 (TypeOf(*(lispobj *)PTR(hash_vector_obj))
2992 == type_SimpleArrayUnsignedByte32)) {
2993 hash_vector = ((unsigned int *)PTR(hash_vector_obj)) + 2;
2994 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
2995 gc_assert(fixnum_value(((unsigned int *)PTR(hash_vector_obj))[1])
2996 == next_vector_length);
2999 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
3003 /* These lengths could be different as the index_vector can be a
3004 * different length from the others, a larger index_vector could help
3005 * reduce collisions. */
3006 gc_assert(next_vector_length*2 == kv_length);
3008 /* now all set up.. */
3010 /* Work through the KV vector. */
3013 for (i = 1; i < next_vector_length; i++) {
3014 lispobj old_key = kv_vector[2*i];
3015 unsigned int old_index = (old_key & 0x1fffffff)%length;
3017 /* Scavenge the key and value. */
3018 scavenge(&kv_vector[2*i],2);
3020 /* Check whether the key has moved and is EQ based. */
3022 lispobj new_key = kv_vector[2*i];
3023 unsigned int new_index = (new_key & 0x1fffffff)%length;
3025 if ((old_index != new_index) &&
3026 ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
3027 ((new_key != empty_symbol) ||
3028 (kv_vector[2*i] != empty_symbol))) {
3031 "* EQ key %d moved from %x to %x; index %d to %d\n",
3032 i, old_key, new_key, old_index, new_index));*/
3034 if (index_vector[old_index] != 0) {
3035 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
3037 /* Unlink the key from the old_index chain. */
3038 if (index_vector[old_index] == i) {
3039 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
3040 index_vector[old_index] = next_vector[i];
3041 /* Link it into the needing rehash chain. */
3042 next_vector[i] = fixnum_value(hash_table[11]);
3043 hash_table[11] = make_fixnum(i);
3046 unsigned prior = index_vector[old_index];
3047 unsigned next = next_vector[prior];
3049 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
3052 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
3055 next_vector[prior] = next_vector[next];
3056 /* Link it into the needing rehash
3059 fixnum_value(hash_table[11]);
3060 hash_table[11] = make_fixnum(next);
3065 next = next_vector[next];
3073 return (CEILING(kv_length + 2, 2));
3077 trans_vector(lispobj object)
3079 struct vector *vector;
3082 gc_assert(Pointerp(object));
3084 vector = (struct vector *) PTR(object);
3086 length = fixnum_value(vector->length);
3087 nwords = CEILING(length + 2, 2);
3089 return copy_large_object(object, nwords);
3093 size_vector(lispobj *where)
3095 struct vector *vector;
3098 vector = (struct vector *) where;
3099 length = fixnum_value(vector->length);
3100 nwords = CEILING(length + 2, 2);
3107 scav_vector_bit(lispobj *where, lispobj object)
3109 struct vector *vector;
3112 vector = (struct vector *) where;
3113 length = fixnum_value(vector->length);
3114 nwords = CEILING(NWORDS(length, 32) + 2, 2);
3120 trans_vector_bit(lispobj object)
3122 struct vector *vector;
3125 gc_assert(Pointerp(object));
3127 vector = (struct vector *) PTR(object);
3128 length = fixnum_value(vector->length);
3129 nwords = CEILING(NWORDS(length, 32) + 2, 2);
3131 return copy_large_unboxed_object(object, nwords);
3135 size_vector_bit(lispobj *where)
3137 struct vector *vector;
3140 vector = (struct vector *) where;
3141 length = fixnum_value(vector->length);
3142 nwords = CEILING(NWORDS(length, 32) + 2, 2);
3149 scav_vector_unsigned_byte_2(lispobj *where, lispobj object)
3151 struct vector *vector;
3154 vector = (struct vector *) where;
3155 length = fixnum_value(vector->length);
3156 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3162 trans_vector_unsigned_byte_2(lispobj object)
3164 struct vector *vector;
3167 gc_assert(Pointerp(object));
3169 vector = (struct vector *) PTR(object);
3170 length = fixnum_value(vector->length);
3171 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3173 return copy_large_unboxed_object(object, nwords);
3177 size_vector_unsigned_byte_2(lispobj *where)
3179 struct vector *vector;
3182 vector = (struct vector *) where;
3183 length = fixnum_value(vector->length);
3184 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3191 scav_vector_unsigned_byte_4(lispobj *where, lispobj object)
3193 struct vector *vector;
3196 vector = (struct vector *) where;
3197 length = fixnum_value(vector->length);
3198 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3204 trans_vector_unsigned_byte_4(lispobj object)
3206 struct vector *vector;
3209 gc_assert(Pointerp(object));
3211 vector = (struct vector *) PTR(object);
3212 length = fixnum_value(vector->length);
3213 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3215 return copy_large_unboxed_object(object, nwords);
3219 size_vector_unsigned_byte_4(lispobj *where)
3221 struct vector *vector;
3224 vector = (struct vector *) where;
3225 length = fixnum_value(vector->length);
3226 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3232 scav_vector_unsigned_byte_8(lispobj *where, lispobj object)
3234 struct vector *vector;
3237 vector = (struct vector *) where;
3238 length = fixnum_value(vector->length);
3239 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3245 trans_vector_unsigned_byte_8(lispobj object)
3247 struct vector *vector;
3250 gc_assert(Pointerp(object));
3252 vector = (struct vector *) PTR(object);
3253 length = fixnum_value(vector->length);
3254 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3256 return copy_large_unboxed_object(object, nwords);
3260 size_vector_unsigned_byte_8(lispobj *where)
3262 struct vector *vector;
3265 vector = (struct vector *) where;
3266 length = fixnum_value(vector->length);
3267 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3274 scav_vector_unsigned_byte_16(lispobj *where, lispobj object)
3276 struct vector *vector;
3279 vector = (struct vector *) where;
3280 length = fixnum_value(vector->length);
3281 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3287 trans_vector_unsigned_byte_16(lispobj object)
3289 struct vector *vector;
3292 gc_assert(Pointerp(object));
3294 vector = (struct vector *) PTR(object);
3295 length = fixnum_value(vector->length);
3296 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3298 return copy_large_unboxed_object(object, nwords);
3302 size_vector_unsigned_byte_16(lispobj *where)
3304 struct vector *vector;
3307 vector = (struct vector *) where;
3308 length = fixnum_value(vector->length);
3309 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3315 scav_vector_unsigned_byte_32(lispobj *where, lispobj object)
3317 struct vector *vector;
3320 vector = (struct vector *) where;
3321 length = fixnum_value(vector->length);
3322 nwords = CEILING(length + 2, 2);
3328 trans_vector_unsigned_byte_32(lispobj object)
3330 struct vector *vector;
3333 gc_assert(Pointerp(object));
3335 vector = (struct vector *) PTR(object);
3336 length = fixnum_value(vector->length);
3337 nwords = CEILING(length + 2, 2);
3339 return copy_large_unboxed_object(object, nwords);
3343 size_vector_unsigned_byte_32(lispobj *where)
3345 struct vector *vector;
3348 vector = (struct vector *) where;
3349 length = fixnum_value(vector->length);
3350 nwords = CEILING(length + 2, 2);
3356 scav_vector_single_float(lispobj *where, lispobj object)
3358 struct vector *vector;
3361 vector = (struct vector *) where;
3362 length = fixnum_value(vector->length);
3363 nwords = CEILING(length + 2, 2);
3369 trans_vector_single_float(lispobj object)
3371 struct vector *vector;
3374 gc_assert(Pointerp(object));
3376 vector = (struct vector *) PTR(object);
3377 length = fixnum_value(vector->length);
3378 nwords = CEILING(length + 2, 2);
3380 return copy_large_unboxed_object(object, nwords);
3384 size_vector_single_float(lispobj *where)
3386 struct vector *vector;
3389 vector = (struct vector *) where;
3390 length = fixnum_value(vector->length);
3391 nwords = CEILING(length + 2, 2);
3397 scav_vector_double_float(lispobj *where, lispobj object)
3399 struct vector *vector;
3402 vector = (struct vector *) where;
3403 length = fixnum_value(vector->length);
3404 nwords = CEILING(length * 2 + 2, 2);
3410 trans_vector_double_float(lispobj object)
3412 struct vector *vector;
3415 gc_assert(Pointerp(object));
3417 vector = (struct vector *) PTR(object);
3418 length = fixnum_value(vector->length);
3419 nwords = CEILING(length * 2 + 2, 2);
3421 return copy_large_unboxed_object(object, nwords);
3425 size_vector_double_float(lispobj *where)
3427 struct vector *vector;
3430 vector = (struct vector *) where;
3431 length = fixnum_value(vector->length);
3432 nwords = CEILING(length * 2 + 2, 2);
3437 #ifdef type_SimpleArrayLongFloat
3439 scav_vector_long_float(lispobj *where, lispobj object)
3441 struct vector *vector;
3444 vector = (struct vector *) where;
3445 length = fixnum_value(vector->length);
3446 nwords = CEILING(length * 3 + 2, 2);
3452 trans_vector_long_float(lispobj object)
3454 struct vector *vector;
3457 gc_assert(Pointerp(object));
3459 vector = (struct vector *) PTR(object);
3460 length = fixnum_value(vector->length);
3461 nwords = CEILING(length * 3 + 2, 2);
3463 return copy_large_unboxed_object(object, nwords);
3467 size_vector_long_float(lispobj *where)
3469 struct vector *vector;
3472 vector = (struct vector *) where;
3473 length = fixnum_value(vector->length);
3474 nwords = CEILING(length * 3 + 2, 2);
3481 #ifdef type_SimpleArrayComplexSingleFloat
3483 scav_vector_complex_single_float(lispobj *where, lispobj object)
3485 struct vector *vector;
3488 vector = (struct vector *) where;
3489 length = fixnum_value(vector->length);
3490 nwords = CEILING(length * 2 + 2, 2);
3496 trans_vector_complex_single_float(lispobj object)
3498 struct vector *vector;
3501 gc_assert(Pointerp(object));
3503 vector = (struct vector *) PTR(object);
3504 length = fixnum_value(vector->length);
3505 nwords = CEILING(length * 2 + 2, 2);
3507 return copy_large_unboxed_object(object, nwords);
3511 size_vector_complex_single_float(lispobj *where)
3513 struct vector *vector;
3516 vector = (struct vector *) where;
3517 length = fixnum_value(vector->length);
3518 nwords = CEILING(length * 2 + 2, 2);
3524 #ifdef type_SimpleArrayComplexDoubleFloat
3526 scav_vector_complex_double_float(lispobj *where, lispobj object)
3528 struct vector *vector;
3531 vector = (struct vector *) where;
3532 length = fixnum_value(vector->length);
3533 nwords = CEILING(length * 4 + 2, 2);
3539 trans_vector_complex_double_float(lispobj object)
3541 struct vector *vector;
3544 gc_assert(Pointerp(object));
3546 vector = (struct vector *) PTR(object);
3547 length = fixnum_value(vector->length);
3548 nwords = CEILING(length * 4 + 2, 2);
3550 return copy_large_unboxed_object(object, nwords);
3554 size_vector_complex_double_float(lispobj *where)
3556 struct vector *vector;
3559 vector = (struct vector *) where;
3560 length = fixnum_value(vector->length);
3561 nwords = CEILING(length * 4 + 2, 2);
3568 #ifdef type_SimpleArrayComplexLongFloat
3570 scav_vector_complex_long_float(lispobj *where, lispobj object)
3572 struct vector *vector;
3575 vector = (struct vector *) where;
3576 length = fixnum_value(vector->length);
3577 nwords = CEILING(length * 6 + 2, 2);
3583 trans_vector_complex_long_float(lispobj object)
3585 struct vector *vector;
3588 gc_assert(Pointerp(object));
3590 vector = (struct vector *) PTR(object);
3591 length = fixnum_value(vector->length);
3592 nwords = CEILING(length * 6 + 2, 2);
3594 return copy_large_unboxed_object(object, nwords);
3598 size_vector_complex_long_float(lispobj *where)
3600 struct vector *vector;
3603 vector = (struct vector *) where;
3604 length = fixnum_value(vector->length);
3605 nwords = CEILING(length * 6 + 2, 2);
3616 /* XX This is a hack adapted from cgc.c. These don't work too well with the
3617 * gencgc as a list of the weak pointers is maintained within the
3618 * objects which causes writes to the pages. A limited attempt is made
3619 * to avoid unnecessary writes, but this needs a re-think. */
3621 #define WEAK_POINTER_NWORDS \
3622 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
3625 scav_weak_pointer(lispobj *where, lispobj object)
3627 struct weak_pointer *wp = weak_pointers;
3628 /* Push the weak pointer onto the list of weak pointers.
3629 * Do I have to watch for duplicates? Originally this was
3630 * part of trans_weak_pointer but that didn't work in the
3631 * case where the WP was in a promoted region.
3634 /* Check whether it's already in the list. */
3635 while (wp != NULL) {
3636 if (wp == (struct weak_pointer*)where) {
3642 /* Add it to the start of the list. */
3643 wp = (struct weak_pointer*)where;
3644 if (wp->next != weak_pointers) {
3645 wp->next = weak_pointers;
3647 /*SHOW("avoided write to weak pointer");*/
3652 /* Do not let GC scavenge the value slot of the weak pointer.
3653 * (That is why it is a weak pointer.) */
3655 return WEAK_POINTER_NWORDS;
3659 trans_weak_pointer(lispobj object)
3662 /* struct weak_pointer *wp; */
3664 gc_assert(Pointerp(object));
3666 #if defined(DEBUG_WEAK)
3667 FSHOW((stderr, "Transporting weak pointer from 0x%08x\n", object));
3670 /* Need to remember where all the weak pointers are that have */
3671 /* been transported so they can be fixed up in a post-GC pass. */
3673 copy = copy_object(object, WEAK_POINTER_NWORDS);
3674 /* wp = (struct weak_pointer *) PTR(copy);*/
3677 /* Push the weak pointer onto the list of weak pointers. */
3678 /* wp->next = weak_pointers;
3679 * weak_pointers = wp;*/
3685 size_weak_pointer(lispobj *where)
3687 return WEAK_POINTER_NWORDS;
3690 void scan_weak_pointers(void)
3692 struct weak_pointer *wp;
3693 for (wp = weak_pointers; wp != NULL; wp = wp->next) {
3694 lispobj value = wp->value;
3695 lispobj *first_pointer;
3697 first_pointer = (lispobj *)PTR(value);
3700 FSHOW((stderr, "/weak pointer at 0x%08x\n", (unsigned long) wp));
3701 FSHOW((stderr, "/value: 0x%08x\n", (unsigned long) value));
3704 if (Pointerp(value) && from_space_p(value)) {
3705 /* Now, we need to check whether the object has been forwarded. If
3706 * it has been, the weak pointer is still good and needs to be
3707 * updated. Otherwise, the weak pointer needs to be nil'ed
3709 if (first_pointer[0] == 0x01) {
3710 wp->value = first_pointer[1];
3726 scav_lose(lispobj *where, lispobj object)
3728 lose("no scavenge function for object 0x%08x", (unsigned long) object);
3729 return 0; /* bogus return value to satisfy static type checking */
3733 trans_lose(lispobj object)
3735 lose("no transport function for object 0x%08x", (unsigned long) object);
3736 return NIL; /* bogus return value to satisfy static type checking */
3740 size_lose(lispobj *where)
3742 lose("no size function for object at 0x%08x", (unsigned long) where);
3743 return 1; /* bogus return value to satisfy static type checking */
3747 gc_init_tables(void)
3751 /* Set default value in all slots of scavenge table. */
3752 for (i = 0; i < 256; i++) { /* FIXME: bare constant length, ick! */
3753 scavtab[i] = scav_lose;
3756 /* For each type which can be selected by the low 3 bits of the tag
3757 * alone, set multiple entries in our 8-bit scavenge table (one for each
3758 * possible value of the high 5 bits). */
3759 for (i = 0; i < 32; i++) { /* FIXME: bare constant length, ick! */
3760 scavtab[type_EvenFixnum|(i<<3)] = scav_immediate;
3761 scavtab[type_FunctionPointer|(i<<3)] = scav_function_pointer;
3762 /* OtherImmediate0 */
3763 scavtab[type_ListPointer|(i<<3)] = scav_list_pointer;
3764 scavtab[type_OddFixnum|(i<<3)] = scav_immediate;
3765 scavtab[type_InstancePointer|(i<<3)] = scav_instance_pointer;
3766 /* OtherImmediate1 */
3767 scavtab[type_OtherPointer|(i<<3)] = scav_other_pointer;
3770 /* Other-pointer types (those selected by all eight bits of the tag) get
3771 * one entry each in the scavenge table. */
3772 scavtab[type_Bignum] = scav_unboxed;
3773 scavtab[type_Ratio] = scav_boxed;
3774 scavtab[type_SingleFloat] = scav_unboxed;
3775 scavtab[type_DoubleFloat] = scav_unboxed;
3776 #ifdef type_LongFloat
3777 scavtab[type_LongFloat] = scav_unboxed;
3779 scavtab[type_Complex] = scav_boxed;
3780 #ifdef type_ComplexSingleFloat
3781 scavtab[type_ComplexSingleFloat] = scav_unboxed;
3783 #ifdef type_ComplexDoubleFloat
3784 scavtab[type_ComplexDoubleFloat] = scav_unboxed;
3786 #ifdef type_ComplexLongFloat
3787 scavtab[type_ComplexLongFloat] = scav_unboxed;
3789 scavtab[type_SimpleArray] = scav_boxed;
3790 scavtab[type_SimpleString] = scav_string;
3791 scavtab[type_SimpleBitVector] = scav_vector_bit;
3792 scavtab[type_SimpleVector] = scav_vector;
3793 scavtab[type_SimpleArrayUnsignedByte2] = scav_vector_unsigned_byte_2;
3794 scavtab[type_SimpleArrayUnsignedByte4] = scav_vector_unsigned_byte_4;
3795 scavtab[type_SimpleArrayUnsignedByte8] = scav_vector_unsigned_byte_8;
3796 scavtab[type_SimpleArrayUnsignedByte16] = scav_vector_unsigned_byte_16;
3797 scavtab[type_SimpleArrayUnsignedByte32] = scav_vector_unsigned_byte_32;
3798 #ifdef type_SimpleArraySignedByte8
3799 scavtab[type_SimpleArraySignedByte8] = scav_vector_unsigned_byte_8;
3801 #ifdef type_SimpleArraySignedByte16
3802 scavtab[type_SimpleArraySignedByte16] = scav_vector_unsigned_byte_16;
3804 #ifdef type_SimpleArraySignedByte30
3805 scavtab[type_SimpleArraySignedByte30] = scav_vector_unsigned_byte_32;
3807 #ifdef type_SimpleArraySignedByte32
3808 scavtab[type_SimpleArraySignedByte32] = scav_vector_unsigned_byte_32;
3810 scavtab[type_SimpleArraySingleFloat] = scav_vector_single_float;
3811 scavtab[type_SimpleArrayDoubleFloat] = scav_vector_double_float;
3812 #ifdef type_SimpleArrayLongFloat
3813 scavtab[type_SimpleArrayLongFloat] = scav_vector_long_float;
3815 #ifdef type_SimpleArrayComplexSingleFloat
3816 scavtab[type_SimpleArrayComplexSingleFloat] = scav_vector_complex_single_float;
3818 #ifdef type_SimpleArrayComplexDoubleFloat
3819 scavtab[type_SimpleArrayComplexDoubleFloat] = scav_vector_complex_double_float;
3821 #ifdef type_SimpleArrayComplexLongFloat
3822 scavtab[type_SimpleArrayComplexLongFloat] = scav_vector_complex_long_float;
3824 scavtab[type_ComplexString] = scav_boxed;
3825 scavtab[type_ComplexBitVector] = scav_boxed;
3826 scavtab[type_ComplexVector] = scav_boxed;
3827 scavtab[type_ComplexArray] = scav_boxed;
3828 scavtab[type_CodeHeader] = scav_code_header;
3829 /*scavtab[type_FunctionHeader] = scav_function_header;*/
3830 /*scavtab[type_ClosureFunctionHeader] = scav_function_header;*/
3831 /*scavtab[type_ReturnPcHeader] = scav_return_pc_header;*/
3833 scavtab[type_ClosureHeader] = scav_closure_header;
3834 scavtab[type_FuncallableInstanceHeader] = scav_closure_header;
3835 scavtab[type_ByteCodeFunction] = scav_closure_header;
3836 scavtab[type_ByteCodeClosure] = scav_closure_header;
3838 scavtab[type_ClosureHeader] = scav_boxed;
3839 scavtab[type_FuncallableInstanceHeader] = scav_boxed;
3840 scavtab[type_ByteCodeFunction] = scav_boxed;
3841 scavtab[type_ByteCodeClosure] = scav_boxed;
3843 scavtab[type_ValueCellHeader] = scav_boxed;
3844 scavtab[type_SymbolHeader] = scav_boxed;
3845 scavtab[type_BaseChar] = scav_immediate;
3846 scavtab[type_Sap] = scav_unboxed;
3847 scavtab[type_UnboundMarker] = scav_immediate;
3848 scavtab[type_WeakPointer] = scav_weak_pointer;
3849 scavtab[type_InstanceHeader] = scav_boxed;
3850 scavtab[type_Fdefn] = scav_fdefn;
3852 /* transport other table, initialized same way as scavtab */
3853 for (i = 0; i < 256; i++)
3854 transother[i] = trans_lose;
3855 transother[type_Bignum] = trans_unboxed;
3856 transother[type_Ratio] = trans_boxed;
3857 transother[type_SingleFloat] = trans_unboxed;
3858 transother[type_DoubleFloat] = trans_unboxed;
3859 #ifdef type_LongFloat
3860 transother[type_LongFloat] = trans_unboxed;
3862 transother[type_Complex] = trans_boxed;
3863 #ifdef type_ComplexSingleFloat
3864 transother[type_ComplexSingleFloat] = trans_unboxed;
3866 #ifdef type_ComplexDoubleFloat
3867 transother[type_ComplexDoubleFloat] = trans_unboxed;
3869 #ifdef type_ComplexLongFloat
3870 transother[type_ComplexLongFloat] = trans_unboxed;
3872 transother[type_SimpleArray] = trans_boxed_large;
3873 transother[type_SimpleString] = trans_string;
3874 transother[type_SimpleBitVector] = trans_vector_bit;
3875 transother[type_SimpleVector] = trans_vector;
3876 transother[type_SimpleArrayUnsignedByte2] = trans_vector_unsigned_byte_2;
3877 transother[type_SimpleArrayUnsignedByte4] = trans_vector_unsigned_byte_4;
3878 transother[type_SimpleArrayUnsignedByte8] = trans_vector_unsigned_byte_8;
3879 transother[type_SimpleArrayUnsignedByte16] = trans_vector_unsigned_byte_16;
3880 transother[type_SimpleArrayUnsignedByte32] = trans_vector_unsigned_byte_32;
3881 #ifdef type_SimpleArraySignedByte8
3882 transother[type_SimpleArraySignedByte8] = trans_vector_unsigned_byte_8;
3884 #ifdef type_SimpleArraySignedByte16
3885 transother[type_SimpleArraySignedByte16] = trans_vector_unsigned_byte_16;
3887 #ifdef type_SimpleArraySignedByte30
3888 transother[type_SimpleArraySignedByte30] = trans_vector_unsigned_byte_32;
3890 #ifdef type_SimpleArraySignedByte32
3891 transother[type_SimpleArraySignedByte32] = trans_vector_unsigned_byte_32;
3893 transother[type_SimpleArraySingleFloat] = trans_vector_single_float;
3894 transother[type_SimpleArrayDoubleFloat] = trans_vector_double_float;
3895 #ifdef type_SimpleArrayLongFloat
3896 transother[type_SimpleArrayLongFloat] = trans_vector_long_float;
3898 #ifdef type_SimpleArrayComplexSingleFloat
3899 transother[type_SimpleArrayComplexSingleFloat] = trans_vector_complex_single_float;
3901 #ifdef type_SimpleArrayComplexDoubleFloat
3902 transother[type_SimpleArrayComplexDoubleFloat] = trans_vector_complex_double_float;
3904 #ifdef type_SimpleArrayComplexLongFloat
3905 transother[type_SimpleArrayComplexLongFloat] = trans_vector_complex_long_float;
3907 transother[type_ComplexString] = trans_boxed;
3908 transother[type_ComplexBitVector] = trans_boxed;
3909 transother[type_ComplexVector] = trans_boxed;
3910 transother[type_ComplexArray] = trans_boxed;
3911 transother[type_CodeHeader] = trans_code_header;
3912 transother[type_FunctionHeader] = trans_function_header;
3913 transother[type_ClosureFunctionHeader] = trans_function_header;
3914 transother[type_ReturnPcHeader] = trans_return_pc_header;
3915 transother[type_ClosureHeader] = trans_boxed;
3916 transother[type_FuncallableInstanceHeader] = trans_boxed;
3917 transother[type_ByteCodeFunction] = trans_boxed;
3918 transother[type_ByteCodeClosure] = trans_boxed;
3919 transother[type_ValueCellHeader] = trans_boxed;
3920 transother[type_SymbolHeader] = trans_boxed;
3921 transother[type_BaseChar] = trans_immediate;
3922 transother[type_Sap] = trans_unboxed;
3923 transother[type_UnboundMarker] = trans_immediate;
3924 transother[type_WeakPointer] = trans_weak_pointer;
3925 transother[type_InstanceHeader] = trans_boxed;
3926 transother[type_Fdefn] = trans_boxed;
3928 /* size table, initialized the same way as scavtab */
3929 for (i = 0; i < 256; i++)
3930 sizetab[i] = size_lose;
3931 for (i = 0; i < 32; i++) {
3932 sizetab[type_EvenFixnum|(i<<3)] = size_immediate;
3933 sizetab[type_FunctionPointer|(i<<3)] = size_pointer;
3934 /* OtherImmediate0 */
3935 sizetab[type_ListPointer|(i<<3)] = size_pointer;
3936 sizetab[type_OddFixnum|(i<<3)] = size_immediate;
3937 sizetab[type_InstancePointer|(i<<3)] = size_pointer;
3938 /* OtherImmediate1 */
3939 sizetab[type_OtherPointer|(i<<3)] = size_pointer;
3941 sizetab[type_Bignum] = size_unboxed;
3942 sizetab[type_Ratio] = size_boxed;
3943 sizetab[type_SingleFloat] = size_unboxed;
3944 sizetab[type_DoubleFloat] = size_unboxed;
3945 #ifdef type_LongFloat
3946 sizetab[type_LongFloat] = size_unboxed;
3948 sizetab[type_Complex] = size_boxed;
3949 #ifdef type_ComplexSingleFloat
3950 sizetab[type_ComplexSingleFloat] = size_unboxed;
3952 #ifdef type_ComplexDoubleFloat
3953 sizetab[type_ComplexDoubleFloat] = size_unboxed;
3955 #ifdef type_ComplexLongFloat
3956 sizetab[type_ComplexLongFloat] = size_unboxed;
3958 sizetab[type_SimpleArray] = size_boxed;
3959 sizetab[type_SimpleString] = size_string;
3960 sizetab[type_SimpleBitVector] = size_vector_bit;
3961 sizetab[type_SimpleVector] = size_vector;
3962 sizetab[type_SimpleArrayUnsignedByte2] = size_vector_unsigned_byte_2;
3963 sizetab[type_SimpleArrayUnsignedByte4] = size_vector_unsigned_byte_4;
3964 sizetab[type_SimpleArrayUnsignedByte8] = size_vector_unsigned_byte_8;
3965 sizetab[type_SimpleArrayUnsignedByte16] = size_vector_unsigned_byte_16;
3966 sizetab[type_SimpleArrayUnsignedByte32] = size_vector_unsigned_byte_32;
3967 #ifdef type_SimpleArraySignedByte8
3968 sizetab[type_SimpleArraySignedByte8] = size_vector_unsigned_byte_8;
3970 #ifdef type_SimpleArraySignedByte16
3971 sizetab[type_SimpleArraySignedByte16] = size_vector_unsigned_byte_16;
3973 #ifdef type_SimpleArraySignedByte30
3974 sizetab[type_SimpleArraySignedByte30] = size_vector_unsigned_byte_32;
3976 #ifdef type_SimpleArraySignedByte32
3977 sizetab[type_SimpleArraySignedByte32] = size_vector_unsigned_byte_32;
3979 sizetab[type_SimpleArraySingleFloat] = size_vector_single_float;
3980 sizetab[type_SimpleArrayDoubleFloat] = size_vector_double_float;
3981 #ifdef type_SimpleArrayLongFloat
3982 sizetab[type_SimpleArrayLongFloat] = size_vector_long_float;
3984 #ifdef type_SimpleArrayComplexSingleFloat
3985 sizetab[type_SimpleArrayComplexSingleFloat] = size_vector_complex_single_float;
3987 #ifdef type_SimpleArrayComplexDoubleFloat
3988 sizetab[type_SimpleArrayComplexDoubleFloat] = size_vector_complex_double_float;
3990 #ifdef type_SimpleArrayComplexLongFloat
3991 sizetab[type_SimpleArrayComplexLongFloat] = size_vector_complex_long_float;
3993 sizetab[type_ComplexString] = size_boxed;
3994 sizetab[type_ComplexBitVector] = size_boxed;
3995 sizetab[type_ComplexVector] = size_boxed;
3996 sizetab[type_ComplexArray] = size_boxed;
3997 sizetab[type_CodeHeader] = size_code_header;
3999 /* We shouldn't see these, so just lose if it happens. */
4000 sizetab[type_FunctionHeader] = size_function_header;
4001 sizetab[type_ClosureFunctionHeader] = size_function_header;
4002 sizetab[type_ReturnPcHeader] = size_return_pc_header;
4004 sizetab[type_ClosureHeader] = size_boxed;
4005 sizetab[type_FuncallableInstanceHeader] = size_boxed;
4006 sizetab[type_ValueCellHeader] = size_boxed;
4007 sizetab[type_SymbolHeader] = size_boxed;
4008 sizetab[type_BaseChar] = size_immediate;
4009 sizetab[type_Sap] = size_unboxed;
4010 sizetab[type_UnboundMarker] = size_immediate;
4011 sizetab[type_WeakPointer] = size_weak_pointer;
4012 sizetab[type_InstanceHeader] = size_boxed;
4013 sizetab[type_Fdefn] = size_boxed;
4016 /* Scan an area looking for an object which encloses the given pointer.
4017 * Return the object start on success or NULL on failure. */
4019 search_space(lispobj *start, size_t words, lispobj *pointer)
4023 lispobj thing = *start;
4025 /* If thing is an immediate then this is a cons */
4027 || ((thing & 3) == 0) /* fixnum */
4028 || (TypeOf(thing) == type_BaseChar)
4029 || (TypeOf(thing) == type_UnboundMarker))
4032 count = (sizetab[TypeOf(thing)])(start);
4034 /* Check whether the pointer is within this object? */
4035 if ((pointer >= start) && (pointer < (start+count))) {
4037 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
4041 /* Round up the count */
4042 count = CEILING(count,2);
4051 search_read_only_space(lispobj *pointer)
4053 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
4054 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
4055 if ((pointer < start) || (pointer >= end))
4057 return (search_space(start, (pointer+2)-start, pointer));
4061 search_static_space(lispobj *pointer)
4063 lispobj* start = (lispobj*)STATIC_SPACE_START;
4064 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER);
4065 if ((pointer < start) || (pointer >= end))
4067 return (search_space(start, (pointer+2)-start, pointer));
4070 /* a faster version for searching the dynamic space. This will work even
4071 * if the object is in a current allocation region. */
4073 search_dynamic_space(lispobj *pointer)
4075 int page_index = find_page_index(pointer);
4078 /* Address may be invalid - do some checks. */
4079 if ((page_index == -1) || (page_table[page_index].allocated == FREE_PAGE))
4081 start = (lispobj *)((void *)page_address(page_index)
4082 + page_table[page_index].first_object_offset);
4083 return (search_space(start, (pointer+2)-start, pointer));
4086 /* FIXME: There is a strong family resemblance between this function
4087 * and the function of the same name in purify.c. Would it be possible
4088 * to implement them as exactly the same function? */
4090 valid_dynamic_space_pointer(lispobj *pointer)
4092 lispobj *start_addr;
4094 /* Find the object start address */
4095 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
4099 /* We need to allow raw pointers into Code objects for return
4100 * addresses. This will also pickup pointers to functions in code
4102 if (TypeOf(*start_addr) == type_CodeHeader) {
4103 /* X Could do some further checks here. */
4107 /* If it's not a return address then it needs to be a valid Lisp
4109 if (!Pointerp((lispobj)pointer)) {
4113 /* Check that the object pointed to is consistent with the pointer
4115 switch (LowtagOf((lispobj)pointer)) {
4116 case type_FunctionPointer:
4117 /* Start_addr should be the enclosing code object, or a closure
4119 switch (TypeOf(*start_addr)) {
4120 case type_CodeHeader:
4121 /* This case is probably caught above. */
4123 case type_ClosureHeader:
4124 case type_FuncallableInstanceHeader:
4125 case type_ByteCodeFunction:
4126 case type_ByteCodeClosure:
4127 if ((unsigned)pointer !=
4128 ((unsigned)start_addr+type_FunctionPointer)) {
4132 pointer, start_addr, *start_addr));
4140 pointer, start_addr, *start_addr));
4144 case type_ListPointer:
4145 if ((unsigned)pointer !=
4146 ((unsigned)start_addr+type_ListPointer)) {
4150 pointer, start_addr, *start_addr));
4153 /* Is it plausible cons? */
4154 if ((Pointerp(start_addr[0])
4155 || ((start_addr[0] & 3) == 0) /* fixnum */
4156 || (TypeOf(start_addr[0]) == type_BaseChar)
4157 || (TypeOf(start_addr[0]) == type_UnboundMarker))
4158 && (Pointerp(start_addr[1])
4159 || ((start_addr[1] & 3) == 0) /* fixnum */
4160 || (TypeOf(start_addr[1]) == type_BaseChar)
4161 || (TypeOf(start_addr[1]) == type_UnboundMarker)))
4167 pointer, start_addr, *start_addr));
4170 case type_InstancePointer:
4171 if ((unsigned)pointer !=
4172 ((unsigned)start_addr+type_InstancePointer)) {
4176 pointer, start_addr, *start_addr));
4179 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
4183 pointer, start_addr, *start_addr));
4187 case type_OtherPointer:
4188 if ((unsigned)pointer !=
4189 ((int)start_addr+type_OtherPointer)) {
4193 pointer, start_addr, *start_addr));
4196 /* Is it plausible? Not a cons. X should check the headers. */
4197 if (Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
4201 pointer, start_addr, *start_addr));
4204 switch (TypeOf(start_addr[0])) {
4205 case type_UnboundMarker:
4210 pointer, start_addr, *start_addr));
4213 /* only pointed to by function pointers? */
4214 case type_ClosureHeader:
4215 case type_FuncallableInstanceHeader:
4216 case type_ByteCodeFunction:
4217 case type_ByteCodeClosure:
4221 pointer, start_addr, *start_addr));
4224 case type_InstanceHeader:
4228 pointer, start_addr, *start_addr));
4231 /* the valid other immediate pointer objects */
4232 case type_SimpleVector:
4235 #ifdef type_ComplexSingleFloat
4236 case type_ComplexSingleFloat:
4238 #ifdef type_ComplexDoubleFloat
4239 case type_ComplexDoubleFloat:
4241 #ifdef type_ComplexLongFloat
4242 case type_ComplexLongFloat:
4244 case type_SimpleArray:
4245 case type_ComplexString:
4246 case type_ComplexBitVector:
4247 case type_ComplexVector:
4248 case type_ComplexArray:
4249 case type_ValueCellHeader:
4250 case type_SymbolHeader:
4252 case type_CodeHeader:
4254 case type_SingleFloat:
4255 case type_DoubleFloat:
4256 #ifdef type_LongFloat
4257 case type_LongFloat:
4259 case type_SimpleString:
4260 case type_SimpleBitVector:
4261 case type_SimpleArrayUnsignedByte2:
4262 case type_SimpleArrayUnsignedByte4:
4263 case type_SimpleArrayUnsignedByte8:
4264 case type_SimpleArrayUnsignedByte16:
4265 case type_SimpleArrayUnsignedByte32:
4266 #ifdef type_SimpleArraySignedByte8
4267 case type_SimpleArraySignedByte8:
4269 #ifdef type_SimpleArraySignedByte16
4270 case type_SimpleArraySignedByte16:
4272 #ifdef type_SimpleArraySignedByte30
4273 case type_SimpleArraySignedByte30:
4275 #ifdef type_SimpleArraySignedByte32
4276 case type_SimpleArraySignedByte32:
4278 case type_SimpleArraySingleFloat:
4279 case type_SimpleArrayDoubleFloat:
4280 #ifdef type_SimpleArrayLongFloat
4281 case type_SimpleArrayLongFloat:
4283 #ifdef type_SimpleArrayComplexSingleFloat
4284 case type_SimpleArrayComplexSingleFloat:
4286 #ifdef type_SimpleArrayComplexDoubleFloat
4287 case type_SimpleArrayComplexDoubleFloat:
4289 #ifdef type_SimpleArrayComplexLongFloat
4290 case type_SimpleArrayComplexLongFloat:
4293 case type_WeakPointer:
4300 pointer, start_addr, *start_addr));
4308 pointer, start_addr, *start_addr));
4316 /* Adjust large bignum and vector objects. This will adjust the allocated
4317 * region if the size has shrunk, and move unboxed objects into unboxed
4318 * pages. The pages are not promoted here, and the promoted region is not
4319 * added to the new_regions; this is really only designed to be called from
4320 * preserve_pointer. Shouldn't fail if this is missed, just may delay the
4321 * moving of objects to unboxed pages, and the freeing of pages. */
4323 maybe_adjust_large_object(lispobj *where)
4328 int remaining_bytes;
4335 /* Check whether it's a vector or bignum object. */
4336 switch (TypeOf(where[0])) {
4337 case type_SimpleVector:
4341 case type_SimpleString:
4342 case type_SimpleBitVector:
4343 case type_SimpleArrayUnsignedByte2:
4344 case type_SimpleArrayUnsignedByte4:
4345 case type_SimpleArrayUnsignedByte8:
4346 case type_SimpleArrayUnsignedByte16:
4347 case type_SimpleArrayUnsignedByte32:
4348 #ifdef type_SimpleArraySignedByte8
4349 case type_SimpleArraySignedByte8:
4351 #ifdef type_SimpleArraySignedByte16
4352 case type_SimpleArraySignedByte16:
4354 #ifdef type_SimpleArraySignedByte30
4355 case type_SimpleArraySignedByte30:
4357 #ifdef type_SimpleArraySignedByte32
4358 case type_SimpleArraySignedByte32:
4360 case type_SimpleArraySingleFloat:
4361 case type_SimpleArrayDoubleFloat:
4362 #ifdef type_SimpleArrayLongFloat
4363 case type_SimpleArrayLongFloat:
4365 #ifdef type_SimpleArrayComplexSingleFloat
4366 case type_SimpleArrayComplexSingleFloat:
4368 #ifdef type_SimpleArrayComplexDoubleFloat
4369 case type_SimpleArrayComplexDoubleFloat:
4371 #ifdef type_SimpleArrayComplexLongFloat
4372 case type_SimpleArrayComplexLongFloat:
4374 boxed = UNBOXED_PAGE;
4380 /* Find its current size. */
4381 nwords = (sizetab[TypeOf(where[0])])(where);
4383 first_page = find_page_index((void *)where);
4384 gc_assert(first_page >= 0);
4386 /* Note: Any page write-protection must be removed, else a later
4387 * scavenge_newspace may incorrectly not scavenge these pages.
4388 * This would not be necessary if they are added to the new areas,
4389 * but lets do it for them all (they'll probably be written
4392 gc_assert(page_table[first_page].first_object_offset == 0);
4394 next_page = first_page;
4395 remaining_bytes = nwords*4;
4396 while (remaining_bytes > 4096) {
4397 gc_assert(page_table[next_page].gen == from_space);
4398 gc_assert((page_table[next_page].allocated == BOXED_PAGE)
4399 || (page_table[next_page].allocated == UNBOXED_PAGE));
4400 gc_assert(page_table[next_page].large_object);
4401 gc_assert(page_table[next_page].first_object_offset ==
4402 -4096*(next_page-first_page));
4403 gc_assert(page_table[next_page].bytes_used == 4096);
4405 page_table[next_page].allocated = boxed;
4407 /* Shouldn't be write-protected at this stage. Essential that the
4409 gc_assert(!page_table[next_page].write_protected);
4410 remaining_bytes -= 4096;
4414 /* Now only one page remains, but the object may have shrunk so
4415 * there may be more unused pages which will be freed. */
4417 /* Object may have shrunk but shouldn't have grown - check. */
4418 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
4420 page_table[next_page].allocated = boxed;
4421 gc_assert(page_table[next_page].allocated ==
4422 page_table[first_page].allocated);
4424 /* Adjust the bytes_used. */
4425 old_bytes_used = page_table[next_page].bytes_used;
4426 page_table[next_page].bytes_used = remaining_bytes;
4428 bytes_freed = old_bytes_used - remaining_bytes;
4430 /* Free any remaining pages; needs care. */
4432 while ((old_bytes_used == 4096) &&
4433 (page_table[next_page].gen == from_space) &&
4434 ((page_table[next_page].allocated == UNBOXED_PAGE)
4435 || (page_table[next_page].allocated == BOXED_PAGE)) &&
4436 page_table[next_page].large_object &&
4437 (page_table[next_page].first_object_offset ==
4438 -(next_page - first_page)*4096)) {
4439 /* It checks out OK, free the page. We don't need to both zeroing
4440 * pages as this should have been done before shrinking the
4441 * object. These pages shouldn't be write protected as they
4442 * should be zero filled. */
4443 gc_assert(page_table[next_page].write_protected == 0);
4445 old_bytes_used = page_table[next_page].bytes_used;
4446 page_table[next_page].allocated = FREE_PAGE;
4447 page_table[next_page].bytes_used = 0;
4448 bytes_freed += old_bytes_used;
4452 if ((bytes_freed > 0) && gencgc_verbose)
4453 FSHOW((stderr, "/adjust_large_object freed %d\n", bytes_freed));
4455 generations[from_space].bytes_allocated -= bytes_freed;
4456 bytes_allocated -= bytes_freed;
4461 /* Take a possible pointer to a list object and mark the page_table
4462 * so that it will not need changing during a GC.
4464 * This involves locating the page it points to, then backing up to
4465 * the first page that has its first object start at offset 0, and
4466 * then marking all pages dont_move from the first until a page that ends
4467 * by being full, or having free gen.
4469 * This ensures that objects spanning pages are not broken.
4471 * It is assumed that all the page static flags have been cleared at
4472 * the start of a GC.
4474 * It is also assumed that the current gc_alloc region has been flushed and
4475 * the tables updated. */
4477 preserve_pointer(void *addr)
4479 int addr_page_index = find_page_index(addr);
4482 unsigned region_allocation;
4484 /* Address is quite likely to have been invalid - do some checks. */
4485 if ((addr_page_index == -1)
4486 || (page_table[addr_page_index].allocated == FREE_PAGE)
4487 || (page_table[addr_page_index].bytes_used == 0)
4488 || (page_table[addr_page_index].gen != from_space)
4489 /* Skip if already marked dont_move */
4490 || (page_table[addr_page_index].dont_move != 0))
4493 region_allocation = page_table[addr_page_index].allocated;
4495 /* Check the offset within the page.
4497 * FIXME: The mask should have a symbolic name, and ideally should
4498 * be derived from page size instead of hardwired to 0xfff.
4499 * (Also fix other uses of 0xfff, elsewhere.) */
4500 if (((unsigned)addr & 0xfff) > page_table[addr_page_index].bytes_used)
4503 if (enable_pointer_filter && !valid_dynamic_space_pointer(addr))
4506 /* Work backwards to find a page with a first_object_offset of 0.
4507 * The pages should be contiguous with all bytes used in the same
4508 * gen. Assumes the first_object_offset is negative or zero. */
4509 first_page = addr_page_index;
4510 while (page_table[first_page].first_object_offset != 0) {
4512 /* Do some checks. */
4513 gc_assert(page_table[first_page].bytes_used == 4096);
4514 gc_assert(page_table[first_page].gen == from_space);
4515 gc_assert(page_table[first_page].allocated == region_allocation);
4518 /* Adjust any large objects before promotion as they won't be copied
4519 * after promotion. */
4520 if (page_table[first_page].large_object) {
4521 maybe_adjust_large_object(page_address(first_page));
4522 /* If a large object has shrunk then addr may now point to a free
4523 * area in which case it's ignored here. Note it gets through the
4524 * valid pointer test above because the tail looks like conses. */
4525 if ((page_table[addr_page_index].allocated == FREE_PAGE)
4526 || (page_table[addr_page_index].bytes_used == 0)
4527 /* Check the offset within the page. */
4528 || (((unsigned)addr & 0xfff)
4529 > page_table[addr_page_index].bytes_used)) {
4531 "weird? ignore ptr 0x%x to freed area of large object\n",
4535 /* It may have moved to unboxed pages. */
4536 region_allocation = page_table[first_page].allocated;
4539 /* Now work forward until the end of this contiguous area is found,
4540 * marking all pages as dont_move. */
4541 for (i = first_page; ;i++) {
4542 gc_assert(page_table[i].allocated == region_allocation);
4544 /* Mark the page static. */
4545 page_table[i].dont_move = 1;
4547 /* Move the page to the new_space. XX I'd rather not do this but
4548 * the GC logic is not quite able to copy with the static pages
4549 * remaining in the from space. This also requires the generation
4550 * bytes_allocated counters be updated. */
4551 page_table[i].gen = new_space;
4552 generations[new_space].bytes_allocated += page_table[i].bytes_used;
4553 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
4555 /* It is essential that the pages are not write protected as they
4556 * may have pointers into the old-space which need scavenging. They
4557 * shouldn't be write protected at this stage. */
4558 gc_assert(!page_table[i].write_protected);
4560 /* Check whether this is the last page in this contiguous block.. */
4561 if ((page_table[i].bytes_used < 4096)
4562 /* ..or it is 4096 and is the last in the block */
4563 || (page_table[i+1].allocated == FREE_PAGE)
4564 || (page_table[i+1].bytes_used == 0) /* next page free */
4565 || (page_table[i+1].gen != from_space) /* diff. gen */
4566 || (page_table[i+1].first_object_offset == 0))
4570 /* Check that the page is now static. */
4571 gc_assert(page_table[addr_page_index].dont_move != 0);
4576 #ifdef CONTROL_STACKS
4577 /* Scavenge the thread stack conservative roots. */
4579 scavenge_thread_stacks(void)
4581 lispobj thread_stacks = SymbolValue(CONTROL_STACKS);
4582 int type = TypeOf(thread_stacks);
4584 if (LowtagOf(thread_stacks) == type_OtherPointer) {
4585 struct vector *vector = (struct vector *) PTR(thread_stacks);
4587 if (TypeOf(vector->header) != type_SimpleVector)
4589 length = fixnum_value(vector->length);
4590 for (i = 0; i < length; i++) {
4591 lispobj stack_obj = vector->data[i];
4592 if (LowtagOf(stack_obj) == type_OtherPointer) {
4593 struct vector *stack = (struct vector *) PTR(stack_obj);
4595 if (TypeOf(stack->header) !=
4596 type_SimpleArrayUnsignedByte32) {
4599 vector_length = fixnum_value(stack->length);
4600 if ((gencgc_verbose > 1) && (vector_length <= 0))
4602 "/weird? control stack vector length %d\n",
4604 if (vector_length > 0) {
4605 lispobj *stack_pointer = (lispobj*)stack->data[0];
4606 if ((stack_pointer < (lispobj *)CONTROL_STACK_START) ||
4607 (stack_pointer > (lispobj *)CONTROL_STACK_END))
4608 lose("invalid stack pointer %x",
4609 (unsigned)stack_pointer);
4610 if ((stack_pointer > (lispobj *)CONTROL_STACK_START) &&
4611 (stack_pointer < (lispobj *)CONTROL_STACK_END)) {
4613 * (1) hardwired word length = 4; and as usual,
4614 * when fixing this, check for other places
4615 * with the same problem
4616 * (2) calling it 'length' suggests bytes;
4617 * perhaps 'size' instead? */
4618 unsigned int length = ((unsigned)CONTROL_STACK_END -
4619 (unsigned)stack_pointer) / 4;
4621 if (length >= vector_length) {
4622 lose("invalid stack size %d >= vector length %d",
4626 if (gencgc_verbose > 1) {
4628 "scavenging %d words of control stack %d of length %d words.\n",
4629 length, i, vector_length));
4631 for (j = 0; j < length; j++) {
4632 preserve_pointer((void *)stack->data[1+j]);
4643 /* If the given page is not write-protected, then scan it for pointers
4644 * to younger generations or the top temp. generation, if no
4645 * suspicious pointers are found then the page is write-protected.
4647 * Care is taken to check for pointers to the current gc_alloc region
4648 * if it is a younger generation or the temp. generation. This frees
4649 * the caller from doing a gc_alloc_update_page_tables. Actually the
4650 * gc_alloc_generation does not need to be checked as this is only
4651 * called from scavenge_generation when the gc_alloc generation is
4652 * younger, so it just checks if there is a pointer to the current
4655 * We return 1 if the page was write-protected, else 0.
4658 update_page_write_prot(int page)
4660 int gen = page_table[page].gen;
4663 void **page_addr = (void **)page_address(page);
4664 int num_words = page_table[page].bytes_used / 4;
4666 /* Shouldn't be a free page. */
4667 gc_assert(page_table[page].allocated != FREE_PAGE);
4668 gc_assert(page_table[page].bytes_used != 0);
4670 /* Skip if it's already write-protected or an unboxed page. */
4671 if (page_table[page].write_protected
4672 || (page_table[page].allocated == UNBOXED_PAGE))
4675 /* Scan the page for pointers to younger generations or the
4676 * top temp. generation. */
4678 for (j = 0; j < num_words; j++) {
4679 void *ptr = *(page_addr+j);
4680 int index = find_page_index(ptr);
4682 /* Check that it's in the dynamic space */
4684 if (/* Does it point to a younger or the temp. generation? */
4685 ((page_table[index].allocated != FREE_PAGE)
4686 && (page_table[index].bytes_used != 0)
4687 && ((page_table[index].gen < gen)
4688 || (page_table[index].gen == NUM_GENERATIONS)))
4690 /* Or does it point within a current gc_alloc region? */
4691 || ((boxed_region.start_addr <= ptr)
4692 && (ptr <= boxed_region.free_pointer))
4693 || ((unboxed_region.start_addr <= ptr)
4694 && (ptr <= unboxed_region.free_pointer))) {
4701 /* Write-protect the page. */
4702 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
4704 os_protect((void *)page_addr,
4706 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
4708 /* Note the page as protected in the page tables. */
4709 page_table[page].write_protected = 1;
4715 /* Scavenge a generation.
4717 * This will not resolve all pointers when generation is the new
4718 * space, as new objects may be added which are not check here - use
4719 * scavenge_newspace generation.
4721 * Write-protected pages should not have any pointers to the
4722 * from_space so do need scavenging; thus write-protected pages are
4723 * not always scavenged. There is some code to check that these pages
4724 * are not written; but to check fully the write-protected pages need
4725 * to be scavenged by disabling the code to skip them.
4727 * Under the current scheme when a generation is GCed the younger
4728 * generations will be empty. So, when a generation is being GCed it
4729 * is only necessary to scavenge the older generations for pointers
4730 * not the younger. So a page that does not have pointers to younger
4731 * generations does not need to be scavenged.
4733 * The write-protection can be used to note pages that don't have
4734 * pointers to younger pages. But pages can be written without having
4735 * pointers to younger generations. After the pages are scavenged here
4736 * they can be scanned for pointers to younger generations and if
4737 * there are none the page can be write-protected.
4739 * One complication is when the newspace is the top temp. generation.
4741 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
4742 * that none were written, which they shouldn't be as they should have
4743 * no pointers to younger generations. This breaks down for weak
4744 * pointers as the objects contain a link to the next and are written
4745 * if a weak pointer is scavenged. Still it's a useful check. */
4747 scavenge_generation(int generation)
4754 /* Clear the write_protected_cleared flags on all pages. */
4755 for (i = 0; i < NUM_PAGES; i++)
4756 page_table[i].write_protected_cleared = 0;
4759 for (i = 0; i < last_free_page; i++) {
4760 if ((page_table[i].allocated == BOXED_PAGE)
4761 && (page_table[i].bytes_used != 0)
4762 && (page_table[i].gen == generation)) {
4765 /* This should be the start of a contiguous block. */
4766 gc_assert(page_table[i].first_object_offset == 0);
4768 /* We need to find the full extent of this contiguous
4769 * block in case objects span pages. */
4771 /* Now work forward until the end of this contiguous area
4772 * is found. A small area is preferred as there is a
4773 * better chance of its pages being write-protected. */
4774 for (last_page = i; ;last_page++)
4775 /* Check whether this is the last page in this contiguous
4777 if ((page_table[last_page].bytes_used < 4096)
4778 /* Or it is 4096 and is the last in the block */
4779 || (page_table[last_page+1].allocated != BOXED_PAGE)
4780 || (page_table[last_page+1].bytes_used == 0)
4781 || (page_table[last_page+1].gen != generation)
4782 || (page_table[last_page+1].first_object_offset == 0))
4785 /* Do a limited check for write_protected pages. If all pages
4786 * are write_protected then there is no need to scavenge. */
4789 for (j = i; j <= last_page; j++)
4790 if (page_table[j].write_protected == 0) {
4798 scavenge(page_address(i), (page_table[last_page].bytes_used
4799 + (last_page-i)*4096)/4);
4801 /* Now scan the pages and write protect those
4802 * that don't have pointers to younger
4804 if (enable_page_protection) {
4805 for (j = i; j <= last_page; j++) {
4806 num_wp += update_page_write_prot(j);
4815 if ((gencgc_verbose > 1) && (num_wp != 0)) {
4817 "/write protected %d pages within generation %d\n",
4818 num_wp, generation));
4822 /* Check that none of the write_protected pages in this generation
4823 * have been written to. */
4824 for (i = 0; i < NUM_PAGES; i++) {
4825 if ((page_table[i].allocation ! =FREE_PAGE)
4826 && (page_table[i].bytes_used != 0)
4827 && (page_table[i].gen == generation)
4828 && (page_table[i].write_protected_cleared != 0)) {
4829 FSHOW((stderr, "/scavenge_generation %d\n", generation));
4831 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
4832 page_table[i].bytes_used,
4833 page_table[i].first_object_offset,
4834 page_table[i].dont_move));
4835 lose("write-protected page %d written to in scavenge_generation",
4843 /* Scavenge a newspace generation. As it is scavenged new objects may
4844 * be allocated to it; these will also need to be scavenged. This
4845 * repeats until there are no more objects unscavenged in the
4846 * newspace generation.
4848 * To help improve the efficiency, areas written are recorded by
4849 * gc_alloc and only these scavenged. Sometimes a little more will be
4850 * scavenged, but this causes no harm. An easy check is done that the
4851 * scavenged bytes equals the number allocated in the previous
4854 * Write-protected pages are not scanned except if they are marked
4855 * dont_move in which case they may have been promoted and still have
4856 * pointers to the from space.
4858 * Write-protected pages could potentially be written by alloc however
4859 * to avoid having to handle re-scavenging of write-protected pages
4860 * gc_alloc does not write to write-protected pages.
4862 * New areas of objects allocated are recorded alternatively in the two
4863 * new_areas arrays below. */
4864 static struct new_area new_areas_1[NUM_NEW_AREAS];
4865 static struct new_area new_areas_2[NUM_NEW_AREAS];
4867 /* Do one full scan of the new space generation. This is not enough to
4868 * complete the job as new objects may be added to the generation in
4869 * the process which are not scavenged. */
4871 scavenge_newspace_generation_one_scan(int generation)
4876 "/starting one full scan of newspace generation %d\n",
4879 for (i = 0; i < last_free_page; i++) {
4880 if ((page_table[i].allocated == BOXED_PAGE)
4881 && (page_table[i].bytes_used != 0)
4882 && (page_table[i].gen == generation)
4883 && ((page_table[i].write_protected == 0)
4884 /* (This may be redundant as write_protected is now
4885 * cleared before promotion.) */
4886 || (page_table[i].dont_move == 1))) {
4889 /* The scavenge will start at the first_object_offset of page i.
4891 * We need to find the full extent of this contiguous block in case
4892 * objects span pages.
4894 * Now work forward until the end of this contiguous area is
4895 * found. A small area is preferred as there is a better chance
4896 * of its pages being write-protected. */
4897 for (last_page = i; ;last_page++) {
4898 /* Check whether this is the last page in this contiguous
4900 if ((page_table[last_page].bytes_used < 4096)
4901 /* Or it is 4096 and is the last in the block */
4902 || (page_table[last_page+1].allocated != BOXED_PAGE)
4903 || (page_table[last_page+1].bytes_used == 0)
4904 || (page_table[last_page+1].gen != generation)
4905 || (page_table[last_page+1].first_object_offset == 0))
4909 /* Do a limited check for write_protected pages. If all pages
4910 * are write_protected then no need to scavenge. Except if the
4911 * pages are marked dont_move. */
4914 for (j = i; j <= last_page; j++)
4915 if ((page_table[j].write_protected == 0)
4916 || (page_table[j].dont_move != 0)) {
4926 /* Calculate the size. */
4928 size = (page_table[last_page].bytes_used
4929 - page_table[i].first_object_offset)/4;
4931 size = (page_table[last_page].bytes_used
4932 + (last_page-i)*4096
4933 - page_table[i].first_object_offset)/4;
4937 int a1 = bytes_allocated;
4940 "/scavenge(%x,%d)\n",
4942 + page_table[i].first_object_offset,
4945 new_areas_ignore_page = last_page;
4947 scavenge(page_address(i)+page_table[i].first_object_offset,size);
4950 /* Flush the alloc regions updating the tables. */
4951 gc_alloc_update_page_tables(0, &boxed_region);
4952 gc_alloc_update_page_tables(1, &unboxed_region);
4954 if ((all_wp != 0) && (a1 != bytes_allocated)) {
4956 "alloc'ed over %d to %d\n",
4959 "/page: bytes_used=%d first_object_offset=%d dont_move=%d wp=%d wpc=%d\n",
4960 page_table[i].bytes_used,
4961 page_table[i].first_object_offset,
4962 page_table[i].dont_move,
4963 page_table[i].write_protected,
4964 page_table[i].write_protected_cleared));
4976 /* Do a complete scavenge of the newspace generation. */
4978 scavenge_newspace_generation(int generation)
4982 /* the new_areas array currently being written to by gc_alloc */
4983 struct new_area (*current_new_areas)[] = &new_areas_1;
4984 int current_new_areas_index;
4986 /* the new_areas created but the previous scavenge cycle */
4987 struct new_area (*previous_new_areas)[] = NULL;
4988 int previous_new_areas_index;
4990 #define SC_NS_GEN_CK 0
4992 /* Clear the write_protected_cleared flags on all pages. */
4993 for (i = 0; i < NUM_PAGES; i++)
4994 page_table[i].write_protected_cleared = 0;
4997 /* Flush the current regions updating the tables. */
4998 gc_alloc_update_page_tables(0, &boxed_region);
4999 gc_alloc_update_page_tables(1, &unboxed_region);
5001 /* Turn on the recording of new areas by gc_alloc. */
5002 new_areas = current_new_areas;
5003 new_areas_index = 0;
5005 /* Don't need to record new areas that get scavenged anyway during
5006 * scavenge_newspace_generation_one_scan. */
5007 record_new_objects = 1;
5009 /* Start with a full scavenge. */
5010 scavenge_newspace_generation_one_scan(generation);
5012 /* Record all new areas now. */
5013 record_new_objects = 2;
5015 /* Flush the current regions updating the tables. */
5016 gc_alloc_update_page_tables(0, &boxed_region);
5017 gc_alloc_update_page_tables(1, &unboxed_region);
5019 /* Grab new_areas_index. */
5020 current_new_areas_index = new_areas_index;
5023 "The first scan is finished; current_new_areas_index=%d.\n",
5024 current_new_areas_index));*/
5026 while (current_new_areas_index > 0) {
5027 /* Move the current to the previous new areas */
5028 previous_new_areas = current_new_areas;
5029 previous_new_areas_index = current_new_areas_index;
5031 /* Scavenge all the areas in previous new areas. Any new areas
5032 * allocated are saved in current_new_areas. */
5034 /* Allocate an array for current_new_areas; alternating between
5035 * new_areas_1 and 2 */
5036 if (previous_new_areas == &new_areas_1)
5037 current_new_areas = &new_areas_2;
5039 current_new_areas = &new_areas_1;
5041 /* Set up for gc_alloc. */
5042 new_areas = current_new_areas;
5043 new_areas_index = 0;
5045 /* Check whether previous_new_areas had overflowed. */
5046 if (previous_new_areas_index >= NUM_NEW_AREAS) {
5047 /* New areas of objects allocated have been lost so need to do a
5048 * full scan to be sure! If this becomes a problem try
5049 * increasing NUM_NEW_AREAS. */
5051 SHOW("new_areas overflow, doing full scavenge");
5053 /* Don't need to record new areas that get scavenge anyway
5054 * during scavenge_newspace_generation_one_scan. */
5055 record_new_objects = 1;
5057 scavenge_newspace_generation_one_scan(generation);
5059 /* Record all new areas now. */
5060 record_new_objects = 2;
5062 /* Flush the current regions updating the tables. */
5063 gc_alloc_update_page_tables(0, &boxed_region);
5064 gc_alloc_update_page_tables(1, &unboxed_region);
5066 /* Work through previous_new_areas. */
5067 for (i = 0; i < previous_new_areas_index; i++) {
5068 int page = (*previous_new_areas)[i].page;
5069 int offset = (*previous_new_areas)[i].offset;
5070 int size = (*previous_new_areas)[i].size / 4;
5071 gc_assert((*previous_new_areas)[i].size % 4 == 0);
5073 /* FIXME: All these bare *4 and /4 should be something
5074 * like BYTES_PER_WORD or WBYTES. */
5077 "/S page %d offset %d size %d\n",
5078 page, offset, size*4));*/
5079 scavenge(page_address(page)+offset, size);
5082 /* Flush the current regions updating the tables. */
5083 gc_alloc_update_page_tables(0, &boxed_region);
5084 gc_alloc_update_page_tables(1, &unboxed_region);
5087 current_new_areas_index = new_areas_index;
5090 "The re-scan has finished; current_new_areas_index=%d.\n",
5091 current_new_areas_index));*/
5094 /* Turn off recording of areas allocated by gc_alloc. */
5095 record_new_objects = 0;
5098 /* Check that none of the write_protected pages in this generation
5099 * have been written to. */
5100 for (i = 0; i < NUM_PAGES; i++) {
5101 if ((page_table[i].allocation != FREE_PAGE)
5102 && (page_table[i].bytes_used != 0)
5103 && (page_table[i].gen == generation)
5104 && (page_table[i].write_protected_cleared != 0)
5105 && (page_table[i].dont_move == 0)) {
5106 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
5107 i, generation, page_table[i].dont_move);
5113 /* Un-write-protect all the pages in from_space. This is done at the
5114 * start of a GC else there may be many page faults while scavenging
5115 * the newspace (I've seen drive the system time to 99%). These pages
5116 * would need to be unprotected anyway before unmapping in
5117 * free_oldspace; not sure what effect this has on paging.. */
5119 unprotect_oldspace(void)
5123 for (i = 0; i < last_free_page; i++) {
5124 if ((page_table[i].allocated != FREE_PAGE)
5125 && (page_table[i].bytes_used != 0)
5126 && (page_table[i].gen == from_space)) {
5129 page_start = (void *)page_address(i);
5131 /* Remove any write-protection. We should be able to rely
5132 * on the write-protect flag to avoid redundant calls. */
5133 if (page_table[i].write_protected) {
5134 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5135 page_table[i].write_protected = 0;
5141 /* Work through all the pages and free any in from_space. This
5142 * assumes that all objects have been copied or promoted to an older
5143 * generation. Bytes_allocated and the generation bytes_allocated
5144 * counter are updated. The number of bytes freed is returned. */
5145 extern void i586_bzero(void *addr, int nbytes);
5149 int bytes_freed = 0;
5150 int first_page, last_page;
5155 /* Find a first page for the next region of pages. */
5156 while ((first_page < last_free_page)
5157 && ((page_table[first_page].allocated == FREE_PAGE)
5158 || (page_table[first_page].bytes_used == 0)
5159 || (page_table[first_page].gen != from_space)))
5162 if (first_page >= last_free_page)
5165 /* Find the last page of this region. */
5166 last_page = first_page;
5169 /* Free the page. */
5170 bytes_freed += page_table[last_page].bytes_used;
5171 generations[page_table[last_page].gen].bytes_allocated -=
5172 page_table[last_page].bytes_used;
5173 page_table[last_page].allocated = FREE_PAGE;
5174 page_table[last_page].bytes_used = 0;
5176 /* Remove any write-protection. We should be able to rely
5177 * on the write-protect flag to avoid redundant calls. */
5179 void *page_start = (void *)page_address(last_page);
5181 if (page_table[last_page].write_protected) {
5182 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5183 page_table[last_page].write_protected = 0;
5188 while ((last_page < last_free_page)
5189 && (page_table[last_page].allocated != FREE_PAGE)
5190 && (page_table[last_page].bytes_used != 0)
5191 && (page_table[last_page].gen == from_space));
5193 /* Zero pages from first_page to (last_page-1).
5195 * FIXME: Why not use os_zero(..) function instead of
5196 * hand-coding this again? (Check other gencgc_unmap_zero
5198 if (gencgc_unmap_zero) {
5199 void *page_start, *addr;
5201 page_start = (void *)page_address(first_page);
5203 os_invalidate(page_start, 4096*(last_page-first_page));
5204 addr = os_validate(page_start, 4096*(last_page-first_page));
5205 if (addr == NULL || addr != page_start) {
5206 /* Is this an error condition? I couldn't really tell from
5207 * the old CMU CL code, which fprintf'ed a message with
5208 * an exclamation point at the end. But I've never seen the
5209 * message, so it must at least be unusual..
5211 * (The same condition is also tested for in gc_free_heap.)
5213 * -- WHN 19991129 */
5214 lose("i586_bzero: page moved, 0x%08x ==> 0x%08x",
5221 page_start = (int *)page_address(first_page);
5222 i586_bzero(page_start, 4096*(last_page-first_page));
5225 first_page = last_page;
5227 } while (first_page < last_free_page);
5229 bytes_allocated -= bytes_freed;
5233 /* Print some information about a pointer at the given address. */
5235 print_ptr(lispobj *addr)
5237 /* If addr is in the dynamic space then out the page information. */
5238 int pi1 = find_page_index((void*)addr);
5241 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
5242 (unsigned int) addr,
5244 page_table[pi1].allocated,
5245 page_table[pi1].gen,
5246 page_table[pi1].bytes_used,
5247 page_table[pi1].first_object_offset,
5248 page_table[pi1].dont_move);
5249 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
5261 extern int undefined_tramp;
5264 verify_space(lispobj *start, size_t words)
5266 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
5267 int is_in_readonly_space =
5268 (READ_ONLY_SPACE_START <= (unsigned)start &&
5269 (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5273 lispobj thing = *(lispobj*)start;
5275 if (Pointerp(thing)) {
5276 int page_index = find_page_index((void*)thing);
5277 int to_readonly_space =
5278 (READ_ONLY_SPACE_START <= thing &&
5279 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5280 int to_static_space =
5281 (STATIC_SPACE_START <= thing &&
5282 thing < SymbolValue(STATIC_SPACE_FREE_POINTER));
5284 /* Does it point to the dynamic space? */
5285 if (page_index != -1) {
5286 /* If it's within the dynamic space it should point to a used
5287 * page. XX Could check the offset too. */
5288 if ((page_table[page_index].allocated != FREE_PAGE)
5289 && (page_table[page_index].bytes_used == 0))
5290 lose ("Ptr %x @ %x sees free page.", thing, start);
5291 /* Check that it doesn't point to a forwarding pointer! */
5292 if (*((lispobj *)PTR(thing)) == 0x01) {
5293 lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
5295 /* Check that its not in the RO space as it would then be a
5296 * pointer from the RO to the dynamic space. */
5297 if (is_in_readonly_space) {
5298 lose("ptr to dynamic space %x from RO space %x",
5301 /* Does it point to a plausible object? This check slows
5302 * it down a lot (so it's commented out).
5304 * FIXME: Add a variable to enable this dynamically. */
5305 /* if (!valid_dynamic_space_pointer((lispobj *)thing)) {
5306 * lose("ptr %x to invalid object %x", thing, start); */
5308 /* Verify that it points to another valid space. */
5309 if (!to_readonly_space && !to_static_space
5310 && (thing != (unsigned)&undefined_tramp)) {
5311 lose("Ptr %x @ %x sees junk.", thing, start);
5315 if (thing & 0x3) { /* Skip fixnums. FIXME: There should be an
5316 * is_fixnum for this. */
5318 switch(TypeOf(*start)) {
5321 case type_SimpleVector:
5324 case type_SimpleArray:
5325 case type_ComplexString:
5326 case type_ComplexBitVector:
5327 case type_ComplexVector:
5328 case type_ComplexArray:
5329 case type_ClosureHeader:
5330 case type_FuncallableInstanceHeader:
5331 case type_ByteCodeFunction:
5332 case type_ByteCodeClosure:
5333 case type_ValueCellHeader:
5334 case type_SymbolHeader:
5336 case type_UnboundMarker:
5337 case type_InstanceHeader:
5342 case type_CodeHeader:
5344 lispobj object = *start;
5346 int nheader_words, ncode_words, nwords;
5348 struct function *fheaderp;
5350 code = (struct code *) start;
5352 /* Check that it's not in the dynamic space.
5353 * FIXME: Isn't is supposed to be OK for code
5354 * objects to be in the dynamic space these days? */
5355 if (is_in_dynamic_space
5356 /* It's ok if it's byte compiled code. The trace
5357 * table offset will be a fixnum if it's x86
5358 * compiled code - check. */
5359 && !(code->trace_table_offset & 0x3)
5360 /* Only when enabled */
5361 && verify_dynamic_code_check) {
5363 "/code object at %x in the dynamic space\n",
5367 ncode_words = fixnum_value(code->code_size);
5368 nheader_words = HeaderValue(object);
5369 nwords = ncode_words + nheader_words;
5370 nwords = CEILING(nwords, 2);
5371 /* Scavenge the boxed section of the code data block */
5372 verify_space(start + 1, nheader_words - 1);
5374 /* Scavenge the boxed section of each function object in
5375 * the code data block. */
5376 fheaderl = code->entry_points;
5377 while (fheaderl != NIL) {
5378 fheaderp = (struct function *) PTR(fheaderl);
5379 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
5380 verify_space(&fheaderp->name, 1);
5381 verify_space(&fheaderp->arglist, 1);
5382 verify_space(&fheaderp->type, 1);
5383 fheaderl = fheaderp->next;
5389 /* unboxed objects */
5391 case type_SingleFloat:
5392 case type_DoubleFloat:
5393 #ifdef type_ComplexLongFloat
5394 case type_LongFloat:
5396 #ifdef type_ComplexSingleFloat
5397 case type_ComplexSingleFloat:
5399 #ifdef type_ComplexDoubleFloat
5400 case type_ComplexDoubleFloat:
5402 #ifdef type_ComplexLongFloat
5403 case type_ComplexLongFloat:
5405 case type_SimpleString:
5406 case type_SimpleBitVector:
5407 case type_SimpleArrayUnsignedByte2:
5408 case type_SimpleArrayUnsignedByte4:
5409 case type_SimpleArrayUnsignedByte8:
5410 case type_SimpleArrayUnsignedByte16:
5411 case type_SimpleArrayUnsignedByte32:
5412 #ifdef type_SimpleArraySignedByte8
5413 case type_SimpleArraySignedByte8:
5415 #ifdef type_SimpleArraySignedByte16
5416 case type_SimpleArraySignedByte16:
5418 #ifdef type_SimpleArraySignedByte30
5419 case type_SimpleArraySignedByte30:
5421 #ifdef type_SimpleArraySignedByte32
5422 case type_SimpleArraySignedByte32:
5424 case type_SimpleArraySingleFloat:
5425 case type_SimpleArrayDoubleFloat:
5426 #ifdef type_SimpleArrayComplexLongFloat
5427 case type_SimpleArrayLongFloat:
5429 #ifdef type_SimpleArrayComplexSingleFloat
5430 case type_SimpleArrayComplexSingleFloat:
5432 #ifdef type_SimpleArrayComplexDoubleFloat
5433 case type_SimpleArrayComplexDoubleFloat:
5435 #ifdef type_SimpleArrayComplexLongFloat
5436 case type_SimpleArrayComplexLongFloat:
5439 case type_WeakPointer:
5440 count = (sizetab[TypeOf(*start)])(start);
5456 /* FIXME: It would be nice to make names consistent so that
5457 * foo_size meant size *in* *bytes* instead of size in some
5458 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
5459 * Some counts of lispobjs are called foo_count; it might be good
5460 * to grep for all foo_size and rename the appropriate ones to
5462 int read_only_space_size =
5463 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER)
5464 - (lispobj*)READ_ONLY_SPACE_START;
5465 int static_space_size =
5466 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER)
5467 - (lispobj*)STATIC_SPACE_START;
5468 int binding_stack_size =
5469 (lispobj*)SymbolValue(BINDING_STACK_POINTER)
5470 - (lispobj*)BINDING_STACK_START;
5472 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
5473 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
5474 verify_space((lispobj*)BINDING_STACK_START , binding_stack_size);
5478 verify_generation(int generation)
5482 for (i = 0; i < last_free_page; i++) {
5483 if ((page_table[i].allocated != FREE_PAGE)
5484 && (page_table[i].bytes_used != 0)
5485 && (page_table[i].gen == generation)) {
5487 int region_allocation = page_table[i].allocated;
5489 /* This should be the start of a contiguous block */
5490 gc_assert(page_table[i].first_object_offset == 0);
5492 /* Need to find the full extent of this contiguous block in case
5493 objects span pages. */
5495 /* Now work forward until the end of this contiguous area is
5497 for (last_page = i; ;last_page++)
5498 /* Check whether this is the last page in this contiguous
5500 if ((page_table[last_page].bytes_used < 4096)
5501 /* Or it is 4096 and is the last in the block */
5502 || (page_table[last_page+1].allocated != region_allocation)
5503 || (page_table[last_page+1].bytes_used == 0)
5504 || (page_table[last_page+1].gen != generation)
5505 || (page_table[last_page+1].first_object_offset == 0))
5508 verify_space(page_address(i), (page_table[last_page].bytes_used
5509 + (last_page-i)*4096)/4);
5515 /* Check the all the free space is zero filled. */
5517 verify_zero_fill(void)
5521 for (page = 0; page < last_free_page; page++) {
5522 if (page_table[page].allocated == FREE_PAGE) {
5523 /* The whole page should be zero filled. */
5524 int *start_addr = (int *)page_address(page);
5527 for (i = 0; i < size; i++) {
5528 if (start_addr[i] != 0) {
5529 lose("free page not zero at %x", start_addr + i);
5533 int free_bytes = 4096 - page_table[page].bytes_used;
5534 if (free_bytes > 0) {
5535 int *start_addr = (int *)((unsigned)page_address(page)
5536 + page_table[page].bytes_used);
5537 int size = free_bytes / 4;
5539 for (i = 0; i < size; i++) {
5540 if (start_addr[i] != 0) {
5541 lose("free region not zero at %x", start_addr + i);
5549 /* External entry point for verify_zero_fill */
5551 gencgc_verify_zero_fill(void)
5553 /* Flush the alloc regions updating the tables. */
5554 boxed_region.free_pointer = current_region_free_pointer;
5555 gc_alloc_update_page_tables(0, &boxed_region);
5556 gc_alloc_update_page_tables(1, &unboxed_region);
5557 SHOW("verifying zero fill");
5559 current_region_free_pointer = boxed_region.free_pointer;
5560 current_region_end_addr = boxed_region.end_addr;
5564 verify_dynamic_space(void)
5568 for (i = 0; i < NUM_GENERATIONS; i++)
5569 verify_generation(i);
5571 if (gencgc_enable_verify_zero_fill)
5575 /* Write-protect all the dynamic boxed pages in the given generation. */
5577 write_protect_generation_pages(int generation)
5581 gc_assert(generation < NUM_GENERATIONS);
5583 for (i = 0; i < last_free_page; i++)
5584 if ((page_table[i].allocated == BOXED_PAGE)
5585 && (page_table[i].bytes_used != 0)
5586 && (page_table[i].gen == generation)) {
5589 page_start = (void *)page_address(i);
5591 os_protect(page_start,
5593 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
5595 /* Note the page as protected in the page tables. */
5596 page_table[i].write_protected = 1;
5599 if (gencgc_verbose > 1) {
5601 "/write protected %d of %d pages in generation %d\n",
5602 count_write_protect_generation_pages(generation),
5603 count_generation_pages(generation),
5608 /* Garbage collect a generation. If raise is 0 the remains of the
5609 * generation are not raised to the next generation. */
5611 garbage_collect_generation(int generation, int raise)
5613 unsigned long bytes_freed;
5615 unsigned long read_only_space_size, static_space_size;
5617 gc_assert(generation <= (NUM_GENERATIONS-1));
5619 /* The oldest generation can't be raised. */
5620 gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
5622 /* Initialize the weak pointer list. */
5623 weak_pointers = NULL;
5625 /* When a generation is not being raised it is transported to a
5626 * temporary generation (NUM_GENERATIONS), and lowered when
5627 * done. Set up this new generation. There should be no pages
5628 * allocated to it yet. */
5630 gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
5632 /* Set the global src and dest. generations */
5633 from_space = generation;
5635 new_space = generation+1;
5637 new_space = NUM_GENERATIONS;
5639 /* Change to a new space for allocation, resetting the alloc_start_page */
5640 gc_alloc_generation = new_space;
5641 generations[new_space].alloc_start_page = 0;
5642 generations[new_space].alloc_unboxed_start_page = 0;
5643 generations[new_space].alloc_large_start_page = 0;
5644 generations[new_space].alloc_large_unboxed_start_page = 0;
5646 /* Before any pointers are preserved, the dont_move flags on the
5647 * pages need to be cleared. */
5648 for (i = 0; i < last_free_page; i++)
5649 page_table[i].dont_move = 0;
5651 /* Un-write-protect the old-space pages. This is essential for the
5652 * promoted pages as they may contain pointers into the old-space
5653 * which need to be scavenged. It also helps avoid unnecessary page
5654 * faults as forwarding pointer are written into them. They need to
5655 * be un-protected anyway before unmapping later. */
5656 unprotect_oldspace();
5658 /* Scavenge the stack's conservative roots. */
5661 for (ptr = (lispobj **)CONTROL_STACK_END - 1;
5662 ptr > (lispobj **)&raise;
5664 preserve_pointer(*ptr);
5667 #ifdef CONTROL_STACKS
5668 scavenge_thread_stacks();
5671 if (gencgc_verbose > 1) {
5672 int num_dont_move_pages = count_dont_move_pages();
5674 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
5675 num_dont_move_pages,
5676 /* FIXME: 4096 should be symbolic constant here and
5677 * prob'ly elsewhere too. */
5678 num_dont_move_pages * 4096));
5681 /* Scavenge all the rest of the roots. */
5683 /* Scavenge the Lisp functions of the interrupt handlers, taking
5684 * care to avoid SIG_DFL, SIG_IGN. */
5685 for (i = 0; i < NSIG; i++) {
5686 union interrupt_handler handler = interrupt_handlers[i];
5687 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
5688 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
5689 scavenge((lispobj *)(interrupt_handlers + i), 1);
5693 /* Scavenge the binding stack. */
5694 scavenge( (lispobj *) BINDING_STACK_START,
5695 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
5696 (lispobj *)BINDING_STACK_START);
5698 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
5699 read_only_space_size =
5700 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
5701 (lispobj*)READ_ONLY_SPACE_START;
5703 "/scavenge read only space: %d bytes\n",
5704 read_only_space_size * sizeof(lispobj)));
5705 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
5709 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER) -
5710 (lispobj *)STATIC_SPACE_START;
5711 if (gencgc_verbose > 1)
5713 "/scavenge static space: %d bytes\n",
5714 static_space_size * sizeof(lispobj)));
5715 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
5717 /* All generations but the generation being GCed need to be
5718 * scavenged. The new_space generation needs special handling as
5719 * objects may be moved in - it is handled separately below. */
5720 for (i = 0; i < NUM_GENERATIONS; i++)
5721 if ((i != generation) && (i != new_space))
5722 scavenge_generation(i);
5724 /* Finally scavenge the new_space generation. Keep going until no
5725 * more objects are moved into the new generation */
5726 scavenge_newspace_generation(new_space);
5728 #define RESCAN_CHECK 0
5730 /* As a check re-scavenge the newspace once; no new objects should
5733 int old_bytes_allocated = bytes_allocated;
5734 int bytes_allocated;
5736 /* Start with a full scavenge. */
5737 scavenge_newspace_generation_one_scan(new_space);
5739 /* Flush the current regions, updating the tables. */
5740 gc_alloc_update_page_tables(0, &boxed_region);
5741 gc_alloc_update_page_tables(1, &unboxed_region);
5743 bytes_allocated = bytes_allocated - old_bytes_allocated;
5745 if (bytes_allocated != 0) {
5746 lose("Rescan of new_space allocated %d more bytes.",
5752 scan_weak_pointers();
5754 /* Flush the current regions, updating the tables. */
5755 gc_alloc_update_page_tables(0, &boxed_region);
5756 gc_alloc_update_page_tables(1, &unboxed_region);
5758 /* Free the pages in oldspace, but not those marked dont_move. */
5759 bytes_freed = free_oldspace();
5761 /* If the GC is not raising the age then lower the generation back
5762 * to its normal generation number */
5764 for (i = 0; i < last_free_page; i++)
5765 if ((page_table[i].bytes_used != 0)
5766 && (page_table[i].gen == NUM_GENERATIONS))
5767 page_table[i].gen = generation;
5768 gc_assert(generations[generation].bytes_allocated == 0);
5769 generations[generation].bytes_allocated =
5770 generations[NUM_GENERATIONS].bytes_allocated;
5771 generations[NUM_GENERATIONS].bytes_allocated = 0;
5774 /* Reset the alloc_start_page for generation. */
5775 generations[generation].alloc_start_page = 0;
5776 generations[generation].alloc_unboxed_start_page = 0;
5777 generations[generation].alloc_large_start_page = 0;
5778 generations[generation].alloc_large_unboxed_start_page = 0;
5780 if (generation >= verify_gens) {
5784 verify_dynamic_space();
5787 /* Set the new gc trigger for the GCed generation. */
5788 generations[generation].gc_trigger =
5789 generations[generation].bytes_allocated
5790 + generations[generation].bytes_consed_between_gc;
5793 generations[generation].num_gc = 0;
5795 ++generations[generation].num_gc;
5798 /* Update last_free_page then ALLOCATION_POINTER */
5800 update_x86_dynamic_space_free_pointer(void)
5805 for (i = 0; i < NUM_PAGES; i++)
5806 if ((page_table[i].allocated != FREE_PAGE)
5807 && (page_table[i].bytes_used != 0))
5810 last_free_page = last_page+1;
5812 SetSymbolValue(ALLOCATION_POINTER,
5813 (lispobj)(((char *)heap_base) + last_free_page*4096));
5814 return 0; /* dummy value: return something ... */
5817 /* GC all generations below last_gen, raising their objects to the
5818 * next generation until all generations below last_gen are empty.
5819 * Then if last_gen is due for a GC then GC it. In the special case
5820 * that last_gen==NUM_GENERATIONS, the last generation is always
5821 * GC'ed. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
5823 * The oldest generation to be GCed will always be
5824 * gencgc_oldest_gen_to_gc, partly ignoring last_gen if necessary. */
5826 collect_garbage(unsigned last_gen)
5833 boxed_region.free_pointer = current_region_free_pointer;
5835 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
5837 if (last_gen > NUM_GENERATIONS) {
5839 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
5844 /* Flush the alloc regions updating the tables. */
5845 gc_alloc_update_page_tables(0, &boxed_region);
5846 gc_alloc_update_page_tables(1, &unboxed_region);
5848 /* Verify the new objects created by Lisp code. */
5849 if (pre_verify_gen_0) {
5850 SHOW((stderr, "pre-checking generation 0\n"));
5851 verify_generation(0);
5854 if (gencgc_verbose > 1)
5855 print_generation_stats(0);
5858 /* Collect the generation. */
5860 if (gen >= gencgc_oldest_gen_to_gc) {
5861 /* Never raise the oldest generation. */
5866 || (generations[gen].num_gc >= generations[gen].trigger_age);
5869 if (gencgc_verbose > 1) {
5871 "Starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
5874 generations[gen].bytes_allocated,
5875 generations[gen].gc_trigger,
5876 generations[gen].num_gc));
5879 /* If an older generation is being filled then update its memory
5882 generations[gen+1].cum_sum_bytes_allocated +=
5883 generations[gen+1].bytes_allocated;
5886 garbage_collect_generation(gen, raise);
5888 /* Reset the memory age cum_sum. */
5889 generations[gen].cum_sum_bytes_allocated = 0;
5891 if (gencgc_verbose > 1) {
5892 FSHOW((stderr, "GC of generation %d finished:\n", gen));
5893 print_generation_stats(0);
5897 } while ((gen <= gencgc_oldest_gen_to_gc)
5898 && ((gen < last_gen)
5899 || ((gen <= gencgc_oldest_gen_to_gc)
5901 && (generations[gen].bytes_allocated
5902 > generations[gen].gc_trigger)
5903 && (gen_av_mem_age(gen)
5904 > generations[gen].min_av_mem_age))));
5906 /* Now if gen-1 was raised all generations before gen are empty.
5907 * If it wasn't raised then all generations before gen-1 are empty.
5909 * Now objects within this gen's pages cannot point to younger
5910 * generations unless they are written to. This can be exploited
5911 * by write-protecting the pages of gen; then when younger
5912 * generations are GCed only the pages which have been written
5917 gen_to_wp = gen - 1;
5919 /* There's not much point in WPing pages in generation 0 as it is
5920 * never scavenged (except promoted pages). */
5921 if ((gen_to_wp > 0) && enable_page_protection) {
5922 /* Check that they are all empty. */
5923 for (i = 0; i < gen_to_wp; i++) {
5924 if (generations[i].bytes_allocated)
5925 lose("trying to write-protect gen. %d when gen. %d nonempty",
5928 write_protect_generation_pages(gen_to_wp);
5931 /* Set gc_alloc back to generation 0. The current regions should
5932 * be flushed after the above GCs */
5933 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
5934 gc_alloc_generation = 0;
5936 update_x86_dynamic_space_free_pointer();
5938 /* This is now done by Lisp SCRUB-CONTROL-STACK in Lisp SUB-GC, so we
5939 * needn't do it here: */
5942 current_region_free_pointer = boxed_region.free_pointer;
5943 current_region_end_addr = boxed_region.end_addr;
5945 SHOW("returning from collect_garbage");
5948 /* This is called by Lisp PURIFY when it is finished. All live objects
5949 * will have been moved to the RO and Static heaps. The dynamic space
5950 * will need a full re-initialization. We don't bother having Lisp
5951 * PURIFY flush the current gc_alloc region, as the page_tables are
5952 * re-initialized, and every page is zeroed to be sure. */
5958 if (gencgc_verbose > 1)
5959 SHOW("entering gc_free_heap");
5961 for (page = 0; page < NUM_PAGES; page++) {
5962 /* Skip free pages which should already be zero filled. */
5963 if (page_table[page].allocated != FREE_PAGE) {
5964 void *page_start, *addr;
5966 /* Mark the page free. The other slots are assumed invalid
5967 * when it is a FREE_PAGE and bytes_used is 0 and it
5968 * should not be write-protected -- except that the
5969 * generation is used for the current region but it sets
5971 page_table[page].allocated = FREE_PAGE;
5972 page_table[page].bytes_used = 0;
5974 /* Zero the page. */
5975 page_start = (void *)page_address(page);
5977 /* First, remove any write-protection. */
5978 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5979 page_table[page].write_protected = 0;
5981 os_invalidate(page_start,4096);
5982 addr = os_validate(page_start,4096);
5983 if (addr == NULL || addr != page_start) {
5984 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
5988 } else if (gencgc_zero_check_during_free_heap) {
5989 /* Double-check that the page is zero filled. */
5991 gc_assert(page_table[page].allocated == FREE_PAGE);
5992 gc_assert(page_table[page].bytes_used == 0);
5993 page_start = (int *)page_address(page);
5994 for (i=0; i<1024; i++) {
5995 if (page_start[i] != 0) {
5996 lose("free region not zero at %x", page_start + i);
6002 bytes_allocated = 0;
6004 /* Initialize the generations. */
6005 for (page = 0; page < NUM_GENERATIONS; page++) {
6006 generations[page].alloc_start_page = 0;
6007 generations[page].alloc_unboxed_start_page = 0;
6008 generations[page].alloc_large_start_page = 0;
6009 generations[page].alloc_large_unboxed_start_page = 0;
6010 generations[page].bytes_allocated = 0;
6011 generations[page].gc_trigger = 2000000;
6012 generations[page].num_gc = 0;
6013 generations[page].cum_sum_bytes_allocated = 0;
6016 if (gencgc_verbose > 1)
6017 print_generation_stats(0);
6019 /* Initialize gc_alloc */
6020 gc_alloc_generation = 0;
6021 boxed_region.first_page = 0;
6022 boxed_region.last_page = -1;
6023 boxed_region.start_addr = page_address(0);
6024 boxed_region.free_pointer = page_address(0);
6025 boxed_region.end_addr = page_address(0);
6027 unboxed_region.first_page = 0;
6028 unboxed_region.last_page = -1;
6029 unboxed_region.start_addr = page_address(0);
6030 unboxed_region.free_pointer = page_address(0);
6031 unboxed_region.end_addr = page_address(0);
6033 #if 0 /* Lisp PURIFY is currently running on the C stack so don't do this. */
6038 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base));
6040 current_region_free_pointer = boxed_region.free_pointer;
6041 current_region_end_addr = boxed_region.end_addr;
6043 if (verify_after_free_heap) {
6044 /* Check whether purify has left any bad pointers. */
6046 SHOW("checking after free_heap\n");
6058 heap_base = (void*)DYNAMIC_SPACE_START;
6060 /* Initialize each page structure. */
6061 for (i = 0; i < NUM_PAGES; i++) {
6062 /* Initialize all pages as free. */
6063 page_table[i].allocated = FREE_PAGE;
6064 page_table[i].bytes_used = 0;
6066 /* Pages are not write-protected at startup. */
6067 page_table[i].write_protected = 0;
6070 bytes_allocated = 0;
6072 /* Initialize the generations. */
6073 for (i = 0; i < NUM_GENERATIONS; i++) {
6074 generations[i].alloc_start_page = 0;
6075 generations[i].alloc_unboxed_start_page = 0;
6076 generations[i].alloc_large_start_page = 0;
6077 generations[i].alloc_large_unboxed_start_page = 0;
6078 generations[i].bytes_allocated = 0;
6079 generations[i].gc_trigger = 2000000;
6080 generations[i].num_gc = 0;
6081 generations[i].cum_sum_bytes_allocated = 0;
6082 /* the tune-able parameters */
6083 generations[i].bytes_consed_between_gc = 2000000;
6084 generations[i].trigger_age = 1;
6085 generations[i].min_av_mem_age = 0.75;
6088 /* Initialize gc_alloc. */
6089 gc_alloc_generation = 0;
6090 boxed_region.first_page = 0;
6091 boxed_region.last_page = -1;
6092 boxed_region.start_addr = page_address(0);
6093 boxed_region.free_pointer = page_address(0);
6094 boxed_region.end_addr = page_address(0);
6096 unboxed_region.first_page = 0;
6097 unboxed_region.last_page = -1;
6098 unboxed_region.start_addr = page_address(0);
6099 unboxed_region.free_pointer = page_address(0);
6100 unboxed_region.end_addr = page_address(0);
6104 current_region_free_pointer = boxed_region.free_pointer;
6105 current_region_end_addr = boxed_region.end_addr;
6108 /* Pick up the dynamic space from after a core load.
6110 * The ALLOCATION_POINTER points to the end of the dynamic space.
6112 * XX A scan is needed to identify the closest first objects for pages. */
6114 gencgc_pickup_dynamic(void)
6117 int addr = DYNAMIC_SPACE_START;
6118 int alloc_ptr = SymbolValue(ALLOCATION_POINTER);
6120 /* Initialize the first region. */
6122 page_table[page].allocated = BOXED_PAGE;
6123 page_table[page].gen = 0;
6124 page_table[page].bytes_used = 4096;
6125 page_table[page].large_object = 0;
6126 page_table[page].first_object_offset =
6127 (void *)DYNAMIC_SPACE_START - page_address(page);
6130 } while (addr < alloc_ptr);
6132 generations[0].bytes_allocated = 4096*page;
6133 bytes_allocated = 4096*page;
6135 current_region_free_pointer = boxed_region.free_pointer;
6136 current_region_end_addr = boxed_region.end_addr;
6139 /* a counter for how deep we are in alloc(..) calls */
6140 int alloc_entered = 0;
6142 /* alloc(..) is the external interface for memory allocation. It
6143 * allocates to generation 0. It is not called from within the garbage
6144 * collector as it is only external uses that need the check for heap
6145 * size (GC trigger) and to disable the interrupts (interrupts are
6146 * always disabled during a GC).
6148 * The vops that call alloc(..) assume that the returned space is zero-filled.
6149 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
6151 * The check for a GC trigger is only performed when the current
6152 * region is full, so in most cases it's not needed. Further MAYBE-GC
6153 * is only called once because Lisp will remember "need to collect
6154 * garbage" and get around to it when it can. */
6158 /* Check for alignment allocation problems. */
6159 gc_assert((((unsigned)current_region_free_pointer & 0x7) == 0)
6160 && ((nbytes & 0x7) == 0));
6162 if (SymbolValue(PSEUDO_ATOMIC_ATOMIC)) {/* if already in a pseudo atomic */
6164 void *new_free_pointer;
6167 if (alloc_entered) {
6168 SHOW("alloc re-entered in already-pseudo-atomic case");
6172 /* Check whether there is room in the current region. */
6173 new_free_pointer = current_region_free_pointer + nbytes;
6175 /* FIXME: Shouldn't we be doing some sort of lock here, to
6176 * keep from getting screwed if an interrupt service routine
6177 * allocates memory between the time we calculate new_free_pointer
6178 * and the time we write it back to current_region_free_pointer?
6179 * Perhaps I just don't understand pseudo-atomics..
6181 * Perhaps I don't. It looks as though what happens is if we
6182 * were interrupted any time during the pseudo-atomic
6183 * interval (which includes now) we discard the allocated
6184 * memory and try again. So, at least we don't return
6185 * a memory area that was allocated out from underneath us
6186 * by code in an ISR.
6187 * Still, that doesn't seem to prevent
6188 * current_region_free_pointer from getting corrupted:
6189 * We read current_region_free_pointer.
6190 * They read current_region_free_pointer.
6191 * They write current_region_free_pointer.
6192 * We write current_region_free_pointer, scribbling over
6193 * whatever they wrote. */
6195 if (new_free_pointer <= boxed_region.end_addr) {
6196 /* If so then allocate from the current region. */
6197 void *new_obj = current_region_free_pointer;
6198 current_region_free_pointer = new_free_pointer;
6200 return((void *)new_obj);
6203 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
6204 /* Double the trigger. */
6205 auto_gc_trigger *= 2;
6207 /* Exit the pseudo-atomic. */
6208 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6209 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6210 /* Handle any interrupts that occurred during
6212 do_pending_interrupt();
6214 funcall0(SymbolFunction(MAYBE_GC));
6215 /* Re-enter the pseudo-atomic. */
6216 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0));
6217 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1));
6220 /* Call gc_alloc. */
6221 boxed_region.free_pointer = current_region_free_pointer;
6223 void *new_obj = gc_alloc(nbytes);
6224 current_region_free_pointer = boxed_region.free_pointer;
6225 current_region_end_addr = boxed_region.end_addr;
6231 void *new_free_pointer;
6234 /* At least wrap this allocation in a pseudo atomic to prevent
6235 * gc_alloc from being re-entered. */
6236 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0));
6237 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1));
6240 SHOW("alloc re-entered in not-already-pseudo-atomic case");
6243 /* Check whether there is room in the current region. */
6244 new_free_pointer = current_region_free_pointer + nbytes;
6246 if (new_free_pointer <= boxed_region.end_addr) {
6247 /* If so then allocate from the current region. */
6248 void *new_obj = current_region_free_pointer;
6249 current_region_free_pointer = new_free_pointer;
6251 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6252 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED)) {
6253 /* Handle any interrupts that occurred during
6255 do_pending_interrupt();
6259 return((void *)new_obj);
6262 /* KLUDGE: There's lots of code around here shared with the
6263 * the other branch. Is there some way to factor out the
6264 * duplicate code? -- WHN 19991129 */
6265 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
6266 /* Double the trigger. */
6267 auto_gc_trigger *= 2;
6269 /* Exit the pseudo atomic. */
6270 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6271 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6272 /* Handle any interrupts that occurred during
6274 do_pending_interrupt();
6276 funcall0(SymbolFunction(MAYBE_GC));
6280 /* Else call gc_alloc. */
6281 boxed_region.free_pointer = current_region_free_pointer;
6282 result = gc_alloc(nbytes);
6283 current_region_free_pointer = boxed_region.free_pointer;
6284 current_region_end_addr = boxed_region.end_addr;
6287 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6288 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6289 /* Handle any interrupts that occurred during
6291 do_pending_interrupt();
6300 * noise to manipulate the gc trigger stuff
6304 set_auto_gc_trigger(os_vm_size_t dynamic_usage)
6306 auto_gc_trigger += dynamic_usage;
6310 clear_auto_gc_trigger(void)
6312 auto_gc_trigger = 0;
6315 /* Find the code object for the given pc, or return NULL on failure.
6317 * FIXME: PC shouldn't be lispobj*, should it? Maybe void*? */
6319 component_ptr_from_pc(lispobj *pc)
6321 lispobj *object = NULL;
6323 if ( (object = search_read_only_space(pc)) )
6325 else if ( (object = search_static_space(pc)) )
6328 object = search_dynamic_space(pc);
6330 if (object) /* if we found something */
6331 if (TypeOf(*object) == type_CodeHeader) /* if it's a code object */
6338 * shared support for the OS-dependent signal handlers which
6339 * catch GENCGC-related write-protect violations
6342 /* Depending on which OS we're running under, different signals might
6343 * be raised for a violation of write protection in the heap. This
6344 * function factors out the common generational GC magic which needs
6345 * to invoked in this case, and should be called from whatever signal
6346 * handler is appropriate for the OS we're running under.
6348 * Return true if this signal is a normal generational GC thing that
6349 * we were able to handle, or false if it was abnormal and control
6350 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
6352 gencgc_handle_wp_violation(void* fault_addr)
6354 int page_index = find_page_index(fault_addr);
6356 #if defined QSHOW_SIGNALS
6357 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
6358 fault_addr, page_index));
6361 /* Check whether the fault is within the dynamic space. */
6362 if (page_index == (-1)) {
6364 /* not within the dynamic space -- not our responsibility */
6369 /* The only acceptable reason for an signal like this from the
6370 * heap is that the generational GC write-protected the page. */
6371 if (page_table[page_index].write_protected != 1) {
6372 lose("access failure in heap page not marked as write-protected");
6375 /* Unprotect the page. */
6376 os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
6377 page_table[page_index].write_protected = 0;
6378 page_table[page_index].write_protected_cleared = 1;
6380 /* Don't worry, we can handle it. */