2 * GENerational Conservative Garbage Collector for SBCL x86
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>.
36 #include "interrupt.h"
41 #include "gc-internal.h"
43 #include "genesis/vector.h"
44 #include "genesis/weak-pointer.h"
45 #include "genesis/simple-fun.h"
47 /* assembly language stub that executes trap_PendingInterrupt */
48 void do_pending_interrupt(void);
50 /* forward declarations */
51 int gc_find_freeish_pages(int *restart_page_ptr, int nbytes, int unboxed);
52 void gc_set_region_empty(struct alloc_region *region);
53 void gc_alloc_update_all_page_tables(void);
54 static void gencgc_pickup_dynamic(void);
55 boolean interrupt_maybe_gc_int(int, siginfo_t *, void *);
62 /* the number of actual generations. (The number of 'struct
63 * generation' objects is one more than this, because one object
64 * serves as scratch when GC'ing.) */
65 #define NUM_GENERATIONS 6
67 /* Should we use page protection to help avoid the scavenging of pages
68 * that don't have pointers to younger generations? */
69 boolean enable_page_protection = 1;
71 /* Should we unmap a page and re-mmap it to have it zero filled? */
72 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
73 /* comment from cmucl-2.4.8: This can waste a lot of swap on FreeBSD
74 * so don't unmap there.
76 * The CMU CL comment didn't specify a version, but was probably an
77 * old version of FreeBSD (pre-4.0), so this might no longer be true.
78 * OTOH, if it is true, this behavior might exist on OpenBSD too, so
79 * for now we don't unmap there either. -- WHN 2001-04-07 */
80 boolean gencgc_unmap_zero = 0;
82 boolean gencgc_unmap_zero = 1;
85 /* the minimum size (in bytes) for a large object*/
86 unsigned large_object_size = 4 * PAGE_BYTES;
95 /* the verbosity level. All non-error messages are disabled at level 0;
96 * and only a few rare messages are printed at level 1. */
97 unsigned gencgc_verbose = (QSHOW ? 1 : 0);
99 /* FIXME: At some point enable the various error-checking things below
100 * and see what they say. */
102 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
103 * Set verify_gens to NUM_GENERATIONS to disable this kind of check. */
104 int verify_gens = NUM_GENERATIONS;
106 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
107 boolean pre_verify_gen_0 = 0;
109 /* Should we check for bad pointers after gc_free_heap is called
110 * from Lisp PURIFY? */
111 boolean verify_after_free_heap = 0;
113 /* Should we print a note when code objects are found in the dynamic space
114 * during a heap verify? */
115 boolean verify_dynamic_code_check = 0;
117 /* Should we check code objects for fixup errors after they are transported? */
118 boolean check_code_fixups = 0;
120 /* Should we check that newly allocated regions are zero filled? */
121 boolean gencgc_zero_check = 0;
123 /* Should we check that the free space is zero filled? */
124 boolean gencgc_enable_verify_zero_fill = 0;
126 /* Should we check that free pages are zero filled during gc_free_heap
127 * called after Lisp PURIFY? */
128 boolean gencgc_zero_check_during_free_heap = 0;
131 * GC structures and variables
134 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
135 unsigned long bytes_allocated = 0;
136 extern unsigned long bytes_consed_between_gcs; /* gc-common.c */
137 unsigned long auto_gc_trigger = 0;
139 /* the source and destination generations. These are set before a GC starts
145 /* An array of page structures is statically allocated.
146 * This helps quickly map between an address its page structure.
147 * NUM_PAGES is set from the size of the dynamic space. */
148 struct page page_table[NUM_PAGES];
150 /* To map addresses to page structures the address of the first page
152 static void *heap_base = NULL;
155 /* Calculate the start address for the given page number. */
157 page_address(int page_num)
159 return (heap_base + (page_num * PAGE_BYTES));
162 /* Find the page index within the page_table for the given
163 * address. Return -1 on failure. */
165 find_page_index(void *addr)
167 int index = addr-heap_base;
170 index = ((unsigned int)index)/PAGE_BYTES;
171 if (index < NUM_PAGES)
178 /* a structure to hold the state of a generation */
181 /* the first page that gc_alloc() checks on its next call */
182 int alloc_start_page;
184 /* the first page that gc_alloc_unboxed() checks on its next call */
185 int alloc_unboxed_start_page;
187 /* the first page that gc_alloc_large (boxed) considers on its next
188 * call. (Although it always allocates after the boxed_region.) */
189 int alloc_large_start_page;
191 /* the first page that gc_alloc_large (unboxed) considers on its
192 * next call. (Although it always allocates after the
193 * current_unboxed_region.) */
194 int alloc_large_unboxed_start_page;
196 /* the bytes allocated to this generation */
199 /* the number of bytes at which to trigger a GC */
202 /* to calculate a new level for gc_trigger */
203 int bytes_consed_between_gc;
205 /* the number of GCs since the last raise */
208 /* the average age after which a GC will raise objects to the
212 /* the cumulative sum of the bytes allocated to this generation. It is
213 * cleared after a GC on this generations, and update before new
214 * objects are added from a GC of a younger generation. Dividing by
215 * the bytes_allocated will give the average age of the memory in
216 * this generation since its last GC. */
217 int cum_sum_bytes_allocated;
219 /* a minimum average memory age before a GC will occur helps
220 * prevent a GC when a large number of new live objects have been
221 * added, in which case a GC could be a waste of time */
222 double min_av_mem_age;
224 /* the number of actual generations. (The number of 'struct
225 * generation' objects is one more than this, because one object
226 * serves as scratch when GC'ing.) */
227 #define NUM_GENERATIONS 6
229 /* an array of generation structures. There needs to be one more
230 * generation structure than actual generations as the oldest
231 * generation is temporarily raised then lowered. */
232 struct generation generations[NUM_GENERATIONS+1];
234 /* the oldest generation that is will currently be GCed by default.
235 * Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
237 * The default of (NUM_GENERATIONS-1) enables GC on all generations.
239 * Setting this to 0 effectively disables the generational nature of
240 * the GC. In some applications generational GC may not be useful
241 * because there are no long-lived objects.
243 * An intermediate value could be handy after moving long-lived data
244 * into an older generation so an unnecessary GC of this long-lived
245 * data can be avoided. */
246 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
248 /* The maximum free page in the heap is maintained and used to update
249 * ALLOCATION_POINTER which is used by the room function to limit its
250 * search of the heap. XX Gencgc obviously needs to be better
251 * integrated with the Lisp code. */
252 static int last_free_page;
254 /* This lock is to prevent multiple threads from simultaneously
255 * allocating new regions which overlap each other. Note that the
256 * majority of GC is single-threaded, but alloc() may be called from
257 * >1 thread at a time and must be thread-safe. This lock must be
258 * seized before all accesses to generations[] or to parts of
259 * page_table[] that other threads may want to see */
261 static lispobj free_pages_lock=0;
265 * miscellaneous heap functions
268 /* Count the number of pages which are write-protected within the
269 * given generation. */
271 count_write_protect_generation_pages(int generation)
276 for (i = 0; i < last_free_page; i++)
277 if ((page_table[i].allocated != FREE_PAGE_FLAG)
278 && (page_table[i].gen == generation)
279 && (page_table[i].write_protected == 1))
284 /* Count the number of pages within the given generation. */
286 count_generation_pages(int generation)
291 for (i = 0; i < last_free_page; i++)
292 if ((page_table[i].allocated != 0)
293 && (page_table[i].gen == generation))
300 count_dont_move_pages(void)
304 for (i = 0; i < last_free_page; i++) {
305 if ((page_table[i].allocated != 0) && (page_table[i].dont_move != 0)) {
313 /* Work through the pages and add up the number of bytes used for the
314 * given generation. */
316 count_generation_bytes_allocated (int gen)
320 for (i = 0; i < last_free_page; i++) {
321 if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
322 result += page_table[i].bytes_used;
327 /* Return the average age of the memory in a generation. */
329 gen_av_mem_age(int gen)
331 if (generations[gen].bytes_allocated == 0)
335 ((double)generations[gen].cum_sum_bytes_allocated)
336 / ((double)generations[gen].bytes_allocated);
339 void fpu_save(int *); /* defined in x86-assem.S */
340 void fpu_restore(int *); /* defined in x86-assem.S */
341 /* The verbose argument controls how much to print: 0 for normal
342 * level of detail; 1 for debugging. */
344 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
349 /* This code uses the FP instructions which may be set up for Lisp
350 * so they need to be saved and reset for C. */
353 /* number of generations to print */
355 gens = NUM_GENERATIONS+1;
357 gens = NUM_GENERATIONS;
359 /* Print the heap stats. */
361 " Gen Boxed Unboxed LB LUB !move Alloc Waste Trig WP GCs Mem-age\n");
363 for (i = 0; i < gens; i++) {
367 int large_boxed_cnt = 0;
368 int large_unboxed_cnt = 0;
371 for (j = 0; j < last_free_page; j++)
372 if (page_table[j].gen == i) {
374 /* Count the number of boxed pages within the given
376 if (page_table[j].allocated & BOXED_PAGE_FLAG) {
377 if (page_table[j].large_object)
382 if(page_table[j].dont_move) pinned_cnt++;
383 /* Count the number of unboxed pages within the given
385 if (page_table[j].allocated & UNBOXED_PAGE_FLAG) {
386 if (page_table[j].large_object)
393 gc_assert(generations[i].bytes_allocated
394 == count_generation_bytes_allocated(i));
396 " %1d: %5d %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4f\n",
398 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
400 generations[i].bytes_allocated,
401 (count_generation_pages(i)*PAGE_BYTES
402 - generations[i].bytes_allocated),
403 generations[i].gc_trigger,
404 count_write_protect_generation_pages(i),
405 generations[i].num_gc,
408 fprintf(stderr," Total bytes allocated=%ld\n", bytes_allocated);
410 fpu_restore(fpu_state);
414 * allocation routines
418 * To support quick and inline allocation, regions of memory can be
419 * allocated and then allocated from with just a free pointer and a
420 * check against an end address.
422 * Since objects can be allocated to spaces with different properties
423 * e.g. boxed/unboxed, generation, ages; there may need to be many
424 * allocation regions.
426 * Each allocation region may be start within a partly used page. Many
427 * features of memory use are noted on a page wise basis, e.g. the
428 * generation; so if a region starts within an existing allocated page
429 * it must be consistent with this page.
431 * During the scavenging of the newspace, objects will be transported
432 * into an allocation region, and pointers updated to point to this
433 * allocation region. It is possible that these pointers will be
434 * scavenged again before the allocation region is closed, e.g. due to
435 * trans_list which jumps all over the place to cleanup the list. It
436 * is important to be able to determine properties of all objects
437 * pointed to when scavenging, e.g to detect pointers to the oldspace.
438 * Thus it's important that the allocation regions have the correct
439 * properties set when allocated, and not just set when closed. The
440 * region allocation routines return regions with the specified
441 * properties, and grab all the pages, setting their properties
442 * appropriately, except that the amount used is not known.
444 * These regions are used to support quicker allocation using just a
445 * free pointer. The actual space used by the region is not reflected
446 * in the pages tables until it is closed. It can't be scavenged until
449 * When finished with the region it should be closed, which will
450 * update the page tables for the actual space used returning unused
451 * space. Further it may be noted in the new regions which is
452 * necessary when scavenging the newspace.
454 * Large objects may be allocated directly without an allocation
455 * region, the page tables are updated immediately.
457 * Unboxed objects don't contain pointers to other objects and so
458 * don't need scavenging. Further they can't contain pointers to
459 * younger generations so WP is not needed. By allocating pages to
460 * unboxed objects the whole page never needs scavenging or
461 * write-protecting. */
463 /* We are only using two regions at present. Both are for the current
464 * newspace generation. */
465 struct alloc_region boxed_region;
466 struct alloc_region unboxed_region;
468 /* The generation currently being allocated to. */
469 static int gc_alloc_generation;
471 /* Find a new region with room for at least the given number of bytes.
473 * It starts looking at the current generation's alloc_start_page. So
474 * may pick up from the previous region if there is enough space. This
475 * keeps the allocation contiguous when scavenging the newspace.
477 * The alloc_region should have been closed by a call to
478 * gc_alloc_update_page_tables(), and will thus be in an empty state.
480 * To assist the scavenging functions write-protected pages are not
481 * used. Free pages should not be write-protected.
483 * It is critical to the conservative GC that the start of regions be
484 * known. To help achieve this only small regions are allocated at a
487 * During scavenging, pointers may be found to within the current
488 * region and the page generation must be set so that pointers to the
489 * from space can be recognized. Therefore the generation of pages in
490 * the region are set to gc_alloc_generation. To prevent another
491 * allocation call using the same pages, all the pages in the region
492 * are allocated, although they will initially be empty.
495 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
504 "/alloc_new_region for %d bytes from gen %d\n",
505 nbytes, gc_alloc_generation));
508 /* Check that the region is in a reset state. */
509 gc_assert((alloc_region->first_page == 0)
510 && (alloc_region->last_page == -1)
511 && (alloc_region->free_pointer == alloc_region->end_addr));
512 get_spinlock(&free_pages_lock,(int) alloc_region);
515 generations[gc_alloc_generation].alloc_unboxed_start_page;
518 generations[gc_alloc_generation].alloc_start_page;
520 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
521 bytes_found=(PAGE_BYTES - page_table[first_page].bytes_used)
522 + PAGE_BYTES*(last_page-first_page);
524 /* Set up the alloc_region. */
525 alloc_region->first_page = first_page;
526 alloc_region->last_page = last_page;
527 alloc_region->start_addr = page_table[first_page].bytes_used
528 + page_address(first_page);
529 alloc_region->free_pointer = alloc_region->start_addr;
530 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
532 /* Set up the pages. */
534 /* The first page may have already been in use. */
535 if (page_table[first_page].bytes_used == 0) {
537 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
539 page_table[first_page].allocated = BOXED_PAGE_FLAG;
540 page_table[first_page].gen = gc_alloc_generation;
541 page_table[first_page].large_object = 0;
542 page_table[first_page].first_object_offset = 0;
546 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
548 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
549 page_table[first_page].allocated |= OPEN_REGION_PAGE_FLAG;
551 gc_assert(page_table[first_page].gen == gc_alloc_generation);
552 gc_assert(page_table[first_page].large_object == 0);
554 for (i = first_page+1; i <= last_page; i++) {
556 page_table[i].allocated = UNBOXED_PAGE_FLAG;
558 page_table[i].allocated = BOXED_PAGE_FLAG;
559 page_table[i].gen = gc_alloc_generation;
560 page_table[i].large_object = 0;
561 /* This may not be necessary for unboxed regions (think it was
563 page_table[i].first_object_offset =
564 alloc_region->start_addr - page_address(i);
565 page_table[i].allocated |= OPEN_REGION_PAGE_FLAG ;
567 /* Bump up last_free_page. */
568 if (last_page+1 > last_free_page) {
569 last_free_page = last_page+1;
570 SetSymbolValue(ALLOCATION_POINTER,
571 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),
574 release_spinlock(&free_pages_lock);
576 /* we can do this after releasing free_pages_lock */
577 if (gencgc_zero_check) {
579 for (p = (int *)alloc_region->start_addr;
580 p < (int *)alloc_region->end_addr; p++) {
582 /* KLUDGE: It would be nice to use %lx and explicit casts
583 * (long) in code like this, so that it is less likely to
584 * break randomly when running on a machine with different
585 * word sizes. -- WHN 19991129 */
586 lose("The new region at %x is not zero.", p);
593 /* If the record_new_objects flag is 2 then all new regions created
596 * If it's 1 then then it is only recorded if the first page of the
597 * current region is <= new_areas_ignore_page. This helps avoid
598 * unnecessary recording when doing full scavenge pass.
600 * The new_object structure holds the page, byte offset, and size of
601 * new regions of objects. Each new area is placed in the array of
602 * these structures pointer to by new_areas. new_areas_index holds the
603 * offset into new_areas.
605 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
606 * later code must detect this and handle it, probably by doing a full
607 * scavenge of a generation. */
608 #define NUM_NEW_AREAS 512
609 static int record_new_objects = 0;
610 static int new_areas_ignore_page;
616 static struct new_area (*new_areas)[];
617 static int new_areas_index;
620 /* Add a new area to new_areas. */
622 add_new_area(int first_page, int offset, int size)
624 unsigned new_area_start,c;
627 /* Ignore if full. */
628 if (new_areas_index >= NUM_NEW_AREAS)
631 switch (record_new_objects) {
635 if (first_page > new_areas_ignore_page)
644 new_area_start = PAGE_BYTES*first_page + offset;
646 /* Search backwards for a prior area that this follows from. If
647 found this will save adding a new area. */
648 for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
650 PAGE_BYTES*((*new_areas)[i].page)
651 + (*new_areas)[i].offset
652 + (*new_areas)[i].size;
654 "/add_new_area S1 %d %d %d %d\n",
655 i, c, new_area_start, area_end));*/
656 if (new_area_start == area_end) {
658 "/adding to [%d] %d %d %d with %d %d %d:\n",
660 (*new_areas)[i].page,
661 (*new_areas)[i].offset,
662 (*new_areas)[i].size,
666 (*new_areas)[i].size += size;
671 (*new_areas)[new_areas_index].page = first_page;
672 (*new_areas)[new_areas_index].offset = offset;
673 (*new_areas)[new_areas_index].size = size;
675 "/new_area %d page %d offset %d size %d\n",
676 new_areas_index, first_page, offset, size));*/
679 /* Note the max new_areas used. */
680 if (new_areas_index > max_new_areas)
681 max_new_areas = new_areas_index;
684 /* Update the tables for the alloc_region. The region may be added to
687 * When done the alloc_region is set up so that the next quick alloc
688 * will fail safely and thus a new region will be allocated. Further
689 * it is safe to try to re-update the page table of this reset
692 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
698 int orig_first_page_bytes_used;
703 first_page = alloc_region->first_page;
705 /* Catch an unused alloc_region. */
706 if ((first_page == 0) && (alloc_region->last_page == -1))
709 next_page = first_page+1;
711 get_spinlock(&free_pages_lock,(int) alloc_region);
712 if (alloc_region->free_pointer != alloc_region->start_addr) {
713 /* some bytes were allocated in the region */
714 orig_first_page_bytes_used = page_table[first_page].bytes_used;
716 gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
718 /* All the pages used need to be updated */
720 /* Update the first page. */
722 /* If the page was free then set up the gen, and
723 * first_object_offset. */
724 if (page_table[first_page].bytes_used == 0)
725 gc_assert(page_table[first_page].first_object_offset == 0);
726 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
729 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
731 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
732 gc_assert(page_table[first_page].gen == gc_alloc_generation);
733 gc_assert(page_table[first_page].large_object == 0);
737 /* Calculate the number of bytes used in this page. This is not
738 * always the number of new bytes, unless it was free. */
740 if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>PAGE_BYTES) {
741 bytes_used = PAGE_BYTES;
744 page_table[first_page].bytes_used = bytes_used;
745 byte_cnt += bytes_used;
748 /* All the rest of the pages should be free. We need to set their
749 * first_object_offset pointer to the start of the region, and set
752 page_table[next_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
754 gc_assert(page_table[next_page].allocated==UNBOXED_PAGE_FLAG);
756 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
757 gc_assert(page_table[next_page].bytes_used == 0);
758 gc_assert(page_table[next_page].gen == gc_alloc_generation);
759 gc_assert(page_table[next_page].large_object == 0);
761 gc_assert(page_table[next_page].first_object_offset ==
762 alloc_region->start_addr - page_address(next_page));
764 /* Calculate the number of bytes used in this page. */
766 if ((bytes_used = (alloc_region->free_pointer
767 - page_address(next_page)))>PAGE_BYTES) {
768 bytes_used = PAGE_BYTES;
771 page_table[next_page].bytes_used = bytes_used;
772 byte_cnt += bytes_used;
777 region_size = alloc_region->free_pointer - alloc_region->start_addr;
778 bytes_allocated += region_size;
779 generations[gc_alloc_generation].bytes_allocated += region_size;
781 gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
783 /* Set the generations alloc restart page to the last page of
786 generations[gc_alloc_generation].alloc_unboxed_start_page =
789 generations[gc_alloc_generation].alloc_start_page = next_page-1;
791 /* Add the region to the new_areas if requested. */
793 add_new_area(first_page,orig_first_page_bytes_used, region_size);
797 "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
799 gc_alloc_generation));
802 /* There are no bytes allocated. Unallocate the first_page if
803 * there are 0 bytes_used. */
804 page_table[first_page].allocated &= ~(OPEN_REGION_PAGE_FLAG);
805 if (page_table[first_page].bytes_used == 0)
806 page_table[first_page].allocated = FREE_PAGE_FLAG;
809 /* Unallocate any unused pages. */
810 while (next_page <= alloc_region->last_page) {
811 gc_assert(page_table[next_page].bytes_used == 0);
812 page_table[next_page].allocated = FREE_PAGE_FLAG;
815 release_spinlock(&free_pages_lock);
816 /* alloc_region is per-thread, we're ok to do this unlocked */
817 gc_set_region_empty(alloc_region);
820 static inline void *gc_quick_alloc(int nbytes);
822 /* Allocate a possibly large object. */
824 gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
828 int orig_first_page_bytes_used;
834 get_spinlock(&free_pages_lock,(int) alloc_region);
838 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
840 first_page = generations[gc_alloc_generation].alloc_large_start_page;
842 if (first_page <= alloc_region->last_page) {
843 first_page = alloc_region->last_page+1;
846 last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed);
848 gc_assert(first_page > alloc_region->last_page);
850 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
853 generations[gc_alloc_generation].alloc_large_start_page = last_page;
855 /* Set up the pages. */
856 orig_first_page_bytes_used = page_table[first_page].bytes_used;
858 /* If the first page was free then set up the gen, and
859 * first_object_offset. */
860 if (page_table[first_page].bytes_used == 0) {
862 page_table[first_page].allocated = UNBOXED_PAGE_FLAG;
864 page_table[first_page].allocated = BOXED_PAGE_FLAG;
865 page_table[first_page].gen = gc_alloc_generation;
866 page_table[first_page].first_object_offset = 0;
867 page_table[first_page].large_object = 1;
871 gc_assert(page_table[first_page].allocated == UNBOXED_PAGE_FLAG);
873 gc_assert(page_table[first_page].allocated == BOXED_PAGE_FLAG);
874 gc_assert(page_table[first_page].gen == gc_alloc_generation);
875 gc_assert(page_table[first_page].large_object == 1);
879 /* Calc. the number of bytes used in this page. This is not
880 * always the number of new bytes, unless it was free. */
882 if ((bytes_used = nbytes+orig_first_page_bytes_used) > PAGE_BYTES) {
883 bytes_used = PAGE_BYTES;
886 page_table[first_page].bytes_used = bytes_used;
887 byte_cnt += bytes_used;
889 next_page = first_page+1;
891 /* All the rest of the pages should be free. We need to set their
892 * first_object_offset pointer to the start of the region, and
893 * set the bytes_used. */
895 gc_assert(page_table[next_page].allocated == FREE_PAGE_FLAG);
896 gc_assert(page_table[next_page].bytes_used == 0);
898 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
900 page_table[next_page].allocated = BOXED_PAGE_FLAG;
901 page_table[next_page].gen = gc_alloc_generation;
902 page_table[next_page].large_object = 1;
904 page_table[next_page].first_object_offset =
905 orig_first_page_bytes_used - PAGE_BYTES*(next_page-first_page);
907 /* Calculate the number of bytes used in this page. */
909 if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > PAGE_BYTES) {
910 bytes_used = PAGE_BYTES;
913 page_table[next_page].bytes_used = bytes_used;
914 page_table[next_page].write_protected=0;
915 page_table[next_page].dont_move=0;
916 byte_cnt += bytes_used;
920 gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
922 bytes_allocated += nbytes;
923 generations[gc_alloc_generation].bytes_allocated += nbytes;
925 /* Add the region to the new_areas if requested. */
927 add_new_area(first_page,orig_first_page_bytes_used,nbytes);
929 /* Bump up last_free_page */
930 if (last_page+1 > last_free_page) {
931 last_free_page = last_page+1;
932 SetSymbolValue(ALLOCATION_POINTER,
933 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),0);
935 release_spinlock(&free_pages_lock);
937 return((void *)(page_address(first_page)+orig_first_page_bytes_used));
941 gc_find_freeish_pages(int *restart_page_ptr, int nbytes, int unboxed)
946 int restart_page=*restart_page_ptr;
949 int large_p=(nbytes>=large_object_size);
950 gc_assert(free_pages_lock);
952 /* Search for a contiguous free space of at least nbytes. If it's
953 * a large object then align it on a page boundary by searching
954 * for a free page. */
957 first_page = restart_page;
959 while ((first_page < NUM_PAGES)
960 && (page_table[first_page].allocated != FREE_PAGE_FLAG))
963 while (first_page < NUM_PAGES) {
964 if(page_table[first_page].allocated == FREE_PAGE_FLAG)
966 if((page_table[first_page].allocated ==
967 (unboxed ? UNBOXED_PAGE_FLAG : BOXED_PAGE_FLAG)) &&
968 (page_table[first_page].large_object == 0) &&
969 (page_table[first_page].gen == gc_alloc_generation) &&
970 (page_table[first_page].bytes_used < (PAGE_BYTES-32)) &&
971 (page_table[first_page].write_protected == 0) &&
972 (page_table[first_page].dont_move == 0)) {
978 if (first_page >= NUM_PAGES) {
980 "Argh! gc_find_free_space failed (first_page), nbytes=%d.\n",
982 print_generation_stats(1);
986 gc_assert(page_table[first_page].write_protected == 0);
988 last_page = first_page;
989 bytes_found = PAGE_BYTES - page_table[first_page].bytes_used;
991 while (((bytes_found < nbytes)
992 || (!large_p && (num_pages < 2)))
993 && (last_page < (NUM_PAGES-1))
994 && (page_table[last_page+1].allocated == FREE_PAGE_FLAG)) {
997 bytes_found += PAGE_BYTES;
998 gc_assert(page_table[last_page].write_protected == 0);
1001 region_size = (PAGE_BYTES - page_table[first_page].bytes_used)
1002 + PAGE_BYTES*(last_page-first_page);
1004 gc_assert(bytes_found == region_size);
1005 restart_page = last_page + 1;
1006 } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1008 /* Check for a failure */
1009 if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1011 "Argh! gc_find_freeish_pages failed (restart_page), nbytes=%d.\n",
1013 print_generation_stats(1);
1016 *restart_page_ptr=first_page;
1020 /* Allocate bytes. All the rest of the special-purpose allocation
1021 * functions will eventually call this */
1024 gc_alloc_with_region(int nbytes,int unboxed_p, struct alloc_region *my_region,
1027 void *new_free_pointer;
1029 if(nbytes>=large_object_size)
1030 return gc_alloc_large(nbytes,unboxed_p,my_region);
1032 /* Check whether there is room in the current alloc region. */
1033 new_free_pointer = my_region->free_pointer + nbytes;
1035 if (new_free_pointer <= my_region->end_addr) {
1036 /* If so then allocate from the current alloc region. */
1037 void *new_obj = my_region->free_pointer;
1038 my_region->free_pointer = new_free_pointer;
1040 /* Unless a `quick' alloc was requested, check whether the
1041 alloc region is almost empty. */
1043 (my_region->end_addr - my_region->free_pointer) <= 32) {
1044 /* If so, finished with the current region. */
1045 gc_alloc_update_page_tables(unboxed_p, my_region);
1046 /* Set up a new region. */
1047 gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1050 return((void *)new_obj);
1053 /* Else not enough free space in the current region: retry with a
1056 gc_alloc_update_page_tables(unboxed_p, my_region);
1057 gc_alloc_new_region(nbytes, unboxed_p, my_region);
1058 return gc_alloc_with_region(nbytes,unboxed_p,my_region,0);
1061 /* these are only used during GC: all allocation from the mutator calls
1062 * alloc() -> gc_alloc_with_region() with the appropriate per-thread
1066 gc_general_alloc(int nbytes,int unboxed_p,int quick_p)
1068 struct alloc_region *my_region =
1069 unboxed_p ? &unboxed_region : &boxed_region;
1070 return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1073 static inline void *
1074 gc_quick_alloc(int nbytes)
1076 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1079 static inline void *
1080 gc_quick_alloc_large(int nbytes)
1082 return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1085 static inline void *
1086 gc_alloc_unboxed(int nbytes)
1088 return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1091 static inline void *
1092 gc_quick_alloc_unboxed(int nbytes)
1094 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1097 static inline void *
1098 gc_quick_alloc_large_unboxed(int nbytes)
1100 return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1104 * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1107 extern int (*scavtab[256])(lispobj *where, lispobj object);
1108 extern lispobj (*transother[256])(lispobj object);
1109 extern int (*sizetab[256])(lispobj *where);
1111 /* Copy a large boxed object. If the object is in a large object
1112 * region then it is simply promoted, else it is copied. If it's large
1113 * enough then it's copied to a large object region.
1115 * Vectors may have shrunk. If the object is not copied the space
1116 * needs to be reclaimed, and the page_tables corrected. */
1118 copy_large_object(lispobj object, int nwords)
1124 gc_assert(is_lisp_pointer(object));
1125 gc_assert(from_space_p(object));
1126 gc_assert((nwords & 0x01) == 0);
1129 /* Check whether it's in a large object region. */
1130 first_page = find_page_index((void *)object);
1131 gc_assert(first_page >= 0);
1133 if (page_table[first_page].large_object) {
1135 /* Promote the object. */
1137 int remaining_bytes;
1142 /* Note: Any page write-protection must be removed, else a
1143 * later scavenge_newspace may incorrectly not scavenge these
1144 * pages. This would not be necessary if they are added to the
1145 * new areas, but let's do it for them all (they'll probably
1146 * be written anyway?). */
1148 gc_assert(page_table[first_page].first_object_offset == 0);
1150 next_page = first_page;
1151 remaining_bytes = nwords*4;
1152 while (remaining_bytes > PAGE_BYTES) {
1153 gc_assert(page_table[next_page].gen == from_space);
1154 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1155 gc_assert(page_table[next_page].large_object);
1156 gc_assert(page_table[next_page].first_object_offset==
1157 -PAGE_BYTES*(next_page-first_page));
1158 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1160 page_table[next_page].gen = new_space;
1162 /* Remove any write-protection. We should be able to rely
1163 * on the write-protect flag to avoid redundant calls. */
1164 if (page_table[next_page].write_protected) {
1165 os_protect(page_address(next_page), PAGE_BYTES, OS_VM_PROT_ALL);
1166 page_table[next_page].write_protected = 0;
1168 remaining_bytes -= PAGE_BYTES;
1172 /* Now only one page remains, but the object may have shrunk
1173 * so there may be more unused pages which will be freed. */
1175 /* The object may have shrunk but shouldn't have grown. */
1176 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1178 page_table[next_page].gen = new_space;
1179 gc_assert(page_table[next_page].allocated == BOXED_PAGE_FLAG);
1181 /* Adjust the bytes_used. */
1182 old_bytes_used = page_table[next_page].bytes_used;
1183 page_table[next_page].bytes_used = remaining_bytes;
1185 bytes_freed = old_bytes_used - remaining_bytes;
1187 /* Free any remaining pages; needs care. */
1189 while ((old_bytes_used == PAGE_BYTES) &&
1190 (page_table[next_page].gen == from_space) &&
1191 (page_table[next_page].allocated == BOXED_PAGE_FLAG) &&
1192 page_table[next_page].large_object &&
1193 (page_table[next_page].first_object_offset ==
1194 -(next_page - first_page)*PAGE_BYTES)) {
1195 /* Checks out OK, free the page. Don't need to bother zeroing
1196 * pages as this should have been done before shrinking the
1197 * object. These pages shouldn't be write-protected as they
1198 * should be zero filled. */
1199 gc_assert(page_table[next_page].write_protected == 0);
1201 old_bytes_used = page_table[next_page].bytes_used;
1202 page_table[next_page].allocated = FREE_PAGE_FLAG;
1203 page_table[next_page].bytes_used = 0;
1204 bytes_freed += old_bytes_used;
1208 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1209 generations[new_space].bytes_allocated += 4*nwords;
1210 bytes_allocated -= bytes_freed;
1212 /* Add the region to the new_areas if requested. */
1213 add_new_area(first_page,0,nwords*4);
1217 /* Get tag of object. */
1218 tag = lowtag_of(object);
1220 /* Allocate space. */
1221 new = gc_quick_alloc_large(nwords*4);
1223 memcpy(new,native_pointer(object),nwords*4);
1225 /* Return Lisp pointer of new object. */
1226 return ((lispobj) new) | tag;
1230 /* to copy unboxed objects */
1232 copy_unboxed_object(lispobj object, int nwords)
1237 gc_assert(is_lisp_pointer(object));
1238 gc_assert(from_space_p(object));
1239 gc_assert((nwords & 0x01) == 0);
1241 /* Get tag of object. */
1242 tag = lowtag_of(object);
1244 /* Allocate space. */
1245 new = gc_quick_alloc_unboxed(nwords*4);
1247 memcpy(new,native_pointer(object),nwords*4);
1249 /* Return Lisp pointer of new object. */
1250 return ((lispobj) new) | tag;
1253 /* to copy large unboxed objects
1255 * If the object is in a large object region then it is simply
1256 * promoted, else it is copied. If it's large enough then it's copied
1257 * to a large object region.
1259 * Bignums and vectors may have shrunk. If the object is not copied
1260 * the space needs to be reclaimed, and the page_tables corrected.
1262 * KLUDGE: There's a lot of cut-and-paste duplication between this
1263 * function and copy_large_object(..). -- WHN 20000619 */
1265 copy_large_unboxed_object(lispobj object, int nwords)
1269 lispobj *source, *dest;
1272 gc_assert(is_lisp_pointer(object));
1273 gc_assert(from_space_p(object));
1274 gc_assert((nwords & 0x01) == 0);
1276 if ((nwords > 1024*1024) && gencgc_verbose)
1277 FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*4));
1279 /* Check whether it's a large object. */
1280 first_page = find_page_index((void *)object);
1281 gc_assert(first_page >= 0);
1283 if (page_table[first_page].large_object) {
1284 /* Promote the object. Note: Unboxed objects may have been
1285 * allocated to a BOXED region so it may be necessary to
1286 * change the region to UNBOXED. */
1287 int remaining_bytes;
1292 gc_assert(page_table[first_page].first_object_offset == 0);
1294 next_page = first_page;
1295 remaining_bytes = nwords*4;
1296 while (remaining_bytes > PAGE_BYTES) {
1297 gc_assert(page_table[next_page].gen == from_space);
1298 gc_assert((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1299 || (page_table[next_page].allocated == BOXED_PAGE_FLAG));
1300 gc_assert(page_table[next_page].large_object);
1301 gc_assert(page_table[next_page].first_object_offset==
1302 -PAGE_BYTES*(next_page-first_page));
1303 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
1305 page_table[next_page].gen = new_space;
1306 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1307 remaining_bytes -= PAGE_BYTES;
1311 /* Now only one page remains, but the object may have shrunk so
1312 * there may be more unused pages which will be freed. */
1314 /* Object may have shrunk but shouldn't have grown - check. */
1315 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1317 page_table[next_page].gen = new_space;
1318 page_table[next_page].allocated = UNBOXED_PAGE_FLAG;
1320 /* Adjust the bytes_used. */
1321 old_bytes_used = page_table[next_page].bytes_used;
1322 page_table[next_page].bytes_used = remaining_bytes;
1324 bytes_freed = old_bytes_used - remaining_bytes;
1326 /* Free any remaining pages; needs care. */
1328 while ((old_bytes_used == PAGE_BYTES) &&
1329 (page_table[next_page].gen == from_space) &&
1330 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
1331 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
1332 page_table[next_page].large_object &&
1333 (page_table[next_page].first_object_offset ==
1334 -(next_page - first_page)*PAGE_BYTES)) {
1335 /* Checks out OK, free the page. Don't need to both zeroing
1336 * pages as this should have been done before shrinking the
1337 * object. These pages shouldn't be write-protected, even if
1338 * boxed they should be zero filled. */
1339 gc_assert(page_table[next_page].write_protected == 0);
1341 old_bytes_used = page_table[next_page].bytes_used;
1342 page_table[next_page].allocated = FREE_PAGE_FLAG;
1343 page_table[next_page].bytes_used = 0;
1344 bytes_freed += old_bytes_used;
1348 if ((bytes_freed > 0) && gencgc_verbose)
1350 "/copy_large_unboxed bytes_freed=%d\n",
1353 generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1354 generations[new_space].bytes_allocated += 4*nwords;
1355 bytes_allocated -= bytes_freed;
1360 /* Get tag of object. */
1361 tag = lowtag_of(object);
1363 /* Allocate space. */
1364 new = gc_quick_alloc_large_unboxed(nwords*4);
1367 source = (lispobj *) native_pointer(object);
1369 /* Copy the object. */
1370 while (nwords > 0) {
1371 dest[0] = source[0];
1372 dest[1] = source[1];
1378 /* Return Lisp pointer of new object. */
1379 return ((lispobj) new) | tag;
1388 * code and code-related objects
1391 static lispobj trans_fun_header(lispobj object);
1392 static lispobj trans_boxed(lispobj object);
1395 /* Scan a x86 compiled code object, looking for possible fixups that
1396 * have been missed after a move.
1398 * Two types of fixups are needed:
1399 * 1. Absolute fixups to within the code object.
1400 * 2. Relative fixups to outside the code object.
1402 * Currently only absolute fixups to the constant vector, or to the
1403 * code area are checked. */
1405 sniff_code_object(struct code *code, unsigned displacement)
1407 int nheader_words, ncode_words, nwords;
1409 void *constants_start_addr, *constants_end_addr;
1410 void *code_start_addr, *code_end_addr;
1411 int fixup_found = 0;
1413 if (!check_code_fixups)
1416 ncode_words = fixnum_value(code->code_size);
1417 nheader_words = HeaderValue(*(lispobj *)code);
1418 nwords = ncode_words + nheader_words;
1420 constants_start_addr = (void *)code + 5*4;
1421 constants_end_addr = (void *)code + nheader_words*4;
1422 code_start_addr = (void *)code + nheader_words*4;
1423 code_end_addr = (void *)code + nwords*4;
1425 /* Work through the unboxed code. */
1426 for (p = code_start_addr; p < code_end_addr; p++) {
1427 void *data = *(void **)p;
1428 unsigned d1 = *((unsigned char *)p - 1);
1429 unsigned d2 = *((unsigned char *)p - 2);
1430 unsigned d3 = *((unsigned char *)p - 3);
1431 unsigned d4 = *((unsigned char *)p - 4);
1433 unsigned d5 = *((unsigned char *)p - 5);
1434 unsigned d6 = *((unsigned char *)p - 6);
1437 /* Check for code references. */
1438 /* Check for a 32 bit word that looks like an absolute
1439 reference to within the code adea of the code object. */
1440 if ((data >= (code_start_addr-displacement))
1441 && (data < (code_end_addr-displacement))) {
1442 /* function header */
1444 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1445 /* Skip the function header */
1449 /* the case of PUSH imm32 */
1453 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1454 p, d6, d5, d4, d3, d2, d1, data));
1455 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1457 /* the case of MOV [reg-8],imm32 */
1459 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1460 || d2==0x45 || d2==0x46 || d2==0x47)
1464 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1465 p, d6, d5, d4, d3, d2, d1, data));
1466 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1468 /* the case of LEA reg,[disp32] */
1469 if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1472 "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1473 p, d6, d5, d4, d3, d2, d1, data));
1474 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1478 /* Check for constant references. */
1479 /* Check for a 32 bit word that looks like an absolute
1480 reference to within the constant vector. Constant references
1482 if ((data >= (constants_start_addr-displacement))
1483 && (data < (constants_end_addr-displacement))
1484 && (((unsigned)data & 0x3) == 0)) {
1489 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1490 p, d6, d5, d4, d3, d2, d1, data));
1491 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1494 /* the case of MOV m32,EAX */
1498 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1499 p, d6, d5, d4, d3, d2, d1, data));
1500 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1503 /* the case of CMP m32,imm32 */
1504 if ((d1 == 0x3d) && (d2 == 0x81)) {
1507 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1508 p, d6, d5, d4, d3, d2, d1, data));
1510 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1513 /* Check for a mod=00, r/m=101 byte. */
1514 if ((d1 & 0xc7) == 5) {
1519 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1520 p, d6, d5, d4, d3, d2, d1, data));
1521 FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1523 /* the case of CMP reg32,m32 */
1527 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1528 p, d6, d5, d4, d3, d2, d1, data));
1529 FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1531 /* the case of MOV m32,reg32 */
1535 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1536 p, d6, d5, d4, d3, d2, d1, data));
1537 FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1539 /* the case of MOV reg32,m32 */
1543 "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1544 p, d6, d5, d4, d3, d2, d1, data));
1545 FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1547 /* the case of LEA reg32,m32 */
1551 "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1552 p, d6, d5, d4, d3, d2, d1, data));
1553 FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1559 /* If anything was found, print some information on the code
1563 "/compiled code object at %x: header words = %d, code words = %d\n",
1564 code, nheader_words, ncode_words));
1566 "/const start = %x, end = %x\n",
1567 constants_start_addr, constants_end_addr));
1569 "/code start = %x, end = %x\n",
1570 code_start_addr, code_end_addr));
1575 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1577 int nheader_words, ncode_words, nwords;
1578 void *constants_start_addr, *constants_end_addr;
1579 void *code_start_addr, *code_end_addr;
1580 lispobj fixups = NIL;
1581 unsigned displacement = (unsigned)new_code - (unsigned)old_code;
1582 struct vector *fixups_vector;
1584 ncode_words = fixnum_value(new_code->code_size);
1585 nheader_words = HeaderValue(*(lispobj *)new_code);
1586 nwords = ncode_words + nheader_words;
1588 "/compiled code object at %x: header words = %d, code words = %d\n",
1589 new_code, nheader_words, ncode_words)); */
1590 constants_start_addr = (void *)new_code + 5*4;
1591 constants_end_addr = (void *)new_code + nheader_words*4;
1592 code_start_addr = (void *)new_code + nheader_words*4;
1593 code_end_addr = (void *)new_code + nwords*4;
1596 "/const start = %x, end = %x\n",
1597 constants_start_addr,constants_end_addr));
1599 "/code start = %x; end = %x\n",
1600 code_start_addr,code_end_addr));
1603 /* The first constant should be a pointer to the fixups for this
1604 code objects. Check. */
1605 fixups = new_code->constants[0];
1607 /* It will be 0 or the unbound-marker if there are no fixups (as
1608 * will be the case if the code object has been purified, for
1609 * example) and will be an other pointer if it is valid. */
1610 if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1611 !is_lisp_pointer(fixups)) {
1612 /* Check for possible errors. */
1613 if (check_code_fixups)
1614 sniff_code_object(new_code, displacement);
1619 fixups_vector = (struct vector *)native_pointer(fixups);
1621 /* Could be pointing to a forwarding pointer. */
1622 /* FIXME is this always in from_space? if so, could replace this code with
1623 * forwarding_pointer_p/forwarding_pointer_value */
1624 if (is_lisp_pointer(fixups) &&
1625 (find_page_index((void*)fixups_vector) != -1) &&
1626 (fixups_vector->header == 0x01)) {
1627 /* If so, then follow it. */
1628 /*SHOW("following pointer to a forwarding pointer");*/
1629 fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1632 /*SHOW("got fixups");*/
1634 if (widetag_of(fixups_vector->header) ==
1635 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
1636 /* Got the fixups for the code block. Now work through the vector,
1637 and apply a fixup at each address. */
1638 int length = fixnum_value(fixups_vector->length);
1640 for (i = 0; i < length; i++) {
1641 unsigned offset = fixups_vector->data[i];
1642 /* Now check the current value of offset. */
1643 unsigned old_value =
1644 *(unsigned *)((unsigned)code_start_addr + offset);
1646 /* If it's within the old_code object then it must be an
1647 * absolute fixup (relative ones are not saved) */
1648 if ((old_value >= (unsigned)old_code)
1649 && (old_value < ((unsigned)old_code + nwords*4)))
1650 /* So add the dispacement. */
1651 *(unsigned *)((unsigned)code_start_addr + offset) =
1652 old_value + displacement;
1654 /* It is outside the old code object so it must be a
1655 * relative fixup (absolute fixups are not saved). So
1656 * subtract the displacement. */
1657 *(unsigned *)((unsigned)code_start_addr + offset) =
1658 old_value - displacement;
1662 /* Check for possible errors. */
1663 if (check_code_fixups) {
1664 sniff_code_object(new_code,displacement);
1670 trans_boxed_large(lispobj object)
1673 unsigned long length;
1675 gc_assert(is_lisp_pointer(object));
1677 header = *((lispobj *) native_pointer(object));
1678 length = HeaderValue(header) + 1;
1679 length = CEILING(length, 2);
1681 return copy_large_object(object, length);
1686 trans_unboxed_large(lispobj object)
1689 unsigned long length;
1692 gc_assert(is_lisp_pointer(object));
1694 header = *((lispobj *) native_pointer(object));
1695 length = HeaderValue(header) + 1;
1696 length = CEILING(length, 2);
1698 return copy_large_unboxed_object(object, length);
1703 * vector-like objects
1707 /* FIXME: What does this mean? */
1708 int gencgc_hash = 1;
1711 scav_vector(lispobj *where, lispobj object)
1713 unsigned int kv_length;
1715 unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
1716 lispobj *hash_table;
1717 lispobj empty_symbol;
1718 unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1719 unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1720 unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1722 unsigned next_vector_length = 0;
1724 /* FIXME: A comment explaining this would be nice. It looks as
1725 * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
1726 * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
1727 if (HeaderValue(object) != subtype_VectorValidHashing)
1731 /* This is set for backward compatibility. FIXME: Do we need
1734 (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
1738 kv_length = fixnum_value(where[1]);
1739 kv_vector = where + 2; /* Skip the header and length. */
1740 /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1742 /* Scavenge element 0, which may be a hash-table structure. */
1743 scavenge(where+2, 1);
1744 if (!is_lisp_pointer(where[2])) {
1745 lose("no pointer at %x in hash table", where[2]);
1747 hash_table = (lispobj *)native_pointer(where[2]);
1748 /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1749 if (widetag_of(hash_table[0]) != INSTANCE_HEADER_WIDETAG) {
1750 lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
1753 /* Scavenge element 1, which should be some internal symbol that
1754 * the hash table code reserves for marking empty slots. */
1755 scavenge(where+3, 1);
1756 if (!is_lisp_pointer(where[3])) {
1757 lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
1759 empty_symbol = where[3];
1760 /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1761 if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1762 SYMBOL_HEADER_WIDETAG) {
1763 lose("not a symbol where empty-hash-table-slot symbol expected: %x",
1764 *(lispobj *)native_pointer(empty_symbol));
1767 /* Scavenge hash table, which will fix the positions of the other
1768 * needed objects. */
1769 scavenge(hash_table, 16);
1771 /* Cross-check the kv_vector. */
1772 if (where != (lispobj *)native_pointer(hash_table[9])) {
1773 lose("hash_table table!=this table %x", hash_table[9]);
1777 weak_p_obj = hash_table[10];
1781 lispobj index_vector_obj = hash_table[13];
1783 if (is_lisp_pointer(index_vector_obj) &&
1784 (widetag_of(*(lispobj *)native_pointer(index_vector_obj)) ==
1785 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1786 index_vector = ((unsigned int *)native_pointer(index_vector_obj)) + 2;
1787 /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
1788 length = fixnum_value(((unsigned int *)native_pointer(index_vector_obj))[1]);
1789 /*FSHOW((stderr, "/length = %d\n", length));*/
1791 lose("invalid index_vector %x", index_vector_obj);
1797 lispobj next_vector_obj = hash_table[14];
1799 if (is_lisp_pointer(next_vector_obj) &&
1800 (widetag_of(*(lispobj *)native_pointer(next_vector_obj)) ==
1801 SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1802 next_vector = ((unsigned int *)native_pointer(next_vector_obj)) + 2;
1803 /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
1804 next_vector_length = fixnum_value(((unsigned int *)native_pointer(next_vector_obj))[1]);
1805 /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
1807 lose("invalid next_vector %x", next_vector_obj);
1811 /* maybe hash vector */
1813 /* FIXME: This bare "15" offset should become a symbolic
1814 * expression of some sort. And all the other bare offsets
1815 * too. And the bare "16" in scavenge(hash_table, 16). And
1816 * probably other stuff too. Ugh.. */
1817 lispobj hash_vector_obj = hash_table[15];
1819 if (is_lisp_pointer(hash_vector_obj) &&
1820 (widetag_of(*(lispobj *)native_pointer(hash_vector_obj))
1821 == SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1822 hash_vector = ((unsigned int *)native_pointer(hash_vector_obj)) + 2;
1823 /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
1824 gc_assert(fixnum_value(((unsigned int *)native_pointer(hash_vector_obj))[1])
1825 == next_vector_length);
1828 /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
1832 /* These lengths could be different as the index_vector can be a
1833 * different length from the others, a larger index_vector could help
1834 * reduce collisions. */
1835 gc_assert(next_vector_length*2 == kv_length);
1837 /* now all set up.. */
1839 /* Work through the KV vector. */
1842 for (i = 1; i < next_vector_length; i++) {
1843 lispobj old_key = kv_vector[2*i];
1844 unsigned int old_index = (old_key & 0x1fffffff)%length;
1846 /* Scavenge the key and value. */
1847 scavenge(&kv_vector[2*i],2);
1849 /* Check whether the key has moved and is EQ based. */
1851 lispobj new_key = kv_vector[2*i];
1852 unsigned int new_index = (new_key & 0x1fffffff)%length;
1854 if ((old_index != new_index) &&
1855 ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
1856 ((new_key != empty_symbol) ||
1857 (kv_vector[2*i] != empty_symbol))) {
1860 "* EQ key %d moved from %x to %x; index %d to %d\n",
1861 i, old_key, new_key, old_index, new_index));*/
1863 if (index_vector[old_index] != 0) {
1864 /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
1866 /* Unlink the key from the old_index chain. */
1867 if (index_vector[old_index] == i) {
1868 /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
1869 index_vector[old_index] = next_vector[i];
1870 /* Link it into the needing rehash chain. */
1871 next_vector[i] = fixnum_value(hash_table[11]);
1872 hash_table[11] = make_fixnum(i);
1875 unsigned prior = index_vector[old_index];
1876 unsigned next = next_vector[prior];
1878 /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
1881 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
1884 next_vector[prior] = next_vector[next];
1885 /* Link it into the needing rehash
1888 fixnum_value(hash_table[11]);
1889 hash_table[11] = make_fixnum(next);
1894 next = next_vector[next];
1902 return (CEILING(kv_length + 2, 2));
1911 /* XX This is a hack adapted from cgc.c. These don't work too
1912 * efficiently with the gencgc as a list of the weak pointers is
1913 * maintained within the objects which causes writes to the pages. A
1914 * limited attempt is made to avoid unnecessary writes, but this needs
1916 #define WEAK_POINTER_NWORDS \
1917 CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
1920 scav_weak_pointer(lispobj *where, lispobj object)
1922 struct weak_pointer *wp = weak_pointers;
1923 /* Push the weak pointer onto the list of weak pointers.
1924 * Do I have to watch for duplicates? Originally this was
1925 * part of trans_weak_pointer but that didn't work in the
1926 * case where the WP was in a promoted region.
1929 /* Check whether it's already in the list. */
1930 while (wp != NULL) {
1931 if (wp == (struct weak_pointer*)where) {
1937 /* Add it to the start of the list. */
1938 wp = (struct weak_pointer*)where;
1939 if (wp->next != weak_pointers) {
1940 wp->next = weak_pointers;
1942 /*SHOW("avoided write to weak pointer");*/
1947 /* Do not let GC scavenge the value slot of the weak pointer.
1948 * (That is why it is a weak pointer.) */
1950 return WEAK_POINTER_NWORDS;
1954 /* Scan an area looking for an object which encloses the given pointer.
1955 * Return the object start on success or NULL on failure. */
1957 search_space(lispobj *start, size_t words, lispobj *pointer)
1961 lispobj thing = *start;
1963 /* If thing is an immediate then this is a cons. */
1964 if (is_lisp_pointer(thing)
1965 || ((thing & 3) == 0) /* fixnum */
1966 || (widetag_of(thing) == BASE_CHAR_WIDETAG)
1967 || (widetag_of(thing) == UNBOUND_MARKER_WIDETAG))
1970 count = (sizetab[widetag_of(thing)])(start);
1972 /* Check whether the pointer is within this object. */
1973 if ((pointer >= start) && (pointer < (start+count))) {
1975 /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
1979 /* Round up the count. */
1980 count = CEILING(count,2);
1989 search_read_only_space(lispobj *pointer)
1991 lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
1992 lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0);
1993 if ((pointer < start) || (pointer >= end))
1995 return (search_space(start, (pointer+2)-start, pointer));
1999 search_static_space(lispobj *pointer)
2001 lispobj* start = (lispobj*)STATIC_SPACE_START;
2002 lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0);
2003 if ((pointer < start) || (pointer >= end))
2005 return (search_space(start, (pointer+2)-start, pointer));
2008 /* a faster version for searching the dynamic space. This will work even
2009 * if the object is in a current allocation region. */
2011 search_dynamic_space(lispobj *pointer)
2013 int page_index = find_page_index(pointer);
2016 /* The address may be invalid, so do some checks. */
2017 if ((page_index == -1) ||
2018 (page_table[page_index].allocated == FREE_PAGE_FLAG))
2020 start = (lispobj *)((void *)page_address(page_index)
2021 + page_table[page_index].first_object_offset);
2022 return (search_space(start, (pointer+2)-start, pointer));
2025 /* Is there any possibility that pointer is a valid Lisp object
2026 * reference, and/or something else (e.g. subroutine call return
2027 * address) which should prevent us from moving the referred-to thing?
2028 * This is called from preserve_pointers() */
2030 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2032 lispobj *start_addr;
2034 /* Find the object start address. */
2035 if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2039 /* We need to allow raw pointers into Code objects for return
2040 * addresses. This will also pick up pointers to functions in code
2042 if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
2043 /* XXX could do some further checks here */
2047 /* If it's not a return address then it needs to be a valid Lisp
2049 if (!is_lisp_pointer((lispobj)pointer)) {
2053 /* Check that the object pointed to is consistent with the pointer
2056 switch (lowtag_of((lispobj)pointer)) {
2057 case FUN_POINTER_LOWTAG:
2058 /* Start_addr should be the enclosing code object, or a closure
2060 switch (widetag_of(*start_addr)) {
2061 case CODE_HEADER_WIDETAG:
2062 /* This case is probably caught above. */
2064 case CLOSURE_HEADER_WIDETAG:
2065 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2066 if ((unsigned)pointer !=
2067 ((unsigned)start_addr+FUN_POINTER_LOWTAG)) {
2071 pointer, start_addr, *start_addr));
2079 pointer, start_addr, *start_addr));
2083 case LIST_POINTER_LOWTAG:
2084 if ((unsigned)pointer !=
2085 ((unsigned)start_addr+LIST_POINTER_LOWTAG)) {
2089 pointer, start_addr, *start_addr));
2092 /* Is it plausible cons? */
2093 if ((is_lisp_pointer(start_addr[0])
2094 || ((start_addr[0] & 3) == 0) /* fixnum */
2095 || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
2096 || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2097 && (is_lisp_pointer(start_addr[1])
2098 || ((start_addr[1] & 3) == 0) /* fixnum */
2099 || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
2100 || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2106 pointer, start_addr, *start_addr));
2109 case INSTANCE_POINTER_LOWTAG:
2110 if ((unsigned)pointer !=
2111 ((unsigned)start_addr+INSTANCE_POINTER_LOWTAG)) {
2115 pointer, start_addr, *start_addr));
2118 if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2122 pointer, start_addr, *start_addr));
2126 case OTHER_POINTER_LOWTAG:
2127 if ((unsigned)pointer !=
2128 ((int)start_addr+OTHER_POINTER_LOWTAG)) {
2132 pointer, start_addr, *start_addr));
2135 /* Is it plausible? Not a cons. XXX should check the headers. */
2136 if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2140 pointer, start_addr, *start_addr));
2143 switch (widetag_of(start_addr[0])) {
2144 case UNBOUND_MARKER_WIDETAG:
2145 case BASE_CHAR_WIDETAG:
2149 pointer, start_addr, *start_addr));
2152 /* only pointed to by function pointers? */
2153 case CLOSURE_HEADER_WIDETAG:
2154 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2158 pointer, start_addr, *start_addr));
2161 case INSTANCE_HEADER_WIDETAG:
2165 pointer, start_addr, *start_addr));
2168 /* the valid other immediate pointer objects */
2169 case SIMPLE_VECTOR_WIDETAG:
2171 case COMPLEX_WIDETAG:
2172 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2173 case COMPLEX_SINGLE_FLOAT_WIDETAG:
2175 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2176 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2178 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2179 case COMPLEX_LONG_FLOAT_WIDETAG:
2181 case SIMPLE_ARRAY_WIDETAG:
2182 case COMPLEX_BASE_STRING_WIDETAG:
2183 case COMPLEX_VECTOR_NIL_WIDETAG:
2184 case COMPLEX_BIT_VECTOR_WIDETAG:
2185 case COMPLEX_VECTOR_WIDETAG:
2186 case COMPLEX_ARRAY_WIDETAG:
2187 case VALUE_CELL_HEADER_WIDETAG:
2188 case SYMBOL_HEADER_WIDETAG:
2190 case CODE_HEADER_WIDETAG:
2191 case BIGNUM_WIDETAG:
2192 case SINGLE_FLOAT_WIDETAG:
2193 case DOUBLE_FLOAT_WIDETAG:
2194 #ifdef LONG_FLOAT_WIDETAG
2195 case LONG_FLOAT_WIDETAG:
2197 case SIMPLE_BASE_STRING_WIDETAG:
2198 case SIMPLE_BIT_VECTOR_WIDETAG:
2199 case SIMPLE_ARRAY_NIL_WIDETAG:
2200 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2201 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2202 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2203 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2204 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2205 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2206 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2207 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2208 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2209 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2210 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2212 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2213 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2215 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2216 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2218 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2219 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2221 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2222 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2223 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2224 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2226 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2227 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2229 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2230 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2232 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2233 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2236 case WEAK_POINTER_WIDETAG:
2243 pointer, start_addr, *start_addr));
2251 pointer, start_addr, *start_addr));
2259 /* Adjust large bignum and vector objects. This will adjust the
2260 * allocated region if the size has shrunk, and move unboxed objects
2261 * into unboxed pages. The pages are not promoted here, and the
2262 * promoted region is not added to the new_regions; this is really
2263 * only designed to be called from preserve_pointer(). Shouldn't fail
2264 * if this is missed, just may delay the moving of objects to unboxed
2265 * pages, and the freeing of pages. */
2267 maybe_adjust_large_object(lispobj *where)
2272 int remaining_bytes;
2279 /* Check whether it's a vector or bignum object. */
2280 switch (widetag_of(where[0])) {
2281 case SIMPLE_VECTOR_WIDETAG:
2282 boxed = BOXED_PAGE_FLAG;
2284 case BIGNUM_WIDETAG:
2285 case SIMPLE_BASE_STRING_WIDETAG:
2286 case SIMPLE_BIT_VECTOR_WIDETAG:
2287 case SIMPLE_ARRAY_NIL_WIDETAG:
2288 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2289 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2290 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
2291 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2292 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
2293 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2294 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
2295 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
2296 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2297 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2298 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2300 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2301 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2303 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2304 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2306 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2307 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2309 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2310 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2311 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2312 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2314 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2315 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2317 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2318 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2320 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2321 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2323 boxed = UNBOXED_PAGE_FLAG;
2329 /* Find its current size. */
2330 nwords = (sizetab[widetag_of(where[0])])(where);
2332 first_page = find_page_index((void *)where);
2333 gc_assert(first_page >= 0);
2335 /* Note: Any page write-protection must be removed, else a later
2336 * scavenge_newspace may incorrectly not scavenge these pages.
2337 * This would not be necessary if they are added to the new areas,
2338 * but lets do it for them all (they'll probably be written
2341 gc_assert(page_table[first_page].first_object_offset == 0);
2343 next_page = first_page;
2344 remaining_bytes = nwords*4;
2345 while (remaining_bytes > PAGE_BYTES) {
2346 gc_assert(page_table[next_page].gen == from_space);
2347 gc_assert((page_table[next_page].allocated == BOXED_PAGE_FLAG)
2348 || (page_table[next_page].allocated == UNBOXED_PAGE_FLAG));
2349 gc_assert(page_table[next_page].large_object);
2350 gc_assert(page_table[next_page].first_object_offset ==
2351 -PAGE_BYTES*(next_page-first_page));
2352 gc_assert(page_table[next_page].bytes_used == PAGE_BYTES);
2354 page_table[next_page].allocated = boxed;
2356 /* Shouldn't be write-protected at this stage. Essential that the
2358 gc_assert(!page_table[next_page].write_protected);
2359 remaining_bytes -= PAGE_BYTES;
2363 /* Now only one page remains, but the object may have shrunk so
2364 * there may be more unused pages which will be freed. */
2366 /* Object may have shrunk but shouldn't have grown - check. */
2367 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2369 page_table[next_page].allocated = boxed;
2370 gc_assert(page_table[next_page].allocated ==
2371 page_table[first_page].allocated);
2373 /* Adjust the bytes_used. */
2374 old_bytes_used = page_table[next_page].bytes_used;
2375 page_table[next_page].bytes_used = remaining_bytes;
2377 bytes_freed = old_bytes_used - remaining_bytes;
2379 /* Free any remaining pages; needs care. */
2381 while ((old_bytes_used == PAGE_BYTES) &&
2382 (page_table[next_page].gen == from_space) &&
2383 ((page_table[next_page].allocated == UNBOXED_PAGE_FLAG)
2384 || (page_table[next_page].allocated == BOXED_PAGE_FLAG)) &&
2385 page_table[next_page].large_object &&
2386 (page_table[next_page].first_object_offset ==
2387 -(next_page - first_page)*PAGE_BYTES)) {
2388 /* It checks out OK, free the page. We don't need to both zeroing
2389 * pages as this should have been done before shrinking the
2390 * object. These pages shouldn't be write protected as they
2391 * should be zero filled. */
2392 gc_assert(page_table[next_page].write_protected == 0);
2394 old_bytes_used = page_table[next_page].bytes_used;
2395 page_table[next_page].allocated = FREE_PAGE_FLAG;
2396 page_table[next_page].bytes_used = 0;
2397 bytes_freed += old_bytes_used;
2401 if ((bytes_freed > 0) && gencgc_verbose) {
2403 "/maybe_adjust_large_object() freed %d\n",
2407 generations[from_space].bytes_allocated -= bytes_freed;
2408 bytes_allocated -= bytes_freed;
2413 /* Take a possible pointer to a Lisp object and mark its page in the
2414 * page_table so that it will not be relocated during a GC.
2416 * This involves locating the page it points to, then backing up to
2417 * the start of its region, then marking all pages dont_move from there
2418 * up to the first page that's not full or has a different generation
2420 * It is assumed that all the page static flags have been cleared at
2421 * the start of a GC.
2423 * It is also assumed that the current gc_alloc() region has been
2424 * flushed and the tables updated. */
2426 preserve_pointer(void *addr)
2428 int addr_page_index = find_page_index(addr);
2431 unsigned region_allocation;
2433 /* quick check 1: Address is quite likely to have been invalid. */
2434 if ((addr_page_index == -1)
2435 || (page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2436 || (page_table[addr_page_index].bytes_used == 0)
2437 || (page_table[addr_page_index].gen != from_space)
2438 /* Skip if already marked dont_move. */
2439 || (page_table[addr_page_index].dont_move != 0))
2441 gc_assert(!(page_table[addr_page_index].allocated&OPEN_REGION_PAGE_FLAG));
2442 /* (Now that we know that addr_page_index is in range, it's
2443 * safe to index into page_table[] with it.) */
2444 region_allocation = page_table[addr_page_index].allocated;
2446 /* quick check 2: Check the offset within the page.
2449 if (((unsigned)addr & (PAGE_BYTES - 1)) > page_table[addr_page_index].bytes_used)
2452 /* Filter out anything which can't be a pointer to a Lisp object
2453 * (or, as a special case which also requires dont_move, a return
2454 * address referring to something in a CodeObject). This is
2455 * expensive but important, since it vastly reduces the
2456 * probability that random garbage will be bogusly interpreted as
2457 * a pointer which prevents a page from moving. */
2458 if (!(possibly_valid_dynamic_space_pointer(addr)))
2461 /* Find the beginning of the region. Note that there may be
2462 * objects in the region preceding the one that we were passed a
2463 * pointer to: if this is the case, we will write-protect all the
2464 * previous objects' pages too. */
2467 /* I think this'd work just as well, but without the assertions.
2468 * -dan 2004.01.01 */
2470 find_page_index(page_address(addr_page_index)+
2471 page_table[addr_page_index].first_object_offset);
2473 first_page = addr_page_index;
2474 while (page_table[first_page].first_object_offset != 0) {
2476 /* Do some checks. */
2477 gc_assert(page_table[first_page].bytes_used == PAGE_BYTES);
2478 gc_assert(page_table[first_page].gen == from_space);
2479 gc_assert(page_table[first_page].allocated == region_allocation);
2483 /* Adjust any large objects before promotion as they won't be
2484 * copied after promotion. */
2485 if (page_table[first_page].large_object) {
2486 maybe_adjust_large_object(page_address(first_page));
2487 /* If a large object has shrunk then addr may now point to a
2488 * free area in which case it's ignored here. Note it gets
2489 * through the valid pointer test above because the tail looks
2491 if ((page_table[addr_page_index].allocated == FREE_PAGE_FLAG)
2492 || (page_table[addr_page_index].bytes_used == 0)
2493 /* Check the offset within the page. */
2494 || (((unsigned)addr & (PAGE_BYTES - 1))
2495 > page_table[addr_page_index].bytes_used)) {
2497 "weird? ignore ptr 0x%x to freed area of large object\n",
2501 /* It may have moved to unboxed pages. */
2502 region_allocation = page_table[first_page].allocated;
2505 /* Now work forward until the end of this contiguous area is found,
2506 * marking all pages as dont_move. */
2507 for (i = first_page; ;i++) {
2508 gc_assert(page_table[i].allocated == region_allocation);
2510 /* Mark the page static. */
2511 page_table[i].dont_move = 1;
2513 /* Move the page to the new_space. XX I'd rather not do this
2514 * but the GC logic is not quite able to copy with the static
2515 * pages remaining in the from space. This also requires the
2516 * generation bytes_allocated counters be updated. */
2517 page_table[i].gen = new_space;
2518 generations[new_space].bytes_allocated += page_table[i].bytes_used;
2519 generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2521 /* It is essential that the pages are not write protected as
2522 * they may have pointers into the old-space which need
2523 * scavenging. They shouldn't be write protected at this
2525 gc_assert(!page_table[i].write_protected);
2527 /* Check whether this is the last page in this contiguous block.. */
2528 if ((page_table[i].bytes_used < PAGE_BYTES)
2529 /* ..or it is PAGE_BYTES and is the last in the block */
2530 || (page_table[i+1].allocated == FREE_PAGE_FLAG)
2531 || (page_table[i+1].bytes_used == 0) /* next page free */
2532 || (page_table[i+1].gen != from_space) /* diff. gen */
2533 || (page_table[i+1].first_object_offset == 0))
2537 /* Check that the page is now static. */
2538 gc_assert(page_table[addr_page_index].dont_move != 0);
2541 /* If the given page is not write-protected, then scan it for pointers
2542 * to younger generations or the top temp. generation, if no
2543 * suspicious pointers are found then the page is write-protected.
2545 * Care is taken to check for pointers to the current gc_alloc()
2546 * region if it is a younger generation or the temp. generation. This
2547 * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2548 * the gc_alloc_generation does not need to be checked as this is only
2549 * called from scavenge_generation() when the gc_alloc generation is
2550 * younger, so it just checks if there is a pointer to the current
2553 * We return 1 if the page was write-protected, else 0. */
2555 update_page_write_prot(int page)
2557 int gen = page_table[page].gen;
2560 void **page_addr = (void **)page_address(page);
2561 int num_words = page_table[page].bytes_used / 4;
2563 /* Shouldn't be a free page. */
2564 gc_assert(page_table[page].allocated != FREE_PAGE_FLAG);
2565 gc_assert(page_table[page].bytes_used != 0);
2567 /* Skip if it's already write-protected, pinned, or unboxed */
2568 if (page_table[page].write_protected
2569 || page_table[page].dont_move
2570 || (page_table[page].allocated & UNBOXED_PAGE_FLAG))
2573 /* Scan the page for pointers to younger generations or the
2574 * top temp. generation. */
2576 for (j = 0; j < num_words; j++) {
2577 void *ptr = *(page_addr+j);
2578 int index = find_page_index(ptr);
2580 /* Check that it's in the dynamic space */
2582 if (/* Does it point to a younger or the temp. generation? */
2583 ((page_table[index].allocated != FREE_PAGE_FLAG)
2584 && (page_table[index].bytes_used != 0)
2585 && ((page_table[index].gen < gen)
2586 || (page_table[index].gen == NUM_GENERATIONS)))
2588 /* Or does it point within a current gc_alloc() region? */
2589 || ((boxed_region.start_addr <= ptr)
2590 && (ptr <= boxed_region.free_pointer))
2591 || ((unboxed_region.start_addr <= ptr)
2592 && (ptr <= unboxed_region.free_pointer))) {
2599 /* Write-protect the page. */
2600 /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2602 os_protect((void *)page_addr,
2604 OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2606 /* Note the page as protected in the page tables. */
2607 page_table[page].write_protected = 1;
2613 /* Scavenge a generation.
2615 * This will not resolve all pointers when generation is the new
2616 * space, as new objects may be added which are not checked here - use
2617 * scavenge_newspace generation.
2619 * Write-protected pages should not have any pointers to the
2620 * from_space so do need scavenging; thus write-protected pages are
2621 * not always scavenged. There is some code to check that these pages
2622 * are not written; but to check fully the write-protected pages need
2623 * to be scavenged by disabling the code to skip them.
2625 * Under the current scheme when a generation is GCed the younger
2626 * generations will be empty. So, when a generation is being GCed it
2627 * is only necessary to scavenge the older generations for pointers
2628 * not the younger. So a page that does not have pointers to younger
2629 * generations does not need to be scavenged.
2631 * The write-protection can be used to note pages that don't have
2632 * pointers to younger pages. But pages can be written without having
2633 * pointers to younger generations. After the pages are scavenged here
2634 * they can be scanned for pointers to younger generations and if
2635 * there are none the page can be write-protected.
2637 * One complication is when the newspace is the top temp. generation.
2639 * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2640 * that none were written, which they shouldn't be as they should have
2641 * no pointers to younger generations. This breaks down for weak
2642 * pointers as the objects contain a link to the next and are written
2643 * if a weak pointer is scavenged. Still it's a useful check. */
2645 scavenge_generation(int generation)
2652 /* Clear the write_protected_cleared flags on all pages. */
2653 for (i = 0; i < NUM_PAGES; i++)
2654 page_table[i].write_protected_cleared = 0;
2657 for (i = 0; i < last_free_page; i++) {
2658 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2659 && (page_table[i].bytes_used != 0)
2660 && (page_table[i].gen == generation)) {
2662 int write_protected=1;
2664 /* This should be the start of a region */
2665 gc_assert(page_table[i].first_object_offset == 0);
2667 /* Now work forward until the end of the region */
2668 for (last_page = i; ; last_page++) {
2670 write_protected && page_table[last_page].write_protected;
2671 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2672 /* Or it is PAGE_BYTES and is the last in the block */
2673 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2674 || (page_table[last_page+1].bytes_used == 0)
2675 || (page_table[last_page+1].gen != generation)
2676 || (page_table[last_page+1].first_object_offset == 0))
2679 if (!write_protected) {
2680 scavenge(page_address(i), (page_table[last_page].bytes_used
2681 + (last_page-i)*PAGE_BYTES)/4);
2683 /* Now scan the pages and write protect those that
2684 * don't have pointers to younger generations. */
2685 if (enable_page_protection) {
2686 for (j = i; j <= last_page; j++) {
2687 num_wp += update_page_write_prot(j);
2694 if ((gencgc_verbose > 1) && (num_wp != 0)) {
2696 "/write protected %d pages within generation %d\n",
2697 num_wp, generation));
2701 /* Check that none of the write_protected pages in this generation
2702 * have been written to. */
2703 for (i = 0; i < NUM_PAGES; i++) {
2704 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2705 && (page_table[i].bytes_used != 0)
2706 && (page_table[i].gen == generation)
2707 && (page_table[i].write_protected_cleared != 0)) {
2708 FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2710 "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2711 page_table[i].bytes_used,
2712 page_table[i].first_object_offset,
2713 page_table[i].dont_move));
2714 lose("write to protected page %d in scavenge_generation()", i);
2721 /* Scavenge a newspace generation. As it is scavenged new objects may
2722 * be allocated to it; these will also need to be scavenged. This
2723 * repeats until there are no more objects unscavenged in the
2724 * newspace generation.
2726 * To help improve the efficiency, areas written are recorded by
2727 * gc_alloc() and only these scavenged. Sometimes a little more will be
2728 * scavenged, but this causes no harm. An easy check is done that the
2729 * scavenged bytes equals the number allocated in the previous
2732 * Write-protected pages are not scanned except if they are marked
2733 * dont_move in which case they may have been promoted and still have
2734 * pointers to the from space.
2736 * Write-protected pages could potentially be written by alloc however
2737 * to avoid having to handle re-scavenging of write-protected pages
2738 * gc_alloc() does not write to write-protected pages.
2740 * New areas of objects allocated are recorded alternatively in the two
2741 * new_areas arrays below. */
2742 static struct new_area new_areas_1[NUM_NEW_AREAS];
2743 static struct new_area new_areas_2[NUM_NEW_AREAS];
2745 /* Do one full scan of the new space generation. This is not enough to
2746 * complete the job as new objects may be added to the generation in
2747 * the process which are not scavenged. */
2749 scavenge_newspace_generation_one_scan(int generation)
2754 "/starting one full scan of newspace generation %d\n",
2756 for (i = 0; i < last_free_page; i++) {
2757 /* Note that this skips over open regions when it encounters them. */
2758 if ((page_table[i].allocated & BOXED_PAGE_FLAG)
2759 && (page_table[i].bytes_used != 0)
2760 && (page_table[i].gen == generation)
2761 && ((page_table[i].write_protected == 0)
2762 /* (This may be redundant as write_protected is now
2763 * cleared before promotion.) */
2764 || (page_table[i].dont_move == 1))) {
2768 /* The scavenge will start at the first_object_offset of page i.
2770 * We need to find the full extent of this contiguous
2771 * block in case objects span pages.
2773 * Now work forward until the end of this contiguous area
2774 * is found. A small area is preferred as there is a
2775 * better chance of its pages being write-protected. */
2776 for (last_page = i; ;last_page++) {
2777 /* If all pages are write-protected and movable,
2778 * then no need to scavenge */
2779 all_wp=all_wp && page_table[last_page].write_protected &&
2780 !page_table[last_page].dont_move;
2782 /* Check whether this is the last page in this
2783 * contiguous block */
2784 if ((page_table[last_page].bytes_used < PAGE_BYTES)
2785 /* Or it is PAGE_BYTES and is the last in the block */
2786 || (!(page_table[last_page+1].allocated & BOXED_PAGE_FLAG))
2787 || (page_table[last_page+1].bytes_used == 0)
2788 || (page_table[last_page+1].gen != generation)
2789 || (page_table[last_page+1].first_object_offset == 0))
2793 /* Do a limited check for write-protected pages. */
2797 size = (page_table[last_page].bytes_used
2798 + (last_page-i)*PAGE_BYTES
2799 - page_table[i].first_object_offset)/4;
2800 new_areas_ignore_page = last_page;
2802 scavenge(page_address(i) +
2803 page_table[i].first_object_offset,
2811 "/done with one full scan of newspace generation %d\n",
2815 /* Do a complete scavenge of the newspace generation. */
2817 scavenge_newspace_generation(int generation)
2821 /* the new_areas array currently being written to by gc_alloc() */
2822 struct new_area (*current_new_areas)[] = &new_areas_1;
2823 int current_new_areas_index;
2825 /* the new_areas created by the previous scavenge cycle */
2826 struct new_area (*previous_new_areas)[] = NULL;
2827 int previous_new_areas_index;
2829 /* Flush the current regions updating the tables. */
2830 gc_alloc_update_all_page_tables();
2832 /* Turn on the recording of new areas by gc_alloc(). */
2833 new_areas = current_new_areas;
2834 new_areas_index = 0;
2836 /* Don't need to record new areas that get scavenged anyway during
2837 * scavenge_newspace_generation_one_scan. */
2838 record_new_objects = 1;
2840 /* Start with a full scavenge. */
2841 scavenge_newspace_generation_one_scan(generation);
2843 /* Record all new areas now. */
2844 record_new_objects = 2;
2846 /* Flush the current regions updating the tables. */
2847 gc_alloc_update_all_page_tables();
2849 /* Grab new_areas_index. */
2850 current_new_areas_index = new_areas_index;
2853 "The first scan is finished; current_new_areas_index=%d.\n",
2854 current_new_areas_index));*/
2856 while (current_new_areas_index > 0) {
2857 /* Move the current to the previous new areas */
2858 previous_new_areas = current_new_areas;
2859 previous_new_areas_index = current_new_areas_index;
2861 /* Scavenge all the areas in previous new areas. Any new areas
2862 * allocated are saved in current_new_areas. */
2864 /* Allocate an array for current_new_areas; alternating between
2865 * new_areas_1 and 2 */
2866 if (previous_new_areas == &new_areas_1)
2867 current_new_areas = &new_areas_2;
2869 current_new_areas = &new_areas_1;
2871 /* Set up for gc_alloc(). */
2872 new_areas = current_new_areas;
2873 new_areas_index = 0;
2875 /* Check whether previous_new_areas had overflowed. */
2876 if (previous_new_areas_index >= NUM_NEW_AREAS) {
2878 /* New areas of objects allocated have been lost so need to do a
2879 * full scan to be sure! If this becomes a problem try
2880 * increasing NUM_NEW_AREAS. */
2882 SHOW("new_areas overflow, doing full scavenge");
2884 /* Don't need to record new areas that get scavenge anyway
2885 * during scavenge_newspace_generation_one_scan. */
2886 record_new_objects = 1;
2888 scavenge_newspace_generation_one_scan(generation);
2890 /* Record all new areas now. */
2891 record_new_objects = 2;
2893 /* Flush the current regions updating the tables. */
2894 gc_alloc_update_all_page_tables();
2898 /* Work through previous_new_areas. */
2899 for (i = 0; i < previous_new_areas_index; i++) {
2900 /* FIXME: All these bare *4 and /4 should be something
2901 * like BYTES_PER_WORD or WBYTES. */
2902 int page = (*previous_new_areas)[i].page;
2903 int offset = (*previous_new_areas)[i].offset;
2904 int size = (*previous_new_areas)[i].size / 4;
2905 gc_assert((*previous_new_areas)[i].size % 4 == 0);
2906 scavenge(page_address(page)+offset, size);
2909 /* Flush the current regions updating the tables. */
2910 gc_alloc_update_all_page_tables();
2913 current_new_areas_index = new_areas_index;
2916 "The re-scan has finished; current_new_areas_index=%d.\n",
2917 current_new_areas_index));*/
2920 /* Turn off recording of areas allocated by gc_alloc(). */
2921 record_new_objects = 0;
2924 /* Check that none of the write_protected pages in this generation
2925 * have been written to. */
2926 for (i = 0; i < NUM_PAGES; i++) {
2927 if ((page_table[i].allocation != FREE_PAGE_FLAG)
2928 && (page_table[i].bytes_used != 0)
2929 && (page_table[i].gen == generation)
2930 && (page_table[i].write_protected_cleared != 0)
2931 && (page_table[i].dont_move == 0)) {
2932 lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
2933 i, generation, page_table[i].dont_move);
2939 /* Un-write-protect all the pages in from_space. This is done at the
2940 * start of a GC else there may be many page faults while scavenging
2941 * the newspace (I've seen drive the system time to 99%). These pages
2942 * would need to be unprotected anyway before unmapping in
2943 * free_oldspace; not sure what effect this has on paging.. */
2945 unprotect_oldspace(void)
2949 for (i = 0; i < last_free_page; i++) {
2950 if ((page_table[i].allocated != FREE_PAGE_FLAG)
2951 && (page_table[i].bytes_used != 0)
2952 && (page_table[i].gen == from_space)) {
2955 page_start = (void *)page_address(i);
2957 /* Remove any write-protection. We should be able to rely
2958 * on the write-protect flag to avoid redundant calls. */
2959 if (page_table[i].write_protected) {
2960 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
2961 page_table[i].write_protected = 0;
2967 /* Work through all the pages and free any in from_space. This
2968 * assumes that all objects have been copied or promoted to an older
2969 * generation. Bytes_allocated and the generation bytes_allocated
2970 * counter are updated. The number of bytes freed is returned. */
2974 int bytes_freed = 0;
2975 int first_page, last_page;
2980 /* Find a first page for the next region of pages. */
2981 while ((first_page < last_free_page)
2982 && ((page_table[first_page].allocated == FREE_PAGE_FLAG)
2983 || (page_table[first_page].bytes_used == 0)
2984 || (page_table[first_page].gen != from_space)))
2987 if (first_page >= last_free_page)
2990 /* Find the last page of this region. */
2991 last_page = first_page;
2994 /* Free the page. */
2995 bytes_freed += page_table[last_page].bytes_used;
2996 generations[page_table[last_page].gen].bytes_allocated -=
2997 page_table[last_page].bytes_used;
2998 page_table[last_page].allocated = FREE_PAGE_FLAG;
2999 page_table[last_page].bytes_used = 0;
3001 /* Remove any write-protection. We should be able to rely
3002 * on the write-protect flag to avoid redundant calls. */
3004 void *page_start = (void *)page_address(last_page);
3006 if (page_table[last_page].write_protected) {
3007 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3008 page_table[last_page].write_protected = 0;
3013 while ((last_page < last_free_page)
3014 && (page_table[last_page].allocated != FREE_PAGE_FLAG)
3015 && (page_table[last_page].bytes_used != 0)
3016 && (page_table[last_page].gen == from_space));
3018 /* Zero pages from first_page to (last_page-1).
3020 * FIXME: Why not use os_zero(..) function instead of
3021 * hand-coding this again? (Check other gencgc_unmap_zero
3023 if (gencgc_unmap_zero) {
3024 void *page_start, *addr;
3026 page_start = (void *)page_address(first_page);
3028 os_invalidate(page_start, PAGE_BYTES*(last_page-first_page));
3029 addr = os_validate(page_start, PAGE_BYTES*(last_page-first_page));
3030 if (addr == NULL || addr != page_start) {
3031 lose("free_oldspace: page moved, 0x%08x ==> 0x%08x",page_start,
3037 page_start = (int *)page_address(first_page);
3038 memset(page_start, 0,PAGE_BYTES*(last_page-first_page));
3041 first_page = last_page;
3043 } while (first_page < last_free_page);
3045 bytes_allocated -= bytes_freed;
3050 /* Print some information about a pointer at the given address. */
3052 print_ptr(lispobj *addr)
3054 /* If addr is in the dynamic space then out the page information. */
3055 int pi1 = find_page_index((void*)addr);
3058 fprintf(stderr," %x: page %d alloc %d gen %d bytes_used %d offset %d dont_move %d\n",
3059 (unsigned int) addr,
3061 page_table[pi1].allocated,
3062 page_table[pi1].gen,
3063 page_table[pi1].bytes_used,
3064 page_table[pi1].first_object_offset,
3065 page_table[pi1].dont_move);
3066 fprintf(stderr," %x %x %x %x (%x) %x %x %x %x\n",
3079 extern int undefined_tramp;
3082 verify_space(lispobj *start, size_t words)
3084 int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3085 int is_in_readonly_space =
3086 (READ_ONLY_SPACE_START <= (unsigned)start &&
3087 (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3091 lispobj thing = *(lispobj*)start;
3093 if (is_lisp_pointer(thing)) {
3094 int page_index = find_page_index((void*)thing);
3095 int to_readonly_space =
3096 (READ_ONLY_SPACE_START <= thing &&
3097 thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0));
3098 int to_static_space =
3099 (STATIC_SPACE_START <= thing &&
3100 thing < SymbolValue(STATIC_SPACE_FREE_POINTER,0));
3102 /* Does it point to the dynamic space? */
3103 if (page_index != -1) {
3104 /* If it's within the dynamic space it should point to a used
3105 * page. XX Could check the offset too. */
3106 if ((page_table[page_index].allocated != FREE_PAGE_FLAG)
3107 && (page_table[page_index].bytes_used == 0))
3108 lose ("Ptr %x @ %x sees free page.", thing, start);
3109 /* Check that it doesn't point to a forwarding pointer! */
3110 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3111 lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
3113 /* Check that its not in the RO space as it would then be a
3114 * pointer from the RO to the dynamic space. */
3115 if (is_in_readonly_space) {
3116 lose("ptr to dynamic space %x from RO space %x",
3119 /* Does it point to a plausible object? This check slows
3120 * it down a lot (so it's commented out).
3122 * "a lot" is serious: it ate 50 minutes cpu time on
3123 * my duron 950 before I came back from lunch and
3126 * FIXME: Add a variable to enable this
3129 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3130 lose("ptr %x to invalid object %x", thing, start);
3134 /* Verify that it points to another valid space. */
3135 if (!to_readonly_space && !to_static_space
3136 && (thing != (unsigned)&undefined_tramp)) {
3137 lose("Ptr %x @ %x sees junk.", thing, start);
3141 if (!(fixnump(thing))) {
3143 switch(widetag_of(*start)) {
3146 case SIMPLE_VECTOR_WIDETAG:
3148 case COMPLEX_WIDETAG:
3149 case SIMPLE_ARRAY_WIDETAG:
3150 case COMPLEX_BASE_STRING_WIDETAG:
3151 case COMPLEX_VECTOR_NIL_WIDETAG:
3152 case COMPLEX_BIT_VECTOR_WIDETAG:
3153 case COMPLEX_VECTOR_WIDETAG:
3154 case COMPLEX_ARRAY_WIDETAG:
3155 case CLOSURE_HEADER_WIDETAG:
3156 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3157 case VALUE_CELL_HEADER_WIDETAG:
3158 case SYMBOL_HEADER_WIDETAG:
3159 case BASE_CHAR_WIDETAG:
3160 case UNBOUND_MARKER_WIDETAG:
3161 case INSTANCE_HEADER_WIDETAG:
3166 case CODE_HEADER_WIDETAG:
3168 lispobj object = *start;
3170 int nheader_words, ncode_words, nwords;
3172 struct simple_fun *fheaderp;
3174 code = (struct code *) start;
3176 /* Check that it's not in the dynamic space.
3177 * FIXME: Isn't is supposed to be OK for code
3178 * objects to be in the dynamic space these days? */
3179 if (is_in_dynamic_space
3180 /* It's ok if it's byte compiled code. The trace
3181 * table offset will be a fixnum if it's x86
3182 * compiled code - check.
3184 * FIXME: #^#@@! lack of abstraction here..
3185 * This line can probably go away now that
3186 * there's no byte compiler, but I've got
3187 * too much to worry about right now to try
3188 * to make sure. -- WHN 2001-10-06 */
3189 && fixnump(code->trace_table_offset)
3190 /* Only when enabled */
3191 && verify_dynamic_code_check) {
3193 "/code object at %x in the dynamic space\n",
3197 ncode_words = fixnum_value(code->code_size);
3198 nheader_words = HeaderValue(object);
3199 nwords = ncode_words + nheader_words;
3200 nwords = CEILING(nwords, 2);
3201 /* Scavenge the boxed section of the code data block */
3202 verify_space(start + 1, nheader_words - 1);
3204 /* Scavenge the boxed section of each function
3205 * object in the code data block. */
3206 fheaderl = code->entry_points;
3207 while (fheaderl != NIL) {
3209 (struct simple_fun *) native_pointer(fheaderl);
3210 gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3211 verify_space(&fheaderp->name, 1);
3212 verify_space(&fheaderp->arglist, 1);
3213 verify_space(&fheaderp->type, 1);
3214 fheaderl = fheaderp->next;
3220 /* unboxed objects */
3221 case BIGNUM_WIDETAG:
3222 case SINGLE_FLOAT_WIDETAG:
3223 case DOUBLE_FLOAT_WIDETAG:
3224 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3225 case LONG_FLOAT_WIDETAG:
3227 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3228 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3230 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3231 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3233 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3234 case COMPLEX_LONG_FLOAT_WIDETAG:
3236 case SIMPLE_BASE_STRING_WIDETAG:
3237 case SIMPLE_BIT_VECTOR_WIDETAG:
3238 case SIMPLE_ARRAY_NIL_WIDETAG:
3239 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3240 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3241 case SIMPLE_ARRAY_UNSIGNED_BYTE_7_WIDETAG:
3242 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3243 case SIMPLE_ARRAY_UNSIGNED_BYTE_15_WIDETAG:
3244 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3245 case SIMPLE_ARRAY_UNSIGNED_BYTE_29_WIDETAG:
3246 case SIMPLE_ARRAY_UNSIGNED_BYTE_31_WIDETAG:
3247 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3248 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3249 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3251 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3252 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3254 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3255 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3257 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3258 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3260 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3261 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3262 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3263 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3265 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3266 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3268 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3269 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3271 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3272 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3275 case WEAK_POINTER_WIDETAG:
3276 count = (sizetab[widetag_of(*start)])(start);
3292 /* FIXME: It would be nice to make names consistent so that
3293 * foo_size meant size *in* *bytes* instead of size in some
3294 * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3295 * Some counts of lispobjs are called foo_count; it might be good
3296 * to grep for all foo_size and rename the appropriate ones to
3298 int read_only_space_size =
3299 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER,0)
3300 - (lispobj*)READ_ONLY_SPACE_START;
3301 int static_space_size =
3302 (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER,0)
3303 - (lispobj*)STATIC_SPACE_START;
3305 for_each_thread(th) {
3306 int binding_stack_size =
3307 (lispobj*)SymbolValue(BINDING_STACK_POINTER,th)
3308 - (lispobj*)th->binding_stack_start;
3309 verify_space(th->binding_stack_start, binding_stack_size);
3311 verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3312 verify_space((lispobj*)STATIC_SPACE_START , static_space_size);
3316 verify_generation(int generation)
3320 for (i = 0; i < last_free_page; i++) {
3321 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3322 && (page_table[i].bytes_used != 0)
3323 && (page_table[i].gen == generation)) {
3325 int region_allocation = page_table[i].allocated;
3327 /* This should be the start of a contiguous block */
3328 gc_assert(page_table[i].first_object_offset == 0);
3330 /* Need to find the full extent of this contiguous block in case
3331 objects span pages. */
3333 /* Now work forward until the end of this contiguous area is
3335 for (last_page = i; ;last_page++)
3336 /* Check whether this is the last page in this contiguous
3338 if ((page_table[last_page].bytes_used < PAGE_BYTES)
3339 /* Or it is PAGE_BYTES and is the last in the block */
3340 || (page_table[last_page+1].allocated != region_allocation)
3341 || (page_table[last_page+1].bytes_used == 0)
3342 || (page_table[last_page+1].gen != generation)
3343 || (page_table[last_page+1].first_object_offset == 0))
3346 verify_space(page_address(i), (page_table[last_page].bytes_used
3347 + (last_page-i)*PAGE_BYTES)/4);
3353 /* Check that all the free space is zero filled. */
3355 verify_zero_fill(void)
3359 for (page = 0; page < last_free_page; page++) {
3360 if (page_table[page].allocated == FREE_PAGE_FLAG) {
3361 /* The whole page should be zero filled. */
3362 int *start_addr = (int *)page_address(page);
3365 for (i = 0; i < size; i++) {
3366 if (start_addr[i] != 0) {
3367 lose("free page not zero at %x", start_addr + i);
3371 int free_bytes = PAGE_BYTES - page_table[page].bytes_used;
3372 if (free_bytes > 0) {
3373 int *start_addr = (int *)((unsigned)page_address(page)
3374 + page_table[page].bytes_used);
3375 int size = free_bytes / 4;
3377 for (i = 0; i < size; i++) {
3378 if (start_addr[i] != 0) {
3379 lose("free region not zero at %x", start_addr + i);
3387 /* External entry point for verify_zero_fill */
3389 gencgc_verify_zero_fill(void)
3391 /* Flush the alloc regions updating the tables. */
3392 gc_alloc_update_all_page_tables();
3393 SHOW("verifying zero fill");
3398 verify_dynamic_space(void)
3402 for (i = 0; i < NUM_GENERATIONS; i++)
3403 verify_generation(i);
3405 if (gencgc_enable_verify_zero_fill)
3409 /* Write-protect all the dynamic boxed pages in the given generation. */
3411 write_protect_generation_pages(int generation)
3415 gc_assert(generation < NUM_GENERATIONS);
3417 for (i = 0; i < last_free_page; i++)
3418 if ((page_table[i].allocated == BOXED_PAGE_FLAG)
3419 && (page_table[i].bytes_used != 0)
3420 && !page_table[i].dont_move
3421 && (page_table[i].gen == generation)) {
3424 page_start = (void *)page_address(i);
3426 os_protect(page_start,
3428 OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3430 /* Note the page as protected in the page tables. */
3431 page_table[i].write_protected = 1;
3434 if (gencgc_verbose > 1) {
3436 "/write protected %d of %d pages in generation %d\n",
3437 count_write_protect_generation_pages(generation),
3438 count_generation_pages(generation),
3443 /* Garbage collect a generation. If raise is 0 then the remains of the
3444 * generation are not raised to the next generation. */
3446 garbage_collect_generation(int generation, int raise)
3448 unsigned long bytes_freed;
3450 unsigned long static_space_size;
3452 gc_assert(generation <= (NUM_GENERATIONS-1));
3454 /* The oldest generation can't be raised. */
3455 gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
3457 /* Initialize the weak pointer list. */
3458 weak_pointers = NULL;
3460 /* When a generation is not being raised it is transported to a
3461 * temporary generation (NUM_GENERATIONS), and lowered when
3462 * done. Set up this new generation. There should be no pages
3463 * allocated to it yet. */
3465 gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
3467 /* Set the global src and dest. generations */
3468 from_space = generation;
3470 new_space = generation+1;
3472 new_space = NUM_GENERATIONS;
3474 /* Change to a new space for allocation, resetting the alloc_start_page */
3475 gc_alloc_generation = new_space;
3476 generations[new_space].alloc_start_page = 0;
3477 generations[new_space].alloc_unboxed_start_page = 0;
3478 generations[new_space].alloc_large_start_page = 0;
3479 generations[new_space].alloc_large_unboxed_start_page = 0;
3481 /* Before any pointers are preserved, the dont_move flags on the
3482 * pages need to be cleared. */
3483 for (i = 0; i < last_free_page; i++)
3484 if(page_table[i].gen==from_space)
3485 page_table[i].dont_move = 0;
3487 /* Un-write-protect the old-space pages. This is essential for the
3488 * promoted pages as they may contain pointers into the old-space
3489 * which need to be scavenged. It also helps avoid unnecessary page
3490 * faults as forwarding pointers are written into them. They need to
3491 * be un-protected anyway before unmapping later. */
3492 unprotect_oldspace();
3494 /* Scavenge the stacks' conservative roots. */
3496 /* there are potentially two stacks for each thread: the main
3497 * stack, which may contain Lisp pointers, and the alternate stack.
3498 * We don't ever run Lisp code on the altstack, but it may
3499 * host a sigcontext with lisp objects in it */
3501 /* what we need to do: (1) find the stack pointer for the main
3502 * stack; scavenge it (2) find the interrupt context on the
3503 * alternate stack that might contain lisp values, and scavenge
3506 /* we assume that none of the preceding applies to the thread that
3507 * initiates GC. If you ever call GC from inside an altstack
3508 * handler, you will lose. */
3509 for_each_thread(th) {
3511 void **esp=(void **)-1;
3512 #ifdef LISP_FEATURE_SB_THREAD
3514 if(th==arch_os_get_current_thread()) {
3515 esp = (void **) &raise;
3518 free=fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX,th));
3519 for(i=free-1;i>=0;i--) {
3520 os_context_t *c=th->interrupt_contexts[i];
3521 esp1 = (void **) *os_context_register_addr(c,reg_ESP);
3522 if(esp1>=th->control_stack_start&& esp1<th->control_stack_end){
3523 if(esp1<esp) esp=esp1;
3524 for(ptr = (void **)(c+1); ptr>=(void **)c; ptr--) {
3525 preserve_pointer(*ptr);
3531 esp = (void **) &raise;
3533 for (ptr = (void **)th->control_stack_end; ptr > esp; ptr--) {
3534 preserve_pointer(*ptr);
3539 if (gencgc_verbose > 1) {
3540 int num_dont_move_pages = count_dont_move_pages();
3542 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
3543 num_dont_move_pages,
3544 num_dont_move_pages * PAGE_BYTES);
3548 /* Scavenge all the rest of the roots. */
3550 /* Scavenge the Lisp functions of the interrupt handlers, taking
3551 * care to avoid SIG_DFL and SIG_IGN. */
3552 for_each_thread(th) {
3553 struct interrupt_data *data=th->interrupt_data;
3554 for (i = 0; i < NSIG; i++) {
3555 union interrupt_handler handler = data->interrupt_handlers[i];
3556 if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3557 !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3558 scavenge((lispobj *)(data->interrupt_handlers + i), 1);
3562 /* Scavenge the binding stacks. */
3565 for_each_thread(th) {
3566 long len= (lispobj *)SymbolValue(BINDING_STACK_POINTER,th) -
3567 th->binding_stack_start;
3568 scavenge((lispobj *) th->binding_stack_start,len);
3569 #ifdef LISP_FEATURE_SB_THREAD
3570 /* do the tls as well */
3571 len=fixnum_value(SymbolValue(FREE_TLS_INDEX,0)) -
3572 (sizeof (struct thread))/(sizeof (lispobj));
3573 scavenge((lispobj *) (th+1),len);
3578 /* The original CMU CL code had scavenge-read-only-space code
3579 * controlled by the Lisp-level variable
3580 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
3581 * wasn't documented under what circumstances it was useful or
3582 * safe to turn it on, so it's been turned off in SBCL. If you
3583 * want/need this functionality, and can test and document it,
3584 * please submit a patch. */
3586 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
3587 unsigned long read_only_space_size =
3588 (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
3589 (lispobj*)READ_ONLY_SPACE_START;
3591 "/scavenge read only space: %d bytes\n",
3592 read_only_space_size * sizeof(lispobj)));
3593 scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
3597 /* Scavenge static space. */
3599 (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER,0) -
3600 (lispobj *)STATIC_SPACE_START;
3601 if (gencgc_verbose > 1) {
3603 "/scavenge static space: %d bytes\n",
3604 static_space_size * sizeof(lispobj)));
3606 scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
3608 /* All generations but the generation being GCed need to be
3609 * scavenged. The new_space generation needs special handling as
3610 * objects may be moved in - it is handled separately below. */
3611 for (i = 0; i < NUM_GENERATIONS; i++) {
3612 if ((i != generation) && (i != new_space)) {
3613 scavenge_generation(i);
3617 /* Finally scavenge the new_space generation. Keep going until no
3618 * more objects are moved into the new generation */
3619 scavenge_newspace_generation(new_space);
3621 /* FIXME: I tried reenabling this check when debugging unrelated
3622 * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3623 * Since the current GC code seems to work well, I'm guessing that
3624 * this debugging code is just stale, but I haven't tried to
3625 * figure it out. It should be figured out and then either made to
3626 * work or just deleted. */
3627 #define RESCAN_CHECK 0
3629 /* As a check re-scavenge the newspace once; no new objects should
3632 int old_bytes_allocated = bytes_allocated;
3633 int bytes_allocated;
3635 /* Start with a full scavenge. */
3636 scavenge_newspace_generation_one_scan(new_space);
3638 /* Flush the current regions, updating the tables. */
3639 gc_alloc_update_all_page_tables();
3641 bytes_allocated = bytes_allocated - old_bytes_allocated;
3643 if (bytes_allocated != 0) {
3644 lose("Rescan of new_space allocated %d more bytes.",
3650 scan_weak_pointers();
3652 /* Flush the current regions, updating the tables. */
3653 gc_alloc_update_all_page_tables();
3655 /* Free the pages in oldspace, but not those marked dont_move. */
3656 bytes_freed = free_oldspace();
3658 /* If the GC is not raising the age then lower the generation back
3659 * to its normal generation number */
3661 for (i = 0; i < last_free_page; i++)
3662 if ((page_table[i].bytes_used != 0)
3663 && (page_table[i].gen == NUM_GENERATIONS))
3664 page_table[i].gen = generation;
3665 gc_assert(generations[generation].bytes_allocated == 0);
3666 generations[generation].bytes_allocated =
3667 generations[NUM_GENERATIONS].bytes_allocated;
3668 generations[NUM_GENERATIONS].bytes_allocated = 0;
3671 /* Reset the alloc_start_page for generation. */
3672 generations[generation].alloc_start_page = 0;
3673 generations[generation].alloc_unboxed_start_page = 0;
3674 generations[generation].alloc_large_start_page = 0;
3675 generations[generation].alloc_large_unboxed_start_page = 0;
3677 if (generation >= verify_gens) {
3681 verify_dynamic_space();
3684 /* Set the new gc trigger for the GCed generation. */
3685 generations[generation].gc_trigger =
3686 generations[generation].bytes_allocated
3687 + generations[generation].bytes_consed_between_gc;
3690 generations[generation].num_gc = 0;
3692 ++generations[generation].num_gc;
3695 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3697 update_x86_dynamic_space_free_pointer(void)
3702 for (i = 0; i < NUM_PAGES; i++)
3703 if ((page_table[i].allocated != FREE_PAGE_FLAG)
3704 && (page_table[i].bytes_used != 0))
3707 last_free_page = last_page+1;
3709 SetSymbolValue(ALLOCATION_POINTER,
3710 (lispobj)(((char *)heap_base) + last_free_page*PAGE_BYTES),0);
3711 return 0; /* dummy value: return something ... */
3714 /* GC all generations newer than last_gen, raising the objects in each
3715 * to the next older generation - we finish when all generations below
3716 * last_gen are empty. Then if last_gen is due for a GC, or if
3717 * last_gen==NUM_GENERATIONS (the scratch generation? eh?) we GC that
3718 * too. The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3720 * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3721 * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3724 collect_garbage(unsigned last_gen)
3731 FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3733 if (last_gen > NUM_GENERATIONS) {
3735 "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3740 /* Flush the alloc regions updating the tables. */
3741 gc_alloc_update_all_page_tables();
3743 /* Verify the new objects created by Lisp code. */
3744 if (pre_verify_gen_0) {
3745 FSHOW((stderr, "pre-checking generation 0\n"));
3746 verify_generation(0);
3749 if (gencgc_verbose > 1)
3750 print_generation_stats(0);
3753 /* Collect the generation. */
3755 if (gen >= gencgc_oldest_gen_to_gc) {
3756 /* Never raise the oldest generation. */
3761 || (generations[gen].num_gc >= generations[gen].trigger_age);
3764 if (gencgc_verbose > 1) {
3766 "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3769 generations[gen].bytes_allocated,
3770 generations[gen].gc_trigger,
3771 generations[gen].num_gc));
3774 /* If an older generation is being filled, then update its
3777 generations[gen+1].cum_sum_bytes_allocated +=
3778 generations[gen+1].bytes_allocated;
3781 garbage_collect_generation(gen, raise);
3783 /* Reset the memory age cum_sum. */
3784 generations[gen].cum_sum_bytes_allocated = 0;
3786 if (gencgc_verbose > 1) {
3787 FSHOW((stderr, "GC of generation %d finished:\n", gen));
3788 print_generation_stats(0);
3792 } while ((gen <= gencgc_oldest_gen_to_gc)
3793 && ((gen < last_gen)
3794 || ((gen <= gencgc_oldest_gen_to_gc)
3796 && (generations[gen].bytes_allocated
3797 > generations[gen].gc_trigger)
3798 && (gen_av_mem_age(gen)
3799 > generations[gen].min_av_mem_age))));
3801 /* Now if gen-1 was raised all generations before gen are empty.
3802 * If it wasn't raised then all generations before gen-1 are empty.
3804 * Now objects within this gen's pages cannot point to younger
3805 * generations unless they are written to. This can be exploited
3806 * by write-protecting the pages of gen; then when younger
3807 * generations are GCed only the pages which have been written
3812 gen_to_wp = gen - 1;
3814 /* There's not much point in WPing pages in generation 0 as it is
3815 * never scavenged (except promoted pages). */
3816 if ((gen_to_wp > 0) && enable_page_protection) {
3817 /* Check that they are all empty. */
3818 for (i = 0; i < gen_to_wp; i++) {
3819 if (generations[i].bytes_allocated)
3820 lose("trying to write-protect gen. %d when gen. %d nonempty",
3823 write_protect_generation_pages(gen_to_wp);
3826 /* Set gc_alloc() back to generation 0. The current regions should
3827 * be flushed after the above GCs. */
3828 gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3829 gc_alloc_generation = 0;
3831 update_x86_dynamic_space_free_pointer();
3832 auto_gc_trigger = bytes_allocated + bytes_consed_between_gcs;
3834 fprintf(stderr,"Next gc when %ld bytes have been consed\n",
3836 SHOW("returning from collect_garbage");
3839 /* This is called by Lisp PURIFY when it is finished. All live objects
3840 * will have been moved to the RO and Static heaps. The dynamic space
3841 * will need a full re-initialization. We don't bother having Lisp
3842 * PURIFY flush the current gc_alloc() region, as the page_tables are
3843 * re-initialized, and every page is zeroed to be sure. */
3849 if (gencgc_verbose > 1)
3850 SHOW("entering gc_free_heap");
3852 for (page = 0; page < NUM_PAGES; page++) {
3853 /* Skip free pages which should already be zero filled. */
3854 if (page_table[page].allocated != FREE_PAGE_FLAG) {
3855 void *page_start, *addr;
3857 /* Mark the page free. The other slots are assumed invalid
3858 * when it is a FREE_PAGE_FLAG and bytes_used is 0 and it
3859 * should not be write-protected -- except that the
3860 * generation is used for the current region but it sets
3862 page_table[page].allocated = FREE_PAGE_FLAG;
3863 page_table[page].bytes_used = 0;
3865 /* Zero the page. */
3866 page_start = (void *)page_address(page);
3868 /* First, remove any write-protection. */
3869 os_protect(page_start, PAGE_BYTES, OS_VM_PROT_ALL);
3870 page_table[page].write_protected = 0;
3872 os_invalidate(page_start,PAGE_BYTES);
3873 addr = os_validate(page_start,PAGE_BYTES);
3874 if (addr == NULL || addr != page_start) {
3875 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
3879 } else if (gencgc_zero_check_during_free_heap) {
3880 /* Double-check that the page is zero filled. */
3882 gc_assert(page_table[page].allocated == FREE_PAGE_FLAG);
3883 gc_assert(page_table[page].bytes_used == 0);
3884 page_start = (int *)page_address(page);
3885 for (i=0; i<1024; i++) {
3886 if (page_start[i] != 0) {
3887 lose("free region not zero at %x", page_start + i);
3893 bytes_allocated = 0;
3895 /* Initialize the generations. */
3896 for (page = 0; page < NUM_GENERATIONS; page++) {
3897 generations[page].alloc_start_page = 0;
3898 generations[page].alloc_unboxed_start_page = 0;
3899 generations[page].alloc_large_start_page = 0;
3900 generations[page].alloc_large_unboxed_start_page = 0;
3901 generations[page].bytes_allocated = 0;
3902 generations[page].gc_trigger = 2000000;
3903 generations[page].num_gc = 0;
3904 generations[page].cum_sum_bytes_allocated = 0;
3907 if (gencgc_verbose > 1)
3908 print_generation_stats(0);
3910 /* Initialize gc_alloc(). */
3911 gc_alloc_generation = 0;
3913 gc_set_region_empty(&boxed_region);
3914 gc_set_region_empty(&unboxed_region);
3917 SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base),0);
3919 if (verify_after_free_heap) {
3920 /* Check whether purify has left any bad pointers. */
3922 SHOW("checking after free_heap\n");
3933 scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
3934 scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
3935 transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
3937 heap_base = (void*)DYNAMIC_SPACE_START;
3939 /* Initialize each page structure. */
3940 for (i = 0; i < NUM_PAGES; i++) {
3941 /* Initialize all pages as free. */
3942 page_table[i].allocated = FREE_PAGE_FLAG;
3943 page_table[i].bytes_used = 0;
3945 /* Pages are not write-protected at startup. */
3946 page_table[i].write_protected = 0;
3949 bytes_allocated = 0;
3951 /* Initialize the generations.
3953 * FIXME: very similar to code in gc_free_heap(), should be shared */
3954 for (i = 0; i < NUM_GENERATIONS; i++) {
3955 generations[i].alloc_start_page = 0;
3956 generations[i].alloc_unboxed_start_page = 0;
3957 generations[i].alloc_large_start_page = 0;
3958 generations[i].alloc_large_unboxed_start_page = 0;
3959 generations[i].bytes_allocated = 0;
3960 generations[i].gc_trigger = 2000000;
3961 generations[i].num_gc = 0;
3962 generations[i].cum_sum_bytes_allocated = 0;
3963 /* the tune-able parameters */
3964 generations[i].bytes_consed_between_gc = 2000000;
3965 generations[i].trigger_age = 1;
3966 generations[i].min_av_mem_age = 0.75;
3969 /* Initialize gc_alloc. */
3970 gc_alloc_generation = 0;
3971 gc_set_region_empty(&boxed_region);
3972 gc_set_region_empty(&unboxed_region);
3978 /* Pick up the dynamic space from after a core load.
3980 * The ALLOCATION_POINTER points to the end of the dynamic space.
3984 gencgc_pickup_dynamic(void)
3987 int alloc_ptr = SymbolValue(ALLOCATION_POINTER,0);
3988 lispobj *prev=(lispobj *)page_address(page);
3991 lispobj *first,*ptr= (lispobj *)page_address(page);
3992 page_table[page].allocated = BOXED_PAGE_FLAG;
3993 page_table[page].gen = 0;
3994 page_table[page].bytes_used = PAGE_BYTES;
3995 page_table[page].large_object = 0;
3997 first=search_space(prev,(ptr+2)-prev,ptr);
3998 if(ptr == first) prev=ptr;
3999 page_table[page].first_object_offset =
4000 (void *)prev - page_address(page);
4002 } while (page_address(page) < alloc_ptr);
4004 generations[0].bytes_allocated = PAGE_BYTES*page;
4005 bytes_allocated = PAGE_BYTES*page;
4011 gc_initialize_pointers(void)
4013 gencgc_pickup_dynamic();
4019 /* alloc(..) is the external interface for memory allocation. It
4020 * allocates to generation 0. It is not called from within the garbage
4021 * collector as it is only external uses that need the check for heap
4022 * size (GC trigger) and to disable the interrupts (interrupts are
4023 * always disabled during a GC).
4025 * The vops that call alloc(..) assume that the returned space is zero-filled.
4026 * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4028 * The check for a GC trigger is only performed when the current
4029 * region is full, so in most cases it's not needed. */
4034 struct thread *th=arch_os_get_current_thread();
4035 struct alloc_region *region=
4036 th ? &(th->alloc_region) : &boxed_region;
4038 void *new_free_pointer;
4040 /* Check for alignment allocation problems. */
4041 gc_assert((((unsigned)region->free_pointer & 0x7) == 0)
4042 && ((nbytes & 0x7) == 0));
4044 /* there are a few places in the C code that allocate data in the
4045 * heap before Lisp starts. This is before interrupts are enabled,
4046 * so we don't need to check for pseudo-atomic */
4047 #ifdef LISP_FEATURE_SB_THREAD
4048 if(!SymbolValue(PSEUDO_ATOMIC_ATOMIC,th)) {
4050 fprintf(stderr, "fatal error in thread 0x%x, pid=%d\n",
4052 __asm__("movl %fs,%0" : "=r" (fs) : );
4053 fprintf(stderr, "fs is %x, th->tls_cookie=%x \n",
4054 debug_get_fs(),th->tls_cookie);
4055 lose("If you see this message before 2004.01.31, mail details to sbcl-devel\n");
4058 gc_assert(SymbolValue(PSEUDO_ATOMIC_ATOMIC,th));
4061 /* maybe we can do this quickly ... */
4062 new_free_pointer = region->free_pointer + nbytes;
4063 if (new_free_pointer <= region->end_addr) {
4064 new_obj = (void*)(region->free_pointer);
4065 region->free_pointer = new_free_pointer;
4066 return(new_obj); /* yup */
4069 /* we have to go the long way around, it seems. Check whether
4070 * we should GC in the near future
4072 if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4073 /* set things up so that GC happens when we finish the PA
4074 * section. We only do this if there wasn't a pending handler
4075 * already, in case it was a gc. If it wasn't a GC, the next
4076 * allocation will get us back to this point anyway, so no harm done
4078 struct interrupt_data *data=th->interrupt_data;
4079 if(!data->pending_handler)
4080 maybe_defer_handler(interrupt_maybe_gc_int,data,0,0,0);
4082 new_obj = gc_alloc_with_region(nbytes,0,region,0);
4087 /* Find the code object for the given pc, or return NULL on failure.
4089 * FIXME: PC shouldn't be lispobj*, should it? Maybe void*? */
4091 component_ptr_from_pc(lispobj *pc)
4093 lispobj *object = NULL;
4095 if ( (object = search_read_only_space(pc)) )
4097 else if ( (object = search_static_space(pc)) )
4100 object = search_dynamic_space(pc);
4102 if (object) /* if we found something */
4103 if (widetag_of(*object) == CODE_HEADER_WIDETAG) /* if it's a code object */
4110 * shared support for the OS-dependent signal handlers which
4111 * catch GENCGC-related write-protect violations
4114 void unhandled_sigmemoryfault(void);
4116 /* Depending on which OS we're running under, different signals might
4117 * be raised for a violation of write protection in the heap. This
4118 * function factors out the common generational GC magic which needs
4119 * to invoked in this case, and should be called from whatever signal
4120 * handler is appropriate for the OS we're running under.
4122 * Return true if this signal is a normal generational GC thing that
4123 * we were able to handle, or false if it was abnormal and control
4124 * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4127 gencgc_handle_wp_violation(void* fault_addr)
4129 int page_index = find_page_index(fault_addr);
4131 #if defined QSHOW_SIGNALS
4132 FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4133 fault_addr, page_index));
4136 /* Check whether the fault is within the dynamic space. */
4137 if (page_index == (-1)) {
4139 /* It can be helpful to be able to put a breakpoint on this
4140 * case to help diagnose low-level problems. */
4141 unhandled_sigmemoryfault();
4143 /* not within the dynamic space -- not our responsibility */
4147 if (page_table[page_index].write_protected) {
4148 /* Unprotect the page. */
4149 os_protect(page_address(page_index), PAGE_BYTES, OS_VM_PROT_ALL);
4150 page_table[page_index].write_protected_cleared = 1;
4151 page_table[page_index].write_protected = 0;
4153 /* The only acceptable reason for this signal on a heap
4154 * access is that GENCGC write-protected the page.
4155 * However, if two CPUs hit a wp page near-simultaneously,
4156 * we had better not have the second one lose here if it
4157 * does this test after the first one has already set wp=0
4159 if(page_table[page_index].write_protected_cleared != 1)
4160 lose("fault in heap page not marked as write-protected");
4162 /* Don't worry, we can handle it. */
4166 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4167 * it's not just a case of the program hitting the write barrier, and
4168 * are about to let Lisp deal with it. It's basically just a
4169 * convenient place to set a gdb breakpoint. */
4171 unhandled_sigmemoryfault()
4174 void gc_alloc_update_all_page_tables(void)
4176 /* Flush the alloc regions updating the tables. */
4179 gc_alloc_update_page_tables(0, &th->alloc_region);
4180 gc_alloc_update_page_tables(1, &unboxed_region);
4181 gc_alloc_update_page_tables(0, &boxed_region);
4184 gc_set_region_empty(struct alloc_region *region)
4186 region->first_page = 0;
4187 region->last_page = -1;
4188 region->start_addr = page_address(0);
4189 region->free_pointer = page_address(0);
4190 region->end_addr = page_address(0);