2 * GENerational Conservative Garbage Collector for SBCL
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>.
37 #include "interrupt.h"
42 #include "gc-internal.h"
44 #include "pseudo-atomic.h"
46 #include "genesis/vector.h"
47 #include "genesis/weak-pointer.h"
48 #include "genesis/fdefn.h"
49 #include "genesis/simple-fun.h"
51 #include "genesis/hash-table.h"
52 #include "genesis/instance.h"
53 #include "genesis/layout.h"
55 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
56 #include "genesis/cons.h"
59 /* forward declarations */
60 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, long nbytes,
68 /* Generations 0-5 are normal collected generations, 6 is only used as
69 * scratch space by the collector, and should never get collected.
72 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
76 /* Should we use page protection to help avoid the scavenging of pages
77 * that don't have pointers to younger generations? */
78 boolean enable_page_protection = 1;
80 /* the minimum size (in bytes) for a large object*/
81 #if (GENCGC_ALLOC_GRANULARITY >= PAGE_BYTES) && (GENCGC_ALLOC_GRANULARITY >= GENCGC_CARD_BYTES)
82 os_vm_size_t large_object_size = 4 * GENCGC_ALLOC_GRANULARITY;
83 #elif (GENCGC_CARD_BYTES >= PAGE_BYTES) && (GENCGC_CARD_BYTES >= GENCGC_ALLOC_GRANULARITY)
84 os_vm_size_t large_object_size = 4 * GENCGC_CARD_BYTES;
86 os_vm_size_t large_object_size = 4 * PAGE_BYTES;
89 /* Largest allocation seen since last GC. */
90 os_vm_size_t large_allocation = 0;
97 /* the verbosity level. All non-error messages are disabled at level 0;
98 * and only a few rare messages are printed at level 1. */
100 boolean gencgc_verbose = 1;
102 boolean gencgc_verbose = 0;
105 /* FIXME: At some point enable the various error-checking things below
106 * and see what they say. */
108 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
109 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
111 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
113 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
114 boolean pre_verify_gen_0 = 0;
116 /* Should we check for bad pointers after gc_free_heap is called
117 * from Lisp PURIFY? */
118 boolean verify_after_free_heap = 0;
120 /* Should we print a note when code objects are found in the dynamic space
121 * during a heap verify? */
122 boolean verify_dynamic_code_check = 0;
124 /* Should we check code objects for fixup errors after they are transported? */
125 boolean check_code_fixups = 0;
127 /* Should we check that newly allocated regions are zero filled? */
128 boolean gencgc_zero_check = 0;
130 /* Should we check that the free space is zero filled? */
131 boolean gencgc_enable_verify_zero_fill = 0;
133 /* Should we check that free pages are zero filled during gc_free_heap
134 * called after Lisp PURIFY? */
135 boolean gencgc_zero_check_during_free_heap = 0;
137 /* When loading a core, don't do a full scan of the memory for the
138 * memory region boundaries. (Set to true by coreparse.c if the core
139 * contained a pagetable entry).
141 boolean gencgc_partial_pickup = 0;
143 /* If defined, free pages are read-protected to ensure that nothing
147 /* #define READ_PROTECT_FREE_PAGES */
151 * GC structures and variables
154 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
155 os_vm_size_t bytes_allocated = 0;
156 os_vm_size_t auto_gc_trigger = 0;
158 /* the source and destination generations. These are set before a GC starts
160 generation_index_t from_space;
161 generation_index_t new_space;
163 /* Set to 1 when in GC */
164 boolean gc_active_p = 0;
166 /* should the GC be conservative on stack. If false (only right before
167 * saving a core), don't scan the stack / mark pages dont_move. */
168 static boolean conservative_stack = 1;
170 /* An array of page structures is allocated on gc initialization.
171 * This helps quickly map between an address its page structure.
172 * page_table_pages is set from the size of the dynamic space. */
173 page_index_t page_table_pages;
174 struct page *page_table;
176 static inline boolean page_allocated_p(page_index_t page) {
177 return (page_table[page].allocated != FREE_PAGE_FLAG);
180 static inline boolean page_no_region_p(page_index_t page) {
181 return !(page_table[page].allocated & OPEN_REGION_PAGE_FLAG);
184 static inline boolean page_allocated_no_region_p(page_index_t page) {
185 return ((page_table[page].allocated & (UNBOXED_PAGE_FLAG | BOXED_PAGE_FLAG))
186 && page_no_region_p(page));
189 static inline boolean page_free_p(page_index_t page) {
190 return (page_table[page].allocated == FREE_PAGE_FLAG);
193 static inline boolean page_boxed_p(page_index_t page) {
194 return (page_table[page].allocated & BOXED_PAGE_FLAG);
197 static inline boolean code_page_p(page_index_t page) {
198 return (page_table[page].allocated & CODE_PAGE_FLAG);
201 static inline boolean page_boxed_no_region_p(page_index_t page) {
202 return page_boxed_p(page) && page_no_region_p(page);
205 static inline boolean page_unboxed_p(page_index_t page) {
206 /* Both flags set == boxed code page */
207 return ((page_table[page].allocated & UNBOXED_PAGE_FLAG)
208 && !page_boxed_p(page));
211 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
212 return (page_boxed_no_region_p(page)
213 && (page_table[page].bytes_used != 0)
214 && !page_table[page].dont_move
215 && (page_table[page].gen == generation));
218 /* To map addresses to page structures the address of the first page
220 static void *heap_base = NULL;
222 /* Calculate the start address for the given page number. */
224 page_address(page_index_t page_num)
226 return (heap_base + (page_num * GENCGC_CARD_BYTES));
229 /* Calculate the address where the allocation region associated with
230 * the page starts. */
232 page_region_start(page_index_t page_index)
234 return page_address(page_index)-page_table[page_index].region_start_offset;
237 /* Find the page index within the page_table for the given
238 * address. Return -1 on failure. */
240 find_page_index(void *addr)
242 if (addr >= heap_base) {
243 page_index_t index = ((pointer_sized_uint_t)addr -
244 (pointer_sized_uint_t)heap_base) / GENCGC_CARD_BYTES;
245 if (index < page_table_pages)
252 npage_bytes(page_index_t npages)
254 gc_assert(npages>=0);
255 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
258 /* Check that X is a higher address than Y and return offset from Y to
260 static inline os_vm_size_t
261 void_diff(void *x, void *y)
264 return (pointer_sized_uint_t)x - (pointer_sized_uint_t)y;
267 /* a structure to hold the state of a generation
269 * CAUTION: If you modify this, make sure to touch up the alien
270 * definition in src/code/gc.lisp accordingly. ...or better yes,
271 * deal with the FIXME there...
275 /* the first page that gc_alloc() checks on its next call */
276 page_index_t alloc_start_page;
278 /* the first page that gc_alloc_unboxed() checks on its next call */
279 page_index_t alloc_unboxed_start_page;
281 /* the first page that gc_alloc_large (boxed) considers on its next
282 * call. (Although it always allocates after the boxed_region.) */
283 page_index_t alloc_large_start_page;
285 /* the first page that gc_alloc_large (unboxed) considers on its
286 * next call. (Although it always allocates after the
287 * current_unboxed_region.) */
288 page_index_t alloc_large_unboxed_start_page;
290 /* the bytes allocated to this generation */
291 os_vm_size_t bytes_allocated;
293 /* the number of bytes at which to trigger a GC */
294 os_vm_size_t gc_trigger;
296 /* to calculate a new level for gc_trigger */
297 os_vm_size_t bytes_consed_between_gc;
299 /* the number of GCs since the last raise */
302 /* the number of GCs to run on the generations before raising objects to the
304 int number_of_gcs_before_promotion;
306 /* the cumulative sum of the bytes allocated to this generation. It is
307 * cleared after a GC on this generations, and update before new
308 * objects are added from a GC of a younger generation. Dividing by
309 * the bytes_allocated will give the average age of the memory in
310 * this generation since its last GC. */
311 os_vm_size_t cum_sum_bytes_allocated;
313 /* a minimum average memory age before a GC will occur helps
314 * prevent a GC when a large number of new live objects have been
315 * added, in which case a GC could be a waste of time */
316 double minimum_age_before_gc;
319 /* an array of generation structures. There needs to be one more
320 * generation structure than actual generations as the oldest
321 * generation is temporarily raised then lowered. */
322 struct generation generations[NUM_GENERATIONS];
324 /* the oldest generation that is will currently be GCed by default.
325 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
327 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
329 * Setting this to 0 effectively disables the generational nature of
330 * the GC. In some applications generational GC may not be useful
331 * because there are no long-lived objects.
333 * An intermediate value could be handy after moving long-lived data
334 * into an older generation so an unnecessary GC of this long-lived
335 * data can be avoided. */
336 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
338 /* The maximum free page in the heap is maintained and used to update
339 * ALLOCATION_POINTER which is used by the room function to limit its
340 * search of the heap. XX Gencgc obviously needs to be better
341 * integrated with the Lisp code. */
342 page_index_t last_free_page;
344 #ifdef LISP_FEATURE_SB_THREAD
345 /* This lock is to prevent multiple threads from simultaneously
346 * allocating new regions which overlap each other. Note that the
347 * majority of GC is single-threaded, but alloc() may be called from
348 * >1 thread at a time and must be thread-safe. This lock must be
349 * seized before all accesses to generations[] or to parts of
350 * page_table[] that other threads may want to see */
351 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
352 /* This lock is used to protect non-thread-local allocation. */
353 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
356 extern os_vm_size_t gencgc_release_granularity;
357 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
359 extern os_vm_size_t gencgc_alloc_granularity;
360 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
364 * miscellaneous heap functions
367 /* Count the number of pages which are write-protected within the
368 * given generation. */
370 count_write_protect_generation_pages(generation_index_t generation)
372 page_index_t i, count = 0;
374 for (i = 0; i < last_free_page; i++)
375 if (page_allocated_p(i)
376 && (page_table[i].gen == generation)
377 && (page_table[i].write_protected == 1))
382 /* Count the number of pages within the given generation. */
384 count_generation_pages(generation_index_t generation)
387 page_index_t count = 0;
389 for (i = 0; i < last_free_page; i++)
390 if (page_allocated_p(i)
391 && (page_table[i].gen == generation))
398 count_dont_move_pages(void)
401 page_index_t count = 0;
402 for (i = 0; i < last_free_page; i++) {
403 if (page_allocated_p(i)
404 && (page_table[i].dont_move != 0)) {
412 /* Work through the pages and add up the number of bytes used for the
413 * given generation. */
415 count_generation_bytes_allocated (generation_index_t gen)
418 os_vm_size_t result = 0;
419 for (i = 0; i < last_free_page; i++) {
420 if (page_allocated_p(i)
421 && (page_table[i].gen == gen))
422 result += page_table[i].bytes_used;
427 /* Return the average age of the memory in a generation. */
429 generation_average_age(generation_index_t gen)
431 if (generations[gen].bytes_allocated == 0)
435 ((double)generations[gen].cum_sum_bytes_allocated)
436 / ((double)generations[gen].bytes_allocated);
440 write_generation_stats(FILE *file)
442 generation_index_t i;
444 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
445 #define FPU_STATE_SIZE 27
446 int fpu_state[FPU_STATE_SIZE];
447 #elif defined(LISP_FEATURE_PPC)
448 #define FPU_STATE_SIZE 32
449 long long fpu_state[FPU_STATE_SIZE];
452 /* This code uses the FP instructions which may be set up for Lisp
453 * so they need to be saved and reset for C. */
456 /* Print the heap stats. */
458 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
460 for (i = 0; i < SCRATCH_GENERATION; i++) {
462 page_index_t boxed_cnt = 0;
463 page_index_t unboxed_cnt = 0;
464 page_index_t large_boxed_cnt = 0;
465 page_index_t large_unboxed_cnt = 0;
466 page_index_t pinned_cnt=0;
468 for (j = 0; j < last_free_page; j++)
469 if (page_table[j].gen == i) {
471 /* Count the number of boxed pages within the given
473 if (page_boxed_p(j)) {
474 if (page_table[j].large_object)
479 if(page_table[j].dont_move) pinned_cnt++;
480 /* Count the number of unboxed pages within the given
482 if (page_unboxed_p(j)) {
483 if (page_table[j].large_object)
490 gc_assert(generations[i].bytes_allocated
491 == count_generation_bytes_allocated(i));
493 " %1d: %5ld %5ld %5ld %5ld",
495 generations[i].alloc_start_page,
496 generations[i].alloc_unboxed_start_page,
497 generations[i].alloc_large_start_page,
498 generations[i].alloc_large_unboxed_start_page);
500 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
501 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
502 boxed_cnt, unboxed_cnt, large_boxed_cnt,
503 large_unboxed_cnt, pinned_cnt);
508 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
509 generations[i].bytes_allocated,
510 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
511 generations[i].gc_trigger,
512 count_write_protect_generation_pages(i),
513 generations[i].num_gc,
514 generation_average_age(i));
516 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
517 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
519 fpu_restore(fpu_state);
523 write_heap_exhaustion_report(FILE *file, long available, long requested,
524 struct thread *thread)
527 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
528 gc_active_p ? "garbage collection" : "allocation",
531 write_generation_stats(file);
532 fprintf(file, "GC control variables:\n");
533 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
534 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
535 (SymbolValue(GC_PENDING, thread) == T) ?
536 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
537 "false" : "in progress"));
538 #ifdef LISP_FEATURE_SB_THREAD
539 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
540 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
545 print_generation_stats(void)
547 write_generation_stats(stderr);
550 extern char* gc_logfile;
551 char * gc_logfile = NULL;
554 log_generation_stats(char *logfile, char *header)
557 FILE * log = fopen(logfile, "a");
559 fprintf(log, "%s\n", header);
560 write_generation_stats(log);
563 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
570 report_heap_exhaustion(long available, long requested, struct thread *th)
573 FILE * log = fopen(gc_logfile, "a");
575 write_heap_exhaustion_report(log, available, requested, th);
578 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
582 /* Always to stderr as well. */
583 write_heap_exhaustion_report(stderr, available, requested, th);
587 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
588 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
591 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
592 * if zeroing it ourselves, i.e. in practice give the memory back to the
593 * OS. Generally done after a large GC.
595 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
597 void *addr = page_address(start), *new_addr;
598 os_vm_size_t length = npage_bytes(1+end-start);
603 gc_assert(length >= gencgc_release_granularity);
604 gc_assert((length % gencgc_release_granularity) == 0);
606 os_invalidate(addr, length);
607 new_addr = os_validate(addr, length);
608 if (new_addr == NULL || new_addr != addr) {
609 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
613 for (i = start; i <= end; i++) {
614 page_table[i].need_to_zero = 0;
618 /* Zero the pages from START to END (inclusive). Generally done just after
619 * a new region has been allocated.
622 zero_pages(page_index_t start, page_index_t end) {
626 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
627 fast_bzero(page_address(start), npage_bytes(1+end-start));
629 bzero(page_address(start), npage_bytes(1+end-start));
635 zero_and_mark_pages(page_index_t start, page_index_t end) {
638 zero_pages(start, end);
639 for (i = start; i <= end; i++)
640 page_table[i].need_to_zero = 0;
643 /* Zero the pages from START to END (inclusive), except for those
644 * pages that are known to already zeroed. Mark all pages in the
645 * ranges as non-zeroed.
648 zero_dirty_pages(page_index_t start, page_index_t end) {
651 for (i = start; i <= end; i++) {
652 if (!page_table[i].need_to_zero) continue;
653 for (j = i+1; (j <= end) && (page_table[j].need_to_zero); j++);
658 for (i = start; i <= end; i++) {
659 page_table[i].need_to_zero = 1;
665 * To support quick and inline allocation, regions of memory can be
666 * allocated and then allocated from with just a free pointer and a
667 * check against an end address.
669 * Since objects can be allocated to spaces with different properties
670 * e.g. boxed/unboxed, generation, ages; there may need to be many
671 * allocation regions.
673 * Each allocation region may start within a partly used page. Many
674 * features of memory use are noted on a page wise basis, e.g. the
675 * generation; so if a region starts within an existing allocated page
676 * it must be consistent with this page.
678 * During the scavenging of the newspace, objects will be transported
679 * into an allocation region, and pointers updated to point to this
680 * allocation region. It is possible that these pointers will be
681 * scavenged again before the allocation region is closed, e.g. due to
682 * trans_list which jumps all over the place to cleanup the list. It
683 * is important to be able to determine properties of all objects
684 * pointed to when scavenging, e.g to detect pointers to the oldspace.
685 * Thus it's important that the allocation regions have the correct
686 * properties set when allocated, and not just set when closed. The
687 * region allocation routines return regions with the specified
688 * properties, and grab all the pages, setting their properties
689 * appropriately, except that the amount used is not known.
691 * These regions are used to support quicker allocation using just a
692 * free pointer. The actual space used by the region is not reflected
693 * in the pages tables until it is closed. It can't be scavenged until
696 * When finished with the region it should be closed, which will
697 * update the page tables for the actual space used returning unused
698 * space. Further it may be noted in the new regions which is
699 * necessary when scavenging the newspace.
701 * Large objects may be allocated directly without an allocation
702 * region, the page tables are updated immediately.
704 * Unboxed objects don't contain pointers to other objects and so
705 * don't need scavenging. Further they can't contain pointers to
706 * younger generations so WP is not needed. By allocating pages to
707 * unboxed objects the whole page never needs scavenging or
708 * write-protecting. */
710 /* We are only using two regions at present. Both are for the current
711 * newspace generation. */
712 struct alloc_region boxed_region;
713 struct alloc_region unboxed_region;
715 /* The generation currently being allocated to. */
716 static generation_index_t gc_alloc_generation;
718 static inline page_index_t
719 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
722 if (UNBOXED_PAGE_FLAG == page_type_flag) {
723 return generations[generation].alloc_large_unboxed_start_page;
724 } else if (BOXED_PAGE_FLAG & page_type_flag) {
725 /* Both code and data. */
726 return generations[generation].alloc_large_start_page;
728 lose("bad page type flag: %d", page_type_flag);
731 if (UNBOXED_PAGE_FLAG == page_type_flag) {
732 return generations[generation].alloc_unboxed_start_page;
733 } else if (BOXED_PAGE_FLAG & page_type_flag) {
734 /* Both code and data. */
735 return generations[generation].alloc_start_page;
737 lose("bad page_type_flag: %d", page_type_flag);
743 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
747 if (UNBOXED_PAGE_FLAG == page_type_flag) {
748 generations[generation].alloc_large_unboxed_start_page = page;
749 } else if (BOXED_PAGE_FLAG & page_type_flag) {
750 /* Both code and data. */
751 generations[generation].alloc_large_start_page = page;
753 lose("bad page type flag: %d", page_type_flag);
756 if (UNBOXED_PAGE_FLAG == page_type_flag) {
757 generations[generation].alloc_unboxed_start_page = page;
758 } else if (BOXED_PAGE_FLAG & page_type_flag) {
759 /* Both code and data. */
760 generations[generation].alloc_start_page = page;
762 lose("bad page type flag: %d", page_type_flag);
767 /* Find a new region with room for at least the given number of bytes.
769 * It starts looking at the current generation's alloc_start_page. So
770 * may pick up from the previous region if there is enough space. This
771 * keeps the allocation contiguous when scavenging the newspace.
773 * The alloc_region should have been closed by a call to
774 * gc_alloc_update_page_tables(), and will thus be in an empty state.
776 * To assist the scavenging functions write-protected pages are not
777 * used. Free pages should not be write-protected.
779 * It is critical to the conservative GC that the start of regions be
780 * known. To help achieve this only small regions are allocated at a
783 * During scavenging, pointers may be found to within the current
784 * region and the page generation must be set so that pointers to the
785 * from space can be recognized. Therefore the generation of pages in
786 * the region are set to gc_alloc_generation. To prevent another
787 * allocation call using the same pages, all the pages in the region
788 * are allocated, although they will initially be empty.
791 gc_alloc_new_region(long nbytes, int page_type_flag, struct alloc_region *alloc_region)
793 page_index_t first_page;
794 page_index_t last_page;
795 os_vm_size_t bytes_found;
801 "/alloc_new_region for %d bytes from gen %d\n",
802 nbytes, gc_alloc_generation));
805 /* Check that the region is in a reset state. */
806 gc_assert((alloc_region->first_page == 0)
807 && (alloc_region->last_page == -1)
808 && (alloc_region->free_pointer == alloc_region->end_addr));
809 ret = thread_mutex_lock(&free_pages_lock);
811 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
812 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
813 bytes_found=(GENCGC_CARD_BYTES - page_table[first_page].bytes_used)
814 + npage_bytes(last_page-first_page);
816 /* Set up the alloc_region. */
817 alloc_region->first_page = first_page;
818 alloc_region->last_page = last_page;
819 alloc_region->start_addr = page_table[first_page].bytes_used
820 + page_address(first_page);
821 alloc_region->free_pointer = alloc_region->start_addr;
822 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
824 /* Set up the pages. */
826 /* The first page may have already been in use. */
827 if (page_table[first_page].bytes_used == 0) {
828 page_table[first_page].allocated = page_type_flag;
829 page_table[first_page].gen = gc_alloc_generation;
830 page_table[first_page].large_object = 0;
831 page_table[first_page].region_start_offset = 0;
834 gc_assert(page_table[first_page].allocated == page_type_flag);
835 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
837 gc_assert(page_table[first_page].gen == gc_alloc_generation);
838 gc_assert(page_table[first_page].large_object == 0);
840 for (i = first_page+1; i <= last_page; i++) {
841 page_table[i].allocated = page_type_flag;
842 page_table[i].gen = gc_alloc_generation;
843 page_table[i].large_object = 0;
844 /* This may not be necessary for unboxed regions (think it was
846 page_table[i].region_start_offset =
847 void_diff(page_address(i),alloc_region->start_addr);
848 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
850 /* Bump up last_free_page. */
851 if (last_page+1 > last_free_page) {
852 last_free_page = last_page+1;
853 /* do we only want to call this on special occasions? like for
855 set_alloc_pointer((lispobj)page_address(last_free_page));
857 ret = thread_mutex_unlock(&free_pages_lock);
860 #ifdef READ_PROTECT_FREE_PAGES
861 os_protect(page_address(first_page),
862 npage_bytes(1+last_page-first_page),
866 /* If the first page was only partial, don't check whether it's
867 * zeroed (it won't be) and don't zero it (since the parts that
868 * we're interested in are guaranteed to be zeroed).
870 if (page_table[first_page].bytes_used) {
874 zero_dirty_pages(first_page, last_page);
876 /* we can do this after releasing free_pages_lock */
877 if (gencgc_zero_check) {
879 for (p = (word_t *)alloc_region->start_addr;
880 p < (word_t *)alloc_region->end_addr; p++) {
882 lose("The new region is not zero at %p (start=%p, end=%p).\n",
883 p, alloc_region->start_addr, alloc_region->end_addr);
889 /* If the record_new_objects flag is 2 then all new regions created
892 * If it's 1 then then it is only recorded if the first page of the
893 * current region is <= new_areas_ignore_page. This helps avoid
894 * unnecessary recording when doing full scavenge pass.
896 * The new_object structure holds the page, byte offset, and size of
897 * new regions of objects. Each new area is placed in the array of
898 * these structures pointer to by new_areas. new_areas_index holds the
899 * offset into new_areas.
901 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
902 * later code must detect this and handle it, probably by doing a full
903 * scavenge of a generation. */
904 #define NUM_NEW_AREAS 512
905 static int record_new_objects = 0;
906 static page_index_t new_areas_ignore_page;
912 static struct new_area (*new_areas)[];
913 static size_t new_areas_index;
914 size_t max_new_areas;
916 /* Add a new area to new_areas. */
918 add_new_area(page_index_t first_page, size_t offset, size_t size)
920 size_t new_area_start, c, i;
922 /* Ignore if full. */
923 if (new_areas_index >= NUM_NEW_AREAS)
926 switch (record_new_objects) {
930 if (first_page > new_areas_ignore_page)
939 new_area_start = npage_bytes(first_page) + offset;
941 /* Search backwards for a prior area that this follows from. If
942 found this will save adding a new area. */
943 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
945 npage_bytes((*new_areas)[i].page)
946 + (*new_areas)[i].offset
947 + (*new_areas)[i].size;
949 "/add_new_area S1 %d %d %d %d\n",
950 i, c, new_area_start, area_end));*/
951 if (new_area_start == area_end) {
953 "/adding to [%d] %d %d %d with %d %d %d:\n",
955 (*new_areas)[i].page,
956 (*new_areas)[i].offset,
957 (*new_areas)[i].size,
961 (*new_areas)[i].size += size;
966 (*new_areas)[new_areas_index].page = first_page;
967 (*new_areas)[new_areas_index].offset = offset;
968 (*new_areas)[new_areas_index].size = size;
970 "/new_area %d page %d offset %d size %d\n",
971 new_areas_index, first_page, offset, size));*/
974 /* Note the max new_areas used. */
975 if (new_areas_index > max_new_areas)
976 max_new_areas = new_areas_index;
979 /* Update the tables for the alloc_region. The region may be added to
982 * When done the alloc_region is set up so that the next quick alloc
983 * will fail safely and thus a new region will be allocated. Further
984 * it is safe to try to re-update the page table of this reset
987 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
990 page_index_t first_page;
991 page_index_t next_page;
992 os_vm_size_t bytes_used;
993 os_vm_size_t region_size;
994 os_vm_size_t byte_cnt;
995 page_bytes_t orig_first_page_bytes_used;
999 first_page = alloc_region->first_page;
1001 /* Catch an unused alloc_region. */
1002 if ((first_page == 0) && (alloc_region->last_page == -1))
1005 next_page = first_page+1;
1007 ret = thread_mutex_lock(&free_pages_lock);
1008 gc_assert(ret == 0);
1009 if (alloc_region->free_pointer != alloc_region->start_addr) {
1010 /* some bytes were allocated in the region */
1011 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1013 gc_assert(alloc_region->start_addr ==
1014 (page_address(first_page)
1015 + page_table[first_page].bytes_used));
1017 /* All the pages used need to be updated */
1019 /* Update the first page. */
1021 /* If the page was free then set up the gen, and
1022 * region_start_offset. */
1023 if (page_table[first_page].bytes_used == 0)
1024 gc_assert(page_table[first_page].region_start_offset == 0);
1025 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1027 gc_assert(page_table[first_page].allocated & page_type_flag);
1028 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1029 gc_assert(page_table[first_page].large_object == 0);
1033 /* Calculate the number of bytes used in this page. This is not
1034 * always the number of new bytes, unless it was free. */
1036 if ((bytes_used = void_diff(alloc_region->free_pointer,
1037 page_address(first_page)))
1038 >GENCGC_CARD_BYTES) {
1039 bytes_used = GENCGC_CARD_BYTES;
1042 page_table[first_page].bytes_used = bytes_used;
1043 byte_cnt += bytes_used;
1046 /* All the rest of the pages should be free. We need to set
1047 * their region_start_offset pointer to the start of the
1048 * region, and set the bytes_used. */
1050 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1051 gc_assert(page_table[next_page].allocated & page_type_flag);
1052 gc_assert(page_table[next_page].bytes_used == 0);
1053 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1054 gc_assert(page_table[next_page].large_object == 0);
1056 gc_assert(page_table[next_page].region_start_offset ==
1057 void_diff(page_address(next_page),
1058 alloc_region->start_addr));
1060 /* Calculate the number of bytes used in this page. */
1062 if ((bytes_used = void_diff(alloc_region->free_pointer,
1063 page_address(next_page)))>GENCGC_CARD_BYTES) {
1064 bytes_used = GENCGC_CARD_BYTES;
1067 page_table[next_page].bytes_used = bytes_used;
1068 byte_cnt += bytes_used;
1073 region_size = void_diff(alloc_region->free_pointer,
1074 alloc_region->start_addr);
1075 bytes_allocated += region_size;
1076 generations[gc_alloc_generation].bytes_allocated += region_size;
1078 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1080 /* Set the generations alloc restart page to the last page of
1082 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1084 /* Add the region to the new_areas if requested. */
1085 if (BOXED_PAGE_FLAG & page_type_flag)
1086 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1090 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1092 gc_alloc_generation));
1095 /* There are no bytes allocated. Unallocate the first_page if
1096 * there are 0 bytes_used. */
1097 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1098 if (page_table[first_page].bytes_used == 0)
1099 page_table[first_page].allocated = FREE_PAGE_FLAG;
1102 /* Unallocate any unused pages. */
1103 while (next_page <= alloc_region->last_page) {
1104 gc_assert(page_table[next_page].bytes_used == 0);
1105 page_table[next_page].allocated = FREE_PAGE_FLAG;
1108 ret = thread_mutex_unlock(&free_pages_lock);
1109 gc_assert(ret == 0);
1111 /* alloc_region is per-thread, we're ok to do this unlocked */
1112 gc_set_region_empty(alloc_region);
1115 static inline void *gc_quick_alloc(long nbytes);
1117 /* Allocate a possibly large object. */
1119 gc_alloc_large(long nbytes, int page_type_flag, struct alloc_region *alloc_region)
1122 page_index_t first_page, next_page, last_page;
1123 page_bytes_t orig_first_page_bytes_used;
1124 os_vm_size_t byte_cnt;
1125 os_vm_size_t bytes_used;
1128 ret = thread_mutex_lock(&free_pages_lock);
1129 gc_assert(ret == 0);
1131 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1132 if (first_page <= alloc_region->last_page) {
1133 first_page = alloc_region->last_page+1;
1136 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1138 gc_assert(first_page > alloc_region->last_page);
1140 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1142 /* Set up the pages. */
1143 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1145 /* If the first page was free then set up the gen, and
1146 * region_start_offset. */
1147 if (page_table[first_page].bytes_used == 0) {
1148 page_table[first_page].allocated = page_type_flag;
1149 page_table[first_page].gen = gc_alloc_generation;
1150 page_table[first_page].region_start_offset = 0;
1151 page_table[first_page].large_object = 1;
1154 gc_assert(page_table[first_page].allocated == page_type_flag);
1155 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1156 gc_assert(page_table[first_page].large_object == 1);
1160 /* Calc. the number of bytes used in this page. This is not
1161 * always the number of new bytes, unless it was free. */
1163 if ((bytes_used = nbytes+orig_first_page_bytes_used) > GENCGC_CARD_BYTES) {
1164 bytes_used = GENCGC_CARD_BYTES;
1167 page_table[first_page].bytes_used = bytes_used;
1168 byte_cnt += bytes_used;
1170 next_page = first_page+1;
1172 /* All the rest of the pages should be free. We need to set their
1173 * region_start_offset pointer to the start of the region, and set
1174 * the bytes_used. */
1176 gc_assert(page_free_p(next_page));
1177 gc_assert(page_table[next_page].bytes_used == 0);
1178 page_table[next_page].allocated = page_type_flag;
1179 page_table[next_page].gen = gc_alloc_generation;
1180 page_table[next_page].large_object = 1;
1182 page_table[next_page].region_start_offset =
1183 npage_bytes(next_page-first_page) - orig_first_page_bytes_used;
1185 /* Calculate the number of bytes used in this page. */
1187 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1188 if (bytes_used > GENCGC_CARD_BYTES) {
1189 bytes_used = GENCGC_CARD_BYTES;
1192 page_table[next_page].bytes_used = bytes_used;
1193 page_table[next_page].write_protected=0;
1194 page_table[next_page].dont_move=0;
1195 byte_cnt += bytes_used;
1199 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
1201 bytes_allocated += nbytes;
1202 generations[gc_alloc_generation].bytes_allocated += nbytes;
1204 /* Add the region to the new_areas if requested. */
1205 if (BOXED_PAGE_FLAG & page_type_flag)
1206 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1208 /* Bump up last_free_page */
1209 if (last_page+1 > last_free_page) {
1210 last_free_page = last_page+1;
1211 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1213 ret = thread_mutex_unlock(&free_pages_lock);
1214 gc_assert(ret == 0);
1216 #ifdef READ_PROTECT_FREE_PAGES
1217 os_protect(page_address(first_page),
1218 npage_bytes(1+last_page-first_page),
1222 zero_dirty_pages(first_page, last_page);
1224 return page_address(first_page);
1227 static page_index_t gencgc_alloc_start_page = -1;
1230 gc_heap_exhausted_error_or_lose (long available, long requested)
1232 struct thread *thread = arch_os_get_current_thread();
1233 /* Write basic information before doing anything else: if we don't
1234 * call to lisp this is a must, and even if we do there is always
1235 * the danger that we bounce back here before the error has been
1236 * handled, or indeed even printed.
1238 report_heap_exhaustion(available, requested, thread);
1239 if (gc_active_p || (available == 0)) {
1240 /* If we are in GC, or totally out of memory there is no way
1241 * to sanely transfer control to the lisp-side of things.
1243 lose("Heap exhausted, game over.");
1246 /* FIXME: assert free_pages_lock held */
1247 (void)thread_mutex_unlock(&free_pages_lock);
1248 gc_assert(get_pseudo_atomic_atomic(thread));
1249 clear_pseudo_atomic_atomic(thread);
1250 if (get_pseudo_atomic_interrupted(thread))
1251 do_pending_interrupt();
1252 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1253 * to running user code at arbitrary places, even in a
1254 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1255 * running out of the heap. So at this point all bets are
1257 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1258 corruption_warning_and_maybe_lose
1259 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1260 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR),
1261 alloc_number(available), alloc_number(requested));
1262 lose("HEAP-EXHAUSTED-ERROR fell through");
1267 gc_find_freeish_pages(page_index_t *restart_page_ptr, long bytes,
1270 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1271 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1272 os_vm_size_t nbytes = bytes;
1273 os_vm_size_t nbytes_goal = nbytes;
1274 os_vm_size_t bytes_found = 0;
1275 os_vm_size_t most_bytes_found = 0;
1276 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1277 /* FIXME: assert(free_pages_lock is held); */
1279 if (nbytes_goal < gencgc_alloc_granularity)
1280 nbytes_goal = gencgc_alloc_granularity;
1282 /* Toggled by gc_and_save for heap compaction, normally -1. */
1283 if (gencgc_alloc_start_page != -1) {
1284 restart_page = gencgc_alloc_start_page;
1287 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1288 * long from the interface. */
1289 gc_assert(bytes>=0);
1290 /* Search for a page with at least nbytes of space. We prefer
1291 * not to split small objects on multiple pages, to reduce the
1292 * number of contiguous allocation regions spaning multiple
1293 * pages: this helps avoid excessive conservativism.
1295 * For other objects, we guarantee that they start on their own
1298 first_page = restart_page;
1299 while (first_page < page_table_pages) {
1301 if (page_free_p(first_page)) {
1302 gc_assert(0 == page_table[first_page].bytes_used);
1303 bytes_found = GENCGC_CARD_BYTES;
1304 } else if (small_object &&
1305 (page_table[first_page].allocated == page_type_flag) &&
1306 (page_table[first_page].large_object == 0) &&
1307 (page_table[first_page].gen == gc_alloc_generation) &&
1308 (page_table[first_page].write_protected == 0) &&
1309 (page_table[first_page].dont_move == 0)) {
1310 bytes_found = GENCGC_CARD_BYTES - page_table[first_page].bytes_used;
1311 if (bytes_found < nbytes) {
1312 if (bytes_found > most_bytes_found)
1313 most_bytes_found = bytes_found;
1322 gc_assert(page_table[first_page].write_protected == 0);
1323 for (last_page = first_page+1;
1324 ((last_page < page_table_pages) &&
1325 page_free_p(last_page) &&
1326 (bytes_found < nbytes_goal));
1328 bytes_found += GENCGC_CARD_BYTES;
1329 gc_assert(0 == page_table[last_page].bytes_used);
1330 gc_assert(0 == page_table[last_page].write_protected);
1333 if (bytes_found > most_bytes_found) {
1334 most_bytes_found = bytes_found;
1335 most_bytes_found_from = first_page;
1336 most_bytes_found_to = last_page;
1338 if (bytes_found >= nbytes_goal)
1341 first_page = last_page;
1344 bytes_found = most_bytes_found;
1345 restart_page = first_page + 1;
1347 /* Check for a failure */
1348 if (bytes_found < nbytes) {
1349 gc_assert(restart_page >= page_table_pages);
1350 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1353 gc_assert(most_bytes_found_to);
1354 *restart_page_ptr = most_bytes_found_from;
1355 return most_bytes_found_to-1;
1358 /* Allocate bytes. All the rest of the special-purpose allocation
1359 * functions will eventually call this */
1362 gc_alloc_with_region(long nbytes,int page_type_flag, struct alloc_region *my_region,
1365 void *new_free_pointer;
1367 if (nbytes>=large_object_size)
1368 return gc_alloc_large(nbytes, page_type_flag, my_region);
1370 /* Check whether there is room in the current alloc region. */
1371 new_free_pointer = my_region->free_pointer + nbytes;
1373 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1374 my_region->free_pointer, new_free_pointer); */
1376 if (new_free_pointer <= my_region->end_addr) {
1377 /* If so then allocate from the current alloc region. */
1378 void *new_obj = my_region->free_pointer;
1379 my_region->free_pointer = new_free_pointer;
1381 /* Unless a `quick' alloc was requested, check whether the
1382 alloc region is almost empty. */
1384 void_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1385 /* If so, finished with the current region. */
1386 gc_alloc_update_page_tables(page_type_flag, my_region);
1387 /* Set up a new region. */
1388 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1391 return((void *)new_obj);
1394 /* Else not enough free space in the current region: retry with a
1397 gc_alloc_update_page_tables(page_type_flag, my_region);
1398 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1399 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1402 /* these are only used during GC: all allocation from the mutator calls
1403 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1406 static inline void *
1407 gc_quick_alloc(long nbytes)
1409 return gc_general_alloc(nbytes, BOXED_PAGE_FLAG, ALLOC_QUICK);
1412 static inline void *
1413 gc_alloc_unboxed(long nbytes)
1415 return gc_general_alloc(nbytes, UNBOXED_PAGE_FLAG, 0);
1418 static inline void *
1419 gc_quick_alloc_unboxed(long nbytes)
1421 return gc_general_alloc(nbytes, UNBOXED_PAGE_FLAG, ALLOC_QUICK);
1424 /* Copy a large object. If the object is in a large object region then
1425 * it is simply promoted, else it is copied. If it's large enough then
1426 * it's copied to a large object region.
1428 * Bignums and vectors may have shrunk. If the object is not copied
1429 * the space needs to be reclaimed, and the page_tables corrected. */
1431 general_copy_large_object(lispobj object, long nwords, boolean boxedp)
1435 page_index_t first_page;
1437 gc_assert(is_lisp_pointer(object));
1438 gc_assert(from_space_p(object));
1439 gc_assert((nwords & 0x01) == 0);
1441 if ((nwords > 1024*1024) && gencgc_verbose) {
1442 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1443 nwords*N_WORD_BYTES));
1446 /* Check whether it's a large object. */
1447 first_page = find_page_index((void *)object);
1448 gc_assert(first_page >= 0);
1450 if (page_table[first_page].large_object) {
1451 /* Promote the object. Note: Unboxed objects may have been
1452 * allocated to a BOXED region so it may be necessary to
1453 * change the region to UNBOXED. */
1454 os_vm_size_t remaining_bytes;
1455 os_vm_size_t bytes_freed;
1456 page_index_t next_page;
1457 page_bytes_t old_bytes_used;
1459 /* FIXME: This comment is somewhat stale.
1461 * Note: Any page write-protection must be removed, else a
1462 * later scavenge_newspace may incorrectly not scavenge these
1463 * pages. This would not be necessary if they are added to the
1464 * new areas, but let's do it for them all (they'll probably
1465 * be written anyway?). */
1467 gc_assert(page_table[first_page].region_start_offset == 0);
1468 next_page = first_page;
1469 remaining_bytes = nwords*N_WORD_BYTES;
1471 while (remaining_bytes > GENCGC_CARD_BYTES) {
1472 gc_assert(page_table[next_page].gen == from_space);
1473 gc_assert(page_table[next_page].large_object);
1474 gc_assert(page_table[next_page].region_start_offset ==
1475 npage_bytes(next_page-first_page));
1476 gc_assert(page_table[next_page].bytes_used == GENCGC_CARD_BYTES);
1477 /* Should have been unprotected by unprotect_oldspace()
1478 * for boxed objects, and after promotion unboxed ones
1479 * should not be on protected pages at all. */
1480 gc_assert(!page_table[next_page].write_protected);
1483 gc_assert(page_boxed_p(next_page));
1485 gc_assert(page_allocated_no_region_p(next_page));
1486 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1488 page_table[next_page].gen = new_space;
1490 remaining_bytes -= GENCGC_CARD_BYTES;
1494 /* Now only one page remains, but the object may have shrunk so
1495 * there may be more unused pages which will be freed. */
1497 /* Object may have shrunk but shouldn't have grown - check. */
1498 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1500 page_table[next_page].gen = new_space;
1503 gc_assert(page_boxed_p(next_page));
1505 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1507 /* Adjust the bytes_used. */
1508 old_bytes_used = page_table[next_page].bytes_used;
1509 page_table[next_page].bytes_used = remaining_bytes;
1511 bytes_freed = old_bytes_used - remaining_bytes;
1513 /* Free any remaining pages; needs care. */
1515 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1516 (page_table[next_page].gen == from_space) &&
1517 /* FIXME: It is not obvious to me why this is necessary
1518 * as a loop condition: it seems to me that the
1519 * region_start_offset test should be sufficient, but
1520 * experimentally that is not the case. --NS
1523 page_boxed_p(next_page) :
1524 page_allocated_no_region_p(next_page)) &&
1525 page_table[next_page].large_object &&
1526 (page_table[next_page].region_start_offset ==
1527 npage_bytes(next_page - first_page))) {
1528 /* Checks out OK, free the page. Don't need to both zeroing
1529 * pages as this should have been done before shrinking the
1530 * object. These pages shouldn't be write-protected, even if
1531 * boxed they should be zero filled. */
1532 gc_assert(page_table[next_page].write_protected == 0);
1534 old_bytes_used = page_table[next_page].bytes_used;
1535 page_table[next_page].allocated = FREE_PAGE_FLAG;
1536 page_table[next_page].bytes_used = 0;
1537 bytes_freed += old_bytes_used;
1541 if ((bytes_freed > 0) && gencgc_verbose) {
1543 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1547 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1549 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1550 bytes_allocated -= bytes_freed;
1552 /* Add the region to the new_areas if requested. */
1554 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1559 /* Get tag of object. */
1560 tag = lowtag_of(object);
1562 /* Allocate space. */
1563 new = gc_general_alloc(nwords*N_WORD_BYTES,
1564 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1567 /* Copy the object. */
1568 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1570 /* Return Lisp pointer of new object. */
1571 return ((lispobj) new) | tag;
1576 copy_large_object(lispobj object, long nwords)
1578 return general_copy_large_object(object, nwords, 1);
1582 copy_large_unboxed_object(lispobj object, long nwords)
1584 return general_copy_large_object(object, nwords, 0);
1587 /* to copy unboxed objects */
1589 copy_unboxed_object(lispobj object, long nwords)
1591 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1596 * code and code-related objects
1599 static lispobj trans_fun_header(lispobj object);
1600 static lispobj trans_boxed(lispobj object);
1603 /* Scan a x86 compiled code object, looking for possible fixups that
1604 * have been missed after a move.
1606 * Two types of fixups are needed:
1607 * 1. Absolute fixups to within the code object.
1608 * 2. Relative fixups to outside the code object.
1610 * Currently only absolute fixups to the constant vector, or to the
1611 * code area are checked. */
1613 sniff_code_object(struct code *code, os_vm_size_t displacement)
1615 #ifdef LISP_FEATURE_X86
1616 long nheader_words, ncode_words, nwords;
1617 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1618 os_vm_address_t code_start_addr, code_end_addr;
1619 os_vm_address_t code_addr = (os_vm_address_t)code;
1620 int fixup_found = 0;
1622 if (!check_code_fixups)
1625 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1627 ncode_words = fixnum_value(code->code_size);
1628 nheader_words = HeaderValue(*(lispobj *)code);
1629 nwords = ncode_words + nheader_words;
1631 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1632 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1633 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1634 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1636 /* Work through the unboxed code. */
1637 for (p = code_start_addr; p < code_end_addr; p++) {
1638 void *data = *(void **)p;
1639 unsigned d1 = *((unsigned char *)p - 1);
1640 unsigned d2 = *((unsigned char *)p - 2);
1641 unsigned d3 = *((unsigned char *)p - 3);
1642 unsigned d4 = *((unsigned char *)p - 4);
1644 unsigned d5 = *((unsigned char *)p - 5);
1645 unsigned d6 = *((unsigned char *)p - 6);
1648 /* Check for code references. */
1649 /* Check for a 32 bit word that looks like an absolute
1650 reference to within the code adea of the code object. */
1651 if ((data >= (void*)(code_start_addr-displacement))
1652 && (data < (void*)(code_end_addr-displacement))) {
1653 /* function header */
1655 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1657 /* Skip the function header */
1661 /* the case of PUSH imm32 */
1665 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1666 p, d6, d5, d4, d3, d2, d1, data));
1667 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1669 /* the case of MOV [reg-8],imm32 */
1671 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1672 || d2==0x45 || d2==0x46 || d2==0x47)
1676 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1677 p, d6, d5, d4, d3, d2, d1, data));
1678 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1680 /* the case of LEA reg,[disp32] */
1681 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1684 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1685 p, d6, d5, d4, d3, d2, d1, data));
1686 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1690 /* Check for constant references. */
1691 /* Check for a 32 bit word that looks like an absolute
1692 reference to within the constant vector. Constant references
1694 if ((data >= (void*)(constants_start_addr-displacement))
1695 && (data < (void*)(constants_end_addr-displacement))
1696 && (((unsigned)data & 0x3) == 0)) {
1701 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1702 p, d6, d5, d4, d3, d2, d1, data));
1703 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1706 /* the case of MOV m32,EAX */
1710 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1711 p, d6, d5, d4, d3, d2, d1, data));
1712 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1715 /* the case of CMP m32,imm32 */
1716 if ((d1 == 0x3d) && (d2 == 0x81)) {
1719 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1720 p, d6, d5, d4, d3, d2, d1, data));
1722 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1725 /* Check for a mod=00, r/m=101 byte. */
1726 if ((d1 & 0xc7) == 5) {
1731 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1732 p, d6, d5, d4, d3, d2, d1, data));
1733 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1735 /* the case of CMP reg32,m32 */
1739 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1740 p, d6, d5, d4, d3, d2, d1, data));
1741 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1743 /* the case of MOV m32,reg32 */
1747 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1748 p, d6, d5, d4, d3, d2, d1, data));
1749 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1751 /* the case of MOV reg32,m32 */
1755 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1756 p, d6, d5, d4, d3, d2, d1, data));
1757 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1759 /* the case of LEA reg32,m32 */
1763 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1764 p, d6, d5, d4, d3, d2, d1, data));
1765 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1771 /* If anything was found, print some information on the code
1775 "/compiled code object at %x: header words = %d, code words = %d\n",
1776 code, nheader_words, ncode_words));
1778 "/const start = %x, end = %x\n",
1779 constants_start_addr, constants_end_addr));
1781 "/code start = %x, end = %x\n",
1782 code_start_addr, code_end_addr));
1788 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1790 /* x86-64 uses pc-relative addressing instead of this kludge */
1791 #ifndef LISP_FEATURE_X86_64
1792 long nheader_words, ncode_words, nwords;
1793 os_vm_address_t constants_start_addr, constants_end_addr;
1794 os_vm_address_t code_start_addr, code_end_addr;
1795 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1796 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1797 os_vm_size_t displacement = code_addr - old_addr;
1798 lispobj fixups = NIL;
1799 struct vector *fixups_vector;
1801 ncode_words = fixnum_value(new_code->code_size);
1802 nheader_words = HeaderValue(*(lispobj *)new_code);
1803 nwords = ncode_words + nheader_words;
1805 "/compiled code object at %x: header words = %d, code words = %d\n",
1806 new_code, nheader_words, ncode_words)); */
1807 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1808 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1809 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1810 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1813 "/const start = %x, end = %x\n",
1814 constants_start_addr,constants_end_addr));
1816 "/code start = %x; end = %x\n",
1817 code_start_addr,code_end_addr));
1820 /* The first constant should be a pointer to the fixups for this
1821 code objects. Check. */
1822 fixups = new_code->constants[0];
1824 /* It will be 0 or the unbound-marker if there are no fixups (as
1825 * will be the case if the code object has been purified, for
1826 * example) and will be an other pointer if it is valid. */
1827 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1828 !is_lisp_pointer(fixups)) {
1829 /* Check for possible errors. */
1830 if (check_code_fixups)
1831 sniff_code_object(new_code, displacement);
1836 fixups_vector = (struct vector *)native_pointer(fixups);
1838 /* Could be pointing to a forwarding pointer. */
1839 /* FIXME is this always in from_space? if so, could replace this code with
1840 * forwarding_pointer_p/forwarding_pointer_value */
1841 if (is_lisp_pointer(fixups) &&
1842 (find_page_index((void*)fixups_vector) != -1) &&
1843 (fixups_vector->header == 0x01)) {
1844 /* If so, then follow it. */
1845 /*SHOW("following pointer to a forwarding pointer");*/
1847 (struct vector *)native_pointer((lispobj)fixups_vector->length);
1850 /*SHOW("got fixups");*/
1852 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1853 /* Got the fixups for the code block. Now work through the vector,
1854 and apply a fixup at each address. */
1855 long length = fixnum_value(fixups_vector->length);
1857 for (i = 0; i < length; i++) {
1858 long offset = fixups_vector->data[i];
1859 /* Now check the current value of offset. */
1860 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1862 /* If it's within the old_code object then it must be an
1863 * absolute fixup (relative ones are not saved) */
1864 if ((old_value >= old_addr)
1865 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1866 /* So add the dispacement. */
1867 *(os_vm_address_t *)(code_start_addr + offset) =
1868 old_value + displacement;
1870 /* It is outside the old code object so it must be a
1871 * relative fixup (absolute fixups are not saved). So
1872 * subtract the displacement. */
1873 *(os_vm_address_t *)(code_start_addr + offset) =
1874 old_value - displacement;
1877 /* This used to just print a note to stderr, but a bogus fixup seems to
1878 * indicate real heap corruption, so a hard hailure is in order. */
1879 lose("fixup vector %p has a bad widetag: %d\n",
1880 fixups_vector, widetag_of(fixups_vector->header));
1883 /* Check for possible errors. */
1884 if (check_code_fixups) {
1885 sniff_code_object(new_code,displacement);
1892 trans_boxed_large(lispobj object)
1895 unsigned long length;
1897 gc_assert(is_lisp_pointer(object));
1899 header = *((lispobj *) native_pointer(object));
1900 length = HeaderValue(header) + 1;
1901 length = CEILING(length, 2);
1903 return copy_large_object(object, length);
1906 /* Doesn't seem to be used, delete it after the grace period. */
1909 trans_unboxed_large(lispobj object)
1912 unsigned long length;
1914 gc_assert(is_lisp_pointer(object));
1916 header = *((lispobj *) native_pointer(object));
1917 length = HeaderValue(header) + 1;
1918 length = CEILING(length, 2);
1920 return copy_large_unboxed_object(object, length);
1928 /* XX This is a hack adapted from cgc.c. These don't work too
1929 * efficiently with the gencgc as a list of the weak pointers is
1930 * maintained within the objects which causes writes to the pages. A
1931 * limited attempt is made to avoid unnecessary writes, but this needs
1933 #define WEAK_POINTER_NWORDS \
1934 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
1937 scav_weak_pointer(lispobj *where, lispobj object)
1939 /* Since we overwrite the 'next' field, we have to make
1940 * sure not to do so for pointers already in the list.
1941 * Instead of searching the list of weak_pointers each
1942 * time, we ensure that next is always NULL when the weak
1943 * pointer isn't in the list, and not NULL otherwise.
1944 * Since we can't use NULL to denote end of list, we
1945 * use a pointer back to the same weak_pointer.
1947 struct weak_pointer * wp = (struct weak_pointer*)where;
1949 if (NULL == wp->next) {
1950 wp->next = weak_pointers;
1952 if (NULL == wp->next)
1956 /* Do not let GC scavenge the value slot of the weak pointer.
1957 * (That is why it is a weak pointer.) */
1959 return WEAK_POINTER_NWORDS;
1964 search_read_only_space(void *pointer)
1966 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1967 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1968 if ((pointer < (void *)start) || (pointer >= (void *)end))
1970 return (gc_search_space(start,
1971 (((lispobj *)pointer)+2)-start,
1972 (lispobj *) pointer));
1976 search_static_space(void *pointer)
1978 lispobj *start = (lispobj *)STATIC_SPACE_START;
1979 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1980 if ((pointer < (void *)start) || (pointer >= (void *)end))
1982 return (gc_search_space(start,
1983 (((lispobj *)pointer)+2)-start,
1984 (lispobj *) pointer));
1987 /* a faster version for searching the dynamic space. This will work even
1988 * if the object is in a current allocation region. */
1990 search_dynamic_space(void *pointer)
1992 page_index_t page_index = find_page_index(pointer);
1995 /* The address may be invalid, so do some checks. */
1996 if ((page_index == -1) || page_free_p(page_index))
1998 start = (lispobj *)page_region_start(page_index);
1999 return (gc_search_space(start,
2000 (((lispobj *)pointer)+2)-start,
2001 (lispobj *)pointer));
2004 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2006 /* Is there any possibility that pointer is a valid Lisp object
2007 * reference, and/or something else (e.g. subroutine call return
2008 * address) which should prevent us from moving the referred-to thing?
2009 * This is called from preserve_pointers() */
2011 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2013 lispobj *start_addr;
2015 /* Find the object start address. */
2016 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2020 return looks_like_valid_lisp_pointer_p(pointer, start_addr);
2023 #endif // defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2025 /* Adjust large bignum and vector objects. This will adjust the
2026 * allocated region if the size has shrunk, and move unboxed objects
2027 * into unboxed pages. The pages are not promoted here, and the
2028 * promoted region is not added to the new_regions; this is really
2029 * only designed to be called from preserve_pointer(). Shouldn't fail
2030 * if this is missed, just may delay the moving of objects to unboxed
2031 * pages, and the freeing of pages. */
2033 maybe_adjust_large_object(lispobj *where)
2035 page_index_t first_page;
2036 page_index_t next_page;
2039 unsigned long remaining_bytes;
2040 unsigned long bytes_freed;
2041 unsigned long old_bytes_used;
2045 /* Check whether it's a vector or bignum object. */
2046 switch (widetag_of(where[0])) {
2047 case SIMPLE_VECTOR_WIDETAG:
2048 boxed = BOXED_PAGE_FLAG;
2050 case BIGNUM_WIDETAG:
2051 case SIMPLE_BASE_STRING_WIDETAG:
2052 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2053 case SIMPLE_CHARACTER_STRING_WIDETAG:
2055 case SIMPLE_BIT_VECTOR_WIDETAG:
2056 case SIMPLE_ARRAY_NIL_WIDETAG:
2057 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2058 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2059 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2060 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2061 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2062 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2064 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
2066 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2067 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2068 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2069 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
2071 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2072 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
2074 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2075 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2077 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2078 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2081 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
2083 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2084 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2086 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2087 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
2089 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2090 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2091 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2092 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2094 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2095 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2097 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2098 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2100 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2101 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2103 boxed = UNBOXED_PAGE_FLAG;
2109 /* Find its current size. */
2110 nwords = (sizetab[widetag_of(where[0])])(where);
2112 first_page = find_page_index((void *)where);
2113 gc_assert(first_page >= 0);
2115 /* Note: Any page write-protection must be removed, else a later
2116 * scavenge_newspace may incorrectly not scavenge these pages.
2117 * This would not be necessary if they are added to the new areas,
2118 * but lets do it for them all (they'll probably be written
2121 gc_assert(page_table[first_page].region_start_offset == 0);
2123 next_page = first_page;
2124 remaining_bytes = nwords*N_WORD_BYTES;
2125 while (remaining_bytes > GENCGC_CARD_BYTES) {
2126 gc_assert(page_table[next_page].gen == from_space);
2127 gc_assert(page_allocated_no_region_p(next_page));
2128 gc_assert(page_table[next_page].large_object);
2129 gc_assert(page_table[next_page].region_start_offset ==
2130 npage_bytes(next_page-first_page));
2131 gc_assert(page_table[next_page].bytes_used == GENCGC_CARD_BYTES);
2133 page_table[next_page].allocated = boxed;
2135 /* Shouldn't be write-protected at this stage. Essential that the
2137 gc_assert(!page_table[next_page].write_protected);
2138 remaining_bytes -= GENCGC_CARD_BYTES;
2142 /* Now only one page remains, but the object may have shrunk so
2143 * there may be more unused pages which will be freed. */
2145 /* Object may have shrunk but shouldn't have grown - check. */
2146 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2148 page_table[next_page].allocated = boxed;
2149 gc_assert(page_table[next_page].allocated ==
2150 page_table[first_page].allocated);
2152 /* Adjust the bytes_used. */
2153 old_bytes_used = page_table[next_page].bytes_used;
2154 page_table[next_page].bytes_used = remaining_bytes;
2156 bytes_freed = old_bytes_used - remaining_bytes;
2158 /* Free any remaining pages; needs care. */
2160 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2161 (page_table[next_page].gen == from_space) &&
2162 page_allocated_no_region_p(next_page) &&
2163 page_table[next_page].large_object &&
2164 (page_table[next_page].region_start_offset ==
2165 npage_bytes(next_page - first_page))) {
2166 /* It checks out OK, free the page. We don't need to both zeroing
2167 * pages as this should have been done before shrinking the
2168 * object. These pages shouldn't be write protected as they
2169 * should be zero filled. */
2170 gc_assert(page_table[next_page].write_protected == 0);
2172 old_bytes_used = page_table[next_page].bytes_used;
2173 page_table[next_page].allocated = FREE_PAGE_FLAG;
2174 page_table[next_page].bytes_used = 0;
2175 bytes_freed += old_bytes_used;
2179 if ((bytes_freed > 0) && gencgc_verbose) {
2181 "/maybe_adjust_large_object() freed %d\n",
2185 generations[from_space].bytes_allocated -= bytes_freed;
2186 bytes_allocated -= bytes_freed;
2191 /* Take a possible pointer to a Lisp object and mark its page in the
2192 * page_table so that it will not be relocated during a GC.
2194 * This involves locating the page it points to, then backing up to
2195 * the start of its region, then marking all pages dont_move from there
2196 * up to the first page that's not full or has a different generation
2198 * It is assumed that all the page static flags have been cleared at
2199 * the start of a GC.
2201 * It is also assumed that the current gc_alloc() region has been
2202 * flushed and the tables updated. */
2205 preserve_pointer(void *addr)
2207 page_index_t addr_page_index = find_page_index(addr);
2208 page_index_t first_page;
2210 unsigned int region_allocation;
2212 /* quick check 1: Address is quite likely to have been invalid. */
2213 if ((addr_page_index == -1)
2214 || page_free_p(addr_page_index)
2215 || (page_table[addr_page_index].bytes_used == 0)
2216 || (page_table[addr_page_index].gen != from_space)
2217 /* Skip if already marked dont_move. */
2218 || (page_table[addr_page_index].dont_move != 0))
2220 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2221 /* (Now that we know that addr_page_index is in range, it's
2222 * safe to index into page_table[] with it.) */
2223 region_allocation = page_table[addr_page_index].allocated;
2225 /* quick check 2: Check the offset within the page.
2228 if (((unsigned long)addr & (GENCGC_CARD_BYTES - 1)) >
2229 page_table[addr_page_index].bytes_used)
2232 /* Filter out anything which can't be a pointer to a Lisp object
2233 * (or, as a special case which also requires dont_move, a return
2234 * address referring to something in a CodeObject). This is
2235 * expensive but important, since it vastly reduces the
2236 * probability that random garbage will be bogusly interpreted as
2237 * a pointer which prevents a page from moving.
2239 * This only needs to happen on x86oids, where this is used for
2240 * conservative roots. Non-x86oid systems only ever call this
2241 * function on known-valid lisp objects. */
2242 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2243 if (!(code_page_p(addr_page_index)
2244 || (is_lisp_pointer((lispobj)addr) &&
2245 possibly_valid_dynamic_space_pointer(addr))))
2249 /* Find the beginning of the region. Note that there may be
2250 * objects in the region preceding the one that we were passed a
2251 * pointer to: if this is the case, we will write-protect all the
2252 * previous objects' pages too. */
2255 /* I think this'd work just as well, but without the assertions.
2256 * -dan 2004.01.01 */
2257 first_page = find_page_index(page_region_start(addr_page_index))
2259 first_page = addr_page_index;
2260 while (page_table[first_page].region_start_offset != 0) {
2262 /* Do some checks. */
2263 gc_assert(page_table[first_page].bytes_used == GENCGC_CARD_BYTES);
2264 gc_assert(page_table[first_page].gen == from_space);
2265 gc_assert(page_table[first_page].allocated == region_allocation);
2269 /* Adjust any large objects before promotion as they won't be
2270 * copied after promotion. */
2271 if (page_table[first_page].large_object) {
2272 maybe_adjust_large_object(page_address(first_page));
2273 /* If a large object has shrunk then addr may now point to a
2274 * free area in which case it's ignored here. Note it gets
2275 * through the valid pointer test above because the tail looks
2277 if (page_free_p(addr_page_index)
2278 || (page_table[addr_page_index].bytes_used == 0)
2279 /* Check the offset within the page. */
2280 || (((unsigned long)addr & (GENCGC_CARD_BYTES - 1))
2281 > page_table[addr_page_index].bytes_used)) {
2283 "weird? ignore ptr 0x%x to freed area of large object\n",
2287 /* It may have moved to unboxed pages. */
2288 region_allocation = page_table[first_page].allocated;
2291 /* Now work forward until the end of this contiguous area is found,
2292 * marking all pages as dont_move. */
2293 for (i = first_page; ;i++) {
2294 gc_assert(page_table[i].allocated == region_allocation);
2296 /* Mark the page static. */
2297 page_table[i].dont_move = 1;
2299 /* Move the page to the new_space. XX I'd rather not do this
2300 * but the GC logic is not quite able to copy with the static
2301 * pages remaining in the from space. This also requires the
2302 * generation bytes_allocated counters be updated. */
2303 page_table[i].gen = new_space;
2304 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2305 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2307 /* It is essential that the pages are not write protected as
2308 * they may have pointers into the old-space which need
2309 * scavenging. They shouldn't be write protected at this
2311 gc_assert(!page_table[i].write_protected);
2313 /* Check whether this is the last page in this contiguous block.. */
2314 if ((page_table[i].bytes_used < GENCGC_CARD_BYTES)
2315 /* ..or it is CARD_BYTES and is the last in the block */
2317 || (page_table[i+1].bytes_used == 0) /* next page free */
2318 || (page_table[i+1].gen != from_space) /* diff. gen */
2319 || (page_table[i+1].region_start_offset == 0))
2323 /* Check that the page is now static. */
2324 gc_assert(page_table[addr_page_index].dont_move != 0);
2327 /* If the given page is not write-protected, then scan it for pointers
2328 * to younger generations or the top temp. generation, if no
2329 * suspicious pointers are found then the page is write-protected.
2331 * Care is taken to check for pointers to the current gc_alloc()
2332 * region if it is a younger generation or the temp. generation. This
2333 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2334 * the gc_alloc_generation does not need to be checked as this is only
2335 * called from scavenge_generation() when the gc_alloc generation is
2336 * younger, so it just checks if there is a pointer to the current
2339 * We return 1 if the page was write-protected, else 0. */
2341 update_page_write_prot(page_index_t page)
2343 generation_index_t gen = page_table[page].gen;
2346 void **page_addr = (void **)page_address(page);
2347 long num_words = page_table[page].bytes_used / N_WORD_BYTES;
2349 /* Shouldn't be a free page. */
2350 gc_assert(page_allocated_p(page));
2351 gc_assert(page_table[page].bytes_used != 0);
2353 /* Skip if it's already write-protected, pinned, or unboxed */
2354 if (page_table[page].write_protected
2355 /* FIXME: What's the reason for not write-protecting pinned pages? */
2356 || page_table[page].dont_move
2357 || page_unboxed_p(page))
2360 /* Scan the page for pointers to younger generations or the
2361 * top temp. generation. */
2363 for (j = 0; j < num_words; j++) {
2364 void *ptr = *(page_addr+j);
2365 page_index_t index = find_page_index(ptr);
2367 /* Check that it's in the dynamic space */
2369 if (/* Does it point to a younger or the temp. generation? */
2370 (page_allocated_p(index)
2371 && (page_table[index].bytes_used != 0)
2372 && ((page_table[index].gen < gen)
2373 || (page_table[index].gen == SCRATCH_GENERATION)))
2375 /* Or does it point within a current gc_alloc() region? */
2376 || ((boxed_region.start_addr <= ptr)
2377 && (ptr <= boxed_region.free_pointer))
2378 || ((unboxed_region.start_addr <= ptr)
2379 && (ptr <= unboxed_region.free_pointer))) {
2386 /* Write-protect the page. */
2387 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2389 os_protect((void *)page_addr,
2391 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2393 /* Note the page as protected in the page tables. */
2394 page_table[page].write_protected = 1;
2400 /* Scavenge all generations from FROM to TO, inclusive, except for
2401 * new_space which needs special handling, as new objects may be
2402 * added which are not checked here - use scavenge_newspace generation.
2404 * Write-protected pages should not have any pointers to the
2405 * from_space so do need scavenging; thus write-protected pages are
2406 * not always scavenged. There is some code to check that these pages
2407 * are not written; but to check fully the write-protected pages need
2408 * to be scavenged by disabling the code to skip them.
2410 * Under the current scheme when a generation is GCed the younger
2411 * generations will be empty. So, when a generation is being GCed it
2412 * is only necessary to scavenge the older generations for pointers
2413 * not the younger. So a page that does not have pointers to younger
2414 * generations does not need to be scavenged.
2416 * The write-protection can be used to note pages that don't have
2417 * pointers to younger pages. But pages can be written without having
2418 * pointers to younger generations. After the pages are scavenged here
2419 * they can be scanned for pointers to younger generations and if
2420 * there are none the page can be write-protected.
2422 * One complication is when the newspace is the top temp. generation.
2424 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2425 * that none were written, which they shouldn't be as they should have
2426 * no pointers to younger generations. This breaks down for weak
2427 * pointers as the objects contain a link to the next and are written
2428 * if a weak pointer is scavenged. Still it's a useful check. */
2430 scavenge_generations(generation_index_t from, generation_index_t to)
2433 page_index_t num_wp = 0;
2437 /* Clear the write_protected_cleared flags on all pages. */
2438 for (i = 0; i < page_table_pages; i++)
2439 page_table[i].write_protected_cleared = 0;
2442 for (i = 0; i < last_free_page; i++) {
2443 generation_index_t generation = page_table[i].gen;
2445 && (page_table[i].bytes_used != 0)
2446 && (generation != new_space)
2447 && (generation >= from)
2448 && (generation <= to)) {
2449 page_index_t last_page,j;
2450 int write_protected=1;
2452 /* This should be the start of a region */
2453 gc_assert(page_table[i].region_start_offset == 0);
2455 /* Now work forward until the end of the region */
2456 for (last_page = i; ; last_page++) {
2458 write_protected && page_table[last_page].write_protected;
2459 if ((page_table[last_page].bytes_used < GENCGC_CARD_BYTES)
2460 /* Or it is CARD_BYTES and is the last in the block */
2461 || (!page_boxed_p(last_page+1))
2462 || (page_table[last_page+1].bytes_used == 0)
2463 || (page_table[last_page+1].gen != generation)
2464 || (page_table[last_page+1].region_start_offset == 0))
2467 if (!write_protected) {
2468 scavenge(page_address(i),
2469 ((unsigned long)(page_table[last_page].bytes_used
2470 + npage_bytes(last_page-i)))
2473 /* Now scan the pages and write protect those that
2474 * don't have pointers to younger generations. */
2475 if (enable_page_protection) {
2476 for (j = i; j <= last_page; j++) {
2477 num_wp += update_page_write_prot(j);
2480 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2482 "/write protected %d pages within generation %d\n",
2483 num_wp, generation));
2491 /* Check that none of the write_protected pages in this generation
2492 * have been written to. */
2493 for (i = 0; i < page_table_pages; i++) {
2494 if (page_allocated_p(i)
2495 && (page_table[i].bytes_used != 0)
2496 && (page_table[i].gen == generation)
2497 && (page_table[i].write_protected_cleared != 0)) {
2498 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2500 "/page bytes_used=%d region_start_offset=%lu dont_move=%d\n",
2501 page_table[i].bytes_used,
2502 page_table[i].region_start_offset,
2503 page_table[i].dont_move));
2504 lose("write to protected page %d in scavenge_generation()\n", i);
2511 /* Scavenge a newspace generation. As it is scavenged new objects may
2512 * be allocated to it; these will also need to be scavenged. This
2513 * repeats until there are no more objects unscavenged in the
2514 * newspace generation.
2516 * To help improve the efficiency, areas written are recorded by
2517 * gc_alloc() and only these scavenged. Sometimes a little more will be
2518 * scavenged, but this causes no harm. An easy check is done that the
2519 * scavenged bytes equals the number allocated in the previous
2522 * Write-protected pages are not scanned except if they are marked
2523 * dont_move in which case they may have been promoted and still have
2524 * pointers to the from space.
2526 * Write-protected pages could potentially be written by alloc however
2527 * to avoid having to handle re-scavenging of write-protected pages
2528 * gc_alloc() does not write to write-protected pages.
2530 * New areas of objects allocated are recorded alternatively in the two
2531 * new_areas arrays below. */
2532 static struct new_area new_areas_1[NUM_NEW_AREAS];
2533 static struct new_area new_areas_2[NUM_NEW_AREAS];
2535 /* Do one full scan of the new space generation. This is not enough to
2536 * complete the job as new objects may be added to the generation in
2537 * the process which are not scavenged. */
2539 scavenge_newspace_generation_one_scan(generation_index_t generation)
2544 "/starting one full scan of newspace generation %d\n",
2546 for (i = 0; i < last_free_page; i++) {
2547 /* Note that this skips over open regions when it encounters them. */
2549 && (page_table[i].bytes_used != 0)
2550 && (page_table[i].gen == generation)
2551 && ((page_table[i].write_protected == 0)
2552 /* (This may be redundant as write_protected is now
2553 * cleared before promotion.) */
2554 || (page_table[i].dont_move == 1))) {
2555 page_index_t last_page;
2558 /* The scavenge will start at the region_start_offset of
2561 * We need to find the full extent of this contiguous
2562 * block in case objects span pages.
2564 * Now work forward until the end of this contiguous area
2565 * is found. A small area is preferred as there is a
2566 * better chance of its pages being write-protected. */
2567 for (last_page = i; ;last_page++) {
2568 /* If all pages are write-protected and movable,
2569 * then no need to scavenge */
2570 all_wp=all_wp && page_table[last_page].write_protected &&
2571 !page_table[last_page].dont_move;
2573 /* Check whether this is the last page in this
2574 * contiguous block */
2575 if ((page_table[last_page].bytes_used < GENCGC_CARD_BYTES)
2576 /* Or it is CARD_BYTES and is the last in the block */
2577 || (!page_boxed_p(last_page+1))
2578 || (page_table[last_page+1].bytes_used == 0)
2579 || (page_table[last_page+1].gen != generation)
2580 || (page_table[last_page+1].region_start_offset == 0))
2584 /* Do a limited check for write-protected pages. */
2586 long nwords = (((unsigned long)
2587 (page_table[last_page].bytes_used
2588 + npage_bytes(last_page-i)
2589 + page_table[i].region_start_offset))
2591 new_areas_ignore_page = last_page;
2593 scavenge(page_region_start(i), nwords);
2600 "/done with one full scan of newspace generation %d\n",
2604 /* Do a complete scavenge of the newspace generation. */
2606 scavenge_newspace_generation(generation_index_t generation)
2610 /* the new_areas array currently being written to by gc_alloc() */
2611 struct new_area (*current_new_areas)[] = &new_areas_1;
2612 size_t current_new_areas_index;
2614 /* the new_areas created by the previous scavenge cycle */
2615 struct new_area (*previous_new_areas)[] = NULL;
2616 size_t previous_new_areas_index;
2618 /* Flush the current regions updating the tables. */
2619 gc_alloc_update_all_page_tables();
2621 /* Turn on the recording of new areas by gc_alloc(). */
2622 new_areas = current_new_areas;
2623 new_areas_index = 0;
2625 /* Don't need to record new areas that get scavenged anyway during
2626 * scavenge_newspace_generation_one_scan. */
2627 record_new_objects = 1;
2629 /* Start with a full scavenge. */
2630 scavenge_newspace_generation_one_scan(generation);
2632 /* Record all new areas now. */
2633 record_new_objects = 2;
2635 /* Give a chance to weak hash tables to make other objects live.
2636 * FIXME: The algorithm implemented here for weak hash table gcing
2637 * is O(W^2+N) as Bruno Haible warns in
2638 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2639 * see "Implementation 2". */
2640 scav_weak_hash_tables();
2642 /* Flush the current regions updating the tables. */
2643 gc_alloc_update_all_page_tables();
2645 /* Grab new_areas_index. */
2646 current_new_areas_index = new_areas_index;
2649 "The first scan is finished; current_new_areas_index=%d.\n",
2650 current_new_areas_index));*/
2652 while (current_new_areas_index > 0) {
2653 /* Move the current to the previous new areas */
2654 previous_new_areas = current_new_areas;
2655 previous_new_areas_index = current_new_areas_index;
2657 /* Scavenge all the areas in previous new areas. Any new areas
2658 * allocated are saved in current_new_areas. */
2660 /* Allocate an array for current_new_areas; alternating between
2661 * new_areas_1 and 2 */
2662 if (previous_new_areas == &new_areas_1)
2663 current_new_areas = &new_areas_2;
2665 current_new_areas = &new_areas_1;
2667 /* Set up for gc_alloc(). */
2668 new_areas = current_new_areas;
2669 new_areas_index = 0;
2671 /* Check whether previous_new_areas had overflowed. */
2672 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2674 /* New areas of objects allocated have been lost so need to do a
2675 * full scan to be sure! If this becomes a problem try
2676 * increasing NUM_NEW_AREAS. */
2677 if (gencgc_verbose) {
2678 SHOW("new_areas overflow, doing full scavenge");
2681 /* Don't need to record new areas that get scavenged
2682 * anyway during scavenge_newspace_generation_one_scan. */
2683 record_new_objects = 1;
2685 scavenge_newspace_generation_one_scan(generation);
2687 /* Record all new areas now. */
2688 record_new_objects = 2;
2690 scav_weak_hash_tables();
2692 /* Flush the current regions updating the tables. */
2693 gc_alloc_update_all_page_tables();
2697 /* Work through previous_new_areas. */
2698 for (i = 0; i < previous_new_areas_index; i++) {
2699 page_index_t page = (*previous_new_areas)[i].page;
2700 size_t offset = (*previous_new_areas)[i].offset;
2701 size_t size = (*previous_new_areas)[i].size / N_WORD_BYTES;
2702 gc_assert((*previous_new_areas)[i].size % N_WORD_BYTES == 0);
2703 scavenge(page_address(page)+offset, size);
2706 scav_weak_hash_tables();
2708 /* Flush the current regions updating the tables. */
2709 gc_alloc_update_all_page_tables();
2712 current_new_areas_index = new_areas_index;
2715 "The re-scan has finished; current_new_areas_index=%d.\n",
2716 current_new_areas_index));*/
2719 /* Turn off recording of areas allocated by gc_alloc(). */
2720 record_new_objects = 0;
2725 /* Check that none of the write_protected pages in this generation
2726 * have been written to. */
2727 for (i = 0; i < page_table_pages; i++) {
2728 if (page_allocated_p(i)
2729 && (page_table[i].bytes_used != 0)
2730 && (page_table[i].gen == generation)
2731 && (page_table[i].write_protected_cleared != 0)
2732 && (page_table[i].dont_move == 0)) {
2733 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2734 i, generation, page_table[i].dont_move);
2741 /* Un-write-protect all the pages in from_space. This is done at the
2742 * start of a GC else there may be many page faults while scavenging
2743 * the newspace (I've seen drive the system time to 99%). These pages
2744 * would need to be unprotected anyway before unmapping in
2745 * free_oldspace; not sure what effect this has on paging.. */
2747 unprotect_oldspace(void)
2750 void *region_addr = 0;
2751 void *page_addr = 0;
2752 unsigned long region_bytes = 0;
2754 for (i = 0; i < last_free_page; i++) {
2755 if (page_allocated_p(i)
2756 && (page_table[i].bytes_used != 0)
2757 && (page_table[i].gen == from_space)) {
2759 /* Remove any write-protection. We should be able to rely
2760 * on the write-protect flag to avoid redundant calls. */
2761 if (page_table[i].write_protected) {
2762 page_table[i].write_protected = 0;
2763 page_addr = page_address(i);
2766 region_addr = page_addr;
2767 region_bytes = GENCGC_CARD_BYTES;
2768 } else if (region_addr + region_bytes == page_addr) {
2769 /* Region continue. */
2770 region_bytes += GENCGC_CARD_BYTES;
2772 /* Unprotect previous region. */
2773 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2774 /* First page in new region. */
2775 region_addr = page_addr;
2776 region_bytes = GENCGC_CARD_BYTES;
2782 /* Unprotect last region. */
2783 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2787 /* Work through all the pages and free any in from_space. This
2788 * assumes that all objects have been copied or promoted to an older
2789 * generation. Bytes_allocated and the generation bytes_allocated
2790 * counter are updated. The number of bytes freed is returned. */
2791 static unsigned long
2794 unsigned long bytes_freed = 0;
2795 page_index_t first_page, last_page;
2800 /* Find a first page for the next region of pages. */
2801 while ((first_page < last_free_page)
2802 && (page_free_p(first_page)
2803 || (page_table[first_page].bytes_used == 0)
2804 || (page_table[first_page].gen != from_space)))
2807 if (first_page >= last_free_page)
2810 /* Find the last page of this region. */
2811 last_page = first_page;
2814 /* Free the page. */
2815 bytes_freed += page_table[last_page].bytes_used;
2816 generations[page_table[last_page].gen].bytes_allocated -=
2817 page_table[last_page].bytes_used;
2818 page_table[last_page].allocated = FREE_PAGE_FLAG;
2819 page_table[last_page].bytes_used = 0;
2820 /* Should already be unprotected by unprotect_oldspace(). */
2821 gc_assert(!page_table[last_page].write_protected);
2824 while ((last_page < last_free_page)
2825 && page_allocated_p(last_page)
2826 && (page_table[last_page].bytes_used != 0)
2827 && (page_table[last_page].gen == from_space));
2829 #ifdef READ_PROTECT_FREE_PAGES
2830 os_protect(page_address(first_page),
2831 npage_bytes(last_page-first_page),
2834 first_page = last_page;
2835 } while (first_page < last_free_page);
2837 bytes_allocated -= bytes_freed;
2842 /* Print some information about a pointer at the given address. */
2844 print_ptr(lispobj *addr)
2846 /* If addr is in the dynamic space then out the page information. */
2847 page_index_t pi1 = find_page_index((void*)addr);
2850 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
2853 page_table[pi1].allocated,
2854 page_table[pi1].gen,
2855 page_table[pi1].bytes_used,
2856 page_table[pi1].region_start_offset,
2857 page_table[pi1].dont_move);
2858 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
2872 is_in_stack_space(lispobj ptr)
2874 /* For space verification: Pointers can be valid if they point
2875 * to a thread stack space. This would be faster if the thread
2876 * structures had page-table entries as if they were part of
2877 * the heap space. */
2879 for_each_thread(th) {
2880 if ((th->control_stack_start <= (lispobj *)ptr) &&
2881 (th->control_stack_end >= (lispobj *)ptr)) {
2889 verify_space(lispobj *start, size_t words)
2891 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
2892 int is_in_readonly_space =
2893 (READ_ONLY_SPACE_START <= (unsigned long)start &&
2894 (unsigned long)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
2898 lispobj thing = *(lispobj*)start;
2900 if (is_lisp_pointer(thing)) {
2901 page_index_t page_index = find_page_index((void*)thing);
2902 long to_readonly_space =
2903 (READ_ONLY_SPACE_START <= thing &&
2904 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
2905 long to_static_space =
2906 (STATIC_SPACE_START <= thing &&
2907 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
2909 /* Does it point to the dynamic space? */
2910 if (page_index != -1) {
2911 /* If it's within the dynamic space it should point to a used
2912 * page. XX Could check the offset too. */
2913 if (page_allocated_p(page_index)
2914 && (page_table[page_index].bytes_used == 0))
2915 lose ("Ptr %p @ %p sees free page.\n", thing, start);
2916 /* Check that it doesn't point to a forwarding pointer! */
2917 if (*((lispobj *)native_pointer(thing)) == 0x01) {
2918 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
2920 /* Check that its not in the RO space as it would then be a
2921 * pointer from the RO to the dynamic space. */
2922 if (is_in_readonly_space) {
2923 lose("ptr to dynamic space %p from RO space %x\n",
2926 /* Does it point to a plausible object? This check slows
2927 * it down a lot (so it's commented out).
2929 * "a lot" is serious: it ate 50 minutes cpu time on
2930 * my duron 950 before I came back from lunch and
2933 * FIXME: Add a variable to enable this
2936 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
2937 lose("ptr %p to invalid object %p\n", thing, start);
2941 extern void funcallable_instance_tramp;
2942 /* Verify that it points to another valid space. */
2943 if (!to_readonly_space && !to_static_space
2944 && (thing != (lispobj)&funcallable_instance_tramp)
2945 && !is_in_stack_space(thing)) {
2946 lose("Ptr %p @ %p sees junk.\n", thing, start);
2950 if (!(fixnump(thing))) {
2952 switch(widetag_of(*start)) {
2955 case SIMPLE_VECTOR_WIDETAG:
2957 case COMPLEX_WIDETAG:
2958 case SIMPLE_ARRAY_WIDETAG:
2959 case COMPLEX_BASE_STRING_WIDETAG:
2960 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
2961 case COMPLEX_CHARACTER_STRING_WIDETAG:
2963 case COMPLEX_VECTOR_NIL_WIDETAG:
2964 case COMPLEX_BIT_VECTOR_WIDETAG:
2965 case COMPLEX_VECTOR_WIDETAG:
2966 case COMPLEX_ARRAY_WIDETAG:
2967 case CLOSURE_HEADER_WIDETAG:
2968 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2969 case VALUE_CELL_HEADER_WIDETAG:
2970 case SYMBOL_HEADER_WIDETAG:
2971 case CHARACTER_WIDETAG:
2972 #if N_WORD_BITS == 64
2973 case SINGLE_FLOAT_WIDETAG:
2975 case UNBOUND_MARKER_WIDETAG:
2980 case INSTANCE_HEADER_WIDETAG:
2983 long ntotal = HeaderValue(thing);
2984 lispobj layout = ((struct instance *)start)->slots[0];
2989 nuntagged = ((struct layout *)
2990 native_pointer(layout))->n_untagged_slots;
2991 verify_space(start + 1,
2992 ntotal - fixnum_value(nuntagged));
2996 case CODE_HEADER_WIDETAG:
2998 lispobj object = *start;
3000 long nheader_words, ncode_words, nwords;
3002 struct simple_fun *fheaderp;
3004 code = (struct code *) start;
3006 /* Check that it's not in the dynamic space.
3007 * FIXME: Isn't is supposed to be OK for code
3008 * objects to be in the dynamic space these days? */
3009 if (is_in_dynamic_space
3010 /* It's ok if it's byte compiled code. The trace
3011 * table offset will be a fixnum if it's x86
3012 * compiled code - check.
3014 * FIXME: #^#@@! lack of abstraction here..
3015 * This line can probably go away now that
3016 * there's no byte compiler, but I've got
3017 * too much to worry about right now to try
3018 * to make sure. -- WHN 2001-10-06 */
3019 && fixnump(code->trace_table_offset)
3020 /* Only when enabled */
3021 && verify_dynamic_code_check) {
3023 "/code object at %p in the dynamic space\n",
3027 ncode_words = fixnum_value(code->code_size);
3028 nheader_words = HeaderValue(object);
3029 nwords = ncode_words + nheader_words;
3030 nwords = CEILING(nwords, 2);
3031 /* Scavenge the boxed section of the code data block */
3032 verify_space(start + 1, nheader_words - 1);
3034 /* Scavenge the boxed section of each function
3035 * object in the code data block. */
3036 fheaderl = code->entry_points;
3037 while (fheaderl != NIL) {
3039 (struct simple_fun *) native_pointer(fheaderl);
3040 gc_assert(widetag_of(fheaderp->header) ==
3041 SIMPLE_FUN_HEADER_WIDETAG);
3042 verify_space(&fheaderp->name, 1);
3043 verify_space(&fheaderp->arglist, 1);
3044 verify_space(&fheaderp->type, 1);
3045 fheaderl = fheaderp->next;
3051 /* unboxed objects */
3052 case BIGNUM_WIDETAG:
3053 #if N_WORD_BITS != 64
3054 case SINGLE_FLOAT_WIDETAG:
3056 case DOUBLE_FLOAT_WIDETAG:
3057 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3058 case LONG_FLOAT_WIDETAG:
3060 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3061 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3063 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3064 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3066 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3067 case COMPLEX_LONG_FLOAT_WIDETAG:
3069 case SIMPLE_BASE_STRING_WIDETAG:
3070 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3071 case SIMPLE_CHARACTER_STRING_WIDETAG:
3073 case SIMPLE_BIT_VECTOR_WIDETAG:
3074 case SIMPLE_ARRAY_NIL_WIDETAG:
3075 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3076 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3077 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3078 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3079 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3080 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3082 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG:
3084 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3085 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3086 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3087 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG:
3089 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3090 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG:
3092 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3093 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3095 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3096 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3099 case SIMPLE_ARRAY_FIXNUM_WIDETAG:
3101 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3102 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3104 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3105 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG:
3107 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3108 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3109 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3110 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3112 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3113 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3115 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3116 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3118 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3119 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3122 case WEAK_POINTER_WIDETAG:
3123 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3124 case NO_TLS_VALUE_MARKER_WIDETAG:
3126 count = (sizetab[widetag_of(*start)])(start);
3130 lose("Unhandled widetag %p at %p\n",
3131 widetag_of(*start), start);
3143 /* FIXME: It would be nice to make names consistent so that
3144 * foo_size meant size *in* *bytes* instead of size in some
3145 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3146 * Some counts of lispobjs are called foo_count; it might be good
3147 * to grep for all foo_size and rename the appropriate ones to
3149 long read_only_space_size =
3150 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3151 - (lispobj*)READ_ONLY_SPACE_START;
3152 long static_space_size =
3153 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3154 - (lispobj*)STATIC_SPACE_START;
3156 for_each_thread(th) {
3157 long binding_stack_size =
3158 (lispobj*)get_binding_stack_pointer(th)
3159 - (lispobj*)th->binding_stack_start;
3160 verify_space(th->binding_stack_start, binding_stack_size);
3162 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3163 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3167 verify_generation(generation_index_t generation)
3171 for (i = 0; i < last_free_page; i++) {
3172 if (page_allocated_p(i)
3173 && (page_table[i].bytes_used != 0)
3174 && (page_table[i].gen == generation)) {
3175 page_index_t last_page;
3176 int region_allocation = page_table[i].allocated;
3178 /* This should be the start of a contiguous block */
3179 gc_assert(page_table[i].region_start_offset == 0);
3181 /* Need to find the full extent of this contiguous block in case
3182 objects span pages. */
3184 /* Now work forward until the end of this contiguous area is
3186 for (last_page = i; ;last_page++)
3187 /* Check whether this is the last page in this contiguous
3189 if ((page_table[last_page].bytes_used < GENCGC_CARD_BYTES)
3190 /* Or it is CARD_BYTES and is the last in the block */
3191 || (page_table[last_page+1].allocated != region_allocation)
3192 || (page_table[last_page+1].bytes_used == 0)
3193 || (page_table[last_page+1].gen != generation)
3194 || (page_table[last_page+1].region_start_offset == 0))
3197 verify_space(page_address(i),
3199 (page_table[last_page].bytes_used
3200 + npage_bytes(last_page-i)))
3207 /* Check that all the free space is zero filled. */
3209 verify_zero_fill(void)
3213 for (page = 0; page < last_free_page; page++) {
3214 if (page_free_p(page)) {
3215 /* The whole page should be zero filled. */
3216 long *start_addr = (long *)page_address(page);
3219 for (i = 0; i < size; i++) {
3220 if (start_addr[i] != 0) {
3221 lose("free page not zero at %x\n", start_addr + i);
3225 long free_bytes = GENCGC_CARD_BYTES - page_table[page].bytes_used;
3226 if (free_bytes > 0) {
3227 long *start_addr = (long *)((unsigned long)page_address(page)
3228 + page_table[page].bytes_used);
3229 long size = free_bytes / N_WORD_BYTES;
3231 for (i = 0; i < size; i++) {
3232 if (start_addr[i] != 0) {
3233 lose("free region not zero at %x\n", start_addr + i);
3241 /* External entry point for verify_zero_fill */
3243 gencgc_verify_zero_fill(void)
3245 /* Flush the alloc regions updating the tables. */
3246 gc_alloc_update_all_page_tables();
3247 SHOW("verifying zero fill");
3252 verify_dynamic_space(void)
3254 generation_index_t i;
3256 for (i = 0; i <= HIGHEST_NORMAL_GENERATION; i++)
3257 verify_generation(i);
3259 if (gencgc_enable_verify_zero_fill)
3263 /* Write-protect all the dynamic boxed pages in the given generation. */
3265 write_protect_generation_pages(generation_index_t generation)
3269 gc_assert(generation < SCRATCH_GENERATION);
3271 for (start = 0; start < last_free_page; start++) {
3272 if (protect_page_p(start, generation)) {
3276 /* Note the page as protected in the page tables. */
3277 page_table[start].write_protected = 1;
3279 for (last = start + 1; last < last_free_page; last++) {
3280 if (!protect_page_p(last, generation))
3282 page_table[last].write_protected = 1;
3285 page_start = (void *)page_address(start);
3287 os_protect(page_start,
3288 npage_bytes(last - start),
3289 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3295 if (gencgc_verbose > 1) {
3297 "/write protected %d of %d pages in generation %d\n",
3298 count_write_protect_generation_pages(generation),
3299 count_generation_pages(generation),
3304 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3306 preserve_context_registers (os_context_t *c)
3309 /* On Darwin the signal context isn't a contiguous block of memory,
3310 * so just preserve_pointering its contents won't be sufficient.
3312 #if defined(LISP_FEATURE_DARWIN)
3313 #if defined LISP_FEATURE_X86
3314 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3315 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3316 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3317 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3318 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3319 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3320 preserve_pointer((void*)*os_context_pc_addr(c));
3321 #elif defined LISP_FEATURE_X86_64
3322 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3323 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3324 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3325 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3326 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3327 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3328 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3329 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3330 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3331 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3332 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3333 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3334 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3335 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3336 preserve_pointer((void*)*os_context_pc_addr(c));
3338 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3341 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3342 preserve_pointer(*ptr);
3347 /* Garbage collect a generation. If raise is 0 then the remains of the
3348 * generation are not raised to the next generation. */
3350 garbage_collect_generation(generation_index_t generation, int raise)
3352 unsigned long bytes_freed;
3354 unsigned long static_space_size;
3357 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3359 /* The oldest generation can't be raised. */
3360 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3362 /* Check if weak hash tables were processed in the previous GC. */
3363 gc_assert(weak_hash_tables == NULL);
3365 /* Initialize the weak pointer list. */
3366 weak_pointers = NULL;
3368 /* When a generation is not being raised it is transported to a
3369 * temporary generation (NUM_GENERATIONS), and lowered when
3370 * done. Set up this new generation. There should be no pages
3371 * allocated to it yet. */
3373 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3376 /* Set the global src and dest. generations */
3377 from_space = generation;
3379 new_space = generation+1;
3381 new_space = SCRATCH_GENERATION;
3383 /* Change to a new space for allocation, resetting the alloc_start_page */
3384 gc_alloc_generation = new_space;
3385 generations[new_space].alloc_start_page = 0;
3386 generations[new_space].alloc_unboxed_start_page = 0;
3387 generations[new_space].alloc_large_start_page = 0;
3388 generations[new_space].alloc_large_unboxed_start_page = 0;
3390 /* Before any pointers are preserved, the dont_move flags on the
3391 * pages need to be cleared. */
3392 for (i = 0; i < last_free_page; i++)
3393 if(page_table[i].gen==from_space)
3394 page_table[i].dont_move = 0;
3396 /* Un-write-protect the old-space pages. This is essential for the
3397 * promoted pages as they may contain pointers into the old-space
3398 * which need to be scavenged. It also helps avoid unnecessary page
3399 * faults as forwarding pointers are written into them. They need to
3400 * be un-protected anyway before unmapping later. */
3401 unprotect_oldspace();
3403 /* Scavenge the stacks' conservative roots. */
3405 /* there are potentially two stacks for each thread: the main
3406 * stack, which may contain Lisp pointers, and the alternate stack.
3407 * We don't ever run Lisp code on the altstack, but it may
3408 * host a sigcontext with lisp objects in it */
3410 /* what we need to do: (1) find the stack pointer for the main
3411 * stack; scavenge it (2) find the interrupt context on the
3412 * alternate stack that might contain lisp values, and scavenge
3415 /* we assume that none of the preceding applies to the thread that
3416 * initiates GC. If you ever call GC from inside an altstack
3417 * handler, you will lose. */
3419 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3420 /* And if we're saving a core, there's no point in being conservative. */
3421 if (conservative_stack) {
3422 for_each_thread(th) {
3424 void **esp=(void **)-1;
3425 #ifdef LISP_FEATURE_SB_THREAD
3427 if(th==arch_os_get_current_thread()) {
3428 /* Somebody is going to burn in hell for this, but casting
3429 * it in two steps shuts gcc up about strict aliasing. */
3430 esp = (void **)((void *)&raise);
3433 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3434 for(i=free-1;i>=0;i--) {
3435 os_context_t *c=th->interrupt_contexts[i];
3436 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3437 if (esp1>=(void **)th->control_stack_start &&
3438 esp1<(void **)th->control_stack_end) {
3439 if(esp1<esp) esp=esp1;
3440 preserve_context_registers(c);
3445 esp = (void **)((void *)&raise);
3447 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3448 preserve_pointer(*ptr);
3453 /* Non-x86oid systems don't have "conservative roots" as such, but
3454 * the same mechanism is used for objects pinned for use by alien
3456 for_each_thread(th) {
3457 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3458 while (pin_list != NIL) {
3459 struct cons *list_entry =
3460 (struct cons *)native_pointer(pin_list);
3461 preserve_pointer(list_entry->car);
3462 pin_list = list_entry->cdr;
3468 if (gencgc_verbose > 1) {
3469 long num_dont_move_pages = count_dont_move_pages();
3471 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
3472 num_dont_move_pages,
3473 npage_bytes(num_dont_move_pages));
3477 /* Scavenge all the rest of the roots. */
3479 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3481 * If not x86, we need to scavenge the interrupt context(s) and the
3486 for_each_thread(th) {
3487 scavenge_interrupt_contexts(th);
3488 scavenge_control_stack(th);
3491 /* Scrub the unscavenged control stack space, so that we can't run
3492 * into any stale pointers in a later GC (this is done by the
3493 * stop-for-gc handler in the other threads). */
3494 scrub_control_stack();
3498 /* Scavenge the Lisp functions of the interrupt handlers, taking
3499 * care to avoid SIG_DFL and SIG_IGN. */
3500 for (i = 0; i < NSIG; i++) {
3501 union interrupt_handler handler = interrupt_handlers[i];
3502 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3503 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3504 scavenge((lispobj *)(interrupt_handlers + i), 1);
3507 /* Scavenge the binding stacks. */
3510 for_each_thread(th) {
3511 long len= (lispobj *)get_binding_stack_pointer(th) -
3512 th->binding_stack_start;
3513 scavenge((lispobj *) th->binding_stack_start,len);
3514 #ifdef LISP_FEATURE_SB_THREAD
3515 /* do the tls as well */
3516 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3517 (sizeof (struct thread))/(sizeof (lispobj));
3518 scavenge((lispobj *) (th+1),len);
3523 /* The original CMU CL code had scavenge-read-only-space code
3524 * controlled by the Lisp-level variable
3525 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
3526 * wasn't documented under what circumstances it was useful or
3527 * safe to turn it on, so it's been turned off in SBCL. If you
3528 * want/need this functionality, and can test and document it,
3529 * please submit a patch. */
3531 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
3532 unsigned long read_only_space_size =
3533 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
3534 (lispobj*)READ_ONLY_SPACE_START;
3536 "/scavenge read only space: %d bytes\n",
3537 read_only_space_size * sizeof(lispobj)));
3538 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
3542 /* Scavenge static space. */
3544 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
3545 (lispobj *)STATIC_SPACE_START;
3546 if (gencgc_verbose > 1) {
3548 "/scavenge static space: %d bytes\n",
3549 static_space_size * sizeof(lispobj)));
3551 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
3553 /* All generations but the generation being GCed need to be
3554 * scavenged. The new_space generation needs special handling as
3555 * objects may be moved in - it is handled separately below. */
3556 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3558 /* Finally scavenge the new_space generation. Keep going until no
3559 * more objects are moved into the new generation */
3560 scavenge_newspace_generation(new_space);
3562 /* FIXME: I tried reenabling this check when debugging unrelated
3563 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3564 * Since the current GC code seems to work well, I'm guessing that
3565 * this debugging code is just stale, but I haven't tried to
3566 * figure it out. It should be figured out and then either made to
3567 * work or just deleted. */
3568 #define RESCAN_CHECK 0
3570 /* As a check re-scavenge the newspace once; no new objects should
3573 os_vm_size_t old_bytes_allocated = bytes_allocated;
3574 os_vm_size_t bytes_allocated;
3576 /* Start with a full scavenge. */
3577 scavenge_newspace_generation_one_scan(new_space);
3579 /* Flush the current regions, updating the tables. */
3580 gc_alloc_update_all_page_tables();
3582 bytes_allocated = bytes_allocated - old_bytes_allocated;
3584 if (bytes_allocated != 0) {
3585 lose("Rescan of new_space allocated %d more bytes.\n",
3591 scan_weak_hash_tables();
3592 scan_weak_pointers();
3594 /* Flush the current regions, updating the tables. */
3595 gc_alloc_update_all_page_tables();
3597 /* Free the pages in oldspace, but not those marked dont_move. */
3598 bytes_freed = free_oldspace();
3600 /* If the GC is not raising the age then lower the generation back
3601 * to its normal generation number */
3603 for (i = 0; i < last_free_page; i++)
3604 if ((page_table[i].bytes_used != 0)
3605 && (page_table[i].gen == SCRATCH_GENERATION))
3606 page_table[i].gen = generation;
3607 gc_assert(generations[generation].bytes_allocated == 0);
3608 generations[generation].bytes_allocated =
3609 generations[SCRATCH_GENERATION].bytes_allocated;
3610 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3613 /* Reset the alloc_start_page for generation. */
3614 generations[generation].alloc_start_page = 0;
3615 generations[generation].alloc_unboxed_start_page = 0;
3616 generations[generation].alloc_large_start_page = 0;
3617 generations[generation].alloc_large_unboxed_start_page = 0;
3619 if (generation >= verify_gens) {
3620 if (gencgc_verbose) {
3624 verify_dynamic_space();
3627 /* Set the new gc trigger for the GCed generation. */
3628 generations[generation].gc_trigger =
3629 generations[generation].bytes_allocated
3630 + generations[generation].bytes_consed_between_gc;
3633 generations[generation].num_gc = 0;
3635 ++generations[generation].num_gc;
3639 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3641 update_dynamic_space_free_pointer(void)
3643 page_index_t last_page = -1, i;
3645 for (i = 0; i < last_free_page; i++)
3646 if (page_allocated_p(i) && (page_table[i].bytes_used != 0))
3649 last_free_page = last_page+1;
3651 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3652 return 0; /* dummy value: return something ... */
3656 remap_page_range (page_index_t from, page_index_t to)
3658 /* There's a mysterious Solaris/x86 problem with using mmap
3659 * tricks for memory zeroing. See sbcl-devel thread
3660 * "Re: patch: standalone executable redux".
3662 #if defined(LISP_FEATURE_SUNOS)
3663 zero_and_mark_pages(from, to);
3666 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3667 release_mask = release_granularity-1,
3669 aligned_from = (from+release_mask)&~release_mask,
3670 aligned_end = (end&~release_mask);
3672 if (aligned_from < aligned_end) {
3673 zero_pages_with_mmap(aligned_from, aligned_end-1);
3674 if (aligned_from != from)
3675 zero_and_mark_pages(from, aligned_from-1);
3676 if (aligned_end != end)
3677 zero_and_mark_pages(aligned_end, end-1);
3679 zero_and_mark_pages(from, to);
3685 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3687 page_index_t first_page, last_page;
3690 return remap_page_range(from, to);
3692 for (first_page = from; first_page <= to; first_page++) {
3693 if (page_allocated_p(first_page) ||
3694 (page_table[first_page].need_to_zero == 0))
3697 last_page = first_page + 1;
3698 while (page_free_p(last_page) &&
3699 (last_page <= to) &&
3700 (page_table[last_page].need_to_zero == 1))
3703 remap_page_range(first_page, last_page-1);
3705 first_page = last_page;
3709 generation_index_t small_generation_limit = 1;
3711 /* GC all generations newer than last_gen, raising the objects in each
3712 * to the next older generation - we finish when all generations below
3713 * last_gen are empty. Then if last_gen is due for a GC, or if
3714 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3715 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3717 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3718 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3720 collect_garbage(generation_index_t last_gen)
3722 generation_index_t gen = 0, i;
3723 int raise, more = 0;
3725 /* The largest value of last_free_page seen since the time
3726 * remap_free_pages was called. */
3727 static page_index_t high_water_mark = 0;
3729 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3730 log_generation_stats(gc_logfile, "=== GC Start ===");
3734 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3736 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3741 /* Flush the alloc regions updating the tables. */
3742 gc_alloc_update_all_page_tables();
3744 /* Verify the new objects created by Lisp code. */
3745 if (pre_verify_gen_0) {
3746 FSHOW((stderr, "pre-checking generation 0\n"));
3747 verify_generation(0);
3750 if (gencgc_verbose > 1)
3751 print_generation_stats();
3754 /* Collect the generation. */
3756 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3757 /* Never raise the oldest generation. Never raise the extra generation
3758 * collected due to more-flag. */
3764 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3765 /* If we would not normally raise this one, but we're
3766 * running low on space in comparison to the object-sizes
3767 * we've been seeing, raise it and collect the next one
3769 if (!raise && gen == last_gen) {
3770 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3775 if (gencgc_verbose > 1) {
3777 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3780 generations[gen].bytes_allocated,
3781 generations[gen].gc_trigger,
3782 generations[gen].num_gc));
3785 /* If an older generation is being filled, then update its
3788 generations[gen+1].cum_sum_bytes_allocated +=
3789 generations[gen+1].bytes_allocated;
3792 garbage_collect_generation(gen, raise);
3794 /* Reset the memory age cum_sum. */
3795 generations[gen].cum_sum_bytes_allocated = 0;
3797 if (gencgc_verbose > 1) {
3798 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3799 print_generation_stats();
3803 } while ((gen <= gencgc_oldest_gen_to_gc)
3804 && ((gen < last_gen)
3807 && (generations[gen].bytes_allocated
3808 > generations[gen].gc_trigger)
3809 && (generation_average_age(gen)
3810 > generations[gen].minimum_age_before_gc))));
3812 /* Now if gen-1 was raised all generations before gen are empty.
3813 * If it wasn't raised then all generations before gen-1 are empty.
3815 * Now objects within this gen's pages cannot point to younger
3816 * generations unless they are written to. This can be exploited
3817 * by write-protecting the pages of gen; then when younger
3818 * generations are GCed only the pages which have been written
3823 gen_to_wp = gen - 1;
3825 /* There's not much point in WPing pages in generation 0 as it is
3826 * never scavenged (except promoted pages). */
3827 if ((gen_to_wp > 0) && enable_page_protection) {
3828 /* Check that they are all empty. */
3829 for (i = 0; i < gen_to_wp; i++) {
3830 if (generations[i].bytes_allocated)
3831 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3834 write_protect_generation_pages(gen_to_wp);
3837 /* Set gc_alloc() back to generation 0. The current regions should
3838 * be flushed after the above GCs. */
3839 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3840 gc_alloc_generation = 0;
3842 /* Save the high-water mark before updating last_free_page */
3843 if (last_free_page > high_water_mark)
3844 high_water_mark = last_free_page;
3846 update_dynamic_space_free_pointer();
3848 /* Update auto_gc_trigger. Make sure we trigger the next GC before
3849 * running out of heap! */
3850 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
3851 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
3853 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
3856 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
3859 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
3862 if (gen > small_generation_limit) {
3863 if (last_free_page > high_water_mark)
3864 high_water_mark = last_free_page;
3865 remap_free_pages(0, high_water_mark, 0);
3866 high_water_mark = 0;
3870 large_allocation = 0;
3872 log_generation_stats(gc_logfile, "=== GC End ===");
3873 SHOW("returning from collect_garbage");
3876 /* This is called by Lisp PURIFY when it is finished. All live objects
3877 * will have been moved to the RO and Static heaps. The dynamic space
3878 * will need a full re-initialization. We don't bother having Lisp
3879 * PURIFY flush the current gc_alloc() region, as the page_tables are
3880 * re-initialized, and every page is zeroed to be sure. */
3884 page_index_t page, last_page;
3886 if (gencgc_verbose > 1) {
3887 SHOW("entering gc_free_heap");
3890 for (page = 0; page < page_table_pages; page++) {
3891 /* Skip free pages which should already be zero filled. */
3892 if (page_allocated_p(page)) {
3894 for (last_page = page;
3895 (last_page < page_table_pages) && page_allocated_p(last_page);
3897 /* Mark the page free. The other slots are assumed invalid
3898 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
3899 * should not be write-protected -- except that the
3900 * generation is used for the current region but it sets
3902 page_table[page].allocated = FREE_PAGE_FLAG;
3903 page_table[page].bytes_used = 0;
3904 page_table[page].write_protected = 0;
3907 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure
3908 * about this change. */
3909 page_start = (void *)page_address(page);
3910 os_protect(page_start, npage_bytes(last_page-page), OS_VM_PROT_ALL);
3911 remap_free_pages(page, last_page-1, 1);
3914 } else if (gencgc_zero_check_during_free_heap) {
3915 /* Double-check that the page is zero filled. */
3918 gc_assert(page_free_p(page));
3919 gc_assert(page_table[page].bytes_used == 0);
3920 page_start = (long *)page_address(page);
3921 for (i=0; i<GENCGC_CARD_BYTES/sizeof(long); i++) {
3922 if (page_start[i] != 0) {
3923 lose("free region not zero at %x\n", page_start + i);
3929 bytes_allocated = 0;
3931 /* Initialize the generations. */
3932 for (page = 0; page < NUM_GENERATIONS; page++) {
3933 generations[page].alloc_start_page = 0;
3934 generations[page].alloc_unboxed_start_page = 0;
3935 generations[page].alloc_large_start_page = 0;
3936 generations[page].alloc_large_unboxed_start_page = 0;
3937 generations[page].bytes_allocated = 0;
3938 generations[page].gc_trigger = 2000000;
3939 generations[page].num_gc = 0;
3940 generations[page].cum_sum_bytes_allocated = 0;
3943 if (gencgc_verbose > 1)
3944 print_generation_stats();
3946 /* Initialize gc_alloc(). */
3947 gc_alloc_generation = 0;
3949 gc_set_region_empty(&boxed_region);
3950 gc_set_region_empty(&unboxed_region);
3953 set_alloc_pointer((lispobj)((char *)heap_base));
3955 if (verify_after_free_heap) {
3956 /* Check whether purify has left any bad pointers. */
3957 FSHOW((stderr, "checking after free_heap\n"));
3967 /* Compute the number of pages needed for the dynamic space.
3968 * Dynamic space size should be aligned on page size. */
3969 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
3970 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
3972 /* Default nursery size to 5% of the total dynamic space size,
3974 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
3975 if (bytes_consed_between_gcs < (1024*1024))
3976 bytes_consed_between_gcs = 1024*1024;
3978 /* The page_table must be allocated using "calloc" to initialize
3979 * the page structures correctly. There used to be a separate
3980 * initialization loop (now commented out; see below) but that was
3981 * unnecessary and did hurt startup time. */
3982 page_table = calloc(page_table_pages, sizeof(struct page));
3983 gc_assert(page_table);
3986 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
3987 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
3989 heap_base = (void*)DYNAMIC_SPACE_START;
3991 /* The page structures are initialized implicitly when page_table
3992 * is allocated with "calloc" above. Formerly we had the following
3993 * explicit initialization here (comments converted to C99 style
3994 * for readability as C's block comments don't nest):
3996 * // Initialize each page structure.
3997 * for (i = 0; i < page_table_pages; i++) {
3998 * // Initialize all pages as free.
3999 * page_table[i].allocated = FREE_PAGE_FLAG;
4000 * page_table[i].bytes_used = 0;
4002 * // Pages are not write-protected at startup.
4003 * page_table[i].write_protected = 0;
4006 * Without this loop the image starts up much faster when dynamic
4007 * space is large -- which it is on 64-bit platforms already by
4008 * default -- and when "calloc" for large arrays is implemented
4009 * using copy-on-write of a page of zeroes -- which it is at least
4010 * on Linux. In this case the pages that page_table_pages is stored
4011 * in are mapped and cleared not before the corresponding part of
4012 * dynamic space is used. For example, this saves clearing 16 MB of
4013 * memory at startup if the page size is 4 KB and the size of
4014 * dynamic space is 4 GB.
4015 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4016 * asserted below: */
4018 /* Compile time assertion: If triggered, declares an array
4019 * of dimension -1 forcing a syntax error. The intent of the
4020 * assignment is to avoid an "unused variable" warning. */
4021 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4022 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4025 bytes_allocated = 0;
4027 /* Initialize the generations.
4029 * FIXME: very similar to code in gc_free_heap(), should be shared */
4030 for (i = 0; i < NUM_GENERATIONS; i++) {
4031 generations[i].alloc_start_page = 0;
4032 generations[i].alloc_unboxed_start_page = 0;
4033 generations[i].alloc_large_start_page = 0;
4034 generations[i].alloc_large_unboxed_start_page = 0;
4035 generations[i].bytes_allocated = 0;
4036 generations[i].gc_trigger = 2000000;
4037 generations[i].num_gc = 0;
4038 generations[i].cum_sum_bytes_allocated = 0;
4039 /* the tune-able parameters */
4040 generations[i].bytes_consed_between_gc = bytes_consed_between_gcs;
4041 generations[i].number_of_gcs_before_promotion = 1;
4042 generations[i].minimum_age_before_gc = 0.75;
4045 /* Initialize gc_alloc. */
4046 gc_alloc_generation = 0;
4047 gc_set_region_empty(&boxed_region);
4048 gc_set_region_empty(&unboxed_region);
4053 /* Pick up the dynamic space from after a core load.
4055 * The ALLOCATION_POINTER points to the end of the dynamic space.
4059 gencgc_pickup_dynamic(void)
4061 page_index_t page = 0;
4062 void *alloc_ptr = (void *)get_alloc_pointer();
4063 lispobj *prev=(lispobj *)page_address(page);
4064 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4066 lispobj *first,*ptr= (lispobj *)page_address(page);
4068 if (!gencgc_partial_pickup || page_allocated_p(page)) {
4069 /* It is possible, though rare, for the saved page table
4070 * to contain free pages below alloc_ptr. */
4071 page_table[page].gen = gen;
4072 page_table[page].bytes_used = GENCGC_CARD_BYTES;
4073 page_table[page].large_object = 0;
4074 page_table[page].write_protected = 0;
4075 page_table[page].write_protected_cleared = 0;
4076 page_table[page].dont_move = 0;
4077 page_table[page].need_to_zero = 1;
4080 if (!gencgc_partial_pickup) {
4081 page_table[page].allocated = BOXED_PAGE_FLAG;
4082 first=gc_search_space(prev,(ptr+2)-prev,ptr);
4085 page_table[page].region_start_offset =
4086 page_address(page) - (void *)prev;
4089 } while (page_address(page) < alloc_ptr);
4091 last_free_page = page;
4093 generations[gen].bytes_allocated = npage_bytes(page);
4094 bytes_allocated = npage_bytes(page);
4096 gc_alloc_update_all_page_tables();
4097 write_protect_generation_pages(gen);
4101 gc_initialize_pointers(void)
4103 gencgc_pickup_dynamic();
4107 /* alloc(..) is the external interface for memory allocation. It
4108 * allocates to generation 0. It is not called from within the garbage
4109 * collector as it is only external uses that need the check for heap
4110 * size (GC trigger) and to disable the interrupts (interrupts are
4111 * always disabled during a GC).
4113 * The vops that call alloc(..) assume that the returned space is zero-filled.
4114 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4116 * The check for a GC trigger is only performed when the current
4117 * region is full, so in most cases it's not needed. */
4119 static inline lispobj *
4120 general_alloc_internal(long nbytes, int page_type_flag, struct alloc_region *region,
4121 struct thread *thread)
4123 #ifndef LISP_FEATURE_WIN32
4124 lispobj alloc_signal;
4127 void *new_free_pointer;
4128 os_vm_size_t trigger_bytes = 0;
4130 gc_assert(nbytes>0);
4132 /* Check for alignment allocation problems. */
4133 gc_assert((((unsigned long)region->free_pointer & LOWTAG_MASK) == 0)
4134 && ((nbytes & LOWTAG_MASK) == 0));
4136 /* Must be inside a PA section. */
4137 gc_assert(get_pseudo_atomic_atomic(thread));
4139 if (nbytes > large_allocation)
4140 large_allocation = nbytes;
4142 /* maybe we can do this quickly ... */
4143 new_free_pointer = region->free_pointer + nbytes;
4144 if (new_free_pointer <= region->end_addr) {
4145 new_obj = (void*)(region->free_pointer);
4146 region->free_pointer = new_free_pointer;
4147 return(new_obj); /* yup */
4150 /* We don't want to count nbytes against auto_gc_trigger unless we
4151 * have to: it speeds up the tenuring of objects and slows down
4152 * allocation. However, unless we do so when allocating _very_
4153 * large objects we are in danger of exhausting the heap without
4154 * running sufficient GCs.
4156 if (nbytes >= bytes_consed_between_gcs)
4157 trigger_bytes = nbytes;
4159 /* we have to go the long way around, it seems. Check whether we
4160 * should GC in the near future
4162 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4163 /* Don't flood the system with interrupts if the need to gc is
4164 * already noted. This can happen for example when SUB-GC
4165 * allocates or after a gc triggered in a WITHOUT-GCING. */
4166 if (SymbolValue(GC_PENDING,thread) == NIL) {
4167 /* set things up so that GC happens when we finish the PA
4169 SetSymbolValue(GC_PENDING,T,thread);
4170 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4171 set_pseudo_atomic_interrupted(thread);
4172 #ifdef LISP_FEATURE_PPC
4173 /* PPC calls alloc() from a trap or from pa_alloc(),
4174 * look up the most context if it's from a trap. */
4176 os_context_t *context =
4177 thread->interrupt_data->allocation_trap_context;
4178 maybe_save_gc_mask_and_block_deferrables
4179 (context ? os_context_sigmask_addr(context) : NULL);
4182 maybe_save_gc_mask_and_block_deferrables(NULL);
4187 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4189 #ifndef LISP_FEATURE_WIN32
4190 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4191 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4192 if ((signed long) alloc_signal <= 0) {
4193 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4196 SetSymbolValue(ALLOC_SIGNAL,
4197 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4207 general_alloc(long nbytes, int page_type_flag)
4209 struct thread *thread = arch_os_get_current_thread();
4210 /* Select correct region, and call general_alloc_internal with it.
4211 * For other then boxed allocation we must lock first, since the
4212 * region is shared. */
4213 if (BOXED_PAGE_FLAG & page_type_flag) {
4214 #ifdef LISP_FEATURE_SB_THREAD
4215 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4217 struct alloc_region *region = &boxed_region;
4219 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4220 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4222 gc_assert(0 == thread_mutex_lock(&allocation_lock));
4223 obj = general_alloc_internal(nbytes, page_type_flag, &unboxed_region, thread);
4224 gc_assert(0 == thread_mutex_unlock(&allocation_lock));
4227 lose("bad page type flag: %d", page_type_flag);
4234 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4235 return general_alloc(nbytes, BOXED_PAGE_FLAG);
4239 * shared support for the OS-dependent signal handlers which
4240 * catch GENCGC-related write-protect violations
4242 void unhandled_sigmemoryfault(void* addr);
4244 /* Depending on which OS we're running under, different signals might
4245 * be raised for a violation of write protection in the heap. This
4246 * function factors out the common generational GC magic which needs
4247 * to invoked in this case, and should be called from whatever signal
4248 * handler is appropriate for the OS we're running under.
4250 * Return true if this signal is a normal generational GC thing that
4251 * we were able to handle, or false if it was abnormal and control
4252 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4254 * We have two control flags for this: one causes us to ignore faults
4255 * on unprotected pages completely, and the second complains to stderr
4256 * but allows us to continue without losing.
4258 extern boolean ignore_memoryfaults_on_unprotected_pages;
4259 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4261 extern boolean continue_after_memoryfault_on_unprotected_pages;
4262 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4265 gencgc_handle_wp_violation(void* fault_addr)
4267 page_index_t page_index = find_page_index(fault_addr);
4270 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4271 fault_addr, page_index));
4274 /* Check whether the fault is within the dynamic space. */
4275 if (page_index == (-1)) {
4277 /* It can be helpful to be able to put a breakpoint on this
4278 * case to help diagnose low-level problems. */
4279 unhandled_sigmemoryfault(fault_addr);
4281 /* not within the dynamic space -- not our responsibility */
4286 ret = thread_mutex_lock(&free_pages_lock);
4287 gc_assert(ret == 0);
4288 if (page_table[page_index].write_protected) {
4289 /* Unprotect the page. */
4290 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4291 page_table[page_index].write_protected_cleared = 1;
4292 page_table[page_index].write_protected = 0;
4293 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4294 /* The only acceptable reason for this signal on a heap
4295 * access is that GENCGC write-protected the page.
4296 * However, if two CPUs hit a wp page near-simultaneously,
4297 * we had better not have the second one lose here if it
4298 * does this test after the first one has already set wp=0
4300 if(page_table[page_index].write_protected_cleared != 1) {
4301 void lisp_backtrace(int frames);
4304 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4305 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4306 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4307 " page.region_start_offset: %"OS_VM_SIZE_FMT"\n"
4308 " page.bytes_used: %"PAGE_BYTES_FMT"\n"
4309 " page.allocated: %d\n"
4310 " page.write_protected: %d\n"
4311 " page.write_protected_cleared: %d\n"
4312 " page.generation: %d\n",
4315 boxed_region.first_page,
4316 boxed_region.last_page,
4317 page_table[page_index].region_start_offset,
4318 page_table[page_index].bytes_used,
4319 page_table[page_index].allocated,
4320 page_table[page_index].write_protected,
4321 page_table[page_index].write_protected_cleared,
4322 page_table[page_index].gen);
4323 if (!continue_after_memoryfault_on_unprotected_pages)
4327 ret = thread_mutex_unlock(&free_pages_lock);
4328 gc_assert(ret == 0);
4329 /* Don't worry, we can handle it. */
4333 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4334 * it's not just a case of the program hitting the write barrier, and
4335 * are about to let Lisp deal with it. It's basically just a
4336 * convenient place to set a gdb breakpoint. */
4338 unhandled_sigmemoryfault(void *addr)
4341 void gc_alloc_update_all_page_tables(void)
4343 /* Flush the alloc regions updating the tables. */
4346 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4347 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4348 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4352 gc_set_region_empty(struct alloc_region *region)
4354 region->first_page = 0;
4355 region->last_page = -1;
4356 region->start_addr = page_address(0);
4357 region->free_pointer = page_address(0);
4358 region->end_addr = page_address(0);
4362 zero_all_free_pages()
4366 for (i = 0; i < last_free_page; i++) {
4367 if (page_free_p(i)) {
4368 #ifdef READ_PROTECT_FREE_PAGES
4369 os_protect(page_address(i),
4378 /* Things to do before doing a final GC before saving a core (without
4381 * + Pages in large_object pages aren't moved by the GC, so we need to
4382 * unset that flag from all pages.
4383 * + The pseudo-static generation isn't normally collected, but it seems
4384 * reasonable to collect it at least when saving a core. So move the
4385 * pages to a normal generation.
4388 prepare_for_final_gc ()
4391 for (i = 0; i < last_free_page; i++) {
4392 page_table[i].large_object = 0;
4393 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4394 int used = page_table[i].bytes_used;
4395 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4396 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4397 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4403 /* Do a non-conservative GC, and then save a core with the initial
4404 * function being set to the value of the static symbol
4405 * SB!VM:RESTART-LISP-FUNCTION */
4407 gc_and_save(char *filename, boolean prepend_runtime,
4408 boolean save_runtime_options,
4409 boolean compressed, int compression_level)
4412 void *runtime_bytes = NULL;
4413 size_t runtime_size;
4415 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4420 conservative_stack = 0;
4422 /* The filename might come from Lisp, and be moved by the now
4423 * non-conservative GC. */
4424 filename = strdup(filename);
4426 /* Collect twice: once into relatively high memory, and then back
4427 * into low memory. This compacts the retained data into the lower
4428 * pages, minimizing the size of the core file.
4430 prepare_for_final_gc();
4431 gencgc_alloc_start_page = last_free_page;
4432 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4434 prepare_for_final_gc();
4435 gencgc_alloc_start_page = -1;
4436 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4438 if (prepend_runtime)
4439 save_runtime_to_filehandle(file, runtime_bytes, runtime_size);
4441 /* The dumper doesn't know that pages need to be zeroed before use. */
4442 zero_all_free_pages();
4443 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4444 prepend_runtime, save_runtime_options,
4445 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4446 /* Oops. Save still managed to fail. Since we've mangled the stack
4447 * beyond hope, there's not much we can do.
4448 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4449 * going to be rather unsatisfactory too... */
4450 lose("Attempt to save core after non-conservative GC failed.\n");