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>.
34 #include "interrupt.h"
41 /* a function defined externally in assembly language, called from
43 void do_pending_interrupt(void);
49 /* the number of actual generations. (The number of 'struct
50 * generation' objects is one more than this, because one object
51 * serves as scratch when GC'ing.) */
52 #define NUM_GENERATIONS 6
54 /* Should we use page protection to help avoid the scavenging of pages
55 * that don't have pointers to younger generations? */
56 boolean enable_page_protection = 1;
58 /* Should we unmap a page and re-mmap it to have it zero filled? */
59 #if defined(__FreeBSD__) || defined(__OpenBSD__)
60 /* comment from cmucl-2.4.8: This can waste a lot of swap on FreeBSD
61 * so don't unmap there.
63 * The CMU CL comment didn't specify a version, but was probably an
64 * old version of FreeBSD (pre-4.0), so this might no longer be true.
65 * OTOH, if it is true, this behavior might exist on OpenBSD too, so
66 * for now we don't unmap there either. -- WHN 2001-04-07 */
67 boolean gencgc_unmap_zero = 0;
69 boolean gencgc_unmap_zero = 1;
72 /* the minimum size (in bytes) for a large object*/
73 unsigned large_object_size = 4 * 4096;
79 #define gc_abort() lose("GC invariant lost, file \"%s\", line %d", \
82 /* FIXME: In CMU CL, this was "#if 0" with no explanation. Find out
83 * how much it costs to make it "#if 1". If it's not too expensive,
86 #define gc_assert(ex) do { \
87 if (!(ex)) gc_abort(); \
93 /* the verbosity level. All non-error messages are disabled at level 0;
94 * and only a few rare messages are printed at level 1. */
95 unsigned gencgc_verbose = (QSHOW ? 1 : 0);
97 /* FIXME: At some point enable the various error-checking things below
98 * and see what they say. */
100 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
101 * Set verify_gens to NUM_GENERATIONS to disable this kind of check. */
102 int verify_gens = NUM_GENERATIONS;
104 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
105 boolean pre_verify_gen_0 = 0;
107 /* Should we check for bad pointers after gc_free_heap is called
108 * from Lisp PURIFY? */
109 boolean verify_after_free_heap = 0;
111 /* Should we print a note when code objects are found in the dynamic space
112 * during a heap verify? */
113 boolean verify_dynamic_code_check = 0;
115 /* Should we check code objects for fixup errors after they are transported? */
116 boolean check_code_fixups = 0;
118 /* Should we check that newly allocated regions are zero filled? */
119 boolean gencgc_zero_check = 0;
121 /* Should we check that the free space is zero filled? */
122 boolean gencgc_enable_verify_zero_fill = 0;
124 /* Should we check that free pages are zero filled during gc_free_heap
125 * called after Lisp PURIFY? */
126 boolean gencgc_zero_check_during_free_heap = 0;
129 * GC structures and variables
132 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
133 unsigned long bytes_allocated = 0;
134 static unsigned long auto_gc_trigger = 0;
136 /* the source and destination generations. These are set before a GC starts
138 static int from_space;
139 static int new_space;
141 /* FIXME: It would be nice to use this symbolic constant instead of
142 * bare 4096 almost everywhere. We could also use an assertion that
143 * it's equal to getpagesize(). */
144 #define PAGE_BYTES 4096
146 /* An array of page structures is statically allocated.
147 * This helps quickly map between an address its page structure.
148 * NUM_PAGES is set from the size of the dynamic space. */
149 struct page page_table[NUM_PAGES];
151 /* To map addresses to page structures the address of the first page
153 static void *heap_base = NULL;
155 /* Calculate the start address for the given page number. */
157 page_address(int page_num)
159 return (heap_base + (page_num * 4096));
162 /* Find the page index within the page_table for the given
163 * address. Return -1 on failure. */
165 find_page_index(void *addr)
167 int index = addr-heap_base;
170 index = ((unsigned int)index)/4096;
171 if (index < NUM_PAGES)
178 /* a structure to hold the state of a generation */
181 /* the first page that gc_alloc() checks on its next call */
182 int alloc_start_page;
184 /* the first page that gc_alloc_unboxed() checks on its next call */
185 int alloc_unboxed_start_page;
187 /* the first page that gc_alloc_large (boxed) considers on its next
188 * call. (Although it always allocates after the boxed_region.) */
189 int alloc_large_start_page;
191 /* the first page that gc_alloc_large (unboxed) considers on its
192 * next call. (Although it always allocates after the
193 * current_unboxed_region.) */
194 int alloc_large_unboxed_start_page;
196 /* the bytes allocated to this generation */
199 /* the number of bytes at which to trigger a GC */
202 /* to calculate a new level for gc_trigger */
203 int bytes_consed_between_gc;
205 /* the number of GCs since the last raise */
208 /* the average age after which a GC will raise objects to the
212 /* the cumulative sum of the bytes allocated to this generation. It is
213 * cleared after a GC on this generations, and update before new
214 * objects are added from a GC of a younger generation. Dividing by
215 * the bytes_allocated will give the average age of the memory in
216 * this generation since its last GC. */
217 int cum_sum_bytes_allocated;
219 /* a minimum average memory age before a GC will occur helps
220 * prevent a GC when a large number of new live objects have been
221 * added, in which case a GC could be a waste of time */
222 double min_av_mem_age;
225 /* an array of generation structures. There needs to be one more
226 * generation structure than actual generations as the oldest
227 * generation is temporarily raised then lowered. */
228 static struct generation generations[NUM_GENERATIONS+1];
230 /* the oldest generation that is will currently be GCed by default.
231 * Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
233 * The default of (NUM_GENERATIONS-1) enables GC on all generations.
235 * Setting this to 0 effectively disables the generational nature of
236 * the GC. In some applications generational GC may not be useful
237 * because there are no long-lived objects.
239 * An intermediate value could be handy after moving long-lived data
240 * into an older generation so an unnecessary GC of this long-lived
241 * data can be avoided. */
242 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
244 /* The maximum free page in the heap is maintained and used to update
245 * ALLOCATION_POINTER which is used by the room function to limit its
246 * search of the heap. XX Gencgc obviously needs to be better
247 * integrated with the Lisp code. */
248 static int last_free_page;
249 static int last_used_page = 0;
252 * miscellaneous heap functions
255 /* Count the number of pages which are write-protected within the
256 * given generation. */
258 count_write_protect_generation_pages(int generation)
263 for (i = 0; i < last_free_page; i++)
264 if ((page_table[i].allocated != FREE_PAGE)
265 && (page_table[i].gen == generation)
266 && (page_table[i].write_protected == 1))
271 /* Count the number of pages within the given generation. */
273 count_generation_pages(int generation)
278 for (i = 0; i < last_free_page; i++)
279 if ((page_table[i].allocated != 0)
280 && (page_table[i].gen == generation))
285 /* Count the number of dont_move pages. */
287 count_dont_move_pages(void)
291 for (i = 0; i < last_free_page; i++) {
292 if ((page_table[i].allocated != 0) && (page_table[i].dont_move != 0)) {
299 /* Work through the pages and add up the number of bytes used for the
300 * given generation. */
302 count_generation_bytes_allocated (int gen)
306 for (i = 0; i < last_free_page; i++) {
307 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
308 result += page_table[i].bytes_used;
313 /* Return the average age of the memory in a generation. */
315 gen_av_mem_age(int gen)
317 if (generations[gen].bytes_allocated == 0)
321 ((double)generations[gen].cum_sum_bytes_allocated)
322 / ((double)generations[gen].bytes_allocated);
325 /* The verbose argument controls how much to print: 0 for normal
326 * level of detail; 1 for debugging. */
328 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
333 /* This code uses the FP instructions which may be set up for Lisp
334 * so they need to be saved and reset for C. */
337 /* number of generations to print */
339 gens = NUM_GENERATIONS+1;
341 gens = NUM_GENERATIONS;
343 /* Print the heap stats. */
345 " Generation Boxed Unboxed LB LUB Alloc Waste Trig WP GCs Mem-age\n");
347 for (i = 0; i < gens; i++) {
351 int large_boxed_cnt = 0;
352 int large_unboxed_cnt = 0;
354 for (j = 0; j < last_free_page; j++)
355 if (page_table[j].gen == i) {
357 /* Count the number of boxed pages within the given
359 if (page_table[j].allocated == BOXED_PAGE) {
360 if (page_table[j].large_object)
366 /* Count the number of unboxed pages within the given
368 if (page_table[j].allocated == UNBOXED_PAGE) {
369 if (page_table[j].large_object)
376 gc_assert(generations[i].bytes_allocated
377 == count_generation_bytes_allocated(i));
379 " %8d: %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4f\n",
381 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
382 generations[i].bytes_allocated,
383 (count_generation_pages(i)*4096
384 - generations[i].bytes_allocated),
385 generations[i].gc_trigger,
386 count_write_protect_generation_pages(i),
387 generations[i].num_gc,
390 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
392 fpu_restore(fpu_state);
396 * allocation routines
400 * To support quick and inline allocation, regions of memory can be
401 * allocated and then allocated from with just a free pointer and a
402 * check against an end address.
404 * Since objects can be allocated to spaces with different properties
405 * e.g. boxed/unboxed, generation, ages; there may need to be many
406 * allocation regions.
408 * Each allocation region may be start within a partly used page. Many
409 * features of memory use are noted on a page wise basis, e.g. the
410 * generation; so if a region starts within an existing allocated page
411 * it must be consistent with this page.
413 * During the scavenging of the newspace, objects will be transported
414 * into an allocation region, and pointers updated to point to this
415 * allocation region. It is possible that these pointers will be
416 * scavenged again before the allocation region is closed, e.g. due to
417 * trans_list which jumps all over the place to cleanup the list. It
418 * is important to be able to determine properties of all objects
419 * pointed to when scavenging, e.g to detect pointers to the oldspace.
420 * Thus it's important that the allocation regions have the correct
421 * properties set when allocated, and not just set when closed. The
422 * region allocation routines return regions with the specified
423 * properties, and grab all the pages, setting their properties
424 * appropriately, except that the amount used is not known.
426 * These regions are used to support quicker allocation using just a
427 * free pointer. The actual space used by the region is not reflected
428 * in the pages tables until it is closed. It can't be scavenged until
431 * When finished with the region it should be closed, which will
432 * update the page tables for the actual space used returning unused
433 * space. Further it may be noted in the new regions which is
434 * necessary when scavenging the newspace.
436 * Large objects may be allocated directly without an allocation
437 * region, the page tables are updated immediately.
439 * Unboxed objects don't contain pointers to other objects and so
440 * don't need scavenging. Further they can't contain pointers to
441 * younger generations so WP is not needed. By allocating pages to
442 * unboxed objects the whole page never needs scavenging or
443 * write-protecting. */
445 /* We are only using two regions at present. Both are for the current
446 * newspace generation. */
447 struct alloc_region boxed_region;
448 struct alloc_region unboxed_region;
450 /* XX hack. Current Lisp code uses the following. Need copying in/out. */
451 void *current_region_free_pointer;
452 void *current_region_end_addr;
454 /* The generation currently being allocated to. */
455 static int gc_alloc_generation;
457 /* Find a new region with room for at least the given number of bytes.
459 * It starts looking at the current generation's alloc_start_page. So
460 * may pick up from the previous region if there is enough space. This
461 * keeps the allocation contiguous when scavenging the newspace.
463 * The alloc_region should have been closed by a call to
464 * gc_alloc_update_page_tables(), and will thus be in an empty state.
466 * To assist the scavenging functions write-protected pages are not
467 * used. Free pages should not be write-protected.
469 * It is critical to the conservative GC that the start of regions be
470 * known. To help achieve this only small regions are allocated at a
473 * During scavenging, pointers may be found to within the current
474 * region and the page generation must be set so that pointers to the
475 * from space can be recognized. Therefore the generation of pages in
476 * the region are set to gc_alloc_generation. To prevent another
477 * allocation call using the same pages, all the pages in the region
478 * are allocated, although they will initially be empty.
481 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
493 "/alloc_new_region for %d bytes from gen %d\n",
494 nbytes, gc_alloc_generation));
497 /* Check that the region is in a reset state. */
498 gc_assert((alloc_region->first_page == 0)
499 && (alloc_region->last_page == -1)
500 && (alloc_region->free_pointer == alloc_region->end_addr));
504 generations[gc_alloc_generation].alloc_unboxed_start_page;
507 generations[gc_alloc_generation].alloc_start_page;
510 /* Search for a contiguous free region of at least nbytes with the
511 * given properties: boxed/unboxed, generation. */
513 first_page = restart_page;
515 /* First search for a page with at least 32 bytes free, which is
516 * not write-protected, and which is not marked dont_move.
518 * FIXME: This looks extremely similar, perhaps identical, to
519 * code in gc_alloc_large(). It should be shared somehow. */
520 while ((first_page < NUM_PAGES)
521 && (page_table[first_page].allocated != FREE_PAGE) /* not free page */
523 (page_table[first_page].allocated != UNBOXED_PAGE))
525 (page_table[first_page].allocated != BOXED_PAGE))
526 || (page_table[first_page].large_object != 0)
527 || (page_table[first_page].gen != gc_alloc_generation)
528 || (page_table[first_page].bytes_used >= (4096-32))
529 || (page_table[first_page].write_protected != 0)
530 || (page_table[first_page].dont_move != 0)))
532 /* Check for a failure. */
533 if (first_page >= NUM_PAGES) {
535 "Argh! gc_alloc_new_region failed on first_page, nbytes=%d.\n",
537 print_generation_stats(1);
541 gc_assert(page_table[first_page].write_protected == 0);
545 "/first_page=%d bytes_used=%d\n",
546 first_page, page_table[first_page].bytes_used));
549 /* Now search forward to calculate the available region size. It
550 * tries to keeps going until nbytes are found and the number of
551 * pages is greater than some level. This helps keep down the
552 * number of pages in a region. */
553 last_page = first_page;
554 bytes_found = 4096 - page_table[first_page].bytes_used;
556 while (((bytes_found < nbytes) || (num_pages < 2))
557 && (last_page < (NUM_PAGES-1))
558 && (page_table[last_page+1].allocated == FREE_PAGE)) {
562 gc_assert(page_table[last_page].write_protected == 0);
565 region_size = (4096 - page_table[first_page].bytes_used)
566 + 4096*(last_page-first_page);
568 gc_assert(bytes_found == region_size);
572 "/last_page=%d bytes_found=%d num_pages=%d\n",
573 last_page, bytes_found, num_pages));
576 restart_page = last_page + 1;
577 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
579 /* Check for a failure. */
580 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
582 "Argh! gc_alloc_new_region() failed on restart_page, nbytes=%d.\n",
584 print_generation_stats(1);
590 "/gc_alloc_new_region() gen %d: %d bytes: pages %d to %d: addr=%x\n",
595 page_address(first_page)));
598 /* Set up the alloc_region. */
599 alloc_region->first_page = first_page;
600 alloc_region->last_page = last_page;
601 alloc_region->start_addr = page_table[first_page].bytes_used
602 + page_address(first_page);
603 alloc_region->free_pointer = alloc_region->start_addr;
604 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
606 if (gencgc_zero_check) {
608 for (p = (int *)alloc_region->start_addr;
609 p < (int *)alloc_region->end_addr; p++) {
611 /* KLUDGE: It would be nice to use %lx and explicit casts
612 * (long) in code like this, so that it is less likely to
613 * break randomly when running on a machine with different
614 * word sizes. -- WHN 19991129 */
615 lose("The new region at %x is not zero.", p);
620 /* Set up the pages. */
622 /* The first page may have already been in use. */
623 if (page_table[first_page].bytes_used == 0) {
625 page_table[first_page].allocated = UNBOXED_PAGE;
627 page_table[first_page].allocated = BOXED_PAGE;
628 page_table[first_page].gen = gc_alloc_generation;
629 page_table[first_page].large_object = 0;
630 page_table[first_page].first_object_offset = 0;
634 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
636 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
637 gc_assert(page_table[first_page].gen == gc_alloc_generation);
638 gc_assert(page_table[first_page].large_object == 0);
640 for (i = first_page+1; i <= last_page; i++) {
642 page_table[i].allocated = UNBOXED_PAGE;
644 page_table[i].allocated = BOXED_PAGE;
645 page_table[i].gen = gc_alloc_generation;
646 page_table[i].large_object = 0;
647 /* This may not be necessary for unboxed regions (think it was
649 page_table[i].first_object_offset =
650 alloc_region->start_addr - page_address(i);
653 /* Bump up last_free_page. */
654 if (last_page+1 > last_free_page) {
655 last_free_page = last_page+1;
656 SetSymbolValue(ALLOCATION_POINTER,
657 (lispobj)(((char *)heap_base) + last_free_page*4096));
658 if (last_page+1 > last_used_page)
659 last_used_page = last_page+1;
663 /* If the record_new_objects flag is 2 then all new regions created
666 * If it's 1 then then it is only recorded if the first page of the
667 * current region is <= new_areas_ignore_page. This helps avoid
668 * unnecessary recording when doing full scavenge pass.
670 * The new_object structure holds the page, byte offset, and size of
671 * new regions of objects. Each new area is placed in the array of
672 * these structures pointer to by new_areas. new_areas_index holds the
673 * offset into new_areas.
675 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
676 * later code must detect this and handle it, probably by doing a full
677 * scavenge of a generation. */
678 #define NUM_NEW_AREAS 512
679 static int record_new_objects = 0;
680 static int new_areas_ignore_page;
686 static struct new_area (*new_areas)[];
687 static int new_areas_index;
690 /* Add a new area to new_areas. */
692 add_new_area(int first_page, int offset, int size)
694 unsigned new_area_start,c;
697 /* Ignore if full. */
698 if (new_areas_index >= NUM_NEW_AREAS)
701 switch (record_new_objects) {
705 if (first_page > new_areas_ignore_page)
714 new_area_start = 4096*first_page + offset;
716 /* Search backwards for a prior area that this follows from. If
717 found this will save adding a new area. */
718 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
720 4096*((*new_areas)[i].page)
721 + (*new_areas)[i].offset
722 + (*new_areas)[i].size;
724 "/add_new_area S1 %d %d %d %d\n",
725 i, c, new_area_start, area_end));*/
726 if (new_area_start == area_end) {
728 "/adding to [%d] %d %d %d with %d %d %d:\n",
730 (*new_areas)[i].page,
731 (*new_areas)[i].offset,
732 (*new_areas)[i].size,
736 (*new_areas)[i].size += size;
740 /*FSHOW((stderr, "/add_new_area S1 %d %d %d\n", i, c, new_area_start));*/
742 (*new_areas)[new_areas_index].page = first_page;
743 (*new_areas)[new_areas_index].offset = offset;
744 (*new_areas)[new_areas_index].size = size;
746 "/new_area %d page %d offset %d size %d\n",
747 new_areas_index, first_page, offset, size));*/
750 /* Note the max new_areas used. */
751 if (new_areas_index > max_new_areas)
752 max_new_areas = new_areas_index;
755 /* Update the tables for the alloc_region. The region maybe added to
758 * When done the alloc_region is set up so that the next quick alloc
759 * will fail safely and thus a new region will be allocated. Further
760 * it is safe to try to re-update the page table of this reset
763 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
769 int orig_first_page_bytes_used;
775 "/gc_alloc_update_page_tables() to gen %d:\n",
776 gc_alloc_generation));
779 first_page = alloc_region->first_page;
781 /* Catch an unused alloc_region. */
782 if ((first_page == 0) && (alloc_region->last_page == -1))
785 next_page = first_page+1;
787 /* Skip if no bytes were allocated. */
788 if (alloc_region->free_pointer != alloc_region->start_addr) {
789 orig_first_page_bytes_used = page_table[first_page].bytes_used;
791 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
793 /* All the pages used need to be updated */
795 /* Update the first page. */
797 /* If the page was free then set up the gen, and
798 * first_object_offset. */
799 if (page_table[first_page].bytes_used == 0)
800 gc_assert(page_table[first_page].first_object_offset == 0);
803 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
805 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
806 gc_assert(page_table[first_page].gen == gc_alloc_generation);
807 gc_assert(page_table[first_page].large_object == 0);
811 /* Calculate the number of bytes used in this page. This is not
812 * always the number of new bytes, unless it was free. */
814 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>4096) {
818 page_table[first_page].bytes_used = bytes_used;
819 byte_cnt += bytes_used;
822 /* All the rest of the pages should be free. We need to set their
823 * first_object_offset pointer to the start of the region, and set
827 gc_assert(page_table[next_page].allocated == UNBOXED_PAGE);
829 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
830 gc_assert(page_table[next_page].bytes_used == 0);
831 gc_assert(page_table[next_page].gen == gc_alloc_generation);
832 gc_assert(page_table[next_page].large_object == 0);
834 gc_assert(page_table[next_page].first_object_offset ==
835 alloc_region->start_addr - page_address(next_page));
837 /* Calculate the number of bytes used in this page. */
839 if ((bytes_used = (alloc_region->free_pointer
840 - page_address(next_page)))>4096) {
844 page_table[next_page].bytes_used = bytes_used;
845 byte_cnt += bytes_used;
850 region_size = alloc_region->free_pointer - alloc_region->start_addr;
851 bytes_allocated += region_size;
852 generations[gc_alloc_generation].bytes_allocated += region_size;
854 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
856 /* Set the generations alloc restart page to the last page of
859 generations[gc_alloc_generation].alloc_unboxed_start_page =
862 generations[gc_alloc_generation].alloc_start_page = next_page-1;
864 /* Add the region to the new_areas if requested. */
866 add_new_area(first_page,orig_first_page_bytes_used, region_size);
870 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
872 gc_alloc_generation));
875 /* There are no bytes allocated. Unallocate the first_page if
876 * there are 0 bytes_used. */
877 if (page_table[first_page].bytes_used == 0)
878 page_table[first_page].allocated = FREE_PAGE;
881 /* Unallocate any unused pages. */
882 while (next_page <= alloc_region->last_page) {
883 gc_assert(page_table[next_page].bytes_used == 0);
884 page_table[next_page].allocated = FREE_PAGE;
888 /* Reset the alloc_region. */
889 alloc_region->first_page = 0;
890 alloc_region->last_page = -1;
891 alloc_region->start_addr = page_address(0);
892 alloc_region->free_pointer = page_address(0);
893 alloc_region->end_addr = page_address(0);
896 static inline void *gc_quick_alloc(int nbytes);
898 /* Allocate a possibly large object. */
900 gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
908 int orig_first_page_bytes_used;
913 int large = (nbytes >= large_object_size);
917 FSHOW((stderr, "/alloc_large %d\n", nbytes));
922 "/gc_alloc_large() for %d bytes from gen %d\n",
923 nbytes, gc_alloc_generation));
926 /* If the object is small, and there is room in the current region
927 then allocation it in the current region. */
929 && ((alloc_region->end_addr-alloc_region->free_pointer) >= nbytes))
930 return gc_quick_alloc(nbytes);
932 /* Search for a contiguous free region of at least nbytes. If it's a
933 large object then align it on a page boundary by searching for a
936 /* To allow the allocation of small objects without the danger of
937 using a page in the current boxed region, the search starts after
938 the current boxed free region. XX could probably keep a page
939 index ahead of the current region and bumped up here to save a
940 lot of re-scanning. */
943 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
945 restart_page = generations[gc_alloc_generation].alloc_large_start_page;
947 if (restart_page <= alloc_region->last_page) {
948 restart_page = alloc_region->last_page+1;
952 first_page = restart_page;
955 while ((first_page < NUM_PAGES)
956 && (page_table[first_page].allocated != FREE_PAGE))
959 /* FIXME: This looks extremely similar, perhaps identical,
960 * to code in gc_alloc_new_region(). It should be shared
962 while ((first_page < NUM_PAGES)
963 && (page_table[first_page].allocated != FREE_PAGE)
965 (page_table[first_page].allocated != UNBOXED_PAGE))
967 (page_table[first_page].allocated != BOXED_PAGE))
968 || (page_table[first_page].large_object != 0)
969 || (page_table[first_page].gen != gc_alloc_generation)
970 || (page_table[first_page].bytes_used >= (4096-32))
971 || (page_table[first_page].write_protected != 0)
972 || (page_table[first_page].dont_move != 0)))
975 if (first_page >= NUM_PAGES) {
977 "Argh! gc_alloc_large failed (first_page), nbytes=%d.\n",
979 print_generation_stats(1);
983 gc_assert(page_table[first_page].write_protected == 0);
987 "/first_page=%d bytes_used=%d\n",
988 first_page, page_table[first_page].bytes_used));
991 last_page = first_page;
992 bytes_found = 4096 - page_table[first_page].bytes_used;
994 while ((bytes_found < nbytes)
995 && (last_page < (NUM_PAGES-1))
996 && (page_table[last_page+1].allocated == FREE_PAGE)) {
1000 gc_assert(page_table[last_page].write_protected == 0);
1003 region_size = (4096 - page_table[first_page].bytes_used)
1004 + 4096*(last_page-first_page);
1006 gc_assert(bytes_found == region_size);
1010 "/last_page=%d bytes_found=%d num_pages=%d\n",
1011 last_page, bytes_found, num_pages));
1014 restart_page = last_page + 1;
1015 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1017 /* Check for a failure */
1018 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1020 "Argh! gc_alloc_large failed (restart_page), nbytes=%d.\n",
1022 print_generation_stats(1);
1029 "/gc_alloc_large() gen %d: %d of %d bytes: from pages %d to %d: addr=%x\n",
1030 gc_alloc_generation,
1035 page_address(first_page)));
1038 gc_assert(first_page > alloc_region->last_page);
1040 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1043 generations[gc_alloc_generation].alloc_large_start_page = last_page;
1045 /* Set up the pages. */
1046 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1048 /* If the first page was free then set up the gen, and
1049 * first_object_offset. */
1050 if (page_table[first_page].bytes_used == 0) {
1052 page_table[first_page].allocated = UNBOXED_PAGE;
1054 page_table[first_page].allocated = BOXED_PAGE;
1055 page_table[first_page].gen = gc_alloc_generation;
1056 page_table[first_page].first_object_offset = 0;
1057 page_table[first_page].large_object = large;
1061 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
1063 gc_assert(page_table[first_page].allocated == BOXED_PAGE);
1064 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1065 gc_assert(page_table[first_page].large_object == large);
1069 /* Calc. the number of bytes used in this page. This is not
1070 * always the number of new bytes, unless it was free. */
1072 if ((bytes_used = nbytes+orig_first_page_bytes_used) > 4096) {
1076 page_table[first_page].bytes_used = bytes_used;
1077 byte_cnt += bytes_used;
1079 next_page = first_page+1;
1081 /* All the rest of the pages should be free. We need to set their
1082 * first_object_offset pointer to the start of the region, and
1083 * set the bytes_used. */
1085 gc_assert(page_table[next_page].allocated == FREE_PAGE);
1086 gc_assert(page_table[next_page].bytes_used == 0);
1088 page_table[next_page].allocated = UNBOXED_PAGE;
1090 page_table[next_page].allocated = BOXED_PAGE;
1091 page_table[next_page].gen = gc_alloc_generation;
1092 page_table[next_page].large_object = large;
1094 page_table[next_page].first_object_offset =
1095 orig_first_page_bytes_used - 4096*(next_page-first_page);
1097 /* Calculate the number of bytes used in this page. */
1099 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > 4096) {
1103 page_table[next_page].bytes_used = bytes_used;
1104 byte_cnt += bytes_used;
1109 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1111 bytes_allocated += nbytes;
1112 generations[gc_alloc_generation].bytes_allocated += nbytes;
1114 /* Add the region to the new_areas if requested. */
1116 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1118 /* Bump up last_free_page */
1119 if (last_page+1 > last_free_page) {
1120 last_free_page = last_page+1;
1121 SetSymbolValue(ALLOCATION_POINTER,
1122 (lispobj)(((char *)heap_base) + last_free_page*4096));
1123 if (last_page+1 > last_used_page)
1124 last_used_page = last_page+1;
1127 return((void *)(page_address(first_page)+orig_first_page_bytes_used));
1130 /* Allocate bytes from the boxed_region. First checks whether there is
1131 * room. If not then call gc_alloc_new_region() to find a new region
1132 * with enough space. Return a pointer to the start of the region. */
1134 gc_alloc(int nbytes)
1136 void *new_free_pointer;
1138 /* FSHOW((stderr, "/gc_alloc %d\n", nbytes)); */
1140 /* Check whether there is room in the current alloc region. */
1141 new_free_pointer = boxed_region.free_pointer + nbytes;
1143 if (new_free_pointer <= boxed_region.end_addr) {
1144 /* If so then allocate from the current alloc region. */
1145 void *new_obj = boxed_region.free_pointer;
1146 boxed_region.free_pointer = new_free_pointer;
1148 /* Check whether the alloc region is almost empty. */
1149 if ((boxed_region.end_addr - boxed_region.free_pointer) <= 32) {
1150 /* If so finished with the current region. */
1151 gc_alloc_update_page_tables(0, &boxed_region);
1152 /* Set up a new region. */
1153 gc_alloc_new_region(32, 0, &boxed_region);
1155 return((void *)new_obj);
1158 /* Else not enough free space in the current region. */
1160 /* If there some room left in the current region, enough to be worth
1161 * saving, then allocate a large object. */
1162 /* FIXME: "32" should be a named parameter. */
1163 if ((boxed_region.end_addr-boxed_region.free_pointer) > 32)
1164 return gc_alloc_large(nbytes, 0, &boxed_region);
1166 /* Else find a new region. */
1168 /* Finished with the current region. */
1169 gc_alloc_update_page_tables(0, &boxed_region);
1171 /* Set up a new region. */
1172 gc_alloc_new_region(nbytes, 0, &boxed_region);
1174 /* Should now be enough room. */
1176 /* Check whether there is room in the current region. */
1177 new_free_pointer = boxed_region.free_pointer + nbytes;
1179 if (new_free_pointer <= boxed_region.end_addr) {
1180 /* If so then allocate from the current region. */
1181 void *new_obj = boxed_region.free_pointer;
1182 boxed_region.free_pointer = new_free_pointer;
1184 /* Check whether the current region is almost empty. */
1185 if ((boxed_region.end_addr - boxed_region.free_pointer) <= 32) {
1186 /* If so find, finished with the current region. */
1187 gc_alloc_update_page_tables(0, &boxed_region);
1189 /* Set up a new region. */
1190 gc_alloc_new_region(32, 0, &boxed_region);
1193 return((void *)new_obj);
1196 /* shouldn't happen */
1198 return((void *) NIL); /* dummy value: return something ... */
1201 /* Allocate space from the boxed_region. If there is not enough free
1202 * space then call gc_alloc to do the job. A pointer to the start of
1203 * the region is returned. */
1204 static inline void *
1205 gc_quick_alloc(int nbytes)
1207 void *new_free_pointer;
1209 /* Check whether there is room in the current region. */
1210 new_free_pointer = boxed_region.free_pointer + nbytes;
1212 if (new_free_pointer <= boxed_region.end_addr) {
1213 /* Allocate from the current region. */
1214 void *new_obj = boxed_region.free_pointer;
1215 boxed_region.free_pointer = new_free_pointer;
1216 return((void *)new_obj);
1218 /* Let full gc_alloc() handle it. */
1219 return gc_alloc(nbytes);
1223 /* Allocate space for the boxed object. If it is a large object then
1224 * do a large alloc else allocate from the current region. If there is
1225 * not enough free space then call gc_alloc() to do the job. A pointer
1226 * to the start of the region is returned. */
1227 static inline void *
1228 gc_quick_alloc_large(int nbytes)
1230 void *new_free_pointer;
1232 if (nbytes >= large_object_size)
1233 return gc_alloc_large(nbytes, 0, &boxed_region);
1235 /* Check whether there is room in the current region. */
1236 new_free_pointer = boxed_region.free_pointer + nbytes;
1238 if (new_free_pointer <= boxed_region.end_addr) {
1239 /* If so then allocate from the current region. */
1240 void *new_obj = boxed_region.free_pointer;
1241 boxed_region.free_pointer = new_free_pointer;
1242 return((void *)new_obj);
1244 /* Let full gc_alloc() handle it. */
1245 return gc_alloc(nbytes);
1250 gc_alloc_unboxed(int nbytes)
1252 void *new_free_pointer;
1255 FSHOW((stderr, "/gc_alloc_unboxed() %d\n", nbytes));
1258 /* Check whether there is room in the current region. */
1259 new_free_pointer = unboxed_region.free_pointer + nbytes;
1261 if (new_free_pointer <= unboxed_region.end_addr) {
1262 /* If so then allocate from the current region. */
1263 void *new_obj = unboxed_region.free_pointer;
1264 unboxed_region.free_pointer = new_free_pointer;
1266 /* Check whether the current region is almost empty. */
1267 if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1268 /* If so finished with the current region. */
1269 gc_alloc_update_page_tables(1, &unboxed_region);
1271 /* Set up a new region. */
1272 gc_alloc_new_region(32, 1, &unboxed_region);
1275 return((void *)new_obj);
1278 /* Else not enough free space in the current region. */
1280 /* If there is a bit of room left in the current region then
1281 allocate a large object. */
1282 if ((unboxed_region.end_addr-unboxed_region.free_pointer) > 32)
1283 return gc_alloc_large(nbytes,1,&unboxed_region);
1285 /* Else find a new region. */
1287 /* Finished with the current region. */
1288 gc_alloc_update_page_tables(1, &unboxed_region);
1290 /* Set up a new region. */
1291 gc_alloc_new_region(nbytes, 1, &unboxed_region);
1293 /* (There should now be enough room.) */
1295 /* Check whether there is room in the current region. */
1296 new_free_pointer = unboxed_region.free_pointer + nbytes;
1298 if (new_free_pointer <= unboxed_region.end_addr) {
1299 /* If so then allocate from the current region. */
1300 void *new_obj = unboxed_region.free_pointer;
1301 unboxed_region.free_pointer = new_free_pointer;
1303 /* Check whether the current region is almost empty. */
1304 if ((unboxed_region.end_addr - unboxed_region.free_pointer) <= 32) {
1305 /* If so find, finished with the current region. */
1306 gc_alloc_update_page_tables(1, &unboxed_region);
1308 /* Set up a new region. */
1309 gc_alloc_new_region(32, 1, &unboxed_region);
1312 return((void *)new_obj);
1315 /* shouldn't happen? */
1317 return((void *) NIL); /* dummy value: return something ... */
1320 static inline void *
1321 gc_quick_alloc_unboxed(int nbytes)
1323 void *new_free_pointer;
1325 /* Check whether there is room in the current region. */
1326 new_free_pointer = unboxed_region.free_pointer + nbytes;
1328 if (new_free_pointer <= unboxed_region.end_addr) {
1329 /* If so then allocate from the current region. */
1330 void *new_obj = unboxed_region.free_pointer;
1331 unboxed_region.free_pointer = new_free_pointer;
1333 return((void *)new_obj);
1335 /* Let general gc_alloc_unboxed() handle it. */
1336 return gc_alloc_unboxed(nbytes);
1340 /* Allocate space for the object. If it is a large object then do a
1341 * large alloc else allocate from the current region. If there is not
1342 * enough free space then call general gc_alloc_unboxed() to do the job.
1344 * A pointer to the start of the region is returned. */
1345 static inline void *
1346 gc_quick_alloc_large_unboxed(int nbytes)
1348 void *new_free_pointer;
1350 if (nbytes >= large_object_size)
1351 return gc_alloc_large(nbytes,1,&unboxed_region);
1353 /* Check whether there is room in the current region. */
1354 new_free_pointer = unboxed_region.free_pointer + nbytes;
1355 if (new_free_pointer <= unboxed_region.end_addr) {
1356 /* Allocate from the current region. */
1357 void *new_obj = unboxed_region.free_pointer;
1358 unboxed_region.free_pointer = new_free_pointer;
1359 return((void *)new_obj);
1361 /* Let full gc_alloc() handle it. */
1362 return gc_alloc_unboxed(nbytes);
1367 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1370 static int (*scavtab[256])(lispobj *where, lispobj object);
1371 static lispobj (*transother[256])(lispobj object);
1372 static int (*sizetab[256])(lispobj *where);
1374 static struct weak_pointer *weak_pointers;
1376 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
1382 static inline boolean
1383 from_space_p(lispobj obj)
1385 int page_index=(void*)obj - heap_base;
1386 return ((page_index >= 0)
1387 && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
1388 && (page_table[page_index].gen == from_space));
1391 static inline boolean
1392 new_space_p(lispobj obj)
1394 int page_index = (void*)obj - heap_base;
1395 return ((page_index >= 0)
1396 && ((page_index = ((unsigned int)page_index)/4096) < NUM_PAGES)
1397 && (page_table[page_index].gen == new_space));
1404 /* to copy a boxed object */
1405 static inline lispobj
1406 copy_object(lispobj object, int nwords)
1410 lispobj *source, *dest;
1412 gc_assert(is_lisp_pointer(object));
1413 gc_assert(from_space_p(object));
1414 gc_assert((nwords & 0x01) == 0);
1416 /* Get tag of object. */
1417 tag = lowtag_of(object);
1419 /* Allocate space. */
1420 new = gc_quick_alloc(nwords*4);
1423 source = (lispobj *) native_pointer(object);
1425 /* Copy the object. */
1426 while (nwords > 0) {
1427 dest[0] = source[0];
1428 dest[1] = source[1];
1434 /* Return Lisp pointer of new object. */
1435 return ((lispobj) new) | tag;
1438 /* to copy a large boxed object. If the object is in a large object
1439 * region then it is simply promoted, else it is copied. If it's large
1440 * enough then it's copied to a large object region.
1442 * Vectors may have shrunk. If the object is not copied the space
1443 * needs to be reclaimed, and the page_tables corrected. */
1445 copy_large_object(lispobj object, int nwords)
1449 lispobj *source, *dest;
1452 gc_assert(is_lisp_pointer(object));
1453 gc_assert(from_space_p(object));
1454 gc_assert((nwords & 0x01) == 0);
1456 if ((nwords > 1024*1024) && gencgc_verbose) {
1457 FSHOW((stderr, "/copy_large_object: %d bytes\n", nwords*4));
1460 /* Check whether it's a large object. */
1461 first_page = find_page_index((void *)object);
1462 gc_assert(first_page >= 0);
1464 if (page_table[first_page].large_object) {
1466 /* Promote the object. */
1468 int remaining_bytes;
1473 /* Note: Any page write-protection must be removed, else a
1474 * later scavenge_newspace may incorrectly not scavenge these
1475 * pages. This would not be necessary if they are added to the
1476 * new areas, but let's do it for them all (they'll probably
1477 * be written anyway?). */
1479 gc_assert(page_table[first_page].first_object_offset == 0);
1481 next_page = first_page;
1482 remaining_bytes = nwords*4;
1483 while (remaining_bytes > 4096) {
1484 gc_assert(page_table[next_page].gen == from_space);
1485 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
1486 gc_assert(page_table[next_page].large_object);
1487 gc_assert(page_table[next_page].first_object_offset==
1488 -4096*(next_page-first_page));
1489 gc_assert(page_table[next_page].bytes_used == 4096);
1491 page_table[next_page].gen = new_space;
1493 /* Remove any write-protection. We should be able to rely
1494 * on the write-protect flag to avoid redundant calls. */
1495 if (page_table[next_page].write_protected) {
1496 os_protect(page_address(next_page), 4096, OS_VM_PROT_ALL);
1497 page_table[next_page].write_protected = 0;
1499 remaining_bytes -= 4096;
1503 /* Now only one page remains, but the object may have shrunk
1504 * so there may be more unused pages which will be freed. */
1506 /* The object may have shrunk but shouldn't have grown. */
1507 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1509 page_table[next_page].gen = new_space;
1510 gc_assert(page_table[next_page].allocated = BOXED_PAGE);
1512 /* Adjust the bytes_used. */
1513 old_bytes_used = page_table[next_page].bytes_used;
1514 page_table[next_page].bytes_used = remaining_bytes;
1516 bytes_freed = old_bytes_used - remaining_bytes;
1518 /* Free any remaining pages; needs care. */
1520 while ((old_bytes_used == 4096) &&
1521 (page_table[next_page].gen == from_space) &&
1522 (page_table[next_page].allocated == BOXED_PAGE) &&
1523 page_table[next_page].large_object &&
1524 (page_table[next_page].first_object_offset ==
1525 -(next_page - first_page)*4096)) {
1526 /* Checks out OK, free the page. Don't need to both zeroing
1527 * pages as this should have been done before shrinking the
1528 * object. These pages shouldn't be write-protected as they
1529 * should be zero filled. */
1530 gc_assert(page_table[next_page].write_protected == 0);
1532 old_bytes_used = page_table[next_page].bytes_used;
1533 page_table[next_page].allocated = FREE_PAGE;
1534 page_table[next_page].bytes_used = 0;
1535 bytes_freed += old_bytes_used;
1539 if ((bytes_freed > 0) && gencgc_verbose)
1540 FSHOW((stderr, "/copy_large_boxed bytes_freed=%d\n", bytes_freed));
1542 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1543 generations[new_space].bytes_allocated += 4*nwords;
1544 bytes_allocated -= bytes_freed;
1546 /* Add the region to the new_areas if requested. */
1547 add_new_area(first_page,0,nwords*4);
1551 /* Get tag of object. */
1552 tag = lowtag_of(object);
1554 /* Allocate space. */
1555 new = gc_quick_alloc_large(nwords*4);
1558 source = (lispobj *) native_pointer(object);
1560 /* Copy the object. */
1561 while (nwords > 0) {
1562 dest[0] = source[0];
1563 dest[1] = source[1];
1569 /* Return Lisp pointer of new object. */
1570 return ((lispobj) new) | tag;
1574 /* to copy unboxed objects */
1575 static inline lispobj
1576 copy_unboxed_object(lispobj object, int nwords)
1580 lispobj *source, *dest;
1582 gc_assert(is_lisp_pointer(object));
1583 gc_assert(from_space_p(object));
1584 gc_assert((nwords & 0x01) == 0);
1586 /* Get tag of object. */
1587 tag = lowtag_of(object);
1589 /* Allocate space. */
1590 new = gc_quick_alloc_unboxed(nwords*4);
1593 source = (lispobj *) native_pointer(object);
1595 /* Copy the object. */
1596 while (nwords > 0) {
1597 dest[0] = source[0];
1598 dest[1] = source[1];
1604 /* Return Lisp pointer of new object. */
1605 return ((lispobj) new) | tag;
1608 /* to copy large unboxed objects
1610 * If the object is in a large object region then it is simply
1611 * promoted, else it is copied. If it's large enough then it's copied
1612 * to a large object region.
1614 * Bignums and vectors may have shrunk. If the object is not copied
1615 * the space needs to be reclaimed, and the page_tables corrected.
1617 * KLUDGE: There's a lot of cut-and-paste duplication between this
1618 * function and copy_large_object(..). -- WHN 20000619 */
1620 copy_large_unboxed_object(lispobj object, int nwords)
1624 lispobj *source, *dest;
1627 gc_assert(is_lisp_pointer(object));
1628 gc_assert(from_space_p(object));
1629 gc_assert((nwords & 0x01) == 0);
1631 if ((nwords > 1024*1024) && gencgc_verbose)
1632 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*4));
1634 /* Check whether it's a large object. */
1635 first_page = find_page_index((void *)object);
1636 gc_assert(first_page >= 0);
1638 if (page_table[first_page].large_object) {
1639 /* Promote the object. Note: Unboxed objects may have been
1640 * allocated to a BOXED region so it may be necessary to
1641 * change the region to UNBOXED. */
1642 int remaining_bytes;
1647 gc_assert(page_table[first_page].first_object_offset == 0);
1649 next_page = first_page;
1650 remaining_bytes = nwords*4;
1651 while (remaining_bytes > 4096) {
1652 gc_assert(page_table[next_page].gen == from_space);
1653 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE)
1654 || (page_table[next_page].allocated == BOXED_PAGE));
1655 gc_assert(page_table[next_page].large_object);
1656 gc_assert(page_table[next_page].first_object_offset==
1657 -4096*(next_page-first_page));
1658 gc_assert(page_table[next_page].bytes_used == 4096);
1660 page_table[next_page].gen = new_space;
1661 page_table[next_page].allocated = UNBOXED_PAGE;
1662 remaining_bytes -= 4096;
1666 /* Now only one page remains, but the object may have shrunk so
1667 * there may be more unused pages which will be freed. */
1669 /* Object may have shrunk but shouldn't have grown - check. */
1670 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1672 page_table[next_page].gen = new_space;
1673 page_table[next_page].allocated = UNBOXED_PAGE;
1675 /* Adjust the bytes_used. */
1676 old_bytes_used = page_table[next_page].bytes_used;
1677 page_table[next_page].bytes_used = remaining_bytes;
1679 bytes_freed = old_bytes_used - remaining_bytes;
1681 /* Free any remaining pages; needs care. */
1683 while ((old_bytes_used == 4096) &&
1684 (page_table[next_page].gen == from_space) &&
1685 ((page_table[next_page].allocated == UNBOXED_PAGE)
1686 || (page_table[next_page].allocated == BOXED_PAGE)) &&
1687 page_table[next_page].large_object &&
1688 (page_table[next_page].first_object_offset ==
1689 -(next_page - first_page)*4096)) {
1690 /* Checks out OK, free the page. Don't need to both zeroing
1691 * pages as this should have been done before shrinking the
1692 * object. These pages shouldn't be write-protected, even if
1693 * boxed they should be zero filled. */
1694 gc_assert(page_table[next_page].write_protected == 0);
1696 old_bytes_used = page_table[next_page].bytes_used;
1697 page_table[next_page].allocated = FREE_PAGE;
1698 page_table[next_page].bytes_used = 0;
1699 bytes_freed += old_bytes_used;
1703 if ((bytes_freed > 0) && gencgc_verbose)
1705 "/copy_large_unboxed bytes_freed=%d\n",
1708 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1709 generations[new_space].bytes_allocated += 4*nwords;
1710 bytes_allocated -= bytes_freed;
1715 /* Get tag of object. */
1716 tag = lowtag_of(object);
1718 /* Allocate space. */
1719 new = gc_quick_alloc_large_unboxed(nwords*4);
1722 source = (lispobj *) native_pointer(object);
1724 /* Copy the object. */
1725 while (nwords > 0) {
1726 dest[0] = source[0];
1727 dest[1] = source[1];
1733 /* Return Lisp pointer of new object. */
1734 return ((lispobj) new) | tag;
1742 /* FIXME: Most calls end up going to some trouble to compute an
1743 * 'n_words' value for this function. The system might be a little
1744 * simpler if this function used an 'end' parameter instead. */
1746 scavenge(lispobj *start, long n_words)
1748 lispobj *end = start + n_words;
1749 lispobj *object_ptr;
1750 int n_words_scavenged;
1752 for (object_ptr = start;
1754 object_ptr += n_words_scavenged) {
1756 lispobj object = *object_ptr;
1758 gc_assert(object != 0x01); /* not a forwarding pointer */
1760 if (is_lisp_pointer(object)) {
1761 if (from_space_p(object)) {
1762 /* It currently points to old space. Check for a
1763 * forwarding pointer. */
1764 lispobj *ptr = (lispobj *)native_pointer(object);
1765 lispobj first_word = *ptr;
1766 if (first_word == 0x01) {
1767 /* Yes, there's a forwarding pointer. */
1768 *object_ptr = ptr[1];
1769 n_words_scavenged = 1;
1771 /* Scavenge that pointer. */
1773 (scavtab[widetag_of(object)])(object_ptr, object);
1776 /* It points somewhere other than oldspace. Leave it
1778 n_words_scavenged = 1;
1780 } else if ((object & 3) == 0) {
1781 /* It's a fixnum: really easy.. */
1782 n_words_scavenged = 1;
1784 /* It's some sort of header object or another. */
1786 (scavtab[widetag_of(object)])(object_ptr, object);
1789 gc_assert(object_ptr == end);
1793 * code and code-related objects
1796 /* FIXME: (1) Shouldn't this be defined in sbcl.h? */
1797 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - FUN_POINTER_LOWTAG)
1799 static lispobj trans_fun_header(lispobj object);
1800 static lispobj trans_boxed(lispobj object);
1803 scav_fun_pointer(lispobj *where, lispobj object)
1805 lispobj *first_pointer;
1808 gc_assert(is_lisp_pointer(object));
1810 /* Object is a pointer into from space - no a FP. */
1811 first_pointer = (lispobj *) native_pointer(object);
1813 /* must transport object -- object may point to either a function
1814 * header, a closure function header, or to a closure header. */
1816 switch (widetag_of(*first_pointer)) {
1817 case SIMPLE_FUN_HEADER_WIDETAG:
1818 case CLOSURE_FUN_HEADER_WIDETAG:
1819 copy = trans_fun_header(object);
1822 copy = trans_boxed(object);
1826 if (copy != object) {
1827 /* Set forwarding pointer */
1828 first_pointer[0] = 0x01;
1829 first_pointer[1] = copy;
1832 gc_assert(is_lisp_pointer(copy));
1833 gc_assert(!from_space_p(copy));
1840 /* Scan a x86 compiled code object, looking for possible fixups that
1841 * have been missed after a move.
1843 * Two types of fixups are needed:
1844 * 1. Absolute fixups to within the code object.
1845 * 2. Relative fixups to outside the code object.
1847 * Currently only absolute fixups to the constant vector, or to the
1848 * code area are checked. */
1850 sniff_code_object(struct code *code, unsigned displacement)
1852 int nheader_words, ncode_words, nwords;
1854 void *constants_start_addr, *constants_end_addr;
1855 void *code_start_addr, *code_end_addr;
1856 int fixup_found = 0;
1858 if (!check_code_fixups)
1861 ncode_words = fixnum_value(code->code_size);
1862 nheader_words = HeaderValue(*(lispobj *)code);
1863 nwords = ncode_words + nheader_words;
1865 constants_start_addr = (void *)code + 5*4;
1866 constants_end_addr = (void *)code + nheader_words*4;
1867 code_start_addr = (void *)code + nheader_words*4;
1868 code_end_addr = (void *)code + nwords*4;
1870 /* Work through the unboxed code. */
1871 for (p = code_start_addr; p < code_end_addr; p++) {
1872 void *data = *(void **)p;
1873 unsigned d1 = *((unsigned char *)p - 1);
1874 unsigned d2 = *((unsigned char *)p - 2);
1875 unsigned d3 = *((unsigned char *)p - 3);
1876 unsigned d4 = *((unsigned char *)p - 4);
1878 unsigned d5 = *((unsigned char *)p - 5);
1879 unsigned d6 = *((unsigned char *)p - 6);
1882 /* Check for code references. */
1883 /* Check for a 32 bit word that looks like an absolute
1884 reference to within the code adea of the code object. */
1885 if ((data >= (code_start_addr-displacement))
1886 && (data < (code_end_addr-displacement))) {
1887 /* function header */
1889 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1890 /* Skip the function header */
1894 /* the case of PUSH imm32 */
1898 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1899 p, d6, d5, d4, d3, d2, d1, data));
1900 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1902 /* the case of MOV [reg-8],imm32 */
1904 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1905 || d2==0x45 || d2==0x46 || d2==0x47)
1909 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1910 p, d6, d5, d4, d3, d2, d1, data));
1911 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1913 /* the case of LEA reg,[disp32] */
1914 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1917 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1918 p, d6, d5, d4, d3, d2, d1, data));
1919 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1923 /* Check for constant references. */
1924 /* Check for a 32 bit word that looks like an absolute
1925 reference to within the constant vector. Constant references
1927 if ((data >= (constants_start_addr-displacement))
1928 && (data < (constants_end_addr-displacement))
1929 && (((unsigned)data & 0x3) == 0)) {
1934 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1935 p, d6, d5, d4, d3, d2, d1, data));
1936 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1939 /* the case of MOV m32,EAX */
1943 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1944 p, d6, d5, d4, d3, d2, d1, data));
1945 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1948 /* the case of CMP m32,imm32 */
1949 if ((d1 == 0x3d) && (d2 == 0x81)) {
1952 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1953 p, d6, d5, d4, d3, d2, d1, data));
1955 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1958 /* Check for a mod=00, r/m=101 byte. */
1959 if ((d1 & 0xc7) == 5) {
1964 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1965 p, d6, d5, d4, d3, d2, d1, data));
1966 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1968 /* the case of CMP reg32,m32 */
1972 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1973 p, d6, d5, d4, d3, d2, d1, data));
1974 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1976 /* the case of MOV m32,reg32 */
1980 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1981 p, d6, d5, d4, d3, d2, d1, data));
1982 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1984 /* the case of MOV reg32,m32 */
1988 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1989 p, d6, d5, d4, d3, d2, d1, data));
1990 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1992 /* the case of LEA reg32,m32 */
1996 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1997 p, d6, d5, d4, d3, d2, d1, data));
1998 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
2004 /* If anything was found, print some information on the code
2008 "/compiled code object at %x: header words = %d, code words = %d\n",
2009 code, nheader_words, ncode_words));
2011 "/const start = %x, end = %x\n",
2012 constants_start_addr, constants_end_addr));
2014 "/code start = %x, end = %x\n",
2015 code_start_addr, code_end_addr));
2020 apply_code_fixups(struct code *old_code, struct code *new_code)
2022 int nheader_words, ncode_words, nwords;
2023 void *constants_start_addr, *constants_end_addr;
2024 void *code_start_addr, *code_end_addr;
2025 lispobj fixups = NIL;
2026 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
2027 struct vector *fixups_vector;
2029 ncode_words = fixnum_value(new_code->code_size);
2030 nheader_words = HeaderValue(*(lispobj *)new_code);
2031 nwords = ncode_words + nheader_words;
2033 "/compiled code object at %x: header words = %d, code words = %d\n",
2034 new_code, nheader_words, ncode_words)); */
2035 constants_start_addr = (void *)new_code + 5*4;
2036 constants_end_addr = (void *)new_code + nheader_words*4;
2037 code_start_addr = (void *)new_code + nheader_words*4;
2038 code_end_addr = (void *)new_code + nwords*4;
2041 "/const start = %x, end = %x\n",
2042 constants_start_addr,constants_end_addr));
2044 "/code start = %x; end = %x\n",
2045 code_start_addr,code_end_addr));
2048 /* The first constant should be a pointer to the fixups for this
2049 code objects. Check. */
2050 fixups = new_code->constants[0];
2052 /* It will be 0 or the unbound-marker if there are no fixups, and
2053 * will be an other pointer if it is valid. */
2054 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
2055 !is_lisp_pointer(fixups)) {
2056 /* Check for possible errors. */
2057 if (check_code_fixups)
2058 sniff_code_object(new_code, displacement);
2060 /*fprintf(stderr,"Fixups for code object not found!?\n");
2061 fprintf(stderr,"*** Compiled code object at %x: header_words=%d code_words=%d .\n",
2062 new_code, nheader_words, ncode_words);
2063 fprintf(stderr,"*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
2064 constants_start_addr,constants_end_addr,
2065 code_start_addr,code_end_addr);*/
2069 fixups_vector = (struct vector *)native_pointer(fixups);
2071 /* Could be pointing to a forwarding pointer. */
2072 if (is_lisp_pointer(fixups) &&
2073 (find_page_index((void*)fixups_vector) != -1) &&
2074 (fixups_vector->header == 0x01)) {
2075 /* If so, then follow it. */
2076 /*SHOW("following pointer to a forwarding pointer");*/
2077 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
2080 /*SHOW("got fixups");*/
2082 if (widetag_of(fixups_vector->header) ==
2083 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
2084 /* Got the fixups for the code block. Now work through the vector,
2085 and apply a fixup at each address. */
2086 int length = fixnum_value(fixups_vector->length);
2088 for (i = 0; i < length; i++) {
2089 unsigned offset = fixups_vector->data[i];
2090 /* Now check the current value of offset. */
2091 unsigned old_value =
2092 *(unsigned *)((unsigned)code_start_addr + offset);
2094 /* If it's within the old_code object then it must be an
2095 * absolute fixup (relative ones are not saved) */
2096 if ((old_value >= (unsigned)old_code)
2097 && (old_value < ((unsigned)old_code + nwords*4)))
2098 /* So add the dispacement. */
2099 *(unsigned *)((unsigned)code_start_addr + offset) =
2100 old_value + displacement;
2102 /* It is outside the old code object so it must be a
2103 * relative fixup (absolute fixups are not saved). So
2104 * subtract the displacement. */
2105 *(unsigned *)((unsigned)code_start_addr + offset) =
2106 old_value - displacement;
2110 /* Check for possible errors. */
2111 if (check_code_fixups) {
2112 sniff_code_object(new_code,displacement);
2116 static struct code *
2117 trans_code(struct code *code)
2119 struct code *new_code;
2120 lispobj l_code, l_new_code;
2121 int nheader_words, ncode_words, nwords;
2122 unsigned long displacement;
2123 lispobj fheaderl, *prev_pointer;
2126 "\n/transporting code object located at 0x%08x\n",
2127 (unsigned long) code)); */
2129 /* If object has already been transported, just return pointer. */
2130 if (*((lispobj *)code) == 0x01)
2131 return (struct code*)(((lispobj *)code)[1]);
2133 gc_assert(widetag_of(code->header) == CODE_HEADER_WIDETAG);
2135 /* Prepare to transport the code vector. */
2136 l_code = (lispobj) code | OTHER_POINTER_LOWTAG;
2138 ncode_words = fixnum_value(code->code_size);
2139 nheader_words = HeaderValue(code->header);
2140 nwords = ncode_words + nheader_words;
2141 nwords = CEILING(nwords, 2);
2143 l_new_code = copy_large_object(l_code, nwords);
2144 new_code = (struct code *) native_pointer(l_new_code);
2146 /* may not have been moved.. */
2147 if (new_code == code)
2150 displacement = l_new_code - l_code;
2154 "/old code object at 0x%08x, new code object at 0x%08x\n",
2155 (unsigned long) code,
2156 (unsigned long) new_code));
2157 FSHOW((stderr, "/Code object is %d words long.\n", nwords));
2160 /* Set forwarding pointer. */
2161 ((lispobj *)code)[0] = 0x01;
2162 ((lispobj *)code)[1] = l_new_code;
2164 /* Set forwarding pointers for all the function headers in the
2165 * code object. Also fix all self pointers. */
2167 fheaderl = code->entry_points;
2168 prev_pointer = &new_code->entry_points;
2170 while (fheaderl != NIL) {
2171 struct simple_fun *fheaderp, *nfheaderp;
2174 fheaderp = (struct simple_fun *) native_pointer(fheaderl);
2175 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
2177 /* Calculate the new function pointer and the new
2178 * function header. */
2179 nfheaderl = fheaderl + displacement;
2180 nfheaderp = (struct simple_fun *) native_pointer(nfheaderl);
2182 /* Set forwarding pointer. */
2183 ((lispobj *)fheaderp)[0] = 0x01;
2184 ((lispobj *)fheaderp)[1] = nfheaderl;
2186 /* Fix self pointer. */
2187 nfheaderp->self = nfheaderl + FUN_RAW_ADDR_OFFSET;
2189 *prev_pointer = nfheaderl;
2191 fheaderl = fheaderp->next;
2192 prev_pointer = &nfheaderp->next;
2195 apply_code_fixups(code, new_code);
2201 scav_code_header(lispobj *where, lispobj object)
2204 int n_header_words, n_code_words, n_words;
2205 lispobj entry_point; /* tagged pointer to entry point */
2206 struct simple_fun *function_ptr; /* untagged pointer to entry point */
2208 code = (struct code *) where;
2209 n_code_words = fixnum_value(code->code_size);
2210 n_header_words = HeaderValue(object);
2211 n_words = n_code_words + n_header_words;
2212 n_words = CEILING(n_words, 2);
2214 /* Scavenge the boxed section of the code data block. */
2215 scavenge(where + 1, n_header_words - 1);
2217 /* Scavenge the boxed section of each function object in the
2218 * code data block. */
2219 for (entry_point = code->entry_points;
2221 entry_point = function_ptr->next) {
2223 gc_assert(is_lisp_pointer(entry_point));
2225 function_ptr = (struct simple_fun *) native_pointer(entry_point);
2226 gc_assert(widetag_of(function_ptr->header)==SIMPLE_FUN_HEADER_WIDETAG);
2228 scavenge(&function_ptr->name, 1);
2229 scavenge(&function_ptr->arglist, 1);
2230 scavenge(&function_ptr->type, 1);
2237 trans_code_header(lispobj object)
2241 ncode = trans_code((struct code *) native_pointer(object));
2242 return (lispobj) ncode | OTHER_POINTER_LOWTAG;
2246 size_code_header(lispobj *where)
2249 int nheader_words, ncode_words, nwords;
2251 code = (struct code *) where;
2253 ncode_words = fixnum_value(code->code_size);
2254 nheader_words = HeaderValue(code->header);
2255 nwords = ncode_words + nheader_words;
2256 nwords = CEILING(nwords, 2);
2262 scav_return_pc_header(lispobj *where, lispobj object)
2264 lose("attempted to scavenge a return PC header where=0x%08x object=0x%08x",
2265 (unsigned long) where,
2266 (unsigned long) object);
2267 return 0; /* bogus return value to satisfy static type checking */
2271 trans_return_pc_header(lispobj object)
2273 struct simple_fun *return_pc;
2274 unsigned long offset;
2275 struct code *code, *ncode;
2277 SHOW("/trans_return_pc_header: Will this work?");
2279 return_pc = (struct simple_fun *) native_pointer(object);
2280 offset = HeaderValue(return_pc->header) * 4;
2282 /* Transport the whole code object. */
2283 code = (struct code *) ((unsigned long) return_pc - offset);
2284 ncode = trans_code(code);
2286 return ((lispobj) ncode + offset) | OTHER_POINTER_LOWTAG;
2289 /* On the 386, closures hold a pointer to the raw address instead of the
2290 * function object. */
2293 scav_closure_header(lispobj *where, lispobj object)
2295 struct closure *closure;
2298 closure = (struct closure *)where;
2299 fun = closure->fun - FUN_RAW_ADDR_OFFSET;
2301 /* The function may have moved so update the raw address. But
2302 * don't write unnecessarily. */
2303 if (closure->fun != fun + FUN_RAW_ADDR_OFFSET)
2304 closure->fun = fun + FUN_RAW_ADDR_OFFSET;
2311 scav_fun_header(lispobj *where, lispobj object)
2313 lose("attempted to scavenge a function header where=0x%08x object=0x%08x",
2314 (unsigned long) where,
2315 (unsigned long) object);
2316 return 0; /* bogus return value to satisfy static type checking */
2320 trans_fun_header(lispobj object)
2322 struct simple_fun *fheader;
2323 unsigned long offset;
2324 struct code *code, *ncode;
2326 fheader = (struct simple_fun *) native_pointer(object);
2327 offset = HeaderValue(fheader->header) * 4;
2329 /* Transport the whole code object. */
2330 code = (struct code *) ((unsigned long) fheader - offset);
2331 ncode = trans_code(code);
2333 return ((lispobj) ncode + offset) | FUN_POINTER_LOWTAG;
2341 scav_instance_pointer(lispobj *where, lispobj object)
2343 lispobj copy, *first_pointer;
2345 /* Object is a pointer into from space - not a FP. */
2346 copy = trans_boxed(object);
2348 gc_assert(copy != object);
2350 first_pointer = (lispobj *) native_pointer(object);
2352 /* Set forwarding pointer. */
2353 first_pointer[0] = 0x01;
2354 first_pointer[1] = copy;
2364 static lispobj trans_list(lispobj object);
2367 scav_list_pointer(lispobj *where, lispobj object)
2369 lispobj first, *first_pointer;
2371 gc_assert(is_lisp_pointer(object));
2373 /* Object is a pointer into from space - not FP. */
2375 first = trans_list(object);
2376 gc_assert(first != object);
2378 first_pointer = (lispobj *) native_pointer(object);
2380 /* Set forwarding pointer */
2381 first_pointer[0] = 0x01;
2382 first_pointer[1] = first;
2384 gc_assert(is_lisp_pointer(first));
2385 gc_assert(!from_space_p(first));
2391 trans_list(lispobj object)
2393 lispobj new_list_pointer;
2394 struct cons *cons, *new_cons;
2397 gc_assert(from_space_p(object));
2399 cons = (struct cons *) native_pointer(object);
2401 /* Copy 'object'. */
2402 new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
2403 new_cons->car = cons->car;
2404 new_cons->cdr = cons->cdr; /* updated later */
2405 new_list_pointer = (lispobj)new_cons | lowtag_of(object);
2407 /* Grab the cdr before it is clobbered. */
2410 /* Set forwarding pointer (clobbers start of list). */
2412 cons->cdr = new_list_pointer;
2414 /* Try to linearize the list in the cdr direction to help reduce
2418 struct cons *cdr_cons, *new_cdr_cons;
2420 if (lowtag_of(cdr) != LIST_POINTER_LOWTAG || !from_space_p(cdr)
2421 || (*((lispobj *)native_pointer(cdr)) == 0x01))
2424 cdr_cons = (struct cons *) native_pointer(cdr);
2427 new_cdr_cons = (struct cons*) gc_quick_alloc(sizeof(struct cons));
2428 new_cdr_cons->car = cdr_cons->car;
2429 new_cdr_cons->cdr = cdr_cons->cdr;
2430 new_cdr = (lispobj)new_cdr_cons | lowtag_of(cdr);
2432 /* Grab the cdr before it is clobbered. */
2433 cdr = cdr_cons->cdr;
2435 /* Set forwarding pointer. */
2436 cdr_cons->car = 0x01;
2437 cdr_cons->cdr = new_cdr;
2439 /* Update the cdr of the last cons copied into new space to
2440 * keep the newspace scavenge from having to do it. */
2441 new_cons->cdr = new_cdr;
2443 new_cons = new_cdr_cons;
2446 return new_list_pointer;
2451 * scavenging and transporting other pointers
2455 scav_other_pointer(lispobj *where, lispobj object)
2457 lispobj first, *first_pointer;
2459 gc_assert(is_lisp_pointer(object));
2461 /* Object is a pointer into from space - not FP. */
2462 first_pointer = (lispobj *) native_pointer(object);
2464 first = (transother[widetag_of(*first_pointer)])(object);
2466 if (first != object) {
2467 /* Set forwarding pointer. */
2468 first_pointer[0] = 0x01;
2469 first_pointer[1] = first;
2473 gc_assert(is_lisp_pointer(first));
2474 gc_assert(!from_space_p(first));
2480 * immediate, boxed, and unboxed objects
2484 size_pointer(lispobj *where)
2490 scav_immediate(lispobj *where, lispobj object)
2496 trans_immediate(lispobj object)
2498 lose("trying to transport an immediate");
2499 return NIL; /* bogus return value to satisfy static type checking */
2503 size_immediate(lispobj *where)
2510 scav_boxed(lispobj *where, lispobj object)
2516 trans_boxed(lispobj object)
2519 unsigned long length;
2521 gc_assert(is_lisp_pointer(object));
2523 header = *((lispobj *) native_pointer(object));
2524 length = HeaderValue(header) + 1;
2525 length = CEILING(length, 2);
2527 return copy_object(object, length);
2531 trans_boxed_large(lispobj object)
2534 unsigned long length;
2536 gc_assert(is_lisp_pointer(object));
2538 header = *((lispobj *) native_pointer(object));
2539 length = HeaderValue(header) + 1;
2540 length = CEILING(length, 2);
2542 return copy_large_object(object, length);
2546 size_boxed(lispobj *where)
2549 unsigned long length;
2552 length = HeaderValue(header) + 1;
2553 length = CEILING(length, 2);
2559 scav_fdefn(lispobj *where, lispobj object)
2561 struct fdefn *fdefn;
2563 fdefn = (struct fdefn *)where;
2565 /* FSHOW((stderr, "scav_fdefn, function = %p, raw_addr = %p\n",
2566 fdefn->fun, fdefn->raw_addr)); */
2568 if ((char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr) {
2569 scavenge(where + 1, sizeof(struct fdefn)/sizeof(lispobj) - 1);
2571 /* Don't write unnecessarily. */
2572 if (fdefn->raw_addr != (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET))
2573 fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
2575 return sizeof(struct fdefn) / sizeof(lispobj);
2582 scav_unboxed(lispobj *where, lispobj object)
2584 unsigned long length;
2586 length = HeaderValue(object) + 1;
2587 length = CEILING(length, 2);
2593 trans_unboxed(lispobj object)
2596 unsigned long length;
2599 gc_assert(is_lisp_pointer(object));
2601 header = *((lispobj *) native_pointer(object));
2602 length = HeaderValue(header) + 1;
2603 length = CEILING(length, 2);
2605 return copy_unboxed_object(object, length);
2609 trans_unboxed_large(lispobj object)
2612 unsigned long length;
2615 gc_assert(is_lisp_pointer(object));
2617 header = *((lispobj *) native_pointer(object));
2618 length = HeaderValue(header) + 1;
2619 length = CEILING(length, 2);
2621 return copy_large_unboxed_object(object, length);
2625 size_unboxed(lispobj *where)
2628 unsigned long length;
2631 length = HeaderValue(header) + 1;
2632 length = CEILING(length, 2);
2638 * vector-like objects
2641 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
2644 scav_string(lispobj *where, lispobj object)
2646 struct vector *vector;
2649 /* NOTE: Strings contain one more byte of data than the length */
2650 /* slot indicates. */
2652 vector = (struct vector *) where;
2653 length = fixnum_value(vector->length) + 1;
2654 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2660 trans_string(lispobj object)
2662 struct vector *vector;
2665 gc_assert(is_lisp_pointer(object));
2667 /* NOTE: A string contains one more byte of data (a terminating
2668 * '\0' to help when interfacing with C functions) than indicated
2669 * by the length slot. */
2671 vector = (struct vector *) native_pointer(object);
2672 length = fixnum_value(vector->length) + 1;
2673 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2675 return copy_large_unboxed_object(object, nwords);
2679 size_string(lispobj *where)
2681 struct vector *vector;
2684 /* NOTE: A string contains one more byte of data (a terminating
2685 * '\0' to help when interfacing with C functions) than indicated
2686 * by the length slot. */
2688 vector = (struct vector *) where;
2689 length = fixnum_value(vector->length) + 1;
2690 nwords = CEILING(NWORDS(length, 4) + 2, 2);
2695 /* FIXME: What does this mean? */
2696 int gencgc_hash = 1;
2699 scav_vector(lispobj *where, lispobj object)
2701 unsigned int kv_length;
2703 unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
2704 lispobj *hash_table;
2705 lispobj empty_symbol;
2706 unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2707 unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2708 unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
2710 unsigned next_vector_length = 0;
2712 /* FIXME: A comment explaining this would be nice. It looks as
2713 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
2714 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
2715 if (HeaderValue(object) != subtype_VectorValidHashing)
2719 /* This is set for backward compatibility. FIXME: Do we need
2722 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
2726 kv_length = fixnum_value(where[1]);
2727 kv_vector = where + 2; /* Skip the header and length. */
2728 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
2730 /* Scavenge element 0, which may be a hash-table structure. */
2731 scavenge(where+2, 1);
2732 if (!is_lisp_pointer(where[2])) {
2733 lose("no pointer at %x in hash table", where[2]);
2735 hash_table = (lispobj *)native_pointer(where[2]);
2736 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
2737 if (widetag_of(hash_table[0]) != INSTANCE_HEADER_WIDETAG) {
2738 lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
2741 /* Scavenge element 1, which should be some internal symbol that
2742 * the hash table code reserves for marking empty slots. */
2743 scavenge(where+3, 1);
2744 if (!is_lisp_pointer(where[3])) {
2745 lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
2747 empty_symbol = where[3];
2748 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
2749 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
2750 SYMBOL_HEADER_WIDETAG) {
2751 lose("not a symbol where empty-hash-table-slot symbol expected: %x",
2752 *(lispobj *)native_pointer(empty_symbol));
2755 /* Scavenge hash table, which will fix the positions of the other
2756 * needed objects. */
2757 scavenge(hash_table, 16);
2759 /* Cross-check the kv_vector. */
2760 if (where != (lispobj *)native_pointer(hash_table[9])) {
2761 lose("hash_table table!=this table %x", hash_table[9]);
2765 weak_p_obj = hash_table[10];
2769 lispobj index_vector_obj = hash_table[13];
2771 if (is_lisp_pointer(index_vector_obj) &&
2772 (widetag_of(*(lispobj *)native_pointer(index_vector_obj)) ==
2773 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
2774 index_vector = ((unsigned int *)native_pointer(index_vector_obj)) + 2;
2775 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
2776 length = fixnum_value(((unsigned int *)native_pointer(index_vector_obj))[1]);
2777 /*FSHOW((stderr, "/length = %d\n", length));*/
2779 lose("invalid index_vector %x", index_vector_obj);
2785 lispobj next_vector_obj = hash_table[14];
2787 if (is_lisp_pointer(next_vector_obj) &&
2788 (widetag_of(*(lispobj *)native_pointer(next_vector_obj)) ==
2789 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
2790 next_vector = ((unsigned int *)native_pointer(next_vector_obj)) + 2;
2791 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
2792 next_vector_length = fixnum_value(((unsigned int *)native_pointer(next_vector_obj))[1]);
2793 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
2795 lose("invalid next_vector %x", next_vector_obj);
2799 /* maybe hash vector */
2801 /* FIXME: This bare "15" offset should become a symbolic
2802 * expression of some sort. And all the other bare offsets
2803 * too. And the bare "16" in scavenge(hash_table, 16). And
2804 * probably other stuff too. Ugh.. */
2805 lispobj hash_vector_obj = hash_table[15];
2807 if (is_lisp_pointer(hash_vector_obj) &&
2808 (widetag_of(*(lispobj *)native_pointer(hash_vector_obj))
2809 == SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
2810 hash_vector = ((unsigned int *)native_pointer(hash_vector_obj)) + 2;
2811 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
2812 gc_assert(fixnum_value(((unsigned int *)native_pointer(hash_vector_obj))[1])
2813 == next_vector_length);
2816 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
2820 /* These lengths could be different as the index_vector can be a
2821 * different length from the others, a larger index_vector could help
2822 * reduce collisions. */
2823 gc_assert(next_vector_length*2 == kv_length);
2825 /* now all set up.. */
2827 /* Work through the KV vector. */
2830 for (i = 1; i < next_vector_length; i++) {
2831 lispobj old_key = kv_vector[2*i];
2832 unsigned int old_index = (old_key & 0x1fffffff)%length;
2834 /* Scavenge the key and value. */
2835 scavenge(&kv_vector[2*i],2);
2837 /* Check whether the key has moved and is EQ based. */
2839 lispobj new_key = kv_vector[2*i];
2840 unsigned int new_index = (new_key & 0x1fffffff)%length;
2842 if ((old_index != new_index) &&
2843 ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
2844 ((new_key != empty_symbol) ||
2845 (kv_vector[2*i] != empty_symbol))) {
2848 "* EQ key %d moved from %x to %x; index %d to %d\n",
2849 i, old_key, new_key, old_index, new_index));*/
2851 if (index_vector[old_index] != 0) {
2852 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
2854 /* Unlink the key from the old_index chain. */
2855 if (index_vector[old_index] == i) {
2856 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
2857 index_vector[old_index] = next_vector[i];
2858 /* Link it into the needing rehash chain. */
2859 next_vector[i] = fixnum_value(hash_table[11]);
2860 hash_table[11] = make_fixnum(i);
2863 unsigned prior = index_vector[old_index];
2864 unsigned next = next_vector[prior];
2866 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
2869 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
2872 next_vector[prior] = next_vector[next];
2873 /* Link it into the needing rehash
2876 fixnum_value(hash_table[11]);
2877 hash_table[11] = make_fixnum(next);
2882 next = next_vector[next];
2890 return (CEILING(kv_length + 2, 2));
2894 trans_vector(lispobj object)
2896 struct vector *vector;
2899 gc_assert(is_lisp_pointer(object));
2901 vector = (struct vector *) native_pointer(object);
2903 length = fixnum_value(vector->length);
2904 nwords = CEILING(length + 2, 2);
2906 return copy_large_object(object, nwords);
2910 size_vector(lispobj *where)
2912 struct vector *vector;
2915 vector = (struct vector *) where;
2916 length = fixnum_value(vector->length);
2917 nwords = CEILING(length + 2, 2);
2924 scav_vector_bit(lispobj *where, lispobj object)
2926 struct vector *vector;
2929 vector = (struct vector *) where;
2930 length = fixnum_value(vector->length);
2931 nwords = CEILING(NWORDS(length, 32) + 2, 2);
2937 trans_vector_bit(lispobj object)
2939 struct vector *vector;
2942 gc_assert(is_lisp_pointer(object));
2944 vector = (struct vector *) native_pointer(object);
2945 length = fixnum_value(vector->length);
2946 nwords = CEILING(NWORDS(length, 32) + 2, 2);
2948 return copy_large_unboxed_object(object, nwords);
2952 size_vector_bit(lispobj *where)
2954 struct vector *vector;
2957 vector = (struct vector *) where;
2958 length = fixnum_value(vector->length);
2959 nwords = CEILING(NWORDS(length, 32) + 2, 2);
2966 scav_vector_unsigned_byte_2(lispobj *where, lispobj object)
2968 struct vector *vector;
2971 vector = (struct vector *) where;
2972 length = fixnum_value(vector->length);
2973 nwords = CEILING(NWORDS(length, 16) + 2, 2);
2979 trans_vector_unsigned_byte_2(lispobj object)
2981 struct vector *vector;
2984 gc_assert(is_lisp_pointer(object));
2986 vector = (struct vector *) native_pointer(object);
2987 length = fixnum_value(vector->length);
2988 nwords = CEILING(NWORDS(length, 16) + 2, 2);
2990 return copy_large_unboxed_object(object, nwords);
2994 size_vector_unsigned_byte_2(lispobj *where)
2996 struct vector *vector;
2999 vector = (struct vector *) where;
3000 length = fixnum_value(vector->length);
3001 nwords = CEILING(NWORDS(length, 16) + 2, 2);
3008 scav_vector_unsigned_byte_4(lispobj *where, lispobj object)
3010 struct vector *vector;
3013 vector = (struct vector *) where;
3014 length = fixnum_value(vector->length);
3015 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3021 trans_vector_unsigned_byte_4(lispobj object)
3023 struct vector *vector;
3026 gc_assert(is_lisp_pointer(object));
3028 vector = (struct vector *) native_pointer(object);
3029 length = fixnum_value(vector->length);
3030 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3032 return copy_large_unboxed_object(object, nwords);
3036 size_vector_unsigned_byte_4(lispobj *where)
3038 struct vector *vector;
3041 vector = (struct vector *) where;
3042 length = fixnum_value(vector->length);
3043 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3049 scav_vector_unsigned_byte_8(lispobj *where, lispobj object)
3051 struct vector *vector;
3054 vector = (struct vector *) where;
3055 length = fixnum_value(vector->length);
3056 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3062 trans_vector_unsigned_byte_8(lispobj object)
3064 struct vector *vector;
3067 gc_assert(is_lisp_pointer(object));
3069 vector = (struct vector *) native_pointer(object);
3070 length = fixnum_value(vector->length);
3071 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3073 return copy_large_unboxed_object(object, nwords);
3077 size_vector_unsigned_byte_8(lispobj *where)
3079 struct vector *vector;
3082 vector = (struct vector *) where;
3083 length = fixnum_value(vector->length);
3084 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3091 scav_vector_unsigned_byte_16(lispobj *where, lispobj object)
3093 struct vector *vector;
3096 vector = (struct vector *) where;
3097 length = fixnum_value(vector->length);
3098 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3104 trans_vector_unsigned_byte_16(lispobj object)
3106 struct vector *vector;
3109 gc_assert(is_lisp_pointer(object));
3111 vector = (struct vector *) native_pointer(object);
3112 length = fixnum_value(vector->length);
3113 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3115 return copy_large_unboxed_object(object, nwords);
3119 size_vector_unsigned_byte_16(lispobj *where)
3121 struct vector *vector;
3124 vector = (struct vector *) where;
3125 length = fixnum_value(vector->length);
3126 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3132 scav_vector_unsigned_byte_32(lispobj *where, lispobj object)
3134 struct vector *vector;
3137 vector = (struct vector *) where;
3138 length = fixnum_value(vector->length);
3139 nwords = CEILING(length + 2, 2);
3145 trans_vector_unsigned_byte_32(lispobj object)
3147 struct vector *vector;
3150 gc_assert(is_lisp_pointer(object));
3152 vector = (struct vector *) native_pointer(object);
3153 length = fixnum_value(vector->length);
3154 nwords = CEILING(length + 2, 2);
3156 return copy_large_unboxed_object(object, nwords);
3160 size_vector_unsigned_byte_32(lispobj *where)
3162 struct vector *vector;
3165 vector = (struct vector *) where;
3166 length = fixnum_value(vector->length);
3167 nwords = CEILING(length + 2, 2);
3173 scav_vector_single_float(lispobj *where, lispobj object)
3175 struct vector *vector;
3178 vector = (struct vector *) where;
3179 length = fixnum_value(vector->length);
3180 nwords = CEILING(length + 2, 2);
3186 trans_vector_single_float(lispobj object)
3188 struct vector *vector;
3191 gc_assert(is_lisp_pointer(object));
3193 vector = (struct vector *) native_pointer(object);
3194 length = fixnum_value(vector->length);
3195 nwords = CEILING(length + 2, 2);
3197 return copy_large_unboxed_object(object, nwords);
3201 size_vector_single_float(lispobj *where)
3203 struct vector *vector;
3206 vector = (struct vector *) where;
3207 length = fixnum_value(vector->length);
3208 nwords = CEILING(length + 2, 2);
3214 scav_vector_double_float(lispobj *where, lispobj object)
3216 struct vector *vector;
3219 vector = (struct vector *) where;
3220 length = fixnum_value(vector->length);
3221 nwords = CEILING(length * 2 + 2, 2);
3227 trans_vector_double_float(lispobj object)
3229 struct vector *vector;
3232 gc_assert(is_lisp_pointer(object));
3234 vector = (struct vector *) native_pointer(object);
3235 length = fixnum_value(vector->length);
3236 nwords = CEILING(length * 2 + 2, 2);
3238 return copy_large_unboxed_object(object, nwords);
3242 size_vector_double_float(lispobj *where)
3244 struct vector *vector;
3247 vector = (struct vector *) where;
3248 length = fixnum_value(vector->length);
3249 nwords = CEILING(length * 2 + 2, 2);
3254 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
3256 scav_vector_long_float(lispobj *where, lispobj object)
3258 struct vector *vector;
3261 vector = (struct vector *) where;
3262 length = fixnum_value(vector->length);
3263 nwords = CEILING(length * 3 + 2, 2);
3269 trans_vector_long_float(lispobj object)
3271 struct vector *vector;
3274 gc_assert(is_lisp_pointer(object));
3276 vector = (struct vector *) native_pointer(object);
3277 length = fixnum_value(vector->length);
3278 nwords = CEILING(length * 3 + 2, 2);
3280 return copy_large_unboxed_object(object, nwords);
3284 size_vector_long_float(lispobj *where)
3286 struct vector *vector;
3289 vector = (struct vector *) where;
3290 length = fixnum_value(vector->length);
3291 nwords = CEILING(length * 3 + 2, 2);
3298 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3300 scav_vector_complex_single_float(lispobj *where, lispobj object)
3302 struct vector *vector;
3305 vector = (struct vector *) where;
3306 length = fixnum_value(vector->length);
3307 nwords = CEILING(length * 2 + 2, 2);
3313 trans_vector_complex_single_float(lispobj object)
3315 struct vector *vector;
3318 gc_assert(is_lisp_pointer(object));
3320 vector = (struct vector *) native_pointer(object);
3321 length = fixnum_value(vector->length);
3322 nwords = CEILING(length * 2 + 2, 2);
3324 return copy_large_unboxed_object(object, nwords);
3328 size_vector_complex_single_float(lispobj *where)
3330 struct vector *vector;
3333 vector = (struct vector *) where;
3334 length = fixnum_value(vector->length);
3335 nwords = CEILING(length * 2 + 2, 2);
3341 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3343 scav_vector_complex_double_float(lispobj *where, lispobj object)
3345 struct vector *vector;
3348 vector = (struct vector *) where;
3349 length = fixnum_value(vector->length);
3350 nwords = CEILING(length * 4 + 2, 2);
3356 trans_vector_complex_double_float(lispobj object)
3358 struct vector *vector;
3361 gc_assert(is_lisp_pointer(object));
3363 vector = (struct vector *) native_pointer(object);
3364 length = fixnum_value(vector->length);
3365 nwords = CEILING(length * 4 + 2, 2);
3367 return copy_large_unboxed_object(object, nwords);
3371 size_vector_complex_double_float(lispobj *where)
3373 struct vector *vector;
3376 vector = (struct vector *) where;
3377 length = fixnum_value(vector->length);
3378 nwords = CEILING(length * 4 + 2, 2);
3385 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3387 scav_vector_complex_long_float(lispobj *where, lispobj object)
3389 struct vector *vector;
3392 vector = (struct vector *) where;
3393 length = fixnum_value(vector->length);
3394 nwords = CEILING(length * 6 + 2, 2);
3400 trans_vector_complex_long_float(lispobj object)
3402 struct vector *vector;
3405 gc_assert(is_lisp_pointer(object));
3407 vector = (struct vector *) native_pointer(object);
3408 length = fixnum_value(vector->length);
3409 nwords = CEILING(length * 6 + 2, 2);
3411 return copy_large_unboxed_object(object, nwords);
3415 size_vector_complex_long_float(lispobj *where)
3417 struct vector *vector;
3420 vector = (struct vector *) where;
3421 length = fixnum_value(vector->length);
3422 nwords = CEILING(length * 6 + 2, 2);
3433 /* XX This is a hack adapted from cgc.c. These don't work too well with the
3434 * gencgc as a list of the weak pointers is maintained within the
3435 * objects which causes writes to the pages. A limited attempt is made
3436 * to avoid unnecessary writes, but this needs a re-think. */
3438 #define WEAK_POINTER_NWORDS \
3439 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
3442 scav_weak_pointer(lispobj *where, lispobj object)
3444 struct weak_pointer *wp = weak_pointers;
3445 /* Push the weak pointer onto the list of weak pointers.
3446 * Do I have to watch for duplicates? Originally this was
3447 * part of trans_weak_pointer but that didn't work in the
3448 * case where the WP was in a promoted region.
3451 /* Check whether it's already in the list. */
3452 while (wp != NULL) {
3453 if (wp == (struct weak_pointer*)where) {
3459 /* Add it to the start of the list. */
3460 wp = (struct weak_pointer*)where;
3461 if (wp->next != weak_pointers) {
3462 wp->next = weak_pointers;
3464 /*SHOW("avoided write to weak pointer");*/
3469 /* Do not let GC scavenge the value slot of the weak pointer.
3470 * (That is why it is a weak pointer.) */
3472 return WEAK_POINTER_NWORDS;
3476 trans_weak_pointer(lispobj object)
3479 /* struct weak_pointer *wp; */
3481 gc_assert(is_lisp_pointer(object));
3483 #if defined(DEBUG_WEAK)
3484 FSHOW((stderr, "Transporting weak pointer from 0x%08x\n", object));
3487 /* Need to remember where all the weak pointers are that have */
3488 /* been transported so they can be fixed up in a post-GC pass. */
3490 copy = copy_object(object, WEAK_POINTER_NWORDS);
3491 /* wp = (struct weak_pointer *) native_pointer(copy);*/
3494 /* Push the weak pointer onto the list of weak pointers. */
3495 /* wp->next = weak_pointers;
3496 * weak_pointers = wp;*/
3502 size_weak_pointer(lispobj *where)
3504 return WEAK_POINTER_NWORDS;
3507 void scan_weak_pointers(void)
3509 struct weak_pointer *wp;
3510 for (wp = weak_pointers; wp != NULL; wp = wp->next) {
3511 lispobj value = wp->value;
3512 lispobj *first_pointer;
3514 first_pointer = (lispobj *)native_pointer(value);
3516 if (is_lisp_pointer(value) && from_space_p(value)) {
3517 /* Now, we need to check whether the object has been forwarded. If
3518 * it has been, the weak pointer is still good and needs to be
3519 * updated. Otherwise, the weak pointer needs to be nil'ed
3521 if (first_pointer[0] == 0x01) {
3522 wp->value = first_pointer[1];
3537 scav_lose(lispobj *where, lispobj object)
3539 lose("no scavenge function for object 0x%08x (widetag 0x%x)",
3540 (unsigned long)object,
3541 widetag_of(*(lispobj*)native_pointer(object)));
3542 return 0; /* bogus return value to satisfy static type checking */
3546 trans_lose(lispobj object)
3548 lose("no transport function for object 0x%08x (widetag 0x%x)",
3549 (unsigned long)object,
3550 widetag_of(*(lispobj*)native_pointer(object)));
3551 return NIL; /* bogus return value to satisfy static type checking */
3555 size_lose(lispobj *where)
3557 lose("no size function for object at 0x%08x (widetag 0x%x)",
3558 (unsigned long)where,
3560 return 1; /* bogus return value to satisfy static type checking */
3564 gc_init_tables(void)
3568 /* Set default value in all slots of scavenge table. */
3569 for (i = 0; i < 256; i++) { /* FIXME: bare constant length, ick! */
3570 scavtab[i] = scav_lose;
3573 /* For each type which can be selected by the lowtag alone, set
3574 * multiple entries in our widetag scavenge table (one for each
3575 * possible value of the high bits).
3577 * FIXME: bare constant 32 and 3 here, ick! */
3578 for (i = 0; i < 32; i++) {
3579 scavtab[EVEN_FIXNUM_LOWTAG|(i<<3)] = scav_immediate;
3580 scavtab[FUN_POINTER_LOWTAG|(i<<3)] = scav_fun_pointer;
3581 /* skipping OTHER_IMMEDIATE_0_LOWTAG */
3582 scavtab[LIST_POINTER_LOWTAG|(i<<3)] = scav_list_pointer;
3583 scavtab[ODD_FIXNUM_LOWTAG|(i<<3)] = scav_immediate;
3584 scavtab[INSTANCE_POINTER_LOWTAG|(i<<3)] = scav_instance_pointer;
3585 /* skipping OTHER_IMMEDIATE_1_LOWTAG */
3586 scavtab[OTHER_POINTER_LOWTAG|(i<<3)] = scav_other_pointer;
3589 /* Other-pointer types (those selected by all eight bits of the
3590 * tag) get one entry each in the scavenge table. */
3591 scavtab[BIGNUM_WIDETAG] = scav_unboxed;
3592 scavtab[RATIO_WIDETAG] = scav_boxed;
3593 scavtab[SINGLE_FLOAT_WIDETAG] = scav_unboxed;
3594 scavtab[DOUBLE_FLOAT_WIDETAG] = scav_unboxed;
3595 #ifdef LONG_FLOAT_WIDETAG
3596 scavtab[LONG_FLOAT_WIDETAG] = scav_unboxed;
3598 scavtab[COMPLEX_WIDETAG] = scav_boxed;
3599 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3600 scavtab[COMPLEX_SINGLE_FLOAT_WIDETAG] = scav_unboxed;
3602 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3603 scavtab[COMPLEX_DOUBLE_FLOAT_WIDETAG] = scav_unboxed;
3605 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3606 scavtab[COMPLEX_LONG_FLOAT_WIDETAG] = scav_unboxed;
3608 scavtab[SIMPLE_ARRAY_WIDETAG] = scav_boxed;
3609 scavtab[SIMPLE_STRING_WIDETAG] = scav_string;
3610 scavtab[SIMPLE_BIT_VECTOR_WIDETAG] = scav_vector_bit;
3611 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
3612 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
3613 scav_vector_unsigned_byte_2;
3614 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
3615 scav_vector_unsigned_byte_4;
3616 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
3617 scav_vector_unsigned_byte_8;
3618 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
3619 scav_vector_unsigned_byte_16;
3620 scavtab[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
3621 scav_vector_unsigned_byte_32;
3622 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3623 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] = scav_vector_unsigned_byte_8;
3625 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3626 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
3627 scav_vector_unsigned_byte_16;
3629 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3630 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG] =
3631 scav_vector_unsigned_byte_32;
3633 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3634 scavtab[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
3635 scav_vector_unsigned_byte_32;
3637 scavtab[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] = scav_vector_single_float;
3638 scavtab[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] = scav_vector_double_float;
3639 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
3640 scavtab[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] = scav_vector_long_float;
3642 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3643 scavtab[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
3644 scav_vector_complex_single_float;
3646 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3647 scavtab[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
3648 scav_vector_complex_double_float;
3650 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3651 scavtab[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
3652 scav_vector_complex_long_float;
3654 scavtab[COMPLEX_STRING_WIDETAG] = scav_boxed;
3655 scavtab[COMPLEX_BIT_VECTOR_WIDETAG] = scav_boxed;
3656 scavtab[COMPLEX_VECTOR_WIDETAG] = scav_boxed;
3657 scavtab[COMPLEX_ARRAY_WIDETAG] = scav_boxed;
3658 scavtab[CODE_HEADER_WIDETAG] = scav_code_header;
3659 /*scavtab[SIMPLE_FUN_HEADER_WIDETAG] = scav_fun_header;*/
3660 /*scavtab[CLOSURE_FUN_HEADER_WIDETAG] = scav_fun_header;*/
3661 /*scavtab[RETURN_PC_HEADER_WIDETAG] = scav_return_pc_header;*/
3663 scavtab[CLOSURE_HEADER_WIDETAG] = scav_closure_header;
3664 scavtab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = scav_closure_header;
3666 scavtab[CLOSURE_HEADER_WIDETAG] = scav_boxed;
3667 scavtab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = scav_boxed;
3669 scavtab[VALUE_CELL_HEADER_WIDETAG] = scav_boxed;
3670 scavtab[SYMBOL_HEADER_WIDETAG] = scav_boxed;
3671 scavtab[BASE_CHAR_WIDETAG] = scav_immediate;
3672 scavtab[SAP_WIDETAG] = scav_unboxed;
3673 scavtab[UNBOUND_MARKER_WIDETAG] = scav_immediate;
3674 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
3675 scavtab[INSTANCE_HEADER_WIDETAG] = scav_boxed;
3676 scavtab[FDEFN_WIDETAG] = scav_fdefn;
3678 /* transport other table, initialized same way as scavtab */
3679 for (i = 0; i < 256; i++)
3680 transother[i] = trans_lose;
3681 transother[BIGNUM_WIDETAG] = trans_unboxed;
3682 transother[RATIO_WIDETAG] = trans_boxed;
3683 transother[SINGLE_FLOAT_WIDETAG] = trans_unboxed;
3684 transother[DOUBLE_FLOAT_WIDETAG] = trans_unboxed;
3685 #ifdef LONG_FLOAT_WIDETAG
3686 transother[LONG_FLOAT_WIDETAG] = trans_unboxed;
3688 transother[COMPLEX_WIDETAG] = trans_boxed;
3689 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3690 transother[COMPLEX_SINGLE_FLOAT_WIDETAG] = trans_unboxed;
3692 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3693 transother[COMPLEX_DOUBLE_FLOAT_WIDETAG] = trans_unboxed;
3695 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3696 transother[COMPLEX_LONG_FLOAT_WIDETAG] = trans_unboxed;
3698 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
3699 transother[SIMPLE_STRING_WIDETAG] = trans_string;
3700 transother[SIMPLE_BIT_VECTOR_WIDETAG] = trans_vector_bit;
3701 transother[SIMPLE_VECTOR_WIDETAG] = trans_vector;
3702 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
3703 trans_vector_unsigned_byte_2;
3704 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
3705 trans_vector_unsigned_byte_4;
3706 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
3707 trans_vector_unsigned_byte_8;
3708 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
3709 trans_vector_unsigned_byte_16;
3710 transother[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
3711 trans_vector_unsigned_byte_32;
3712 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3713 transother[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] =
3714 trans_vector_unsigned_byte_8;
3716 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3717 transother[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
3718 trans_vector_unsigned_byte_16;
3720 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3721 transother[SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG] =
3722 trans_vector_unsigned_byte_32;
3724 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3725 transother[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
3726 trans_vector_unsigned_byte_32;
3728 transother[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] =
3729 trans_vector_single_float;
3730 transother[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] =
3731 trans_vector_double_float;
3732 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
3733 transother[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] =
3734 trans_vector_long_float;
3736 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3737 transother[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
3738 trans_vector_complex_single_float;
3740 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3741 transother[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
3742 trans_vector_complex_double_float;
3744 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3745 transother[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
3746 trans_vector_complex_long_float;
3748 transother[COMPLEX_STRING_WIDETAG] = trans_boxed;
3749 transother[COMPLEX_BIT_VECTOR_WIDETAG] = trans_boxed;
3750 transother[COMPLEX_VECTOR_WIDETAG] = trans_boxed;
3751 transother[COMPLEX_ARRAY_WIDETAG] = trans_boxed;
3752 transother[CODE_HEADER_WIDETAG] = trans_code_header;
3753 transother[SIMPLE_FUN_HEADER_WIDETAG] = trans_fun_header;
3754 transother[CLOSURE_FUN_HEADER_WIDETAG] = trans_fun_header;
3755 transother[RETURN_PC_HEADER_WIDETAG] = trans_return_pc_header;
3756 transother[CLOSURE_HEADER_WIDETAG] = trans_boxed;
3757 transother[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = trans_boxed;
3758 transother[VALUE_CELL_HEADER_WIDETAG] = trans_boxed;
3759 transother[SYMBOL_HEADER_WIDETAG] = trans_boxed;
3760 transother[BASE_CHAR_WIDETAG] = trans_immediate;
3761 transother[SAP_WIDETAG] = trans_unboxed;
3762 transother[UNBOUND_MARKER_WIDETAG] = trans_immediate;
3763 transother[WEAK_POINTER_WIDETAG] = trans_weak_pointer;
3764 transother[INSTANCE_HEADER_WIDETAG] = trans_boxed;
3765 transother[FDEFN_WIDETAG] = trans_boxed;
3767 /* size table, initialized the same way as scavtab */
3768 for (i = 0; i < 256; i++)
3769 sizetab[i] = size_lose;
3770 for (i = 0; i < 32; i++) {
3771 sizetab[EVEN_FIXNUM_LOWTAG|(i<<3)] = size_immediate;
3772 sizetab[FUN_POINTER_LOWTAG|(i<<3)] = size_pointer;
3773 /* skipping OTHER_IMMEDIATE_0_LOWTAG */
3774 sizetab[LIST_POINTER_LOWTAG|(i<<3)] = size_pointer;
3775 sizetab[ODD_FIXNUM_LOWTAG|(i<<3)] = size_immediate;
3776 sizetab[INSTANCE_POINTER_LOWTAG|(i<<3)] = size_pointer;
3777 /* skipping OTHER_IMMEDIATE_1_LOWTAG */
3778 sizetab[OTHER_POINTER_LOWTAG|(i<<3)] = size_pointer;
3780 sizetab[BIGNUM_WIDETAG] = size_unboxed;
3781 sizetab[RATIO_WIDETAG] = size_boxed;
3782 sizetab[SINGLE_FLOAT_WIDETAG] = size_unboxed;
3783 sizetab[DOUBLE_FLOAT_WIDETAG] = size_unboxed;
3784 #ifdef LONG_FLOAT_WIDETAG
3785 sizetab[LONG_FLOAT_WIDETAG] = size_unboxed;
3787 sizetab[COMPLEX_WIDETAG] = size_boxed;
3788 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3789 sizetab[COMPLEX_SINGLE_FLOAT_WIDETAG] = size_unboxed;
3791 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3792 sizetab[COMPLEX_DOUBLE_FLOAT_WIDETAG] = size_unboxed;
3794 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3795 sizetab[COMPLEX_LONG_FLOAT_WIDETAG] = size_unboxed;
3797 sizetab[SIMPLE_ARRAY_WIDETAG] = size_boxed;
3798 sizetab[SIMPLE_STRING_WIDETAG] = size_string;
3799 sizetab[SIMPLE_BIT_VECTOR_WIDETAG] = size_vector_bit;
3800 sizetab[SIMPLE_VECTOR_WIDETAG] = size_vector;
3801 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG] =
3802 size_vector_unsigned_byte_2;
3803 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG] =
3804 size_vector_unsigned_byte_4;
3805 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG] =
3806 size_vector_unsigned_byte_8;
3807 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG] =
3808 size_vector_unsigned_byte_16;
3809 sizetab[SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG] =
3810 size_vector_unsigned_byte_32;
3811 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3812 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG] = size_vector_unsigned_byte_8;
3814 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3815 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG] =
3816 size_vector_unsigned_byte_16;
3818 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3819 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG] =
3820 size_vector_unsigned_byte_32;
3822 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3823 sizetab[SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG] =
3824 size_vector_unsigned_byte_32;
3826 sizetab[SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG] = size_vector_single_float;
3827 sizetab[SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG] = size_vector_double_float;
3828 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
3829 sizetab[SIMPLE_ARRAY_LONG_FLOAT_WIDETAG] = size_vector_long_float;
3831 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3832 sizetab[SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG] =
3833 size_vector_complex_single_float;
3835 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3836 sizetab[SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG] =
3837 size_vector_complex_double_float;
3839 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3840 sizetab[SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG] =
3841 size_vector_complex_long_float;
3843 sizetab[COMPLEX_STRING_WIDETAG] = size_boxed;
3844 sizetab[COMPLEX_BIT_VECTOR_WIDETAG] = size_boxed;
3845 sizetab[COMPLEX_VECTOR_WIDETAG] = size_boxed;
3846 sizetab[COMPLEX_ARRAY_WIDETAG] = size_boxed;
3847 sizetab[CODE_HEADER_WIDETAG] = size_code_header;
3849 /* We shouldn't see these, so just lose if it happens. */
3850 sizetab[SIMPLE_FUN_HEADER_WIDETAG] = size_function_header;
3851 sizetab[CLOSURE_FUN_HEADER_WIDETAG] = size_function_header;
3852 sizetab[RETURN_PC_HEADER_WIDETAG] = size_return_pc_header;
3854 sizetab[CLOSURE_HEADER_WIDETAG] = size_boxed;
3855 sizetab[FUNCALLABLE_INSTANCE_HEADER_WIDETAG] = size_boxed;
3856 sizetab[VALUE_CELL_HEADER_WIDETAG] = size_boxed;
3857 sizetab[SYMBOL_HEADER_WIDETAG] = size_boxed;
3858 sizetab[BASE_CHAR_WIDETAG] = size_immediate;
3859 sizetab[SAP_WIDETAG] = size_unboxed;
3860 sizetab[UNBOUND_MARKER_WIDETAG] = size_immediate;
3861 sizetab[WEAK_POINTER_WIDETAG] = size_weak_pointer;
3862 sizetab[INSTANCE_HEADER_WIDETAG] = size_boxed;
3863 sizetab[FDEFN_WIDETAG] = size_boxed;
3866 /* Scan an area looking for an object which encloses the given pointer.
3867 * Return the object start on success or NULL on failure. */
3869 search_space(lispobj *start, size_t words, lispobj *pointer)
3873 lispobj thing = *start;
3875 /* If thing is an immediate then this is a cons. */
3876 if (is_lisp_pointer(thing)
3877 || ((thing & 3) == 0) /* fixnum */
3878 || (widetag_of(thing) == BASE_CHAR_WIDETAG)
3879 || (widetag_of(thing) == UNBOUND_MARKER_WIDETAG))
3882 count = (sizetab[widetag_of(thing)])(start);
3884 /* Check whether the pointer is within this object. */
3885 if ((pointer >= start) && (pointer < (start+count))) {
3887 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
3891 /* Round up the count. */
3892 count = CEILING(count,2);
3901 search_read_only_space(lispobj *pointer)
3903 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
3904 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
3905 if ((pointer < start) || (pointer >= end))
3907 return (search_space(start, (pointer+2)-start, pointer));
3911 search_static_space(lispobj *pointer)
3913 lispobj* start = (lispobj*)STATIC_SPACE_START;
3914 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER);
3915 if ((pointer < start) || (pointer >= end))
3917 return (search_space(start, (pointer+2)-start, pointer));
3920 /* a faster version for searching the dynamic space. This will work even
3921 * if the object is in a current allocation region. */
3923 search_dynamic_space(lispobj *pointer)
3925 int page_index = find_page_index(pointer);
3928 /* The address may be invalid, so do some checks. */
3929 if ((page_index == -1) || (page_table[page_index].allocated == FREE_PAGE))
3931 start = (lispobj *)((void *)page_address(page_index)
3932 + page_table[page_index].first_object_offset);
3933 return (search_space(start, (pointer+2)-start, pointer));
3936 /* Is there any possibility that pointer is a valid Lisp object
3937 * reference, and/or something else (e.g. subroutine call return
3938 * address) which should prevent us from moving the referred-to thing? */
3940 possibly_valid_dynamic_space_pointer(lispobj *pointer)
3942 lispobj *start_addr;
3944 /* Find the object start address. */
3945 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
3949 /* We need to allow raw pointers into Code objects for return
3950 * addresses. This will also pick up pointers to functions in code
3952 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
3953 /* XXX could do some further checks here */
3957 /* If it's not a return address then it needs to be a valid Lisp
3959 if (!is_lisp_pointer((lispobj)pointer)) {
3963 /* Check that the object pointed to is consistent with the pointer
3966 * FIXME: It's not safe to rely on the result from this check
3967 * before an object is initialized. Thus, if we were interrupted
3968 * just as an object had been allocated but not initialized, the
3969 * GC relying on this result could bogusly reclaim the memory.
3970 * However, we can't really afford to do without this check. So
3971 * we should make it safe somehow.
3972 * (1) Perhaps just review the code to make sure
3973 * that WITHOUT-GCING or WITHOUT-INTERRUPTS or some such
3974 * thing is wrapped around critical sections where allocated
3975 * memory type bits haven't been set.
3976 * (2) Perhaps find some other hack to protect against this, e.g.
3977 * recording the result of the last call to allocate-lisp-memory,
3978 * and returning true from this function when *pointer is
3979 * a reference to that result. */
3980 switch (lowtag_of((lispobj)pointer)) {
3981 case FUN_POINTER_LOWTAG:
3982 /* Start_addr should be the enclosing code object, or a closure
3984 switch (widetag_of(*start_addr)) {
3985 case CODE_HEADER_WIDETAG:
3986 /* This case is probably caught above. */
3988 case CLOSURE_HEADER_WIDETAG:
3989 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3990 if ((unsigned)pointer !=
3991 ((unsigned)start_addr+FUN_POINTER_LOWTAG)) {
3995 pointer, start_addr, *start_addr));
4003 pointer, start_addr, *start_addr));
4007 case LIST_POINTER_LOWTAG:
4008 if ((unsigned)pointer !=
4009 ((unsigned)start_addr+LIST_POINTER_LOWTAG)) {
4013 pointer, start_addr, *start_addr));
4016 /* Is it plausible cons? */
4017 if ((is_lisp_pointer(start_addr[0])
4018 || ((start_addr[0] & 3) == 0) /* fixnum */
4019 || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
4020 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
4021 && (is_lisp_pointer(start_addr[1])
4022 || ((start_addr[1] & 3) == 0) /* fixnum */
4023 || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
4024 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
4030 pointer, start_addr, *start_addr));
4033 case INSTANCE_POINTER_LOWTAG:
4034 if ((unsigned)pointer !=
4035 ((unsigned)start_addr+INSTANCE_POINTER_LOWTAG)) {
4039 pointer, start_addr, *start_addr));
4042 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
4046 pointer, start_addr, *start_addr));
4050 case OTHER_POINTER_LOWTAG:
4051 if ((unsigned)pointer !=
4052 ((int)start_addr+OTHER_POINTER_LOWTAG)) {
4056 pointer, start_addr, *start_addr));
4059 /* Is it plausible? Not a cons. XXX should check the headers. */
4060 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
4064 pointer, start_addr, *start_addr));
4067 switch (widetag_of(start_addr[0])) {
4068 case UNBOUND_MARKER_WIDETAG:
4069 case BASE_CHAR_WIDETAG:
4073 pointer, start_addr, *start_addr));
4076 /* only pointed to by function pointers? */
4077 case CLOSURE_HEADER_WIDETAG:
4078 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
4082 pointer, start_addr, *start_addr));
4085 case INSTANCE_HEADER_WIDETAG:
4089 pointer, start_addr, *start_addr));
4092 /* the valid other immediate pointer objects */
4093 case SIMPLE_VECTOR_WIDETAG:
4095 case COMPLEX_WIDETAG:
4096 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
4097 case COMPLEX_SINGLE_FLOAT_WIDETAG:
4099 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
4100 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
4102 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
4103 case COMPLEX_LONG_FLOAT_WIDETAG:
4105 case SIMPLE_ARRAY_WIDETAG:
4106 case COMPLEX_STRING_WIDETAG:
4107 case COMPLEX_BIT_VECTOR_WIDETAG:
4108 case COMPLEX_VECTOR_WIDETAG:
4109 case COMPLEX_ARRAY_WIDETAG:
4110 case VALUE_CELL_HEADER_WIDETAG:
4111 case SYMBOL_HEADER_WIDETAG:
4113 case CODE_HEADER_WIDETAG:
4114 case BIGNUM_WIDETAG:
4115 case SINGLE_FLOAT_WIDETAG:
4116 case DOUBLE_FLOAT_WIDETAG:
4117 #ifdef LONG_FLOAT_WIDETAG
4118 case LONG_FLOAT_WIDETAG:
4120 case SIMPLE_STRING_WIDETAG:
4121 case SIMPLE_BIT_VECTOR_WIDETAG:
4122 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
4123 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
4124 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
4125 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
4126 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
4127 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
4128 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
4130 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
4131 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
4133 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
4134 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
4136 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
4137 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
4139 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
4140 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
4141 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
4142 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
4144 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
4145 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
4147 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
4148 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
4150 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
4151 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
4154 case WEAK_POINTER_WIDETAG:
4161 pointer, start_addr, *start_addr));
4169 pointer, start_addr, *start_addr));
4177 /* Adjust large bignum and vector objects. This will adjust the
4178 * allocated region if the size has shrunk, and move unboxed objects
4179 * into unboxed pages. The pages are not promoted here, and the
4180 * promoted region is not added to the new_regions; this is really
4181 * only designed to be called from preserve_pointer(). Shouldn't fail
4182 * if this is missed, just may delay the moving of objects to unboxed
4183 * pages, and the freeing of pages. */
4185 maybe_adjust_large_object(lispobj *where)
4190 int remaining_bytes;
4197 /* Check whether it's a vector or bignum object. */
4198 switch (widetag_of(where[0])) {
4199 case SIMPLE_VECTOR_WIDETAG:
4202 case BIGNUM_WIDETAG:
4203 case SIMPLE_STRING_WIDETAG:
4204 case SIMPLE_BIT_VECTOR_WIDETAG:
4205 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
4206 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
4207 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
4208 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
4209 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
4210 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
4211 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
4213 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
4214 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
4216 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
4217 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
4219 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
4220 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
4222 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
4223 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
4224 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
4225 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
4227 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
4228 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
4230 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
4231 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
4233 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
4234 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
4236 boxed = UNBOXED_PAGE;
4242 /* Find its current size. */
4243 nwords = (sizetab[widetag_of(where[0])])(where);
4245 first_page = find_page_index((void *)where);
4246 gc_assert(first_page >= 0);
4248 /* Note: Any page write-protection must be removed, else a later
4249 * scavenge_newspace may incorrectly not scavenge these pages.
4250 * This would not be necessary if they are added to the new areas,
4251 * but lets do it for them all (they'll probably be written
4254 gc_assert(page_table[first_page].first_object_offset == 0);
4256 next_page = first_page;
4257 remaining_bytes = nwords*4;
4258 while (remaining_bytes > 4096) {
4259 gc_assert(page_table[next_page].gen == from_space);
4260 gc_assert((page_table[next_page].allocated == BOXED_PAGE)
4261 || (page_table[next_page].allocated == UNBOXED_PAGE));
4262 gc_assert(page_table[next_page].large_object);
4263 gc_assert(page_table[next_page].first_object_offset ==
4264 -4096*(next_page-first_page));
4265 gc_assert(page_table[next_page].bytes_used == 4096);
4267 page_table[next_page].allocated = boxed;
4269 /* Shouldn't be write-protected at this stage. Essential that the
4271 gc_assert(!page_table[next_page].write_protected);
4272 remaining_bytes -= 4096;
4276 /* Now only one page remains, but the object may have shrunk so
4277 * there may be more unused pages which will be freed. */
4279 /* Object may have shrunk but shouldn't have grown - check. */
4280 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
4282 page_table[next_page].allocated = boxed;
4283 gc_assert(page_table[next_page].allocated ==
4284 page_table[first_page].allocated);
4286 /* Adjust the bytes_used. */
4287 old_bytes_used = page_table[next_page].bytes_used;
4288 page_table[next_page].bytes_used = remaining_bytes;
4290 bytes_freed = old_bytes_used - remaining_bytes;
4292 /* Free any remaining pages; needs care. */
4294 while ((old_bytes_used == 4096) &&
4295 (page_table[next_page].gen == from_space) &&
4296 ((page_table[next_page].allocated == UNBOXED_PAGE)
4297 || (page_table[next_page].allocated == BOXED_PAGE)) &&
4298 page_table[next_page].large_object &&
4299 (page_table[next_page].first_object_offset ==
4300 -(next_page - first_page)*4096)) {
4301 /* It checks out OK, free the page. We don't need to both zeroing
4302 * pages as this should have been done before shrinking the
4303 * object. These pages shouldn't be write protected as they
4304 * should be zero filled. */
4305 gc_assert(page_table[next_page].write_protected == 0);
4307 old_bytes_used = page_table[next_page].bytes_used;
4308 page_table[next_page].allocated = FREE_PAGE;
4309 page_table[next_page].bytes_used = 0;
4310 bytes_freed += old_bytes_used;
4314 if ((bytes_freed > 0) && gencgc_verbose) {
4316 "/maybe_adjust_large_object() freed %d\n",
4320 generations[from_space].bytes_allocated -= bytes_freed;
4321 bytes_allocated -= bytes_freed;
4326 /* Take a possible pointer to a Lisp object and mark its page in the
4327 * page_table so that it will not be relocated during a GC.
4329 * This involves locating the page it points to, then backing up to
4330 * the first page that has its first object start at offset 0, and
4331 * then marking all pages dont_move from the first until a page that
4332 * ends by being full, or having free gen.
4334 * This ensures that objects spanning pages are not broken.
4336 * It is assumed that all the page static flags have been cleared at
4337 * the start of a GC.
4339 * It is also assumed that the current gc_alloc() region has been
4340 * flushed and the tables updated. */
4342 preserve_pointer(void *addr)
4344 int addr_page_index = find_page_index(addr);
4347 unsigned region_allocation;
4349 /* quick check 1: Address is quite likely to have been invalid. */
4350 if ((addr_page_index == -1)
4351 || (page_table[addr_page_index].allocated == FREE_PAGE)
4352 || (page_table[addr_page_index].bytes_used == 0)
4353 || (page_table[addr_page_index].gen != from_space)
4354 /* Skip if already marked dont_move. */
4355 || (page_table[addr_page_index].dont_move != 0))
4358 /* (Now that we know that addr_page_index is in range, it's
4359 * safe to index into page_table[] with it.) */
4360 region_allocation = page_table[addr_page_index].allocated;
4362 /* quick check 2: Check the offset within the page.
4364 * FIXME: The mask should have a symbolic name, and ideally should
4365 * be derived from page size instead of hardwired to 0xfff.
4366 * (Also fix other uses of 0xfff, elsewhere.) */
4367 if (((unsigned)addr & 0xfff) > page_table[addr_page_index].bytes_used)
4370 /* Filter out anything which can't be a pointer to a Lisp object
4371 * (or, as a special case which also requires dont_move, a return
4372 * address referring to something in a CodeObject). This is
4373 * expensive but important, since it vastly reduces the
4374 * probability that random garbage will be bogusly interpreter as
4375 * a pointer which prevents a page from moving. */
4376 if (!possibly_valid_dynamic_space_pointer(addr))
4379 /* Work backwards to find a page with a first_object_offset of 0.
4380 * The pages should be contiguous with all bytes used in the same
4381 * gen. Assumes the first_object_offset is negative or zero. */
4382 first_page = addr_page_index;
4383 while (page_table[first_page].first_object_offset != 0) {
4385 /* Do some checks. */
4386 gc_assert(page_table[first_page].bytes_used == 4096);
4387 gc_assert(page_table[first_page].gen == from_space);
4388 gc_assert(page_table[first_page].allocated == region_allocation);
4391 /* Adjust any large objects before promotion as they won't be
4392 * copied after promotion. */
4393 if (page_table[first_page].large_object) {
4394 maybe_adjust_large_object(page_address(first_page));
4395 /* If a large object has shrunk then addr may now point to a
4396 * free area in which case it's ignored here. Note it gets
4397 * through the valid pointer test above because the tail looks
4399 if ((page_table[addr_page_index].allocated == FREE_PAGE)
4400 || (page_table[addr_page_index].bytes_used == 0)
4401 /* Check the offset within the page. */
4402 || (((unsigned)addr & 0xfff)
4403 > page_table[addr_page_index].bytes_used)) {
4405 "weird? ignore ptr 0x%x to freed area of large object\n",
4409 /* It may have moved to unboxed pages. */
4410 region_allocation = page_table[first_page].allocated;
4413 /* Now work forward until the end of this contiguous area is found,
4414 * marking all pages as dont_move. */
4415 for (i = first_page; ;i++) {
4416 gc_assert(page_table[i].allocated == region_allocation);
4418 /* Mark the page static. */
4419 page_table[i].dont_move = 1;
4421 /* Move the page to the new_space. XX I'd rather not do this
4422 * but the GC logic is not quite able to copy with the static
4423 * pages remaining in the from space. This also requires the
4424 * generation bytes_allocated counters be updated. */
4425 page_table[i].gen = new_space;
4426 generations[new_space].bytes_allocated += page_table[i].bytes_used;
4427 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
4429 /* It is essential that the pages are not write protected as
4430 * they may have pointers into the old-space which need
4431 * scavenging. They shouldn't be write protected at this
4433 gc_assert(!page_table[i].write_protected);
4435 /* Check whether this is the last page in this contiguous block.. */
4436 if ((page_table[i].bytes_used < 4096)
4437 /* ..or it is 4096 and is the last in the block */
4438 || (page_table[i+1].allocated == FREE_PAGE)
4439 || (page_table[i+1].bytes_used == 0) /* next page free */
4440 || (page_table[i+1].gen != from_space) /* diff. gen */
4441 || (page_table[i+1].first_object_offset == 0))
4445 /* Check that the page is now static. */
4446 gc_assert(page_table[addr_page_index].dont_move != 0);
4449 /* If the given page is not write-protected, then scan it for pointers
4450 * to younger generations or the top temp. generation, if no
4451 * suspicious pointers are found then the page is write-protected.
4453 * Care is taken to check for pointers to the current gc_alloc()
4454 * region if it is a younger generation or the temp. generation. This
4455 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
4456 * the gc_alloc_generation does not need to be checked as this is only
4457 * called from scavenge_generation() when the gc_alloc generation is
4458 * younger, so it just checks if there is a pointer to the current
4461 * We return 1 if the page was write-protected, else 0. */
4463 update_page_write_prot(int page)
4465 int gen = page_table[page].gen;
4468 void **page_addr = (void **)page_address(page);
4469 int num_words = page_table[page].bytes_used / 4;
4471 /* Shouldn't be a free page. */
4472 gc_assert(page_table[page].allocated != FREE_PAGE);
4473 gc_assert(page_table[page].bytes_used != 0);
4475 /* Skip if it's already write-protected or an unboxed page. */
4476 if (page_table[page].write_protected
4477 || (page_table[page].allocated == UNBOXED_PAGE))
4480 /* Scan the page for pointers to younger generations or the
4481 * top temp. generation. */
4483 for (j = 0; j < num_words; j++) {
4484 void *ptr = *(page_addr+j);
4485 int index = find_page_index(ptr);
4487 /* Check that it's in the dynamic space */
4489 if (/* Does it point to a younger or the temp. generation? */
4490 ((page_table[index].allocated != FREE_PAGE)
4491 && (page_table[index].bytes_used != 0)
4492 && ((page_table[index].gen < gen)
4493 || (page_table[index].gen == NUM_GENERATIONS)))
4495 /* Or does it point within a current gc_alloc() region? */
4496 || ((boxed_region.start_addr <= ptr)
4497 && (ptr <= boxed_region.free_pointer))
4498 || ((unboxed_region.start_addr <= ptr)
4499 && (ptr <= unboxed_region.free_pointer))) {
4506 /* Write-protect the page. */
4507 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
4509 os_protect((void *)page_addr,
4511 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
4513 /* Note the page as protected in the page tables. */
4514 page_table[page].write_protected = 1;
4520 /* Scavenge a generation.
4522 * This will not resolve all pointers when generation is the new
4523 * space, as new objects may be added which are not check here - use
4524 * scavenge_newspace generation.
4526 * Write-protected pages should not have any pointers to the
4527 * from_space so do need scavenging; thus write-protected pages are
4528 * not always scavenged. There is some code to check that these pages
4529 * are not written; but to check fully the write-protected pages need
4530 * to be scavenged by disabling the code to skip them.
4532 * Under the current scheme when a generation is GCed the younger
4533 * generations will be empty. So, when a generation is being GCed it
4534 * is only necessary to scavenge the older generations for pointers
4535 * not the younger. So a page that does not have pointers to younger
4536 * generations does not need to be scavenged.
4538 * The write-protection can be used to note pages that don't have
4539 * pointers to younger pages. But pages can be written without having
4540 * pointers to younger generations. After the pages are scavenged here
4541 * they can be scanned for pointers to younger generations and if
4542 * there are none the page can be write-protected.
4544 * One complication is when the newspace is the top temp. generation.
4546 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
4547 * that none were written, which they shouldn't be as they should have
4548 * no pointers to younger generations. This breaks down for weak
4549 * pointers as the objects contain a link to the next and are written
4550 * if a weak pointer is scavenged. Still it's a useful check. */
4552 scavenge_generation(int generation)
4559 /* Clear the write_protected_cleared flags on all pages. */
4560 for (i = 0; i < NUM_PAGES; i++)
4561 page_table[i].write_protected_cleared = 0;
4564 for (i = 0; i < last_free_page; i++) {
4565 if ((page_table[i].allocated == BOXED_PAGE)
4566 && (page_table[i].bytes_used != 0)
4567 && (page_table[i].gen == generation)) {
4570 /* This should be the start of a contiguous block. */
4571 gc_assert(page_table[i].first_object_offset == 0);
4573 /* We need to find the full extent of this contiguous
4574 * block in case objects span pages. */
4576 /* Now work forward until the end of this contiguous area
4577 * is found. A small area is preferred as there is a
4578 * better chance of its pages being write-protected. */
4579 for (last_page = i; ; last_page++)
4580 /* Check whether this is the last page in this contiguous
4582 if ((page_table[last_page].bytes_used < 4096)
4583 /* Or it is 4096 and is the last in the block */
4584 || (page_table[last_page+1].allocated != BOXED_PAGE)
4585 || (page_table[last_page+1].bytes_used == 0)
4586 || (page_table[last_page+1].gen != generation)
4587 || (page_table[last_page+1].first_object_offset == 0))
4590 /* Do a limited check for write_protected pages. If all pages
4591 * are write_protected then there is no need to scavenge. */
4594 for (j = i; j <= last_page; j++)
4595 if (page_table[j].write_protected == 0) {
4603 scavenge(page_address(i), (page_table[last_page].bytes_used
4604 + (last_page-i)*4096)/4);
4606 /* Now scan the pages and write protect those
4607 * that don't have pointers to younger
4609 if (enable_page_protection) {
4610 for (j = i; j <= last_page; j++) {
4611 num_wp += update_page_write_prot(j);
4620 if ((gencgc_verbose > 1) && (num_wp != 0)) {
4622 "/write protected %d pages within generation %d\n",
4623 num_wp, generation));
4627 /* Check that none of the write_protected pages in this generation
4628 * have been written to. */
4629 for (i = 0; i < NUM_PAGES; i++) {
4630 if ((page_table[i].allocation ! =FREE_PAGE)
4631 && (page_table[i].bytes_used != 0)
4632 && (page_table[i].gen == generation)
4633 && (page_table[i].write_protected_cleared != 0)) {
4634 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
4636 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
4637 page_table[i].bytes_used,
4638 page_table[i].first_object_offset,
4639 page_table[i].dont_move));
4640 lose("write to protected page %d in scavenge_generation()", i);
4647 /* Scavenge a newspace generation. As it is scavenged new objects may
4648 * be allocated to it; these will also need to be scavenged. This
4649 * repeats until there are no more objects unscavenged in the
4650 * newspace generation.
4652 * To help improve the efficiency, areas written are recorded by
4653 * gc_alloc() and only these scavenged. Sometimes a little more will be
4654 * scavenged, but this causes no harm. An easy check is done that the
4655 * scavenged bytes equals the number allocated in the previous
4658 * Write-protected pages are not scanned except if they are marked
4659 * dont_move in which case they may have been promoted and still have
4660 * pointers to the from space.
4662 * Write-protected pages could potentially be written by alloc however
4663 * to avoid having to handle re-scavenging of write-protected pages
4664 * gc_alloc() does not write to write-protected pages.
4666 * New areas of objects allocated are recorded alternatively in the two
4667 * new_areas arrays below. */
4668 static struct new_area new_areas_1[NUM_NEW_AREAS];
4669 static struct new_area new_areas_2[NUM_NEW_AREAS];
4671 /* Do one full scan of the new space generation. This is not enough to
4672 * complete the job as new objects may be added to the generation in
4673 * the process which are not scavenged. */
4675 scavenge_newspace_generation_one_scan(int generation)
4680 "/starting one full scan of newspace generation %d\n",
4683 for (i = 0; i < last_free_page; i++) {
4684 if ((page_table[i].allocated == BOXED_PAGE)
4685 && (page_table[i].bytes_used != 0)
4686 && (page_table[i].gen == generation)
4687 && ((page_table[i].write_protected == 0)
4688 /* (This may be redundant as write_protected is now
4689 * cleared before promotion.) */
4690 || (page_table[i].dont_move == 1))) {
4693 /* The scavenge will start at the first_object_offset of page i.
4695 * We need to find the full extent of this contiguous
4696 * block in case objects span pages.
4698 * Now work forward until the end of this contiguous area
4699 * is found. A small area is preferred as there is a
4700 * better chance of its pages being write-protected. */
4701 for (last_page = i; ;last_page++) {
4702 /* Check whether this is the last page in this
4703 * contiguous block */
4704 if ((page_table[last_page].bytes_used < 4096)
4705 /* Or it is 4096 and is the last in the block */
4706 || (page_table[last_page+1].allocated != BOXED_PAGE)
4707 || (page_table[last_page+1].bytes_used == 0)
4708 || (page_table[last_page+1].gen != generation)
4709 || (page_table[last_page+1].first_object_offset == 0))
4713 /* Do a limited check for write-protected pages. If all
4714 * pages are write-protected then no need to scavenge,
4715 * except if the pages are marked dont_move. */
4718 for (j = i; j <= last_page; j++)
4719 if ((page_table[j].write_protected == 0)
4720 || (page_table[j].dont_move != 0)) {
4728 /* Calculate the size. */
4730 size = (page_table[last_page].bytes_used
4731 - page_table[i].first_object_offset)/4;
4733 size = (page_table[last_page].bytes_used
4734 + (last_page-i)*4096
4735 - page_table[i].first_object_offset)/4;
4738 new_areas_ignore_page = last_page;
4740 scavenge(page_address(i) +
4741 page_table[i].first_object_offset,
4752 "/done with one full scan of newspace generation %d\n",
4756 /* Do a complete scavenge of the newspace generation. */
4758 scavenge_newspace_generation(int generation)
4762 /* the new_areas array currently being written to by gc_alloc() */
4763 struct new_area (*current_new_areas)[] = &new_areas_1;
4764 int current_new_areas_index;
4766 /* the new_areas created but the previous scavenge cycle */
4767 struct new_area (*previous_new_areas)[] = NULL;
4768 int previous_new_areas_index;
4770 /* Flush the current regions updating the tables. */
4771 gc_alloc_update_page_tables(0, &boxed_region);
4772 gc_alloc_update_page_tables(1, &unboxed_region);
4774 /* Turn on the recording of new areas by gc_alloc(). */
4775 new_areas = current_new_areas;
4776 new_areas_index = 0;
4778 /* Don't need to record new areas that get scavenged anyway during
4779 * scavenge_newspace_generation_one_scan. */
4780 record_new_objects = 1;
4782 /* Start with a full scavenge. */
4783 scavenge_newspace_generation_one_scan(generation);
4785 /* Record all new areas now. */
4786 record_new_objects = 2;
4788 /* Flush the current regions updating the tables. */
4789 gc_alloc_update_page_tables(0, &boxed_region);
4790 gc_alloc_update_page_tables(1, &unboxed_region);
4792 /* Grab new_areas_index. */
4793 current_new_areas_index = new_areas_index;
4796 "The first scan is finished; current_new_areas_index=%d.\n",
4797 current_new_areas_index));*/
4799 while (current_new_areas_index > 0) {
4800 /* Move the current to the previous new areas */
4801 previous_new_areas = current_new_areas;
4802 previous_new_areas_index = current_new_areas_index;
4804 /* Scavenge all the areas in previous new areas. Any new areas
4805 * allocated are saved in current_new_areas. */
4807 /* Allocate an array for current_new_areas; alternating between
4808 * new_areas_1 and 2 */
4809 if (previous_new_areas == &new_areas_1)
4810 current_new_areas = &new_areas_2;
4812 current_new_areas = &new_areas_1;
4814 /* Set up for gc_alloc(). */
4815 new_areas = current_new_areas;
4816 new_areas_index = 0;
4818 /* Check whether previous_new_areas had overflowed. */
4819 if (previous_new_areas_index >= NUM_NEW_AREAS) {
4821 /* New areas of objects allocated have been lost so need to do a
4822 * full scan to be sure! If this becomes a problem try
4823 * increasing NUM_NEW_AREAS. */
4825 SHOW("new_areas overflow, doing full scavenge");
4827 /* Don't need to record new areas that get scavenge anyway
4828 * during scavenge_newspace_generation_one_scan. */
4829 record_new_objects = 1;
4831 scavenge_newspace_generation_one_scan(generation);
4833 /* Record all new areas now. */
4834 record_new_objects = 2;
4836 /* Flush the current regions updating the tables. */
4837 gc_alloc_update_page_tables(0, &boxed_region);
4838 gc_alloc_update_page_tables(1, &unboxed_region);
4842 /* Work through previous_new_areas. */
4843 for (i = 0; i < previous_new_areas_index; i++) {
4844 /* FIXME: All these bare *4 and /4 should be something
4845 * like BYTES_PER_WORD or WBYTES. */
4846 int page = (*previous_new_areas)[i].page;
4847 int offset = (*previous_new_areas)[i].offset;
4848 int size = (*previous_new_areas)[i].size / 4;
4849 gc_assert((*previous_new_areas)[i].size % 4 == 0);
4851 scavenge(page_address(page)+offset, size);
4854 /* Flush the current regions updating the tables. */
4855 gc_alloc_update_page_tables(0, &boxed_region);
4856 gc_alloc_update_page_tables(1, &unboxed_region);
4859 current_new_areas_index = new_areas_index;
4862 "The re-scan has finished; current_new_areas_index=%d.\n",
4863 current_new_areas_index));*/
4866 /* Turn off recording of areas allocated by gc_alloc(). */
4867 record_new_objects = 0;
4870 /* Check that none of the write_protected pages in this generation
4871 * have been written to. */
4872 for (i = 0; i < NUM_PAGES; i++) {
4873 if ((page_table[i].allocation != FREE_PAGE)
4874 && (page_table[i].bytes_used != 0)
4875 && (page_table[i].gen == generation)
4876 && (page_table[i].write_protected_cleared != 0)
4877 && (page_table[i].dont_move == 0)) {
4878 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
4879 i, generation, page_table[i].dont_move);
4885 /* Un-write-protect all the pages in from_space. This is done at the
4886 * start of a GC else there may be many page faults while scavenging
4887 * the newspace (I've seen drive the system time to 99%). These pages
4888 * would need to be unprotected anyway before unmapping in
4889 * free_oldspace; not sure what effect this has on paging.. */
4891 unprotect_oldspace(void)
4895 for (i = 0; i < last_free_page; i++) {
4896 if ((page_table[i].allocated != FREE_PAGE)
4897 && (page_table[i].bytes_used != 0)
4898 && (page_table[i].gen == from_space)) {
4901 page_start = (void *)page_address(i);
4903 /* Remove any write-protection. We should be able to rely
4904 * on the write-protect flag to avoid redundant calls. */
4905 if (page_table[i].write_protected) {
4906 os_protect(page_start, 4096, OS_VM_PROT_ALL);
4907 page_table[i].write_protected = 0;
4913 /* Work through all the pages and free any in from_space. This
4914 * assumes that all objects have been copied or promoted to an older
4915 * generation. Bytes_allocated and the generation bytes_allocated
4916 * counter are updated. The number of bytes freed is returned. */
4917 extern void i586_bzero(void *addr, int nbytes);
4921 int bytes_freed = 0;
4922 int first_page, last_page;
4927 /* Find a first page for the next region of pages. */
4928 while ((first_page < last_free_page)
4929 && ((page_table[first_page].allocated == FREE_PAGE)
4930 || (page_table[first_page].bytes_used == 0)
4931 || (page_table[first_page].gen != from_space)))
4934 if (first_page >= last_free_page)
4937 /* Find the last page of this region. */
4938 last_page = first_page;
4941 /* Free the page. */
4942 bytes_freed += page_table[last_page].bytes_used;
4943 generations[page_table[last_page].gen].bytes_allocated -=
4944 page_table[last_page].bytes_used;
4945 page_table[last_page].allocated = FREE_PAGE;
4946 page_table[last_page].bytes_used = 0;
4948 /* Remove any write-protection. We should be able to rely
4949 * on the write-protect flag to avoid redundant calls. */
4951 void *page_start = (void *)page_address(last_page);
4953 if (page_table[last_page].write_protected) {
4954 os_protect(page_start, 4096, OS_VM_PROT_ALL);
4955 page_table[last_page].write_protected = 0;
4960 while ((last_page < last_free_page)
4961 && (page_table[last_page].allocated != FREE_PAGE)
4962 && (page_table[last_page].bytes_used != 0)
4963 && (page_table[last_page].gen == from_space));
4965 /* Zero pages from first_page to (last_page-1).
4967 * FIXME: Why not use os_zero(..) function instead of
4968 * hand-coding this again? (Check other gencgc_unmap_zero
4970 if (gencgc_unmap_zero) {
4971 void *page_start, *addr;
4973 page_start = (void *)page_address(first_page);
4975 os_invalidate(page_start, 4096*(last_page-first_page));
4976 addr = os_validate(page_start, 4096*(last_page-first_page));
4977 if (addr == NULL || addr != page_start) {
4978 /* Is this an error condition? I couldn't really tell from
4979 * the old CMU CL code, which fprintf'ed a message with
4980 * an exclamation point at the end. But I've never seen the
4981 * message, so it must at least be unusual..
4983 * (The same condition is also tested for in gc_free_heap.)
4985 * -- WHN 19991129 */
4986 lose("i586_bzero: page moved, 0x%08x ==> 0x%08x",
4993 page_start = (int *)page_address(first_page);
4994 i586_bzero(page_start, 4096*(last_page-first_page));
4997 first_page = last_page;
4999 } while (first_page < last_free_page);
5001 bytes_allocated -= bytes_freed;
5006 /* Print some information about a pointer at the given address. */
5008 print_ptr(lispobj *addr)
5010 /* If addr is in the dynamic space then out the page information. */
5011 int pi1 = find_page_index((void*)addr);
5014 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
5015 (unsigned int) addr,
5017 page_table[pi1].allocated,
5018 page_table[pi1].gen,
5019 page_table[pi1].bytes_used,
5020 page_table[pi1].first_object_offset,
5021 page_table[pi1].dont_move);
5022 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
5035 extern int undefined_tramp;
5038 verify_space(lispobj *start, size_t words)
5040 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
5041 int is_in_readonly_space =
5042 (READ_ONLY_SPACE_START <= (unsigned)start &&
5043 (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5047 lispobj thing = *(lispobj*)start;
5049 if (is_lisp_pointer(thing)) {
5050 int page_index = find_page_index((void*)thing);
5051 int to_readonly_space =
5052 (READ_ONLY_SPACE_START <= thing &&
5053 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
5054 int to_static_space =
5055 (STATIC_SPACE_START <= thing &&
5056 thing < SymbolValue(STATIC_SPACE_FREE_POINTER));
5058 /* Does it point to the dynamic space? */
5059 if (page_index != -1) {
5060 /* If it's within the dynamic space it should point to a used
5061 * page. XX Could check the offset too. */
5062 if ((page_table[page_index].allocated != FREE_PAGE)
5063 && (page_table[page_index].bytes_used == 0))
5064 lose ("Ptr %x @ %x sees free page.", thing, start);
5065 /* Check that it doesn't point to a forwarding pointer! */
5066 if (*((lispobj *)native_pointer(thing)) == 0x01) {
5067 lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
5069 /* Check that its not in the RO space as it would then be a
5070 * pointer from the RO to the dynamic space. */
5071 if (is_in_readonly_space) {
5072 lose("ptr to dynamic space %x from RO space %x",
5075 /* Does it point to a plausible object? This check slows
5076 * it down a lot (so it's commented out).
5078 * FIXME: Add a variable to enable this dynamically. */
5079 /* if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
5080 * lose("ptr %x to invalid object %x", thing, start); */
5082 /* Verify that it points to another valid space. */
5083 if (!to_readonly_space && !to_static_space
5084 && (thing != (unsigned)&undefined_tramp)) {
5085 lose("Ptr %x @ %x sees junk.", thing, start);
5089 if (thing & 0x3) { /* Skip fixnums. FIXME: There should be an
5090 * is_fixnum for this. */
5092 switch(widetag_of(*start)) {
5095 case SIMPLE_VECTOR_WIDETAG:
5097 case COMPLEX_WIDETAG:
5098 case SIMPLE_ARRAY_WIDETAG:
5099 case COMPLEX_STRING_WIDETAG:
5100 case COMPLEX_BIT_VECTOR_WIDETAG:
5101 case COMPLEX_VECTOR_WIDETAG:
5102 case COMPLEX_ARRAY_WIDETAG:
5103 case CLOSURE_HEADER_WIDETAG:
5104 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
5105 case VALUE_CELL_HEADER_WIDETAG:
5106 case SYMBOL_HEADER_WIDETAG:
5107 case BASE_CHAR_WIDETAG:
5108 case UNBOUND_MARKER_WIDETAG:
5109 case INSTANCE_HEADER_WIDETAG:
5114 case CODE_HEADER_WIDETAG:
5116 lispobj object = *start;
5118 int nheader_words, ncode_words, nwords;
5120 struct simple_fun *fheaderp;
5122 code = (struct code *) start;
5124 /* Check that it's not in the dynamic space.
5125 * FIXME: Isn't is supposed to be OK for code
5126 * objects to be in the dynamic space these days? */
5127 if (is_in_dynamic_space
5128 /* It's ok if it's byte compiled code. The trace
5129 * table offset will be a fixnum if it's x86
5130 * compiled code - check.
5132 * FIXME: #^#@@! lack of abstraction here..
5133 * This line can probably go away now that
5134 * there's no byte compiler, but I've got
5135 * too much to worry about right now to try
5136 * to make sure. -- WHN 2001-10-06 */
5137 && !(code->trace_table_offset & 0x3)
5138 /* Only when enabled */
5139 && verify_dynamic_code_check) {
5141 "/code object at %x in the dynamic space\n",
5145 ncode_words = fixnum_value(code->code_size);
5146 nheader_words = HeaderValue(object);
5147 nwords = ncode_words + nheader_words;
5148 nwords = CEILING(nwords, 2);
5149 /* Scavenge the boxed section of the code data block */
5150 verify_space(start + 1, nheader_words - 1);
5152 /* Scavenge the boxed section of each function
5153 * object in the code data block. */
5154 fheaderl = code->entry_points;
5155 while (fheaderl != NIL) {
5157 (struct simple_fun *) native_pointer(fheaderl);
5158 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
5159 verify_space(&fheaderp->name, 1);
5160 verify_space(&fheaderp->arglist, 1);
5161 verify_space(&fheaderp->type, 1);
5162 fheaderl = fheaderp->next;
5168 /* unboxed objects */
5169 case BIGNUM_WIDETAG:
5170 case SINGLE_FLOAT_WIDETAG:
5171 case DOUBLE_FLOAT_WIDETAG:
5172 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
5173 case LONG_FLOAT_WIDETAG:
5175 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
5176 case COMPLEX_SINGLE_FLOAT_WIDETAG:
5178 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
5179 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
5181 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
5182 case COMPLEX_LONG_FLOAT_WIDETAG:
5184 case SIMPLE_STRING_WIDETAG:
5185 case SIMPLE_BIT_VECTOR_WIDETAG:
5186 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
5187 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
5188 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
5189 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
5190 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
5191 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
5192 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
5194 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
5195 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
5197 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
5198 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
5200 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
5201 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
5203 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
5204 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
5205 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
5206 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
5208 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
5209 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
5211 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
5212 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
5214 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
5215 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
5218 case WEAK_POINTER_WIDETAG:
5219 count = (sizetab[widetag_of(*start)])(start);
5235 /* FIXME: It would be nice to make names consistent so that
5236 * foo_size meant size *in* *bytes* instead of size in some
5237 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
5238 * Some counts of lispobjs are called foo_count; it might be good
5239 * to grep for all foo_size and rename the appropriate ones to
5241 int read_only_space_size =
5242 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER)
5243 - (lispobj*)READ_ONLY_SPACE_START;
5244 int static_space_size =
5245 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER)
5246 - (lispobj*)STATIC_SPACE_START;
5247 int binding_stack_size =
5248 (lispobj*)SymbolValue(BINDING_STACK_POINTER)
5249 - (lispobj*)BINDING_STACK_START;
5251 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
5252 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
5253 verify_space((lispobj*)BINDING_STACK_START , binding_stack_size);
5257 verify_generation(int generation)
5261 for (i = 0; i < last_free_page; i++) {
5262 if ((page_table[i].allocated != FREE_PAGE)
5263 && (page_table[i].bytes_used != 0)
5264 && (page_table[i].gen == generation)) {
5266 int region_allocation = page_table[i].allocated;
5268 /* This should be the start of a contiguous block */
5269 gc_assert(page_table[i].first_object_offset == 0);
5271 /* Need to find the full extent of this contiguous block in case
5272 objects span pages. */
5274 /* Now work forward until the end of this contiguous area is
5276 for (last_page = i; ;last_page++)
5277 /* Check whether this is the last page in this contiguous
5279 if ((page_table[last_page].bytes_used < 4096)
5280 /* Or it is 4096 and is the last in the block */
5281 || (page_table[last_page+1].allocated != region_allocation)
5282 || (page_table[last_page+1].bytes_used == 0)
5283 || (page_table[last_page+1].gen != generation)
5284 || (page_table[last_page+1].first_object_offset == 0))
5287 verify_space(page_address(i), (page_table[last_page].bytes_used
5288 + (last_page-i)*4096)/4);
5294 /* Check that all the free space is zero filled. */
5296 verify_zero_fill(void)
5300 for (page = 0; page < last_free_page; page++) {
5301 if (page_table[page].allocated == FREE_PAGE) {
5302 /* The whole page should be zero filled. */
5303 int *start_addr = (int *)page_address(page);
5306 for (i = 0; i < size; i++) {
5307 if (start_addr[i] != 0) {
5308 lose("free page not zero at %x", start_addr + i);
5312 int free_bytes = 4096 - page_table[page].bytes_used;
5313 if (free_bytes > 0) {
5314 int *start_addr = (int *)((unsigned)page_address(page)
5315 + page_table[page].bytes_used);
5316 int size = free_bytes / 4;
5318 for (i = 0; i < size; i++) {
5319 if (start_addr[i] != 0) {
5320 lose("free region not zero at %x", start_addr + i);
5328 /* External entry point for verify_zero_fill */
5330 gencgc_verify_zero_fill(void)
5332 /* Flush the alloc regions updating the tables. */
5333 boxed_region.free_pointer = current_region_free_pointer;
5334 gc_alloc_update_page_tables(0, &boxed_region);
5335 gc_alloc_update_page_tables(1, &unboxed_region);
5336 SHOW("verifying zero fill");
5338 current_region_free_pointer = boxed_region.free_pointer;
5339 current_region_end_addr = boxed_region.end_addr;
5343 verify_dynamic_space(void)
5347 for (i = 0; i < NUM_GENERATIONS; i++)
5348 verify_generation(i);
5350 if (gencgc_enable_verify_zero_fill)
5354 /* Write-protect all the dynamic boxed pages in the given generation. */
5356 write_protect_generation_pages(int generation)
5360 gc_assert(generation < NUM_GENERATIONS);
5362 for (i = 0; i < last_free_page; i++)
5363 if ((page_table[i].allocated == BOXED_PAGE)
5364 && (page_table[i].bytes_used != 0)
5365 && (page_table[i].gen == generation)) {
5368 page_start = (void *)page_address(i);
5370 os_protect(page_start,
5372 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
5374 /* Note the page as protected in the page tables. */
5375 page_table[i].write_protected = 1;
5378 if (gencgc_verbose > 1) {
5380 "/write protected %d of %d pages in generation %d\n",
5381 count_write_protect_generation_pages(generation),
5382 count_generation_pages(generation),
5387 /* Garbage collect a generation. If raise is 0 then the remains of the
5388 * generation are not raised to the next generation. */
5390 garbage_collect_generation(int generation, int raise)
5392 unsigned long bytes_freed;
5394 unsigned long static_space_size;
5396 gc_assert(generation <= (NUM_GENERATIONS-1));
5398 /* The oldest generation can't be raised. */
5399 gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
5401 /* Initialize the weak pointer list. */
5402 weak_pointers = NULL;
5404 /* When a generation is not being raised it is transported to a
5405 * temporary generation (NUM_GENERATIONS), and lowered when
5406 * done. Set up this new generation. There should be no pages
5407 * allocated to it yet. */
5409 gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
5411 /* Set the global src and dest. generations */
5412 from_space = generation;
5414 new_space = generation+1;
5416 new_space = NUM_GENERATIONS;
5418 /* Change to a new space for allocation, resetting the alloc_start_page */
5419 gc_alloc_generation = new_space;
5420 generations[new_space].alloc_start_page = 0;
5421 generations[new_space].alloc_unboxed_start_page = 0;
5422 generations[new_space].alloc_large_start_page = 0;
5423 generations[new_space].alloc_large_unboxed_start_page = 0;
5425 /* Before any pointers are preserved, the dont_move flags on the
5426 * pages need to be cleared. */
5427 for (i = 0; i < last_free_page; i++)
5428 page_table[i].dont_move = 0;
5430 /* Un-write-protect the old-space pages. This is essential for the
5431 * promoted pages as they may contain pointers into the old-space
5432 * which need to be scavenged. It also helps avoid unnecessary page
5433 * faults as forwarding pointers are written into them. They need to
5434 * be un-protected anyway before unmapping later. */
5435 unprotect_oldspace();
5437 /* Scavenge the stack's conservative roots. */
5440 for (ptr = (void **)CONTROL_STACK_END - 1;
5441 ptr > (void **)&raise;
5443 preserve_pointer(*ptr);
5448 if (gencgc_verbose > 1) {
5449 int num_dont_move_pages = count_dont_move_pages();
5451 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
5452 num_dont_move_pages,
5453 /* FIXME: 4096 should be symbolic constant here and
5454 * prob'ly elsewhere too. */
5455 num_dont_move_pages * 4096);
5459 /* Scavenge all the rest of the roots. */
5461 /* Scavenge the Lisp functions of the interrupt handlers, taking
5462 * care to avoid SIG_DFL and SIG_IGN. */
5463 for (i = 0; i < NSIG; i++) {
5464 union interrupt_handler handler = interrupt_handlers[i];
5465 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
5466 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
5467 scavenge((lispobj *)(interrupt_handlers + i), 1);
5471 /* Scavenge the binding stack. */
5472 scavenge((lispobj *) BINDING_STACK_START,
5473 (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
5474 (lispobj *)BINDING_STACK_START);
5476 /* The original CMU CL code had scavenge-read-only-space code
5477 * controlled by the Lisp-level variable
5478 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
5479 * wasn't documented under what circumstances it was useful or
5480 * safe to turn it on, so it's been turned off in SBCL. If you
5481 * want/need this functionality, and can test and document it,
5482 * please submit a patch. */
5484 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
5485 unsigned long read_only_space_size =
5486 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
5487 (lispobj*)READ_ONLY_SPACE_START;
5489 "/scavenge read only space: %d bytes\n",
5490 read_only_space_size * sizeof(lispobj)));
5491 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
5495 /* Scavenge static space. */
5497 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER) -
5498 (lispobj *)STATIC_SPACE_START;
5499 if (gencgc_verbose > 1) {
5501 "/scavenge static space: %d bytes\n",
5502 static_space_size * sizeof(lispobj)));
5504 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
5506 /* All generations but the generation being GCed need to be
5507 * scavenged. The new_space generation needs special handling as
5508 * objects may be moved in - it is handled separately below. */
5509 for (i = 0; i < NUM_GENERATIONS; i++) {
5510 if ((i != generation) && (i != new_space)) {
5511 scavenge_generation(i);
5515 /* Finally scavenge the new_space generation. Keep going until no
5516 * more objects are moved into the new generation */
5517 scavenge_newspace_generation(new_space);
5519 /* FIXME: I tried reenabling this check when debugging unrelated
5520 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
5521 * Since the current GC code seems to work well, I'm guessing that
5522 * this debugging code is just stale, but I haven't tried to
5523 * figure it out. It should be figured out and then either made to
5524 * work or just deleted. */
5525 #define RESCAN_CHECK 0
5527 /* As a check re-scavenge the newspace once; no new objects should
5530 int old_bytes_allocated = bytes_allocated;
5531 int bytes_allocated;
5533 /* Start with a full scavenge. */
5534 scavenge_newspace_generation_one_scan(new_space);
5536 /* Flush the current regions, updating the tables. */
5537 gc_alloc_update_page_tables(0, &boxed_region);
5538 gc_alloc_update_page_tables(1, &unboxed_region);
5540 bytes_allocated = bytes_allocated - old_bytes_allocated;
5542 if (bytes_allocated != 0) {
5543 lose("Rescan of new_space allocated %d more bytes.",
5549 scan_weak_pointers();
5551 /* Flush the current regions, updating the tables. */
5552 gc_alloc_update_page_tables(0, &boxed_region);
5553 gc_alloc_update_page_tables(1, &unboxed_region);
5555 /* Free the pages in oldspace, but not those marked dont_move. */
5556 bytes_freed = free_oldspace();
5558 /* If the GC is not raising the age then lower the generation back
5559 * to its normal generation number */
5561 for (i = 0; i < last_free_page; i++)
5562 if ((page_table[i].bytes_used != 0)
5563 && (page_table[i].gen == NUM_GENERATIONS))
5564 page_table[i].gen = generation;
5565 gc_assert(generations[generation].bytes_allocated == 0);
5566 generations[generation].bytes_allocated =
5567 generations[NUM_GENERATIONS].bytes_allocated;
5568 generations[NUM_GENERATIONS].bytes_allocated = 0;
5571 /* Reset the alloc_start_page for generation. */
5572 generations[generation].alloc_start_page = 0;
5573 generations[generation].alloc_unboxed_start_page = 0;
5574 generations[generation].alloc_large_start_page = 0;
5575 generations[generation].alloc_large_unboxed_start_page = 0;
5577 if (generation >= verify_gens) {
5581 verify_dynamic_space();
5584 /* Set the new gc trigger for the GCed generation. */
5585 generations[generation].gc_trigger =
5586 generations[generation].bytes_allocated
5587 + generations[generation].bytes_consed_between_gc;
5590 generations[generation].num_gc = 0;
5592 ++generations[generation].num_gc;
5595 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
5597 update_x86_dynamic_space_free_pointer(void)
5602 for (i = 0; i < NUM_PAGES; i++)
5603 if ((page_table[i].allocated != FREE_PAGE)
5604 && (page_table[i].bytes_used != 0))
5607 last_free_page = last_page+1;
5609 SetSymbolValue(ALLOCATION_POINTER,
5610 (lispobj)(((char *)heap_base) + last_free_page*4096));
5611 return 0; /* dummy value: return something ... */
5614 /* GC all generations below last_gen, raising their objects to the
5615 * next generation until all generations below last_gen are empty.
5616 * Then if last_gen is due for a GC then GC it. In the special case
5617 * that last_gen==NUM_GENERATIONS, the last generation is always
5618 * GC'ed. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
5620 * The oldest generation to be GCed will always be
5621 * gencgc_oldest_gen_to_gc, partly ignoring last_gen if necessary. */
5623 collect_garbage(unsigned last_gen)
5630 boxed_region.free_pointer = current_region_free_pointer;
5632 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
5634 if (last_gen > NUM_GENERATIONS) {
5636 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
5641 /* Flush the alloc regions updating the tables. */
5642 gc_alloc_update_page_tables(0, &boxed_region);
5643 gc_alloc_update_page_tables(1, &unboxed_region);
5645 /* Verify the new objects created by Lisp code. */
5646 if (pre_verify_gen_0) {
5647 SHOW((stderr, "pre-checking generation 0\n"));
5648 verify_generation(0);
5651 if (gencgc_verbose > 1)
5652 print_generation_stats(0);
5655 /* Collect the generation. */
5657 if (gen >= gencgc_oldest_gen_to_gc) {
5658 /* Never raise the oldest generation. */
5663 || (generations[gen].num_gc >= generations[gen].trigger_age);
5666 if (gencgc_verbose > 1) {
5668 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
5671 generations[gen].bytes_allocated,
5672 generations[gen].gc_trigger,
5673 generations[gen].num_gc));
5676 /* If an older generation is being filled, then update its
5679 generations[gen+1].cum_sum_bytes_allocated +=
5680 generations[gen+1].bytes_allocated;
5683 garbage_collect_generation(gen, raise);
5685 /* Reset the memory age cum_sum. */
5686 generations[gen].cum_sum_bytes_allocated = 0;
5688 if (gencgc_verbose > 1) {
5689 FSHOW((stderr, "GC of generation %d finished:\n", gen));
5690 print_generation_stats(0);
5694 } while ((gen <= gencgc_oldest_gen_to_gc)
5695 && ((gen < last_gen)
5696 || ((gen <= gencgc_oldest_gen_to_gc)
5698 && (generations[gen].bytes_allocated
5699 > generations[gen].gc_trigger)
5700 && (gen_av_mem_age(gen)
5701 > generations[gen].min_av_mem_age))));
5703 /* Now if gen-1 was raised all generations before gen are empty.
5704 * If it wasn't raised then all generations before gen-1 are empty.
5706 * Now objects within this gen's pages cannot point to younger
5707 * generations unless they are written to. This can be exploited
5708 * by write-protecting the pages of gen; then when younger
5709 * generations are GCed only the pages which have been written
5714 gen_to_wp = gen - 1;
5716 /* There's not much point in WPing pages in generation 0 as it is
5717 * never scavenged (except promoted pages). */
5718 if ((gen_to_wp > 0) && enable_page_protection) {
5719 /* Check that they are all empty. */
5720 for (i = 0; i < gen_to_wp; i++) {
5721 if (generations[i].bytes_allocated)
5722 lose("trying to write-protect gen. %d when gen. %d nonempty",
5725 write_protect_generation_pages(gen_to_wp);
5728 /* Set gc_alloc() back to generation 0. The current regions should
5729 * be flushed after the above GCs. */
5730 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
5731 gc_alloc_generation = 0;
5733 update_x86_dynamic_space_free_pointer();
5735 /* This is now done by Lisp SCRUB-CONTROL-STACK in Lisp SUB-GC, so
5736 * we needn't do it here: */
5739 current_region_free_pointer = boxed_region.free_pointer;
5740 current_region_end_addr = boxed_region.end_addr;
5742 SHOW("returning from collect_garbage");
5745 /* This is called by Lisp PURIFY when it is finished. All live objects
5746 * will have been moved to the RO and Static heaps. The dynamic space
5747 * will need a full re-initialization. We don't bother having Lisp
5748 * PURIFY flush the current gc_alloc() region, as the page_tables are
5749 * re-initialized, and every page is zeroed to be sure. */
5755 if (gencgc_verbose > 1)
5756 SHOW("entering gc_free_heap");
5758 for (page = 0; page < NUM_PAGES; page++) {
5759 /* Skip free pages which should already be zero filled. */
5760 if (page_table[page].allocated != FREE_PAGE) {
5761 void *page_start, *addr;
5763 /* Mark the page free. The other slots are assumed invalid
5764 * when it is a FREE_PAGE and bytes_used is 0 and it
5765 * should not be write-protected -- except that the
5766 * generation is used for the current region but it sets
5768 page_table[page].allocated = FREE_PAGE;
5769 page_table[page].bytes_used = 0;
5771 /* Zero the page. */
5772 page_start = (void *)page_address(page);
5774 /* First, remove any write-protection. */
5775 os_protect(page_start, 4096, OS_VM_PROT_ALL);
5776 page_table[page].write_protected = 0;
5778 os_invalidate(page_start,4096);
5779 addr = os_validate(page_start,4096);
5780 if (addr == NULL || addr != page_start) {
5781 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
5785 } else if (gencgc_zero_check_during_free_heap) {
5786 /* Double-check that the page is zero filled. */
5788 gc_assert(page_table[page].allocated == FREE_PAGE);
5789 gc_assert(page_table[page].bytes_used == 0);
5790 page_start = (int *)page_address(page);
5791 for (i=0; i<1024; i++) {
5792 if (page_start[i] != 0) {
5793 lose("free region not zero at %x", page_start + i);
5799 bytes_allocated = 0;
5801 /* Initialize the generations. */
5802 for (page = 0; page < NUM_GENERATIONS; page++) {
5803 generations[page].alloc_start_page = 0;
5804 generations[page].alloc_unboxed_start_page = 0;
5805 generations[page].alloc_large_start_page = 0;
5806 generations[page].alloc_large_unboxed_start_page = 0;
5807 generations[page].bytes_allocated = 0;
5808 generations[page].gc_trigger = 2000000;
5809 generations[page].num_gc = 0;
5810 generations[page].cum_sum_bytes_allocated = 0;
5813 if (gencgc_verbose > 1)
5814 print_generation_stats(0);
5816 /* Initialize gc_alloc(). */
5817 gc_alloc_generation = 0;
5818 boxed_region.first_page = 0;
5819 boxed_region.last_page = -1;
5820 boxed_region.start_addr = page_address(0);
5821 boxed_region.free_pointer = page_address(0);
5822 boxed_region.end_addr = page_address(0);
5823 unboxed_region.first_page = 0;
5824 unboxed_region.last_page = -1;
5825 unboxed_region.start_addr = page_address(0);
5826 unboxed_region.free_pointer = page_address(0);
5827 unboxed_region.end_addr = page_address(0);
5829 #if 0 /* Lisp PURIFY is currently running on the C stack so don't do this. */
5834 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base));
5836 current_region_free_pointer = boxed_region.free_pointer;
5837 current_region_end_addr = boxed_region.end_addr;
5839 if (verify_after_free_heap) {
5840 /* Check whether purify has left any bad pointers. */
5842 SHOW("checking after free_heap\n");
5854 heap_base = (void*)DYNAMIC_SPACE_START;
5856 /* Initialize each page structure. */
5857 for (i = 0; i < NUM_PAGES; i++) {
5858 /* Initialize all pages as free. */
5859 page_table[i].allocated = FREE_PAGE;
5860 page_table[i].bytes_used = 0;
5862 /* Pages are not write-protected at startup. */
5863 page_table[i].write_protected = 0;
5866 bytes_allocated = 0;
5868 /* Initialize the generations.
5870 * FIXME: very similar to code in gc_free_heap(), should be shared */
5871 for (i = 0; i < NUM_GENERATIONS; i++) {
5872 generations[i].alloc_start_page = 0;
5873 generations[i].alloc_unboxed_start_page = 0;
5874 generations[i].alloc_large_start_page = 0;
5875 generations[i].alloc_large_unboxed_start_page = 0;
5876 generations[i].bytes_allocated = 0;
5877 generations[i].gc_trigger = 2000000;
5878 generations[i].num_gc = 0;
5879 generations[i].cum_sum_bytes_allocated = 0;
5880 /* the tune-able parameters */
5881 generations[i].bytes_consed_between_gc = 2000000;
5882 generations[i].trigger_age = 1;
5883 generations[i].min_av_mem_age = 0.75;
5886 /* Initialize gc_alloc.
5888 * FIXME: identical with code in gc_free_heap(), should be shared */
5889 gc_alloc_generation = 0;
5890 boxed_region.first_page = 0;
5891 boxed_region.last_page = -1;
5892 boxed_region.start_addr = page_address(0);
5893 boxed_region.free_pointer = page_address(0);
5894 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 gc_alloc(..). */
6089 do_pending_interrupt();
6098 * noise to manipulate the gc trigger stuff
6102 set_auto_gc_trigger(os_vm_size_t dynamic_usage)
6104 auto_gc_trigger += dynamic_usage;
6108 clear_auto_gc_trigger(void)
6110 auto_gc_trigger = 0;
6113 /* Find the code object for the given pc, or return NULL on failure.
6115 * FIXME: PC shouldn't be lispobj*, should it? Maybe void*? */
6117 component_ptr_from_pc(lispobj *pc)
6119 lispobj *object = NULL;
6121 if ( (object = search_read_only_space(pc)) )
6123 else if ( (object = search_static_space(pc)) )
6126 object = search_dynamic_space(pc);
6128 if (object) /* if we found something */
6129 if (widetag_of(*object) == CODE_HEADER_WIDETAG) /* if it's a code object */
6136 * shared support for the OS-dependent signal handlers which
6137 * catch GENCGC-related write-protect violations
6140 void unhandled_sigmemoryfault(void);
6142 /* Depending on which OS we're running under, different signals might
6143 * be raised for a violation of write protection in the heap. This
6144 * function factors out the common generational GC magic which needs
6145 * to invoked in this case, and should be called from whatever signal
6146 * handler is appropriate for the OS we're running under.
6148 * Return true if this signal is a normal generational GC thing that
6149 * we were able to handle, or false if it was abnormal and control
6150 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
6152 gencgc_handle_wp_violation(void* fault_addr)
6154 int page_index = find_page_index(fault_addr);
6156 #if defined QSHOW_SIGNALS
6157 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
6158 fault_addr, page_index));
6161 /* Check whether the fault is within the dynamic space. */
6162 if (page_index == (-1)) {
6164 /* It can be helpful to be able to put a breakpoint on this
6165 * case to help diagnose low-level problems. */
6166 unhandled_sigmemoryfault();
6168 /* not within the dynamic space -- not our responsibility */
6173 /* The only acceptable reason for an signal like this from the
6174 * heap is that the generational GC write-protected the page. */
6175 if (page_table[page_index].write_protected != 1) {
6176 lose("access failure in heap page not marked as write-protected");
6179 /* Unprotect the page. */
6180 os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
6181 page_table[page_index].write_protected = 0;
6182 page_table[page_index].write_protected_cleared = 1;
6184 /* Don't worry, we can handle it. */
6189 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
6190 * it's not just a case of the program hitting the write barrier, and
6191 * are about to let Lisp deal with it. It's basically just a
6192 * convenient place to set a gdb breakpoint. */
6194 unhandled_sigmemoryfault()