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