Take pointer, not word count, as upper limit in verify_space()
[sbcl.git] / src / runtime / gencgc.c
blob5ebe4d84cabe0918500232c1c84db6e3f74a6326
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 #include "hopscotch.h"
61 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
62 #include "genesis/cons.h"
63 #endif
64 #include "forwarding-ptr.h"
66 /* forward declarations */
67 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
68 int page_type_flag);
72 * GC parameters
75 /* As usually configured, generations 0-5 are normal collected generations,
76 6 is pseudo-static (the objects in which are never moved nor reclaimed),
77 and 7 is scratch space used when collecting a generation without promotion,
78 wherein it is moved to generation 7 and back again.
80 enum {
81 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
82 NUM_GENERATIONS
85 /* Should we use page protection to help avoid the scavenging of pages
86 * that don't have pointers to younger generations? */
87 boolean enable_page_protection = 1;
89 /* Largest allocation seen since last GC. */
90 os_vm_size_t large_allocation = 0;
94 * debugging
97 /* the verbosity level. All non-error messages are disabled at level 0;
98 * and only a few rare messages are printed at level 1. */
99 #if QSHOW == 2
100 boolean gencgc_verbose = 1;
101 #else
102 boolean gencgc_verbose = 0;
103 #endif
105 /* FIXME: At some point enable the various error-checking things below
106 * and see what they say. */
108 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
109 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
110 * check. */
111 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
113 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
114 boolean pre_verify_gen_0 = 0;
116 #ifdef LISP_FEATURE_X86
117 /* Should we check code objects for fixup errors after they are transported? */
118 boolean check_code_fixups = 0;
119 #endif
121 /* Should we check that newly allocated regions are zero filled? */
122 boolean gencgc_zero_check = 0;
124 /* Should we check that the free space is zero filled? */
125 boolean gencgc_enable_verify_zero_fill = 0;
127 /* When loading a core, don't do a full scan of the memory for the
128 * memory region boundaries. (Set to true by coreparse.c if the core
129 * contained a pagetable entry).
131 boolean gencgc_partial_pickup = 0;
133 /* If defined, free pages are read-protected to ensure that nothing
134 * accesses them.
137 /* #define READ_PROTECT_FREE_PAGES */
141 * GC structures and variables
144 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
145 os_vm_size_t bytes_allocated = 0;
146 os_vm_size_t auto_gc_trigger = 0;
148 /* the source and destination generations. These are set before a GC starts
149 * scavenging. */
150 generation_index_t from_space;
151 generation_index_t new_space;
153 /* Set to 1 when in GC */
154 boolean gc_active_p = 0;
156 /* should the GC be conservative on stack. If false (only right before
157 * saving a core), don't scan the stack / mark pages dont_move. */
158 static boolean conservative_stack = 1;
160 /* An array of page structures is allocated on gc initialization.
161 * This helps to quickly map between an address and its page structure.
162 * page_table_pages is set from the size of the dynamic space. */
163 page_index_t page_table_pages;
164 struct page *page_table;
165 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
166 struct hopscotch_table pinned_objects;
167 #endif
169 /* In GC cards that have conservative pointers to them, should we wipe out
170 * dwords in there that are not used, so that they do not act as false
171 * root to other things in the heap from then on? This is a new feature
172 * but in testing it is both reliable and no noticeable slowdown. */
173 int do_wipe_p = 1;
175 /// Constants defined in gc-internal:
176 /// #define BOXED_PAGE_FLAG 1
177 /// #define UNBOXED_PAGE_FLAG 2
178 /// #define OPEN_REGION_PAGE_FLAG 4
180 /// Return true if 'allocated' bits are: {001, 010, 011}, false if 1zz or 000.
181 static inline boolean page_allocated_no_region_p(page_index_t page) {
182 return (page_table[page].allocated ^ OPEN_REGION_PAGE_FLAG) > OPEN_REGION_PAGE_FLAG;
185 static inline boolean page_free_p(page_index_t page) {
186 return (page_table[page].allocated == FREE_PAGE_FLAG);
189 static inline boolean page_boxed_p(page_index_t page) {
190 return (page_table[page].allocated & BOXED_PAGE_FLAG);
193 /// Return true if 'allocated' bits are: {001, 011}, false otherwise.
194 /// i.e. true of pages which could hold boxed or partially boxed objects.
195 static inline boolean page_boxed_no_region_p(page_index_t page) {
196 return (page_table[page].allocated & 5) == BOXED_PAGE_FLAG;
199 /// Return true if page MUST NOT hold boxed objects (including code).
200 static inline boolean page_unboxed_p(page_index_t page) {
201 /* Both flags set == boxed code page */
202 return (page_table[page].allocated & 3) == UNBOXED_PAGE_FLAG;
205 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
206 return (page_boxed_no_region_p(page)
207 && (page_bytes_used(page) != 0)
208 && !page_table[page].dont_move
209 && (page_table[page].gen == generation));
212 /* Calculate the start address for the given page number. */
213 inline void *
214 page_address(page_index_t page_num)
216 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
219 /* Calculate the address where the allocation region associated with
220 * the page starts. */
221 static inline void *
222 page_scan_start(page_index_t page_index)
224 return page_address(page_index)-page_scan_start_offset(page_index);
227 /* True if the page starts a contiguous block. */
228 static inline boolean
229 page_starts_contiguous_block_p(page_index_t page_index)
231 // Don't use the preprocessor macro: 0 means 0.
232 return page_table[page_index].scan_start_offset_ == 0;
235 /* True if the page is the last page in a contiguous block. */
236 static inline boolean
237 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
239 return (/* page doesn't fill block */
240 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
241 /* page is last allocated page */
242 || ((page_index + 1) >= last_free_page)
243 /* next page free */
244 || page_free_p(page_index + 1)
245 /* next page contains no data */
246 || (page_bytes_used(page_index + 1) == 0)
247 /* next page is in different generation */
248 || (page_table[page_index + 1].gen != gen)
249 /* next page starts its own contiguous block */
250 || (page_starts_contiguous_block_p(page_index + 1)));
253 /// External function for calling from Lisp.
254 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
256 static os_vm_size_t
257 npage_bytes(page_index_t npages)
259 gc_assert(npages>=0);
260 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
263 /* Check that X is a higher address than Y and return offset from Y to
264 * X in bytes. */
265 static inline os_vm_size_t
266 void_diff(void *x, void *y)
268 gc_assert(x >= y);
269 return (pointer_sized_uint_t)x - (pointer_sized_uint_t)y;
272 /* a structure to hold the state of a generation
274 * CAUTION: If you modify this, make sure to touch up the alien
275 * definition in src/code/gc.lisp accordingly. ...or better yes,
276 * deal with the FIXME there...
278 struct generation {
280 /* the first page that gc_alloc() checks on its next call */
281 page_index_t alloc_start_page;
283 /* the first page that gc_alloc_unboxed() checks on its next call */
284 page_index_t alloc_unboxed_start_page;
286 /* the first page that gc_alloc_large (boxed) considers on its next
287 * call. (Although it always allocates after the boxed_region.) */
288 page_index_t alloc_large_start_page;
290 /* the first page that gc_alloc_large (unboxed) considers on its
291 * next call. (Although it always allocates after the
292 * current_unboxed_region.) */
293 page_index_t alloc_large_unboxed_start_page;
295 /* the bytes allocated to this generation */
296 os_vm_size_t bytes_allocated;
298 /* the number of bytes at which to trigger a GC */
299 os_vm_size_t gc_trigger;
301 /* to calculate a new level for gc_trigger */
302 os_vm_size_t bytes_consed_between_gc;
304 /* the number of GCs since the last raise */
305 int num_gc;
307 /* the number of GCs to run on the generations before raising objects to the
308 * next generation */
309 int number_of_gcs_before_promotion;
311 /* the cumulative sum of the bytes allocated to this generation. It is
312 * cleared after a GC on this generations, and update before new
313 * objects are added from a GC of a younger generation. Dividing by
314 * the bytes_allocated will give the average age of the memory in
315 * this generation since its last GC. */
316 os_vm_size_t cum_sum_bytes_allocated;
318 /* a minimum average memory age before a GC will occur helps
319 * prevent a GC when a large number of new live objects have been
320 * added, in which case a GC could be a waste of time */
321 double minimum_age_before_gc;
324 /* an array of generation structures. There needs to be one more
325 * generation structure than actual generations as the oldest
326 * generation is temporarily raised then lowered. */
327 struct generation generations[NUM_GENERATIONS];
329 /* the oldest generation that is will currently be GCed by default.
330 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
332 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
334 * Setting this to 0 effectively disables the generational nature of
335 * the GC. In some applications generational GC may not be useful
336 * because there are no long-lived objects.
338 * An intermediate value could be handy after moving long-lived data
339 * into an older generation so an unnecessary GC of this long-lived
340 * data can be avoided. */
341 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
343 /* META: Is nobody aside from me bothered by this especially misleading
344 * use of the word "last"? It could mean either "ultimate" or "prior",
345 * but in fact means neither. It is the *FIRST* page that should be grabbed
346 * for more space, so it is min free page, or 1+ the max used page. */
347 /* The maximum free page in the heap is maintained and used to update
348 * ALLOCATION_POINTER which is used by the room function to limit its
349 * search of the heap. XX Gencgc obviously needs to be better
350 * integrated with the Lisp code. */
352 page_index_t last_free_page;
354 #ifdef LISP_FEATURE_SB_THREAD
355 /* This lock is to prevent multiple threads from simultaneously
356 * allocating new regions which overlap each other. Note that the
357 * majority of GC is single-threaded, but alloc() may be called from
358 * >1 thread at a time and must be thread-safe. This lock must be
359 * seized before all accesses to generations[] or to parts of
360 * page_table[] that other threads may want to see */
361 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
362 /* This lock is used to protect non-thread-local allocation. */
363 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
364 #endif
366 extern os_vm_size_t gencgc_release_granularity;
367 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
369 extern os_vm_size_t gencgc_alloc_granularity;
370 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
374 * miscellaneous heap functions
377 /* Count the number of pages which are write-protected within the
378 * given generation. */
379 static page_index_t
380 count_write_protect_generation_pages(generation_index_t generation)
382 page_index_t i, count = 0;
384 for (i = 0; i < last_free_page; i++)
385 if (!page_free_p(i)
386 && (page_table[i].gen == generation)
387 && (page_table[i].write_protected == 1))
388 count++;
389 return count;
392 /* Count the number of pages within the given generation. */
393 static page_index_t
394 count_generation_pages(generation_index_t generation)
396 page_index_t i;
397 page_index_t count = 0;
399 for (i = 0; i < last_free_page; i++)
400 if (!page_free_p(i)
401 && (page_table[i].gen == generation))
402 count++;
403 return count;
406 #if QSHOW
407 static page_index_t
408 count_dont_move_pages(void)
410 page_index_t i;
411 page_index_t count = 0;
412 for (i = 0; i < last_free_page; i++) {
413 if (!page_free_p(i)
414 && (page_table[i].dont_move != 0)) {
415 ++count;
418 return count;
420 #endif /* QSHOW */
422 /* Work through the pages and add up the number of bytes used for the
423 * given generation. */
424 static __attribute__((unused)) os_vm_size_t
425 count_generation_bytes_allocated (generation_index_t gen)
427 page_index_t i;
428 os_vm_size_t result = 0;
429 for (i = 0; i < last_free_page; i++) {
430 if (!page_free_p(i)
431 && (page_table[i].gen == gen))
432 result += page_bytes_used(i);
434 return result;
437 /* Return the average age of the memory in a generation. */
438 extern double
439 generation_average_age(generation_index_t gen)
441 if (generations[gen].bytes_allocated == 0)
442 return 0.0;
444 return
445 ((double)generations[gen].cum_sum_bytes_allocated)
446 / ((double)generations[gen].bytes_allocated);
449 #ifdef LISP_FEATURE_X86
450 extern void fpu_save(void *);
451 extern void fpu_restore(void *);
452 #endif
454 extern void
455 write_generation_stats(FILE *file)
457 generation_index_t i;
459 #ifdef LISP_FEATURE_X86
460 int fpu_state[27];
462 /* Can end up here after calling alloc_tramp which doesn't prepare
463 * the x87 state, and the C ABI uses a different mode */
464 fpu_save(fpu_state);
465 #endif
467 /* Print the heap stats. */
468 fprintf(file,
469 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
471 for (i = 0; i <= SCRATCH_GENERATION; i++) {
472 page_index_t j;
473 page_index_t boxed_cnt = 0;
474 page_index_t unboxed_cnt = 0;
475 page_index_t large_boxed_cnt = 0;
476 page_index_t large_unboxed_cnt = 0;
477 page_index_t pinned_cnt=0;
479 for (j = 0; j < last_free_page; j++)
480 if (page_table[j].gen == i) {
482 /* Count the number of boxed pages within the given
483 * generation. */
484 if (page_boxed_p(j)) {
485 if (page_table[j].large_object)
486 large_boxed_cnt++;
487 else
488 boxed_cnt++;
490 if(page_table[j].dont_move) pinned_cnt++;
491 /* Count the number of unboxed pages within the given
492 * generation. */
493 if (page_unboxed_p(j)) {
494 if (page_table[j].large_object)
495 large_unboxed_cnt++;
496 else
497 unboxed_cnt++;
501 gc_assert(generations[i].bytes_allocated
502 == count_generation_bytes_allocated(i));
503 fprintf(file,
504 " %1d: %5ld %5ld %5ld %5ld",
506 (long)generations[i].alloc_start_page,
507 (long)generations[i].alloc_unboxed_start_page,
508 (long)generations[i].alloc_large_start_page,
509 (long)generations[i].alloc_large_unboxed_start_page);
510 fprintf(file,
511 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
512 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
513 boxed_cnt, unboxed_cnt, large_boxed_cnt,
514 large_unboxed_cnt, pinned_cnt);
515 fprintf(file,
516 " %8"OS_VM_SIZE_FMT
517 " %5"OS_VM_SIZE_FMT
518 " %8"OS_VM_SIZE_FMT
519 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
520 generations[i].bytes_allocated,
521 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
522 generations[i].gc_trigger,
523 count_write_protect_generation_pages(i),
524 generations[i].num_gc,
525 generation_average_age(i));
527 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
528 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
530 #ifdef LISP_FEATURE_X86
531 fpu_restore(fpu_state);
532 #endif
535 extern void
536 write_heap_exhaustion_report(FILE *file, long available, long requested,
537 struct thread *thread)
539 fprintf(file,
540 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
541 gc_active_p ? "garbage collection" : "allocation",
542 available,
543 requested);
544 write_generation_stats(file);
545 fprintf(file, "GC control variables:\n");
546 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
547 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
548 (SymbolValue(GC_PENDING, thread) == T) ?
549 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
550 "false" : "in progress"));
551 #ifdef LISP_FEATURE_SB_THREAD
552 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
553 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
554 #endif
557 extern void
558 print_generation_stats(void)
560 write_generation_stats(stderr);
563 extern char* gc_logfile;
564 char * gc_logfile = NULL;
566 extern void
567 log_generation_stats(char *logfile, char *header)
569 if (logfile) {
570 FILE * log = fopen(logfile, "a");
571 if (log) {
572 fprintf(log, "%s\n", header);
573 write_generation_stats(log);
574 fclose(log);
575 } else {
576 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
577 fflush(stderr);
582 extern void
583 report_heap_exhaustion(long available, long requested, struct thread *th)
585 if (gc_logfile) {
586 FILE * log = fopen(gc_logfile, "a");
587 if (log) {
588 write_heap_exhaustion_report(log, available, requested, th);
589 fclose(log);
590 } else {
591 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
592 fflush(stderr);
595 /* Always to stderr as well. */
596 write_heap_exhaustion_report(stderr, available, requested, th);
600 #if defined(LISP_FEATURE_X86)
601 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
602 #endif
604 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
605 * if zeroing it ourselves, i.e. in practice give the memory back to the
606 * OS. Generally done after a large GC.
608 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
609 page_index_t i;
610 void *addr = page_address(start), *new_addr;
611 os_vm_size_t length = npage_bytes(1+end-start);
613 if (start > end)
614 return;
616 gc_assert(length >= gencgc_release_granularity);
617 gc_assert((length % gencgc_release_granularity) == 0);
619 #ifdef LISP_FEATURE_LINUX
620 extern os_vm_address_t anon_dynamic_space_start;
621 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
622 // Linux treats it as a demand that the memory be 0-filled, or refreshed
623 // from a file that backs the range. BSD takes it as a hint that you don't
624 // care if the memory has to brought in from swap when next accessed,
625 // i.e. it's not a request to make a user-visible alteration to memory.
626 // So in theory this can bring a page in from the core file, if we happen
627 // to hit a page that resides in the portion of memory mapped by coreparse.
628 // In practice this should not happen because objects from a core file can't
629 // become garbage. Except in save-lisp-and-die they can, and we must be
630 // cautious not to resurrect bytes that originally came from the file.
631 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
632 if (madvise(addr, length, MADV_DONTNEED) != 0)
633 lose("madvise failed\n");
634 } else
635 #endif
637 os_invalidate(addr, length);
638 new_addr = os_validate(addr, length);
639 if (new_addr == NULL || new_addr != addr) {
640 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
641 start, new_addr);
645 for (i = start; i <= end; i++)
646 set_page_need_to_zero(i, 0);
649 /* Zero the pages from START to END (inclusive). Generally done just after
650 * a new region has been allocated.
652 static void
653 zero_pages(page_index_t start, page_index_t end) {
654 if (start > end)
655 return;
657 #if defined(LISP_FEATURE_X86)
658 fast_bzero(page_address(start), npage_bytes(1+end-start));
659 #else
660 bzero(page_address(start), npage_bytes(1+end-start));
661 #endif
665 static void
666 zero_and_mark_pages(page_index_t start, page_index_t end) {
667 page_index_t i;
669 zero_pages(start, end);
670 for (i = start; i <= end; i++)
671 set_page_need_to_zero(i, 0);
674 /* Zero the pages from START to END (inclusive), except for those
675 * pages that are known to already zeroed. Mark all pages in the
676 * ranges as non-zeroed.
678 static void
679 zero_dirty_pages(page_index_t start, page_index_t end) {
680 page_index_t i, j;
682 for (i = start; i <= end; i++) {
683 if (!page_need_to_zero(i)) continue;
684 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
685 ; /* empty body */
686 zero_pages(i, j-1);
687 i = j;
690 for (i = start; i <= end; i++) {
691 set_page_need_to_zero(i, 1);
697 * To support quick and inline allocation, regions of memory can be
698 * allocated and then allocated from with just a free pointer and a
699 * check against an end address.
701 * Since objects can be allocated to spaces with different properties
702 * e.g. boxed/unboxed, generation, ages; there may need to be many
703 * allocation regions.
705 * Each allocation region may start within a partly used page. Many
706 * features of memory use are noted on a page wise basis, e.g. the
707 * generation; so if a region starts within an existing allocated page
708 * it must be consistent with this page.
710 * During the scavenging of the newspace, objects will be transported
711 * into an allocation region, and pointers updated to point to this
712 * allocation region. It is possible that these pointers will be
713 * scavenged again before the allocation region is closed, e.g. due to
714 * trans_list which jumps all over the place to cleanup the list. It
715 * is important to be able to determine properties of all objects
716 * pointed to when scavenging, e.g to detect pointers to the oldspace.
717 * Thus it's important that the allocation regions have the correct
718 * properties set when allocated, and not just set when closed. The
719 * region allocation routines return regions with the specified
720 * properties, and grab all the pages, setting their properties
721 * appropriately, except that the amount used is not known.
723 * These regions are used to support quicker allocation using just a
724 * free pointer. The actual space used by the region is not reflected
725 * in the pages tables until it is closed. It can't be scavenged until
726 * closed.
728 * When finished with the region it should be closed, which will
729 * update the page tables for the actual space used returning unused
730 * space. Further it may be noted in the new regions which is
731 * necessary when scavenging the newspace.
733 * Large objects may be allocated directly without an allocation
734 * region, the page tables are updated immediately.
736 * Unboxed objects don't contain pointers to other objects and so
737 * don't need scavenging. Further they can't contain pointers to
738 * younger generations so WP is not needed. By allocating pages to
739 * unboxed objects the whole page never needs scavenging or
740 * write-protecting. */
742 /* We are only using two regions at present. Both are for the current
743 * newspace generation. */
744 struct alloc_region boxed_region;
745 struct alloc_region unboxed_region;
747 /* The generation currently being allocated to. */
748 static generation_index_t gc_alloc_generation;
750 static inline page_index_t
751 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
753 if (large) {
754 if (UNBOXED_PAGE_FLAG == page_type_flag) {
755 return generations[generation].alloc_large_unboxed_start_page;
756 } else if (BOXED_PAGE_FLAG & page_type_flag) {
757 /* Both code and data. */
758 return generations[generation].alloc_large_start_page;
759 } else {
760 lose("bad page type flag: %d", page_type_flag);
762 } else {
763 if (UNBOXED_PAGE_FLAG == page_type_flag) {
764 return generations[generation].alloc_unboxed_start_page;
765 } else if (BOXED_PAGE_FLAG & page_type_flag) {
766 /* Both code and data. */
767 return generations[generation].alloc_start_page;
768 } else {
769 lose("bad page_type_flag: %d", page_type_flag);
774 static inline void
775 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
776 page_index_t page)
778 if (large) {
779 if (UNBOXED_PAGE_FLAG == page_type_flag) {
780 generations[generation].alloc_large_unboxed_start_page = page;
781 } else if (BOXED_PAGE_FLAG & page_type_flag) {
782 /* Both code and data. */
783 generations[generation].alloc_large_start_page = page;
784 } else {
785 lose("bad page type flag: %d", page_type_flag);
787 } else {
788 if (UNBOXED_PAGE_FLAG == page_type_flag) {
789 generations[generation].alloc_unboxed_start_page = page;
790 } else if (BOXED_PAGE_FLAG & page_type_flag) {
791 /* Both code and data. */
792 generations[generation].alloc_start_page = page;
793 } else {
794 lose("bad page type flag: %d", page_type_flag);
799 /* Find a new region with room for at least the given number of bytes.
801 * It starts looking at the current generation's alloc_start_page. So
802 * may pick up from the previous region if there is enough space. This
803 * keeps the allocation contiguous when scavenging the newspace.
805 * The alloc_region should have been closed by a call to
806 * gc_alloc_update_page_tables(), and will thus be in an empty state.
808 * To assist the scavenging functions write-protected pages are not
809 * used. Free pages should not be write-protected.
811 * It is critical to the conservative GC that the start of regions be
812 * known. To help achieve this only small regions are allocated at a
813 * time.
815 * During scavenging, pointers may be found to within the current
816 * region and the page generation must be set so that pointers to the
817 * from space can be recognized. Therefore the generation of pages in
818 * the region are set to gc_alloc_generation. To prevent another
819 * allocation call using the same pages, all the pages in the region
820 * are allocated, although they will initially be empty.
822 static void
823 gc_alloc_new_region(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
825 page_index_t first_page;
826 page_index_t last_page;
827 os_vm_size_t bytes_found;
828 page_index_t i;
829 int ret;
832 FSHOW((stderr,
833 "/alloc_new_region for %d bytes from gen %d\n",
834 nbytes, gc_alloc_generation));
837 /* Check that the region is in a reset state. */
838 gc_assert((alloc_region->first_page == 0)
839 && (alloc_region->last_page == -1)
840 && (alloc_region->free_pointer == alloc_region->end_addr));
841 ret = thread_mutex_lock(&free_pages_lock);
842 gc_assert(ret == 0);
843 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
844 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
845 bytes_found=(GENCGC_CARD_BYTES - page_bytes_used(first_page))
846 + npage_bytes(last_page-first_page);
848 /* Set up the alloc_region. */
849 alloc_region->first_page = first_page;
850 alloc_region->last_page = last_page;
851 alloc_region->start_addr = page_bytes_used(first_page)
852 + page_address(first_page);
853 alloc_region->free_pointer = alloc_region->start_addr;
854 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
856 /* Set up the pages. */
858 /* The first page may have already been in use. */
859 if (page_bytes_used(first_page) == 0) {
860 page_table[first_page].allocated = page_type_flag;
861 page_table[first_page].gen = gc_alloc_generation;
862 page_table[first_page].large_object = 0;
863 set_page_scan_start_offset(first_page, 0);
866 gc_assert(page_table[first_page].allocated == page_type_flag);
867 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
869 gc_assert(page_table[first_page].gen == gc_alloc_generation);
870 gc_assert(page_table[first_page].large_object == 0);
872 for (i = first_page+1; i <= last_page; i++) {
873 page_table[i].allocated = page_type_flag;
874 page_table[i].gen = gc_alloc_generation;
875 page_table[i].large_object = 0;
876 /* This may not be necessary for unboxed regions (think it was
877 * broken before!) */
878 set_page_scan_start_offset(i,
879 void_diff(page_address(i), alloc_region->start_addr));
880 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
882 /* Bump up last_free_page. */
883 if (last_page+1 > last_free_page) {
884 last_free_page = last_page+1;
885 /* do we only want to call this on special occasions? like for
886 * boxed_region? */
887 set_alloc_pointer((lispobj)page_address(last_free_page));
889 ret = thread_mutex_unlock(&free_pages_lock);
890 gc_assert(ret == 0);
892 #ifdef READ_PROTECT_FREE_PAGES
893 os_protect(page_address(first_page),
894 npage_bytes(1+last_page-first_page),
895 OS_VM_PROT_ALL);
896 #endif
898 /* If the first page was only partial, don't check whether it's
899 * zeroed (it won't be) and don't zero it (since the parts that
900 * we're interested in are guaranteed to be zeroed).
902 if (page_bytes_used(first_page)) {
903 first_page++;
906 zero_dirty_pages(first_page, last_page);
908 /* we can do this after releasing free_pages_lock */
909 if (gencgc_zero_check) {
910 word_t *p;
911 for (p = (word_t *)alloc_region->start_addr;
912 p < (word_t *)alloc_region->end_addr; p++) {
913 if (*p != 0) {
914 lose("The new region is not zero at %p (start=%p, end=%p).\n",
915 p, alloc_region->start_addr, alloc_region->end_addr);
921 /* If the record_new_objects flag is 2 then all new regions created
922 * are recorded.
924 * If it's 1 then then it is only recorded if the first page of the
925 * current region is <= new_areas_ignore_page. This helps avoid
926 * unnecessary recording when doing full scavenge pass.
928 * The new_object structure holds the page, byte offset, and size of
929 * new regions of objects. Each new area is placed in the array of
930 * these structures pointer to by new_areas. new_areas_index holds the
931 * offset into new_areas.
933 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
934 * later code must detect this and handle it, probably by doing a full
935 * scavenge of a generation. */
936 #define NUM_NEW_AREAS 512
937 static int record_new_objects = 0;
938 static page_index_t new_areas_ignore_page;
939 struct new_area {
940 page_index_t page;
941 size_t offset;
942 size_t size;
944 static struct new_area (*new_areas)[];
945 static size_t new_areas_index;
946 size_t max_new_areas;
948 /* Add a new area to new_areas. */
949 static void
950 add_new_area(page_index_t first_page, size_t offset, size_t size)
952 size_t new_area_start, c;
953 ssize_t i;
955 /* Ignore if full. */
956 if (new_areas_index >= NUM_NEW_AREAS)
957 return;
959 switch (record_new_objects) {
960 case 0:
961 return;
962 case 1:
963 if (first_page > new_areas_ignore_page)
964 return;
965 break;
966 case 2:
967 break;
968 default:
969 gc_abort();
972 new_area_start = npage_bytes(first_page) + offset;
974 /* Search backwards for a prior area that this follows from. If
975 found this will save adding a new area. */
976 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
977 size_t area_end =
978 npage_bytes((*new_areas)[i].page)
979 + (*new_areas)[i].offset
980 + (*new_areas)[i].size;
981 /*FSHOW((stderr,
982 "/add_new_area S1 %d %d %d %d\n",
983 i, c, new_area_start, area_end));*/
984 if (new_area_start == area_end) {
985 /*FSHOW((stderr,
986 "/adding to [%d] %d %d %d with %d %d %d:\n",
988 (*new_areas)[i].page,
989 (*new_areas)[i].offset,
990 (*new_areas)[i].size,
991 first_page,
992 offset,
993 size);*/
994 (*new_areas)[i].size += size;
995 return;
999 (*new_areas)[new_areas_index].page = first_page;
1000 (*new_areas)[new_areas_index].offset = offset;
1001 (*new_areas)[new_areas_index].size = size;
1002 /*FSHOW((stderr,
1003 "/new_area %d page %d offset %d size %d\n",
1004 new_areas_index, first_page, offset, size));*/
1005 new_areas_index++;
1007 /* Note the max new_areas used. */
1008 if (new_areas_index > max_new_areas)
1009 max_new_areas = new_areas_index;
1012 /* Update the tables for the alloc_region. The region may be added to
1013 * the new_areas.
1015 * When done the alloc_region is set up so that the next quick alloc
1016 * will fail safely and thus a new region will be allocated. Further
1017 * it is safe to try to re-update the page table of this reset
1018 * alloc_region. */
1019 void
1020 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1022 boolean more;
1023 page_index_t first_page;
1024 page_index_t next_page;
1025 os_vm_size_t bytes_used;
1026 os_vm_size_t region_size;
1027 os_vm_size_t byte_cnt;
1028 page_bytes_t orig_first_page_bytes_used;
1029 int ret;
1032 first_page = alloc_region->first_page;
1034 /* Catch an unused alloc_region. */
1035 if ((first_page == 0) && (alloc_region->last_page == -1))
1036 return;
1038 next_page = first_page+1;
1040 ret = thread_mutex_lock(&free_pages_lock);
1041 gc_assert(ret == 0);
1042 if (alloc_region->free_pointer != alloc_region->start_addr) {
1043 /* some bytes were allocated in the region */
1044 orig_first_page_bytes_used = page_bytes_used(first_page);
1046 gc_assert(alloc_region->start_addr ==
1047 (page_address(first_page) + page_bytes_used(first_page)));
1049 /* All the pages used need to be updated */
1051 /* Update the first page. */
1053 /* If the page was free then set up the gen, and
1054 * scan_start_offset. */
1055 if (page_bytes_used(first_page) == 0)
1056 gc_assert(page_starts_contiguous_block_p(first_page));
1057 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1059 gc_assert(page_table[first_page].allocated & page_type_flag);
1060 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1061 gc_assert(page_table[first_page].large_object == 0);
1063 byte_cnt = 0;
1065 /* Calculate the number of bytes used in this page. This is not
1066 * always the number of new bytes, unless it was free. */
1067 more = 0;
1068 if ((bytes_used = void_diff(alloc_region->free_pointer,
1069 page_address(first_page)))
1070 >GENCGC_CARD_BYTES) {
1071 bytes_used = GENCGC_CARD_BYTES;
1072 more = 1;
1074 set_page_bytes_used(first_page, bytes_used);
1075 byte_cnt += bytes_used;
1078 /* All the rest of the pages should be free. We need to set
1079 * their scan_start_offset pointer to the start of the
1080 * region, and set the bytes_used. */
1081 while (more) {
1082 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1083 gc_assert(page_table[next_page].allocated & page_type_flag);
1084 gc_assert(page_bytes_used(next_page) == 0);
1085 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1086 gc_assert(page_table[next_page].large_object == 0);
1087 gc_assert(page_scan_start_offset(next_page) ==
1088 void_diff(page_address(next_page),
1089 alloc_region->start_addr));
1091 /* Calculate the number of bytes used in this page. */
1092 more = 0;
1093 if ((bytes_used = void_diff(alloc_region->free_pointer,
1094 page_address(next_page)))>GENCGC_CARD_BYTES) {
1095 bytes_used = GENCGC_CARD_BYTES;
1096 more = 1;
1098 set_page_bytes_used(next_page, bytes_used);
1099 byte_cnt += bytes_used;
1101 next_page++;
1104 region_size = void_diff(alloc_region->free_pointer,
1105 alloc_region->start_addr);
1106 bytes_allocated += region_size;
1107 generations[gc_alloc_generation].bytes_allocated += region_size;
1109 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1111 /* Set the generations alloc restart page to the last page of
1112 * the region. */
1113 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1115 /* Add the region to the new_areas if requested. */
1116 if (BOXED_PAGE_FLAG & page_type_flag)
1117 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1120 FSHOW((stderr,
1121 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1122 region_size,
1123 gc_alloc_generation));
1125 } else {
1126 /* There are no bytes allocated. Unallocate the first_page if
1127 * there are 0 bytes_used. */
1128 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1129 if (page_bytes_used(first_page) == 0)
1130 page_table[first_page].allocated = FREE_PAGE_FLAG;
1133 /* Unallocate any unused pages. */
1134 while (next_page <= alloc_region->last_page) {
1135 gc_assert(page_bytes_used(next_page) == 0);
1136 page_table[next_page].allocated = FREE_PAGE_FLAG;
1137 next_page++;
1139 ret = thread_mutex_unlock(&free_pages_lock);
1140 gc_assert(ret == 0);
1142 /* alloc_region is per-thread, we're ok to do this unlocked */
1143 gc_set_region_empty(alloc_region);
1146 /* Allocate a possibly large object. */
1147 void *
1148 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1150 boolean more;
1151 page_index_t first_page, next_page, last_page;
1152 page_bytes_t orig_first_page_bytes_used;
1153 os_vm_size_t byte_cnt;
1154 os_vm_size_t bytes_used;
1155 int ret;
1157 ret = thread_mutex_lock(&free_pages_lock);
1158 gc_assert(ret == 0);
1160 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1161 if (first_page <= alloc_region->last_page) {
1162 first_page = alloc_region->last_page+1;
1165 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1167 gc_assert(first_page > alloc_region->last_page);
1169 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1171 /* Set up the pages. */
1172 orig_first_page_bytes_used = page_bytes_used(first_page);
1174 /* If the first page was free then set up the gen, and
1175 * scan_start_offset. */
1176 if (page_bytes_used(first_page) == 0) {
1177 page_table[first_page].allocated = page_type_flag;
1178 page_table[first_page].gen = gc_alloc_generation;
1179 set_page_scan_start_offset(first_page, 0);
1180 page_table[first_page].large_object = 1;
1183 gc_assert(page_table[first_page].allocated == page_type_flag);
1184 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1185 gc_assert(page_table[first_page].large_object == 1);
1187 byte_cnt = 0;
1189 /* Calc. the number of bytes used in this page. This is not
1190 * always the number of new bytes, unless it was free. */
1191 more = 0;
1192 if ((bytes_used = nbytes+orig_first_page_bytes_used) > GENCGC_CARD_BYTES) {
1193 bytes_used = GENCGC_CARD_BYTES;
1194 more = 1;
1196 set_page_bytes_used(first_page, bytes_used);
1197 byte_cnt += bytes_used;
1199 next_page = first_page+1;
1201 /* All the rest of the pages should be free. We need to set their
1202 * scan_start_offset pointer to the start of the region, and set
1203 * the bytes_used. */
1204 while (more) {
1205 gc_assert(page_free_p(next_page));
1206 gc_assert(page_bytes_used(next_page) == 0);
1207 page_table[next_page].allocated = page_type_flag;
1208 page_table[next_page].gen = gc_alloc_generation;
1209 page_table[next_page].large_object = 1;
1211 set_page_scan_start_offset(next_page,
1212 npage_bytes(next_page-first_page) - orig_first_page_bytes_used);
1214 /* Calculate the number of bytes used in this page. */
1215 more = 0;
1216 bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt;
1217 if (bytes_used > GENCGC_CARD_BYTES) {
1218 bytes_used = GENCGC_CARD_BYTES;
1219 more = 1;
1221 set_page_bytes_used(next_page, bytes_used);
1222 page_table[next_page].write_protected=0;
1223 page_table[next_page].dont_move=0;
1224 byte_cnt += bytes_used;
1225 next_page++;
1228 gc_assert((byte_cnt-orig_first_page_bytes_used) == (size_t)nbytes);
1230 bytes_allocated += nbytes;
1231 generations[gc_alloc_generation].bytes_allocated += nbytes;
1233 /* Add the region to the new_areas if requested. */
1234 if (BOXED_PAGE_FLAG & page_type_flag)
1235 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
1237 /* Bump up last_free_page */
1238 if (last_page+1 > last_free_page) {
1239 last_free_page = last_page+1;
1240 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1242 ret = thread_mutex_unlock(&free_pages_lock);
1243 gc_assert(ret == 0);
1245 #ifdef READ_PROTECT_FREE_PAGES
1246 os_protect(page_address(first_page),
1247 npage_bytes(1+last_page-first_page),
1248 OS_VM_PROT_ALL);
1249 #endif
1251 zero_dirty_pages(first_page, last_page);
1253 return page_address(first_page);
1256 static page_index_t gencgc_alloc_start_page = -1;
1258 void
1259 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1261 struct thread *thread = arch_os_get_current_thread();
1262 /* Write basic information before doing anything else: if we don't
1263 * call to lisp this is a must, and even if we do there is always
1264 * the danger that we bounce back here before the error has been
1265 * handled, or indeed even printed.
1267 report_heap_exhaustion(available, requested, thread);
1268 if (gc_active_p || (available == 0)) {
1269 /* If we are in GC, or totally out of memory there is no way
1270 * to sanely transfer control to the lisp-side of things.
1272 lose("Heap exhausted, game over.");
1274 else {
1275 /* FIXME: assert free_pages_lock held */
1276 (void)thread_mutex_unlock(&free_pages_lock);
1277 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1278 gc_assert(get_pseudo_atomic_atomic(thread));
1279 clear_pseudo_atomic_atomic(thread);
1280 if (get_pseudo_atomic_interrupted(thread))
1281 do_pending_interrupt();
1282 #endif
1283 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1284 * to running user code at arbitrary places, even in a
1285 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1286 * running out of the heap. So at this point all bets are
1287 * off. */
1288 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1289 corruption_warning_and_maybe_lose
1290 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1291 /* available and requested should be double word aligned, thus
1292 they can passed as fixnums and shifted later. */
1293 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1294 lose("HEAP-EXHAUSTED-ERROR fell through");
1298 page_index_t
1299 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1300 int page_type_flag)
1302 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1303 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1304 os_vm_size_t nbytes = bytes;
1305 os_vm_size_t nbytes_goal = nbytes;
1306 os_vm_size_t bytes_found = 0;
1307 os_vm_size_t most_bytes_found = 0;
1308 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1309 /* FIXME: assert(free_pages_lock is held); */
1311 if (nbytes_goal < gencgc_alloc_granularity)
1312 nbytes_goal = gencgc_alloc_granularity;
1314 /* Toggled by gc_and_save for heap compaction, normally -1. */
1315 if (gencgc_alloc_start_page != -1) {
1316 restart_page = gencgc_alloc_start_page;
1319 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1320 * long from the interface. */
1321 gc_assert(bytes>=0);
1322 /* Search for a page with at least nbytes of space. We prefer
1323 * not to split small objects on multiple pages, to reduce the
1324 * number of contiguous allocation regions spaning multiple
1325 * pages: this helps avoid excessive conservativism.
1327 * For other objects, we guarantee that they start on their own
1328 * page boundary.
1330 first_page = restart_page;
1331 while (first_page < page_table_pages) {
1332 bytes_found = 0;
1333 if (page_free_p(first_page)) {
1334 gc_assert(0 == page_bytes_used(first_page));
1335 bytes_found = GENCGC_CARD_BYTES;
1336 } else if (small_object &&
1337 (page_table[first_page].allocated == page_type_flag) &&
1338 (page_table[first_page].large_object == 0) &&
1339 (page_table[first_page].gen == gc_alloc_generation) &&
1340 (page_table[first_page].write_protected == 0) &&
1341 (page_table[first_page].dont_move == 0)) {
1342 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1343 if (bytes_found < nbytes) {
1344 if (bytes_found > most_bytes_found)
1345 most_bytes_found = bytes_found;
1346 first_page++;
1347 continue;
1349 } else {
1350 first_page++;
1351 continue;
1354 gc_assert(page_table[first_page].write_protected == 0);
1355 for (last_page = first_page+1;
1356 ((last_page < page_table_pages) &&
1357 page_free_p(last_page) &&
1358 (bytes_found < nbytes_goal));
1359 last_page++) {
1360 bytes_found += GENCGC_CARD_BYTES;
1361 gc_assert(0 == page_bytes_used(last_page));
1362 gc_assert(0 == page_table[last_page].write_protected);
1365 if (bytes_found > most_bytes_found) {
1366 most_bytes_found = bytes_found;
1367 most_bytes_found_from = first_page;
1368 most_bytes_found_to = last_page;
1370 if (bytes_found >= nbytes_goal)
1371 break;
1373 first_page = last_page;
1376 bytes_found = most_bytes_found;
1377 restart_page = first_page + 1;
1379 /* Check for a failure */
1380 if (bytes_found < nbytes) {
1381 gc_assert(restart_page >= page_table_pages);
1382 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1385 gc_assert(most_bytes_found_to);
1386 *restart_page_ptr = most_bytes_found_from;
1387 return most_bytes_found_to-1;
1390 /* Allocate bytes. All the rest of the special-purpose allocation
1391 * functions will eventually call this */
1393 void *
1394 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1395 int quick_p)
1397 void *new_free_pointer;
1399 if (nbytes>=LARGE_OBJECT_SIZE)
1400 return gc_alloc_large(nbytes, page_type_flag, my_region);
1402 /* Check whether there is room in the current alloc region. */
1403 new_free_pointer = my_region->free_pointer + nbytes;
1405 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1406 my_region->free_pointer, new_free_pointer); */
1408 if (new_free_pointer <= my_region->end_addr) {
1409 /* If so then allocate from the current alloc region. */
1410 void *new_obj = my_region->free_pointer;
1411 my_region->free_pointer = new_free_pointer;
1413 /* Unless a `quick' alloc was requested, check whether the
1414 alloc region is almost empty. */
1415 if (!quick_p &&
1416 void_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1417 /* If so, finished with the current region. */
1418 gc_alloc_update_page_tables(page_type_flag, my_region);
1419 /* Set up a new region. */
1420 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1423 return((void *)new_obj);
1426 /* Else not enough free space in the current region: retry with a
1427 * new region. */
1429 gc_alloc_update_page_tables(page_type_flag, my_region);
1430 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1431 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1434 /* Copy a large object. If the object is in a large object region then
1435 * it is simply promoted, else it is copied. If it's large enough then
1436 * it's copied to a large object region.
1438 * Bignums and vectors may have shrunk. If the object is not copied
1439 * the space needs to be reclaimed, and the page_tables corrected. */
1440 static lispobj
1441 general_copy_large_object(lispobj object, word_t nwords, boolean boxedp)
1443 lispobj *new;
1444 page_index_t first_page;
1446 CHECK_COPY_PRECONDITIONS(object, nwords);
1448 if ((nwords > 1024*1024) && gencgc_verbose) {
1449 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1450 nwords*N_WORD_BYTES));
1453 /* Check whether it's a large object. */
1454 first_page = find_page_index((void *)object);
1455 gc_assert(first_page >= 0);
1457 if (page_table[first_page].large_object) {
1458 /* Promote the object. Note: Unboxed objects may have been
1459 * allocated to a BOXED region so it may be necessary to
1460 * change the region to UNBOXED. */
1461 os_vm_size_t remaining_bytes;
1462 os_vm_size_t bytes_freed;
1463 page_index_t next_page;
1464 page_bytes_t old_bytes_used;
1466 /* FIXME: This comment is somewhat stale.
1468 * Note: Any page write-protection must be removed, else a
1469 * later scavenge_newspace may incorrectly not scavenge these
1470 * pages. This would not be necessary if they are added to the
1471 * new areas, but let's do it for them all (they'll probably
1472 * be written anyway?). */
1474 gc_assert(page_starts_contiguous_block_p(first_page));
1475 next_page = first_page;
1476 remaining_bytes = nwords*N_WORD_BYTES;
1478 while (remaining_bytes > GENCGC_CARD_BYTES) {
1479 gc_assert(page_table[next_page].gen == from_space);
1480 gc_assert(page_table[next_page].large_object);
1481 gc_assert(page_scan_start_offset(next_page) ==
1482 npage_bytes(next_page-first_page));
1483 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1484 /* Should have been unprotected by unprotect_oldspace()
1485 * for boxed objects, and after promotion unboxed ones
1486 * should not be on protected pages at all. */
1487 gc_assert(!page_table[next_page].write_protected);
1489 if (boxedp)
1490 gc_assert(page_boxed_p(next_page));
1491 else {
1492 gc_assert(page_allocated_no_region_p(next_page));
1493 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1495 page_table[next_page].gen = new_space;
1497 remaining_bytes -= GENCGC_CARD_BYTES;
1498 next_page++;
1501 /* Now only one page remains, but the object may have shrunk so
1502 * there may be more unused pages which will be freed. */
1504 /* Object may have shrunk but shouldn't have grown - check. */
1505 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1507 page_table[next_page].gen = new_space;
1509 if (boxedp)
1510 gc_assert(page_boxed_p(next_page));
1511 else
1512 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1514 /* Adjust the bytes_used. */
1515 old_bytes_used = page_bytes_used(next_page);
1516 set_page_bytes_used(next_page, remaining_bytes);
1518 bytes_freed = old_bytes_used - remaining_bytes;
1520 /* Free any remaining pages; needs care. */
1521 next_page++;
1522 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1523 (page_table[next_page].gen == from_space) &&
1524 /* FIXME: It is not obvious to me why this is necessary
1525 * as a loop condition: it seems to me that the
1526 * scan_start_offset test should be sufficient, but
1527 * experimentally that is not the case. --NS
1528 * 2011-11-28 */
1529 (boxedp ?
1530 page_boxed_p(next_page) :
1531 page_allocated_no_region_p(next_page)) &&
1532 page_table[next_page].large_object &&
1533 (page_scan_start_offset(next_page) ==
1534 npage_bytes(next_page - first_page))) {
1535 /* Checks out OK, free the page. Don't need to both zeroing
1536 * pages as this should have been done before shrinking the
1537 * object. These pages shouldn't be write-protected, even if
1538 * boxed they should be zero filled. */
1539 gc_assert(page_table[next_page].write_protected == 0);
1541 old_bytes_used = page_bytes_used(next_page);
1542 page_table[next_page].allocated = FREE_PAGE_FLAG;
1543 set_page_bytes_used(next_page, 0);
1544 bytes_freed += old_bytes_used;
1545 next_page++;
1548 if ((bytes_freed > 0) && gencgc_verbose) {
1549 FSHOW((stderr,
1550 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1551 bytes_freed));
1554 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1555 + bytes_freed;
1556 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1557 bytes_allocated -= bytes_freed;
1559 /* Add the region to the new_areas if requested. */
1560 if (boxedp)
1561 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1563 return(object);
1565 } else {
1566 /* Allocate space. */
1567 new = gc_general_alloc(nwords*N_WORD_BYTES,
1568 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1569 ALLOC_QUICK);
1571 /* Copy the object. */
1572 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1574 /* Return Lisp pointer of new object. */
1575 return make_lispobj(new, lowtag_of(object));
1579 lispobj
1580 copy_large_object(lispobj object, sword_t nwords)
1582 return general_copy_large_object(object, nwords, 1);
1585 lispobj
1586 copy_large_unboxed_object(lispobj object, sword_t nwords)
1588 return general_copy_large_object(object, nwords, 0);
1591 /* to copy unboxed objects */
1592 lispobj
1593 copy_unboxed_object(lispobj object, sword_t nwords)
1595 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1600 * code and code-related objects
1603 static lispobj trans_fun_header(lispobj object);
1604 static lispobj trans_boxed(lispobj object);
1607 /* Scan a x86 compiled code object, looking for possible fixups that
1608 * have been missed after a move.
1610 * Two types of fixups are needed:
1611 * 1. Absolute fixups to within the code object.
1612 * 2. Relative fixups to outside the code object.
1614 * Currently only absolute fixups to the constant vector, or to the
1615 * code area are checked. */
1616 #ifdef LISP_FEATURE_X86
1617 void
1618 sniff_code_object(struct code *code, os_vm_size_t displacement)
1620 sword_t nheader_words, ncode_words, nwords;
1621 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1622 os_vm_address_t code_start_addr, code_end_addr;
1623 os_vm_address_t code_addr = (os_vm_address_t)code;
1624 int fixup_found = 0;
1626 if (!check_code_fixups)
1627 return;
1629 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1631 ncode_words = code_instruction_words(code->code_size);
1632 nheader_words = code_header_words(*(lispobj *)code);
1633 nwords = ncode_words + nheader_words;
1635 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1636 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1637 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1638 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1640 /* Work through the unboxed code. */
1641 for (p = code_start_addr; p < code_end_addr; p++) {
1642 void *data = *(void **)p;
1643 unsigned d1 = *((unsigned char *)p - 1);
1644 unsigned d2 = *((unsigned char *)p - 2);
1645 unsigned d3 = *((unsigned char *)p - 3);
1646 unsigned d4 = *((unsigned char *)p - 4);
1647 #if QSHOW
1648 unsigned d5 = *((unsigned char *)p - 5);
1649 unsigned d6 = *((unsigned char *)p - 6);
1650 #endif
1652 /* Check for code references. */
1653 /* Check for a 32 bit word that looks like an absolute
1654 reference to within the code adea of the code object. */
1655 if ((data >= (void*)(code_start_addr-displacement))
1656 && (data < (void*)(code_end_addr-displacement))) {
1657 /* function header */
1658 if ((d4 == 0x5e)
1659 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1660 (unsigned)code)) {
1661 /* Skip the function header */
1662 p += 6*4 - 4 - 1;
1663 continue;
1665 /* the case of PUSH imm32 */
1666 if (d1 == 0x68) {
1667 fixup_found = 1;
1668 FSHOW((stderr,
1669 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1670 p, d6, d5, d4, d3, d2, d1, data));
1671 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1673 /* the case of MOV [reg-8],imm32 */
1674 if ((d3 == 0xc7)
1675 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1676 || d2==0x45 || d2==0x46 || d2==0x47)
1677 && (d1 == 0xf8)) {
1678 fixup_found = 1;
1679 FSHOW((stderr,
1680 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1681 p, d6, d5, d4, d3, d2, d1, data));
1682 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1684 /* the case of LEA reg,[disp32] */
1685 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1686 fixup_found = 1;
1687 FSHOW((stderr,
1688 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1689 p, d6, d5, d4, d3, d2, d1, data));
1690 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1694 /* Check for constant references. */
1695 /* Check for a 32 bit word that looks like an absolute
1696 reference to within the constant vector. Constant references
1697 will be aligned. */
1698 if ((data >= (void*)(constants_start_addr-displacement))
1699 && (data < (void*)(constants_end_addr-displacement))
1700 && (((unsigned)data & 0x3) == 0)) {
1701 /* Mov eax,m32 */
1702 if (d1 == 0xa1) {
1703 fixup_found = 1;
1704 FSHOW((stderr,
1705 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1706 p, d6, d5, d4, d3, d2, d1, data));
1707 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1710 /* the case of MOV m32,EAX */
1711 if (d1 == 0xa3) {
1712 fixup_found = 1;
1713 FSHOW((stderr,
1714 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1715 p, d6, d5, d4, d3, d2, d1, data));
1716 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1719 /* the case of CMP m32,imm32 */
1720 if ((d1 == 0x3d) && (d2 == 0x81)) {
1721 fixup_found = 1;
1722 FSHOW((stderr,
1723 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1724 p, d6, d5, d4, d3, d2, d1, data));
1725 /* XX Check this */
1726 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1729 /* Check for a mod=00, r/m=101 byte. */
1730 if ((d1 & 0xc7) == 5) {
1731 /* Cmp m32,reg */
1732 if (d2 == 0x39) {
1733 fixup_found = 1;
1734 FSHOW((stderr,
1735 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1736 p, d6, d5, d4, d3, d2, d1, data));
1737 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1739 /* the case of CMP reg32,m32 */
1740 if (d2 == 0x3b) {
1741 fixup_found = 1;
1742 FSHOW((stderr,
1743 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1744 p, d6, d5, d4, d3, d2, d1, data));
1745 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1747 /* the case of MOV m32,reg32 */
1748 if (d2 == 0x89) {
1749 fixup_found = 1;
1750 FSHOW((stderr,
1751 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1752 p, d6, d5, d4, d3, d2, d1, data));
1753 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1755 /* the case of MOV reg32,m32 */
1756 if (d2 == 0x8b) {
1757 fixup_found = 1;
1758 FSHOW((stderr,
1759 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1760 p, d6, d5, d4, d3, d2, d1, data));
1761 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1763 /* the case of LEA reg32,m32 */
1764 if (d2 == 0x8d) {
1765 fixup_found = 1;
1766 FSHOW((stderr,
1767 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1768 p, d6, d5, d4, d3, d2, d1, data));
1769 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1775 /* If anything was found, print some information on the code
1776 * object. */
1777 if (fixup_found) {
1778 FSHOW((stderr,
1779 "/compiled code object at %x: header words = %d, code words = %d\n",
1780 code, nheader_words, ncode_words));
1781 FSHOW((stderr,
1782 "/const start = %x, end = %x\n",
1783 constants_start_addr, constants_end_addr));
1784 FSHOW((stderr,
1785 "/code start = %x, end = %x\n",
1786 code_start_addr, code_end_addr));
1789 #endif
1791 #ifdef LISP_FEATURE_X86
1792 void
1793 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1795 sword_t nheader_words, ncode_words, nwords;
1796 os_vm_address_t __attribute__((unused)) constants_start_addr, constants_end_addr;
1797 os_vm_address_t __attribute__((unused)) code_start_addr, code_end_addr;
1798 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1799 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1800 os_vm_size_t displacement = code_addr - old_addr;
1801 lispobj fixups = NIL;
1802 struct vector *fixups_vector;
1804 ncode_words = code_instruction_words(new_code->code_size);
1805 nheader_words = code_header_words(*(lispobj *)new_code);
1806 nwords = ncode_words + nheader_words;
1807 /* FSHOW((stderr,
1808 "/compiled code object at %x: header words = %d, code words = %d\n",
1809 new_code, nheader_words, ncode_words)); */
1810 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1811 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1812 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1813 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1815 FSHOW((stderr,
1816 "/const start = %x, end = %x\n",
1817 constants_start_addr,constants_end_addr));
1818 FSHOW((stderr,
1819 "/code start = %x; end = %x\n",
1820 code_start_addr,code_end_addr));
1823 fixups = new_code->fixups;
1824 /* It will be a Lisp vector if valid, or 0 if there are no fixups */
1825 if (fixups == 0 || !is_lisp_pointer(fixups)) {
1826 /* Check for possible errors. */
1827 if (check_code_fixups)
1828 sniff_code_object(new_code, displacement);
1830 return;
1833 fixups_vector = (struct vector *)native_pointer(fixups);
1835 /* Could be pointing to a forwarding pointer. */
1836 /* This is extremely unlikely, because the only referent of the fixups
1837 is usually the code itself; so scavenging the vector won't occur
1838 until after the code object is known to be live. As we're just now
1839 enlivening the code, the fixups shouldn't have been forwarded.
1840 Maybe the vector is on the special binding stack though ... */
1841 if (is_lisp_pointer(fixups) &&
1842 (find_page_index((void*)fixups_vector) != -1) &&
1843 forwarding_pointer_p((lispobj*)fixups_vector)) {
1844 /* If so, then follow it. */
1845 /*SHOW("following pointer to a forwarding pointer");*/
1846 fixups_vector = (struct vector *)
1847 native_pointer(forwarding_pointer_value((lispobj*)fixups_vector));
1850 /*SHOW("got fixups");*/
1852 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1853 /* Got the fixups for the code block. Now work through the vector,
1854 and apply a fixup at each address. */
1855 sword_t length = fixnum_value(fixups_vector->length);
1856 sword_t i;
1857 for (i = 0; i < length; i++) {
1858 long offset = fixups_vector->data[i];
1859 /* Now check the current value of offset. */
1860 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1862 /* If it's within the old_code object then it must be an
1863 * absolute fixup (relative ones are not saved) */
1864 if ((old_value >= old_addr)
1865 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1866 /* So add the dispacement. */
1867 *(os_vm_address_t *)(code_start_addr + offset) =
1868 old_value + displacement;
1869 else
1870 /* It is outside the old code object so it must be a
1871 * relative fixup (absolute fixups are not saved). So
1872 * subtract the displacement. */
1873 *(os_vm_address_t *)(code_start_addr + offset) =
1874 old_value - displacement;
1876 } else {
1877 /* This used to just print a note to stderr, but a bogus fixup seems to
1878 * indicate real heap corruption, so a hard hailure is in order. */
1879 lose("fixup vector %p has a bad widetag: %d\n",
1880 fixups_vector, widetag_of(fixups_vector->header));
1883 /* Check for possible errors. */
1884 if (check_code_fixups) {
1885 sniff_code_object(new_code,displacement);
1888 #endif
1890 static lispobj
1891 trans_boxed_large(lispobj object)
1893 gc_assert(is_lisp_pointer(object));
1894 return copy_large_object(object,
1895 (HeaderValue(*native_pointer(object)) | 1) + 1);
1899 * weak pointers
1902 /* XX This is a hack adapted from cgc.c. These don't work too
1903 * efficiently with the gencgc as a list of the weak pointers is
1904 * maintained within the objects which causes writes to the pages. A
1905 * limited attempt is made to avoid unnecessary writes, but this needs
1906 * a re-think. */
1907 /* FIXME: now that we have non-Lisp hashtables in the GC, it might make sense
1908 * to stop chaining weak pointers through a slot in the object, as a remedy to
1909 * the above concern. It would also shorten the object by 2 words. */
1910 static sword_t
1911 scav_weak_pointer(lispobj *where, lispobj object)
1913 /* Since we overwrite the 'next' field, we have to make
1914 * sure not to do so for pointers already in the list.
1915 * Instead of searching the list of weak_pointers each
1916 * time, we ensure that next is always NULL when the weak
1917 * pointer isn't in the list, and not NULL otherwise.
1918 * Since we can't use NULL to denote end of list, we
1919 * use a pointer back to the same weak_pointer.
1921 struct weak_pointer * wp = (struct weak_pointer*)where;
1923 if (NULL == wp->next && weak_pointer_breakable_p(wp)) {
1924 wp->next = weak_pointers;
1925 weak_pointers = wp;
1926 if (NULL == wp->next)
1927 wp->next = wp;
1930 /* Do not let GC scavenge the value slot of the weak pointer.
1931 * (That is why it is a weak pointer.) */
1933 return WEAK_POINTER_NWORDS;
1937 lispobj *
1938 search_read_only_space(void *pointer)
1940 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1941 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1942 if ((pointer < (void *)start) || (pointer >= (void *)end))
1943 return NULL;
1944 return gc_search_space(start, pointer);
1947 lispobj *
1948 search_static_space(void *pointer)
1950 lispobj *start = (lispobj *)STATIC_SPACE_START;
1951 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1952 if ((pointer < (void *)start) || (pointer >= (void *)end))
1953 return NULL;
1954 return gc_search_space(start, pointer);
1957 /* a faster version for searching the dynamic space. This will work even
1958 * if the object is in a current allocation region. */
1959 lispobj *
1960 search_dynamic_space(void *pointer)
1962 page_index_t page_index = find_page_index(pointer);
1963 lispobj *start;
1965 /* The address may be invalid, so do some checks. */
1966 if ((page_index == -1) || page_free_p(page_index))
1967 return NULL;
1968 start = (lispobj *)page_scan_start(page_index);
1969 return gc_search_space(start, pointer);
1972 // Return the starting address of the object containing 'addr'
1973 // if and only if the object is one which would be evacuated from 'from_space'
1974 // were it allowed to be either discarded as garbage or moved.
1975 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1976 // Return 0 if there is no such object - that is, if addr is past the
1977 // end of the used bytes, or its pages are not in 'from_space' etc.
1978 static lispobj*
1979 conservative_root_p(void *addr, page_index_t addr_page_index)
1981 #ifdef GENCGC_IS_PRECISE
1982 /* If we're in precise gencgc (non-x86oid as of this writing) then
1983 * we are only called on valid object pointers in the first place,
1984 * so we just have to do a bounds-check against the heap, a
1985 * generation check, and the already-pinned check. */
1986 if ((page_table[addr_page_index].gen != from_space)
1987 || (page_table[addr_page_index].dont_move != 0))
1988 return 0;
1989 return (lispobj*)1;
1990 #else
1991 /* quick check 1: Address is quite likely to have been invalid. */
1992 if (page_free_p(addr_page_index)
1993 || (page_bytes_used(addr_page_index) == 0)
1994 || (page_table[addr_page_index].gen != from_space))
1995 return 0;
1996 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
1998 /* quick check 2: Check the offset within the page.
2001 if (((uword_t)addr & (GENCGC_CARD_BYTES - 1)) > page_bytes_used(addr_page_index))
2002 return 0;
2004 /* Filter out anything which can't be a pointer to a Lisp object
2005 * (or, as a special case which also requires dont_move, a return
2006 * address referring to something in a CodeObject). This is
2007 * expensive but important, since it vastly reduces the
2008 * probability that random garbage will be bogusly interpreted as
2009 * a pointer which prevents a page from moving. */
2010 lispobj* object_start = search_dynamic_space(addr);
2011 if (!object_start) return 0;
2013 /* If the containing object is a code object and 'addr' points
2014 * anywhere beyond the boxed words,
2015 * presume it to be a valid unboxed return address. */
2016 if (instruction_ptr_p(addr, object_start))
2017 return object_start;
2019 /* Large object pages only contain ONE object, and it will never
2020 * be a CONS. However, arrays and bignums can be allocated larger
2021 * than necessary and then shrunk to fit, leaving what look like
2022 * (0 . 0) CONSes at the end. These appear valid to
2023 * properly_tagged_descriptor_p(), so pick them off here. */
2024 if (((lowtag_of((lispobj)addr) == LIST_POINTER_LOWTAG) &&
2025 page_table[addr_page_index].large_object)
2026 || !properly_tagged_descriptor_p(addr, object_start))
2027 return 0;
2029 return object_start;
2030 #endif
2033 /* Adjust large bignum and vector objects. This will adjust the
2034 * allocated region if the size has shrunk, and move unboxed objects
2035 * into unboxed pages. The pages are not promoted here, and the
2036 * promoted region is not added to the new_regions; this is really
2037 * only designed to be called from preserve_pointer(). Shouldn't fail
2038 * if this is missed, just may delay the moving of objects to unboxed
2039 * pages, and the freeing of pages. */
2040 static void
2041 maybe_adjust_large_object(lispobj *where)
2043 page_index_t first_page;
2044 page_index_t next_page;
2045 sword_t nwords;
2047 uword_t remaining_bytes;
2048 uword_t bytes_freed;
2049 uword_t old_bytes_used;
2051 int boxed;
2053 /* Check whether it's a vector or bignum object. */
2054 lispobj widetag = widetag_of(where[0]);
2055 if (widetag == SIMPLE_VECTOR_WIDETAG)
2056 boxed = BOXED_PAGE_FLAG;
2057 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
2058 boxed = UNBOXED_PAGE_FLAG;
2059 else
2060 return;
2062 /* Find its current size. */
2063 nwords = sizetab[widetag](where);
2065 first_page = find_page_index((void *)where);
2066 gc_assert(first_page >= 0);
2068 /* Note: Any page write-protection must be removed, else a later
2069 * scavenge_newspace may incorrectly not scavenge these pages.
2070 * This would not be necessary if they are added to the new areas,
2071 * but lets do it for them all (they'll probably be written
2072 * anyway?). */
2074 gc_assert(page_starts_contiguous_block_p(first_page));
2076 next_page = first_page;
2077 remaining_bytes = nwords*N_WORD_BYTES;
2078 while (remaining_bytes > GENCGC_CARD_BYTES) {
2079 gc_assert(page_table[next_page].gen == from_space);
2080 gc_assert(page_allocated_no_region_p(next_page));
2081 gc_assert(page_table[next_page].large_object);
2082 gc_assert(page_scan_start_offset(next_page) ==
2083 npage_bytes(next_page-first_page));
2084 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
2086 page_table[next_page].allocated = boxed;
2088 /* Shouldn't be write-protected at this stage. Essential that the
2089 * pages aren't. */
2090 gc_assert(!page_table[next_page].write_protected);
2091 remaining_bytes -= GENCGC_CARD_BYTES;
2092 next_page++;
2095 /* Now only one page remains, but the object may have shrunk so
2096 * there may be more unused pages which will be freed. */
2098 /* Object may have shrunk but shouldn't have grown - check. */
2099 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
2101 page_table[next_page].allocated = boxed;
2102 gc_assert(page_table[next_page].allocated ==
2103 page_table[first_page].allocated);
2105 /* Adjust the bytes_used. */
2106 old_bytes_used = page_bytes_used(next_page);
2107 set_page_bytes_used(next_page, remaining_bytes);
2109 bytes_freed = old_bytes_used - remaining_bytes;
2111 /* Free any remaining pages; needs care. */
2112 next_page++;
2113 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2114 (page_table[next_page].gen == from_space) &&
2115 page_allocated_no_region_p(next_page) &&
2116 page_table[next_page].large_object &&
2117 (page_scan_start_offset(next_page) ==
2118 npage_bytes(next_page - first_page))) {
2119 /* It checks out OK, free the page. We don't need to both zeroing
2120 * pages as this should have been done before shrinking the
2121 * object. These pages shouldn't be write protected as they
2122 * should be zero filled. */
2123 gc_assert(page_table[next_page].write_protected == 0);
2125 old_bytes_used = page_bytes_used(next_page);
2126 page_table[next_page].allocated = FREE_PAGE_FLAG;
2127 set_page_bytes_used(next_page, 0);
2128 bytes_freed += old_bytes_used;
2129 next_page++;
2132 if ((bytes_freed > 0) && gencgc_verbose) {
2133 FSHOW((stderr,
2134 "/maybe_adjust_large_object() freed %d\n",
2135 bytes_freed));
2138 generations[from_space].bytes_allocated -= bytes_freed;
2139 bytes_allocated -= bytes_freed;
2141 return;
2144 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
2145 # define hopscotch_init()
2146 # define hopscotch_reset(a)
2147 # define scavenge_pinned_ranges()
2148 # define wipe_nonpinned_words()
2149 # define hopscotch_create(a,b,c,d)
2150 # define hopscotch_log_stats(a)
2151 /* After scavenging of the roots is done, we go back to the pinned objects
2152 * and look within them for pointers. While heap_scavenge() could certainly
2153 * do this, it would potentially lead to extra work, since we can't know
2154 * whether any given object has been examined at least once, since there is
2155 * no telltale forwarding-pointer. The easiest thing to do is defer all
2156 * pinned objects to a subsequent pass, as is done here.
2158 #else
2159 static void
2160 scavenge_pinned_ranges()
2162 int i;
2163 lispobj key;
2164 for_each_hopscotch_key(i, key, pinned_objects) {
2165 lispobj* obj = native_pointer(key);
2166 lispobj header = *obj;
2167 // Never invoke scavenger on a simple-fun, just code components.
2168 if (is_cons_half(header))
2169 scavenge(obj, 2);
2170 else if (widetag_of(header) != SIMPLE_FUN_HEADER_WIDETAG)
2171 scavtab[widetag_of(header)](obj, header);
2175 static int addrcmp(const void* a, const void* b) { // For qsort()
2176 sword_t diff = *(uword_t*)a - *(uword_t*)b;
2177 return diff < 0 ? -1 : (diff > 0 ? 1 : 0);
2180 /* Zero out the byte ranges on small object pages marked dont_move,
2181 * carefully skipping over objects in the pin hashtable.
2182 * TODO: by recording an additional bit per page indicating whether
2183 * there is more than one pinned object on it, we could avoid qsort()
2184 * except in the case where there is more than one. */
2185 static void
2186 wipe_nonpinned_words()
2188 // Loop over the keys in pinned_objects and pack them densely into
2189 // the same array - pinned_objects.keys[] - but skip any simple-funs.
2190 // Admittedly this is abstraction breakage.
2191 int limit = hopscotch_max_key_index(pinned_objects);
2192 int n_pins = 0, i;
2193 for (i = 0; i <= limit; ++i) {
2194 lispobj key = pinned_objects.keys[i];
2195 if (key) {
2196 lispobj* obj = native_pointer(key);
2197 // No need to check for is_cons_half() - it will be false
2198 // on a simple-fun header, and that's the correct answer.
2199 if (widetag_of(*obj) != SIMPLE_FUN_HEADER_WIDETAG)
2200 pinned_objects.keys[n_pins++] = (uword_t)obj;
2203 // Store a sentinel at the end. Even if n_pins = table capacity (unlikely),
2204 // it is safe to write one more word, because the hops[] array immediately
2205 // follows the keys[] array in memory. At worst, 2 elements of hops[]
2206 // are clobbered, which is irrelevant since the table has already been
2207 // rendered unusable by stealing its key array for a different purpose.
2208 pinned_objects.keys[n_pins] = 0;
2209 // Order by ascending address, stopping short of the sentinel.
2210 qsort(pinned_objects.keys, n_pins, sizeof (uword_t), addrcmp);
2211 #if 0
2212 printf("Sorted pin list:\n");
2213 for (i = 0; i < n_pins; ++i) {
2214 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2215 if (!is_cons_half(*obj))
2216 printf("%p: %5d words\n", obj, (int)sizetab[widetag_of(*obj)](obj));
2217 else printf("%p: CONS\n", obj);
2219 #endif
2220 // Each entry in the pinned objects demarcates two ranges to be cleared:
2221 // - the range preceding it back to either the page start, or prior object.
2222 // - the range after it, up to the lesser of page bytes used or next object.
2223 uword_t preceding_object = 0;
2224 uword_t this_page_end = 0;
2225 #define page_base_address(x) (x&~(GENCGC_CARD_BYTES-1))
2226 for (i = 0; i < n_pins; ++i) {
2227 // Handle the preceding range. If this object is on the same page as
2228 // its predecessor, then intervening bytes were already zeroed.
2229 // If not, then start a new page and do some bookkeeping.
2230 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2231 uword_t this_page_base = page_base_address((uword_t)obj);
2232 /* printf("i=%d obj=%p base=%p\n", i, obj, (void*)this_page_base); */
2233 if (this_page_base > page_base_address(preceding_object)) {
2234 bzero((void*)this_page_base, (uword_t)obj - this_page_base);
2235 // Move the page to newspace
2236 page_index_t page = find_page_index(obj);
2237 int used = page_bytes_used(page);
2238 this_page_end = this_page_base + used;
2239 /* printf(" Clearing %p .. %p (limit=%p)\n",
2240 (void*)this_page_base, obj, (void*)this_page_end); */
2241 generations[new_space].bytes_allocated += used;
2242 generations[page_table[page].gen].bytes_allocated -= used;
2243 page_table[page].gen = new_space;
2244 page_table[page].has_pins = 0;
2246 // Handle the following range.
2247 lispobj word = *obj;
2248 size_t nwords = is_cons_half(word) ? 2 : sizetab[widetag_of(word)](obj);
2249 uword_t range_start = (uword_t)(obj + nwords);
2250 uword_t range_end = this_page_end;
2251 // There is always an i+1'th key due to the sentinel value.
2252 if (page_base_address(pinned_objects.keys[i+1]) == this_page_base)
2253 range_end = pinned_objects.keys[i+1];
2254 /* printf(" Clearing %p .. %p\n", (void*)range_start, (void*)range_end); */
2255 bzero((void*)range_start, range_end - range_start);
2256 preceding_object = (uword_t)obj;
2260 /* Add 'object' to the hashtable, and if the object is a code component,
2261 * then also add all of the embedded simple-funs.
2262 * The rationale for the extra work on code components is that without it,
2263 * every test of pinned_p() on an object would have to check if the pointer
2264 * is to a simple-fun - entailing an extra read of the header - and mapping
2265 * to its code component if so. Since more calls to pinned_p occur than to
2266 * pin_object, the extra burden should be on this function.
2267 * Experimentation bears out that this is the better technique.
2268 * Also, we wouldn't often expect code components in the collected generation
2269 * so the extra work here is quite minimal, even if it can generally add to
2270 * the number of keys in the hashtable.
2272 static void
2273 pin_object(lispobj object)
2275 if (!hopscotch_containsp(&pinned_objects, object)) {
2276 hopscotch_put(&pinned_objects, object, 1);
2277 struct code* maybe_code = (struct code*)native_pointer(object);
2278 if (widetag_of(maybe_code->header) == CODE_HEADER_WIDETAG) {
2279 for_each_simple_fun(i, fun, maybe_code, 0, {
2280 hopscotch_put(&pinned_objects,
2281 make_lispobj(fun, FUN_POINTER_LOWTAG),
2287 #endif
2289 /* Take a possible pointer to a Lisp object and mark its page in the
2290 * page_table so that it will not be relocated during a GC.
2292 * This involves locating the page it points to, then backing up to
2293 * the start of its region, then marking all pages dont_move from there
2294 * up to the first page that's not full or has a different generation
2296 * It is assumed that all the page static flags have been cleared at
2297 * the start of a GC.
2299 * It is also assumed that the current gc_alloc() region has been
2300 * flushed and the tables updated. */
2302 // TODO: there's probably a way to be a little more efficient here.
2303 // As things are, we start by finding the object that encloses 'addr',
2304 // then we see if 'addr' was a "valid" Lisp pointer to that object
2305 // - meaning we expect the correct lowtag on the pointer - except
2306 // that for code objects we don't require a correct lowtag
2307 // and we allow a pointer to anywhere in the object.
2309 // It should be possible to avoid calling search_dynamic_space
2310 // more of the time. First, check if the page pointed to might hold code.
2311 // If it does, then we continue regardless of the pointer's lowtag
2312 // (because of the special allowance). If the page definitely does *not*
2313 // hold code, then we require up front that the lowtake make sense,
2314 // by doing the same checks that are in properly_tagged_descriptor_p.
2316 // Problem: when code is allocated from a per-thread region,
2317 // does it ensure that the occupied pages are flagged as having code?
2319 static void
2320 preserve_pointer(void *addr)
2322 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2323 /* Immobile space MUST be lower than dynamic space,
2324 or else this test needs to be revised */
2325 if (addr < (void*)IMMOBILE_SPACE_END) {
2326 extern void immobile_space_preserve_pointer(void*);
2327 immobile_space_preserve_pointer(addr);
2328 return;
2330 #endif
2331 page_index_t addr_page_index = find_page_index(addr);
2332 lispobj *object_start;
2334 if (addr_page_index == -1
2335 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2336 return;
2338 /* (Now that we know that addr_page_index is in range, it's
2339 * safe to index into page_table[] with it.) */
2340 unsigned int region_allocation = page_table[addr_page_index].allocated;
2342 /* Find the beginning of the region. Note that there may be
2343 * objects in the region preceding the one that we were passed a
2344 * pointer to: if this is the case, we will write-protect all the
2345 * previous objects' pages too. */
2347 #if 0
2348 /* I think this'd work just as well, but without the assertions.
2349 * -dan 2004.01.01 */
2350 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2351 #else
2352 page_index_t first_page = addr_page_index;
2353 while (!page_starts_contiguous_block_p(first_page)) {
2354 --first_page;
2355 /* Do some checks. */
2356 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2357 gc_assert(page_table[first_page].gen == from_space);
2358 gc_assert(page_table[first_page].allocated == region_allocation);
2360 #endif
2362 /* Adjust any large objects before promotion as they won't be
2363 * copied after promotion. */
2364 if (page_table[first_page].large_object) {
2365 maybe_adjust_large_object(page_address(first_page));
2366 /* It may have moved to unboxed pages. */
2367 region_allocation = page_table[first_page].allocated;
2370 /* Now work forward until the end of this contiguous area is found,
2371 * marking all pages as dont_move. */
2372 page_index_t i;
2373 for (i = first_page; ;i++) {
2374 gc_assert(page_table[i].allocated == region_allocation);
2376 /* Mark the page static. */
2377 page_table[i].dont_move = 1;
2379 /* It is essential that the pages are not write protected as
2380 * they may have pointers into the old-space which need
2381 * scavenging. They shouldn't be write protected at this
2382 * stage. */
2383 gc_assert(!page_table[i].write_protected);
2385 /* Check whether this is the last page in this contiguous block.. */
2386 if (page_ends_contiguous_block_p(i, from_space))
2387 break;
2390 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2391 /* Do not do this for multi-page objects. Those pages do not need
2392 * object wipeout anyway.
2394 if (do_wipe_p && i == first_page) { // single-page object
2395 lispobj word = *object_start;
2396 int lowtag = is_cons_half(word) ?
2397 LIST_POINTER_LOWTAG : lowtag_for_widetag[widetag_of(word)>>2];
2398 pin_object(make_lispobj(object_start, lowtag));
2399 page_table[i].has_pins = 1;
2401 #endif
2403 /* Check that the page is now static. */
2404 gc_assert(page_table[addr_page_index].dont_move != 0);
2407 /* If the given page is not write-protected, then scan it for pointers
2408 * to younger generations or the top temp. generation, if no
2409 * suspicious pointers are found then the page is write-protected.
2411 * Care is taken to check for pointers to the current gc_alloc()
2412 * region if it is a younger generation or the temp. generation. This
2413 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2414 * the gc_alloc_generation does not need to be checked as this is only
2415 * called from scavenge_generation() when the gc_alloc generation is
2416 * younger, so it just checks if there is a pointer to the current
2417 * region.
2419 * We return 1 if the page was write-protected, else 0. */
2420 static int
2421 update_page_write_prot(page_index_t page)
2423 generation_index_t gen = page_table[page].gen;
2424 sword_t j;
2425 int wp_it = 1;
2426 void **page_addr = (void **)page_address(page);
2427 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2429 /* Shouldn't be a free page. */
2430 gc_assert(!page_free_p(page));
2431 gc_assert(page_bytes_used(page) != 0);
2433 /* Skip if it's already write-protected, pinned, or unboxed */
2434 if (page_table[page].write_protected
2435 /* FIXME: What's the reason for not write-protecting pinned pages? */
2436 || page_table[page].dont_move
2437 || page_unboxed_p(page))
2438 return (0);
2440 /* Scan the page for pointers to younger generations or the
2441 * top temp. generation. */
2443 /* This is conservative: any word satisfying is_lisp_pointer() is
2444 * assumed to be a pointer. To do otherwise would require a family
2445 * of scavenge-like functions. */
2446 for (j = 0; j < num_words; j++) {
2447 void *ptr = *(page_addr+j);
2448 page_index_t index;
2449 lispobj __attribute__((unused)) header;
2451 if (!is_lisp_pointer((lispobj)ptr))
2452 continue;
2453 /* Check that it's in the dynamic space */
2454 if ((index = find_page_index(ptr)) != -1) {
2455 if (/* Does it point to a younger or the temp. generation? */
2456 (!page_free_p(index)
2457 && (page_bytes_used(index) != 0)
2458 && ((page_table[index].gen < gen)
2459 || (page_table[index].gen == SCRATCH_GENERATION)))
2461 /* Or does it point within a current gc_alloc() region? */
2462 || ((boxed_region.start_addr <= ptr)
2463 && (ptr <= boxed_region.free_pointer))
2464 || ((unboxed_region.start_addr <= ptr)
2465 && (ptr <= unboxed_region.free_pointer))) {
2466 wp_it = 0;
2467 break;
2470 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2471 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2472 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2473 // This is *possibly* a pointer to an object in immobile space,
2474 // given that above two conditions were satisfied.
2475 // But unlike in the dynamic space case, we need to read a byte
2476 // from the object to determine its generation, which requires care.
2477 // Consider an unboxed word that looks like a pointer to a word that
2478 // looks like fun-header-widetag. We can't naively back up to the
2479 // underlying code object since the alleged header might not be one.
2480 int obj_gen = gen; // Make comparison fail if we fall through
2481 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2482 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2483 } else if (widetag_of(header) == SIMPLE_FUN_HEADER_WIDETAG) {
2484 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2485 // This is a heuristic, since we're not actually looking for
2486 // an object boundary. Precise scanning of 'page' would obviate
2487 // the guard conditions here.
2488 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2489 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2490 obj_gen = __immobile_obj_generation(code);
2492 // A bogus generation number implies a not-really-pointer,
2493 // but it won't cause misbehavior.
2494 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2495 wp_it = 0;
2496 break;
2499 #endif
2502 if (wp_it == 1) {
2503 /* Write-protect the page. */
2504 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2506 os_protect((void *)page_addr,
2507 GENCGC_CARD_BYTES,
2508 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2510 /* Note the page as protected in the page tables. */
2511 page_table[page].write_protected = 1;
2514 return (wp_it);
2517 /* Is this page holding a normal (non-hashtable) large-object
2518 * simple-vector? */
2519 static inline boolean large_simple_vector_p(page_index_t page) {
2520 if (!page_table[page].large_object)
2521 return 0;
2522 lispobj object = *(lispobj *)page_address(page);
2523 return widetag_of(object) == SIMPLE_VECTOR_WIDETAG &&
2524 (HeaderValue(object) & 0xFF) == subtype_VectorNormal;
2528 /* Scavenge all generations from FROM to TO, inclusive, except for
2529 * new_space which needs special handling, as new objects may be
2530 * added which are not checked here - use scavenge_newspace generation.
2532 * Write-protected pages should not have any pointers to the
2533 * from_space so do need scavenging; thus write-protected pages are
2534 * not always scavenged. There is some code to check that these pages
2535 * are not written; but to check fully the write-protected pages need
2536 * to be scavenged by disabling the code to skip them.
2538 * Under the current scheme when a generation is GCed the younger
2539 * generations will be empty. So, when a generation is being GCed it
2540 * is only necessary to scavenge the older generations for pointers
2541 * not the younger. So a page that does not have pointers to younger
2542 * generations does not need to be scavenged.
2544 * The write-protection can be used to note pages that don't have
2545 * pointers to younger pages. But pages can be written without having
2546 * pointers to younger generations. After the pages are scavenged here
2547 * they can be scanned for pointers to younger generations and if
2548 * there are none the page can be write-protected.
2550 * One complication is when the newspace is the top temp. generation.
2552 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2553 * that none were written, which they shouldn't be as they should have
2554 * no pointers to younger generations. This breaks down for weak
2555 * pointers as the objects contain a link to the next and are written
2556 * if a weak pointer is scavenged. Still it's a useful check. */
2557 static void
2558 scavenge_generations(generation_index_t from, generation_index_t to)
2560 page_index_t i;
2561 page_index_t num_wp = 0;
2563 #define SC_GEN_CK 0
2564 #if SC_GEN_CK
2565 /* Clear the write_protected_cleared flags on all pages. */
2566 for (i = 0; i < page_table_pages; i++)
2567 page_table[i].write_protected_cleared = 0;
2568 #endif
2570 for (i = 0; i < last_free_page; i++) {
2571 generation_index_t generation = page_table[i].gen;
2572 if (page_boxed_p(i)
2573 && (page_bytes_used(i) != 0)
2574 && (generation != new_space)
2575 && (generation >= from)
2576 && (generation <= to)) {
2577 page_index_t last_page,j;
2578 int write_protected=1;
2580 /* This should be the start of a region */
2581 gc_assert(page_starts_contiguous_block_p(i));
2583 if (large_simple_vector_p(i)) {
2584 /* Scavenge only the unprotected pages of a
2585 * large-object vector, other large objects could be
2586 * handled as well, but vectors are easier to deal
2587 * with and are more likely to grow to very large
2588 * sizes where avoiding scavenging the whole thing is
2589 * worthwile */
2590 if (!page_table[i].write_protected) {
2591 scavenge((lispobj*)page_address(i) + 2,
2592 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2593 update_page_write_prot(i);
2595 for (last_page = i + 1; ; last_page++) {
2596 lispobj* start = page_address(last_page);
2597 write_protected = page_table[last_page].write_protected;
2598 if (page_ends_contiguous_block_p(last_page, generation)) {
2599 if (!write_protected) {
2600 scavenge(start, page_bytes_used(last_page) / N_WORD_BYTES);
2601 update_page_write_prot(last_page);
2603 break;
2605 if (!write_protected) {
2606 scavenge(start, GENCGC_CARD_BYTES / N_WORD_BYTES);
2607 update_page_write_prot(last_page);
2610 } else {
2611 /* Now work forward until the end of the region */
2612 for (last_page = i; ; last_page++) {
2613 write_protected =
2614 write_protected && page_table[last_page].write_protected;
2615 if (page_ends_contiguous_block_p(last_page, generation))
2616 break;
2618 if (!write_protected) {
2619 heap_scavenge(page_address(i),
2620 (lispobj*)((char*)page_address(last_page)
2621 + page_bytes_used(last_page)));
2623 /* Now scan the pages and write protect those that
2624 * don't have pointers to younger generations. */
2625 if (enable_page_protection) {
2626 for (j = i; j <= last_page; j++) {
2627 num_wp += update_page_write_prot(j);
2630 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2631 FSHOW((stderr,
2632 "/write protected %d pages within generation %d\n",
2633 num_wp, generation));
2637 i = last_page;
2641 #if SC_GEN_CK
2642 /* Check that none of the write_protected pages in this generation
2643 * have been written to. */
2644 for (i = 0; i < page_table_pages; i++) {
2645 if (!page_free_p(i)
2646 && (page_bytes_used(i) != 0)
2647 && (page_table[i].gen == generation)
2648 && (page_table[i].write_protected_cleared != 0)) {
2649 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2650 FSHOW((stderr,
2651 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2652 page_bytes_used(i),
2653 scan_start_offset(page_table[i]),
2654 page_table[i].dont_move));
2655 lose("write to protected page %d in scavenge_generation()\n", i);
2658 #endif
2662 /* Scavenge a newspace generation. As it is scavenged new objects may
2663 * be allocated to it; these will also need to be scavenged. This
2664 * repeats until there are no more objects unscavenged in the
2665 * newspace generation.
2667 * To help improve the efficiency, areas written are recorded by
2668 * gc_alloc() and only these scavenged. Sometimes a little more will be
2669 * scavenged, but this causes no harm. An easy check is done that the
2670 * scavenged bytes equals the number allocated in the previous
2671 * scavenge.
2673 * Write-protected pages are not scanned except if they are marked
2674 * dont_move in which case they may have been promoted and still have
2675 * pointers to the from space.
2677 * Write-protected pages could potentially be written by alloc however
2678 * to avoid having to handle re-scavenging of write-protected pages
2679 * gc_alloc() does not write to write-protected pages.
2681 * New areas of objects allocated are recorded alternatively in the two
2682 * new_areas arrays below. */
2683 static struct new_area new_areas_1[NUM_NEW_AREAS];
2684 static struct new_area new_areas_2[NUM_NEW_AREAS];
2686 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2687 extern unsigned int immobile_scav_queue_count;
2688 extern void
2689 gc_init_immobile(),
2690 update_immobile_nursery_bits(),
2691 scavenge_immobile_roots(generation_index_t,generation_index_t),
2692 scavenge_immobile_newspace(),
2693 sweep_immobile_space(int raise),
2694 write_protect_immobile_space();
2695 #else
2696 #define immobile_scav_queue_count 0
2697 #endif
2699 /* Do one full scan of the new space generation. This is not enough to
2700 * complete the job as new objects may be added to the generation in
2701 * the process which are not scavenged. */
2702 static void
2703 scavenge_newspace_generation_one_scan(generation_index_t generation)
2705 page_index_t i;
2707 FSHOW((stderr,
2708 "/starting one full scan of newspace generation %d\n",
2709 generation));
2710 for (i = 0; i < last_free_page; i++) {
2711 /* Note that this skips over open regions when it encounters them. */
2712 if (page_boxed_p(i)
2713 && (page_bytes_used(i) != 0)
2714 && (page_table[i].gen == generation)
2715 && ((page_table[i].write_protected == 0)
2716 /* (This may be redundant as write_protected is now
2717 * cleared before promotion.) */
2718 || (page_table[i].dont_move == 1))) {
2719 page_index_t last_page;
2720 int all_wp=1;
2722 /* The scavenge will start at the scan_start_offset of
2723 * page i.
2725 * We need to find the full extent of this contiguous
2726 * block in case objects span pages.
2728 * Now work forward until the end of this contiguous area
2729 * is found. A small area is preferred as there is a
2730 * better chance of its pages being write-protected. */
2731 for (last_page = i; ;last_page++) {
2732 /* If all pages are write-protected and movable,
2733 * then no need to scavenge */
2734 all_wp=all_wp && page_table[last_page].write_protected &&
2735 !page_table[last_page].dont_move;
2737 /* Check whether this is the last page in this
2738 * contiguous block */
2739 if (page_ends_contiguous_block_p(last_page, generation))
2740 break;
2743 /* Do a limited check for write-protected pages. */
2744 if (!all_wp) {
2745 new_areas_ignore_page = last_page;
2746 heap_scavenge(page_scan_start(i),
2747 (lispobj*)((char*)page_address(last_page)
2748 + page_bytes_used(last_page)));
2750 i = last_page;
2753 FSHOW((stderr,
2754 "/done with one full scan of newspace generation %d\n",
2755 generation));
2758 /* Do a complete scavenge of the newspace generation. */
2759 static void
2760 scavenge_newspace_generation(generation_index_t generation)
2762 size_t i;
2764 /* the new_areas array currently being written to by gc_alloc() */
2765 struct new_area (*current_new_areas)[] = &new_areas_1;
2766 size_t current_new_areas_index;
2768 /* the new_areas created by the previous scavenge cycle */
2769 struct new_area (*previous_new_areas)[] = NULL;
2770 size_t previous_new_areas_index;
2772 /* Flush the current regions updating the tables. */
2773 gc_alloc_update_all_page_tables(0);
2775 /* Turn on the recording of new areas by gc_alloc(). */
2776 new_areas = current_new_areas;
2777 new_areas_index = 0;
2779 /* Don't need to record new areas that get scavenged anyway during
2780 * scavenge_newspace_generation_one_scan. */
2781 record_new_objects = 1;
2783 /* Start with a full scavenge. */
2784 scavenge_newspace_generation_one_scan(generation);
2786 /* Record all new areas now. */
2787 record_new_objects = 2;
2789 /* Give a chance to weak hash tables to make other objects live.
2790 * FIXME: The algorithm implemented here for weak hash table gcing
2791 * is O(W^2+N) as Bruno Haible warns in
2792 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2793 * see "Implementation 2". */
2794 scav_weak_hash_tables();
2796 /* Flush the current regions updating the tables. */
2797 gc_alloc_update_all_page_tables(0);
2799 /* Grab new_areas_index. */
2800 current_new_areas_index = new_areas_index;
2802 /*FSHOW((stderr,
2803 "The first scan is finished; current_new_areas_index=%d.\n",
2804 current_new_areas_index));*/
2806 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2807 /* Move the current to the previous new areas */
2808 previous_new_areas = current_new_areas;
2809 previous_new_areas_index = current_new_areas_index;
2811 /* Scavenge all the areas in previous new areas. Any new areas
2812 * allocated are saved in current_new_areas. */
2814 /* Allocate an array for current_new_areas; alternating between
2815 * new_areas_1 and 2 */
2816 if (previous_new_areas == &new_areas_1)
2817 current_new_areas = &new_areas_2;
2818 else
2819 current_new_areas = &new_areas_1;
2821 /* Set up for gc_alloc(). */
2822 new_areas = current_new_areas;
2823 new_areas_index = 0;
2825 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2826 scavenge_immobile_newspace();
2827 #endif
2828 /* Check whether previous_new_areas had overflowed. */
2829 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2831 /* New areas of objects allocated have been lost so need to do a
2832 * full scan to be sure! If this becomes a problem try
2833 * increasing NUM_NEW_AREAS. */
2834 if (gencgc_verbose) {
2835 SHOW("new_areas overflow, doing full scavenge");
2838 /* Don't need to record new areas that get scavenged
2839 * anyway during scavenge_newspace_generation_one_scan. */
2840 record_new_objects = 1;
2842 scavenge_newspace_generation_one_scan(generation);
2844 /* Record all new areas now. */
2845 record_new_objects = 2;
2847 scav_weak_hash_tables();
2849 /* Flush the current regions updating the tables. */
2850 gc_alloc_update_all_page_tables(0);
2852 } else {
2854 /* Work through previous_new_areas. */
2855 for (i = 0; i < previous_new_areas_index; i++) {
2856 page_index_t page = (*previous_new_areas)[i].page;
2857 size_t offset = (*previous_new_areas)[i].offset;
2858 size_t size = (*previous_new_areas)[i].size;
2859 gc_assert(size % N_WORD_BYTES == 0);
2860 lispobj *start = (lispobj*)((char*)page_address(page) + offset);
2861 heap_scavenge(start, (lispobj*)((char*)start + size));
2864 scav_weak_hash_tables();
2866 /* Flush the current regions updating the tables. */
2867 gc_alloc_update_all_page_tables(0);
2870 current_new_areas_index = new_areas_index;
2872 /*FSHOW((stderr,
2873 "The re-scan has finished; current_new_areas_index=%d.\n",
2874 current_new_areas_index));*/
2877 /* Turn off recording of areas allocated by gc_alloc(). */
2878 record_new_objects = 0;
2880 #if SC_NS_GEN_CK
2882 page_index_t i;
2883 /* Check that none of the write_protected pages in this generation
2884 * have been written to. */
2885 for (i = 0; i < page_table_pages; i++) {
2886 if (!page_free_p(i)
2887 && (page_bytes_used(i) != 0)
2888 && (page_table[i].gen == generation)
2889 && (page_table[i].write_protected_cleared != 0)
2890 && (page_table[i].dont_move == 0)) {
2891 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2892 i, generation, page_table[i].dont_move);
2896 #endif
2899 /* Un-write-protect all the pages in from_space. This is done at the
2900 * start of a GC else there may be many page faults while scavenging
2901 * the newspace (I've seen drive the system time to 99%). These pages
2902 * would need to be unprotected anyway before unmapping in
2903 * free_oldspace; not sure what effect this has on paging.. */
2904 static void
2905 unprotect_oldspace(void)
2907 page_index_t i;
2908 void *region_addr = 0;
2909 void *page_addr = 0;
2910 uword_t region_bytes = 0;
2912 for (i = 0; i < last_free_page; i++) {
2913 if (!page_free_p(i)
2914 && (page_bytes_used(i) != 0)
2915 && (page_table[i].gen == from_space)) {
2917 /* Remove any write-protection. We should be able to rely
2918 * on the write-protect flag to avoid redundant calls. */
2919 if (page_table[i].write_protected) {
2920 page_table[i].write_protected = 0;
2921 page_addr = page_address(i);
2922 if (!region_addr) {
2923 /* First region. */
2924 region_addr = page_addr;
2925 region_bytes = GENCGC_CARD_BYTES;
2926 } else if (region_addr + region_bytes == page_addr) {
2927 /* Region continue. */
2928 region_bytes += GENCGC_CARD_BYTES;
2929 } else {
2930 /* Unprotect previous region. */
2931 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2932 /* First page in new region. */
2933 region_addr = page_addr;
2934 region_bytes = GENCGC_CARD_BYTES;
2939 if (region_addr) {
2940 /* Unprotect last region. */
2941 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2945 /* Work through all the pages and free any in from_space. This
2946 * assumes that all objects have been copied or promoted to an older
2947 * generation. Bytes_allocated and the generation bytes_allocated
2948 * counter are updated. The number of bytes freed is returned. */
2949 static uword_t
2950 free_oldspace(void)
2952 uword_t bytes_freed = 0;
2953 page_index_t first_page, last_page;
2955 first_page = 0;
2957 do {
2958 /* Find a first page for the next region of pages. */
2959 while ((first_page < last_free_page)
2960 && (page_free_p(first_page)
2961 || (page_bytes_used(first_page) == 0)
2962 || (page_table[first_page].gen != from_space)))
2963 first_page++;
2965 if (first_page >= last_free_page)
2966 break;
2968 /* Find the last page of this region. */
2969 last_page = first_page;
2971 do {
2972 /* Free the page. */
2973 bytes_freed += page_bytes_used(last_page);
2974 generations[page_table[last_page].gen].bytes_allocated -=
2975 page_bytes_used(last_page);
2976 page_table[last_page].allocated = FREE_PAGE_FLAG;
2977 set_page_bytes_used(last_page, 0);
2978 /* Should already be unprotected by unprotect_oldspace(). */
2979 gc_assert(!page_table[last_page].write_protected);
2980 last_page++;
2982 while ((last_page < last_free_page)
2983 && !page_free_p(last_page)
2984 && (page_bytes_used(last_page) != 0)
2985 && (page_table[last_page].gen == from_space));
2987 #ifdef READ_PROTECT_FREE_PAGES
2988 os_protect(page_address(first_page),
2989 npage_bytes(last_page-first_page),
2990 OS_VM_PROT_NONE);
2991 #endif
2992 first_page = last_page;
2993 } while (first_page < last_free_page);
2995 bytes_allocated -= bytes_freed;
2996 return bytes_freed;
2999 #if 0
3000 /* Print some information about a pointer at the given address. */
3001 static void
3002 print_ptr(lispobj *addr)
3004 /* If addr is in the dynamic space then out the page information. */
3005 page_index_t pi1 = find_page_index((void*)addr);
3007 if (pi1 != -1)
3008 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3009 addr,
3010 pi1,
3011 page_table[pi1].allocated,
3012 page_table[pi1].gen,
3013 page_bytes_used(pi1),
3014 scan_start_offset(page_table[pi1]),
3015 page_table[pi1].dont_move);
3016 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3017 *(addr-4),
3018 *(addr-3),
3019 *(addr-2),
3020 *(addr-1),
3021 *(addr-0),
3022 *(addr+1),
3023 *(addr+2),
3024 *(addr+3),
3025 *(addr+4));
3027 #endif
3029 static int
3030 is_in_stack_space(lispobj ptr)
3032 /* For space verification: Pointers can be valid if they point
3033 * to a thread stack space. This would be faster if the thread
3034 * structures had page-table entries as if they were part of
3035 * the heap space. */
3036 struct thread *th;
3037 for_each_thread(th) {
3038 if ((th->control_stack_start <= (lispobj *)ptr) &&
3039 (th->control_stack_end >= (lispobj *)ptr)) {
3040 return 1;
3043 return 0;
3046 // NOTE: This function can produces false failure indications,
3047 // usually related to dynamic space pointing to the stack of a
3048 // dead thread, but there may be other reasons as well.
3049 static void
3050 verify_range(lispobj *start, size_t words)
3052 extern int valid_lisp_pointer_p(lispobj);
3053 int is_in_readonly_space =
3054 (READ_ONLY_SPACE_START <= (uword_t)start &&
3055 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3056 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3057 int is_in_immobile_space =
3058 (IMMOBILE_SPACE_START <= (uword_t)start &&
3059 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3060 #endif
3062 lispobj *end = start + words;
3063 while (start < end) {
3064 size_t count = 1;
3065 lispobj thing = *start;
3066 lispobj __attribute__((unused)) pointee;
3068 if (is_lisp_pointer(thing)) {
3069 page_index_t page_index = find_page_index((void*)thing);
3070 sword_t to_readonly_space =
3071 (READ_ONLY_SPACE_START <= thing &&
3072 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3073 sword_t to_static_space =
3074 (STATIC_SPACE_START <= thing &&
3075 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3076 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3077 sword_t to_immobile_space =
3078 (IMMOBILE_SPACE_START <= thing &&
3079 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
3080 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
3081 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3082 #endif
3084 /* Does it point to the dynamic space? */
3085 if (page_index != -1) {
3086 /* If it's within the dynamic space it should point to a used page. */
3087 if (page_free_p(page_index))
3088 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3089 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3090 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3091 /* Check that it doesn't point to a forwarding pointer! */
3092 if (*native_pointer(thing) == 0x01) {
3093 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3095 /* Check that its not in the RO space as it would then be a
3096 * pointer from the RO to the dynamic space. */
3097 if (is_in_readonly_space) {
3098 lose("ptr to dynamic space %p from RO space %x\n",
3099 thing, start);
3101 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3102 // verify all immobile space -> dynamic space pointers
3103 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3104 lose("Ptr %p @ %p sees junk.\n", thing, start);
3106 #endif
3107 /* Does it point to a plausible object? This check slows
3108 * it down a lot (so it's commented out).
3110 * "a lot" is serious: it ate 50 minutes cpu time on
3111 * my duron 950 before I came back from lunch and
3112 * killed it.
3114 * FIXME: Add a variable to enable this
3115 * dynamically. */
3117 if (!valid_lisp_pointer_p((lispobj *)thing) {
3118 lose("ptr %p to invalid object %p\n", thing, start);
3121 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3122 } else if (to_immobile_space) {
3123 // the object pointed to must not have been discarded as garbage
3124 if (!other_immediate_lowtag_p(*native_pointer(thing))
3125 || immobile_filler_p(native_pointer(thing)))
3126 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3127 // verify all pointers to immobile space
3128 if (!valid_lisp_pointer_p(thing))
3129 lose("Ptr %p @ %p sees junk.\n", thing, start);
3130 #endif
3131 } else {
3132 extern char __attribute__((unused)) funcallable_instance_tramp;
3133 /* Verify that it points to another valid space. */
3134 if (!to_readonly_space && !to_static_space
3135 && !is_in_stack_space(thing)) {
3136 lose("Ptr %p @ %p sees junk.\n", thing, start);
3139 } else if (is_lisp_immediate(thing) || /* skip immediates */
3140 widetag_of(thing) == NO_TLS_VALUE_MARKER_WIDETAG) {
3141 } else if (unboxed_obj_widetag_p(widetag_of(thing))) {
3142 count = sizetab[widetag_of(thing)](start);
3143 } else switch(widetag_of(thing)) {
3144 /* boxed objects */
3145 case SIMPLE_VECTOR_WIDETAG:
3146 case RATIO_WIDETAG:
3147 case COMPLEX_WIDETAG:
3148 case SIMPLE_ARRAY_WIDETAG:
3149 case COMPLEX_BASE_STRING_WIDETAG:
3150 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3151 case COMPLEX_CHARACTER_STRING_WIDETAG:
3152 #endif
3153 case COMPLEX_VECTOR_NIL_WIDETAG:
3154 case COMPLEX_BIT_VECTOR_WIDETAG:
3155 case COMPLEX_VECTOR_WIDETAG:
3156 case COMPLEX_ARRAY_WIDETAG:
3157 case CLOSURE_HEADER_WIDETAG:
3158 // FIXME: x86-64 can have partially unboxed FINs. The raw words
3159 // are at the moment valid fixnums by blind luck.
3160 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3161 case VALUE_CELL_HEADER_WIDETAG:
3162 case SYMBOL_HEADER_WIDETAG:
3163 case WEAK_POINTER_WIDETAG:
3164 // skip 1 word; any following words are descriptors
3165 break;
3166 case FDEFN_WIDETAG:
3167 #ifdef LISP_FEATURE_IMMOBILE_CODE
3168 verify_range(start + 1, 2);
3169 pointee = fdefn_raw_referent((struct fdefn*)start);
3170 verify_range(&pointee, 1);
3171 count = 4;
3172 #endif
3173 break;
3175 case INSTANCE_HEADER_WIDETAG:
3176 if (instance_layout(start)) {
3177 lispobj bitmap =
3178 ((struct layout*)
3179 native_pointer(instance_layout(start)))->bitmap;
3180 sword_t nslots = instance_length(thing) | 1;
3181 instance_scan(verify_range, start+1, nslots, bitmap);
3182 count = 1 + nslots;
3184 break;
3185 case CODE_HEADER_WIDETAG:
3187 struct code *code = (struct code *) start;
3188 sword_t nheader_words = code_header_words(code->header);
3189 /* Scavenge the boxed section of the code data block */
3190 verify_range(start + 1, nheader_words - 1);
3192 /* Scavenge the boxed section of each function
3193 * object in the code data block. */
3194 for_each_simple_fun(i, fheaderp, code, 1, {
3195 verify_range(SIMPLE_FUN_SCAV_START(fheaderp),
3196 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3197 count = nheader_words + code_instruction_words(code->code_size);
3198 break;
3200 default:
3201 lose("Unhandled widetag %p at %p\n",
3202 widetag_of(*start), start);
3204 start += count;
3207 static void verify_space(lispobj *start, lispobj *end) {
3208 verify_range(start, end-start);
3211 static void verify_dynamic_space();
3213 static void
3214 verify_gc(void)
3216 /* FIXME: It would be nice to make names consistent so that
3217 * foo_size meant size *in* *bytes* instead of size in some
3218 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3219 * Some counts of lispobjs are called foo_count; it might be good
3220 * to grep for all foo_size and rename the appropriate ones to
3221 * foo_count. */
3222 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3223 # ifdef __linux__
3224 // Try this verification if marknsweep was compiled with extra debugging.
3225 // But weak symbols don't work on macOS.
3226 extern void __attribute__((weak)) check_varyobj_pages();
3227 if (&check_varyobj_pages) check_varyobj_pages();
3228 # endif
3229 verify_space((lispobj*)IMMOBILE_SPACE_START,
3230 (lispobj*)SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0));
3231 verify_space((lispobj*)IMMOBILE_VARYOBJ_SUBSPACE_START,
3232 (lispobj*)SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3233 #endif
3234 struct thread *th;
3235 for_each_thread(th) {
3236 verify_space(th->binding_stack_start,
3237 (lispobj*)get_binding_stack_pointer(th));
3239 verify_space((lispobj*)READ_ONLY_SPACE_START,
3240 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3241 verify_space((lispobj*)STATIC_SPACE_START,
3242 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3243 verify_dynamic_space();
3246 void
3247 walk_generation(void (*proc)(lispobj*,lispobj*),
3248 generation_index_t generation)
3250 page_index_t i;
3251 int genmask = generation >= 0 ? 1 << generation : ~0;
3253 for (i = 0; i < last_free_page; i++) {
3254 if (!page_free_p(i)
3255 && (page_bytes_used(i) != 0)
3256 && ((1 << page_table[i].gen) & genmask)) {
3257 page_index_t last_page;
3259 /* This should be the start of a contiguous block */
3260 gc_assert(page_starts_contiguous_block_p(i));
3262 /* Need to find the full extent of this contiguous block in case
3263 objects span pages. */
3265 /* Now work forward until the end of this contiguous area is
3266 found. */
3267 for (last_page = i; ;last_page++)
3268 /* Check whether this is the last page in this contiguous
3269 * block. */
3270 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3271 break;
3273 proc(page_address(i), (lispobj*)((char*)page_address(last_page) +
3274 page_bytes_used(last_page)));
3275 i = last_page;
3279 static void verify_generation(generation_index_t generation)
3281 walk_generation(verify_space, generation);
3284 /* Check that all the free space is zero filled. */
3285 static void
3286 verify_zero_fill(void)
3288 page_index_t page;
3290 for (page = 0; page < last_free_page; page++) {
3291 if (page_free_p(page)) {
3292 /* The whole page should be zero filled. */
3293 sword_t *start_addr = (sword_t *)page_address(page);
3294 sword_t i;
3295 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3296 if (start_addr[i] != 0) {
3297 lose("free page not zero at %x\n", start_addr + i);
3300 } else {
3301 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3302 if (free_bytes > 0) {
3303 sword_t *start_addr = (sword_t *)((uword_t)page_address(page)
3304 + page_bytes_used(page));
3305 sword_t size = free_bytes / N_WORD_BYTES;
3306 sword_t i;
3307 for (i = 0; i < size; i++) {
3308 if (start_addr[i] != 0) {
3309 lose("free region not zero at %x\n", start_addr + i);
3317 /* External entry point for verify_zero_fill */
3318 void
3319 gencgc_verify_zero_fill(void)
3321 /* Flush the alloc regions updating the tables. */
3322 gc_alloc_update_all_page_tables(1);
3323 SHOW("verifying zero fill");
3324 verify_zero_fill();
3327 static void
3328 verify_dynamic_space(void)
3330 verify_generation(-1);
3331 if (gencgc_enable_verify_zero_fill)
3332 verify_zero_fill();
3335 /* Write-protect all the dynamic boxed pages in the given generation. */
3336 static void
3337 write_protect_generation_pages(generation_index_t generation)
3339 page_index_t start;
3341 gc_assert(generation < SCRATCH_GENERATION);
3343 for (start = 0; start < last_free_page; start++) {
3344 if (protect_page_p(start, generation)) {
3345 void *page_start;
3346 page_index_t last;
3348 /* Note the page as protected in the page tables. */
3349 page_table[start].write_protected = 1;
3351 for (last = start + 1; last < last_free_page; last++) {
3352 if (!protect_page_p(last, generation))
3353 break;
3354 page_table[last].write_protected = 1;
3357 page_start = (void *)page_address(start);
3359 os_protect(page_start,
3360 npage_bytes(last - start),
3361 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3363 start = last;
3367 if (gencgc_verbose > 1) {
3368 FSHOW((stderr,
3369 "/write protected %d of %d pages in generation %d\n",
3370 count_write_protect_generation_pages(generation),
3371 count_generation_pages(generation),
3372 generation));
3376 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3377 static void
3378 preserve_context_registers (os_context_t *c)
3380 void **ptr;
3381 /* On Darwin the signal context isn't a contiguous block of memory,
3382 * so just preserve_pointering its contents won't be sufficient.
3384 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3385 #if defined LISP_FEATURE_X86
3386 preserve_pointer((void*)*os_context_register_addr(c,reg_EAX));
3387 preserve_pointer((void*)*os_context_register_addr(c,reg_ECX));
3388 preserve_pointer((void*)*os_context_register_addr(c,reg_EDX));
3389 preserve_pointer((void*)*os_context_register_addr(c,reg_EBX));
3390 preserve_pointer((void*)*os_context_register_addr(c,reg_ESI));
3391 preserve_pointer((void*)*os_context_register_addr(c,reg_EDI));
3392 preserve_pointer((void*)*os_context_pc_addr(c));
3393 #elif defined LISP_FEATURE_X86_64
3394 preserve_pointer((void*)*os_context_register_addr(c,reg_RAX));
3395 preserve_pointer((void*)*os_context_register_addr(c,reg_RCX));
3396 preserve_pointer((void*)*os_context_register_addr(c,reg_RDX));
3397 preserve_pointer((void*)*os_context_register_addr(c,reg_RBX));
3398 preserve_pointer((void*)*os_context_register_addr(c,reg_RSI));
3399 preserve_pointer((void*)*os_context_register_addr(c,reg_RDI));
3400 preserve_pointer((void*)*os_context_register_addr(c,reg_R8));
3401 preserve_pointer((void*)*os_context_register_addr(c,reg_R9));
3402 preserve_pointer((void*)*os_context_register_addr(c,reg_R10));
3403 preserve_pointer((void*)*os_context_register_addr(c,reg_R11));
3404 preserve_pointer((void*)*os_context_register_addr(c,reg_R12));
3405 preserve_pointer((void*)*os_context_register_addr(c,reg_R13));
3406 preserve_pointer((void*)*os_context_register_addr(c,reg_R14));
3407 preserve_pointer((void*)*os_context_register_addr(c,reg_R15));
3408 preserve_pointer((void*)*os_context_pc_addr(c));
3409 #else
3410 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3411 #endif
3412 #endif
3413 #if !defined(LISP_FEATURE_WIN32)
3414 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3415 preserve_pointer(*ptr);
3417 #endif
3419 #endif
3421 static void
3422 move_pinned_pages_to_newspace()
3424 page_index_t i;
3426 /* scavenge() will evacuate all oldspace pages, but no newspace
3427 * pages. Pinned pages are precisely those pages which must not
3428 * be evacuated, so move them to newspace directly. */
3430 for (i = 0; i < last_free_page; i++) {
3431 if (page_table[i].dont_move &&
3432 /* dont_move is cleared lazily, so validate the space as well. */
3433 page_table[i].gen == from_space) {
3434 if (do_wipe_p && page_table[i].has_pins) {
3435 // do not move to newspace after all, this will be word-wiped
3436 continue;
3438 page_table[i].gen = new_space;
3439 /* And since we're moving the pages wholesale, also adjust
3440 * the generation allocation counters. */
3441 int used = page_bytes_used(i);
3442 generations[new_space].bytes_allocated += used;
3443 generations[from_space].bytes_allocated -= used;
3448 /* Garbage collect a generation. If raise is 0 then the remains of the
3449 * generation are not raised to the next generation. */
3450 static void
3451 garbage_collect_generation(generation_index_t generation, int raise)
3453 page_index_t i;
3454 struct thread *th;
3456 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3458 /* The oldest generation can't be raised. */
3459 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3461 /* Check if weak hash tables were processed in the previous GC. */
3462 gc_assert(weak_hash_tables == NULL);
3464 /* Initialize the weak pointer list. */
3465 weak_pointers = NULL;
3467 /* When a generation is not being raised it is transported to a
3468 * temporary generation (NUM_GENERATIONS), and lowered when
3469 * done. Set up this new generation. There should be no pages
3470 * allocated to it yet. */
3471 if (!raise) {
3472 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3475 /* Set the global src and dest. generations */
3476 from_space = generation;
3477 if (raise)
3478 new_space = generation+1;
3479 else
3480 new_space = SCRATCH_GENERATION;
3482 /* Change to a new space for allocation, resetting the alloc_start_page */
3483 gc_alloc_generation = new_space;
3484 generations[new_space].alloc_start_page = 0;
3485 generations[new_space].alloc_unboxed_start_page = 0;
3486 generations[new_space].alloc_large_start_page = 0;
3487 generations[new_space].alloc_large_unboxed_start_page = 0;
3489 hopscotch_reset(&pinned_objects);
3490 /* Before any pointers are preserved, the dont_move flags on the
3491 * pages need to be cleared. */
3492 /* FIXME: consider moving this bitmap into its own range of words,
3493 * out of the page table. Then we can just bzero() it.
3494 * This will also obviate the extra test at the comment
3495 * "dont_move is cleared lazily" in move_pinned_pages_to_newspace().
3497 for (i = 0; i < last_free_page; i++)
3498 if(page_table[i].gen==from_space) {
3499 page_table[i].dont_move = 0;
3502 /* Un-write-protect the old-space pages. This is essential for the
3503 * promoted pages as they may contain pointers into the old-space
3504 * which need to be scavenged. It also helps avoid unnecessary page
3505 * faults as forwarding pointers are written into them. They need to
3506 * be un-protected anyway before unmapping later. */
3507 unprotect_oldspace();
3509 /* Scavenge the stacks' conservative roots. */
3511 /* there are potentially two stacks for each thread: the main
3512 * stack, which may contain Lisp pointers, and the alternate stack.
3513 * We don't ever run Lisp code on the altstack, but it may
3514 * host a sigcontext with lisp objects in it */
3516 /* what we need to do: (1) find the stack pointer for the main
3517 * stack; scavenge it (2) find the interrupt context on the
3518 * alternate stack that might contain lisp values, and scavenge
3519 * that */
3521 /* we assume that none of the preceding applies to the thread that
3522 * initiates GC. If you ever call GC from inside an altstack
3523 * handler, you will lose. */
3525 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3526 /* And if we're saving a core, there's no point in being conservative. */
3527 if (conservative_stack) {
3528 for_each_thread(th) {
3529 void **ptr;
3530 void **esp=(void **)-1;
3531 if (th->state == STATE_DEAD)
3532 continue;
3533 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3534 /* Conservative collect_garbage is always invoked with a
3535 * foreign C call or an interrupt handler on top of every
3536 * existing thread, so the stored SP in each thread
3537 * structure is valid, no matter which thread we are looking
3538 * at. For threads that were running Lisp code, the pitstop
3539 * and edge functions maintain this value within the
3540 * interrupt or exception handler. */
3541 esp = os_get_csp(th);
3542 assert_on_stack(th, esp);
3544 /* In addition to pointers on the stack, also preserve the
3545 * return PC, the only value from the context that we need
3546 * in addition to the SP. The return PC gets saved by the
3547 * foreign call wrapper, and removed from the control stack
3548 * into a register. */
3549 preserve_pointer(th->pc_around_foreign_call);
3551 /* And on platforms with interrupts: scavenge ctx registers. */
3553 /* Disabled on Windows, because it does not have an explicit
3554 * stack of `interrupt_contexts'. The reported CSP has been
3555 * chosen so that the current context on the stack is
3556 * covered by the stack scan. See also set_csp_from_context(). */
3557 # ifndef LISP_FEATURE_WIN32
3558 if (th != arch_os_get_current_thread()) {
3559 long k = fixnum_value(
3560 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3561 while (k > 0)
3562 preserve_context_registers(th->interrupt_contexts[--k]);
3564 # endif
3565 # elif defined(LISP_FEATURE_SB_THREAD)
3566 sword_t i,free;
3567 if(th==arch_os_get_current_thread()) {
3568 /* Somebody is going to burn in hell for this, but casting
3569 * it in two steps shuts gcc up about strict aliasing. */
3570 esp = (void **)((void *)&raise);
3571 } else {
3572 void **esp1;
3573 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3574 for(i=free-1;i>=0;i--) {
3575 os_context_t *c=th->interrupt_contexts[i];
3576 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3577 if (esp1>=(void **)th->control_stack_start &&
3578 esp1<(void **)th->control_stack_end) {
3579 if(esp1<esp) esp=esp1;
3580 preserve_context_registers(c);
3584 # else
3585 esp = (void **)((void *)&raise);
3586 # endif
3587 if (!esp || esp == (void*) -1)
3588 lose("garbage_collect: no SP known for thread %x (OS %x)",
3589 th, th->os_thread);
3590 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3591 preserve_pointer(*ptr);
3595 #else
3596 /* Non-x86oid systems don't have "conservative roots" as such, but
3597 * the same mechanism is used for objects pinned for use by alien
3598 * code. */
3599 for_each_thread(th) {
3600 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3601 while (pin_list != NIL) {
3602 struct cons *list_entry =
3603 (struct cons *)native_pointer(pin_list);
3604 preserve_pointer((void*)list_entry->car);
3605 pin_list = list_entry->cdr;
3608 #endif
3610 #if QSHOW
3611 if (gencgc_verbose > 1) {
3612 sword_t num_dont_move_pages = count_dont_move_pages();
3613 fprintf(stderr,
3614 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3615 num_dont_move_pages,
3616 npage_bytes(num_dont_move_pages));
3618 #endif
3620 /* Now that all of the pinned (dont_move) pages are known, and
3621 * before we start to scavenge (and thus relocate) objects,
3622 * relocate the pinned pages to newspace, so that the scavenger
3623 * will not attempt to relocate their contents. */
3624 move_pinned_pages_to_newspace();
3626 /* Scavenge all the rest of the roots. */
3628 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3630 * If not x86, we need to scavenge the interrupt context(s) and the
3631 * control stack.
3634 struct thread *th;
3635 for_each_thread(th) {
3636 scavenge_interrupt_contexts(th);
3637 scavenge_control_stack(th);
3640 # ifdef LISP_FEATURE_SB_SAFEPOINT
3641 /* In this case, scrub all stacks right here from the GCing thread
3642 * instead of doing what the comment below says. Suboptimal, but
3643 * easier. */
3644 for_each_thread(th)
3645 scrub_thread_control_stack(th);
3646 # else
3647 /* Scrub the unscavenged control stack space, so that we can't run
3648 * into any stale pointers in a later GC (this is done by the
3649 * stop-for-gc handler in the other threads). */
3650 scrub_control_stack();
3651 # endif
3653 #endif
3655 /* Scavenge the Lisp functions of the interrupt handlers, taking
3656 * care to avoid SIG_DFL and SIG_IGN. */
3657 for (i = 0; i < NSIG; i++) {
3658 union interrupt_handler handler = interrupt_handlers[i];
3659 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3660 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3661 scavenge((lispobj *)(interrupt_handlers + i), 1);
3664 /* Scavenge the binding stacks. */
3666 struct thread *th;
3667 for_each_thread(th) {
3668 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3669 th->binding_stack_start;
3670 scavenge((lispobj *) th->binding_stack_start,len);
3671 #ifdef LISP_FEATURE_SB_THREAD
3672 /* do the tls as well */
3673 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3674 (sizeof (struct thread))/(sizeof (lispobj));
3675 scavenge((lispobj *) (th+1),len);
3676 #endif
3680 /* Scavenge static space. */
3681 if (gencgc_verbose > 1) {
3682 FSHOW((stderr,
3683 "/scavenge static space: %d bytes\n",
3684 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3686 heap_scavenge((lispobj*)STATIC_SPACE_START,
3687 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3689 /* All generations but the generation being GCed need to be
3690 * scavenged. The new_space generation needs special handling as
3691 * objects may be moved in - it is handled separately below. */
3692 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3693 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3694 #endif
3695 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3697 scavenge_pinned_ranges();
3699 /* Finally scavenge the new_space generation. Keep going until no
3700 * more objects are moved into the new generation */
3701 scavenge_newspace_generation(new_space);
3703 /* FIXME: I tried reenabling this check when debugging unrelated
3704 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3705 * Since the current GC code seems to work well, I'm guessing that
3706 * this debugging code is just stale, but I haven't tried to
3707 * figure it out. It should be figured out and then either made to
3708 * work or just deleted. */
3710 #define RESCAN_CHECK 0
3711 #if RESCAN_CHECK
3712 /* As a check re-scavenge the newspace once; no new objects should
3713 * be found. */
3715 os_vm_size_t old_bytes_allocated = bytes_allocated;
3716 os_vm_size_t bytes_allocated;
3718 /* Start with a full scavenge. */
3719 scavenge_newspace_generation_one_scan(new_space);
3721 /* Flush the current regions, updating the tables. */
3722 gc_alloc_update_all_page_tables(1);
3724 bytes_allocated = bytes_allocated - old_bytes_allocated;
3726 if (bytes_allocated != 0) {
3727 lose("Rescan of new_space allocated %d more bytes.\n",
3728 bytes_allocated);
3731 #endif
3733 scan_weak_hash_tables();
3734 scan_weak_pointers();
3735 wipe_nonpinned_words();
3736 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3737 // Do this last, because until wipe_nonpinned_words() happens,
3738 // not all page table entries have the 'gen' value updated,
3739 // which we need to correctly find all old->young pointers.
3740 sweep_immobile_space(raise);
3741 #endif
3743 /* Flush the current regions, updating the tables. */
3744 gc_alloc_update_all_page_tables(0);
3745 hopscotch_log_stats(&pinned_objects);
3747 /* Free the pages in oldspace, but not those marked dont_move. */
3748 free_oldspace();
3750 /* If the GC is not raising the age then lower the generation back
3751 * to its normal generation number */
3752 if (!raise) {
3753 for (i = 0; i < last_free_page; i++)
3754 if ((page_bytes_used(i) != 0)
3755 && (page_table[i].gen == SCRATCH_GENERATION))
3756 page_table[i].gen = generation;
3757 gc_assert(generations[generation].bytes_allocated == 0);
3758 generations[generation].bytes_allocated =
3759 generations[SCRATCH_GENERATION].bytes_allocated;
3760 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3763 /* Reset the alloc_start_page for generation. */
3764 generations[generation].alloc_start_page = 0;
3765 generations[generation].alloc_unboxed_start_page = 0;
3766 generations[generation].alloc_large_start_page = 0;
3767 generations[generation].alloc_large_unboxed_start_page = 0;
3769 if (generation >= verify_gens) {
3770 if (gencgc_verbose) {
3771 SHOW("verifying");
3773 verify_gc();
3776 /* Set the new gc trigger for the GCed generation. */
3777 generations[generation].gc_trigger =
3778 generations[generation].bytes_allocated
3779 + generations[generation].bytes_consed_between_gc;
3781 if (raise)
3782 generations[generation].num_gc = 0;
3783 else
3784 ++generations[generation].num_gc;
3788 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3789 sword_t
3790 update_dynamic_space_free_pointer(void)
3792 page_index_t last_page = -1, i;
3794 for (i = 0; i < last_free_page; i++)
3795 if (!page_free_p(i) && (page_bytes_used(i) != 0))
3796 last_page = i;
3798 last_free_page = last_page+1;
3800 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3801 return 0; /* dummy value: return something ... */
3804 static void
3805 remap_page_range (page_index_t from, page_index_t to)
3807 /* There's a mysterious Solaris/x86 problem with using mmap
3808 * tricks for memory zeroing. See sbcl-devel thread
3809 * "Re: patch: standalone executable redux".
3811 #if defined(LISP_FEATURE_SUNOS)
3812 zero_and_mark_pages(from, to);
3813 #else
3814 const page_index_t
3815 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3816 release_mask = release_granularity-1,
3817 end = to+1,
3818 aligned_from = (from+release_mask)&~release_mask,
3819 aligned_end = (end&~release_mask);
3821 if (aligned_from < aligned_end) {
3822 zero_pages_with_mmap(aligned_from, aligned_end-1);
3823 if (aligned_from != from)
3824 zero_and_mark_pages(from, aligned_from-1);
3825 if (aligned_end != end)
3826 zero_and_mark_pages(aligned_end, end-1);
3827 } else {
3828 zero_and_mark_pages(from, to);
3830 #endif
3833 static void
3834 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3836 page_index_t first_page, last_page;
3838 if (forcibly)
3839 return remap_page_range(from, to);
3841 for (first_page = from; first_page <= to; first_page++) {
3842 if (!page_free_p(first_page) || !page_need_to_zero(first_page))
3843 continue;
3845 last_page = first_page + 1;
3846 while (page_free_p(last_page) &&
3847 (last_page <= to) &&
3848 (page_need_to_zero(last_page)))
3849 last_page++;
3851 remap_page_range(first_page, last_page-1);
3853 first_page = last_page;
3857 generation_index_t small_generation_limit = 1;
3859 /* GC all generations newer than last_gen, raising the objects in each
3860 * to the next older generation - we finish when all generations below
3861 * last_gen are empty. Then if last_gen is due for a GC, or if
3862 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3863 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3865 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3866 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3867 void
3868 collect_garbage(generation_index_t last_gen)
3870 generation_index_t gen = 0, i;
3871 int raise, more = 0;
3872 int gen_to_wp;
3873 /* The largest value of last_free_page seen since the time
3874 * remap_free_pages was called. */
3875 static page_index_t high_water_mark = 0;
3877 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3878 log_generation_stats(gc_logfile, "=== GC Start ===");
3880 gc_active_p = 1;
3882 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3883 FSHOW((stderr,
3884 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3885 last_gen));
3886 last_gen = 0;
3889 /* Flush the alloc regions updating the tables. */
3890 gc_alloc_update_all_page_tables(1);
3892 /* Verify the new objects created by Lisp code. */
3893 if (pre_verify_gen_0) {
3894 FSHOW((stderr, "pre-checking generation 0\n"));
3895 verify_generation(0);
3898 if (gencgc_verbose > 1)
3899 print_generation_stats();
3901 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3902 /* Immobile space generation bits are lazily updated for gen0
3903 (not touched on every object allocation) so do it now */
3904 update_immobile_nursery_bits();
3905 #endif
3907 do {
3908 /* Collect the generation. */
3910 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3911 /* Never raise the oldest generation. Never raise the extra generation
3912 * collected due to more-flag. */
3913 raise = 0;
3914 more = 0;
3915 } else {
3916 raise =
3917 (gen < last_gen)
3918 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3919 /* If we would not normally raise this one, but we're
3920 * running low on space in comparison to the object-sizes
3921 * we've been seeing, raise it and collect the next one
3922 * too. */
3923 if (!raise && gen == last_gen) {
3924 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3925 raise = more;
3929 if (gencgc_verbose > 1) {
3930 FSHOW((stderr,
3931 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3932 gen,
3933 raise,
3934 generations[gen].bytes_allocated,
3935 generations[gen].gc_trigger,
3936 generations[gen].num_gc));
3939 /* If an older generation is being filled, then update its
3940 * memory age. */
3941 if (raise == 1) {
3942 generations[gen+1].cum_sum_bytes_allocated +=
3943 generations[gen+1].bytes_allocated;
3946 garbage_collect_generation(gen, raise);
3948 /* Reset the memory age cum_sum. */
3949 generations[gen].cum_sum_bytes_allocated = 0;
3951 if (gencgc_verbose > 1) {
3952 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3953 print_generation_stats();
3956 gen++;
3957 } while ((gen <= gencgc_oldest_gen_to_gc)
3958 && ((gen < last_gen)
3959 || more
3960 || (raise
3961 && (generations[gen].bytes_allocated
3962 > generations[gen].gc_trigger)
3963 && (generation_average_age(gen)
3964 > generations[gen].minimum_age_before_gc))));
3966 /* Now if gen-1 was raised all generations before gen are empty.
3967 * If it wasn't raised then all generations before gen-1 are empty.
3969 * Now objects within this gen's pages cannot point to younger
3970 * generations unless they are written to. This can be exploited
3971 * by write-protecting the pages of gen; then when younger
3972 * generations are GCed only the pages which have been written
3973 * need scanning. */
3974 if (raise)
3975 gen_to_wp = gen;
3976 else
3977 gen_to_wp = gen - 1;
3979 /* There's not much point in WPing pages in generation 0 as it is
3980 * never scavenged (except promoted pages). */
3981 if ((gen_to_wp > 0) && enable_page_protection) {
3982 /* Check that they are all empty. */
3983 for (i = 0; i < gen_to_wp; i++) {
3984 if (generations[i].bytes_allocated)
3985 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3986 gen_to_wp, i);
3988 write_protect_generation_pages(gen_to_wp);
3990 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3991 write_protect_immobile_space();
3992 #endif
3994 /* Set gc_alloc() back to generation 0. The current regions should
3995 * be flushed after the above GCs. */
3996 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3997 gc_alloc_generation = 0;
3999 /* Save the high-water mark before updating last_free_page */
4000 if (last_free_page > high_water_mark)
4001 high_water_mark = last_free_page;
4003 update_dynamic_space_free_pointer();
4005 /* Update auto_gc_trigger. Make sure we trigger the next GC before
4006 * running out of heap! */
4007 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
4008 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
4009 else
4010 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
4012 if(gencgc_verbose)
4013 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
4014 auto_gc_trigger);
4016 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4017 * back to the OS.
4019 if (gen > small_generation_limit) {
4020 if (last_free_page > high_water_mark)
4021 high_water_mark = last_free_page;
4022 remap_free_pages(0, high_water_mark, 0);
4023 high_water_mark = 0;
4026 gc_active_p = 0;
4027 large_allocation = 0;
4029 log_generation_stats(gc_logfile, "=== GC End ===");
4030 SHOW("returning from collect_garbage");
4033 void
4034 gc_init(void)
4036 page_index_t i;
4038 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4039 alloc_gc_page();
4040 #endif
4042 /* Compute the number of pages needed for the dynamic space.
4043 * Dynamic space size should be aligned on page size. */
4044 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
4045 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4047 /* Default nursery size to 5% of the total dynamic space size,
4048 * min 1Mb. */
4049 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
4050 if (bytes_consed_between_gcs < (1024*1024))
4051 bytes_consed_between_gcs = 1024*1024;
4053 /* The page_table must be allocated using "calloc" to initialize
4054 * the page structures correctly. There used to be a separate
4055 * initialization loop (now commented out; see below) but that was
4056 * unnecessary and did hurt startup time. */
4057 page_table = calloc(page_table_pages, sizeof(struct page));
4058 gc_assert(page_table);
4059 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4060 gc_init_immobile();
4061 #endif
4063 hopscotch_init();
4064 hopscotch_create(&pinned_objects, 0 /* no values */,
4065 32 /* logical bin count */, 0 /* default range */);
4067 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4068 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4070 /* The page structures are initialized implicitly when page_table
4071 * is allocated with "calloc" above. Formerly we had the following
4072 * explicit initialization here (comments converted to C99 style
4073 * for readability as C's block comments don't nest):
4075 * // Initialize each page structure.
4076 * for (i = 0; i < page_table_pages; i++) {
4077 * // Initialize all pages as free.
4078 * page_table[i].allocated = FREE_PAGE_FLAG;
4079 * page_table[i].bytes_used = 0;
4081 * // Pages are not write-protected at startup.
4082 * page_table[i].write_protected = 0;
4085 * Without this loop the image starts up much faster when dynamic
4086 * space is large -- which it is on 64-bit platforms already by
4087 * default -- and when "calloc" for large arrays is implemented
4088 * using copy-on-write of a page of zeroes -- which it is at least
4089 * on Linux. In this case the pages that page_table_pages is stored
4090 * in are mapped and cleared not before the corresponding part of
4091 * dynamic space is used. For example, this saves clearing 16 MB of
4092 * memory at startup if the page size is 4 KB and the size of
4093 * dynamic space is 4 GB.
4094 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4095 * asserted below: */
4097 /* Compile time assertion: If triggered, declares an array
4098 * of dimension -1 forcing a syntax error. The intent of the
4099 * assignment is to avoid an "unused variable" warning. */
4100 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4101 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4104 bytes_allocated = 0;
4106 /* Initialize the generations. */
4107 for (i = 0; i < NUM_GENERATIONS; i++) {
4108 generations[i].alloc_start_page = 0;
4109 generations[i].alloc_unboxed_start_page = 0;
4110 generations[i].alloc_large_start_page = 0;
4111 generations[i].alloc_large_unboxed_start_page = 0;
4112 generations[i].bytes_allocated = 0;
4113 generations[i].gc_trigger = 2000000;
4114 generations[i].num_gc = 0;
4115 generations[i].cum_sum_bytes_allocated = 0;
4116 /* the tune-able parameters */
4117 generations[i].bytes_consed_between_gc
4118 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4119 generations[i].number_of_gcs_before_promotion = 1;
4120 generations[i].minimum_age_before_gc = 0.75;
4123 /* Initialize gc_alloc. */
4124 gc_alloc_generation = 0;
4125 gc_set_region_empty(&boxed_region);
4126 gc_set_region_empty(&unboxed_region);
4128 last_free_page = 0;
4131 /* Pick up the dynamic space from after a core load.
4133 * The ALLOCATION_POINTER points to the end of the dynamic space.
4136 static void
4137 gencgc_pickup_dynamic(void)
4139 page_index_t page = 0;
4140 void *alloc_ptr = (void *)get_alloc_pointer();
4141 lispobj *prev=(lispobj *)page_address(page);
4142 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4144 bytes_allocated = 0;
4146 do {
4147 lispobj *first,*ptr= (lispobj *)page_address(page);
4149 if (!gencgc_partial_pickup || !page_free_p(page)) {
4150 /* It is possible, though rare, for the saved page table
4151 * to contain free pages below alloc_ptr. */
4152 page_table[page].gen = gen;
4153 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4154 page_table[page].large_object = 0;
4155 page_table[page].write_protected = 0;
4156 page_table[page].write_protected_cleared = 0;
4157 page_table[page].dont_move = 0;
4158 set_page_need_to_zero(page, 1);
4160 bytes_allocated += GENCGC_CARD_BYTES;
4163 if (!gencgc_partial_pickup) {
4164 page_table[page].allocated = BOXED_PAGE_FLAG;
4165 first = gc_search_space3(ptr, prev, (ptr+2));
4166 if(ptr == first)
4167 prev=ptr;
4168 set_page_scan_start_offset(page,
4169 page_address(page) - (void *)prev);
4171 page++;
4172 } while (page_address(page) < alloc_ptr);
4174 last_free_page = page;
4176 generations[gen].bytes_allocated = bytes_allocated;
4178 gc_alloc_update_all_page_tables(1);
4179 write_protect_generation_pages(gen);
4182 void
4183 gc_initialize_pointers(void)
4185 gencgc_pickup_dynamic();
4189 /* alloc(..) is the external interface for memory allocation. It
4190 * allocates to generation 0. It is not called from within the garbage
4191 * collector as it is only external uses that need the check for heap
4192 * size (GC trigger) and to disable the interrupts (interrupts are
4193 * always disabled during a GC).
4195 * The vops that call alloc(..) assume that the returned space is zero-filled.
4196 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4198 * The check for a GC trigger is only performed when the current
4199 * region is full, so in most cases it's not needed. */
4201 static inline lispobj *
4202 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4203 struct thread *thread)
4205 #ifndef LISP_FEATURE_WIN32
4206 lispobj alloc_signal;
4207 #endif
4208 void *new_obj;
4209 void *new_free_pointer;
4210 os_vm_size_t trigger_bytes = 0;
4212 gc_assert(nbytes > 0);
4214 /* Check for alignment allocation problems. */
4215 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4216 && ((nbytes & LOWTAG_MASK) == 0));
4218 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4219 /* Must be inside a PA section. */
4220 gc_assert(get_pseudo_atomic_atomic(thread));
4221 #endif
4223 if ((os_vm_size_t) nbytes > large_allocation)
4224 large_allocation = nbytes;
4226 /* maybe we can do this quickly ... */
4227 new_free_pointer = region->free_pointer + nbytes;
4228 if (new_free_pointer <= region->end_addr) {
4229 new_obj = (void*)(region->free_pointer);
4230 region->free_pointer = new_free_pointer;
4231 return(new_obj); /* yup */
4234 /* We don't want to count nbytes against auto_gc_trigger unless we
4235 * have to: it speeds up the tenuring of objects and slows down
4236 * allocation. However, unless we do so when allocating _very_
4237 * large objects we are in danger of exhausting the heap without
4238 * running sufficient GCs.
4240 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4241 trigger_bytes = nbytes;
4243 /* we have to go the long way around, it seems. Check whether we
4244 * should GC in the near future
4246 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4247 /* Don't flood the system with interrupts if the need to gc is
4248 * already noted. This can happen for example when SUB-GC
4249 * allocates or after a gc triggered in a WITHOUT-GCING. */
4250 if (SymbolValue(GC_PENDING,thread) == NIL) {
4251 /* set things up so that GC happens when we finish the PA
4252 * section */
4253 SetSymbolValue(GC_PENDING,T,thread);
4254 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4255 #ifdef LISP_FEATURE_SB_SAFEPOINT
4256 thread_register_gc_trigger();
4257 #else
4258 set_pseudo_atomic_interrupted(thread);
4259 #ifdef GENCGC_IS_PRECISE
4260 /* PPC calls alloc() from a trap
4261 * look up the most context if it's from a trap. */
4263 os_context_t *context =
4264 thread->interrupt_data->allocation_trap_context;
4265 maybe_save_gc_mask_and_block_deferrables
4266 (context ? os_context_sigmask_addr(context) : NULL);
4268 #else
4269 maybe_save_gc_mask_and_block_deferrables(NULL);
4270 #endif
4271 #endif
4275 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4277 #ifndef LISP_FEATURE_WIN32
4278 /* for sb-prof, and not supported on Windows yet */
4279 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4280 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4281 if ((sword_t) alloc_signal <= 0) {
4282 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4283 raise(SIGPROF);
4284 } else {
4285 SetSymbolValue(ALLOC_SIGNAL,
4286 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4287 thread);
4290 #endif
4292 return (new_obj);
4295 lispobj *
4296 general_alloc(sword_t nbytes, int page_type_flag)
4298 struct thread *thread = arch_os_get_current_thread();
4299 /* Select correct region, and call general_alloc_internal with it.
4300 * For other then boxed allocation we must lock first, since the
4301 * region is shared. */
4302 if (BOXED_PAGE_FLAG & page_type_flag) {
4303 #ifdef LISP_FEATURE_SB_THREAD
4304 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4305 #else
4306 struct alloc_region *region = &boxed_region;
4307 #endif
4308 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4309 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4310 lispobj * obj;
4311 int result;
4312 result = thread_mutex_lock(&allocation_lock);
4313 gc_assert(!result);
4314 obj = general_alloc_internal(nbytes, page_type_flag, &unboxed_region, thread);
4315 result = thread_mutex_unlock(&allocation_lock);
4316 gc_assert(!result);
4317 return obj;
4318 } else {
4319 lose("bad page type flag: %d", page_type_flag);
4323 lispobj AMD64_SYSV_ABI *
4324 alloc(sword_t nbytes)
4326 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4327 struct thread *self = arch_os_get_current_thread();
4328 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4329 if (!was_pseudo_atomic)
4330 set_pseudo_atomic_atomic(self);
4331 #else
4332 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4333 #endif
4335 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4337 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4338 if (!was_pseudo_atomic)
4339 clear_pseudo_atomic_atomic(self);
4340 #endif
4342 return result;
4346 * shared support for the OS-dependent signal handlers which
4347 * catch GENCGC-related write-protect violations
4349 void unhandled_sigmemoryfault(void* addr);
4351 /* Depending on which OS we're running under, different signals might
4352 * be raised for a violation of write protection in the heap. This
4353 * function factors out the common generational GC magic which needs
4354 * to invoked in this case, and should be called from whatever signal
4355 * handler is appropriate for the OS we're running under.
4357 * Return true if this signal is a normal generational GC thing that
4358 * we were able to handle, or false if it was abnormal and control
4359 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4361 * We have two control flags for this: one causes us to ignore faults
4362 * on unprotected pages completely, and the second complains to stderr
4363 * but allows us to continue without losing.
4365 extern boolean ignore_memoryfaults_on_unprotected_pages;
4366 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4368 extern boolean continue_after_memoryfault_on_unprotected_pages;
4369 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4372 gencgc_handle_wp_violation(void* fault_addr)
4374 page_index_t page_index = find_page_index(fault_addr);
4376 #if QSHOW_SIGNALS
4377 FSHOW((stderr,
4378 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4379 fault_addr, page_index));
4380 #endif
4382 /* Check whether the fault is within the dynamic space. */
4383 if (page_index == (-1)) {
4384 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4385 extern int immobile_space_handle_wp_violation(void*);
4386 if (immobile_space_handle_wp_violation(fault_addr))
4387 return 1;
4388 #endif
4390 /* It can be helpful to be able to put a breakpoint on this
4391 * case to help diagnose low-level problems. */
4392 unhandled_sigmemoryfault(fault_addr);
4394 /* not within the dynamic space -- not our responsibility */
4395 return 0;
4397 } else {
4398 int ret;
4399 ret = thread_mutex_lock(&free_pages_lock);
4400 gc_assert(ret == 0);
4401 if (page_table[page_index].write_protected) {
4402 /* Unprotect the page. */
4403 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4404 page_table[page_index].write_protected_cleared = 1;
4405 page_table[page_index].write_protected = 0;
4406 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4407 /* The only acceptable reason for this signal on a heap
4408 * access is that GENCGC write-protected the page.
4409 * However, if two CPUs hit a wp page near-simultaneously,
4410 * we had better not have the second one lose here if it
4411 * does this test after the first one has already set wp=0
4413 if(page_table[page_index].write_protected_cleared != 1) {
4414 void lisp_backtrace(int frames);
4415 lisp_backtrace(10);
4416 fprintf(stderr,
4417 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4418 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4419 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4420 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4421 " page.bytes_used: %u\n"
4422 " page.allocated: %d\n"
4423 " page.write_protected: %d\n"
4424 " page.write_protected_cleared: %d\n"
4425 " page.generation: %d\n",
4426 fault_addr,
4427 page_index,
4428 boxed_region.first_page,
4429 boxed_region.last_page,
4430 page_scan_start_offset(page_index),
4431 page_bytes_used(page_index),
4432 page_table[page_index].allocated,
4433 page_table[page_index].write_protected,
4434 page_table[page_index].write_protected_cleared,
4435 page_table[page_index].gen);
4436 if (!continue_after_memoryfault_on_unprotected_pages)
4437 lose("Feh.\n");
4440 ret = thread_mutex_unlock(&free_pages_lock);
4441 gc_assert(ret == 0);
4442 /* Don't worry, we can handle it. */
4443 return 1;
4446 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4447 * it's not just a case of the program hitting the write barrier, and
4448 * are about to let Lisp deal with it. It's basically just a
4449 * convenient place to set a gdb breakpoint. */
4450 void
4451 unhandled_sigmemoryfault(void *addr)
4454 static void
4455 update_thread_page_tables(struct thread *th)
4457 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4458 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4459 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4460 #endif
4463 /* GC is single-threaded and all memory allocations during a
4464 collection happen in the GC thread, so it is sufficient to update
4465 all the the page tables once at the beginning of a collection and
4466 update only page tables of the GC thread during the collection. */
4467 void gc_alloc_update_all_page_tables(int for_all_threads)
4469 /* Flush the alloc regions updating the tables. */
4470 struct thread *th;
4471 if (for_all_threads) {
4472 for_each_thread(th) {
4473 update_thread_page_tables(th);
4476 else {
4477 th = arch_os_get_current_thread();
4478 if (th) {
4479 update_thread_page_tables(th);
4482 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4483 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4486 void
4487 gc_set_region_empty(struct alloc_region *region)
4489 region->first_page = 0;
4490 region->last_page = -1;
4491 region->start_addr = page_address(0);
4492 region->free_pointer = page_address(0);
4493 region->end_addr = page_address(0);
4496 static void
4497 zero_all_free_pages()
4499 page_index_t i;
4501 for (i = 0; i < last_free_page; i++) {
4502 if (page_free_p(i)) {
4503 #ifdef READ_PROTECT_FREE_PAGES
4504 os_protect(page_address(i),
4505 GENCGC_CARD_BYTES,
4506 OS_VM_PROT_ALL);
4507 #endif
4508 zero_pages(i, i);
4513 /* Things to do before doing a final GC before saving a core (without
4514 * purify).
4516 * + Pages in large_object pages aren't moved by the GC, so we need to
4517 * unset that flag from all pages.
4518 * + The pseudo-static generation isn't normally collected, but it seems
4519 * reasonable to collect it at least when saving a core. So move the
4520 * pages to a normal generation.
4522 static void
4523 prepare_for_final_gc ()
4525 page_index_t i;
4527 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4528 extern void prepare_immobile_space_for_final_gc();
4529 prepare_immobile_space_for_final_gc ();
4530 #endif
4531 do_wipe_p = 0;
4532 for (i = 0; i < last_free_page; i++) {
4533 page_table[i].large_object = 0;
4534 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4535 int used = page_bytes_used(i);
4536 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4537 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4538 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4544 /* Do a non-conservative GC, and then save a core with the initial
4545 * function being set to the value of the static symbol
4546 * SB!VM:RESTART-LISP-FUNCTION */
4547 void
4548 gc_and_save(char *filename, boolean prepend_runtime,
4549 boolean save_runtime_options, boolean compressed,
4550 int compression_level, int application_type)
4552 FILE *file;
4553 void *runtime_bytes = NULL;
4554 size_t runtime_size;
4556 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4557 &runtime_size);
4558 if (file == NULL)
4559 return;
4561 conservative_stack = 0;
4563 /* The filename might come from Lisp, and be moved by the now
4564 * non-conservative GC. */
4565 filename = strdup(filename);
4567 /* Collect twice: once into relatively high memory, and then back
4568 * into low memory. This compacts the retained data into the lower
4569 * pages, minimizing the size of the core file.
4571 prepare_for_final_gc();
4572 gencgc_alloc_start_page = last_free_page;
4573 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4575 prepare_for_final_gc();
4576 gencgc_alloc_start_page = -1;
4577 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4579 if (prepend_runtime)
4580 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4581 application_type);
4583 /* The dumper doesn't know that pages need to be zeroed before use. */
4584 zero_all_free_pages();
4585 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4586 prepend_runtime, save_runtime_options,
4587 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4588 /* Oops. Save still managed to fail. Since we've mangled the stack
4589 * beyond hope, there's not much we can do.
4590 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4591 * going to be rather unsatisfactory too... */
4592 lose("Attempt to save core after non-conservative GC failed.\n");