2 * Generational Conservative Garbage Collector for CMUCL x86.
4 * This code was written by Douglas T. Crosher, based on Public Domain
5 * codes from Carnegie Mellon University. This code has been placed in
6 * the public domain, and is provided 'as is'.
8 * Douglas Crosher, 1996, 1997, 1998, 1999.
10 * $Header: /project/cmucl/cvsroot/src/lisp/gencgc.c,v 1.112 2011-01-09 00:12:36 rtoy Exp $
21 #include "internals.h"
24 #include "interrupt.h"
31 * This value in a hash table hash-vector means that the key uses
32 * EQ-based hashing. That is, the key might be using EQ or EQL for
33 * the test. This MUST match the value used in hash-new.lisp!
35 #define EQ_BASED_HASH_VALUE 0x80000000
37 #define gc_abort() lose("GC invariant lost! File \"%s\", line %d\n", \
40 #if (defined(i386) || defined(__x86_64))
42 #define set_alloc_pointer(value) \
43 SetSymbolValue (ALLOCATION_POINTER, (value))
44 #define get_alloc_pointer() \
45 SymbolValue (ALLOCATION_POINTER)
46 #define get_binding_stack_pointer() \
47 SymbolValue (BINDING_STACK_POINTER)
48 #define get_pseudo_atomic_atomic() \
49 SymbolValue (PSEUDO_ATOMIC_ATOMIC)
50 #define set_pseudo_atomic_atomic() \
51 SetSymbolValue (PSEUDO_ATOMIC_ATOMIC, make_fixnum (1))
52 #define clr_pseudo_atomic_atomic() \
53 SetSymbolValue (PSEUDO_ATOMIC_ATOMIC, make_fixnum (0))
54 #define get_pseudo_atomic_interrupted() \
55 SymbolValue (PSEUDO_ATOMIC_INTERRUPTED)
56 #define clr_pseudo_atomic_interrupted() \
57 SetSymbolValue (PSEUDO_ATOMIC_INTERRUPTED, make_fixnum (0))
59 #define set_current_region_free(value) \
60 SetSymbolValue(CURRENT_REGION_FREE_POINTER, (value))
61 #define set_current_region_end(value) \
62 SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
63 #define get_current_region_free() \
64 SymbolValue(CURRENT_REGION_FREE_POINTER)
66 #define set_current_region_end(value) \
67 SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
72 * current_dynamic_space_free_pointer contains the pseudo-atomic
73 * stuff, so we need to preserve those bits when we give it a value.
74 * This value better not have any bits set there either!
78 * On sparc, we don't need to set the alloc_pointer in the code here
79 * because the alloc pointer (current_dynamic_space_free_pointer) is
80 * the same as *current-region-free-pointer* and is stored in
83 #define set_alloc_pointer(value)
84 #define get_alloc_pointer() \
85 ((unsigned long) current_dynamic_space_free_pointer & ~lowtag_Mask)
86 #define get_binding_stack_pointer() \
87 (current_binding_stack_pointer)
88 #define get_pseudo_atomic_atomic() \
89 ((unsigned long)current_dynamic_space_free_pointer & pseudo_atomic_Value)
90 #define set_pseudo_atomic_atomic() \
91 (current_dynamic_space_free_pointer \
92 = (lispobj*) ((unsigned long)current_dynamic_space_free_pointer | pseudo_atomic_Value))
93 #define clr_pseudo_atomic_atomic() \
94 (current_dynamic_space_free_pointer \
95 = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_Value))
96 #define get_pseudo_atomic_interrupted() \
97 ((unsigned long) current_dynamic_space_free_pointer & pseudo_atomic_InterruptedValue)
98 #define clr_pseudo_atomic_interrupted() \
99 (current_dynamic_space_free_pointer \
100 = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_InterruptedValue))
102 #define set_current_region_free(value) \
103 current_dynamic_space_free_pointer = (lispobj*)((value) | ((long)current_dynamic_space_free_pointer & lowtag_Mask))
105 #define get_current_region_free() \
106 ((long)current_dynamic_space_free_pointer & (~(lowtag_Mask)))
108 #define set_current_region_end(value) \
109 SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
111 #elif defined(DARWIN) && defined(__ppc__)
112 #ifndef pseudo_atomic_InterruptedValue
113 #define pseudo_atomic_InterruptedValue 1
115 #ifndef pseudo_atomic_Value
116 #define pseudo_atomic_Value 4
119 #define set_alloc_pointer(value)
120 #define get_alloc_pointer() \
121 ((unsigned long) current_dynamic_space_free_pointer & ~lowtag_Mask)
122 #define get_binding_stack_pointer() \
123 (current_binding_stack_pointer)
124 #define get_pseudo_atomic_atomic() \
125 ((unsigned long)current_dynamic_space_free_pointer & pseudo_atomic_Value)
126 #define set_pseudo_atomic_atomic() \
127 (current_dynamic_space_free_pointer \
128 = (lispobj*) ((unsigned long)current_dynamic_space_free_pointer | pseudo_atomic_Value))
129 #define clr_pseudo_atomic_atomic() \
130 (current_dynamic_space_free_pointer \
131 = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_Value))
132 #define get_pseudo_atomic_interrupted() \
133 ((unsigned long) current_dynamic_space_free_pointer & pseudo_atomic_InterruptedValue)
134 #define clr_pseudo_atomic_interrupted() \
135 (current_dynamic_space_free_pointer \
136 = (lispobj*) ((unsigned long) current_dynamic_space_free_pointer & ~pseudo_atomic_InterruptedValue))
138 #define set_current_region_free(value) \
139 current_dynamic_space_free_pointer = (lispobj*)((value) | ((long)current_dynamic_space_free_pointer & lowtag_Mask))
141 #define get_current_region_free() \
142 ((long)current_dynamic_space_free_pointer & (~(lowtag_Mask)))
144 #define set_current_region_end(value) \
145 SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
148 #error gencgc is not supported on this platform
151 /* Define for activating assertions. */
153 #if defined(x86) && defined(SOLARIS)
154 #define GC_ASSERTIONS 1
157 /* Check for references to stack-allocated objects. */
161 static void *invalid_stack_start, *invalid_stack_end;
164 check_escaped_stack_object(lispobj * where, lispobj obj)
166 #if !defined(DARWIN) && !defined(__ppc__)
170 && (p = (void *) PTR(obj),
171 (p >= (void *) CONTROL_STACK_START
172 && p < (void *) control_stack_end))) {
175 if (where >= (lispobj *) DYNAMIC_0_SPACE_START
176 && where < (lispobj *) (DYNAMIC_0_SPACE_START + dynamic_space_size))
177 space = "dynamic space";
178 else if (where >= (lispobj *) STATIC_SPACE_START
180 (lispobj *) (STATIC_SPACE_START + static_space_size)) space =
182 else if (where >= (lispobj *) READ_ONLY_SPACE_START
184 (lispobj *) (READ_ONLY_SPACE_START +
185 read_only_space_size)) space = "read-only space";
189 /* GC itself uses some stack, so we can't tell exactly where the
190 invalid stack area starts. Usually, it should be an error if a
191 reference to a stack-allocated object is found, although it
192 is valid to store a reference to a stack-allocated object
193 temporarily in another reachable object, as long as the
194 reference goes away at the end of a dynamic extent. */
196 if (p >= invalid_stack_start && p < invalid_stack_end)
197 lose("Escaped stack-allocated object 0x%08lx at %p in %s\n",
198 (unsigned long) obj, where, space);
200 else if ((where >= (lispobj *) CONTROL_STACK_START
201 && where < (lispobj *) (control_stack_end))
202 || (space == NULL)) {
203 /* Do nothing if it the reference is from the control stack,
204 because that will happen, and that's ok. Or if it's from
205 an unknown space (typically from scavenging an interrupt
212 "Reference to stack-allocated object 0x%08lx at %p in %s\n",
213 (unsigned long) obj, where,
214 space ? space : "Unknown space");
219 #endif /* GC_ASSERTIONS */
223 #define gc_assert(ex) \
225 if (!(ex)) gc_abort (); \
228 #define gc_assert(ex) (void) 0
233 * The number of generations, an extra is added to this for use as a temp.
235 #define NUM_GENERATIONS 6
237 /* Debugging variables. */
240 * The verbose level. All non-error messages are disabled at level 0;
241 * and only a few rare messages are printed at level 1.
243 unsigned gencgc_verbose = 0;
244 unsigned counters_verbose = 0;
247 * If true, then some debugging information is printed when scavenging
248 * static (malloc'ed) arrays.
250 boolean debug_static_array_p = 0;
253 * To enable the use of page protection to help avoid the scavenging
254 * of pages that don't have pointers to younger generations.
256 boolean enable_page_protection = TRUE;
259 * Hunt for pointers to old-space, when GCing generations >= verify_gen.
260 * Set to NUM_GENERATIONS to disable.
262 int verify_gens = NUM_GENERATIONS;
265 * Enable a pre-scan verify of generation 0 before it's GCed. (This
266 * makes GC very, very slow, so don't enable this unless you really
269 boolean pre_verify_gen_0 = FALSE;
272 * Enable checking for bad pointers after gc_free_heap called from purify.
274 #if 0 && defined(DARWIN)
275 boolean verify_after_free_heap = TRUE;
277 boolean verify_after_free_heap = FALSE;
281 * Enable the printing of a note when code objects are found in the
282 * dynamic space during a heap verify.
284 boolean verify_dynamic_code_check = FALSE;
287 * Enable the checking of code objects for fixup errors after they are
288 * transported. (Only used for x86.)
290 boolean check_code_fixups = FALSE;
293 * To enable unmapping of a page and re-mmaping it to have it zero filled.
294 * Note: this can waste a lot of swap on FreeBSD and Open/NetBSD(?) so
297 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
298 boolean gencgc_unmap_zero = FALSE;
300 boolean gencgc_unmap_zero = TRUE;
304 * Enable checking that newly allocated regions are zero filled.
306 #if 0 && defined(DARWIN)
307 boolean gencgc_zero_check = TRUE;
308 boolean gencgc_enable_verify_zero_fill = TRUE;
310 boolean gencgc_zero_check = FALSE;
311 boolean gencgc_enable_verify_zero_fill = FALSE;
315 * Enable checking that free pages are zero filled during gc_free_heap
316 * called after purify.
318 #if 0 && defined(DARWIN)
319 boolean gencgc_zero_check_during_free_heap = TRUE;
321 boolean gencgc_zero_check_during_free_heap = FALSE;
325 * The minimum size for a large object.
327 unsigned large_object_size = 4 * GC_PAGE_SIZE;
330 * Enable the filtering of stack/register pointers. This could reduce
331 * the number of invalid pointers accepted. It will probably degrades
332 * interrupt safety during object initialisation.
334 boolean enable_pointer_filter = TRUE;
338 * The total bytes allocated. Seen by (dynamic-usage)
340 unsigned long bytes_allocated = 0;
343 * The total amount of bytes ever allocated. Not decreased by GC.
346 volatile unsigned long long bytes_allocated_sum = 0;
349 * GC trigger; a value of 0xffffffff represents disabled.
351 unsigned long auto_gc_trigger = 0xffffffff;
354 * Number of pages to reserve for heap overflow. We want some space
355 * available on the heap when we are close to a heap overflow, so we
356 * can handle the overflow. But how much do we really need? I (rtoy)
357 * think 256 pages is probably a decent amount. (That's 1 MB for x86,
358 * 2 MB for sparc, which has 8K pages.)
361 unsigned long reserved_heap_pages = 256;
364 * The src. and dest. generations. Set before a GC starts scavenging.
366 static int from_space;
367 static int new_space;
371 * GC structures and variables.
375 * Number of pages within the dynamic heap, setup from the size of the
378 unsigned dynamic_space_pages;
381 * An array of page structures is statically allocated.
382 * This helps quickly map between an address and its page structure.
384 struct page *page_table;
387 * Heap base, needed for mapping addresses to page structures.
389 static char *heap_base = NULL;
392 * Calculate the start address for the given page number.
395 page_address(int page_num)
397 return heap_base + GC_PAGE_SIZE * page_num;
401 * Find the page index within the page_table for the given address.
402 * Returns -1 on failure.
405 find_page_index(void *addr)
407 int index = (char *) addr - heap_base;
410 index = (unsigned int) index / GC_PAGE_SIZE;
411 if (index < dynamic_space_pages)
419 * This routine implements a write barrier used to record stores into
420 * to boxed regions outside of generation 0. When such a store occurs
421 * this routine will be automatically invoked by the page fault
422 * handler. If passed an address outside of the dynamic space, this
423 * routine will return immediately with a value of 0. Otherwise, the
424 * page belonging to the address is made writable, the protection
425 * change is recorded in the garbage collector page table, and a value
429 gc_write_barrier(void *addr)
431 int page_index = find_page_index(addr);
433 /* Check if the fault is within the dynamic space. */
434 if (page_index == -1) {
438 /* The page should have been marked write protected */
439 if (!PAGE_WRITE_PROTECTED(page_index))
441 "*** Page fault in page not marked as write protected\n");
443 /* Un-protect the page */
444 os_protect((os_vm_address_t) page_address(page_index), GC_PAGE_SIZE, OS_VM_PROT_ALL);
445 page_table[page_index].flags &= ~PAGE_WRITE_PROTECTED_MASK;
446 page_table[page_index].flags |= PAGE_WRITE_PROTECT_CLEARED_MASK;
452 * A structure to hold the state of a generation.
454 #define MEM_AGE_SHIFT 16
455 #define MEM_AGE_SCALE (1 << MEM_AGE_SHIFT)
459 /* The first page that gc_alloc checks on its next call. */
460 int alloc_start_page;
462 /* The first page that gc_alloc_unboxed checks on its next call. */
463 int alloc_unboxed_start_page;
466 * The first page that gc_alloc_large (boxed) considers on its next call.
467 * Although it always allocates after the boxed_region.
469 int alloc_large_start_page;
472 * The first page that gc_alloc_large (unboxed) considers on its next call.
473 * Although it always allocates after the current_unboxed_region.
475 int alloc_large_unboxed_start_page;
477 /* The bytes allocate to this generation. */
480 /* The number of bytes at which to trigger a GC */
483 /* To calculate a new level for gc_trigger */
484 int bytes_consed_between_gc;
486 /* The number of GCs since the last raise. */
490 * The average age at after which a GC will raise objects to the
496 * The cumulative sum of the bytes allocated to this generation. It
497 * is cleared after a GC on this generation, and update before new
498 * objects are added from a GC of a younger generation. Dividing by
499 * the bytes_allocated will give the average age of the memory in
500 * this generation since its last GC.
502 int cum_sum_bytes_allocated;
505 * A minimum average memory age before a GC will occur helps prevent
506 * a GC when a large number of new live objects have been added, in
507 * which case a GC could be a waste of time.
509 * The age is represented as an integer between 0 and 32767
510 * corresponding to an age of 0 to (just less than) 1.
516 * An array of generation structures. There needs to be one more
517 * generation structure than actual generations as the oldest
518 * generations is temporarily raised then lowered.
520 static struct generation generations[NUM_GENERATIONS + 1];
522 /* Statistics about a generation, extracted from the generations
523 array. This gets returned to Lisp.
526 struct generation_stats {
529 int bytes_consed_between_gc;
532 int cum_sum_bytes_allocated;
538 * The oldest generation that will currently be GCed by default.
539 * Valid values are: 0, 1, ... (NUM_GENERATIONS - 1)
541 * The default of (NUM_GENERATIONS - 1) enables GC on all generations.
543 * Setting this to 0 effectively disables the generational nature of
544 * the GC. In some applications generational GC may not be useful
545 * because there are no long-lived objects.
547 * An intermediate value could be handy after moving long-lived data
548 * into an older generation so an unnecessary GC of this long-lived
549 * data can be avoided.
551 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS - 1;
555 * The maximum free page in the heap is maintained and used to update
556 * ALLOCATION_POINTER which is used by the room function to limit its
557 * search of the heap. XX Gencgc obviously needs to be better
558 * integrated with the lisp code.
560 * Except on sparc and ppc, there's no ALLOCATION_POINTER, so it's
561 * never updated. So make this available (non-static).
566 static void scan_weak_tables(void);
567 static void scan_weak_objects(void);
570 * Misc. heap functions.
574 * Count the number of write protected pages within the given generation.
577 count_write_protect_generation_pages(int generation)
583 mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
584 | PAGE_GENERATION_MASK;
585 mflags = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK | generation;
587 for (i = 0; i < last_free_page; i++)
588 if (PAGE_FLAGS(i, mmask) == mflags)
594 * Count the number of pages within the given generation.
597 count_generation_pages(int generation)
603 mmask = PAGE_ALLOCATED_MASK | PAGE_GENERATION_MASK;
604 mflags = PAGE_ALLOCATED_MASK | generation;
606 for (i = 0; i < last_free_page; i++)
607 if (PAGE_FLAGS(i, mmask) == mflags)
613 * Count the number of dont_move pages.
616 count_dont_move_pages(void)
622 mmask = PAGE_ALLOCATED_MASK | PAGE_DONT_MOVE_MASK;
624 for (i = 0; i < last_free_page; i++)
625 if (PAGE_FLAGS(i, mmask) == mmask)
631 * Work through the pages and add up the number of bytes used for the
636 generation_bytes_allocated(int generation)
639 int bytes_allocated = 0;
642 mmask = PAGE_ALLOCATED_MASK | PAGE_GENERATION_MASK;
643 mflags = PAGE_ALLOCATED_MASK | generation;
645 for (i = 0; i < last_free_page; i++) {
646 if (PAGE_FLAGS(i, mmask) == mflags)
647 bytes_allocated += page_table[i].bytes_used;
649 return bytes_allocated;
654 * Return the average age of the memory in a generation.
657 gen_av_mem_age(int gen)
659 if (generations[gen].bytes_allocated == 0)
662 return (((long long) generations[gen].cum_sum_bytes_allocated) << MEM_AGE_SHIFT) /
663 generations[gen].bytes_allocated;
668 save_fpu_state(void* state)
670 #if defined(i386) || defined(__x86_64)
671 if (fpu_mode == SSE2) {
682 restore_fpu_state(void* state)
684 #if defined(i386) || defined(__x86_64)
685 if (fpu_mode == SSE2) {
697 * The verbose argument controls how much to print out:
698 * 0 for normal level of detail; 1 for debugging.
701 print_generation_stats(int verbose)
705 FPU_STATE(fpu_state);
708 * This code uses the FP instructions which may be setup for Lisp so
709 * they need to the saved and reset for C.
712 save_fpu_state(fpu_state);
714 /* Number of generations to print out. */
716 gens = NUM_GENERATIONS + 1;
718 gens = NUM_GENERATIONS;
720 /* Print the heap stats */
721 fprintf(stderr, " Page count (%d KB)\n", GC_PAGE_SIZE / 1024);
723 " Gen Boxed Unboxed LB LUB Alloc Waste Trigger WP GCs Mem-age\n");
725 for (i = 0; i < gens; i++) {
729 int large_boxed_cnt = 0;
730 int large_unboxed_cnt = 0;
732 for (j = 0; j < last_free_page; j++) {
733 int flags = page_table[j].flags;
735 if ((flags & PAGE_GENERATION_MASK) == i) {
736 if (flags & PAGE_ALLOCATED_MASK) {
738 * Count the number of boxed and unboxed pages within the
741 if (flags & PAGE_UNBOXED_MASK)
742 if (flags & PAGE_LARGE_OBJECT_MASK)
746 else if (flags & PAGE_LARGE_OBJECT_MASK)
754 gc_assert(generations[i].bytes_allocated ==
755 generation_bytes_allocated(i));
756 fprintf(stderr, " %5d: %5d %5d %5d %5d %10d %6d %10d %4d %3d %7.4f\n",
757 i, boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
758 generations[i].bytes_allocated,
759 GC_PAGE_SIZE * count_generation_pages(i) -
760 generations[i].bytes_allocated, generations[i].gc_trigger,
761 count_write_protect_generation_pages(i), generations[i].num_gc,
762 (double)gen_av_mem_age(i) / MEM_AGE_SCALE);
764 fprintf(stderr, " Total bytes alloc=%ld\n", bytes_allocated);
766 restore_fpu_state(fpu_state);
769 /* Get statistics that are kept "on the fly" out of the generation
773 get_generation_stats(int gen, struct generation_stats *stats)
775 if (gen <= NUM_GENERATIONS) {
776 stats->bytes_allocated = generations[gen].bytes_allocated;
777 stats->gc_trigger = generations[gen].gc_trigger;
778 stats->bytes_consed_between_gc =
779 generations[gen].bytes_consed_between_gc;
780 stats->num_gc = generations[gen].num_gc;
781 stats->trigger_age = generations[gen].trigger_age;
782 stats->cum_sum_bytes_allocated =
783 generations[gen].cum_sum_bytes_allocated;
784 stats->min_av_mem_age = generations[gen].min_av_mem_age;
789 set_gc_trigger(int gen, int trigger)
791 if (gen <= NUM_GENERATIONS) {
792 generations[gen].gc_trigger = trigger;
797 set_trigger_age(int gen, int trigger_age)
799 if (gen <= NUM_GENERATIONS) {
800 generations[gen].trigger_age = trigger_age;
805 set_min_mem_age(int gen, double min_mem_age)
807 if (gen <= NUM_GENERATIONS) {
808 generations[gen].min_av_mem_age = min_mem_age * MEM_AGE_SCALE;
813 * Allocation routines.
816 * To support quick and inline allocation, regions of memory can be
817 * allocated and then allocated from with just a free pointer and a
818 * check against an end address.
820 * Since objects can be allocated to spaces with different properties
821 * e.g. boxed/unboxed, generation, ages; there may need to be many
822 * allocation regions.
824 * Each allocation region may be start within a partly used page.
825 * Many features of memory use are noted on a page wise basis,
826 * E.g. the generation; so if a region starts within an existing
827 * allocated page it must be consistent with this page.
829 * During the scavenging of the newspace, objects will be transported
830 * into an allocation region, and pointers updated to point to this
831 * allocation region. It is possible that these pointers will be
832 * scavenged again before the allocation region is closed, E.g. due to
833 * trans_list which jumps all over the place to cleanup the list. It
834 * is important to be able to determine properties of all objects
835 * pointed to when scavenging, E.g to detect pointers to the
836 * oldspace. Thus it's important that the allocation regions have the
837 * correct properties set when allocated, and not just set when
838 * closed. The region allocation routines return regions with the
839 * specified properties, and grab all the pages, setting there
840 * properties appropriately, except that the amount used is not known.
842 * These regions are used to support quicker allocation using just a
843 * free pointer. The actual space used by the region is not reflected
844 * in the pages tables until it is closed. It can't be scavenged until
847 * When finished with the region it should be closed, which will
848 * update the page tables for the actual space used returning unused
849 * space. Further it may be noted in the new regions which is
850 * necessary when scavenging the newspace.
852 * Large objects may be allocated directly without an allocation
853 * region, the page tables are updated immediately.
855 * Unboxed objects don't contain points to other objects so don't need
856 * scavenging. Further they can't contain pointers to younger
857 * generations so WP is not needed. By allocating pages to unboxed
858 * objects the whole page never needs scavenging or write protecting.
862 * Only using two regions at present, both are for the current
863 * newspace generation.
865 struct alloc_region boxed_region;
866 struct alloc_region unboxed_region;
870 * X hack. current lisp code uses the following. Need coping in/out.
872 void *current_region_free_pointer;
873 void *current_region_end_addr;
876 /* The generation currently being allocated to. X */
877 static int gc_alloc_generation = 0;
879 extern void do_dynamic_space_overflow_warning(void);
880 extern void do_dynamic_space_overflow_error(void);
882 /* Handle heap overflow here, maybe. */
884 handle_heap_overflow(const char *msg, int size)
886 unsigned long heap_size_mb;
889 fprintf(stderr, msg, size);
891 #ifndef SPARSE_BLOCK_SIZE
892 #define SPARSE_BLOCK_SIZE (0)
895 /* Figure out how many MB of heap we have */
896 heap_size_mb = (dynamic_space_size + SPARSE_BLOCK_SIZE) >> 20;
898 fprintf(stderr, " CMUCL has run out of dynamic heap space (%lu MB).\n",
900 /* Try to handle heap overflow somewhat gracefully if we can. */
901 #if defined(trap_DynamicSpaceOverflow) || defined(FEATURE_HEAP_OVERFLOW_CHECK)
902 if (reserved_heap_pages == 0) {
903 fprintf(stderr, "\n Returning to top-level.\n");
904 do_dynamic_space_overflow_error();
907 " You can control heap size with the -dynamic-space-size commandline option.\n");
908 do_dynamic_space_overflow_warning();
911 print_generation_stats(1);
918 * Find a new region with room for at least the given number of bytes.
920 * It starts looking at the current generations alloc_start_page. So
921 * may pick up from the previous region if there is enough space. This
922 * keeps the allocation contiguous when scavenging the newspace.
924 * The alloc_region should have been closed by a call to
925 * gc_alloc_update_page_tables, and will thus be in an empty state.
927 * To assist the scavenging functions, write protected pages are not
928 * used. Free pages should not be write protected.
930 * It is critical to the conservative GC that the start of regions be
931 * known. To help achieve this only small regions are allocated at a
934 * During scavenging, pointers may be found that point within the
935 * current region and the page generation must be set so pointers to
936 * the from space can be recognised. So the generation of pages in
937 * the region are set to gc_alloc_generation. To prevent another
938 * allocation call using the same pages, all the pages in the region
939 * are allocated, although they will initially be empty.
942 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
953 /* Shut up some compiler warnings */
954 last_page = bytes_found = 0;
957 fprintf(stderr, "alloc_new_region for %d bytes from gen %d\n",
958 nbytes, gc_alloc_generation);
961 /* Check that the region is in a reset state. */
962 gc_assert(alloc_region->first_page == 0
963 && alloc_region->last_page == -1
964 && alloc_region->free_pointer == alloc_region->end_addr);
968 generations[gc_alloc_generation].alloc_unboxed_start_page;
970 restart_page = generations[gc_alloc_generation].alloc_start_page;
973 * Search for a contiguous free region of at least nbytes with the
974 * given properties: boxed/unboxed, generation. First setting up the
975 * mask and matching flags.
978 mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
979 | PAGE_LARGE_OBJECT_MASK | PAGE_DONT_MOVE_MASK
980 | PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK;
981 mflags = PAGE_ALLOCATED_MASK | (unboxed << PAGE_UNBOXED_SHIFT)
982 | gc_alloc_generation;
985 first_page = restart_page;
988 * First search for a page with at least 32 bytes free, that is
989 * not write protected, or marked dont_move.
992 while (first_page < dynamic_space_pages) {
993 int flags = page_table[first_page].flags;
995 if (!(flags & PAGE_ALLOCATED_MASK)
996 || ((flags & mmask) == mflags &&
997 page_table[first_page].bytes_used < GC_PAGE_SIZE - 32))
1002 /* Check for a failure */
1003 if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1005 handle_heap_overflow("*A2 gc_alloc_new_region failed, nbytes=%d.\n",
1012 gc_assert(!PAGE_WRITE_PROTECTED(first_page));
1015 fprintf(stderr, " first_page=%d bytes_used=%d\n",
1016 first_page, page_table[first_page].bytes_used);
1020 * Now search forward to calculate the available region size. It
1021 * tries to keeps going until nbytes are found and the number of
1022 * pages is greater than some level. This helps keep down the
1023 * number of pages in a region.
1025 last_page = first_page;
1026 bytes_found = GC_PAGE_SIZE - page_table[first_page].bytes_used;
1028 while ((bytes_found < nbytes || num_pages < 2)
1029 && last_page < dynamic_space_pages - 1
1030 && !PAGE_ALLOCATED(last_page + 1)) {
1033 bytes_found += GC_PAGE_SIZE;
1034 gc_assert(!PAGE_WRITE_PROTECTED(last_page));
1037 region_size = (GC_PAGE_SIZE - page_table[first_page].bytes_used)
1038 + GC_PAGE_SIZE * (last_page - first_page);
1040 gc_assert(bytes_found == region_size);
1043 fprintf(stderr, " last_page=%d bytes_found=%d num_pages=%d\n",
1044 last_page, bytes_found, num_pages);
1047 restart_page = last_page + 1;
1049 while (restart_page < dynamic_space_pages && bytes_found < nbytes);
1051 if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1052 handle_heap_overflow("*A2 gc_alloc_new_region failed, nbytes=%d.\n",
1056 /* Check for a failure */
1057 if (restart_page >= (dynamic_space_pages - reserved_heap_pages)
1058 && bytes_found < nbytes) {
1059 handle_heap_overflow("*A1 gc_alloc_new_region failed, nbytes=%d.\n",
1064 "gc_alloc_new_region gen %d: %d bytes: from pages %d to %d: addr=%x\n",
1065 gc_alloc_generation, bytes_found, first_page, last_page,
1066 page_address(first_page));
1069 /* Setup the alloc_region. */
1070 alloc_region->first_page = first_page;
1071 alloc_region->last_page = last_page;
1072 alloc_region->start_addr = page_table[first_page].bytes_used
1073 + page_address(first_page);
1074 alloc_region->free_pointer = alloc_region->start_addr;
1075 alloc_region->end_addr = alloc_region->start_addr + bytes_found;
1077 if (gencgc_zero_check) {
1080 for (p = (int *) alloc_region->start_addr;
1081 p < (int *) alloc_region->end_addr; p++)
1083 fprintf(stderr, "** new region not zero @ %lx\n",
1087 /* Setup the pages. */
1089 /* The first page may have already been in use. */
1090 if (page_table[first_page].bytes_used == 0) {
1091 PAGE_FLAGS_UPDATE(first_page, mmask, mflags);
1092 page_table[first_page].first_object_offset = 0;
1095 gc_assert(PAGE_ALLOCATED(first_page));
1096 gc_assert(PAGE_UNBOXED_VAL(first_page) == unboxed);
1097 gc_assert(PAGE_GENERATION(first_page) == gc_alloc_generation);
1098 gc_assert(!PAGE_LARGE_OBJECT(first_page));
1100 for (i = first_page + 1; i <= last_page; i++) {
1101 PAGE_FLAGS_UPDATE(i, PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK
1102 | PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK,
1103 PAGE_ALLOCATED_MASK | (unboxed << PAGE_UNBOXED_SHIFT)
1104 | gc_alloc_generation);
1106 * This may not be necessary for unboxed regions (think it was
1109 page_table[i].first_object_offset =
1110 alloc_region->start_addr - page_address(i);
1113 /* Bump up the last_free_page */
1114 if (last_page + 1 > last_free_page) {
1115 last_free_page = last_page + 1;
1116 set_alloc_pointer((lispobj) ((char *) heap_base +
1117 GC_PAGE_SIZE * last_free_page));
1125 * If the record_new_objects flag is 2 then all new regions created
1128 * If it's 1 then it is only recorded if the first page of the
1129 * current region is <= new_areas_ignore_page. This helps avoid
1130 * unnecessary recording when doing full scavenge pass.
1132 * The new_object structure holds the page, byte offset, and size of
1133 * new regions of objects. Each new area is placed in the array of
1134 * these structures pointed to by new_areas; new_areas_index holds the
1135 * offset into new_areas.
1137 * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
1138 * later code must detect this an handle it, probably by doing a full
1139 * scavenge of a generation.
1142 #define NUM_NEW_AREAS 512
1143 static int record_new_objects = 0;
1144 static int new_areas_ignore_page;
1150 static struct new_area (*new_areas)[];
1151 static int new_areas_index = 0;
1154 /* Add a new area to new_areas. */
1156 add_new_area(int first_page, int offset, int size)
1158 unsigned new_area_start, c;
1161 /* Ignore if full */
1162 if (new_areas_index >= NUM_NEW_AREAS)
1165 switch (record_new_objects) {
1169 if (first_page > new_areas_ignore_page)
1178 new_area_start = GC_PAGE_SIZE * first_page + offset;
1181 * Search backwards for a prior area that this follows from. If
1182 * found this will save adding a new area.
1184 for (i = new_areas_index - 1, c = 0; i >= 0 && c < 8; i--, c++) {
1185 unsigned area_end = GC_PAGE_SIZE * (*new_areas)[i].page
1186 + (*new_areas)[i].offset + (*new_areas)[i].size;
1189 fprintf(stderr, "*S1 %d %d %d %d\n", i, c, new_area_start, area_end);
1191 if (new_area_start == area_end) {
1193 fprintf(stderr, "-> Adding to [%d] %d %d %d with %d %d %d:\n",
1194 i, (*new_areas)[i].page, (*new_areas)[i].offset,
1195 (*new_areas)[i].size, first_page, offset, size);
1197 (*new_areas)[i].size += size;
1202 fprintf(stderr, "*S1 %d %d %d\n", i, c, new_area_start);
1205 (*new_areas)[new_areas_index].page = first_page;
1206 (*new_areas)[new_areas_index].offset = offset;
1207 (*new_areas)[new_areas_index].size = size;
1209 fprintf(stderr, " new_area %d page %d offset %d size %d\n",
1210 new_areas_index, first_page, offset, size);
1214 /* Note the max new_areas used. */
1215 if (new_areas_index > max_new_areas)
1216 max_new_areas = new_areas_index;
1221 * Update the tables for the alloc_region. The region may be added to
1224 * When done the alloc_region its setup so that the next quick alloc
1225 * will fail safely and thus a new region will be allocated. Further
1226 * it is safe to try and re-update the page table of this reset
1230 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
1236 int orig_first_page_bytes_used;
1241 fprintf(stderr, "gc_alloc_update_page_tables to gen %d: ",
1242 gc_alloc_generation);
1245 first_page = alloc_region->first_page;
1247 /* Catch an unused alloc_region. */
1248 if (first_page == 0 && alloc_region->last_page == -1)
1251 next_page = first_page + 1;
1253 /* Skip if no bytes were allocated */
1254 if (alloc_region->free_pointer != alloc_region->start_addr) {
1255 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1257 gc_assert(alloc_region->start_addr == page_address(first_page) +
1258 page_table[first_page].bytes_used);
1260 /* All the pages used need to be updated */
1262 /* Update the first page. */
1265 fprintf(stderr, "0");
1268 /* If the page was free then setup the gen, and first_object_offset. */
1269 if (page_table[first_page].bytes_used == 0)
1270 gc_assert(page_table[first_page].first_object_offset == 0);
1272 gc_assert(PAGE_ALLOCATED(first_page));
1273 gc_assert(PAGE_UNBOXED_VAL(first_page) == unboxed);
1274 gc_assert(PAGE_GENERATION(first_page) == gc_alloc_generation);
1275 gc_assert(!PAGE_LARGE_OBJECT(first_page));
1280 * Calc. the number of bytes used in this page. This is not always
1281 * the number of new bytes, unless it was free.
1284 bytes_used = alloc_region->free_pointer - page_address(first_page);
1285 if (bytes_used > GC_PAGE_SIZE) {
1286 bytes_used = GC_PAGE_SIZE;
1289 page_table[first_page].bytes_used = bytes_used;
1290 byte_cnt += bytes_used;
1293 * All the rest of the pages should be free. Need to set their
1294 * first_object_offset pointer to the start of the region, and set
1299 fprintf(stderr, "+");
1301 gc_assert(PAGE_ALLOCATED(next_page));
1302 gc_assert(PAGE_UNBOXED_VAL(next_page) == unboxed);
1303 gc_assert(page_table[next_page].bytes_used == 0);
1304 gc_assert(PAGE_GENERATION(next_page) == gc_alloc_generation);
1305 gc_assert(!PAGE_LARGE_OBJECT(next_page));
1307 gc_assert(page_table[next_page].first_object_offset ==
1308 alloc_region->start_addr - page_address(next_page));
1310 /* Calc. the number of bytes used in this page. */
1312 bytes_used = alloc_region->free_pointer - page_address(next_page);
1313 if (bytes_used > GC_PAGE_SIZE) {
1314 bytes_used = GC_PAGE_SIZE;
1317 page_table[next_page].bytes_used = bytes_used;
1318 byte_cnt += bytes_used;
1323 region_size = alloc_region->free_pointer - alloc_region->start_addr;
1324 bytes_allocated += region_size;
1325 generations[gc_alloc_generation].bytes_allocated += region_size;
1327 gc_assert(byte_cnt - orig_first_page_bytes_used == region_size);
1330 * Set the generations alloc restart page to the last page of
1334 generations[gc_alloc_generation].alloc_unboxed_start_page =
1337 generations[gc_alloc_generation].alloc_start_page = next_page - 1;
1339 /* Add the region to the new_areas if requested. */
1341 add_new_area(first_page, orig_first_page_bytes_used, region_size);
1345 " gc_alloc_update_page_tables update %d bytes to gen %d\n",
1346 region_size, gc_alloc_generation);
1350 * No bytes allocated. Unallocate the first_page if there are 0 bytes_used.
1352 if (page_table[first_page].bytes_used == 0)
1353 page_table[first_page].flags &= ~PAGE_ALLOCATED_MASK;
1355 /* Unallocate any unused pages. */
1356 while (next_page <= alloc_region->last_page) {
1357 gc_assert(page_table[next_page].bytes_used == 0);
1358 page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
1362 /* Reset the alloc_region. */
1363 alloc_region->first_page = 0;
1364 alloc_region->last_page = -1;
1365 alloc_region->start_addr = page_address(0);
1366 alloc_region->free_pointer = page_address(0);
1367 alloc_region->end_addr = page_address(0);
1370 fprintf(stderr, "\n");
1376 static inline void *gc_quick_alloc(int nbytes);
1379 * Allocate a possibly large object.
1382 gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
1390 int orig_first_page_bytes_used;
1395 int large = (nbytes >= large_object_size);
1399 /* Shut up some compiler warnings */
1400 last_page = bytes_found = 0;
1403 if (nbytes > 200000)
1404 fprintf(stderr, "*** alloc_large %d\n", nbytes);
1408 fprintf(stderr, "gc_alloc_large for %d bytes from gen %d\n",
1409 nbytes, gc_alloc_generation);
1413 * If the object is small, and there is room in the current region
1414 * then allocation it in the current region.
1416 if (!large && alloc_region->end_addr - alloc_region->free_pointer >= nbytes)
1417 return gc_quick_alloc(nbytes);
1420 * Search for a contiguous free region of at least nbytes. If it's a
1421 * large object then align it on a page boundary by searching for a
1426 * To allow the allocation of small objects without the danger of
1427 * using a page in the current boxed region, the search starts after
1428 * the current boxed free region. XX could probably keep a page
1429 * index ahead of the current region and bumped up here to save a
1430 * lot of re-scanning.
1434 generations[gc_alloc_generation].alloc_large_unboxed_start_page;
1436 restart_page = generations[gc_alloc_generation].alloc_large_start_page;
1437 if (restart_page <= alloc_region->last_page)
1438 restart_page = alloc_region->last_page + 1;
1440 /* Setup the mask and matching flags. */
1442 mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
1443 | PAGE_LARGE_OBJECT_MASK | PAGE_DONT_MOVE_MASK
1444 | PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK;
1445 mflags = PAGE_ALLOCATED_MASK | (unboxed << PAGE_UNBOXED_SHIFT)
1446 | gc_alloc_generation;
1449 first_page = restart_page;
1452 while (first_page < dynamic_space_pages
1453 && PAGE_ALLOCATED(first_page)) first_page++;
1455 while (first_page < dynamic_space_pages) {
1456 int flags = page_table[first_page].flags;
1458 if (!(flags & PAGE_ALLOCATED_MASK)
1459 || ((flags & mmask) == mflags &&
1460 page_table[first_page].bytes_used < GC_PAGE_SIZE - 32))
1465 /* Check for a failure */
1466 if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1468 handle_heap_overflow("*A2 gc_alloc_large failed, nbytes=%d.\n",
1474 gc_assert(!PAGE_WRITE_PROTECTED(first_page));
1477 fprintf(stderr, " first_page=%d bytes_used=%d\n",
1478 first_page, page_table[first_page].bytes_used);
1481 last_page = first_page;
1482 bytes_found = GC_PAGE_SIZE - page_table[first_page].bytes_used;
1484 while (bytes_found < nbytes
1485 && last_page < dynamic_space_pages - 1
1486 && !PAGE_ALLOCATED(last_page + 1)) {
1489 bytes_found += GC_PAGE_SIZE;
1490 gc_assert(!PAGE_WRITE_PROTECTED(last_page));
1493 region_size = (GC_PAGE_SIZE - page_table[first_page].bytes_used)
1494 + GC_PAGE_SIZE * (last_page - first_page);
1496 gc_assert(bytes_found == region_size);
1499 fprintf(stderr, " last_page=%d bytes_found=%d num_pages=%d\n",
1500 last_page, bytes_found, num_pages);
1503 restart_page = last_page + 1;
1505 while ((restart_page < dynamic_space_pages) && (bytes_found < nbytes));
1507 if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1508 handle_heap_overflow("*A2 gc_alloc_large failed, nbytes=%d.\n", nbytes);
1511 /* Check for a failure */
1512 if (restart_page >= (dynamic_space_pages - reserved_heap_pages)
1513 && bytes_found < nbytes) {
1514 handle_heap_overflow("*A1 gc_alloc_large failed, nbytes=%d.\n", nbytes);
1519 "gc_alloc_large gen %d: %d of %d bytes: from pages %d to %d: addr=%x\n",
1520 gc_alloc_generation, nbytes, bytes_found, first_page, last_page,
1521 page_address(first_page));
1524 gc_assert(first_page > alloc_region->last_page);
1526 generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1529 generations[gc_alloc_generation].alloc_large_start_page = last_page;
1531 /* Setup the pages. */
1532 orig_first_page_bytes_used = page_table[first_page].bytes_used;
1535 * If the first page was free then setup the gen, and
1536 * first_object_offset.
1540 mflags |= PAGE_LARGE_OBJECT_MASK;
1541 if (page_table[first_page].bytes_used == 0) {
1542 PAGE_FLAGS_UPDATE(first_page, mmask, mflags);
1543 page_table[first_page].first_object_offset = 0;
1546 gc_assert(PAGE_ALLOCATED(first_page));
1547 gc_assert(PAGE_UNBOXED_VAL(first_page) == unboxed);
1548 gc_assert(PAGE_GENERATION(first_page) == gc_alloc_generation);
1549 gc_assert(PAGE_LARGE_OBJECT_VAL(first_page) == large);
1554 * Calc. the number of bytes used in this page. This is not
1555 * always the number of new bytes, unless it was free.
1558 bytes_used = nbytes + orig_first_page_bytes_used;
1559 if (bytes_used > GC_PAGE_SIZE) {
1560 bytes_used = GC_PAGE_SIZE;
1563 page_table[first_page].bytes_used = bytes_used;
1564 byte_cnt += bytes_used;
1566 next_page = first_page + 1;
1569 * All the rest of the pages should be free. Need to set their
1570 * first_object_offset pointer to the start of the region, and set
1575 fprintf(stderr, "+");
1578 gc_assert(!PAGE_ALLOCATED(next_page));
1579 gc_assert(page_table[next_page].bytes_used == 0);
1580 PAGE_FLAGS_UPDATE(next_page, mmask, mflags);
1582 page_table[next_page].first_object_offset =
1583 orig_first_page_bytes_used - GC_PAGE_SIZE * (next_page - first_page);
1585 /* Calc. the number of bytes used in this page. */
1587 bytes_used = nbytes + orig_first_page_bytes_used - byte_cnt;
1588 if (bytes_used > GC_PAGE_SIZE) {
1589 bytes_used = GC_PAGE_SIZE;
1592 page_table[next_page].bytes_used = bytes_used;
1593 byte_cnt += bytes_used;
1598 gc_assert(byte_cnt - orig_first_page_bytes_used == nbytes);
1600 bytes_allocated += nbytes;
1601 generations[gc_alloc_generation].bytes_allocated += nbytes;
1603 /* Add the region to the new_areas if requested. */
1605 add_new_area(first_page, orig_first_page_bytes_used, nbytes);
1607 /* Bump up the last_free_page */
1608 if (last_page + 1 > last_free_page) {
1609 last_free_page = last_page + 1;
1610 set_alloc_pointer((lispobj) ((char *) heap_base +
1611 GC_PAGE_SIZE * last_free_page));
1614 return (void *) (page_address(first_page) + orig_first_page_bytes_used);
1618 * If the current region has more than this much space left, we don't
1619 * want to abandon the region (wasting space), but do a "large" alloc
1623 int region_empty_threshold = 32;
1627 * How many consecutive large alloc we can do before we abandon the
1630 int consecutive_large_alloc_limit = 10;
1634 * Statistics for the current region
1639 * How many consecutive allocations we have tried with the current
1640 * region (in saved_region)
1642 int consecutive_alloc;
1644 * How many times we tried to allocate to this region but didn't
1645 * because we didn't have enough room and did a large alloc in a
1648 int abandon_region_count;
1651 * A copy of the current allocation region which we use to compare
1654 struct alloc_region saved_region;
1657 /* Statistics for boxed and unboxed regions */
1658 struct alloc_stats boxed_stats =
1660 {NULL, NULL, -1, -1, NULL}};
1662 struct alloc_stats unboxed_stats =
1664 {NULL, NULL, -1, -1, NULL}};
1667 * Try to allocate from the current region. If it's possible, do the
1668 * allocation and return the object. If it's not possible, return
1671 static inline void *
1672 gc_alloc_try_current_region(int nbytes, struct alloc_region *region, int unboxed,
1673 struct alloc_stats *stats)
1675 char *new_free_pointer;
1677 /* Check if there is room in the current alloc region. */
1678 new_free_pointer = region->free_pointer + nbytes;
1680 if (new_free_pointer <= region->end_addr) {
1681 /* If so then allocate from the current alloc region. */
1682 char *new_obj = region->free_pointer;
1684 region->free_pointer = new_free_pointer;
1686 /* Check if the alloc region is almost empty. */
1687 if (region->end_addr - region->free_pointer <= region_empty_threshold) {
1688 /* If so finished with the current region. */
1689 gc_alloc_update_page_tables(unboxed, region);
1690 /* Setup a new region. */
1691 gc_alloc_new_region(region_empty_threshold, unboxed, region);
1694 stats->consecutive_alloc = 0;
1695 stats->abandon_region_count = 0;
1696 memcpy(&stats->saved_region, region, sizeof(stats->saved_region));
1698 return (void *) new_obj;
1705 * Allocate bytes from a boxed or unboxed region. It first checks if
1706 * there is room, if not then it calls gc_alloc_new_region to find a
1707 * new region with enough space. A pointer to the start of the region
1708 * is returned. The parameter "unboxed" should be 0 (boxed) or 1
1712 gc_alloc_region(int nbytes, struct alloc_region *region, int unboxed, struct alloc_stats *stats)
1717 fprintf(stderr, "gc_alloc %d\n", nbytes);
1720 /* Check if there is room in the current alloc region. */
1722 new_obj = gc_alloc_try_current_region(nbytes, region, unboxed, stats);
1723 if (new_obj != (void *) -1) {
1727 /* Else not enough free space in the current region. */
1730 * If the allocation is large enough, always do a large alloc This
1731 * helps GC so we don't have to copy this object again.
1734 if (nbytes >= large_object_size) {
1735 return gc_alloc_large(nbytes, unboxed, region);
1739 * If there is a bit of room left in the current region then
1740 * allocate a large object.
1744 * This has potentially very bad behavior on sparc if the current
1745 * boxed region is too small for the allocation, but the free
1746 * space is greater than 32 (region_empty_threshold). The
1747 * scenario is where we're always allocating something that won't
1748 * fit in the boxed region, and we keep calling gc_alloc_large.
1749 * Since gc_alloc_large doesn't change the region, the next
1750 * allocation will again be out-of-line and we hit a kernel trap
1751 * again. And so on, so we waste all of our time doing kernel
1752 * traps to allocate small things. This also affects ppc.
1754 * X86 has the same issue, but the affect is less because the
1755 * out-of-line allocation is a just a function call, not a kernel
1758 * Heuristic: If we do too many consecutive large allocations
1759 * because the current region has some space left, we give up and
1760 * abandon the region. This will prevent the bad scenario above
1761 * from killing allocation performance.
1765 if ((region->end_addr - region->free_pointer > region_empty_threshold)
1766 && (stats->consecutive_alloc < consecutive_large_alloc_limit)) {
1768 * Is the saved region the same as the current region? If so,
1769 * update the counter. If not, that means we did some other
1770 * (inline) allocation, so reset the counters and region to
1771 * the current region.
1773 if (memcmp(&stats->saved_region, region, sizeof(stats->saved_region)) == 0) {
1774 ++stats->consecutive_alloc;
1776 stats->consecutive_alloc = 0;
1777 stats->abandon_region_count = 0;
1778 memcpy(&stats->saved_region, region, sizeof(stats->saved_region));
1781 return gc_alloc_large(nbytes, unboxed, region);
1785 * We given up on the current region because the
1786 * consecutive_large_alloc_limit has been reached.
1788 stats->consecutive_alloc = 0;
1789 ++stats->abandon_region_count;
1791 /* Finished with the current region. */
1792 gc_alloc_update_page_tables(unboxed, region);
1794 /* Setup a new region. */
1795 gc_alloc_new_region(nbytes, unboxed, region);
1797 /* Should now be enough room. */
1799 new_obj = gc_alloc_try_current_region(nbytes, region, unboxed, stats);
1800 if (new_obj != (void *) -1) {
1804 /* Shouldn't happen? */
1810 * Allocate bytes from the boxed_region. It first checks if there is
1811 * room, if not then it calls gc_alloc_new_region to find a new region
1812 * with enough space. A pointer to the start of the region is returned.
1814 static inline void *
1815 gc_alloc(int nbytes)
1819 obj = gc_alloc_region(nbytes, &boxed_region, 0, &boxed_stats);
1825 * Allocate space from the boxed_region. If there is not enough free
1826 * space then call gc_alloc to do the job. A pointer to the start of
1827 * the region is returned.
1829 static inline void *
1830 gc_quick_alloc(int nbytes)
1832 char *new_free_pointer;
1834 /* Check if there is room in the current region. */
1835 new_free_pointer = boxed_region.free_pointer + nbytes;
1837 if (new_free_pointer <= boxed_region.end_addr) {
1838 /* If so then allocate from the current region. */
1839 void *new_obj = boxed_region.free_pointer;
1841 boxed_region.free_pointer = new_free_pointer;
1842 return (void *) new_obj;
1845 /* Else call gc_alloc */
1846 return gc_alloc(nbytes);
1850 * Allocate space for the boxed object. If it is a large object then
1851 * do a large alloc else allocate from the current region. If there is
1852 * not enough free space then call gc_alloc to do the job. A pointer
1853 * to the start of the region is returned.
1855 static inline void *
1856 gc_quick_alloc_large(int nbytes)
1858 char *new_free_pointer;
1860 if (nbytes >= large_object_size)
1861 return gc_alloc_large(nbytes, 0, &boxed_region);
1863 /* Check if there is room in the current region. */
1864 new_free_pointer = boxed_region.free_pointer + nbytes;
1866 if (new_free_pointer <= boxed_region.end_addr) {
1867 /* If so then allocate from the current region. */
1868 void *new_obj = boxed_region.free_pointer;
1870 boxed_region.free_pointer = new_free_pointer;
1871 return (void *) new_obj;
1874 /* Else call gc_alloc */
1875 return gc_alloc(nbytes);
1878 static inline void *
1879 gc_alloc_unboxed(int nbytes)
1883 obj = gc_alloc_region(nbytes, &unboxed_region, 1, &unboxed_stats);
1888 static inline void *
1889 gc_quick_alloc_unboxed(int nbytes)
1891 char *new_free_pointer;
1893 /* Check if there is room in the current region. */
1894 new_free_pointer = unboxed_region.free_pointer + nbytes;
1896 if (new_free_pointer <= unboxed_region.end_addr) {
1897 /* If so then allocate from the current region. */
1898 void *new_obj = unboxed_region.free_pointer;
1900 unboxed_region.free_pointer = new_free_pointer;
1902 return (void *) new_obj;
1905 /* Else call gc_alloc */
1906 return gc_alloc_unboxed(nbytes);
1910 * Allocate space for the object. If it is a large object then do a
1911 * large alloc else allocate from the current region. If there is not
1912 * enough free space then call gc_alloc to do the job.
1914 * A pointer to the start of the region is returned.
1916 static inline void *
1917 gc_quick_alloc_large_unboxed(int nbytes)
1919 char *new_free_pointer;
1921 if (nbytes >= large_object_size)
1922 return gc_alloc_large(nbytes, 1, &unboxed_region);
1924 /* Check if there is room in the current region. */
1925 new_free_pointer = unboxed_region.free_pointer + nbytes;
1927 if (new_free_pointer <= unboxed_region.end_addr) {
1928 /* If so then allocate from the current region. */
1929 void *new_obj = unboxed_region.free_pointer;
1931 unboxed_region.free_pointer = new_free_pointer;
1933 return (void *) new_obj;
1936 /* Else call gc_alloc */
1937 return gc_alloc_unboxed(nbytes);
1940 /***************************************************************************/
1943 /* Scavenging/transporting routines derived from gc.c */
1945 static int (*scavtab[256]) (lispobj * where, lispobj object);
1946 static lispobj(*transother[256]) (lispobj object);
1947 static int (*sizetab[256]) (lispobj * where);
1949 static struct weak_pointer *weak_pointers;
1950 static struct scavenger_hook *scavenger_hooks = (struct scavenger_hook *) NIL;
1952 /* Like (ceiling x y), but y is constrained to be a power of two */
1953 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
1958 static inline boolean
1959 from_space_p(lispobj obj)
1961 int page_index = (char *) obj - heap_base;
1963 return page_index >= 0
1965 (unsigned int) page_index / GC_PAGE_SIZE) < dynamic_space_pages
1966 && PAGE_GENERATION(page_index) == from_space;
1969 static inline boolean
1970 new_space_p(lispobj obj)
1972 int page_index = (char *) obj - heap_base;
1974 return page_index >= 0
1976 (unsigned int) page_index / GC_PAGE_SIZE) < dynamic_space_pages
1977 && PAGE_GENERATION(page_index) == new_space;
1980 static inline boolean
1981 dynamic_space_p(lispobj obj)
1983 lispobj end = DYNAMIC_0_SPACE_START + DYNAMIC_SPACE_SIZE;
1985 return (obj >= DYNAMIC_0_SPACE_START) && (obj < end);
1988 static inline boolean
1989 static_space_p(lispobj obj)
1991 lispobj end = SymbolValue(STATIC_SPACE_FREE_POINTER);
1993 return (obj >= STATIC_SPACE_START) && (obj < end);
1996 static inline boolean
1997 read_only_space_p(lispobj obj)
1999 lispobj end = SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
2001 return (obj >= READ_ONLY_SPACE_START) && (obj < end);
2004 static inline boolean
2005 control_stack_space_p(lispobj obj)
2007 lispobj end = CONTROL_STACK_START + control_stack_size;
2009 return (obj >= CONTROL_STACK_START) && (obj < end);
2012 static inline boolean
2013 binding_stack_space_p(lispobj obj)
2015 lispobj end = BINDING_STACK_START + binding_stack_size;
2017 return (obj >= BINDING_STACK_START) && (obj < end);
2020 static inline boolean
2021 signal_space_p(lispobj obj)
2023 #ifdef SIGNAL_STACK_START
2024 lispobj end = SIGNAL_STACK_START + SIGSTKSZ;
2026 return (obj >= SIGNAL_STACK_START) && (obj < end);
2032 #if (defined(DARWIN) && defined(__ppc__))
2034 * The assembly code defines these as functions, so we make them
2035 * functions. We only care about their addresses anyway.
2037 extern char closure_tramp();
2038 extern char undefined_tramp();
2039 #elif defined(sparc)
2040 /* closure tramp and undefined tramp are Lisp assembly routines */
2041 #elif (defined(i386) || defined(__x86_64))
2042 /* undefined tramp are Lisp assembly routines */
2044 extern int undefined_tramp;
2048 * Other random places that can't be in malloc space. Return TRUE if
2049 * obj is in some other known space
2051 static inline boolean
2052 other_space_p(lispobj obj)
2054 boolean in_space = FALSE;
2060 * Skip over any objects in the C runtime which includes the
2061 * closure_tramp and undefined_tramp objects. There appears to be
2062 * one other object that points to somewhere in call_into_c, but I
2063 * don't know what that is. I think that's probably all for
2066 if ((char*) obj <= &_end) {
2070 #if defined(DARWIN) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
2072 * For x86, we see some object at 0xffffffe9. I (rtoy) am not
2073 * sure that is, but it clearly can't be in malloc space so we
2074 * want to skip that (by returning TRUE).
2076 * Is there anything else?
2078 if (obj == (lispobj) 0xffffffe9) {
2081 #elif defined(__ppc__)
2083 * For ppc, just ignore anything below fpu_restore, which is
2084 * currently at the end of ppc-assem.S.
2086 if (obj <= (lispobj) &fpu_restore) {
2097 /* Copying Objects */
2100 /* Copying Boxed Objects */
2101 static inline lispobj
2102 copy_object(lispobj object, int nwords)
2106 lispobj *source, *dest;
2108 gc_assert(Pointerp(object));
2109 gc_assert(from_space_p(object));
2110 gc_assert((nwords & 0x01) == 0);
2112 /* get tag of object */
2113 tag = LowtagOf(object);
2115 /* allocate space */
2116 new = gc_quick_alloc(nwords * sizeof(lispobj));
2119 source = (lispobj *) PTR(object);
2121 /* copy the object */
2122 while (nwords > 0) {
2123 dest[0] = source[0];
2124 dest[1] = source[1];
2130 /* return lisp pointer of new object */
2131 return (lispobj) new | tag;
2135 * Copying Large Boxed Objects. If the object is in a large object
2136 * region then it is simply promoted, else it is copied. If it's large
2137 * enough then it's copied to a large object region.
2139 * Vectors may have shrunk. If the object is not copied the space
2140 * needs to be reclaimed, and the page_tables corrected.
2143 copy_large_object(lispobj object, int nwords)
2147 lispobj *source, *dest;
2150 gc_assert(Pointerp(object));
2151 gc_assert(from_space_p(object));
2152 gc_assert((nwords & 0x01) == 0);
2154 if (gencgc_verbose && nwords > 1024 * 1024)
2155 fprintf(stderr, "** copy_large_object: %lu\n",
2156 (unsigned long) (nwords * sizeof(lispobj)));
2158 /* Check if it's a large object. */
2159 first_page = find_page_index((void *) object);
2160 gc_assert(first_page >= 0);
2162 if (PAGE_LARGE_OBJECT(first_page)) {
2163 /* Promote the object. */
2164 int remaining_bytes;
2171 * Note: Any page write protection must be removed, else a later
2172 * scavenge_newspace may incorrectly not scavenge these pages.
2173 * This would not be necessary if they are added to the new areas,
2174 * but lets do it for them all (they'll probably be written
2178 gc_assert(page_table[first_page].first_object_offset == 0);
2180 next_page = first_page;
2181 remaining_bytes = nwords * sizeof(lispobj);
2182 while (remaining_bytes > GC_PAGE_SIZE) {
2183 gc_assert(PAGE_GENERATION(next_page) == from_space);
2184 gc_assert(PAGE_ALLOCATED(next_page));
2185 gc_assert(!PAGE_UNBOXED(next_page));
2186 gc_assert(PAGE_LARGE_OBJECT(next_page));
2187 gc_assert(page_table[next_page].first_object_offset ==
2188 GC_PAGE_SIZE * (first_page - next_page));
2189 gc_assert(page_table[next_page].bytes_used == GC_PAGE_SIZE);
2191 PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
2194 * Remove any write protection. Should be able to religh on the
2195 * WP flag to avoid redundant calls.
2197 if (PAGE_WRITE_PROTECTED(next_page)) {
2198 os_protect((os_vm_address_t) page_address(next_page), GC_PAGE_SIZE,
2200 page_table[next_page].flags &= ~PAGE_WRITE_PROTECTED_MASK;
2202 remaining_bytes -= GC_PAGE_SIZE;
2207 * Now only one page remains, but the object may have shrunk so
2208 * there may be more unused pages which will be freed.
2211 /* Object may have shrunk but shouldn't have grown - check. */
2212 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2214 PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
2215 gc_assert(PAGE_ALLOCATED(next_page));
2216 gc_assert(!PAGE_UNBOXED(next_page));
2218 /* Adjust the bytes_used. */
2219 old_bytes_used = page_table[next_page].bytes_used;
2220 page_table[next_page].bytes_used = remaining_bytes;
2222 bytes_freed = old_bytes_used - remaining_bytes;
2224 mmask = PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK | PAGE_LARGE_OBJECT_MASK
2225 | PAGE_GENERATION_MASK;
2226 mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2228 /* Free any remaining pages; needs care. */
2230 while (old_bytes_used == GC_PAGE_SIZE &&
2231 PAGE_FLAGS(next_page, mmask) == mflags &&
2232 page_table[next_page].first_object_offset ==
2233 GC_PAGE_SIZE * (first_page - next_page)) {
2235 * Checks out OK, free the page. Don't need to both zeroing
2236 * pages as this should have been done before shrinking the
2237 * object. These pages shouldn't be write protected as they
2238 * should be zero filled.
2240 gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2242 old_bytes_used = page_table[next_page].bytes_used;
2243 page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
2244 page_table[next_page].bytes_used = 0;
2245 bytes_freed += old_bytes_used;
2249 if (gencgc_verbose && bytes_freed > 0)
2250 fprintf(stderr, "* copy_large_boxed bytes_freed %d\n", bytes_freed);
2252 generations[from_space].bytes_allocated -=
2253 sizeof(lispobj) * nwords + bytes_freed;
2254 generations[new_space].bytes_allocated += sizeof(lispobj) * nwords;
2255 bytes_allocated -= bytes_freed;
2257 /* Add the region to the new_areas if requested. */
2258 add_new_area(first_page, 0, nwords * sizeof(lispobj));
2262 /* get tag of object */
2263 tag = LowtagOf(object);
2265 /* allocate space */
2266 new = gc_quick_alloc_large(nwords * sizeof(lispobj));
2269 source = (lispobj *) PTR(object);
2271 /* copy the object */
2272 while (nwords > 0) {
2273 dest[0] = source[0];
2274 dest[1] = source[1];
2280 /* return lisp pointer of new object */
2281 return (lispobj) new | tag;
2285 /* Copying UnBoxed Objects. */
2286 static inline lispobj
2287 copy_unboxed_object(lispobj object, int nwords)
2291 lispobj *source, *dest;
2293 gc_assert(Pointerp(object));
2294 gc_assert(from_space_p(object));
2295 gc_assert((nwords & 0x01) == 0);
2297 /* get tag of object */
2298 tag = LowtagOf(object);
2300 /* allocate space */
2301 new = gc_quick_alloc_unboxed(nwords * sizeof(lispobj));
2304 source = (lispobj *) PTR(object);
2306 /* Copy the object */
2307 while (nwords > 0) {
2308 dest[0] = source[0];
2309 dest[1] = source[1];
2315 /* Return lisp pointer of new object. */
2316 return (lispobj) new | tag;
2321 * Copying Large Unboxed Objects. If the object is in a large object
2322 * region then it is simply promoted, else it is copied. If it's large
2323 * enough then it's copied to a large object region.
2325 * Bignums and vectors may have shrunk. If the object is not copied
2326 * the space needs to be reclaimed, and the page_tables corrected.
2329 copy_large_unboxed_object(lispobj object, int nwords)
2333 lispobj *source, *dest;
2336 gc_assert(Pointerp(object));
2337 gc_assert(from_space_p(object));
2338 gc_assert((nwords & 0x01) == 0);
2340 if (gencgc_verbose && nwords > 1024 * 1024)
2341 fprintf(stderr, "** copy_large_unboxed_object: %lu\n",
2342 (unsigned long) (nwords * sizeof(lispobj)));
2344 /* Check if it's a large object. */
2345 first_page = find_page_index((void *) object);
2346 gc_assert(first_page >= 0);
2348 if (PAGE_LARGE_OBJECT(first_page)) {
2350 * Promote the object. Note: Unboxed objects may have been
2351 * allocated to a BOXED region so it may be necessary to change
2352 * the region to UNBOXED.
2354 int remaining_bytes;
2360 gc_assert(page_table[first_page].first_object_offset == 0);
2362 next_page = first_page;
2363 remaining_bytes = nwords * sizeof(lispobj);
2364 while (remaining_bytes > GC_PAGE_SIZE) {
2365 gc_assert(PAGE_GENERATION(next_page) == from_space);
2366 gc_assert(PAGE_ALLOCATED(next_page));
2367 gc_assert(PAGE_LARGE_OBJECT(next_page));
2368 gc_assert(page_table[next_page].first_object_offset ==
2369 GC_PAGE_SIZE * (first_page - next_page));
2370 gc_assert(page_table[next_page].bytes_used == GC_PAGE_SIZE);
2372 PAGE_FLAGS_UPDATE(next_page,
2373 PAGE_UNBOXED_MASK | PAGE_GENERATION_MASK,
2374 PAGE_UNBOXED_MASK | new_space);
2375 remaining_bytes -= GC_PAGE_SIZE;
2380 * Now only one page remains, but the object may have shrunk so
2381 * there may be more unused pages which will be freed.
2384 /* Object may have shrunk but shouldn't have grown - check. */
2385 gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2387 PAGE_FLAGS_UPDATE(next_page, PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK
2388 | PAGE_GENERATION_MASK,
2389 PAGE_ALLOCATED_MASK | PAGE_UNBOXED_MASK | new_space);
2391 /* Adjust the bytes_used. */
2392 old_bytes_used = page_table[next_page].bytes_used;
2393 page_table[next_page].bytes_used = remaining_bytes;
2395 bytes_freed = old_bytes_used - remaining_bytes;
2397 mmask = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK
2398 | PAGE_GENERATION_MASK;
2399 mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2401 /* Free any remaining pages; needs care. */
2403 while (old_bytes_used == GC_PAGE_SIZE &&
2404 PAGE_FLAGS(next_page, mmask) == mflags &&
2405 page_table[next_page].first_object_offset ==
2406 GC_PAGE_SIZE * (first_page - next_page)) {
2408 * Checks out OK, free the page. Don't need to both zeroing
2409 * pages as this should have been done before shrinking the
2410 * object. These pages shouldn't be write protected, even if
2411 * boxed they should be zero filled.
2413 gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2415 old_bytes_used = page_table[next_page].bytes_used;
2416 page_table[next_page].flags &= ~PAGE_ALLOCATED_MASK;
2417 page_table[next_page].bytes_used = 0;
2418 bytes_freed += old_bytes_used;
2422 if (gencgc_verbose && bytes_freed > 0)
2423 fprintf(stderr, "* copy_large_unboxed bytes_freed %d\n",
2426 generations[from_space].bytes_allocated -=
2427 sizeof(lispobj) * nwords + bytes_freed;
2428 generations[new_space].bytes_allocated += sizeof(lispobj) * nwords;
2429 bytes_allocated -= bytes_freed;
2433 /* get tag of object */
2434 tag = LowtagOf(object);
2436 /* allocate space */
2437 new = gc_quick_alloc_large_unboxed(nwords * sizeof(lispobj));
2440 source = (lispobj *) PTR(object);
2442 /* copy the object */
2443 while (nwords > 0) {
2444 dest[0] = source[0];
2445 dest[1] = source[1];
2451 /* return lisp pointer of new object */
2452 return (lispobj) new | tag;
2456 static inline boolean
2457 maybe_static_array_p(lispobj header)
2461 switch (TypeOf(header)) {
2463 * This needs to be coordinated to the set of allowed
2464 * static vectors in make-array.
2466 case type_SimpleString:
2467 case type_SimpleArrayUnsignedByte8:
2468 case type_SimpleArrayUnsignedByte16:
2469 case type_SimpleArrayUnsignedByte32:
2470 #ifdef type_SimpleArraySignedByte8
2471 case type_SimpleArraySignedByte8:
2473 #ifdef type_SimpleArraySignedByte16
2474 case type_SimpleArraySignedByte16:
2476 #ifdef type_SimpleArraySignedByte32
2477 case type_SimpleArraySignedByte32:
2479 case type_SimpleArraySingleFloat:
2480 case type_SimpleArrayDoubleFloat:
2481 #ifdef type_SimpleArrayLongFloat
2482 case type_SimpleArrayLongFloat:
2484 #ifdef type_SimpleArrayComplexSingleFloat
2485 case type_SimpleArrayComplexSingleFloat:
2487 #ifdef type_SimpleArrayComplexDoubleFloat
2488 case type_SimpleArrayComplexDoubleFloat:
2490 #ifdef type_SimpleArrayComplexLongFloat
2491 case type_SimpleArrayComplexLongFloat:
2506 * Douglas Crosher says:
2508 * There were two different ways in which the scavenger dispatched,
2509 * and DIRECT_SCAV was one option. This code did work at one stage
2510 * but testing showed it to be slower. When DIRECT_SCAV is enabled
2511 * the scavenger dispatches via the scavtab for all objects, and when
2512 * disabled the scavenger firstly detects and handles some common
2513 * cases itself before dispatching.
2516 #define DIRECT_SCAV 0
2519 scavenge(void *start_obj, long nwords)
2523 start = (lispobj *) start_obj;
2525 while (nwords > 0) {
2527 int words_scavenged;
2530 /* Not a forwarding pointer. */
2531 gc_assert(object != 0x01);
2534 words_scavenged = scavtab[TypeOf(object)] (start, object);
2535 #else /* not DIRECT_SCAV */
2536 if (Pointerp(object)) {
2537 #ifdef GC_ASSERTIONS
2538 check_escaped_stack_object(start, object);
2541 if (from_space_p(object)) {
2542 lispobj *ptr = (lispobj *) PTR(object);
2543 lispobj first_word = *ptr;
2545 if (first_word == 0x01) {
2547 words_scavenged = 1;
2549 words_scavenged = scavtab[TypeOf(object)] (start, object);
2551 } else if (dynamic_space_p(object) || new_space_p(object) || static_space_p(object)
2552 || read_only_space_p(object) || control_stack_space_p(object)
2553 || binding_stack_space_p(object) || signal_space_p(object)
2554 || other_space_p(object)) {
2555 words_scavenged = 1;
2557 lispobj *ptr = (lispobj *) PTR(object);
2558 words_scavenged = 1;
2559 if (debug_static_array_p) {
2560 fprintf(stderr, "Not in Lisp spaces: object = %p, ptr = %p\n",
2561 (void*)object, ptr);
2565 lispobj header = *ptr;
2566 if (debug_static_array_p) {
2567 fprintf(stderr, " Header value = 0x%lx\n", (unsigned long) header);
2570 if (maybe_static_array_p(header)) {
2573 if (debug_static_array_p) {
2574 fprintf(stderr, "Possible static vector at %p. header = 0x%lx\n",
2575 ptr, (unsigned long) header);
2578 static_p = (HeaderValue(header) & 1) == 1;
2581 * We have a static vector. Mark it as
2582 * reachable by setting the MSB of the header.
2584 *ptr = header | 0x80000000;
2585 if (debug_static_array_p) {
2586 fprintf(stderr, "Scavenged static vector @%p, header = 0x%lx\n",
2587 ptr, (unsigned long) header);
2593 } else if ((object & 3) == 0)
2594 words_scavenged = 1;
2596 words_scavenged = scavtab[TypeOf(object)] (start, object);
2597 #endif /* not DIRECT_SCAV */
2599 start += words_scavenged;
2600 nwords -= words_scavenged;
2603 gc_assert(nwords == 0);
2607 #if !(defined(i386) || defined(__x86_64))
2608 /* Scavenging Interrupt Contexts */
2610 static int boxed_registers[] = BOXED_REGISTERS;
2612 /* The GC has a notion of an "interior pointer" register, an unboxed
2613 * register that typically contains a pointer to inside an object
2614 * referenced by another pointer. The most obvious of these is the
2615 * program counter, although many compiler backends define a "Lisp
2616 * Interior Pointer" register known as reg_LIP, and various CPU
2617 * architectures have other registers that also partake of the
2618 * interior-pointer nature. As the code for pairing an interior
2619 * pointer value up with its "base" register, and fixing it up after
2620 * scavenging is complete is horribly repetitive, a few macros paper
2621 * over the monotony. --AB, 2010-Jul-14 */
2623 #define INTERIOR_POINTER_VARS(name) \
2624 unsigned long name; \
2625 unsigned long name##_offset; \
2626 int name##_register_pair
2628 #define PAIR_INTERIOR_POINTER(name, accessor) \
2630 pair_interior_pointer(context, name, \
2632 &name##_register_pair)
2635 * Do we need to check if the register we're fixing up is in the
2638 #define FIXUP_INTERIOR_POINTER(name, accessor) \
2640 if (name##_register_pair >= 0) { \
2642 PTR(SC_REG(context, name##_register_pair)) \
2649 pair_interior_pointer(os_context_t *context, unsigned long pointer,
2650 unsigned long *saved_offset, int *register_pair)
2655 * I (RLT) think this is trying to find the boxed register that is
2656 * closest to the LIP address, without going past it. Usually, it's
2657 * reg_CODE or reg_LRA. But sometimes, nothing can be found.
2659 *saved_offset = 0x7FFFFFFF;
2660 *register_pair = -1;
2661 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2666 index = boxed_registers[i];
2667 reg = SC_REG(context, index);
2669 /* An interior pointer is never relative to a non-pointer
2670 * register (an oversight in the original implementation).
2671 * The simplest argument for why this is true is to consider
2672 * the fixnum that happens by coincide to be the word-index in
2673 * memory of the header for some object plus two. This is
2674 * happenstance would cause the register containing the fixnum
2675 * to be selected as the register_pair if the interior pointer
2676 * is to anywhere after the first two words of the object.
2677 * The fixnum won't be changed during GC, but the object might
2678 * move, thus destroying the interior pointer. --AB,
2681 if (Pointerp(reg) && (PTR(reg) <= pointer)) {
2682 offset = pointer - PTR(reg);
2683 if (offset < *saved_offset) {
2684 *saved_offset = offset;
2685 *register_pair = index;
2693 scavenge_interrupt_context(os_context_t * context)
2697 INTERIOR_POINTER_VARS(pc);
2699 INTERIOR_POINTER_VARS(lip);
2702 INTERIOR_POINTER_VARS(lr);
2705 INTERIOR_POINTER_VARS(ctr);
2708 INTERIOR_POINTER_VARS(npc);
2712 PAIR_INTERIOR_POINTER(lip, SC_REG(context, reg_LIP));
2713 #endif /* reg_LIP */
2715 PAIR_INTERIOR_POINTER(pc, SC_PC(context));
2718 PAIR_INTERIOR_POINTER(npc, SC_NPC(context));
2722 PAIR_INTERIOR_POINTER(lr, SC_REG(context, reg_LR));
2726 PAIR_INTERIOR_POINTER(ctr, SC_REG(context, reg_CTR));
2729 /* Scanvenge all boxed registers in the context. */
2730 for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2734 index = boxed_registers[i];
2735 foo = SC_REG(context, index);
2737 SC_REG(context, index) = foo;
2739 scavenge(&(SC_REG(context, index)), 1);
2743 * Now that the scavenging is done, repair the various interior
2747 FIXUP_INTERIOR_POINTER(lip, SC_REG(context, reg_LIP));
2750 FIXUP_INTERIOR_POINTER(pc, SC_PC(context));
2753 FIXUP_INTERIOR_POINTER(npc, SC_NPC(context));
2757 FIXUP_INTERIOR_POINTER(lr, SC_REG(context, reg_LR));
2761 FIXUP_INTERIOR_POINTER(ctr, SC_REG(context, reg_CTR));
2766 scavenge_interrupt_contexts(void)
2769 os_context_t *context;
2772 printf("Scavenging interrupt contexts ...\n");
2775 index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX));
2777 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
2778 printf("Number of active contexts: %d\n", index);
2781 for (i = 0; i < index; i++) {
2782 context = lisp_interrupt_contexts[i];
2783 scavenge_interrupt_context(context);
2788 /* Code and Code-Related Objects */
2791 * Aargh! Why is SPARC so different here? What is the advantage of
2792 * making it different from all the other ports?
2794 #if defined(sparc) || (defined(DARWIN) && defined(__ppc__))
2795 #define RAW_ADDR_OFFSET 0
2797 #define RAW_ADDR_OFFSET (6 * sizeof(lispobj) - type_FunctionPointer)
2800 static lispobj trans_function_header(lispobj object);
2801 static lispobj trans_boxed(lispobj object);
2805 scav_function_pointer(lispobj * where, lispobj object)
2807 gc_assert(Pointerp(object));
2809 if (from_space_p(object)) {
2810 lispobj first, *first_pointer;
2813 * Object is a pointer into from space - check to see if it has
2816 first_pointer = (lispobj *) PTR(object);
2817 first = *first_pointer;
2819 if (first == 0x01) {
2821 *where = first_pointer[1];
2828 * Must transport object -- object may point to either a
2829 * function header, a closure function header, or to a closure
2833 type = TypeOf(first);
2835 case type_FunctionHeader:
2836 case type_ClosureFunctionHeader:
2837 copy = trans_function_header(object);
2840 copy = trans_boxed(object);
2844 if (copy != object) {
2845 /* Set forwarding pointer. */
2846 first_pointer[0] = 0x01;
2847 first_pointer[1] = copy;
2853 gc_assert(Pointerp(first));
2854 gc_assert(!from_space_p(first));
2862 scav_function_pointer(lispobj * where, lispobj object)
2864 lispobj *first_pointer;
2867 gc_assert(Pointerp(object));
2869 /* Object is a pointer into from space - no a FP. */
2870 first_pointer = (lispobj *) PTR(object);
2873 * Must transport object -- object may point to either a function
2874 * header, a closure function header, or to a closure header.
2877 switch (TypeOf(*first_pointer)) {
2878 case type_FunctionHeader:
2879 case type_ClosureFunctionHeader:
2880 copy = trans_function_header(object);
2883 copy = trans_boxed(object);
2887 if (copy != object) {
2888 /* Set forwarding pointer */
2889 first_pointer[0] = 0x01;
2890 first_pointer[1] = copy;
2893 gc_assert(Pointerp(copy));
2894 gc_assert(!from_space_p(copy));
2902 #if defined(i386) || defined(__x86_64)
2904 * Scan an x86 compiled code object, looking for possible fixups that
2905 * have been missed after a move.
2907 * Two types of fixups are needed:
2908 * 1. Absolution fixups to within the code object.
2909 * 2. Relative fixups to outside the code object.
2911 * Currently only absolution fixups to the constant vector, or to the
2912 * code area are checked.
2915 sniff_code_object(struct code *code, unsigned displacement)
2917 int nheader_words, ncode_words, nwords;
2919 char *constants_start_addr, *constants_end_addr;
2920 char *code_start_addr, *code_end_addr;
2921 int fixup_found = 0;
2923 if (!check_code_fixups)
2927 * It's ok if it's byte compiled code. The trace table offset will
2928 * be a fixnum if it's x86 compiled code - check.
2930 if (code->trace_table_offset & 0x3) {
2932 fprintf(stderr, "*** Sniffing byte compiled code object at %x.\n",
2938 /* Else it's x86 machine code. */
2940 ncode_words = fixnum_value(code->code_size);
2941 nheader_words = HeaderValue(*(lispobj *) code);
2942 nwords = ncode_words + nheader_words;
2944 constants_start_addr = (char *) code + 5 * sizeof(lispobj);
2945 constants_end_addr = (char *) code + nheader_words * sizeof(lispobj);
2946 code_start_addr = (char *) code + nheader_words * sizeof(lispobj);
2947 code_end_addr = (char *) code + nwords * sizeof(lispobj);
2949 /* Work through the unboxed code. */
2950 for (p = code_start_addr; p < code_end_addr; p++) {
2951 char *data = *(char **) p;
2952 unsigned d1 = *((unsigned char *) p - 1);
2953 unsigned d2 = *((unsigned char *) p - 2);
2954 unsigned d3 = *((unsigned char *) p - 3);
2955 unsigned d4 = *((unsigned char *) p - 4);
2956 unsigned d5 = *((unsigned char *) p - 5);
2957 unsigned d6 = *((unsigned char *) p - 6);
2960 * Check for code references.
2962 * Check for a 32 bit word that looks like an absolute reference
2963 * to within the code adea of the code object.
2965 if (data >= code_start_addr - displacement
2966 && data < code_end_addr - displacement) {
2967 /* Function header */
2969 && ((unsigned long) p - 4 -
2970 4 * HeaderValue(*((unsigned long *) p - 1))) ==
2971 (unsigned long) code) {
2972 /* Skip the function header */
2980 "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2981 (unsigned long) p, d6, d5, d4, d3, d2, d1,
2982 (unsigned long) data);
2983 fprintf(stderr, "*** Push $0x%.8lx\n", (unsigned long) data);
2985 /* Mov [reg-8],imm32 */
2987 && (d2 == 0x40 || d2 == 0x41 || d2 == 0x42 || d2 == 0x43
2988 || d2 == 0x45 || d2 == 0x46 || d2 == 0x47)
2992 "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
2993 (unsigned long) p, d6, d5, d4, d3, d2, d1,
2994 (unsigned long) data);
2995 fprintf(stderr, "*** Mov [reg-8],$0x%.8lx\n",
2996 (unsigned long) data);
2998 /* Lea reg, [disp32] */
2999 if (d2 == 0x8d && (d1 & 0xc7) == 5) {
3002 "Code ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3003 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3004 (unsigned long) data);
3005 fprintf(stderr, "*** Lea reg,[$0x%.8lx]\n",
3006 (unsigned long) data);
3011 * Check for constant references.
3013 * Check for a 32 bit word that looks like an absolution reference
3014 * to within the constant vector. Constant references will be
3017 if (data >= constants_start_addr - displacement
3018 && data < constants_end_addr - displacement
3019 && ((unsigned long) data & 0x3) == 0) {
3024 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3025 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3026 (unsigned long) data);
3027 fprintf(stderr, "*** Mov eax,0x%.8lx\n", (unsigned long) data);
3034 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3035 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3036 (unsigned long) data);
3037 fprintf(stderr, "*** Mov 0x%.8lx,eax\n", (unsigned long) data);
3041 if (d1 == 0x3d && d2 == 0x81) {
3044 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3045 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3046 (unsigned long) data);
3048 fprintf(stderr, "*** Cmp 0x%.8lx,immed32\n",
3049 (unsigned long) data);
3052 /* Check for a mod=00, r/m=101 byte. */
3053 if ((d1 & 0xc7) == 5) {
3058 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3059 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3060 (unsigned long) data);
3061 fprintf(stderr, "*** Cmp 0x%.8lx,reg\n",
3062 (unsigned long) data);
3068 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3069 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3070 (unsigned long) data);
3071 fprintf(stderr, "*** Cmp reg32,0x%.8lx\n",
3072 (unsigned long) data);
3078 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3079 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3080 (unsigned long) data);
3081 fprintf(stderr, "*** Mov 0x%.8lx,reg32\n",
3082 (unsigned long) data);
3088 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3089 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3090 (unsigned long) data);
3091 fprintf(stderr, "*** Mov reg32,0x%.8lx\n",
3092 (unsigned long) data);
3098 "Abs. const. ref. @ %lx: %.2x %.2x %.2x %.2x %.2x %.2x (%.8lx)\n",
3099 (unsigned long) p, d6, d5, d4, d3, d2, d1,
3100 (unsigned long) data);
3101 fprintf(stderr, "*** Lea reg32,0x%.8lx\n",
3102 (unsigned long) data);
3108 /* If anything was found print out some info. on the code object. */
3111 "*** Compiled code object at %lx: header_words=%d code_words=%d .\n",
3112 (unsigned long) code, nheader_words, ncode_words);
3114 "*** Const. start = %lx; end= %lx; Code start = %lx; end = %lx\n",
3115 (unsigned long) constants_start_addr,
3116 (unsigned long) constants_end_addr,
3117 (unsigned long) code_start_addr, (unsigned long) code_end_addr);
3122 apply_code_fixups(struct code *old_code, struct code *new_code)
3124 int nheader_words, ncode_words, nwords;
3125 char *constants_start_addr, *constants_end_addr;
3126 char *code_start_addr, *code_end_addr;
3127 lispobj fixups = NIL;
3128 unsigned long displacement =
3130 (unsigned long) new_code - (unsigned long) old_code;
3131 struct vector *fixups_vector;
3134 * It's ok if it's byte compiled code. The trace table offset will
3135 * be a fixnum if it's x86 compiled code - check.
3137 if (new_code->trace_table_offset & 0x3) {
3139 fprintf(stderr, "*** Byte compiled code object at %x.\n", new_code);
3144 /* Else it's x86 machine code. */
3145 ncode_words = fixnum_value(new_code->code_size);
3146 nheader_words = HeaderValue(*(lispobj *) new_code);
3147 nwords = ncode_words + nheader_words;
3150 "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
3151 new_code, nheader_words, ncode_words);
3153 constants_start_addr = (char *) new_code + 5 * sizeof(lispobj);
3154 constants_end_addr = (char *) new_code + nheader_words * sizeof(lispobj);
3155 code_start_addr = (char *) new_code + nheader_words * sizeof(lispobj);
3156 code_end_addr = (char *) new_code + nwords * sizeof(lispobj);
3159 "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
3160 constants_start_addr, constants_end_addr, code_start_addr,
3165 * The first constant should be a pointer to the fixups for this
3166 * code objects - Check.
3168 fixups = new_code->constants[0];
3171 * It will be 0 or the unbound-marker if there are no fixups, and
3172 * will be an other pointer if it is valid.
3174 if (fixups == 0 || fixups == type_UnboundMarker || !Pointerp(fixups)) {
3175 /* Check for possible errors. */
3176 if (check_code_fixups)
3177 sniff_code_object(new_code, displacement);
3180 fprintf(stderr, "Fixups for code object not found!?\n");
3182 "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
3183 new_code, nheader_words, ncode_words);
3185 "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
3186 constants_start_addr, constants_end_addr, code_start_addr,
3192 fixups_vector = (struct vector *) PTR(fixups);
3194 /* Could be pointing to a forwarding pointer. */
3195 if (Pointerp(fixups) && find_page_index((void *) fixups_vector) != -1
3196 && fixups_vector->header == 0x01) {
3198 fprintf(stderr, "* FF\n");
3200 /* If so then follow it. */
3201 fixups_vector = (struct vector *) PTR((lispobj) fixups_vector->length);
3204 fprintf(stderr, "Got the fixups\n");
3207 if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
3209 * Got the fixups for the code block. Now work through the
3210 * vector, and apply a fixup at each address.
3212 int length = fixnum_value(fixups_vector->length);
3215 for (i = 0; i < length; i++) {
3216 unsigned offset = fixups_vector->data[i];
3218 /* Now check the current value of offset. */
3219 unsigned long old_value =
3220 *(unsigned long *) ((unsigned long) code_start_addr + offset);
3223 * If it's within the old_code object then it must be an
3224 * absolute fixup (relative ones are not saved).
3226 if (old_value >= (unsigned long) old_code
3228 (unsigned long) old_code + nwords * sizeof(lispobj))
3229 /* So add the dispacement. */
3230 *(unsigned long *) ((unsigned long) code_start_addr + offset) =
3231 old_value + displacement;
3234 * It is outside the old code object so it must be a relative
3235 * fixup (absolute fixups are not saved). So subtract the
3238 *(unsigned long *) ((unsigned long) code_start_addr + offset) =
3239 old_value - displacement;
3243 /* Check for possible errors. */
3244 if (check_code_fixups)
3245 sniff_code_object(new_code, displacement);
3249 static struct code *
3250 trans_code(struct code *code)
3252 struct code *new_code;
3253 lispobj l_code, l_new_code;
3254 int nheader_words, ncode_words, nwords;
3255 unsigned long displacement;
3256 lispobj fheaderl, *prev_pointer;
3259 fprintf(stderr, "\nTransporting code object located at 0x%08x.\n",
3260 (unsigned long) code);
3263 /* If object has already been transported, just return pointer */
3264 if (*(lispobj *) code == 0x01) {
3265 return (struct code *) (((lispobj *) code)[1]);
3269 gc_assert(TypeOf(code->header) == type_CodeHeader);
3271 /* prepare to transport the code vector */
3272 l_code = (lispobj) code | type_OtherPointer;
3274 ncode_words = fixnum_value(code->code_size);
3275 nheader_words = HeaderValue(code->header);
3276 nwords = ncode_words + nheader_words;
3277 nwords = CEILING(nwords, 2);
3279 l_new_code = copy_large_object(l_code, nwords);
3280 new_code = (struct code *) PTR(l_new_code);
3282 /* May not have been moved. */
3283 if (new_code == code)
3286 displacement = l_new_code - l_code;
3289 fprintf(stderr, "Old code object at 0x%08x, new code object at 0x%08x.\n",
3290 (unsigned long) code, (unsigned long) new_code);
3291 fprintf(stderr, "Code object is %d words long.\n", nwords);
3294 /* set forwarding pointer */
3295 ((lispobj *) code)[0] = 0x01;
3296 ((lispobj *) code)[1] = l_new_code;
3299 * Set forwarding pointers for all the function headers in the code
3300 * object; also fix all self pointers.
3303 fheaderl = code->entry_points;
3304 prev_pointer = &new_code->entry_points;
3306 while (fheaderl != NIL) {
3307 struct function *fheaderp, *nfheaderp;
3310 fheaderp = (struct function *) PTR(fheaderl);
3311 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3314 * Calcuate the new function pointer and the new function header.
3316 nfheaderl = fheaderl + displacement;
3317 nfheaderp = (struct function *) PTR(nfheaderl);
3319 /* set forwarding pointer */
3320 ((lispobj *) fheaderp)[0] = 0x01;
3321 ((lispobj *) fheaderp)[1] = nfheaderl;
3323 /* Fix self pointer */
3324 nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
3326 *prev_pointer = nfheaderl;
3328 fheaderl = fheaderp->next;
3329 prev_pointer = &nfheaderp->next;
3333 sniff_code_object(new_code, displacement);
3335 #if defined(i386) || defined(__x86_64)
3336 apply_code_fixups(code, new_code);
3340 os_flush_icache((os_vm_address_t) (((int *) new_code) + nheader_words),
3341 ncode_words * sizeof(int));
3349 scav_code_header(lispobj * where, lispobj object)
3352 int nheader_words, ncode_words, nwords;
3354 struct function *fheaderp;
3356 code = (struct code *) where;
3357 ncode_words = fixnum_value(code->code_size);
3358 nheader_words = HeaderValue(object);
3359 nwords = ncode_words + nheader_words;
3360 nwords = CEILING(nwords, 2);
3362 /* Scavenge the boxed section of the code data block */
3363 scavenge(where + 1, nheader_words - 1);
3366 * Scavenge the boxed section of each function object in the code
3369 fheaderl = code->entry_points;
3370 while (fheaderl != NIL) {
3371 fheaderp = (struct function *) PTR(fheaderl);
3372 gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3374 scavenge(&fheaderp->name, 1);
3375 scavenge(&fheaderp->arglist, 1);
3376 scavenge(&fheaderp->type, 1);
3378 fheaderl = fheaderp->next;
3385 trans_code_header(lispobj object)
3389 ncode = trans_code((struct code *) PTR(object));
3390 return (lispobj) ncode | type_OtherPointer;
3394 size_code_header(lispobj * where)
3397 int nheader_words, ncode_words, nwords;
3399 code = (struct code *) where;
3401 ncode_words = fixnum_value(code->code_size);
3402 nheader_words = HeaderValue(code->header);
3403 nwords = ncode_words + nheader_words;
3404 nwords = CEILING(nwords, 2);
3409 #if !(defined(i386) || defined(__x86_64))
3412 scav_return_pc_header(lispobj * where, lispobj object)
3414 fprintf(stderr, "GC lossage. Should not be scavenging a ");
3415 fprintf(stderr, "Return PC Header.\n");
3416 fprintf(stderr, "where = 0x%08lx, object = 0x%08lx",
3417 (unsigned long) where, (unsigned long) object);
3422 #endif /* not i386 */
3425 trans_return_pc_header(lispobj object)
3427 struct function *return_pc;
3428 unsigned long offset;
3429 struct code *code, *ncode;
3431 return_pc = (struct function *) PTR(object);
3432 offset = HeaderValue(return_pc->header) * sizeof(lispobj);
3434 /* Transport the whole code object */
3435 code = (struct code *) ((unsigned long) return_pc - offset);
3437 ncode = trans_code(code);
3439 return ((lispobj) ncode + offset) | type_OtherPointer;
3443 * On the 386, closures hold a pointer to the raw address instead of
3444 * the function object.
3446 #if defined(i386) || defined(__x86_64)
3449 scav_closure_header(lispobj * where, lispobj object)
3451 struct closure *closure;
3454 closure = (struct closure *) where;
3455 fun = closure->function - RAW_ADDR_OFFSET;
3456 #if !(defined(i386) && defined(SOLARIS))
3458 /* The function may have moved so update the raw address. But don't
3459 write unnecessarily. */
3460 if (closure->function != fun + RAW_ADDR_OFFSET)
3461 closure->function = fun + RAW_ADDR_OFFSET;
3464 * For some reason, on solaris/x86, we get closures (actually, it
3465 * appears to be funcallable instances where the closure function
3466 * is zero. I don't know why, but they are. They don't seem to
3467 * be created anywhere and it doesn't seem to be caused by GC
3470 * Anyway, we check for zero and skip scavenging if so.
3471 * (Previously, we'd get a segfault scavenging the object at
3472 * address -RAW_ADDR_OFFSET.
3474 if (closure->function) {
3477 * The function may have moved so update the raw address. But don't
3478 * write unnecessarily.
3480 if (closure->function != fun + RAW_ADDR_OFFSET) {
3482 fprintf(stderr, "closure header 0x%04x moved from %p to %p\n",
3483 closure->header, (void*) closure->function, (void*) (fun + RAW_ADDR_OFFSET));
3485 closure->function = fun + RAW_ADDR_OFFSET;
3490 fprintf(stderr, "Weird closure!\n");
3491 fprintf(stderr, " where = %p, object = 0x%04x\n", where, object);
3492 fprintf(stderr, " closure->function = %p, fun = %p\n", closure->function, fun);
3501 #if !(defined(i386) || defined(__x86_64))
3504 scav_function_header(lispobj * where, lispobj object)
3506 fprintf(stderr, "GC lossage. Should not be scavenging a ");
3507 fprintf(stderr, "Function Header.\n");
3508 fprintf(stderr, "where = 0x%08lx, object = 0x%08lx",
3509 (unsigned long) where, (unsigned long) object);
3514 #endif /* not i386 */
3517 trans_function_header(lispobj object)
3519 struct function *fheader;
3520 unsigned long offset;
3521 struct code *code, *ncode;
3523 fheader = (struct function *) PTR(object);
3524 offset = HeaderValue(fheader->header) * sizeof(lispobj);
3526 /* Transport the whole code object */
3527 code = (struct code *) ((unsigned long) fheader - offset);
3528 ncode = trans_code(code);
3530 return ((lispobj) ncode + offset) | type_FunctionPointer;
3538 scav_instance_pointer(lispobj * where, lispobj object)
3540 if (from_space_p(object)) {
3541 lispobj first, *first_pointer;
3544 * object is a pointer into from space. check to see if it has
3547 first_pointer = (lispobj *) PTR(object);
3548 first = *first_pointer;
3552 first = first_pointer[1];
3554 first = trans_boxed(object);
3555 gc_assert(first != object);
3556 /* Set forwarding pointer */
3557 first_pointer[0] = 0x01;
3558 first_pointer[1] = first;
3566 scav_instance_pointer(lispobj * where, lispobj object)
3568 lispobj copy, *first_pointer;
3570 /* Object is a pointer into from space - not a FP */
3571 copy = trans_boxed(object);
3573 gc_assert(copy != object);
3575 first_pointer = (lispobj *) PTR(object);
3577 /* Set forwarding pointer. */
3578 first_pointer[0] = 0x01;
3579 first_pointer[1] = copy;
3587 /* Lists and Conses */
3589 static lispobj trans_list(lispobj object);
3593 scav_list_pointer(lispobj * where, lispobj object)
3595 gc_assert(Pointerp(object));
3597 if (from_space_p(object)) {
3598 lispobj first, *first_pointer;
3601 * Object is a pointer into from space - check to see if it has
3604 first_pointer = (lispobj *) PTR(object);
3605 first = *first_pointer;
3609 first = first_pointer[1];
3611 first = trans_list(object);
3613 /* Set forwarding pointer */
3614 first_pointer[0] = 0x01;
3615 first_pointer[1] = first;
3618 gc_assert(Pointerp(first));
3619 gc_assert(!from_space_p(first));
3626 scav_list_pointer(lispobj * where, lispobj object)
3628 lispobj first, *first_pointer;
3630 gc_assert(Pointerp(object));
3632 /* Object is a pointer into from space - not FP */
3634 first = trans_list(object);
3635 gc_assert(first != object);
3637 first_pointer = (lispobj *) PTR(object);
3639 /* Set forwarding pointer */
3640 first_pointer[0] = 0x01;
3641 first_pointer[1] = first;
3643 gc_assert(Pointerp(first));
3644 gc_assert(!from_space_p(first));
3651 trans_list(lispobj object)
3653 lispobj new_list_pointer;
3654 struct cons *cons, *new_cons;
3657 gc_assert(from_space_p(object));
3659 cons = (struct cons *) PTR(object);
3662 new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3664 new_cons->car = cons->car;
3665 new_cons->cdr = cons->cdr; /* updated later */
3666 new_list_pointer = (lispobj) new_cons | LowtagOf(object);
3668 /* Grab the cdr before it is clobbered */
3671 /* Set forwarding pointer (clobbers start of list). */
3673 cons->cdr = new_list_pointer;
3675 /* Try to linearize the list in the cdr direction to help reduce paging. */
3678 struct cons *cdr_cons, *new_cdr_cons;
3680 if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
3681 || *((lispobj *) PTR(cdr)) == 0x01)
3684 cdr_cons = (struct cons *) PTR(cdr);
3687 new_cdr_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3689 new_cdr_cons->car = cdr_cons->car;
3690 new_cdr_cons->cdr = cdr_cons->cdr;
3691 new_cdr = (lispobj) new_cdr_cons | LowtagOf(cdr);
3693 /* Grab the cdr before it is clobbered */
3694 cdr = cdr_cons->cdr;
3696 /* Set forwarding pointer */
3697 cdr_cons->car = 0x01;
3698 cdr_cons->cdr = new_cdr;
3701 * Update the cdr of the last cons copied into new space to keep
3702 * the newspace scavenge from having to do it.
3704 new_cons->cdr = new_cdr;
3706 new_cons = new_cdr_cons;
3709 return new_list_pointer;
3713 /* Scavenging and Transporting Other Pointers */
3717 scav_other_pointer(lispobj * where, lispobj object)
3719 gc_assert(Pointerp(object));
3721 if (from_space_p(object)) {
3722 lispobj first, *first_pointer;
3725 * Object is a pointer into from space. check to see if it has
3728 first_pointer = (lispobj *) PTR(object);
3729 first = *first_pointer;
3731 if (first == 0x01) {
3733 first = first_pointer[1];
3736 first = (transother[TypeOf(first)]) (object);
3738 if (first != object) {
3739 /* Set forwarding pointer */
3740 first_pointer[0] = 0x01;
3741 first_pointer[1] = first;
3746 gc_assert(Pointerp(first));
3747 gc_assert(!from_space_p(first));
3753 scav_other_pointer(lispobj * where, lispobj object)
3755 lispobj first, *first_pointer;
3757 gc_assert(Pointerp(object));
3759 /* Object is a pointer into from space - not FP */
3760 first_pointer = (lispobj *) PTR(object);
3762 first = (transother[TypeOf(*first_pointer)]) (object);
3764 if (first != object) {
3765 /* Set forwarding pointer */
3766 first_pointer[0] = 0x01;
3767 first_pointer[1] = first;
3771 gc_assert(Pointerp(first));
3772 gc_assert(!from_space_p(first));
3779 /* Immediate, Boxed, and Unboxed Objects */
3782 size_pointer(lispobj * where)
3788 scav_immediate(lispobj * where, lispobj object)
3794 trans_immediate(lispobj object)
3796 fprintf(stderr, "GC lossage. Trying to transport an immediate!?\n");
3802 size_immediate(lispobj * where)
3809 scav_boxed(lispobj * where, lispobj object)
3815 trans_boxed(lispobj object)
3818 unsigned long length;
3820 gc_assert(Pointerp(object));
3822 header = *((lispobj *) PTR(object));
3823 length = HeaderValue(header) + 1;
3824 length = CEILING(length, 2);
3826 return copy_object(object, length);
3830 trans_boxed_large(lispobj object)
3833 unsigned long length;
3835 gc_assert(Pointerp(object));
3837 header = *((lispobj *) PTR(object));
3838 length = HeaderValue(header) + 1;
3839 length = CEILING(length, 2);
3841 return copy_large_object(object, length);
3845 size_boxed(lispobj * where)
3848 unsigned long length;
3851 length = HeaderValue(header) + 1;
3852 length = CEILING(length, 2);
3857 /* Not needed on sparc and ppc because the raw_addr has a function lowtag */
3858 #if !(defined(sparc) || (defined(DARWIN) && defined(__ppc__)))
3860 scav_fdefn(lispobj * where, lispobj object)
3862 struct fdefn *fdefn;
3864 fdefn = (struct fdefn *) where;
3866 if ((char *) (fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
3867 scavenge(where + 1, sizeof(struct fdefn) / sizeof(lispobj) - 1);
3869 /* Don't write unnecessarily */
3870 if (fdefn->raw_addr != (char *) (fdefn->function + RAW_ADDR_OFFSET))
3871 fdefn->raw_addr = (char *) (fdefn->function + RAW_ADDR_OFFSET);
3873 return sizeof(struct fdefn) / sizeof(lispobj);
3880 scav_unboxed(lispobj * where, lispobj object)
3882 unsigned long length;
3884 length = HeaderValue(object) + 1;
3885 length = CEILING(length, 2);
3891 trans_unboxed(lispobj object)
3894 unsigned long length;
3897 gc_assert(Pointerp(object));
3899 header = *((lispobj *) PTR(object));
3900 length = HeaderValue(header) + 1;
3901 length = CEILING(length, 2);
3903 return copy_unboxed_object(object, length);
3907 trans_unboxed_large(lispobj object)
3910 unsigned long length;
3913 gc_assert(Pointerp(object));
3915 header = *((lispobj *) PTR(object));
3916 length = HeaderValue(header) + 1;
3917 length = CEILING(length, 2);
3919 return copy_large_unboxed_object(object, length);
3923 size_unboxed(lispobj * where)
3926 unsigned long length;
3929 length = HeaderValue(header) + 1;
3930 length = CEILING(length, 2);
3936 /* Vector-Like Objects */
3938 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
3941 size_string(lispobj * where)
3943 struct vector *vector;
3947 * NOTE: Strings contain one more byte of data than the length
3951 vector = (struct vector *) where;
3952 length = fixnum_value(vector->length) + 1;
3955 nwords = CEILING(NWORDS(length, 8) + 2, 2);
3957 nwords = CEILING(NWORDS(length, 4) + 2, 2);
3961 * Strings are just like arrays with 16-bit elements, and contain
3962 * one more element than the slot length indicates.
3964 nwords = CEILING(NWORDS(length, 2) + 2, 2);
3970 scav_string(lispobj * where, lispobj object)
3972 return size_string(where);
3976 trans_string(lispobj object)
3978 gc_assert(Pointerp(object));
3979 return copy_large_unboxed_object(object,
3980 size_string((lispobj *) PTR(object)));
3984 /************************************************************************
3986 ************************************************************************/
3988 /* This struct corresponds to the Lisp HASH-TABLE structure defined in
3992 lispobj instance_header; /* 0 */
3997 lispobj rehash_size; /* 5 */
3998 lispobj rehash_threshold;
3999 lispobj rehash_trigger;
4000 lispobj number_entries;
4002 lispobj weak_p; /* 10 */
4003 lispobj needing_rehash;
4004 lispobj next_free_kv;
4005 lispobj index_vector;
4006 lispobj next_vector;
4007 lispobj hash_vector; /* 15 */
4008 lispobj next_weak_table;
4011 /* The size of a hash-table in Lisp objects. */
4013 #define HASH_TABLE_SIZE (sizeof (struct hash_table) / sizeof (lispobj))
4015 /* Compute the EQ-hash of KEY. This must be the same as what's used
4016 in hash-new.lisp. */
4018 #define EQ_HASH(key) ((key) & 0x1fffffff)
4020 /* List of weak hash tables chained through their WEAK-P slot. Set to
4021 NIL at the start of a collection.
4023 This is not optimal because, when a table is tenured, it won't be
4024 processed automatically; only the yougest generation is GC'd by
4025 default. On the other hand, all applications will need an
4026 occasional full GC anyway, so it's not that bad either. */
4028 static lispobj weak_hash_tables;
4030 /* Return true if OBJ will survive the current GC. */
4033 survives_gc(lispobj obj)
4035 if (!Pointerp(obj) || !from_space_p(obj))
4037 return *(lispobj *) PTR(obj) == 1;
4040 /* If OBJ is a (UNSIGNED-BYTE 32) array, return a pointer to its first
4041 element, otherwise return null. If LENGTH is not null, return in it
4042 the array's length. */
4044 static inline unsigned *
4045 u32_vector(lispobj obj, unsigned *length)
4047 unsigned *ptr = NULL;
4049 if (Pointerp(obj)) {
4050 lispobj *p = (lispobj *) PTR(obj);
4052 if (TypeOf(p[0]) == type_SimpleArrayUnsignedByte32) {
4053 ptr = (unsigned *) (p + 2);
4055 *length = fixnum_value(p[1]);
4062 /* Free an entry of hash-table HASH-TABLE whose hash index (index in
4063 the hash-table's INDEX-VECTOR) is HASH_INDEX, and whose index
4064 in the hash-table's TABLE vector is KV_INDEX. */
4067 free_hash_entry(struct hash_table *hash_table, int hash_index, int kv_index)
4069 unsigned length = UINT_MAX; // to compare to
4070 unsigned *index_vector = u32_vector(hash_table->index_vector, &length);
4071 unsigned *next_vector = u32_vector(hash_table->next_vector, 0);
4074 gc_assert(length != UINT_MAX);
4076 if (index_vector[hash_index] == kv_index)
4077 /* The entry is the first in the collinion chain.
4078 Pop it from the list. */
4079 index_vector[hash_index] = next_vector[kv_index];
4081 /* The entry is not the first in the collision chain. */
4082 unsigned prev = index_vector[hash_index];
4083 unsigned i = next_vector[prev];
4085 while (i && i != kv_index)
4086 prev = i, i = next_vector[i];
4089 next_vector[prev] = next_vector[kv_index];
4095 unsigned count = fixnum_value(hash_table->number_entries);
4096 lispobj* kv_vector = (lispobj *) PTR(hash_table->table);
4097 unsigned *hash_vector = u32_vector(hash_table->hash_vector, 0);
4098 unsigned hash_index;
4099 lispobj empty_symbol;
4101 gc_assert(count > 0);
4102 hash_table->number_entries = make_fixnum(count - 1);
4103 next_vector[kv_index] = fixnum_value(hash_table->next_free_kv);
4104 hash_table->next_free_kv = make_fixnum(kv_index);
4106 * I (rtoy) think we also need to clear out the key and value
4107 * in the kv-vector. If we don't, maphash and
4108 * with-hash-table-iterator thinks this entry is not empty.
4111 kv_vector += 2; /* Skip over vector header and length slots */
4112 empty_symbol = kv_vector[1];
4114 hash_index = EQ_HASH(kv_vector[2 * kv_index]) % length;
4116 kv_vector[2 * kv_index] = empty_symbol;
4117 kv_vector[2 * kv_index + 1] = empty_symbol;
4119 hash_vector[hash_index] = EQ_BASED_HASH_VALUE;
4124 /* Record an entry of hash-table HASH-TABLE whose hash index (index in
4125 the hash-table's INDEX-VECTOR) is HASH_INDEX, and whose index
4126 in the hash-table's TABLE vector is KV_INDEX, for rehashing. */
4129 record_for_rehashing(struct hash_table *hash_table, int hash_index,
4132 unsigned *index_vector = u32_vector(hash_table->index_vector, 0);
4133 unsigned *next_vector = u32_vector(hash_table->next_vector, 0);
4136 if (index_vector[hash_index] == kv_index)
4137 /* This entry is at the head of the collision chain.
4138 Pop it from that list. */
4139 index_vector[hash_index] = next_vector[kv_index];
4141 unsigned prev = index_vector[hash_index];
4142 unsigned i = next_vector[prev];
4144 while (i && i != kv_index)
4145 prev = i, i = next_vector[i];
4148 next_vector[prev] = next_vector[kv_index];
4154 next_vector[kv_index] = fixnum_value(hash_table->needing_rehash);
4155 hash_table->needing_rehash = make_fixnum(kv_index);
4159 static inline boolean
4160 eq_based_hash_vector(unsigned int* hash_vector, unsigned int index)
4162 return (hash_vector == 0) || (hash_vector[index] == EQ_BASED_HASH_VALUE);
4165 static inline boolean
4166 removable_weak_key(lispobj old_key, unsigned int index_value, boolean eq_hash_p)
4168 return (!survives_gc(old_key)
4170 && (index_value != 0));
4173 static inline boolean
4174 removable_weak_value(lispobj value, unsigned int index_value)
4177 * The entry can be removed if the value can be GCed.
4179 return (!survives_gc(value)
4180 && (index_value != 0));
4183 static inline boolean
4184 removable_weak_key_and_value(lispobj old_key, lispobj value, unsigned int index_value,
4187 boolean removable_key;
4188 boolean removable_val;
4190 removable_key = (!survives_gc(old_key)
4192 && (index_value != 0));
4193 removable_val = (!survives_gc(value)
4194 && (index_value != 0));
4197 * The entry must stay if the key and value are alive. In other
4198 * words, the entry can be removed if the key or value can be GCed.
4200 return removable_key || removable_val;
4203 static inline boolean
4204 removable_weak_key_or_value(lispobj old_key, lispobj value, unsigned int index_value,
4207 boolean removable_key;
4208 boolean removable_val;
4210 removable_key = (!survives_gc(old_key)
4212 && (index_value != 0));
4213 removable_val = (!survives_gc(value)
4214 && (index_value != 0));
4217 * The entry must be kept if either the key or value is alive. In
4218 * other words, the entry can be removed only if both the key and
4219 * value can be GCed.
4221 return (removable_key && removable_val);
4225 maybe_record_for_rehashing(struct hash_table *hash_table, lispobj* kv_vector,