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>.
36 #include "interrupt.h"
41 #include "gc-internal.h"
43 #include "genesis/vector.h"
44 #include "genesis/weak-pointer.h"
45 #include "genesis/simple-fun.h"
47 /* assembly language stub that executes trap_PendingInterrupt */
48 void do_pending_interrupt(void);
50 /* forward declarations */
51 int gc_find_freeish_pages(int *restart_page_ptr, int nbytes, int unboxed);
52 void gc_set_region_empty(struct alloc_region *region);
53 void gc_alloc_update_all_page_tables(void);
54 static void gencgc_pickup_dynamic(void);
55 boolean interrupt_maybe_gc_int(int, siginfo_t *, void *);
62 /* the number of actual generations. (The number of 'struct
63 * generation' objects is one more than this, because one object
64 * serves as scratch when GC'ing.) */
65 #define NUM_GENERATIONS 6
67 /* Should we use page protection to help avoid the scavenging of pages
68 * that don't have pointers to younger generations? */
69 boolean enable_page_protection = 1;
71 /* Should we unmap a page and re-mmap it to have it zero filled? */
72 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
73 /* comment from cmucl-2.4.8: This can waste a lot of swap on FreeBSD
74 * so don't unmap there.
76 * The CMU CL comment didn't specify a version, but was probably an
77 * old version of FreeBSD (pre-4.0), so this might no longer be true.
78 * OTOH, if it is true, this behavior might exist on OpenBSD too, so
79 * for now we don't unmap there either. -- WHN 2001-04-07 */
80 boolean gencgc_unmap_zero = 0;
82 boolean gencgc_unmap_zero = 1;
85 /* the minimum size (in bytes) for a large object*/
86 unsigned large_object_size = 4 * PAGE_BYTES;
95 /* the verbosity level. All non-error messages are disabled at level 0;
96 * and only a few rare messages are printed at level 1. */
98 unsigned gencgc_verbose = 1;
100 unsigned gencgc_verbose = 0;
103 /* FIXME: At some point enable the various error-checking things below
104 * and see what they say. */
106 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
107 * Set verify_gens to NUM_GENERATIONS to disable this kind of check. */
108 int verify_gens = NUM_GENERATIONS;
110 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
111 boolean pre_verify_gen_0 = 0;
113 /* Should we check for bad pointers after gc_free_heap is called
114 * from Lisp PURIFY? */
115 boolean verify_after_free_heap = 0;
117 /* Should we print a note when code objects are found in the dynamic space
118 * during a heap verify? */
119 boolean verify_dynamic_code_check = 0;
121 /* Should we check code objects for fixup errors after they are transported? */
122 boolean check_code_fixups = 0;
124 /* Should we check that newly allocated regions are zero filled? */
125 boolean gencgc_zero_check = 0;
127 /* Should we check that the free space is zero filled? */
128 boolean gencgc_enable_verify_zero_fill = 0;
130 /* Should we check that free pages are zero filled during gc_free_heap
131 * called after Lisp PURIFY? */
132 boolean gencgc_zero_check_during_free_heap = 0;
135 * GC structures and variables
138 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
139 unsigned long bytes_allocated = 0;
140 extern unsigned long bytes_consed_between_gcs; /* gc-common.c */
141 unsigned long auto_gc_trigger = 0;
143 /* the source and destination generations. These are set before a GC starts
149 /* An array of page structures is statically allocated.
150 * This helps quickly map between an address its page structure.
151 * NUM_PAGES is set from the size of the dynamic space. */
152 struct page page_table[NUM_PAGES];
154 /* To map addresses to page structures the address of the first page
156 static void *heap_base = NULL;
159 /* Calculate the start address for the given page number. */
161 page_address(int page_num)
163 return (heap_base + (page_num * PAGE_BYTES));
166 /* Find the page index within the page_table for the given
167 * address. Return -1 on failure. */
169 find_page_index(void *addr)
171 int index = addr-heap_base;
174 index = ((unsigned int)index)/PAGE_BYTES;
175 if (index < NUM_PAGES)
182 /* a structure to hold the state of a generation */
185 /* the first page that gc_alloc() checks on its next call */
186 int alloc_start_page;
188 /* the first page that gc_alloc_unboxed() checks on its next call */
189 int alloc_unboxed_start_page;
191 /* the first page that gc_alloc_large (boxed) considers on its next
192 * call. (Although it always allocates after the boxed_region.) */
193 int alloc_large_start_page;
195 /* the first page that gc_alloc_large (unboxed) considers on its
196 * next call. (Although it always allocates after the
197 * current_unboxed_region.) */
198 int alloc_large_unboxed_start_page;
200 /* the bytes allocated to this generation */
203 /* the number of bytes at which to trigger a GC */
206 /* to calculate a new level for gc_trigger */
207 int bytes_consed_between_gc;
209 /* the number of GCs since the last raise */
212 /* the average age after which a GC will raise objects to the
216 /* the cumulative sum of the bytes allocated to this generation. It is
217 * cleared after a GC on this generations, and update before new
218 * objects are added from a GC of a younger generation. Dividing by
219 * the bytes_allocated will give the average age of the memory in
220 * this generation since its last GC. */
221 int cum_sum_bytes_allocated;
223 /* a minimum average memory age before a GC will occur helps
224 * prevent a GC when a large number of new live objects have been
225 * added, in which case a GC could be a waste of time */
226 double min_av_mem_age;
228 /* the number of actual generations. (The number of 'struct
229 * generation' objects is one more than this, because one object
230 * serves as scratch when GC'ing.) */
231 #define NUM_GENERATIONS 6
233 /* an array of generation structures. There needs to be one more
234 * generation structure than actual generations as the oldest
235 * generation is temporarily raised then lowered. */
236 struct generation generations[NUM_GENERATIONS+1];
238 /* the oldest generation that is will currently be GCed by default.
239 * Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
241 * The default of (NUM_GENERATIONS-1) enables GC on all generations.
243 * Setting this to 0 effectively disables the generational nature of
244 * the GC. In some applications generational GC may not be useful
245 * because there are no long-lived objects.
247 * An intermediate value could be handy after moving long-lived data
248 * into an older generation so an unnecessary GC of this long-lived
249 * data can be avoided. */
250 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
252 /* The maximum free page in the heap is maintained and used to update
253 * ALLOCATION_POINTER which is used by the room function to limit its
254 * search of the heap. XX Gencgc obviously needs to be better
255 * integrated with the Lisp code. */
256 static int last_free_page;
258 /* This lock is to prevent multiple threads from simultaneously
259 * allocating new regions which overlap each other. Note that the
260 * majority of GC is single-threaded, but alloc() may be called from
261 * >1 thread at a time and must be thread-safe. This lock must be
262 * seized before all accesses to generations[] or to parts of
263 * page_table[] that other threads may want to see */
265 static lispobj free_pages_lock=0;
269 * miscellaneous heap functions
272 /* Count the number of pages which are write-protected within the
273 * given generation. */
275 count_write_protect_generation_pages(int generation)
280 for (i = 0; i < last_free_page; i++)
281 if ((page_table[i].allocated != FREE_PAGE_FLAG)
282 && (page_table[i].gen == generation)
283 && (page_table[i].write_protected == 1))
288 /* Count the number of pages within the given generation. */
290 count_generation_pages(int generation)
295 for (i = 0; i < last_free_page; i++)
296 if ((page_table[i].allocated != 0)
297 && (page_table[i].gen == generation))
304 count_dont_move_pages(void)
308 for (i = 0; i < last_free_page; i++) {
309 if ((page_table[i].allocated != 0) && (page_table[i].dont_move != 0)) {
317 /* Work through the pages and add up the number of bytes used for the
318 * given generation. */
320 count_generation_bytes_allocated (int gen)
324 for (i = 0; i < last_free_page; i++) {
325 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
326 result += page_table[i].bytes_used;
331 /* Return the average age of the memory in a generation. */
333 gen_av_mem_age(int gen)
335 if (generations[gen].bytes_allocated == 0)
339 ((double)generations[gen].cum_sum_bytes_allocated)
340 / ((double)generations[gen].bytes_allocated);
343 void fpu_save(int *); /* defined in x86-assem.S */
344 void fpu_restore(int *); /* defined in x86-assem.S */
345 /* The verbose argument controls how much to print: 0 for normal
346 * level of detail; 1 for debugging. */
348 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
353 /* This code uses the FP instructions which may be set up for Lisp
354 * so they need to be saved and reset for C. */
357 /* number of generations to print */
359 gens = NUM_GENERATIONS+1;
361 gens = NUM_GENERATIONS;
363 /* Print the heap stats. */
365 " Gen Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
367 for (i = 0; i < gens; i++) {
371 int large_boxed_cnt = 0;
372 int large_unboxed_cnt = 0;
375 for (j = 0; j < last_free_page; j++)
376 if (page_table[j].gen == i) {
378 /* Count the number of boxed pages within the given
380 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
381 if (page_table[j].large_object)
386 if(page_table[j].dont_move) pinned_cnt++;
387 /* Count the number of unboxed pages within the given
389 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
390 if (page_table[j].large_object)
397 gc_assert(generations[i].bytes_allocated
398 == count_generation_bytes_allocated(i));
400 " %1d: %5d %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4f\n",
402 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
404 generations[i].bytes_allocated,
405 (count_generation_pages(i)*PAGE_BYTES
406 - generations[i].bytes_allocated),
407 generations[i].gc_trigger,
408 count_write_protect_generation_pages(i),
409 generations[i].num_gc,
412 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
414 fpu_restore(fpu_state);
418 * allocation routines
422 * To support quick and inline allocation, regions of memory can be
423 * allocated and then allocated from with just a free pointer and a
424 * check against an end address.
426 * Since objects can be allocated to spaces with different properties
427 * e.g. boxed/unboxed, generation, ages; there may need to be many
428 * allocation regions.
430 * Each allocation region may be start within a partly used page. Many
431 * features of memory use are noted on a page wise basis, e.g. the
432 * generation; so if a region starts within an existing allocated page
433 * it must be consistent with this page.
435 * During the scavenging of the newspace, objects will be transported
436 * into an allocation region, and pointers updated to point to this
437 * allocation region. It is possible that these pointers will be
438 * scavenged again before the allocation region is closed, e.g. due to
439 * trans_list which jumps all over the place to cleanup the list. It
440 * is important to be able to determine properties of all objects
441 * pointed to when scavenging, e.g to detect pointers to the oldspace.
442 * Thus it's important that the allocation regions have the correct
443 * properties set when allocated, and not just set when closed. The
444 * region allocation routines return regions with the specified
445 * properties, and grab all the pages, setting their properties
446 * appropriately, except that the amount used is not known.
448 * These regions are used to support quicker allocation using just a
449 * free pointer. The actual space used by the region is not reflected
450 * in the pages tables until it is closed. It can't be scavenged until
453 * When finished with the region it should be closed, which will
454 * update the page tables for the actual space used returning unused
455 * space. Further it may be noted in the new regions which is
456 * necessary when scavenging the newspace.
458 * Large objects may be allocated directly without an allocation
459 * region, the page tables are updated immediately.
461 * Unboxed objects don't contain pointers to other objects and so
462 * don't need scavenging. Further they can't contain pointers to
463 * younger generations so WP is not needed. By allocating pages to
464 * unboxed objects the whole page never needs scavenging or
465 * write-protecting. */
467 /* We are only using two regions at present. Both are for the current
468 * newspace generation. */
469 struct alloc_region boxed_region;
470 struct alloc_region unboxed_region;
472 /* The generation currently being allocated to. */
473 static int gc_alloc_generation;
475 /* Find a new region with room for at least the given number of bytes.
477 * It starts looking at the current generation's alloc_start_page. So
478 * may pick up from the previous region if there is enough space. This
479 * keeps the allocation contiguous when scavenging the newspace.
481 * The alloc_region should have been closed by a call to
482 * gc_alloc_update_page_tables(), and will thus be in an empty state.
484 * To assist the scavenging functions write-protected pages are not
485 * used. Free pages should not be write-protected.
487 * It is critical to the conservative GC that the start of regions be
488 * known. To help achieve this only small regions are allocated at a
491 * During scavenging, pointers may be found to within the current
492 * region and the page generation must be set so that pointers to the
493 * from space can be recognized. Therefore the generation of pages in
494 * the region are set to gc_alloc_generation. To prevent another
495 * allocation call using the same pages, all the pages in the region
496 * are allocated, although they will initially be empty.
499 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
508 "/alloc_new_region for %d bytes from gen %d\n",
509 nbytes, gc_alloc_generation));
512 /* Check that the region is in a reset state. */
513 gc_assert((alloc_region->first_page == 0)
514 && (alloc_region->last_page == -1)
515 && (alloc_region->free_pointer == alloc_region->end_addr));
516 get_spinlock(&free_pages_lock,(int) alloc_region);
519 generations[gc_alloc_generation].alloc_unboxed_start_page;
522 generations[gc_alloc_generation].alloc_start_page;
524 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
525 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
526 + PAGE_BYTES*(last_page-first_page);
528 /* Set up the alloc_region. */
529 alloc_region->first_page = first_page;
530 alloc_region->last_page = last_page;
531 alloc_region->start_addr = page_table[first_page].bytes_used
532 + page_address(first_page);
533 alloc_region->free_pointer = alloc_region->start_addr;
534 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
536 /* Set up the pages. */
538 /* The first page may have already been in use. */
539 if (page_table[first_page].bytes_used == 0) {
541 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
543 page_table[first_page].allocated = BOXED_PAGE_FLAG;
544 page_table[first_page].gen = gc_alloc_generation;
545 page_table[first_page].large_object = 0;
546 page_table[first_page].first_object_offset = 0;
550 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
552 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
553 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
555 gc_assert(page_table[first_page].gen == gc_alloc_generation);
556 gc_assert(page_table[first_page].large_object == 0);
558 for (i = first_page+1; i <= last_page; i++) {
560 page_table[i].allocated = UNBOXED_PAGE_FLAG;
562 page_table[i].allocated = BOXED_PAGE_FLAG;
563 page_table[i].gen = gc_alloc_generation;
564 page_table[i].large_object = 0;
565 /* This may not be necessary for unboxed regions (think it was
567 page_table[i].first_object_offset =
568 alloc_region->start_addr - page_address(i);
569 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
571 /* Bump up last_free_page. */
572 if (last_page+1 > last_free_page) {
573 last_free_page = last_page+1;
574 SetSymbolValue(ALLOCATION_POINTER,
575 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),
578 release_spinlock(&free_pages_lock);
580 /* we can do this after releasing free_pages_lock */
581 if (gencgc_zero_check) {
583 for (p = (int *)alloc_region->start_addr;
584 p < (int *)alloc_region->end_addr; p++) {
586 /* KLUDGE: It would be nice to use %lx and explicit casts
587 * (long) in code like this, so that it is less likely to
588 * break randomly when running on a machine with different
589 * word sizes. -- WHN 19991129 */
590 lose("The new region at %x is not zero.", p);
597 /* If the record_new_objects flag is 2 then all new regions created
600 * If it's 1 then then it is only recorded if the first page of the
601 * current region is <= new_areas_ignore_page. This helps avoid
602 * unnecessary recording when doing full scavenge pass.
604 * The new_object structure holds the page, byte offset, and size of
605 * new regions of objects. Each new area is placed in the array of
606 * these structures pointer to by new_areas. new_areas_index holds the
607 * offset into new_areas.
609 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
610 * later code must detect this and handle it, probably by doing a full
611 * scavenge of a generation. */
612 #define NUM_NEW_AREAS 512
613 static int record_new_objects = 0;
614 static int new_areas_ignore_page;
620 static struct new_area (*new_areas)[];
621 static int new_areas_index;
624 /* Add a new area to new_areas. */
626 add_new_area(int first_page, int offset, int size)
628 unsigned new_area_start,c;
631 /* Ignore if full. */
632 if (new_areas_index >= NUM_NEW_AREAS)
635 switch (record_new_objects) {
639 if (first_page > new_areas_ignore_page)
648 new_area_start = PAGE_BYTES*first_page + offset;
650 /* Search backwards for a prior area that this follows from. If
651 found this will save adding a new area. */
652 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
654 PAGE_BYTES*((*new_areas)[i].page)
655 + (*new_areas)[i].offset
656 + (*new_areas)[i].size;
658 "/add_new_area S1 %d %d %d %d\n",
659 i, c, new_area_start, area_end));*/
660 if (new_area_start == area_end) {
662 "/adding to [%d] %d %d %d with %d %d %d:\n",
664 (*new_areas)[i].page,
665 (*new_areas)[i].offset,
666 (*new_areas)[i].size,
670 (*new_areas)[i].size += size;
675 (*new_areas)[new_areas_index].page = first_page;
676 (*new_areas)[new_areas_index].offset = offset;
677 (*new_areas)[new_areas_index].size = size;
679 "/new_area %d page %d offset %d size %d\n",
680 new_areas_index, first_page, offset, size));*/
683 /* Note the max new_areas used. */
684 if (new_areas_index > max_new_areas)
685 max_new_areas = new_areas_index;
688 /* Update the tables for the alloc_region. The region may be added to
691 * When done the alloc_region is set up so that the next quick alloc
692 * will fail safely and thus a new region will be allocated. Further
693 * it is safe to try to re-update the page table of this reset
696 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
702 int orig_first_page_bytes_used;
707 first_page = alloc_region->first_page;
709 /* Catch an unused alloc_region. */
710 if ((first_page == 0) && (alloc_region->last_page == -1))
713 next_page = first_page+1;
715 get_spinlock(&free_pages_lock,(int) alloc_region);
716 if (alloc_region->free_pointer != alloc_region->start_addr) {
717 /* some bytes were allocated in the region */
718 orig_first_page_bytes_used = page_table[first_page].bytes_used;
720 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
722 /* All the pages used need to be updated */
724 /* Update the first page. */
726 /* If the page was free then set up the gen, and
727 * first_object_offset. */
728 if (page_table[first_page].bytes_used == 0)
729 gc_assert(page_table[first_page].first_object_offset == 0);
730 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
733 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
735 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
736 gc_assert(page_table[first_page].gen == gc_alloc_generation);
737 gc_assert(page_table[first_page].large_object == 0);
741 /* Calculate the number of bytes used in this page. This is not
742 * always the number of new bytes, unless it was free. */
744 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>PAGE_BYTES) {
745 bytes_used = PAGE_BYTES;
748 page_table[first_page].bytes_used = bytes_used;
749 byte_cnt += bytes_used;
752 /* All the rest of the pages should be free. We need to set their
753 * first_object_offset pointer to the start of the region, and set
756 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
758 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
760 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
761 gc_assert(page_table[next_page].bytes_used == 0);
762 gc_assert(page_table[next_page].gen == gc_alloc_generation);
763 gc_assert(page_table[next_page].large_object == 0);
765 gc_assert(page_table[next_page].first_object_offset ==
766 alloc_region->start_addr - page_address(next_page));
768 /* Calculate the number of bytes used in this page. */
770 if ((bytes_used = (alloc_region->free_pointer
771 - page_address(next_page)))>PAGE_BYTES) {
772 bytes_used = PAGE_BYTES;
775 page_table[next_page].bytes_used = bytes_used;
776 byte_cnt += bytes_used;
781 region_size = alloc_region->free_pointer - alloc_region->start_addr;
782 bytes_allocated += region_size;
783 generations[gc_alloc_generation].bytes_allocated += region_size;
785 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
787 /* Set the generations alloc restart page to the last page of
790 generations[gc_alloc_generation].alloc_unboxed_start_page =
793 generations[gc_alloc_generation].alloc_start_page = next_page-1;
795 /* Add the region to the new_areas if requested. */
797 add_new_area(first_page,orig_first_page_bytes_used, region_size);
801 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
803 gc_alloc_generation));
806 /* There are no bytes allocated. Unallocate the first_page if
807 * there are 0 bytes_used. */
808 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
809 if (page_table[first_page].bytes_used == 0)
810 page_table[first_page].allocated = FREE_PAGE_FLAG;
813 /* Unallocate any unused pages. */
814 while (next_page <= alloc_region->last_page) {
815 gc_assert(page_table[next_page].bytes_used == 0);
816 page_table[next_page].allocated = FREE_PAGE_FLAG;
819 release_spinlock(&free_pages_lock);
820 /* alloc_region is per-thread, we're ok to do this unlocked */
821 gc_set_region_empty(alloc_region);
824 static inline void *gc_quick_alloc(int nbytes);
826 /* Allocate a possibly large object. */
828 gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
832 int orig_first_page_bytes_used;
838 get_spinlock(&free_pages_lock,(int) alloc_region);
842 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
844 first_page = generations[gc_alloc_generation].alloc_large_start_page;
846 if (first_page <= alloc_region->last_page) {
847 first_page = alloc_region->last_page+1;
850 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
852 gc_assert(first_page > alloc_region->last_page);
854 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
857 generations[gc_alloc_generation].alloc_large_start_page = last_page;
859 /* Set up the pages. */
860 orig_first_page_bytes_used = page_table[first_page].bytes_used;
862 /* If the first page was free then set up the gen, and
863 * first_object_offset. */
864 if (page_table[first_page].bytes_used == 0) {
866 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
868 page_table[first_page].allocated = BOXED_PAGE_FLAG;
869 page_table[first_page].gen = gc_alloc_generation;
870 page_table[first_page].first_object_offset = 0;
871 page_table[first_page].large_object = 1;
875 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
877 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
878 gc_assert(page_table[first_page].gen == gc_alloc_generation);
879 gc_assert(page_table[first_page].large_object == 1);
883 /* Calc. the number of bytes used in this page. This is not
884 * always the number of new bytes, unless it was free. */
886 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
887 bytes_used = PAGE_BYTES;
890 page_table[first_page].bytes_used = bytes_used;
891 byte_cnt += bytes_used;
893 next_page = first_page+1;
895 /* All the rest of the pages should be free. We need to set their
896 * first_object_offset pointer to the start of the region, and
897 * set the bytes_used. */
899 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
900 gc_assert(page_table[next_page].bytes_used == 0);
902 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
904 page_table[next_page].allocated = BOXED_PAGE_FLAG;
905 page_table[next_page].gen = gc_alloc_generation;
906 page_table[next_page].large_object = 1;
908 page_table[next_page].first_object_offset =
909 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
911 /* Calculate the number of bytes used in this page. */
913 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > PAGE_BYTES) {
914 bytes_used = PAGE_BYTES;
917 page_table[next_page].bytes_used = bytes_used;
918 page_table[next_page].write_protected=0;
919 page_table[next_page].dont_move=0;
920 byte_cnt += bytes_used;
924 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
926 bytes_allocated += nbytes;
927 generations[gc_alloc_generation].bytes_allocated += nbytes;
929 /* Add the region to the new_areas if requested. */
931 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
933 /* Bump up last_free_page */
934 if (last_page+1 > last_free_page) {
935 last_free_page = last_page+1;
936 SetSymbolValue(ALLOCATION_POINTER,
937 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),0);
939 release_spinlock(&free_pages_lock);
941 return((void *)(page_address(first_page)+orig_first_page_bytes_used));
945 gc_find_freeish_pages(int *restart_page_ptr, int nbytes, int unboxed)
950 int restart_page=*restart_page_ptr;
953 int large_p=(nbytes>=large_object_size);
954 gc_assert(free_pages_lock);
956 /* Search for a contiguous free space of at least nbytes. If it's
957 * a large object then align it on a page boundary by searching
958 * for a free page. */
961 first_page = restart_page;
963 while ((first_page < NUM_PAGES)
964 && (page_table[first_page].allocated != FREE_PAGE_FLAG))
967 while (first_page < NUM_PAGES) {
968 if(page_table[first_page].allocated == FREE_PAGE_FLAG)
970 if((page_table[first_page].allocated ==
971 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
972 (page_table[first_page].large_object == 0) &&
973 (page_table[first_page].gen == gc_alloc_generation) &&
974 (page_table[first_page].bytes_used < (PAGE_BYTES-32)) &&
975 (page_table[first_page].write_protected == 0) &&
976 (page_table[first_page].dont_move == 0)) {
982 if (first_page >= NUM_PAGES) {
984 "Argh! gc_find_free_space failed (first_page), nbytes=%d.\n",
986 print_generation_stats(1);
990 gc_assert(page_table[first_page].write_protected == 0);
992 last_page = first_page;
993 bytes_found = PAGE_BYTES - page_table[first_page].bytes_used;
995 while (((bytes_found < nbytes)
996 || (!large_p && (num_pages < 2)))
997 && (last_page < (NUM_PAGES-1))
998 && (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
1001 bytes_found += PAGE_BYTES;
1002 gc_assert(page_table[last_page].write_protected == 0);
1005 region_size = (PAGE_BYTES - page_table[first_page].bytes_used)
1006 + PAGE_BYTES*(last_page-first_page);
1008 gc_assert(bytes_found == region_size);
1009 restart_page = last_page + 1;
1010 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1012 /* Check for a failure */
1013 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1015 "Argh! gc_find_freeish_pages failed (restart_page), nbytes=%d.\n",
1017 print_generation_stats(1);
1020 *restart_page_ptr=first_page;
1024 /* Allocate bytes. All the rest of the special-purpose allocation
1025 * functions will eventually call this */
1028 gc_alloc_with_region(int nbytes,int unboxed_p, struct alloc_region *my_region,
1031 void *new_free_pointer;
1033 if(nbytes>=large_object_size)
1034 return gc_alloc_large(nbytes,unboxed_p,my_region);
1036 /* Check whether there is room in the current alloc region. */
1037 new_free_pointer = my_region->free_pointer + nbytes;
1039 if (new_free_pointer <= my_region->end_addr) {
1040 /* If so then allocate from the current alloc region. */
1041 void *new_obj = my_region->free_pointer;
1042 my_region->free_pointer = new_free_pointer;
1044 /* Unless a `quick' alloc was requested, check whether the
1045 alloc region is almost empty. */
1047 (my_region->end_addr - my_region->free_pointer) <= 32) {
1048 /* If so, finished with the current region. */
1049 gc_alloc_update_page_tables(unboxed_p, my_region);
1050 /* Set up a new region. */
1051 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1054 return((void *)new_obj);
1057 /* Else not enough free space in the current region: retry with a
1060 gc_alloc_update_page_tables(unboxed_p, my_region);
1061 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1062 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1065 /* these are only used during GC: all allocation from the mutator calls
1066 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1070 gc_general_alloc(int nbytes,int unboxed_p,int quick_p)
1072 struct alloc_region *my_region =
1073 unboxed_p ? &unboxed_region : &boxed_region;
1074 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1077 static inline void *
1078 gc_quick_alloc(int nbytes)
1080 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1083 static inline void *
1084 gc_quick_alloc_large(int nbytes)
1086 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1089 static inline void *
1090 gc_alloc_unboxed(int nbytes)
1092 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1095 static inline void *
1096 gc_quick_alloc_unboxed(int nbytes)
1098 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1101 static inline void *
1102 gc_quick_alloc_large_unboxed(int nbytes)
1104 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1108 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1111 extern int (*scavtab[256])(lispobj *where, lispobj object);
1112 extern lispobj (*transother[256])(lispobj object);
1113 extern int (*sizetab[256])(lispobj *where);
1115 /* Copy a large boxed object. If the object is in a large object
1116 * region then it is simply promoted, else it is copied. If it's large
1117 * enough then it's copied to a large object region.
1119 * Vectors may have shrunk. If the object is not copied the space
1120 * needs to be reclaimed, and the page_tables corrected. */
1122 copy_large_object(lispobj object, int nwords)
1128 gc_assert(is_lisp_pointer(object));
1129 gc_assert(from_space_p(object));
1130 gc_assert((nwords & 0x01) == 0);
1133 /* Check whether it's in a large object region. */
1134 first_page = find_page_index((void *)object);
1135 gc_assert(first_page >= 0);
1137 if (page_table[first_page].large_object) {
1139 /* Promote the object. */
1141 int remaining_bytes;
1146 /* Note: Any page write-protection must be removed, else a
1147 * later scavenge_newspace may incorrectly not scavenge these
1148 * pages. This would not be necessary if they are added to the
1149 * new areas, but let's do it for them all (they'll probably
1150 * be written anyway?). */
1152 gc_assert(page_table[first_page].first_object_offset == 0);
1154 next_page = first_page;
1155 remaining_bytes = nwords*4;
1156 while (remaining_bytes > PAGE_BYTES) {
1157 gc_assert(page_table[next_page].gen == from_space);
1158 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1159 gc_assert(page_table[next_page].large_object);
1160 gc_assert(page_table[next_page].first_object_offset==
1161 -PAGE_BYTES*(next_page-first_page));
1162 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1164 page_table[next_page].gen = new_space;
1166 /* Remove any write-protection. We should be able to rely
1167 * on the write-protect flag to avoid redundant calls. */
1168 if (page_table[next_page].write_protected) {
1169 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1170 page_table[next_page].write_protected = 0;
1172 remaining_bytes -= PAGE_BYTES;
1176 /* Now only one page remains, but the object may have shrunk
1177 * so there may be more unused pages which will be freed. */
1179 /* The object may have shrunk but shouldn't have grown. */
1180 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1182 page_table[next_page].gen = new_space;
1183 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1185 /* Adjust the bytes_used. */
1186 old_bytes_used = page_table[next_page].bytes_used;
1187 page_table[next_page].bytes_used = remaining_bytes;
1189 bytes_freed = old_bytes_used - remaining_bytes;
1191 /* Free any remaining pages; needs care. */
1193 while ((old_bytes_used == PAGE_BYTES) &&
1194 (page_table[next_page].gen == from_space) &&
1195 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1196 page_table[next_page].large_object &&
1197 (page_table[next_page].first_object_offset ==
1198 -(next_page - first_page)*PAGE_BYTES)) {
1199 /* Checks out OK, free the page. Don't need to bother zeroing
1200 * pages as this should have been done before shrinking the
1201 * object. These pages shouldn't be write-protected as they
1202 * should be zero filled. */
1203 gc_assert(page_table[next_page].write_protected == 0);
1205 old_bytes_used = page_table[next_page].bytes_used;
1206 page_table[next_page].allocated = FREE_PAGE_FLAG;
1207 page_table[next_page].bytes_used = 0;
1208 bytes_freed += old_bytes_used;
1212 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1213 generations[new_space].bytes_allocated += 4*nwords;
1214 bytes_allocated -= bytes_freed;
1216 /* Add the region to the new_areas if requested. */
1217 add_new_area(first_page,0,nwords*4);
1221 /* Get tag of object. */
1222 tag = lowtag_of(object);
1224 /* Allocate space. */
1225 new = gc_quick_alloc_large(nwords*4);
1227 memcpy(new,native_pointer(object),nwords*4);
1229 /* Return Lisp pointer of new object. */
1230 return ((lispobj) new) | tag;
1234 /* to copy unboxed objects */
1236 copy_unboxed_object(lispobj object, int nwords)
1241 gc_assert(is_lisp_pointer(object));
1242 gc_assert(from_space_p(object));
1243 gc_assert((nwords & 0x01) == 0);
1245 /* Get tag of object. */
1246 tag = lowtag_of(object);
1248 /* Allocate space. */
1249 new = gc_quick_alloc_unboxed(nwords*4);
1251 memcpy(new,native_pointer(object),nwords*4);
1253 /* Return Lisp pointer of new object. */
1254 return ((lispobj) new) | tag;
1257 /* to copy large unboxed objects
1259 * If the object is in a large object region then it is simply
1260 * promoted, else it is copied. If it's large enough then it's copied
1261 * to a large object region.
1263 * Bignums and vectors may have shrunk. If the object is not copied
1264 * the space needs to be reclaimed, and the page_tables corrected.
1266 * KLUDGE: There's a lot of cut-and-paste duplication between this
1267 * function and copy_large_object(..). -- WHN 20000619 */
1269 copy_large_unboxed_object(lispobj object, int nwords)
1273 lispobj *source, *dest;
1276 gc_assert(is_lisp_pointer(object));
1277 gc_assert(from_space_p(object));
1278 gc_assert((nwords & 0x01) == 0);
1280 if ((nwords > 1024*1024) && gencgc_verbose)
1281 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*4));
1283 /* Check whether it's a large object. */
1284 first_page = find_page_index((void *)object);
1285 gc_assert(first_page >= 0);
1287 if (page_table[first_page].large_object) {
1288 /* Promote the object. Note: Unboxed objects may have been
1289 * allocated to a BOXED region so it may be necessary to
1290 * change the region to UNBOXED. */
1291 int remaining_bytes;
1296 gc_assert(page_table[first_page].first_object_offset == 0);
1298 next_page = first_page;
1299 remaining_bytes = nwords*4;
1300 while (remaining_bytes > PAGE_BYTES) {
1301 gc_assert(page_table[next_page].gen == from_space);
1302 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1303 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1304 gc_assert(page_table[next_page].large_object);
1305 gc_assert(page_table[next_page].first_object_offset==
1306 -PAGE_BYTES*(next_page-first_page));
1307 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1309 page_table[next_page].gen = new_space;
1310 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1311 remaining_bytes -= PAGE_BYTES;
1315 /* Now only one page remains, but the object may have shrunk so
1316 * there may be more unused pages which will be freed. */
1318 /* Object may have shrunk but shouldn't have grown - check. */
1319 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1321 page_table[next_page].gen = new_space;
1322 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1324 /* Adjust the bytes_used. */
1325 old_bytes_used = page_table[next_page].bytes_used;
1326 page_table[next_page].bytes_used = remaining_bytes;
1328 bytes_freed = old_bytes_used - remaining_bytes;
1330 /* Free any remaining pages; needs care. */
1332 while ((old_bytes_used == PAGE_BYTES) &&
1333 (page_table[next_page].gen == from_space) &&
1334 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1335 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1336 page_table[next_page].large_object &&
1337 (page_table[next_page].first_object_offset ==
1338 -(next_page - first_page)*PAGE_BYTES)) {
1339 /* Checks out OK, free the page. Don't need to both zeroing
1340 * pages as this should have been done before shrinking the
1341 * object. These pages shouldn't be write-protected, even if
1342 * boxed they should be zero filled. */
1343 gc_assert(page_table[next_page].write_protected == 0);
1345 old_bytes_used = page_table[next_page].bytes_used;
1346 page_table[next_page].allocated = FREE_PAGE_FLAG;
1347 page_table[next_page].bytes_used = 0;
1348 bytes_freed += old_bytes_used;
1352 if ((bytes_freed > 0) && gencgc_verbose)
1354 "/copy_large_unboxed bytes_freed=%d\n",
1357 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1358 generations[new_space].bytes_allocated += 4*nwords;
1359 bytes_allocated -= bytes_freed;
1364 /* Get tag of object. */
1365 tag = lowtag_of(object);
1367 /* Allocate space. */
1368 new = gc_quick_alloc_large_unboxed(nwords*4);
1371 source = (lispobj *) native_pointer(object);
1373 /* Copy the object. */
1374 while (nwords > 0) {
1375 dest[0] = source[0];
1376 dest[1] = source[1];
1382 /* Return Lisp pointer of new object. */
1383 return ((lispobj) new) | tag;
1392 * code and code-related objects
1395 static lispobj trans_fun_header(lispobj object);
1396 static lispobj trans_boxed(lispobj object);
1399 /* Scan a x86 compiled code object, looking for possible fixups that
1400 * have been missed after a move.
1402 * Two types of fixups are needed:
1403 * 1. Absolute fixups to within the code object.
1404 * 2. Relative fixups to outside the code object.
1406 * Currently only absolute fixups to the constant vector, or to the
1407 * code area are checked. */
1409 sniff_code_object(struct code *code, unsigned displacement)
1411 int nheader_words, ncode_words, nwords;
1413 void *constants_start_addr, *constants_end_addr;
1414 void *code_start_addr, *code_end_addr;
1415 int fixup_found = 0;
1417 if (!check_code_fixups)
1420 ncode_words = fixnum_value(code->code_size);
1421 nheader_words = HeaderValue(*(lispobj *)code);
1422 nwords = ncode_words + nheader_words;
1424 constants_start_addr = (void *)code + 5*4;
1425 constants_end_addr = (void *)code + nheader_words*4;
1426 code_start_addr = (void *)code + nheader_words*4;
1427 code_end_addr = (void *)code + nwords*4;
1429 /* Work through the unboxed code. */
1430 for (p = code_start_addr; p < code_end_addr; p++) {
1431 void *data = *(void **)p;
1432 unsigned d1 = *((unsigned char *)p - 1);
1433 unsigned d2 = *((unsigned char *)p - 2);
1434 unsigned d3 = *((unsigned char *)p - 3);
1435 unsigned d4 = *((unsigned char *)p - 4);
1437 unsigned d5 = *((unsigned char *)p - 5);
1438 unsigned d6 = *((unsigned char *)p - 6);
1441 /* Check for code references. */
1442 /* Check for a 32 bit word that looks like an absolute
1443 reference to within the code adea of the code object. */
1444 if ((data >= (code_start_addr-displacement))
1445 && (data < (code_end_addr-displacement))) {
1446 /* function header */
1448 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1449 /* Skip the function header */
1453 /* the case of PUSH imm32 */
1457 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1458 p, d6, d5, d4, d3, d2, d1, data));
1459 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1461 /* the case of MOV [reg-8],imm32 */
1463 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1464 || d2==0x45 || d2==0x46 || d2==0x47)
1468 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1469 p, d6, d5, d4, d3, d2, d1, data));
1470 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1472 /* the case of LEA reg,[disp32] */
1473 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1476 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1477 p, d6, d5, d4, d3, d2, d1, data));
1478 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1482 /* Check for constant references. */
1483 /* Check for a 32 bit word that looks like an absolute
1484 reference to within the constant vector. Constant references
1486 if ((data >= (constants_start_addr-displacement))
1487 && (data < (constants_end_addr-displacement))
1488 && (((unsigned)data & 0x3) == 0)) {
1493 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1494 p, d6, d5, d4, d3, d2, d1, data));
1495 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1498 /* the case of MOV m32,EAX */
1502 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1503 p, d6, d5, d4, d3, d2, d1, data));
1504 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1507 /* the case of CMP m32,imm32 */
1508 if ((d1 == 0x3d) && (d2 == 0x81)) {
1511 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1512 p, d6, d5, d4, d3, d2, d1, data));
1514 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1517 /* Check for a mod=00, r/m=101 byte. */
1518 if ((d1 & 0xc7) == 5) {
1523 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1524 p, d6, d5, d4, d3, d2, d1, data));
1525 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1527 /* the case of CMP reg32,m32 */
1531 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1532 p, d6, d5, d4, d3, d2, d1, data));
1533 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1535 /* the case of MOV m32,reg32 */
1539 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1540 p, d6, d5, d4, d3, d2, d1, data));
1541 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1543 /* the case of MOV reg32,m32 */
1547 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1548 p, d6, d5, d4, d3, d2, d1, data));
1549 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1551 /* the case of LEA reg32,m32 */
1555 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1556 p, d6, d5, d4, d3, d2, d1, data));
1557 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1563 /* If anything was found, print some information on the code
1567 "/compiled code object at %x: header words = %d, code words = %d\n",
1568 code, nheader_words, ncode_words));
1570 "/const start = %x, end = %x\n",
1571 constants_start_addr, constants_end_addr));
1573 "/code start = %x, end = %x\n",
1574 code_start_addr, code_end_addr));
1579 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1581 int nheader_words, ncode_words, nwords;
1582 void *constants_start_addr, *constants_end_addr;
1583 void *code_start_addr, *code_end_addr;
1584 lispobj fixups = NIL;
1585 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
1586 struct vector *fixups_vector;
1588 ncode_words = fixnum_value(new_code->code_size);
1589 nheader_words = HeaderValue(*(lispobj *)new_code);
1590 nwords = ncode_words + nheader_words;
1592 "/compiled code object at %x: header words = %d, code words = %d\n",
1593 new_code, nheader_words, ncode_words)); */
1594 constants_start_addr = (void *)new_code + 5*4;
1595 constants_end_addr = (void *)new_code + nheader_words*4;
1596 code_start_addr = (void *)new_code + nheader_words*4;
1597 code_end_addr = (void *)new_code + nwords*4;
1600 "/const start = %x, end = %x\n",
1601 constants_start_addr,constants_end_addr));
1603 "/code start = %x; end = %x\n",
1604 code_start_addr,code_end_addr));
1607 /* The first constant should be a pointer to the fixups for this
1608 code objects. Check. */
1609 fixups = new_code->constants[0];
1611 /* It will be 0 or the unbound-marker if there are no fixups (as
1612 * will be the case if the code object has been purified, for
1613 * example) and will be an other pointer if it is valid. */
1614 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1615 !is_lisp_pointer(fixups)) {
1616 /* Check for possible errors. */
1617 if (check_code_fixups)
1618 sniff_code_object(new_code, displacement);
1623 fixups_vector = (struct vector *)native_pointer(fixups);
1625 /* Could be pointing to a forwarding pointer. */
1626 /* FIXME is this always in from_space? if so, could replace this code with
1627 * forwarding_pointer_p/forwarding_pointer_value */
1628 if (is_lisp_pointer(fixups) &&
1629 (find_page_index((void*)fixups_vector) != -1) &&
1630 (fixups_vector->header == 0x01)) {
1631 /* If so, then follow it. */
1632 /*SHOW("following pointer to a forwarding pointer");*/
1633 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1636 /*SHOW("got fixups");*/
1638 if (widetag_of(fixups_vector->header) ==
1639 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
1640 /* Got the fixups for the code block. Now work through the vector,
1641 and apply a fixup at each address. */
1642 int length = fixnum_value(fixups_vector->length);
1644 for (i = 0; i < length; i++) {
1645 unsigned offset = fixups_vector->data[i];
1646 /* Now check the current value of offset. */
1647 unsigned old_value =
1648 *(unsigned *)((unsigned)code_start_addr + offset);
1650 /* If it's within the old_code object then it must be an
1651 * absolute fixup (relative ones are not saved) */
1652 if ((old_value >= (unsigned)old_code)
1653 && (old_value < ((unsigned)old_code + nwords*4)))
1654 /* So add the dispacement. */
1655 *(unsigned *)((unsigned)code_start_addr + offset) =
1656 old_value + displacement;
1658 /* It is outside the old code object so it must be a
1659 * relative fixup (absolute fixups are not saved). So
1660 * subtract the displacement. */
1661 *(unsigned *)((unsigned)code_start_addr + offset) =
1662 old_value - displacement;
1666 /* Check for possible errors. */
1667 if (check_code_fixups) {
1668 sniff_code_object(new_code,displacement);
1674 trans_boxed_large(lispobj object)
1677 unsigned long length;
1679 gc_assert(is_lisp_pointer(object));
1681 header = *((lispobj *) native_pointer(object));
1682 length = HeaderValue(header) + 1;
1683 length = CEILING(length, 2);
1685 return copy_large_object(object, length);
1690 trans_unboxed_large(lispobj object)
1693 unsigned long length;
1696 gc_assert(is_lisp_pointer(object));
1698 header = *((lispobj *) native_pointer(object));
1699 length = HeaderValue(header) + 1;
1700 length = CEILING(length, 2);
1702 return copy_large_unboxed_object(object, length);
1707 * vector-like objects
1711 /* FIXME: What does this mean? */
1712 int gencgc_hash = 1;
1715 scav_vector(lispobj *where, lispobj object)
1717 unsigned int kv_length;
1719 unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
1720 lispobj *hash_table;
1721 lispobj empty_symbol;
1722 unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1723 unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1724 unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1726 unsigned next_vector_length = 0;
1728 /* FIXME: A comment explaining this would be nice. It looks as
1729 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
1730 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
1731 if (HeaderValue(object) != subtype_VectorValidHashing)
1735 /* This is set for backward compatibility. FIXME: Do we need
1738 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
1742 kv_length = fixnum_value(where[1]);
1743 kv_vector = where + 2; /* Skip the header and length. */
1744 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1746 /* Scavenge element 0, which may be a hash-table structure. */
1747 scavenge(where+2, 1);
1748 if (!is_lisp_pointer(where[2])) {
1749 lose("no pointer at %x in hash table", where[2]);
1751 hash_table = (lispobj *)native_pointer(where[2]);
1752 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1753 if (widetag_of(hash_table[0]) != INSTANCE_HEADER_WIDETAG) {
1754 lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
1757 /* Scavenge element 1, which should be some internal symbol that
1758 * the hash table code reserves for marking empty slots. */
1759 scavenge(where+3, 1);
1760 if (!is_lisp_pointer(where[3])) {
1761 lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
1763 empty_symbol = where[3];
1764 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1765 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1766 SYMBOL_HEADER_WIDETAG) {
1767 lose("not a symbol where empty-hash-table-slot symbol expected: %x",
1768 *(lispobj *)native_pointer(empty_symbol));
1771 /* Scavenge hash table, which will fix the positions of the other
1772 * needed objects. */
1773 scavenge(hash_table, 16);
1775 /* Cross-check the kv_vector. */
1776 if (where != (lispobj *)native_pointer(hash_table[9])) {
1777 lose("hash_table table!=this table %x", hash_table[9]);
1781 weak_p_obj = hash_table[10];
1785 lispobj index_vector_obj = hash_table[13];
1787 if (is_lisp_pointer(index_vector_obj) &&
1788 (widetag_of(*(lispobj *)native_pointer(index_vector_obj)) ==
1789 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1790 index_vector = ((unsigned int *)native_pointer(index_vector_obj)) + 2;
1791 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
1792 length = fixnum_value(((unsigned int *)native_pointer(index_vector_obj))[1]);
1793 /*FSHOW((stderr, "/length = %d\n", length));*/
1795 lose("invalid index_vector %x", index_vector_obj);
1801 lispobj next_vector_obj = hash_table[14];
1803 if (is_lisp_pointer(next_vector_obj) &&
1804 (widetag_of(*(lispobj *)native_pointer(next_vector_obj)) ==
1805 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1806 next_vector = ((unsigned int *)native_pointer(next_vector_obj)) + 2;
1807 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
1808 next_vector_length = fixnum_value(((unsigned int *)native_pointer(next_vector_obj))[1]);
1809 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
1811 lose("invalid next_vector %x", next_vector_obj);
1815 /* maybe hash vector */
1817 /* FIXME: This bare "15" offset should become a symbolic
1818 * expression of some sort. And all the other bare offsets
1819 * too. And the bare "16" in scavenge(hash_table, 16). And
1820 * probably other stuff too. Ugh.. */
1821 lispobj hash_vector_obj = hash_table[15];
1823 if (is_lisp_pointer(hash_vector_obj) &&
1824 (widetag_of(*(lispobj *)native_pointer(hash_vector_obj))
1825 == SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1826 hash_vector = ((unsigned int *)native_pointer(hash_vector_obj)) + 2;
1827 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
1828 gc_assert(fixnum_value(((unsigned int *)native_pointer(hash_vector_obj))[1])
1829 == next_vector_length);
1832 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
1836 /* These lengths could be different as the index_vector can be a
1837 * different length from the others, a larger index_vector could help
1838 * reduce collisions. */
1839 gc_assert(next_vector_length*2 == kv_length);
1841 /* now all set up.. */
1843 /* Work through the KV vector. */
1846 for (i = 1; i < next_vector_length; i++) {
1847 lispobj old_key = kv_vector[2*i];
1848 unsigned int old_index = (old_key & 0x1fffffff)%length;
1850 /* Scavenge the key and value. */
1851 scavenge(&kv_vector[2*i],2);
1853 /* Check whether the key has moved and is EQ based. */
1855 lispobj new_key = kv_vector[2*i];
1856 unsigned int new_index = (new_key & 0x1fffffff)%length;
1858 if ((old_index != new_index) &&
1859 ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
1860 ((new_key != empty_symbol) ||
1861 (kv_vector[2*i] != empty_symbol))) {
1864 "* EQ key %d moved from %x to %x; index %d to %d\n",
1865 i, old_key, new_key, old_index, new_index));*/
1867 if (index_vector[old_index] != 0) {
1868 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
1870 /* Unlink the key from the old_index chain. */
1871 if (index_vector[old_index] == i) {
1872 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
1873 index_vector[old_index] = next_vector[i];
1874 /* Link it into the needing rehash chain. */
1875 next_vector[i] = fixnum_value(hash_table[11]);
1876 hash_table[11] = make_fixnum(i);
1879 unsigned prior = index_vector[old_index];
1880 unsigned next = next_vector[prior];
1882 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
1885 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
1888 next_vector[prior] = next_vector[next];
1889 /* Link it into the needing rehash
1892 fixnum_value(hash_table[11]);
1893 hash_table[11] = make_fixnum(next);
1898 next = next_vector[next];
1906 return (CEILING(kv_length + 2, 2));
1915 /* XX This is a hack adapted from cgc.c. These don't work too
1916 * efficiently with the gencgc as a list of the weak pointers is
1917 * maintained within the objects which causes writes to the pages. A
1918 * limited attempt is made to avoid unnecessary writes, but this needs
1920 #define WEAK_POINTER_NWORDS \
1921 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
1924 scav_weak_pointer(lispobj *where, lispobj object)
1926 struct weak_pointer *wp = weak_pointers;
1927 /* Push the weak pointer onto the list of weak pointers.
1928 * Do I have to watch for duplicates? Originally this was
1929 * part of trans_weak_pointer but that didn't work in the
1930 * case where the WP was in a promoted region.
1933 /* Check whether it's already in the list. */
1934 while (wp != NULL) {
1935 if (wp == (struct weak_pointer*)where) {
1941 /* Add it to the start of the list. */
1942 wp = (struct weak_pointer*)where;
1943 if (wp->next != weak_pointers) {
1944 wp->next = weak_pointers;
1946 /*SHOW("avoided write to weak pointer");*/
1951 /* Do not let GC scavenge the value slot of the weak pointer.
1952 * (That is why it is a weak pointer.) */
1954 return WEAK_POINTER_NWORDS;
1959 search_read_only_space(void *pointer)
1961 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1962 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1963 if ((pointer < (void *)start) || (pointer >= (void *)end))
1965 return (search_space(start,
1966 (((lispobj *)pointer)+2)-start,
1967 (lispobj *) pointer));
1971 search_static_space(void *pointer)
1973 lispobj *start = (lispobj *)STATIC_SPACE_START;
1974 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1975 if ((pointer < (void *)start) || (pointer >= (void *)end))
1977 return (search_space(start,
1978 (((lispobj *)pointer)+2)-start,
1979 (lispobj *) pointer));
1982 /* a faster version for searching the dynamic space. This will work even
1983 * if the object is in a current allocation region. */
1985 search_dynamic_space(void *pointer)
1987 int page_index = find_page_index(pointer);
1990 /* The address may be invalid, so do some checks. */
1991 if ((page_index == -1) ||
1992 (page_table[page_index].allocated == FREE_PAGE_FLAG))
1994 start = (lispobj *)((void *)page_address(page_index)
1995 + page_table[page_index].first_object_offset);
1996 return (search_space(start,
1997 (((lispobj *)pointer)+2)-start,
1998 (lispobj *)pointer));
2001 /* Is there any possibility that pointer is a valid Lisp object
2002 * reference, and/or something else (e.g. subroutine call return
2003 * address) which should prevent us from moving the referred-to thing?
2004 * This is called from preserve_pointers() */
2006 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2008 lispobj *start_addr;
2010 /* Find the object start address. */
2011 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2015 /* We need to allow raw pointers into Code objects for return
2016 * addresses. This will also pick up pointers to functions in code
2018 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
2019 /* XXX could do some further checks here */
2023 /* If it's not a return address then it needs to be a valid Lisp
2025 if (!is_lisp_pointer((lispobj)pointer)) {
2029 /* Check that the object pointed to is consistent with the pointer
2032 switch (lowtag_of((lispobj)pointer)) {
2033 case FUN_POINTER_LOWTAG:
2034 /* Start_addr should be the enclosing code object, or a closure
2036 switch (widetag_of(*start_addr)) {
2037 case CODE_HEADER_WIDETAG:
2038 /* This case is probably caught above. */
2040 case CLOSURE_HEADER_WIDETAG:
2041 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2042 if ((unsigned)pointer !=
2043 ((unsigned)start_addr+FUN_POINTER_LOWTAG)) {
2047 pointer, start_addr, *start_addr));
2055 pointer, start_addr, *start_addr));
2059 case LIST_POINTER_LOWTAG:
2060 if ((unsigned)pointer !=
2061 ((unsigned)start_addr+LIST_POINTER_LOWTAG)) {
2065 pointer, start_addr, *start_addr));
2068 /* Is it plausible cons? */
2069 if ((is_lisp_pointer(start_addr[0])
2070 || ((start_addr[0] & 3) == 0) /* fixnum */
2071 || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
2072 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2073 && (is_lisp_pointer(start_addr[1])
2074 || ((start_addr[1] & 3) == 0) /* fixnum */
2075 || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
2076 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2082 pointer, start_addr, *start_addr));
2085 case INSTANCE_POINTER_LOWTAG:
2086 if ((unsigned)pointer !=
2087 ((unsigned)start_addr+INSTANCE_POINTER_LOWTAG)) {
2091 pointer, start_addr, *start_addr));
2094 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2098 pointer, start_addr, *start_addr));
2102 case OTHER_POINTER_LOWTAG:
2103 if ((unsigned)pointer !=
2104 ((int)start_addr+OTHER_POINTER_LOWTAG)) {
2108 pointer, start_addr, *start_addr));
2111 /* Is it plausible? Not a cons. XXX should check the headers. */
2112 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2116 pointer, start_addr, *start_addr));
2119 switch (widetag_of(start_addr[0])) {
2120 case UNBOUND_MARKER_WIDETAG:
2121 case BASE_CHAR_WIDETAG:
2125 pointer, start_addr, *start_addr));
2128 /* only pointed to by function pointers? */
2129 case CLOSURE_HEADER_WIDETAG:
2130 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2134 pointer, start_addr, *start_addr));
2137 case INSTANCE_HEADER_WIDETAG:
2141 pointer, start_addr, *start_addr));
2144 /* the valid other immediate pointer objects */
2145 case SIMPLE_VECTOR_WIDETAG:
2147 case COMPLEX_WIDETAG:
2148 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2149 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2151 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2152 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2154 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2155 case COMPLEX_LONG_FLOAT_WIDETAG:
2157 case SIMPLE_ARRAY_WIDETAG:
2158 case COMPLEX_BASE_STRING_WIDETAG:
2159 case COMPLEX_VECTOR_NIL_WIDETAG:
2160 case COMPLEX_BIT_VECTOR_WIDETAG:
2161 case COMPLEX_VECTOR_WIDETAG:
2162 case COMPLEX_ARRAY_WIDETAG:
2163 case VALUE_CELL_HEADER_WIDETAG:
2164 case SYMBOL_HEADER_WIDETAG:
2166 case CODE_HEADER_WIDETAG:
2167 case BIGNUM_WIDETAG:
2168 case SINGLE_FLOAT_WIDETAG:
2169 case DOUBLE_FLOAT_WIDETAG:
2170 #ifdef LONG_FLOAT_WIDETAG
2171 case LONG_FLOAT_WIDETAG:
2173 case SIMPLE_BASE_STRING_WIDETAG:
2174 case SIMPLE_BIT_VECTOR_WIDETAG:
2175 case SIMPLE_ARRAY_NIL_WIDETAG:
2176 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2177 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2178 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2179 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2180 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2181 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2182 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2183 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2184 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2185 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2186 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2188 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2189 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2191 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2192 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2194 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2195 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2197 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2198 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2199 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2200 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2202 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2203 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2205 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2206 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2208 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2209 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2212 case WEAK_POINTER_WIDETAG:
2219 pointer, start_addr, *start_addr));
2227 pointer, start_addr, *start_addr));
2235 /* Adjust large bignum and vector objects. This will adjust the
2236 * allocated region if the size has shrunk, and move unboxed objects
2237 * into unboxed pages. The pages are not promoted here, and the
2238 * promoted region is not added to the new_regions; this is really
2239 * only designed to be called from preserve_pointer(). Shouldn't fail
2240 * if this is missed, just may delay the moving of objects to unboxed
2241 * pages, and the freeing of pages. */
2243 maybe_adjust_large_object(lispobj *where)
2248 int remaining_bytes;
2255 /* Check whether it's a vector or bignum object. */
2256 switch (widetag_of(where[0])) {
2257 case SIMPLE_VECTOR_WIDETAG:
2258 boxed = BOXED_PAGE_FLAG;
2260 case BIGNUM_WIDETAG:
2261 case SIMPLE_BASE_STRING_WIDETAG:
2262 case SIMPLE_BIT_VECTOR_WIDETAG:
2263 case SIMPLE_ARRAY_NIL_WIDETAG:
2264 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2265 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2266 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2267 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2268 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2269 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2270 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2271 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2272 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2273 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2274 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2276 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2277 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2279 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2280 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2282 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2283 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2285 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2286 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2287 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2288 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2290 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2291 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2293 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2294 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2296 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2297 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2299 boxed = UNBOXED_PAGE_FLAG;
2305 /* Find its current size. */
2306 nwords = (sizetab[widetag_of(where[0])])(where);
2308 first_page = find_page_index((void *)where);
2309 gc_assert(first_page >= 0);
2311 /* Note: Any page write-protection must be removed, else a later
2312 * scavenge_newspace may incorrectly not scavenge these pages.
2313 * This would not be necessary if they are added to the new areas,
2314 * but lets do it for them all (they'll probably be written
2317 gc_assert(page_table[first_page].first_object_offset == 0);
2319 next_page = first_page;
2320 remaining_bytes = nwords*4;
2321 while (remaining_bytes > PAGE_BYTES) {
2322 gc_assert(page_table[next_page].gen == from_space);
2323 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2324 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2325 gc_assert(page_table[next_page].large_object);
2326 gc_assert(page_table[next_page].first_object_offset ==
2327 -PAGE_BYTES*(next_page-first_page));
2328 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2330 page_table[next_page].allocated = boxed;
2332 /* Shouldn't be write-protected at this stage. Essential that the
2334 gc_assert(!page_table[next_page].write_protected);
2335 remaining_bytes -= PAGE_BYTES;
2339 /* Now only one page remains, but the object may have shrunk so
2340 * there may be more unused pages which will be freed. */
2342 /* Object may have shrunk but shouldn't have grown - check. */
2343 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2345 page_table[next_page].allocated = boxed;
2346 gc_assert(page_table[next_page].allocated ==
2347 page_table[first_page].allocated);
2349 /* Adjust the bytes_used. */
2350 old_bytes_used = page_table[next_page].bytes_used;
2351 page_table[next_page].bytes_used = remaining_bytes;
2353 bytes_freed = old_bytes_used - remaining_bytes;
2355 /* Free any remaining pages; needs care. */
2357 while ((old_bytes_used == PAGE_BYTES) &&
2358 (page_table[next_page].gen == from_space) &&
2359 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2360 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2361 page_table[next_page].large_object &&
2362 (page_table[next_page].first_object_offset ==
2363 -(next_page - first_page)*PAGE_BYTES)) {
2364 /* It checks out OK, free the page. We don't need to both zeroing
2365 * pages as this should have been done before shrinking the
2366 * object. These pages shouldn't be write protected as they
2367 * should be zero filled. */
2368 gc_assert(page_table[next_page].write_protected == 0);
2370 old_bytes_used = page_table[next_page].bytes_used;
2371 page_table[next_page].allocated = FREE_PAGE_FLAG;
2372 page_table[next_page].bytes_used = 0;
2373 bytes_freed += old_bytes_used;
2377 if ((bytes_freed > 0) && gencgc_verbose) {
2379 "/maybe_adjust_large_object() freed %d\n",
2383 generations[from_space].bytes_allocated -= bytes_freed;
2384 bytes_allocated -= bytes_freed;
2389 /* Take a possible pointer to a Lisp object and mark its page in the
2390 * page_table so that it will not be relocated during a GC.
2392 * This involves locating the page it points to, then backing up to
2393 * the start of its region, then marking all pages dont_move from there
2394 * up to the first page that's not full or has a different generation
2396 * It is assumed that all the page static flags have been cleared at
2397 * the start of a GC.
2399 * It is also assumed that the current gc_alloc() region has been
2400 * flushed and the tables updated. */
2402 preserve_pointer(void *addr)
2404 int addr_page_index = find_page_index(addr);
2407 unsigned region_allocation;
2409 /* quick check 1: Address is quite likely to have been invalid. */
2410 if ((addr_page_index == -1)
2411 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2412 || (page_table[addr_page_index].bytes_used == 0)
2413 || (page_table[addr_page_index].gen != from_space)
2414 /* Skip if already marked dont_move. */
2415 || (page_table[addr_page_index].dont_move != 0))
2417 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2418 /* (Now that we know that addr_page_index is in range, it's
2419 * safe to index into page_table[] with it.) */
2420 region_allocation = page_table[addr_page_index].allocated;
2422 /* quick check 2: Check the offset within the page.
2425 if (((unsigned)addr & (PAGE_BYTES - 1)) > page_table[addr_page_index].bytes_used)
2428 /* Filter out anything which can't be a pointer to a Lisp object
2429 * (or, as a special case which also requires dont_move, a return
2430 * address referring to something in a CodeObject). This is
2431 * expensive but important, since it vastly reduces the
2432 * probability that random garbage will be bogusly interpreted as
2433 * a pointer which prevents a page from moving. */
2434 if (!(possibly_valid_dynamic_space_pointer(addr)))
2437 /* Find the beginning of the region. Note that there may be
2438 * objects in the region preceding the one that we were passed a
2439 * pointer to: if this is the case, we will write-protect all the
2440 * previous objects' pages too. */
2443 /* I think this'd work just as well, but without the assertions.
2444 * -dan 2004.01.01 */
2446 find_page_index(page_address(addr_page_index)+
2447 page_table[addr_page_index].first_object_offset);
2449 first_page = addr_page_index;
2450 while (page_table[first_page].first_object_offset != 0) {
2452 /* Do some checks. */
2453 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2454 gc_assert(page_table[first_page].gen == from_space);
2455 gc_assert(page_table[first_page].allocated == region_allocation);
2459 /* Adjust any large objects before promotion as they won't be
2460 * copied after promotion. */
2461 if (page_table[first_page].large_object) {
2462 maybe_adjust_large_object(page_address(first_page));
2463 /* If a large object has shrunk then addr may now point to a
2464 * free area in which case it's ignored here. Note it gets
2465 * through the valid pointer test above because the tail looks
2467 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2468 || (page_table[addr_page_index].bytes_used == 0)
2469 /* Check the offset within the page. */
2470 || (((unsigned)addr & (PAGE_BYTES - 1))
2471 > page_table[addr_page_index].bytes_used)) {
2473 "weird? ignore ptr 0x%x to freed area of large object\n",
2477 /* It may have moved to unboxed pages. */
2478 region_allocation = page_table[first_page].allocated;
2481 /* Now work forward until the end of this contiguous area is found,
2482 * marking all pages as dont_move. */
2483 for (i = first_page; ;i++) {
2484 gc_assert(page_table[i].allocated == region_allocation);
2486 /* Mark the page static. */
2487 page_table[i].dont_move = 1;
2489 /* Move the page to the new_space. XX I'd rather not do this
2490 * but the GC logic is not quite able to copy with the static
2491 * pages remaining in the from space. This also requires the
2492 * generation bytes_allocated counters be updated. */
2493 page_table[i].gen = new_space;
2494 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2495 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2497 /* It is essential that the pages are not write protected as
2498 * they may have pointers into the old-space which need
2499 * scavenging. They shouldn't be write protected at this
2501 gc_assert(!page_table[i].write_protected);
2503 /* Check whether this is the last page in this contiguous block.. */
2504 if ((page_table[i].bytes_used < PAGE_BYTES)
2505 /* ..or it is PAGE_BYTES and is the last in the block */
2506 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2507 || (page_table[i+1].bytes_used == 0) /* next page free */
2508 || (page_table[i+1].gen != from_space) /* diff. gen */
2509 || (page_table[i+1].first_object_offset == 0))
2513 /* Check that the page is now static. */
2514 gc_assert(page_table[addr_page_index].dont_move != 0);
2517 /* If the given page is not write-protected, then scan it for pointers
2518 * to younger generations or the top temp. generation, if no
2519 * suspicious pointers are found then the page is write-protected.
2521 * Care is taken to check for pointers to the current gc_alloc()
2522 * region if it is a younger generation or the temp. generation. This
2523 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2524 * the gc_alloc_generation does not need to be checked as this is only
2525 * called from scavenge_generation() when the gc_alloc generation is
2526 * younger, so it just checks if there is a pointer to the current
2529 * We return 1 if the page was write-protected, else 0. */
2531 update_page_write_prot(int page)
2533 int gen = page_table[page].gen;
2536 void **page_addr = (void **)page_address(page);
2537 int num_words = page_table[page].bytes_used / 4;
2539 /* Shouldn't be a free page. */
2540 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2541 gc_assert(page_table[page].bytes_used != 0);
2543 /* Skip if it's already write-protected, pinned, or unboxed */
2544 if (page_table[page].write_protected
2545 || page_table[page].dont_move
2546 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2549 /* Scan the page for pointers to younger generations or the
2550 * top temp. generation. */
2552 for (j = 0; j < num_words; j++) {
2553 void *ptr = *(page_addr+j);
2554 int index = find_page_index(ptr);
2556 /* Check that it's in the dynamic space */
2558 if (/* Does it point to a younger or the temp. generation? */
2559 ((page_table[index].allocated != FREE_PAGE_FLAG)
2560 && (page_table[index].bytes_used != 0)
2561 && ((page_table[index].gen < gen)
2562 || (page_table[index].gen == NUM_GENERATIONS)))
2564 /* Or does it point within a current gc_alloc() region? */
2565 || ((boxed_region.start_addr <= ptr)
2566 && (ptr <= boxed_region.free_pointer))
2567 || ((unboxed_region.start_addr <= ptr)
2568 && (ptr <= unboxed_region.free_pointer))) {
2575 /* Write-protect the page. */
2576 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2578 os_protect((void *)page_addr,
2580 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2582 /* Note the page as protected in the page tables. */
2583 page_table[page].write_protected = 1;
2589 /* Scavenge a generation.
2591 * This will not resolve all pointers when generation is the new
2592 * space, as new objects may be added which are not checked here - use
2593 * scavenge_newspace generation.
2595 * Write-protected pages should not have any pointers to the
2596 * from_space so do need scavenging; thus write-protected pages are
2597 * not always scavenged. There is some code to check that these pages
2598 * are not written; but to check fully the write-protected pages need
2599 * to be scavenged by disabling the code to skip them.
2601 * Under the current scheme when a generation is GCed the younger
2602 * generations will be empty. So, when a generation is being GCed it
2603 * is only necessary to scavenge the older generations for pointers
2604 * not the younger. So a page that does not have pointers to younger
2605 * generations does not need to be scavenged.
2607 * The write-protection can be used to note pages that don't have
2608 * pointers to younger pages. But pages can be written without having
2609 * pointers to younger generations. After the pages are scavenged here
2610 * they can be scanned for pointers to younger generations and if
2611 * there are none the page can be write-protected.
2613 * One complication is when the newspace is the top temp. generation.
2615 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2616 * that none were written, which they shouldn't be as they should have
2617 * no pointers to younger generations. This breaks down for weak
2618 * pointers as the objects contain a link to the next and are written
2619 * if a weak pointer is scavenged. Still it's a useful check. */
2621 scavenge_generation(int generation)
2628 /* Clear the write_protected_cleared flags on all pages. */
2629 for (i = 0; i < NUM_PAGES; i++)
2630 page_table[i].write_protected_cleared = 0;
2633 for (i = 0; i < last_free_page; i++) {
2634 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2635 && (page_table[i].bytes_used != 0)
2636 && (page_table[i].gen == generation)) {
2638 int write_protected=1;
2640 /* This should be the start of a region */
2641 gc_assert(page_table[i].first_object_offset == 0);
2643 /* Now work forward until the end of the region */
2644 for (last_page = i; ; last_page++) {
2646 write_protected && page_table[last_page].write_protected;
2647 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2648 /* Or it is PAGE_BYTES and is the last in the block */
2649 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2650 || (page_table[last_page+1].bytes_used == 0)
2651 || (page_table[last_page+1].gen != generation)
2652 || (page_table[last_page+1].first_object_offset == 0))
2655 if (!write_protected) {
2656 scavenge(page_address(i), (page_table[last_page].bytes_used
2657 + (last_page-i)*PAGE_BYTES)/4);
2659 /* Now scan the pages and write protect those that
2660 * don't have pointers to younger generations. */
2661 if (enable_page_protection) {
2662 for (j = i; j <= last_page; j++) {
2663 num_wp += update_page_write_prot(j);
2670 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2672 "/write protected %d pages within generation %d\n",
2673 num_wp, generation));
2677 /* Check that none of the write_protected pages in this generation
2678 * have been written to. */
2679 for (i = 0; i < NUM_PAGES; i++) {
2680 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2681 && (page_table[i].bytes_used != 0)
2682 && (page_table[i].gen == generation)
2683 && (page_table[i].write_protected_cleared != 0)) {
2684 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2686 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2687 page_table[i].bytes_used,
2688 page_table[i].first_object_offset,
2689 page_table[i].dont_move));
2690 lose("write to protected page %d in scavenge_generation()", i);
2697 /* Scavenge a newspace generation. As it is scavenged new objects may
2698 * be allocated to it; these will also need to be scavenged. This
2699 * repeats until there are no more objects unscavenged in the
2700 * newspace generation.
2702 * To help improve the efficiency, areas written are recorded by
2703 * gc_alloc() and only these scavenged. Sometimes a little more will be
2704 * scavenged, but this causes no harm. An easy check is done that the
2705 * scavenged bytes equals the number allocated in the previous
2708 * Write-protected pages are not scanned except if they are marked
2709 * dont_move in which case they may have been promoted and still have
2710 * pointers to the from space.
2712 * Write-protected pages could potentially be written by alloc however
2713 * to avoid having to handle re-scavenging of write-protected pages
2714 * gc_alloc() does not write to write-protected pages.
2716 * New areas of objects allocated are recorded alternatively in the two
2717 * new_areas arrays below. */
2718 static struct new_area new_areas_1[NUM_NEW_AREAS];
2719 static struct new_area new_areas_2[NUM_NEW_AREAS];
2721 /* Do one full scan of the new space generation. This is not enough to
2722 * complete the job as new objects may be added to the generation in
2723 * the process which are not scavenged. */
2725 scavenge_newspace_generation_one_scan(int generation)
2730 "/starting one full scan of newspace generation %d\n",
2732 for (i = 0; i < last_free_page; i++) {
2733 /* Note that this skips over open regions when it encounters them. */
2734 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2735 && (page_table[i].bytes_used != 0)
2736 && (page_table[i].gen == generation)
2737 && ((page_table[i].write_protected == 0)
2738 /* (This may be redundant as write_protected is now
2739 * cleared before promotion.) */
2740 || (page_table[i].dont_move == 1))) {
2744 /* The scavenge will start at the first_object_offset of page i.
2746 * We need to find the full extent of this contiguous
2747 * block in case objects span pages.
2749 * Now work forward until the end of this contiguous area
2750 * is found. A small area is preferred as there is a
2751 * better chance of its pages being write-protected. */
2752 for (last_page = i; ;last_page++) {
2753 /* If all pages are write-protected and movable,
2754 * then no need to scavenge */
2755 all_wp=all_wp && page_table[last_page].write_protected &&
2756 !page_table[last_page].dont_move;
2758 /* Check whether this is the last page in this
2759 * contiguous block */
2760 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2761 /* Or it is PAGE_BYTES and is the last in the block */
2762 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2763 || (page_table[last_page+1].bytes_used == 0)
2764 || (page_table[last_page+1].gen != generation)
2765 || (page_table[last_page+1].first_object_offset == 0))
2769 /* Do a limited check for write-protected pages. */
2773 size = (page_table[last_page].bytes_used
2774 + (last_page-i)*PAGE_BYTES
2775 - page_table[i].first_object_offset)/4;
2776 new_areas_ignore_page = last_page;
2778 scavenge(page_address(i) +
2779 page_table[i].first_object_offset,
2787 "/done with one full scan of newspace generation %d\n",
2791 /* Do a complete scavenge of the newspace generation. */
2793 scavenge_newspace_generation(int generation)
2797 /* the new_areas array currently being written to by gc_alloc() */
2798 struct new_area (*current_new_areas)[] = &new_areas_1;
2799 int current_new_areas_index;
2801 /* the new_areas created by the previous scavenge cycle */
2802 struct new_area (*previous_new_areas)[] = NULL;
2803 int previous_new_areas_index;
2805 /* Flush the current regions updating the tables. */
2806 gc_alloc_update_all_page_tables();
2808 /* Turn on the recording of new areas by gc_alloc(). */
2809 new_areas = current_new_areas;
2810 new_areas_index = 0;
2812 /* Don't need to record new areas that get scavenged anyway during
2813 * scavenge_newspace_generation_one_scan. */
2814 record_new_objects = 1;
2816 /* Start with a full scavenge. */
2817 scavenge_newspace_generation_one_scan(generation);
2819 /* Record all new areas now. */
2820 record_new_objects = 2;
2822 /* Flush the current regions updating the tables. */
2823 gc_alloc_update_all_page_tables();
2825 /* Grab new_areas_index. */
2826 current_new_areas_index = new_areas_index;
2829 "The first scan is finished; current_new_areas_index=%d.\n",
2830 current_new_areas_index));*/
2832 while (current_new_areas_index > 0) {
2833 /* Move the current to the previous new areas */
2834 previous_new_areas = current_new_areas;
2835 previous_new_areas_index = current_new_areas_index;
2837 /* Scavenge all the areas in previous new areas. Any new areas
2838 * allocated are saved in current_new_areas. */
2840 /* Allocate an array for current_new_areas; alternating between
2841 * new_areas_1 and 2 */
2842 if (previous_new_areas == &new_areas_1)
2843 current_new_areas = &new_areas_2;
2845 current_new_areas = &new_areas_1;
2847 /* Set up for gc_alloc(). */
2848 new_areas = current_new_areas;
2849 new_areas_index = 0;
2851 /* Check whether previous_new_areas had overflowed. */
2852 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2854 /* New areas of objects allocated have been lost so need to do a
2855 * full scan to be sure! If this becomes a problem try
2856 * increasing NUM_NEW_AREAS. */
2858 SHOW("new_areas overflow, doing full scavenge");
2860 /* Don't need to record new areas that get scavenge anyway
2861 * during scavenge_newspace_generation_one_scan. */
2862 record_new_objects = 1;
2864 scavenge_newspace_generation_one_scan(generation);
2866 /* Record all new areas now. */
2867 record_new_objects = 2;
2869 /* Flush the current regions updating the tables. */
2870 gc_alloc_update_all_page_tables();
2874 /* Work through previous_new_areas. */
2875 for (i = 0; i < previous_new_areas_index; i++) {
2876 /* FIXME: All these bare *4 and /4 should be something
2877 * like BYTES_PER_WORD or WBYTES. */
2878 int page = (*previous_new_areas)[i].page;
2879 int offset = (*previous_new_areas)[i].offset;
2880 int size = (*previous_new_areas)[i].size / 4;
2881 gc_assert((*previous_new_areas)[i].size % 4 == 0);
2882 scavenge(page_address(page)+offset, size);
2885 /* Flush the current regions updating the tables. */
2886 gc_alloc_update_all_page_tables();
2889 current_new_areas_index = new_areas_index;
2892 "The re-scan has finished; current_new_areas_index=%d.\n",
2893 current_new_areas_index));*/
2896 /* Turn off recording of areas allocated by gc_alloc(). */
2897 record_new_objects = 0;
2900 /* Check that none of the write_protected pages in this generation
2901 * have been written to. */
2902 for (i = 0; i < NUM_PAGES; i++) {
2903 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2904 && (page_table[i].bytes_used != 0)
2905 && (page_table[i].gen == generation)
2906 && (page_table[i].write_protected_cleared != 0)
2907 && (page_table[i].dont_move == 0)) {
2908 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
2909 i, generation, page_table[i].dont_move);
2915 /* Un-write-protect all the pages in from_space. This is done at the
2916 * start of a GC else there may be many page faults while scavenging
2917 * the newspace (I've seen drive the system time to 99%). These pages
2918 * would need to be unprotected anyway before unmapping in
2919 * free_oldspace; not sure what effect this has on paging.. */
2921 unprotect_oldspace(void)
2925 for (i = 0; i < last_free_page; i++) {
2926 if ((page_table[i].allocated != FREE_PAGE_FLAG)
2927 && (page_table[i].bytes_used != 0)
2928 && (page_table[i].gen == from_space)) {
2931 page_start = (void *)page_address(i);
2933 /* Remove any write-protection. We should be able to rely
2934 * on the write-protect flag to avoid redundant calls. */
2935 if (page_table[i].write_protected) {
2936 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
2937 page_table[i].write_protected = 0;
2943 /* Work through all the pages and free any in from_space. This
2944 * assumes that all objects have been copied or promoted to an older
2945 * generation. Bytes_allocated and the generation bytes_allocated
2946 * counter are updated. The number of bytes freed is returned. */
2950 int bytes_freed = 0;
2951 int first_page, last_page;
2956 /* Find a first page for the next region of pages. */
2957 while ((first_page < last_free_page)
2958 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
2959 || (page_table[first_page].bytes_used == 0)
2960 || (page_table[first_page].gen != from_space)))
2963 if (first_page >= last_free_page)
2966 /* Find the last page of this region. */
2967 last_page = first_page;
2970 /* Free the page. */
2971 bytes_freed += page_table[last_page].bytes_used;
2972 generations[page_table[last_page].gen].bytes_allocated -=
2973 page_table[last_page].bytes_used;
2974 page_table[last_page].allocated = FREE_PAGE_FLAG;
2975 page_table[last_page].bytes_used = 0;
2977 /* Remove any write-protection. We should be able to rely
2978 * on the write-protect flag to avoid redundant calls. */
2980 void *page_start = (void *)page_address(last_page);
2982 if (page_table[last_page].write_protected) {
2983 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
2984 page_table[last_page].write_protected = 0;
2989 while ((last_page < last_free_page)
2990 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
2991 && (page_table[last_page].bytes_used != 0)
2992 && (page_table[last_page].gen == from_space));
2994 /* Zero pages from first_page to (last_page-1).
2996 * FIXME: Why not use os_zero(..) function instead of
2997 * hand-coding this again? (Check other gencgc_unmap_zero
2999 if (gencgc_unmap_zero) {
3000 void *page_start, *addr;
3002 page_start = (void *)page_address(first_page);
3004 os_invalidate(page_start, PAGE_BYTES*(last_page-first_page));
3005 addr = os_validate(page_start, PAGE_BYTES*(last_page-first_page));
3006 if (addr == NULL || addr != page_start) {
3007 lose("free_oldspace: page moved, 0x%08x ==> 0x%08x",page_start,
3013 page_start = (int *)page_address(first_page);
3014 memset(page_start, 0,PAGE_BYTES*(last_page-first_page));
3017 first_page = last_page;
3019 } while (first_page < last_free_page);
3021 bytes_allocated -= bytes_freed;
3026 /* Print some information about a pointer at the given address. */
3028 print_ptr(lispobj *addr)
3030 /* If addr is in the dynamic space then out the page information. */
3031 int pi1 = find_page_index((void*)addr);
3034 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3035 (unsigned int) addr,
3037 page_table[pi1].allocated,
3038 page_table[pi1].gen,
3039 page_table[pi1].bytes_used,
3040 page_table[pi1].first_object_offset,
3041 page_table[pi1].dont_move);
3042 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3055 extern int undefined_tramp;
3058 verify_space(lispobj *start, size_t words)
3060 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3061 int is_in_readonly_space =
3062 (READ_ONLY_SPACE_START <= (unsigned)start &&
3063 (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3067 lispobj thing = *(lispobj*)start;
3069 if (is_lisp_pointer(thing)) {
3070 int page_index = find_page_index((void*)thing);
3071 int to_readonly_space =
3072 (READ_ONLY_SPACE_START <= thing &&
3073 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3074 int to_static_space =
3075 (STATIC_SPACE_START <= thing &&
3076 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3078 /* Does it point to the dynamic space? */
3079 if (page_index != -1) {
3080 /* If it's within the dynamic space it should point to a used
3081 * page. XX Could check the offset too. */
3082 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3083 && (page_table[page_index].bytes_used == 0))
3084 lose ("Ptr %x @ %x sees free page.", thing, start);
3085 /* Check that it doesn't point to a forwarding pointer! */
3086 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3087 lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
3089 /* Check that its not in the RO space as it would then be a
3090 * pointer from the RO to the dynamic space. */
3091 if (is_in_readonly_space) {
3092 lose("ptr to dynamic space %x from RO space %x",
3095 /* Does it point to a plausible object? This check slows
3096 * it down a lot (so it's commented out).
3098 * "a lot" is serious: it ate 50 minutes cpu time on
3099 * my duron 950 before I came back from lunch and
3102 * FIXME: Add a variable to enable this
3105 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3106 lose("ptr %x to invalid object %x", thing, start);
3110 /* Verify that it points to another valid space. */
3111 if (!to_readonly_space && !to_static_space
3112 && (thing != (unsigned)&undefined_tramp)) {
3113 lose("Ptr %x @ %x sees junk.", thing, start);
3117 if (!(fixnump(thing))) {
3119 switch(widetag_of(*start)) {
3122 case SIMPLE_VECTOR_WIDETAG:
3124 case COMPLEX_WIDETAG:
3125 case SIMPLE_ARRAY_WIDETAG:
3126 case COMPLEX_BASE_STRING_WIDETAG:
3127 case COMPLEX_VECTOR_NIL_WIDETAG:
3128 case COMPLEX_BIT_VECTOR_WIDETAG:
3129 case COMPLEX_VECTOR_WIDETAG:
3130 case COMPLEX_ARRAY_WIDETAG:
3131 case CLOSURE_HEADER_WIDETAG:
3132 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3133 case VALUE_CELL_HEADER_WIDETAG:
3134 case SYMBOL_HEADER_WIDETAG:
3135 case BASE_CHAR_WIDETAG:
3136 case UNBOUND_MARKER_WIDETAG:
3137 case INSTANCE_HEADER_WIDETAG:
3142 case CODE_HEADER_WIDETAG:
3144 lispobj object = *start;
3146 int nheader_words, ncode_words, nwords;
3148 struct simple_fun *fheaderp;
3150 code = (struct code *) start;
3152 /* Check that it's not in the dynamic space.
3153 * FIXME: Isn't is supposed to be OK for code
3154 * objects to be in the dynamic space these days? */
3155 if (is_in_dynamic_space
3156 /* It's ok if it's byte compiled code. The trace
3157 * table offset will be a fixnum if it's x86
3158 * compiled code - check.
3160 * FIXME: #^#@@! lack of abstraction here..
3161 * This line can probably go away now that
3162 * there's no byte compiler, but I've got
3163 * too much to worry about right now to try
3164 * to make sure. -- WHN 2001-10-06 */
3165 && fixnump(code->trace_table_offset)
3166 /* Only when enabled */
3167 && verify_dynamic_code_check) {
3169 "/code object at %x in the dynamic space\n",
3173 ncode_words = fixnum_value(code->code_size);
3174 nheader_words = HeaderValue(object);
3175 nwords = ncode_words + nheader_words;
3176 nwords = CEILING(nwords, 2);
3177 /* Scavenge the boxed section of the code data block */
3178 verify_space(start + 1, nheader_words - 1);
3180 /* Scavenge the boxed section of each function
3181 * object in the code data block. */
3182 fheaderl = code->entry_points;
3183 while (fheaderl != NIL) {
3185 (struct simple_fun *) native_pointer(fheaderl);
3186 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3187 verify_space(&fheaderp->name, 1);
3188 verify_space(&fheaderp->arglist, 1);
3189 verify_space(&fheaderp->type, 1);
3190 fheaderl = fheaderp->next;
3196 /* unboxed objects */
3197 case BIGNUM_WIDETAG:
3198 case SINGLE_FLOAT_WIDETAG:
3199 case DOUBLE_FLOAT_WIDETAG:
3200 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3201 case LONG_FLOAT_WIDETAG:
3203 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3204 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3206 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3207 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3209 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3210 case COMPLEX_LONG_FLOAT_WIDETAG:
3212 case SIMPLE_BASE_STRING_WIDETAG:
3213 case SIMPLE_BIT_VECTOR_WIDETAG:
3214 case SIMPLE_ARRAY_NIL_WIDETAG:
3215 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3216 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3217 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3218 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3219 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3220 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3221 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3222 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3223 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3224 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3225 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3227 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3228 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3230 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3231 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3233 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3234 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3236 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3237 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3238 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3239 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3241 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3242 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3244 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3245 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3247 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3248 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3251 case WEAK_POINTER_WIDETAG:
3252 count = (sizetab[widetag_of(*start)])(start);
3268 /* FIXME: It would be nice to make names consistent so that
3269 * foo_size meant size *in* *bytes* instead of size in some
3270 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3271 * Some counts of lispobjs are called foo_count; it might be good
3272 * to grep for all foo_size and rename the appropriate ones to
3274 int read_only_space_size =
3275 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3276 - (lispobj*)READ_ONLY_SPACE_START;
3277 int static_space_size =
3278 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3279 - (lispobj*)STATIC_SPACE_START;
3281 for_each_thread(th) {
3282 int binding_stack_size =
3283 (lispobj*)SymbolValue(BINDING_STACK_POINTER,th)
3284 - (lispobj*)th->binding_stack_start;
3285 verify_space(th->binding_stack_start, binding_stack_size);
3287 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3288 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3292 verify_generation(int generation)
3296 for (i = 0; i < last_free_page; i++) {
3297 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3298 && (page_table[i].bytes_used != 0)
3299 && (page_table[i].gen == generation)) {
3301 int region_allocation = page_table[i].allocated;
3303 /* This should be the start of a contiguous block */
3304 gc_assert(page_table[i].first_object_offset == 0);
3306 /* Need to find the full extent of this contiguous block in case
3307 objects span pages. */
3309 /* Now work forward until the end of this contiguous area is
3311 for (last_page = i; ;last_page++)
3312 /* Check whether this is the last page in this contiguous
3314 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3315 /* Or it is PAGE_BYTES and is the last in the block */
3316 || (page_table[last_page+1].allocated != region_allocation)
3317 || (page_table[last_page+1].bytes_used == 0)
3318 || (page_table[last_page+1].gen != generation)
3319 || (page_table[last_page+1].first_object_offset == 0))
3322 verify_space(page_address(i), (page_table[last_page].bytes_used
3323 + (last_page-i)*PAGE_BYTES)/4);
3329 /* Check that all the free space is zero filled. */
3331 verify_zero_fill(void)
3335 for (page = 0; page < last_free_page; page++) {
3336 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3337 /* The whole page should be zero filled. */
3338 int *start_addr = (int *)page_address(page);
3341 for (i = 0; i < size; i++) {
3342 if (start_addr[i] != 0) {
3343 lose("free page not zero at %x", start_addr + i);
3347 int free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3348 if (free_bytes > 0) {
3349 int *start_addr = (int *)((unsigned)page_address(page)
3350 + page_table[page].bytes_used);
3351 int size = free_bytes / 4;
3353 for (i = 0; i < size; i++) {
3354 if (start_addr[i] != 0) {
3355 lose("free region not zero at %x", start_addr + i);
3363 /* External entry point for verify_zero_fill */
3365 gencgc_verify_zero_fill(void)
3367 /* Flush the alloc regions updating the tables. */
3368 gc_alloc_update_all_page_tables();
3369 SHOW("verifying zero fill");
3374 verify_dynamic_space(void)
3378 for (i = 0; i < NUM_GENERATIONS; i++)
3379 verify_generation(i);
3381 if (gencgc_enable_verify_zero_fill)
3385 /* Write-protect all the dynamic boxed pages in the given generation. */
3387 write_protect_generation_pages(int generation)
3391 gc_assert(generation < NUM_GENERATIONS);
3393 for (i = 0; i < last_free_page; i++)
3394 if ((page_table[i].allocated == BOXED_PAGE_FLAG)
3395 && (page_table[i].bytes_used != 0)
3396 && !page_table[i].dont_move
3397 && (page_table[i].gen == generation)) {
3400 page_start = (void *)page_address(i);
3402 os_protect(page_start,
3404 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3406 /* Note the page as protected in the page tables. */
3407 page_table[i].write_protected = 1;
3410 if (gencgc_verbose > 1) {
3412 "/write protected %d of %d pages in generation %d\n",
3413 count_write_protect_generation_pages(generation),
3414 count_generation_pages(generation),
3419 /* Garbage collect a generation. If raise is 0 then the remains of the
3420 * generation are not raised to the next generation. */
3422 garbage_collect_generation(int generation, int raise)
3424 unsigned long bytes_freed;
3426 unsigned long static_space_size;
3428 gc_assert(generation <= (NUM_GENERATIONS-1));
3430 /* The oldest generation can't be raised. */
3431 gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
3433 /* Initialize the weak pointer list. */
3434 weak_pointers = NULL;
3436 /* When a generation is not being raised it is transported to a
3437 * temporary generation (NUM_GENERATIONS), and lowered when
3438 * done. Set up this new generation. There should be no pages
3439 * allocated to it yet. */
3441 gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
3443 /* Set the global src and dest. generations */
3444 from_space = generation;
3446 new_space = generation+1;
3448 new_space = NUM_GENERATIONS;
3450 /* Change to a new space for allocation, resetting the alloc_start_page */
3451 gc_alloc_generation = new_space;
3452 generations[new_space].alloc_start_page = 0;
3453 generations[new_space].alloc_unboxed_start_page = 0;
3454 generations[new_space].alloc_large_start_page = 0;
3455 generations[new_space].alloc_large_unboxed_start_page = 0;
3457 /* Before any pointers are preserved, the dont_move flags on the
3458 * pages need to be cleared. */
3459 for (i = 0; i < last_free_page; i++)
3460 if(page_table[i].gen==from_space)
3461 page_table[i].dont_move = 0;
3463 /* Un-write-protect the old-space pages. This is essential for the
3464 * promoted pages as they may contain pointers into the old-space
3465 * which need to be scavenged. It also helps avoid unnecessary page
3466 * faults as forwarding pointers are written into them. They need to
3467 * be un-protected anyway before unmapping later. */
3468 unprotect_oldspace();
3470 /* Scavenge the stacks' conservative roots. */
3472 /* there are potentially two stacks for each thread: the main
3473 * stack, which may contain Lisp pointers, and the alternate stack.
3474 * We don't ever run Lisp code on the altstack, but it may
3475 * host a sigcontext with lisp objects in it */
3477 /* what we need to do: (1) find the stack pointer for the main
3478 * stack; scavenge it (2) find the interrupt context on the
3479 * alternate stack that might contain lisp values, and scavenge
3482 /* we assume that none of the preceding applies to the thread that
3483 * initiates GC. If you ever call GC from inside an altstack
3484 * handler, you will lose. */
3485 for_each_thread(th) {
3487 void **esp=(void **)-1;
3488 #ifdef LISP_FEATURE_SB_THREAD
3490 if(th==arch_os_get_current_thread()) {
3491 esp = (void **) &raise;
3494 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3495 for(i=free-1;i>=0;i--) {
3496 os_context_t *c=th->interrupt_contexts[i];
3497 esp1 = (void **) *os_context_register_addr(c,reg_ESP);
3498 if(esp1>=th->control_stack_start&& esp1<th->control_stack_end){
3499 if(esp1<esp) esp=esp1;
3500 for(ptr = (void **)(c+1); ptr>=(void **)c; ptr--) {
3501 preserve_pointer(*ptr);
3507 esp = (void **) &raise;
3509 for (ptr = (void **)th->control_stack_end; ptr > esp; ptr--) {
3510 preserve_pointer(*ptr);
3515 if (gencgc_verbose > 1) {
3516 int num_dont_move_pages = count_dont_move_pages();
3518 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
3519 num_dont_move_pages,
3520 num_dont_move_pages * PAGE_BYTES);
3524 /* Scavenge all the rest of the roots. */
3526 /* Scavenge the Lisp functions of the interrupt handlers, taking
3527 * care to avoid SIG_DFL and SIG_IGN. */
3528 for_each_thread(th) {
3529 struct interrupt_data *data=th->interrupt_data;
3530 for (i = 0; i < NSIG; i++) {
3531 union interrupt_handler handler = data->interrupt_handlers[i];
3532 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3533 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3534 scavenge((lispobj *)(data->interrupt_handlers + i), 1);
3538 /* Scavenge the binding stacks. */
3541 for_each_thread(th) {
3542 long len= (lispobj *)SymbolValue(BINDING_STACK_POINTER,th) -
3543 th->binding_stack_start;
3544 scavenge((lispobj *) th->binding_stack_start,len);
3545 #ifdef LISP_FEATURE_SB_THREAD
3546 /* do the tls as well */
3547 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
3548 (sizeof (struct thread))/(sizeof (lispobj));
3549 scavenge((lispobj *) (th+1),len);
3554 /* The original CMU CL code had scavenge-read-only-space code
3555 * controlled by the Lisp-level variable
3556 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
3557 * wasn't documented under what circumstances it was useful or
3558 * safe to turn it on, so it's been turned off in SBCL. If you
3559 * want/need this functionality, and can test and document it,
3560 * please submit a patch. */
3562 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
3563 unsigned long read_only_space_size =
3564 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
3565 (lispobj*)READ_ONLY_SPACE_START;
3567 "/scavenge read only space: %d bytes\n",
3568 read_only_space_size * sizeof(lispobj)));
3569 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
3573 /* Scavenge static space. */
3575 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
3576 (lispobj *)STATIC_SPACE_START;
3577 if (gencgc_verbose > 1) {
3579 "/scavenge static space: %d bytes\n",
3580 static_space_size * sizeof(lispobj)));
3582 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
3584 /* All generations but the generation being GCed need to be
3585 * scavenged. The new_space generation needs special handling as
3586 * objects may be moved in - it is handled separately below. */
3587 for (i = 0; i < NUM_GENERATIONS; i++) {
3588 if ((i != generation) && (i != new_space)) {
3589 scavenge_generation(i);
3593 /* Finally scavenge the new_space generation. Keep going until no
3594 * more objects are moved into the new generation */
3595 scavenge_newspace_generation(new_space);
3597 /* FIXME: I tried reenabling this check when debugging unrelated
3598 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3599 * Since the current GC code seems to work well, I'm guessing that
3600 * this debugging code is just stale, but I haven't tried to
3601 * figure it out. It should be figured out and then either made to
3602 * work or just deleted. */
3603 #define RESCAN_CHECK 0
3605 /* As a check re-scavenge the newspace once; no new objects should
3608 int old_bytes_allocated = bytes_allocated;
3609 int bytes_allocated;
3611 /* Start with a full scavenge. */
3612 scavenge_newspace_generation_one_scan(new_space);
3614 /* Flush the current regions, updating the tables. */
3615 gc_alloc_update_all_page_tables();
3617 bytes_allocated = bytes_allocated - old_bytes_allocated;
3619 if (bytes_allocated != 0) {
3620 lose("Rescan of new_space allocated %d more bytes.",
3626 scan_weak_pointers();
3628 /* Flush the current regions, updating the tables. */
3629 gc_alloc_update_all_page_tables();
3631 /* Free the pages in oldspace, but not those marked dont_move. */
3632 bytes_freed = free_oldspace();
3634 /* If the GC is not raising the age then lower the generation back
3635 * to its normal generation number */
3637 for (i = 0; i < last_free_page; i++)
3638 if ((page_table[i].bytes_used != 0)
3639 && (page_table[i].gen == NUM_GENERATIONS))
3640 page_table[i].gen = generation;
3641 gc_assert(generations[generation].bytes_allocated == 0);
3642 generations[generation].bytes_allocated =
3643 generations[NUM_GENERATIONS].bytes_allocated;
3644 generations[NUM_GENERATIONS].bytes_allocated = 0;
3647 /* Reset the alloc_start_page for generation. */
3648 generations[generation].alloc_start_page = 0;
3649 generations[generation].alloc_unboxed_start_page = 0;
3650 generations[generation].alloc_large_start_page = 0;
3651 generations[generation].alloc_large_unboxed_start_page = 0;
3653 if (generation >= verify_gens) {
3657 verify_dynamic_space();
3660 /* Set the new gc trigger for the GCed generation. */
3661 generations[generation].gc_trigger =
3662 generations[generation].bytes_allocated
3663 + generations[generation].bytes_consed_between_gc;
3666 generations[generation].num_gc = 0;
3668 ++generations[generation].num_gc;
3671 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3673 update_x86_dynamic_space_free_pointer(void)
3678 for (i = 0; i < NUM_PAGES; i++)
3679 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3680 && (page_table[i].bytes_used != 0))
3683 last_free_page = last_page+1;
3685 SetSymbolValue(ALLOCATION_POINTER,
3686 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),0);
3687 return 0; /* dummy value: return something ... */
3690 /* GC all generations newer than last_gen, raising the objects in each
3691 * to the next older generation - we finish when all generations below
3692 * last_gen are empty. Then if last_gen is due for a GC, or if
3693 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3694 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3696 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3697 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3700 collect_garbage(unsigned last_gen)
3707 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3709 if (last_gen > NUM_GENERATIONS) {
3711 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3716 /* Flush the alloc regions updating the tables. */
3717 gc_alloc_update_all_page_tables();
3719 /* Verify the new objects created by Lisp code. */
3720 if (pre_verify_gen_0) {
3721 FSHOW((stderr, "pre-checking generation 0\n"));
3722 verify_generation(0);
3725 if (gencgc_verbose > 1)
3726 print_generation_stats(0);
3729 /* Collect the generation. */
3731 if (gen >= gencgc_oldest_gen_to_gc) {
3732 /* Never raise the oldest generation. */
3737 || (generations[gen].num_gc >= generations[gen].trigger_age);
3740 if (gencgc_verbose > 1) {
3742 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3745 generations[gen].bytes_allocated,
3746 generations[gen].gc_trigger,
3747 generations[gen].num_gc));
3750 /* If an older generation is being filled, then update its
3753 generations[gen+1].cum_sum_bytes_allocated +=
3754 generations[gen+1].bytes_allocated;
3757 garbage_collect_generation(gen, raise);
3759 /* Reset the memory age cum_sum. */
3760 generations[gen].cum_sum_bytes_allocated = 0;
3762 if (gencgc_verbose > 1) {
3763 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3764 print_generation_stats(0);
3768 } while ((gen <= gencgc_oldest_gen_to_gc)
3769 && ((gen < last_gen)
3770 || ((gen <= gencgc_oldest_gen_to_gc)
3772 && (generations[gen].bytes_allocated
3773 > generations[gen].gc_trigger)
3774 && (gen_av_mem_age(gen)
3775 > generations[gen].min_av_mem_age))));
3777 /* Now if gen-1 was raised all generations before gen are empty.
3778 * If it wasn't raised then all generations before gen-1 are empty.
3780 * Now objects within this gen's pages cannot point to younger
3781 * generations unless they are written to. This can be exploited
3782 * by write-protecting the pages of gen; then when younger
3783 * generations are GCed only the pages which have been written
3788 gen_to_wp = gen - 1;
3790 /* There's not much point in WPing pages in generation 0 as it is
3791 * never scavenged (except promoted pages). */
3792 if ((gen_to_wp > 0) && enable_page_protection) {
3793 /* Check that they are all empty. */
3794 for (i = 0; i < gen_to_wp; i++) {
3795 if (generations[i].bytes_allocated)
3796 lose("trying to write-protect gen. %d when gen. %d nonempty",
3799 write_protect_generation_pages(gen_to_wp);
3802 /* Set gc_alloc() back to generation 0. The current regions should
3803 * be flushed after the above GCs. */
3804 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3805 gc_alloc_generation = 0;
3807 update_x86_dynamic_space_free_pointer();
3808 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
3810 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
3812 SHOW("returning from collect_garbage");
3815 /* This is called by Lisp PURIFY when it is finished. All live objects
3816 * will have been moved to the RO and Static heaps. The dynamic space
3817 * will need a full re-initialization. We don't bother having Lisp
3818 * PURIFY flush the current gc_alloc() region, as the page_tables are
3819 * re-initialized, and every page is zeroed to be sure. */
3825 if (gencgc_verbose > 1)
3826 SHOW("entering gc_free_heap");
3828 for (page = 0; page < NUM_PAGES; page++) {
3829 /* Skip free pages which should already be zero filled. */
3830 if (page_table[page].allocated != FREE_PAGE_FLAG) {
3831 void *page_start, *addr;
3833 /* Mark the page free. The other slots are assumed invalid
3834 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
3835 * should not be write-protected -- except that the
3836 * generation is used for the current region but it sets
3838 page_table[page].allocated = FREE_PAGE_FLAG;
3839 page_table[page].bytes_used = 0;
3841 /* Zero the page. */
3842 page_start = (void *)page_address(page);
3844 /* First, remove any write-protection. */
3845 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3846 page_table[page].write_protected = 0;
3848 os_invalidate(page_start,PAGE_BYTES);
3849 addr = os_validate(page_start,PAGE_BYTES);
3850 if (addr == NULL || addr != page_start) {
3851 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
3855 } else if (gencgc_zero_check_during_free_heap) {
3856 /* Double-check that the page is zero filled. */
3858 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
3859 gc_assert(page_table[page].bytes_used == 0);
3860 page_start = (int *)page_address(page);
3861 for (i=0; i<1024; i++) {
3862 if (page_start[i] != 0) {
3863 lose("free region not zero at %x", page_start + i);
3869 bytes_allocated = 0;
3871 /* Initialize the generations. */
3872 for (page = 0; page < NUM_GENERATIONS; page++) {
3873 generations[page].alloc_start_page = 0;
3874 generations[page].alloc_unboxed_start_page = 0;
3875 generations[page].alloc_large_start_page = 0;
3876 generations[page].alloc_large_unboxed_start_page = 0;
3877 generations[page].bytes_allocated = 0;
3878 generations[page].gc_trigger = 2000000;
3879 generations[page].num_gc = 0;
3880 generations[page].cum_sum_bytes_allocated = 0;
3883 if (gencgc_verbose > 1)
3884 print_generation_stats(0);
3886 /* Initialize gc_alloc(). */
3887 gc_alloc_generation = 0;
3889 gc_set_region_empty(&boxed_region);
3890 gc_set_region_empty(&unboxed_region);
3893 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base),0);
3895 if (verify_after_free_heap) {
3896 /* Check whether purify has left any bad pointers. */
3898 SHOW("checking after free_heap\n");
3909 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
3910 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
3911 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
3913 heap_base = (void*)DYNAMIC_SPACE_START;
3915 /* Initialize each page structure. */
3916 for (i = 0; i < NUM_PAGES; i++) {
3917 /* Initialize all pages as free. */
3918 page_table[i].allocated = FREE_PAGE_FLAG;
3919 page_table[i].bytes_used = 0;
3921 /* Pages are not write-protected at startup. */
3922 page_table[i].write_protected = 0;
3925 bytes_allocated = 0;
3927 /* Initialize the generations.
3929 * FIXME: very similar to code in gc_free_heap(), should be shared */
3930 for (i = 0; i < NUM_GENERATIONS; i++) {
3931 generations[i].alloc_start_page = 0;
3932 generations[i].alloc_unboxed_start_page = 0;
3933 generations[i].alloc_large_start_page = 0;
3934 generations[i].alloc_large_unboxed_start_page = 0;
3935 generations[i].bytes_allocated = 0;
3936 generations[i].gc_trigger = 2000000;
3937 generations[i].num_gc = 0;
3938 generations[i].cum_sum_bytes_allocated = 0;
3939 /* the tune-able parameters */
3940 generations[i].bytes_consed_between_gc = 2000000;
3941 generations[i].trigger_age = 1;
3942 generations[i].min_av_mem_age = 0.75;
3945 /* Initialize gc_alloc. */
3946 gc_alloc_generation = 0;
3947 gc_set_region_empty(&boxed_region);
3948 gc_set_region_empty(&unboxed_region);
3954 /* Pick up the dynamic space from after a core load.
3956 * The ALLOCATION_POINTER points to the end of the dynamic space.
3960 gencgc_pickup_dynamic(void)
3963 int alloc_ptr = SymbolValue(ALLOCATION_POINTER,0);
3964 lispobj *prev=(lispobj *)page_address(page);
3967 lispobj *first,*ptr= (lispobj *)page_address(page);
3968 page_table[page].allocated = BOXED_PAGE_FLAG;
3969 page_table[page].gen = 0;
3970 page_table[page].bytes_used = PAGE_BYTES;
3971 page_table[page].large_object = 0;
3973 first=search_space(prev,(ptr+2)-prev,ptr);
3974 if(ptr == first) prev=ptr;
3975 page_table[page].first_object_offset =
3976 (void *)prev - page_address(page);
3978 } while (page_address(page) < alloc_ptr);
3980 generations[0].bytes_allocated = PAGE_BYTES*page;
3981 bytes_allocated = PAGE_BYTES*page;
3987 gc_initialize_pointers(void)
3989 gencgc_pickup_dynamic();
3995 /* alloc(..) is the external interface for memory allocation. It
3996 * allocates to generation 0. It is not called from within the garbage
3997 * collector as it is only external uses that need the check for heap
3998 * size (GC trigger) and to disable the interrupts (interrupts are
3999 * always disabled during a GC).
4001 * The vops that call alloc(..) assume that the returned space is zero-filled.
4002 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4004 * The check for a GC trigger is only performed when the current
4005 * region is full, so in most cases it's not needed. */
4010 struct thread *th=arch_os_get_current_thread();
4011 struct alloc_region *region=
4012 th ? &(th->alloc_region) : &boxed_region;
4014 void *new_free_pointer;
4016 /* Check for alignment allocation problems. */
4017 gc_assert((((unsigned)region->free_pointer & 0x7) == 0)
4018 && ((nbytes & 0x7) == 0));
4020 /* there are a few places in the C code that allocate data in the
4021 * heap before Lisp starts. This is before interrupts are enabled,
4022 * so we don't need to check for pseudo-atomic */
4023 #ifdef LISP_FEATURE_SB_THREAD
4024 if(!SymbolValue(PSEUDO_ATOMIC_ATOMIC,th)) {
4026 fprintf(stderr, "fatal error in thread 0x%x, pid=%d\n",
4028 __asm__("movl %fs,%0" : "=r" (fs) : );
4029 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4030 debug_get_fs(),th->tls_cookie);
4031 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4034 gc_assert(SymbolValue(PSEUDO_ATOMIC_ATOMIC,th));
4037 /* maybe we can do this quickly ... */
4038 new_free_pointer = region->free_pointer + nbytes;
4039 if (new_free_pointer <= region->end_addr) {
4040 new_obj = (void*)(region->free_pointer);
4041 region->free_pointer = new_free_pointer;
4042 return(new_obj); /* yup */
4045 /* we have to go the long way around, it seems. Check whether
4046 * we should GC in the near future
4048 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4049 /* set things up so that GC happens when we finish the PA
4050 * section. We only do this if there wasn't a pending handler
4051 * already, in case it was a gc. If it wasn't a GC, the next
4052 * allocation will get us back to this point anyway, so no harm done
4054 struct interrupt_data *data=th->interrupt_data;
4055 if(!data->pending_handler)
4056 maybe_defer_handler(interrupt_maybe_gc_int,data,0,0,0);
4058 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4063 * shared support for the OS-dependent signal handlers which
4064 * catch GENCGC-related write-protect violations
4067 void unhandled_sigmemoryfault(void);
4069 /* Depending on which OS we're running under, different signals might
4070 * be raised for a violation of write protection in the heap. This
4071 * function factors out the common generational GC magic which needs
4072 * to invoked in this case, and should be called from whatever signal
4073 * handler is appropriate for the OS we're running under.
4075 * Return true if this signal is a normal generational GC thing that
4076 * we were able to handle, or false if it was abnormal and control
4077 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4080 gencgc_handle_wp_violation(void* fault_addr)
4082 int page_index = find_page_index(fault_addr);
4084 #ifdef QSHOW_SIGNALS
4085 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4086 fault_addr, page_index));
4089 /* Check whether the fault is within the dynamic space. */
4090 if (page_index == (-1)) {
4092 /* It can be helpful to be able to put a breakpoint on this
4093 * case to help diagnose low-level problems. */
4094 unhandled_sigmemoryfault();
4096 /* not within the dynamic space -- not our responsibility */
4100 if (page_table[page_index].write_protected) {
4101 /* Unprotect the page. */
4102 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4103 page_table[page_index].write_protected_cleared = 1;
4104 page_table[page_index].write_protected = 0;
4106 /* The only acceptable reason for this signal on a heap
4107 * access is that GENCGC write-protected the page.
4108 * However, if two CPUs hit a wp page near-simultaneously,
4109 * we had better not have the second one lose here if it
4110 * does this test after the first one has already set wp=0
4112 if(page_table[page_index].write_protected_cleared != 1)
4113 lose("fault in heap page not marked as write-protected");
4115 /* Don't worry, we can handle it. */
4119 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4120 * it's not just a case of the program hitting the write barrier, and
4121 * are about to let Lisp deal with it. It's basically just a
4122 * convenient place to set a gdb breakpoint. */
4124 unhandled_sigmemoryfault()
4127 void gc_alloc_update_all_page_tables(void)
4129 /* Flush the alloc regions updating the tables. */
4132 gc_alloc_update_page_tables(0, &th->alloc_region);
4133 gc_alloc_update_page_tables(1, &unboxed_region);
4134 gc_alloc_update_page_tables(0, &boxed_region);
4137 gc_set_region_empty(struct alloc_region *region)
4139 region->first_page = 0;
4140 region->last_page = -1;
4141 region->start_addr = page_address(0);
4142 region->free_pointer = page_address(0);
4143 region->end_addr = page_address(0);