2 * GENerational Conservative Garbage Collector for SBCL
6 * This software is part of the SBCL system. See the README file for
9 * This software is derived from the CMU CL system, which was
10 * written at Carnegie Mellon University and released into the
11 * public domain. The software is in the public domain and is
12 * provided with absolutely no warranty. See the COPYING and CREDITS
13 * files for more information.
17 * For a review of garbage collection techniques (e.g. generational
18 * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19 * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20 * had been accepted for _ACM Computing Surveys_ and was available
21 * as a PostScript preprint through
22 * <http://www.cs.utexas.edu/users/oops/papers.html>
24 * <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
32 #if defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD)
33 #include "pthreads_win32.h"
41 #include "interrupt.h"
46 #include "gc-internal.h"
48 #include "pseudo-atomic.h"
50 #include "genesis/vector.h"
51 #include "genesis/weak-pointer.h"
52 #include "genesis/fdefn.h"
53 #include "genesis/simple-fun.h"
55 #include "genesis/hash-table.h"
56 #include "genesis/instance.h"
57 #include "genesis/layout.h"
59 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
60 #include "genesis/cons.h"
63 /* forward declarations */
64 page_index_t
gc_find_freeish_pages(page_index_t
*restart_page_ptr
, sword_t nbytes
,
72 /* As usually configured, generations 0-5 are normal collected generations,
73 6 is pseudo-static (the objects in which are never moved nor reclaimed),
74 and 7 is scratch space used when collecting a generation without promotion,
75 wherein it is moved to generation 7 and back again.
78 SCRATCH_GENERATION
= PSEUDO_STATIC_GENERATION
+1,
82 /* Should we use page protection to help avoid the scavenging of pages
83 * that don't have pointers to younger generations? */
84 boolean enable_page_protection
= 1;
86 /* Largest allocation seen since last GC. */
87 os_vm_size_t large_allocation
= 0;
94 /* the verbosity level. All non-error messages are disabled at level 0;
95 * and only a few rare messages are printed at level 1. */
97 boolean gencgc_verbose
= 1;
99 boolean gencgc_verbose
= 0;
102 /* FIXME: At some point enable the various error-checking things below
103 * and see what they say. */
105 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
106 * Set verify_gens to HIGHEST_NORMAL_GENERATION + 1 to disable this kind of
108 generation_index_t verify_gens
= HIGHEST_NORMAL_GENERATION
+ 1;
110 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
111 boolean pre_verify_gen_0
= 0;
113 /* Should we check for bad pointers after gc_free_heap is called
114 * from Lisp PURIFY? */
115 boolean verify_after_free_heap
= 0;
117 /* Should we print a note when code objects are found in the dynamic space
118 * during a heap verify? */
119 boolean verify_dynamic_code_check
= 0;
121 #ifdef LISP_FEATURE_X86
122 /* Should we check code objects for fixup errors after they are transported? */
123 boolean check_code_fixups
= 0;
126 /* Should we check that newly allocated regions are zero filled? */
127 boolean gencgc_zero_check
= 0;
129 /* Should we check that the free space is zero filled? */
130 boolean gencgc_enable_verify_zero_fill
= 0;
132 /* Should we check that free pages are zero filled during gc_free_heap
133 * called after Lisp PURIFY? */
134 boolean gencgc_zero_check_during_free_heap
= 0;
136 /* When loading a core, don't do a full scan of the memory for the
137 * memory region boundaries. (Set to true by coreparse.c if the core
138 * contained a pagetable entry).
140 boolean gencgc_partial_pickup
= 0;
142 /* If defined, free pages are read-protected to ensure that nothing
146 /* #define READ_PROTECT_FREE_PAGES */
150 * GC structures and variables
153 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
154 os_vm_size_t bytes_allocated
= 0;
155 os_vm_size_t auto_gc_trigger
= 0;
157 /* the source and destination generations. These are set before a GC starts
159 generation_index_t from_space
;
160 generation_index_t new_space
;
162 /* Set to 1 when in GC */
163 boolean gc_active_p
= 0;
165 /* should the GC be conservative on stack. If false (only right before
166 * saving a core), don't scan the stack / mark pages dont_move. */
167 static boolean conservative_stack
= 1;
169 /* An array of page structures is allocated on gc initialization.
170 * This helps to quickly map between an address and its page structure.
171 * page_table_pages is set from the size of the dynamic space. */
172 page_index_t page_table_pages
;
173 struct page
*page_table
;
175 in_use_marker_t
*page_table_pinned_dwords
;
176 size_t pins_map_size_in_bytes
;
178 /* In GC cards that have conservative pointers to them, should we wipe out
179 * dwords in there that are not used, so that they do not act as false
180 * root to other things in the heap from then on? This is a new feature
181 * but in testing it is both reliable and no noticeable slowdown. */
184 static inline boolean
page_allocated_p(page_index_t page
) {
185 return (page_table
[page
].allocated
!= FREE_PAGE_FLAG
);
188 static inline boolean
page_no_region_p(page_index_t page
) {
189 return !(page_table
[page
].allocated
& OPEN_REGION_PAGE_FLAG
);
192 static inline boolean
page_allocated_no_region_p(page_index_t page
) {
193 return ((page_table
[page
].allocated
& (UNBOXED_PAGE_FLAG
| BOXED_PAGE_FLAG
))
194 && page_no_region_p(page
));
197 static inline boolean
page_free_p(page_index_t page
) {
198 return (page_table
[page
].allocated
== FREE_PAGE_FLAG
);
201 static inline boolean
page_boxed_p(page_index_t page
) {
202 return (page_table
[page
].allocated
& BOXED_PAGE_FLAG
);
205 static inline boolean
page_boxed_no_region_p(page_index_t page
) {
206 return page_boxed_p(page
) && page_no_region_p(page
);
209 static inline boolean
page_unboxed_p(page_index_t page
) {
210 /* Both flags set == boxed code page */
211 return ((page_table
[page
].allocated
& UNBOXED_PAGE_FLAG
)
212 && !page_boxed_p(page
));
215 static inline boolean
protect_page_p(page_index_t page
, generation_index_t generation
) {
216 return (page_boxed_no_region_p(page
)
217 && (page_table
[page
].bytes_used
!= 0)
218 && !page_table
[page
].dont_move
219 && (page_table
[page
].gen
== generation
));
222 /* To map addresses to page structures the address of the first page
224 void *heap_base
= NULL
;
226 /* Calculate the start address for the given page number. */
228 page_address(page_index_t page_num
)
230 return (heap_base
+ (page_num
* GENCGC_CARD_BYTES
));
233 /* Calculate the address where the allocation region associated with
234 * the page starts. */
236 page_scan_start(page_index_t page_index
)
238 return page_address(page_index
)-page_table
[page_index
].scan_start_offset
;
241 /* True if the page starts a contiguous block. */
242 static inline boolean
243 page_starts_contiguous_block_p(page_index_t page_index
)
245 return page_table
[page_index
].scan_start_offset
== 0;
248 /* True if the page is the last page in a contiguous block. */
249 static inline boolean
250 page_ends_contiguous_block_p(page_index_t page_index
, generation_index_t gen
)
252 return (/* page doesn't fill block */
253 (page_table
[page_index
].bytes_used
< GENCGC_CARD_BYTES
)
254 /* page is last allocated page */
255 || ((page_index
+ 1) >= last_free_page
)
257 || page_free_p(page_index
+ 1)
258 /* next page contains no data */
259 || (page_table
[page_index
+ 1].bytes_used
== 0)
260 /* next page is in different generation */
261 || (page_table
[page_index
+ 1].gen
!= gen
)
262 /* next page starts its own contiguous block */
263 || (page_starts_contiguous_block_p(page_index
+ 1)));
266 /* Find the page index within the page_table for the given
267 * address. Return -1 on failure. */
269 find_page_index(void *addr
)
271 if (addr
>= heap_base
) {
272 page_index_t index
= ((pointer_sized_uint_t
)addr
-
273 (pointer_sized_uint_t
)heap_base
) / GENCGC_CARD_BYTES
;
274 if (index
< page_table_pages
)
281 npage_bytes(page_index_t npages
)
283 gc_assert(npages
>=0);
284 return ((os_vm_size_t
)npages
)*GENCGC_CARD_BYTES
;
287 /* Check that X is a higher address than Y and return offset from Y to
289 static inline os_vm_size_t
290 void_diff(void *x
, void *y
)
293 return (pointer_sized_uint_t
)x
- (pointer_sized_uint_t
)y
;
296 /* a structure to hold the state of a generation
298 * CAUTION: If you modify this, make sure to touch up the alien
299 * definition in src/code/gc.lisp accordingly. ...or better yes,
300 * deal with the FIXME there...
304 /* the first page that gc_alloc() checks on its next call */
305 page_index_t alloc_start_page
;
307 /* the first page that gc_alloc_unboxed() checks on its next call */
308 page_index_t alloc_unboxed_start_page
;
310 /* the first page that gc_alloc_large (boxed) considers on its next
311 * call. (Although it always allocates after the boxed_region.) */
312 page_index_t alloc_large_start_page
;
314 /* the first page that gc_alloc_large (unboxed) considers on its
315 * next call. (Although it always allocates after the
316 * current_unboxed_region.) */
317 page_index_t alloc_large_unboxed_start_page
;
319 /* the bytes allocated to this generation */
320 os_vm_size_t bytes_allocated
;
322 /* the number of bytes at which to trigger a GC */
323 os_vm_size_t gc_trigger
;
325 /* to calculate a new level for gc_trigger */
326 os_vm_size_t bytes_consed_between_gc
;
328 /* the number of GCs since the last raise */
331 /* the number of GCs to run on the generations before raising objects to the
333 int number_of_gcs_before_promotion
;
335 /* the cumulative sum of the bytes allocated to this generation. It is
336 * cleared after a GC on this generations, and update before new
337 * objects are added from a GC of a younger generation. Dividing by
338 * the bytes_allocated will give the average age of the memory in
339 * this generation since its last GC. */
340 os_vm_size_t cum_sum_bytes_allocated
;
342 /* a minimum average memory age before a GC will occur helps
343 * prevent a GC when a large number of new live objects have been
344 * added, in which case a GC could be a waste of time */
345 double minimum_age_before_gc
;
348 /* an array of generation structures. There needs to be one more
349 * generation structure than actual generations as the oldest
350 * generation is temporarily raised then lowered. */
351 struct generation generations
[NUM_GENERATIONS
];
353 /* the oldest generation that is will currently be GCed by default.
354 * Valid values are: 0, 1, ... HIGHEST_NORMAL_GENERATION
356 * The default of HIGHEST_NORMAL_GENERATION enables GC on all generations.
358 * Setting this to 0 effectively disables the generational nature of
359 * the GC. In some applications generational GC may not be useful
360 * because there are no long-lived objects.
362 * An intermediate value could be handy after moving long-lived data
363 * into an older generation so an unnecessary GC of this long-lived
364 * data can be avoided. */
365 generation_index_t gencgc_oldest_gen_to_gc
= HIGHEST_NORMAL_GENERATION
;
367 /* META: Is nobody aside from me bothered by this especially misleading
368 * use of the word "last"? It could mean either "ultimate" or "prior",
369 * but in fact means neither. It is the *FIRST* page that should be grabbed
370 * for more space, so it is min free page, or 1+ the max used page. */
371 /* The maximum free page in the heap is maintained and used to update
372 * ALLOCATION_POINTER which is used by the room function to limit its
373 * search of the heap. XX Gencgc obviously needs to be better
374 * integrated with the Lisp code. */
376 page_index_t last_free_page
;
378 #ifdef LISP_FEATURE_SB_THREAD
379 /* This lock is to prevent multiple threads from simultaneously
380 * allocating new regions which overlap each other. Note that the
381 * majority of GC is single-threaded, but alloc() may be called from
382 * >1 thread at a time and must be thread-safe. This lock must be
383 * seized before all accesses to generations[] or to parts of
384 * page_table[] that other threads may want to see */
385 static pthread_mutex_t free_pages_lock
= PTHREAD_MUTEX_INITIALIZER
;
386 /* This lock is used to protect non-thread-local allocation. */
387 static pthread_mutex_t allocation_lock
= PTHREAD_MUTEX_INITIALIZER
;
390 extern os_vm_size_t gencgc_release_granularity
;
391 os_vm_size_t gencgc_release_granularity
= GENCGC_RELEASE_GRANULARITY
;
393 extern os_vm_size_t gencgc_alloc_granularity
;
394 os_vm_size_t gencgc_alloc_granularity
= GENCGC_ALLOC_GRANULARITY
;
398 * miscellaneous heap functions
401 /* Count the number of pages which are write-protected within the
402 * given generation. */
404 count_write_protect_generation_pages(generation_index_t generation
)
406 page_index_t i
, count
= 0;
408 for (i
= 0; i
< last_free_page
; i
++)
409 if (page_allocated_p(i
)
410 && (page_table
[i
].gen
== generation
)
411 && (page_table
[i
].write_protected
== 1))
416 /* Count the number of pages within the given generation. */
418 count_generation_pages(generation_index_t generation
)
421 page_index_t count
= 0;
423 for (i
= 0; i
< last_free_page
; i
++)
424 if (page_allocated_p(i
)
425 && (page_table
[i
].gen
== generation
))
432 count_dont_move_pages(void)
435 page_index_t count
= 0;
436 for (i
= 0; i
< last_free_page
; i
++) {
437 if (page_allocated_p(i
)
438 && (page_table
[i
].dont_move
!= 0)) {
446 /* Work through the pages and add up the number of bytes used for the
447 * given generation. */
449 count_generation_bytes_allocated (generation_index_t gen
)
452 os_vm_size_t result
= 0;
453 for (i
= 0; i
< last_free_page
; i
++) {
454 if (page_allocated_p(i
)
455 && (page_table
[i
].gen
== gen
))
456 result
+= page_table
[i
].bytes_used
;
461 /* Return the average age of the memory in a generation. */
463 generation_average_age(generation_index_t gen
)
465 if (generations
[gen
].bytes_allocated
== 0)
469 ((double)generations
[gen
].cum_sum_bytes_allocated
)
470 / ((double)generations
[gen
].bytes_allocated
);
473 #ifdef LISP_FEATURE_X86
474 extern void fpu_save(void *);
475 extern void fpu_restore(void *);
479 write_generation_stats(FILE *file
)
481 generation_index_t i
;
483 #ifdef LISP_FEATURE_X86
486 /* Can end up here after calling alloc_tramp which doesn't prepare
487 * the x87 state, and the C ABI uses a different mode */
491 /* Print the heap stats. */
493 " Gen StaPg UbSta LaSta LUbSt Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
495 for (i
= 0; i
< SCRATCH_GENERATION
; i
++) {
497 page_index_t boxed_cnt
= 0;
498 page_index_t unboxed_cnt
= 0;
499 page_index_t large_boxed_cnt
= 0;
500 page_index_t large_unboxed_cnt
= 0;
501 page_index_t pinned_cnt
=0;
503 for (j
= 0; j
< last_free_page
; j
++)
504 if (page_table
[j
].gen
== i
) {
506 /* Count the number of boxed pages within the given
508 if (page_boxed_p(j
)) {
509 if (page_table
[j
].large_object
)
514 if(page_table
[j
].dont_move
) pinned_cnt
++;
515 /* Count the number of unboxed pages within the given
517 if (page_unboxed_p(j
)) {
518 if (page_table
[j
].large_object
)
525 gc_assert(generations
[i
].bytes_allocated
526 == count_generation_bytes_allocated(i
));
528 " %1d: %5ld %5ld %5ld %5ld",
530 generations
[i
].alloc_start_page
,
531 generations
[i
].alloc_unboxed_start_page
,
532 generations
[i
].alloc_large_start_page
,
533 generations
[i
].alloc_large_unboxed_start_page
);
535 " %5"PAGE_INDEX_FMT
" %5"PAGE_INDEX_FMT
" %5"PAGE_INDEX_FMT
536 " %5"PAGE_INDEX_FMT
" %5"PAGE_INDEX_FMT
,
537 boxed_cnt
, unboxed_cnt
, large_boxed_cnt
,
538 large_unboxed_cnt
, pinned_cnt
);
543 " %4"PAGE_INDEX_FMT
" %3d %7.4f\n",
544 generations
[i
].bytes_allocated
,
545 (npage_bytes(count_generation_pages(i
)) - generations
[i
].bytes_allocated
),
546 generations
[i
].gc_trigger
,
547 count_write_protect_generation_pages(i
),
548 generations
[i
].num_gc
,
549 generation_average_age(i
));
551 fprintf(file
," Total bytes allocated = %"OS_VM_SIZE_FMT
"\n", bytes_allocated
);
552 fprintf(file
," Dynamic-space-size bytes = %"OS_VM_SIZE_FMT
"\n", dynamic_space_size
);
554 #ifdef LISP_FEATURE_X86
555 fpu_restore(fpu_state
);
560 write_heap_exhaustion_report(FILE *file
, long available
, long requested
,
561 struct thread
*thread
)
564 "Heap exhausted during %s: %ld bytes available, %ld requested.\n",
565 gc_active_p
? "garbage collection" : "allocation",
568 write_generation_stats(file
);
569 fprintf(file
, "GC control variables:\n");
570 fprintf(file
, " *GC-INHIBIT* = %s\n *GC-PENDING* = %s\n",
571 SymbolValue(GC_INHIBIT
,thread
)==NIL
? "false" : "true",
572 (SymbolValue(GC_PENDING
, thread
) == T
) ?
573 "true" : ((SymbolValue(GC_PENDING
, thread
) == NIL
) ?
574 "false" : "in progress"));
575 #ifdef LISP_FEATURE_SB_THREAD
576 fprintf(file
, " *STOP-FOR-GC-PENDING* = %s\n",
577 SymbolValue(STOP_FOR_GC_PENDING
,thread
)==NIL
? "false" : "true");
582 print_generation_stats(void)
584 write_generation_stats(stderr
);
587 extern char* gc_logfile
;
588 char * gc_logfile
= NULL
;
591 log_generation_stats(char *logfile
, char *header
)
594 FILE * log
= fopen(logfile
, "a");
596 fprintf(log
, "%s\n", header
);
597 write_generation_stats(log
);
600 fprintf(stderr
, "Could not open gc logfile: %s\n", logfile
);
607 report_heap_exhaustion(long available
, long requested
, struct thread
*th
)
610 FILE * log
= fopen(gc_logfile
, "a");
612 write_heap_exhaustion_report(log
, available
, requested
, th
);
615 fprintf(stderr
, "Could not open gc logfile: %s\n", gc_logfile
);
619 /* Always to stderr as well. */
620 write_heap_exhaustion_report(stderr
, available
, requested
, th
);
624 #if defined(LISP_FEATURE_X86)
625 void fast_bzero(void*, size_t); /* in <arch>-assem.S */
628 /* Zero the pages from START to END (inclusive), but use mmap/munmap instead
629 * if zeroing it ourselves, i.e. in practice give the memory back to the
630 * OS. Generally done after a large GC.
632 void zero_pages_with_mmap(page_index_t start
, page_index_t end
) {
634 void *addr
= page_address(start
), *new_addr
;
635 os_vm_size_t length
= npage_bytes(1+end
-start
);
640 gc_assert(length
>= gencgc_release_granularity
);
641 gc_assert((length
% gencgc_release_granularity
) == 0);
643 os_invalidate(addr
, length
);
644 new_addr
= os_validate(addr
, length
);
645 if (new_addr
== NULL
|| new_addr
!= addr
) {
646 lose("remap_free_pages: page moved, 0x%08x ==> 0x%08x",
650 for (i
= start
; i
<= end
; i
++) {
651 page_table
[i
].need_to_zero
= 0;
655 /* Zero the pages from START to END (inclusive). Generally done just after
656 * a new region has been allocated.
659 zero_pages(page_index_t start
, page_index_t end
) {
663 #if defined(LISP_FEATURE_X86)
664 fast_bzero(page_address(start
), npage_bytes(1+end
-start
));
666 bzero(page_address(start
), npage_bytes(1+end
-start
));
672 zero_and_mark_pages(page_index_t start
, page_index_t end
) {
675 zero_pages(start
, end
);
676 for (i
= start
; i
<= end
; i
++)
677 page_table
[i
].need_to_zero
= 0;
680 /* Zero the pages from START to END (inclusive), except for those
681 * pages that are known to already zeroed. Mark all pages in the
682 * ranges as non-zeroed.
685 zero_dirty_pages(page_index_t start
, page_index_t end
) {
688 for (i
= start
; i
<= end
; i
++) {
689 if (!page_table
[i
].need_to_zero
) continue;
690 for (j
= i
+1; (j
<= end
) && (page_table
[j
].need_to_zero
); j
++);
695 for (i
= start
; i
<= end
; i
++) {
696 page_table
[i
].need_to_zero
= 1;
702 * To support quick and inline allocation, regions of memory can be
703 * allocated and then allocated from with just a free pointer and a
704 * check against an end address.
706 * Since objects can be allocated to spaces with different properties
707 * e.g. boxed/unboxed, generation, ages; there may need to be many
708 * allocation regions.
710 * Each allocation region may start within a partly used page. Many
711 * features of memory use are noted on a page wise basis, e.g. the
712 * generation; so if a region starts within an existing allocated page
713 * it must be consistent with this page.
715 * During the scavenging of the newspace, objects will be transported
716 * into an allocation region, and pointers updated to point to this
717 * allocation region. It is possible that these pointers will be
718 * scavenged again before the allocation region is closed, e.g. due to
719 * trans_list which jumps all over the place to cleanup the list. It
720 * is important to be able to determine properties of all objects
721 * pointed to when scavenging, e.g to detect pointers to the oldspace.
722 * Thus it's important that the allocation regions have the correct
723 * properties set when allocated, and not just set when closed. The
724 * region allocation routines return regions with the specified
725 * properties, and grab all the pages, setting their properties
726 * appropriately, except that the amount used is not known.
728 * These regions are used to support quicker allocation using just a
729 * free pointer. The actual space used by the region is not reflected
730 * in the pages tables until it is closed. It can't be scavenged until
733 * When finished with the region it should be closed, which will
734 * update the page tables for the actual space used returning unused
735 * space. Further it may be noted in the new regions which is
736 * necessary when scavenging the newspace.
738 * Large objects may be allocated directly without an allocation
739 * region, the page tables are updated immediately.
741 * Unboxed objects don't contain pointers to other objects and so
742 * don't need scavenging. Further they can't contain pointers to
743 * younger generations so WP is not needed. By allocating pages to
744 * unboxed objects the whole page never needs scavenging or
745 * write-protecting. */
747 /* We are only using two regions at present. Both are for the current
748 * newspace generation. */
749 struct alloc_region boxed_region
;
750 struct alloc_region unboxed_region
;
752 /* The generation currently being allocated to. */
753 static generation_index_t gc_alloc_generation
;
755 static inline page_index_t
756 generation_alloc_start_page(generation_index_t generation
, int page_type_flag
, int large
)
759 if (UNBOXED_PAGE_FLAG
== page_type_flag
) {
760 return generations
[generation
].alloc_large_unboxed_start_page
;
761 } else if (BOXED_PAGE_FLAG
& page_type_flag
) {
762 /* Both code and data. */
763 return generations
[generation
].alloc_large_start_page
;
765 lose("bad page type flag: %d", page_type_flag
);
768 if (UNBOXED_PAGE_FLAG
== page_type_flag
) {
769 return generations
[generation
].alloc_unboxed_start_page
;
770 } else if (BOXED_PAGE_FLAG
& page_type_flag
) {
771 /* Both code and data. */
772 return generations
[generation
].alloc_start_page
;
774 lose("bad page_type_flag: %d", page_type_flag
);
780 set_generation_alloc_start_page(generation_index_t generation
, int page_type_flag
, int large
,
784 if (UNBOXED_PAGE_FLAG
== page_type_flag
) {
785 generations
[generation
].alloc_large_unboxed_start_page
= page
;
786 } else if (BOXED_PAGE_FLAG
& page_type_flag
) {
787 /* Both code and data. */
788 generations
[generation
].alloc_large_start_page
= page
;
790 lose("bad page type flag: %d", page_type_flag
);
793 if (UNBOXED_PAGE_FLAG
== page_type_flag
) {
794 generations
[generation
].alloc_unboxed_start_page
= page
;
795 } else if (BOXED_PAGE_FLAG
& page_type_flag
) {
796 /* Both code and data. */
797 generations
[generation
].alloc_start_page
= page
;
799 lose("bad page type flag: %d", page_type_flag
);
804 const int n_dwords_in_card
= GENCGC_CARD_BYTES
/ N_WORD_BYTES
/ 2;
806 pinned_dwords(page_index_t page
)
808 if (page_table
[page
].has_pin_map
)
809 return &page_table_pinned_dwords
[page
* (n_dwords_in_card
/N_WORD_BITS
)];
813 /* Find a new region with room for at least the given number of bytes.
815 * It starts looking at the current generation's alloc_start_page. So
816 * may pick up from the previous region if there is enough space. This
817 * keeps the allocation contiguous when scavenging the newspace.
819 * The alloc_region should have been closed by a call to
820 * gc_alloc_update_page_tables(), and will thus be in an empty state.
822 * To assist the scavenging functions write-protected pages are not
823 * used. Free pages should not be write-protected.
825 * It is critical to the conservative GC that the start of regions be
826 * known. To help achieve this only small regions are allocated at a
829 * During scavenging, pointers may be found to within the current
830 * region and the page generation must be set so that pointers to the
831 * from space can be recognized. Therefore the generation of pages in
832 * the region are set to gc_alloc_generation. To prevent another
833 * allocation call using the same pages, all the pages in the region
834 * are allocated, although they will initially be empty.
837 gc_alloc_new_region(sword_t nbytes
, int page_type_flag
, struct alloc_region
*alloc_region
)
839 page_index_t first_page
;
840 page_index_t last_page
;
841 os_vm_size_t bytes_found
;
847 "/alloc_new_region for %d bytes from gen %d\n",
848 nbytes, gc_alloc_generation));
851 /* Check that the region is in a reset state. */
852 gc_assert((alloc_region
->first_page
== 0)
853 && (alloc_region
->last_page
== -1)
854 && (alloc_region
->free_pointer
== alloc_region
->end_addr
));
855 ret
= thread_mutex_lock(&free_pages_lock
);
857 first_page
= generation_alloc_start_page(gc_alloc_generation
, page_type_flag
, 0);
858 last_page
=gc_find_freeish_pages(&first_page
, nbytes
, page_type_flag
);
859 bytes_found
=(GENCGC_CARD_BYTES
- page_table
[first_page
].bytes_used
)
860 + npage_bytes(last_page
-first_page
);
862 /* Set up the alloc_region. */
863 alloc_region
->first_page
= first_page
;
864 alloc_region
->last_page
= last_page
;
865 alloc_region
->start_addr
= page_table
[first_page
].bytes_used
866 + page_address(first_page
);
867 alloc_region
->free_pointer
= alloc_region
->start_addr
;
868 alloc_region
->end_addr
= alloc_region
->start_addr
+ bytes_found
;
870 /* Set up the pages. */
872 /* The first page may have already been in use. */
873 if (page_table
[first_page
].bytes_used
== 0) {
874 page_table
[first_page
].allocated
= page_type_flag
;
875 page_table
[first_page
].gen
= gc_alloc_generation
;
876 page_table
[first_page
].large_object
= 0;
877 page_table
[first_page
].scan_start_offset
= 0;
878 // wiping should have free()ed and :=NULL
879 gc_assert(pinned_dwords(first_page
) == NULL
);
882 gc_assert(page_table
[first_page
].allocated
== page_type_flag
);
883 page_table
[first_page
].allocated
|= OPEN_REGION_PAGE_FLAG
;
885 gc_assert(page_table
[first_page
].gen
== gc_alloc_generation
);
886 gc_assert(page_table
[first_page
].large_object
== 0);
888 for (i
= first_page
+1; i
<= last_page
; i
++) {
889 page_table
[i
].allocated
= page_type_flag
;
890 page_table
[i
].gen
= gc_alloc_generation
;
891 page_table
[i
].large_object
= 0;
892 /* This may not be necessary for unboxed regions (think it was
894 page_table
[i
].scan_start_offset
=
895 void_diff(page_address(i
),alloc_region
->start_addr
);
896 page_table
[i
].allocated
|= OPEN_REGION_PAGE_FLAG
;
898 /* Bump up last_free_page. */
899 if (last_page
+1 > last_free_page
) {
900 last_free_page
= last_page
+1;
901 /* do we only want to call this on special occasions? like for
903 set_alloc_pointer((lispobj
)page_address(last_free_page
));
905 ret
= thread_mutex_unlock(&free_pages_lock
);
908 #ifdef READ_PROTECT_FREE_PAGES
909 os_protect(page_address(first_page
),
910 npage_bytes(1+last_page
-first_page
),
914 /* If the first page was only partial, don't check whether it's
915 * zeroed (it won't be) and don't zero it (since the parts that
916 * we're interested in are guaranteed to be zeroed).
918 if (page_table
[first_page
].bytes_used
) {
922 zero_dirty_pages(first_page
, last_page
);
924 /* we can do this after releasing free_pages_lock */
925 if (gencgc_zero_check
) {
927 for (p
= (word_t
*)alloc_region
->start_addr
;
928 p
< (word_t
*)alloc_region
->end_addr
; p
++) {
930 lose("The new region is not zero at %p (start=%p, end=%p).\n",
931 p
, alloc_region
->start_addr
, alloc_region
->end_addr
);
937 /* If the record_new_objects flag is 2 then all new regions created
940 * If it's 1 then then it is only recorded if the first page of the
941 * current region is <= new_areas_ignore_page. This helps avoid
942 * unnecessary recording when doing full scavenge pass.
944 * The new_object structure holds the page, byte offset, and size of
945 * new regions of objects. Each new area is placed in the array of
946 * these structures pointer to by new_areas. new_areas_index holds the
947 * offset into new_areas.
949 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
950 * later code must detect this and handle it, probably by doing a full
951 * scavenge of a generation. */
952 #define NUM_NEW_AREAS 512
953 static int record_new_objects
= 0;
954 static page_index_t new_areas_ignore_page
;
960 static struct new_area (*new_areas
)[];
961 static size_t new_areas_index
;
962 size_t max_new_areas
;
964 /* Add a new area to new_areas. */
966 add_new_area(page_index_t first_page
, size_t offset
, size_t size
)
968 size_t new_area_start
, c
;
971 /* Ignore if full. */
972 if (new_areas_index
>= NUM_NEW_AREAS
)
975 switch (record_new_objects
) {
979 if (first_page
> new_areas_ignore_page
)
988 new_area_start
= npage_bytes(first_page
) + offset
;
990 /* Search backwards for a prior area that this follows from. If
991 found this will save adding a new area. */
992 for (i
= new_areas_index
-1, c
= 0; (i
>= 0) && (c
< 8); i
--, c
++) {
994 npage_bytes((*new_areas
)[i
].page
)
995 + (*new_areas
)[i
].offset
996 + (*new_areas
)[i
].size
;
998 "/add_new_area S1 %d %d %d %d\n",
999 i, c, new_area_start, area_end));*/
1000 if (new_area_start
== area_end
) {
1002 "/adding to [%d] %d %d %d with %d %d %d:\n",
1004 (*new_areas)[i].page,
1005 (*new_areas)[i].offset,
1006 (*new_areas)[i].size,
1010 (*new_areas
)[i
].size
+= size
;
1015 (*new_areas
)[new_areas_index
].page
= first_page
;
1016 (*new_areas
)[new_areas_index
].offset
= offset
;
1017 (*new_areas
)[new_areas_index
].size
= size
;
1019 "/new_area %d page %d offset %d size %d\n",
1020 new_areas_index, first_page, offset, size));*/
1023 /* Note the max new_areas used. */
1024 if (new_areas_index
> max_new_areas
)
1025 max_new_areas
= new_areas_index
;
1028 /* Update the tables for the alloc_region. The region may be added to
1031 * When done the alloc_region is set up so that the next quick alloc
1032 * will fail safely and thus a new region will be allocated. Further
1033 * it is safe to try to re-update the page table of this reset
1036 gc_alloc_update_page_tables(int page_type_flag
, struct alloc_region
*alloc_region
)
1039 page_index_t first_page
;
1040 page_index_t next_page
;
1041 os_vm_size_t bytes_used
;
1042 os_vm_size_t region_size
;
1043 os_vm_size_t byte_cnt
;
1044 page_bytes_t orig_first_page_bytes_used
;
1048 first_page
= alloc_region
->first_page
;
1050 /* Catch an unused alloc_region. */
1051 if ((first_page
== 0) && (alloc_region
->last_page
== -1))
1054 next_page
= first_page
+1;
1056 ret
= thread_mutex_lock(&free_pages_lock
);
1057 gc_assert(ret
== 0);
1058 if (alloc_region
->free_pointer
!= alloc_region
->start_addr
) {
1059 /* some bytes were allocated in the region */
1060 orig_first_page_bytes_used
= page_table
[first_page
].bytes_used
;
1062 gc_assert(alloc_region
->start_addr
==
1063 (page_address(first_page
)
1064 + page_table
[first_page
].bytes_used
));
1066 /* All the pages used need to be updated */
1068 /* Update the first page. */
1070 /* If the page was free then set up the gen, and
1071 * scan_start_offset. */
1072 if (page_table
[first_page
].bytes_used
== 0)
1073 gc_assert(page_starts_contiguous_block_p(first_page
));
1074 page_table
[first_page
].allocated
&= ~(OPEN_REGION_PAGE_FLAG
);
1076 gc_assert(page_table
[first_page
].allocated
& page_type_flag
);
1077 gc_assert(page_table
[first_page
].gen
== gc_alloc_generation
);
1078 gc_assert(page_table
[first_page
].large_object
== 0);
1082 /* Calculate the number of bytes used in this page. This is not
1083 * always the number of new bytes, unless it was free. */
1085 if ((bytes_used
= void_diff(alloc_region
->free_pointer
,
1086 page_address(first_page
)))
1087 >GENCGC_CARD_BYTES
) {
1088 bytes_used
= GENCGC_CARD_BYTES
;
1091 page_table
[first_page
].bytes_used
= bytes_used
;
1092 byte_cnt
+= bytes_used
;
1095 /* All the rest of the pages should be free. We need to set
1096 * their scan_start_offset pointer to the start of the
1097 * region, and set the bytes_used. */
1099 page_table
[next_page
].allocated
&= ~(OPEN_REGION_PAGE_FLAG
);
1100 gc_assert(page_table
[next_page
].allocated
& page_type_flag
);
1101 gc_assert(page_table
[next_page
].bytes_used
== 0);
1102 gc_assert(page_table
[next_page
].gen
== gc_alloc_generation
);
1103 gc_assert(page_table
[next_page
].large_object
== 0);
1105 gc_assert(page_table
[next_page
].scan_start_offset
==
1106 void_diff(page_address(next_page
),
1107 alloc_region
->start_addr
));
1109 /* Calculate the number of bytes used in this page. */
1111 if ((bytes_used
= void_diff(alloc_region
->free_pointer
,
1112 page_address(next_page
)))>GENCGC_CARD_BYTES
) {
1113 bytes_used
= GENCGC_CARD_BYTES
;
1116 page_table
[next_page
].bytes_used
= bytes_used
;
1117 byte_cnt
+= bytes_used
;
1122 region_size
= void_diff(alloc_region
->free_pointer
,
1123 alloc_region
->start_addr
);
1124 bytes_allocated
+= region_size
;
1125 generations
[gc_alloc_generation
].bytes_allocated
+= region_size
;
1127 gc_assert((byte_cnt
- orig_first_page_bytes_used
) == region_size
);
1129 /* Set the generations alloc restart page to the last page of
1131 set_generation_alloc_start_page(gc_alloc_generation
, page_type_flag
, 0, next_page
-1);
1133 /* Add the region to the new_areas if requested. */
1134 if (BOXED_PAGE_FLAG
& page_type_flag
)
1135 add_new_area(first_page
,orig_first_page_bytes_used
, region_size
);
1139 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
1141 gc_alloc_generation));
1144 /* There are no bytes allocated. Unallocate the first_page if
1145 * there are 0 bytes_used. */
1146 page_table
[first_page
].allocated
&= ~(OPEN_REGION_PAGE_FLAG
);
1147 if (page_table
[first_page
].bytes_used
== 0)
1148 page_table
[first_page
].allocated
= FREE_PAGE_FLAG
;
1151 /* Unallocate any unused pages. */
1152 while (next_page
<= alloc_region
->last_page
) {
1153 gc_assert(page_table
[next_page
].bytes_used
== 0);
1154 page_table
[next_page
].allocated
= FREE_PAGE_FLAG
;
1157 ret
= thread_mutex_unlock(&free_pages_lock
);
1158 gc_assert(ret
== 0);
1160 /* alloc_region is per-thread, we're ok to do this unlocked */
1161 gc_set_region_empty(alloc_region
);
1164 /* Allocate a possibly large object. */
1166 gc_alloc_large(sword_t nbytes
, int page_type_flag
, struct alloc_region
*alloc_region
)
1169 page_index_t first_page
, next_page
, last_page
;
1170 page_bytes_t orig_first_page_bytes_used
;
1171 os_vm_size_t byte_cnt
;
1172 os_vm_size_t bytes_used
;
1175 ret
= thread_mutex_lock(&free_pages_lock
);
1176 gc_assert(ret
== 0);
1178 first_page
= generation_alloc_start_page(gc_alloc_generation
, page_type_flag
, 1);
1179 if (first_page
<= alloc_region
->last_page
) {
1180 first_page
= alloc_region
->last_page
+1;
1183 last_page
=gc_find_freeish_pages(&first_page
,nbytes
, page_type_flag
);
1185 gc_assert(first_page
> alloc_region
->last_page
);
1187 set_generation_alloc_start_page(gc_alloc_generation
, page_type_flag
, 1, last_page
);
1189 /* Set up the pages. */
1190 orig_first_page_bytes_used
= page_table
[first_page
].bytes_used
;
1192 /* If the first page was free then set up the gen, and
1193 * scan_start_offset. */
1194 if (page_table
[first_page
].bytes_used
== 0) {
1195 page_table
[first_page
].allocated
= page_type_flag
;
1196 page_table
[first_page
].gen
= gc_alloc_generation
;
1197 page_table
[first_page
].scan_start_offset
= 0;
1198 page_table
[first_page
].large_object
= 1;
1201 gc_assert(page_table
[first_page
].allocated
== page_type_flag
);
1202 gc_assert(page_table
[first_page
].gen
== gc_alloc_generation
);
1203 gc_assert(page_table
[first_page
].large_object
== 1);
1207 /* Calc. the number of bytes used in this page. This is not
1208 * always the number of new bytes, unless it was free. */
1210 if ((bytes_used
= nbytes
+orig_first_page_bytes_used
) > GENCGC_CARD_BYTES
) {
1211 bytes_used
= GENCGC_CARD_BYTES
;
1214 page_table
[first_page
].bytes_used
= bytes_used
;
1215 byte_cnt
+= bytes_used
;
1217 next_page
= first_page
+1;
1219 /* All the rest of the pages should be free. We need to set their
1220 * scan_start_offset pointer to the start of the region, and set
1221 * the bytes_used. */
1223 gc_assert(page_free_p(next_page
));
1224 gc_assert(page_table
[next_page
].bytes_used
== 0);
1225 page_table
[next_page
].allocated
= page_type_flag
;
1226 page_table
[next_page
].gen
= gc_alloc_generation
;
1227 page_table
[next_page
].large_object
= 1;
1229 page_table
[next_page
].scan_start_offset
=
1230 npage_bytes(next_page
-first_page
) - orig_first_page_bytes_used
;
1232 /* Calculate the number of bytes used in this page. */
1234 bytes_used
=(nbytes
+orig_first_page_bytes_used
)-byte_cnt
;
1235 if (bytes_used
> GENCGC_CARD_BYTES
) {
1236 bytes_used
= GENCGC_CARD_BYTES
;
1239 page_table
[next_page
].bytes_used
= bytes_used
;
1240 page_table
[next_page
].write_protected
=0;
1241 page_table
[next_page
].dont_move
=0;
1242 byte_cnt
+= bytes_used
;
1246 gc_assert((byte_cnt
-orig_first_page_bytes_used
) == (size_t)nbytes
);
1248 bytes_allocated
+= nbytes
;
1249 generations
[gc_alloc_generation
].bytes_allocated
+= nbytes
;
1251 /* Add the region to the new_areas if requested. */
1252 if (BOXED_PAGE_FLAG
& page_type_flag
)
1253 add_new_area(first_page
,orig_first_page_bytes_used
,nbytes
);
1255 /* Bump up last_free_page */
1256 if (last_page
+1 > last_free_page
) {
1257 last_free_page
= last_page
+1;
1258 set_alloc_pointer((lispobj
)(page_address(last_free_page
)));
1260 ret
= thread_mutex_unlock(&free_pages_lock
);
1261 gc_assert(ret
== 0);
1263 #ifdef READ_PROTECT_FREE_PAGES
1264 os_protect(page_address(first_page
),
1265 npage_bytes(1+last_page
-first_page
),
1269 zero_dirty_pages(first_page
, last_page
);
1271 return page_address(first_page
);
1274 static page_index_t gencgc_alloc_start_page
= -1;
1277 gc_heap_exhausted_error_or_lose (sword_t available
, sword_t requested
)
1279 struct thread
*thread
= arch_os_get_current_thread();
1280 /* Write basic information before doing anything else: if we don't
1281 * call to lisp this is a must, and even if we do there is always
1282 * the danger that we bounce back here before the error has been
1283 * handled, or indeed even printed.
1285 report_heap_exhaustion(available
, requested
, thread
);
1286 if (gc_active_p
|| (available
== 0)) {
1287 /* If we are in GC, or totally out of memory there is no way
1288 * to sanely transfer control to the lisp-side of things.
1290 lose("Heap exhausted, game over.");
1293 /* FIXME: assert free_pages_lock held */
1294 (void)thread_mutex_unlock(&free_pages_lock
);
1295 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
1296 gc_assert(get_pseudo_atomic_atomic(thread
));
1297 clear_pseudo_atomic_atomic(thread
);
1298 if (get_pseudo_atomic_interrupted(thread
))
1299 do_pending_interrupt();
1301 /* Another issue is that signalling HEAP-EXHAUSTED error leads
1302 * to running user code at arbitrary places, even in a
1303 * WITHOUT-INTERRUPTS which may lead to a deadlock without
1304 * running out of the heap. So at this point all bets are
1306 if (SymbolValue(INTERRUPTS_ENABLED
,thread
) == NIL
)
1307 corruption_warning_and_maybe_lose
1308 ("Signalling HEAP-EXHAUSTED in a WITHOUT-INTERRUPTS.");
1309 /* available and requested should be double word aligned, thus
1310 they can passed as fixnums and shifted later. */
1311 funcall2(StaticSymbolFunction(HEAP_EXHAUSTED_ERROR
), available
, requested
);
1312 lose("HEAP-EXHAUSTED-ERROR fell through");
1317 gc_find_freeish_pages(page_index_t
*restart_page_ptr
, sword_t bytes
,
1320 page_index_t most_bytes_found_from
= 0, most_bytes_found_to
= 0;
1321 page_index_t first_page
, last_page
, restart_page
= *restart_page_ptr
;
1322 os_vm_size_t nbytes
= bytes
;
1323 os_vm_size_t nbytes_goal
= nbytes
;
1324 os_vm_size_t bytes_found
= 0;
1325 os_vm_size_t most_bytes_found
= 0;
1326 boolean small_object
= nbytes
< GENCGC_CARD_BYTES
;
1327 /* FIXME: assert(free_pages_lock is held); */
1329 if (nbytes_goal
< gencgc_alloc_granularity
)
1330 nbytes_goal
= gencgc_alloc_granularity
;
1332 /* Toggled by gc_and_save for heap compaction, normally -1. */
1333 if (gencgc_alloc_start_page
!= -1) {
1334 restart_page
= gencgc_alloc_start_page
;
1337 /* FIXME: This is on bytes instead of nbytes pending cleanup of
1338 * long from the interface. */
1339 gc_assert(bytes
>=0);
1340 /* Search for a page with at least nbytes of space. We prefer
1341 * not to split small objects on multiple pages, to reduce the
1342 * number of contiguous allocation regions spaning multiple
1343 * pages: this helps avoid excessive conservativism.
1345 * For other objects, we guarantee that they start on their own
1348 first_page
= restart_page
;
1349 while (first_page
< page_table_pages
) {
1351 if (page_free_p(first_page
)) {
1352 gc_assert(0 == page_table
[first_page
].bytes_used
);
1353 bytes_found
= GENCGC_CARD_BYTES
;
1354 } else if (small_object
&&
1355 (page_table
[first_page
].allocated
== page_type_flag
) &&
1356 (page_table
[first_page
].large_object
== 0) &&
1357 (page_table
[first_page
].gen
== gc_alloc_generation
) &&
1358 (page_table
[first_page
].write_protected
== 0) &&
1359 (page_table
[first_page
].dont_move
== 0)) {
1360 bytes_found
= GENCGC_CARD_BYTES
- page_table
[first_page
].bytes_used
;
1361 if (bytes_found
< nbytes
) {
1362 if (bytes_found
> most_bytes_found
)
1363 most_bytes_found
= bytes_found
;
1372 gc_assert(page_table
[first_page
].write_protected
== 0);
1373 for (last_page
= first_page
+1;
1374 ((last_page
< page_table_pages
) &&
1375 page_free_p(last_page
) &&
1376 (bytes_found
< nbytes_goal
));
1378 bytes_found
+= GENCGC_CARD_BYTES
;
1379 gc_assert(0 == page_table
[last_page
].bytes_used
);
1380 gc_assert(0 == page_table
[last_page
].write_protected
);
1383 if (bytes_found
> most_bytes_found
) {
1384 most_bytes_found
= bytes_found
;
1385 most_bytes_found_from
= first_page
;
1386 most_bytes_found_to
= last_page
;
1388 if (bytes_found
>= nbytes_goal
)
1391 first_page
= last_page
;
1394 bytes_found
= most_bytes_found
;
1395 restart_page
= first_page
+ 1;
1397 /* Check for a failure */
1398 if (bytes_found
< nbytes
) {
1399 gc_assert(restart_page
>= page_table_pages
);
1400 gc_heap_exhausted_error_or_lose(most_bytes_found
, nbytes
);
1403 gc_assert(most_bytes_found_to
);
1404 *restart_page_ptr
= most_bytes_found_from
;
1405 return most_bytes_found_to
-1;
1408 /* Allocate bytes. All the rest of the special-purpose allocation
1409 * functions will eventually call this */
1412 gc_alloc_with_region(sword_t nbytes
,int page_type_flag
, struct alloc_region
*my_region
,
1415 void *new_free_pointer
;
1417 if (nbytes
>=LARGE_OBJECT_SIZE
)
1418 return gc_alloc_large(nbytes
, page_type_flag
, my_region
);
1420 /* Check whether there is room in the current alloc region. */
1421 new_free_pointer
= my_region
->free_pointer
+ nbytes
;
1423 /* fprintf(stderr, "alloc %d bytes from %p to %p\n", nbytes,
1424 my_region->free_pointer, new_free_pointer); */
1426 if (new_free_pointer
<= my_region
->end_addr
) {
1427 /* If so then allocate from the current alloc region. */
1428 void *new_obj
= my_region
->free_pointer
;
1429 my_region
->free_pointer
= new_free_pointer
;
1431 /* Unless a `quick' alloc was requested, check whether the
1432 alloc region is almost empty. */
1434 void_diff(my_region
->end_addr
,my_region
->free_pointer
) <= 32) {
1435 /* If so, finished with the current region. */
1436 gc_alloc_update_page_tables(page_type_flag
, my_region
);
1437 /* Set up a new region. */
1438 gc_alloc_new_region(32 /*bytes*/, page_type_flag
, my_region
);
1441 return((void *)new_obj
);
1444 /* Else not enough free space in the current region: retry with a
1447 gc_alloc_update_page_tables(page_type_flag
, my_region
);
1448 gc_alloc_new_region(nbytes
, page_type_flag
, my_region
);
1449 return gc_alloc_with_region(nbytes
, page_type_flag
, my_region
,0);
1452 /* Copy a large object. If the object is in a large object region then
1453 * it is simply promoted, else it is copied. If it's large enough then
1454 * it's copied to a large object region.
1456 * Bignums and vectors may have shrunk. If the object is not copied
1457 * the space needs to be reclaimed, and the page_tables corrected. */
1459 general_copy_large_object(lispobj object
, word_t nwords
, boolean boxedp
)
1463 page_index_t first_page
;
1465 gc_assert(is_lisp_pointer(object
));
1466 gc_assert(from_space_p(object
));
1467 gc_assert((nwords
& 0x01) == 0);
1469 if ((nwords
> 1024*1024) && gencgc_verbose
) {
1470 FSHOW((stderr
, "/general_copy_large_object: %d bytes\n",
1471 nwords
*N_WORD_BYTES
));
1474 /* Check whether it's a large object. */
1475 first_page
= find_page_index((void *)object
);
1476 gc_assert(first_page
>= 0);
1478 if (page_table
[first_page
].large_object
) {
1479 /* Promote the object. Note: Unboxed objects may have been
1480 * allocated to a BOXED region so it may be necessary to
1481 * change the region to UNBOXED. */
1482 os_vm_size_t remaining_bytes
;
1483 os_vm_size_t bytes_freed
;
1484 page_index_t next_page
;
1485 page_bytes_t old_bytes_used
;
1487 /* FIXME: This comment is somewhat stale.
1489 * Note: Any page write-protection must be removed, else a
1490 * later scavenge_newspace may incorrectly not scavenge these
1491 * pages. This would not be necessary if they are added to the
1492 * new areas, but let's do it for them all (they'll probably
1493 * be written anyway?). */
1495 gc_assert(page_starts_contiguous_block_p(first_page
));
1496 next_page
= first_page
;
1497 remaining_bytes
= nwords
*N_WORD_BYTES
;
1499 while (remaining_bytes
> GENCGC_CARD_BYTES
) {
1500 gc_assert(page_table
[next_page
].gen
== from_space
);
1501 gc_assert(page_table
[next_page
].large_object
);
1502 gc_assert(page_table
[next_page
].scan_start_offset
==
1503 npage_bytes(next_page
-first_page
));
1504 gc_assert(page_table
[next_page
].bytes_used
== GENCGC_CARD_BYTES
);
1505 /* Should have been unprotected by unprotect_oldspace()
1506 * for boxed objects, and after promotion unboxed ones
1507 * should not be on protected pages at all. */
1508 gc_assert(!page_table
[next_page
].write_protected
);
1511 gc_assert(page_boxed_p(next_page
));
1513 gc_assert(page_allocated_no_region_p(next_page
));
1514 page_table
[next_page
].allocated
= UNBOXED_PAGE_FLAG
;
1516 page_table
[next_page
].gen
= new_space
;
1518 remaining_bytes
-= GENCGC_CARD_BYTES
;
1522 /* Now only one page remains, but the object may have shrunk so
1523 * there may be more unused pages which will be freed. */
1525 /* Object may have shrunk but shouldn't have grown - check. */
1526 gc_assert(page_table
[next_page
].bytes_used
>= remaining_bytes
);
1528 page_table
[next_page
].gen
= new_space
;
1531 gc_assert(page_boxed_p(next_page
));
1533 page_table
[next_page
].allocated
= UNBOXED_PAGE_FLAG
;
1535 /* Adjust the bytes_used. */
1536 old_bytes_used
= page_table
[next_page
].bytes_used
;
1537 page_table
[next_page
].bytes_used
= remaining_bytes
;
1539 bytes_freed
= old_bytes_used
- remaining_bytes
;
1541 /* Free any remaining pages; needs care. */
1543 while ((old_bytes_used
== GENCGC_CARD_BYTES
) &&
1544 (page_table
[next_page
].gen
== from_space
) &&
1545 /* FIXME: It is not obvious to me why this is necessary
1546 * as a loop condition: it seems to me that the
1547 * scan_start_offset test should be sufficient, but
1548 * experimentally that is not the case. --NS
1551 page_boxed_p(next_page
) :
1552 page_allocated_no_region_p(next_page
)) &&
1553 page_table
[next_page
].large_object
&&
1554 (page_table
[next_page
].scan_start_offset
==
1555 npage_bytes(next_page
- first_page
))) {
1556 /* Checks out OK, free the page. Don't need to both zeroing
1557 * pages as this should have been done before shrinking the
1558 * object. These pages shouldn't be write-protected, even if
1559 * boxed they should be zero filled. */
1560 gc_assert(page_table
[next_page
].write_protected
== 0);
1562 old_bytes_used
= page_table
[next_page
].bytes_used
;
1563 page_table
[next_page
].allocated
= FREE_PAGE_FLAG
;
1564 page_table
[next_page
].bytes_used
= 0;
1565 bytes_freed
+= old_bytes_used
;
1569 if ((bytes_freed
> 0) && gencgc_verbose
) {
1571 "/general_copy_large_object bytes_freed=%"OS_VM_SIZE_FMT
"\n",
1575 generations
[from_space
].bytes_allocated
-= nwords
*N_WORD_BYTES
1577 generations
[new_space
].bytes_allocated
+= nwords
*N_WORD_BYTES
;
1578 bytes_allocated
-= bytes_freed
;
1580 /* Add the region to the new_areas if requested. */
1582 add_new_area(first_page
,0,nwords
*N_WORD_BYTES
);
1587 /* Get tag of object. */
1588 tag
= lowtag_of(object
);
1590 /* Allocate space. */
1591 new = gc_general_alloc(nwords
*N_WORD_BYTES
,
1592 (boxedp
? BOXED_PAGE_FLAG
: UNBOXED_PAGE_FLAG
),
1595 /* Copy the object. */
1596 memcpy(new,native_pointer(object
),nwords
*N_WORD_BYTES
);
1598 /* Return Lisp pointer of new object. */
1599 return ((lispobj
) new) | tag
;
1604 copy_large_object(lispobj object
, sword_t nwords
)
1606 return general_copy_large_object(object
, nwords
, 1);
1610 copy_large_unboxed_object(lispobj object
, sword_t nwords
)
1612 return general_copy_large_object(object
, nwords
, 0);
1615 /* to copy unboxed objects */
1617 copy_unboxed_object(lispobj object
, sword_t nwords
)
1619 return gc_general_copy_object(object
, nwords
, UNBOXED_PAGE_FLAG
);
1624 * code and code-related objects
1627 static lispobj trans_fun_header(lispobj object);
1628 static lispobj trans_boxed(lispobj object);
1631 /* Scan a x86 compiled code object, looking for possible fixups that
1632 * have been missed after a move.
1634 * Two types of fixups are needed:
1635 * 1. Absolute fixups to within the code object.
1636 * 2. Relative fixups to outside the code object.
1638 * Currently only absolute fixups to the constant vector, or to the
1639 * code area are checked. */
1640 #ifdef LISP_FEATURE_X86
1642 sniff_code_object(struct code
*code
, os_vm_size_t displacement
)
1644 sword_t nheader_words
, ncode_words
, nwords
;
1645 os_vm_address_t constants_start_addr
= NULL
, constants_end_addr
, p
;
1646 os_vm_address_t code_start_addr
, code_end_addr
;
1647 os_vm_address_t code_addr
= (os_vm_address_t
)code
;
1648 int fixup_found
= 0;
1650 if (!check_code_fixups
)
1653 FSHOW((stderr
, "/sniffing code: %p, %lu\n", code
, displacement
));
1655 ncode_words
= code_instruction_words(code
->code_size
);
1656 nheader_words
= code_header_words(*(lispobj
*)code
);
1657 nwords
= ncode_words
+ nheader_words
;
1659 constants_start_addr
= code_addr
+ 5*N_WORD_BYTES
;
1660 constants_end_addr
= code_addr
+ nheader_words
*N_WORD_BYTES
;
1661 code_start_addr
= code_addr
+ nheader_words
*N_WORD_BYTES
;
1662 code_end_addr
= code_addr
+ nwords
*N_WORD_BYTES
;
1664 /* Work through the unboxed code. */
1665 for (p
= code_start_addr
; p
< code_end_addr
; p
++) {
1666 void *data
= *(void **)p
;
1667 unsigned d1
= *((unsigned char *)p
- 1);
1668 unsigned d2
= *((unsigned char *)p
- 2);
1669 unsigned d3
= *((unsigned char *)p
- 3);
1670 unsigned d4
= *((unsigned char *)p
- 4);
1672 unsigned d5
= *((unsigned char *)p
- 5);
1673 unsigned d6
= *((unsigned char *)p
- 6);
1676 /* Check for code references. */
1677 /* Check for a 32 bit word that looks like an absolute
1678 reference to within the code adea of the code object. */
1679 if ((data
>= (void*)(code_start_addr
-displacement
))
1680 && (data
< (void*)(code_end_addr
-displacement
))) {
1681 /* function header */
1683 && (((unsigned)p
- 4 - 4*HeaderValue(*((unsigned *)p
-1))) ==
1685 /* Skip the function header */
1689 /* the case of PUSH imm32 */
1693 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1694 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1695 FSHOW((stderr
, "/PUSH $0x%.8x\n", data
));
1697 /* the case of MOV [reg-8],imm32 */
1699 && (d2
==0x40 || d2
==0x41 || d2
==0x42 || d2
==0x43
1700 || d2
==0x45 || d2
==0x46 || d2
==0x47)
1704 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1705 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1706 FSHOW((stderr
, "/MOV [reg-8],$0x%.8x\n", data
));
1708 /* the case of LEA reg,[disp32] */
1709 if ((d2
== 0x8d) && ((d1
& 0xc7) == 5)) {
1712 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1713 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1714 FSHOW((stderr
,"/LEA reg,[$0x%.8x]\n", data
));
1718 /* Check for constant references. */
1719 /* Check for a 32 bit word that looks like an absolute
1720 reference to within the constant vector. Constant references
1722 if ((data
>= (void*)(constants_start_addr
-displacement
))
1723 && (data
< (void*)(constants_end_addr
-displacement
))
1724 && (((unsigned)data
& 0x3) == 0)) {
1729 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1730 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1731 FSHOW((stderr
,"/MOV eax,0x%.8x\n", data
));
1734 /* the case of MOV m32,EAX */
1738 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1739 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1740 FSHOW((stderr
, "/MOV 0x%.8x,eax\n", data
));
1743 /* the case of CMP m32,imm32 */
1744 if ((d1
== 0x3d) && (d2
== 0x81)) {
1747 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1748 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1750 FSHOW((stderr
, "/CMP 0x%.8x,immed32\n", data
));
1753 /* Check for a mod=00, r/m=101 byte. */
1754 if ((d1
& 0xc7) == 5) {
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
,"/CMP 0x%.8x,reg\n", data
));
1763 /* the case of CMP reg32,m32 */
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
, "/CMP reg32,0x%.8x\n", data
));
1771 /* the case of MOV m32,reg32 */
1775 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1776 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1777 FSHOW((stderr
, "/MOV 0x%.8x,reg32\n", data
));
1779 /* the case of MOV reg32,m32 */
1783 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1784 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1785 FSHOW((stderr
, "/MOV reg32,0x%.8x\n", data
));
1787 /* the case of LEA reg32,m32 */
1791 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1792 p
, d6
, d5
, d4
, d3
, d2
, d1
, data
));
1793 FSHOW((stderr
, "/LEA reg32,0x%.8x\n", data
));
1799 /* If anything was found, print some information on the code
1803 "/compiled code object at %x: header words = %d, code words = %d\n",
1804 code
, nheader_words
, ncode_words
));
1806 "/const start = %x, end = %x\n",
1807 constants_start_addr
, constants_end_addr
));
1809 "/code start = %x, end = %x\n",
1810 code_start_addr
, code_end_addr
));
1815 #ifdef LISP_FEATURE_X86
1817 gencgc_apply_code_fixups(struct code
*old_code
, struct code
*new_code
)
1819 sword_t nheader_words
, ncode_words
, nwords
;
1820 os_vm_address_t constants_start_addr
, constants_end_addr
;
1821 os_vm_address_t code_start_addr
, code_end_addr
;
1822 os_vm_address_t code_addr
= (os_vm_address_t
)new_code
;
1823 os_vm_address_t old_addr
= (os_vm_address_t
)old_code
;
1824 os_vm_size_t displacement
= code_addr
- old_addr
;
1825 lispobj fixups
= NIL
;
1826 struct vector
*fixups_vector
;
1828 ncode_words
= code_instruction_words(new_code
->code_size
);
1829 nheader_words
= code_header_words(*(lispobj
*)new_code
);
1830 nwords
= ncode_words
+ nheader_words
;
1832 "/compiled code object at %x: header words = %d, code words = %d\n",
1833 new_code, nheader_words, ncode_words)); */
1834 constants_start_addr
= code_addr
+ 5*N_WORD_BYTES
;
1835 constants_end_addr
= code_addr
+ nheader_words
*N_WORD_BYTES
;
1836 code_start_addr
= code_addr
+ nheader_words
*N_WORD_BYTES
;
1837 code_end_addr
= code_addr
+ nwords
*N_WORD_BYTES
;
1840 "/const start = %x, end = %x\n",
1841 constants_start_addr,constants_end_addr));
1843 "/code start = %x; end = %x\n",
1844 code_start_addr,code_end_addr));
1847 /* The first constant should be a pointer to the fixups for this
1848 code objects. Check. */
1849 fixups
= new_code
->constants
[0];
1851 /* It will be 0 or the unbound-marker if there are no fixups (as
1852 * will be the case if the code object has been purified, for
1853 * example) and will be an other pointer if it is valid. */
1854 if ((fixups
== 0) || (fixups
== UNBOUND_MARKER_WIDETAG
) ||
1855 !is_lisp_pointer(fixups
)) {
1856 /* Check for possible errors. */
1857 if (check_code_fixups
)
1858 sniff_code_object(new_code
, displacement
);
1863 fixups_vector
= (struct vector
*)native_pointer(fixups
);
1865 /* Could be pointing to a forwarding pointer. */
1866 /* FIXME is this always in from_space? if so, could replace this code with
1867 * forwarding_pointer_p/forwarding_pointer_value */
1868 if (is_lisp_pointer(fixups
) &&
1869 (find_page_index((void*)fixups_vector
) != -1) &&
1870 (fixups_vector
->header
== 0x01)) {
1871 /* If so, then follow it. */
1872 /*SHOW("following pointer to a forwarding pointer");*/
1874 (struct vector
*)native_pointer((lispobj
)fixups_vector
->length
);
1877 /*SHOW("got fixups");*/
1879 if (widetag_of(fixups_vector
->header
) == SIMPLE_ARRAY_WORD_WIDETAG
) {
1880 /* Got the fixups for the code block. Now work through the vector,
1881 and apply a fixup at each address. */
1882 sword_t length
= fixnum_value(fixups_vector
->length
);
1884 for (i
= 0; i
< length
; i
++) {
1885 long offset
= fixups_vector
->data
[i
];
1886 /* Now check the current value of offset. */
1887 os_vm_address_t old_value
= *(os_vm_address_t
*)(code_start_addr
+ offset
);
1889 /* If it's within the old_code object then it must be an
1890 * absolute fixup (relative ones are not saved) */
1891 if ((old_value
>= old_addr
)
1892 && (old_value
< (old_addr
+ nwords
*N_WORD_BYTES
)))
1893 /* So add the dispacement. */
1894 *(os_vm_address_t
*)(code_start_addr
+ offset
) =
1895 old_value
+ displacement
;
1897 /* It is outside the old code object so it must be a
1898 * relative fixup (absolute fixups are not saved). So
1899 * subtract the displacement. */
1900 *(os_vm_address_t
*)(code_start_addr
+ offset
) =
1901 old_value
- displacement
;
1904 /* This used to just print a note to stderr, but a bogus fixup seems to
1905 * indicate real heap corruption, so a hard hailure is in order. */
1906 lose("fixup vector %p has a bad widetag: %d\n",
1907 fixups_vector
, widetag_of(fixups_vector
->header
));
1910 /* Check for possible errors. */
1911 if (check_code_fixups
) {
1912 sniff_code_object(new_code
,displacement
);
1918 trans_boxed_large(lispobj object
)
1923 gc_assert(is_lisp_pointer(object
));
1925 header
= *((lispobj
*) native_pointer(object
));
1926 length
= HeaderValue(header
) + 1;
1927 length
= CEILING(length
, 2);
1929 return copy_large_object(object
, length
);
1936 /* XX This is a hack adapted from cgc.c. These don't work too
1937 * efficiently with the gencgc as a list of the weak pointers is
1938 * maintained within the objects which causes writes to the pages. A
1939 * limited attempt is made to avoid unnecessary writes, but this needs
1941 #define WEAK_POINTER_NWORDS \
1942 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
1945 scav_weak_pointer(lispobj
*where
, lispobj object
)
1947 /* Since we overwrite the 'next' field, we have to make
1948 * sure not to do so for pointers already in the list.
1949 * Instead of searching the list of weak_pointers each
1950 * time, we ensure that next is always NULL when the weak
1951 * pointer isn't in the list, and not NULL otherwise.
1952 * Since we can't use NULL to denote end of list, we
1953 * use a pointer back to the same weak_pointer.
1955 struct weak_pointer
* wp
= (struct weak_pointer
*)where
;
1957 if (NULL
== wp
->next
) {
1958 wp
->next
= weak_pointers
;
1960 if (NULL
== wp
->next
)
1964 /* Do not let GC scavenge the value slot of the weak pointer.
1965 * (That is why it is a weak pointer.) */
1967 return WEAK_POINTER_NWORDS
;
1972 search_read_only_space(void *pointer
)
1974 lispobj
*start
= (lispobj
*) READ_ONLY_SPACE_START
;
1975 lispobj
*end
= (lispobj
*) SymbolValue(READ_ONLY_SPACE_FREE_POINTER
,0);
1976 if ((pointer
< (void *)start
) || (pointer
>= (void *)end
))
1978 return (gc_search_space(start
,
1979 (((lispobj
*)pointer
)+2)-start
,
1980 (lispobj
*) pointer
));
1984 search_static_space(void *pointer
)
1986 lispobj
*start
= (lispobj
*)STATIC_SPACE_START
;
1987 lispobj
*end
= (lispobj
*)SymbolValue(STATIC_SPACE_FREE_POINTER
,0);
1988 if ((pointer
< (void *)start
) || (pointer
>= (void *)end
))
1990 return (gc_search_space(start
,
1991 (((lispobj
*)pointer
)+2)-start
,
1992 (lispobj
*) pointer
));
1995 /* a faster version for searching the dynamic space. This will work even
1996 * if the object is in a current allocation region. */
1998 search_dynamic_space(void *pointer
)
2000 page_index_t page_index
= find_page_index(pointer
);
2003 /* The address may be invalid, so do some checks. */
2004 if ((page_index
== -1) || page_free_p(page_index
))
2006 start
= (lispobj
*)page_scan_start(page_index
);
2007 return (gc_search_space(start
,
2008 (((lispobj
*)pointer
)+2)-start
,
2009 (lispobj
*)pointer
));
2012 // Return the starting address of the object containing 'addr'
2013 // if and only if the object is one which would be evacuated from 'from_space'
2014 // were it allowed to be either discarded as garbage or moved.
2015 // 'addr_page_index' is the page containing 'addr' and must not be -1.
2016 // Return 0 if there is no such object - that is, if addr is past the
2017 // end of the used bytes, or its pages are not in 'from_space' etc.
2019 conservative_root_p(void *addr
, page_index_t addr_page_index
)
2021 #ifdef GENCGC_IS_PRECISE
2022 /* If we're in precise gencgc (non-x86oid as of this writing) then
2023 * we are only called on valid object pointers in the first place,
2024 * so we just have to do a bounds-check against the heap, a
2025 * generation check, and the already-pinned check. */
2026 if ((page_table
[addr_page_index
].gen
!= from_space
)
2027 || (page_table
[addr_page_index
].dont_move
!= 0))
2031 /* quick check 1: Address is quite likely to have been invalid. */
2032 if (page_free_p(addr_page_index
)
2033 || (page_table
[addr_page_index
].bytes_used
== 0)
2034 || (page_table
[addr_page_index
].gen
!= from_space
))
2036 gc_assert(!(page_table
[addr_page_index
].allocated
&OPEN_REGION_PAGE_FLAG
));
2038 /* quick check 2: Check the offset within the page.
2041 if (((uword_t
)addr
& (GENCGC_CARD_BYTES
- 1)) >
2042 page_table
[addr_page_index
].bytes_used
)
2045 /* Filter out anything which can't be a pointer to a Lisp object
2046 * (or, as a special case which also requires dont_move, a return
2047 * address referring to something in a CodeObject). This is
2048 * expensive but important, since it vastly reduces the
2049 * probability that random garbage will be bogusly interpreted as
2050 * a pointer which prevents a page from moving. */
2051 lispobj
* object_start
= search_dynamic_space(addr
);
2052 if (!object_start
) return 0;
2054 /* If the containing object is a code object, presume that the
2055 * pointer is valid, simply because it could be an unboxed return
2057 * FIXME: only if the addr points to a simple-fun instruction area
2058 * should we skip the stronger tests. Otherwise, require a properly
2059 * tagged pointer to the code component or an embedded simple-fun
2060 * (or LRA?) just as with any other kind of object. */
2061 if (widetag_of(*object_start
) == CODE_HEADER_WIDETAG
)
2062 return object_start
;
2064 /* Large object pages only contain ONE object, and it will never
2065 * be a CONS. However, arrays and bignums can be allocated larger
2066 * than necessary and then shrunk to fit, leaving what look like
2067 * (0 . 0) CONSes at the end. These appear valid to
2068 * properly_tagged_descriptor_p(), so pick them off here. */
2069 if (((lowtag_of((lispobj
)addr
) == LIST_POINTER_LOWTAG
) &&
2070 page_table
[addr_page_index
].large_object
)
2071 || !properly_tagged_descriptor_p((lispobj
)addr
, object_start
))
2074 return object_start
;
2079 in_dontmove_nativeptr_p(page_index_t page_index
, lispobj
*native_ptr
)
2081 in_use_marker_t
*markers
= pinned_dwords(page_index
);
2083 lispobj
*begin
= page_address(page_index
);
2084 int dword_in_page
= (native_ptr
- begin
) / 2;
2085 return (markers
[dword_in_page
/ N_WORD_BITS
] >> (dword_in_page
% N_WORD_BITS
)) & 1;
2091 /* Adjust large bignum and vector objects. This will adjust the
2092 * allocated region if the size has shrunk, and move unboxed objects
2093 * into unboxed pages. The pages are not promoted here, and the
2094 * promoted region is not added to the new_regions; this is really
2095 * only designed to be called from preserve_pointer(). Shouldn't fail
2096 * if this is missed, just may delay the moving of objects to unboxed
2097 * pages, and the freeing of pages. */
2099 maybe_adjust_large_object(lispobj
*where
)
2101 page_index_t first_page
;
2102 page_index_t next_page
;
2105 uword_t remaining_bytes
;
2106 uword_t bytes_freed
;
2107 uword_t old_bytes_used
;
2111 /* Check whether it's a vector or bignum object. */
2112 switch (widetag_of(where
[0])) {
2113 case SIMPLE_VECTOR_WIDETAG
:
2114 boxed
= BOXED_PAGE_FLAG
;
2116 case BIGNUM_WIDETAG
:
2117 case SIMPLE_BASE_STRING_WIDETAG
:
2118 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
2119 case SIMPLE_CHARACTER_STRING_WIDETAG
:
2121 case SIMPLE_BIT_VECTOR_WIDETAG
:
2122 case SIMPLE_ARRAY_NIL_WIDETAG
:
2123 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG
:
2124 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG
:
2125 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG
:
2126 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG
:
2127 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG
:
2128 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG
:
2130 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG
:
2132 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG
:
2133 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
:
2134 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
2135 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
:
2137 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
2138 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
:
2140 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2141 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
:
2143 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2144 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
:
2147 case SIMPLE_ARRAY_FIXNUM_WIDETAG
:
2149 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2150 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
:
2152 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
2153 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
:
2155 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG
:
2156 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG
:
2157 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2158 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
:
2160 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2161 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
:
2163 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2164 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
:
2166 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2167 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
:
2169 boxed
= UNBOXED_PAGE_FLAG
;
2175 /* Find its current size. */
2176 nwords
= (sizetab
[widetag_of(where
[0])])(where
);
2178 first_page
= find_page_index((void *)where
);
2179 gc_assert(first_page
>= 0);
2181 /* Note: Any page write-protection must be removed, else a later
2182 * scavenge_newspace may incorrectly not scavenge these pages.
2183 * This would not be necessary if they are added to the new areas,
2184 * but lets do it for them all (they'll probably be written
2187 gc_assert(page_starts_contiguous_block_p(first_page
));
2189 next_page
= first_page
;
2190 remaining_bytes
= nwords
*N_WORD_BYTES
;
2191 while (remaining_bytes
> GENCGC_CARD_BYTES
) {
2192 gc_assert(page_table
[next_page
].gen
== from_space
);
2193 gc_assert(page_allocated_no_region_p(next_page
));
2194 gc_assert(page_table
[next_page
].large_object
);
2195 gc_assert(page_table
[next_page
].scan_start_offset
==
2196 npage_bytes(next_page
-first_page
));
2197 gc_assert(page_table
[next_page
].bytes_used
== GENCGC_CARD_BYTES
);
2199 page_table
[next_page
].allocated
= boxed
;
2201 /* Shouldn't be write-protected at this stage. Essential that the
2203 gc_assert(!page_table
[next_page
].write_protected
);
2204 remaining_bytes
-= GENCGC_CARD_BYTES
;
2208 /* Now only one page remains, but the object may have shrunk so
2209 * there may be more unused pages which will be freed. */
2211 /* Object may have shrunk but shouldn't have grown - check. */
2212 gc_assert(page_table
[next_page
].bytes_used
>= remaining_bytes
);
2214 page_table
[next_page
].allocated
= boxed
;
2215 gc_assert(page_table
[next_page
].allocated
==
2216 page_table
[first_page
].allocated
);
2218 /* Adjust the bytes_used. */
2219 old_bytes_used
= page_table
[next_page
].bytes_used
;
2220 page_table
[next_page
].bytes_used
= remaining_bytes
;
2222 bytes_freed
= old_bytes_used
- remaining_bytes
;
2224 /* Free any remaining pages; needs care. */
2226 while ((old_bytes_used
== GENCGC_CARD_BYTES
) &&
2227 (page_table
[next_page
].gen
== from_space
) &&
2228 page_allocated_no_region_p(next_page
) &&
2229 page_table
[next_page
].large_object
&&
2230 (page_table
[next_page
].scan_start_offset
==
2231 npage_bytes(next_page
- first_page
))) {
2232 /* It checks out OK, free the page. We don't need to both zeroing
2233 * pages as this should have been done before shrinking the
2234 * object. These pages shouldn't be write protected as they
2235 * should be zero filled. */
2236 gc_assert(page_table
[next_page
].write_protected
== 0);
2238 old_bytes_used
= page_table
[next_page
].bytes_used
;
2239 page_table
[next_page
].allocated
= FREE_PAGE_FLAG
;
2240 page_table
[next_page
].bytes_used
= 0;
2241 bytes_freed
+= old_bytes_used
;
2245 if ((bytes_freed
> 0) && gencgc_verbose
) {
2247 "/maybe_adjust_large_object() freed %d\n",
2251 generations
[from_space
].bytes_allocated
-= bytes_freed
;
2252 bytes_allocated
-= bytes_freed
;
2258 * Why is this restricted to protected objects only?
2259 * Because the rest of the page has been scavenged already,
2260 * and since that leaves forwarding pointers in the unprotected
2261 * areas you cannot scavenge it again until those are gone.
2264 scavenge_pinned_range(void* page_base
, int start
, int count
)
2266 // 'start' and 'count' are expressed in units of dwords
2267 scavenge((lispobj
*)page_base
+ 2*start
, 2*count
);
2271 scavenge_pinned_ranges()
2274 for (page
= 0; page
< last_free_page
; page
++) {
2275 in_use_marker_t
* bitmap
= pinned_dwords(page
);
2278 GENCGC_CARD_BYTES
/ (2*N_WORD_BYTES
) / N_WORD_BITS
,
2279 0, scavenge_pinned_range
, page_address(page
));
2283 static void wipe_range(void* page_base
, int start
, int count
)
2285 bzero((lispobj
*)page_base
+ 2*start
, count
*2*N_WORD_BYTES
);
2289 wipe_nonpinned_words()
2292 in_use_marker_t
* bitmap
;
2294 for (i
= 0; i
< last_free_page
; i
++) {
2295 if (page_table
[i
].dont_move
&& (bitmap
= pinned_dwords(i
)) != 0) {
2297 GENCGC_CARD_BYTES
/ (2*N_WORD_BYTES
) / N_WORD_BITS
,
2298 BIT_SCAN_INVERT
| BIT_SCAN_CLEAR
,
2299 wipe_range
, page_address(i
));
2300 page_table
[i
].has_pin_map
= 0;
2301 // move the page to newspace
2302 generations
[new_space
].bytes_allocated
+= page_table
[i
].bytes_used
;
2303 generations
[page_table
[i
].gen
].bytes_allocated
-= page_table
[i
].bytes_used
;
2304 page_table
[i
].gen
= new_space
;
2307 #ifndef LISP_FEATURE_WIN32
2308 madvise(page_table_pinned_dwords
, pins_map_size_in_bytes
, MADV_DONTNEED
);
2313 pin_words(page_index_t pageindex
, lispobj
*mark_which_pointer
)
2315 struct page
*page
= &page_table
[pageindex
];
2316 gc_assert(mark_which_pointer
);
2317 if (!page
->has_pin_map
) {
2318 page
->has_pin_map
= 1;
2322 in_use_marker_t
* map
= pinned_dwords(pageindex
);
2323 for (i
=0; i
<n_dwords_in_card
/N_WORD_BITS
; ++i
)
2324 gc_assert(map
[i
] == 0);
2328 lispobj
*page_base
= page_address(pageindex
);
2329 unsigned int begin_dword_index
= (mark_which_pointer
- page_base
) / 2;
2330 in_use_marker_t
*bitmap
= pinned_dwords(pageindex
);
2331 if (bitmap
[begin_dword_index
/N_WORD_BITS
]
2332 & ((uword_t
)1 << (begin_dword_index
% N_WORD_BITS
)))
2333 return; // already seen this object
2335 lispobj header
= *mark_which_pointer
;
2337 // Don't bother calling a sizing function for fixnums or pointers.
2338 // The object pointed to must be a cons.
2339 if (!fixnump(header
) && !is_lisp_pointer(header
)) {
2340 size
= (sizetab
[widetag_of(header
)])(mark_which_pointer
);
2341 if (size
== 1 && (lowtag_of(header
) == 9 || lowtag_of(header
) == 2))
2344 gc_assert(size
% 2 == 0);
2345 unsigned int end_dword_index
= begin_dword_index
+ size
/ 2;
2347 for (index
= begin_dword_index
; index
< end_dword_index
; index
++)
2348 bitmap
[index
/N_WORD_BITS
] |= (uword_t
)1 << (index
% N_WORD_BITS
);
2351 /* Take a possible pointer to a Lisp object and mark its page in the
2352 * page_table so that it will not be relocated during a GC.
2354 * This involves locating the page it points to, then backing up to
2355 * the start of its region, then marking all pages dont_move from there
2356 * up to the first page that's not full or has a different generation
2358 * It is assumed that all the page static flags have been cleared at
2359 * the start of a GC.
2361 * It is also assumed that the current gc_alloc() region has been
2362 * flushed and the tables updated. */
2364 // TODO: there's probably a way to be a little more efficient here.
2365 // As things are, we start by finding the object that encloses 'addr',
2366 // then we see if 'addr' was a "valid" Lisp pointer to that object
2367 // - meaning we expect the correct lowtag on the pointer - except
2368 // that for code objects we don't require a correct lowtag
2369 // and we allow a pointer to anywhere in the object.
2371 // It should be possible to avoid calling search_dynamic_space
2372 // more of the time. First, check if the page pointed to might hold code.
2373 // If it does, then we continue regardless of the pointer's lowtag
2374 // (because of the special allowance). If the page definitely does *not*
2375 // hold code, then we require up front that the lowtake make sense,
2376 // by doing the same checks that are in properly_tagged_descriptor_p.
2378 // Problem: when code is allocated from a per-thread region,
2379 // does it ensure that the occupied pages are flagged as having code?
2382 preserve_pointer(void *addr
)
2384 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2385 /* Immobile space MUST be lower than dynamic space,
2386 or else this test needs to be revised */
2387 if (addr
< (void*)IMMOBILE_SPACE_END
) {
2388 extern void immobile_space_preserve_pointer(void*);
2389 immobile_space_preserve_pointer(addr
);
2393 page_index_t addr_page_index
= find_page_index(addr
);
2394 lispobj
*object_start
;
2396 if (addr_page_index
== -1
2397 || (object_start
= conservative_root_p(addr
, addr_page_index
)) == 0)
2400 /* (Now that we know that addr_page_index is in range, it's
2401 * safe to index into page_table[] with it.) */
2402 unsigned int region_allocation
= page_table
[addr_page_index
].allocated
;
2404 /* Find the beginning of the region. Note that there may be
2405 * objects in the region preceding the one that we were passed a
2406 * pointer to: if this is the case, we will write-protect all the
2407 * previous objects' pages too. */
2410 /* I think this'd work just as well, but without the assertions.
2411 * -dan 2004.01.01 */
2412 page_index_t first_page
= find_page_index(page_scan_start(addr_page_index
))
2414 page_index_t first_page
= addr_page_index
;
2415 while (!page_starts_contiguous_block_p(first_page
)) {
2417 /* Do some checks. */
2418 gc_assert(page_table
[first_page
].bytes_used
== GENCGC_CARD_BYTES
);
2419 gc_assert(page_table
[first_page
].gen
== from_space
);
2420 gc_assert(page_table
[first_page
].allocated
== region_allocation
);
2424 /* Adjust any large objects before promotion as they won't be
2425 * copied after promotion. */
2426 if (page_table
[first_page
].large_object
) {
2427 maybe_adjust_large_object(page_address(first_page
));
2428 /* It may have moved to unboxed pages. */
2429 region_allocation
= page_table
[first_page
].allocated
;
2432 /* Now work forward until the end of this contiguous area is found,
2433 * marking all pages as dont_move. */
2435 for (i
= first_page
; ;i
++) {
2436 gc_assert(page_table
[i
].allocated
== region_allocation
);
2438 /* Mark the page static. */
2439 page_table
[i
].dont_move
= 1;
2441 /* It is essential that the pages are not write protected as
2442 * they may have pointers into the old-space which need
2443 * scavenging. They shouldn't be write protected at this
2445 gc_assert(!page_table
[i
].write_protected
);
2447 /* Check whether this is the last page in this contiguous block.. */
2448 if (page_ends_contiguous_block_p(i
, from_space
))
2452 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
2453 /* Do not do this for multi-page objects. Those pages do not need
2454 * object wipeout anyway.
2456 if (do_wipe_p
&& i
== first_page
) // single-page object
2457 pin_words(first_page
, object_start
);
2460 /* Check that the page is now static. */
2461 gc_assert(page_table
[addr_page_index
].dont_move
!= 0);
2464 /* If the given page is not write-protected, then scan it for pointers
2465 * to younger generations or the top temp. generation, if no
2466 * suspicious pointers are found then the page is write-protected.
2468 * Care is taken to check for pointers to the current gc_alloc()
2469 * region if it is a younger generation or the temp. generation. This
2470 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2471 * the gc_alloc_generation does not need to be checked as this is only
2472 * called from scavenge_generation() when the gc_alloc generation is
2473 * younger, so it just checks if there is a pointer to the current
2476 * We return 1 if the page was write-protected, else 0. */
2478 update_page_write_prot(page_index_t page
)
2480 generation_index_t gen
= page_table
[page
].gen
;
2483 void **page_addr
= (void **)page_address(page
);
2484 sword_t num_words
= page_table
[page
].bytes_used
/ N_WORD_BYTES
;
2486 /* Shouldn't be a free page. */
2487 gc_assert(page_allocated_p(page
));
2488 gc_assert(page_table
[page
].bytes_used
!= 0);
2490 /* Skip if it's already write-protected, pinned, or unboxed */
2491 if (page_table
[page
].write_protected
2492 /* FIXME: What's the reason for not write-protecting pinned pages? */
2493 || page_table
[page
].dont_move
2494 || page_unboxed_p(page
))
2497 /* Scan the page for pointers to younger generations or the
2498 * top temp. generation. */
2500 /* This is conservative: any word satisfying is_lisp_pointer() is
2501 * assumed to be a pointer. To do otherwise would require a family
2502 * of scavenge-like functions. */
2503 for (j
= 0; j
< num_words
; j
++) {
2504 void *ptr
= *(page_addr
+j
);
2506 lispobj
__attribute__((unused
)) header
;
2508 if (!is_lisp_pointer((lispobj
)ptr
))
2510 /* Check that it's in the dynamic space */
2511 if ((index
= find_page_index(ptr
)) != -1) {
2512 if (/* Does it point to a younger or the temp. generation? */
2513 (page_allocated_p(index
)
2514 && (page_table
[index
].bytes_used
!= 0)
2515 && ((page_table
[index
].gen
< gen
)
2516 || (page_table
[index
].gen
== SCRATCH_GENERATION
)))
2518 /* Or does it point within a current gc_alloc() region? */
2519 || ((boxed_region
.start_addr
<= ptr
)
2520 && (ptr
<= boxed_region
.free_pointer
))
2521 || ((unboxed_region
.start_addr
<= ptr
)
2522 && (ptr
<= unboxed_region
.free_pointer
))) {
2527 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2528 else if ((index
= find_immobile_page_index(ptr
)) >= 0 &&
2529 other_immediate_lowtag_p(header
= *native_pointer((lispobj
)ptr
))) {
2530 // This is *possibly* a pointer to an object in immobile space,
2531 // given that above two conditions were satisfied.
2532 // But unlike in the dynamic space case, we need to read a byte
2533 // from the object to determine its generation, which requires care.
2534 // Consider an unboxed word that looks like a pointer to a word that
2535 // looks like fun-header-widetag. We can't naively back up to the
2536 // underlying code object since the alleged header might not be one.
2537 int obj_gen
= gen
; // Make comparison fail if we fall through
2538 if (lowtag_of((lispobj
)ptr
) != FUN_POINTER_LOWTAG
) {
2539 obj_gen
= __immobile_obj_generation(native_pointer((lispobj
)ptr
));
2540 } else if (widetag_of(header
) == SIMPLE_FUN_HEADER_WIDETAG
) {
2542 code_obj_from_simple_fun((struct simple_fun
*)
2543 ((lispobj
)ptr
- FUN_POINTER_LOWTAG
));
2544 // This is a heuristic, since we're not actually looking for
2545 // an object boundary. Precise scanning of 'page' would obviate
2546 // the guard conditions here.
2547 if ((lispobj
)code
>= IMMOBILE_VARYOBJ_SUBSPACE_START
2548 && widetag_of(code
->header
) == CODE_HEADER_WIDETAG
)
2549 obj_gen
= __immobile_obj_generation((lispobj
*)code
);
2551 // A bogus generation number implies a not-really-pointer,
2552 // but it won't cause misbehavior.
2553 if (obj_gen
< gen
|| obj_gen
== SCRATCH_GENERATION
) {
2562 /* Write-protect the page. */
2563 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2565 os_protect((void *)page_addr
,
2567 OS_VM_PROT_READ
|OS_VM_PROT_EXECUTE
);
2569 /* Note the page as protected in the page tables. */
2570 page_table
[page
].write_protected
= 1;
2576 /* Scavenge all generations from FROM to TO, inclusive, except for
2577 * new_space which needs special handling, as new objects may be
2578 * added which are not checked here - use scavenge_newspace generation.
2580 * Write-protected pages should not have any pointers to the
2581 * from_space so do need scavenging; thus write-protected pages are
2582 * not always scavenged. There is some code to check that these pages
2583 * are not written; but to check fully the write-protected pages need
2584 * to be scavenged by disabling the code to skip them.
2586 * Under the current scheme when a generation is GCed the younger
2587 * generations will be empty. So, when a generation is being GCed it
2588 * is only necessary to scavenge the older generations for pointers
2589 * not the younger. So a page that does not have pointers to younger
2590 * generations does not need to be scavenged.
2592 * The write-protection can be used to note pages that don't have
2593 * pointers to younger pages. But pages can be written without having
2594 * pointers to younger generations. After the pages are scavenged here
2595 * they can be scanned for pointers to younger generations and if
2596 * there are none the page can be write-protected.
2598 * One complication is when the newspace is the top temp. generation.
2600 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2601 * that none were written, which they shouldn't be as they should have
2602 * no pointers to younger generations. This breaks down for weak
2603 * pointers as the objects contain a link to the next and are written
2604 * if a weak pointer is scavenged. Still it's a useful check. */
2606 scavenge_generations(generation_index_t from
, generation_index_t to
)
2609 page_index_t num_wp
= 0;
2613 /* Clear the write_protected_cleared flags on all pages. */
2614 for (i
= 0; i
< page_table_pages
; i
++)
2615 page_table
[i
].write_protected_cleared
= 0;
2618 for (i
= 0; i
< last_free_page
; i
++) {
2619 generation_index_t generation
= page_table
[i
].gen
;
2621 && (page_table
[i
].bytes_used
!= 0)
2622 && (generation
!= new_space
)
2623 && (generation
>= from
)
2624 && (generation
<= to
)) {
2625 page_index_t last_page
,j
;
2626 int write_protected
=1;
2628 /* This should be the start of a region */
2629 gc_assert(page_starts_contiguous_block_p(i
));
2631 /* Now work forward until the end of the region */
2632 for (last_page
= i
; ; last_page
++) {
2634 write_protected
&& page_table
[last_page
].write_protected
;
2635 if (page_ends_contiguous_block_p(last_page
, generation
))
2638 if (!write_protected
) {
2639 scavenge(page_address(i
),
2640 ((uword_t
)(page_table
[last_page
].bytes_used
2641 + npage_bytes(last_page
-i
)))
2644 /* Now scan the pages and write protect those that
2645 * don't have pointers to younger generations. */
2646 if (enable_page_protection
) {
2647 for (j
= i
; j
<= last_page
; j
++) {
2648 num_wp
+= update_page_write_prot(j
);
2651 if ((gencgc_verbose
> 1) && (num_wp
!= 0)) {
2653 "/write protected %d pages within generation %d\n",
2654 num_wp
, generation
));
2662 /* Check that none of the write_protected pages in this generation
2663 * have been written to. */
2664 for (i
= 0; i
< page_table_pages
; i
++) {
2665 if (page_allocated_p(i
)
2666 && (page_table
[i
].bytes_used
!= 0)
2667 && (page_table
[i
].gen
== generation
)
2668 && (page_table
[i
].write_protected_cleared
!= 0)) {
2669 FSHOW((stderr
, "/scavenge_generation() %d\n", generation
));
2671 "/page bytes_used=%d scan_start_offset=%lu dont_move=%d\n",
2672 page_table
[i
].bytes_used
,
2673 page_table
[i
].scan_start_offset
,
2674 page_table
[i
].dont_move
));
2675 lose("write to protected page %d in scavenge_generation()\n", i
);
2682 /* Scavenge a newspace generation. As it is scavenged new objects may
2683 * be allocated to it; these will also need to be scavenged. This
2684 * repeats until there are no more objects unscavenged in the
2685 * newspace generation.
2687 * To help improve the efficiency, areas written are recorded by
2688 * gc_alloc() and only these scavenged. Sometimes a little more will be
2689 * scavenged, but this causes no harm. An easy check is done that the
2690 * scavenged bytes equals the number allocated in the previous
2693 * Write-protected pages are not scanned except if they are marked
2694 * dont_move in which case they may have been promoted and still have
2695 * pointers to the from space.
2697 * Write-protected pages could potentially be written by alloc however
2698 * to avoid having to handle re-scavenging of write-protected pages
2699 * gc_alloc() does not write to write-protected pages.
2701 * New areas of objects allocated are recorded alternatively in the two
2702 * new_areas arrays below. */
2703 static struct new_area new_areas_1
[NUM_NEW_AREAS
];
2704 static struct new_area new_areas_2
[NUM_NEW_AREAS
];
2706 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2707 extern unsigned int immobile_scav_queue_count
;
2710 update_immobile_nursery_bits(),
2711 scavenge_immobile_roots(generation_index_t
,generation_index_t
),
2712 scavenge_immobile_newspace(),
2713 sweep_immobile_space(int raise
),
2714 write_protect_immobile_space();
2716 #define immobile_scav_queue_count 0
2719 /* Do one full scan of the new space generation. This is not enough to
2720 * complete the job as new objects may be added to the generation in
2721 * the process which are not scavenged. */
2723 scavenge_newspace_generation_one_scan(generation_index_t generation
)
2728 "/starting one full scan of newspace generation %d\n",
2730 for (i
= 0; i
< last_free_page
; i
++) {
2731 /* Note that this skips over open regions when it encounters them. */
2733 && (page_table
[i
].bytes_used
!= 0)
2734 && (page_table
[i
].gen
== generation
)
2735 && ((page_table
[i
].write_protected
== 0)
2736 /* (This may be redundant as write_protected is now
2737 * cleared before promotion.) */
2738 || (page_table
[i
].dont_move
== 1))) {
2739 page_index_t last_page
;
2742 /* The scavenge will start at the scan_start_offset of
2745 * We need to find the full extent of this contiguous
2746 * block in case objects span pages.
2748 * Now work forward until the end of this contiguous area
2749 * is found. A small area is preferred as there is a
2750 * better chance of its pages being write-protected. */
2751 for (last_page
= i
; ;last_page
++) {
2752 /* If all pages are write-protected and movable,
2753 * then no need to scavenge */
2754 all_wp
=all_wp
&& page_table
[last_page
].write_protected
&&
2755 !page_table
[last_page
].dont_move
;
2757 /* Check whether this is the last page in this
2758 * contiguous block */
2759 if (page_ends_contiguous_block_p(last_page
, generation
))
2763 /* Do a limited check for write-protected pages. */
2765 sword_t nwords
= (((uword_t
)
2766 (page_table
[last_page
].bytes_used
2767 + npage_bytes(last_page
-i
)
2768 + page_table
[i
].scan_start_offset
))
2770 new_areas_ignore_page
= last_page
;
2772 scavenge(page_scan_start(i
), nwords
);
2779 "/done with one full scan of newspace generation %d\n",
2783 /* Do a complete scavenge of the newspace generation. */
2785 scavenge_newspace_generation(generation_index_t generation
)
2789 /* the new_areas array currently being written to by gc_alloc() */
2790 struct new_area (*current_new_areas
)[] = &new_areas_1
;
2791 size_t current_new_areas_index
;
2793 /* the new_areas created by the previous scavenge cycle */
2794 struct new_area (*previous_new_areas
)[] = NULL
;
2795 size_t previous_new_areas_index
;
2797 /* Flush the current regions updating the tables. */
2798 gc_alloc_update_all_page_tables(0);
2800 /* Turn on the recording of new areas by gc_alloc(). */
2801 new_areas
= current_new_areas
;
2802 new_areas_index
= 0;
2804 /* Don't need to record new areas that get scavenged anyway during
2805 * scavenge_newspace_generation_one_scan. */
2806 record_new_objects
= 1;
2808 /* Start with a full scavenge. */
2809 scavenge_newspace_generation_one_scan(generation
);
2811 /* Record all new areas now. */
2812 record_new_objects
= 2;
2814 /* Give a chance to weak hash tables to make other objects live.
2815 * FIXME: The algorithm implemented here for weak hash table gcing
2816 * is O(W^2+N) as Bruno Haible warns in
2817 * http://www.haible.de/bruno/papers/cs/weak/WeakDatastructures-writeup.html
2818 * see "Implementation 2". */
2819 scav_weak_hash_tables();
2821 /* Flush the current regions updating the tables. */
2822 gc_alloc_update_all_page_tables(0);
2824 /* Grab new_areas_index. */
2825 current_new_areas_index
= new_areas_index
;
2828 "The first scan is finished; current_new_areas_index=%d.\n",
2829 current_new_areas_index));*/
2831 while (current_new_areas_index
> 0 || immobile_scav_queue_count
) {
2832 /* Move the current to the previous new areas */
2833 previous_new_areas
= current_new_areas
;
2834 previous_new_areas_index
= current_new_areas_index
;
2836 /* Scavenge all the areas in previous new areas. Any new areas
2837 * allocated are saved in current_new_areas. */
2839 /* Allocate an array for current_new_areas; alternating between
2840 * new_areas_1 and 2 */
2841 if (previous_new_areas
== &new_areas_1
)
2842 current_new_areas
= &new_areas_2
;
2844 current_new_areas
= &new_areas_1
;
2846 /* Set up for gc_alloc(). */
2847 new_areas
= current_new_areas
;
2848 new_areas_index
= 0;
2850 #ifdef LISP_FEATURE_IMMOBILE_SPACE
2851 scavenge_immobile_newspace();
2853 /* Check whether previous_new_areas had overflowed. */
2854 if (previous_new_areas_index
>= NUM_NEW_AREAS
) {
2856 /* New areas of objects allocated have been lost so need to do a
2857 * full scan to be sure! If this becomes a problem try
2858 * increasing NUM_NEW_AREAS. */
2859 if (gencgc_verbose
) {
2860 SHOW("new_areas overflow, doing full scavenge");
2863 /* Don't need to record new areas that get scavenged
2864 * anyway during scavenge_newspace_generation_one_scan. */
2865 record_new_objects
= 1;
2867 scavenge_newspace_generation_one_scan(generation
);
2869 /* Record all new areas now. */
2870 record_new_objects
= 2;
2872 scav_weak_hash_tables();
2874 /* Flush the current regions updating the tables. */
2875 gc_alloc_update_all_page_tables(0);
2879 /* Work through previous_new_areas. */
2880 for (i
= 0; i
< previous_new_areas_index
; i
++) {
2881 page_index_t page
= (*previous_new_areas
)[i
].page
;
2882 size_t offset
= (*previous_new_areas
)[i
].offset
;
2883 size_t size
= (*previous_new_areas
)[i
].size
/ N_WORD_BYTES
;
2884 gc_assert((*previous_new_areas
)[i
].size
% N_WORD_BYTES
== 0);
2885 scavenge(page_address(page
)+offset
, size
);
2888 scav_weak_hash_tables();
2890 /* Flush the current regions updating the tables. */
2891 gc_alloc_update_all_page_tables(0);
2894 current_new_areas_index
= new_areas_index
;
2897 "The re-scan has finished; current_new_areas_index=%d.\n",
2898 current_new_areas_index));*/
2901 /* Turn off recording of areas allocated by gc_alloc(). */
2902 record_new_objects
= 0;
2907 /* Check that none of the write_protected pages in this generation
2908 * have been written to. */
2909 for (i
= 0; i
< page_table_pages
; i
++) {
2910 if (page_allocated_p(i
)
2911 && (page_table
[i
].bytes_used
!= 0)
2912 && (page_table
[i
].gen
== generation
)
2913 && (page_table
[i
].write_protected_cleared
!= 0)
2914 && (page_table
[i
].dont_move
== 0)) {
2915 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d\n",
2916 i
, generation
, page_table
[i
].dont_move
);
2923 /* Un-write-protect all the pages in from_space. This is done at the
2924 * start of a GC else there may be many page faults while scavenging
2925 * the newspace (I've seen drive the system time to 99%). These pages
2926 * would need to be unprotected anyway before unmapping in
2927 * free_oldspace; not sure what effect this has on paging.. */
2929 unprotect_oldspace(void)
2932 void *region_addr
= 0;
2933 void *page_addr
= 0;
2934 uword_t region_bytes
= 0;
2936 for (i
= 0; i
< last_free_page
; i
++) {
2937 if (page_allocated_p(i
)
2938 && (page_table
[i
].bytes_used
!= 0)
2939 && (page_table
[i
].gen
== from_space
)) {
2941 /* Remove any write-protection. We should be able to rely
2942 * on the write-protect flag to avoid redundant calls. */
2943 if (page_table
[i
].write_protected
) {
2944 page_table
[i
].write_protected
= 0;
2945 page_addr
= page_address(i
);
2948 region_addr
= page_addr
;
2949 region_bytes
= GENCGC_CARD_BYTES
;
2950 } else if (region_addr
+ region_bytes
== page_addr
) {
2951 /* Region continue. */
2952 region_bytes
+= GENCGC_CARD_BYTES
;
2954 /* Unprotect previous region. */
2955 os_protect(region_addr
, region_bytes
, OS_VM_PROT_ALL
);
2956 /* First page in new region. */
2957 region_addr
= page_addr
;
2958 region_bytes
= GENCGC_CARD_BYTES
;
2964 /* Unprotect last region. */
2965 os_protect(region_addr
, region_bytes
, OS_VM_PROT_ALL
);
2969 /* Work through all the pages and free any in from_space. This
2970 * assumes that all objects have been copied or promoted to an older
2971 * generation. Bytes_allocated and the generation bytes_allocated
2972 * counter are updated. The number of bytes freed is returned. */
2976 uword_t bytes_freed
= 0;
2977 page_index_t first_page
, last_page
;
2982 /* Find a first page for the next region of pages. */
2983 while ((first_page
< last_free_page
)
2984 && (page_free_p(first_page
)
2985 || (page_table
[first_page
].bytes_used
== 0)
2986 || (page_table
[first_page
].gen
!= from_space
)))
2989 if (first_page
>= last_free_page
)
2992 /* Find the last page of this region. */
2993 last_page
= first_page
;
2996 /* Free the page. */
2997 bytes_freed
+= page_table
[last_page
].bytes_used
;
2998 generations
[page_table
[last_page
].gen
].bytes_allocated
-=
2999 page_table
[last_page
].bytes_used
;
3000 page_table
[last_page
].allocated
= FREE_PAGE_FLAG
;
3001 page_table
[last_page
].bytes_used
= 0;
3002 /* Should already be unprotected by unprotect_oldspace(). */
3003 gc_assert(!page_table
[last_page
].write_protected
);
3006 while ((last_page
< last_free_page
)
3007 && page_allocated_p(last_page
)
3008 && (page_table
[last_page
].bytes_used
!= 0)
3009 && (page_table
[last_page
].gen
== from_space
));
3011 #ifdef READ_PROTECT_FREE_PAGES
3012 os_protect(page_address(first_page
),
3013 npage_bytes(last_page
-first_page
),
3016 first_page
= last_page
;
3017 } while (first_page
< last_free_page
);
3019 bytes_allocated
-= bytes_freed
;
3024 /* Print some information about a pointer at the given address. */
3026 print_ptr(lispobj
*addr
)
3028 /* If addr is in the dynamic space then out the page information. */
3029 page_index_t pi1
= find_page_index((void*)addr
);
3032 fprintf(stderr
," %p: page %d alloc %d gen %d bytes_used %d offset %lu dont_move %d\n",
3035 page_table
[pi1
].allocated
,
3036 page_table
[pi1
].gen
,
3037 page_table
[pi1
].bytes_used
,
3038 page_table
[pi1
].scan_start_offset
,
3039 page_table
[pi1
].dont_move
);
3040 fprintf(stderr
," %x %x %x %x (%x) %x %x %x %x\n",
3054 is_in_stack_space(lispobj ptr
)
3056 /* For space verification: Pointers can be valid if they point
3057 * to a thread stack space. This would be faster if the thread
3058 * structures had page-table entries as if they were part of
3059 * the heap space. */
3061 for_each_thread(th
) {
3062 if ((th
->control_stack_start
<= (lispobj
*)ptr
) &&
3063 (th
->control_stack_end
>= (lispobj
*)ptr
)) {
3070 // NOTE: This function can produces false failure indications,
3071 // usually related to dynamic space pointing to the stack of a
3072 // dead thread, but there may be other reasons as well.
3074 verify_space(lispobj
*start
, size_t words
)
3076 extern int valid_lisp_pointer_p(lispobj
);
3077 int is_in_dynamic_space
= (find_page_index((void*)start
) != -1);
3078 int is_in_readonly_space
=
3079 (READ_ONLY_SPACE_START
<= (uword_t
)start
&&
3080 (uword_t
)start
< SymbolValue(READ_ONLY_SPACE_FREE_POINTER
,0));
3081 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3082 int is_in_immobile_space
=
3083 (IMMOBILE_SPACE_START
<= (uword_t
)start
&&
3084 (uword_t
)start
< SymbolValue(IMMOBILE_SPACE_FREE_POINTER
,0));
3089 lispobj thing
= *start
;
3091 if (is_lisp_pointer(thing
)) {
3092 page_index_t page_index
= find_page_index((void*)thing
);
3093 sword_t to_readonly_space
=
3094 (READ_ONLY_SPACE_START
<= thing
&&
3095 thing
< SymbolValue(READ_ONLY_SPACE_FREE_POINTER
,0));
3096 sword_t to_static_space
=
3097 (STATIC_SPACE_START
<= thing
&&
3098 thing
< SymbolValue(STATIC_SPACE_FREE_POINTER
,0));
3099 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3100 sword_t to_immobile_space
=
3101 (IMMOBILE_SPACE_START
<= thing
&&
3102 thing
< SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER
,0)) ||
3103 (IMMOBILE_VARYOBJ_SUBSPACE_START
<= thing
&&
3104 thing
< SymbolValue(IMMOBILE_SPACE_FREE_POINTER
,0));
3107 /* Does it point to the dynamic space? */
3108 if (page_index
!= -1) {
3109 /* If it's within the dynamic space it should point to a used page. */
3110 if (!page_allocated_p(page_index
))
3111 lose ("Ptr %p @ %p sees free page.\n", thing
, start
);
3112 if ((char*)thing
- (char*)page_address(page_index
)
3113 >= page_table
[page_index
].bytes_used
)
3114 lose ("Ptr %p @ %p sees unallocated space.\n", thing
, start
);
3115 /* Check that it doesn't point to a forwarding pointer! */
3116 if (*((lispobj
*)native_pointer(thing
)) == 0x01) {
3117 lose("Ptr %p @ %p sees forwarding ptr.\n", thing
, start
);
3119 /* Check that its not in the RO space as it would then be a
3120 * pointer from the RO to the dynamic space. */
3121 if (is_in_readonly_space
) {
3122 lose("ptr to dynamic space %p from RO space %x\n",
3125 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3126 // verify all immobile space -> dynamic space pointers
3127 if (is_in_immobile_space
&& !valid_lisp_pointer_p(thing
)) {
3128 lose("Ptr %p @ %p sees junk.\n", thing
, start
);
3131 /* Does it point to a plausible object? This check slows
3132 * it down a lot (so it's commented out).
3134 * "a lot" is serious: it ate 50 minutes cpu time on
3135 * my duron 950 before I came back from lunch and
3138 * FIXME: Add a variable to enable this
3141 if (!valid_lisp_pointer_p((lispobj *)thing) {
3142 lose("ptr %p to invalid object %p\n", thing, start);
3145 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3146 } else if (to_immobile_space
) {
3147 // the object pointed to must not have been discarded as garbage
3148 if (!other_immediate_lowtag_p(*native_pointer(thing
))
3149 || immobile_filler_p(native_pointer(thing
)))
3150 lose("Ptr %p @ %p sees trashed object.\n", (void*)thing
, start
);
3151 // verify all pointers to immobile space
3152 if (!valid_lisp_pointer_p(thing
))
3153 lose("Ptr %p @ %p sees junk.\n", thing
, start
);
3156 extern char __attribute__((unused
)) funcallable_instance_tramp
;
3157 /* Verify that it points to another valid space. */
3158 if (!to_readonly_space
&& !to_static_space
3159 && !is_in_stack_space(thing
)) {
3160 lose("Ptr %p @ %p sees junk.\n", thing
, start
);
3164 if (!(fixnump(thing
))) {
3166 switch(widetag_of(*start
)) {
3169 case SIMPLE_VECTOR_WIDETAG
:
3171 case COMPLEX_WIDETAG
:
3172 case SIMPLE_ARRAY_WIDETAG
:
3173 case COMPLEX_BASE_STRING_WIDETAG
:
3174 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
3175 case COMPLEX_CHARACTER_STRING_WIDETAG
:
3177 case COMPLEX_VECTOR_NIL_WIDETAG
:
3178 case COMPLEX_BIT_VECTOR_WIDETAG
:
3179 case COMPLEX_VECTOR_WIDETAG
:
3180 case COMPLEX_ARRAY_WIDETAG
:
3181 case CLOSURE_HEADER_WIDETAG
:
3182 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG
:
3183 case VALUE_CELL_HEADER_WIDETAG
:
3184 case SYMBOL_HEADER_WIDETAG
:
3185 case CHARACTER_WIDETAG
:
3186 #if N_WORD_BITS == 64
3187 case SINGLE_FLOAT_WIDETAG
:
3189 case UNBOUND_MARKER_WIDETAG
:
3194 case INSTANCE_HEADER_WIDETAG
:
3196 sword_t ntotal
= instance_length(thing
);
3197 lispobj layout
= instance_layout(start
);
3202 instance_scan_interleaved(verify_space
,
3204 native_pointer(layout
));
3208 case CODE_HEADER_WIDETAG
:
3210 /* Check that it's not in the dynamic space.
3211 * FIXME: Isn't is supposed to be OK for code
3212 * objects to be in the dynamic space these days? */
3213 /* It is for byte compiled code, but there's
3214 * no byte compilation in SBCL anymore. */
3215 if (is_in_dynamic_space
3216 /* Only when enabled */
3217 && verify_dynamic_code_check
) {
3219 "/code object at %p in the dynamic space\n",
3223 struct code
*code
= (struct code
*) start
;
3224 sword_t nheader_words
= code_header_words(code
->header
);
3225 /* Scavenge the boxed section of the code data block */
3226 verify_space(start
+ 1, nheader_words
- 1);
3228 /* Scavenge the boxed section of each function
3229 * object in the code data block. */
3230 lispobj fheaderl
= code
->entry_points
;
3231 while (fheaderl
!= NIL
) {
3232 struct simple_fun
*fheaderp
=
3233 (struct simple_fun
*) native_pointer(fheaderl
);
3234 gc_assert(widetag_of(fheaderp
->header
) ==
3235 SIMPLE_FUN_HEADER_WIDETAG
);
3236 verify_space(SIMPLE_FUN_SCAV_START(fheaderp
),
3237 SIMPLE_FUN_SCAV_NWORDS(fheaderp
));
3238 fheaderl
= fheaderp
->next
;
3240 count
= nheader_words
+ code_instruction_words(code
->code_size
);
3244 /* unboxed objects */
3245 case BIGNUM_WIDETAG
:
3246 #if N_WORD_BITS != 64
3247 case SINGLE_FLOAT_WIDETAG
:
3249 case DOUBLE_FLOAT_WIDETAG
:
3250 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3251 case LONG_FLOAT_WIDETAG
:
3253 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3254 case COMPLEX_SINGLE_FLOAT_WIDETAG
:
3256 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3257 case COMPLEX_DOUBLE_FLOAT_WIDETAG
:
3259 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3260 case COMPLEX_LONG_FLOAT_WIDETAG
:
3262 #ifdef SIMD_PACK_WIDETAG
3263 case SIMD_PACK_WIDETAG
:
3265 case SIMPLE_BASE_STRING_WIDETAG
:
3266 #ifdef SIMPLE_CHARACTER_STRING_WIDETAG
3267 case SIMPLE_CHARACTER_STRING_WIDETAG
:
3269 case SIMPLE_BIT_VECTOR_WIDETAG
:
3270 case SIMPLE_ARRAY_NIL_WIDETAG
:
3271 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG
:
3272 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG
:
3273 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG
:
3274 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG
:
3275 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG
:
3276 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG
:
3278 case SIMPLE_ARRAY_UNSIGNED_FIXNUM_WIDETAG
:
3280 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG
:
3281 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG
:
3282 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
3283 case SIMPLE_ARRAY_UNSIGNED_BYTE_63_WIDETAG
:
3285 #ifdef SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
3286 case SIMPLE_ARRAY_UNSIGNED_BYTE_64_WIDETAG
:
3288 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3289 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
:
3291 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3292 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
:
3295 case SIMPLE_ARRAY_FIXNUM_WIDETAG
:
3297 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3298 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
:
3300 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
3301 case SIMPLE_ARRAY_SIGNED_BYTE_64_WIDETAG
:
3303 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG
:
3304 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG
:
3305 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3306 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
:
3308 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3309 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
:
3311 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3312 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
:
3314 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3315 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
:
3318 case WEAK_POINTER_WIDETAG
:
3319 #ifdef NO_TLS_VALUE_MARKER_WIDETAG
3320 case NO_TLS_VALUE_MARKER_WIDETAG
:
3322 count
= (sizetab
[widetag_of(*start
)])(start
);
3326 lose("Unhandled widetag %p at %p\n",
3327 widetag_of(*start
), start
);
3336 static void verify_dynamic_space();
3341 /* FIXME: It would be nice to make names consistent so that
3342 * foo_size meant size *in* *bytes* instead of size in some
3343 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3344 * Some counts of lispobjs are called foo_count; it might be good
3345 * to grep for all foo_size and rename the appropriate ones to
3347 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3349 // Try this verification if marknsweep was compiled with extra debugging.
3350 // But weak symbols don't work on macOS.
3351 extern void __attribute__((weak
)) check_varyobj_pages();
3352 if (&check_varyobj_pages
) check_varyobj_pages();
3354 verify_space((lispobj
*)IMMOBILE_SPACE_START
,
3355 (lispobj
*)SymbolValue(IMMOBILE_FIXEDOBJ_FREE_POINTER
,0)
3356 - (lispobj
*)IMMOBILE_SPACE_START
);
3357 verify_space((lispobj
*)IMMOBILE_VARYOBJ_SUBSPACE_START
,
3358 (lispobj
*)SymbolValue(IMMOBILE_SPACE_FREE_POINTER
,0)
3359 - (lispobj
*)IMMOBILE_VARYOBJ_SUBSPACE_START
);
3361 sword_t read_only_space_size
=
3362 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
,0)
3363 - (lispobj
*)READ_ONLY_SPACE_START
;
3364 sword_t static_space_size
=
3365 (lispobj
*)SymbolValue(STATIC_SPACE_FREE_POINTER
,0)
3366 - (lispobj
*)STATIC_SPACE_START
;
3368 for_each_thread(th
) {
3369 sword_t binding_stack_size
=
3370 (lispobj
*)get_binding_stack_pointer(th
)
3371 - (lispobj
*)th
->binding_stack_start
;
3372 verify_space(th
->binding_stack_start
, binding_stack_size
);
3374 verify_space((lispobj
*)READ_ONLY_SPACE_START
, read_only_space_size
);
3375 verify_space((lispobj
*)STATIC_SPACE_START
, static_space_size
);
3376 verify_dynamic_space();
3380 walk_generation(void (*proc
)(lispobj
*,size_t),
3381 generation_index_t generation
)
3384 int genmask
= generation
>= 0 ? 1 << generation
: ~0;
3386 for (i
= 0; i
< last_free_page
; i
++) {
3387 if (page_allocated_p(i
)
3388 && (page_table
[i
].bytes_used
!= 0)
3389 && ((1 << page_table
[i
].gen
) & genmask
)) {
3390 page_index_t last_page
;
3392 /* This should be the start of a contiguous block */
3393 gc_assert(page_starts_contiguous_block_p(i
));
3395 /* Need to find the full extent of this contiguous block in case
3396 objects span pages. */
3398 /* Now work forward until the end of this contiguous area is
3400 for (last_page
= i
; ;last_page
++)
3401 /* Check whether this is the last page in this contiguous
3403 if (page_ends_contiguous_block_p(last_page
, page_table
[i
].gen
))
3406 proc(page_address(i
),
3407 ((uword_t
)(page_table
[last_page
].bytes_used
3408 + npage_bytes(last_page
-i
)))
3414 static void verify_generation(generation_index_t generation
)
3416 walk_generation(verify_space
, generation
);
3419 /* Check that all the free space is zero filled. */
3421 verify_zero_fill(void)
3425 for (page
= 0; page
< last_free_page
; page
++) {
3426 if (page_free_p(page
)) {
3427 /* The whole page should be zero filled. */
3428 sword_t
*start_addr
= (sword_t
*)page_address(page
);
3429 sword_t size
= 1024;
3431 for (i
= 0; i
< size
; i
++) {
3432 if (start_addr
[i
] != 0) {
3433 lose("free page not zero at %x\n", start_addr
+ i
);
3437 sword_t free_bytes
= GENCGC_CARD_BYTES
- page_table
[page
].bytes_used
;
3438 if (free_bytes
> 0) {
3439 sword_t
*start_addr
= (sword_t
*)((uword_t
)page_address(page
)
3440 + page_table
[page
].bytes_used
);
3441 sword_t size
= free_bytes
/ N_WORD_BYTES
;
3443 for (i
= 0; i
< size
; i
++) {
3444 if (start_addr
[i
] != 0) {
3445 lose("free region not zero at %x\n", start_addr
+ i
);
3453 /* External entry point for verify_zero_fill */
3455 gencgc_verify_zero_fill(void)
3457 /* Flush the alloc regions updating the tables. */
3458 gc_alloc_update_all_page_tables(1);
3459 SHOW("verifying zero fill");
3464 verify_dynamic_space(void)
3466 verify_generation(-1);
3467 if (gencgc_enable_verify_zero_fill
)
3471 /* Write-protect all the dynamic boxed pages in the given generation. */
3473 write_protect_generation_pages(generation_index_t generation
)
3477 gc_assert(generation
< SCRATCH_GENERATION
);
3479 for (start
= 0; start
< last_free_page
; start
++) {
3480 if (protect_page_p(start
, generation
)) {
3484 /* Note the page as protected in the page tables. */
3485 page_table
[start
].write_protected
= 1;
3487 for (last
= start
+ 1; last
< last_free_page
; last
++) {
3488 if (!protect_page_p(last
, generation
))
3490 page_table
[last
].write_protected
= 1;
3493 page_start
= (void *)page_address(start
);
3495 os_protect(page_start
,
3496 npage_bytes(last
- start
),
3497 OS_VM_PROT_READ
| OS_VM_PROT_EXECUTE
);
3503 if (gencgc_verbose
> 1) {
3505 "/write protected %d of %d pages in generation %d\n",
3506 count_write_protect_generation_pages(generation
),
3507 count_generation_pages(generation
),
3512 #if defined(LISP_FEATURE_SB_THREAD) && (defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64))
3514 preserve_context_registers (os_context_t
*c
)
3517 /* On Darwin the signal context isn't a contiguous block of memory,
3518 * so just preserve_pointering its contents won't be sufficient.
3520 #if defined(LISP_FEATURE_DARWIN)||defined(LISP_FEATURE_WIN32)
3521 #if defined LISP_FEATURE_X86
3522 preserve_pointer((void*)*os_context_register_addr(c
,reg_EAX
));
3523 preserve_pointer((void*)*os_context_register_addr(c
,reg_ECX
));
3524 preserve_pointer((void*)*os_context_register_addr(c
,reg_EDX
));
3525 preserve_pointer((void*)*os_context_register_addr(c
,reg_EBX
));
3526 preserve_pointer((void*)*os_context_register_addr(c
,reg_ESI
));
3527 preserve_pointer((void*)*os_context_register_addr(c
,reg_EDI
));
3528 preserve_pointer((void*)*os_context_pc_addr(c
));
3529 #elif defined LISP_FEATURE_X86_64
3530 preserve_pointer((void*)*os_context_register_addr(c
,reg_RAX
));
3531 preserve_pointer((void*)*os_context_register_addr(c
,reg_RCX
));
3532 preserve_pointer((void*)*os_context_register_addr(c
,reg_RDX
));
3533 preserve_pointer((void*)*os_context_register_addr(c
,reg_RBX
));
3534 preserve_pointer((void*)*os_context_register_addr(c
,reg_RSI
));
3535 preserve_pointer((void*)*os_context_register_addr(c
,reg_RDI
));
3536 preserve_pointer((void*)*os_context_register_addr(c
,reg_R8
));
3537 preserve_pointer((void*)*os_context_register_addr(c
,reg_R9
));
3538 preserve_pointer((void*)*os_context_register_addr(c
,reg_R10
));
3539 preserve_pointer((void*)*os_context_register_addr(c
,reg_R11
));
3540 preserve_pointer((void*)*os_context_register_addr(c
,reg_R12
));
3541 preserve_pointer((void*)*os_context_register_addr(c
,reg_R13
));
3542 preserve_pointer((void*)*os_context_register_addr(c
,reg_R14
));
3543 preserve_pointer((void*)*os_context_register_addr(c
,reg_R15
));
3544 preserve_pointer((void*)*os_context_pc_addr(c
));
3546 #error "preserve_context_registers needs to be tweaked for non-x86 Darwin"
3549 #if !defined(LISP_FEATURE_WIN32)
3550 for(ptr
= ((void **)(c
+1))-1; ptr
>=(void **)c
; ptr
--) {
3551 preserve_pointer(*ptr
);
3558 move_pinned_pages_to_newspace()
3562 /* scavenge() will evacuate all oldspace pages, but no newspace
3563 * pages. Pinned pages are precisely those pages which must not
3564 * be evacuated, so move them to newspace directly. */
3566 for (i
= 0; i
< last_free_page
; i
++) {
3567 if (page_table
[i
].dont_move
&&
3568 /* dont_move is cleared lazily, so validate the space as well. */
3569 page_table
[i
].gen
== from_space
) {
3570 if (pinned_dwords(i
) && do_wipe_p
) {
3571 // do not move to newspace after all, this will be word-wiped
3574 page_table
[i
].gen
= new_space
;
3575 /* And since we're moving the pages wholesale, also adjust
3576 * the generation allocation counters. */
3577 generations
[new_space
].bytes_allocated
+= page_table
[i
].bytes_used
;
3578 generations
[from_space
].bytes_allocated
-= page_table
[i
].bytes_used
;
3583 /* Garbage collect a generation. If raise is 0 then the remains of the
3584 * generation are not raised to the next generation. */
3586 garbage_collect_generation(generation_index_t generation
, int raise
)
3589 uword_t static_space_size
;
3592 gc_assert(generation
<= HIGHEST_NORMAL_GENERATION
);
3594 /* The oldest generation can't be raised. */
3595 gc_assert((generation
!= HIGHEST_NORMAL_GENERATION
) || (raise
== 0));
3597 /* Check if weak hash tables were processed in the previous GC. */
3598 gc_assert(weak_hash_tables
== NULL
);
3600 /* Initialize the weak pointer list. */
3601 weak_pointers
= NULL
;
3603 /* When a generation is not being raised it is transported to a
3604 * temporary generation (NUM_GENERATIONS), and lowered when
3605 * done. Set up this new generation. There should be no pages
3606 * allocated to it yet. */
3608 gc_assert(generations
[SCRATCH_GENERATION
].bytes_allocated
== 0);
3611 /* Set the global src and dest. generations */
3612 from_space
= generation
;
3614 new_space
= generation
+1;
3616 new_space
= SCRATCH_GENERATION
;
3618 /* Change to a new space for allocation, resetting the alloc_start_page */
3619 gc_alloc_generation
= new_space
;
3620 generations
[new_space
].alloc_start_page
= 0;
3621 generations
[new_space
].alloc_unboxed_start_page
= 0;
3622 generations
[new_space
].alloc_large_start_page
= 0;
3623 generations
[new_space
].alloc_large_unboxed_start_page
= 0;
3625 /* Before any pointers are preserved, the dont_move flags on the
3626 * pages need to be cleared. */
3627 for (i
= 0; i
< last_free_page
; i
++)
3628 if(page_table
[i
].gen
==from_space
) {
3629 page_table
[i
].dont_move
= 0;
3630 gc_assert(pinned_dwords(i
) == NULL
);
3633 /* Un-write-protect the old-space pages. This is essential for the
3634 * promoted pages as they may contain pointers into the old-space
3635 * which need to be scavenged. It also helps avoid unnecessary page
3636 * faults as forwarding pointers are written into them. They need to
3637 * be un-protected anyway before unmapping later. */
3638 unprotect_oldspace();
3640 /* Scavenge the stacks' conservative roots. */
3642 /* there are potentially two stacks for each thread: the main
3643 * stack, which may contain Lisp pointers, and the alternate stack.
3644 * We don't ever run Lisp code on the altstack, but it may
3645 * host a sigcontext with lisp objects in it */
3647 /* what we need to do: (1) find the stack pointer for the main
3648 * stack; scavenge it (2) find the interrupt context on the
3649 * alternate stack that might contain lisp values, and scavenge
3652 /* we assume that none of the preceding applies to the thread that
3653 * initiates GC. If you ever call GC from inside an altstack
3654 * handler, you will lose. */
3656 #if defined(LISP_FEATURE_X86) || defined(LISP_FEATURE_X86_64)
3657 /* And if we're saving a core, there's no point in being conservative. */
3658 if (conservative_stack
) {
3659 for_each_thread(th
) {
3661 void **esp
=(void **)-1;
3662 if (th
->state
== STATE_DEAD
)
3664 # if defined(LISP_FEATURE_SB_SAFEPOINT)
3665 /* Conservative collect_garbage is always invoked with a
3666 * foreign C call or an interrupt handler on top of every
3667 * existing thread, so the stored SP in each thread
3668 * structure is valid, no matter which thread we are looking
3669 * at. For threads that were running Lisp code, the pitstop
3670 * and edge functions maintain this value within the
3671 * interrupt or exception handler. */
3672 esp
= os_get_csp(th
);
3673 assert_on_stack(th
, esp
);
3675 /* In addition to pointers on the stack, also preserve the
3676 * return PC, the only value from the context that we need
3677 * in addition to the SP. The return PC gets saved by the
3678 * foreign call wrapper, and removed from the control stack
3679 * into a register. */
3680 preserve_pointer(th
->pc_around_foreign_call
);
3682 /* And on platforms with interrupts: scavenge ctx registers. */
3684 /* Disabled on Windows, because it does not have an explicit
3685 * stack of `interrupt_contexts'. The reported CSP has been
3686 * chosen so that the current context on the stack is
3687 * covered by the stack scan. See also set_csp_from_context(). */
3688 # ifndef LISP_FEATURE_WIN32
3689 if (th
!= arch_os_get_current_thread()) {
3690 long k
= fixnum_value(
3691 SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,th
));
3693 preserve_context_registers(th
->interrupt_contexts
[--k
]);
3696 # elif defined(LISP_FEATURE_SB_THREAD)
3698 if(th
==arch_os_get_current_thread()) {
3699 /* Somebody is going to burn in hell for this, but casting
3700 * it in two steps shuts gcc up about strict aliasing. */
3701 esp
= (void **)((void *)&raise
);
3704 free
=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,th
));
3705 for(i
=free
-1;i
>=0;i
--) {
3706 os_context_t
*c
=th
->interrupt_contexts
[i
];
3707 esp1
= (void **) *os_context_register_addr(c
,reg_SP
);
3708 if (esp1
>=(void **)th
->control_stack_start
&&
3709 esp1
<(void **)th
->control_stack_end
) {
3710 if(esp1
<esp
) esp
=esp1
;
3711 preserve_context_registers(c
);
3716 esp
= (void **)((void *)&raise
);
3718 if (!esp
|| esp
== (void*) -1)
3719 lose("garbage_collect: no SP known for thread %x (OS %x)",
3721 for (ptr
= ((void **)th
->control_stack_end
)-1; ptr
>= esp
; ptr
--) {
3722 preserve_pointer(*ptr
);
3727 /* Non-x86oid systems don't have "conservative roots" as such, but
3728 * the same mechanism is used for objects pinned for use by alien
3730 for_each_thread(th
) {
3731 lispobj pin_list
= SymbolTlValue(PINNED_OBJECTS
,th
);
3732 while (pin_list
!= NIL
) {
3733 struct cons
*list_entry
=
3734 (struct cons
*)native_pointer(pin_list
);
3735 preserve_pointer(list_entry
->car
);
3736 pin_list
= list_entry
->cdr
;
3742 if (gencgc_verbose
> 1) {
3743 sword_t num_dont_move_pages
= count_dont_move_pages();
3745 "/non-movable pages due to conservative pointers = %ld (%lu bytes)\n",
3746 num_dont_move_pages
,
3747 npage_bytes(num_dont_move_pages
));
3751 /* Now that all of the pinned (dont_move) pages are known, and
3752 * before we start to scavenge (and thus relocate) objects,
3753 * relocate the pinned pages to newspace, so that the scavenger
3754 * will not attempt to relocate their contents. */
3755 move_pinned_pages_to_newspace();
3757 /* Scavenge all the rest of the roots. */
3759 #if !defined(LISP_FEATURE_X86) && !defined(LISP_FEATURE_X86_64)
3761 * If not x86, we need to scavenge the interrupt context(s) and the
3766 for_each_thread(th
) {
3767 scavenge_interrupt_contexts(th
);
3768 scavenge_control_stack(th
);
3771 # ifdef LISP_FEATURE_SB_SAFEPOINT
3772 /* In this case, scrub all stacks right here from the GCing thread
3773 * instead of doing what the comment below says. Suboptimal, but
3776 scrub_thread_control_stack(th
);
3778 /* Scrub the unscavenged control stack space, so that we can't run
3779 * into any stale pointers in a later GC (this is done by the
3780 * stop-for-gc handler in the other threads). */
3781 scrub_control_stack();
3786 /* Scavenge the Lisp functions of the interrupt handlers, taking
3787 * care to avoid SIG_DFL and SIG_IGN. */
3788 for (i
= 0; i
< NSIG
; i
++) {
3789 union interrupt_handler handler
= interrupt_handlers
[i
];
3790 if (!ARE_SAME_HANDLER(handler
.c
, SIG_IGN
) &&
3791 !ARE_SAME_HANDLER(handler
.c
, SIG_DFL
)) {
3792 scavenge((lispobj
*)(interrupt_handlers
+ i
), 1);
3795 /* Scavenge the binding stacks. */
3798 for_each_thread(th
) {
3799 sword_t len
= (lispobj
*)get_binding_stack_pointer(th
) -
3800 th
->binding_stack_start
;
3801 scavenge((lispobj
*) th
->binding_stack_start
,len
);
3802 #ifdef LISP_FEATURE_SB_THREAD
3803 /* do the tls as well */
3804 len
=(SymbolValue(FREE_TLS_INDEX
,0) >> WORD_SHIFT
) -
3805 (sizeof (struct thread
))/(sizeof (lispobj
));
3806 scavenge((lispobj
*) (th
+1),len
);
3811 /* The original CMU CL code had scavenge-read-only-space code
3812 * controlled by the Lisp-level variable
3813 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
3814 * wasn't documented under what circumstances it was useful or
3815 * safe to turn it on, so it's been turned off in SBCL. If you
3816 * want/need this functionality, and can test and document it,
3817 * please submit a patch. */
3819 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE
) != NIL
) {
3820 uword_t read_only_space_size
=
3821 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
) -
3822 (lispobj
*)READ_ONLY_SPACE_START
;
3824 "/scavenge read only space: %d bytes\n",
3825 read_only_space_size
* sizeof(lispobj
)));
3826 scavenge( (lispobj
*) READ_ONLY_SPACE_START
, read_only_space_size
);
3830 /* Scavenge static space. */
3832 (lispobj
*)SymbolValue(STATIC_SPACE_FREE_POINTER
,0) -
3833 (lispobj
*)STATIC_SPACE_START
;
3834 if (gencgc_verbose
> 1) {
3836 "/scavenge static space: %d bytes\n",
3837 static_space_size
* sizeof(lispobj
)));
3839 scavenge( (lispobj
*) STATIC_SPACE_START
, static_space_size
);
3841 /* All generations but the generation being GCed need to be
3842 * scavenged. The new_space generation needs special handling as
3843 * objects may be moved in - it is handled separately below. */
3844 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3845 scavenge_immobile_roots(generation
+1, SCRATCH_GENERATION
);
3847 scavenge_generations(generation
+1, PSEUDO_STATIC_GENERATION
);
3849 scavenge_pinned_ranges();
3851 /* Finally scavenge the new_space generation. Keep going until no
3852 * more objects are moved into the new generation */
3853 scavenge_newspace_generation(new_space
);
3855 /* FIXME: I tried reenabling this check when debugging unrelated
3856 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3857 * Since the current GC code seems to work well, I'm guessing that
3858 * this debugging code is just stale, but I haven't tried to
3859 * figure it out. It should be figured out and then either made to
3860 * work or just deleted. */
3862 #define RESCAN_CHECK 0
3864 /* As a check re-scavenge the newspace once; no new objects should
3867 os_vm_size_t old_bytes_allocated
= bytes_allocated
;
3868 os_vm_size_t bytes_allocated
;
3870 /* Start with a full scavenge. */
3871 scavenge_newspace_generation_one_scan(new_space
);
3873 /* Flush the current regions, updating the tables. */
3874 gc_alloc_update_all_page_tables(1);
3876 bytes_allocated
= bytes_allocated
- old_bytes_allocated
;
3878 if (bytes_allocated
!= 0) {
3879 lose("Rescan of new_space allocated %d more bytes.\n",
3885 scan_weak_hash_tables();
3886 scan_weak_pointers();
3887 wipe_nonpinned_words();
3888 #ifdef LISP_FEATURE_IMMOBILE_SPACE
3889 // Do this last, because until wipe_nonpinned_words() happens,
3890 // not all page table entries have the 'gen' value updated,
3891 // which we need to correctly find all old->young pointers.
3892 sweep_immobile_space(raise
);
3895 /* Flush the current regions, updating the tables. */
3896 gc_alloc_update_all_page_tables(0);
3898 /* Free the pages in oldspace, but not those marked dont_move. */
3901 /* If the GC is not raising the age then lower the generation back
3902 * to its normal generation number */
3904 for (i
= 0; i
< last_free_page
; i
++)
3905 if ((page_table
[i
].bytes_used
!= 0)
3906 && (page_table
[i
].gen
== SCRATCH_GENERATION
))
3907 page_table
[i
].gen
= generation
;
3908 gc_assert(generations
[generation
].bytes_allocated
== 0);
3909 generations
[generation
].bytes_allocated
=
3910 generations
[SCRATCH_GENERATION
].bytes_allocated
;
3911 generations
[SCRATCH_GENERATION
].bytes_allocated
= 0;
3914 /* Reset the alloc_start_page for generation. */
3915 generations
[generation
].alloc_start_page
= 0;
3916 generations
[generation
].alloc_unboxed_start_page
= 0;
3917 generations
[generation
].alloc_large_start_page
= 0;
3918 generations
[generation
].alloc_large_unboxed_start_page
= 0;
3920 if (generation
>= verify_gens
) {
3921 if (gencgc_verbose
) {
3927 /* Set the new gc trigger for the GCed generation. */
3928 generations
[generation
].gc_trigger
=
3929 generations
[generation
].bytes_allocated
3930 + generations
[generation
].bytes_consed_between_gc
;
3933 generations
[generation
].num_gc
= 0;
3935 ++generations
[generation
].num_gc
;
3939 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3941 update_dynamic_space_free_pointer(void)
3943 page_index_t last_page
= -1, i
;
3945 for (i
= 0; i
< last_free_page
; i
++)
3946 if (page_allocated_p(i
) && (page_table
[i
].bytes_used
!= 0))
3949 last_free_page
= last_page
+1;
3951 set_alloc_pointer((lispobj
)(page_address(last_free_page
)));
3952 return 0; /* dummy value: return something ... */
3956 remap_page_range (page_index_t from
, page_index_t to
)
3958 /* There's a mysterious Solaris/x86 problem with using mmap
3959 * tricks for memory zeroing. See sbcl-devel thread
3960 * "Re: patch: standalone executable redux".
3962 #if defined(LISP_FEATURE_SUNOS)
3963 zero_and_mark_pages(from
, to
);
3966 release_granularity
= gencgc_release_granularity
/GENCGC_CARD_BYTES
,
3967 release_mask
= release_granularity
-1,
3969 aligned_from
= (from
+release_mask
)&~release_mask
,
3970 aligned_end
= (end
&~release_mask
);
3972 if (aligned_from
< aligned_end
) {
3973 zero_pages_with_mmap(aligned_from
, aligned_end
-1);
3974 if (aligned_from
!= from
)
3975 zero_and_mark_pages(from
, aligned_from
-1);
3976 if (aligned_end
!= end
)
3977 zero_and_mark_pages(aligned_end
, end
-1);
3979 zero_and_mark_pages(from
, to
);
3985 remap_free_pages (page_index_t from
, page_index_t to
, int forcibly
)
3987 page_index_t first_page
, last_page
;
3990 return remap_page_range(from
, to
);
3992 for (first_page
= from
; first_page
<= to
; first_page
++) {
3993 if (page_allocated_p(first_page
) ||
3994 (page_table
[first_page
].need_to_zero
== 0))
3997 last_page
= first_page
+ 1;
3998 while (page_free_p(last_page
) &&
3999 (last_page
<= to
) &&
4000 (page_table
[last_page
].need_to_zero
== 1))
4003 remap_page_range(first_page
, last_page
-1);
4005 first_page
= last_page
;
4009 generation_index_t small_generation_limit
= 1;
4011 /* GC all generations newer than last_gen, raising the objects in each
4012 * to the next older generation - we finish when all generations below
4013 * last_gen are empty. Then if last_gen is due for a GC, or if
4014 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
4015 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
4017 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
4018 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
4020 collect_garbage(generation_index_t last_gen
)
4022 generation_index_t gen
= 0, i
;
4023 int raise
, more
= 0;
4025 /* The largest value of last_free_page seen since the time
4026 * remap_free_pages was called. */
4027 static page_index_t high_water_mark
= 0;
4029 FSHOW((stderr
, "/entering collect_garbage(%d)\n", last_gen
));
4030 log_generation_stats(gc_logfile
, "=== GC Start ===");
4034 if (last_gen
> HIGHEST_NORMAL_GENERATION
+1) {
4036 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
4041 /* Flush the alloc regions updating the tables. */
4042 gc_alloc_update_all_page_tables(1);
4044 /* Verify the new objects created by Lisp code. */
4045 if (pre_verify_gen_0
) {
4046 FSHOW((stderr
, "pre-checking generation 0\n"));
4047 verify_generation(0);
4050 if (gencgc_verbose
> 1)
4051 print_generation_stats();
4053 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4054 /* Immobile space generation bits are lazily updated for gen0
4055 (not touched on every object allocation) so do it now */
4056 update_immobile_nursery_bits();
4060 /* Collect the generation. */
4062 if (more
|| (gen
>= gencgc_oldest_gen_to_gc
)) {
4063 /* Never raise the oldest generation. Never raise the extra generation
4064 * collected due to more-flag. */
4070 || (generations
[gen
].num_gc
>= generations
[gen
].number_of_gcs_before_promotion
);
4071 /* If we would not normally raise this one, but we're
4072 * running low on space in comparison to the object-sizes
4073 * we've been seeing, raise it and collect the next one
4075 if (!raise
&& gen
== last_gen
) {
4076 more
= (2*large_allocation
) >= (dynamic_space_size
- bytes_allocated
);
4081 if (gencgc_verbose
> 1) {
4083 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
4086 generations
[gen
].bytes_allocated
,
4087 generations
[gen
].gc_trigger
,
4088 generations
[gen
].num_gc
));
4091 /* If an older generation is being filled, then update its
4094 generations
[gen
+1].cum_sum_bytes_allocated
+=
4095 generations
[gen
+1].bytes_allocated
;
4098 garbage_collect_generation(gen
, raise
);
4100 /* Reset the memory age cum_sum. */
4101 generations
[gen
].cum_sum_bytes_allocated
= 0;
4103 if (gencgc_verbose
> 1) {
4104 FSHOW((stderr
, "GC of generation %d finished:\n", gen
));
4105 print_generation_stats();
4109 } while ((gen
<= gencgc_oldest_gen_to_gc
)
4110 && ((gen
< last_gen
)
4113 && (generations
[gen
].bytes_allocated
4114 > generations
[gen
].gc_trigger
)
4115 && (generation_average_age(gen
)
4116 > generations
[gen
].minimum_age_before_gc
))));
4118 /* Now if gen-1 was raised all generations before gen are empty.
4119 * If it wasn't raised then all generations before gen-1 are empty.
4121 * Now objects within this gen's pages cannot point to younger
4122 * generations unless they are written to. This can be exploited
4123 * by write-protecting the pages of gen; then when younger
4124 * generations are GCed only the pages which have been written
4129 gen_to_wp
= gen
- 1;
4131 /* There's not much point in WPing pages in generation 0 as it is
4132 * never scavenged (except promoted pages). */
4133 if ((gen_to_wp
> 0) && enable_page_protection
) {
4134 /* Check that they are all empty. */
4135 for (i
= 0; i
< gen_to_wp
; i
++) {
4136 if (generations
[i
].bytes_allocated
)
4137 lose("trying to write-protect gen. %d when gen. %d nonempty\n",
4140 write_protect_generation_pages(gen_to_wp
);
4142 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4143 write_protect_immobile_space();
4146 /* Set gc_alloc() back to generation 0. The current regions should
4147 * be flushed after the above GCs. */
4148 gc_assert((boxed_region
.free_pointer
- boxed_region
.start_addr
) == 0);
4149 gc_alloc_generation
= 0;
4151 /* Save the high-water mark before updating last_free_page */
4152 if (last_free_page
> high_water_mark
)
4153 high_water_mark
= last_free_page
;
4155 update_dynamic_space_free_pointer();
4157 /* Update auto_gc_trigger. Make sure we trigger the next GC before
4158 * running out of heap! */
4159 if (bytes_consed_between_gcs
<= (dynamic_space_size
- bytes_allocated
))
4160 auto_gc_trigger
= bytes_allocated
+ bytes_consed_between_gcs
;
4162 auto_gc_trigger
= bytes_allocated
+ (dynamic_space_size
- bytes_allocated
)/2;
4165 fprintf(stderr
,"Next gc when %"OS_VM_SIZE_FMT
" bytes have been consed\n",
4168 /* If we did a big GC (arbitrarily defined as gen > 1), release memory
4171 if (gen
> small_generation_limit
) {
4172 if (last_free_page
> high_water_mark
)
4173 high_water_mark
= last_free_page
;
4174 remap_free_pages(0, high_water_mark
, 0);
4175 high_water_mark
= 0;
4179 large_allocation
= 0;
4181 log_generation_stats(gc_logfile
, "=== GC End ===");
4182 SHOW("returning from collect_garbage");
4185 /* This is called by Lisp PURIFY when it is finished. All live objects
4186 * will have been moved to the RO and Static heaps. The dynamic space
4187 * will need a full re-initialization. We don't bother having Lisp
4188 * PURIFY flush the current gc_alloc() region, as the page_tables are
4189 * re-initialized, and every page is zeroed to be sure. */
4191 gc_free_heap(void) // FIXME: remove, not used
4193 page_index_t page
, last_page
;
4195 if (gencgc_verbose
> 1) {
4196 SHOW("entering gc_free_heap");
4199 for (page
= 0; page
< page_table_pages
; page
++) {
4200 /* Skip free pages which should already be zero filled. */
4201 if (page_allocated_p(page
)) {
4203 for (last_page
= page
;
4204 (last_page
< page_table_pages
) && page_allocated_p(last_page
);
4206 /* Mark the page free. The other slots are assumed invalid
4207 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
4208 * should not be write-protected -- except that the
4209 * generation is used for the current region but it sets
4211 page_table
[page
].allocated
= FREE_PAGE_FLAG
;
4212 page_table
[page
].bytes_used
= 0;
4213 page_table
[page
].write_protected
= 0;
4216 #ifndef LISP_FEATURE_WIN32 /* Pages already zeroed on win32? Not sure
4217 * about this change. */
4218 page_start
= (void *)page_address(page
);
4219 os_protect(page_start
, npage_bytes(last_page
-page
), OS_VM_PROT_ALL
);
4220 remap_free_pages(page
, last_page
-1, 1);
4223 } else if (gencgc_zero_check_during_free_heap
) {
4224 /* Double-check that the page is zero filled. */
4225 sword_t
*page_start
;
4227 gc_assert(page_free_p(page
));
4228 gc_assert(page_table
[page
].bytes_used
== 0);
4229 page_start
= (sword_t
*)page_address(page
);
4230 for (i
=0; i
<(long)(GENCGC_CARD_BYTES
/sizeof(sword_t
)); i
++) {
4231 if (page_start
[i
] != 0) {
4232 lose("free region not zero at %x\n", page_start
+ i
);
4238 bytes_allocated
= 0;
4240 /* Initialize the generations. */
4241 for (page
= 0; page
< NUM_GENERATIONS
; page
++) {
4242 generations
[page
].alloc_start_page
= 0;
4243 generations
[page
].alloc_unboxed_start_page
= 0;
4244 generations
[page
].alloc_large_start_page
= 0;
4245 generations
[page
].alloc_large_unboxed_start_page
= 0;
4246 generations
[page
].bytes_allocated
= 0;
4247 generations
[page
].gc_trigger
= 2000000;
4248 generations
[page
].num_gc
= 0;
4249 generations
[page
].cum_sum_bytes_allocated
= 0;
4252 if (gencgc_verbose
> 1)
4253 print_generation_stats();
4255 /* Initialize gc_alloc(). */
4256 gc_alloc_generation
= 0;
4258 gc_set_region_empty(&boxed_region
);
4259 gc_set_region_empty(&unboxed_region
);
4262 set_alloc_pointer((lispobj
)((char *)heap_base
));
4264 if (verify_after_free_heap
) {
4265 /* Check whether purify has left any bad pointers. */
4266 FSHOW((stderr
, "checking after free_heap\n"));
4276 #if defined(LISP_FEATURE_SB_SAFEPOINT)
4280 /* Compute the number of pages needed for the dynamic space.
4281 * Dynamic space size should be aligned on page size. */
4282 page_table_pages
= dynamic_space_size
/GENCGC_CARD_BYTES
;
4283 gc_assert(dynamic_space_size
== npage_bytes(page_table_pages
));
4285 /* Default nursery size to 5% of the total dynamic space size,
4287 bytes_consed_between_gcs
= dynamic_space_size
/(os_vm_size_t
)20;
4288 if (bytes_consed_between_gcs
< (1024*1024))
4289 bytes_consed_between_gcs
= 1024*1024;
4291 /* The page_table must be allocated using "calloc" to initialize
4292 * the page structures correctly. There used to be a separate
4293 * initialization loop (now commented out; see below) but that was
4294 * unnecessary and did hurt startup time. */
4295 page_table
= calloc(page_table_pages
, sizeof(struct page
));
4296 gc_assert(page_table
);
4297 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4301 size_t pins_map_size_in_bytes
=
4302 (n_dwords_in_card
/ N_WORD_BITS
) * sizeof (uword_t
) * page_table_pages
;
4303 /* We use mmap directly here so that we can use a minimum of
4304 system calls per page during GC.
4305 All we need here now is a madvise(DONTNEED) at the end of GC. */
4306 page_table_pinned_dwords
4307 = (in_use_marker_t
*)os_validate(NULL
, pins_map_size_in_bytes
);
4308 /* We do not need to zero */
4309 gc_assert(page_table_pinned_dwords
);
4312 scavtab
[WEAK_POINTER_WIDETAG
] = scav_weak_pointer
;
4313 transother
[SIMPLE_ARRAY_WIDETAG
] = trans_boxed_large
;
4315 heap_base
= (void*)DYNAMIC_SPACE_START
;
4317 /* The page structures are initialized implicitly when page_table
4318 * is allocated with "calloc" above. Formerly we had the following
4319 * explicit initialization here (comments converted to C99 style
4320 * for readability as C's block comments don't nest):
4322 * // Initialize each page structure.
4323 * for (i = 0; i < page_table_pages; i++) {
4324 * // Initialize all pages as free.
4325 * page_table[i].allocated = FREE_PAGE_FLAG;
4326 * page_table[i].bytes_used = 0;
4328 * // Pages are not write-protected at startup.
4329 * page_table[i].write_protected = 0;
4332 * Without this loop the image starts up much faster when dynamic
4333 * space is large -- which it is on 64-bit platforms already by
4334 * default -- and when "calloc" for large arrays is implemented
4335 * using copy-on-write of a page of zeroes -- which it is at least
4336 * on Linux. In this case the pages that page_table_pages is stored
4337 * in are mapped and cleared not before the corresponding part of
4338 * dynamic space is used. For example, this saves clearing 16 MB of
4339 * memory at startup if the page size is 4 KB and the size of
4340 * dynamic space is 4 GB.
4341 * FREE_PAGE_FLAG must be 0 for this to work correctly which is
4342 * asserted below: */
4344 /* Compile time assertion: If triggered, declares an array
4345 * of dimension -1 forcing a syntax error. The intent of the
4346 * assignment is to avoid an "unused variable" warning. */
4347 char assert_free_page_flag_0
[(FREE_PAGE_FLAG
) ? -1 : 1];
4348 assert_free_page_flag_0
[0] = assert_free_page_flag_0
[0];
4351 bytes_allocated
= 0;
4353 /* Initialize the generations.
4355 * FIXME: very similar to code in gc_free_heap(), should be shared */
4356 for (i
= 0; i
< NUM_GENERATIONS
; i
++) {
4357 generations
[i
].alloc_start_page
= 0;
4358 generations
[i
].alloc_unboxed_start_page
= 0;
4359 generations
[i
].alloc_large_start_page
= 0;
4360 generations
[i
].alloc_large_unboxed_start_page
= 0;
4361 generations
[i
].bytes_allocated
= 0;
4362 generations
[i
].gc_trigger
= 2000000;
4363 generations
[i
].num_gc
= 0;
4364 generations
[i
].cum_sum_bytes_allocated
= 0;
4365 /* the tune-able parameters */
4366 generations
[i
].bytes_consed_between_gc
4367 = bytes_consed_between_gcs
/(os_vm_size_t
)HIGHEST_NORMAL_GENERATION
;
4368 generations
[i
].number_of_gcs_before_promotion
= 1;
4369 generations
[i
].minimum_age_before_gc
= 0.75;
4372 /* Initialize gc_alloc. */
4373 gc_alloc_generation
= 0;
4374 gc_set_region_empty(&boxed_region
);
4375 gc_set_region_empty(&unboxed_region
);
4380 /* Pick up the dynamic space from after a core load.
4382 * The ALLOCATION_POINTER points to the end of the dynamic space.
4386 gencgc_pickup_dynamic(void)
4388 page_index_t page
= 0;
4389 void *alloc_ptr
= (void *)get_alloc_pointer();
4390 lispobj
*prev
=(lispobj
*)page_address(page
);
4391 generation_index_t gen
= PSEUDO_STATIC_GENERATION
;
4393 bytes_allocated
= 0;
4396 lispobj
*first
,*ptr
= (lispobj
*)page_address(page
);
4398 if (!gencgc_partial_pickup
|| page_allocated_p(page
)) {
4399 /* It is possible, though rare, for the saved page table
4400 * to contain free pages below alloc_ptr. */
4401 page_table
[page
].gen
= gen
;
4402 page_table
[page
].bytes_used
= GENCGC_CARD_BYTES
;
4403 page_table
[page
].large_object
= 0;
4404 page_table
[page
].write_protected
= 0;
4405 page_table
[page
].write_protected_cleared
= 0;
4406 page_table
[page
].dont_move
= 0;
4407 page_table
[page
].need_to_zero
= 1;
4409 bytes_allocated
+= GENCGC_CARD_BYTES
;
4412 if (!gencgc_partial_pickup
) {
4413 page_table
[page
].allocated
= BOXED_PAGE_FLAG
;
4414 first
=gc_search_space(prev
,(ptr
+2)-prev
,ptr
);
4417 page_table
[page
].scan_start_offset
=
4418 page_address(page
) - (void *)prev
;
4421 } while (page_address(page
) < alloc_ptr
);
4423 last_free_page
= page
;
4425 generations
[gen
].bytes_allocated
= bytes_allocated
;
4427 gc_alloc_update_all_page_tables(1);
4428 write_protect_generation_pages(gen
);
4432 gc_initialize_pointers(void)
4434 gencgc_pickup_dynamic();
4438 /* alloc(..) is the external interface for memory allocation. It
4439 * allocates to generation 0. It is not called from within the garbage
4440 * collector as it is only external uses that need the check for heap
4441 * size (GC trigger) and to disable the interrupts (interrupts are
4442 * always disabled during a GC).
4444 * The vops that call alloc(..) assume that the returned space is zero-filled.
4445 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4447 * The check for a GC trigger is only performed when the current
4448 * region is full, so in most cases it's not needed. */
4450 static inline lispobj
*
4451 general_alloc_internal(sword_t nbytes
, int page_type_flag
, struct alloc_region
*region
,
4452 struct thread
*thread
)
4454 #ifndef LISP_FEATURE_WIN32
4455 lispobj alloc_signal
;
4458 void *new_free_pointer
;
4459 os_vm_size_t trigger_bytes
= 0;
4461 gc_assert(nbytes
> 0);
4463 /* Check for alignment allocation problems. */
4464 gc_assert((((uword_t
)region
->free_pointer
& LOWTAG_MASK
) == 0)
4465 && ((nbytes
& LOWTAG_MASK
) == 0));
4467 #if !(defined(LISP_FEATURE_WIN32) && defined(LISP_FEATURE_SB_THREAD))
4468 /* Must be inside a PA section. */
4469 gc_assert(get_pseudo_atomic_atomic(thread
));
4472 if ((os_vm_size_t
) nbytes
> large_allocation
)
4473 large_allocation
= nbytes
;
4475 /* maybe we can do this quickly ... */
4476 new_free_pointer
= region
->free_pointer
+ nbytes
;
4477 if (new_free_pointer
<= region
->end_addr
) {
4478 new_obj
= (void*)(region
->free_pointer
);
4479 region
->free_pointer
= new_free_pointer
;
4480 return(new_obj
); /* yup */
4483 /* We don't want to count nbytes against auto_gc_trigger unless we
4484 * have to: it speeds up the tenuring of objects and slows down
4485 * allocation. However, unless we do so when allocating _very_
4486 * large objects we are in danger of exhausting the heap without
4487 * running sufficient GCs.
4489 if ((os_vm_size_t
) nbytes
>= bytes_consed_between_gcs
)
4490 trigger_bytes
= nbytes
;
4492 /* we have to go the long way around, it seems. Check whether we
4493 * should GC in the near future
4495 if (auto_gc_trigger
&& (bytes_allocated
+trigger_bytes
> auto_gc_trigger
)) {
4496 /* Don't flood the system with interrupts if the need to gc is
4497 * already noted. This can happen for example when SUB-GC
4498 * allocates or after a gc triggered in a WITHOUT-GCING. */
4499 if (SymbolValue(GC_PENDING
,thread
) == NIL
) {
4500 /* set things up so that GC happens when we finish the PA
4502 SetSymbolValue(GC_PENDING
,T
,thread
);
4503 if (SymbolValue(GC_INHIBIT
,thread
) == NIL
) {
4504 #ifdef LISP_FEATURE_SB_SAFEPOINT
4505 thread_register_gc_trigger();
4507 set_pseudo_atomic_interrupted(thread
);
4508 #ifdef GENCGC_IS_PRECISE
4509 /* PPC calls alloc() from a trap
4510 * look up the most context if it's from a trap. */
4512 os_context_t
*context
=
4513 thread
->interrupt_data
->allocation_trap_context
;
4514 maybe_save_gc_mask_and_block_deferrables
4515 (context
? os_context_sigmask_addr(context
) : NULL
);
4518 maybe_save_gc_mask_and_block_deferrables(NULL
);
4524 new_obj
= gc_alloc_with_region(nbytes
, page_type_flag
, region
, 0);
4526 #ifndef LISP_FEATURE_WIN32
4527 /* for sb-prof, and not supported on Windows yet */
4528 alloc_signal
= SymbolValue(ALLOC_SIGNAL
,thread
);
4529 if ((alloc_signal
& FIXNUM_TAG_MASK
) == 0) {
4530 if ((sword_t
) alloc_signal
<= 0) {
4531 SetSymbolValue(ALLOC_SIGNAL
, T
, thread
);
4534 SetSymbolValue(ALLOC_SIGNAL
,
4535 alloc_signal
- (1 << N_FIXNUM_TAG_BITS
),
4545 general_alloc(sword_t nbytes
, int page_type_flag
)
4547 struct thread
*thread
= arch_os_get_current_thread();
4548 /* Select correct region, and call general_alloc_internal with it.
4549 * For other then boxed allocation we must lock first, since the
4550 * region is shared. */
4551 if (BOXED_PAGE_FLAG
& page_type_flag
) {
4552 #ifdef LISP_FEATURE_SB_THREAD
4553 struct alloc_region
*region
= (thread
? &(thread
->alloc_region
) : &boxed_region
);
4555 struct alloc_region
*region
= &boxed_region
;
4557 return general_alloc_internal(nbytes
, page_type_flag
, region
, thread
);
4558 } else if (UNBOXED_PAGE_FLAG
== page_type_flag
) {
4560 gc_assert(0 == thread_mutex_lock(&allocation_lock
));
4561 obj
= general_alloc_internal(nbytes
, page_type_flag
, &unboxed_region
, thread
);
4562 gc_assert(0 == thread_mutex_unlock(&allocation_lock
));
4565 lose("bad page type flag: %d", page_type_flag
);
4569 lispobj AMD64_SYSV_ABI
*
4570 alloc(sword_t nbytes
)
4572 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4573 struct thread
*self
= arch_os_get_current_thread();
4574 int was_pseudo_atomic
= get_pseudo_atomic_atomic(self
);
4575 if (!was_pseudo_atomic
)
4576 set_pseudo_atomic_atomic(self
);
4578 gc_assert(get_pseudo_atomic_atomic(arch_os_get_current_thread()));
4581 lispobj
*result
= general_alloc(nbytes
, BOXED_PAGE_FLAG
);
4583 #ifdef LISP_FEATURE_SB_SAFEPOINT_STRICTLY
4584 if (!was_pseudo_atomic
)
4585 clear_pseudo_atomic_atomic(self
);
4592 * shared support for the OS-dependent signal handlers which
4593 * catch GENCGC-related write-protect violations
4595 void unhandled_sigmemoryfault(void* addr
);
4597 /* Depending on which OS we're running under, different signals might
4598 * be raised for a violation of write protection in the heap. This
4599 * function factors out the common generational GC magic which needs
4600 * to invoked in this case, and should be called from whatever signal
4601 * handler is appropriate for the OS we're running under.
4603 * Return true if this signal is a normal generational GC thing that
4604 * we were able to handle, or false if it was abnormal and control
4605 * should fall through to the general SIGSEGV/SIGBUS/whatever logic.
4607 * We have two control flags for this: one causes us to ignore faults
4608 * on unprotected pages completely, and the second complains to stderr
4609 * but allows us to continue without losing.
4611 extern boolean ignore_memoryfaults_on_unprotected_pages
;
4612 boolean ignore_memoryfaults_on_unprotected_pages
= 0;
4614 extern boolean continue_after_memoryfault_on_unprotected_pages
;
4615 boolean continue_after_memoryfault_on_unprotected_pages
= 0;
4618 gencgc_handle_wp_violation(void* fault_addr
)
4620 page_index_t page_index
= find_page_index(fault_addr
);
4624 "heap WP violation? fault_addr=%p, page_index=%"PAGE_INDEX_FMT
"\n",
4625 fault_addr
, page_index
));
4628 /* Check whether the fault is within the dynamic space. */
4629 if (page_index
== (-1)) {
4630 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4631 extern int immobile_space_handle_wp_violation(void*);
4632 if (immobile_space_handle_wp_violation(fault_addr
))
4636 /* It can be helpful to be able to put a breakpoint on this
4637 * case to help diagnose low-level problems. */
4638 unhandled_sigmemoryfault(fault_addr
);
4640 /* not within the dynamic space -- not our responsibility */
4645 ret
= thread_mutex_lock(&free_pages_lock
);
4646 gc_assert(ret
== 0);
4647 if (page_table
[page_index
].write_protected
) {
4648 /* Unprotect the page. */
4649 os_protect(page_address(page_index
), GENCGC_CARD_BYTES
, OS_VM_PROT_ALL
);
4650 page_table
[page_index
].write_protected_cleared
= 1;
4651 page_table
[page_index
].write_protected
= 0;
4652 } else if (!ignore_memoryfaults_on_unprotected_pages
) {
4653 /* The only acceptable reason for this signal on a heap
4654 * access is that GENCGC write-protected the page.
4655 * However, if two CPUs hit a wp page near-simultaneously,
4656 * we had better not have the second one lose here if it
4657 * does this test after the first one has already set wp=0
4659 if(page_table
[page_index
].write_protected_cleared
!= 1) {
4660 void lisp_backtrace(int frames
);
4663 "Fault @ %p, page %"PAGE_INDEX_FMT
" not marked as write-protected:\n"
4664 " boxed_region.first_page: %"PAGE_INDEX_FMT
","
4665 " boxed_region.last_page %"PAGE_INDEX_FMT
"\n"
4666 " page.scan_start_offset: %"OS_VM_SIZE_FMT
"\n"
4667 " page.bytes_used: %"PAGE_BYTES_FMT
"\n"
4668 " page.allocated: %d\n"
4669 " page.write_protected: %d\n"
4670 " page.write_protected_cleared: %d\n"
4671 " page.generation: %d\n",
4674 boxed_region
.first_page
,
4675 boxed_region
.last_page
,
4676 page_table
[page_index
].scan_start_offset
,
4677 page_table
[page_index
].bytes_used
,
4678 page_table
[page_index
].allocated
,
4679 page_table
[page_index
].write_protected
,
4680 page_table
[page_index
].write_protected_cleared
,
4681 page_table
[page_index
].gen
);
4682 if (!continue_after_memoryfault_on_unprotected_pages
)
4686 ret
= thread_mutex_unlock(&free_pages_lock
);
4687 gc_assert(ret
== 0);
4688 /* Don't worry, we can handle it. */
4692 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4693 * it's not just a case of the program hitting the write barrier, and
4694 * are about to let Lisp deal with it. It's basically just a
4695 * convenient place to set a gdb breakpoint. */
4697 unhandled_sigmemoryfault(void *addr
)
4701 update_thread_page_tables(struct thread
*th
)
4703 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &th
->alloc_region
);
4704 #if defined(LISP_FEATURE_SB_SAFEPOINT_STRICTLY) && !defined(LISP_FEATURE_WIN32)
4705 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &th
->sprof_alloc_region
);
4709 /* GC is single-threaded and all memory allocations during a
4710 collection happen in the GC thread, so it is sufficient to update
4711 all the the page tables once at the beginning of a collection and
4712 update only page tables of the GC thread during the collection. */
4713 void gc_alloc_update_all_page_tables(int for_all_threads
)
4715 /* Flush the alloc regions updating the tables. */
4717 if (for_all_threads
) {
4718 for_each_thread(th
) {
4719 update_thread_page_tables(th
);
4723 th
= arch_os_get_current_thread();
4725 update_thread_page_tables(th
);
4728 gc_alloc_update_page_tables(UNBOXED_PAGE_FLAG
, &unboxed_region
);
4729 gc_alloc_update_page_tables(BOXED_PAGE_FLAG
, &boxed_region
);
4733 gc_set_region_empty(struct alloc_region
*region
)
4735 region
->first_page
= 0;
4736 region
->last_page
= -1;
4737 region
->start_addr
= page_address(0);
4738 region
->free_pointer
= page_address(0);
4739 region
->end_addr
= page_address(0);
4743 zero_all_free_pages()
4747 for (i
= 0; i
< last_free_page
; i
++) {
4748 if (page_free_p(i
)) {
4749 #ifdef READ_PROTECT_FREE_PAGES
4750 os_protect(page_address(i
),
4759 /* Things to do before doing a final GC before saving a core (without
4762 * + Pages in large_object pages aren't moved by the GC, so we need to
4763 * unset that flag from all pages.
4764 * + The pseudo-static generation isn't normally collected, but it seems
4765 * reasonable to collect it at least when saving a core. So move the
4766 * pages to a normal generation.
4769 prepare_for_final_gc ()
4773 #ifdef LISP_FEATURE_IMMOBILE_SPACE
4774 extern void prepare_immobile_space_for_final_gc();
4775 prepare_immobile_space_for_final_gc ();
4778 for (i
= 0; i
< last_free_page
; i
++) {
4779 page_table
[i
].large_object
= 0;
4780 if (page_table
[i
].gen
== PSEUDO_STATIC_GENERATION
) {
4781 int used
= page_table
[i
].bytes_used
;
4782 page_table
[i
].gen
= HIGHEST_NORMAL_GENERATION
;
4783 generations
[PSEUDO_STATIC_GENERATION
].bytes_allocated
-= used
;
4784 generations
[HIGHEST_NORMAL_GENERATION
].bytes_allocated
+= used
;
4790 /* Do a non-conservative GC, and then save a core with the initial
4791 * function being set to the value of the static symbol
4792 * SB!VM:RESTART-LISP-FUNCTION */
4794 gc_and_save(char *filename
, boolean prepend_runtime
,
4795 boolean save_runtime_options
, boolean compressed
,
4796 int compression_level
, int application_type
)
4799 void *runtime_bytes
= NULL
;
4800 size_t runtime_size
;
4802 file
= prepare_to_save(filename
, prepend_runtime
, &runtime_bytes
,
4807 conservative_stack
= 0;
4809 /* The filename might come from Lisp, and be moved by the now
4810 * non-conservative GC. */
4811 filename
= strdup(filename
);
4813 /* Collect twice: once into relatively high memory, and then back
4814 * into low memory. This compacts the retained data into the lower
4815 * pages, minimizing the size of the core file.
4817 prepare_for_final_gc();
4818 gencgc_alloc_start_page
= last_free_page
;
4819 collect_garbage(HIGHEST_NORMAL_GENERATION
+1);
4821 prepare_for_final_gc();
4822 gencgc_alloc_start_page
= -1;
4823 collect_garbage(HIGHEST_NORMAL_GENERATION
+1);
4825 if (prepend_runtime
)
4826 save_runtime_to_filehandle(file
, runtime_bytes
, runtime_size
,
4829 /* The dumper doesn't know that pages need to be zeroed before use. */
4830 zero_all_free_pages();
4831 save_to_filehandle(file
, filename
, SymbolValue(RESTART_LISP_FUNCTION
,0),
4832 prepend_runtime
, save_runtime_options
,
4833 compressed
? compression_level
: COMPRESSION_LEVEL_NONE
);
4834 /* Oops. Save still managed to fail. Since we've mangled the stack
4835 * beyond hope, there's not much we can do.
4836 * (beyond FUNCALLing RESTART_LISP_FUNCTION, but I suspect that's
4837 * going to be rather unsatisfactory too... */
4838 lose("Attempt to save core after non-conservative GC failed.\n");