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 /* FIXME: Most calls end up going to some trouble to compute an
1749 * 'n_words' value for this function. The system might be a little
1750 * simpler if this function used an 'end' parameter instead. */
1752 scavenge(lispobj *start, long n_words)
1754 lispobj *end = start + n_words;
1755 lispobj *object_ptr;
1756 int n_words_scavenged;
1758 for (object_ptr = start;
1760 object_ptr += n_words_scavenged) {
1762 lispobj object = *object_ptr;
1764 gc_assert(object != 0x01); /* not a forwarding pointer */
1766 if (Pointerp(object)) {
1767 if (from_space_p(object)) {
1768 /* It currently points to old space. Check for a
1769 * forwarding pointer. */
1770 lispobj *ptr = (lispobj *)PTR(object);
1771 lispobj first_word = *ptr;
1772 if (first_word == 0x01) {
1773 /* Yes, there's a forwarding pointer. */
1774 *object_ptr = ptr[1];
1775 n_words_scavenged = 1;
1777 /* Scavenge that pointer. */
1779 (scavtab[TypeOf(object)])(object_ptr, object);
1782 /* It points somewhere other than oldspace. Leave it
1784 n_words_scavenged = 1;
1786 } else if ((object & 3) == 0) {
1787 /* It's a fixnum: really easy.. */
1788 n_words_scavenged = 1;
1790 /* It's some sort of header object or another. */
1792 (scavtab[TypeOf(object)])(object_ptr, object);
1795 gc_assert(object_ptr == end);
1799 * code and code-related objects
1802 #define RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunctionPointer)
1804 static lispobj trans_function_header(lispobj object);
1805 static lispobj trans_boxed(lispobj object);
1808 scav_function_pointer(lispobj *where, lispobj object)
1810 lispobj *first_pointer;
1813 gc_assert(Pointerp(object));
1815 /* Object is a pointer into from space - no a FP. */
1816 first_pointer = (lispobj *) PTR(object);
1818 /* must transport object -- object may point to either a function
1819 * header, a closure function header, or to a closure header. */
1821 switch (TypeOf(*first_pointer)) {
1822 case type_FunctionHeader:
1823 case type_ClosureFunctionHeader:
1824 copy = trans_function_header(object);
1827 copy = trans_boxed(object);
1831 if (copy != object) {
1832 /* Set forwarding pointer */
1833 first_pointer[0] = 0x01;
1834 first_pointer[1] = copy;
1837 gc_assert(Pointerp(copy));
1838 gc_assert(!from_space_p(copy));
1845 /* Scan a x86 compiled code object, looking for possible fixups that
1846 * have been missed after a move.
1848 * Two types of fixups are needed:
1849 * 1. Absolute fixups to within the code object.
1850 * 2. Relative fixups to outside the code object.
1852 * Currently only absolute fixups to the constant vector, or to the
1853 * code area are checked. */
1855 sniff_code_object(struct code *code, unsigned displacement)
1857 int nheader_words, ncode_words, nwords;
1859 void *constants_start_addr, *constants_end_addr;
1860 void *code_start_addr, *code_end_addr;
1861 int fixup_found = 0;
1863 if (!check_code_fixups)
1866 /* It's ok if it's byte compiled code. The trace table offset will
1867 * be a fixnum if it's x86 compiled code - check. */
1868 if (code->trace_table_offset & 0x3) {
1869 FSHOW((stderr, "/Sniffing byte compiled code object at %x.\n", code));
1873 /* Else it's x86 machine code. */
1875 ncode_words = fixnum_value(code->code_size);
1876 nheader_words = HeaderValue(*(lispobj *)code);
1877 nwords = ncode_words + nheader_words;
1879 constants_start_addr = (void *)code + 5*4;
1880 constants_end_addr = (void *)code + nheader_words*4;
1881 code_start_addr = (void *)code + nheader_words*4;
1882 code_end_addr = (void *)code + nwords*4;
1884 /* Work through the unboxed code. */
1885 for (p = code_start_addr; p < code_end_addr; p++) {
1886 void *data = *(void **)p;
1887 unsigned d1 = *((unsigned char *)p - 1);
1888 unsigned d2 = *((unsigned char *)p - 2);
1889 unsigned d3 = *((unsigned char *)p - 3);
1890 unsigned d4 = *((unsigned char *)p - 4);
1891 unsigned d5 = *((unsigned char *)p - 5);
1892 unsigned d6 = *((unsigned char *)p - 6);
1894 /* Check for code references. */
1895 /* Check for a 32 bit word that looks like an absolute
1896 reference to within the code adea of the code object. */
1897 if ((data >= (code_start_addr-displacement))
1898 && (data < (code_end_addr-displacement))) {
1899 /* function header */
1901 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1902 /* Skip the function header */
1906 /* the case of PUSH imm32 */
1910 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1911 p, d6, d5, d4, d3, d2, d1, data));
1912 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1914 /* the case of MOV [reg-8],imm32 */
1916 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1917 || d2==0x45 || d2==0x46 || d2==0x47)
1921 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1922 p, d6, d5, d4, d3, d2, d1, data));
1923 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1925 /* the case of LEA reg,[disp32] */
1926 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1929 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1930 p, d6, d5, d4, d3, d2, d1, data));
1931 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1935 /* Check for constant references. */
1936 /* Check for a 32 bit word that looks like an absolute
1937 reference to within the constant vector. Constant references
1939 if ((data >= (constants_start_addr-displacement))
1940 && (data < (constants_end_addr-displacement))
1941 && (((unsigned)data & 0x3) == 0)) {
1946 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1947 p, d6, d5, d4, d3, d2, d1, data));
1948 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1951 /* the case of MOV m32,EAX */
1955 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1956 p, d6, d5, d4, d3, d2, d1, data));
1957 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1960 /* the case of CMP m32,imm32 */
1961 if ((d1 == 0x3d) && (d2 == 0x81)) {
1964 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1965 p, d6, d5, d4, d3, d2, d1, data));
1967 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1970 /* Check for a mod=00, r/m=101 byte. */
1971 if ((d1 & 0xc7) == 5) {
1976 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1977 p, d6, d5, d4, d3, d2, d1, data));
1978 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1980 /* the case of CMP reg32,m32 */
1984 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1985 p, d6, d5, d4, d3, d2, d1, data));
1986 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1988 /* the case of MOV m32,reg32 */
1992 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1993 p, d6, d5, d4, d3, d2, d1, data));
1994 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1996 /* the case of MOV reg32,m32 */
2000 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2001 p, d6, d5, d4, d3, d2, d1, data));
2002 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
2004 /* the case of LEA reg32,m32 */
2008 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
2009 p, d6, d5, d4, d3, d2, d1, data));
2010 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
2016 /* If anything was found, print some information on the code
2020 "/compiled code object at %x: header words = %d, code words = %d\n",
2021 code, nheader_words, ncode_words));
2023 "/const start = %x, end = %x\n",
2024 constants_start_addr, constants_end_addr));
2026 "/code start = %x, end = %x\n",
2027 code_start_addr, code_end_addr));
2032 apply_code_fixups(struct code *old_code, struct code *new_code)
2034 int nheader_words, ncode_words, nwords;
2035 void *constants_start_addr, *constants_end_addr;
2036 void *code_start_addr, *code_end_addr;
2037 lispobj fixups = NIL;
2038 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
2039 struct vector *fixups_vector;
2041 /* It's OK if it's byte compiled code. The trace table offset will
2042 * be a fixnum if it's x86 compiled code - check. */
2043 if (new_code->trace_table_offset & 0x3) {
2044 /* FSHOW((stderr, "/byte compiled code object at %x\n", new_code)); */
2048 /* Else it's x86 machine code. */
2049 ncode_words = fixnum_value(new_code->code_size);
2050 nheader_words = HeaderValue(*(lispobj *)new_code);
2051 nwords = ncode_words + nheader_words;
2053 "/compiled code object at %x: header words = %d, code words = %d\n",
2054 new_code, nheader_words, ncode_words)); */
2055 constants_start_addr = (void *)new_code + 5*4;
2056 constants_end_addr = (void *)new_code + nheader_words*4;
2057 code_start_addr = (void *)new_code + nheader_words*4;
2058 code_end_addr = (void *)new_code + nwords*4;
2061 "/const start = %x, end = %x\n",
2062 constants_start_addr,constants_end_addr));
2064 "/code start = %x; end = %x\n",
2065 code_start_addr,code_end_addr));
2068 /* The first constant should be a pointer to the fixups for this
2069 code objects. Check. */
2070 fixups = new_code->constants[0];
2072 /* It will be 0 or the unbound-marker if there are no fixups, and
2073 * will be an other pointer if it is valid. */
2074 if ((fixups == 0) || (fixups == type_UnboundMarker) || !Pointerp(fixups)) {
2075 /* Check for possible errors. */
2076 if (check_code_fixups)
2077 sniff_code_object(new_code, displacement);
2079 /*fprintf(stderr,"Fixups for code object not found!?\n");
2080 fprintf(stderr,"*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2081 new_code, nheader_words, ncode_words);
2082 fprintf(stderr,"*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2083 constants_start_addr,constants_end_addr,
2084 code_start_addr,code_end_addr);*/
2088 fixups_vector = (struct vector *)PTR(fixups);
2090 /* Could be pointing to a forwarding pointer. */
2091 if (Pointerp(fixups) && (find_page_index((void*)fixups_vector) != -1)
2092 && (fixups_vector->header == 0x01)) {
2093 /* If so, then follow it. */
2094 /*SHOW("following pointer to a forwarding pointer");*/
2095 fixups_vector = (struct vector *)PTR((lispobj)fixups_vector->length);
2098 /*SHOW("got fixups");*/
2100 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
2101 /* Got the fixups for the code block. Now work through the vector,
2102 and apply a fixup at each address. */
2103 int length = fixnum_value(fixups_vector->length);
2105 for (i = 0; i < length; i++) {
2106 unsigned offset = fixups_vector->data[i];
2107 /* Now check the current value of offset. */
2108 unsigned old_value =
2109 *(unsigned *)((unsigned)code_start_addr + offset);
2111 /* If it's within the old_code object then it must be an
2112 * absolute fixup (relative ones are not saved) */
2113 if ((old_value >= (unsigned)old_code)
2114 && (old_value < ((unsigned)old_code + nwords*4)))
2115 /* So add the dispacement. */
2116 *(unsigned *)((unsigned)code_start_addr + offset) =
2117 old_value + displacement;
2119 /* It is outside the old code object so it must be a
2120 * relative fixup (absolute fixups are not saved). So
2121 * subtract the displacement. */
2122 *(unsigned *)((unsigned)code_start_addr + offset) =
2123 old_value - displacement;
2127 /* Check for possible errors. */
2128 if (check_code_fixups) {
2129 sniff_code_object(new_code,displacement);
2133 static struct code *
2134 trans_code(struct code *code)
2136 struct code *new_code;
2137 lispobj l_code, l_new_code;
2138 int nheader_words, ncode_words, nwords;
2139 unsigned long displacement;
2140 lispobj fheaderl, *prev_pointer;
2143 "\n/transporting code object located at 0x%08x\n",
2144 (unsigned long) code)); */
2146 /* If object has already been transported, just return pointer. */
2147 if (*((lispobj *)code) == 0x01)
2148 return (struct code*)(((lispobj *)code)[1]);
2150 gc_assert(TypeOf(code->header) == type_CodeHeader);
2152 /* Prepare to transport the code vector. */
2153 l_code = (lispobj) code | type_OtherPointer;
2155 ncode_words = fixnum_value(code->code_size);
2156 nheader_words = HeaderValue(code->header);
2157 nwords = ncode_words + nheader_words;
2158 nwords = CEILING(nwords, 2);
2160 l_new_code = copy_large_object(l_code, nwords);
2161 new_code = (struct code *) PTR(l_new_code);
2163 /* may not have been moved.. */
2164 if (new_code == code)
2167 displacement = l_new_code - l_code;
2171 "/old code object at 0x%08x, new code object at 0x%08x\n",
2172 (unsigned long) code,
2173 (unsigned long) new_code));
2174 FSHOW((stderr, "/Code object is %d words long.\n", nwords));
2177 /* Set forwarding pointer. */
2178 ((lispobj *)code)[0] = 0x01;
2179 ((lispobj *)code)[1] = l_new_code;
2181 /* Set forwarding pointers for all the function headers in the
2182 * code object. Also fix all self pointers. */
2184 fheaderl = code->entry_points;
2185 prev_pointer = &new_code->entry_points;
2187 while (fheaderl != NIL) {
2188 struct function *fheaderp, *nfheaderp;
2191 fheaderp = (struct function *) PTR(fheaderl);
2192 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
2194 /* Calculate the new function pointer and the new */
2195 /* function header. */
2196 nfheaderl = fheaderl + displacement;
2197 nfheaderp = (struct function *) PTR(nfheaderl);
2199 /* Set forwarding pointer. */
2200 ((lispobj *)fheaderp)[0] = 0x01;
2201 ((lispobj *)fheaderp)[1] = nfheaderl;
2203 /* Fix self pointer. */
2204 nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
2206 *prev_pointer = nfheaderl;
2208 fheaderl = fheaderp->next;
2209 prev_pointer = &nfheaderp->next;
2212 /* sniff_code_object(new_code,displacement);*/
2213 apply_code_fixups(code,new_code);
2219 scav_code_header(lispobj *where, lispobj object)
2222 int n_header_words, n_code_words, n_words;
2223 lispobj entry_point; /* tagged pointer to entry point */
2224 struct function *function_ptr; /* untagged pointer to entry point */
2226 code = (struct code *) where;
2227 n_code_words = fixnum_value(code->code_size);
2228 n_header_words = HeaderValue(object);
2229 n_words = n_code_words + n_header_words;
2230 n_words = CEILING(n_words, 2);
2232 /* Scavenge the boxed section of the code data block. */
2233 scavenge(where + 1, n_header_words - 1);
2235 /* Scavenge the boxed section of each function object in the */
2236 /* code data block. */
2237 for (entry_point = code->entry_points;
2239 entry_point = function_ptr->next) {
2241 gc_assert(Pointerp(entry_point));
2243 function_ptr = (struct function *) PTR(entry_point);
2244 gc_assert(TypeOf(function_ptr->header) == type_FunctionHeader);
2246 scavenge(&function_ptr->name, 1);
2247 scavenge(&function_ptr->arglist, 1);
2248 scavenge(&function_ptr->type, 1);
2255 trans_code_header(lispobj object)
2259 ncode = trans_code((struct code *) PTR(object));
2260 return (lispobj) ncode | type_OtherPointer;
2264 size_code_header(lispobj *where)
2267 int nheader_words, ncode_words, nwords;
2269 code = (struct code *) where;
2271 ncode_words = fixnum_value(code->code_size);
2272 nheader_words = HeaderValue(code->header);
2273 nwords = ncode_words + nheader_words;
2274 nwords = CEILING(nwords, 2);
2280 scav_return_pc_header(lispobj *where, lispobj object)
2282 lose("attempted to scavenge a return PC header where=0x%08x object=0x%08x",
2283 (unsigned long) where,
2284 (unsigned long) object);
2285 return 0; /* bogus return value to satisfy static type checking */
2289 trans_return_pc_header(lispobj object)
2291 struct function *return_pc;
2292 unsigned long offset;
2293 struct code *code, *ncode;
2295 SHOW("/trans_return_pc_header: Will this work?");
2297 return_pc = (struct function *) PTR(object);
2298 offset = HeaderValue(return_pc->header) * 4;
2300 /* Transport the whole code object. */
2301 code = (struct code *) ((unsigned long) return_pc - offset);
2302 ncode = trans_code(code);
2304 return ((lispobj) ncode + offset) | type_OtherPointer;
2307 /* On the 386, closures hold a pointer to the raw address instead of the
2308 * function object. */
2311 scav_closure_header(lispobj *where, lispobj object)
2313 struct closure *closure;
2316 closure = (struct closure *)where;
2317 fun = closure->function - RAW_ADDR_OFFSET;
2319 /* The function may have moved so update the raw address. But
2320 * don't write unnecessarily. */
2321 if (closure->function != fun + RAW_ADDR_OFFSET)
2322 closure->function = fun + RAW_ADDR_OFFSET;
2329 scav_function_header(lispobj *where, lispobj object)
2331 lose("attempted to scavenge a function header where=0x%08x object=0x%08x",
2332 (unsigned long) where,
2333 (unsigned long) object);
2334 return 0; /* bogus return value to satisfy static type checking */
2338 trans_function_header(lispobj object)
2340 struct function *fheader;
2341 unsigned long offset;
2342 struct code *code, *ncode;
2344 fheader = (struct function *) PTR(object);
2345 offset = HeaderValue(fheader->header) * 4;
2347 /* Transport the whole code object. */
2348 code = (struct code *) ((unsigned long) fheader - offset);
2349 ncode = trans_code(code);
2351 return ((lispobj) ncode + offset) | type_FunctionPointer;
2359 scav_instance_pointer(lispobj *where, lispobj object)
2361 lispobj copy, *first_pointer;
2363 /* Object is a pointer into from space - not a FP. */
2364 copy = trans_boxed(object);
2366 gc_assert(copy != object);
2368 first_pointer = (lispobj *) PTR(object);
2370 /* Set forwarding pointer. */
2371 first_pointer[0] = 0x01;
2372 first_pointer[1] = copy;
2382 static lispobj trans_list(lispobj object);
2385 scav_list_pointer(lispobj *where, lispobj object)
2387 lispobj first, *first_pointer;
2389 gc_assert(Pointerp(object));
2391 /* Object is a pointer into from space - not FP. */
2393 first = trans_list(object);
2394 gc_assert(first != object);
2396 first_pointer = (lispobj *) PTR(object);
2398 /* Set forwarding pointer */
2399 first_pointer[0] = 0x01;
2400 first_pointer[1] = first;
2402 gc_assert(Pointerp(first));
2403 gc_assert(!from_space_p(first));
2409 trans_list(lispobj object)
2411 lispobj new_list_pointer;
2412 struct cons *cons, *new_cons;
2415 gc_assert(from_space_p(object));
2417 cons = (struct cons *) PTR(object);
2419 /* Copy 'object'. */
2420 new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
2421 new_cons->car = cons->car;
2422 new_cons->cdr = cons->cdr; /* updated later */
2423 new_list_pointer = (lispobj)new_cons | LowtagOf(object);
2425 /* Grab the cdr before it is clobbered. */
2428 /* Set forwarding pointer (clobbers start of list). */
2430 cons->cdr = new_list_pointer;
2432 /* Try to linearize the list in the cdr direction to help reduce
2436 struct cons *cdr_cons, *new_cdr_cons;
2438 if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
2439 || (*((lispobj *)PTR(cdr)) == 0x01))
2442 cdr_cons = (struct cons *) PTR(cdr);
2445 new_cdr_cons = (struct cons*) gc_quick_alloc(sizeof(struct cons));
2446 new_cdr_cons->car = cdr_cons->car;
2447 new_cdr_cons->cdr = cdr_cons->cdr;
2448 new_cdr = (lispobj)new_cdr_cons | LowtagOf(cdr);
2450 /* Grab the cdr before it is clobbered. */
2451 cdr = cdr_cons->cdr;
2453 /* Set forwarding pointer. */
2454 cdr_cons->car = 0x01;
2455 cdr_cons->cdr = new_cdr;
2457 /* Update the cdr of the last cons copied into new space to
2458 * keep the newspace scavenge from having to do it. */
2459 new_cons->cdr = new_cdr;
2461 new_cons = new_cdr_cons;
2464 return new_list_pointer;
2469 * scavenging and transporting other pointers
2473 scav_other_pointer(lispobj *where, lispobj object)
2475 lispobj first, *first_pointer;
2477 gc_assert(Pointerp(object));
2479 /* Object is a pointer into from space - not FP. */
2480 first_pointer = (lispobj *) PTR(object);
2482 first = (transother[TypeOf(*first_pointer)])(object);
2484 if (first != object) {
2485 /* Set forwarding pointer. */
2486 first_pointer[0] = 0x01;
2487 first_pointer[1] = first;
2491 gc_assert(Pointerp(first));
2492 gc_assert(!from_space_p(first));
2498 * immediate, boxed, and unboxed objects
2502 size_pointer(lispobj *where)
2508 scav_immediate(lispobj *where, lispobj object)
2514 trans_immediate(lispobj object)
2516 lose("trying to transport an immediate");
2517 return NIL; /* bogus return value to satisfy static type checking */
2521 size_immediate(lispobj *where)
2528 scav_boxed(lispobj *where, lispobj object)
2534 trans_boxed(lispobj object)
2537 unsigned long length;
2539 gc_assert(Pointerp(object));
2541 header = *((lispobj *) PTR(object));
2542 length = HeaderValue(header) + 1;
2543 length = CEILING(length, 2);
2545 return copy_object(object, length);
2549 trans_boxed_large(lispobj object)
2552 unsigned long length;
2554 gc_assert(Pointerp(object));
2556 header = *((lispobj *) PTR(object));
2557 length = HeaderValue(header) + 1;
2558 length = CEILING(length, 2);
2560 return copy_large_object(object, length);
2564 size_boxed(lispobj *where)
2567 unsigned long length;
2570 length = HeaderValue(header) + 1;
2571 length = CEILING(length, 2);
2577 scav_fdefn(lispobj *where, lispobj object)
2579 struct fdefn *fdefn;
2581 fdefn = (struct fdefn *)where;
2583 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
2584 fdefn->function, fdefn->raw_addr)); */
2586 if ((char *)(fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
2587 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
2589 /* Don't write unnecessarily. */
2590 if (fdefn->raw_addr != (char *)(fdefn->function + RAW_ADDR_OFFSET))
2591 fdefn->raw_addr = (char *)(fdefn->function + RAW_ADDR_OFFSET);
2593 return sizeof(struct fdefn) / sizeof(lispobj);
2600 scav_unboxed(lispobj *where, lispobj object)
2602 unsigned long length;
2604 length = HeaderValue(object) + 1;
2605 length = CEILING(length, 2);
2611 trans_unboxed(lispobj object)
2614 unsigned long length;
2617 gc_assert(Pointerp(object));
2619 header = *((lispobj *) PTR(object));
2620 length = HeaderValue(header) + 1;
2621 length = CEILING(length, 2);
2623 return copy_unboxed_object(object, length);
2627 trans_unboxed_large(lispobj object)
2630 unsigned long length;
2633 gc_assert(Pointerp(object));
2635 header = *((lispobj *) PTR(object));
2636 length = HeaderValue(header) + 1;
2637 length = CEILING(length, 2);
2639 return copy_large_unboxed_object(object, length);
2643 size_unboxed(lispobj *where)
2646 unsigned long length;
2649 length = HeaderValue(header) + 1;
2650 length = CEILING(length, 2);
2656 * vector-like objects
2659 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
2662 scav_string(lispobj *where, lispobj object)
2664 struct vector *vector;
2667 /* NOTE: Strings contain one more byte of data than the length */
2668 /* slot indicates. */
2670 vector = (struct vector *) where;
2671 length = fixnum_value(vector->length) + 1;
2672 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2678 trans_string(lispobj object)
2680 struct vector *vector;
2683 gc_assert(Pointerp(object));
2685 /* NOTE: A string contains one more byte of data (a terminating
2686 * '\0' to help when interfacing with C functions) than indicated
2687 * by the length slot. */
2689 vector = (struct vector *) PTR(object);
2690 length = fixnum_value(vector->length) + 1;
2691 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2693 return copy_large_unboxed_object(object, nwords);
2697 size_string(lispobj *where)
2699 struct vector *vector;
2702 /* NOTE: A string contains one more byte of data (a terminating
2703 * '\0' to help when interfacing with C functions) than indicated
2704 * by the length slot. */
2706 vector = (struct vector *) where;
2707 length = fixnum_value(vector->length) + 1;
2708 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2713 /* FIXME: What does this mean? */
2714 int gencgc_hash = 1;
2717 scav_vector(lispobj *where, lispobj object)
2719 unsigned int kv_length;
2721 unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
2722 lispobj *hash_table;
2723 lispobj empty_symbol;
2724 unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2725 unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2726 unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2728 unsigned next_vector_length = 0;
2730 /* FIXME: A comment explaining this would be nice. It looks as
2731 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
2732 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
2733 if (HeaderValue(object) != subtype_VectorValidHashing)
2737 /* This is set for backward compatibility. FIXME: Do we need
2739 *where = (subtype_VectorMustRehash << type_Bits) | type_SimpleVector;
2743 kv_length = fixnum_value(where[1]);
2744 kv_vector = where + 2; /* Skip the header and length. */
2745 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
2747 /* Scavenge element 0, which may be a hash-table structure. */
2748 scavenge(where+2, 1);
2749 if (!Pointerp(where[2])) {
2750 lose("no pointer at %x in hash table", where[2]);
2752 hash_table = (lispobj *)PTR(where[2]);
2753 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
2754 if (TypeOf(hash_table[0]) != type_InstanceHeader) {
2755 lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
2758 /* Scavenge element 1, which should be some internal symbol that
2759 * the hash table code reserves for marking empty slots. */
2760 scavenge(where+3, 1);
2761 if (!Pointerp(where[3])) {
2762 lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
2764 empty_symbol = where[3];
2765 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
2766 if (TypeOf(*(lispobj *)PTR(empty_symbol)) != type_SymbolHeader) {
2767 lose("not a symbol where empty-hash-table-slot symbol expected: %x",
2768 *(lispobj *)PTR(empty_symbol));
2771 /* Scavenge hash table, which will fix the positions of the other
2772 * needed objects. */
2773 scavenge(hash_table, 16);
2775 /* Cross-check the kv_vector. */
2776 if (where != (lispobj *)PTR(hash_table[9])) {
2777 lose("hash_table table!=this table %x", hash_table[9]);
2781 weak_p_obj = hash_table[10];
2785 lispobj index_vector_obj = hash_table[13];
2787 if (Pointerp(index_vector_obj) &&
2788 (TypeOf(*(lispobj *)PTR(index_vector_obj)) == type_SimpleArrayUnsignedByte32)) {
2789 index_vector = ((unsigned int *)PTR(index_vector_obj)) + 2;
2790 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
2791 length = fixnum_value(((unsigned int *)PTR(index_vector_obj))[1]);
2792 /*FSHOW((stderr, "/length = %d\n", length));*/
2794 lose("invalid index_vector %x", index_vector_obj);
2800 lispobj next_vector_obj = hash_table[14];
2802 if (Pointerp(next_vector_obj) &&
2803 (TypeOf(*(lispobj *)PTR(next_vector_obj)) == type_SimpleArrayUnsignedByte32)) {
2804 next_vector = ((unsigned int *)PTR(next_vector_obj)) + 2;
2805 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
2806 next_vector_length = fixnum_value(((unsigned int *)PTR(next_vector_obj))[1]);
2807 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
2809 lose("invalid next_vector %x", next_vector_obj);
2813 /* maybe hash vector */
2815 /* FIXME: This bare "15" offset should become a symbolic
2816 * expression of some sort. And all the other bare offsets
2817 * too. And the bare "16" in scavenge(hash_table, 16). And
2818 * probably other stuff too. Ugh.. */
2819 lispobj hash_vector_obj = hash_table[15];
2821 if (Pointerp(hash_vector_obj) &&
2822 (TypeOf(*(lispobj *)PTR(hash_vector_obj))
2823 == type_SimpleArrayUnsignedByte32)) {
2824 hash_vector = ((unsigned int *)PTR(hash_vector_obj)) + 2;
2825 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
2826 gc_assert(fixnum_value(((unsigned int *)PTR(hash_vector_obj))[1])
2827 == next_vector_length);
2830 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
2834 /* These lengths could be different as the index_vector can be a
2835 * different length from the others, a larger index_vector could help
2836 * reduce collisions. */
2837 gc_assert(next_vector_length*2 == kv_length);
2839 /* now all set up.. */
2841 /* Work through the KV vector. */
2844 for (i = 1; i < next_vector_length; i++) {
2845 lispobj old_key = kv_vector[2*i];
2846 unsigned int old_index = (old_key & 0x1fffffff)%length;
2848 /* Scavenge the key and value. */
2849 scavenge(&kv_vector[2*i],2);
2851 /* Check whether the key has moved and is EQ based. */
2853 lispobj new_key = kv_vector[2*i];
2854 unsigned int new_index = (new_key & 0x1fffffff)%length;
2856 if ((old_index != new_index) &&
2857 ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
2858 ((new_key != empty_symbol) ||
2859 (kv_vector[2*i] != empty_symbol))) {
2862 "* EQ key %d moved from %x to %x; index %d to %d\n",
2863 i, old_key, new_key, old_index, new_index));*/
2865 if (index_vector[old_index] != 0) {
2866 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
2868 /* Unlink the key from the old_index chain. */
2869 if (index_vector[old_index] == i) {
2870 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
2871 index_vector[old_index] = next_vector[i];
2872 /* Link it into the needing rehash chain. */
2873 next_vector[i] = fixnum_value(hash_table[11]);
2874 hash_table[11] = make_fixnum(i);
2877 unsigned prior = index_vector[old_index];
2878 unsigned next = next_vector[prior];
2880 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
2883 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
2886 next_vector[prior] = next_vector[next];
2887 /* Link it into the needing rehash
2890 fixnum_value(hash_table[11]);
2891 hash_table[11] = make_fixnum(next);
2896 next = next_vector[next];
2904 return (CEILING(kv_length + 2, 2));
2908 trans_vector(lispobj object)
2910 struct vector *vector;
2913 gc_assert(Pointerp(object));
2915 vector = (struct vector *) PTR(object);
2917 length = fixnum_value(vector->length);
2918 nwords = CEILING(length + 2, 2);
2920 return copy_large_object(object, nwords);
2924 size_vector(lispobj *where)
2926 struct vector *vector;
2929 vector = (struct vector *) where;
2930 length = fixnum_value(vector->length);
2931 nwords = CEILING(length + 2, 2);
2938 scav_vector_bit(lispobj *where, lispobj object)
2940 struct vector *vector;
2943 vector = (struct vector *) where;
2944 length = fixnum_value(vector->length);
2945 nwords = CEILING(NWORDS(length, 32) + 2, 2);
2951 trans_vector_bit(lispobj object)
2953 struct vector *vector;
2956 gc_assert(Pointerp(object));
2958 vector = (struct vector *) PTR(object);
2959 length = fixnum_value(vector->length);
2960 nwords = CEILING(NWORDS(length, 32) + 2, 2);
2962 return copy_large_unboxed_object(object, nwords);
2966 size_vector_bit(lispobj *where)
2968 struct vector *vector;
2971 vector = (struct vector *) where;
2972 length = fixnum_value(vector->length);
2973 nwords = CEILING(NWORDS(length, 32) + 2, 2);
2980 scav_vector_unsigned_byte_2(lispobj *where, lispobj object)
2982 struct vector *vector;
2985 vector = (struct vector *) where;
2986 length = fixnum_value(vector->length);
2987 nwords = CEILING(NWORDS(length, 16) + 2, 2);
2993 trans_vector_unsigned_byte_2(lispobj object)
2995 struct vector *vector;
2998 gc_assert(Pointerp(object));
3000 vector = (struct vector *) PTR(object);
3001 length = fixnum_value(vector->length);
3002 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3004 return copy_large_unboxed_object(object, nwords);
3008 size_vector_unsigned_byte_2(lispobj *where)
3010 struct vector *vector;
3013 vector = (struct vector *) where;
3014 length = fixnum_value(vector->length);
3015 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3022 scav_vector_unsigned_byte_4(lispobj *where, lispobj object)
3024 struct vector *vector;
3027 vector = (struct vector *) where;
3028 length = fixnum_value(vector->length);
3029 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3035 trans_vector_unsigned_byte_4(lispobj object)
3037 struct vector *vector;
3040 gc_assert(Pointerp(object));
3042 vector = (struct vector *) PTR(object);
3043 length = fixnum_value(vector->length);
3044 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3046 return copy_large_unboxed_object(object, nwords);
3050 size_vector_unsigned_byte_4(lispobj *where)
3052 struct vector *vector;
3055 vector = (struct vector *) where;
3056 length = fixnum_value(vector->length);
3057 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3063 scav_vector_unsigned_byte_8(lispobj *where, lispobj object)
3065 struct vector *vector;
3068 vector = (struct vector *) where;
3069 length = fixnum_value(vector->length);
3070 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3076 trans_vector_unsigned_byte_8(lispobj object)
3078 struct vector *vector;
3081 gc_assert(Pointerp(object));
3083 vector = (struct vector *) PTR(object);
3084 length = fixnum_value(vector->length);
3085 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3087 return copy_large_unboxed_object(object, nwords);
3091 size_vector_unsigned_byte_8(lispobj *where)
3093 struct vector *vector;
3096 vector = (struct vector *) where;
3097 length = fixnum_value(vector->length);
3098 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3105 scav_vector_unsigned_byte_16(lispobj *where, lispobj object)
3107 struct vector *vector;
3110 vector = (struct vector *) where;
3111 length = fixnum_value(vector->length);
3112 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3118 trans_vector_unsigned_byte_16(lispobj object)
3120 struct vector *vector;
3123 gc_assert(Pointerp(object));
3125 vector = (struct vector *) PTR(object);
3126 length = fixnum_value(vector->length);
3127 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3129 return copy_large_unboxed_object(object, nwords);
3133 size_vector_unsigned_byte_16(lispobj *where)
3135 struct vector *vector;
3138 vector = (struct vector *) where;
3139 length = fixnum_value(vector->length);
3140 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3146 scav_vector_unsigned_byte_32(lispobj *where, lispobj object)
3148 struct vector *vector;
3151 vector = (struct vector *) where;
3152 length = fixnum_value(vector->length);
3153 nwords = CEILING(length + 2, 2);
3159 trans_vector_unsigned_byte_32(lispobj object)
3161 struct vector *vector;
3164 gc_assert(Pointerp(object));
3166 vector = (struct vector *) PTR(object);
3167 length = fixnum_value(vector->length);
3168 nwords = CEILING(length + 2, 2);
3170 return copy_large_unboxed_object(object, nwords);
3174 size_vector_unsigned_byte_32(lispobj *where)
3176 struct vector *vector;
3179 vector = (struct vector *) where;
3180 length = fixnum_value(vector->length);
3181 nwords = CEILING(length + 2, 2);
3187 scav_vector_single_float(lispobj *where, lispobj object)
3189 struct vector *vector;
3192 vector = (struct vector *) where;
3193 length = fixnum_value(vector->length);
3194 nwords = CEILING(length + 2, 2);
3200 trans_vector_single_float(lispobj object)
3202 struct vector *vector;
3205 gc_assert(Pointerp(object));
3207 vector = (struct vector *) PTR(object);
3208 length = fixnum_value(vector->length);
3209 nwords = CEILING(length + 2, 2);
3211 return copy_large_unboxed_object(object, nwords);
3215 size_vector_single_float(lispobj *where)
3217 struct vector *vector;
3220 vector = (struct vector *) where;
3221 length = fixnum_value(vector->length);
3222 nwords = CEILING(length + 2, 2);
3228 scav_vector_double_float(lispobj *where, lispobj object)
3230 struct vector *vector;
3233 vector = (struct vector *) where;
3234 length = fixnum_value(vector->length);
3235 nwords = CEILING(length * 2 + 2, 2);
3241 trans_vector_double_float(lispobj object)
3243 struct vector *vector;
3246 gc_assert(Pointerp(object));
3248 vector = (struct vector *) PTR(object);
3249 length = fixnum_value(vector->length);
3250 nwords = CEILING(length * 2 + 2, 2);
3252 return copy_large_unboxed_object(object, nwords);
3256 size_vector_double_float(lispobj *where)
3258 struct vector *vector;
3261 vector = (struct vector *) where;
3262 length = fixnum_value(vector->length);
3263 nwords = CEILING(length * 2 + 2, 2);
3268 #ifdef type_SimpleArrayLongFloat
3270 scav_vector_long_float(lispobj *where, lispobj object)
3272 struct vector *vector;
3275 vector = (struct vector *) where;
3276 length = fixnum_value(vector->length);
3277 nwords = CEILING(length * 3 + 2, 2);
3283 trans_vector_long_float(lispobj object)
3285 struct vector *vector;
3288 gc_assert(Pointerp(object));
3290 vector = (struct vector *) PTR(object);
3291 length = fixnum_value(vector->length);
3292 nwords = CEILING(length * 3 + 2, 2);
3294 return copy_large_unboxed_object(object, nwords);
3298 size_vector_long_float(lispobj *where)
3300 struct vector *vector;
3303 vector = (struct vector *) where;
3304 length = fixnum_value(vector->length);
3305 nwords = CEILING(length * 3 + 2, 2);
3312 #ifdef type_SimpleArrayComplexSingleFloat
3314 scav_vector_complex_single_float(lispobj *where, lispobj object)
3316 struct vector *vector;
3319 vector = (struct vector *) where;
3320 length = fixnum_value(vector->length);
3321 nwords = CEILING(length * 2 + 2, 2);
3327 trans_vector_complex_single_float(lispobj object)
3329 struct vector *vector;
3332 gc_assert(Pointerp(object));
3334 vector = (struct vector *) PTR(object);
3335 length = fixnum_value(vector->length);
3336 nwords = CEILING(length * 2 + 2, 2);
3338 return copy_large_unboxed_object(object, nwords);
3342 size_vector_complex_single_float(lispobj *where)
3344 struct vector *vector;
3347 vector = (struct vector *) where;
3348 length = fixnum_value(vector->length);
3349 nwords = CEILING(length * 2 + 2, 2);
3355 #ifdef type_SimpleArrayComplexDoubleFloat
3357 scav_vector_complex_double_float(lispobj *where, lispobj object)
3359 struct vector *vector;
3362 vector = (struct vector *) where;
3363 length = fixnum_value(vector->length);
3364 nwords = CEILING(length * 4 + 2, 2);
3370 trans_vector_complex_double_float(lispobj object)
3372 struct vector *vector;
3375 gc_assert(Pointerp(object));
3377 vector = (struct vector *) PTR(object);
3378 length = fixnum_value(vector->length);
3379 nwords = CEILING(length * 4 + 2, 2);
3381 return copy_large_unboxed_object(object, nwords);
3385 size_vector_complex_double_float(lispobj *where)
3387 struct vector *vector;
3390 vector = (struct vector *) where;
3391 length = fixnum_value(vector->length);
3392 nwords = CEILING(length * 4 + 2, 2);
3399 #ifdef type_SimpleArrayComplexLongFloat
3401 scav_vector_complex_long_float(lispobj *where, lispobj object)
3403 struct vector *vector;
3406 vector = (struct vector *) where;
3407 length = fixnum_value(vector->length);
3408 nwords = CEILING(length * 6 + 2, 2);
3414 trans_vector_complex_long_float(lispobj object)
3416 struct vector *vector;
3419 gc_assert(Pointerp(object));
3421 vector = (struct vector *) PTR(object);
3422 length = fixnum_value(vector->length);
3423 nwords = CEILING(length * 6 + 2, 2);
3425 return copy_large_unboxed_object(object, nwords);
3429 size_vector_complex_long_float(lispobj *where)
3431 struct vector *vector;
3434 vector = (struct vector *) where;
3435 length = fixnum_value(vector->length);
3436 nwords = CEILING(length * 6 + 2, 2);
3447 /* XX This is a hack adapted from cgc.c. These don't work too well with the
3448 * gencgc as a list of the weak pointers is maintained within the
3449 * objects which causes writes to the pages. A limited attempt is made
3450 * to avoid unnecessary writes, but this needs a re-think. */
3452 #define WEAK_POINTER_NWORDS \
3453 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
3456 scav_weak_pointer(lispobj *where, lispobj object)
3458 struct weak_pointer *wp = weak_pointers;
3459 /* Push the weak pointer onto the list of weak pointers.
3460 * Do I have to watch for duplicates? Originally this was
3461 * part of trans_weak_pointer but that didn't work in the
3462 * case where the WP was in a promoted region.
3465 /* Check whether it's already in the list. */
3466 while (wp != NULL) {
3467 if (wp == (struct weak_pointer*)where) {
3473 /* Add it to the start of the list. */
3474 wp = (struct weak_pointer*)where;
3475 if (wp->next != weak_pointers) {
3476 wp->next = weak_pointers;
3478 /*SHOW("avoided write to weak pointer");*/
3483 /* Do not let GC scavenge the value slot of the weak pointer.
3484 * (That is why it is a weak pointer.) */
3486 return WEAK_POINTER_NWORDS;
3490 trans_weak_pointer(lispobj object)
3493 /* struct weak_pointer *wp; */
3495 gc_assert(Pointerp(object));
3497 #if defined(DEBUG_WEAK)
3498 FSHOW((stderr, "Transporting weak pointer from 0x%08x\n", object));
3501 /* Need to remember where all the weak pointers are that have */
3502 /* been transported so they can be fixed up in a post-GC pass. */
3504 copy = copy_object(object, WEAK_POINTER_NWORDS);
3505 /* wp = (struct weak_pointer *) PTR(copy);*/
3508 /* Push the weak pointer onto the list of weak pointers. */
3509 /* wp->next = weak_pointers;
3510 * weak_pointers = wp;*/
3516 size_weak_pointer(lispobj *where)
3518 return WEAK_POINTER_NWORDS;
3521 void scan_weak_pointers(void)
3523 struct weak_pointer *wp;
3524 for (wp = weak_pointers; wp != NULL; wp = wp->next) {
3525 lispobj value = wp->value;
3526 lispobj *first_pointer;
3528 first_pointer = (lispobj *)PTR(value);
3531 FSHOW((stderr, "/weak pointer at 0x%08x\n", (unsigned long) wp));
3532 FSHOW((stderr, "/value: 0x%08x\n", (unsigned long) value));
3535 if (Pointerp(value) && from_space_p(value)) {
3536 /* Now, we need to check whether the object has been forwarded. If
3537 * it has been, the weak pointer is still good and needs to be
3538 * updated. Otherwise, the weak pointer needs to be nil'ed
3540 if (first_pointer[0] == 0x01) {
3541 wp->value = first_pointer[1];
3557 scav_lose(lispobj *where, lispobj object)
3559 lose("no scavenge function for object 0x%08x", (unsigned long) object);
3560 return 0; /* bogus return value to satisfy static type checking */
3564 trans_lose(lispobj object)
3566 lose("no transport function for object 0x%08x", (unsigned long) object);
3567 return NIL; /* bogus return value to satisfy static type checking */
3571 size_lose(lispobj *where)
3573 lose("no size function for object at 0x%08x", (unsigned long) where);
3574 return 1; /* bogus return value to satisfy static type checking */
3578 gc_init_tables(void)
3582 /* Set default value in all slots of scavenge table. */
3583 for (i = 0; i < 256; i++) { /* FIXME: bare constant length, ick! */
3584 scavtab[i] = scav_lose;
3587 /* For each type which can be selected by the low 3 bits of the tag
3588 * alone, set multiple entries in our 8-bit scavenge table (one for each
3589 * possible value of the high 5 bits). */
3590 for (i = 0; i < 32; i++) { /* FIXME: bare constant length, ick! */
3591 scavtab[type_EvenFixnum|(i<<3)] = scav_immediate;
3592 scavtab[type_FunctionPointer|(i<<3)] = scav_function_pointer;
3593 /* OtherImmediate0 */
3594 scavtab[type_ListPointer|(i<<3)] = scav_list_pointer;
3595 scavtab[type_OddFixnum|(i<<3)] = scav_immediate;
3596 scavtab[type_InstancePointer|(i<<3)] = scav_instance_pointer;
3597 /* OtherImmediate1 */
3598 scavtab[type_OtherPointer|(i<<3)] = scav_other_pointer;
3601 /* Other-pointer types (those selected by all eight bits of the tag) get
3602 * one entry each in the scavenge table. */
3603 scavtab[type_Bignum] = scav_unboxed;
3604 scavtab[type_Ratio] = scav_boxed;
3605 scavtab[type_SingleFloat] = scav_unboxed;
3606 scavtab[type_DoubleFloat] = scav_unboxed;
3607 #ifdef type_LongFloat
3608 scavtab[type_LongFloat] = scav_unboxed;
3610 scavtab[type_Complex] = scav_boxed;
3611 #ifdef type_ComplexSingleFloat
3612 scavtab[type_ComplexSingleFloat] = scav_unboxed;
3614 #ifdef type_ComplexDoubleFloat
3615 scavtab[type_ComplexDoubleFloat] = scav_unboxed;
3617 #ifdef type_ComplexLongFloat
3618 scavtab[type_ComplexLongFloat] = scav_unboxed;
3620 scavtab[type_SimpleArray] = scav_boxed;
3621 scavtab[type_SimpleString] = scav_string;
3622 scavtab[type_SimpleBitVector] = scav_vector_bit;
3623 scavtab[type_SimpleVector] = scav_vector;
3624 scavtab[type_SimpleArrayUnsignedByte2] = scav_vector_unsigned_byte_2;
3625 scavtab[type_SimpleArrayUnsignedByte4] = scav_vector_unsigned_byte_4;
3626 scavtab[type_SimpleArrayUnsignedByte8] = scav_vector_unsigned_byte_8;
3627 scavtab[type_SimpleArrayUnsignedByte16] = scav_vector_unsigned_byte_16;
3628 scavtab[type_SimpleArrayUnsignedByte32] = scav_vector_unsigned_byte_32;
3629 #ifdef type_SimpleArraySignedByte8
3630 scavtab[type_SimpleArraySignedByte8] = scav_vector_unsigned_byte_8;
3632 #ifdef type_SimpleArraySignedByte16
3633 scavtab[type_SimpleArraySignedByte16] = scav_vector_unsigned_byte_16;
3635 #ifdef type_SimpleArraySignedByte30
3636 scavtab[type_SimpleArraySignedByte30] = scav_vector_unsigned_byte_32;
3638 #ifdef type_SimpleArraySignedByte32
3639 scavtab[type_SimpleArraySignedByte32] = scav_vector_unsigned_byte_32;
3641 scavtab[type_SimpleArraySingleFloat] = scav_vector_single_float;
3642 scavtab[type_SimpleArrayDoubleFloat] = scav_vector_double_float;
3643 #ifdef type_SimpleArrayLongFloat
3644 scavtab[type_SimpleArrayLongFloat] = scav_vector_long_float;
3646 #ifdef type_SimpleArrayComplexSingleFloat
3647 scavtab[type_SimpleArrayComplexSingleFloat] = scav_vector_complex_single_float;
3649 #ifdef type_SimpleArrayComplexDoubleFloat
3650 scavtab[type_SimpleArrayComplexDoubleFloat] = scav_vector_complex_double_float;
3652 #ifdef type_SimpleArrayComplexLongFloat
3653 scavtab[type_SimpleArrayComplexLongFloat] = scav_vector_complex_long_float;
3655 scavtab[type_ComplexString] = scav_boxed;
3656 scavtab[type_ComplexBitVector] = scav_boxed;
3657 scavtab[type_ComplexVector] = scav_boxed;
3658 scavtab[type_ComplexArray] = scav_boxed;
3659 scavtab[type_CodeHeader] = scav_code_header;
3660 /*scavtab[type_FunctionHeader] = scav_function_header;*/
3661 /*scavtab[type_ClosureFunctionHeader] = scav_function_header;*/
3662 /*scavtab[type_ReturnPcHeader] = scav_return_pc_header;*/
3664 scavtab[type_ClosureHeader] = scav_closure_header;
3665 scavtab[type_FuncallableInstanceHeader] = scav_closure_header;
3666 scavtab[type_ByteCodeFunction] = scav_closure_header;
3667 scavtab[type_ByteCodeClosure] = scav_closure_header;
3669 scavtab[type_ClosureHeader] = scav_boxed;
3670 scavtab[type_FuncallableInstanceHeader] = scav_boxed;
3671 scavtab[type_ByteCodeFunction] = scav_boxed;
3672 scavtab[type_ByteCodeClosure] = scav_boxed;
3674 scavtab[type_ValueCellHeader] = scav_boxed;
3675 scavtab[type_SymbolHeader] = scav_boxed;
3676 scavtab[type_BaseChar] = scav_immediate;
3677 scavtab[type_Sap] = scav_unboxed;
3678 scavtab[type_UnboundMarker] = scav_immediate;
3679 scavtab[type_WeakPointer] = scav_weak_pointer;
3680 scavtab[type_InstanceHeader] = scav_boxed;
3681 scavtab[type_Fdefn] = scav_fdefn;
3683 /* transport other table, initialized same way as scavtab */
3684 for (i = 0; i < 256; i++)
3685 transother[i] = trans_lose;
3686 transother[type_Bignum] = trans_unboxed;
3687 transother[type_Ratio] = trans_boxed;
3688 transother[type_SingleFloat] = trans_unboxed;
3689 transother[type_DoubleFloat] = trans_unboxed;
3690 #ifdef type_LongFloat
3691 transother[type_LongFloat] = trans_unboxed;
3693 transother[type_Complex] = trans_boxed;
3694 #ifdef type_ComplexSingleFloat
3695 transother[type_ComplexSingleFloat] = trans_unboxed;
3697 #ifdef type_ComplexDoubleFloat
3698 transother[type_ComplexDoubleFloat] = trans_unboxed;
3700 #ifdef type_ComplexLongFloat
3701 transother[type_ComplexLongFloat] = trans_unboxed;
3703 transother[type_SimpleArray] = trans_boxed_large;
3704 transother[type_SimpleString] = trans_string;
3705 transother[type_SimpleBitVector] = trans_vector_bit;
3706 transother[type_SimpleVector] = trans_vector;
3707 transother[type_SimpleArrayUnsignedByte2] = trans_vector_unsigned_byte_2;
3708 transother[type_SimpleArrayUnsignedByte4] = trans_vector_unsigned_byte_4;
3709 transother[type_SimpleArrayUnsignedByte8] = trans_vector_unsigned_byte_8;
3710 transother[type_SimpleArrayUnsignedByte16] = trans_vector_unsigned_byte_16;
3711 transother[type_SimpleArrayUnsignedByte32] = trans_vector_unsigned_byte_32;
3712 #ifdef type_SimpleArraySignedByte8
3713 transother[type_SimpleArraySignedByte8] = trans_vector_unsigned_byte_8;
3715 #ifdef type_SimpleArraySignedByte16
3716 transother[type_SimpleArraySignedByte16] = trans_vector_unsigned_byte_16;
3718 #ifdef type_SimpleArraySignedByte30
3719 transother[type_SimpleArraySignedByte30] = trans_vector_unsigned_byte_32;
3721 #ifdef type_SimpleArraySignedByte32
3722 transother[type_SimpleArraySignedByte32] = trans_vector_unsigned_byte_32;
3724 transother[type_SimpleArraySingleFloat] = trans_vector_single_float;
3725 transother[type_SimpleArrayDoubleFloat] = trans_vector_double_float;
3726 #ifdef type_SimpleArrayLongFloat
3727 transother[type_SimpleArrayLongFloat] = trans_vector_long_float;
3729 #ifdef type_SimpleArrayComplexSingleFloat
3730 transother[type_SimpleArrayComplexSingleFloat] = trans_vector_complex_single_float;
3732 #ifdef type_SimpleArrayComplexDoubleFloat
3733 transother[type_SimpleArrayComplexDoubleFloat] = trans_vector_complex_double_float;
3735 #ifdef type_SimpleArrayComplexLongFloat
3736 transother[type_SimpleArrayComplexLongFloat] = trans_vector_complex_long_float;
3738 transother[type_ComplexString] = trans_boxed;
3739 transother[type_ComplexBitVector] = trans_boxed;
3740 transother[type_ComplexVector] = trans_boxed;
3741 transother[type_ComplexArray] = trans_boxed;
3742 transother[type_CodeHeader] = trans_code_header;
3743 transother[type_FunctionHeader] = trans_function_header;
3744 transother[type_ClosureFunctionHeader] = trans_function_header;
3745 transother[type_ReturnPcHeader] = trans_return_pc_header;
3746 transother[type_ClosureHeader] = trans_boxed;
3747 transother[type_FuncallableInstanceHeader] = trans_boxed;
3748 transother[type_ByteCodeFunction] = trans_boxed;
3749 transother[type_ByteCodeClosure] = trans_boxed;
3750 transother[type_ValueCellHeader] = trans_boxed;
3751 transother[type_SymbolHeader] = trans_boxed;
3752 transother[type_BaseChar] = trans_immediate;
3753 transother[type_Sap] = trans_unboxed;
3754 transother[type_UnboundMarker] = trans_immediate;
3755 transother[type_WeakPointer] = trans_weak_pointer;
3756 transother[type_InstanceHeader] = trans_boxed;
3757 transother[type_Fdefn] = trans_boxed;
3759 /* size table, initialized the same way as scavtab */
3760 for (i = 0; i < 256; i++)
3761 sizetab[i] = size_lose;
3762 for (i = 0; i < 32; i++) {
3763 sizetab[type_EvenFixnum|(i<<3)] = size_immediate;
3764 sizetab[type_FunctionPointer|(i<<3)] = size_pointer;
3765 /* OtherImmediate0 */
3766 sizetab[type_ListPointer|(i<<3)] = size_pointer;
3767 sizetab[type_OddFixnum|(i<<3)] = size_immediate;
3768 sizetab[type_InstancePointer|(i<<3)] = size_pointer;
3769 /* OtherImmediate1 */
3770 sizetab[type_OtherPointer|(i<<3)] = size_pointer;
3772 sizetab[type_Bignum] = size_unboxed;
3773 sizetab[type_Ratio] = size_boxed;
3774 sizetab[type_SingleFloat] = size_unboxed;
3775 sizetab[type_DoubleFloat] = size_unboxed;
3776 #ifdef type_LongFloat
3777 sizetab[type_LongFloat] = size_unboxed;
3779 sizetab[type_Complex] = size_boxed;
3780 #ifdef type_ComplexSingleFloat
3781 sizetab[type_ComplexSingleFloat] = size_unboxed;
3783 #ifdef type_ComplexDoubleFloat
3784 sizetab[type_ComplexDoubleFloat] = size_unboxed;
3786 #ifdef type_ComplexLongFloat
3787 sizetab[type_ComplexLongFloat] = size_unboxed;
3789 sizetab[type_SimpleArray] = size_boxed;
3790 sizetab[type_SimpleString] = size_string;
3791 sizetab[type_SimpleBitVector] = size_vector_bit;
3792 sizetab[type_SimpleVector] = size_vector;
3793 sizetab[type_SimpleArrayUnsignedByte2] = size_vector_unsigned_byte_2;
3794 sizetab[type_SimpleArrayUnsignedByte4] = size_vector_unsigned_byte_4;
3795 sizetab[type_SimpleArrayUnsignedByte8] = size_vector_unsigned_byte_8;
3796 sizetab[type_SimpleArrayUnsignedByte16] = size_vector_unsigned_byte_16;
3797 sizetab[type_SimpleArrayUnsignedByte32] = size_vector_unsigned_byte_32;
3798 #ifdef type_SimpleArraySignedByte8
3799 sizetab[type_SimpleArraySignedByte8] = size_vector_unsigned_byte_8;
3801 #ifdef type_SimpleArraySignedByte16
3802 sizetab[type_SimpleArraySignedByte16] = size_vector_unsigned_byte_16;
3804 #ifdef type_SimpleArraySignedByte30
3805 sizetab[type_SimpleArraySignedByte30] = size_vector_unsigned_byte_32;
3807 #ifdef type_SimpleArraySignedByte32
3808 sizetab[type_SimpleArraySignedByte32] = size_vector_unsigned_byte_32;
3810 sizetab[type_SimpleArraySingleFloat] = size_vector_single_float;
3811 sizetab[type_SimpleArrayDoubleFloat] = size_vector_double_float;
3812 #ifdef type_SimpleArrayLongFloat
3813 sizetab[type_SimpleArrayLongFloat] = size_vector_long_float;
3815 #ifdef type_SimpleArrayComplexSingleFloat
3816 sizetab[type_SimpleArrayComplexSingleFloat] = size_vector_complex_single_float;
3818 #ifdef type_SimpleArrayComplexDoubleFloat
3819 sizetab[type_SimpleArrayComplexDoubleFloat] = size_vector_complex_double_float;
3821 #ifdef type_SimpleArrayComplexLongFloat
3822 sizetab[type_SimpleArrayComplexLongFloat] = size_vector_complex_long_float;
3824 sizetab[type_ComplexString] = size_boxed;
3825 sizetab[type_ComplexBitVector] = size_boxed;
3826 sizetab[type_ComplexVector] = size_boxed;
3827 sizetab[type_ComplexArray] = size_boxed;
3828 sizetab[type_CodeHeader] = size_code_header;
3830 /* We shouldn't see these, so just lose if it happens. */
3831 sizetab[type_FunctionHeader] = size_function_header;
3832 sizetab[type_ClosureFunctionHeader] = size_function_header;
3833 sizetab[type_ReturnPcHeader] = size_return_pc_header;
3835 sizetab[type_ClosureHeader] = size_boxed;
3836 sizetab[type_FuncallableInstanceHeader] = size_boxed;
3837 sizetab[type_ValueCellHeader] = size_boxed;
3838 sizetab[type_SymbolHeader] = size_boxed;
3839 sizetab[type_BaseChar] = size_immediate;
3840 sizetab[type_Sap] = size_unboxed;
3841 sizetab[type_UnboundMarker] = size_immediate;
3842 sizetab[type_WeakPointer] = size_weak_pointer;
3843 sizetab[type_InstanceHeader] = size_boxed;
3844 sizetab[type_Fdefn] = size_boxed;
3847 /* Scan an area looking for an object which encloses the given pointer.
3848 * Return the object start on success or NULL on failure. */
3850 search_space(lispobj *start, size_t words, lispobj *pointer)
3854 lispobj thing = *start;
3856 /* If thing is an immediate then this is a cons */
3858 || ((thing & 3) == 0) /* fixnum */
3859 || (TypeOf(thing) == type_BaseChar)
3860 || (TypeOf(thing) == type_UnboundMarker))
3863 count = (sizetab[TypeOf(thing)])(start);
3865 /* Check whether the pointer is within this object? */
3866 if ((pointer >= start) && (pointer < (start+count))) {
3868 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
3872 /* Round up the count */
3873 count = CEILING(count,2);
3882 search_read_only_space(lispobj *pointer)
3884 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
3885 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
3886 if ((pointer < start) || (pointer >= end))
3888 return (search_space(start, (pointer+2)-start, pointer));
3892 search_static_space(lispobj *pointer)
3894 lispobj* start = (lispobj*)STATIC_SPACE_START;
3895 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER);
3896 if ((pointer < start) || (pointer >= end))
3898 return (search_space(start, (pointer+2)-start, pointer));
3901 /* a faster version for searching the dynamic space. This will work even
3902 * if the object is in a current allocation region. */
3904 search_dynamic_space(lispobj *pointer)
3906 int page_index = find_page_index(pointer);
3909 /* Address may be invalid - do some checks. */
3910 if ((page_index == -1) || (page_table[page_index].allocated == FREE_PAGE))
3912 start = (lispobj *)((void *)page_address(page_index)
3913 + page_table[page_index].first_object_offset);
3914 return (search_space(start, (pointer+2)-start, pointer));
3917 /* FIXME: There is a strong family resemblance between this function
3918 * and the function of the same name in purify.c. Would it be possible
3919 * to implement them as exactly the same function? */
3921 valid_dynamic_space_pointer(lispobj *pointer)
3923 lispobj *start_addr;
3925 /* Find the object start address */
3926 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
3930 /* We need to allow raw pointers into Code objects for return
3931 * addresses. This will also pickup pointers to functions in code
3933 if (TypeOf(*start_addr) == type_CodeHeader) {
3934 /* X Could do some further checks here. */
3938 /* If it's not a return address then it needs to be a valid Lisp
3940 if (!Pointerp((lispobj)pointer)) {
3944 /* Check that the object pointed to is consistent with the pointer
3946 switch (LowtagOf((lispobj)pointer)) {
3947 case type_FunctionPointer:
3948 /* Start_addr should be the enclosing code object, or a closure
3950 switch (TypeOf(*start_addr)) {
3951 case type_CodeHeader:
3952 /* This case is probably caught above. */
3954 case type_ClosureHeader:
3955 case type_FuncallableInstanceHeader:
3956 case type_ByteCodeFunction:
3957 case type_ByteCodeClosure:
3958 if ((unsigned)pointer !=
3959 ((unsigned)start_addr+type_FunctionPointer)) {
3963 pointer, start_addr, *start_addr));
3971 pointer, start_addr, *start_addr));
3975 case type_ListPointer:
3976 if ((unsigned)pointer !=
3977 ((unsigned)start_addr+type_ListPointer)) {
3981 pointer, start_addr, *start_addr));
3984 /* Is it plausible cons? */
3985 if ((Pointerp(start_addr[0])
3986 || ((start_addr[0] & 3) == 0) /* fixnum */
3987 || (TypeOf(start_addr[0]) == type_BaseChar)
3988 || (TypeOf(start_addr[0]) == type_UnboundMarker))
3989 && (Pointerp(start_addr[1])
3990 || ((start_addr[1] & 3) == 0) /* fixnum */
3991 || (TypeOf(start_addr[1]) == type_BaseChar)
3992 || (TypeOf(start_addr[1]) == type_UnboundMarker)))
3998 pointer, start_addr, *start_addr));
4001 case type_InstancePointer:
4002 if ((unsigned)pointer !=
4003 ((unsigned)start_addr+type_InstancePointer)) {
4007 pointer, start_addr, *start_addr));
4010 if (TypeOf(start_addr[0]) != type_InstanceHeader) {
4014 pointer, start_addr, *start_addr));
4018 case type_OtherPointer:
4019 if ((unsigned)pointer !=
4020 ((int)start_addr+type_OtherPointer)) {
4024 pointer, start_addr, *start_addr));
4027 /* Is it plausible? Not a cons. X should check the headers. */
4028 if (Pointerp(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
4032 pointer, start_addr, *start_addr));
4035 switch (TypeOf(start_addr[0])) {
4036 case type_UnboundMarker:
4041 pointer, start_addr, *start_addr));
4044 /* only pointed to by function pointers? */
4045 case type_ClosureHeader:
4046 case type_FuncallableInstanceHeader:
4047 case type_ByteCodeFunction:
4048 case type_ByteCodeClosure:
4052 pointer, start_addr, *start_addr));
4055 case type_InstanceHeader:
4059 pointer, start_addr, *start_addr));
4062 /* the valid other immediate pointer objects */
4063 case type_SimpleVector:
4066 #ifdef type_ComplexSingleFloat
4067 case type_ComplexSingleFloat:
4069 #ifdef type_ComplexDoubleFloat
4070 case type_ComplexDoubleFloat:
4072 #ifdef type_ComplexLongFloat
4073 case type_ComplexLongFloat:
4075 case type_SimpleArray:
4076 case type_ComplexString:
4077 case type_ComplexBitVector:
4078 case type_ComplexVector:
4079 case type_ComplexArray:
4080 case type_ValueCellHeader:
4081 case type_SymbolHeader:
4083 case type_CodeHeader:
4085 case type_SingleFloat:
4086 case type_DoubleFloat:
4087 #ifdef type_LongFloat
4088 case type_LongFloat:
4090 case type_SimpleString:
4091 case type_SimpleBitVector:
4092 case type_SimpleArrayUnsignedByte2:
4093 case type_SimpleArrayUnsignedByte4:
4094 case type_SimpleArrayUnsignedByte8:
4095 case type_SimpleArrayUnsignedByte16:
4096 case type_SimpleArrayUnsignedByte32:
4097 #ifdef type_SimpleArraySignedByte8
4098 case type_SimpleArraySignedByte8:
4100 #ifdef type_SimpleArraySignedByte16
4101 case type_SimpleArraySignedByte16:
4103 #ifdef type_SimpleArraySignedByte30
4104 case type_SimpleArraySignedByte30:
4106 #ifdef type_SimpleArraySignedByte32
4107 case type_SimpleArraySignedByte32:
4109 case type_SimpleArraySingleFloat:
4110 case type_SimpleArrayDoubleFloat:
4111 #ifdef type_SimpleArrayLongFloat
4112 case type_SimpleArrayLongFloat:
4114 #ifdef type_SimpleArrayComplexSingleFloat
4115 case type_SimpleArrayComplexSingleFloat:
4117 #ifdef type_SimpleArrayComplexDoubleFloat
4118 case type_SimpleArrayComplexDoubleFloat:
4120 #ifdef type_SimpleArrayComplexLongFloat
4121 case type_SimpleArrayComplexLongFloat:
4124 case type_WeakPointer:
4131 pointer, start_addr, *start_addr));
4139 pointer, start_addr, *start_addr));
4147 /* Adjust large bignum and vector objects. This will adjust the allocated
4148 * region if the size has shrunk, and move unboxed objects into unboxed
4149 * pages. The pages are not promoted here, and the promoted region is not
4150 * added to the new_regions; this is really only designed to be called from
4151 * preserve_pointer. Shouldn't fail if this is missed, just may delay the
4152 * moving of objects to unboxed pages, and the freeing of pages. */
4154 maybe_adjust_large_object(lispobj *where)
4159 int remaining_bytes;
4166 /* Check whether it's a vector or bignum object. */
4167 switch (TypeOf(where[0])) {
4168 case type_SimpleVector:
4172 case type_SimpleString:
4173 case type_SimpleBitVector:
4174 case type_SimpleArrayUnsignedByte2:
4175 case type_SimpleArrayUnsignedByte4:
4176 case type_SimpleArrayUnsignedByte8:
4177 case type_SimpleArrayUnsignedByte16:
4178 case type_SimpleArrayUnsignedByte32:
4179 #ifdef type_SimpleArraySignedByte8
4180 case type_SimpleArraySignedByte8:
4182 #ifdef type_SimpleArraySignedByte16
4183 case type_SimpleArraySignedByte16:
4185 #ifdef type_SimpleArraySignedByte30
4186 case type_SimpleArraySignedByte30:
4188 #ifdef type_SimpleArraySignedByte32
4189 case type_SimpleArraySignedByte32:
4191 case type_SimpleArraySingleFloat:
4192 case type_SimpleArrayDoubleFloat:
4193 #ifdef type_SimpleArrayLongFloat
4194 case type_SimpleArrayLongFloat:
4196 #ifdef type_SimpleArrayComplexSingleFloat
4197 case type_SimpleArrayComplexSingleFloat:
4199 #ifdef type_SimpleArrayComplexDoubleFloat
4200 case type_SimpleArrayComplexDoubleFloat:
4202 #ifdef type_SimpleArrayComplexLongFloat
4203 case type_SimpleArrayComplexLongFloat:
4205 boxed = UNBOXED_PAGE;
4211 /* Find its current size. */
4212 nwords = (sizetab[TypeOf(where[0])])(where);
4214 first_page = find_page_index((void *)where);
4215 gc_assert(first_page >= 0);
4217 /* Note: Any page write-protection must be removed, else a later
4218 * scavenge_newspace may incorrectly not scavenge these pages.
4219 * This would not be necessary if they are added to the new areas,
4220 * but lets do it for them all (they'll probably be written
4223 gc_assert(page_table[first_page].first_object_offset == 0);
4225 next_page = first_page;
4226 remaining_bytes = nwords*4;
4227 while (remaining_bytes > 4096) {
4228 gc_assert(page_table[next_page].gen == from_space);
4229 gc_assert((page_table[next_page].allocated == BOXED_PAGE)
4230 || (page_table[next_page].allocated == UNBOXED_PAGE));
4231 gc_assert(page_table[next_page].large_object);
4232 gc_assert(page_table[next_page].first_object_offset ==
4233 -4096*(next_page-first_page));
4234 gc_assert(page_table[next_page].bytes_used == 4096);
4236 page_table[next_page].allocated = boxed;
4238 /* Shouldn't be write-protected at this stage. Essential that the
4240 gc_assert(!page_table[next_page].write_protected);
4241 remaining_bytes -= 4096;
4245 /* Now only one page remains, but the object may have shrunk so
4246 * there may be more unused pages which will be freed. */
4248 /* Object may have shrunk but shouldn't have grown - check. */
4249 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
4251 page_table[next_page].allocated = boxed;
4252 gc_assert(page_table[next_page].allocated ==
4253 page_table[first_page].allocated);
4255 /* Adjust the bytes_used. */
4256 old_bytes_used = page_table[next_page].bytes_used;
4257 page_table[next_page].bytes_used = remaining_bytes;
4259 bytes_freed = old_bytes_used - remaining_bytes;
4261 /* Free any remaining pages; needs care. */
4263 while ((old_bytes_used == 4096) &&
4264 (page_table[next_page].gen == from_space) &&
4265 ((page_table[next_page].allocated == UNBOXED_PAGE)
4266 || (page_table[next_page].allocated == BOXED_PAGE)) &&
4267 page_table[next_page].large_object &&
4268 (page_table[next_page].first_object_offset ==
4269 -(next_page - first_page)*4096)) {
4270 /* It checks out OK, free the page. We don't need to both zeroing
4271 * pages as this should have been done before shrinking the
4272 * object. These pages shouldn't be write protected as they
4273 * should be zero filled. */
4274 gc_assert(page_table[next_page].write_protected == 0);
4276 old_bytes_used = page_table[next_page].bytes_used;
4277 page_table[next_page].allocated = FREE_PAGE;
4278 page_table[next_page].bytes_used = 0;
4279 bytes_freed += old_bytes_used;
4283 if ((bytes_freed > 0) && gencgc_verbose)
4284 FSHOW((stderr, "/adjust_large_object freed %d\n", bytes_freed));
4286 generations[from_space].bytes_allocated -= bytes_freed;
4287 bytes_allocated -= bytes_freed;
4292 /* Take a possible pointer to a list object and mark the page_table
4293 * so that it will not need changing during a GC.
4295 * This involves locating the page it points to, then backing up to
4296 * the first page that has its first object start at offset 0, and
4297 * then marking all pages dont_move from the first until a page that ends
4298 * by being full, or having free gen.
4300 * This ensures that objects spanning pages are not broken.
4302 * It is assumed that all the page static flags have been cleared at
4303 * the start of a GC.
4305 * It is also assumed that the current gc_alloc region has been flushed and
4306 * the tables updated. */
4308 preserve_pointer(void *addr)
4310 int addr_page_index = find_page_index(addr);
4313 unsigned region_allocation;
4315 /* Address is quite likely to have been invalid - do some checks. */
4316 if ((addr_page_index == -1)
4317 || (page_table[addr_page_index].allocated == FREE_PAGE)
4318 || (page_table[addr_page_index].bytes_used == 0)
4319 || (page_table[addr_page_index].gen != from_space)
4320 /* Skip if already marked dont_move */
4321 || (page_table[addr_page_index].dont_move != 0))
4324 region_allocation = page_table[addr_page_index].allocated;
4326 /* Check the offset within the page.
4328 * FIXME: The mask should have a symbolic name, and ideally should
4329 * be derived from page size instead of hardwired to 0xfff.
4330 * (Also fix other uses of 0xfff, elsewhere.) */
4331 if (((unsigned)addr & 0xfff) > page_table[addr_page_index].bytes_used)
4334 if (enable_pointer_filter && !valid_dynamic_space_pointer(addr))
4337 /* Work backwards to find a page with a first_object_offset of 0.
4338 * The pages should be contiguous with all bytes used in the same
4339 * gen. Assumes the first_object_offset is negative or zero. */
4340 first_page = addr_page_index;
4341 while (page_table[first_page].first_object_offset != 0) {
4343 /* Do some checks. */
4344 gc_assert(page_table[first_page].bytes_used == 4096);
4345 gc_assert(page_table[first_page].gen == from_space);
4346 gc_assert(page_table[first_page].allocated == region_allocation);
4349 /* Adjust any large objects before promotion as they won't be copied
4350 * after promotion. */
4351 if (page_table[first_page].large_object) {
4352 maybe_adjust_large_object(page_address(first_page));
4353 /* If a large object has shrunk then addr may now point to a free
4354 * area in which case it's ignored here. Note it gets through the
4355 * valid pointer test above because the tail looks like conses. */
4356 if ((page_table[addr_page_index].allocated == FREE_PAGE)
4357 || (page_table[addr_page_index].bytes_used == 0)
4358 /* Check the offset within the page. */
4359 || (((unsigned)addr & 0xfff)
4360 > page_table[addr_page_index].bytes_used)) {
4362 "weird? ignore ptr 0x%x to freed area of large object\n",
4366 /* It may have moved to unboxed pages. */
4367 region_allocation = page_table[first_page].allocated;
4370 /* Now work forward until the end of this contiguous area is found,
4371 * marking all pages as dont_move. */
4372 for (i = first_page; ;i++) {
4373 gc_assert(page_table[i].allocated == region_allocation);
4375 /* Mark the page static. */
4376 page_table[i].dont_move = 1;
4378 /* Move the page to the new_space. XX I'd rather not do this but
4379 * the GC logic is not quite able to copy with the static pages
4380 * remaining in the from space. This also requires the generation
4381 * bytes_allocated counters be updated. */
4382 page_table[i].gen = new_space;
4383 generations[new_space].bytes_allocated += page_table[i].bytes_used;
4384 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
4386 /* It is essential that the pages are not write protected as they
4387 * may have pointers into the old-space which need scavenging. They
4388 * shouldn't be write protected at this stage. */
4389 gc_assert(!page_table[i].write_protected);
4391 /* Check whether this is the last page in this contiguous block.. */
4392 if ((page_table[i].bytes_used < 4096)
4393 /* ..or it is 4096 and is the last in the block */
4394 || (page_table[i+1].allocated == FREE_PAGE)
4395 || (page_table[i+1].bytes_used == 0) /* next page free */
4396 || (page_table[i+1].gen != from_space) /* diff. gen */
4397 || (page_table[i+1].first_object_offset == 0))
4401 /* Check that the page is now static. */
4402 gc_assert(page_table[addr_page_index].dont_move != 0);
4407 #ifdef CONTROL_STACKS
4408 /* Scavenge the thread stack conservative roots. */
4410 scavenge_thread_stacks(void)
4412 lispobj thread_stacks = SymbolValue(CONTROL_STACKS);
4413 int type = TypeOf(thread_stacks);
4415 if (LowtagOf(thread_stacks) == type_OtherPointer) {
4416 struct vector *vector = (struct vector *) PTR(thread_stacks);
4418 if (TypeOf(vector->header) != type_SimpleVector)
4420 length = fixnum_value(vector->length);
4421 for (i = 0; i < length; i++) {
4422 lispobj stack_obj = vector->data[i];
4423 if (LowtagOf(stack_obj) == type_OtherPointer) {
4424 struct vector *stack = (struct vector *) PTR(stack_obj);
4426 if (TypeOf(stack->header) !=
4427 type_SimpleArrayUnsignedByte32) {
4430 vector_length = fixnum_value(stack->length);
4431 if ((gencgc_verbose > 1) && (vector_length <= 0))
4433 "/weird? control stack vector length %d\n",
4435 if (vector_length > 0) {
4436 lispobj *stack_pointer = (lispobj*)stack->data[0];
4437 if ((stack_pointer < (lispobj *)CONTROL_STACK_START) ||
4438 (stack_pointer > (lispobj *)CONTROL_STACK_END))
4439 lose("invalid stack pointer %x",
4440 (unsigned)stack_pointer);
4441 if ((stack_pointer > (lispobj *)CONTROL_STACK_START) &&
4442 (stack_pointer < (lispobj *)CONTROL_STACK_END)) {
4444 * (1) hardwired word length = 4; and as usual,
4445 * when fixing this, check for other places
4446 * with the same problem
4447 * (2) calling it 'length' suggests bytes;
4448 * perhaps 'size' instead? */
4449 unsigned int length = ((unsigned)CONTROL_STACK_END -
4450 (unsigned)stack_pointer) / 4;
4452 if (length >= vector_length) {
4453 lose("invalid stack size %d >= vector length %d",
4457 if (gencgc_verbose > 1) {
4459 "scavenging %d words of control stack %d of length %d words.\n",
4460 length, i, vector_length));
4462 for (j = 0; j < length; j++) {
4463 preserve_pointer((void *)stack->data[1+j]);
4474 /* If the given page is not write-protected, then scan it for pointers
4475 * to younger generations or the top temp. generation, if no
4476 * suspicious pointers are found then the page is write-protected.
4478 * Care is taken to check for pointers to the current gc_alloc region
4479 * if it is a younger generation or the temp. generation. This frees
4480 * the caller from doing a gc_alloc_update_page_tables. Actually the
4481 * gc_alloc_generation does not need to be checked as this is only
4482 * called from scavenge_generation when the gc_alloc generation is
4483 * younger, so it just checks if there is a pointer to the current
4486 * We return 1 if the page was write-protected, else 0.
4489 update_page_write_prot(int page)
4491 int gen = page_table[page].gen;
4494 void **page_addr = (void **)page_address(page);
4495 int num_words = page_table[page].bytes_used / 4;
4497 /* Shouldn't be a free page. */
4498 gc_assert(page_table[page].allocated != FREE_PAGE);
4499 gc_assert(page_table[page].bytes_used != 0);
4501 /* Skip if it's already write-protected or an unboxed page. */
4502 if (page_table[page].write_protected
4503 || (page_table[page].allocated == UNBOXED_PAGE))
4506 /* Scan the page for pointers to younger generations or the
4507 * top temp. generation. */
4509 for (j = 0; j < num_words; j++) {
4510 void *ptr = *(page_addr+j);
4511 int index = find_page_index(ptr);
4513 /* Check that it's in the dynamic space */
4515 if (/* Does it point to a younger or the temp. generation? */
4516 ((page_table[index].allocated != FREE_PAGE)
4517 && (page_table[index].bytes_used != 0)
4518 && ((page_table[index].gen < gen)
4519 || (page_table[index].gen == NUM_GENERATIONS)))
4521 /* Or does it point within a current gc_alloc region? */
4522 || ((boxed_region.start_addr <= ptr)
4523 && (ptr <= boxed_region.free_pointer))
4524 || ((unboxed_region.start_addr <= ptr)
4525 && (ptr <= unboxed_region.free_pointer))) {
4532 /* Write-protect the page. */
4533 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
4535 os_protect((void *)page_addr,
4537 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
4539 /* Note the page as protected in the page tables. */
4540 page_table[page].write_protected = 1;
4546 /* Scavenge a generation.
4548 * This will not resolve all pointers when generation is the new
4549 * space, as new objects may be added which are not check here - use
4550 * scavenge_newspace generation.
4552 * Write-protected pages should not have any pointers to the
4553 * from_space so do need scavenging; thus write-protected pages are
4554 * not always scavenged. There is some code to check that these pages
4555 * are not written; but to check fully the write-protected pages need
4556 * to be scavenged by disabling the code to skip them.
4558 * Under the current scheme when a generation is GCed the younger
4559 * generations will be empty. So, when a generation is being GCed it
4560 * is only necessary to scavenge the older generations for pointers
4561 * not the younger. So a page that does not have pointers to younger
4562 * generations does not need to be scavenged.
4564 * The write-protection can be used to note pages that don't have
4565 * pointers to younger pages. But pages can be written without having
4566 * pointers to younger generations. After the pages are scavenged here
4567 * they can be scanned for pointers to younger generations and if
4568 * there are none the page can be write-protected.
4570 * One complication is when the newspace is the top temp. generation.
4572 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
4573 * that none were written, which they shouldn't be as they should have
4574 * no pointers to younger generations. This breaks down for weak
4575 * pointers as the objects contain a link to the next and are written
4576 * if a weak pointer is scavenged. Still it's a useful check. */
4578 scavenge_generation(int generation)
4585 /* Clear the write_protected_cleared flags on all pages. */
4586 for (i = 0; i < NUM_PAGES; i++)
4587 page_table[i].write_protected_cleared = 0;
4590 for (i = 0; i < last_free_page; i++) {
4591 if ((page_table[i].allocated == BOXED_PAGE)
4592 && (page_table[i].bytes_used != 0)
4593 && (page_table[i].gen == generation)) {
4596 /* This should be the start of a contiguous block. */
4597 gc_assert(page_table[i].first_object_offset == 0);
4599 /* We need to find the full extent of this contiguous
4600 * block in case objects span pages. */
4602 /* Now work forward until the end of this contiguous area
4603 * is found. A small area is preferred as there is a
4604 * better chance of its pages being write-protected. */
4605 for (last_page = i; ;last_page++)
4606 /* Check whether this is the last page in this contiguous
4608 if ((page_table[last_page].bytes_used < 4096)
4609 /* Or it is 4096 and is the last in the block */
4610 || (page_table[last_page+1].allocated != BOXED_PAGE)
4611 || (page_table[last_page+1].bytes_used == 0)
4612 || (page_table[last_page+1].gen != generation)
4613 || (page_table[last_page+1].first_object_offset == 0))
4616 /* Do a limited check for write_protected pages. If all pages
4617 * are write_protected then there is no need to scavenge. */
4620 for (j = i; j <= last_page; j++)
4621 if (page_table[j].write_protected == 0) {
4629 scavenge(page_address(i), (page_table[last_page].bytes_used
4630 + (last_page-i)*4096)/4);
4632 /* Now scan the pages and write protect those
4633 * that don't have pointers to younger
4635 if (enable_page_protection) {
4636 for (j = i; j <= last_page; j++) {
4637 num_wp += update_page_write_prot(j);
4646 if ((gencgc_verbose > 1) && (num_wp != 0)) {
4648 "/write protected %d pages within generation %d\n",
4649 num_wp, generation));
4653 /* Check that none of the write_protected pages in this generation
4654 * have been written to. */
4655 for (i = 0; i < NUM_PAGES; i++) {
4656 if ((page_table[i].allocation ! =FREE_PAGE)
4657 && (page_table[i].bytes_used != 0)
4658 && (page_table[i].gen == generation)
4659 && (page_table[i].write_protected_cleared != 0)) {
4660 FSHOW((stderr, "/scavenge_generation %d\n", generation));
4662 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
4663 page_table[i].bytes_used,
4664 page_table[i].first_object_offset,
4665 page_table[i].dont_move));
4666 lose("write-protected page %d written to in scavenge_generation",
4674 /* Scavenge a newspace generation. As it is scavenged new objects may
4675 * be allocated to it; these will also need to be scavenged. This
4676 * repeats until there are no more objects unscavenged in the
4677 * newspace generation.
4679 * To help improve the efficiency, areas written are recorded by
4680 * gc_alloc and only these scavenged. Sometimes a little more will be
4681 * scavenged, but this causes no harm. An easy check is done that the
4682 * scavenged bytes equals the number allocated in the previous
4685 * Write-protected pages are not scanned except if they are marked
4686 * dont_move in which case they may have been promoted and still have
4687 * pointers to the from space.
4689 * Write-protected pages could potentially be written by alloc however
4690 * to avoid having to handle re-scavenging of write-protected pages
4691 * gc_alloc does not write to write-protected pages.
4693 * New areas of objects allocated are recorded alternatively in the two
4694 * new_areas arrays below. */
4695 static struct new_area new_areas_1[NUM_NEW_AREAS];
4696 static struct new_area new_areas_2[NUM_NEW_AREAS];
4698 /* Do one full scan of the new space generation. This is not enough to
4699 * complete the job as new objects may be added to the generation in
4700 * the process which are not scavenged. */
4702 scavenge_newspace_generation_one_scan(int generation)
4707 "/starting one full scan of newspace generation %d\n",
4710 for (i = 0; i < last_free_page; i++) {
4711 if ((page_table[i].allocated == BOXED_PAGE)
4712 && (page_table[i].bytes_used != 0)
4713 && (page_table[i].gen == generation)
4714 && ((page_table[i].write_protected == 0)
4715 /* (This may be redundant as write_protected is now
4716 * cleared before promotion.) */
4717 || (page_table[i].dont_move == 1))) {
4720 /* The scavenge will start at the first_object_offset of page i.
4722 * We need to find the full extent of this contiguous
4723 * block in case objects span pages.
4725 * Now work forward until the end of this contiguous area
4726 * is found. A small area is preferred as there is a
4727 * better chance of its pages being write-protected. */
4728 for (last_page = i; ;last_page++) {
4729 /* Check whether this is the last page in this
4730 * contiguous block */
4731 if ((page_table[last_page].bytes_used < 4096)
4732 /* Or it is 4096 and is the last in the block */
4733 || (page_table[last_page+1].allocated != BOXED_PAGE)
4734 || (page_table[last_page+1].bytes_used == 0)
4735 || (page_table[last_page+1].gen != generation)
4736 || (page_table[last_page+1].first_object_offset == 0))
4740 /* Do a limited check for write-protected pages. If all
4741 * pages are write-protected then no need to scavenge,
4742 * except if the pages are marked dont_move. */
4745 for (j = i; j <= last_page; j++)
4746 if ((page_table[j].write_protected == 0)
4747 || (page_table[j].dont_move != 0)) {
4755 /* Calculate the size. */
4757 size = (page_table[last_page].bytes_used
4758 - page_table[i].first_object_offset)/4;
4760 size = (page_table[last_page].bytes_used
4761 + (last_page-i)*4096
4762 - page_table[i].first_object_offset)/4;
4765 new_areas_ignore_page = last_page;
4767 scavenge(page_address(i) +
4768 page_table[i].first_object_offset,
4779 "/done with one full scan of newspace generation %d\n",
4783 /* Do a complete scavenge of the newspace generation. */
4785 scavenge_newspace_generation(int generation)
4789 /* the new_areas array currently being written to by gc_alloc */
4790 struct new_area (*current_new_areas)[] = &new_areas_1;
4791 int current_new_areas_index;
4793 /* the new_areas created but the previous scavenge cycle */
4794 struct new_area (*previous_new_areas)[] = NULL;
4795 int previous_new_areas_index;
4797 /* Flush the current regions updating the tables. */
4798 gc_alloc_update_page_tables(0, &boxed_region);
4799 gc_alloc_update_page_tables(1, &unboxed_region);
4801 /* Turn on the recording of new areas by gc_alloc. */
4802 new_areas = current_new_areas;
4803 new_areas_index = 0;
4805 /* Don't need to record new areas that get scavenged anyway during
4806 * scavenge_newspace_generation_one_scan. */
4807 record_new_objects = 1;
4809 /* Start with a full scavenge. */
4810 scavenge_newspace_generation_one_scan(generation);
4812 /* Record all new areas now. */
4813 record_new_objects = 2;
4815 /* Flush the current regions updating the tables. */
4816 gc_alloc_update_page_tables(0, &boxed_region);
4817 gc_alloc_update_page_tables(1, &unboxed_region);
4819 /* Grab new_areas_index. */
4820 current_new_areas_index = new_areas_index;
4823 "The first scan is finished; current_new_areas_index=%d.\n",
4824 current_new_areas_index));*/
4826 while (current_new_areas_index > 0) {
4827 /* Move the current to the previous new areas */
4828 previous_new_areas = current_new_areas;
4829 previous_new_areas_index = current_new_areas_index;
4831 /* Scavenge all the areas in previous new areas. Any new areas
4832 * allocated are saved in current_new_areas. */
4834 /* Allocate an array for current_new_areas; alternating between
4835 * new_areas_1 and 2 */
4836 if (previous_new_areas == &new_areas_1)
4837 current_new_areas = &new_areas_2;
4839 current_new_areas = &new_areas_1;
4841 /* Set up for gc_alloc. */
4842 new_areas = current_new_areas;
4843 new_areas_index = 0;
4845 /* Check whether previous_new_areas had overflowed. */
4846 if (previous_new_areas_index >= NUM_NEW_AREAS) {
4848 /* New areas of objects allocated have been lost so need to do a
4849 * full scan to be sure! If this becomes a problem try
4850 * increasing NUM_NEW_AREAS. */
4852 SHOW("new_areas overflow, doing full scavenge");
4854 /* Don't need to record new areas that get scavenge anyway
4855 * during scavenge_newspace_generation_one_scan. */
4856 record_new_objects = 1;
4858 scavenge_newspace_generation_one_scan(generation);
4860 /* Record all new areas now. */
4861 record_new_objects = 2;
4863 /* Flush the current regions updating the tables. */
4864 gc_alloc_update_page_tables(0, &boxed_region);
4865 gc_alloc_update_page_tables(1, &unboxed_region);
4869 /* Work through previous_new_areas. */
4870 for (i = 0; i < previous_new_areas_index; i++) {
4871 /* FIXME: All these bare *4 and /4 should be something
4872 * like BYTES_PER_WORD or WBYTES. */
4873 int page = (*previous_new_areas)[i].page;
4874 int offset = (*previous_new_areas)[i].offset;
4875 int size = (*previous_new_areas)[i].size / 4;
4876 gc_assert((*previous_new_areas)[i].size % 4 == 0);
4878 scavenge(page_address(page)+offset, size);
4881 /* Flush the current regions updating the tables. */
4882 gc_alloc_update_page_tables(0, &boxed_region);
4883 gc_alloc_update_page_tables(1, &unboxed_region);
4886 current_new_areas_index = new_areas_index;
4889 "The re-scan has finished; current_new_areas_index=%d.\n",
4890 current_new_areas_index));*/
4893 /* Turn off recording of areas allocated by gc_alloc. */
4894 record_new_objects = 0;
4897 /* Check that none of the write_protected pages in this generation
4898 * have been written to. */
4899 for (i = 0; i < NUM_PAGES; i++) {
4900 if ((page_table[i].allocation != FREE_PAGE)
4901 && (page_table[i].bytes_used != 0)
4902 && (page_table[i].gen == generation)
4903 && (page_table[i].write_protected_cleared != 0)
4904 && (page_table[i].dont_move == 0)) {
4905 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
4906 i, generation, page_table[i].dont_move);
4912 /* Un-write-protect all the pages in from_space. This is done at the
4913 * start of a GC else there may be many page faults while scavenging
4914 * the newspace (I've seen drive the system time to 99%). These pages
4915 * would need to be unprotected anyway before unmapping in
4916 * free_oldspace; not sure what effect this has on paging.. */
4918 unprotect_oldspace(void)
4922 for (i = 0; i < last_free_page; i++) {
4923 if ((page_table[i].allocated != FREE_PAGE)
4924 && (page_table[i].bytes_used != 0)
4925 && (page_table[i].gen == from_space)) {
4928 page_start = (void *)page_address(i);
4930 /* Remove any write-protection. We should be able to rely
4931 * on the write-protect flag to avoid redundant calls. */
4932 if (page_table[i].write_protected) {
4933 os_protect(page_start, 4096, OS_VM_PROT_ALL);
4934 page_table[i].write_protected = 0;
4940 /* Work through all the pages and free any in from_space. This
4941 * assumes that all objects have been copied or promoted to an older
4942 * generation. Bytes_allocated and the generation bytes_allocated
4943 * counter are updated. The number of bytes freed is returned. */
4944 extern void i586_bzero(void *addr, int nbytes);
4948 int bytes_freed = 0;
4949 int first_page, last_page;
4954 /* Find a first page for the next region of pages. */
4955 while ((first_page < last_free_page)
4956 && ((page_table[first_page].allocated == FREE_PAGE)
4957 || (page_table[first_page].bytes_used == 0)
4958 || (page_table[first_page].gen != from_space)))
4961 if (first_page >= last_free_page)
4964 /* Find the last page of this region. */
4965 last_page = first_page;
4968 /* Free the page. */
4969 bytes_freed += page_table[last_page].bytes_used;
4970 generations[page_table[last_page].gen].bytes_allocated -=
4971 page_table[last_page].bytes_used;
4972 page_table[last_page].allocated = FREE_PAGE;
4973 page_table[last_page].bytes_used = 0;
4975 /* Remove any write-protection. We should be able to rely
4976 * on the write-protect flag to avoid redundant calls. */
4978 void *page_start = (void *)page_address(last_page);
4980 if (page_table[last_page].write_protected) {
4981 os_protect(page_start, 4096, OS_VM_PROT_ALL);
4982 page_table[last_page].write_protected = 0;
4987 while ((last_page < last_free_page)
4988 && (page_table[last_page].allocated != FREE_PAGE)
4989 && (page_table[last_page].bytes_used != 0)
4990 && (page_table[last_page].gen == from_space));
4992 /* Zero pages from first_page to (last_page-1).
4994 * FIXME: Why not use os_zero(..) function instead of
4995 * hand-coding this again? (Check other gencgc_unmap_zero
4997 if (gencgc_unmap_zero) {
4998 void *page_start, *addr;
5000 page_start = (void *)page_address(first_page);
5002 os_invalidate(page_start, 4096*(last_page-first_page));
5003 addr = os_validate(page_start, 4096*(last_page-first_page));
5004 if (addr == NULL || addr != page_start) {
5005 /* Is this an error condition? I couldn't really tell from
5006 * the old CMU CL code, which fprintf'ed a message with
5007 * an exclamation point at the end. But I've never seen the
5008 * message, so it must at least be unusual..
5010 * (The same condition is also tested for in gc_free_heap.)
5012 * -- WHN 19991129 */
5013 lose("i586_bzero: page moved, 0x%08x ==> 0x%08x",
5020 page_start = (int *)page_address(first_page);
5021 i586_bzero(page_start, 4096*(last_page-first_page));
5024 first_page = last_page;
5026 } while (first_page < last_free_page);
5028 bytes_allocated -= bytes_freed;
5032 /* Print some information about a pointer at the given address. */
5034 print_ptr(lispobj *addr)
5036 /* If addr is in the dynamic space then out the page information. */
5037 int pi1 = find_page_index((void*)addr);
5040 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
5041 (unsigned int) addr,
5043 page_table[pi1].allocated,
5044 page_table[pi1].gen,
5045 page_table[pi1].bytes_used,
5046 page_table[pi1].first_object_offset,
5047 page_table[pi1].dont_move);
5048 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
5060 extern int undefined_tramp;
5063 verify_space(lispobj *start, size_t words)
5065 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
5066 int is_in_readonly_space =
5067 (READ_ONLY_SPACE_START <= (unsigned)start &&
5068 (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5072 lispobj thing = *(lispobj*)start;
5074 if (Pointerp(thing)) {
5075 int page_index = find_page_index((void*)thing);
5076 int to_readonly_space =
5077 (READ_ONLY_SPACE_START <= thing &&
5078 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5079 int to_static_space =
5080 (STATIC_SPACE_START <= thing &&
5081 thing < SymbolValue(STATIC_SPACE_FREE_POINTER));
5083 /* Does it point to the dynamic space? */
5084 if (page_index != -1) {
5085 /* If it's within the dynamic space it should point to a used
5086 * page. XX Could check the offset too. */
5087 if ((page_table[page_index].allocated != FREE_PAGE)
5088 && (page_table[page_index].bytes_used == 0))
5089 lose ("Ptr %x @ %x sees free page.", thing, start);
5090 /* Check that it doesn't point to a forwarding pointer! */
5091 if (*((lispobj *)PTR(thing)) == 0x01) {
5092 lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
5094 /* Check that its not in the RO space as it would then be a
5095 * pointer from the RO to the dynamic space. */
5096 if (is_in_readonly_space) {
5097 lose("ptr to dynamic space %x from RO space %x",
5100 /* Does it point to a plausible object? This check slows
5101 * it down a lot (so it's commented out).
5103 * FIXME: Add a variable to enable this dynamically. */
5104 /* if (!valid_dynamic_space_pointer((lispobj *)thing)) {
5105 * lose("ptr %x to invalid object %x", thing, start); */
5107 /* Verify that it points to another valid space. */
5108 if (!to_readonly_space && !to_static_space
5109 && (thing != (unsigned)&undefined_tramp)) {
5110 lose("Ptr %x @ %x sees junk.", thing, start);
5114 if (thing & 0x3) { /* Skip fixnums. FIXME: There should be an
5115 * is_fixnum for this. */
5117 switch(TypeOf(*start)) {
5120 case type_SimpleVector:
5123 case type_SimpleArray:
5124 case type_ComplexString:
5125 case type_ComplexBitVector:
5126 case type_ComplexVector:
5127 case type_ComplexArray:
5128 case type_ClosureHeader:
5129 case type_FuncallableInstanceHeader:
5130 case type_ByteCodeFunction:
5131 case type_ByteCodeClosure:
5132 case type_ValueCellHeader:
5133 case type_SymbolHeader:
5135 case type_UnboundMarker:
5136 case type_InstanceHeader:
5141 case type_CodeHeader:
5143 lispobj object = *start;
5145 int nheader_words, ncode_words, nwords;
5147 struct function *fheaderp;
5149 code = (struct code *) start;
5151 /* Check that it's not in the dynamic space.
5152 * FIXME: Isn't is supposed to be OK for code
5153 * objects to be in the dynamic space these days? */
5154 if (is_in_dynamic_space
5155 /* It's ok if it's byte compiled code. The trace
5156 * table offset will be a fixnum if it's x86
5157 * compiled code - check. */
5158 && !(code->trace_table_offset & 0x3)
5159 /* Only when enabled */
5160 && verify_dynamic_code_check) {
5162 "/code object at %x in the dynamic space\n",
5166 ncode_words = fixnum_value(code->code_size);
5167 nheader_words = HeaderValue(object);
5168 nwords = ncode_words + nheader_words;
5169 nwords = CEILING(nwords, 2);
5170 /* Scavenge the boxed section of the code data block */
5171 verify_space(start + 1, nheader_words - 1);
5173 /* Scavenge the boxed section of each function object in
5174 * the code data block. */
5175 fheaderl = code->entry_points;
5176 while (fheaderl != NIL) {
5177 fheaderp = (struct function *) PTR(fheaderl);
5178 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
5179 verify_space(&fheaderp->name, 1);
5180 verify_space(&fheaderp->arglist, 1);
5181 verify_space(&fheaderp->type, 1);
5182 fheaderl = fheaderp->next;
5188 /* unboxed objects */
5190 case type_SingleFloat:
5191 case type_DoubleFloat:
5192 #ifdef type_ComplexLongFloat
5193 case type_LongFloat:
5195 #ifdef type_ComplexSingleFloat
5196 case type_ComplexSingleFloat:
5198 #ifdef type_ComplexDoubleFloat
5199 case type_ComplexDoubleFloat:
5201 #ifdef type_ComplexLongFloat
5202 case type_ComplexLongFloat:
5204 case type_SimpleString:
5205 case type_SimpleBitVector:
5206 case type_SimpleArrayUnsignedByte2:
5207 case type_SimpleArrayUnsignedByte4:
5208 case type_SimpleArrayUnsignedByte8:
5209 case type_SimpleArrayUnsignedByte16:
5210 case type_SimpleArrayUnsignedByte32:
5211 #ifdef type_SimpleArraySignedByte8
5212 case type_SimpleArraySignedByte8:
5214 #ifdef type_SimpleArraySignedByte16
5215 case type_SimpleArraySignedByte16:
5217 #ifdef type_SimpleArraySignedByte30
5218 case type_SimpleArraySignedByte30:
5220 #ifdef type_SimpleArraySignedByte32
5221 case type_SimpleArraySignedByte32:
5223 case type_SimpleArraySingleFloat:
5224 case type_SimpleArrayDoubleFloat:
5225 #ifdef type_SimpleArrayComplexLongFloat
5226 case type_SimpleArrayLongFloat:
5228 #ifdef type_SimpleArrayComplexSingleFloat
5229 case type_SimpleArrayComplexSingleFloat:
5231 #ifdef type_SimpleArrayComplexDoubleFloat
5232 case type_SimpleArrayComplexDoubleFloat:
5234 #ifdef type_SimpleArrayComplexLongFloat
5235 case type_SimpleArrayComplexLongFloat:
5238 case type_WeakPointer:
5239 count = (sizetab[TypeOf(*start)])(start);
5255 /* FIXME: It would be nice to make names consistent so that
5256 * foo_size meant size *in* *bytes* instead of size in some
5257 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
5258 * Some counts of lispobjs are called foo_count; it might be good
5259 * to grep for all foo_size and rename the appropriate ones to
5261 int read_only_space_size =
5262 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER)
5263 - (lispobj*)READ_ONLY_SPACE_START;
5264 int static_space_size =
5265 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER)
5266 - (lispobj*)STATIC_SPACE_START;
5267 int binding_stack_size =
5268 (lispobj*)SymbolValue(BINDING_STACK_POINTER)
5269 - (lispobj*)BINDING_STACK_START;
5271 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
5272 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
5273 verify_space((lispobj*)BINDING_STACK_START , binding_stack_size);
5277 verify_generation(int generation)
5281 for (i = 0; i < last_free_page; i++) {
5282 if ((page_table[i].allocated != FREE_PAGE)
5283 && (page_table[i].bytes_used != 0)
5284 && (page_table[i].gen == generation)) {
5286 int region_allocation = page_table[i].allocated;
5288 /* This should be the start of a contiguous block */
5289 gc_assert(page_table[i].first_object_offset == 0);
5291 /* Need to find the full extent of this contiguous block in case
5292 objects span pages. */
5294 /* Now work forward until the end of this contiguous area is
5296 for (last_page = i; ;last_page++)
5297 /* Check whether this is the last page in this contiguous
5299 if ((page_table[last_page].bytes_used < 4096)
5300 /* Or it is 4096 and is the last in the block */
5301 || (page_table[last_page+1].allocated != region_allocation)
5302 || (page_table[last_page+1].bytes_used == 0)
5303 || (page_table[last_page+1].gen != generation)
5304 || (page_table[last_page+1].first_object_offset == 0))
5307 verify_space(page_address(i), (page_table[last_page].bytes_used
5308 + (last_page-i)*4096)/4);
5314 /* Check the all the free space is zero filled. */
5316 verify_zero_fill(void)
5320 for (page = 0; page < last_free_page; page++) {
5321 if (page_table[page].allocated == FREE_PAGE) {
5322 /* The whole page should be zero filled. */
5323 int *start_addr = (int *)page_address(page);
5326 for (i = 0; i < size; i++) {
5327 if (start_addr[i] != 0) {
5328 lose("free page not zero at %x", start_addr + i);
5332 int free_bytes = 4096 - page_table[page].bytes_used;
5333 if (free_bytes > 0) {
5334 int *start_addr = (int *)((unsigned)page_address(page)
5335 + page_table[page].bytes_used);
5336 int size = free_bytes / 4;
5338 for (i = 0; i < size; i++) {
5339 if (start_addr[i] != 0) {
5340 lose("free region not zero at %x", start_addr + i);
5348 /* External entry point for verify_zero_fill */
5350 gencgc_verify_zero_fill(void)
5352 /* Flush the alloc regions updating the tables. */
5353 boxed_region.free_pointer = current_region_free_pointer;
5354 gc_alloc_update_page_tables(0, &boxed_region);
5355 gc_alloc_update_page_tables(1, &unboxed_region);
5356 SHOW("verifying zero fill");
5358 current_region_free_pointer = boxed_region.free_pointer;
5359 current_region_end_addr = boxed_region.end_addr;
5363 verify_dynamic_space(void)
5367 for (i = 0; i < NUM_GENERATIONS; i++)
5368 verify_generation(i);
5370 if (gencgc_enable_verify_zero_fill)
5374 /* Write-protect all the dynamic boxed pages in the given generation. */
5376 write_protect_generation_pages(int generation)
5380 gc_assert(generation < NUM_GENERATIONS);
5382 for (i = 0; i < last_free_page; i++)
5383 if ((page_table[i].allocated == BOXED_PAGE)
5384 && (page_table[i].bytes_used != 0)
5385 && (page_table[i].gen == generation)) {
5388 page_start = (void *)page_address(i);
5390 os_protect(page_start,
5392 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
5394 /* Note the page as protected in the page tables. */
5395 page_table[i].write_protected = 1;
5398 if (gencgc_verbose > 1) {
5400 "/write protected %d of %d pages in generation %d\n",
5401 count_write_protect_generation_pages(generation),
5402 count_generation_pages(generation),
5407 /* Garbage collect a generation. If raise is 0 the remains of the
5408 * generation are not raised to the next generation. */
5410 garbage_collect_generation(int generation, int raise)
5412 unsigned long bytes_freed;
5414 unsigned long read_only_space_size, static_space_size;
5416 gc_assert(generation <= (NUM_GENERATIONS-1));
5418 /* The oldest generation can't be raised. */
5419 gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
5421 /* Initialize the weak pointer list. */
5422 weak_pointers = NULL;
5424 /* When a generation is not being raised it is transported to a
5425 * temporary generation (NUM_GENERATIONS), and lowered when
5426 * done. Set up this new generation. There should be no pages
5427 * allocated to it yet. */
5429 gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
5431 /* Set the global src and dest. generations */
5432 from_space = generation;
5434 new_space = generation+1;
5436 new_space = NUM_GENERATIONS;
5438 /* Change to a new space for allocation, resetting the alloc_start_page */
5439 gc_alloc_generation = new_space;
5440 generations[new_space].alloc_start_page = 0;
5441 generations[new_space].alloc_unboxed_start_page = 0;
5442 generations[new_space].alloc_large_start_page = 0;
5443 generations[new_space].alloc_large_unboxed_start_page = 0;
5445 /* Before any pointers are preserved, the dont_move flags on the
5446 * pages need to be cleared. */
5447 for (i = 0; i < last_free_page; i++)
5448 page_table[i].dont_move = 0;
5450 /* Un-write-protect the old-space pages. This is essential for the
5451 * promoted pages as they may contain pointers into the old-space
5452 * which need to be scavenged. It also helps avoid unnecessary page
5453 * faults as forwarding pointer are written into them. They need to
5454 * be un-protected anyway before unmapping later. */
5455 unprotect_oldspace();
5457 /* Scavenge the stack's conservative roots. */
5460 for (ptr = (lispobj **)CONTROL_STACK_END - 1;
5461 ptr > (lispobj **)&raise;
5463 preserve_pointer(*ptr);
5466 #ifdef CONTROL_STACKS
5467 scavenge_thread_stacks();
5470 if (gencgc_verbose > 1) {
5471 int num_dont_move_pages = count_dont_move_pages();
5473 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
5474 num_dont_move_pages,
5475 /* FIXME: 4096 should be symbolic constant here and
5476 * prob'ly elsewhere too. */
5477 num_dont_move_pages * 4096));
5480 /* Scavenge all the rest of the roots. */
5482 /* Scavenge the Lisp functions of the interrupt handlers, taking
5483 * care to avoid SIG_DFL, SIG_IGN. */
5484 for (i = 0; i < NSIG; i++) {
5485 union interrupt_handler handler = interrupt_handlers[i];
5486 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
5487 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
5488 scavenge((lispobj *)(interrupt_handlers + i), 1);
5492 /* Scavenge the binding stack. */
5493 scavenge( (lispobj *) BINDING_STACK_START,
5494 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
5495 (lispobj *)BINDING_STACK_START);
5497 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
5498 read_only_space_size =
5499 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
5500 (lispobj*)READ_ONLY_SPACE_START;
5502 "/scavenge read only space: %d bytes\n",
5503 read_only_space_size * sizeof(lispobj)));
5504 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
5508 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER) -
5509 (lispobj *)STATIC_SPACE_START;
5510 if (gencgc_verbose > 1)
5512 "/scavenge static space: %d bytes\n",
5513 static_space_size * sizeof(lispobj)));
5514 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
5516 /* All generations but the generation being GCed need to be
5517 * scavenged. The new_space generation needs special handling as
5518 * objects may be moved in - it is handled separately below. */
5519 for (i = 0; i < NUM_GENERATIONS; i++)
5520 if ((i != generation) && (i != new_space))
5521 scavenge_generation(i);
5523 /* Finally scavenge the new_space generation. Keep going until no
5524 * more objects are moved into the new generation */
5525 scavenge_newspace_generation(new_space);
5527 #define RESCAN_CHECK 0
5529 /* As a check re-scavenge the newspace once; no new objects should
5532 int old_bytes_allocated = bytes_allocated;
5533 int bytes_allocated;
5535 /* Start with a full scavenge. */
5536 scavenge_newspace_generation_one_scan(new_space);
5538 /* Flush the current regions, updating the tables. */
5539 gc_alloc_update_page_tables(0, &boxed_region);
5540 gc_alloc_update_page_tables(1, &unboxed_region);
5542 bytes_allocated = bytes_allocated - old_bytes_allocated;
5544 if (bytes_allocated != 0) {
5545 lose("Rescan of new_space allocated %d more bytes.",
5551 scan_weak_pointers();
5553 /* Flush the current regions, updating the tables. */
5554 gc_alloc_update_page_tables(0, &boxed_region);
5555 gc_alloc_update_page_tables(1, &unboxed_region);
5557 /* Free the pages in oldspace, but not those marked dont_move. */
5558 bytes_freed = free_oldspace();
5560 /* If the GC is not raising the age then lower the generation back
5561 * to its normal generation number */
5563 for (i = 0; i < last_free_page; i++)
5564 if ((page_table[i].bytes_used != 0)
5565 && (page_table[i].gen == NUM_GENERATIONS))
5566 page_table[i].gen = generation;
5567 gc_assert(generations[generation].bytes_allocated == 0);
5568 generations[generation].bytes_allocated =
5569 generations[NUM_GENERATIONS].bytes_allocated;
5570 generations[NUM_GENERATIONS].bytes_allocated = 0;
5573 /* Reset the alloc_start_page for generation. */
5574 generations[generation].alloc_start_page = 0;
5575 generations[generation].alloc_unboxed_start_page = 0;
5576 generations[generation].alloc_large_start_page = 0;
5577 generations[generation].alloc_large_unboxed_start_page = 0;
5579 if (generation >= verify_gens) {
5583 verify_dynamic_space();
5586 /* Set the new gc trigger for the GCed generation. */
5587 generations[generation].gc_trigger =
5588 generations[generation].bytes_allocated
5589 + generations[generation].bytes_consed_between_gc;
5592 generations[generation].num_gc = 0;
5594 ++generations[generation].num_gc;
5597 /* Update last_free_page then ALLOCATION_POINTER */
5599 update_x86_dynamic_space_free_pointer(void)
5604 for (i = 0; i < NUM_PAGES; i++)
5605 if ((page_table[i].allocated != FREE_PAGE)
5606 && (page_table[i].bytes_used != 0))
5609 last_free_page = last_page+1;
5611 SetSymbolValue(ALLOCATION_POINTER,
5612 (lispobj)(((char *)heap_base) + last_free_page*4096));
5613 return 0; /* dummy value: return something ... */
5616 /* GC all generations below last_gen, raising their objects to the
5617 * next generation until all generations below last_gen are empty.
5618 * Then if last_gen is due for a GC then GC it. In the special case
5619 * that last_gen==NUM_GENERATIONS, the last generation is always
5620 * GC'ed. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
5622 * The oldest generation to be GCed will always be
5623 * gencgc_oldest_gen_to_gc, partly ignoring last_gen if necessary. */
5625 collect_garbage(unsigned last_gen)
5632 boxed_region.free_pointer = current_region_free_pointer;
5634 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
5636 if (last_gen > NUM_GENERATIONS) {
5638 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
5643 /* Flush the alloc regions updating the tables. */
5644 gc_alloc_update_page_tables(0, &boxed_region);
5645 gc_alloc_update_page_tables(1, &unboxed_region);
5647 /* Verify the new objects created by Lisp code. */
5648 if (pre_verify_gen_0) {
5649 SHOW((stderr, "pre-checking generation 0\n"));
5650 verify_generation(0);
5653 if (gencgc_verbose > 1)
5654 print_generation_stats(0);
5657 /* Collect the generation. */
5659 if (gen >= gencgc_oldest_gen_to_gc) {
5660 /* Never raise the oldest generation. */
5665 || (generations[gen].num_gc >= generations[gen].trigger_age);
5668 if (gencgc_verbose > 1) {
5670 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
5673 generations[gen].bytes_allocated,
5674 generations[gen].gc_trigger,
5675 generations[gen].num_gc));
5678 /* If an older generation is being filled, then update its
5681 generations[gen+1].cum_sum_bytes_allocated +=
5682 generations[gen+1].bytes_allocated;
5685 garbage_collect_generation(gen, raise);
5687 /* Reset the memory age cum_sum. */
5688 generations[gen].cum_sum_bytes_allocated = 0;
5690 if (gencgc_verbose > 1) {
5691 FSHOW((stderr, "GC of generation %d finished:\n", gen));
5692 print_generation_stats(0);
5696 } while ((gen <= gencgc_oldest_gen_to_gc)
5697 && ((gen < last_gen)
5698 || ((gen <= gencgc_oldest_gen_to_gc)
5700 && (generations[gen].bytes_allocated
5701 > generations[gen].gc_trigger)
5702 && (gen_av_mem_age(gen)
5703 > generations[gen].min_av_mem_age))));
5705 /* Now if gen-1 was raised all generations before gen are empty.
5706 * If it wasn't raised then all generations before gen-1 are empty.
5708 * Now objects within this gen's pages cannot point to younger
5709 * generations unless they are written to. This can be exploited
5710 * by write-protecting the pages of gen; then when younger
5711 * generations are GCed only the pages which have been written
5716 gen_to_wp = gen - 1;
5718 /* There's not much point in WPing pages in generation 0 as it is
5719 * never scavenged (except promoted pages). */
5720 if ((gen_to_wp > 0) && enable_page_protection) {
5721 /* Check that they are all empty. */
5722 for (i = 0; i < gen_to_wp; i++) {
5723 if (generations[i].bytes_allocated)
5724 lose("trying to write-protect gen. %d when gen. %d nonempty",
5727 write_protect_generation_pages(gen_to_wp);
5730 /* Set gc_alloc back to generation 0. The current regions should
5731 * be flushed after the above GCs */
5732 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
5733 gc_alloc_generation = 0;
5735 update_x86_dynamic_space_free_pointer();
5737 /* This is now done by Lisp SCRUB-CONTROL-STACK in Lisp SUB-GC, so we
5738 * needn't do it here: */
5741 current_region_free_pointer = boxed_region.free_pointer;
5742 current_region_end_addr = boxed_region.end_addr;
5744 SHOW("returning from collect_garbage");
5747 /* This is called by Lisp PURIFY when it is finished. All live objects
5748 * will have been moved to the RO and Static heaps. The dynamic space
5749 * will need a full re-initialization. We don't bother having Lisp
5750 * PURIFY flush the current gc_alloc region, as the page_tables are
5751 * re-initialized, and every page is zeroed to be sure. */
5757 if (gencgc_verbose > 1)
5758 SHOW("entering gc_free_heap");
5760 for (page = 0; page < NUM_PAGES; page++) {
5761 /* Skip free pages which should already be zero filled. */
5762 if (page_table[page].allocated != FREE_PAGE) {
5763 void *page_start, *addr;
5765 /* Mark the page free. The other slots are assumed invalid
5766 * when it is a FREE_PAGE and bytes_used is 0 and it
5767 * should not be write-protected -- except that the
5768 * generation is used for the current region but it sets
5770 page_table[page].allocated = FREE_PAGE;
5771 page_table[page].bytes_used = 0;
5773 /* Zero the page. */
5774 page_start = (void *)page_address(page);
5776 /* First, remove any write-protection. */
5777 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5778 page_table[page].write_protected = 0;
5780 os_invalidate(page_start,4096);
5781 addr = os_validate(page_start,4096);
5782 if (addr == NULL || addr != page_start) {
5783 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
5787 } else if (gencgc_zero_check_during_free_heap) {
5788 /* Double-check that the page is zero filled. */
5790 gc_assert(page_table[page].allocated == FREE_PAGE);
5791 gc_assert(page_table[page].bytes_used == 0);
5792 page_start = (int *)page_address(page);
5793 for (i=0; i<1024; i++) {
5794 if (page_start[i] != 0) {
5795 lose("free region not zero at %x", page_start + i);
5801 bytes_allocated = 0;
5803 /* Initialize the generations. */
5804 for (page = 0; page < NUM_GENERATIONS; page++) {
5805 generations[page].alloc_start_page = 0;
5806 generations[page].alloc_unboxed_start_page = 0;
5807 generations[page].alloc_large_start_page = 0;
5808 generations[page].alloc_large_unboxed_start_page = 0;
5809 generations[page].bytes_allocated = 0;
5810 generations[page].gc_trigger = 2000000;
5811 generations[page].num_gc = 0;
5812 generations[page].cum_sum_bytes_allocated = 0;
5815 if (gencgc_verbose > 1)
5816 print_generation_stats(0);
5818 /* Initialize gc_alloc */
5819 gc_alloc_generation = 0;
5820 boxed_region.first_page = 0;
5821 boxed_region.last_page = -1;
5822 boxed_region.start_addr = page_address(0);
5823 boxed_region.free_pointer = page_address(0);
5824 boxed_region.end_addr = page_address(0);
5826 unboxed_region.first_page = 0;
5827 unboxed_region.last_page = -1;
5828 unboxed_region.start_addr = page_address(0);
5829 unboxed_region.free_pointer = page_address(0);
5830 unboxed_region.end_addr = page_address(0);
5832 #if 0 /* Lisp PURIFY is currently running on the C stack so don't do this. */
5837 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base));
5839 current_region_free_pointer = boxed_region.free_pointer;
5840 current_region_end_addr = boxed_region.end_addr;
5842 if (verify_after_free_heap) {
5843 /* Check whether purify has left any bad pointers. */
5845 SHOW("checking after free_heap\n");
5857 heap_base = (void*)DYNAMIC_SPACE_START;
5859 /* Initialize each page structure. */
5860 for (i = 0; i < NUM_PAGES; i++) {
5861 /* Initialize all pages as free. */
5862 page_table[i].allocated = FREE_PAGE;
5863 page_table[i].bytes_used = 0;
5865 /* Pages are not write-protected at startup. */
5866 page_table[i].write_protected = 0;
5869 bytes_allocated = 0;
5871 /* Initialize the generations. */
5872 for (i = 0; i < NUM_GENERATIONS; i++) {
5873 generations[i].alloc_start_page = 0;
5874 generations[i].alloc_unboxed_start_page = 0;
5875 generations[i].alloc_large_start_page = 0;
5876 generations[i].alloc_large_unboxed_start_page = 0;
5877 generations[i].bytes_allocated = 0;
5878 generations[i].gc_trigger = 2000000;
5879 generations[i].num_gc = 0;
5880 generations[i].cum_sum_bytes_allocated = 0;
5881 /* the tune-able parameters */
5882 generations[i].bytes_consed_between_gc = 2000000;
5883 generations[i].trigger_age = 1;
5884 generations[i].min_av_mem_age = 0.75;
5887 /* Initialize gc_alloc. */
5888 gc_alloc_generation = 0;
5889 boxed_region.first_page = 0;
5890 boxed_region.last_page = -1;
5891 boxed_region.start_addr = page_address(0);
5892 boxed_region.free_pointer = page_address(0);
5893 boxed_region.end_addr = page_address(0);
5895 unboxed_region.first_page = 0;
5896 unboxed_region.last_page = -1;
5897 unboxed_region.start_addr = page_address(0);
5898 unboxed_region.free_pointer = page_address(0);
5899 unboxed_region.end_addr = page_address(0);
5903 current_region_free_pointer = boxed_region.free_pointer;
5904 current_region_end_addr = boxed_region.end_addr;
5907 /* Pick up the dynamic space from after a core load.
5909 * The ALLOCATION_POINTER points to the end of the dynamic space.
5911 * XX A scan is needed to identify the closest first objects for pages. */
5913 gencgc_pickup_dynamic(void)
5916 int addr = DYNAMIC_SPACE_START;
5917 int alloc_ptr = SymbolValue(ALLOCATION_POINTER);
5919 /* Initialize the first region. */
5921 page_table[page].allocated = BOXED_PAGE;
5922 page_table[page].gen = 0;
5923 page_table[page].bytes_used = 4096;
5924 page_table[page].large_object = 0;
5925 page_table[page].first_object_offset =
5926 (void *)DYNAMIC_SPACE_START - page_address(page);
5929 } while (addr < alloc_ptr);
5931 generations[0].bytes_allocated = 4096*page;
5932 bytes_allocated = 4096*page;
5934 current_region_free_pointer = boxed_region.free_pointer;
5935 current_region_end_addr = boxed_region.end_addr;
5938 /* a counter for how deep we are in alloc(..) calls */
5939 int alloc_entered = 0;
5941 /* alloc(..) is the external interface for memory allocation. It
5942 * allocates to generation 0. It is not called from within the garbage
5943 * collector as it is only external uses that need the check for heap
5944 * size (GC trigger) and to disable the interrupts (interrupts are
5945 * always disabled during a GC).
5947 * The vops that call alloc(..) assume that the returned space is zero-filled.
5948 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
5950 * The check for a GC trigger is only performed when the current
5951 * region is full, so in most cases it's not needed. Further MAYBE-GC
5952 * is only called once because Lisp will remember "need to collect
5953 * garbage" and get around to it when it can. */
5957 /* Check for alignment allocation problems. */
5958 gc_assert((((unsigned)current_region_free_pointer & 0x7) == 0)
5959 && ((nbytes & 0x7) == 0));
5961 if (SymbolValue(PSEUDO_ATOMIC_ATOMIC)) {/* if already in a pseudo atomic */
5963 void *new_free_pointer;
5966 if (alloc_entered) {
5967 SHOW("alloc re-entered in already-pseudo-atomic case");
5971 /* Check whether there is room in the current region. */
5972 new_free_pointer = current_region_free_pointer + nbytes;
5974 /* FIXME: Shouldn't we be doing some sort of lock here, to
5975 * keep from getting screwed if an interrupt service routine
5976 * allocates memory between the time we calculate new_free_pointer
5977 * and the time we write it back to current_region_free_pointer?
5978 * Perhaps I just don't understand pseudo-atomics..
5980 * Perhaps I don't. It looks as though what happens is if we
5981 * were interrupted any time during the pseudo-atomic
5982 * interval (which includes now) we discard the allocated
5983 * memory and try again. So, at least we don't return
5984 * a memory area that was allocated out from underneath us
5985 * by code in an ISR.
5986 * Still, that doesn't seem to prevent
5987 * current_region_free_pointer from getting corrupted:
5988 * We read current_region_free_pointer.
5989 * They read current_region_free_pointer.
5990 * They write current_region_free_pointer.
5991 * We write current_region_free_pointer, scribbling over
5992 * whatever they wrote. */
5994 if (new_free_pointer <= boxed_region.end_addr) {
5995 /* If so then allocate from the current region. */
5996 void *new_obj = current_region_free_pointer;
5997 current_region_free_pointer = new_free_pointer;
5999 return((void *)new_obj);
6002 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
6003 /* Double the trigger. */
6004 auto_gc_trigger *= 2;
6006 /* Exit the pseudo-atomic. */
6007 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6008 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6009 /* Handle any interrupts that occurred during
6011 do_pending_interrupt();
6013 funcall0(SymbolFunction(MAYBE_GC));
6014 /* Re-enter the pseudo-atomic. */
6015 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0));
6016 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1));
6019 /* Call gc_alloc. */
6020 boxed_region.free_pointer = current_region_free_pointer;
6022 void *new_obj = gc_alloc(nbytes);
6023 current_region_free_pointer = boxed_region.free_pointer;
6024 current_region_end_addr = boxed_region.end_addr;
6030 void *new_free_pointer;
6033 /* At least wrap this allocation in a pseudo atomic to prevent
6034 * gc_alloc from being re-entered. */
6035 SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(0));
6036 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(1));
6039 SHOW("alloc re-entered in not-already-pseudo-atomic case");
6042 /* Check whether there is room in the current region. */
6043 new_free_pointer = current_region_free_pointer + nbytes;
6045 if (new_free_pointer <= boxed_region.end_addr) {
6046 /* If so then allocate from the current region. */
6047 void *new_obj = current_region_free_pointer;
6048 current_region_free_pointer = new_free_pointer;
6050 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6051 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED)) {
6052 /* Handle any interrupts that occurred during
6054 do_pending_interrupt();
6058 return((void *)new_obj);
6061 /* KLUDGE: There's lots of code around here shared with the
6062 * the other branch. Is there some way to factor out the
6063 * duplicate code? -- WHN 19991129 */
6064 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
6065 /* Double the trigger. */
6066 auto_gc_trigger *= 2;
6068 /* Exit the pseudo atomic. */
6069 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6070 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6071 /* Handle any interrupts that occurred during
6073 do_pending_interrupt();
6075 funcall0(SymbolFunction(MAYBE_GC));
6079 /* Else call gc_alloc. */
6080 boxed_region.free_pointer = current_region_free_pointer;
6081 result = gc_alloc(nbytes);
6082 current_region_free_pointer = boxed_region.free_pointer;
6083 current_region_end_addr = boxed_region.end_addr;
6086 SetSymbolValue(PSEUDO_ATOMIC_ATOMIC, make_fixnum(0));
6087 if (SymbolValue(PSEUDO_ATOMIC_INTERRUPTED) != 0) {
6088 /* Handle any interrupts that occurred during
6090 do_pending_interrupt();
6099 * noise to manipulate the gc trigger stuff
6103 set_auto_gc_trigger(os_vm_size_t dynamic_usage)
6105 auto_gc_trigger += dynamic_usage;
6109 clear_auto_gc_trigger(void)
6111 auto_gc_trigger = 0;
6114 /* Find the code object for the given pc, or return NULL on failure.
6116 * FIXME: PC shouldn't be lispobj*, should it? Maybe void*? */
6118 component_ptr_from_pc(lispobj *pc)
6120 lispobj *object = NULL;
6122 if ( (object = search_read_only_space(pc)) )
6124 else if ( (object = search_static_space(pc)) )
6127 object = search_dynamic_space(pc);
6129 if (object) /* if we found something */
6130 if (TypeOf(*object) == type_CodeHeader) /* if it's a code object */
6137 * shared support for the OS-dependent signal handlers which
6138 * catch GENCGC-related write-protect violations
6141 void unhandled_sigmemoryfault(void);
6143 /* Depending on which OS we're running under, different signals might
6144 * be raised for a violation of write protection in the heap. This
6145 * function factors out the common generational GC magic which needs
6146 * to invoked in this case, and should be called from whatever signal
6147 * handler is appropriate for the OS we're running under.
6149 * Return true if this signal is a normal generational GC thing that
6150 * we were able to handle, or false if it was abnormal and control
6151 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
6153 gencgc_handle_wp_violation(void* fault_addr)
6155 int page_index = find_page_index(fault_addr);
6157 #if defined QSHOW_SIGNALS
6158 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
6159 fault_addr, page_index));
6162 /* Check whether the fault is within the dynamic space. */
6163 if (page_index == (-1)) {
6165 /* It can be helpful to be able to put a breakpoint on this
6166 * case to help diagnose low-level problems. */
6167 unhandled_sigmemoryfault();
6169 /* not within the dynamic space -- not our responsibility */
6174 /* The only acceptable reason for an signal like this from the
6175 * heap is that the generational GC write-protected the page. */
6176 if (page_table[page_index].write_protected != 1) {
6177 lose("access failure in heap page not marked as write-protected");
6180 /* Unprotect the page. */
6181 os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
6182 page_table[page_index].write_protected = 0;
6183 page_table[page_index].write_protected_cleared = 1;
6185 /* Don't worry, we can handle it. */
6190 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
6191 * it's not just a case of the program hitting the write barrier, and
6192 * are about to let Lisp deal with it. It's basically just a
6193 * convenient place to set a gdb breakpoint. */
6195 unhandled_sigmemoryfault()