Remove variable that's asserted to be 0 always.
[sbcl.git] / src / runtime / gencgc.c
blob12f0170873277218b16f3b87d71246af388825f6
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 <inttypes.h>
32 #include "sbcl.h"
33 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
34 #include "pthreads_win32.h"
35 #else
36 #include <signal.h>
37 #endif
38 #include "runtime.h"
39 #include "os.h"
40 #include "interr.h"
41 #include "globals.h"
42 #include "interrupt.h"
43 #include "validate.h"
44 #include "lispregs.h"
45 #include "arch.h"
46 #include "gc.h"
47 #include "gc-internal.h"
48 #include "thread.h"
49 #include "pseudo-atomic.h"
50 #include "alloc.h"
51 #include "genesis/gc-tables.h"
52 #include "genesis/vector.h"
53 #include "genesis/weak-pointer.h"
54 #include "genesis/fdefn.h"
55 #include "genesis/simple-fun.h"
56 #include "save.h"
57 #include "genesis/hash-table.h"
58 #include "genesis/instance.h"
59 #include "genesis/layout.h"
60 #include "gencgc.h"
61 #include "hopscotch.h"
62 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
63 #include "genesis/cons.h"
64 #endif
65 #include "forwarding-ptr.h"
67 /* forward declarations */
68 page_index_t gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t nbytes,
69 int page_type_flag);
73 * GC parameters
76 /* As usually configured, generations 0-5 are normal collected generations,
77 6 is pseudo-static (the objects in which are never moved nor reclaimed),
78 and 7 is scratch space used when collecting a generation without promotion,
79 wherein it is moved to generation 7 and back again.
81 enum {
82 SCRATCH_GENERATION = PSEUDO_STATIC_GENERATION+1,
83 NUM_GENERATIONS
86 /* Should we use page protection to help avoid the scavenging of pages
87 * that don't have pointers to younger generations? */
88 boolean enable_page_protection = 1;
90 /* Largest allocation seen since last GC. */
91 os_vm_size_t large_allocation = 0;
95 * debugging
98 /* the verbosity level. All non-error messages are disabled at level 0;
99 * and only a few rare messages are printed at level 1. */
100 #if QSHOW == 2
101 boolean gencgc_verbose = 1;
102 #else
103 boolean gencgc_verbose = 0;
104 #endif
106 /* FIXME: At some point enable the various error-checking things below
107 * and see what they say. */
109 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
110 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
111 * check. */
112 generation_index_t verify_gens = HIGHEST_NORMAL_GENERATION + 1;
114 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
115 boolean pre_verify_gen_0 = 0;
117 #ifdef LISP_FEATURE_X86
118 /* Should we check code objects for fixup errors after they are transported? */
119 boolean check_code_fixups = 0;
120 #endif
122 /* Should we check that newly allocated regions are zero filled? */
123 boolean gencgc_zero_check = 0;
125 /* Should we check that the free space is zero filled? */
126 boolean gencgc_enable_verify_zero_fill = 0;
128 /* When loading a core, don't do a full scan of the memory for the
129 * memory region boundaries. (Set to true by coreparse.c if the core
130 * contained a pagetable entry).
132 boolean gencgc_partial_pickup = 0;
134 /* If defined, free pages are read-protected to ensure that nothing
135 * accesses them.
138 /* #define READ_PROTECT_FREE_PAGES */
142 * GC structures and variables
145 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
146 os_vm_size_t bytes_allocated = 0;
147 os_vm_size_t auto_gc_trigger = 0;
149 /* the source and destination generations. These are set before a GC starts
150 * scavenging. */
151 generation_index_t from_space;
152 generation_index_t new_space;
154 /* Set to 1 when in GC */
155 boolean gc_active_p = 0;
157 /* should the GC be conservative on stack. If false (only right before
158 * saving a core), don't scan the stack / mark pages dont_move. */
159 static boolean conservative_stack = 1;
161 /* An array of page structures is allocated on gc initialization.
162 * This helps to quickly map between an address and its page structure.
163 * page_table_pages is set from the size of the dynamic space. */
164 page_index_t page_table_pages;
165 struct page *page_table;
166 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
167 struct hopscotch_table pinned_objects;
168 lispobj gc_object_watcher;
169 int gc_n_stack_pins;
170 #endif
172 /* In GC cards that have conservative pointers to them, should we wipe out
173 * dwords in there that are not used, so that they do not act as false
174 * root to other things in the heap from then on? This is a new feature
175 * but in testing it is both reliable and no noticeable slowdown. */
176 int do_wipe_p = 1;
178 /// Constants defined in gc-internal:
179 /// #define BOXED_PAGE_FLAG 1
180 /// #define UNBOXED_PAGE_FLAG 2
181 /// #define OPEN_REGION_PAGE_FLAG 4
183 /// Return true if 'allocated' bits are: {001, 010, 011}, false if 1zz or 000.
184 static inline boolean page_allocated_no_region_p(page_index_t page) {
185 return (page_table[page].allocated ^ OPEN_REGION_PAGE_FLAG) > OPEN_REGION_PAGE_FLAG;
188 static inline boolean page_free_p(page_index_t page) {
189 return (page_table[page].allocated == FREE_PAGE_FLAG);
192 static inline boolean page_boxed_p(page_index_t page) {
193 return (page_table[page].allocated & BOXED_PAGE_FLAG);
196 /// Return true if 'allocated' bits are: {001, 011}, false otherwise.
197 /// i.e. true of pages which could hold boxed or partially boxed objects.
198 static inline boolean page_boxed_no_region_p(page_index_t page) {
199 return (page_table[page].allocated & 5) == BOXED_PAGE_FLAG;
202 /// Return true if page MUST NOT hold boxed objects (including code).
203 static inline boolean page_unboxed_p(page_index_t page) {
204 /* Both flags set == boxed code page */
205 return (page_table[page].allocated & 3) == UNBOXED_PAGE_FLAG;
208 static inline boolean protect_page_p(page_index_t page, generation_index_t generation) {
209 return (page_boxed_no_region_p(page)
210 && (page_bytes_used(page) != 0)
211 && !page_table[page].dont_move
212 && (page_table[page].gen == generation));
215 /* Calculate the start address for the given page number. */
216 inline void *
217 page_address(page_index_t page_num)
219 return (void*)(DYNAMIC_SPACE_START + (page_num * GENCGC_CARD_BYTES));
222 /* Calculate the address where the allocation region associated with
223 * the page starts. */
224 static inline void *
225 page_scan_start(page_index_t page_index)
227 return page_address(page_index)-page_scan_start_offset(page_index);
230 /* True if the page starts a contiguous block. */
231 static inline boolean
232 page_starts_contiguous_block_p(page_index_t page_index)
234 // Don't use the preprocessor macro: 0 means 0.
235 return page_table[page_index].scan_start_offset_ == 0;
238 /* True if the page is the last page in a contiguous block. */
239 static inline boolean
240 page_ends_contiguous_block_p(page_index_t page_index, generation_index_t gen)
242 return (/* page doesn't fill block */
243 (page_bytes_used(page_index) < GENCGC_CARD_BYTES)
244 /* page is last allocated page */
245 || ((page_index + 1) >= last_free_page)
246 /* next page free */
247 || page_free_p(page_index + 1)
248 /* next page contains no data */
249 || (page_bytes_used(page_index + 1) == 0)
250 /* next page is in different generation */
251 || (page_table[page_index + 1].gen != gen)
252 /* next page starts its own contiguous block */
253 || (page_starts_contiguous_block_p(page_index + 1)));
256 /// External function for calling from Lisp.
257 page_index_t ext_find_page_index(void *addr) { return find_page_index(addr); }
259 static os_vm_size_t
260 npage_bytes(page_index_t npages)
262 gc_assert(npages>=0);
263 return ((os_vm_size_t)npages)*GENCGC_CARD_BYTES;
266 /* Check that X is a higher address than Y and return offset from Y to
267 * X in bytes. */
268 static inline os_vm_size_t
269 addr_diff(void *x, void *y)
271 gc_assert(x >= y);
272 return (uintptr_t)x - (uintptr_t)y;
275 /* a structure to hold the state of a generation
277 * CAUTION: If you modify this, make sure to touch up the alien
278 * definition in src/code/gc.lisp accordingly. ...or better yes,
279 * deal with the FIXME there...
281 struct generation {
283 /* the first page that gc_alloc() checks on its next call */
284 page_index_t alloc_start_page;
286 /* the first page that gc_alloc_unboxed() checks on its next call */
287 page_index_t alloc_unboxed_start_page;
289 /* the first page that gc_alloc_large (boxed) considers on its next
290 * call. (Although it always allocates after the boxed_region.) */
291 page_index_t alloc_large_start_page;
293 /* the first page that gc_alloc_large (unboxed) considers on its
294 * next call. (Although it always allocates after the
295 * current_unboxed_region.) */
296 page_index_t alloc_large_unboxed_start_page;
298 /* the bytes allocated to this generation */
299 os_vm_size_t bytes_allocated;
301 /* the number of bytes at which to trigger a GC */
302 os_vm_size_t gc_trigger;
304 /* to calculate a new level for gc_trigger */
305 os_vm_size_t bytes_consed_between_gc;
307 /* the number of GCs since the last raise */
308 int num_gc;
310 /* the number of GCs to run on the generations before raising objects to the
311 * next generation */
312 int number_of_gcs_before_promotion;
314 /* the cumulative sum of the bytes allocated to this generation. It is
315 * cleared after a GC on this generations, and update before new
316 * objects are added from a GC of a younger generation. Dividing by
317 * the bytes_allocated will give the average age of the memory in
318 * this generation since its last GC. */
319 os_vm_size_t cum_sum_bytes_allocated;
321 /* a minimum average memory age before a GC will occur helps
322 * prevent a GC when a large number of new live objects have been
323 * added, in which case a GC could be a waste of time */
324 double minimum_age_before_gc;
327 /* an array of generation structures. There needs to be one more
328 * generation structure than actual generations as the oldest
329 * generation is temporarily raised then lowered. */
330 struct generation generations[NUM_GENERATIONS];
332 /* the oldest generation that is will currently be GCed by default.
333 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
335 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
337 * Setting this to 0 effectively disables the generational nature of
338 * the GC. In some applications generational GC may not be useful
339 * because there are no long-lived objects.
341 * An intermediate value could be handy after moving long-lived data
342 * into an older generation so an unnecessary GC of this long-lived
343 * data can be avoided. */
344 generation_index_t gencgc_oldest_gen_to_gc = HIGHEST_NORMAL_GENERATION;
346 /* META: Is nobody aside from me bothered by this especially misleading
347 * use of the word "last"? It could mean either "ultimate" or "prior",
348 * but in fact means neither. It is the *FIRST* page that should be grabbed
349 * for more space, so it is min free page, or 1+ the max used page. */
350 /* The maximum free page in the heap is maintained and used to update
351 * ALLOCATION_POINTER which is used by the room function to limit its
352 * search of the heap. XX Gencgc obviously needs to be better
353 * integrated with the Lisp code. */
355 page_index_t last_free_page;
357 #ifdef LISP_FEATURE_SB_THREAD
358 /* This lock is to prevent multiple threads from simultaneously
359 * allocating new regions which overlap each other. Note that the
360 * majority of GC is single-threaded, but alloc() may be called from
361 * >1 thread at a time and must be thread-safe. This lock must be
362 * seized before all accesses to generations[] or to parts of
363 * page_table[] that other threads may want to see */
364 static pthread_mutex_t free_pages_lock = PTHREAD_MUTEX_INITIALIZER;
365 /* This lock is used to protect non-thread-local allocation. */
366 static pthread_mutex_t allocation_lock = PTHREAD_MUTEX_INITIALIZER;
367 #endif
369 extern os_vm_size_t gencgc_release_granularity;
370 os_vm_size_t gencgc_release_granularity = GENCGC_RELEASE_GRANULARITY;
372 extern os_vm_size_t gencgc_alloc_granularity;
373 os_vm_size_t gencgc_alloc_granularity = GENCGC_ALLOC_GRANULARITY;
377 * miscellaneous heap functions
380 /* Count the number of pages which are write-protected within the
381 * given generation. */
382 static page_index_t
383 count_write_protect_generation_pages(generation_index_t generation)
385 page_index_t i, count = 0;
387 for (i = 0; i < last_free_page; i++)
388 if (!page_free_p(i)
389 && (page_table[i].gen == generation)
390 && (page_table[i].write_protected == 1))
391 count++;
392 return count;
395 /* Count the number of pages within the given generation. */
396 static page_index_t
397 count_generation_pages(generation_index_t generation)
399 page_index_t i;
400 page_index_t count = 0;
402 for (i = 0; i < last_free_page; i++)
403 if (!page_free_p(i)
404 && (page_table[i].gen == generation))
405 count++;
406 return count;
409 #if QSHOW
410 static page_index_t
411 count_dont_move_pages(void)
413 page_index_t i;
414 page_index_t count = 0;
415 for (i = 0; i < last_free_page; i++) {
416 if (!page_free_p(i)
417 && (page_table[i].dont_move != 0)) {
418 ++count;
421 return count;
423 #endif /* QSHOW */
425 /* Work through the pages and add up the number of bytes used for the
426 * given generation. */
427 static __attribute__((unused)) os_vm_size_t
428 count_generation_bytes_allocated (generation_index_t gen)
430 page_index_t i;
431 os_vm_size_t result = 0;
432 for (i = 0; i < last_free_page; i++) {
433 if (!page_free_p(i)
434 && (page_table[i].gen == gen))
435 result += page_bytes_used(i);
437 return result;
440 /* Return the average age of the memory in a generation. */
441 extern double
442 generation_average_age(generation_index_t gen)
444 if (generations[gen].bytes_allocated == 0)
445 return 0.0;
447 return
448 ((double)generations[gen].cum_sum_bytes_allocated)
449 / ((double)generations[gen].bytes_allocated);
452 #ifdef LISP_FEATURE_X86
453 extern void fpu_save(void *);
454 extern void fpu_restore(void *);
455 #endif
457 #define PAGE_INDEX_FMT PRIdPTR
459 extern void
460 write_generation_stats(FILE *file)
462 generation_index_t i;
464 #ifdef LISP_FEATURE_X86
465 int fpu_state[27];
467 /* Can end up here after calling alloc_tramp which doesn't prepare
468 * the x87 state, and the C ABI uses a different mode */
469 fpu_save(fpu_state);
470 #endif
472 /* Print the heap stats. */
473 fprintf(file,
474 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
476 for (i = 0; i <= SCRATCH_GENERATION; i++) {
477 page_index_t j;
478 page_index_t boxed_cnt = 0;
479 page_index_t unboxed_cnt = 0;
480 page_index_t large_boxed_cnt = 0;
481 page_index_t large_unboxed_cnt = 0;
482 page_index_t pinned_cnt=0;
484 for (j = 0; j < last_free_page; j++)
485 if (page_table[j].gen == i) {
487 /* Count the number of boxed pages within the given
488 * generation. */
489 if (page_boxed_p(j)) {
490 if (page_table[j].large_object)
491 large_boxed_cnt++;
492 else
493 boxed_cnt++;
495 if(page_table[j].dont_move) pinned_cnt++;
496 /* Count the number of unboxed pages within the given
497 * generation. */
498 if (page_unboxed_p(j)) {
499 if (page_table[j].large_object)
500 large_unboxed_cnt++;
501 else
502 unboxed_cnt++;
506 gc_assert(generations[i].bytes_allocated
507 == count_generation_bytes_allocated(i));
508 fprintf(file,
509 " %1d: %5ld %5ld %5ld %5ld",
511 (long)generations[i].alloc_start_page,
512 (long)generations[i].alloc_unboxed_start_page,
513 (long)generations[i].alloc_large_start_page,
514 (long)generations[i].alloc_large_unboxed_start_page);
515 fprintf(file,
516 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT
517 " %5"PAGE_INDEX_FMT" %5"PAGE_INDEX_FMT,
518 boxed_cnt, unboxed_cnt, large_boxed_cnt,
519 large_unboxed_cnt, pinned_cnt);
520 fprintf(file,
521 " %8"OS_VM_SIZE_FMT
522 " %5"OS_VM_SIZE_FMT
523 " %8"OS_VM_SIZE_FMT
524 " %4"PAGE_INDEX_FMT" %3d %7.4f\n",
525 generations[i].bytes_allocated,
526 (npage_bytes(count_generation_pages(i)) - generations[i].bytes_allocated),
527 generations[i].gc_trigger,
528 count_write_protect_generation_pages(i),
529 generations[i].num_gc,
530 generation_average_age(i));
532 fprintf(file," Total bytes allocated = %"OS_VM_SIZE_FMT"\n", bytes_allocated);
533 fprintf(file," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT"\n", dynamic_space_size);
535 #ifdef LISP_FEATURE_X86
536 fpu_restore(fpu_state);
537 #endif
540 extern void
541 write_heap_exhaustion_report(FILE *file, long available, long requested,
542 struct thread *thread)
544 fprintf(file,
545 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
546 gc_active_p ? "garbage collection" : "allocation",
547 available,
548 requested);
549 write_generation_stats(file);
550 fprintf(file, "GC control variables:\n");
551 fprintf(file, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
552 SymbolValue(GC_INHIBIT,thread)==NIL ? "false" : "true",
553 (SymbolValue(GC_PENDING, thread) == T) ?
554 "true" : ((SymbolValue(GC_PENDING, thread) == NIL) ?
555 "false" : "in progress"));
556 #ifdef LISP_FEATURE_SB_THREAD
557 fprintf(file, " *STOP-FOR-GC-PENDING* = %s\n",
558 SymbolValue(STOP_FOR_GC_PENDING,thread)==NIL ? "false" : "true");
559 #endif
562 extern void
563 print_generation_stats(void)
565 write_generation_stats(stderr);
568 extern char* gc_logfile;
569 char * gc_logfile = NULL;
571 extern void
572 log_generation_stats(char *logfile, char *header)
574 if (logfile) {
575 FILE * log = fopen(logfile, "a");
576 if (log) {
577 fprintf(log, "%s\n", header);
578 write_generation_stats(log);
579 fclose(log);
580 } else {
581 fprintf(stderr, "Could not open gc logfile: %s\n", logfile);
582 fflush(stderr);
587 extern void
588 report_heap_exhaustion(long available, long requested, struct thread *th)
590 if (gc_logfile) {
591 FILE * log = fopen(gc_logfile, "a");
592 if (log) {
593 write_heap_exhaustion_report(log, available, requested, th);
594 fclose(log);
595 } else {
596 fprintf(stderr, "Could not open gc logfile: %s\n", gc_logfile);
597 fflush(stderr);
600 /* Always to stderr as well. */
601 write_heap_exhaustion_report(stderr, available, requested, th);
605 #if defined(LISP_FEATURE_X86)
606 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
607 #endif
609 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
610 * if zeroing it ourselves, i.e. in practice give the memory back to the
611 * OS. Generally done after a large GC.
613 void zero_pages_with_mmap(page_index_t start, page_index_t end) {
614 page_index_t i;
615 void *addr = page_address(start), *new_addr;
616 os_vm_size_t length = npage_bytes(1+end-start);
618 if (start > end)
619 return;
621 gc_assert(length >= gencgc_release_granularity);
622 gc_assert((length % gencgc_release_granularity) == 0);
624 #ifdef LISP_FEATURE_LINUX
625 extern os_vm_address_t anon_dynamic_space_start;
626 // We use MADV_DONTNEED only on Linux due to differing semantics from BSD.
627 // Linux treats it as a demand that the memory be 0-filled, or refreshed
628 // from a file that backs the range. BSD takes it as a hint that you don't
629 // care if the memory has to brought in from swap when next accessed,
630 // i.e. it's not a request to make a user-visible alteration to memory.
631 // So in theory this can bring a page in from the core file, if we happen
632 // to hit a page that resides in the portion of memory mapped by coreparse.
633 // In practice this should not happen because objects from a core file can't
634 // become garbage. Except in save-lisp-and-die they can, and we must be
635 // cautious not to resurrect bytes that originally came from the file.
636 if ((os_vm_address_t)addr >= anon_dynamic_space_start) {
637 if (madvise(addr, length, MADV_DONTNEED) != 0)
638 lose("madvise failed\n");
639 } else
640 #endif
642 os_invalidate(addr, length);
643 new_addr = os_validate(addr, length);
644 if (new_addr == NULL || new_addr != addr) {
645 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
646 start, new_addr);
650 for (i = start; i <= end; i++)
651 set_page_need_to_zero(i, 0);
654 /* Zero the pages from START to END (inclusive). Generally done just after
655 * a new region has been allocated.
657 static void
658 zero_pages(page_index_t start, page_index_t end) {
659 if (start > end)
660 return;
662 #if defined(LISP_FEATURE_X86)
663 fast_bzero(page_address(start), npage_bytes(1+end-start));
664 #else
665 bzero(page_address(start), npage_bytes(1+end-start));
666 #endif
670 static void
671 zero_and_mark_pages(page_index_t start, page_index_t end) {
672 page_index_t i;
674 zero_pages(start, end);
675 for (i = start; i <= end; i++)
676 set_page_need_to_zero(i, 0);
679 /* Zero the pages from START to END (inclusive), except for those
680 * pages that are known to already zeroed. Mark all pages in the
681 * ranges as non-zeroed.
683 static void
684 zero_dirty_pages(page_index_t start, page_index_t end) {
685 page_index_t i, j;
687 for (i = start; i <= end; i++) {
688 if (!page_need_to_zero(i)) continue;
689 for (j = i+1; (j <= end) && page_need_to_zero(j) ; j++)
690 ; /* empty body */
691 zero_pages(i, j-1);
692 i = j;
695 for (i = start; i <= end; i++) {
696 set_page_need_to_zero(i, 1);
702 * To support quick and inline allocation, regions of memory can be
703 * allocated and then allocated from with just a free pointer and a
704 * check against an end address.
706 * Since objects can be allocated to spaces with different properties
707 * e.g. boxed/unboxed, generation, ages; there may need to be many
708 * allocation regions.
710 * Each allocation region may start within a partly used page. Many
711 * features of memory use are noted on a page wise basis, e.g. the
712 * generation; so if a region starts within an existing allocated page
713 * it must be consistent with this page.
715 * During the scavenging of the newspace, objects will be transported
716 * into an allocation region, and pointers updated to point to this
717 * allocation region. It is possible that these pointers will be
718 * scavenged again before the allocation region is closed, e.g. due to
719 * trans_list which jumps all over the place to cleanup the list. It
720 * is important to be able to determine properties of all objects
721 * pointed to when scavenging, e.g to detect pointers to the oldspace.
722 * Thus it's important that the allocation regions have the correct
723 * properties set when allocated, and not just set when closed. The
724 * region allocation routines return regions with the specified
725 * properties, and grab all the pages, setting their properties
726 * appropriately, except that the amount used is not known.
728 * These regions are used to support quicker allocation using just a
729 * free pointer. The actual space used by the region is not reflected
730 * in the pages tables until it is closed. It can't be scavenged until
731 * closed.
733 * When finished with the region it should be closed, which will
734 * update the page tables for the actual space used returning unused
735 * space. Further it may be noted in the new regions which is
736 * necessary when scavenging the newspace.
738 * Large objects may be allocated directly without an allocation
739 * region, the page tables are updated immediately.
741 * Unboxed objects don't contain pointers to other objects and so
742 * don't need scavenging. Further they can't contain pointers to
743 * younger generations so WP is not needed. By allocating pages to
744 * unboxed objects the whole page never needs scavenging or
745 * write-protecting. */
747 /* We are only using two regions at present. Both are for the current
748 * newspace generation. */
749 struct alloc_region boxed_region;
750 struct alloc_region unboxed_region;
752 /* The generation currently being allocated to. */
753 static generation_index_t gc_alloc_generation;
755 static inline page_index_t
756 generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large)
758 if (large) {
759 if (UNBOXED_PAGE_FLAG == page_type_flag) {
760 return generations[generation].alloc_large_unboxed_start_page;
761 } else if (BOXED_PAGE_FLAG & page_type_flag) {
762 /* Both code and data. */
763 return generations[generation].alloc_large_start_page;
764 } else {
765 lose("bad page type flag: %d", page_type_flag);
767 } else {
768 if (UNBOXED_PAGE_FLAG == page_type_flag) {
769 return generations[generation].alloc_unboxed_start_page;
770 } else if (BOXED_PAGE_FLAG & page_type_flag) {
771 /* Both code and data. */
772 return generations[generation].alloc_start_page;
773 } else {
774 lose("bad page_type_flag: %d", page_type_flag);
779 static inline void
780 set_generation_alloc_start_page(generation_index_t generation, int page_type_flag, int large,
781 page_index_t page)
783 if (large) {
784 if (UNBOXED_PAGE_FLAG == page_type_flag) {
785 generations[generation].alloc_large_unboxed_start_page = page;
786 } else if (BOXED_PAGE_FLAG & page_type_flag) {
787 /* Both code and data. */
788 generations[generation].alloc_large_start_page = page;
789 } else {
790 lose("bad page type flag: %d", page_type_flag);
792 } else {
793 if (UNBOXED_PAGE_FLAG == page_type_flag) {
794 generations[generation].alloc_unboxed_start_page = page;
795 } else if (BOXED_PAGE_FLAG & page_type_flag) {
796 /* Both code and data. */
797 generations[generation].alloc_start_page = page;
798 } else {
799 lose("bad page type flag: %d", page_type_flag);
804 /* Find a new region with room for at least the given number of bytes.
806 * It starts looking at the current generation's alloc_start_page. So
807 * may pick up from the previous region if there is enough space. This
808 * keeps the allocation contiguous when scavenging the newspace.
810 * The alloc_region should have been closed by a call to
811 * gc_alloc_update_page_tables(), and will thus be in an empty state.
813 * To assist the scavenging functions write-protected pages are not
814 * used. Free pages should not be write-protected.
816 * It is critical to the conservative GC that the start of regions be
817 * known. To help achieve this only small regions are allocated at a
818 * time.
820 * During scavenging, pointers may be found to within the current
821 * region and the page generation must be set so that pointers to the
822 * from space can be recognized. Therefore the generation of pages in
823 * the region are set to gc_alloc_generation. To prevent another
824 * allocation call using the same pages, all the pages in the region
825 * are allocated, although they will initially be empty.
827 static void
828 gc_alloc_new_region(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
830 page_index_t first_page;
831 page_index_t last_page;
832 page_index_t i;
833 int ret;
836 FSHOW((stderr,
837 "/alloc_new_region for %d bytes from gen %d\n",
838 nbytes, gc_alloc_generation));
841 /* Check that the region is in a reset state. */
842 gc_assert((alloc_region->first_page == 0)
843 && (alloc_region->last_page == -1)
844 && (alloc_region->free_pointer == alloc_region->end_addr));
845 ret = thread_mutex_lock(&free_pages_lock);
846 gc_assert(ret == 0);
847 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0);
848 last_page=gc_find_freeish_pages(&first_page, nbytes, page_type_flag);
850 /* Set up the alloc_region. */
851 alloc_region->first_page = first_page;
852 alloc_region->last_page = last_page;
853 alloc_region->start_addr = page_bytes_used(first_page)
854 + page_address(first_page);
855 alloc_region->free_pointer = alloc_region->start_addr;
856 alloc_region->end_addr = page_address(last_page+1);
858 /* Set up the pages. */
860 /* The first page may have already been in use. */
861 /* If so, just assert that it's consistent, otherwise, set it up. */
862 if (page_bytes_used(first_page)) {
863 gc_assert(page_table[first_page].allocated == page_type_flag);
864 gc_assert(page_table[first_page].gen == gc_alloc_generation);
865 gc_assert(page_table[first_page].large_object == 0);
866 } else {
867 page_table[first_page].allocated = page_type_flag;
868 page_table[first_page].gen = gc_alloc_generation;
869 page_table[first_page].large_object = 0;
870 set_page_scan_start_offset(first_page, 0);
872 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
874 for (i = first_page+1; i <= last_page; i++) {
875 page_table[i].allocated = page_type_flag;
876 page_table[i].gen = gc_alloc_generation;
877 page_table[i].large_object = 0;
878 /* This may not be necessary for unboxed regions (think it was
879 * broken before!) */
880 set_page_scan_start_offset(i,
881 addr_diff(page_address(i), alloc_region->start_addr));
882 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
884 /* Bump up last_free_page. */
885 if (last_page+1 > last_free_page) {
886 last_free_page = last_page+1;
887 /* do we only want to call this on special occasions? like for
888 * boxed_region? */
889 set_alloc_pointer((lispobj)page_address(last_free_page));
891 ret = thread_mutex_unlock(&free_pages_lock);
892 gc_assert(ret == 0);
894 #ifdef READ_PROTECT_FREE_PAGES
895 os_protect(page_address(first_page),
896 npage_bytes(1+last_page-first_page),
897 OS_VM_PROT_ALL);
898 #endif
900 /* If the first page was only partial, don't check whether it's
901 * zeroed (it won't be) and don't zero it (since the parts that
902 * we're interested in are guaranteed to be zeroed).
904 if (page_bytes_used(first_page)) {
905 first_page++;
908 zero_dirty_pages(first_page, last_page);
910 /* we can do this after releasing free_pages_lock */
911 if (gencgc_zero_check) {
912 word_t *p;
913 for (p = (word_t *)alloc_region->start_addr;
914 p < (word_t *)alloc_region->end_addr; p++) {
915 if (*p != 0) {
916 lose("The new region is not zero at %p (start=%p, end=%p).\n",
917 p, alloc_region->start_addr, alloc_region->end_addr);
923 /* If the record_new_objects flag is 2 then all new regions created
924 * are recorded.
926 * If it's 1 then then it is only recorded if the first page of the
927 * current region is <= new_areas_ignore_page. This helps avoid
928 * unnecessary recording when doing full scavenge pass.
930 * The new_object structure holds the page, byte offset, and size of
931 * new regions of objects. Each new area is placed in the array of
932 * these structures pointer to by new_areas. new_areas_index holds the
933 * offset into new_areas.
935 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
936 * later code must detect this and handle it, probably by doing a full
937 * scavenge of a generation. */
938 #define NUM_NEW_AREAS 512
939 static int record_new_objects = 0;
940 static page_index_t new_areas_ignore_page;
941 struct new_area {
942 page_index_t page;
943 size_t offset;
944 size_t size;
946 static struct new_area (*new_areas)[];
947 static size_t new_areas_index;
948 size_t max_new_areas;
950 /* Add a new area to new_areas. */
951 static void
952 add_new_area(page_index_t first_page, size_t offset, size_t size)
954 size_t new_area_start, c;
955 ssize_t i;
957 /* Ignore if full. */
958 if (new_areas_index >= NUM_NEW_AREAS)
959 return;
961 switch (record_new_objects) {
962 case 0:
963 return;
964 case 1:
965 if (first_page > new_areas_ignore_page)
966 return;
967 break;
968 case 2:
969 break;
970 default:
971 gc_abort();
974 new_area_start = npage_bytes(first_page) + offset;
976 /* Search backwards for a prior area that this follows from. If
977 found this will save adding a new area. */
978 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
979 size_t area_end =
980 npage_bytes((*new_areas)[i].page)
981 + (*new_areas)[i].offset
982 + (*new_areas)[i].size;
983 /*FSHOW((stderr,
984 "/add_new_area S1 %d %d %d %d\n",
985 i, c, new_area_start, area_end));*/
986 if (new_area_start == area_end) {
987 /*FSHOW((stderr,
988 "/adding to [%d] %d %d %d with %d %d %d:\n",
990 (*new_areas)[i].page,
991 (*new_areas)[i].offset,
992 (*new_areas)[i].size,
993 first_page,
994 offset,
995 size);*/
996 (*new_areas)[i].size += size;
997 return;
1001 (*new_areas)[new_areas_index].page = first_page;
1002 (*new_areas)[new_areas_index].offset = offset;
1003 (*new_areas)[new_areas_index].size = size;
1004 /*FSHOW((stderr,
1005 "/new_area %d page %d offset %d size %d\n",
1006 new_areas_index, first_page, offset, size));*/
1007 new_areas_index++;
1009 /* Note the max new_areas used. */
1010 if (new_areas_index > max_new_areas)
1011 max_new_areas = new_areas_index;
1014 /* Update the tables for the alloc_region. The region may be added to
1015 * the new_areas.
1017 * When done the alloc_region is set up so that the next quick alloc
1018 * will fail safely and thus a new region will be allocated. Further
1019 * it is safe to try to re-update the page table of this reset
1020 * alloc_region. */
1021 void
1022 gc_alloc_update_page_tables(int page_type_flag, struct alloc_region *alloc_region)
1024 boolean more;
1025 page_index_t first_page;
1026 page_index_t next_page;
1027 os_vm_size_t bytes_used;
1028 os_vm_size_t region_size;
1029 os_vm_size_t byte_cnt;
1030 page_bytes_t orig_first_page_bytes_used;
1031 int ret;
1034 first_page = alloc_region->first_page;
1036 /* Catch an unused alloc_region. */
1037 if ((first_page == 0) && (alloc_region->last_page == -1))
1038 return;
1040 next_page = first_page+1;
1042 ret = thread_mutex_lock(&free_pages_lock);
1043 gc_assert(ret == 0);
1044 if (alloc_region->free_pointer != alloc_region->start_addr) {
1045 /* some bytes were allocated in the region */
1046 orig_first_page_bytes_used = page_bytes_used(first_page);
1048 gc_assert(alloc_region->start_addr ==
1049 (page_address(first_page) + page_bytes_used(first_page)));
1051 /* All the pages used need to be updated */
1053 /* Update the first page. */
1055 /* If the page was free then set up the gen, and
1056 * scan_start_offset. */
1057 if (page_bytes_used(first_page) == 0)
1058 gc_assert(page_starts_contiguous_block_p(first_page));
1059 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1061 gc_assert(page_table[first_page].allocated & page_type_flag);
1062 gc_assert(page_table[first_page].gen == gc_alloc_generation);
1063 gc_assert(page_table[first_page].large_object == 0);
1065 byte_cnt = 0;
1067 /* Calculate the number of bytes used in this page. This is not
1068 * always the number of new bytes, unless it was free. */
1069 more = 0;
1070 if ((bytes_used = addr_diff(alloc_region->free_pointer,
1071 page_address(first_page)))
1072 >GENCGC_CARD_BYTES) {
1073 bytes_used = GENCGC_CARD_BYTES;
1074 more = 1;
1076 set_page_bytes_used(first_page, bytes_used);
1077 byte_cnt += bytes_used;
1080 /* All the rest of the pages should be free. We need to set
1081 * their scan_start_offset pointer to the start of the
1082 * region, and set the bytes_used. */
1083 while (more) {
1084 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1085 gc_assert(page_table[next_page].allocated & page_type_flag);
1086 gc_assert(page_bytes_used(next_page) == 0);
1087 gc_assert(page_table[next_page].gen == gc_alloc_generation);
1088 gc_assert(page_table[next_page].large_object == 0);
1089 gc_assert(page_scan_start_offset(next_page) ==
1090 addr_diff(page_address(next_page),
1091 alloc_region->start_addr));
1093 /* Calculate the number of bytes used in this page. */
1094 more = 0;
1095 if ((bytes_used = addr_diff(alloc_region->free_pointer,
1096 page_address(next_page)))>GENCGC_CARD_BYTES) {
1097 bytes_used = GENCGC_CARD_BYTES;
1098 more = 1;
1100 set_page_bytes_used(next_page, bytes_used);
1101 byte_cnt += bytes_used;
1103 next_page++;
1106 region_size = addr_diff(alloc_region->free_pointer,
1107 alloc_region->start_addr);
1108 bytes_allocated += region_size;
1109 generations[gc_alloc_generation].bytes_allocated += region_size;
1111 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
1113 /* Set the generations alloc restart page to the last page of
1114 * the region. */
1115 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 0, next_page-1);
1117 /* Add the region to the new_areas if requested. */
1118 if (BOXED_PAGE_FLAG & page_type_flag)
1119 add_new_area(first_page,orig_first_page_bytes_used, region_size);
1122 FSHOW((stderr,
1123 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1124 region_size,
1125 gc_alloc_generation));
1127 } else {
1128 /* There are no bytes allocated. Unallocate the first_page if
1129 * there are 0 bytes_used. */
1130 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
1131 if (page_bytes_used(first_page) == 0)
1132 page_table[first_page].allocated = FREE_PAGE_FLAG;
1135 /* Unallocate any unused pages. */
1136 while (next_page <= alloc_region->last_page) {
1137 gc_assert(page_bytes_used(next_page) == 0);
1138 page_table[next_page].allocated = FREE_PAGE_FLAG;
1139 next_page++;
1141 ret = thread_mutex_unlock(&free_pages_lock);
1142 gc_assert(ret == 0);
1144 /* alloc_region is per-thread, we're ok to do this unlocked */
1145 gc_set_region_empty(alloc_region);
1148 /* Allocate a possibly large object. */
1149 void *
1150 gc_alloc_large(sword_t nbytes, int page_type_flag, struct alloc_region *alloc_region)
1152 boolean more;
1153 page_index_t first_page, next_page, last_page;
1154 os_vm_size_t byte_cnt;
1155 os_vm_size_t bytes_used;
1156 int ret;
1158 ret = thread_mutex_lock(&free_pages_lock);
1159 gc_assert(ret == 0);
1161 first_page = generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1);
1162 if (first_page <= alloc_region->last_page) {
1163 first_page = alloc_region->last_page+1;
1166 last_page=gc_find_freeish_pages(&first_page,nbytes, page_type_flag);
1168 gc_assert(first_page > alloc_region->last_page);
1170 set_generation_alloc_start_page(gc_alloc_generation, page_type_flag, 1, last_page);
1172 /* Large objects don't share pages with other objects. */
1173 gc_assert(page_bytes_used(first_page) == 0);
1175 /* Set up the pages. */
1176 page_table[first_page].allocated = page_type_flag;
1177 page_table[first_page].gen = gc_alloc_generation;
1178 page_table[first_page].large_object = 1;
1179 set_page_scan_start_offset(first_page, 0);
1181 byte_cnt = 0;
1183 /* Calc. the number of bytes used in this page. This is not
1184 * always the number of new bytes, unless it was free. */
1185 more = 0;
1186 if ((bytes_used = nbytes) > GENCGC_CARD_BYTES) {
1187 bytes_used = GENCGC_CARD_BYTES;
1188 more = 1;
1190 set_page_bytes_used(first_page, bytes_used);
1191 byte_cnt += bytes_used;
1193 next_page = first_page+1;
1195 /* All the rest of the pages should be free. We need to set their
1196 * scan_start_offset pointer to the start of the region, and set
1197 * the bytes_used. */
1198 while (more) {
1199 gc_assert(page_free_p(next_page));
1200 gc_assert(page_bytes_used(next_page) == 0);
1201 page_table[next_page].allocated = page_type_flag;
1202 page_table[next_page].gen = gc_alloc_generation;
1203 page_table[next_page].large_object = 1;
1205 set_page_scan_start_offset(next_page, npage_bytes(next_page-first_page));
1207 /* Calculate the number of bytes used in this page. */
1208 more = 0;
1209 bytes_used = nbytes - byte_cnt;
1210 if (bytes_used > GENCGC_CARD_BYTES) {
1211 bytes_used = GENCGC_CARD_BYTES;
1212 more = 1;
1214 set_page_bytes_used(next_page, bytes_used);
1215 page_table[next_page].write_protected=0;
1216 page_table[next_page].dont_move=0;
1217 byte_cnt += bytes_used;
1218 next_page++;
1221 gc_assert(byte_cnt == (size_t)nbytes);
1223 bytes_allocated += nbytes;
1224 generations[gc_alloc_generation].bytes_allocated += nbytes;
1226 /* Add the region to the new_areas if requested. */
1227 if (BOXED_PAGE_FLAG & page_type_flag)
1228 add_new_area(first_page, 0, nbytes);
1230 /* Bump up last_free_page */
1231 if (last_page+1 > last_free_page) {
1232 last_free_page = last_page+1;
1233 set_alloc_pointer((lispobj)(page_address(last_free_page)));
1235 ret = thread_mutex_unlock(&free_pages_lock);
1236 gc_assert(ret == 0);
1238 #ifdef READ_PROTECT_FREE_PAGES
1239 os_protect(page_address(first_page),
1240 npage_bytes(1+last_page-first_page),
1241 OS_VM_PROT_ALL);
1242 #endif
1244 zero_dirty_pages(first_page, last_page);
1246 return page_address(first_page);
1249 static page_index_t gencgc_alloc_start_page = -1;
1251 void
1252 gc_heap_exhausted_error_or_lose (sword_t available, sword_t requested)
1254 struct thread *thread = arch_os_get_current_thread();
1255 /* Write basic information before doing anything else: if we don't
1256 * call to lisp this is a must, and even if we do there is always
1257 * the danger that we bounce back here before the error has been
1258 * handled, or indeed even printed.
1260 report_heap_exhaustion(available, requested, thread);
1261 if (gc_active_p || (available == 0)) {
1262 /* If we are in GC, or totally out of memory there is no way
1263 * to sanely transfer control to the lisp-side of things.
1265 lose("Heap exhausted, game over.");
1267 else {
1268 /* FIXME: assert free_pages_lock held */
1269 (void)thread_mutex_unlock(&free_pages_lock);
1270 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1271 gc_assert(get_pseudo_atomic_atomic(thread));
1272 clear_pseudo_atomic_atomic(thread);
1273 if (get_pseudo_atomic_interrupted(thread))
1274 do_pending_interrupt();
1275 #endif
1276 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1277 * to running user code at arbitrary places, even in a
1278 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1279 * running out of the heap. So at this point all bets are
1280 * off. */
1281 if (SymbolValue(INTERRUPTS_ENABLED,thread) == NIL)
1282 corruption_warning_and_maybe_lose
1283 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1284 /* available and requested should be double word aligned, thus
1285 they can passed as fixnums and shifted later. */
1286 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR), available, requested);
1287 lose("HEAP-EXHAUSTED-ERROR fell through");
1291 page_index_t
1292 gc_find_freeish_pages(page_index_t *restart_page_ptr, sword_t bytes,
1293 int page_type_flag)
1295 page_index_t most_bytes_found_from = 0, most_bytes_found_to = 0;
1296 page_index_t first_page, last_page, restart_page = *restart_page_ptr;
1297 os_vm_size_t nbytes = bytes;
1298 os_vm_size_t nbytes_goal = nbytes;
1299 os_vm_size_t bytes_found = 0;
1300 os_vm_size_t most_bytes_found = 0;
1301 boolean small_object = nbytes < GENCGC_CARD_BYTES;
1302 /* FIXME: assert(free_pages_lock is held); */
1304 if (nbytes_goal < gencgc_alloc_granularity)
1305 nbytes_goal = gencgc_alloc_granularity;
1307 /* Toggled by gc_and_save for heap compaction, normally -1. */
1308 if (gencgc_alloc_start_page != -1) {
1309 restart_page = gencgc_alloc_start_page;
1312 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1313 * long from the interface. */
1314 gc_assert(bytes>=0);
1315 /* Search for a page with at least nbytes of space. We prefer
1316 * not to split small objects on multiple pages, to reduce the
1317 * number of contiguous allocation regions spaning multiple
1318 * pages: this helps avoid excessive conservativism.
1320 * For other objects, we guarantee that they start on their own
1321 * page boundary.
1323 first_page = restart_page;
1324 while (first_page < page_table_pages) {
1325 bytes_found = 0;
1326 if (page_free_p(first_page)) {
1327 gc_assert(0 == page_bytes_used(first_page));
1328 bytes_found = GENCGC_CARD_BYTES;
1329 } else if (small_object &&
1330 (page_table[first_page].allocated == page_type_flag) &&
1331 (page_table[first_page].large_object == 0) &&
1332 (page_table[first_page].gen == gc_alloc_generation) &&
1333 (page_table[first_page].write_protected == 0) &&
1334 (page_table[first_page].dont_move == 0)) {
1335 bytes_found = GENCGC_CARD_BYTES - page_bytes_used(first_page);
1336 if (bytes_found < nbytes) {
1337 if (bytes_found > most_bytes_found)
1338 most_bytes_found = bytes_found;
1339 first_page++;
1340 continue;
1342 } else {
1343 first_page++;
1344 continue;
1347 gc_assert(page_table[first_page].write_protected == 0);
1348 for (last_page = first_page+1;
1349 ((last_page < page_table_pages) &&
1350 page_free_p(last_page) &&
1351 (bytes_found < nbytes_goal));
1352 last_page++) {
1353 bytes_found += GENCGC_CARD_BYTES;
1354 gc_assert(0 == page_bytes_used(last_page));
1355 gc_assert(0 == page_table[last_page].write_protected);
1358 if (bytes_found > most_bytes_found) {
1359 most_bytes_found = bytes_found;
1360 most_bytes_found_from = first_page;
1361 most_bytes_found_to = last_page;
1363 if (bytes_found >= nbytes_goal)
1364 break;
1366 first_page = last_page;
1369 bytes_found = most_bytes_found;
1370 restart_page = first_page + 1;
1372 /* Check for a failure */
1373 if (bytes_found < nbytes) {
1374 gc_assert(restart_page >= page_table_pages);
1375 gc_heap_exhausted_error_or_lose(most_bytes_found, nbytes);
1378 gc_assert(most_bytes_found_to);
1379 *restart_page_ptr = most_bytes_found_from;
1380 return most_bytes_found_to-1;
1383 /* Allocate bytes. All the rest of the special-purpose allocation
1384 * functions will eventually call this */
1386 void *
1387 gc_alloc_with_region(sword_t nbytes,int page_type_flag, struct alloc_region *my_region,
1388 int quick_p)
1390 void *new_free_pointer;
1392 if (nbytes>=LARGE_OBJECT_SIZE)
1393 return gc_alloc_large(nbytes, page_type_flag, my_region);
1395 /* Check whether there is room in the current alloc region. */
1396 new_free_pointer = my_region->free_pointer + nbytes;
1398 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1399 my_region->free_pointer, new_free_pointer); */
1401 if (new_free_pointer <= my_region->end_addr) {
1402 /* If so then allocate from the current alloc region. */
1403 void *new_obj = my_region->free_pointer;
1404 my_region->free_pointer = new_free_pointer;
1406 /* Unless a `quick' alloc was requested, check whether the
1407 alloc region is almost empty. */
1408 if (!quick_p &&
1409 addr_diff(my_region->end_addr,my_region->free_pointer) <= 32) {
1410 /* If so, finished with the current region. */
1411 gc_alloc_update_page_tables(page_type_flag, my_region);
1412 /* Set up a new region. */
1413 gc_alloc_new_region(32 /*bytes*/, page_type_flag, my_region);
1416 return((void *)new_obj);
1419 /* Else not enough free space in the current region: retry with a
1420 * new region. */
1422 gc_alloc_update_page_tables(page_type_flag, my_region);
1423 gc_alloc_new_region(nbytes, page_type_flag, my_region);
1424 return gc_alloc_with_region(nbytes, page_type_flag, my_region,0);
1427 /* Copy a large object. If the object is in a large object region then
1428 * it is simply promoted, else it is copied. If it's large enough then
1429 * it's copied to a large object region.
1431 * Bignums and vectors may have shrunk. If the object is not copied
1432 * the space needs to be reclaimed, and the page_tables corrected. */
1433 static lispobj
1434 general_copy_large_object(lispobj object, word_t nwords, boolean boxedp)
1436 lispobj *new;
1437 page_index_t first_page;
1439 CHECK_COPY_PRECONDITIONS(object, nwords);
1441 if ((nwords > 1024*1024) && gencgc_verbose) {
1442 FSHOW((stderr, "/general_copy_large_object: %d bytes\n",
1443 nwords*N_WORD_BYTES));
1446 /* Check whether it's a large object. */
1447 first_page = find_page_index((void *)object);
1448 gc_assert(first_page >= 0);
1450 if (page_table[first_page].large_object) {
1451 /* Promote the object. Note: Unboxed objects may have been
1452 * allocated to a BOXED region so it may be necessary to
1453 * change the region to UNBOXED. */
1454 os_vm_size_t remaining_bytes;
1455 os_vm_size_t bytes_freed;
1456 page_index_t next_page;
1457 page_bytes_t old_bytes_used;
1459 /* FIXME: This comment is somewhat stale.
1461 * Note: Any page write-protection must be removed, else a
1462 * later scavenge_newspace may incorrectly not scavenge these
1463 * pages. This would not be necessary if they are added to the
1464 * new areas, but let's do it for them all (they'll probably
1465 * be written anyway?). */
1467 gc_assert(page_starts_contiguous_block_p(first_page));
1468 next_page = first_page;
1469 remaining_bytes = nwords*N_WORD_BYTES;
1471 while (remaining_bytes > GENCGC_CARD_BYTES) {
1472 gc_assert(page_table[next_page].gen == from_space);
1473 gc_assert(page_table[next_page].large_object);
1474 gc_assert(page_scan_start_offset(next_page) ==
1475 npage_bytes(next_page-first_page));
1476 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
1477 /* Should have been unprotected by unprotect_oldspace()
1478 * for boxed objects, and after promotion unboxed ones
1479 * should not be on protected pages at all. */
1480 gc_assert(!page_table[next_page].write_protected);
1482 if (boxedp)
1483 gc_assert(page_boxed_p(next_page));
1484 else {
1485 gc_assert(page_allocated_no_region_p(next_page));
1486 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1488 page_table[next_page].gen = new_space;
1490 remaining_bytes -= GENCGC_CARD_BYTES;
1491 next_page++;
1494 /* Now only one page remains, but the object may have shrunk so
1495 * there may be more unused pages which will be freed. */
1497 /* Object may have shrunk but shouldn't have grown - check. */
1498 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
1500 page_table[next_page].gen = new_space;
1502 if (boxedp)
1503 gc_assert(page_boxed_p(next_page));
1504 else
1505 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1507 /* Adjust the bytes_used. */
1508 old_bytes_used = page_bytes_used(next_page);
1509 set_page_bytes_used(next_page, remaining_bytes);
1511 bytes_freed = old_bytes_used - remaining_bytes;
1513 /* Free any remaining pages; needs care. */
1514 next_page++;
1515 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
1516 (page_table[next_page].gen == from_space) &&
1517 /* FIXME: It is not obvious to me why this is necessary
1518 * as a loop condition: it seems to me that the
1519 * scan_start_offset test should be sufficient, but
1520 * experimentally that is not the case. --NS
1521 * 2011-11-28 */
1522 (boxedp ?
1523 page_boxed_p(next_page) :
1524 page_allocated_no_region_p(next_page)) &&
1525 page_table[next_page].large_object &&
1526 (page_scan_start_offset(next_page) ==
1527 npage_bytes(next_page - first_page))) {
1528 /* Checks out OK, free the page. Don't need to both zeroing
1529 * pages as this should have been done before shrinking the
1530 * object. These pages shouldn't be write-protected, even if
1531 * boxed they should be zero filled. */
1532 gc_assert(page_table[next_page].write_protected == 0);
1534 old_bytes_used = page_bytes_used(next_page);
1535 page_table[next_page].allocated = FREE_PAGE_FLAG;
1536 set_page_bytes_used(next_page, 0);
1537 bytes_freed += old_bytes_used;
1538 next_page++;
1541 if ((bytes_freed > 0) && gencgc_verbose) {
1542 FSHOW((stderr,
1543 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT"\n",
1544 bytes_freed));
1547 generations[from_space].bytes_allocated -= nwords*N_WORD_BYTES
1548 + bytes_freed;
1549 generations[new_space].bytes_allocated += nwords*N_WORD_BYTES;
1550 bytes_allocated -= bytes_freed;
1552 /* Add the region to the new_areas if requested. */
1553 if (boxedp)
1554 add_new_area(first_page,0,nwords*N_WORD_BYTES);
1556 return(object);
1558 } else {
1559 /* Allocate space. */
1560 new = gc_general_alloc(nwords*N_WORD_BYTES,
1561 (boxedp ? BOXED_PAGE_FLAG : UNBOXED_PAGE_FLAG),
1562 ALLOC_QUICK);
1564 /* Copy the object. */
1565 memcpy(new,native_pointer(object),nwords*N_WORD_BYTES);
1567 /* Return Lisp pointer of new object. */
1568 return make_lispobj(new, lowtag_of(object));
1572 lispobj
1573 copy_large_object(lispobj object, sword_t nwords)
1575 return general_copy_large_object(object, nwords, 1);
1578 lispobj
1579 copy_large_unboxed_object(lispobj object, sword_t nwords)
1581 return general_copy_large_object(object, nwords, 0);
1584 /* to copy unboxed objects */
1585 lispobj
1586 copy_unboxed_object(lispobj object, sword_t nwords)
1588 return gc_general_copy_object(object, nwords, UNBOXED_PAGE_FLAG);
1593 * code and code-related objects
1596 static lispobj trans_fun_header(lispobj object);
1597 static lispobj trans_boxed(lispobj object);
1600 /* Scan a x86 compiled code object, looking for possible fixups that
1601 * have been missed after a move.
1603 * Two types of fixups are needed:
1604 * 1. Absolute fixups to within the code object.
1605 * 2. Relative fixups to outside the code object.
1607 * Currently only absolute fixups to the constant vector, or to the
1608 * code area are checked. */
1609 #ifdef LISP_FEATURE_X86
1610 void
1611 sniff_code_object(struct code *code, os_vm_size_t displacement)
1613 sword_t nheader_words, ncode_words, nwords;
1614 os_vm_address_t constants_start_addr = NULL, constants_end_addr, p;
1615 os_vm_address_t code_start_addr, code_end_addr;
1616 os_vm_address_t code_addr = (os_vm_address_t)code;
1617 int fixup_found = 0;
1619 if (!check_code_fixups)
1620 return;
1622 FSHOW((stderr, "/sniffing code: %p, %lu\n", code, displacement));
1624 ncode_words = code_instruction_words(code->code_size);
1625 nheader_words = code_header_words(*(lispobj *)code);
1626 nwords = ncode_words + nheader_words;
1628 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1629 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1630 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1631 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1633 /* Work through the unboxed code. */
1634 for (p = code_start_addr; p < code_end_addr; p++) {
1635 void *data = *(void **)p;
1636 unsigned d1 = *((unsigned char *)p - 1);
1637 unsigned d2 = *((unsigned char *)p - 2);
1638 unsigned d3 = *((unsigned char *)p - 3);
1639 unsigned d4 = *((unsigned char *)p - 4);
1640 #if QSHOW
1641 unsigned d5 = *((unsigned char *)p - 5);
1642 unsigned d6 = *((unsigned char *)p - 6);
1643 #endif
1645 /* Check for code references. */
1646 /* Check for a 32 bit word that looks like an absolute
1647 reference to within the code adea of the code object. */
1648 if ((data >= (void*)(code_start_addr-displacement))
1649 && (data < (void*)(code_end_addr-displacement))) {
1650 /* function header */
1651 if ((d4 == 0x5e)
1652 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) ==
1653 (unsigned)code)) {
1654 /* Skip the function header */
1655 p += 6*4 - 4 - 1;
1656 continue;
1658 /* the case of PUSH imm32 */
1659 if (d1 == 0x68) {
1660 fixup_found = 1;
1661 FSHOW((stderr,
1662 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1663 p, d6, d5, d4, d3, d2, d1, data));
1664 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1666 /* the case of MOV [reg-8],imm32 */
1667 if ((d3 == 0xc7)
1668 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1669 || d2==0x45 || d2==0x46 || d2==0x47)
1670 && (d1 == 0xf8)) {
1671 fixup_found = 1;
1672 FSHOW((stderr,
1673 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1674 p, d6, d5, d4, d3, d2, d1, data));
1675 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1677 /* the case of LEA reg,[disp32] */
1678 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1679 fixup_found = 1;
1680 FSHOW((stderr,
1681 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1682 p, d6, d5, d4, d3, d2, d1, data));
1683 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1687 /* Check for constant references. */
1688 /* Check for a 32 bit word that looks like an absolute
1689 reference to within the constant vector. Constant references
1690 will be aligned. */
1691 if ((data >= (void*)(constants_start_addr-displacement))
1692 && (data < (void*)(constants_end_addr-displacement))
1693 && (((unsigned)data & 0x3) == 0)) {
1694 /* Mov eax,m32 */
1695 if (d1 == 0xa1) {
1696 fixup_found = 1;
1697 FSHOW((stderr,
1698 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1699 p, d6, d5, d4, d3, d2, d1, data));
1700 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1703 /* the case of MOV m32,EAX */
1704 if (d1 == 0xa3) {
1705 fixup_found = 1;
1706 FSHOW((stderr,
1707 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1708 p, d6, d5, d4, d3, d2, d1, data));
1709 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1712 /* the case of CMP m32,imm32 */
1713 if ((d1 == 0x3d) && (d2 == 0x81)) {
1714 fixup_found = 1;
1715 FSHOW((stderr,
1716 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1717 p, d6, d5, d4, d3, d2, d1, data));
1718 /* XX Check this */
1719 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1722 /* Check for a mod=00, r/m=101 byte. */
1723 if ((d1 & 0xc7) == 5) {
1724 /* Cmp m32,reg */
1725 if (d2 == 0x39) {
1726 fixup_found = 1;
1727 FSHOW((stderr,
1728 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1729 p, d6, d5, d4, d3, d2, d1, data));
1730 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1732 /* the case of CMP reg32,m32 */
1733 if (d2 == 0x3b) {
1734 fixup_found = 1;
1735 FSHOW((stderr,
1736 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1737 p, d6, d5, d4, d3, d2, d1, data));
1738 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1740 /* the case of MOV m32,reg32 */
1741 if (d2 == 0x89) {
1742 fixup_found = 1;
1743 FSHOW((stderr,
1744 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1745 p, d6, d5, d4, d3, d2, d1, data));
1746 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1748 /* the case of MOV reg32,m32 */
1749 if (d2 == 0x8b) {
1750 fixup_found = 1;
1751 FSHOW((stderr,
1752 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1753 p, d6, d5, d4, d3, d2, d1, data));
1754 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1756 /* the case of LEA reg32,m32 */
1757 if (d2 == 0x8d) {
1758 fixup_found = 1;
1759 FSHOW((stderr,
1760 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1761 p, d6, d5, d4, d3, d2, d1, data));
1762 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1768 /* If anything was found, print some information on the code
1769 * object. */
1770 if (fixup_found) {
1771 FSHOW((stderr,
1772 "/compiled code object at %x: header words = %d, code words = %d\n",
1773 code, nheader_words, ncode_words));
1774 FSHOW((stderr,
1775 "/const start = %x, end = %x\n",
1776 constants_start_addr, constants_end_addr));
1777 FSHOW((stderr,
1778 "/code start = %x, end = %x\n",
1779 code_start_addr, code_end_addr));
1782 #endif
1784 #ifdef LISP_FEATURE_X86
1785 void
1786 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1788 sword_t nheader_words, ncode_words, nwords;
1789 os_vm_address_t __attribute__((unused)) constants_start_addr, constants_end_addr;
1790 os_vm_address_t __attribute__((unused)) code_start_addr, code_end_addr;
1791 os_vm_address_t code_addr = (os_vm_address_t)new_code;
1792 os_vm_address_t old_addr = (os_vm_address_t)old_code;
1793 os_vm_size_t displacement = code_addr - old_addr;
1794 lispobj fixups = NIL;
1795 struct vector *fixups_vector;
1797 ncode_words = code_instruction_words(new_code->code_size);
1798 nheader_words = code_header_words(*(lispobj *)new_code);
1799 nwords = ncode_words + nheader_words;
1800 /* FSHOW((stderr,
1801 "/compiled code object at %x: header words = %d, code words = %d\n",
1802 new_code, nheader_words, ncode_words)); */
1803 constants_start_addr = code_addr + 5*N_WORD_BYTES;
1804 constants_end_addr = code_addr + nheader_words*N_WORD_BYTES;
1805 code_start_addr = code_addr + nheader_words*N_WORD_BYTES;
1806 code_end_addr = code_addr + nwords*N_WORD_BYTES;
1808 FSHOW((stderr,
1809 "/const start = %x, end = %x\n",
1810 constants_start_addr,constants_end_addr));
1811 FSHOW((stderr,
1812 "/code start = %x; end = %x\n",
1813 code_start_addr,code_end_addr));
1816 fixups = new_code->fixups;
1817 /* It will be a Lisp vector if valid, or 0 if there are no fixups */
1818 if (fixups == 0 || !is_lisp_pointer(fixups)) {
1819 /* Check for possible errors. */
1820 if (check_code_fixups)
1821 sniff_code_object(new_code, displacement);
1823 return;
1826 fixups_vector = (struct vector *)native_pointer(fixups);
1828 /* Could be pointing to a forwarding pointer. */
1829 /* This is extremely unlikely, because the only referent of the fixups
1830 is usually the code itself; so scavenging the vector won't occur
1831 until after the code object is known to be live. As we're just now
1832 enlivening the code, the fixups shouldn't have been forwarded.
1833 Maybe the vector is on the special binding stack though ... */
1834 if (is_lisp_pointer(fixups) &&
1835 (find_page_index((void*)fixups_vector) != -1) &&
1836 forwarding_pointer_p((lispobj*)fixups_vector)) {
1837 /* If so, then follow it. */
1838 /*SHOW("following pointer to a forwarding pointer");*/
1839 fixups_vector = (struct vector *)
1840 native_pointer(forwarding_pointer_value((lispobj*)fixups_vector));
1843 /*SHOW("got fixups");*/
1845 if (widetag_of(fixups_vector->header) == SIMPLE_ARRAY_WORD_WIDETAG) {
1846 /* Got the fixups for the code block. Now work through the vector,
1847 and apply a fixup at each address. */
1848 sword_t length = fixnum_value(fixups_vector->length);
1849 sword_t i;
1850 for (i = 0; i < length; i++) {
1851 long offset = fixups_vector->data[i];
1852 /* Now check the current value of offset. */
1853 os_vm_address_t old_value = *(os_vm_address_t *)(code_start_addr + offset);
1855 /* If it's within the old_code object then it must be an
1856 * absolute fixup (relative ones are not saved) */
1857 if ((old_value >= old_addr)
1858 && (old_value < (old_addr + nwords*N_WORD_BYTES)))
1859 /* So add the dispacement. */
1860 *(os_vm_address_t *)(code_start_addr + offset) =
1861 old_value + displacement;
1862 else
1863 /* It is outside the old code object so it must be a
1864 * relative fixup (absolute fixups are not saved). So
1865 * subtract the displacement. */
1866 *(os_vm_address_t *)(code_start_addr + offset) =
1867 old_value - displacement;
1869 } else {
1870 /* This used to just print a note to stderr, but a bogus fixup seems to
1871 * indicate real heap corruption, so a hard hailure is in order. */
1872 lose("fixup vector %p has a bad widetag: %d\n",
1873 fixups_vector, widetag_of(fixups_vector->header));
1876 /* Check for possible errors. */
1877 if (check_code_fixups) {
1878 sniff_code_object(new_code,displacement);
1881 #endif
1883 static lispobj
1884 trans_boxed_large(lispobj object)
1886 gc_assert(is_lisp_pointer(object));
1887 return copy_large_object(object,
1888 (HeaderValue(*native_pointer(object)) | 1) + 1);
1892 * weak pointers
1895 /* XX This is a hack adapted from cgc.c. These don't work too
1896 * efficiently with the gencgc as a list of the weak pointers is
1897 * maintained within the objects which causes writes to the pages. A
1898 * limited attempt is made to avoid unnecessary writes, but this needs
1899 * a re-think. */
1900 /* FIXME: now that we have non-Lisp hashtables in the GC, it might make sense
1901 * to stop chaining weak pointers through a slot in the object, as a remedy to
1902 * the above concern. It would also shorten the object by 2 words. */
1903 static sword_t
1904 scav_weak_pointer(lispobj *where, lispobj object)
1906 /* Since we overwrite the 'next' field, we have to make
1907 * sure not to do so for pointers already in the list.
1908 * Instead of searching the list of weak_pointers each
1909 * time, we ensure that next is always NULL when the weak
1910 * pointer isn't in the list, and not NULL otherwise.
1911 * Since we can't use NULL to denote end of list, we
1912 * use a pointer back to the same weak_pointer.
1914 struct weak_pointer * wp = (struct weak_pointer*)where;
1916 if (NULL == wp->next && weak_pointer_breakable_p(wp)) {
1917 wp->next = weak_pointers;
1918 weak_pointers = wp;
1919 if (NULL == wp->next)
1920 wp->next = wp;
1923 /* Do not let GC scavenge the value slot of the weak pointer.
1924 * (That is why it is a weak pointer.) */
1926 return WEAK_POINTER_NWORDS;
1930 lispobj *
1931 search_read_only_space(void *pointer)
1933 lispobj *start = (lispobj *) READ_ONLY_SPACE_START;
1934 lispobj *end = (lispobj *) SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1935 if ((pointer < (void *)start) || (pointer >= (void *)end))
1936 return NULL;
1937 return gc_search_space(start, pointer);
1940 lispobj *
1941 search_static_space(void *pointer)
1943 lispobj *start = (lispobj *)STATIC_SPACE_START;
1944 lispobj *end = (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
1945 if ((pointer < (void *)start) || (pointer >= (void *)end))
1946 return NULL;
1947 return gc_search_space(start, pointer);
1950 /* a faster version for searching the dynamic space. This will work even
1951 * if the object is in a current allocation region. */
1952 lispobj *
1953 search_dynamic_space(void *pointer)
1955 page_index_t page_index = find_page_index(pointer);
1956 lispobj *start;
1958 /* The address may be invalid, so do some checks. */
1959 if ((page_index == -1) || page_free_p(page_index))
1960 return NULL;
1961 start = (lispobj *)page_scan_start(page_index);
1962 return gc_search_space(start, pointer);
1965 // Return the starting address of the object containing 'addr'
1966 // if and only if the object is one which would be evacuated from 'from_space'
1967 // were it allowed to be either discarded as garbage or moved.
1968 // 'addr_page_index' is the page containing 'addr' and must not be -1.
1969 // Return 0 if there is no such object - that is, if addr is past the
1970 // end of the used bytes, or its pages are not in 'from_space' etc.
1971 static lispobj*
1972 conservative_root_p(void *addr, page_index_t addr_page_index)
1974 #ifdef GENCGC_IS_PRECISE
1975 /* If we're in precise gencgc (non-x86oid as of this writing) then
1976 * we are only called on valid object pointers in the first place,
1977 * so we just have to do a bounds-check against the heap, a
1978 * generation check, and the already-pinned check. */
1979 if ((page_table[addr_page_index].gen != from_space)
1980 || (page_table[addr_page_index].dont_move != 0))
1981 return 0;
1982 return (lispobj*)1;
1983 #else
1984 /* quick check 1: Address is quite likely to have been invalid. */
1985 if (page_free_p(addr_page_index)
1986 || (page_bytes_used(addr_page_index) == 0)
1987 || (page_table[addr_page_index].gen != from_space))
1988 return 0;
1989 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
1991 /* quick check 2: Check the offset within the page.
1994 if (((uword_t)addr & (GENCGC_CARD_BYTES - 1)) > page_bytes_used(addr_page_index))
1995 return 0;
1997 /* Filter out anything which can't be a pointer to a Lisp object
1998 * (or, as a special case which also requires dont_move, a return
1999 * address referring to something in a CodeObject). This is
2000 * expensive but important, since it vastly reduces the
2001 * probability that random garbage will be bogusly interpreted as
2002 * a pointer which prevents a page from moving. */
2003 lispobj* object_start = search_dynamic_space(addr);
2004 if (!object_start) return 0;
2006 /* If the containing object is a code object and 'addr' points
2007 * anywhere beyond the boxed words,
2008 * presume it to be a valid unboxed return address. */
2009 if (instruction_ptr_p(addr, object_start))
2010 return object_start;
2012 /* Large object pages only contain ONE object, and it will never
2013 * be a CONS. However, arrays and bignums can be allocated larger
2014 * than necessary and then shrunk to fit, leaving what look like
2015 * (0 . 0) CONSes at the end. These appear valid to
2016 * properly_tagged_descriptor_p(), so pick them off here. */
2017 if (((lowtag_of((lispobj)addr) == LIST_POINTER_LOWTAG) &&
2018 page_table[addr_page_index].large_object)
2019 || !properly_tagged_descriptor_p(addr, object_start))
2020 return 0;
2022 return object_start;
2023 #endif
2026 /* Adjust large bignum and vector objects. This will adjust the
2027 * allocated region if the size has shrunk, and move unboxed objects
2028 * into unboxed pages. The pages are not promoted here, and the
2029 * promoted region is not added to the new_regions; this is really
2030 * only designed to be called from preserve_pointer(). Shouldn't fail
2031 * if this is missed, just may delay the moving of objects to unboxed
2032 * pages, and the freeing of pages. */
2033 static void
2034 maybe_adjust_large_object(lispobj *where)
2036 page_index_t first_page;
2037 page_index_t next_page;
2038 sword_t nwords;
2040 uword_t remaining_bytes;
2041 uword_t bytes_freed;
2042 uword_t old_bytes_used;
2044 int boxed;
2046 /* Check whether it's a vector or bignum object. */
2047 lispobj widetag = widetag_of(where[0]);
2048 if (widetag == SIMPLE_VECTOR_WIDETAG)
2049 boxed = BOXED_PAGE_FLAG;
2050 else if (specialized_vector_widetag_p(widetag) || widetag == BIGNUM_WIDETAG)
2051 boxed = UNBOXED_PAGE_FLAG;
2052 else
2053 return;
2055 /* Find its current size. */
2056 nwords = sizetab[widetag](where);
2058 first_page = find_page_index((void *)where);
2059 gc_assert(first_page >= 0);
2061 /* Note: Any page write-protection must be removed, else a later
2062 * scavenge_newspace may incorrectly not scavenge these pages.
2063 * This would not be necessary if they are added to the new areas,
2064 * but lets do it for them all (they'll probably be written
2065 * anyway?). */
2067 gc_assert(page_starts_contiguous_block_p(first_page));
2069 next_page = first_page;
2070 remaining_bytes = nwords*N_WORD_BYTES;
2071 while (remaining_bytes > GENCGC_CARD_BYTES) {
2072 gc_assert(page_table[next_page].gen == from_space);
2073 gc_assert(page_allocated_no_region_p(next_page));
2074 gc_assert(page_table[next_page].large_object);
2075 gc_assert(page_scan_start_offset(next_page) ==
2076 npage_bytes(next_page-first_page));
2077 gc_assert(page_bytes_used(next_page) == GENCGC_CARD_BYTES);
2079 page_table[next_page].allocated = boxed;
2081 /* Shouldn't be write-protected at this stage. Essential that the
2082 * pages aren't. */
2083 gc_assert(!page_table[next_page].write_protected);
2084 remaining_bytes -= GENCGC_CARD_BYTES;
2085 next_page++;
2088 /* Now only one page remains, but the object may have shrunk so
2089 * there may be more unused pages which will be freed. */
2091 /* Object may have shrunk but shouldn't have grown - check. */
2092 gc_assert(page_bytes_used(next_page) >= remaining_bytes);
2094 page_table[next_page].allocated = boxed;
2095 gc_assert(page_table[next_page].allocated ==
2096 page_table[first_page].allocated);
2098 /* Adjust the bytes_used. */
2099 old_bytes_used = page_bytes_used(next_page);
2100 set_page_bytes_used(next_page, remaining_bytes);
2102 bytes_freed = old_bytes_used - remaining_bytes;
2104 /* Free any remaining pages; needs care. */
2105 next_page++;
2106 while ((old_bytes_used == GENCGC_CARD_BYTES) &&
2107 (page_table[next_page].gen == from_space) &&
2108 page_allocated_no_region_p(next_page) &&
2109 page_table[next_page].large_object &&
2110 (page_scan_start_offset(next_page) ==
2111 npage_bytes(next_page - first_page))) {
2112 /* It checks out OK, free the page. We don't need to both zeroing
2113 * pages as this should have been done before shrinking the
2114 * object. These pages shouldn't be write protected as they
2115 * should be zero filled. */
2116 gc_assert(page_table[next_page].write_protected == 0);
2118 old_bytes_used = page_bytes_used(next_page);
2119 page_table[next_page].allocated = FREE_PAGE_FLAG;
2120 set_page_bytes_used(next_page, 0);
2121 bytes_freed += old_bytes_used;
2122 next_page++;
2125 if ((bytes_freed > 0) && gencgc_verbose) {
2126 FSHOW((stderr,
2127 "/maybe_adjust_large_object() freed %d\n",
2128 bytes_freed));
2131 generations[from_space].bytes_allocated -= bytes_freed;
2132 bytes_allocated -= bytes_freed;
2134 return;
2137 #if !(defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
2138 # define hopscotch_init()
2139 # define hopscotch_reset(a)
2140 # define scavenge_pinned_ranges()
2141 # define wipe_nonpinned_words()
2142 # define hopscotch_create(a,b,c,d,e)
2143 # define hopscotch_log_stats(a,b)
2144 /* After scavenging of the roots is done, we go back to the pinned objects
2145 * and look within them for pointers. While heap_scavenge() could certainly
2146 * do this, it would potentially lead to extra work, since we can't know
2147 * whether any given object has been examined at least once, since there is
2148 * no telltale forwarding-pointer. The easiest thing to do is defer all
2149 * pinned objects to a subsequent pass, as is done here.
2151 #else
2152 static void
2153 scavenge_pinned_ranges()
2155 int i;
2156 lispobj key;
2157 for_each_hopscotch_key(i, key, pinned_objects) {
2158 lispobj* obj = native_pointer(key);
2159 lispobj header = *obj;
2160 // Never invoke scavenger on a simple-fun, just code components.
2161 if (is_cons_half(header))
2162 scavenge(obj, 2);
2163 else if (widetag_of(header) != SIMPLE_FUN_WIDETAG)
2164 scavtab[widetag_of(header)](obj, header);
2168 /* Zero out the byte ranges on small object pages marked dont_move,
2169 * carefully skipping over objects in the pin hashtable.
2170 * TODO: by recording an additional bit per page indicating whether
2171 * there is more than one pinned object on it, we could avoid qsort()
2172 * except in the case where there is more than one. */
2173 static void
2174 wipe_nonpinned_words()
2176 void gc_heapsort_uwords(uword_t*, int);
2177 // Loop over the keys in pinned_objects and pack them densely into
2178 // the same array - pinned_objects.keys[] - but skip any simple-funs.
2179 // Admittedly this is abstraction breakage.
2180 int limit = hopscotch_max_key_index(pinned_objects);
2181 int n_pins = 0, i;
2182 for (i = 0; i <= limit; ++i) {
2183 lispobj key = pinned_objects.keys[i];
2184 if (key) {
2185 lispobj* obj = native_pointer(key);
2186 // No need to check for is_cons_half() - it will be false
2187 // on a simple-fun header, and that's the correct answer.
2188 if (widetag_of(*obj) != SIMPLE_FUN_WIDETAG)
2189 pinned_objects.keys[n_pins++] = (uword_t)obj;
2192 // Store a sentinel at the end. Even if n_pins = table capacity (unlikely),
2193 // it is safe to write one more word, because the hops[] array immediately
2194 // follows the keys[] array in memory. At worst, 2 elements of hops[]
2195 // are clobbered, which is irrelevant since the table has already been
2196 // rendered unusable by stealing its key array for a different purpose.
2197 pinned_objects.keys[n_pins] = 0;
2198 // Don't touch pinned_objects.count in case the reset function uses it
2199 // to decide how to resize for next use (which it doesn't, but could).
2200 gc_n_stack_pins = n_pins;
2201 // Order by ascending address, stopping short of the sentinel.
2202 gc_heapsort_uwords(pinned_objects.keys, n_pins);
2203 #if 0
2204 printf("Sorted pin list:\n");
2205 for (i = 0; i < n_pins; ++i) {
2206 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2207 if (!is_cons_half(*obj))
2208 printf("%p: %5d words\n", obj, (int)sizetab[widetag_of(*obj)](obj));
2209 else printf("%p: CONS\n", obj);
2211 #endif
2212 // Each entry in the pinned objects demarcates two ranges to be cleared:
2213 // - the range preceding it back to either the page start, or prior object.
2214 // - the range after it, up to the lesser of page bytes used or next object.
2215 uword_t preceding_object = 0;
2216 uword_t this_page_end = 0;
2217 #define page_base_address(x) (x&~(GENCGC_CARD_BYTES-1))
2218 for (i = 0; i < n_pins; ++i) {
2219 // Handle the preceding range. If this object is on the same page as
2220 // its predecessor, then intervening bytes were already zeroed.
2221 // If not, then start a new page and do some bookkeeping.
2222 lispobj* obj = (lispobj*)pinned_objects.keys[i];
2223 uword_t this_page_base = page_base_address((uword_t)obj);
2224 /* printf("i=%d obj=%p base=%p\n", i, obj, (void*)this_page_base); */
2225 if (this_page_base > page_base_address(preceding_object)) {
2226 bzero((void*)this_page_base,
2227 addr_diff((void*)obj, (void*)this_page_base));
2228 // Move the page to newspace
2229 page_index_t page = find_page_index(obj);
2230 int used = page_bytes_used(page);
2231 this_page_end = this_page_base + used;
2232 /* printf(" Clearing %p .. %p (limit=%p)\n",
2233 (void*)this_page_base, obj, (void*)this_page_end); */
2234 generations[new_space].bytes_allocated += used;
2235 generations[page_table[page].gen].bytes_allocated -= used;
2236 page_table[page].gen = new_space;
2237 page_table[page].has_pins = 0;
2239 // Handle the following range.
2240 lispobj word = *obj;
2241 size_t nwords = is_cons_half(word) ? 2 : sizetab[widetag_of(word)](obj);
2242 uword_t range_start = (uword_t)(obj + nwords);
2243 uword_t range_end = this_page_end;
2244 // There is always an i+1'th key due to the sentinel value.
2245 if (page_base_address(pinned_objects.keys[i+1]) == this_page_base)
2246 range_end = pinned_objects.keys[i+1];
2247 /* printf(" Clearing %p .. %p\n", (void*)range_start, (void*)range_end); */
2248 bzero((void*)range_start,
2249 addr_diff((void*)range_end, (void*)range_start));
2250 preceding_object = (uword_t)obj;
2254 /* Add 'object' to the hashtable, and if the object is a code component,
2255 * then also add all of the embedded simple-funs.
2256 * The rationale for the extra work on code components is that without it,
2257 * every test of pinned_p() on an object would have to check if the pointer
2258 * is to a simple-fun - entailing an extra read of the header - and mapping
2259 * to its code component if so. Since more calls to pinned_p occur than to
2260 * pin_object, the extra burden should be on this function.
2261 * Experimentation bears out that this is the better technique.
2262 * Also, we wouldn't often expect code components in the collected generation
2263 * so the extra work here is quite minimal, even if it can generally add to
2264 * the number of keys in the hashtable.
2266 static void
2267 pin_object(lispobj object)
2269 if (!hopscotch_containsp(&pinned_objects, object)) {
2270 hopscotch_insert(&pinned_objects, object, 1);
2271 struct code* maybe_code = (struct code*)native_pointer(object);
2272 if (widetag_of(maybe_code->header) == CODE_HEADER_WIDETAG) {
2273 for_each_simple_fun(i, fun, maybe_code, 0, {
2274 hopscotch_insert(&pinned_objects,
2275 make_lispobj(fun, FUN_POINTER_LOWTAG),
2281 #endif
2283 /* Take a possible pointer to a Lisp object and mark its page in the
2284 * page_table so that it will not be relocated during a GC.
2286 * This involves locating the page it points to, then backing up to
2287 * the start of its region, then marking all pages dont_move from there
2288 * up to the first page that's not full or has a different generation
2290 * It is assumed that all the page static flags have been cleared at
2291 * the start of a GC.
2293 * It is also assumed that the current gc_alloc() region has been
2294 * flushed and the tables updated. */
2296 // TODO: there's probably a way to be a little more efficient here.
2297 // As things are, we start by finding the object that encloses 'addr',
2298 // then we see if 'addr' was a "valid" Lisp pointer to that object
2299 // - meaning we expect the correct lowtag on the pointer - except
2300 // that for code objects we don't require a correct lowtag
2301 // and we allow a pointer to anywhere in the object.
2303 // It should be possible to avoid calling search_dynamic_space
2304 // more of the time. First, check if the page pointed to might hold code.
2305 // If it does, then we continue regardless of the pointer's lowtag
2306 // (because of the special allowance). If the page definitely does *not*
2307 // hold code, then we require up front that the lowtake make sense,
2308 // by doing the same checks that are in properly_tagged_descriptor_p.
2310 // Problem: when code is allocated from a per-thread region,
2311 // does it ensure that the occupied pages are flagged as having code?
2313 static void
2314 preserve_pointer(void *addr)
2316 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2317 /* Immobile space MUST be lower than dynamic space,
2318 or else this test needs to be revised */
2319 if (addr < (void*)IMMOBILE_SPACE_END) {
2320 extern void immobile_space_preserve_pointer(void*);
2321 immobile_space_preserve_pointer(addr);
2322 return;
2324 #endif
2325 page_index_t addr_page_index = find_page_index(addr);
2326 lispobj *object_start;
2328 if (addr_page_index == -1
2329 || (object_start = conservative_root_p(addr, addr_page_index)) == 0)
2330 return;
2332 /* (Now that we know that addr_page_index is in range, it's
2333 * safe to index into page_table[] with it.) */
2334 unsigned int region_allocation = page_table[addr_page_index].allocated;
2336 /* Find the beginning of the region. Note that there may be
2337 * objects in the region preceding the one that we were passed a
2338 * pointer to: if this is the case, we will write-protect all the
2339 * previous objects' pages too. */
2341 #if 0
2342 /* I think this'd work just as well, but without the assertions.
2343 * -dan 2004.01.01 */
2344 page_index_t first_page = find_page_index(page_scan_start(addr_page_index))
2345 #else
2346 page_index_t first_page = addr_page_index;
2347 while (!page_starts_contiguous_block_p(first_page)) {
2348 --first_page;
2349 /* Do some checks. */
2350 gc_assert(page_bytes_used(first_page) == GENCGC_CARD_BYTES);
2351 gc_assert(page_table[first_page].gen == from_space);
2352 gc_assert(page_table[first_page].allocated == region_allocation);
2354 #endif
2356 /* Adjust any large objects before promotion as they won't be
2357 * copied after promotion. */
2358 if (page_table[first_page].large_object) {
2359 maybe_adjust_large_object(page_address(first_page));
2360 /* It may have moved to unboxed pages. */
2361 region_allocation = page_table[first_page].allocated;
2364 /* Now work forward until the end of this contiguous area is found,
2365 * marking all pages as dont_move. */
2366 page_index_t i;
2367 for (i = first_page; ;i++) {
2368 gc_assert(page_table[i].allocated == region_allocation);
2370 /* Mark the page static. */
2371 page_table[i].dont_move = 1;
2373 /* It is essential that the pages are not write protected as
2374 * they may have pointers into the old-space which need
2375 * scavenging. They shouldn't be write protected at this
2376 * stage. */
2377 gc_assert(!page_table[i].write_protected);
2379 /* Check whether this is the last page in this contiguous block.. */
2380 if (page_ends_contiguous_block_p(i, from_space))
2381 break;
2384 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2385 /* Do not do this for multi-page objects. Those pages do not need
2386 * object wipeout anyway.
2388 if (do_wipe_p && i == first_page) { // single-page object
2389 lispobj word = *object_start;
2390 int lowtag = is_cons_half(word) ?
2391 LIST_POINTER_LOWTAG : lowtag_for_widetag[widetag_of(word)>>2];
2392 pin_object(make_lispobj(object_start, lowtag));
2393 page_table[i].has_pins = 1;
2395 #endif
2397 /* Check that the page is now static. */
2398 gc_assert(page_table[addr_page_index].dont_move != 0);
2401 /* If the given page is not write-protected, then scan it for pointers
2402 * to younger generations or the top temp. generation, if no
2403 * suspicious pointers are found then the page is write-protected.
2405 * Care is taken to check for pointers to the current gc_alloc()
2406 * region if it is a younger generation or the temp. generation. This
2407 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2408 * the gc_alloc_generation does not need to be checked as this is only
2409 * called from scavenge_generation() when the gc_alloc generation is
2410 * younger, so it just checks if there is a pointer to the current
2411 * region.
2413 * We return 1 if the page was write-protected, else 0. */
2414 static int
2415 update_page_write_prot(page_index_t page)
2417 generation_index_t gen = page_table[page].gen;
2418 sword_t j;
2419 int wp_it = 1;
2420 void **page_addr = (void **)page_address(page);
2421 sword_t num_words = page_bytes_used(page) / N_WORD_BYTES;
2423 /* Shouldn't be a free page. */
2424 gc_assert(!page_free_p(page));
2425 gc_assert(page_bytes_used(page) != 0);
2427 /* Skip if it's already write-protected, pinned, or unboxed */
2428 if (page_table[page].write_protected
2429 /* FIXME: What's the reason for not write-protecting pinned pages? */
2430 || page_table[page].dont_move
2431 || page_unboxed_p(page))
2432 return (0);
2434 /* Scan the page for pointers to younger generations or the
2435 * top temp. generation. */
2437 /* This is conservative: any word satisfying is_lisp_pointer() is
2438 * assumed to be a pointer. To do otherwise would require a family
2439 * of scavenge-like functions. */
2440 for (j = 0; j < num_words; j++) {
2441 void *ptr = *(page_addr+j);
2442 page_index_t index;
2443 lispobj __attribute__((unused)) header;
2445 if (!is_lisp_pointer((lispobj)ptr))
2446 continue;
2447 /* Check that it's in the dynamic space */
2448 if ((index = find_page_index(ptr)) != -1) {
2449 if (/* Does it point to a younger or the temp. generation? */
2450 (!page_free_p(index)
2451 && (page_bytes_used(index) != 0)
2452 && ((page_table[index].gen < gen)
2453 || (page_table[index].gen == SCRATCH_GENERATION)))
2455 /* Or does it point within a current gc_alloc() region? */
2456 || ((boxed_region.start_addr <= ptr)
2457 && (ptr <= boxed_region.free_pointer))
2458 || ((unboxed_region.start_addr <= ptr)
2459 && (ptr <= unboxed_region.free_pointer))) {
2460 wp_it = 0;
2461 break;
2464 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2465 else if ((index = find_immobile_page_index(ptr)) >= 0 &&
2466 other_immediate_lowtag_p(header = *native_pointer((lispobj)ptr))) {
2467 // This is *possibly* a pointer to an object in immobile space,
2468 // given that above two conditions were satisfied.
2469 // But unlike in the dynamic space case, we need to read a byte
2470 // from the object to determine its generation, which requires care.
2471 // Consider an unboxed word that looks like a pointer to a word that
2472 // looks like fun-header-widetag. We can't naively back up to the
2473 // underlying code object since the alleged header might not be one.
2474 int obj_gen = gen; // Make comparison fail if we fall through
2475 if (lowtag_of((lispobj)ptr) != FUN_POINTER_LOWTAG) {
2476 obj_gen = __immobile_obj_generation(native_pointer((lispobj)ptr));
2477 } else if (widetag_of(header) == SIMPLE_FUN_WIDETAG) {
2478 lispobj* code = fun_code_header((lispobj)ptr - FUN_POINTER_LOWTAG);
2479 // This is a heuristic, since we're not actually looking for
2480 // an object boundary. Precise scanning of 'page' would obviate
2481 // the guard conditions here.
2482 if ((lispobj)code >= IMMOBILE_VARYOBJ_SUBSPACE_START
2483 && widetag_of(*code) == CODE_HEADER_WIDETAG)
2484 obj_gen = __immobile_obj_generation(code);
2486 // A bogus generation number implies a not-really-pointer,
2487 // but it won't cause misbehavior.
2488 if (obj_gen < gen || obj_gen == SCRATCH_GENERATION) {
2489 wp_it = 0;
2490 break;
2493 #endif
2496 if (wp_it == 1) {
2497 /* Write-protect the page. */
2498 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2500 os_protect((void *)page_addr,
2501 GENCGC_CARD_BYTES,
2502 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2504 /* Note the page as protected in the page tables. */
2505 page_table[page].write_protected = 1;
2508 return (wp_it);
2511 /* Is this page holding a normal (non-hashtable) large-object
2512 * simple-vector? */
2513 static inline boolean large_simple_vector_p(page_index_t page) {
2514 if (!page_table[page].large_object)
2515 return 0;
2516 lispobj object = *(lispobj *)page_address(page);
2517 return widetag_of(object) == SIMPLE_VECTOR_WIDETAG &&
2518 (HeaderValue(object) & 0xFF) == subtype_VectorNormal;
2522 /* Scavenge all generations from FROM to TO, inclusive, except for
2523 * new_space which needs special handling, as new objects may be
2524 * added which are not checked here - use scavenge_newspace generation.
2526 * Write-protected pages should not have any pointers to the
2527 * from_space so do need scavenging; thus write-protected pages are
2528 * not always scavenged. There is some code to check that these pages
2529 * are not written; but to check fully the write-protected pages need
2530 * to be scavenged by disabling the code to skip them.
2532 * Under the current scheme when a generation is GCed the younger
2533 * generations will be empty. So, when a generation is being GCed it
2534 * is only necessary to scavenge the older generations for pointers
2535 * not the younger. So a page that does not have pointers to younger
2536 * generations does not need to be scavenged.
2538 * The write-protection can be used to note pages that don't have
2539 * pointers to younger pages. But pages can be written without having
2540 * pointers to younger generations. After the pages are scavenged here
2541 * they can be scanned for pointers to younger generations and if
2542 * there are none the page can be write-protected.
2544 * One complication is when the newspace is the top temp. generation.
2546 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2547 * that none were written, which they shouldn't be as they should have
2548 * no pointers to younger generations. This breaks down for weak
2549 * pointers as the objects contain a link to the next and are written
2550 * if a weak pointer is scavenged. Still it's a useful check. */
2551 static void
2552 scavenge_generations(generation_index_t from, generation_index_t to)
2554 page_index_t i;
2555 page_index_t num_wp = 0;
2557 #define SC_GEN_CK 0
2558 #if SC_GEN_CK
2559 /* Clear the write_protected_cleared flags on all pages. */
2560 for (i = 0; i < page_table_pages; i++)
2561 page_table[i].write_protected_cleared = 0;
2562 #endif
2564 for (i = 0; i < last_free_page; i++) {
2565 generation_index_t generation = page_table[i].gen;
2566 if (page_boxed_p(i)
2567 && (page_bytes_used(i) != 0)
2568 && (generation != new_space)
2569 && (generation >= from)
2570 && (generation <= to)) {
2571 page_index_t last_page,j;
2572 int write_protected=1;
2574 /* This should be the start of a region */
2575 gc_assert(page_starts_contiguous_block_p(i));
2577 if (large_simple_vector_p(i)) {
2578 /* Scavenge only the unprotected pages of a
2579 * large-object vector, other large objects could be
2580 * handled as well, but vectors are easier to deal
2581 * with and are more likely to grow to very large
2582 * sizes where avoiding scavenging the whole thing is
2583 * worthwile */
2584 if (!page_table[i].write_protected) {
2585 scavenge((lispobj*)page_address(i) + 2,
2586 GENCGC_CARD_BYTES / N_WORD_BYTES - 2);
2587 update_page_write_prot(i);
2589 for (last_page = i + 1; ; last_page++) {
2590 lispobj* start = page_address(last_page);
2591 write_protected = page_table[last_page].write_protected;
2592 if (page_ends_contiguous_block_p(last_page, generation)) {
2593 if (!write_protected) {
2594 scavenge(start, page_bytes_used(last_page) / N_WORD_BYTES);
2595 update_page_write_prot(last_page);
2597 break;
2599 if (!write_protected) {
2600 scavenge(start, GENCGC_CARD_BYTES / N_WORD_BYTES);
2601 update_page_write_prot(last_page);
2604 } else {
2605 /* Now work forward until the end of the region */
2606 for (last_page = i; ; last_page++) {
2607 write_protected =
2608 write_protected && page_table[last_page].write_protected;
2609 if (page_ends_contiguous_block_p(last_page, generation))
2610 break;
2612 if (!write_protected) {
2613 heap_scavenge(page_address(i),
2614 (lispobj*)((char*)page_address(last_page)
2615 + page_bytes_used(last_page)));
2617 /* Now scan the pages and write protect those that
2618 * don't have pointers to younger generations. */
2619 if (enable_page_protection) {
2620 for (j = i; j <= last_page; j++) {
2621 num_wp += update_page_write_prot(j);
2624 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2625 FSHOW((stderr,
2626 "/write protected %d pages within generation %d\n",
2627 num_wp, generation));
2631 i = last_page;
2635 #if SC_GEN_CK
2636 /* Check that none of the write_protected pages in this generation
2637 * have been written to. */
2638 for (i = 0; i < page_table_pages; i++) {
2639 if (!page_free_p(i)
2640 && (page_bytes_used(i) != 0)
2641 && (page_table[i].gen == generation)
2642 && (page_table[i].write_protected_cleared != 0)) {
2643 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2644 FSHOW((stderr,
2645 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2646 page_bytes_used(i),
2647 scan_start_offset(page_table[i]),
2648 page_table[i].dont_move));
2649 lose("write to protected page %d in scavenge_generation()\n", i);
2652 #endif
2656 /* Scavenge a newspace generation. As it is scavenged new objects may
2657 * be allocated to it; these will also need to be scavenged. This
2658 * repeats until there are no more objects unscavenged in the
2659 * newspace generation.
2661 * To help improve the efficiency, areas written are recorded by
2662 * gc_alloc() and only these scavenged. Sometimes a little more will be
2663 * scavenged, but this causes no harm. An easy check is done that the
2664 * scavenged bytes equals the number allocated in the previous
2665 * scavenge.
2667 * Write-protected pages are not scanned except if they are marked
2668 * dont_move in which case they may have been promoted and still have
2669 * pointers to the from space.
2671 * Write-protected pages could potentially be written by alloc however
2672 * to avoid having to handle re-scavenging of write-protected pages
2673 * gc_alloc() does not write to write-protected pages.
2675 * New areas of objects allocated are recorded alternatively in the two
2676 * new_areas arrays below. */
2677 static struct new_area new_areas_1[NUM_NEW_AREAS];
2678 static struct new_area new_areas_2[NUM_NEW_AREAS];
2680 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2681 extern unsigned int immobile_scav_queue_count;
2682 extern void
2683 gc_init_immobile(),
2684 update_immobile_nursery_bits(),
2685 scavenge_immobile_roots(generation_index_t,generation_index_t),
2686 scavenge_immobile_newspace(),
2687 sweep_immobile_space(int raise),
2688 write_protect_immobile_space();
2689 #else
2690 #define immobile_scav_queue_count 0
2691 #endif
2693 /* Do one full scan of the new space generation. This is not enough to
2694 * complete the job as new objects may be added to the generation in
2695 * the process which are not scavenged. */
2696 static void
2697 scavenge_newspace_generation_one_scan(generation_index_t generation)
2699 page_index_t i;
2701 FSHOW((stderr,
2702 "/starting one full scan of newspace generation %d\n",
2703 generation));
2704 for (i = 0; i < last_free_page; i++) {
2705 /* Note that this skips over open regions when it encounters them. */
2706 if (page_boxed_p(i)
2707 && (page_bytes_used(i) != 0)
2708 && (page_table[i].gen == generation)
2709 && ((page_table[i].write_protected == 0)
2710 /* (This may be redundant as write_protected is now
2711 * cleared before promotion.) */
2712 || (page_table[i].dont_move == 1))) {
2713 page_index_t last_page;
2714 int all_wp=1;
2716 /* The scavenge will start at the scan_start_offset of
2717 * page i.
2719 * We need to find the full extent of this contiguous
2720 * block in case objects span pages.
2722 * Now work forward until the end of this contiguous area
2723 * is found. A small area is preferred as there is a
2724 * better chance of its pages being write-protected. */
2725 for (last_page = i; ;last_page++) {
2726 /* If all pages are write-protected and movable,
2727 * then no need to scavenge */
2728 all_wp=all_wp && page_table[last_page].write_protected &&
2729 !page_table[last_page].dont_move;
2731 /* Check whether this is the last page in this
2732 * contiguous block */
2733 if (page_ends_contiguous_block_p(last_page, generation))
2734 break;
2737 /* Do a limited check for write-protected pages. */
2738 if (!all_wp) {
2739 new_areas_ignore_page = last_page;
2740 heap_scavenge(page_scan_start(i),
2741 (lispobj*)((char*)page_address(last_page)
2742 + page_bytes_used(last_page)));
2744 i = last_page;
2747 FSHOW((stderr,
2748 "/done with one full scan of newspace generation %d\n",
2749 generation));
2752 /* Do a complete scavenge of the newspace generation. */
2753 static void
2754 scavenge_newspace_generation(generation_index_t generation)
2756 size_t i;
2758 /* the new_areas array currently being written to by gc_alloc() */
2759 struct new_area (*current_new_areas)[] = &new_areas_1;
2760 size_t current_new_areas_index;
2762 /* the new_areas created by the previous scavenge cycle */
2763 struct new_area (*previous_new_areas)[] = NULL;
2764 size_t previous_new_areas_index;
2766 /* Flush the current regions updating the tables. */
2767 gc_alloc_update_all_page_tables(0);
2769 /* Turn on the recording of new areas by gc_alloc(). */
2770 new_areas = current_new_areas;
2771 new_areas_index = 0;
2773 /* Don't need to record new areas that get scavenged anyway during
2774 * scavenge_newspace_generation_one_scan. */
2775 record_new_objects = 1;
2777 /* Start with a full scavenge. */
2778 scavenge_newspace_generation_one_scan(generation);
2780 /* Record all new areas now. */
2781 record_new_objects = 2;
2783 /* Give a chance to weak hash tables to make other objects live.
2784 * FIXME: The algorithm implemented here for weak hash table gcing
2785 * is O(W^2+N) as Bruno Haible warns in
2786 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2787 * see "Implementation 2". */
2788 scav_weak_hash_tables();
2790 /* Flush the current regions updating the tables. */
2791 gc_alloc_update_all_page_tables(0);
2793 /* Grab new_areas_index. */
2794 current_new_areas_index = new_areas_index;
2796 /*FSHOW((stderr,
2797 "The first scan is finished; current_new_areas_index=%d.\n",
2798 current_new_areas_index));*/
2800 while (current_new_areas_index > 0 || immobile_scav_queue_count) {
2801 /* Move the current to the previous new areas */
2802 previous_new_areas = current_new_areas;
2803 previous_new_areas_index = current_new_areas_index;
2805 /* Scavenge all the areas in previous new areas. Any new areas
2806 * allocated are saved in current_new_areas. */
2808 /* Allocate an array for current_new_areas; alternating between
2809 * new_areas_1 and 2 */
2810 if (previous_new_areas == &new_areas_1)
2811 current_new_areas = &new_areas_2;
2812 else
2813 current_new_areas = &new_areas_1;
2815 /* Set up for gc_alloc(). */
2816 new_areas = current_new_areas;
2817 new_areas_index = 0;
2819 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2820 scavenge_immobile_newspace();
2821 #endif
2822 /* Check whether previous_new_areas had overflowed. */
2823 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2825 /* New areas of objects allocated have been lost so need to do a
2826 * full scan to be sure! If this becomes a problem try
2827 * increasing NUM_NEW_AREAS. */
2828 if (gencgc_verbose) {
2829 SHOW("new_areas overflow, doing full scavenge");
2832 /* Don't need to record new areas that get scavenged
2833 * anyway during scavenge_newspace_generation_one_scan. */
2834 record_new_objects = 1;
2836 scavenge_newspace_generation_one_scan(generation);
2838 /* Record all new areas now. */
2839 record_new_objects = 2;
2841 scav_weak_hash_tables();
2843 /* Flush the current regions updating the tables. */
2844 gc_alloc_update_all_page_tables(0);
2846 } else {
2848 /* Work through previous_new_areas. */
2849 for (i = 0; i < previous_new_areas_index; i++) {
2850 page_index_t page = (*previous_new_areas)[i].page;
2851 size_t offset = (*previous_new_areas)[i].offset;
2852 size_t size = (*previous_new_areas)[i].size;
2853 gc_assert(size % N_WORD_BYTES == 0);
2854 lispobj *start = (lispobj*)((char*)page_address(page) + offset);
2855 heap_scavenge(start, (lispobj*)((char*)start + size));
2858 scav_weak_hash_tables();
2860 /* Flush the current regions updating the tables. */
2861 gc_alloc_update_all_page_tables(0);
2864 current_new_areas_index = new_areas_index;
2866 /*FSHOW((stderr,
2867 "The re-scan has finished; current_new_areas_index=%d.\n",
2868 current_new_areas_index));*/
2871 /* Turn off recording of areas allocated by gc_alloc(). */
2872 record_new_objects = 0;
2874 #if SC_NS_GEN_CK
2876 page_index_t i;
2877 /* Check that none of the write_protected pages in this generation
2878 * have been written to. */
2879 for (i = 0; i < page_table_pages; i++) {
2880 if (!page_free_p(i)
2881 && (page_bytes_used(i) != 0)
2882 && (page_table[i].gen == generation)
2883 && (page_table[i].write_protected_cleared != 0)
2884 && (page_table[i].dont_move == 0)) {
2885 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2886 i, generation, page_table[i].dont_move);
2890 #endif
2893 /* Un-write-protect all the pages in from_space. This is done at the
2894 * start of a GC else there may be many page faults while scavenging
2895 * the newspace (I've seen drive the system time to 99%). These pages
2896 * would need to be unprotected anyway before unmapping in
2897 * free_oldspace; not sure what effect this has on paging.. */
2898 static void
2899 unprotect_oldspace(void)
2901 page_index_t i;
2902 void *region_addr = 0;
2903 void *page_addr = 0;
2904 uword_t region_bytes = 0;
2906 for (i = 0; i < last_free_page; i++) {
2907 if (!page_free_p(i)
2908 && (page_bytes_used(i) != 0)
2909 && (page_table[i].gen == from_space)) {
2911 /* Remove any write-protection. We should be able to rely
2912 * on the write-protect flag to avoid redundant calls. */
2913 if (page_table[i].write_protected) {
2914 page_table[i].write_protected = 0;
2915 page_addr = page_address(i);
2916 if (!region_addr) {
2917 /* First region. */
2918 region_addr = page_addr;
2919 region_bytes = GENCGC_CARD_BYTES;
2920 } else if (region_addr + region_bytes == page_addr) {
2921 /* Region continue. */
2922 region_bytes += GENCGC_CARD_BYTES;
2923 } else {
2924 /* Unprotect previous region. */
2925 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2926 /* First page in new region. */
2927 region_addr = page_addr;
2928 region_bytes = GENCGC_CARD_BYTES;
2933 if (region_addr) {
2934 /* Unprotect last region. */
2935 os_protect(region_addr, region_bytes, OS_VM_PROT_ALL);
2939 /* Work through all the pages and free any in from_space. This
2940 * assumes that all objects have been copied or promoted to an older
2941 * generation. Bytes_allocated and the generation bytes_allocated
2942 * counter are updated. The number of bytes freed is returned. */
2943 static uword_t
2944 free_oldspace(void)
2946 uword_t bytes_freed = 0;
2947 page_index_t first_page, last_page;
2949 first_page = 0;
2951 do {
2952 /* Find a first page for the next region of pages. */
2953 while ((first_page < last_free_page)
2954 && (page_free_p(first_page)
2955 || (page_bytes_used(first_page) == 0)
2956 || (page_table[first_page].gen != from_space)))
2957 first_page++;
2959 if (first_page >= last_free_page)
2960 break;
2962 /* Find the last page of this region. */
2963 last_page = first_page;
2965 do {
2966 /* Free the page. */
2967 bytes_freed += page_bytes_used(last_page);
2968 generations[page_table[last_page].gen].bytes_allocated -=
2969 page_bytes_used(last_page);
2970 page_table[last_page].allocated = FREE_PAGE_FLAG;
2971 set_page_bytes_used(last_page, 0);
2972 /* Should already be unprotected by unprotect_oldspace(). */
2973 gc_assert(!page_table[last_page].write_protected);
2974 last_page++;
2976 while ((last_page < last_free_page)
2977 && !page_free_p(last_page)
2978 && (page_bytes_used(last_page) != 0)
2979 && (page_table[last_page].gen == from_space));
2981 #ifdef READ_PROTECT_FREE_PAGES
2982 os_protect(page_address(first_page),
2983 npage_bytes(last_page-first_page),
2984 OS_VM_PROT_NONE);
2985 #endif
2986 first_page = last_page;
2987 } while (first_page < last_free_page);
2989 bytes_allocated -= bytes_freed;
2990 return bytes_freed;
2993 #if 0
2994 /* Print some information about a pointer at the given address. */
2995 static void
2996 print_ptr(lispobj *addr)
2998 /* If addr is in the dynamic space then out the page information. */
2999 page_index_t pi1 = find_page_index((void*)addr);
3001 if (pi1 != -1)
3002 fprintf(stderr," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3003 addr,
3004 pi1,
3005 page_table[pi1].allocated,
3006 page_table[pi1].gen,
3007 page_bytes_used(pi1),
3008 scan_start_offset(page_table[pi1]),
3009 page_table[pi1].dont_move);
3010 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3011 *(addr-4),
3012 *(addr-3),
3013 *(addr-2),
3014 *(addr-1),
3015 *(addr-0),
3016 *(addr+1),
3017 *(addr+2),
3018 *(addr+3),
3019 *(addr+4));
3021 #endif
3023 static int
3024 is_in_stack_space(lispobj ptr)
3026 /* For space verification: Pointers can be valid if they point
3027 * to a thread stack space. This would be faster if the thread
3028 * structures had page-table entries as if they were part of
3029 * the heap space. */
3030 struct thread *th;
3031 for_each_thread(th) {
3032 if ((th->control_stack_start <= (lispobj *)ptr) &&
3033 (th->control_stack_end >= (lispobj *)ptr)) {
3034 return 1;
3037 return 0;
3040 // NOTE: This function can produces false failure indications,
3041 // usually related to dynamic space pointing to the stack of a
3042 // dead thread, but there may be other reasons as well.
3043 static void
3044 verify_range(lispobj *start, size_t words)
3046 extern int valid_lisp_pointer_p(lispobj);
3047 int is_in_readonly_space =
3048 (READ_ONLY_SPACE_START <= (uword_t)start &&
3049 (uword_t)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3050 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3051 int is_in_immobile_space =
3052 (IMMOBILE_SPACE_START <= (uword_t)start &&
3053 (uword_t)start < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3054 #endif
3056 lispobj *end = start + words;
3057 size_t count;
3058 for ( ; start < end ; start += count) {
3059 count = 1;
3060 lispobj thing = *start;
3061 lispobj __attribute__((unused)) pointee;
3063 if (is_lisp_pointer(thing)) {
3064 page_index_t page_index = find_page_index((void*)thing);
3065 sword_t to_readonly_space =
3066 (READ_ONLY_SPACE_START <= thing &&
3067 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3068 sword_t to_static_space =
3069 (STATIC_SPACE_START <= thing &&
3070 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3071 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3072 sword_t to_immobile_space =
3073 (IMMOBILE_SPACE_START <= thing &&
3074 thing < SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0)) ||
3075 (IMMOBILE_VARYOBJ_SUBSPACE_START <= thing &&
3076 thing < SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3077 #endif
3079 /* Does it point to the dynamic space? */
3080 if (page_index != -1) {
3081 /* If it's within the dynamic space it should point to a used page. */
3082 if (page_free_p(page_index))
3083 lose ("Ptr %p @ %p sees free page.\n", thing, start);
3084 if ((thing & (GENCGC_CARD_BYTES-1)) >= page_bytes_used(page_index))
3085 lose ("Ptr %p @ %p sees unallocated space.\n", thing, start);
3086 /* Check that it doesn't point to a forwarding pointer! */
3087 if (*native_pointer(thing) == 0x01) {
3088 lose("Ptr %p @ %p sees forwarding ptr.\n", thing, start);
3090 /* Check that its not in the RO space as it would then be a
3091 * pointer from the RO to the dynamic space. */
3092 if (is_in_readonly_space) {
3093 lose("ptr to dynamic space %p from RO space %x\n",
3094 thing, start);
3096 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3097 // verify all immobile space -> dynamic space pointers
3098 if (is_in_immobile_space && !valid_lisp_pointer_p(thing)) {
3099 lose("Ptr %p @ %p sees junk.\n", thing, start);
3101 #endif
3102 /* Does it point to a plausible object? This check slows
3103 * it down a lot (so it's commented out).
3105 * "a lot" is serious: it ate 50 minutes cpu time on
3106 * my duron 950 before I came back from lunch and
3107 * killed it.
3109 * FIXME: Add a variable to enable this
3110 * dynamically. */
3112 if (!valid_lisp_pointer_p((lispobj *)thing) {
3113 lose("ptr %p to invalid object %p\n", thing, start);
3116 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3117 } else if (to_immobile_space) {
3118 // the object pointed to must not have been discarded as garbage
3119 if (!other_immediate_lowtag_p(*native_pointer(thing))
3120 || immobile_filler_p(native_pointer(thing)))
3121 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing, start);
3122 // verify all pointers to immobile space
3123 if (!valid_lisp_pointer_p(thing))
3124 lose("Ptr %p @ %p sees junk.\n", thing, start);
3125 #endif
3126 } else {
3127 extern char __attribute__((unused)) funcallable_instance_tramp;
3128 /* Verify that it points to another valid space. */
3129 if (!to_readonly_space && !to_static_space
3130 && !is_in_stack_space(thing)) {
3131 lose("Ptr %p @ %p sees junk.\n", thing, start);
3134 continue;
3136 int widetag = widetag_of(thing);
3137 if (is_lisp_immediate(thing) || widetag == NO_TLS_VALUE_MARKER_WIDETAG) {
3138 /* skip immediates */
3139 } else if (!(other_immediate_lowtag_p(widetag)
3140 && lowtag_for_widetag[widetag>>2])) {
3141 lose("Unhandled widetag %p at %p\n", widetag, start);
3142 } else if (unboxed_obj_widetag_p(widetag)) {
3143 count = sizetab[widetag](start);
3144 } else switch(widetag) {
3145 /* boxed or partially boxed objects */
3146 // FIXME: x86-64 can have partially unboxed FINs. The raw words
3147 // are at the moment valid fixnums by blind luck.
3148 case INSTANCE_WIDETAG:
3149 if (instance_layout(start)) {
3150 sword_t nslots = instance_length(thing) | 1;
3151 instance_scan(verify_range, start+1, nslots,
3152 ((struct layout*)
3153 native_pointer(instance_layout(start)))->bitmap);
3154 count = 1 + nslots;
3156 break;
3157 case CODE_HEADER_WIDETAG:
3159 struct code *code = (struct code *) start;
3160 sword_t nheader_words = code_header_words(code->header);
3161 /* Scavenge the boxed section of the code data block */
3162 verify_range(start + 1, nheader_words - 1);
3164 /* Scavenge the boxed section of each function
3165 * object in the code data block. */
3166 for_each_simple_fun(i, fheaderp, code, 1, {
3167 verify_range(SIMPLE_FUN_SCAV_START(fheaderp),
3168 SIMPLE_FUN_SCAV_NWORDS(fheaderp)); });
3169 count = nheader_words + code_instruction_words(code->code_size);
3170 break;
3172 #ifdef LISP_FEATURE_IMMOBILE_CODE
3173 case FDEFN_WIDETAG:
3174 verify_range(start + 1, 2);
3175 pointee = fdefn_raw_referent((struct fdefn*)start);
3176 verify_range(&pointee, 1);
3177 count = CEILING(sizeof (struct fdefn)/sizeof(lispobj), 2);
3178 break;
3179 #endif
3183 static uword_t verify_space(lispobj start, lispobj end) {
3184 verify_range((lispobj*)start, (end-start)>>WORD_SHIFT);
3185 return 0;
3188 static void verify_dynamic_space();
3190 static void
3191 verify_gc(void)
3193 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3194 # ifdef __linux__
3195 // Try this verification if marknsweep was compiled with extra debugging.
3196 // But weak symbols don't work on macOS.
3197 extern void __attribute__((weak)) check_varyobj_pages();
3198 if (&check_varyobj_pages) check_varyobj_pages();
3199 # endif
3200 verify_space(IMMOBILE_SPACE_START,
3201 SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER,0));
3202 verify_space(IMMOBILE_VARYOBJ_SUBSPACE_START,
3203 SymbolValue(IMMOBILE_SPACE_FREE_POINTER,0));
3204 #endif
3205 struct thread *th;
3206 for_each_thread(th) {
3207 verify_space((lispobj)th->binding_stack_start,
3208 (lispobj)get_binding_stack_pointer(th));
3210 verify_space(READ_ONLY_SPACE_START,
3211 SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3212 verify_space(STATIC_SPACE_START,
3213 SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3214 verify_dynamic_space();
3217 /* Call 'proc' with pairs of addresses demarcating ranges in the
3218 * specified generation.
3219 * Stop if any invocation returns non-zero, and return that value */
3220 uword_t
3221 walk_generation(uword_t (*proc)(lispobj*,lispobj*,uword_t),
3222 generation_index_t generation, uword_t extra)
3224 page_index_t i;
3225 int genmask = generation >= 0 ? 1 << generation : ~0;
3227 for (i = 0; i < last_free_page; i++) {
3228 if (!page_free_p(i)
3229 && (page_bytes_used(i) != 0)
3230 && ((1 << page_table[i].gen) & genmask)) {
3231 page_index_t last_page;
3233 /* This should be the start of a contiguous block */
3234 gc_assert(page_starts_contiguous_block_p(i));
3236 /* Need to find the full extent of this contiguous block in case
3237 objects span pages. */
3239 /* Now work forward until the end of this contiguous area is
3240 found. */
3241 for (last_page = i; ;last_page++)
3242 /* Check whether this is the last page in this contiguous
3243 * block. */
3244 if (page_ends_contiguous_block_p(last_page, page_table[i].gen))
3245 break;
3247 uword_t result =
3248 proc(page_address(i),
3249 (lispobj*)(page_bytes_used(last_page)
3250 + (char*)page_address(last_page)),
3251 extra);
3252 if (result) return result;
3254 i = last_page;
3257 return 0;
3259 static void verify_generation(generation_index_t generation)
3261 walk_generation((uword_t(*)(lispobj*,lispobj*,uword_t))verify_space,
3262 generation, 0);
3265 /* Check that all the free space is zero filled. */
3266 static void
3267 verify_zero_fill(void)
3269 page_index_t page;
3271 for (page = 0; page < last_free_page; page++) {
3272 if (page_free_p(page)) {
3273 /* The whole page should be zero filled. */
3274 sword_t *start_addr = (sword_t *)page_address(page);
3275 sword_t i;
3276 for (i = 0; i < (sword_t)GENCGC_CARD_BYTES/N_WORD_BYTES; i++) {
3277 if (start_addr[i] != 0) {
3278 lose("free page not zero at %x\n", start_addr + i);
3281 } else {
3282 sword_t free_bytes = GENCGC_CARD_BYTES - page_bytes_used(page);
3283 if (free_bytes > 0) {
3284 sword_t *start_addr = (sword_t *)((uword_t)page_address(page)
3285 + page_bytes_used(page));
3286 sword_t size = free_bytes / N_WORD_BYTES;
3287 sword_t i;
3288 for (i = 0; i < size; i++) {
3289 if (start_addr[i] != 0) {
3290 lose("free region not zero at %x\n", start_addr + i);
3298 /* External entry point for verify_zero_fill */
3299 void
3300 gencgc_verify_zero_fill(void)
3302 /* Flush the alloc regions updating the tables. */
3303 gc_alloc_update_all_page_tables(1);
3304 SHOW("verifying zero fill");
3305 verify_zero_fill();
3308 static void
3309 verify_dynamic_space(void)
3311 verify_generation(-1);
3312 if (gencgc_enable_verify_zero_fill)
3313 verify_zero_fill();
3316 /* Write-protect all the dynamic boxed pages in the given generation. */
3317 static void
3318 write_protect_generation_pages(generation_index_t generation)
3320 page_index_t start;
3322 gc_assert(generation < SCRATCH_GENERATION);
3324 for (start = 0; start < last_free_page; start++) {
3325 if (protect_page_p(start, generation)) {
3326 void *page_start;
3327 page_index_t last;
3329 /* Note the page as protected in the page tables. */
3330 page_table[start].write_protected = 1;
3332 for (last = start + 1; last < last_free_page; last++) {
3333 if (!protect_page_p(last, generation))
3334 break;
3335 page_table[last].write_protected = 1;
3338 page_start = (void *)page_address(start);
3340 os_protect(page_start,
3341 npage_bytes(last - start),
3342 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3344 start = last;
3348 if (gencgc_verbose > 1) {
3349 FSHOW((stderr,
3350 "/write protected %d of %d pages in generation %d\n",
3351 count_write_protect_generation_pages(generation),
3352 count_generation_pages(generation),
3353 generation));
3357 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3358 static void
3359 preserve_context_registers (void (*proc)(os_context_register_t), os_context_t *c)
3361 void **ptr;
3362 /* On Darwin the signal context isn't a contiguous block of memory,
3363 * so just preserve_pointering its contents won't be sufficient.
3365 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3366 #if defined LISP_FEATURE_X86
3367 proc(*os_context_register_addr(c,reg_EAX));
3368 proc(*os_context_register_addr(c,reg_ECX));
3369 proc(*os_context_register_addr(c,reg_EDX));
3370 proc(*os_context_register_addr(c,reg_EBX));
3371 proc(*os_context_register_addr(c,reg_ESI));
3372 proc(*os_context_register_addr(c,reg_EDI));
3373 proc(*os_context_pc_addr(c));
3374 #elif defined LISP_FEATURE_X86_64
3375 proc(*os_context_register_addr(c,reg_RAX));
3376 proc(*os_context_register_addr(c,reg_RCX));
3377 proc(*os_context_register_addr(c,reg_RDX));
3378 proc(*os_context_register_addr(c,reg_RBX));
3379 proc(*os_context_register_addr(c,reg_RSI));
3380 proc(*os_context_register_addr(c,reg_RDI));
3381 proc(*os_context_register_addr(c,reg_R8));
3382 proc(*os_context_register_addr(c,reg_R9));
3383 proc(*os_context_register_addr(c,reg_R10));
3384 proc(*os_context_register_addr(c,reg_R11));
3385 proc(*os_context_register_addr(c,reg_R12));
3386 proc(*os_context_register_addr(c,reg_R13));
3387 proc(*os_context_register_addr(c,reg_R14));
3388 proc(*os_context_register_addr(c,reg_R15));
3389 proc(*os_context_pc_addr(c));
3390 #else
3391 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3392 #endif
3393 #endif
3394 #if !defined(LISP_FEATURE_WIN32)
3395 for(ptr = ((void **)(c+1))-1; ptr>=(void **)c; ptr--) {
3396 proc((os_context_register_t)*ptr);
3398 #endif
3400 #endif
3402 static void
3403 move_pinned_pages_to_newspace()
3405 page_index_t i;
3407 /* scavenge() will evacuate all oldspace pages, but no newspace
3408 * pages. Pinned pages are precisely those pages which must not
3409 * be evacuated, so move them to newspace directly. */
3411 for (i = 0; i < last_free_page; i++) {
3412 if (page_table[i].dont_move &&
3413 /* dont_move is cleared lazily, so validate the space as well. */
3414 page_table[i].gen == from_space) {
3415 if (do_wipe_p && page_table[i].has_pins) {
3416 // do not move to newspace after all, this will be word-wiped
3417 continue;
3419 page_table[i].gen = new_space;
3420 /* And since we're moving the pages wholesale, also adjust
3421 * the generation allocation counters. */
3422 int used = page_bytes_used(i);
3423 generations[new_space].bytes_allocated += used;
3424 generations[from_space].bytes_allocated -= used;
3429 /* Garbage collect a generation. If raise is 0 then the remains of the
3430 * generation are not raised to the next generation. */
3431 static void
3432 garbage_collect_generation(generation_index_t generation, int raise)
3434 page_index_t i;
3435 struct thread *th;
3437 gc_assert(generation <= HIGHEST_NORMAL_GENERATION);
3439 /* The oldest generation can't be raised. */
3440 gc_assert((generation != HIGHEST_NORMAL_GENERATION) || (raise == 0));
3442 /* Check if weak hash tables were processed in the previous GC. */
3443 gc_assert(weak_hash_tables == NULL);
3445 /* Initialize the weak pointer list. */
3446 weak_pointers = NULL;
3448 /* When a generation is not being raised it is transported to a
3449 * temporary generation (NUM_GENERATIONS), and lowered when
3450 * done. Set up this new generation. There should be no pages
3451 * allocated to it yet. */
3452 if (!raise) {
3453 gc_assert(generations[SCRATCH_GENERATION].bytes_allocated == 0);
3456 /* Set the global src and dest. generations */
3457 from_space = generation;
3458 if (raise)
3459 new_space = generation+1;
3460 else
3461 new_space = SCRATCH_GENERATION;
3463 /* Change to a new space for allocation, resetting the alloc_start_page */
3464 gc_alloc_generation = new_space;
3465 generations[new_space].alloc_start_page = 0;
3466 generations[new_space].alloc_unboxed_start_page = 0;
3467 generations[new_space].alloc_large_start_page = 0;
3468 generations[new_space].alloc_large_unboxed_start_page = 0;
3470 hopscotch_reset(&pinned_objects);
3471 /* Before any pointers are preserved, the dont_move flags on the
3472 * pages need to be cleared. */
3473 /* FIXME: consider moving this bitmap into its own range of words,
3474 * out of the page table. Then we can just bzero() it.
3475 * This will also obviate the extra test at the comment
3476 * "dont_move is cleared lazily" in move_pinned_pages_to_newspace().
3478 for (i = 0; i < last_free_page; i++)
3479 if(page_table[i].gen==from_space) {
3480 page_table[i].dont_move = 0;
3483 /* Un-write-protect the old-space pages. This is essential for the
3484 * promoted pages as they may contain pointers into the old-space
3485 * which need to be scavenged. It also helps avoid unnecessary page
3486 * faults as forwarding pointers are written into them. They need to
3487 * be un-protected anyway before unmapping later. */
3488 unprotect_oldspace();
3490 /* Scavenge the stacks' conservative roots. */
3492 /* there are potentially two stacks for each thread: the main
3493 * stack, which may contain Lisp pointers, and the alternate stack.
3494 * We don't ever run Lisp code on the altstack, but it may
3495 * host a sigcontext with lisp objects in it */
3497 /* what we need to do: (1) find the stack pointer for the main
3498 * stack; scavenge it (2) find the interrupt context on the
3499 * alternate stack that might contain lisp values, and scavenge
3500 * that */
3502 /* we assume that none of the preceding applies to the thread that
3503 * initiates GC. If you ever call GC from inside an altstack
3504 * handler, you will lose. */
3506 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3507 /* And if we're saving a core, there's no point in being conservative. */
3508 if (conservative_stack) {
3509 for_each_thread(th) {
3510 void **ptr;
3511 void **esp=(void **)-1;
3512 if (th->state == STATE_DEAD)
3513 continue;
3514 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3515 /* Conservative collect_garbage is always invoked with a
3516 * foreign C call or an interrupt handler on top of every
3517 * existing thread, so the stored SP in each thread
3518 * structure is valid, no matter which thread we are looking
3519 * at. For threads that were running Lisp code, the pitstop
3520 * and edge functions maintain this value within the
3521 * interrupt or exception handler. */
3522 esp = os_get_csp(th);
3523 assert_on_stack(th, esp);
3525 /* In addition to pointers on the stack, also preserve the
3526 * return PC, the only value from the context that we need
3527 * in addition to the SP. The return PC gets saved by the
3528 * foreign call wrapper, and removed from the control stack
3529 * into a register. */
3530 preserve_pointer(th->pc_around_foreign_call);
3532 /* And on platforms with interrupts: scavenge ctx registers. */
3534 /* Disabled on Windows, because it does not have an explicit
3535 * stack of `interrupt_contexts'. The reported CSP has been
3536 * chosen so that the current context on the stack is
3537 * covered by the stack scan. See also set_csp_from_context(). */
3538 # ifndef LISP_FEATURE_WIN32
3539 if (th != arch_os_get_current_thread()) {
3540 long k = fixnum_value(
3541 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3542 while (k > 0)
3543 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3544 th->interrupt_contexts[--k]);
3546 # endif
3547 # elif defined(LISP_FEATURE_SB_THREAD)
3548 sword_t i,free;
3549 if(th==arch_os_get_current_thread()) {
3550 /* Somebody is going to burn in hell for this, but casting
3551 * it in two steps shuts gcc up about strict aliasing. */
3552 esp = (void **)((void *)&raise);
3553 } else {
3554 void **esp1;
3555 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3556 for(i=free-1;i>=0;i--) {
3557 os_context_t *c=th->interrupt_contexts[i];
3558 esp1 = (void **) *os_context_register_addr(c,reg_SP);
3559 if (esp1>=(void **)th->control_stack_start &&
3560 esp1<(void **)th->control_stack_end) {
3561 if(esp1<esp) esp=esp1;
3562 preserve_context_registers((void(*)(os_context_register_t))preserve_pointer,
3567 # else
3568 esp = (void **)((void *)&raise);
3569 # endif
3570 if (!esp || esp == (void*) -1)
3571 lose("garbage_collect: no SP known for thread %x (OS %x)",
3572 th, th->os_thread);
3573 for (ptr = ((void **)th->control_stack_end)-1; ptr >= esp; ptr--) {
3574 preserve_pointer(*ptr);
3578 #else
3579 /* Non-x86oid systems don't have "conservative roots" as such, but
3580 * the same mechanism is used for objects pinned for use by alien
3581 * code. */
3582 for_each_thread(th) {
3583 lispobj pin_list = SymbolTlValue(PINNED_OBJECTS,th);
3584 while (pin_list != NIL) {
3585 struct cons *list_entry =
3586 (struct cons *)native_pointer(pin_list);
3587 preserve_pointer((void*)list_entry->car);
3588 pin_list = list_entry->cdr;
3591 #endif
3593 #if QSHOW
3594 if (gencgc_verbose > 1) {
3595 sword_t num_dont_move_pages = count_dont_move_pages();
3596 fprintf(stderr,
3597 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3598 num_dont_move_pages,
3599 npage_bytes(num_dont_move_pages));
3601 #endif
3603 /* Now that all of the pinned (dont_move) pages are known, and
3604 * before we start to scavenge (and thus relocate) objects,
3605 * relocate the pinned pages to newspace, so that the scavenger
3606 * will not attempt to relocate their contents. */
3607 move_pinned_pages_to_newspace();
3609 /* Scavenge all the rest of the roots. */
3611 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3613 * If not x86, we need to scavenge the interrupt context(s) and the
3614 * control stack.
3617 struct thread *th;
3618 for_each_thread(th) {
3619 scavenge_interrupt_contexts(th);
3620 scavenge_control_stack(th);
3623 # ifdef LISP_FEATURE_SB_SAFEPOINT
3624 /* In this case, scrub all stacks right here from the GCing thread
3625 * instead of doing what the comment below says. Suboptimal, but
3626 * easier. */
3627 for_each_thread(th)
3628 scrub_thread_control_stack(th);
3629 # else
3630 /* Scrub the unscavenged control stack space, so that we can't run
3631 * into any stale pointers in a later GC (this is done by the
3632 * stop-for-gc handler in the other threads). */
3633 scrub_control_stack();
3634 # endif
3636 #endif
3638 /* Scavenge the Lisp functions of the interrupt handlers, taking
3639 * care to avoid SIG_DFL and SIG_IGN. */
3640 for (i = 0; i < NSIG; i++) {
3641 union interrupt_handler handler = interrupt_handlers[i];
3642 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3643 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3644 scavenge((lispobj *)(interrupt_handlers + i), 1);
3647 /* Scavenge the binding stacks. */
3649 struct thread *th;
3650 for_each_thread(th) {
3651 sword_t len= (lispobj *)get_binding_stack_pointer(th) -
3652 th->binding_stack_start;
3653 scavenge((lispobj *) th->binding_stack_start,len);
3654 #ifdef LISP_FEATURE_SB_THREAD
3655 /* do the tls as well */
3656 len=(SymbolValue(FREE_TLS_INDEX,0) >> WORD_SHIFT) -
3657 (sizeof (struct thread))/(sizeof (lispobj));
3658 scavenge((lispobj *) (th+1),len);
3659 #endif
3663 /* Scavenge static space. */
3664 if (gencgc_verbose > 1) {
3665 FSHOW((stderr,
3666 "/scavenge static space: %d bytes\n",
3667 SymbolValue(STATIC_SPACE_FREE_POINTER,0) - STATIC_SPACE_START));
3669 heap_scavenge((lispobj*)STATIC_SPACE_START,
3670 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3672 /* All generations but the generation being GCed need to be
3673 * scavenged. The new_space generation needs special handling as
3674 * objects may be moved in - it is handled separately below. */
3675 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3676 scavenge_immobile_roots(generation+1, SCRATCH_GENERATION);
3677 #endif
3678 scavenge_generations(generation+1, PSEUDO_STATIC_GENERATION);
3680 #ifdef LISP_FEATURE_SB_TRACEROOT
3681 scavenge(&gc_object_watcher, 1);
3682 #endif
3683 scavenge_pinned_ranges();
3685 /* Finally scavenge the new_space generation. Keep going until no
3686 * more objects are moved into the new generation */
3687 scavenge_newspace_generation(new_space);
3689 /* FIXME: I tried reenabling this check when debugging unrelated
3690 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3691 * Since the current GC code seems to work well, I'm guessing that
3692 * this debugging code is just stale, but I haven't tried to
3693 * figure it out. It should be figured out and then either made to
3694 * work or just deleted. */
3696 #define RESCAN_CHECK 0
3697 #if RESCAN_CHECK
3698 /* As a check re-scavenge the newspace once; no new objects should
3699 * be found. */
3701 os_vm_size_t old_bytes_allocated = bytes_allocated;
3702 os_vm_size_t bytes_allocated;
3704 /* Start with a full scavenge. */
3705 scavenge_newspace_generation_one_scan(new_space);
3707 /* Flush the current regions, updating the tables. */
3708 gc_alloc_update_all_page_tables(1);
3710 bytes_allocated = bytes_allocated - old_bytes_allocated;
3712 if (bytes_allocated != 0) {
3713 lose("Rescan of new_space allocated %d more bytes.\n",
3714 bytes_allocated);
3717 #endif
3719 scan_weak_hash_tables();
3720 scan_weak_pointers();
3721 wipe_nonpinned_words();
3722 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3723 // Do this last, because until wipe_nonpinned_words() happens,
3724 // not all page table entries have the 'gen' value updated,
3725 // which we need to correctly find all old->young pointers.
3726 sweep_immobile_space(raise);
3727 #endif
3729 /* Flush the current regions, updating the tables. */
3730 gc_alloc_update_all_page_tables(0);
3731 hopscotch_log_stats(&pinned_objects, "pins");
3733 /* Free the pages in oldspace, but not those marked dont_move. */
3734 free_oldspace();
3736 /* If the GC is not raising the age then lower the generation back
3737 * to its normal generation number */
3738 if (!raise) {
3739 for (i = 0; i < last_free_page; i++)
3740 if ((page_bytes_used(i) != 0)
3741 && (page_table[i].gen == SCRATCH_GENERATION))
3742 page_table[i].gen = generation;
3743 gc_assert(generations[generation].bytes_allocated == 0);
3744 generations[generation].bytes_allocated =
3745 generations[SCRATCH_GENERATION].bytes_allocated;
3746 generations[SCRATCH_GENERATION].bytes_allocated = 0;
3749 /* Reset the alloc_start_page for generation. */
3750 generations[generation].alloc_start_page = 0;
3751 generations[generation].alloc_unboxed_start_page = 0;
3752 generations[generation].alloc_large_start_page = 0;
3753 generations[generation].alloc_large_unboxed_start_page = 0;
3755 if (generation >= verify_gens) {
3756 if (gencgc_verbose) {
3757 SHOW("verifying");
3759 verify_gc();
3762 /* Set the new gc trigger for the GCed generation. */
3763 generations[generation].gc_trigger =
3764 generations[generation].bytes_allocated
3765 + generations[generation].bytes_consed_between_gc;
3767 if (raise)
3768 generations[generation].num_gc = 0;
3769 else
3770 ++generations[generation].num_gc;
3774 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3775 sword_t
3776 update_dynamic_space_free_pointer(void)
3778 page_index_t last_page = -1, i;
3780 for (i = 0; i < last_free_page; i++)
3781 if (!page_free_p(i) && (page_bytes_used(i) != 0))
3782 last_page = i;
3784 last_free_page = last_page+1;
3786 set_alloc_pointer((lispobj)(page_address(last_free_page)));
3787 return 0; /* dummy value: return something ... */
3790 static void
3791 remap_page_range (page_index_t from, page_index_t to)
3793 /* There's a mysterious Solaris/x86 problem with using mmap
3794 * tricks for memory zeroing. See sbcl-devel thread
3795 * "Re: patch: standalone executable redux".
3797 #if defined(LISP_FEATURE_SUNOS)
3798 zero_and_mark_pages(from, to);
3799 #else
3800 const page_index_t
3801 release_granularity = gencgc_release_granularity/GENCGC_CARD_BYTES,
3802 release_mask = release_granularity-1,
3803 end = to+1,
3804 aligned_from = (from+release_mask)&~release_mask,
3805 aligned_end = (end&~release_mask);
3807 if (aligned_from < aligned_end) {
3808 zero_pages_with_mmap(aligned_from, aligned_end-1);
3809 if (aligned_from != from)
3810 zero_and_mark_pages(from, aligned_from-1);
3811 if (aligned_end != end)
3812 zero_and_mark_pages(aligned_end, end-1);
3813 } else {
3814 zero_and_mark_pages(from, to);
3816 #endif
3819 static void
3820 remap_free_pages (page_index_t from, page_index_t to, int forcibly)
3822 page_index_t first_page, last_page;
3824 if (forcibly)
3825 return remap_page_range(from, to);
3827 for (first_page = from; first_page <= to; first_page++) {
3828 if (!page_free_p(first_page) || !page_need_to_zero(first_page))
3829 continue;
3831 last_page = first_page + 1;
3832 while (page_free_p(last_page) &&
3833 (last_page <= to) &&
3834 (page_need_to_zero(last_page)))
3835 last_page++;
3837 remap_page_range(first_page, last_page-1);
3839 first_page = last_page;
3843 generation_index_t small_generation_limit = 1;
3845 /* GC all generations newer than last_gen, raising the objects in each
3846 * to the next older generation - we finish when all generations below
3847 * last_gen are empty. Then if last_gen is due for a GC, or if
3848 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3849 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3851 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3852 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3853 void
3854 collect_garbage(generation_index_t last_gen)
3856 generation_index_t gen = 0, i;
3857 int raise, more = 0;
3858 int gen_to_wp;
3859 /* The largest value of last_free_page seen since the time
3860 * remap_free_pages was called. */
3861 static page_index_t high_water_mark = 0;
3863 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3864 log_generation_stats(gc_logfile, "=== GC Start ===");
3866 gc_active_p = 1;
3868 if (last_gen > HIGHEST_NORMAL_GENERATION+1) {
3869 FSHOW((stderr,
3870 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3871 last_gen));
3872 last_gen = 0;
3875 /* Flush the alloc regions updating the tables. */
3876 gc_alloc_update_all_page_tables(1);
3878 /* Verify the new objects created by Lisp code. */
3879 if (pre_verify_gen_0) {
3880 FSHOW((stderr, "pre-checking generation 0\n"));
3881 verify_generation(0);
3884 if (gencgc_verbose > 1)
3885 print_generation_stats();
3887 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3888 /* Immobile space generation bits are lazily updated for gen0
3889 (not touched on every object allocation) so do it now */
3890 update_immobile_nursery_bits();
3891 #endif
3893 do {
3894 /* Collect the generation. */
3896 if (more || (gen >= gencgc_oldest_gen_to_gc)) {
3897 /* Never raise the oldest generation. Never raise the extra generation
3898 * collected due to more-flag. */
3899 raise = 0;
3900 more = 0;
3901 } else {
3902 raise =
3903 (gen < last_gen)
3904 || (generations[gen].num_gc >= generations[gen].number_of_gcs_before_promotion);
3905 /* If we would not normally raise this one, but we're
3906 * running low on space in comparison to the object-sizes
3907 * we've been seeing, raise it and collect the next one
3908 * too. */
3909 if (!raise && gen == last_gen) {
3910 more = (2*large_allocation) >= (dynamic_space_size - bytes_allocated);
3911 raise = more;
3915 if (gencgc_verbose > 1) {
3916 FSHOW((stderr,
3917 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3918 gen,
3919 raise,
3920 generations[gen].bytes_allocated,
3921 generations[gen].gc_trigger,
3922 generations[gen].num_gc));
3925 /* If an older generation is being filled, then update its
3926 * memory age. */
3927 if (raise == 1) {
3928 generations[gen+1].cum_sum_bytes_allocated +=
3929 generations[gen+1].bytes_allocated;
3932 garbage_collect_generation(gen, raise);
3934 /* Reset the memory age cum_sum. */
3935 generations[gen].cum_sum_bytes_allocated = 0;
3937 if (gencgc_verbose > 1) {
3938 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3939 print_generation_stats();
3942 gen++;
3943 } while ((gen <= gencgc_oldest_gen_to_gc)
3944 && ((gen < last_gen)
3945 || more
3946 || (raise
3947 && (generations[gen].bytes_allocated
3948 > generations[gen].gc_trigger)
3949 && (generation_average_age(gen)
3950 > generations[gen].minimum_age_before_gc))));
3952 /* Now if gen-1 was raised all generations before gen are empty.
3953 * If it wasn't raised then all generations before gen-1 are empty.
3955 * Now objects within this gen's pages cannot point to younger
3956 * generations unless they are written to. This can be exploited
3957 * by write-protecting the pages of gen; then when younger
3958 * generations are GCed only the pages which have been written
3959 * need scanning. */
3960 if (raise)
3961 gen_to_wp = gen;
3962 else
3963 gen_to_wp = gen - 1;
3965 /* There's not much point in WPing pages in generation 0 as it is
3966 * never scavenged (except promoted pages). */
3967 if ((gen_to_wp > 0) && enable_page_protection) {
3968 /* Check that they are all empty. */
3969 for (i = 0; i < gen_to_wp; i++) {
3970 if (generations[i].bytes_allocated)
3971 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
3972 gen_to_wp, i);
3974 write_protect_generation_pages(gen_to_wp);
3976 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3977 write_protect_immobile_space();
3978 #endif
3980 /* Set gc_alloc() back to generation 0. The current regions should
3981 * be flushed after the above GCs. */
3982 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3983 gc_alloc_generation = 0;
3985 /* Save the high-water mark before updating last_free_page */
3986 if (last_free_page > high_water_mark)
3987 high_water_mark = last_free_page;
3989 update_dynamic_space_free_pointer();
3991 /* Update auto_gc_trigger. Make sure we trigger the next GC before
3992 * running out of heap! */
3993 if (bytes_consed_between_gcs <= (dynamic_space_size - bytes_allocated))
3994 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
3995 else
3996 auto_gc_trigger = bytes_allocated + (dynamic_space_size - bytes_allocated)/2;
3998 if(gencgc_verbose)
3999 fprintf(stderr,"Next gc when %"OS_VM_SIZE_FMT" bytes have been consed\n",
4000 auto_gc_trigger);
4002 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4003 * back to the OS.
4005 if (gen > small_generation_limit) {
4006 if (last_free_page > high_water_mark)
4007 high_water_mark = last_free_page;
4008 remap_free_pages(0, high_water_mark, 0);
4009 high_water_mark = 0;
4012 gc_active_p = 0;
4013 large_allocation = 0;
4015 #ifdef LISP_FEATURE_SB_TRACEROOT
4016 if (gc_object_watcher) {
4017 extern void gc_prove_liveness(void(*)(), lispobj, int, uword_t*);
4018 gc_prove_liveness(preserve_context_registers,
4019 gc_object_watcher,
4020 gc_n_stack_pins, pinned_objects.keys);
4022 #endif
4024 log_generation_stats(gc_logfile, "=== GC End ===");
4025 SHOW("returning from collect_garbage");
4028 void
4029 gc_init(void)
4031 page_index_t i;
4033 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4034 alloc_gc_page();
4035 #endif
4037 /* Compute the number of pages needed for the dynamic space.
4038 * Dynamic space size should be aligned on page size. */
4039 page_table_pages = dynamic_space_size/GENCGC_CARD_BYTES;
4040 gc_assert(dynamic_space_size == npage_bytes(page_table_pages));
4042 /* Default nursery size to 5% of the total dynamic space size,
4043 * min 1Mb. */
4044 bytes_consed_between_gcs = dynamic_space_size/(os_vm_size_t)20;
4045 if (bytes_consed_between_gcs < (1024*1024))
4046 bytes_consed_between_gcs = 1024*1024;
4048 /* The page_table must be allocated using "calloc" to initialize
4049 * the page structures correctly. There used to be a separate
4050 * initialization loop (now commented out; see below) but that was
4051 * unnecessary and did hurt startup time. */
4052 page_table = calloc(page_table_pages, sizeof(struct page));
4053 gc_assert(page_table);
4054 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4055 gc_init_immobile();
4056 #endif
4058 hopscotch_init();
4059 hopscotch_create(&pinned_objects, HOPSCOTCH_HASH_FUN_DEFAULT, 0 /* hashset */,
4060 32 /* logical bin count */, 0 /* default range */);
4062 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4063 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4065 /* The page structures are initialized implicitly when page_table
4066 * is allocated with "calloc" above. Formerly we had the following
4067 * explicit initialization here (comments converted to C99 style
4068 * for readability as C's block comments don't nest):
4070 * // Initialize each page structure.
4071 * for (i = 0; i < page_table_pages; i++) {
4072 * // Initialize all pages as free.
4073 * page_table[i].allocated = FREE_PAGE_FLAG;
4074 * page_table[i].bytes_used = 0;
4076 * // Pages are not write-protected at startup.
4077 * page_table[i].write_protected = 0;
4080 * Without this loop the image starts up much faster when dynamic
4081 * space is large -- which it is on 64-bit platforms already by
4082 * default -- and when "calloc" for large arrays is implemented
4083 * using copy-on-write of a page of zeroes -- which it is at least
4084 * on Linux. In this case the pages that page_table_pages is stored
4085 * in are mapped and cleared not before the corresponding part of
4086 * dynamic space is used. For example, this saves clearing 16 MB of
4087 * memory at startup if the page size is 4 KB and the size of
4088 * dynamic space is 4 GB.
4089 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4090 * asserted below: */
4092 /* Compile time assertion: If triggered, declares an array
4093 * of dimension -1 forcing a syntax error. The intent of the
4094 * assignment is to avoid an "unused variable" warning. */
4095 char assert_free_page_flag_0[(FREE_PAGE_FLAG) ? -1 : 1];
4096 assert_free_page_flag_0[0] = assert_free_page_flag_0[0];
4099 bytes_allocated = 0;
4101 /* Initialize the generations. */
4102 for (i = 0; i < NUM_GENERATIONS; i++) {
4103 generations[i].alloc_start_page = 0;
4104 generations[i].alloc_unboxed_start_page = 0;
4105 generations[i].alloc_large_start_page = 0;
4106 generations[i].alloc_large_unboxed_start_page = 0;
4107 generations[i].bytes_allocated = 0;
4108 generations[i].gc_trigger = 2000000;
4109 generations[i].num_gc = 0;
4110 generations[i].cum_sum_bytes_allocated = 0;
4111 /* the tune-able parameters */
4112 generations[i].bytes_consed_between_gc
4113 = bytes_consed_between_gcs/(os_vm_size_t)HIGHEST_NORMAL_GENERATION;
4114 generations[i].number_of_gcs_before_promotion = 1;
4115 generations[i].minimum_age_before_gc = 0.75;
4118 /* Initialize gc_alloc. */
4119 gc_alloc_generation = 0;
4120 gc_set_region_empty(&boxed_region);
4121 gc_set_region_empty(&unboxed_region);
4123 last_free_page = 0;
4126 /* Pick up the dynamic space from after a core load.
4128 * The ALLOCATION_POINTER points to the end of the dynamic space.
4131 static void
4132 gencgc_pickup_dynamic(void)
4134 page_index_t page = 0;
4135 void *alloc_ptr = (void *)get_alloc_pointer();
4136 lispobj *prev=(lispobj *)page_address(page);
4137 generation_index_t gen = PSEUDO_STATIC_GENERATION;
4139 bytes_allocated = 0;
4141 do {
4142 lispobj *first,*ptr= (lispobj *)page_address(page);
4144 if (!gencgc_partial_pickup || !page_free_p(page)) {
4145 /* It is possible, though rare, for the saved page table
4146 * to contain free pages below alloc_ptr. */
4147 page_table[page].gen = gen;
4148 set_page_bytes_used(page, GENCGC_CARD_BYTES);
4149 page_table[page].large_object = 0;
4150 page_table[page].write_protected = 0;
4151 page_table[page].write_protected_cleared = 0;
4152 page_table[page].dont_move = 0;
4153 set_page_need_to_zero(page, 1);
4155 bytes_allocated += GENCGC_CARD_BYTES;
4158 if (!gencgc_partial_pickup) {
4159 page_table[page].allocated = BOXED_PAGE_FLAG;
4160 first = gc_search_space3(ptr, prev, (ptr+2));
4161 if(ptr == first)
4162 prev=ptr;
4163 set_page_scan_start_offset(page,
4164 page_address(page) - (void *)prev);
4166 page++;
4167 } while (page_address(page) < alloc_ptr);
4169 last_free_page = page;
4171 generations[gen].bytes_allocated = bytes_allocated;
4173 gc_alloc_update_all_page_tables(1);
4174 write_protect_generation_pages(gen);
4177 void
4178 gc_initialize_pointers(void)
4180 gencgc_pickup_dynamic();
4184 /* alloc(..) is the external interface for memory allocation. It
4185 * allocates to generation 0. It is not called from within the garbage
4186 * collector as it is only external uses that need the check for heap
4187 * size (GC trigger) and to disable the interrupts (interrupts are
4188 * always disabled during a GC).
4190 * The vops that call alloc(..) assume that the returned space is zero-filled.
4191 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4193 * The check for a GC trigger is only performed when the current
4194 * region is full, so in most cases it's not needed. */
4196 static inline lispobj *
4197 general_alloc_internal(sword_t nbytes, int page_type_flag, struct alloc_region *region,
4198 struct thread *thread)
4200 #ifndef LISP_FEATURE_WIN32
4201 lispobj alloc_signal;
4202 #endif
4203 void *new_obj;
4204 void *new_free_pointer;
4205 os_vm_size_t trigger_bytes = 0;
4207 gc_assert(nbytes > 0);
4209 /* Check for alignment allocation problems. */
4210 gc_assert((((uword_t)region->free_pointer & LOWTAG_MASK) == 0)
4211 && ((nbytes & LOWTAG_MASK) == 0));
4213 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4214 /* Must be inside a PA section. */
4215 gc_assert(get_pseudo_atomic_atomic(thread));
4216 #endif
4218 if ((os_vm_size_t) nbytes > large_allocation)
4219 large_allocation = nbytes;
4221 /* maybe we can do this quickly ... */
4222 new_free_pointer = region->free_pointer + nbytes;
4223 if (new_free_pointer <= region->end_addr) {
4224 new_obj = (void*)(region->free_pointer);
4225 region->free_pointer = new_free_pointer;
4226 return(new_obj); /* yup */
4229 /* We don't want to count nbytes against auto_gc_trigger unless we
4230 * have to: it speeds up the tenuring of objects and slows down
4231 * allocation. However, unless we do so when allocating _very_
4232 * large objects we are in danger of exhausting the heap without
4233 * running sufficient GCs.
4235 if ((os_vm_size_t) nbytes >= bytes_consed_between_gcs)
4236 trigger_bytes = nbytes;
4238 /* we have to go the long way around, it seems. Check whether we
4239 * should GC in the near future
4241 if (auto_gc_trigger && (bytes_allocated+trigger_bytes > auto_gc_trigger)) {
4242 /* Don't flood the system with interrupts if the need to gc is
4243 * already noted. This can happen for example when SUB-GC
4244 * allocates or after a gc triggered in a WITHOUT-GCING. */
4245 if (SymbolValue(GC_PENDING,thread) == NIL) {
4246 /* set things up so that GC happens when we finish the PA
4247 * section */
4248 SetSymbolValue(GC_PENDING,T,thread);
4249 if (SymbolValue(GC_INHIBIT,thread) == NIL) {
4250 #ifdef LISP_FEATURE_SB_SAFEPOINT
4251 thread_register_gc_trigger();
4252 #else
4253 set_pseudo_atomic_interrupted(thread);
4254 #ifdef GENCGC_IS_PRECISE
4255 /* PPC calls alloc() from a trap
4256 * look up the most context if it's from a trap. */
4258 os_context_t *context =
4259 thread->interrupt_data->allocation_trap_context;
4260 maybe_save_gc_mask_and_block_deferrables
4261 (context ? os_context_sigmask_addr(context) : NULL);
4263 #else
4264 maybe_save_gc_mask_and_block_deferrables(NULL);
4265 #endif
4266 #endif
4270 new_obj = gc_alloc_with_region(nbytes, page_type_flag, region, 0);
4272 #ifndef LISP_FEATURE_WIN32
4273 /* for sb-prof, and not supported on Windows yet */
4274 alloc_signal = SymbolValue(ALLOC_SIGNAL,thread);
4275 if ((alloc_signal & FIXNUM_TAG_MASK) == 0) {
4276 if ((sword_t) alloc_signal <= 0) {
4277 SetSymbolValue(ALLOC_SIGNAL, T, thread);
4278 raise(SIGPROF);
4279 } else {
4280 SetSymbolValue(ALLOC_SIGNAL,
4281 alloc_signal - (1 << N_FIXNUM_TAG_BITS),
4282 thread);
4285 #endif
4287 return (new_obj);
4290 lispobj *
4291 general_alloc(sword_t nbytes, int page_type_flag)
4293 struct thread *thread = arch_os_get_current_thread();
4294 /* Select correct region, and call general_alloc_internal with it.
4295 * For other then boxed allocation we must lock first, since the
4296 * region is shared. */
4297 if (BOXED_PAGE_FLAG & page_type_flag) {
4298 #ifdef LISP_FEATURE_SB_THREAD
4299 struct alloc_region *region = (thread ? &(thread->alloc_region) : &boxed_region);
4300 #else
4301 struct alloc_region *region = &boxed_region;
4302 #endif
4303 return general_alloc_internal(nbytes, page_type_flag, region, thread);
4304 } else if (UNBOXED_PAGE_FLAG == page_type_flag) {
4305 lispobj * obj;
4306 int result;
4307 result = thread_mutex_lock(&allocation_lock);
4308 gc_assert(!result);
4309 obj = general_alloc_internal(nbytes, page_type_flag, &unboxed_region, thread);
4310 result = thread_mutex_unlock(&allocation_lock);
4311 gc_assert(!result);
4312 return obj;
4313 } else {
4314 lose("bad page type flag: %d", page_type_flag);
4318 lispobj AMD64_SYSV_ABI *
4319 alloc(sword_t nbytes)
4321 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4322 struct thread *self = arch_os_get_current_thread();
4323 int was_pseudo_atomic = get_pseudo_atomic_atomic(self);
4324 if (!was_pseudo_atomic)
4325 set_pseudo_atomic_atomic(self);
4326 #else
4327 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4328 #endif
4330 lispobj *result = general_alloc(nbytes, BOXED_PAGE_FLAG);
4332 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4333 if (!was_pseudo_atomic)
4334 clear_pseudo_atomic_atomic(self);
4335 #endif
4337 return result;
4341 * shared support for the OS-dependent signal handlers which
4342 * catch GENCGC-related write-protect violations
4344 void unhandled_sigmemoryfault(void* addr);
4346 /* Depending on which OS we're running under, different signals might
4347 * be raised for a violation of write protection in the heap. This
4348 * function factors out the common generational GC magic which needs
4349 * to invoked in this case, and should be called from whatever signal
4350 * handler is appropriate for the OS we're running under.
4352 * Return true if this signal is a normal generational GC thing that
4353 * we were able to handle, or false if it was abnormal and control
4354 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4356 * We have two control flags for this: one causes us to ignore faults
4357 * on unprotected pages completely, and the second complains to stderr
4358 * but allows us to continue without losing.
4360 extern boolean ignore_memoryfaults_on_unprotected_pages;
4361 boolean ignore_memoryfaults_on_unprotected_pages = 0;
4363 extern boolean continue_after_memoryfault_on_unprotected_pages;
4364 boolean continue_after_memoryfault_on_unprotected_pages = 0;
4367 gencgc_handle_wp_violation(void* fault_addr)
4369 page_index_t page_index = find_page_index(fault_addr);
4371 #if QSHOW_SIGNALS
4372 FSHOW((stderr,
4373 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT"\n",
4374 fault_addr, page_index));
4375 #endif
4377 /* Check whether the fault is within the dynamic space. */
4378 if (page_index == (-1)) {
4379 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4380 extern int immobile_space_handle_wp_violation(void*);
4381 if (immobile_space_handle_wp_violation(fault_addr))
4382 return 1;
4383 #endif
4385 /* It can be helpful to be able to put a breakpoint on this
4386 * case to help diagnose low-level problems. */
4387 unhandled_sigmemoryfault(fault_addr);
4389 /* not within the dynamic space -- not our responsibility */
4390 return 0;
4392 } else {
4393 int ret;
4394 ret = thread_mutex_lock(&free_pages_lock);
4395 gc_assert(ret == 0);
4396 if (page_table[page_index].write_protected) {
4397 /* Unprotect the page. */
4398 os_protect(page_address(page_index), GENCGC_CARD_BYTES, OS_VM_PROT_ALL);
4399 page_table[page_index].write_protected_cleared = 1;
4400 page_table[page_index].write_protected = 0;
4401 } else if (!ignore_memoryfaults_on_unprotected_pages) {
4402 /* The only acceptable reason for this signal on a heap
4403 * access is that GENCGC write-protected the page.
4404 * However, if two CPUs hit a wp page near-simultaneously,
4405 * we had better not have the second one lose here if it
4406 * does this test after the first one has already set wp=0
4408 if(page_table[page_index].write_protected_cleared != 1) {
4409 void lisp_backtrace(int frames);
4410 lisp_backtrace(10);
4411 fprintf(stderr,
4412 "Fault @ %p, page %"PAGE_INDEX_FMT" not marked as write-protected:\n"
4413 " boxed_region.first_page: %"PAGE_INDEX_FMT","
4414 " boxed_region.last_page %"PAGE_INDEX_FMT"\n"
4415 " page.scan_start_offset: %"OS_VM_SIZE_FMT"\n"
4416 " page.bytes_used: %u\n"
4417 " page.allocated: %d\n"
4418 " page.write_protected: %d\n"
4419 " page.write_protected_cleared: %d\n"
4420 " page.generation: %d\n",
4421 fault_addr,
4422 page_index,
4423 boxed_region.first_page,
4424 boxed_region.last_page,
4425 page_scan_start_offset(page_index),
4426 page_bytes_used(page_index),
4427 page_table[page_index].allocated,
4428 page_table[page_index].write_protected,
4429 page_table[page_index].write_protected_cleared,
4430 page_table[page_index].gen);
4431 if (!continue_after_memoryfault_on_unprotected_pages)
4432 lose("Feh.\n");
4435 ret = thread_mutex_unlock(&free_pages_lock);
4436 gc_assert(ret == 0);
4437 /* Don't worry, we can handle it. */
4438 return 1;
4441 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4442 * it's not just a case of the program hitting the write barrier, and
4443 * are about to let Lisp deal with it. It's basically just a
4444 * convenient place to set a gdb breakpoint. */
4445 void
4446 unhandled_sigmemoryfault(void *addr)
4449 static void
4450 update_thread_page_tables(struct thread *th)
4452 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->alloc_region);
4453 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4454 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &th->sprof_alloc_region);
4455 #endif
4458 /* GC is single-threaded and all memory allocations during a
4459 collection happen in the GC thread, so it is sufficient to update
4460 all the the page tables once at the beginning of a collection and
4461 update only page tables of the GC thread during the collection. */
4462 void gc_alloc_update_all_page_tables(int for_all_threads)
4464 /* Flush the alloc regions updating the tables. */
4465 struct thread *th;
4466 if (for_all_threads) {
4467 for_each_thread(th) {
4468 update_thread_page_tables(th);
4471 else {
4472 th = arch_os_get_current_thread();
4473 if (th) {
4474 update_thread_page_tables(th);
4477 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG, &unboxed_region);
4478 gc_alloc_update_page_tables(BOXED_PAGE_FLAG, &boxed_region);
4481 void
4482 gc_set_region_empty(struct alloc_region *region)
4484 region->first_page = 0;
4485 region->last_page = -1;
4486 region->start_addr = page_address(0);
4487 region->free_pointer = page_address(0);
4488 region->end_addr = page_address(0);
4491 static void
4492 zero_all_free_pages()
4494 page_index_t i;
4496 for (i = 0; i < last_free_page; i++) {
4497 if (page_free_p(i)) {
4498 #ifdef READ_PROTECT_FREE_PAGES
4499 os_protect(page_address(i),
4500 GENCGC_CARD_BYTES,
4501 OS_VM_PROT_ALL);
4502 #endif
4503 zero_pages(i, i);
4508 /* Things to do before doing a final GC before saving a core (without
4509 * purify).
4511 * + Pages in large_object pages aren't moved by the GC, so we need to
4512 * unset that flag from all pages.
4513 * + The pseudo-static generation isn't normally collected, but it seems
4514 * reasonable to collect it at least when saving a core. So move the
4515 * pages to a normal generation.
4517 static void
4518 prepare_for_final_gc ()
4520 page_index_t i;
4522 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4523 extern void prepare_immobile_space_for_final_gc();
4524 prepare_immobile_space_for_final_gc ();
4525 #endif
4526 do_wipe_p = 0;
4527 for (i = 0; i < last_free_page; i++) {
4528 page_table[i].large_object = 0;
4529 if (page_table[i].gen == PSEUDO_STATIC_GENERATION) {
4530 int used = page_bytes_used(i);
4531 page_table[i].gen = HIGHEST_NORMAL_GENERATION;
4532 generations[PSEUDO_STATIC_GENERATION].bytes_allocated -= used;
4533 generations[HIGHEST_NORMAL_GENERATION].bytes_allocated += used;
4539 /* Do a non-conservative GC, and then save a core with the initial
4540 * function being set to the value of the static symbol
4541 * SB!VM:RESTART-LISP-FUNCTION */
4542 void
4543 gc_and_save(char *filename, boolean prepend_runtime,
4544 boolean save_runtime_options, boolean compressed,
4545 int compression_level, int application_type)
4547 FILE *file;
4548 void *runtime_bytes = NULL;
4549 size_t runtime_size;
4551 file = prepare_to_save(filename, prepend_runtime, &runtime_bytes,
4552 &runtime_size);
4553 if (file == NULL)
4554 return;
4556 conservative_stack = 0;
4558 /* The filename might come from Lisp, and be moved by the now
4559 * non-conservative GC. */
4560 filename = strdup(filename);
4562 /* Collect twice: once into relatively high memory, and then back
4563 * into low memory. This compacts the retained data into the lower
4564 * pages, minimizing the size of the core file.
4566 prepare_for_final_gc();
4567 gencgc_alloc_start_page = last_free_page;
4568 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4570 prepare_for_final_gc();
4571 gencgc_alloc_start_page = -1;
4572 collect_garbage(HIGHEST_NORMAL_GENERATION+1);
4574 if (prepend_runtime)
4575 save_runtime_to_filehandle(file, runtime_bytes, runtime_size,
4576 application_type);
4578 /* The dumper doesn't know that pages need to be zeroed before use. */
4579 zero_all_free_pages();
4580 save_to_filehandle(file, filename, SymbolValue(RESTART_LISP_FUNCTION,0),
4581 prepend_runtime, save_runtime_options,
4582 compressed ? compression_level : COMPRESSION_LEVEL_NONE);
4583 /* Oops. Save still managed to fail. Since we've mangled the stack
4584 * beyond hope, there's not much we can do.
4585 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4586 * going to be rather unsatisfactory too... */
4587 lose("Attempt to save core after non-conservative GC failed.\n");