Define fun_code_header in C for symmetry with Lisp
[sbcl.git] / src / runtime / gencgc.c
bloba90e9f59ae8b85ca3773317668b27b75281070f9
1 /*
2 * GENerational Conservative Garbage Collector for SBCL
3 */
5 /*
6 * This software is part of the SBCL system. See the README file for
7 * more information.
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>
23 * as
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <errno.h>
30 #include <string.h>
31 #include "sbcl.h"
32 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
33 #include "pthreads_win32.h"
34 #else
35 #include <signal.h>
36 #endif
37 #include "runtime.h"
38 #include "os.h"
39 #include "interr.h"
40 #include "globals.h"
41 #include "interrupt.h"
42 #include "validate.h"
43 #include "lispregs.h"
44 #include "arch.h"
45 #include "gc.h"
46 #include "gc-internal.h"
47 #include "thread.h"
48 #include "pseudo-atomic.h"
49 #include "alloc.h"
50 #include "genesis/gc-tables.h"
51 #include "genesis/vector.h"
52 #include "genesis/weak-pointer.h"
53 #include "genesis/fdefn.h"
54 #include "genesis/simple-fun.h"
55 #include "save.h"
56 #include "genesis/hash-table.h"
57 #include "genesis/instance.h"
58 #include "genesis/layout.h"
59 #include "gencgc.h"
60 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
61 #include "genesis/cons.h"
62 #endif
63 #ifdef LISP_FEATURE_X86
64 #include "forwarding-ptr.h"
65 #endif
67 /* forward declarations */
68 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
69 int page_type_flag);
73 * GC parameters
76 /* As usually configured, generations 0-5 are normal collected generations,
77 6 is pseudo-static (the objects in which are never moved nor reclaimed),
78 and 7 is scratch space used when collecting a generation without promotion,
79 wherein it is moved to generation 7 and back again.
81 enum {
82 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
83 NUM_GENERATIONS
86 /* Should we use page protection to help avoid the scavenging of pages
87 * that don't have pointers to younger generations? */
88 boolean enable_page_protection = 1;
90 /* Largest allocation seen since last GC. */
91 os_vm_size_t large_allocation = 0;
95 * debugging
98 /* the verbosity level. All non-error messages are disabled at level 0;
99 * and only a few rare messages are printed at level 1. */
100 #if QSHOW == 2
101 boolean gencgc_verbose = 1;
102 #else
103 boolean gencgc_verbose = 0;
104 #endif
106 /* FIXME: At some point enable the various error-checking things below
107 * and see what they say. */
109 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
110 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
111 * check. */
112 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
114 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
115 boolean pre_verify_gen_0 = 0;
117 /* Should we print a note when code objects are found in the dynamic space
118 * during a heap verify? */
119 boolean verify_dynamic_code_check = 0;
121 #ifdef LISP_FEATURE_X86
122 /* Should we check code objects for fixup errors after they are transported? */
123 boolean check_code_fixups = 0;
124 #endif
126 /* Should we check that newly allocated regions are zero filled? */
127 boolean gencgc_zero_check = 0;
129 /* Should we check that the free space is zero filled? */
130 boolean gencgc_enable_verify_zero_fill = 0;
132 /* When loading a core, don't do a full scan of the memory for the
133 * memory region boundaries. (Set to true by coreparse.c if the core
134 * contained a pagetable entry).
136 boolean gencgc_partial_pickup = 0;
138 /* If defined, free pages are read-protected to ensure that nothing
139 * accesses them.
142 /* #define READ_PROTECT_FREE_PAGES */
146 * GC structures and variables
149 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
150 os_vm_size_t bytes_allocated = 0;
151 os_vm_size_t auto_gc_trigger = 0;
153 /* the source and destination generations. These are set before a GC starts
154 * scavenging. */
155 generation_index_t from_space;
156 generation_index_t new_space;
158 /* Set to 1 when in GC */
159 boolean gc_active_p = 0;
161 /* should the GC be conservative on stack. If false (only right before
162 * saving a core), don't scan the stack / mark pages dont_move. */
163 static boolean conservative_stack = 1;
165 /* An array of page structures is allocated on gc initialization.
166 * This helps to quickly map between an address and its page structure.
167 * page_table_pages is set from the size of the dynamic space. */
168 page_index_t page_table_pages;
169 struct page *page_table;
171 in_use_marker_t *page_table_pinned_dwords;
172 size_t pins_map_size_in_bytes;
174 /* In GC cards that have conservative pointers to them, should we wipe out
175 * dwords in there that are not used, so that they do not act as false
176 * root to other things in the heap from then on? This is a new feature
177 * but in testing it is both reliable and no noticeable slowdown. */
178 int do_wipe_p = 1;
180 static inline boolean page_allocated_p(page_index_t page) {
181 return (page_table[page].allocated != FREE_PAGE_FLAG);
184 static inline boolean page_no_region_p(page_index_t page) {
185 return !(page_table[page].allocated & OPEN_REGION_PAGE_FLAG);
188 static inline boolean page_allocated_no_region_p(page_index_t page) {
189 return ((page_table[page].allocated & (UNBOXED_PAGE_FLAG | BOXED_PAGE_FLAG))
190 && page_no_region_p(page));
193 static inline boolean page_free_p(page_index_t page) {
194 return (page_table[page].allocated == FREE_PAGE_FLAG);
197 static inline boolean page_boxed_p(page_index_t page) {
198 return (page_table[page].allocated & BOXED_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_bytes_used(page) != 0)
214 && !page_table[page].dont_move
215 && (page_table[page].gen == generation));
218 /* Calculate the start address for the given page number. */
219 inline void *
220 page_address(page_index_t page_num)
222 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
225 /* Calculate the address where the allocation region associated with
226 * the page starts. */
227 static inline void *
228 page_scan_start(page_index_t page_index)
230 return page_address(page_index)-page_scan_start_offset(page_index);
233 /* True if the page starts a contiguous block. */
234 static inline boolean
235 page_starts_contiguous_block_p(page_index_t page_index)
237 return page_scan_start_offset(page_index) == 0;
240 /* True if the page is the last page in a contiguous block. */
241 static inline boolean
242 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
244 return (/* page doesn't fill block */
245 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
246 /* page is last allocated page */
247 || ((page_index + 1) >= last_free_page)
248 /* next page free */
249 || page_free_p(page_index + 1)
250 /* next page contains no data */
251 || (page_bytes_used(page_index + 1) == 0)
252 /* next page is in different generation */
253 || (page_table[page_index + 1].gen != gen)
254 /* next page starts its own contiguous block */
255 || (page_starts_contiguous_block_p(page_index + 1)));
258 /// External function for calling from Lisp.
259 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
261 static os_vm_size_t
262 npage_bytes(page_index_t npages)
264 gc_assert(npages>=0);
265 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
268 /* Check that X is a higher address than Y and return offset from Y to
269 * X in bytes. */
270 static inline os_vm_size_t
271 void_diff(void *x, void *y)
273 gc_assert(x >= y);
274 return (pointer_sized_uint_t)x - (pointer_sized_uint_t)y;
277 /* a structure to hold the state of a generation
279 * CAUTION: If you modify this, make sure to touch up the alien
280 * definition in src/code/gc.lisp accordingly. ...or better yes,
281 * deal with the FIXME there...
283 struct generation {
285 /* the first page that gc_alloc() checks on its next call */
286 page_index_t alloc_start_page;
288 /* the first page that gc_alloc_unboxed() checks on its next call */
289 page_index_t alloc_unboxed_start_page;
291 /* the first page that gc_alloc_large (boxed) considers on its next
292 * call. (Although it always allocates after the boxed_region.) */
293 page_index_t alloc_large_start_page;
295 /* the first page that gc_alloc_large (unboxed) considers on its
296 * next call. (Although it always allocates after the
297 * current_unboxed_region.) */
298 page_index_t alloc_large_unboxed_start_page;
300 /* the bytes allocated to this generation */
301 os_vm_size_t bytes_allocated;
303 /* the number of bytes at which to trigger a GC */
304 os_vm_size_t gc_trigger;
306 /* to calculate a new level for gc_trigger */
307 os_vm_size_t bytes_consed_between_gc;
309 /* the number of GCs since the last raise */
310 int num_gc;
312 /* the number of GCs to run on the generations before raising objects to the
313 * next generation */
314 int number_of_gcs_before_promotion;
316 /* the cumulative sum of the bytes allocated to this generation. It is
317 * cleared after a GC on this generations, and update before new
318 * objects are added from a GC of a younger generation. Dividing by
319 * the bytes_allocated will give the average age of the memory in
320 * this generation since its last GC. */
321 os_vm_size_t cum_sum_bytes_allocated;
323 /* a minimum average memory age before a GC will occur helps
324 * prevent a GC when a large number of new live objects have been
325 * added, in which case a GC could be a waste of time */
326 double minimum_age_before_gc;
329 /* an array of generation structures. There needs to be one more
330 * generation structure than actual generations as the oldest
331 * generation is temporarily raised then lowered. */
332 struct generation generations[NUM_GENERATIONS];
334 /* the oldest generation that is will currently be GCed by default.
335 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
337 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
339 * Setting this to 0 effectively disables the generational nature of
340 * the GC. In some applications generational GC may not be useful
341 * because there are no long-lived objects.
343 * An intermediate value could be handy after moving long-lived data
344 * into an older generation so an unnecessary GC of this long-lived
345 * data can be avoided. */
346 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
348 /* META: Is nobody aside from me bothered by this especially misleading
349 * use of the word "last"? It could mean either "ultimate" or "prior",
350 * but in fact means neither. It is the *FIRST* page that should be grabbed
351 * for more space, so it is min free page, or 1+ the max used page. */
352 /* The maximum free page in the heap is maintained and used to update
353 * ALLOCATION_POINTER which is used by the room function to limit its
354 * search of the heap. XX Gencgc obviously needs to be better
355 * integrated with the Lisp code. */
357 page_index_t last_free_page;
359 #ifdef LISP_FEATURE_SB_THREAD
360 /* This lock is to prevent multiple threads from simultaneously
361 * allocating new regions which overlap each other. Note that the
362 * majority of GC is single-threaded, but alloc() may be called from
363 * >1 thread at a time and must be thread-safe. This lock must be
364 * seized before all accesses to generations[] or to parts of
365 * page_table[] that other threads may want to see */
366 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
367 /* This lock is used to protect non-thread-local allocation. */
368 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
369 #endif
371 extern os_vm_size_t gencgc_release_granularity;
372 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
374 extern os_vm_size_t gencgc_alloc_granularity;
375 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
379 * miscellaneous heap functions
382 /* Count the number of pages which are write-protected within the
383 * given generation. */
384 static page_index_t
385 count_write_protect_generation_pages(generation_index_t generation)
387 page_index_t i, count = 0;
389 for (i = 0; i < last_free_page; i++)
390 if (page_allocated_p(i)
391 && (page_table[i].gen == generation)
392 && (page_table[i].write_protected == 1))
393 count++;
394 return count;
397 /* Count the number of pages within the given generation. */
398 static page_index_t
399 count_generation_pages(generation_index_t generation)
401 page_index_t i;
402 page_index_t count = 0;
404 for (i = 0; i < last_free_page; i++)
405 if (page_allocated_p(i)
406 && (page_table[i].gen == generation))
407 count++;
408 return count;
411 #if QSHOW
412 static page_index_t
413 count_dont_move_pages(void)
415 page_index_t i;
416 page_index_t count = 0;
417 for (i = 0; i < last_free_page; i++) {
418 if (page_allocated_p(i)
419 && (page_table[i].dont_move != 0)) {
420 ++count;
423 return count;
425 #endif /* QSHOW */
427 /* Work through the pages and add up the number of bytes used for the
428 * given generation. */
429 static __attribute__((unused)) os_vm_size_t
430 count_generation_bytes_allocated (generation_index_t gen)
432 page_index_t i;
433 os_vm_size_t result = 0;
434 for (i = 0; i < last_free_page; i++) {
435 if (page_allocated_p(i)
436 && (page_table[i].gen == gen))
437 result += page_bytes_used(i);
439 return result;
442 /* Return the average age of the memory in a generation. */
443 extern double
444 generation_average_age(generation_index_t gen)
446 if (generations[gen].bytes_allocated == 0)
447 return 0.0;
449 return
450 ((double)generations[gen].cum_sum_bytes_allocated)
451 / ((double)generations[gen].bytes_allocated);
454 #ifdef LISP_FEATURE_X86
455 extern void fpu_save(void *);
456 extern void fpu_restore(void *);
457 #endif
459 extern void
460 write_generation_stats(FILE *file)
462 generation_index_t i;
464 #ifdef LISP_FEATURE_X86
465 int fpu_state[27];
467 /* Can end up here after calling alloc_tramp which doesn't prepare
468 * the x87 state, and the C ABI uses a different mode */
469 fpu_save(fpu_state);
470 #endif
472 /* Print the heap stats. */
473 fprintf(file,
474 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
476 for (i = 0; i < SCRATCH_GENERATION; i++) {
477 page_index_t j;
478 page_index_t boxed_cnt = 0;
479 page_index_t unboxed_cnt = 0;
480 page_index_t large_boxed_cnt = 0;
481 page_index_t large_unboxed_cnt = 0;
482 page_index_t pinned_cnt=0;
484 for (j = 0; j < last_free_page; j++)
485 if (page_table[j].gen == i) {
487 /* Count the number of boxed pages within the given
488 * generation. */
489 if (page_boxed_p(j)) {
490 if (page_table[j].large_object)
491 large_boxed_cnt++;
492 else
493 boxed_cnt++;
495 if(page_table[j].dont_move) pinned_cnt++;
496 /* Count the number of unboxed pages within the given
497 * generation. */
498 if (page_unboxed_p(j)) {
499 if (page_table[j].large_object)
500 large_unboxed_cnt++;
501 else
502 unboxed_cnt++;
506 gc_assert(generations[i].bytes_allocated
507 == count_generation_bytes_allocated(i));
508 fprintf(file,
509 " %1d: %5ld %5ld %5ld %5ld",
511 (long)generations[i].alloc_start_page,
512 (long)generations[i].alloc_unboxed_start_page,
513 (long)generations[i].alloc_large_start_page,
514 (long)generations[i].alloc_large_unboxed_start_page);
515 fprintf(file,
516 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
517 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
518 boxed_cnt, unboxed_cnt, large_boxed_cnt,
519 large_unboxed_cnt, pinned_cnt);
520 fprintf(file,
521 " %8"OS_VM_SIZE_FMT
522 " %5"OS_VM_SIZE_FMT
523 " %8"OS_VM_SIZE_FMT
524 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
525 generations[i].bytes_allocated,
526 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
527 generations[i].gc_trigger,
528 count_write_protect_generation_pages(i),
529 generations[i].num_gc,
530 generation_average_age(i));
532 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
533 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
535 #ifdef LISP_FEATURE_X86
536 fpu_restore(fpu_state);
537 #endif
540 extern void
541 write_heap_exhaustion_report(FILE *file, long available, long requested,
542 struct thread *thread)
544 fprintf(file,
545 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
546 gc_active_p ? "garbage collection" : "allocation",
547 available,
548 requested);
549 write_generation_stats(file);
550 fprintf(file, "GC control variables:\n");
551 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
552 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
553 (SymbolValue(GC_PENDING, thread) == T) ?
554 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
555 "false" : "in progress"));
556 #ifdef LISP_FEATURE_SB_THREAD
557 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
558 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
559 #endif
562 extern void
563 print_generation_stats(void)
565 write_generation_stats(stderr);
568 extern char* gc_logfile;
569 char * gc_logfile = NULL;
571 extern void
572 log_generation_stats(char *logfile, char *header)
574 if (logfile) {
575 FILE * log = fopen(logfile, "a");
576 if (log) {
577 fprintf(log, "%s\n", header);
578 write_generation_stats(log);
579 fclose(log);
580 } else {
581 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
582 fflush(stderr);
587 extern void
588 report_heap_exhaustion(long available, long requested, struct thread *th)
590 if (gc_logfile) {
591 FILE * log = fopen(gc_logfile, "a");
592 if (log) {
593 write_heap_exhaustion_report(log, available, requested, th);
594 fclose(log);
595 } else {
596 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
597 fflush(stderr);
600 /* Always to stderr as well. */
601 write_heap_exhaustion_report(stderr, available, requested, th);
605 #if defined(LISP_FEATURE_X86)
606 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
607 #endif
609 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
610 * if zeroing it ourselves, i.e. in practice give the memory back to the
611 * OS. Generally done after a large GC.
613 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
614 page_index_t i;
615 void *addr = page_address(start), *new_addr;
616 os_vm_size_t length = npage_bytes(1+end-start);
618 if (start > end)
619 return;
621 gc_assert(length >= gencgc_release_granularity);
622 gc_assert((length % gencgc_release_granularity) == 0);
624 #ifdef LISP_FEATURE_LINUX
625 extern os_vm_address_t anon_dynamic_space_start;
626 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
627 // Linux treats it as a demand that the memory be 0-filled, or refreshed
628 // from a file that backs the range. BSD takes it as a hint that you don't
629 // care if the memory has to brought in from swap when next accessed,
630 // i.e. it's not a request to make a user-visible alteration to memory.
631 // So in theory this can bring a page in from the core file, if we happen
632 // to hit a page that resides in the portion of memory mapped by coreparse.
633 // In practice this should not happen because objects from a core file can't
634 // become garbage. Except in save-lisp-and-die they can, and we must be
635 // cautious not to resurrect bytes that originally came from the file.
636 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
637 if (madvise(addr, length, MADV_DONTNEED) != 0)
638 lose("madvise failed\n");
639 } else
640 #endif
642 os_invalidate(addr, length);
643 new_addr = os_validate(addr, length);
644 if (new_addr == NULL || new_addr != addr) {
645 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
646 start, new_addr);
650 for (i = start; i <= end; i++)
651 set_page_need_to_zero(i, 0);
654 /* Zero the pages from START to END (inclusive). Generally done just after
655 * a new region has been allocated.
657 static void
658 zero_pages(page_index_t start, page_index_t end) {
659 if (start > end)
660 return;
662 #if defined(LISP_FEATURE_X86)
663 fast_bzero(page_address(start), npage_bytes(1+end-start));
664 #else
665 bzero(page_address(start), npage_bytes(1+end-start));
666 #endif
670 static void
671 zero_and_mark_pages(page_index_t start, page_index_t end) {
672 page_index_t i;
674 zero_pages(start, end);
675 for (i = start; i <= end; i++)
676 set_page_need_to_zero(i, 0);
679 /* Zero the pages from START to END (inclusive), except for those
680 * pages that are known to already zeroed. Mark all pages in the
681 * ranges as non-zeroed.
683 static void
684 zero_dirty_pages(page_index_t start, page_index_t end) {
685 page_index_t i, j;
687 for (i = start; i <= end; i++) {
688 if (!page_need_to_zero(i)) continue;
689 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
690 ; /* empty body */
691 zero_pages(i, j-1);
692 i = j;
695 for (i = start; i <= end; i++) {
696 set_page_need_to_zero(i, 1);
702 * To support quick and inline allocation, regions of memory can be
703 * allocated and then allocated from with just a free pointer and a
704 * check against an end address.
706 * Since objects can be allocated to spaces with different properties
707 * e.g. boxed/unboxed, generation, ages; there may need to be many
708 * allocation regions.
710 * Each allocation region may start within a partly used page. Many
711 * features of memory use are noted on a page wise basis, e.g. the
712 * generation; so if a region starts within an existing allocated page
713 * it must be consistent with this page.
715 * During the scavenging of the newspace, objects will be transported
716 * into an allocation region, and pointers updated to point to this
717 * allocation region. It is possible that these pointers will be
718 * scavenged again before the allocation region is closed, e.g. due to
719 * trans_list which jumps all over the place to cleanup the list. It
720 * is important to be able to determine properties of all objects
721 * pointed to when scavenging, e.g to detect pointers to the oldspace.
722 * Thus it's important that the allocation regions have the correct
723 * properties set when allocated, and not just set when closed. The
724 * region allocation routines return regions with the specified
725 * properties, and grab all the pages, setting their properties
726 * appropriately, except that the amount used is not known.
728 * These regions are used to support quicker allocation using just a
729 * free pointer. The actual space used by the region is not reflected
730 * in the pages tables until it is closed. It can't be scavenged until
731 * closed.
733 * When finished with the region it should be closed, which will
734 * update the page tables for the actual space used returning unused
735 * space. Further it may be noted in the new regions which is
736 * necessary when scavenging the newspace.
738 * Large objects may be allocated directly without an allocation
739 * region, the page tables are updated immediately.
741 * Unboxed objects don't contain pointers to other objects and so
742 * don't need scavenging. Further they can't contain pointers to
743 * younger generations so WP is not needed. By allocating pages to
744 * unboxed objects the whole page never needs scavenging or
745 * write-protecting. */
747 /* We are only using two regions at present. Both are for the current
748 * newspace generation. */
749 struct alloc_region boxed_region;
750 struct alloc_region unboxed_region;
752 /* The generation currently being allocated to. */
753 static generation_index_t gc_alloc_generation;
755 static inline page_index_t
756 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
758 if (large) {
759 if (UNBOXED_PAGE_FLAG == page_type_flag) {
760 return generations[generation].alloc_large_unboxed_start_page;
761 } else if (BOXED_PAGE_FLAG & page_type_flag) {
762 /* Both code and data. */
763 return generations[generation].alloc_large_start_page;
764 } else {
765 lose("bad page type flag: %d", page_type_flag);
767 } else {
768 if (UNBOXED_PAGE_FLAG == page_type_flag) {
769 return generations[generation].alloc_unboxed_start_page;
770 } else if (BOXED_PAGE_FLAG & page_type_flag) {
771 /* Both code and data. */
772 return generations[generation].alloc_start_page;
773 } else {
774 lose("bad page_type_flag: %d", page_type_flag);
779 static inline void
780 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
781 page_index_t page)
783 if (large) {
784 if (UNBOXED_PAGE_FLAG == page_type_flag) {
785 generations[generation].alloc_large_unboxed_start_page = page;
786 } else if (BOXED_PAGE_FLAG & page_type_flag) {
787 /* Both code and data. */
788 generations[generation].alloc_large_start_page = page;
789 } else {
790 lose("bad page type flag: %d", page_type_flag);
792 } else {
793 if (UNBOXED_PAGE_FLAG == page_type_flag) {
794 generations[generation].alloc_unboxed_start_page = page;
795 } else if (BOXED_PAGE_FLAG & page_type_flag) {
796 /* Both code and data. */
797 generations[generation].alloc_start_page = page;
798 } else {
799 lose("bad page type flag: %d", page_type_flag);
804 in_use_marker_t *
805 pinned_dwords(page_index_t page)
807 if (page_table[page].has_pin_map)
808 return &page_table_pinned_dwords[page * (n_dwords_in_card/N_WORD_BITS)];
809 return NULL;
812 /* Find a new region with room for at least the given number of bytes.
814 * It starts looking at the current generation's alloc_start_page. So
815 * may pick up from the previous region if there is enough space. This
816 * keeps the allocation contiguous when scavenging the newspace.
818 * The alloc_region should have been closed by a call to
819 * gc_alloc_update_page_tables(), and will thus be in an empty state.
821 * To assist the scavenging functions write-protected pages are not
822 * used. Free pages should not be write-protected.
824 * It is critical to the conservative GC that the start of regions be
825 * known. To help achieve this only small regions are allocated at a
826 * time.
828 * During scavenging, pointers may be found to within the current
829 * region and the page generation must be set so that pointers to the
830 * from space can be recognized. Therefore the generation of pages in
831 * the region are set to gc_alloc_generation. To prevent another
832 * allocation call using the same pages, all the pages in the region
833 * are allocated, although they will initially be empty.
835 static void
836 gc_alloc_new_region(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
838 page_index_t first_page;
839 page_index_t last_page;
840 os_vm_size_t bytes_found;
841 page_index_t i;
842 int ret;
845 FSHOW((stderr,
846 "/alloc_new_region for %d bytes from gen %d\n",
847 nbytes, gc_alloc_generation));
850 /* Check that the region is in a reset state. */
851 gc_assert((alloc_region->first_page == 0)
852 && (alloc_region->last_page == -1)
853 && (alloc_region->free_pointer == alloc_region->end_addr));
854 ret = thread_mutex_lock(&free_pages_lock);
855 gc_assert(ret == 0);
856 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
857 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
858 bytes_found=(GENCGC_CARD_BYTES - page_bytes_used(first_page))
859 + npage_bytes(last_page-first_page);
861 /* Set up the alloc_region. */
862 alloc_region->first_page = first_page;
863 alloc_region->last_page = last_page;
864 alloc_region->start_addr = page_bytes_used(first_page)
865 + page_address(first_page);
866 alloc_region->free_pointer = alloc_region->start_addr;
867 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
869 /* Set up the pages. */
871 /* The first page may have already been in use. */
872 if (page_bytes_used(first_page) == 0) {
873 page_table[first_page].allocated = page_type_flag;
874 page_table[first_page].gen = gc_alloc_generation;
875 page_table[first_page].large_object = 0;
876 set_page_scan_start_offset(first_page, 0);
877 // wiping should have free()ed and :=NULL
878 gc_assert(pinned_dwords(first_page) == NULL);
881 gc_assert(page_table[first_page].allocated == page_type_flag);
882 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
884 gc_assert(page_table[first_page].gen == gc_alloc_generation);
885 gc_assert(page_table[first_page].large_object == 0);
887 for (i = first_page+1; i <= last_page; i++) {
888 page_table[i].allocated = page_type_flag;
889 page_table[i].gen = gc_alloc_generation;
890 page_table[i].large_object = 0;
891 /* This may not be necessary for unboxed regions (think it was
892 * broken before!) */
893 set_page_scan_start_offset(i,
894 void_diff(page_address(i), alloc_region->start_addr));
895 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
897 /* Bump up last_free_page. */
898 if (last_page+1 > last_free_page) {
899 last_free_page = last_page+1;
900 /* do we only want to call this on special occasions? like for
901 * boxed_region? */
902 set_alloc_pointer((lispobj)page_address(last_free_page));
904 ret = thread_mutex_unlock(&free_pages_lock);
905 gc_assert(ret == 0);
907 #ifdef READ_PROTECT_FREE_PAGES
908 os_protect(page_address(first_page),
909 npage_bytes(1+last_page-first_page),
910 OS_VM_PROT_ALL);
911 #endif
913 /* If the first page was only partial, don't check whether it's
914 * zeroed (it won't be) and don't zero it (since the parts that
915 * we're interested in are guaranteed to be zeroed).
917 if (page_bytes_used(first_page)) {
918 first_page++;
921 zero_dirty_pages(first_page, last_page);
923 /* we can do this after releasing free_pages_lock */
924 if (gencgc_zero_check) {
925 word_t *p;
926 for (p = (word_t *)alloc_region->start_addr;
927 p < (word_t *)alloc_region->end_addr; p++) {
928 if (*p != 0) {
929 lose("The new region is not zero at %p (start=%p, end=%p).\n",
930 p, alloc_region->start_addr, alloc_region->end_addr);
936 /* If the record_new_objects flag is 2 then all new regions created
937 * are recorded.
939 * If it's 1 then then it is only recorded if the first page of the
940 * current region is <= new_areas_ignore_page. This helps avoid
941 * unnecessary recording when doing full scavenge pass.
943 * The new_object structure holds the page, byte offset, and size of
944 * new regions of objects. Each new area is placed in the array of
945 * these structures pointer to by new_areas. new_areas_index holds the
946 * offset into new_areas.
948 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
949 * later code must detect this and handle it, probably by doing a full
950 * scavenge of a generation. */
951 #define NUM_NEW_AREAS 512
952 static int record_new_objects = 0;
953 static page_index_t new_areas_ignore_page;
954 struct new_area {
955 page_index_t page;
956 size_t offset;
957 size_t size;
959 static struct new_area (*new_areas)[];
960 static size_t new_areas_index;
961 size_t max_new_areas;
963 /* Add a new area to new_areas. */
964 static void
965 add_new_area(page_index_t first_page, size_t offset, size_t size)
967 size_t new_area_start, c;
968 ssize_t i;
970 /* Ignore if full. */
971 if (new_areas_index >= NUM_NEW_AREAS)
972 return;
974 switch (record_new_objects) {
975 case 0:
976 return;
977 case 1:
978 if (first_page > new_areas_ignore_page)
979 return;
980 break;
981 case 2:
982 break;
983 default:
984 gc_abort();
987 new_area_start = npage_bytes(first_page) + offset;
989 /* Search backwards for a prior area that this follows from. If
990 found this will save adding a new area. */
991 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
992 size_t area_end =
993 npage_bytes((*new_areas)[i].page)
994 + (*new_areas)[i].offset
995 + (*new_areas)[i].size;
996 /*FSHOW((stderr,
997 "/add_new_area S1 %d %d %d %d\n",
998 i, c, new_area_start, area_end));*/
999 if (new_area_start == area_end) {
1000 /*FSHOW((stderr,
1001 "/adding to [%d] %d %d %d with %d %d %d:\n",
1003 (*new_areas)[i].page,
1004 (*new_areas)[i].offset,
1005 (*new_areas)[i].size,
1006 first_page,
1007 offset,
1008 size);*/
1009 (*new_areas)[i].size += size;
1010 return;
1014 (*new_areas)[new_areas_index].page = first_page;
1015 (*new_areas)[new_areas_index].offset = offset;
1016 (*new_areas)[new_areas_index].size = size;
1017 /*FSHOW((stderr,
1018 "/new_area %d page %d offset %d size %d\n",
1019 new_areas_index, first_page, offset, size));*/
1020 new_areas_index++;
1022 /* Note the max new_areas used. */
1023 if (new_areas_index > max_new_areas)
1024 max_new_areas = new_areas_index;
1027 /* Update the tables for the alloc_region. The region may be added to
1028 * the new_areas.
1030 * When done the alloc_region is set up so that the next quick alloc
1031 * will fail safely and thus a new region will be allocated. Further
1032 * it is safe to try to re-update the page table of this reset
1033 * alloc_region. */
1034 void
1035 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1037 boolean more;
1038 page_index_t first_page;
1039 page_index_t next_page;
1040 os_vm_size_t bytes_used;
1041 os_vm_size_t region_size;
1042 os_vm_size_t byte_cnt;
1043 page_bytes_t orig_first_page_bytes_used;
1044 int ret;
1047 first_page = alloc_region->first_page;
1049 /* Catch an unused alloc_region. */
1050 if ((first_page == 0) && (alloc_region->last_page == -1))
1051 return;
1053 next_page = first_page+1;
1055 ret = thread_mutex_lock(&free_pages_lock);
1056 gc_assert(ret == 0);
1057 if (alloc_region->free_pointer != alloc_region->start_addr) {
1058 /* some bytes were allocated in the region */
1059 orig_first_page_bytes_used = page_bytes_used(first_page);
1061 gc_assert(alloc_region->start_addr ==
1062 (page_address(first_page) + page_bytes_used(first_page)));
1064 /* All the pages used need to be updated */
1066 /* Update the first page. */
1068 /* If the page was free then set up the gen, and
1069 * scan_start_offset. */
1070 if (page_bytes_used(first_page) == 0)
1071 gc_assert(page_starts_contiguous_block_p(first_page));
1072 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1074 gc_assert(page_table[first_page].allocated & page_type_flag);
1075 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1076 gc_assert(page_table[first_page].large_object == 0);
1078 byte_cnt = 0;
1080 /* Calculate the number of bytes used in this page. This is not
1081 * always the number of new bytes, unless it was free. */
1082 more = 0;
1083 if ((bytes_used = void_diff(alloc_region->free_pointer,
1084 page_address(first_page)))
1085 >GENCGC_CARD_BYTES) {
1086 bytes_used = GENCGC_CARD_BYTES;
1087 more = 1;
1089 set_page_bytes_used(first_page, bytes_used);
1090 byte_cnt += bytes_used;
1093 /* All the rest of the pages should be free. We need to set
1094 * their scan_start_offset pointer to the start of the
1095 * region, and set the bytes_used. */
1096 while (more) {
1097 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1098 gc_assert(page_table[next_page].allocated & page_type_flag);
1099 gc_assert(page_bytes_used(next_page) == 0);
1100 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1101 gc_assert(page_table[next_page].large_object == 0);
1102 gc_assert(page_scan_start_offset(next_page) ==
1103 void_diff(page_address(next_page),
1104 alloc_region->start_addr));
1106 /* Calculate the number of bytes used in this page. */
1107 more = 0;
1108 if ((bytes_used = void_diff(alloc_region->free_pointer,
1109 page_address(next_page)))>GENCGC_CARD_BYTES) {
1110 bytes_used = GENCGC_CARD_BYTES;
1111 more = 1;
1113 set_page_bytes_used(next_page, bytes_used);
1114 byte_cnt += bytes_used;
1116 next_page++;
1119 region_size = void_diff(alloc_region->free_pointer,
1120 alloc_region->start_addr);
1121 bytes_allocated += region_size;
1122 generations[gc_alloc_generation].bytes_allocated += region_size;
1124 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1126 /* Set the generations alloc restart page to the last page of
1127 * the region. */
1128 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1130 /* Add the region to the new_areas if requested. */
1131 if (BOXED_PAGE_FLAG & page_type_flag)
1132 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1135 FSHOW((stderr,
1136 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1137 region_size,
1138 gc_alloc_generation));
1140 } else {
1141 /* There are no bytes allocated. Unallocate the first_page if
1142 * there are 0 bytes_used. */
1143 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1144 if (page_bytes_used(first_page) == 0)
1145 page_table[first_page].allocated = FREE_PAGE_FLAG;
1148 /* Unallocate any unused pages. */
1149 while (next_page <= alloc_region->last_page) {
1150 gc_assert(page_bytes_used(next_page) == 0);
1151 page_table[next_page].allocated = FREE_PAGE_FLAG;
1152 next_page++;
1154 ret = thread_mutex_unlock(&free_pages_lock);
1155 gc_assert(ret == 0);
1157 /* alloc_region is per-thread, we're ok to do this unlocked */
1158 gc_set_region_empty(alloc_region);
1161 /* Allocate a possibly large object. */
1162 void *
1163 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1165 boolean more;
1166 page_index_t first_page, next_page, last_page;
1167 page_bytes_t orig_first_page_bytes_used;
1168 os_vm_size_t byte_cnt;
1169 os_vm_size_t bytes_used;
1170 int ret;
1172 ret = thread_mutex_lock(&free_pages_lock);
1173 gc_assert(ret == 0);
1175 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1176 if (first_page <= alloc_region->last_page) {
1177 first_page = alloc_region->last_page+1;
1180 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1182 gc_assert(first_page > alloc_region->last_page);
1184 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1186 /* Set up the pages. */
1187 orig_first_page_bytes_used = page_bytes_used(first_page);
1189 /* If the first page was free then set up the gen, and
1190 * scan_start_offset. */
1191 if (page_bytes_used(first_page) == 0) {
1192 page_table[first_page].allocated = page_type_flag;
1193 page_table[first_page].gen = gc_alloc_generation;
1194 set_page_scan_start_offset(first_page, 0);
1195 page_table[first_page].large_object = 1;
1198 gc_assert(page_table[first_page].allocated == page_type_flag);
1199 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1200 gc_assert(page_table[first_page].large_object == 1);
1202 byte_cnt = 0;
1204 /* Calc. the number of bytes used in this page. This is not
1205 * always the number of new bytes, unless it was free. */
1206 more = 0;
1207 if ((bytes_used = nbytes+orig_first_page_bytes_used) > GENCGC_CARD_BYTES) {
1208 bytes_used = GENCGC_CARD_BYTES;
1209 more = 1;
1211 set_page_bytes_used(first_page, bytes_used);
1212 byte_cnt += bytes_used;
1214 next_page = first_page+1;
1216 /* All the rest of the pages should be free. We need to set their
1217 * scan_start_offset pointer to the start of the region, and set
1218 * the bytes_used. */
1219 while (more) {
1220 gc_assert(page_free_p(next_page));
1221 gc_assert(page_bytes_used(next_page) == 0);
1222 page_table[next_page].allocated = page_type_flag;
1223 page_table[next_page].gen = gc_alloc_generation;
1224 page_table[next_page].large_object = 1;
1226 set_page_scan_start_offset(next_page,
1227 npage_bytes(next_page-first_page) - orig_first_page_bytes_used);
1229 /* Calculate the number of bytes used in this page. */
1230 more = 0;
1231 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1232 if (bytes_used > GENCGC_CARD_BYTES) {
1233 bytes_used = GENCGC_CARD_BYTES;
1234 more = 1;
1236 set_page_bytes_used(next_page, bytes_used);
1237 page_table[next_page].write_protected=0;
1238 page_table[next_page].dont_move=0;
1239 byte_cnt += bytes_used;
1240 next_page++;
1243 gc_assert((byte_cnt-orig_first_page_bytes_used) == (size_t)nbytes);
1245 bytes_allocated += nbytes;
1246 generations[gc_alloc_generation].bytes_allocated += nbytes;
1248 /* Add the region to the new_areas if requested. */
1249 if (BOXED_PAGE_FLAG & page_type_flag)
1250 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1252 /* Bump up last_free_page */
1253 if (last_page+1 > last_free_page) {
1254 last_free_page = last_page+1;
1255 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1257 ret = thread_mutex_unlock(&free_pages_lock);
1258 gc_assert(ret == 0);
1260 #ifdef READ_PROTECT_FREE_PAGES
1261 os_protect(page_address(first_page),
1262 npage_bytes(1+last_page-first_page),
1263 OS_VM_PROT_ALL);
1264 #endif
1266 zero_dirty_pages(first_page, last_page);
1268 return page_address(first_page);
1271 static page_index_t gencgc_alloc_start_page = -1;
1273 void
1274 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1276 struct thread *thread = arch_os_get_current_thread();
1277 /* Write basic information before doing anything else: if we don't
1278 * call to lisp this is a must, and even if we do there is always
1279 * the danger that we bounce back here before the error has been
1280 * handled, or indeed even printed.
1282 report_heap_exhaustion(available, requested, thread);
1283 if (gc_active_p || (available == 0)) {
1284 /* If we are in GC, or totally out of memory there is no way
1285 * to sanely transfer control to the lisp-side of things.
1287 lose("Heap exhausted, game over.");
1289 else {
1290 /* FIXME: assert free_pages_lock held */
1291 (void)thread_mutex_unlock(&free_pages_lock);
1292 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1293 gc_assert(get_pseudo_atomic_atomic(thread));
1294 clear_pseudo_atomic_atomic(thread);
1295 if (get_pseudo_atomic_interrupted(thread))
1296 do_pending_interrupt();
1297 #endif
1298 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1299 * to running user code at arbitrary places, even in a
1300 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1301 * running out of the heap. So at this point all bets are
1302 * off. */
1303 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1304 corruption_warning_and_maybe_lose
1305 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1306 /* available and requested should be double word aligned, thus
1307 they can passed as fixnums and shifted later. */
1308 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1309 lose("HEAP-EXHAUSTED-ERROR fell through");
1313 page_index_t
1314 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1315 int page_type_flag)
1317 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1318 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1319 os_vm_size_t nbytes = bytes;
1320 os_vm_size_t nbytes_goal = nbytes;
1321 os_vm_size_t bytes_found = 0;
1322 os_vm_size_t most_bytes_found = 0;
1323 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1324 /* FIXME: assert(free_pages_lock is held); */
1326 if (nbytes_goal < gencgc_alloc_granularity)
1327 nbytes_goal = gencgc_alloc_granularity;
1329 /* Toggled by gc_and_save for heap compaction, normally -1. */
1330 if (gencgc_alloc_start_page != -1) {
1331 restart_page = gencgc_alloc_start_page;
1334 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1335 * long from the interface. */
1336 gc_assert(bytes>=0);
1337 /* Search for a page with at least nbytes of space. We prefer
1338 * not to split small objects on multiple pages, to reduce the
1339 * number of contiguous allocation regions spaning multiple
1340 * pages: this helps avoid excessive conservativism.
1342 * For other objects, we guarantee that they start on their own
1343 * page boundary.
1345 first_page = restart_page;
1346 while (first_page < page_table_pages) {
1347 bytes_found = 0;
1348 if (page_free_p(first_page)) {
1349 gc_assert(0 == page_bytes_used(first_page));
1350 bytes_found = GENCGC_CARD_BYTES;
1351 } else if (small_object &&
1352 (page_table[first_page].allocated == page_type_flag) &&
1353 (page_table[first_page].large_object == 0) &&
1354 (page_table[first_page].gen == gc_alloc_generation) &&
1355 (page_table[first_page].write_protected == 0) &&
1356 (page_table[first_page].dont_move == 0)) {
1357 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1358 if (bytes_found < nbytes) {
1359 if (bytes_found > most_bytes_found)
1360 most_bytes_found = bytes_found;
1361 first_page++;
1362 continue;
1364 } else {
1365 first_page++;
1366 continue;
1369 gc_assert(page_table[first_page].write_protected == 0);
1370 for (last_page = first_page+1;
1371 ((last_page < page_table_pages) &&
1372 page_free_p(last_page) &&
1373 (bytes_found < nbytes_goal));
1374 last_page++) {
1375 bytes_found += GENCGC_CARD_BYTES;
1376 gc_assert(0 == page_bytes_used(last_page));
1377 gc_assert(0 == page_table[last_page].write_protected);
1380 if (bytes_found > most_bytes_found) {
1381 most_bytes_found = bytes_found;
1382 most_bytes_found_from = first_page;
1383 most_bytes_found_to = last_page;
1385 if (bytes_found >= nbytes_goal)
1386 break;
1388 first_page = last_page;
1391 bytes_found = most_bytes_found;
1392 restart_page = first_page + 1;
1394 /* Check for a failure */
1395 if (bytes_found < nbytes) {
1396 gc_assert(restart_page >= page_table_pages);
1397 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1400 gc_assert(most_bytes_found_to);
1401 *restart_page_ptr = most_bytes_found_from;
1402 return most_bytes_found_to-1;
1405 /* Allocate bytes. All the rest of the special-purpose allocation
1406 * functions will eventually call this */
1408 void *
1409 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1410 int quick_p)
1412 void *new_free_pointer;
1414 if (nbytes>=LARGE_OBJECT_SIZE)
1415 return gc_alloc_large(nbytes, page_type_flag, my_region);
1417 /* Check whether there is room in the current alloc region. */
1418 new_free_pointer = my_region->free_pointer + nbytes;
1420 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1421 my_region->free_pointer, new_free_pointer); */
1423 if (new_free_pointer <= my_region->end_addr) {
1424 /* If so then allocate from the current alloc region. */
1425 void *new_obj = my_region->free_pointer;
1426 my_region->free_pointer = new_free_pointer;
1428 /* Unless a `quick' alloc was requested, check whether the
1429 alloc region is almost empty. */
1430 if (!quick_p &&
1431 void_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1432 /* If so, finished with the current region. */
1433 gc_alloc_update_page_tables(page_type_flag, my_region);
1434 /* Set up a new region. */
1435 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1438 return((void *)new_obj);
1441 /* Else not enough free space in the current region: retry with a
1442 * new region. */
1444 gc_alloc_update_page_tables(page_type_flag, my_region);
1445 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1446 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1449 /* Copy a large object. If the object is in a large object region then
1450 * it is simply promoted, else it is copied. If it's large enough then
1451 * it's copied to a large object region.
1453 * Bignums and vectors may have shrunk. If the object is not copied
1454 * the space needs to be reclaimed, and the page_tables corrected. */
1455 static lispobj
1456 general_copy_large_object(lispobj object, word_t nwords, boolean boxedp)
1458 lispobj *new;
1459 page_index_t first_page;
1461 CHECK_COPY_PRECONDITIONS(object, nwords);
1463 if ((nwords > 1024*1024) && gencgc_verbose) {
1464 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1465 nwords*N_WORD_BYTES));
1468 /* Check whether it's a large object. */
1469 first_page = find_page_index((void *)object);
1470 gc_assert(first_page >= 0);
1472 if (page_table[first_page].large_object) {
1473 /* Promote the object. Note: Unboxed objects may have been
1474 * allocated to a BOXED region so it may be necessary to
1475 * change the region to UNBOXED. */
1476 os_vm_size_t remaining_bytes;
1477 os_vm_size_t bytes_freed;
1478 page_index_t next_page;
1479 page_bytes_t old_bytes_used;
1481 /* FIXME: This comment is somewhat stale.
1483 * Note: Any page write-protection must be removed, else a
1484 * later scavenge_newspace may incorrectly not scavenge these
1485 * pages. This would not be necessary if they are added to the
1486 * new areas, but let's do it for them all (they'll probably
1487 * be written anyway?). */
1489 gc_assert(page_starts_contiguous_block_p(first_page));
1490 next_page = first_page;
1491 remaining_bytes = nwords*N_WORD_BYTES;
1493 while (remaining_bytes > GENCGC_CARD_BYTES) {
1494 gc_assert(page_table[next_page].gen == from_space);
1495 gc_assert(page_table[next_page].large_object);
1496 gc_assert(page_scan_start_offset(next_page) ==
1497 npage_bytes(next_page-first_page));
1498 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1499 /* Should have been unprotected by unprotect_oldspace()
1500 * for boxed objects, and after promotion unboxed ones
1501 * should not be on protected pages at all. */
1502 gc_assert(!page_table[next_page].write_protected);
1504 if (boxedp)
1505 gc_assert(page_boxed_p(next_page));
1506 else {
1507 gc_assert(page_allocated_no_region_p(next_page));
1508 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1510 page_table[next_page].gen = new_space;
1512 remaining_bytes -= GENCGC_CARD_BYTES;
1513 next_page++;
1516 /* Now only one page remains, but the object may have shrunk so
1517 * there may be more unused pages which will be freed. */
1519 /* Object may have shrunk but shouldn't have grown - check. */
1520 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1522 page_table[next_page].gen = new_space;
1524 if (boxedp)
1525 gc_assert(page_boxed_p(next_page));
1526 else
1527 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1529 /* Adjust the bytes_used. */
1530 old_bytes_used = page_bytes_used(next_page);
1531 set_page_bytes_used(next_page, remaining_bytes);
1533 bytes_freed = old_bytes_used - remaining_bytes;
1535 /* Free any remaining pages; needs care. */
1536 next_page++;
1537 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1538 (page_table[next_page].gen == from_space) &&
1539 /* FIXME: It is not obvious to me why this is necessary
1540 * as a loop condition: it seems to me that the
1541 * scan_start_offset test should be sufficient, but
1542 * experimentally that is not the case. --NS
1543 * 2011-11-28 */
1544 (boxedp ?
1545 page_boxed_p(next_page) :
1546 page_allocated_no_region_p(next_page)) &&
1547 page_table[next_page].large_object &&
1548 (page_scan_start_offset(next_page) ==
1549 npage_bytes(next_page - first_page))) {
1550 /* Checks out OK, free the page. Don't need to both zeroing
1551 * pages as this should have been done before shrinking the
1552 * object. These pages shouldn't be write-protected, even if
1553 * boxed they should be zero filled. */
1554 gc_assert(page_table[next_page].write_protected == 0);
1556 old_bytes_used = page_bytes_used(next_page);
1557 page_table[next_page].allocated = FREE_PAGE_FLAG;
1558 set_page_bytes_used(next_page, 0);
1559 bytes_freed += old_bytes_used;
1560 next_page++;
1563 if ((bytes_freed > 0) && gencgc_verbose) {
1564 FSHOW((stderr,
1565 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1566 bytes_freed));
1569 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1570 + bytes_freed;
1571 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1572 bytes_allocated -= bytes_freed;
1574 /* Add the region to the new_areas if requested. */
1575 if (boxedp)
1576 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1578 return(object);
1580 } else {
1581 /* Allocate space. */
1582 new = gc_general_alloc(nwords*N_WORD_BYTES,
1583 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1584 ALLOC_QUICK);
1586 /* Copy the object. */
1587 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1589 /* Return Lisp pointer of new object. */
1590 return make_lispobj(new, lowtag_of(object));
1594 lispobj
1595 copy_large_object(lispobj object, sword_t nwords)
1597 return general_copy_large_object(object, nwords, 1);
1600 lispobj
1601 copy_large_unboxed_object(lispobj object, sword_t nwords)
1603 return general_copy_large_object(object, nwords, 0);
1606 /* to copy unboxed objects */
1607 lispobj
1608 copy_unboxed_object(lispobj object, sword_t nwords)
1610 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1615 * code and code-related objects
1618 static lispobj trans_fun_header(lispobj object);
1619 static lispobj trans_boxed(lispobj object);
1622 /* Scan a x86 compiled code object, looking for possible fixups that
1623 * have been missed after a move.
1625 * Two types of fixups are needed:
1626 * 1. Absolute fixups to within the code object.
1627 * 2. Relative fixups to outside the code object.
1629 * Currently only absolute fixups to the constant vector, or to the
1630 * code area are checked. */
1631 #ifdef LISP_FEATURE_X86
1632 void
1633 sniff_code_object(struct code *code, os_vm_size_t displacement)
1635 sword_t nheader_words, ncode_words, nwords;
1636 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1637 os_vm_address_t code_start_addr, code_end_addr;
1638 os_vm_address_t code_addr = (os_vm_address_t)code;
1639 int fixup_found = 0;
1641 if (!check_code_fixups)
1642 return;
1644 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1646 ncode_words = code_instruction_words(code->code_size);
1647 nheader_words = code_header_words(*(lispobj *)code);
1648 nwords = ncode_words + nheader_words;
1650 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1651 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1652 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1653 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1655 /* Work through the unboxed code. */
1656 for (p = code_start_addr; p < code_end_addr; p++) {
1657 void *data = *(void **)p;
1658 unsigned d1 = *((unsigned char *)p - 1);
1659 unsigned d2 = *((unsigned char *)p - 2);
1660 unsigned d3 = *((unsigned char *)p - 3);
1661 unsigned d4 = *((unsigned char *)p - 4);
1662 #if QSHOW
1663 unsigned d5 = *((unsigned char *)p - 5);
1664 unsigned d6 = *((unsigned char *)p - 6);
1665 #endif
1667 /* Check for code references. */
1668 /* Check for a 32 bit word that looks like an absolute
1669 reference to within the code adea of the code object. */
1670 if ((data >= (void*)(code_start_addr-displacement))
1671 && (data < (void*)(code_end_addr-displacement))) {
1672 /* function header */
1673 if ((d4 == 0x5e)
1674 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1675 (unsigned)code)) {
1676 /* Skip the function header */
1677 p += 6*4 - 4 - 1;
1678 continue;
1680 /* the case of PUSH imm32 */
1681 if (d1 == 0x68) {
1682 fixup_found = 1;
1683 FSHOW((stderr,
1684 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1685 p, d6, d5, d4, d3, d2, d1, data));
1686 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1688 /* the case of MOV [reg-8],imm32 */
1689 if ((d3 == 0xc7)
1690 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1691 || d2==0x45 || d2==0x46 || d2==0x47)
1692 && (d1 == 0xf8)) {
1693 fixup_found = 1;
1694 FSHOW((stderr,
1695 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1696 p, d6, d5, d4, d3, d2, d1, data));
1697 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1699 /* the case of LEA reg,[disp32] */
1700 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1701 fixup_found = 1;
1702 FSHOW((stderr,
1703 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1704 p, d6, d5, d4, d3, d2, d1, data));
1705 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1709 /* Check for constant references. */
1710 /* Check for a 32 bit word that looks like an absolute
1711 reference to within the constant vector. Constant references
1712 will be aligned. */
1713 if ((data >= (void*)(constants_start_addr-displacement))
1714 && (data < (void*)(constants_end_addr-displacement))
1715 && (((unsigned)data & 0x3) == 0)) {
1716 /* Mov eax,m32 */
1717 if (d1 == 0xa1) {
1718 fixup_found = 1;
1719 FSHOW((stderr,
1720 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1721 p, d6, d5, d4, d3, d2, d1, data));
1722 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1725 /* the case of MOV m32,EAX */
1726 if (d1 == 0xa3) {
1727 fixup_found = 1;
1728 FSHOW((stderr,
1729 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1730 p, d6, d5, d4, d3, d2, d1, data));
1731 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1734 /* the case of CMP m32,imm32 */
1735 if ((d1 == 0x3d) && (d2 == 0x81)) {
1736 fixup_found = 1;
1737 FSHOW((stderr,
1738 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1739 p, d6, d5, d4, d3, d2, d1, data));
1740 /* XX Check this */
1741 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1744 /* Check for a mod=00, r/m=101 byte. */
1745 if ((d1 & 0xc7) == 5) {
1746 /* Cmp m32,reg */
1747 if (d2 == 0x39) {
1748 fixup_found = 1;
1749 FSHOW((stderr,
1750 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1751 p, d6, d5, d4, d3, d2, d1, data));
1752 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1754 /* the case of CMP reg32,m32 */
1755 if (d2 == 0x3b) {
1756 fixup_found = 1;
1757 FSHOW((stderr,
1758 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1759 p, d6, d5, d4, d3, d2, d1, data));
1760 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1762 /* the case of MOV m32,reg32 */
1763 if (d2 == 0x89) {
1764 fixup_found = 1;
1765 FSHOW((stderr,
1766 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1767 p, d6, d5, d4, d3, d2, d1, data));
1768 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1770 /* the case of MOV reg32,m32 */
1771 if (d2 == 0x8b) {
1772 fixup_found = 1;
1773 FSHOW((stderr,
1774 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1775 p, d6, d5, d4, d3, d2, d1, data));
1776 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1778 /* the case of LEA reg32,m32 */
1779 if (d2 == 0x8d) {
1780 fixup_found = 1;
1781 FSHOW((stderr,
1782 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1783 p, d6, d5, d4, d3, d2, d1, data));
1784 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1790 /* If anything was found, print some information on the code
1791 * object. */
1792 if (fixup_found) {
1793 FSHOW((stderr,
1794 "/compiled code object at %x: header words = %d, code words = %d\n",
1795 code, nheader_words, ncode_words));
1796 FSHOW((stderr,
1797 "/const start = %x, end = %x\n",
1798 constants_start_addr, constants_end_addr));
1799 FSHOW((stderr,
1800 "/code start = %x, end = %x\n",
1801 code_start_addr, code_end_addr));
1804 #endif
1806 #ifdef LISP_FEATURE_X86
1807 void
1808 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1810 sword_t nheader_words, ncode_words, nwords;
1811 os_vm_address_t __attribute__((unused)) constants_start_addr, constants_end_addr;
1812 os_vm_address_t __attribute__((unused)) code_start_addr, code_end_addr;
1813 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1814 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1815 os_vm_size_t displacement = code_addr - old_addr;
1816 lispobj fixups = NIL;
1817 struct vector *fixups_vector;
1819 ncode_words = code_instruction_words(new_code->code_size);
1820 nheader_words = code_header_words(*(lispobj *)new_code);
1821 nwords = ncode_words + nheader_words;
1822 /* FSHOW((stderr,
1823 "/compiled code object at %x: header words = %d, code words = %d\n",
1824 new_code, nheader_words, ncode_words)); */
1825 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1826 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1827 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1828 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1830 FSHOW((stderr,
1831 "/const start = %x, end = %x\n",
1832 constants_start_addr,constants_end_addr));
1833 FSHOW((stderr,
1834 "/code start = %x; end = %x\n",
1835 code_start_addr,code_end_addr));
1838 fixups = new_code->fixups;
1839 /* It will be a Lisp vector if valid, or 0 if there are no fixups */
1840 if (fixups == 0 || !is_lisp_pointer(fixups)) {
1841 /* Check for possible errors. */
1842 if (check_code_fixups)
1843 sniff_code_object(new_code, displacement);
1845 return;
1848 fixups_vector = (struct vector *)native_pointer(fixups);
1850 /* Could be pointing to a forwarding pointer. */
1851 /* This is extremely unlikely, because the only referent of the fixups
1852 is usually the code itself; so scavenging the vector won't occur
1853 until after the code object is known to be live. As we're just now
1854 enlivening the code, the fixups shouldn't have been forwarded.
1855 Maybe the vector is on the special binding stack though ... */
1856 if (is_lisp_pointer(fixups) &&
1857 (find_page_index((void*)fixups_vector) != -1) &&
1858 forwarding_pointer_p((lispobj*)fixups_vector)) {
1859 /* If so, then follow it. */
1860 /*SHOW("following pointer to a forwarding pointer");*/
1861 fixups_vector = (struct vector *)
1862 native_pointer(forwarding_pointer_value((lispobj*)fixups_vector));
1865 /*SHOW("got fixups");*/
1867 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1868 /* Got the fixups for the code block. Now work through the vector,
1869 and apply a fixup at each address. */
1870 sword_t length = fixnum_value(fixups_vector->length);
1871 sword_t i;
1872 for (i = 0; i < length; i++) {
1873 long offset = fixups_vector->data[i];
1874 /* Now check the current value of offset. */
1875 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1877 /* If it's within the old_code object then it must be an
1878 * absolute fixup (relative ones are not saved) */
1879 if ((old_value >= old_addr)
1880 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1881 /* So add the dispacement. */
1882 *(os_vm_address_t *)(code_start_addr + offset) =
1883 old_value + displacement;
1884 else
1885 /* It is outside the old code object so it must be a
1886 * relative fixup (absolute fixups are not saved). So
1887 * subtract the displacement. */
1888 *(os_vm_address_t *)(code_start_addr + offset) =
1889 old_value - displacement;
1891 } else {
1892 /* This used to just print a note to stderr, but a bogus fixup seems to
1893 * indicate real heap corruption, so a hard hailure is in order. */
1894 lose("fixup vector %p has a bad widetag: %d\n",
1895 fixups_vector, widetag_of(fixups_vector->header));
1898 /* Check for possible errors. */
1899 if (check_code_fixups) {
1900 sniff_code_object(new_code,displacement);
1903 #endif
1905 static lispobj
1906 trans_boxed_large(lispobj object)
1908 gc_assert(is_lisp_pointer(object));
1909 return copy_large_object(object,
1910 (HeaderValue(*native_pointer(object)) | 1) + 1);
1914 * weak pointers
1917 /* XX This is a hack adapted from cgc.c. These don't work too
1918 * efficiently with the gencgc as a list of the weak pointers is
1919 * maintained within the objects which causes writes to the pages. A
1920 * limited attempt is made to avoid unnecessary writes, but this needs
1921 * a re-think. */
1922 static sword_t
1923 scav_weak_pointer(lispobj *where, lispobj object)
1925 /* Since we overwrite the 'next' field, we have to make
1926 * sure not to do so for pointers already in the list.
1927 * Instead of searching the list of weak_pointers each
1928 * time, we ensure that next is always NULL when the weak
1929 * pointer isn't in the list, and not NULL otherwise.
1930 * Since we can't use NULL to denote end of list, we
1931 * use a pointer back to the same weak_pointer.
1933 struct weak_pointer * wp = (struct weak_pointer*)where;
1935 if (NULL == wp->next && weak_pointer_breakable_p(wp)) {
1936 wp->next = weak_pointers;
1937 weak_pointers = wp;
1938 if (NULL == wp->next)
1939 wp->next = wp;
1942 /* Do not let GC scavenge the value slot of the weak pointer.
1943 * (That is why it is a weak pointer.) */
1945 return WEAK_POINTER_NWORDS;
1949 lispobj *
1950 search_read_only_space(void *pointer)
1952 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1953 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1954 if ((pointer < (void *)start) || (pointer >= (void *)end))
1955 return NULL;
1956 return gc_search_space(start, pointer);
1959 lispobj *
1960 search_static_space(void *pointer)
1962 lispobj *start = (lispobj *)STATIC_SPACE_START;
1963 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1964 if ((pointer < (void *)start) || (pointer >= (void *)end))
1965 return NULL;
1966 return gc_search_space(start, pointer);
1969 /* a faster version for searching the dynamic space. This will work even
1970 * if the object is in a current allocation region. */
1971 lispobj *
1972 search_dynamic_space(void *pointer)
1974 page_index_t page_index = find_page_index(pointer);
1975 lispobj *start;
1977 /* The address may be invalid, so do some checks. */
1978 if ((page_index == -1) || page_free_p(page_index))
1979 return NULL;
1980 start = (lispobj *)page_scan_start(page_index);
1981 return gc_search_space(start, pointer);
1984 // Return the starting address of the object containing 'addr'
1985 // if and only if the object is one which would be evacuated from 'from_space'
1986 // were it allowed to be either discarded as garbage or moved.
1987 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1988 // Return 0 if there is no such object - that is, if addr is past the
1989 // end of the used bytes, or its pages are not in 'from_space' etc.
1990 static lispobj*
1991 conservative_root_p(void *addr, page_index_t addr_page_index)
1993 #ifdef GENCGC_IS_PRECISE
1994 /* If we're in precise gencgc (non-x86oid as of this writing) then
1995 * we are only called on valid object pointers in the first place,
1996 * so we just have to do a bounds-check against the heap, a
1997 * generation check, and the already-pinned check. */
1998 if ((page_table[addr_page_index].gen != from_space)
1999 || (page_table[addr_page_index].dont_move != 0))
2000 return 0;
2001 return (lispobj*)1;
2002 #else
2003 /* quick check 1: Address is quite likely to have been invalid. */
2004 if (page_free_p(addr_page_index)
2005 || (page_bytes_used(addr_page_index) == 0)
2006 || (page_table[addr_page_index].gen != from_space))
2007 return 0;
2008 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2010 /* quick check 2: Check the offset within the page.
2013 if (((uword_t)addr & (GENCGC_CARD_BYTES - 1)) > page_bytes_used(addr_page_index))
2014 return 0;
2016 /* Filter out anything which can't be a pointer to a Lisp object
2017 * (or, as a special case which also requires dont_move, a return
2018 * address referring to something in a CodeObject). This is
2019 * expensive but important, since it vastly reduces the
2020 * probability that random garbage will be bogusly interpreted as
2021 * a pointer which prevents a page from moving. */
2022 lispobj* object_start = search_dynamic_space(addr);
2023 if (!object_start) return 0;
2025 /* If the containing object is a code object and 'addr' points
2026 * anywhere beyond the boxed words,
2027 * presume it to be a valid unboxed return address. */
2028 if (instruction_ptr_p(addr, object_start))
2029 return object_start;
2031 /* Large object pages only contain ONE object, and it will never
2032 * be a CONS. However, arrays and bignums can be allocated larger
2033 * than necessary and then shrunk to fit, leaving what look like
2034 * (0 . 0) CONSes at the end. These appear valid to
2035 * properly_tagged_descriptor_p(), so pick them off here. */
2036 if (((lowtag_of((lispobj)addr) == LIST_POINTER_LOWTAG) &&
2037 page_table[addr_page_index].large_object)
2038 || !properly_tagged_descriptor_p(addr, object_start))
2039 return 0;
2041 return object_start;
2042 #endif
2045 /* Adjust large bignum and vector objects. This will adjust the
2046 * allocated region if the size has shrunk, and move unboxed objects
2047 * into unboxed pages. The pages are not promoted here, and the
2048 * promoted region is not added to the new_regions; this is really
2049 * only designed to be called from preserve_pointer(). Shouldn't fail
2050 * if this is missed, just may delay the moving of objects to unboxed
2051 * pages, and the freeing of pages. */
2052 static void
2053 maybe_adjust_large_object(lispobj *where)
2055 page_index_t first_page;
2056 page_index_t next_page;
2057 sword_t nwords;
2059 uword_t remaining_bytes;
2060 uword_t bytes_freed;
2061 uword_t old_bytes_used;
2063 int boxed;
2065 /* Check whether it's a vector or bignum object. */
2066 lispobj widetag = widetag_of(where[0]);
2067 if (widetag == SIMPLE_VECTOR_WIDETAG)
2068 boxed = BOXED_PAGE_FLAG;
2069 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
2070 boxed = UNBOXED_PAGE_FLAG;
2071 else
2072 return;
2074 /* Find its current size. */
2075 nwords = sizetab[widetag](where);
2077 first_page = find_page_index((void *)where);
2078 gc_assert(first_page >= 0);
2080 /* Note: Any page write-protection must be removed, else a later
2081 * scavenge_newspace may incorrectly not scavenge these pages.
2082 * This would not be necessary if they are added to the new areas,
2083 * but lets do it for them all (they'll probably be written
2084 * anyway?). */
2086 gc_assert(page_starts_contiguous_block_p(first_page));
2088 next_page = first_page;
2089 remaining_bytes = nwords*N_WORD_BYTES;
2090 while (remaining_bytes > GENCGC_CARD_BYTES) {
2091 gc_assert(page_table[next_page].gen == from_space);
2092 gc_assert(page_allocated_no_region_p(next_page));
2093 gc_assert(page_table[next_page].large_object);
2094 gc_assert(page_scan_start_offset(next_page) ==
2095 npage_bytes(next_page-first_page));
2096 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
2098 page_table[next_page].allocated = boxed;
2100 /* Shouldn't be write-protected at this stage. Essential that the
2101 * pages aren't. */
2102 gc_assert(!page_table[next_page].write_protected);
2103 remaining_bytes -= GENCGC_CARD_BYTES;
2104 next_page++;
2107 /* Now only one page remains, but the object may have shrunk so
2108 * there may be more unused pages which will be freed. */
2110 /* Object may have shrunk but shouldn't have grown - check. */
2111 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
2113 page_table[next_page].allocated = boxed;
2114 gc_assert(page_table[next_page].allocated ==
2115 page_table[first_page].allocated);
2117 /* Adjust the bytes_used. */
2118 old_bytes_used = page_bytes_used(next_page);
2119 set_page_bytes_used(next_page, remaining_bytes);
2121 bytes_freed = old_bytes_used - remaining_bytes;
2123 /* Free any remaining pages; needs care. */
2124 next_page++;
2125 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2126 (page_table[next_page].gen == from_space) &&
2127 page_allocated_no_region_p(next_page) &&
2128 page_table[next_page].large_object &&
2129 (page_scan_start_offset(next_page) ==
2130 npage_bytes(next_page - first_page))) {
2131 /* It checks out OK, free the page. We don't need to both zeroing
2132 * pages as this should have been done before shrinking the
2133 * object. These pages shouldn't be write protected as they
2134 * should be zero filled. */
2135 gc_assert(page_table[next_page].write_protected == 0);
2137 old_bytes_used = page_bytes_used(next_page);
2138 page_table[next_page].allocated = FREE_PAGE_FLAG;
2139 set_page_bytes_used(next_page, 0);
2140 bytes_freed += old_bytes_used;
2141 next_page++;
2144 if ((bytes_freed > 0) && gencgc_verbose) {
2145 FSHOW((stderr,
2146 "/maybe_adjust_large_object() freed %d\n",
2147 bytes_freed));
2150 generations[from_space].bytes_allocated -= bytes_freed;
2151 bytes_allocated -= bytes_freed;
2153 return;
2157 * Why is this restricted to protected objects only?
2158 * Because the rest of the page has been scavenged already,
2159 * and since that leaves forwarding pointers in the unprotected
2160 * areas you cannot scavenge it again until those are gone.
2162 static void
2163 scavenge_pinned_range(void* page_base, int start, int count)
2165 // 'start' and 'count' are expressed in units of dwords
2166 lispobj *where = (lispobj*)page_base + 2*start;
2167 heap_scavenge(where, where + 2*count);
2170 static void
2171 scavenge_pinned_ranges()
2173 page_index_t page;
2174 for (page = 0; page < last_free_page; page++) {
2175 in_use_marker_t* bitmap = pinned_dwords(page);
2176 if (bitmap)
2177 bitmap_scan(bitmap,
2178 GENCGC_CARD_BYTES / (2*N_WORD_BYTES) / N_WORD_BITS,
2179 0, scavenge_pinned_range, page_address(page));
2183 static void wipe_range(void* page_base, int start, int count)
2185 bzero((lispobj*)page_base + 2*start, count*2*N_WORD_BYTES);
2188 static void
2189 wipe_nonpinned_words()
2191 page_index_t i;
2192 in_use_marker_t* bitmap;
2194 for (i = 0; i < last_free_page; i++) {
2195 if (page_table[i].dont_move && (bitmap = pinned_dwords(i)) != 0) {
2196 bitmap_scan(bitmap,
2197 GENCGC_CARD_BYTES / (2*N_WORD_BYTES) / N_WORD_BITS,
2198 BIT_SCAN_INVERT | BIT_SCAN_CLEAR,
2199 wipe_range, page_address(i));
2200 page_table[i].has_pin_map = 0;
2201 // move the page to newspace
2202 int used = page_bytes_used(i);
2203 generations[new_space].bytes_allocated += used;
2204 generations[page_table[i].gen].bytes_allocated -= used;
2205 page_table[i].gen = new_space;
2208 #ifndef LISP_FEATURE_WIN32
2209 madvise(page_table_pinned_dwords, pins_map_size_in_bytes, MADV_DONTNEED);
2210 #endif
2213 static void __attribute__((unused))
2214 pin_words(page_index_t pageindex, lispobj *mark_which_pointer)
2216 gc_assert(mark_which_pointer);
2217 if (!page_table[pageindex].has_pin_map) {
2218 page_table[pageindex].has_pin_map = 1;
2219 #ifdef DEBUG
2221 int i;
2222 in_use_marker_t* map = pinned_dwords(pageindex);
2223 for (i=0; i<n_dwords_in_card/N_WORD_BITS; ++i)
2224 gc_assert(map[i] == 0);
2226 #endif
2228 lispobj *page_base = page_address(pageindex);
2229 unsigned int begin_dword_index = (mark_which_pointer - page_base) / 2;
2230 in_use_marker_t *bitmap = pinned_dwords(pageindex);
2231 if (bitmap[begin_dword_index/N_WORD_BITS]
2232 & ((uword_t)1 << (begin_dword_index % N_WORD_BITS)))
2233 return; // already seen this object
2235 lispobj header = *mark_which_pointer;
2236 int size = 2;
2237 // Don't bother calling a sizing function for cons cells.
2238 if (!is_cons_half(header))
2239 size = (sizetab[widetag_of(header)])(mark_which_pointer);
2240 gc_assert(size % 2 == 0);
2241 unsigned int end_dword_index = begin_dword_index + size / 2;
2242 unsigned int index;
2243 for (index = begin_dword_index; index < end_dword_index; index++)
2244 bitmap[index/N_WORD_BITS] |= (uword_t)1 << (index % N_WORD_BITS);
2247 /* Take a possible pointer to a Lisp object and mark its page in the
2248 * page_table so that it will not be relocated during a GC.
2250 * This involves locating the page it points to, then backing up to
2251 * the start of its region, then marking all pages dont_move from there
2252 * up to the first page that's not full or has a different generation
2254 * It is assumed that all the page static flags have been cleared at
2255 * the start of a GC.
2257 * It is also assumed that the current gc_alloc() region has been
2258 * flushed and the tables updated. */
2260 // TODO: there's probably a way to be a little more efficient here.
2261 // As things are, we start by finding the object that encloses 'addr',
2262 // then we see if 'addr' was a "valid" Lisp pointer to that object
2263 // - meaning we expect the correct lowtag on the pointer - except
2264 // that for code objects we don't require a correct lowtag
2265 // and we allow a pointer to anywhere in the object.
2267 // It should be possible to avoid calling search_dynamic_space
2268 // more of the time. First, check if the page pointed to might hold code.
2269 // If it does, then we continue regardless of the pointer's lowtag
2270 // (because of the special allowance). If the page definitely does *not*
2271 // hold code, then we require up front that the lowtake make sense,
2272 // by doing the same checks that are in properly_tagged_descriptor_p.
2274 // Problem: when code is allocated from a per-thread region,
2275 // does it ensure that the occupied pages are flagged as having code?
2277 static void
2278 preserve_pointer(void *addr)
2280 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2281 /* Immobile space MUST be lower than dynamic space,
2282 or else this test needs to be revised */
2283 if (addr < (void*)IMMOBILE_SPACE_END) {
2284 extern void immobile_space_preserve_pointer(void*);
2285 immobile_space_preserve_pointer(addr);
2286 return;
2288 #endif
2289 page_index_t addr_page_index = find_page_index(addr);
2290 lispobj *object_start;
2292 if (addr_page_index == -1
2293 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2294 return;
2296 /* (Now that we know that addr_page_index is in range, it's
2297 * safe to index into page_table[] with it.) */
2298 unsigned int region_allocation = page_table[addr_page_index].allocated;
2300 /* Find the beginning of the region. Note that there may be
2301 * objects in the region preceding the one that we were passed a
2302 * pointer to: if this is the case, we will write-protect all the
2303 * previous objects' pages too. */
2305 #if 0
2306 /* I think this'd work just as well, but without the assertions.
2307 * -dan 2004.01.01 */
2308 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2309 #else
2310 page_index_t first_page = addr_page_index;
2311 while (!page_starts_contiguous_block_p(first_page)) {
2312 --first_page;
2313 /* Do some checks. */
2314 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2315 gc_assert(page_table[first_page].gen == from_space);
2316 gc_assert(page_table[first_page].allocated == region_allocation);
2318 #endif
2320 /* Adjust any large objects before promotion as they won't be
2321 * copied after promotion. */
2322 if (page_table[first_page].large_object) {
2323 maybe_adjust_large_object(page_address(first_page));
2324 /* It may have moved to unboxed pages. */
2325 region_allocation = page_table[first_page].allocated;
2328 /* Now work forward until the end of this contiguous area is found,
2329 * marking all pages as dont_move. */
2330 page_index_t i;
2331 for (i = first_page; ;i++) {
2332 gc_assert(page_table[i].allocated == region_allocation);
2334 /* Mark the page static. */
2335 page_table[i].dont_move = 1;
2337 /* It is essential that the pages are not write protected as
2338 * they may have pointers into the old-space which need
2339 * scavenging. They shouldn't be write protected at this
2340 * stage. */
2341 gc_assert(!page_table[i].write_protected);
2343 /* Check whether this is the last page in this contiguous block.. */
2344 if (page_ends_contiguous_block_p(i, from_space))
2345 break;
2348 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2349 /* Do not do this for multi-page objects. Those pages do not need
2350 * object wipeout anyway.
2352 if (do_wipe_p && i == first_page) // single-page object
2353 pin_words(first_page, object_start);
2354 #endif
2356 /* Check that the page is now static. */
2357 gc_assert(page_table[addr_page_index].dont_move != 0);
2360 /* If the given page is not write-protected, then scan it for pointers
2361 * to younger generations or the top temp. generation, if no
2362 * suspicious pointers are found then the page is write-protected.
2364 * Care is taken to check for pointers to the current gc_alloc()
2365 * region if it is a younger generation or the temp. generation. This
2366 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2367 * the gc_alloc_generation does not need to be checked as this is only
2368 * called from scavenge_generation() when the gc_alloc generation is
2369 * younger, so it just checks if there is a pointer to the current
2370 * region.
2372 * We return 1 if the page was write-protected, else 0. */
2373 static int
2374 update_page_write_prot(page_index_t page)
2376 generation_index_t gen = page_table[page].gen;
2377 sword_t j;
2378 int wp_it = 1;
2379 void **page_addr = (void **)page_address(page);
2380 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2382 /* Shouldn't be a free page. */
2383 gc_assert(page_allocated_p(page));
2384 gc_assert(page_bytes_used(page) != 0);
2386 /* Skip if it's already write-protected, pinned, or unboxed */
2387 if (page_table[page].write_protected
2388 /* FIXME: What's the reason for not write-protecting pinned pages? */
2389 || page_table[page].dont_move
2390 || page_unboxed_p(page))
2391 return (0);
2393 /* Scan the page for pointers to younger generations or the
2394 * top temp. generation. */
2396 /* This is conservative: any word satisfying is_lisp_pointer() is
2397 * assumed to be a pointer. To do otherwise would require a family
2398 * of scavenge-like functions. */
2399 for (j = 0; j < num_words; j++) {
2400 void *ptr = *(page_addr+j);
2401 page_index_t index;
2402 lispobj __attribute__((unused)) header;
2404 if (!is_lisp_pointer((lispobj)ptr))
2405 continue;
2406 /* Check that it's in the dynamic space */
2407 if ((index = find_page_index(ptr)) != -1) {
2408 if (/* Does it point to a younger or the temp. generation? */
2409 (page_allocated_p(index)
2410 && (page_bytes_used(index) != 0)
2411 && ((page_table[index].gen < gen)
2412 || (page_table[index].gen == SCRATCH_GENERATION)))
2414 /* Or does it point within a current gc_alloc() region? */
2415 || ((boxed_region.start_addr <= ptr)
2416 && (ptr <= boxed_region.free_pointer))
2417 || ((unboxed_region.start_addr <= ptr)
2418 && (ptr <= unboxed_region.free_pointer))) {
2419 wp_it = 0;
2420 break;
2423 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2424 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2425 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2426 // This is *possibly* a pointer to an object in immobile space,
2427 // given that above two conditions were satisfied.
2428 // But unlike in the dynamic space case, we need to read a byte
2429 // from the object to determine its generation, which requires care.
2430 // Consider an unboxed word that looks like a pointer to a word that
2431 // looks like fun-header-widetag. We can't naively back up to the
2432 // underlying code object since the alleged header might not be one.
2433 int obj_gen = gen; // Make comparison fail if we fall through
2434 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2435 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2436 } else if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
2437 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2438 // This is a heuristic, since we're not actually looking for
2439 // an object boundary. Precise scanning of 'page' would obviate
2440 // the guard conditions here.
2441 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2442 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2443 obj_gen = __immobile_obj_generation(code);
2445 // A bogus generation number implies a not-really-pointer,
2446 // but it won't cause misbehavior.
2447 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2448 wp_it = 0;
2449 break;
2452 #endif
2455 if (wp_it == 1) {
2456 /* Write-protect the page. */
2457 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2459 os_protect((void *)page_addr,
2460 GENCGC_CARD_BYTES,
2461 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2463 /* Note the page as protected in the page tables. */
2464 page_table[page].write_protected = 1;
2467 return (wp_it);
2470 /* Is this page holding a normal (non-hashtable) large-object
2471 * simple-vector? */
2472 static inline boolean large_simple_vector_p(page_index_t page) {
2473 if (!page_table[page].large_object)
2474 return 0;
2475 lispobj object = *(lispobj *)page_address(page);
2476 return widetag_of(object) == SIMPLE_VECTOR_WIDETAG &&
2477 (HeaderValue(object) & 0xFF) == subtype_VectorNormal;
2481 /* Scavenge all generations from FROM to TO, inclusive, except for
2482 * new_space which needs special handling, as new objects may be
2483 * added which are not checked here - use scavenge_newspace generation.
2485 * Write-protected pages should not have any pointers to the
2486 * from_space so do need scavenging; thus write-protected pages are
2487 * not always scavenged. There is some code to check that these pages
2488 * are not written; but to check fully the write-protected pages need
2489 * to be scavenged by disabling the code to skip them.
2491 * Under the current scheme when a generation is GCed the younger
2492 * generations will be empty. So, when a generation is being GCed it
2493 * is only necessary to scavenge the older generations for pointers
2494 * not the younger. So a page that does not have pointers to younger
2495 * generations does not need to be scavenged.
2497 * The write-protection can be used to note pages that don't have
2498 * pointers to younger pages. But pages can be written without having
2499 * pointers to younger generations. After the pages are scavenged here
2500 * they can be scanned for pointers to younger generations and if
2501 * there are none the page can be write-protected.
2503 * One complication is when the newspace is the top temp. generation.
2505 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2506 * that none were written, which they shouldn't be as they should have
2507 * no pointers to younger generations. This breaks down for weak
2508 * pointers as the objects contain a link to the next and are written
2509 * if a weak pointer is scavenged. Still it's a useful check. */
2510 static void
2511 scavenge_generations(generation_index_t from, generation_index_t to)
2513 page_index_t i;
2514 page_index_t num_wp = 0;
2516 #define SC_GEN_CK 0
2517 #if SC_GEN_CK
2518 /* Clear the write_protected_cleared flags on all pages. */
2519 for (i = 0; i < page_table_pages; i++)
2520 page_table[i].write_protected_cleared = 0;
2521 #endif
2523 for (i = 0; i < last_free_page; i++) {
2524 generation_index_t generation = page_table[i].gen;
2525 if (page_boxed_p(i)
2526 && (page_bytes_used(i) != 0)
2527 && (generation != new_space)
2528 && (generation >= from)
2529 && (generation <= to)) {
2530 page_index_t last_page,j;
2531 int write_protected=1;
2533 /* This should be the start of a region */
2534 gc_assert(page_starts_contiguous_block_p(i));
2536 if (large_simple_vector_p(i)) {
2537 /* Scavenge only the unprotected pages of a
2538 * large-object vector, other large objects could be
2539 * handled as well, but vectors are easier to deal
2540 * with and are more likely to grow to very large
2541 * sizes where avoiding scavenging the whole thing is
2542 * worthwile */
2543 if (!page_table[i].write_protected) {
2544 scavenge((lispobj*)page_address(i) + 2,
2545 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2546 update_page_write_prot(i);
2548 for (last_page = i + 1; ; last_page++) {
2549 lispobj* start = page_address(last_page);
2550 write_protected = page_table[last_page].write_protected;
2551 if (page_ends_contiguous_block_p(last_page, generation)) {
2552 if (!write_protected) {
2553 scavenge(start, page_bytes_used(last_page) / N_WORD_BYTES);
2554 update_page_write_prot(last_page);
2556 break;
2558 if (!write_protected) {
2559 scavenge(start, GENCGC_CARD_BYTES / N_WORD_BYTES);
2560 update_page_write_prot(last_page);
2563 } else {
2564 /* Now work forward until the end of the region */
2565 for (last_page = i; ; last_page++) {
2566 write_protected =
2567 write_protected && page_table[last_page].write_protected;
2568 if (page_ends_contiguous_block_p(last_page, generation))
2569 break;
2571 if (!write_protected) {
2572 heap_scavenge(page_address(i),
2573 (lispobj*)((char*)page_address(last_page)
2574 + page_bytes_used(last_page)));
2576 /* Now scan the pages and write protect those that
2577 * don't have pointers to younger generations. */
2578 if (enable_page_protection) {
2579 for (j = i; j <= last_page; j++) {
2580 num_wp += update_page_write_prot(j);
2583 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2584 FSHOW((stderr,
2585 "/write protected %d pages within generation %d\n",
2586 num_wp, generation));
2590 i = last_page;
2594 #if SC_GEN_CK
2595 /* Check that none of the write_protected pages in this generation
2596 * have been written to. */
2597 for (i = 0; i < page_table_pages; i++) {
2598 if (page_allocated_p(i)
2599 && (page_bytes_used(i) != 0)
2600 && (page_table[i].gen == generation)
2601 && (page_table[i].write_protected_cleared != 0)) {
2602 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2603 FSHOW((stderr,
2604 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2605 page_bytes_used(i),
2606 scan_start_offset(page_table[i]),
2607 page_table[i].dont_move));
2608 lose("write to protected page %d in scavenge_generation()\n", i);
2611 #endif
2615 /* Scavenge a newspace generation. As it is scavenged new objects may
2616 * be allocated to it; these will also need to be scavenged. This
2617 * repeats until there are no more objects unscavenged in the
2618 * newspace generation.
2620 * To help improve the efficiency, areas written are recorded by
2621 * gc_alloc() and only these scavenged. Sometimes a little more will be
2622 * scavenged, but this causes no harm. An easy check is done that the
2623 * scavenged bytes equals the number allocated in the previous
2624 * scavenge.
2626 * Write-protected pages are not scanned except if they are marked
2627 * dont_move in which case they may have been promoted and still have
2628 * pointers to the from space.
2630 * Write-protected pages could potentially be written by alloc however
2631 * to avoid having to handle re-scavenging of write-protected pages
2632 * gc_alloc() does not write to write-protected pages.
2634 * New areas of objects allocated are recorded alternatively in the two
2635 * new_areas arrays below. */
2636 static struct new_area new_areas_1[NUM_NEW_AREAS];
2637 static struct new_area new_areas_2[NUM_NEW_AREAS];
2639 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2640 extern unsigned int immobile_scav_queue_count;
2641 extern void
2642 gc_init_immobile(),
2643 update_immobile_nursery_bits(),
2644 scavenge_immobile_roots(generation_index_t,generation_index_t),
2645 scavenge_immobile_newspace(),
2646 sweep_immobile_space(int raise),
2647 write_protect_immobile_space();
2648 #else
2649 #define immobile_scav_queue_count 0
2650 #endif
2652 /* Do one full scan of the new space generation. This is not enough to
2653 * complete the job as new objects may be added to the generation in
2654 * the process which are not scavenged. */
2655 static void
2656 scavenge_newspace_generation_one_scan(generation_index_t generation)
2658 page_index_t i;
2660 FSHOW((stderr,
2661 "/starting one full scan of newspace generation %d\n",
2662 generation));
2663 for (i = 0; i < last_free_page; i++) {
2664 /* Note that this skips over open regions when it encounters them. */
2665 if (page_boxed_p(i)
2666 && (page_bytes_used(i) != 0)
2667 && (page_table[i].gen == generation)
2668 && ((page_table[i].write_protected == 0)
2669 /* (This may be redundant as write_protected is now
2670 * cleared before promotion.) */
2671 || (page_table[i].dont_move == 1))) {
2672 page_index_t last_page;
2673 int all_wp=1;
2675 /* The scavenge will start at the scan_start_offset of
2676 * page i.
2678 * We need to find the full extent of this contiguous
2679 * block in case objects span pages.
2681 * Now work forward until the end of this contiguous area
2682 * is found. A small area is preferred as there is a
2683 * better chance of its pages being write-protected. */
2684 for (last_page = i; ;last_page++) {
2685 /* If all pages are write-protected and movable,
2686 * then no need to scavenge */
2687 all_wp=all_wp && page_table[last_page].write_protected &&
2688 !page_table[last_page].dont_move;
2690 /* Check whether this is the last page in this
2691 * contiguous block */
2692 if (page_ends_contiguous_block_p(last_page, generation))
2693 break;
2696 /* Do a limited check for write-protected pages. */
2697 if (!all_wp) {
2698 new_areas_ignore_page = last_page;
2699 heap_scavenge(page_scan_start(i),
2700 (lispobj*)((char*)page_address(last_page)
2701 + page_bytes_used(last_page)));
2703 i = last_page;
2706 FSHOW((stderr,
2707 "/done with one full scan of newspace generation %d\n",
2708 generation));
2711 /* Do a complete scavenge of the newspace generation. */
2712 static void
2713 scavenge_newspace_generation(generation_index_t generation)
2715 size_t i;
2717 /* the new_areas array currently being written to by gc_alloc() */
2718 struct new_area (*current_new_areas)[] = &new_areas_1;
2719 size_t current_new_areas_index;
2721 /* the new_areas created by the previous scavenge cycle */
2722 struct new_area (*previous_new_areas)[] = NULL;
2723 size_t previous_new_areas_index;
2725 /* Flush the current regions updating the tables. */
2726 gc_alloc_update_all_page_tables(0);
2728 /* Turn on the recording of new areas by gc_alloc(). */
2729 new_areas = current_new_areas;
2730 new_areas_index = 0;
2732 /* Don't need to record new areas that get scavenged anyway during
2733 * scavenge_newspace_generation_one_scan. */
2734 record_new_objects = 1;
2736 /* Start with a full scavenge. */
2737 scavenge_newspace_generation_one_scan(generation);
2739 /* Record all new areas now. */
2740 record_new_objects = 2;
2742 /* Give a chance to weak hash tables to make other objects live.
2743 * FIXME: The algorithm implemented here for weak hash table gcing
2744 * is O(W^2+N) as Bruno Haible warns in
2745 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2746 * see "Implementation 2". */
2747 scav_weak_hash_tables();
2749 /* Flush the current regions updating the tables. */
2750 gc_alloc_update_all_page_tables(0);
2752 /* Grab new_areas_index. */
2753 current_new_areas_index = new_areas_index;
2755 /*FSHOW((stderr,
2756 "The first scan is finished; current_new_areas_index=%d.\n",
2757 current_new_areas_index));*/
2759 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2760 /* Move the current to the previous new areas */
2761 previous_new_areas = current_new_areas;
2762 previous_new_areas_index = current_new_areas_index;
2764 /* Scavenge all the areas in previous new areas. Any new areas
2765 * allocated are saved in current_new_areas. */
2767 /* Allocate an array for current_new_areas; alternating between
2768 * new_areas_1 and 2 */
2769 if (previous_new_areas == &new_areas_1)
2770 current_new_areas = &new_areas_2;
2771 else
2772 current_new_areas = &new_areas_1;
2774 /* Set up for gc_alloc(). */
2775 new_areas = current_new_areas;
2776 new_areas_index = 0;
2778 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2779 scavenge_immobile_newspace();
2780 #endif
2781 /* Check whether previous_new_areas had overflowed. */
2782 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2784 /* New areas of objects allocated have been lost so need to do a
2785 * full scan to be sure! If this becomes a problem try
2786 * increasing NUM_NEW_AREAS. */
2787 if (gencgc_verbose) {
2788 SHOW("new_areas overflow, doing full scavenge");
2791 /* Don't need to record new areas that get scavenged
2792 * anyway during scavenge_newspace_generation_one_scan. */
2793 record_new_objects = 1;
2795 scavenge_newspace_generation_one_scan(generation);
2797 /* Record all new areas now. */
2798 record_new_objects = 2;
2800 scav_weak_hash_tables();
2802 /* Flush the current regions updating the tables. */
2803 gc_alloc_update_all_page_tables(0);
2805 } else {
2807 /* Work through previous_new_areas. */
2808 for (i = 0; i < previous_new_areas_index; i++) {
2809 page_index_t page = (*previous_new_areas)[i].page;
2810 size_t offset = (*previous_new_areas)[i].offset;
2811 size_t size = (*previous_new_areas)[i].size;
2812 gc_assert(size % N_WORD_BYTES == 0);
2813 lispobj *start = (lispobj*)((char*)page_address(page) + offset);
2814 heap_scavenge(start, (lispobj*)((char*)start + size));
2817 scav_weak_hash_tables();
2819 /* Flush the current regions updating the tables. */
2820 gc_alloc_update_all_page_tables(0);
2823 current_new_areas_index = new_areas_index;
2825 /*FSHOW((stderr,
2826 "The re-scan has finished; current_new_areas_index=%d.\n",
2827 current_new_areas_index));*/
2830 /* Turn off recording of areas allocated by gc_alloc(). */
2831 record_new_objects = 0;
2833 #if SC_NS_GEN_CK
2835 page_index_t i;
2836 /* Check that none of the write_protected pages in this generation
2837 * have been written to. */
2838 for (i = 0; i < page_table_pages; i++) {
2839 if (page_allocated_p(i)
2840 && (page_bytes_used(i) != 0)
2841 && (page_table[i].gen == generation)
2842 && (page_table[i].write_protected_cleared != 0)
2843 && (page_table[i].dont_move == 0)) {
2844 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2845 i, generation, page_table[i].dont_move);
2849 #endif
2852 /* Un-write-protect all the pages in from_space. This is done at the
2853 * start of a GC else there may be many page faults while scavenging
2854 * the newspace (I've seen drive the system time to 99%). These pages
2855 * would need to be unprotected anyway before unmapping in
2856 * free_oldspace; not sure what effect this has on paging.. */
2857 static void
2858 unprotect_oldspace(void)
2860 page_index_t i;
2861 void *region_addr = 0;
2862 void *page_addr = 0;
2863 uword_t region_bytes = 0;
2865 for (i = 0; i < last_free_page; i++) {
2866 if (page_allocated_p(i)
2867 && (page_bytes_used(i) != 0)
2868 && (page_table[i].gen == from_space)) {
2870 /* Remove any write-protection. We should be able to rely
2871 * on the write-protect flag to avoid redundant calls. */
2872 if (page_table[i].write_protected) {
2873 page_table[i].write_protected = 0;
2874 page_addr = page_address(i);
2875 if (!region_addr) {
2876 /* First region. */
2877 region_addr = page_addr;
2878 region_bytes = GENCGC_CARD_BYTES;
2879 } else if (region_addr + region_bytes == page_addr) {
2880 /* Region continue. */
2881 region_bytes += GENCGC_CARD_BYTES;
2882 } else {
2883 /* Unprotect previous region. */
2884 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2885 /* First page in new region. */
2886 region_addr = page_addr;
2887 region_bytes = GENCGC_CARD_BYTES;
2892 if (region_addr) {
2893 /* Unprotect last region. */
2894 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2898 /* Work through all the pages and free any in from_space. This
2899 * assumes that all objects have been copied or promoted to an older
2900 * generation. Bytes_allocated and the generation bytes_allocated
2901 * counter are updated. The number of bytes freed is returned. */
2902 static uword_t
2903 free_oldspace(void)
2905 uword_t bytes_freed = 0;
2906 page_index_t first_page, last_page;
2908 first_page = 0;
2910 do {
2911 /* Find a first page for the next region of pages. */
2912 while ((first_page < last_free_page)
2913 && (page_free_p(first_page)
2914 || (page_bytes_used(first_page) == 0)
2915 || (page_table[first_page].gen != from_space)))
2916 first_page++;
2918 if (first_page >= last_free_page)
2919 break;
2921 /* Find the last page of this region. */
2922 last_page = first_page;
2924 do {
2925 /* Free the page. */
2926 bytes_freed += page_bytes_used(last_page);
2927 generations[page_table[last_page].gen].bytes_allocated -=
2928 page_bytes_used(last_page);
2929 page_table[last_page].allocated = FREE_PAGE_FLAG;
2930 set_page_bytes_used(last_page, 0);
2931 /* Should already be unprotected by unprotect_oldspace(). */
2932 gc_assert(!page_table[last_page].write_protected);
2933 last_page++;
2935 while ((last_page < last_free_page)
2936 && page_allocated_p(last_page)
2937 && (page_bytes_used(last_page) != 0)
2938 && (page_table[last_page].gen == from_space));
2940 #ifdef READ_PROTECT_FREE_PAGES
2941 os_protect(page_address(first_page),
2942 npage_bytes(last_page-first_page),
2943 OS_VM_PROT_NONE);
2944 #endif
2945 first_page = last_page;
2946 } while (first_page < last_free_page);
2948 bytes_allocated -= bytes_freed;
2949 return bytes_freed;
2952 #if 0
2953 /* Print some information about a pointer at the given address. */
2954 static void
2955 print_ptr(lispobj *addr)
2957 /* If addr is in the dynamic space then out the page information. */
2958 page_index_t pi1 = find_page_index((void*)addr);
2960 if (pi1 != -1)
2961 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
2962 addr,
2963 pi1,
2964 page_table[pi1].allocated,
2965 page_table[pi1].gen,
2966 page_bytes_used(pi1),
2967 scan_start_offset(page_table[pi1]),
2968 page_table[pi1].dont_move);
2969 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
2970 *(addr-4),
2971 *(addr-3),
2972 *(addr-2),
2973 *(addr-1),
2974 *(addr-0),
2975 *(addr+1),
2976 *(addr+2),
2977 *(addr+3),
2978 *(addr+4));
2980 #endif
2982 static int
2983 is_in_stack_space(lispobj ptr)
2985 /* For space verification: Pointers can be valid if they point
2986 * to a thread stack space. This would be faster if the thread
2987 * structures had page-table entries as if they were part of
2988 * the heap space. */
2989 struct thread *th;
2990 for_each_thread(th) {
2991 if ((th->control_stack_start <= (lispobj *)ptr) &&
2992 (th->control_stack_end >= (lispobj *)ptr)) {
2993 return 1;
2996 return 0;
2999 // NOTE: This function can produces false failure indications,
3000 // usually related to dynamic space pointing to the stack of a
3001 // dead thread, but there may be other reasons as well.
3002 static void
3003 verify_space(lispobj *start, size_t words)
3005 extern int valid_lisp_pointer_p(lispobj);
3006 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3007 int is_in_readonly_space =
3008 (READ_ONLY_SPACE_START <= (uword_t)start &&
3009 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3010 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3011 int is_in_immobile_space =
3012 (IMMOBILE_SPACE_START <= (uword_t)start &&
3013 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3014 #endif
3016 while (words > 0) {
3017 size_t count = 1;
3018 lispobj thing = *start;
3019 lispobj __attribute__((unused)) pointee;
3021 if (is_lisp_pointer(thing)) {
3022 page_index_t page_index = find_page_index((void*)thing);
3023 sword_t to_readonly_space =
3024 (READ_ONLY_SPACE_START <= thing &&
3025 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3026 sword_t to_static_space =
3027 (STATIC_SPACE_START <= thing &&
3028 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3029 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3030 sword_t to_immobile_space =
3031 (IMMOBILE_SPACE_START <= thing &&
3032 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
3033 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
3034 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3035 #endif
3037 /* Does it point to the dynamic space? */
3038 if (page_index != -1) {
3039 /* If it's within the dynamic space it should point to a used page. */
3040 if (!page_allocated_p(page_index))
3041 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3042 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3043 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3044 /* Check that it doesn't point to a forwarding pointer! */
3045 if (*native_pointer(thing) == 0x01) {
3046 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3048 /* Check that its not in the RO space as it would then be a
3049 * pointer from the RO to the dynamic space. */
3050 if (is_in_readonly_space) {
3051 lose("ptr to dynamic space %p from RO space %x\n",
3052 thing, start);
3054 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3055 // verify all immobile space -> dynamic space pointers
3056 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3057 lose("Ptr %p @ %p sees junk.\n", thing, start);
3059 #endif
3060 /* Does it point to a plausible object? This check slows
3061 * it down a lot (so it's commented out).
3063 * "a lot" is serious: it ate 50 minutes cpu time on
3064 * my duron 950 before I came back from lunch and
3065 * killed it.
3067 * FIXME: Add a variable to enable this
3068 * dynamically. */
3070 if (!valid_lisp_pointer_p((lispobj *)thing) {
3071 lose("ptr %p to invalid object %p\n", thing, start);
3074 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3075 } else if (to_immobile_space) {
3076 // the object pointed to must not have been discarded as garbage
3077 if (!other_immediate_lowtag_p(*native_pointer(thing))
3078 || immobile_filler_p(native_pointer(thing)))
3079 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3080 // verify all pointers to immobile space
3081 if (!valid_lisp_pointer_p(thing))
3082 lose("Ptr %p @ %p sees junk.\n", thing, start);
3083 #endif
3084 } else {
3085 extern char __attribute__((unused)) funcallable_instance_tramp;
3086 /* Verify that it points to another valid space. */
3087 if (!to_readonly_space && !to_static_space
3088 && !is_in_stack_space(thing)) {
3089 lose("Ptr %p @ %p sees junk.\n", thing, start);
3092 } else {
3093 if (!(fixnump(thing))) {
3094 /* skip fixnums */
3095 switch(widetag_of(*start)) {
3097 /* boxed objects */
3098 case SIMPLE_VECTOR_WIDETAG:
3099 case RATIO_WIDETAG:
3100 case COMPLEX_WIDETAG:
3101 case SIMPLE_ARRAY_WIDETAG:
3102 case COMPLEX_BASE_STRING_WIDETAG:
3103 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3104 case COMPLEX_CHARACTER_STRING_WIDETAG:
3105 #endif
3106 case COMPLEX_VECTOR_NIL_WIDETAG:
3107 case COMPLEX_BIT_VECTOR_WIDETAG:
3108 case COMPLEX_VECTOR_WIDETAG:
3109 case COMPLEX_ARRAY_WIDETAG:
3110 case CLOSURE_HEADER_WIDETAG:
3111 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3112 case VALUE_CELL_HEADER_WIDETAG:
3113 case SYMBOL_HEADER_WIDETAG:
3114 case CHARACTER_WIDETAG:
3115 #if N_WORD_BITS == 64
3116 case SINGLE_FLOAT_WIDETAG:
3117 #endif
3118 case UNBOUND_MARKER_WIDETAG:
3119 break;
3120 case FDEFN_WIDETAG:
3121 #ifdef LISP_FEATURE_IMMOBILE_CODE
3122 verify_space(start + 1, 2);
3123 pointee = fdefn_raw_referent((struct fdefn*)start);
3124 verify_space(&pointee, 1);
3125 count = 4;
3126 #endif
3127 break;
3129 case INSTANCE_HEADER_WIDETAG:
3130 if (instance_layout(start)) {
3131 lispobj bitmap =
3132 ((struct layout*)
3133 native_pointer(instance_layout(start)))->bitmap;
3134 sword_t nslots = instance_length(thing) | 1;
3135 instance_scan(verify_space, start+1, nslots, bitmap);
3136 count = 1 + nslots;
3138 break;
3139 case CODE_HEADER_WIDETAG:
3141 /* Check that it's not in the dynamic space.
3142 * FIXME: Isn't is supposed to be OK for code
3143 * objects to be in the dynamic space these days? */
3144 /* It is for byte compiled code, but there's
3145 * no byte compilation in SBCL anymore. */
3146 if (is_in_dynamic_space
3147 /* Only when enabled */
3148 && verify_dynamic_code_check) {
3149 FSHOW((stderr,
3150 "/code object at %p in the dynamic space\n",
3151 start));
3154 struct code *code = (struct code *) start;
3155 sword_t nheader_words = code_header_words(code->header);
3156 /* Scavenge the boxed section of the code data block */
3157 verify_space(start + 1, nheader_words - 1);
3159 /* Scavenge the boxed section of each function
3160 * object in the code data block. */
3161 for_each_simple_fun(i, fheaderp, code, 1, {
3162 verify_space(SIMPLE_FUN_SCAV_START(fheaderp),
3163 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3164 count = nheader_words + code_instruction_words(code->code_size);
3165 break;
3168 /* unboxed objects */
3169 case BIGNUM_WIDETAG:
3170 #if N_WORD_BITS != 64
3171 case SINGLE_FLOAT_WIDETAG:
3172 #endif
3173 case DOUBLE_FLOAT_WIDETAG:
3174 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3175 case LONG_FLOAT_WIDETAG:
3176 #endif
3177 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3178 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3179 #endif
3180 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3181 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3182 #endif
3183 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3184 case COMPLEX_LONG_FLOAT_WIDETAG:
3185 #endif
3186 #ifdef SIMD_PACK_WIDETAG
3187 case SIMD_PACK_WIDETAG:
3188 #endif
3189 #include "genesis/specialized-vectors.inc"
3190 case SAP_WIDETAG:
3191 case WEAK_POINTER_WIDETAG:
3192 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3193 case NO_TLS_VALUE_MARKER_WIDETAG:
3194 #endif
3195 count = (sizetab[widetag_of(*start)])(start);
3196 break;
3198 default:
3199 lose("Unhandled widetag %p at %p\n",
3200 widetag_of(*start), start);
3204 start += count;
3205 words -= count;
3209 static void verify_dynamic_space();
3211 static void
3212 verify_gc(void)
3214 /* FIXME: It would be nice to make names consistent so that
3215 * foo_size meant size *in* *bytes* instead of size in some
3216 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3217 * Some counts of lispobjs are called foo_count; it might be good
3218 * to grep for all foo_size and rename the appropriate ones to
3219 * foo_count. */
3220 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3221 # ifdef __linux__
3222 // Try this verification if marknsweep was compiled with extra debugging.
3223 // But weak symbols don't work on macOS.
3224 extern void __attribute__((weak)) check_varyobj_pages();
3225 if (&check_varyobj_pages) check_varyobj_pages();
3226 # endif
3227 verify_space((lispobj*)IMMOBILE_SPACE_START,
3228 (lispobj*)SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)
3229 - (lispobj*)IMMOBILE_SPACE_START);
3230 verify_space((lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START,
3231 (lispobj*)SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0)
3232 - (lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START);
3233 #endif
3234 sword_t read_only_space_size =
3235 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3236 - (lispobj*)READ_ONLY_SPACE_START;
3237 sword_t static_space_size =
3238 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3239 - (lispobj*)STATIC_SPACE_START;
3240 struct thread *th;
3241 for_each_thread(th) {
3242 sword_t binding_stack_size =
3243 (lispobj*)get_binding_stack_pointer(th)
3244 - (lispobj*)th->binding_stack_start;
3245 verify_space(th->binding_stack_start, binding_stack_size);
3247 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3248 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3249 verify_dynamic_space();
3252 void
3253 walk_generation(void (*proc)(lispobj*,size_t),
3254 generation_index_t generation)
3256 page_index_t i;
3257 int genmask = generation >= 0 ? 1 << generation : ~0;
3259 for (i = 0; i < last_free_page; i++) {
3260 if (page_allocated_p(i)
3261 && (page_bytes_used(i) != 0)
3262 && ((1 << page_table[i].gen) & genmask)) {
3263 page_index_t last_page;
3265 /* This should be the start of a contiguous block */
3266 gc_assert(page_starts_contiguous_block_p(i));
3268 /* Need to find the full extent of this contiguous block in case
3269 objects span pages. */
3271 /* Now work forward until the end of this contiguous area is
3272 found. */
3273 for (last_page = i; ;last_page++)
3274 /* Check whether this is the last page in this contiguous
3275 * block. */
3276 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3277 break;
3279 proc(page_address(i),
3280 ((uword_t)(page_bytes_used(last_page) + npage_bytes(last_page-i)))
3281 / N_WORD_BYTES);
3282 i = last_page;
3286 static void verify_generation(generation_index_t generation)
3288 walk_generation(verify_space, generation);
3291 /* Check that all the free space is zero filled. */
3292 static void
3293 verify_zero_fill(void)
3295 page_index_t page;
3297 for (page = 0; page < last_free_page; page++) {
3298 if (page_free_p(page)) {
3299 /* The whole page should be zero filled. */
3300 sword_t *start_addr = (sword_t *)page_address(page);
3301 sword_t i;
3302 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3303 if (start_addr[i] != 0) {
3304 lose("free page not zero at %x\n", start_addr + i);
3307 } else {
3308 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3309 if (free_bytes > 0) {
3310 sword_t *start_addr = (sword_t *)((uword_t)page_address(page)
3311 + page_bytes_used(page));
3312 sword_t size = free_bytes / N_WORD_BYTES;
3313 sword_t i;
3314 for (i = 0; i < size; i++) {
3315 if (start_addr[i] != 0) {
3316 lose("free region not zero at %x\n", start_addr + i);
3324 /* External entry point for verify_zero_fill */
3325 void
3326 gencgc_verify_zero_fill(void)
3328 /* Flush the alloc regions updating the tables. */
3329 gc_alloc_update_all_page_tables(1);
3330 SHOW("verifying zero fill");
3331 verify_zero_fill();
3334 static void
3335 verify_dynamic_space(void)
3337 verify_generation(-1);
3338 if (gencgc_enable_verify_zero_fill)
3339 verify_zero_fill();
3342 /* Write-protect all the dynamic boxed pages in the given generation. */
3343 static void
3344 write_protect_generation_pages(generation_index_t generation)
3346 page_index_t start;
3348 gc_assert(generation < SCRATCH_GENERATION);
3350 for (start = 0; start < last_free_page; start++) {
3351 if (protect_page_p(start, generation)) {
3352 void *page_start;
3353 page_index_t last;
3355 /* Note the page as protected in the page tables. */
3356 page_table[start].write_protected = 1;
3358 for (last = start + 1; last < last_free_page; last++) {
3359 if (!protect_page_p(last, generation))
3360 break;
3361 page_table[last].write_protected = 1;
3364 page_start = (void *)page_address(start);
3366 os_protect(page_start,
3367 npage_bytes(last - start),
3368 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3370 start = last;
3374 if (gencgc_verbose > 1) {
3375 FSHOW((stderr,
3376 "/write protected %d of %d pages in generation %d\n",
3377 count_write_protect_generation_pages(generation),
3378 count_generation_pages(generation),
3379 generation));
3383 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3384 static void
3385 preserve_context_registers (os_context_t *c)
3387 void **ptr;
3388 /* On Darwin the signal context isn't a contiguous block of memory,
3389 * so just preserve_pointering its contents won't be sufficient.
3391 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3392 #if defined LISP_FEATURE_X86
3393 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3394 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3395 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3396 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3397 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3398 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3399 preserve_pointer((void*)*os_context_pc_addr(c));
3400 #elif defined LISP_FEATURE_X86_64
3401 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3402 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3403 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3404 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3405 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3406 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3407 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3408 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3409 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3410 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3411 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3412 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3413 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3414 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3415 preserve_pointer((void*)*os_context_pc_addr(c));
3416 #else
3417 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3418 #endif
3419 #endif
3420 #if !defined(LISP_FEATURE_WIN32)
3421 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3422 preserve_pointer(*ptr);
3424 #endif
3426 #endif
3428 static void
3429 move_pinned_pages_to_newspace()
3431 page_index_t i;
3433 /* scavenge() will evacuate all oldspace pages, but no newspace
3434 * pages. Pinned pages are precisely those pages which must not
3435 * be evacuated, so move them to newspace directly. */
3437 for (i = 0; i < last_free_page; i++) {
3438 if (page_table[i].dont_move &&
3439 /* dont_move is cleared lazily, so validate the space as well. */
3440 page_table[i].gen == from_space) {
3441 if (pinned_dwords(i) && do_wipe_p) {
3442 // do not move to newspace after all, this will be word-wiped
3443 continue;
3445 page_table[i].gen = new_space;
3446 /* And since we're moving the pages wholesale, also adjust
3447 * the generation allocation counters. */
3448 int used = page_bytes_used(i);
3449 generations[new_space].bytes_allocated += used;
3450 generations[from_space].bytes_allocated -= used;
3455 /* Garbage collect a generation. If raise is 0 then the remains of the
3456 * generation are not raised to the next generation. */
3457 static void
3458 garbage_collect_generation(generation_index_t generation, int raise)
3460 page_index_t i;
3461 struct thread *th;
3463 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3465 /* The oldest generation can't be raised. */
3466 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3468 /* Check if weak hash tables were processed in the previous GC. */
3469 gc_assert(weak_hash_tables == NULL);
3471 /* Initialize the weak pointer list. */
3472 weak_pointers = NULL;
3474 /* When a generation is not being raised it is transported to a
3475 * temporary generation (NUM_GENERATIONS), and lowered when
3476 * done. Set up this new generation. There should be no pages
3477 * allocated to it yet. */
3478 if (!raise) {
3479 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3482 /* Set the global src and dest. generations */
3483 from_space = generation;
3484 if (raise)
3485 new_space = generation+1;
3486 else
3487 new_space = SCRATCH_GENERATION;
3489 /* Change to a new space for allocation, resetting the alloc_start_page */
3490 gc_alloc_generation = new_space;
3491 generations[new_space].alloc_start_page = 0;
3492 generations[new_space].alloc_unboxed_start_page = 0;
3493 generations[new_space].alloc_large_start_page = 0;
3494 generations[new_space].alloc_large_unboxed_start_page = 0;
3496 /* Before any pointers are preserved, the dont_move flags on the
3497 * pages need to be cleared. */
3498 for (i = 0; i < last_free_page; i++)
3499 if(page_table[i].gen==from_space) {
3500 page_table[i].dont_move = 0;
3501 gc_assert(pinned_dwords(i) == NULL);
3504 /* Un-write-protect the old-space pages. This is essential for the
3505 * promoted pages as they may contain pointers into the old-space
3506 * which need to be scavenged. It also helps avoid unnecessary page
3507 * faults as forwarding pointers are written into them. They need to
3508 * be un-protected anyway before unmapping later. */
3509 unprotect_oldspace();
3511 /* Scavenge the stacks' conservative roots. */
3513 /* there are potentially two stacks for each thread: the main
3514 * stack, which may contain Lisp pointers, and the alternate stack.
3515 * We don't ever run Lisp code on the altstack, but it may
3516 * host a sigcontext with lisp objects in it */
3518 /* what we need to do: (1) find the stack pointer for the main
3519 * stack; scavenge it (2) find the interrupt context on the
3520 * alternate stack that might contain lisp values, and scavenge
3521 * that */
3523 /* we assume that none of the preceding applies to the thread that
3524 * initiates GC. If you ever call GC from inside an altstack
3525 * handler, you will lose. */
3527 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3528 /* And if we're saving a core, there's no point in being conservative. */
3529 if (conservative_stack) {
3530 for_each_thread(th) {
3531 void **ptr;
3532 void **esp=(void **)-1;
3533 if (th->state == STATE_DEAD)
3534 continue;
3535 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3536 /* Conservative collect_garbage is always invoked with a
3537 * foreign C call or an interrupt handler on top of every
3538 * existing thread, so the stored SP in each thread
3539 * structure is valid, no matter which thread we are looking
3540 * at. For threads that were running Lisp code, the pitstop
3541 * and edge functions maintain this value within the
3542 * interrupt or exception handler. */
3543 esp = os_get_csp(th);
3544 assert_on_stack(th, esp);
3546 /* In addition to pointers on the stack, also preserve the
3547 * return PC, the only value from the context that we need
3548 * in addition to the SP. The return PC gets saved by the
3549 * foreign call wrapper, and removed from the control stack
3550 * into a register. */
3551 preserve_pointer(th->pc_around_foreign_call);
3553 /* And on platforms with interrupts: scavenge ctx registers. */
3555 /* Disabled on Windows, because it does not have an explicit
3556 * stack of `interrupt_contexts'. The reported CSP has been
3557 * chosen so that the current context on the stack is
3558 * covered by the stack scan. See also set_csp_from_context(). */
3559 # ifndef LISP_FEATURE_WIN32
3560 if (th != arch_os_get_current_thread()) {
3561 long k = fixnum_value(
3562 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3563 while (k > 0)
3564 preserve_context_registers(th->interrupt_contexts[--k]);
3566 # endif
3567 # elif defined(LISP_FEATURE_SB_THREAD)
3568 sword_t i,free;
3569 if(th==arch_os_get_current_thread()) {
3570 /* Somebody is going to burn in hell for this, but casting
3571 * it in two steps shuts gcc up about strict aliasing. */
3572 esp = (void **)((void *)&raise);
3573 } else {
3574 void **esp1;
3575 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3576 for(i=free-1;i>=0;i--) {
3577 os_context_t *c=th->interrupt_contexts[i];
3578 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3579 if (esp1>=(void **)th->control_stack_start &&
3580 esp1<(void **)th->control_stack_end) {
3581 if(esp1<esp) esp=esp1;
3582 preserve_context_registers(c);
3586 # else
3587 esp = (void **)((void *)&raise);
3588 # endif
3589 if (!esp || esp == (void*) -1)
3590 lose("garbage_collect: no SP known for thread %x (OS %x)",
3591 th, th->os_thread);
3592 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3593 preserve_pointer(*ptr);
3597 #else
3598 /* Non-x86oid systems don't have "conservative roots" as such, but
3599 * the same mechanism is used for objects pinned for use by alien
3600 * code. */
3601 for_each_thread(th) {
3602 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3603 while (pin_list != NIL) {
3604 struct cons *list_entry =
3605 (struct cons *)native_pointer(pin_list);
3606 preserve_pointer((void*)list_entry->car);
3607 pin_list = list_entry->cdr;
3610 #endif
3612 #if QSHOW
3613 if (gencgc_verbose > 1) {
3614 sword_t num_dont_move_pages = count_dont_move_pages();
3615 fprintf(stderr,
3616 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3617 num_dont_move_pages,
3618 npage_bytes(num_dont_move_pages));
3620 #endif
3622 /* Now that all of the pinned (dont_move) pages are known, and
3623 * before we start to scavenge (and thus relocate) objects,
3624 * relocate the pinned pages to newspace, so that the scavenger
3625 * will not attempt to relocate their contents. */
3626 move_pinned_pages_to_newspace();
3628 /* Scavenge all the rest of the roots. */
3630 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3632 * If not x86, we need to scavenge the interrupt context(s) and the
3633 * control stack.
3636 struct thread *th;
3637 for_each_thread(th) {
3638 scavenge_interrupt_contexts(th);
3639 scavenge_control_stack(th);
3642 # ifdef LISP_FEATURE_SB_SAFEPOINT
3643 /* In this case, scrub all stacks right here from the GCing thread
3644 * instead of doing what the comment below says. Suboptimal, but
3645 * easier. */
3646 for_each_thread(th)
3647 scrub_thread_control_stack(th);
3648 # else
3649 /* Scrub the unscavenged control stack space, so that we can't run
3650 * into any stale pointers in a later GC (this is done by the
3651 * stop-for-gc handler in the other threads). */
3652 scrub_control_stack();
3653 # endif
3655 #endif
3657 /* Scavenge the Lisp functions of the interrupt handlers, taking
3658 * care to avoid SIG_DFL and SIG_IGN. */
3659 for (i = 0; i < NSIG; i++) {
3660 union interrupt_handler handler = interrupt_handlers[i];
3661 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3662 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3663 scavenge((lispobj *)(interrupt_handlers + i), 1);
3666 /* Scavenge the binding stacks. */
3668 struct thread *th;
3669 for_each_thread(th) {
3670 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3671 th->binding_stack_start;
3672 scavenge((lispobj *) th->binding_stack_start,len);
3673 #ifdef LISP_FEATURE_SB_THREAD
3674 /* do the tls as well */
3675 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3676 (sizeof (struct thread))/(sizeof (lispobj));
3677 scavenge((lispobj *) (th+1),len);
3678 #endif
3682 /* Scavenge static space. */
3683 if (gencgc_verbose > 1) {
3684 FSHOW((stderr,
3685 "/scavenge static space: %d bytes\n",
3686 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3688 heap_scavenge((lispobj*)STATIC_SPACE_START,
3689 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3691 /* All generations but the generation being GCed need to be
3692 * scavenged. The new_space generation needs special handling as
3693 * objects may be moved in - it is handled separately below. */
3694 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3695 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3696 #endif
3697 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3699 scavenge_pinned_ranges();
3701 /* Finally scavenge the new_space generation. Keep going until no
3702 * more objects are moved into the new generation */
3703 scavenge_newspace_generation(new_space);
3705 /* FIXME: I tried reenabling this check when debugging unrelated
3706 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3707 * Since the current GC code seems to work well, I'm guessing that
3708 * this debugging code is just stale, but I haven't tried to
3709 * figure it out. It should be figured out and then either made to
3710 * work or just deleted. */
3712 #define RESCAN_CHECK 0
3713 #if RESCAN_CHECK
3714 /* As a check re-scavenge the newspace once; no new objects should
3715 * be found. */
3717 os_vm_size_t old_bytes_allocated = bytes_allocated;
3718 os_vm_size_t bytes_allocated;
3720 /* Start with a full scavenge. */
3721 scavenge_newspace_generation_one_scan(new_space);
3723 /* Flush the current regions, updating the tables. */
3724 gc_alloc_update_all_page_tables(1);
3726 bytes_allocated = bytes_allocated - old_bytes_allocated;
3728 if (bytes_allocated != 0) {
3729 lose("Rescan of new_space allocated %d more bytes.\n",
3730 bytes_allocated);
3733 #endif
3735 scan_weak_hash_tables();
3736 scan_weak_pointers();
3737 wipe_nonpinned_words();
3738 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3739 // Do this last, because until wipe_nonpinned_words() happens,
3740 // not all page table entries have the 'gen' value updated,
3741 // which we need to correctly find all old->young pointers.
3742 sweep_immobile_space(raise);
3743 #endif
3745 /* Flush the current regions, updating the tables. */
3746 gc_alloc_update_all_page_tables(0);
3748 /* Free the pages in oldspace, but not those marked dont_move. */
3749 free_oldspace();
3751 /* If the GC is not raising the age then lower the generation back
3752 * to its normal generation number */
3753 if (!raise) {
3754 for (i = 0; i < last_free_page; i++)
3755 if ((page_bytes_used(i) != 0)
3756 && (page_table[i].gen == SCRATCH_GENERATION))
3757 page_table[i].gen = generation;
3758 gc_assert(generations[generation].bytes_allocated == 0);
3759 generations[generation].bytes_allocated =
3760 generations[SCRATCH_GENERATION].bytes_allocated;
3761 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3764 /* Reset the alloc_start_page for generation. */
3765 generations[generation].alloc_start_page = 0;
3766 generations[generation].alloc_unboxed_start_page = 0;
3767 generations[generation].alloc_large_start_page = 0;
3768 generations[generation].alloc_large_unboxed_start_page = 0;
3770 if (generation >= verify_gens) {
3771 if (gencgc_verbose) {
3772 SHOW("verifying");
3774 verify_gc();
3777 /* Set the new gc trigger for the GCed generation. */
3778 generations[generation].gc_trigger =
3779 generations[generation].bytes_allocated
3780 + generations[generation].bytes_consed_between_gc;
3782 if (raise)
3783 generations[generation].num_gc = 0;
3784 else
3785 ++generations[generation].num_gc;
3789 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3790 sword_t
3791 update_dynamic_space_free_pointer(void)
3793 page_index_t last_page = -1, i;
3795 for (i = 0; i < last_free_page; i++)
3796 if (page_allocated_p(i) && (page_bytes_used(i) != 0))
3797 last_page = i;
3799 last_free_page = last_page+1;
3801 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3802 return 0; /* dummy value: return something ... */
3805 static void
3806 remap_page_range (page_index_t from, page_index_t to)
3808 /* There's a mysterious Solaris/x86 problem with using mmap
3809 * tricks for memory zeroing. See sbcl-devel thread
3810 * "Re: patch: standalone executable redux".
3812 #if defined(LISP_FEATURE_SUNOS)
3813 zero_and_mark_pages(from, to);
3814 #else
3815 const page_index_t
3816 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3817 release_mask = release_granularity-1,
3818 end = to+1,
3819 aligned_from = (from+release_mask)&~release_mask,
3820 aligned_end = (end&~release_mask);
3822 if (aligned_from < aligned_end) {
3823 zero_pages_with_mmap(aligned_from, aligned_end-1);
3824 if (aligned_from != from)
3825 zero_and_mark_pages(from, aligned_from-1);
3826 if (aligned_end != end)
3827 zero_and_mark_pages(aligned_end, end-1);
3828 } else {
3829 zero_and_mark_pages(from, to);
3831 #endif
3834 static void
3835 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3837 page_index_t first_page, last_page;
3839 if (forcibly)
3840 return remap_page_range(from, to);
3842 for (first_page = from; first_page <= to; first_page++) {
3843 if (page_allocated_p(first_page) || !page_need_to_zero(first_page))
3844 continue;
3846 last_page = first_page + 1;
3847 while (page_free_p(last_page) &&
3848 (last_page <= to) &&
3849 (page_need_to_zero(last_page)))
3850 last_page++;
3852 remap_page_range(first_page, last_page-1);
3854 first_page = last_page;
3858 generation_index_t small_generation_limit = 1;
3860 /* GC all generations newer than last_gen, raising the objects in each
3861 * to the next older generation - we finish when all generations below
3862 * last_gen are empty. Then if last_gen is due for a GC, or if
3863 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3864 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3866 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3867 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3868 void
3869 collect_garbage(generation_index_t last_gen)
3871 generation_index_t gen = 0, i;
3872 int raise, more = 0;
3873 int gen_to_wp;
3874 /* The largest value of last_free_page seen since the time
3875 * remap_free_pages was called. */
3876 static page_index_t high_water_mark = 0;
3878 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3879 log_generation_stats(gc_logfile, "=== GC Start ===");
3881 gc_active_p = 1;
3883 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3884 FSHOW((stderr,
3885 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3886 last_gen));
3887 last_gen = 0;
3890 /* Flush the alloc regions updating the tables. */
3891 gc_alloc_update_all_page_tables(1);
3893 /* Verify the new objects created by Lisp code. */
3894 if (pre_verify_gen_0) {
3895 FSHOW((stderr, "pre-checking generation 0\n"));
3896 verify_generation(0);
3899 if (gencgc_verbose > 1)
3900 print_generation_stats();
3902 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3903 /* Immobile space generation bits are lazily updated for gen0
3904 (not touched on every object allocation) so do it now */
3905 update_immobile_nursery_bits();
3906 #endif
3908 do {
3909 /* Collect the generation. */
3911 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3912 /* Never raise the oldest generation. Never raise the extra generation
3913 * collected due to more-flag. */
3914 raise = 0;
3915 more = 0;
3916 } else {
3917 raise =
3918 (gen < last_gen)
3919 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3920 /* If we would not normally raise this one, but we're
3921 * running low on space in comparison to the object-sizes
3922 * we've been seeing, raise it and collect the next one
3923 * too. */
3924 if (!raise && gen == last_gen) {
3925 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3926 raise = more;
3930 if (gencgc_verbose > 1) {
3931 FSHOW((stderr,
3932 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3933 gen,
3934 raise,
3935 generations[gen].bytes_allocated,
3936 generations[gen].gc_trigger,
3937 generations[gen].num_gc));
3940 /* If an older generation is being filled, then update its
3941 * memory age. */
3942 if (raise == 1) {
3943 generations[gen+1].cum_sum_bytes_allocated +=
3944 generations[gen+1].bytes_allocated;
3947 garbage_collect_generation(gen, raise);
3949 /* Reset the memory age cum_sum. */
3950 generations[gen].cum_sum_bytes_allocated = 0;
3952 if (gencgc_verbose > 1) {
3953 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3954 print_generation_stats();
3957 gen++;
3958 } while ((gen <= gencgc_oldest_gen_to_gc)
3959 && ((gen < last_gen)
3960 || more
3961 || (raise
3962 && (generations[gen].bytes_allocated
3963 > generations[gen].gc_trigger)
3964 && (generation_average_age(gen)
3965 > generations[gen].minimum_age_before_gc))));
3967 /* Now if gen-1 was raised all generations before gen are empty.
3968 * If it wasn't raised then all generations before gen-1 are empty.
3970 * Now objects within this gen's pages cannot point to younger
3971 * generations unless they are written to. This can be exploited
3972 * by write-protecting the pages of gen; then when younger
3973 * generations are GCed only the pages which have been written
3974 * need scanning. */
3975 if (raise)
3976 gen_to_wp = gen;
3977 else
3978 gen_to_wp = gen - 1;
3980 /* There's not much point in WPing pages in generation 0 as it is
3981 * never scavenged (except promoted pages). */
3982 if ((gen_to_wp > 0) && enable_page_protection) {
3983 /* Check that they are all empty. */
3984 for (i = 0; i < gen_to_wp; i++) {
3985 if (generations[i].bytes_allocated)
3986 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3987 gen_to_wp, i);
3989 write_protect_generation_pages(gen_to_wp);
3991 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3992 write_protect_immobile_space();
3993 #endif
3995 /* Set gc_alloc() back to generation 0. The current regions should
3996 * be flushed after the above GCs. */
3997 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3998 gc_alloc_generation = 0;
4000 /* Save the high-water mark before updating last_free_page */
4001 if (last_free_page > high_water_mark)
4002 high_water_mark = last_free_page;
4004 update_dynamic_space_free_pointer();
4006 /* Update auto_gc_trigger. Make sure we trigger the next GC before
4007 * running out of heap! */
4008 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
4009 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4010 else
4011 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
4013 if(gencgc_verbose)
4014 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
4015 auto_gc_trigger);
4017 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4018 * back to the OS.
4020 if (gen > small_generation_limit) {
4021 if (last_free_page > high_water_mark)
4022 high_water_mark = last_free_page;
4023 remap_free_pages(0, high_water_mark, 0);
4024 high_water_mark = 0;
4027 gc_active_p = 0;
4028 large_allocation = 0;
4030 log_generation_stats(gc_logfile, "=== GC End ===");
4031 SHOW("returning from collect_garbage");
4034 void
4035 gc_init(void)
4037 page_index_t i;
4039 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4040 alloc_gc_page();
4041 #endif
4043 /* Compute the number of pages needed for the dynamic space.
4044 * Dynamic space size should be aligned on page size. */
4045 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
4046 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4048 /* Default nursery size to 5% of the total dynamic space size,
4049 * min 1Mb. */
4050 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
4051 if (bytes_consed_between_gcs < (1024*1024))
4052 bytes_consed_between_gcs = 1024*1024;
4054 /* The page_table must be allocated using "calloc" to initialize
4055 * the page structures correctly. There used to be a separate
4056 * initialization loop (now commented out; see below) but that was
4057 * unnecessary and did hurt startup time. */
4058 page_table = calloc(page_table_pages, sizeof(struct page));
4059 gc_assert(page_table);
4060 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4061 gc_init_immobile();
4062 #endif
4064 size_t pins_map_size_in_bytes =
4065 (n_dwords_in_card / N_WORD_BITS) * sizeof (uword_t) * page_table_pages;
4066 /* We use mmap directly here so that we can use a minimum of
4067 system calls per page during GC.
4068 All we need here now is a madvise(DONTNEED) at the end of GC. */
4069 page_table_pinned_dwords
4070 = (in_use_marker_t*)os_validate(NULL, pins_map_size_in_bytes);
4071 /* We do not need to zero */
4072 gc_assert(page_table_pinned_dwords);
4074 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4075 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4077 /* The page structures are initialized implicitly when page_table
4078 * is allocated with "calloc" above. Formerly we had the following
4079 * explicit initialization here (comments converted to C99 style
4080 * for readability as C's block comments don't nest):
4082 * // Initialize each page structure.
4083 * for (i = 0; i < page_table_pages; i++) {
4084 * // Initialize all pages as free.
4085 * page_table[i].allocated = FREE_PAGE_FLAG;
4086 * page_table[i].bytes_used = 0;
4088 * // Pages are not write-protected at startup.
4089 * page_table[i].write_protected = 0;
4092 * Without this loop the image starts up much faster when dynamic
4093 * space is large -- which it is on 64-bit platforms already by
4094 * default -- and when "calloc" for large arrays is implemented
4095 * using copy-on-write of a page of zeroes -- which it is at least
4096 * on Linux. In this case the pages that page_table_pages is stored
4097 * in are mapped and cleared not before the corresponding part of
4098 * dynamic space is used. For example, this saves clearing 16 MB of
4099 * memory at startup if the page size is 4 KB and the size of
4100 * dynamic space is 4 GB.
4101 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4102 * asserted below: */
4104 /* Compile time assertion: If triggered, declares an array
4105 * of dimension -1 forcing a syntax error. The intent of the
4106 * assignment is to avoid an "unused variable" warning. */
4107 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4108 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4111 bytes_allocated = 0;
4113 /* Initialize the generations. */
4114 for (i = 0; i < NUM_GENERATIONS; i++) {
4115 generations[i].alloc_start_page = 0;
4116 generations[i].alloc_unboxed_start_page = 0;
4117 generations[i].alloc_large_start_page = 0;
4118 generations[i].alloc_large_unboxed_start_page = 0;
4119 generations[i].bytes_allocated = 0;
4120 generations[i].gc_trigger = 2000000;
4121 generations[i].num_gc = 0;
4122 generations[i].cum_sum_bytes_allocated = 0;
4123 /* the tune-able parameters */
4124 generations[i].bytes_consed_between_gc
4125 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4126 generations[i].number_of_gcs_before_promotion = 1;
4127 generations[i].minimum_age_before_gc = 0.75;
4130 /* Initialize gc_alloc. */
4131 gc_alloc_generation = 0;
4132 gc_set_region_empty(&boxed_region);
4133 gc_set_region_empty(&unboxed_region);
4135 last_free_page = 0;
4138 /* Pick up the dynamic space from after a core load.
4140 * The ALLOCATION_POINTER points to the end of the dynamic space.
4143 static void
4144 gencgc_pickup_dynamic(void)
4146 page_index_t page = 0;
4147 void *alloc_ptr = (void *)get_alloc_pointer();
4148 lispobj *prev=(lispobj *)page_address(page);
4149 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4151 bytes_allocated = 0;
4153 do {
4154 lispobj *first,*ptr= (lispobj *)page_address(page);
4156 if (!gencgc_partial_pickup || page_allocated_p(page)) {
4157 /* It is possible, though rare, for the saved page table
4158 * to contain free pages below alloc_ptr. */
4159 page_table[page].gen = gen;
4160 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4161 page_table[page].large_object = 0;
4162 page_table[page].write_protected = 0;
4163 page_table[page].write_protected_cleared = 0;
4164 page_table[page].dont_move = 0;
4165 set_page_need_to_zero(page, 1);
4167 bytes_allocated += GENCGC_CARD_BYTES;
4170 if (!gencgc_partial_pickup) {
4171 page_table[page].allocated = BOXED_PAGE_FLAG;
4172 first = gc_search_space3(ptr, prev, (ptr+2));
4173 if(ptr == first)
4174 prev=ptr;
4175 set_page_scan_start_offset(page,
4176 page_address(page) - (void *)prev);
4178 page++;
4179 } while (page_address(page) < alloc_ptr);
4181 last_free_page = page;
4183 generations[gen].bytes_allocated = bytes_allocated;
4185 gc_alloc_update_all_page_tables(1);
4186 write_protect_generation_pages(gen);
4189 void
4190 gc_initialize_pointers(void)
4192 gencgc_pickup_dynamic();
4196 /* alloc(..) is the external interface for memory allocation. It
4197 * allocates to generation 0. It is not called from within the garbage
4198 * collector as it is only external uses that need the check for heap
4199 * size (GC trigger) and to disable the interrupts (interrupts are
4200 * always disabled during a GC).
4202 * The vops that call alloc(..) assume that the returned space is zero-filled.
4203 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4205 * The check for a GC trigger is only performed when the current
4206 * region is full, so in most cases it's not needed. */
4208 static inline lispobj *
4209 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4210 struct thread *thread)
4212 #ifndef LISP_FEATURE_WIN32
4213 lispobj alloc_signal;
4214 #endif
4215 void *new_obj;
4216 void *new_free_pointer;
4217 os_vm_size_t trigger_bytes = 0;
4219 gc_assert(nbytes > 0);
4221 /* Check for alignment allocation problems. */
4222 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4223 && ((nbytes & LOWTAG_MASK) == 0));
4225 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4226 /* Must be inside a PA section. */
4227 gc_assert(get_pseudo_atomic_atomic(thread));
4228 #endif
4230 if ((os_vm_size_t) nbytes > large_allocation)
4231 large_allocation = nbytes;
4233 /* maybe we can do this quickly ... */
4234 new_free_pointer = region->free_pointer + nbytes;
4235 if (new_free_pointer <= region->end_addr) {
4236 new_obj = (void*)(region->free_pointer);
4237 region->free_pointer = new_free_pointer;
4238 return(new_obj); /* yup */
4241 /* We don't want to count nbytes against auto_gc_trigger unless we
4242 * have to: it speeds up the tenuring of objects and slows down
4243 * allocation. However, unless we do so when allocating _very_
4244 * large objects we are in danger of exhausting the heap without
4245 * running sufficient GCs.
4247 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4248 trigger_bytes = nbytes;
4250 /* we have to go the long way around, it seems. Check whether we
4251 * should GC in the near future
4253 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4254 /* Don't flood the system with interrupts if the need to gc is
4255 * already noted. This can happen for example when SUB-GC
4256 * allocates or after a gc triggered in a WITHOUT-GCING. */
4257 if (SymbolValue(GC_PENDING,thread) == NIL) {
4258 /* set things up so that GC happens when we finish the PA
4259 * section */
4260 SetSymbolValue(GC_PENDING,T,thread);
4261 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4262 #ifdef LISP_FEATURE_SB_SAFEPOINT
4263 thread_register_gc_trigger();
4264 #else
4265 set_pseudo_atomic_interrupted(thread);
4266 #ifdef GENCGC_IS_PRECISE
4267 /* PPC calls alloc() from a trap
4268 * look up the most context if it's from a trap. */
4270 os_context_t *context =
4271 thread->interrupt_data->allocation_trap_context;
4272 maybe_save_gc_mask_and_block_deferrables
4273 (context ? os_context_sigmask_addr(context) : NULL);
4275 #else
4276 maybe_save_gc_mask_and_block_deferrables(NULL);
4277 #endif
4278 #endif
4282 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4284 #ifndef LISP_FEATURE_WIN32
4285 /* for sb-prof, and not supported on Windows yet */
4286 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4287 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4288 if ((sword_t) alloc_signal <= 0) {
4289 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4290 raise(SIGPROF);
4291 } else {
4292 SetSymbolValue(ALLOC_SIGNAL,
4293 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4294 thread);
4297 #endif
4299 return (new_obj);
4302 lispobj *
4303 general_alloc(sword_t nbytes, int page_type_flag)
4305 struct thread *thread = arch_os_get_current_thread();
4306 /* Select correct region, and call general_alloc_internal with it.
4307 * For other then boxed allocation we must lock first, since the
4308 * region is shared. */
4309 if (BOXED_PAGE_FLAG & page_type_flag) {
4310 #ifdef LISP_FEATURE_SB_THREAD
4311 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4312 #else
4313 struct alloc_region *region = &boxed_region;
4314 #endif
4315 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4316 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4317 lispobj * obj;
4318 int result;
4319 result = thread_mutex_lock(&allocation_lock);
4320 gc_assert(!result);
4321 obj = general_alloc_internal(nbytes, page_type_flag, &unboxed_region, thread);
4322 result = thread_mutex_unlock(&allocation_lock);
4323 gc_assert(!result);
4324 return obj;
4325 } else {
4326 lose("bad page type flag: %d", page_type_flag);
4330 lispobj AMD64_SYSV_ABI *
4331 alloc(sword_t nbytes)
4333 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4334 struct thread *self = arch_os_get_current_thread();
4335 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4336 if (!was_pseudo_atomic)
4337 set_pseudo_atomic_atomic(self);
4338 #else
4339 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4340 #endif
4342 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4344 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4345 if (!was_pseudo_atomic)
4346 clear_pseudo_atomic_atomic(self);
4347 #endif
4349 return result;
4353 * shared support for the OS-dependent signal handlers which
4354 * catch GENCGC-related write-protect violations
4356 void unhandled_sigmemoryfault(void* addr);
4358 /* Depending on which OS we're running under, different signals might
4359 * be raised for a violation of write protection in the heap. This
4360 * function factors out the common generational GC magic which needs
4361 * to invoked in this case, and should be called from whatever signal
4362 * handler is appropriate for the OS we're running under.
4364 * Return true if this signal is a normal generational GC thing that
4365 * we were able to handle, or false if it was abnormal and control
4366 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4368 * We have two control flags for this: one causes us to ignore faults
4369 * on unprotected pages completely, and the second complains to stderr
4370 * but allows us to continue without losing.
4372 extern boolean ignore_memoryfaults_on_unprotected_pages;
4373 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4375 extern boolean continue_after_memoryfault_on_unprotected_pages;
4376 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4379 gencgc_handle_wp_violation(void* fault_addr)
4381 page_index_t page_index = find_page_index(fault_addr);
4383 #if QSHOW_SIGNALS
4384 FSHOW((stderr,
4385 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4386 fault_addr, page_index));
4387 #endif
4389 /* Check whether the fault is within the dynamic space. */
4390 if (page_index == (-1)) {
4391 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4392 extern int immobile_space_handle_wp_violation(void*);
4393 if (immobile_space_handle_wp_violation(fault_addr))
4394 return 1;
4395 #endif
4397 /* It can be helpful to be able to put a breakpoint on this
4398 * case to help diagnose low-level problems. */
4399 unhandled_sigmemoryfault(fault_addr);
4401 /* not within the dynamic space -- not our responsibility */
4402 return 0;
4404 } else {
4405 int ret;
4406 ret = thread_mutex_lock(&free_pages_lock);
4407 gc_assert(ret == 0);
4408 if (page_table[page_index].write_protected) {
4409 /* Unprotect the page. */
4410 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4411 page_table[page_index].write_protected_cleared = 1;
4412 page_table[page_index].write_protected = 0;
4413 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4414 /* The only acceptable reason for this signal on a heap
4415 * access is that GENCGC write-protected the page.
4416 * However, if two CPUs hit a wp page near-simultaneously,
4417 * we had better not have the second one lose here if it
4418 * does this test after the first one has already set wp=0
4420 if(page_table[page_index].write_protected_cleared != 1) {
4421 void lisp_backtrace(int frames);
4422 lisp_backtrace(10);
4423 fprintf(stderr,
4424 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4425 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4426 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4427 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4428 " page.bytes_used: %u\n"
4429 " page.allocated: %d\n"
4430 " page.write_protected: %d\n"
4431 " page.write_protected_cleared: %d\n"
4432 " page.generation: %d\n",
4433 fault_addr,
4434 page_index,
4435 boxed_region.first_page,
4436 boxed_region.last_page,
4437 page_scan_start_offset(page_index),
4438 page_bytes_used(page_index),
4439 page_table[page_index].allocated,
4440 page_table[page_index].write_protected,
4441 page_table[page_index].write_protected_cleared,
4442 page_table[page_index].gen);
4443 if (!continue_after_memoryfault_on_unprotected_pages)
4444 lose("Feh.\n");
4447 ret = thread_mutex_unlock(&free_pages_lock);
4448 gc_assert(ret == 0);
4449 /* Don't worry, we can handle it. */
4450 return 1;
4453 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4454 * it's not just a case of the program hitting the write barrier, and
4455 * are about to let Lisp deal with it. It's basically just a
4456 * convenient place to set a gdb breakpoint. */
4457 void
4458 unhandled_sigmemoryfault(void *addr)
4461 static void
4462 update_thread_page_tables(struct thread *th)
4464 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4465 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4466 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4467 #endif
4470 /* GC is single-threaded and all memory allocations during a
4471 collection happen in the GC thread, so it is sufficient to update
4472 all the the page tables once at the beginning of a collection and
4473 update only page tables of the GC thread during the collection. */
4474 void gc_alloc_update_all_page_tables(int for_all_threads)
4476 /* Flush the alloc regions updating the tables. */
4477 struct thread *th;
4478 if (for_all_threads) {
4479 for_each_thread(th) {
4480 update_thread_page_tables(th);
4483 else {
4484 th = arch_os_get_current_thread();
4485 if (th) {
4486 update_thread_page_tables(th);
4489 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4490 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4493 void
4494 gc_set_region_empty(struct alloc_region *region)
4496 region->first_page = 0;
4497 region->last_page = -1;
4498 region->start_addr = page_address(0);
4499 region->free_pointer = page_address(0);
4500 region->end_addr = page_address(0);
4503 static void
4504 zero_all_free_pages()
4506 page_index_t i;
4508 for (i = 0; i < last_free_page; i++) {
4509 if (page_free_p(i)) {
4510 #ifdef READ_PROTECT_FREE_PAGES
4511 os_protect(page_address(i),
4512 GENCGC_CARD_BYTES,
4513 OS_VM_PROT_ALL);
4514 #endif
4515 zero_pages(i, i);
4520 /* Things to do before doing a final GC before saving a core (without
4521 * purify).
4523 * + Pages in large_object pages aren't moved by the GC, so we need to
4524 * unset that flag from all pages.
4525 * + The pseudo-static generation isn't normally collected, but it seems
4526 * reasonable to collect it at least when saving a core. So move the
4527 * pages to a normal generation.
4529 static void
4530 prepare_for_final_gc ()
4532 page_index_t i;
4534 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4535 extern void prepare_immobile_space_for_final_gc();
4536 prepare_immobile_space_for_final_gc ();
4537 #endif
4538 do_wipe_p = 0;
4539 for (i = 0; i < last_free_page; i++) {
4540 page_table[i].large_object = 0;
4541 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4542 int used = page_bytes_used(i);
4543 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4544 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4545 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4551 /* Do a non-conservative GC, and then save a core with the initial
4552 * function being set to the value of the static symbol
4553 * SB!VM:RESTART-LISP-FUNCTION */
4554 void
4555 gc_and_save(char *filename, boolean prepend_runtime,
4556 boolean save_runtime_options, boolean compressed,
4557 int compression_level, int application_type)
4559 FILE *file;
4560 void *runtime_bytes = NULL;
4561 size_t runtime_size;
4563 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4564 &runtime_size);
4565 if (file == NULL)
4566 return;
4568 conservative_stack = 0;
4570 /* The filename might come from Lisp, and be moved by the now
4571 * non-conservative GC. */
4572 filename = strdup(filename);
4574 /* Collect twice: once into relatively high memory, and then back
4575 * into low memory. This compacts the retained data into the lower
4576 * pages, minimizing the size of the core file.
4578 prepare_for_final_gc();
4579 gencgc_alloc_start_page = last_free_page;
4580 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4582 prepare_for_final_gc();
4583 gencgc_alloc_start_page = -1;
4584 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4586 if (prepend_runtime)
4587 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4588 application_type);
4590 /* The dumper doesn't know that pages need to be zeroed before use. */
4591 zero_all_free_pages();
4592 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4593 prepend_runtime, save_runtime_options,
4594 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4595 /* Oops. Save still managed to fail. Since we've mangled the stack
4596 * beyond hope, there's not much we can do.
4597 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4598 * going to be rather unsatisfactory too... */
4599 lose("Attempt to save core after non-conservative GC failed.\n");