905f551751099f96e63a3b154e5227b56a797138
[projects/cmucl/cmucl.git] / src / lisp / gencgc.c
1 /*
2  * Generational Conservative Garbage Collector for CMUCL x86.
3  *
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'.
7  *
8  * Douglas Crosher, 1996, 1997, 1998, 1999.
9  *
10  * $Header: /project/cmucl/cvsroot/src/lisp/gencgc.c,v 1.112 2011-01-09 00:12:36 rtoy Exp $
11  *
12  */
13
14 #include <limits.h>
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <signal.h>
18 #include <string.h>
19 #include "lisp.h"
20 #include "arch.h"
21 #include "internals.h"
22 #include "os.h"
23 #include "globals.h"
24 #include "interrupt.h"
25 #include "validate.h"
26 #include "lispregs.h"
27 #include "interr.h"
28 #include "gencgc.h"
29
30 /*
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!
34  */
35 #define EQ_BASED_HASH_VALUE     0x80000000
36
37 #define gc_abort() lose("GC invariant lost!  File \"%s\", line %d\n", \
38                         __FILE__, __LINE__)
39
40 #if (defined(i386) || defined(__x86_64))
41
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))
58
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)
65
66 #define set_current_region_end(value) \
67   SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
68
69 #elif defined(sparc)
70
71 /*
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!
75  */
76
77 /*
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
81  * alloc-tn.
82  */
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))
101
102 #define set_current_region_free(value) \
103   current_dynamic_space_free_pointer = (lispobj*)((value) | ((long)current_dynamic_space_free_pointer & lowtag_Mask))
104
105 #define get_current_region_free() \
106   ((long)current_dynamic_space_free_pointer & (~(lowtag_Mask)))
107
108 #define set_current_region_end(value) \
109   SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
110
111 #elif defined(DARWIN) && defined(__ppc__)
112 #ifndef pseudo_atomic_InterruptedValue
113 #define pseudo_atomic_InterruptedValue 1
114 #endif
115 #ifndef pseudo_atomic_Value
116 #define pseudo_atomic_Value 4
117 #endif
118
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))
137
138 #define set_current_region_free(value) \
139   current_dynamic_space_free_pointer = (lispobj*)((value) | ((long)current_dynamic_space_free_pointer & lowtag_Mask))
140
141 #define get_current_region_free() \
142   ((long)current_dynamic_space_free_pointer & (~(lowtag_Mask)))
143
144 #define set_current_region_end(value) \
145   SetSymbolValue(CURRENT_REGION_END_ADDR, (value))
146
147 #else
148 #error gencgc is not supported on this platform
149 #endif
150
151 /* Define for activating assertions.  */
152
153 #if defined(x86) && defined(SOLARIS)
154 #define GC_ASSERTIONS 1
155 #endif
156
157 /* Check for references to stack-allocated objects.  */
158
159 #ifdef GC_ASSERTIONS
160
161 static void *invalid_stack_start, *invalid_stack_end;
162
163 static inline void
164 check_escaped_stack_object(lispobj * where, lispobj obj)
165 {
166 #if !defined(DARWIN) && !defined(__ppc__)
167     void *p;
168
169     if (Pointerp(obj)
170         && (p = (void *) PTR(obj),
171             (p >= (void *) CONTROL_STACK_START
172              && p < (void *) control_stack_end))) {
173         char *space;
174
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
179                  && where <
180                  (lispobj *) (STATIC_SPACE_START + static_space_size)) space =
181                 "static space";
182         else if (where >= (lispobj *) READ_ONLY_SPACE_START
183                  && where <
184                  (lispobj *) (READ_ONLY_SPACE_START +
185                               read_only_space_size)) space = "read-only space";
186         else
187             space = NULL;
188
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.  */
195
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);
199 #ifndef i386
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
206                context. */
207         }
208 #endif
209
210         else
211             fprintf(stderr,
212                     "Reference to stack-allocated object 0x%08lx at %p in %s\n",
213                     (unsigned long) obj, where,
214                     space ? space : "Unknown space");
215     }
216 #endif
217 }
218
219 #endif /* GC_ASSERTIONS */
220
221
222 #ifdef GC_ASSERTIONS
223 #define gc_assert(ex)           \
224   do {                          \
225     if (!(ex)) gc_abort ();     \
226   } while (0)
227 #else
228 #define gc_assert(ex)  (void) 0
229 #endif
230 \f
231
232 /*
233  * The number of generations, an extra is added to this for use as a temp.
234  */
235 #define NUM_GENERATIONS 6
236
237 /* Debugging variables. */
238
239 /*
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.
242  */
243 unsigned gencgc_verbose = 0;
244 unsigned counters_verbose = 0;
245
246 /*
247  * If true, then some debugging information is printed when scavenging
248  * static (malloc'ed) arrays.
249  */
250 boolean debug_static_array_p = 0;
251
252 /*
253  * To enable the use of page protection to help avoid the scavenging
254  * of pages that don't have pointers to younger generations.
255  */
256 boolean enable_page_protection = TRUE;
257
258 /*
259  * Hunt for pointers to old-space, when GCing generations >= verify_gen.
260  * Set to NUM_GENERATIONS to disable.
261  */
262 int verify_gens = NUM_GENERATIONS;
263
264 /*
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
267  * need it!)
268  */
269 boolean pre_verify_gen_0 = FALSE;
270
271 /*
272  * Enable checking for bad pointers after gc_free_heap called from purify.
273  */
274 #if 0 && defined(DARWIN)
275 boolean verify_after_free_heap = TRUE;
276 #else
277 boolean verify_after_free_heap = FALSE;
278 #endif
279
280 /*
281  * Enable the printing of a note when code objects are found in the
282  * dynamic space during a heap verify.
283  */
284 boolean verify_dynamic_code_check = FALSE;
285
286 /*
287  * Enable the checking of code objects for fixup errors after they are
288  * transported.  (Only used for x86.)
289  */
290 boolean check_code_fixups = FALSE;
291
292 /*
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
295  * don't unmap.
296  */
297 #if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
298 boolean gencgc_unmap_zero = FALSE;
299 #else
300 boolean gencgc_unmap_zero = TRUE;
301 #endif
302
303 /*
304  * Enable checking that newly allocated regions are zero filled.
305  */
306 #if 0 && defined(DARWIN)
307 boolean gencgc_zero_check = TRUE;
308 boolean gencgc_enable_verify_zero_fill = TRUE;
309 #else
310 boolean gencgc_zero_check = FALSE;
311 boolean gencgc_enable_verify_zero_fill = FALSE;
312 #endif
313
314 /*
315  * Enable checking that free pages are zero filled during gc_free_heap
316  * called after purify.
317  */
318 #if 0 && defined(DARWIN)
319 boolean gencgc_zero_check_during_free_heap = TRUE;
320 #else
321 boolean gencgc_zero_check_during_free_heap = FALSE;
322 #endif
323
324 /*
325  * The minimum size for a large object.
326  */
327 unsigned large_object_size = 4 * GC_PAGE_SIZE;
328
329 /*
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.
333  */
334 boolean enable_pointer_filter = TRUE;
335 \f
336
337 /*
338  * The total bytes allocated. Seen by (dynamic-usage)
339  */
340 unsigned long bytes_allocated = 0;
341
342 /*
343  * The total amount of bytes ever allocated.  Not decreased by GC.
344  */
345
346 volatile unsigned long long bytes_allocated_sum = 0;
347
348 /*
349  * GC trigger; a value of 0xffffffff represents disabled.
350  */
351 unsigned long auto_gc_trigger = 0xffffffff;
352
353 /*
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.)
359  */
360
361 unsigned long reserved_heap_pages = 256;
362
363 /*
364  * The src. and dest. generations. Set before a GC starts scavenging.
365  */
366 static int from_space;
367 static int new_space;
368 \f
369
370 /*
371  * GC structures and variables.
372  */
373
374 /*
375  * Number of pages within the dynamic heap, setup from the size of the
376  * dynamic space.
377  */
378 unsigned dynamic_space_pages;
379
380 /*
381  * An array of page structures is statically allocated.
382  * This helps quickly map between an address and its page structure.
383  */
384 struct page *page_table;
385
386 /*
387  * Heap base, needed for mapping addresses to page structures.
388  */
389 static char *heap_base = NULL;
390
391 /*
392  * Calculate the start address for the given page number.
393  */
394 static char *
395 page_address(int page_num)
396 {
397     return heap_base + GC_PAGE_SIZE * page_num;
398 }
399
400 /*
401  * Find the page index within the page_table for the given address.
402  * Returns -1 on failure.
403  */
404 int
405 find_page_index(void *addr)
406 {
407     int index = (char *) addr - heap_base;
408
409     if (index >= 0) {
410         index = (unsigned int) index / GC_PAGE_SIZE;
411         if (index < dynamic_space_pages)
412             return index;
413     }
414
415     return -1;
416 }
417
418 /*
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
426  * of 1 is returned.
427  */
428 int
429 gc_write_barrier(void *addr)
430 {
431     int page_index = find_page_index(addr);
432
433     /* Check if the fault is within the dynamic space. */
434     if (page_index == -1) {
435          return 0;
436     }
437
438     /* The page should have been marked write protected */
439     if (!PAGE_WRITE_PROTECTED(page_index))
440          fprintf(stderr,
441                  "*** Page fault in page not marked as write protected\n");
442
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;
447
448     return 1;
449 }
450
451 /*
452  * A structure to hold the state of a generation.
453  */
454 #define MEM_AGE_SHIFT 16
455 #define MEM_AGE_SCALE (1 << MEM_AGE_SHIFT)
456
457 struct generation {
458
459     /* The first page that gc_alloc checks on its next call. */
460     int alloc_start_page;
461
462     /* The first page that gc_alloc_unboxed checks on its next call. */
463     int alloc_unboxed_start_page;
464
465     /*
466      * The first page that gc_alloc_large (boxed) considers on its next call.
467      * Although it always allocates after the boxed_region.
468      */
469     int alloc_large_start_page;
470
471     /*
472      * The first page that gc_alloc_large (unboxed) considers on its next call.
473      * Although it always allocates after the current_unboxed_region.
474      */
475     int alloc_large_unboxed_start_page;
476
477     /* The bytes allocate to this generation. */
478     int bytes_allocated;
479
480     /* The number of bytes at which to trigger a GC */
481     int gc_trigger;
482
483     /* To calculate a new level for gc_trigger */
484     int bytes_consed_between_gc;
485
486     /* The number of GCs since the last raise. */
487     int num_gc;
488
489     /*
490      * The average age at after which a GC will raise objects to the
491      * next generation.
492      */
493     int trigger_age;
494
495     /*
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.
501      */
502     int cum_sum_bytes_allocated;
503
504     /*
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.
508      *
509      * The age is represented as an integer between 0 and 32767
510      * corresponding to an age of 0 to (just less than) 1.
511      */
512     int min_av_mem_age;
513 };
514
515 /*
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.
519  */
520 static struct generation generations[NUM_GENERATIONS + 1];
521
522 /* Statistics about a generation, extracted from the generations
523    array.  This gets returned to Lisp.
524 */
525
526 struct generation_stats {
527     int bytes_allocated;
528     int gc_trigger;
529     int bytes_consed_between_gc;
530     int num_gc;
531     int trigger_age;
532     int cum_sum_bytes_allocated;
533     int min_av_mem_age;
534 };
535
536
537 /*
538  * The oldest generation that will currently be GCed by default.
539  * Valid values are: 0, 1, ... (NUM_GENERATIONS - 1)
540  *
541  * The default of (NUM_GENERATIONS - 1) enables GC on all generations.
542  *
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.
546  *
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.
550  */
551 unsigned int gencgc_oldest_gen_to_gc = NUM_GENERATIONS - 1;
552
553
554 /*
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.
559  *
560  * Except on sparc and ppc, there's no ALLOCATION_POINTER, so it's
561  * never updated.  So make this available (non-static).
562  */
563 int last_free_page;
564 \f
565
566 static void scan_weak_tables(void);
567 static void scan_weak_objects(void);
568
569 /*
570  * Misc. heap functions.
571  */
572
573 /*
574  * Count the number of write protected pages within the given generation.
575  */
576 static int
577 count_write_protect_generation_pages(int generation)
578 {
579     int i;
580     int cnt = 0;
581     int mmask, mflags;
582
583     mmask = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK
584         | PAGE_GENERATION_MASK;
585     mflags = PAGE_ALLOCATED_MASK | PAGE_WRITE_PROTECTED_MASK | generation;
586
587     for (i = 0; i < last_free_page; i++)
588         if (PAGE_FLAGS(i, mmask) == mflags)
589             cnt++;
590     return cnt;
591 }
592
593 /*
594  * Count the number of pages within the given generation.
595  */
596 static int
597 count_generation_pages(int generation)
598 {
599     int i;
600     int cnt = 0;
601     int mmask, mflags;
602
603     mmask = PAGE_ALLOCATED_MASK | PAGE_GENERATION_MASK;
604     mflags = PAGE_ALLOCATED_MASK | generation;
605
606     for (i = 0; i < last_free_page; i++)
607         if (PAGE_FLAGS(i, mmask) == mflags)
608             cnt++;
609     return cnt;
610 }
611
612 /*
613  * Count the number of dont_move pages.
614  */
615 static int
616 count_dont_move_pages(void)
617 {
618     int i;
619     int cnt = 0;
620     int mmask;
621
622     mmask = PAGE_ALLOCATED_MASK | PAGE_DONT_MOVE_MASK;
623
624     for (i = 0; i < last_free_page; i++)
625         if (PAGE_FLAGS(i, mmask) == mmask)
626             cnt++;
627     return cnt;
628 }
629
630 /*
631  * Work through the pages and add up the number of bytes used for the
632  * given generation.
633  */
634 #ifdef GC_ASSERTIONS
635 static int
636 generation_bytes_allocated(int generation)
637 {
638     int i;
639     int bytes_allocated = 0;
640     int mmask, mflags;
641
642     mmask = PAGE_ALLOCATED_MASK | PAGE_GENERATION_MASK;
643     mflags = PAGE_ALLOCATED_MASK | generation;
644
645     for (i = 0; i < last_free_page; i++) {
646         if (PAGE_FLAGS(i, mmask) == mflags)
647             bytes_allocated += page_table[i].bytes_used;
648     }
649     return bytes_allocated;
650 }
651 #endif
652
653 /*
654  * Return the average age of the memory in a generation.
655  */
656 static int
657 gen_av_mem_age(int gen)
658 {
659     if (generations[gen].bytes_allocated == 0)
660         return 0;
661
662     return (((long long) generations[gen].cum_sum_bytes_allocated) << MEM_AGE_SHIFT) /
663         generations[gen].bytes_allocated;
664 }
665
666
667 void
668 save_fpu_state(void* state)
669 {
670 #if defined(i386) || defined(__x86_64)
671     if (fpu_mode == SSE2) {
672         sse_save(state);
673     } else {
674         fpu_save(state);
675     }
676 #else
677     fpu_save(state);
678 #endif    
679 }
680
681 void
682 restore_fpu_state(void* state)
683 {
684 #if defined(i386) || defined(__x86_64)
685     if (fpu_mode == SSE2) {
686         sse_restore(state);
687     } else {
688         fpu_restore(state);
689     }
690 #else
691     fpu_restore(state);
692 #endif
693 }
694
695
696 /*
697  * The verbose argument controls how much to print out:
698  * 0 for normal level of detail; 1 for debugging.
699  */
700 void
701 print_generation_stats(int verbose)
702 {
703     int i, gens;
704
705     FPU_STATE(fpu_state);
706     
707     /*
708      * This code uses the FP instructions which may be setup for Lisp so
709      * they need to the saved and reset for C.
710      */
711
712     save_fpu_state(fpu_state);
713
714     /* Number of generations to print out. */
715     if (verbose)
716         gens = NUM_GENERATIONS + 1;
717     else
718         gens = NUM_GENERATIONS;
719
720     /* Print the heap stats */
721     fprintf(stderr, "          Page count (%d KB)\n", GC_PAGE_SIZE / 1024);
722     fprintf(stderr,
723             "   Gen  Boxed Unboxed  LB   LUB    Alloc    Waste    Trigger   WP  GCs Mem-age\n");
724
725     for (i = 0; i < gens; i++) {
726         int j;
727         int boxed_cnt = 0;
728         int unboxed_cnt = 0;
729         int large_boxed_cnt = 0;
730         int large_unboxed_cnt = 0;
731
732         for (j = 0; j < last_free_page; j++) {
733             int flags = page_table[j].flags;
734
735             if ((flags & PAGE_GENERATION_MASK) == i) {
736                 if (flags & PAGE_ALLOCATED_MASK) {
737                     /*
738                      * Count the number of boxed and unboxed pages within the
739                      * given generation.
740                      */
741                     if (flags & PAGE_UNBOXED_MASK)
742                         if (flags & PAGE_LARGE_OBJECT_MASK)
743                             large_unboxed_cnt++;
744                         else
745                             unboxed_cnt++;
746                     else if (flags & PAGE_LARGE_OBJECT_MASK)
747                         large_boxed_cnt++;
748                     else
749                         boxed_cnt++;
750                 }
751             }
752         }
753
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);
763     }
764     fprintf(stderr, "   Total bytes alloc=%ld\n", bytes_allocated);
765
766     restore_fpu_state(fpu_state);
767 }
768
769 /* Get statistics that are kept "on the fly" out of the generation
770    array.
771 */
772 void
773 get_generation_stats(int gen, struct generation_stats *stats)
774 {
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;
785     }
786 }
787
788 void
789 set_gc_trigger(int gen, int trigger)
790 {
791     if (gen <= NUM_GENERATIONS) {
792         generations[gen].gc_trigger = trigger;
793     }
794 }
795
796 void
797 set_trigger_age(int gen, int trigger_age)
798 {
799     if (gen <= NUM_GENERATIONS) {
800         generations[gen].trigger_age = trigger_age;
801     }
802 }
803
804 void
805 set_min_mem_age(int gen, double min_mem_age)
806 {
807     if (gen <= NUM_GENERATIONS) {
808         generations[gen].min_av_mem_age = min_mem_age * MEM_AGE_SCALE;
809     }
810 }
811 \f
812 /*
813  * Allocation routines.
814  *
815  *
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.
819  *
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.
823  *
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.
828  *
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.
841  *
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
845  * closed.
846  *
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.
851  *
852  * Large objects may be allocated directly without an allocation
853  * region, the page tables are updated immediately.
854  *
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.
859  */
860
861 /*
862  * Only using two regions at present, both are for the current
863  * newspace generation.
864  */
865 struct alloc_region boxed_region;
866 struct alloc_region unboxed_region;
867
868 #if 0
869 /*
870  * X hack. current lisp code uses the following. Need coping in/out.
871  */
872 void *current_region_free_pointer;
873 void *current_region_end_addr;
874 #endif
875
876 /* The generation currently being allocated to. X */
877 static int gc_alloc_generation = 0;
878
879 extern void do_dynamic_space_overflow_warning(void);
880 extern void do_dynamic_space_overflow_error(void);
881
882 /* Handle heap overflow here, maybe. */
883 static void
884 handle_heap_overflow(const char *msg, int size)
885 {
886     unsigned long heap_size_mb;
887
888     if (msg) {
889         fprintf(stderr, msg, size);
890     }
891 #ifndef SPARSE_BLOCK_SIZE
892 #define SPARSE_BLOCK_SIZE (0)
893 #endif
894
895     /* Figure out how many MB of heap we have */
896     heap_size_mb = (dynamic_space_size + SPARSE_BLOCK_SIZE) >> 20;
897
898     fprintf(stderr, " CMUCL has run out of dynamic heap space (%lu MB).\n",
899             heap_size_mb);
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();
905     } else {
906         fprintf(stderr,
907                 "  You can control heap size with the -dynamic-space-size commandline option.\n");
908         do_dynamic_space_overflow_warning();
909     }
910 #else
911     print_generation_stats(1);
912
913     exit(1);
914 #endif
915 }
916
917 /*
918  * Find a new region with room for at least the given number of bytes.
919  *
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.
923  *
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.
926  *
927  * To assist the scavenging functions, write protected pages are not
928  * used. Free pages should not be write protected.
929  *
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
932  * time.
933  *
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.
940  */
941 static void
942 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
943 {
944     int first_page;
945     int last_page;
946     int region_size;
947     int restart_page;
948     int bytes_found;
949     int num_pages;
950     int i;
951     int mmask, mflags;
952
953     /* Shut up some compiler warnings */
954     last_page = bytes_found = 0;
955
956 #if 0
957     fprintf(stderr, "alloc_new_region for %d bytes from gen %d\n",
958             nbytes, gc_alloc_generation);
959 #endif
960
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);
965
966     if (unboxed)
967         restart_page =
968             generations[gc_alloc_generation].alloc_unboxed_start_page;
969     else
970         restart_page = generations[gc_alloc_generation].alloc_start_page;
971
972     /*
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.
976      */
977
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;
983
984     do {
985         first_page = restart_page;
986
987         /*
988          * First search for a page with at least 32 bytes free, that is
989          * not write protected, or marked dont_move.
990          */
991
992         while (first_page < dynamic_space_pages) {
993             int flags = page_table[first_page].flags;
994
995             if (!(flags & PAGE_ALLOCATED_MASK)
996                 || ((flags & mmask) == mflags &&
997                     page_table[first_page].bytes_used < GC_PAGE_SIZE - 32))
998                 break;
999             first_page++;
1000         }
1001
1002         /* Check for a failure */
1003         if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1004 #if 0
1005             handle_heap_overflow("*A2 gc_alloc_new_region failed, nbytes=%d.\n",
1006                                  nbytes);
1007 #else
1008             break;
1009 #endif
1010         }
1011
1012         gc_assert(!PAGE_WRITE_PROTECTED(first_page));
1013
1014 #if 0
1015         fprintf(stderr, "  first_page=%d bytes_used=%d\n",
1016                 first_page, page_table[first_page].bytes_used);
1017 #endif
1018
1019         /*
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.
1024          */
1025         last_page = first_page;
1026         bytes_found = GC_PAGE_SIZE - page_table[first_page].bytes_used;
1027         num_pages = 1;
1028         while ((bytes_found < nbytes || num_pages < 2)
1029                && last_page < dynamic_space_pages - 1
1030                && !PAGE_ALLOCATED(last_page + 1)) {
1031             last_page++;
1032             num_pages++;
1033             bytes_found += GC_PAGE_SIZE;
1034             gc_assert(!PAGE_WRITE_PROTECTED(last_page));
1035         }
1036
1037         region_size = (GC_PAGE_SIZE - page_table[first_page].bytes_used)
1038             + GC_PAGE_SIZE * (last_page - first_page);
1039
1040         gc_assert(bytes_found == region_size);
1041
1042 #if 0
1043         fprintf(stderr, "  last_page=%d bytes_found=%d num_pages=%d\n",
1044                 last_page, bytes_found, num_pages);
1045 #endif
1046
1047         restart_page = last_page + 1;
1048     }
1049     while (restart_page < dynamic_space_pages && bytes_found < nbytes);
1050
1051     if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1052         handle_heap_overflow("*A2 gc_alloc_new_region failed, nbytes=%d.\n",
1053                              nbytes);
1054     }
1055
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",
1060                              nbytes);
1061     }
1062 #if 0
1063     fprintf(stderr,
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));
1067 #endif
1068
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;
1076
1077     if (gencgc_zero_check) {
1078         int *p;
1079
1080         for (p = (int *) alloc_region->start_addr;
1081              p < (int *) alloc_region->end_addr; p++)
1082             if (*p != 0)
1083                 fprintf(stderr, "** new region not zero @ %lx\n",
1084                         (unsigned long) p);
1085     }
1086
1087     /* Setup the pages. */
1088
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;
1093     }
1094
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));
1099
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);
1105         /*
1106          * This may not be necessary for unboxed regions (think it was
1107          * broken before!)
1108          */
1109         page_table[i].first_object_offset =
1110             alloc_region->start_addr - page_address(i);
1111     }
1112
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));
1118
1119     }
1120 }
1121
1122
1123
1124 /*
1125  * If the record_new_objects flag is 2 then all new regions created
1126  * are recorded.
1127  *
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.
1131  *
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.
1136  *
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.
1140  */
1141
1142 #define NUM_NEW_AREAS 512
1143 static int record_new_objects = 0;
1144 static int new_areas_ignore_page;
1145 struct new_area {
1146     int page;
1147     int offset;
1148     int size;
1149 };
1150 static struct new_area (*new_areas)[];
1151 static int new_areas_index = 0;
1152 int max_new_areas;
1153
1154 /* Add a new area to new_areas. */
1155 static void
1156 add_new_area(int first_page, int offset, int size)
1157 {
1158     unsigned new_area_start, c;
1159     int i;
1160
1161     /* Ignore if full */
1162     if (new_areas_index >= NUM_NEW_AREAS)
1163         return;
1164
1165     switch (record_new_objects) {
1166       case 0:
1167           return;
1168       case 1:
1169           if (first_page > new_areas_ignore_page)
1170               return;
1171           break;
1172       case 2:
1173           break;
1174       default:
1175           gc_abort();
1176     }
1177
1178     new_area_start = GC_PAGE_SIZE * first_page + offset;
1179
1180     /*
1181      * Search backwards for a prior area that this follows from.  If
1182      * found this will save adding a new area.
1183      */
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;
1187
1188 #if 0
1189         fprintf(stderr, "*S1 %d %d %d %d\n", i, c, new_area_start, area_end);
1190 #endif
1191         if (new_area_start == area_end) {
1192 #if 0
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);
1196 #endif
1197             (*new_areas)[i].size += size;
1198             return;
1199         }
1200     }
1201 #if 0
1202     fprintf(stderr, "*S1 %d %d %d\n", i, c, new_area_start);
1203 #endif
1204
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;
1208 #if 0
1209     fprintf(stderr, "  new_area %d page %d offset %d size %d\n",
1210             new_areas_index, first_page, offset, size);
1211 #endif
1212     new_areas_index++;
1213
1214     /* Note the max new_areas used. */
1215     if (new_areas_index > max_new_areas)
1216         max_new_areas = new_areas_index;
1217 }
1218
1219
1220 /*
1221  * Update the tables for the alloc_region. The region may be added to
1222  * the new_areas.
1223  *
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
1227  * alloc_region.
1228  */
1229 void
1230 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
1231 {
1232     int more;
1233     int first_page;
1234     int next_page;
1235     int bytes_used;
1236     int orig_first_page_bytes_used;
1237     int region_size;
1238     int byte_cnt;
1239
1240 #if 0
1241     fprintf(stderr, "gc_alloc_update_page_tables to gen %d: ",
1242             gc_alloc_generation);
1243 #endif
1244
1245     first_page = alloc_region->first_page;
1246
1247     /* Catch an unused alloc_region. */
1248     if (first_page == 0 && alloc_region->last_page == -1)
1249         return;
1250
1251     next_page = first_page + 1;
1252
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;
1256
1257         gc_assert(alloc_region->start_addr == page_address(first_page) +
1258                   page_table[first_page].bytes_used);
1259
1260         /* All the pages used need to be updated */
1261
1262         /* Update the first page. */
1263
1264 #if 0
1265         fprintf(stderr, "0");
1266 #endif
1267
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);
1271
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));
1276
1277         byte_cnt = 0;
1278
1279         /*
1280          * Calc. the number of bytes used in this page. This is not always
1281          * the number of new bytes, unless it was free.
1282          */
1283         more = 0;
1284         bytes_used = alloc_region->free_pointer - page_address(first_page);
1285         if (bytes_used > GC_PAGE_SIZE) {
1286             bytes_used = GC_PAGE_SIZE;
1287             more = 1;
1288         }
1289         page_table[first_page].bytes_used = bytes_used;
1290         byte_cnt += bytes_used;
1291
1292         /*
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
1295          * the bytes_used.
1296          */
1297         while (more) {
1298 #if 0
1299             fprintf(stderr, "+");
1300 #endif
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));
1306
1307             gc_assert(page_table[next_page].first_object_offset ==
1308                       alloc_region->start_addr - page_address(next_page));
1309
1310             /* Calc. the number of bytes used in this page. */
1311             more = 0;
1312             bytes_used = alloc_region->free_pointer - page_address(next_page);
1313             if (bytes_used > GC_PAGE_SIZE) {
1314                 bytes_used = GC_PAGE_SIZE;
1315                 more = 1;
1316             }
1317             page_table[next_page].bytes_used = bytes_used;
1318             byte_cnt += bytes_used;
1319
1320             next_page++;
1321         }
1322
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;
1326
1327         gc_assert(byte_cnt - orig_first_page_bytes_used == region_size);
1328
1329         /*
1330          * Set the generations alloc restart page to the last page of
1331          * the region.
1332          */
1333         if (unboxed)
1334             generations[gc_alloc_generation].alloc_unboxed_start_page =
1335                 next_page - 1;
1336         else
1337             generations[gc_alloc_generation].alloc_start_page = next_page - 1;
1338
1339         /* Add the region to the new_areas if requested. */
1340         if (!unboxed)
1341             add_new_area(first_page, orig_first_page_bytes_used, region_size);
1342
1343 #if 0
1344         fprintf(stderr,
1345                 "  gc_alloc_update_page_tables update %d bytes to gen %d\n",
1346                 region_size, gc_alloc_generation);
1347 #endif
1348     } else
1349         /*
1350          * No bytes allocated. Unallocate the first_page if there are 0 bytes_used.
1351          */
1352     if (page_table[first_page].bytes_used == 0)
1353         page_table[first_page].flags &= ~PAGE_ALLOCATED_MASK;
1354
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;
1359         next_page++;
1360     }
1361
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);
1368
1369 #if 0
1370     fprintf(stderr, "\n");
1371 #endif
1372 }
1373
1374
1375
1376 static inline void *gc_quick_alloc(int nbytes);
1377
1378 /*
1379  * Allocate a possibly large object.
1380  */
1381 static void *
1382 gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
1383 {
1384     int first_page;
1385     int last_page;
1386     int region_size;
1387     int restart_page;
1388     int bytes_found;
1389     int num_pages;
1390     int orig_first_page_bytes_used;
1391     int byte_cnt;
1392     int more;
1393     int bytes_used;
1394     int next_page;
1395     int large = (nbytes >= large_object_size);
1396     int mmask, mflags;
1397
1398
1399     /* Shut up some compiler warnings */
1400     last_page = bytes_found = 0;
1401
1402 #if 0
1403     if (nbytes > 200000)
1404         fprintf(stderr, "*** alloc_large %d\n", nbytes);
1405 #endif
1406
1407 #if 0
1408     fprintf(stderr, "gc_alloc_large for %d bytes from gen %d\n",
1409             nbytes, gc_alloc_generation);
1410 #endif
1411
1412     /*
1413      * If the object is small, and there is room in the current region
1414      * then allocation it in the current region.
1415      */
1416     if (!large && alloc_region->end_addr - alloc_region->free_pointer >= nbytes)
1417         return gc_quick_alloc(nbytes);
1418
1419     /*
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
1422      * free page.
1423      */
1424
1425     /*
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.
1431      */
1432     if (unboxed)
1433         restart_page =
1434             generations[gc_alloc_generation].alloc_large_unboxed_start_page;
1435     else
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;
1439
1440     /* Setup the mask and matching flags. */
1441
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;
1447
1448     do {
1449         first_page = restart_page;
1450
1451         if (large)
1452             while (first_page < dynamic_space_pages
1453                    && PAGE_ALLOCATED(first_page)) first_page++;
1454         else
1455             while (first_page < dynamic_space_pages) {
1456                 int flags = page_table[first_page].flags;
1457
1458                 if (!(flags & PAGE_ALLOCATED_MASK)
1459                     || ((flags & mmask) == mflags &&
1460                         page_table[first_page].bytes_used < GC_PAGE_SIZE - 32))
1461                     break;
1462                 first_page++;
1463             }
1464
1465         /* Check for a failure */
1466         if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1467 #if 0
1468             handle_heap_overflow("*A2 gc_alloc_large failed, nbytes=%d.\n",
1469                                  nbytes);
1470 #else
1471             break;
1472 #endif
1473         }
1474         gc_assert(!PAGE_WRITE_PROTECTED(first_page));
1475
1476 #if 0
1477         fprintf(stderr, "  first_page=%d bytes_used=%d\n",
1478                 first_page, page_table[first_page].bytes_used);
1479 #endif
1480
1481         last_page = first_page;
1482         bytes_found = GC_PAGE_SIZE - page_table[first_page].bytes_used;
1483         num_pages = 1;
1484         while (bytes_found < nbytes
1485                && last_page < dynamic_space_pages - 1
1486                && !PAGE_ALLOCATED(last_page + 1)) {
1487             last_page++;
1488             num_pages++;
1489             bytes_found += GC_PAGE_SIZE;
1490             gc_assert(!PAGE_WRITE_PROTECTED(last_page));
1491         }
1492
1493         region_size = (GC_PAGE_SIZE - page_table[first_page].bytes_used)
1494             + GC_PAGE_SIZE * (last_page - first_page);
1495
1496         gc_assert(bytes_found == region_size);
1497
1498 #if 0
1499         fprintf(stderr, "  last_page=%d bytes_found=%d num_pages=%d\n",
1500                 last_page, bytes_found, num_pages);
1501 #endif
1502
1503         restart_page = last_page + 1;
1504     }
1505     while ((restart_page < dynamic_space_pages) && (bytes_found < nbytes));
1506
1507     if (first_page >= dynamic_space_pages - reserved_heap_pages) {
1508         handle_heap_overflow("*A2 gc_alloc_large failed, nbytes=%d.\n", nbytes);
1509     }
1510
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);
1515     }
1516 #if 0
1517     if (large)
1518         fprintf(stderr,
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));
1522 #endif
1523
1524     gc_assert(first_page > alloc_region->last_page);
1525     if (unboxed)
1526         generations[gc_alloc_generation].alloc_large_unboxed_start_page =
1527             last_page;
1528     else
1529         generations[gc_alloc_generation].alloc_large_start_page = last_page;
1530
1531     /* Setup the pages. */
1532     orig_first_page_bytes_used = page_table[first_page].bytes_used;
1533
1534     /*
1535      * If the first page was free then setup the gen, and
1536      * first_object_offset.
1537      */
1538
1539     if (large)
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;
1544     }
1545
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);
1550
1551     byte_cnt = 0;
1552
1553     /*
1554      * Calc. the number of bytes used in this page. This is not
1555      * always the number of new bytes, unless it was free.
1556      */
1557     more = 0;
1558     bytes_used = nbytes + orig_first_page_bytes_used;
1559     if (bytes_used > GC_PAGE_SIZE) {
1560         bytes_used = GC_PAGE_SIZE;
1561         more = 1;
1562     }
1563     page_table[first_page].bytes_used = bytes_used;
1564     byte_cnt += bytes_used;
1565
1566     next_page = first_page + 1;
1567
1568     /*
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
1571      * the bytes_used.
1572      */
1573     while (more) {
1574 #if 0
1575         fprintf(stderr, "+");
1576 #endif
1577
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);
1581
1582         page_table[next_page].first_object_offset =
1583             orig_first_page_bytes_used - GC_PAGE_SIZE * (next_page - first_page);
1584
1585         /* Calc. the number of bytes used in this page. */
1586         more = 0;
1587         bytes_used = nbytes + orig_first_page_bytes_used - byte_cnt;
1588         if (bytes_used > GC_PAGE_SIZE) {
1589             bytes_used = GC_PAGE_SIZE;
1590             more = 1;
1591         }
1592         page_table[next_page].bytes_used = bytes_used;
1593         byte_cnt += bytes_used;
1594
1595         next_page++;
1596     }
1597
1598     gc_assert(byte_cnt - orig_first_page_bytes_used == nbytes);
1599
1600     bytes_allocated += nbytes;
1601     generations[gc_alloc_generation].bytes_allocated += nbytes;
1602
1603     /* Add the region to the new_areas if requested. */
1604     if (!unboxed)
1605         add_new_area(first_page, orig_first_page_bytes_used, nbytes);
1606
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));
1612     }
1613
1614     return (void *) (page_address(first_page) + orig_first_page_bytes_used);
1615 }
1616
1617 /*
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
1620  * to a new region.
1621  */
1622
1623 int region_empty_threshold = 32;
1624
1625
1626 /*
1627  * How many consecutive large alloc we can do before we abandon the
1628  * current region.
1629  */
1630 int consecutive_large_alloc_limit = 10;
1631
1632
1633 /*
1634  * Statistics for the current region
1635  */
1636 struct alloc_stats 
1637 {
1638     /*
1639      * How many consecutive allocations we have tried with the current
1640      * region (in saved_region)
1641      */
1642     int consecutive_alloc;
1643     /*
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
1646      * different region.
1647      */
1648     int abandon_region_count;
1649
1650     /*
1651      * A copy of the current allocation region which we use to compare
1652      * against.
1653      */
1654     struct alloc_region saved_region;
1655 };
1656
1657 /* Statistics for boxed and unboxed regions */
1658 struct alloc_stats boxed_stats =
1659 {0, 0, 
1660  {NULL, NULL, -1, -1, NULL}};
1661      
1662 struct alloc_stats unboxed_stats =
1663 {0, 0, 
1664  {NULL, NULL, -1, -1, NULL}};
1665
1666 /*
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
1669  * (void*) -1.
1670  */
1671 static inline void *
1672 gc_alloc_try_current_region(int nbytes, struct alloc_region *region, int unboxed,
1673                             struct alloc_stats *stats)
1674 {
1675     char *new_free_pointer;
1676
1677     /* Check if there is room in the current alloc region. */
1678     new_free_pointer = region->free_pointer + nbytes;
1679
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;
1683
1684         region->free_pointer = new_free_pointer;
1685
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);
1692         }
1693
1694         stats->consecutive_alloc = 0;
1695         stats->abandon_region_count = 0;
1696         memcpy(&stats->saved_region, region, sizeof(stats->saved_region));
1697         
1698         return (void *) new_obj;
1699     } else {
1700         return (void *) -1;
1701     }
1702 }
1703
1704 /*
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
1709  * (unboxed).
1710  */
1711 static void *
1712 gc_alloc_region(int nbytes, struct alloc_region *region, int unboxed, struct alloc_stats *stats)
1713 {
1714     void *new_obj;
1715     
1716 #if 0
1717     fprintf(stderr, "gc_alloc %d\n", nbytes);
1718 #endif
1719
1720     /* Check if there is room in the current alloc region. */
1721
1722     new_obj = gc_alloc_try_current_region(nbytes, region, unboxed, stats);
1723     if (new_obj != (void *) -1) {
1724         return new_obj;
1725     }
1726
1727     /* Else not enough free space in the current region. */
1728
1729     /*
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.
1732      */
1733     
1734     if (nbytes >= large_object_size) {
1735         return gc_alloc_large(nbytes, unboxed, region);
1736     }
1737     
1738     /*
1739      * If there is a bit of room left in the current region then
1740      * allocate a large object.
1741      */
1742
1743     /*
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.
1753      *
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
1756      * trap.
1757      *
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.
1762      *
1763      */
1764
1765     if ((region->end_addr - region->free_pointer > region_empty_threshold)
1766         && (stats->consecutive_alloc < consecutive_large_alloc_limit)) {
1767         /*
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.
1772          */
1773         if (memcmp(&stats->saved_region, region, sizeof(stats->saved_region)) == 0) {
1774             ++stats->consecutive_alloc;
1775         } else {
1776             stats->consecutive_alloc = 0;
1777             stats->abandon_region_count = 0;
1778             memcpy(&stats->saved_region, region, sizeof(stats->saved_region));
1779         }
1780         
1781         return gc_alloc_large(nbytes, unboxed, region);
1782     }
1783
1784     /*
1785      * We given up on the current region because the
1786      * consecutive_large_alloc_limit has been reached.
1787      */
1788     stats->consecutive_alloc = 0;
1789     ++stats->abandon_region_count;
1790
1791     /* Finished with the current region. */
1792     gc_alloc_update_page_tables(unboxed, region);
1793
1794     /* Setup a new region. */
1795     gc_alloc_new_region(nbytes, unboxed, region);
1796
1797     /* Should now be enough room. */
1798
1799     new_obj = gc_alloc_try_current_region(nbytes, region, unboxed, stats);
1800     if (new_obj != (void *) -1) {
1801         return new_obj;
1802     }
1803
1804     /* Shouldn't happen? */
1805     gc_assert(0);
1806     return 0;
1807 }
1808
1809 /*
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.
1813  */
1814 static inline void *
1815 gc_alloc(int nbytes)
1816 {
1817     void* obj;
1818
1819     obj = gc_alloc_region(nbytes, &boxed_region, 0, &boxed_stats);
1820
1821     return obj;
1822 }
1823
1824 /*
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.
1828  */
1829 static inline void *
1830 gc_quick_alloc(int nbytes)
1831 {
1832     char *new_free_pointer;
1833
1834     /* Check if there is room in the current region. */
1835     new_free_pointer = boxed_region.free_pointer + nbytes;
1836
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;
1840
1841         boxed_region.free_pointer = new_free_pointer;
1842         return (void *) new_obj;
1843     }
1844
1845     /* Else call gc_alloc */
1846     return gc_alloc(nbytes);
1847 }
1848
1849 /*
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.
1854  */
1855 static inline void *
1856 gc_quick_alloc_large(int nbytes)
1857 {
1858     char *new_free_pointer;
1859
1860     if (nbytes >= large_object_size)
1861         return gc_alloc_large(nbytes, 0, &boxed_region);
1862
1863     /* Check if there is room in the current region. */
1864     new_free_pointer = boxed_region.free_pointer + nbytes;
1865
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;
1869
1870         boxed_region.free_pointer = new_free_pointer;
1871         return (void *) new_obj;
1872     }
1873
1874     /* Else call gc_alloc */
1875     return gc_alloc(nbytes);
1876 }
1877
1878 static inline void *
1879 gc_alloc_unboxed(int nbytes)
1880 {
1881     void *obj;
1882
1883     obj = gc_alloc_region(nbytes, &unboxed_region, 1, &unboxed_stats);
1884
1885     return obj;
1886 }
1887
1888 static inline void *
1889 gc_quick_alloc_unboxed(int nbytes)
1890 {
1891     char *new_free_pointer;
1892
1893     /* Check if there is room in the current region. */
1894     new_free_pointer = unboxed_region.free_pointer + nbytes;
1895
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;
1899
1900         unboxed_region.free_pointer = new_free_pointer;
1901
1902         return (void *) new_obj;
1903     }
1904
1905     /* Else call gc_alloc */
1906     return gc_alloc_unboxed(nbytes);
1907 }
1908
1909 /*
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.
1913  *
1914  * A pointer to the start of the region is returned.
1915  */
1916 static inline void *
1917 gc_quick_alloc_large_unboxed(int nbytes)
1918 {
1919     char *new_free_pointer;
1920
1921     if (nbytes >= large_object_size)
1922         return gc_alloc_large(nbytes, 1, &unboxed_region);
1923
1924     /* Check if there is room in the current region. */
1925     new_free_pointer = unboxed_region.free_pointer + nbytes;
1926
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;
1930
1931         unboxed_region.free_pointer = new_free_pointer;
1932
1933         return (void *) new_obj;
1934     }
1935
1936     /* Else call gc_alloc */
1937     return gc_alloc_unboxed(nbytes);
1938 }
1939
1940 /***************************************************************************/
1941 \f
1942
1943 /* Scavenging/transporting routines derived from gc.c */
1944
1945 static int (*scavtab[256]) (lispobj * where, lispobj object);
1946 static lispobj(*transother[256]) (lispobj object);
1947 static int (*sizetab[256]) (lispobj * where);
1948
1949 static struct weak_pointer *weak_pointers;
1950 static struct scavenger_hook *scavenger_hooks = (struct scavenger_hook *) NIL;
1951
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)))
1954 \f
1955
1956 /* Predicates */
1957
1958 static inline boolean
1959 from_space_p(lispobj obj)
1960 {
1961     int page_index = (char *) obj - heap_base;
1962
1963     return page_index >= 0
1964         && (page_index =
1965             (unsigned int) page_index / GC_PAGE_SIZE) < dynamic_space_pages
1966         && PAGE_GENERATION(page_index) == from_space;
1967 }
1968
1969 static inline boolean
1970 new_space_p(lispobj obj)
1971 {
1972     int page_index = (char *) obj - heap_base;
1973
1974     return page_index >= 0
1975         && (page_index =
1976             (unsigned int) page_index / GC_PAGE_SIZE) < dynamic_space_pages
1977         && PAGE_GENERATION(page_index) == new_space;
1978 }
1979
1980 static inline boolean
1981 dynamic_space_p(lispobj obj)
1982 {
1983     lispobj end = DYNAMIC_0_SPACE_START + DYNAMIC_SPACE_SIZE;
1984
1985     return (obj >= DYNAMIC_0_SPACE_START) && (obj < end);
1986 }
1987
1988 static inline boolean
1989 static_space_p(lispobj obj)
1990 {
1991     lispobj end = SymbolValue(STATIC_SPACE_FREE_POINTER);
1992
1993     return (obj >= STATIC_SPACE_START) && (obj < end);
1994 }
1995
1996 static inline boolean
1997 read_only_space_p(lispobj obj)
1998 {
1999     lispobj end = SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
2000
2001     return (obj >= READ_ONLY_SPACE_START) && (obj < end);
2002 }
2003
2004 static inline boolean
2005 control_stack_space_p(lispobj obj)
2006 {
2007     lispobj end = CONTROL_STACK_START + control_stack_size;
2008
2009     return (obj >= CONTROL_STACK_START) && (obj < end);
2010 }
2011
2012 static inline boolean
2013 binding_stack_space_p(lispobj obj)
2014 {
2015     lispobj end = BINDING_STACK_START + binding_stack_size;
2016
2017     return (obj >= BINDING_STACK_START) && (obj < end);
2018 }
2019     
2020 static inline boolean
2021 signal_space_p(lispobj obj)
2022 {
2023 #ifdef SIGNAL_STACK_START
2024     lispobj end = SIGNAL_STACK_START + SIGSTKSZ;
2025
2026     return (obj >= SIGNAL_STACK_START) && (obj < end);
2027 #else
2028     return FALSE;
2029 #endif    
2030 }
2031
2032 #if (defined(DARWIN) && defined(__ppc__))
2033 /*
2034  * The assembly code defines these as functions, so we make them
2035  * functions.  We only care about their addresses anyway.
2036  */
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 */
2043 #else
2044 extern int undefined_tramp;
2045 #endif
2046
2047 /*
2048  * Other random places that can't be in malloc space.  Return TRUE if
2049  * obj is in some other known space
2050  */
2051 static inline boolean
2052 other_space_p(lispobj obj)
2053 {
2054     boolean in_space = FALSE;
2055     
2056 #if defined(sparc)
2057     extern char _end;
2058     
2059     /*
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
2064      * sparc.
2065      */
2066     if ((char*) obj <= &_end) {
2067         in_space = TRUE;
2068     }
2069 #elif defined(i386)
2070 #if defined(DARWIN) || defined(__linux__) || defined(__FreeBSD__) || defined(__NetBSD__)
2071     /*
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).
2075      *
2076      * Is there anything else?
2077      */
2078     if (obj == (lispobj) 0xffffffe9) {
2079         in_space = TRUE;
2080     }
2081 #elif defined(__ppc__)
2082     /*
2083      * For ppc, just ignore anything below fpu_restore, which is
2084      * currently at the end of ppc-assem.S.
2085      */
2086     if (obj <= (lispobj) &fpu_restore) {
2087         in_space = TRUE;
2088     }
2089 #endif
2090 #endif  
2091
2092     return in_space;
2093 }
2094
2095 \f
2096
2097 /* Copying Objects */
2098
2099
2100 /* Copying Boxed Objects */
2101 static inline lispobj
2102 copy_object(lispobj object, int nwords)
2103 {
2104     int tag;
2105     lispobj *new;
2106     lispobj *source, *dest;
2107
2108     gc_assert(Pointerp(object));
2109     gc_assert(from_space_p(object));
2110     gc_assert((nwords & 0x01) == 0);
2111
2112     /* get tag of object */
2113     tag = LowtagOf(object);
2114
2115     /* allocate space */
2116     new = gc_quick_alloc(nwords * sizeof(lispobj));
2117
2118     dest = new;
2119     source = (lispobj *) PTR(object);
2120
2121     /* copy the object */
2122     while (nwords > 0) {
2123         dest[0] = source[0];
2124         dest[1] = source[1];
2125         dest += 2;
2126         source += 2;
2127         nwords -= 2;
2128     }
2129
2130     /* return lisp pointer of new object */
2131     return (lispobj) new | tag;
2132 }
2133
2134 /*
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.
2138  *
2139  * Vectors may have shrunk. If the object is not copied the space
2140  * needs to be reclaimed, and the page_tables corrected.
2141  */
2142 static lispobj
2143 copy_large_object(lispobj object, int nwords)
2144 {
2145     int tag;
2146     lispobj *new;
2147     lispobj *source, *dest;
2148     int first_page;
2149
2150     gc_assert(Pointerp(object));
2151     gc_assert(from_space_p(object));
2152     gc_assert((nwords & 0x01) == 0);
2153
2154     if (gencgc_verbose && nwords > 1024 * 1024)
2155         fprintf(stderr, "** copy_large_object: %lu\n",
2156                 (unsigned long) (nwords * sizeof(lispobj)));
2157
2158     /* Check if it's a large object. */
2159     first_page = find_page_index((void *) object);
2160     gc_assert(first_page >= 0);
2161
2162     if (PAGE_LARGE_OBJECT(first_page)) {
2163         /* Promote the object. */
2164         int remaining_bytes;
2165         int next_page;
2166         int bytes_freed;
2167         int old_bytes_used;
2168         int mmask, mflags;
2169
2170         /*
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
2175          * anyway?).
2176          */
2177
2178         gc_assert(page_table[first_page].first_object_offset == 0);
2179
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);
2190
2191             PAGE_FLAGS_UPDATE(next_page, PAGE_GENERATION_MASK, new_space);
2192
2193             /*
2194              * Remove any write protection.  Should be able to religh on the
2195              * WP flag to avoid redundant calls.
2196              */
2197             if (PAGE_WRITE_PROTECTED(next_page)) {
2198                 os_protect((os_vm_address_t) page_address(next_page), GC_PAGE_SIZE,
2199                            OS_VM_PROT_ALL);
2200                 page_table[next_page].flags &= ~PAGE_WRITE_PROTECTED_MASK;
2201             }
2202             remaining_bytes -= GC_PAGE_SIZE;
2203             next_page++;
2204         }
2205
2206         /*
2207          * Now only one page remains, but the object may have shrunk so
2208          * there may be more unused pages which will be freed.
2209          */
2210
2211         /* Object may have shrunk but shouldn't have grown - check. */
2212         gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2213
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));
2217
2218         /* Adjust the bytes_used. */
2219         old_bytes_used = page_table[next_page].bytes_used;
2220         page_table[next_page].bytes_used = remaining_bytes;
2221
2222         bytes_freed = old_bytes_used - remaining_bytes;
2223
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;
2227
2228         /* Free any remaining pages; needs care. */
2229         next_page++;
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)) {
2234             /*
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.
2239              */
2240             gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2241
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;
2246             next_page++;
2247         }
2248
2249         if (gencgc_verbose && bytes_freed > 0)
2250             fprintf(stderr, "* copy_large_boxed bytes_freed %d\n", bytes_freed);
2251
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;
2256
2257         /* Add the region to the new_areas if requested. */
2258         add_new_area(first_page, 0, nwords * sizeof(lispobj));
2259
2260         return object;
2261     } else {
2262         /* get tag of object */
2263         tag = LowtagOf(object);
2264
2265         /* allocate space */
2266         new = gc_quick_alloc_large(nwords * sizeof(lispobj));
2267
2268         dest = new;
2269         source = (lispobj *) PTR(object);
2270
2271         /* copy the object */
2272         while (nwords > 0) {
2273             dest[0] = source[0];
2274             dest[1] = source[1];
2275             dest += 2;
2276             source += 2;
2277             nwords -= 2;
2278         }
2279
2280         /* return lisp pointer of new object */
2281         return (lispobj) new | tag;
2282     }
2283 }
2284
2285 /* Copying UnBoxed Objects. */
2286 static inline lispobj
2287 copy_unboxed_object(lispobj object, int nwords)
2288 {
2289     int tag;
2290     lispobj *new;
2291     lispobj *source, *dest;
2292
2293     gc_assert(Pointerp(object));
2294     gc_assert(from_space_p(object));
2295     gc_assert((nwords & 0x01) == 0);
2296
2297     /* get tag of object */
2298     tag = LowtagOf(object);
2299
2300     /* allocate space */
2301     new = gc_quick_alloc_unboxed(nwords * sizeof(lispobj));
2302
2303     dest = new;
2304     source = (lispobj *) PTR(object);
2305
2306     /* Copy the object */
2307     while (nwords > 0) {
2308         dest[0] = source[0];
2309         dest[1] = source[1];
2310         dest += 2;
2311         source += 2;
2312         nwords -= 2;
2313     }
2314
2315     /* Return lisp pointer of new object. */
2316     return (lispobj) new | tag;
2317 }
2318
2319
2320 /*
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.
2324  *
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.
2327  */
2328 static lispobj
2329 copy_large_unboxed_object(lispobj object, int nwords)
2330 {
2331     int tag;
2332     lispobj *new;
2333     lispobj *source, *dest;
2334     int first_page;
2335
2336     gc_assert(Pointerp(object));
2337     gc_assert(from_space_p(object));
2338     gc_assert((nwords & 0x01) == 0);
2339
2340     if (gencgc_verbose && nwords > 1024 * 1024)
2341         fprintf(stderr, "** copy_large_unboxed_object: %lu\n",
2342                 (unsigned long) (nwords * sizeof(lispobj)));
2343
2344     /* Check if it's a large object. */
2345     first_page = find_page_index((void *) object);
2346     gc_assert(first_page >= 0);
2347
2348     if (PAGE_LARGE_OBJECT(first_page)) {
2349         /*
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.
2353          */
2354         int remaining_bytes;
2355         int next_page;
2356         int bytes_freed;
2357         int old_bytes_used;
2358         int mmask, mflags;
2359
2360         gc_assert(page_table[first_page].first_object_offset == 0);
2361
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);
2371
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;
2376             next_page++;
2377         }
2378
2379         /*
2380          * Now only one page remains, but the object may have shrunk so
2381          * there may be more unused pages which will be freed.
2382          */
2383
2384         /* Object may have shrunk but shouldn't have grown - check. */
2385         gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2386
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);
2390
2391         /* Adjust the bytes_used. */
2392         old_bytes_used = page_table[next_page].bytes_used;
2393         page_table[next_page].bytes_used = remaining_bytes;
2394
2395         bytes_freed = old_bytes_used - remaining_bytes;
2396
2397         mmask = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK
2398             | PAGE_GENERATION_MASK;
2399         mflags = PAGE_ALLOCATED_MASK | PAGE_LARGE_OBJECT_MASK | from_space;
2400
2401         /* Free any remaining pages; needs care. */
2402         next_page++;
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)) {
2407             /*
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.
2412              */
2413             gc_assert(!PAGE_WRITE_PROTECTED(next_page));
2414
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;
2419             next_page++;
2420         }
2421
2422         if (gencgc_verbose && bytes_freed > 0)
2423             fprintf(stderr, "* copy_large_unboxed bytes_freed %d\n",
2424                     bytes_freed);
2425
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;
2430
2431         return object;
2432     } else {
2433         /* get tag of object */
2434         tag = LowtagOf(object);
2435
2436         /* allocate space */
2437         new = gc_quick_alloc_large_unboxed(nwords * sizeof(lispobj));
2438
2439         dest = new;
2440         source = (lispobj *) PTR(object);
2441
2442         /* copy the object */
2443         while (nwords > 0) {
2444             dest[0] = source[0];
2445             dest[1] = source[1];
2446             dest += 2;
2447             source += 2;
2448             nwords -= 2;
2449         }
2450
2451         /* return lisp pointer of new object */
2452         return (lispobj) new | tag;
2453     }
2454 }
2455
2456 static inline boolean
2457 maybe_static_array_p(lispobj header)
2458 {
2459     boolean result;
2460     
2461     switch (TypeOf(header)) {
2462         /*
2463          * This needs to be coordinated to the set of allowed
2464          * static vectors in make-array.
2465          */
2466       case type_SimpleString:
2467       case type_SimpleArrayUnsignedByte8:
2468       case type_SimpleArrayUnsignedByte16:
2469       case type_SimpleArrayUnsignedByte32:
2470 #ifdef type_SimpleArraySignedByte8
2471       case type_SimpleArraySignedByte8:
2472 #endif
2473 #ifdef type_SimpleArraySignedByte16
2474       case type_SimpleArraySignedByte16:
2475 #endif
2476 #ifdef type_SimpleArraySignedByte32
2477       case type_SimpleArraySignedByte32:
2478 #endif
2479       case type_SimpleArraySingleFloat:
2480       case type_SimpleArrayDoubleFloat:
2481 #ifdef type_SimpleArrayLongFloat
2482       case type_SimpleArrayLongFloat:
2483 #endif
2484 #ifdef type_SimpleArrayComplexSingleFloat
2485       case type_SimpleArrayComplexSingleFloat:
2486 #endif
2487 #ifdef type_SimpleArrayComplexDoubleFloat
2488       case type_SimpleArrayComplexDoubleFloat:
2489 #endif
2490 #ifdef type_SimpleArrayComplexLongFloat
2491       case type_SimpleArrayComplexLongFloat:
2492 #endif
2493           result = TRUE;
2494           break;
2495       default:
2496           result = FALSE;
2497     }
2498     return result;
2499 }
2500
2501 \f
2502
2503 /* Scavenging */
2504
2505 /*
2506  * Douglas Crosher says:
2507  *
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.
2514  */
2515
2516 #define DIRECT_SCAV 0
2517
2518 static void
2519 scavenge(void *start_obj, long nwords)
2520 {
2521     lispobj *start;
2522
2523     start = (lispobj *) start_obj;
2524
2525     while (nwords > 0) {
2526         lispobj object;
2527         int words_scavenged;
2528
2529         object = *start;
2530         /* Not a forwarding pointer. */
2531         gc_assert(object != 0x01);
2532
2533 #if DIRECT_SCAV
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);
2539 #endif
2540
2541             if (from_space_p(object)) {
2542                 lispobj *ptr = (lispobj *) PTR(object);
2543                 lispobj first_word = *ptr;
2544
2545                 if (first_word == 0x01) {
2546                     *start = ptr[1];
2547                     words_scavenged = 1;
2548                 } else {
2549                     words_scavenged = scavtab[TypeOf(object)] (start, object);
2550                 }
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;
2556             } else {
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);    
2562                 }
2563                 
2564                 if (1) {
2565                     lispobj header = *ptr;
2566                     if (debug_static_array_p) {
2567                         fprintf(stderr, "  Header value = 0x%lx\n", (unsigned long) header);
2568                     }
2569                     
2570                     if (maybe_static_array_p(header)) {
2571                         int static_p;
2572
2573                         if (debug_static_array_p) {
2574                             fprintf(stderr, "Possible static vector at %p.  header = 0x%lx\n",
2575                                     ptr, (unsigned long) header);
2576                         }
2577                       
2578                         static_p = (HeaderValue(header) & 1) == 1;
2579                         if (static_p) {
2580                             /*
2581                              * We have a static vector.  Mark it as
2582                              * reachable by setting the MSB of the header.
2583                              */
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);
2588                             }
2589                         }
2590                     }
2591                 }
2592             }
2593         } else if ((object & 3) == 0)
2594             words_scavenged = 1;
2595         else
2596             words_scavenged = scavtab[TypeOf(object)] (start, object);
2597 #endif /* not DIRECT_SCAV */
2598
2599         start += words_scavenged;
2600         nwords -= words_scavenged;
2601     }
2602
2603     gc_assert(nwords == 0);
2604 }
2605 \f
2606
2607 #if !(defined(i386) || defined(__x86_64))
2608 /* Scavenging Interrupt Contexts */
2609
2610 static int boxed_registers[] = BOXED_REGISTERS;
2611
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 */
2622
2623 #define INTERIOR_POINTER_VARS(name) \
2624     unsigned long name;             \
2625     unsigned long name##_offset;    \
2626     int name##_register_pair
2627
2628 #define PAIR_INTERIOR_POINTER(name, accessor)           \
2629     name = accessor;                                    \
2630     pair_interior_pointer(context, name,                \
2631                           &name##_offset,               \
2632                           &name##_register_pair)
2633
2634 /*
2635  * Do we need to check if the register we're fixing up is in the
2636  * from-space?
2637  */
2638 #define FIXUP_INTERIOR_POINTER(name, accessor)                          \
2639     do {                                                                \
2640         if (name##_register_pair >= 0) {                                \
2641             accessor =                                                  \
2642                 PTR(SC_REG(context, name##_register_pair))              \
2643                 + name##_offset;                                        \
2644         }                                                               \
2645     } while (0)
2646
2647
2648 static void
2649 pair_interior_pointer(os_context_t *context, unsigned long pointer,
2650                       unsigned long *saved_offset, int *register_pair)
2651 {
2652     int i;
2653
2654     /*
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.
2658      */
2659     *saved_offset = 0x7FFFFFFF;
2660     *register_pair = -1;
2661     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2662         unsigned long reg;
2663         long offset;
2664         int index;
2665
2666         index = boxed_registers[i];
2667         reg = SC_REG(context, index);
2668
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,
2679          * 2010-Jul-14 */
2680
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;
2686             }
2687         }
2688     }
2689 }
2690
2691
2692 static void
2693 scavenge_interrupt_context(os_context_t * context)
2694 {
2695     int i;
2696
2697     INTERIOR_POINTER_VARS(pc);
2698 #ifdef reg_LIP
2699     INTERIOR_POINTER_VARS(lip);
2700 #endif
2701 #ifdef reg_LR
2702     INTERIOR_POINTER_VARS(lr);
2703 #endif
2704 #ifdef reg_CTR    
2705     INTERIOR_POINTER_VARS(ctr);
2706 #endif
2707 #ifdef SC_NPC
2708     INTERIOR_POINTER_VARS(npc);
2709 #endif
2710
2711 #ifdef reg_LIP
2712     PAIR_INTERIOR_POINTER(lip, SC_REG(context, reg_LIP));
2713 #endif /* reg_LIP */
2714
2715     PAIR_INTERIOR_POINTER(pc, SC_PC(context));
2716
2717 #ifdef SC_NPC
2718     PAIR_INTERIOR_POINTER(npc, SC_NPC(context));
2719 #endif    
2720
2721 #ifdef reg_LR
2722     PAIR_INTERIOR_POINTER(lr, SC_REG(context, reg_LR));
2723 #endif
2724
2725 #ifdef reg_CTR
2726     PAIR_INTERIOR_POINTER(ctr, SC_REG(context, reg_CTR));
2727 #endif    
2728     
2729     /* Scanvenge all boxed registers in the context. */
2730     for (i = 0; i < (sizeof(boxed_registers) / sizeof(int)); i++) {
2731         int index;
2732         lispobj foo;
2733
2734         index = boxed_registers[i];
2735         foo = SC_REG(context, index);
2736         scavenge(&foo, 1);
2737         SC_REG(context, index) = foo;
2738
2739         scavenge(&(SC_REG(context, index)), 1);
2740     }
2741
2742     /*
2743      * Now that the scavenging is done, repair the various interior
2744      * pointers.
2745      */
2746 #ifdef reg_LIP
2747     FIXUP_INTERIOR_POINTER(lip, SC_REG(context, reg_LIP));
2748 #endif
2749
2750     FIXUP_INTERIOR_POINTER(pc, SC_PC(context));
2751
2752 #ifdef SC_NPC
2753     FIXUP_INTERIOR_POINTER(npc, SC_NPC(context));
2754 #endif
2755
2756 #ifdef reg_LR
2757     FIXUP_INTERIOR_POINTER(lr, SC_REG(context, reg_LR));
2758 #endif
2759
2760 #ifdef reg_CTR
2761     FIXUP_INTERIOR_POINTER(ctr, SC_REG(context, reg_CTR));
2762 #endif
2763 }
2764
2765 void
2766 scavenge_interrupt_contexts(void)
2767 {
2768     int i, index;
2769     os_context_t *context;
2770
2771 #ifdef PRINTNOISE
2772     printf("Scavenging interrupt contexts ...\n");
2773 #endif
2774
2775     index = fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX));
2776
2777 #if defined(DEBUG_PRINT_CONTEXT_INDEX)
2778     printf("Number of active contexts: %d\n", index);
2779 #endif
2780
2781     for (i = 0; i < index; i++) {
2782         context = lisp_interrupt_contexts[i];
2783         scavenge_interrupt_context(context);
2784     }
2785 }
2786 #endif
2787 \f
2788 /* Code and Code-Related Objects */
2789
2790 /*
2791  * Aargh!  Why is SPARC so different here?  What is the advantage of
2792  * making it different from all the other ports?
2793  */
2794 #if defined(sparc) || (defined(DARWIN) && defined(__ppc__))
2795 #define RAW_ADDR_OFFSET 0
2796 #else
2797 #define RAW_ADDR_OFFSET (6 * sizeof(lispobj) - type_FunctionPointer)
2798 #endif
2799
2800 static lispobj trans_function_header(lispobj object);
2801 static lispobj trans_boxed(lispobj object);
2802
2803 #if DIRECT_SCAV
2804 static int
2805 scav_function_pointer(lispobj * where, lispobj object)
2806 {
2807     gc_assert(Pointerp(object));
2808
2809     if (from_space_p(object)) {
2810         lispobj first, *first_pointer;
2811
2812         /*
2813          * Object is a pointer into from space - check to see if it has
2814          * been forwarded.
2815          */
2816         first_pointer = (lispobj *) PTR(object);
2817         first = *first_pointer;
2818
2819         if (first == 0x01) {
2820             /* Forwarded */
2821             *where = first_pointer[1];
2822             return 1;
2823         } else {
2824             int type;
2825             lispobj copy;
2826
2827             /*
2828              * Must transport object -- object may point to either a
2829              * function header, a closure function header, or to a closure
2830              * header.
2831              */
2832
2833             type = TypeOf(first);
2834             switch (type) {
2835               case type_FunctionHeader:
2836               case type_ClosureFunctionHeader:
2837                   copy = trans_function_header(object);
2838                   break;
2839               default:
2840                   copy = trans_boxed(object);
2841                   break;
2842             }
2843
2844             if (copy != object) {
2845                 /* Set forwarding pointer. */
2846                 first_pointer[0] = 0x01;
2847                 first_pointer[1] = copy;
2848             }
2849
2850             first = copy;
2851         }
2852
2853         gc_assert(Pointerp(first));
2854         gc_assert(!from_space_p(first));
2855
2856         *where = first;
2857     }
2858     return 1;
2859 }
2860 #else
2861 static int
2862 scav_function_pointer(lispobj * where, lispobj object)
2863 {
2864     lispobj *first_pointer;
2865     lispobj copy;
2866
2867     gc_assert(Pointerp(object));
2868
2869     /* Object is a pointer into from space - no a FP. */
2870     first_pointer = (lispobj *) PTR(object);
2871
2872     /*
2873      * Must transport object -- object may point to either a function
2874      * header, a closure function header, or to a closure header.
2875      */
2876
2877     switch (TypeOf(*first_pointer)) {
2878       case type_FunctionHeader:
2879       case type_ClosureFunctionHeader:
2880           copy = trans_function_header(object);
2881           break;
2882       default:
2883           copy = trans_boxed(object);
2884           break;
2885     }
2886
2887     if (copy != object) {
2888         /* Set forwarding pointer */
2889         first_pointer[0] = 0x01;
2890         first_pointer[1] = copy;
2891     }
2892
2893     gc_assert(Pointerp(copy));
2894     gc_assert(!from_space_p(copy));
2895
2896     *where = copy;
2897
2898     return 1;
2899 }
2900 #endif
2901
2902 #if defined(i386) || defined(__x86_64)
2903 /*
2904  * Scan an x86 compiled code object, looking for possible fixups that
2905  * have been missed after a move.
2906  *
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.
2910  *
2911  * Currently only absolution fixups to the constant vector, or to the
2912  * code area are checked.
2913  */
2914 void
2915 sniff_code_object(struct code *code, unsigned displacement)
2916 {
2917     int nheader_words, ncode_words, nwords;
2918     char *p;
2919     char *constants_start_addr, *constants_end_addr;
2920     char *code_start_addr, *code_end_addr;
2921     int fixup_found = 0;
2922
2923     if (!check_code_fixups)
2924         return;
2925
2926     /*
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.
2929      */
2930     if (code->trace_table_offset & 0x3) {
2931 #if 0
2932         fprintf(stderr, "*** Sniffing byte compiled code object at %x.\n",
2933                 code);
2934 #endif
2935         return;
2936     }
2937
2938     /* Else it's x86 machine code. */
2939
2940     ncode_words = fixnum_value(code->code_size);
2941     nheader_words = HeaderValue(*(lispobj *) code);
2942     nwords = ncode_words + nheader_words;
2943
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);
2948
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);
2958
2959         /*
2960          * Check for code references.
2961          *
2962          * Check for a 32 bit word that looks like an absolute reference
2963          * to within the code adea of the code object.
2964          */
2965         if (data >= code_start_addr - displacement
2966             && data < code_end_addr - displacement) {
2967             /* Function header */
2968             if (d4 == 0x5e
2969                 && ((unsigned long) p - 4 -
2970                     4 * HeaderValue(*((unsigned long *) p - 1))) ==
2971                 (unsigned long) code) {
2972                 /* Skip the function header */
2973                 p += 6 * 4 - 4 - 1;
2974                 continue;
2975             }
2976             /* Push imm32 */
2977             if (d1 == 0x68) {
2978                 fixup_found = 1;
2979                 fprintf(stderr,
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);
2984             }
2985             /* Mov [reg-8],imm32 */
2986             if (d3 == 0xc7
2987                 && (d2 == 0x40 || d2 == 0x41 || d2 == 0x42 || d2 == 0x43
2988                     || d2 == 0x45 || d2 == 0x46 || d2 == 0x47)
2989                 && d1 == 0xf8) {
2990                 fixup_found = 1;
2991                 fprintf(stderr,
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);
2997             }
2998             /* Lea reg, [disp32] */
2999             if (d2 == 0x8d && (d1 & 0xc7) == 5) {
3000                 fixup_found = 1;
3001                 fprintf(stderr,
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);
3007             }
3008         }
3009
3010         /*
3011          * Check for constant references.
3012          *
3013          * Check for a 32 bit word that looks like an absolution reference
3014          * to within the constant vector. Constant references will be
3015          * aligned.
3016          */
3017         if (data >= constants_start_addr - displacement
3018             && data < constants_end_addr - displacement
3019             && ((unsigned long) data & 0x3) == 0) {
3020             /*  Mov eax,m32 */
3021             if (d1 == 0xa1) {
3022                 fixup_found = 1;
3023                 fprintf(stderr,
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);
3028             }
3029
3030             /*  Mov m32,eax */
3031             if (d1 == 0xa3) {
3032                 fixup_found = 1;
3033                 fprintf(stderr,
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);
3038             }
3039
3040             /* Cmp m32,imm32 */
3041             if (d1 == 0x3d && d2 == 0x81) {
3042                 fixup_found = 1;
3043                 fprintf(stderr,
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);
3047                 /* XX Check this */
3048                 fprintf(stderr, "***  Cmp 0x%.8lx,immed32\n",
3049                         (unsigned long) data);
3050             }
3051
3052             /* Check for a mod=00, r/m=101 byte. */
3053             if ((d1 & 0xc7) == 5) {
3054                 /* Cmp m32,reg */
3055                 if (d2 == 0x39) {
3056                     fixup_found = 1;
3057                     fprintf(stderr,
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);
3063                 }
3064                 /* Cmp reg32,m32 */
3065                 if (d2 == 0x3b) {
3066                     fixup_found = 1;
3067                     fprintf(stderr,
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);
3073                 }
3074                 /* Mov m32,reg32 */
3075                 if (d2 == 0x89) {
3076                     fixup_found = 1;
3077                     fprintf(stderr,
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);
3083                 }
3084                 /* Mov reg32,m32 */
3085                 if (d2 == 0x8b) {
3086                     fixup_found = 1;
3087                     fprintf(stderr,
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);
3093                 }
3094                 /* Lea reg32,m32 */
3095                 if (d2 == 0x8d) {
3096                     fixup_found = 1;
3097                     fprintf(stderr,
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);
3103                 }
3104             }
3105         }
3106     }
3107
3108     /* If anything was found print out some info. on the code object. */
3109     if (fixup_found) {
3110         fprintf(stderr,
3111                 "*** Compiled code object at %lx: header_words=%d code_words=%d .\n",
3112                 (unsigned long) code, nheader_words, ncode_words);
3113         fprintf(stderr,
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);
3118     }
3119 }
3120
3121 static void
3122 apply_code_fixups(struct code *old_code, struct code *new_code)
3123 {
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 =
3129
3130         (unsigned long) new_code - (unsigned long) old_code;
3131     struct vector *fixups_vector;
3132
3133     /*
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.
3136      */
3137     if (new_code->trace_table_offset & 0x3) {
3138 #if 0
3139         fprintf(stderr, "*** Byte compiled code object at %x.\n", new_code);
3140 #endif
3141         return;
3142     }
3143
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;
3148 #if 0
3149     fprintf(stderr,
3150             "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
3151             new_code, nheader_words, ncode_words);
3152 #endif
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);
3157 #if 0
3158     fprintf(stderr,
3159             "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
3160             constants_start_addr, constants_end_addr, code_start_addr,
3161             code_end_addr);
3162 #endif
3163
3164     /*
3165      * The first constant should be a pointer to the fixups for this
3166      * code objects - Check.
3167      */
3168     fixups = new_code->constants[0];
3169
3170     /*
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.
3173      */
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);
3178
3179 #if 0
3180         fprintf(stderr, "Fixups for code object not found!?\n");
3181         fprintf(stderr,
3182                 "*** Compiled code object at %x: header_words=%d code_words=%d .\n",
3183                 new_code, nheader_words, ncode_words);
3184         fprintf(stderr,
3185                 "*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
3186                 constants_start_addr, constants_end_addr, code_start_addr,
3187                 code_end_addr);
3188 #endif
3189         return;
3190     }
3191
3192     fixups_vector = (struct vector *) PTR(fixups);
3193
3194     /* Could be pointing to a forwarding pointer. */
3195     if (Pointerp(fixups) && find_page_index((void *) fixups_vector) != -1
3196         && fixups_vector->header == 0x01) {
3197 #if 0
3198         fprintf(stderr, "* FF\n");
3199 #endif
3200         /* If so then follow it. */
3201         fixups_vector = (struct vector *) PTR((lispobj) fixups_vector->length);
3202     }
3203 #if 0
3204     fprintf(stderr, "Got the fixups\n");
3205 #endif
3206
3207     if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
3208         /*
3209          * Got the fixups for the code block.  Now work through the
3210          * vector, and apply a fixup at each address.
3211          */
3212         int length = fixnum_value(fixups_vector->length);
3213         int i;
3214
3215         for (i = 0; i < length; i++) {
3216             unsigned offset = fixups_vector->data[i];
3217
3218             /* Now check the current value of offset. */
3219             unsigned long old_value =
3220                 *(unsigned long *) ((unsigned long) code_start_addr + offset);
3221
3222             /*
3223              * If it's within the old_code object then it must be an
3224              * absolute fixup (relative ones are not saved).
3225              */
3226             if (old_value >= (unsigned long) old_code
3227                 && old_value <
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;
3232             else
3233                 /*
3234                  * It is outside the old code object so it must be a relative
3235                  * fixup (absolute fixups are not saved). So subtract the
3236                  * displacement.
3237                  */
3238                 *(unsigned long *) ((unsigned long) code_start_addr + offset) =
3239                     old_value - displacement;
3240         }
3241     }
3242
3243     /* Check for possible errors. */
3244     if (check_code_fixups)
3245         sniff_code_object(new_code, displacement);
3246 }
3247 #endif
3248
3249 static struct code *
3250 trans_code(struct code *code)
3251 {
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;
3257
3258 #if 0
3259     fprintf(stderr, "\nTransporting code object located at 0x%08x.\n",
3260             (unsigned long) code);
3261 #endif
3262
3263     /* If object has already been transported, just return pointer */
3264     if (*(lispobj *) code == 0x01) {
3265         return (struct code *) (((lispobj *) code)[1]);
3266     }
3267
3268
3269     gc_assert(TypeOf(code->header) == type_CodeHeader);
3270
3271     /* prepare to transport the code vector */
3272     l_code = (lispobj) code | type_OtherPointer;
3273
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);
3278
3279     l_new_code = copy_large_object(l_code, nwords);
3280     new_code = (struct code *) PTR(l_new_code);
3281
3282     /* May not have been moved. */
3283     if (new_code == code)
3284         return new_code;
3285
3286     displacement = l_new_code - l_code;
3287
3288 #if 0
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);
3292 #endif
3293
3294     /* set forwarding pointer */
3295     ((lispobj *) code)[0] = 0x01;
3296     ((lispobj *) code)[1] = l_new_code;
3297
3298     /*
3299      * Set forwarding pointers for all the function headers in the code
3300      * object; also fix all self pointers.
3301      */
3302
3303     fheaderl = code->entry_points;
3304     prev_pointer = &new_code->entry_points;
3305
3306     while (fheaderl != NIL) {
3307         struct function *fheaderp, *nfheaderp;
3308         lispobj nfheaderl;
3309
3310         fheaderp = (struct function *) PTR(fheaderl);
3311         gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3312
3313         /*
3314          * Calcuate the new function pointer and the new function header.
3315          */
3316         nfheaderl = fheaderl + displacement;
3317         nfheaderp = (struct function *) PTR(nfheaderl);
3318
3319         /* set forwarding pointer */
3320         ((lispobj *) fheaderp)[0] = 0x01;
3321         ((lispobj *) fheaderp)[1] = nfheaderl;
3322
3323         /* Fix self pointer */
3324         nfheaderp->self = nfheaderl + RAW_ADDR_OFFSET;
3325
3326         *prev_pointer = nfheaderl;
3327
3328         fheaderl = fheaderp->next;
3329         prev_pointer = &nfheaderp->next;
3330     }
3331
3332 #if 0
3333     sniff_code_object(new_code, displacement);
3334 #endif
3335 #if defined(i386) || defined(__x86_64)
3336     apply_code_fixups(code, new_code);
3337 #else
3338     /* From gc.c */
3339 #ifndef MACH
3340     os_flush_icache((os_vm_address_t) (((int *) new_code) + nheader_words),
3341                     ncode_words * sizeof(int));
3342 #endif
3343 #endif
3344
3345     return new_code;
3346 }
3347
3348 static int
3349 scav_code_header(lispobj * where, lispobj object)
3350 {
3351     struct code *code;
3352     int nheader_words, ncode_words, nwords;
3353     lispobj fheaderl;
3354     struct function *fheaderp;
3355
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);
3361
3362     /* Scavenge the boxed section of the code data block */
3363     scavenge(where + 1, nheader_words - 1);
3364
3365     /*
3366      * Scavenge the boxed section of each function object in the code
3367      * data block
3368      */
3369     fheaderl = code->entry_points;
3370     while (fheaderl != NIL) {
3371         fheaderp = (struct function *) PTR(fheaderl);
3372         gc_assert(TypeOf(fheaderp->header) == type_FunctionHeader);
3373
3374         scavenge(&fheaderp->name, 1);
3375         scavenge(&fheaderp->arglist, 1);
3376         scavenge(&fheaderp->type, 1);
3377
3378         fheaderl = fheaderp->next;
3379     }
3380
3381     return nwords;
3382 }
3383
3384 static lispobj
3385 trans_code_header(lispobj object)
3386 {
3387     struct code *ncode;
3388
3389     ncode = trans_code((struct code *) PTR(object));
3390     return (lispobj) ncode | type_OtherPointer;
3391 }
3392
3393 static int
3394 size_code_header(lispobj * where)
3395 {
3396     struct code *code;
3397     int nheader_words, ncode_words, nwords;
3398
3399     code = (struct code *) where;
3400
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);
3405
3406     return nwords;
3407 }
3408
3409 #if !(defined(i386) || defined(__x86_64))
3410
3411 static int
3412 scav_return_pc_header(lispobj * where, lispobj object)
3413 {
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);
3418     lose(NULL);
3419     return 0;
3420 }
3421
3422 #endif /* not i386 */
3423
3424 static lispobj
3425 trans_return_pc_header(lispobj object)
3426 {
3427     struct function *return_pc;
3428     unsigned long offset;
3429     struct code *code, *ncode;
3430
3431     return_pc = (struct function *) PTR(object);
3432     offset = HeaderValue(return_pc->header) * sizeof(lispobj);
3433
3434     /* Transport the whole code object */
3435     code = (struct code *) ((unsigned long) return_pc - offset);
3436
3437     ncode = trans_code(code);
3438
3439     return ((lispobj) ncode + offset) | type_OtherPointer;
3440 }
3441
3442 /*
3443  * On the 386, closures hold a pointer to the raw address instead of
3444  * the function object.
3445  */
3446 #if defined(i386) || defined(__x86_64)
3447
3448 static int
3449 scav_closure_header(lispobj * where, lispobj object)
3450 {
3451     struct closure *closure;
3452     lispobj fun;
3453
3454     closure = (struct closure *) where;
3455     fun = closure->function - RAW_ADDR_OFFSET;
3456 #if !(defined(i386) && defined(SOLARIS))
3457     scavenge(&fun, 1);
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;
3462 #else
3463     /*
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
3468      * transport.
3469      *
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.
3473      */
3474     if (closure->function) {
3475         scavenge(&fun, 1);
3476         /*
3477          * The function may have moved so update the raw address. But don't
3478          * write unnecessarily.
3479          */
3480         if (closure->function != fun + RAW_ADDR_OFFSET) {
3481 #if 0
3482             fprintf(stderr, "closure header 0x%04x moved from %p to %p\n",
3483                     closure->header, (void*) closure->function, (void*) (fun + RAW_ADDR_OFFSET));
3484 #endif
3485             closure->function = fun + RAW_ADDR_OFFSET;
3486         }
3487     }
3488 #if 0
3489      else {
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);
3493     }
3494 #endif
3495 #endif
3496     return 2;
3497 }
3498
3499 #endif /* i386 */
3500
3501 #if !(defined(i386) || defined(__x86_64))
3502
3503 static int
3504 scav_function_header(lispobj * where, lispobj object)
3505 {
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);
3510     lose(NULL);
3511     return 0;
3512 }
3513
3514 #endif /* not i386 */
3515
3516 static lispobj
3517 trans_function_header(lispobj object)
3518 {
3519     struct function *fheader;
3520     unsigned long offset;
3521     struct code *code, *ncode;
3522
3523     fheader = (struct function *) PTR(object);
3524     offset = HeaderValue(fheader->header) * sizeof(lispobj);
3525
3526     /* Transport the whole code object */
3527     code = (struct code *) ((unsigned long) fheader - offset);
3528     ncode = trans_code(code);
3529
3530     return ((lispobj) ncode + offset) | type_FunctionPointer;
3531 }
3532 \f
3533
3534 /* Instances */
3535
3536 #if DIRECT_SCAV
3537 static int
3538 scav_instance_pointer(lispobj * where, lispobj object)
3539 {
3540     if (from_space_p(object)) {
3541         lispobj first, *first_pointer;
3542
3543         /*
3544          * object is a pointer into from space.  check to see if it has
3545          * been forwarded
3546          */
3547         first_pointer = (lispobj *) PTR(object);
3548         first = *first_pointer;
3549
3550         if (first == 0x01)
3551             /* Forwarded. */
3552             first = first_pointer[1];
3553         else {
3554             first = trans_boxed(object);
3555             gc_assert(first != object);
3556             /* Set forwarding pointer */
3557             first_pointer[0] = 0x01;
3558             first_pointer[1] = first;
3559         }
3560         *where = first;
3561     }
3562     return 1;
3563 }
3564 #else
3565 static int
3566 scav_instance_pointer(lispobj * where, lispobj object)
3567 {
3568     lispobj copy, *first_pointer;
3569
3570     /* Object is a pointer into from space - not a FP */
3571     copy = trans_boxed(object);
3572
3573     gc_assert(copy != object);
3574
3575     first_pointer = (lispobj *) PTR(object);
3576
3577     /* Set forwarding pointer. */
3578     first_pointer[0] = 0x01;
3579     first_pointer[1] = copy;
3580     *where = copy;
3581
3582     return 1;
3583 }
3584 #endif
3585 \f
3586
3587 /* Lists and Conses */
3588
3589 static lispobj trans_list(lispobj object);
3590
3591 #if DIRECT_SCAV
3592 static int
3593 scav_list_pointer(lispobj * where, lispobj object)
3594 {
3595     gc_assert(Pointerp(object));
3596
3597     if (from_space_p(object)) {
3598         lispobj first, *first_pointer;
3599
3600         /*
3601          * Object is a pointer into from space - check to see if it has
3602          * been forwarded.
3603          */
3604         first_pointer = (lispobj *) PTR(object);
3605         first = *first_pointer;
3606
3607         if (first == 0x01)
3608             /* Forwarded. */
3609             first = first_pointer[1];
3610         else {
3611             first = trans_list(object);
3612
3613             /* Set forwarding pointer */
3614             first_pointer[0] = 0x01;
3615             first_pointer[1] = first;
3616         }
3617
3618         gc_assert(Pointerp(first));
3619         gc_assert(!from_space_p(first));
3620         *where = first;
3621     }
3622     return 1;
3623 }
3624 #else
3625 static int
3626 scav_list_pointer(lispobj * where, lispobj object)
3627 {
3628     lispobj first, *first_pointer;
3629
3630     gc_assert(Pointerp(object));
3631
3632     /* Object is a pointer into from space - not FP */
3633
3634     first = trans_list(object);
3635     gc_assert(first != object);
3636
3637     first_pointer = (lispobj *) PTR(object);
3638
3639     /* Set forwarding pointer */
3640     first_pointer[0] = 0x01;
3641     first_pointer[1] = first;
3642
3643     gc_assert(Pointerp(first));
3644     gc_assert(!from_space_p(first));
3645     *where = first;
3646     return 1;
3647 }
3648 #endif
3649
3650 static lispobj
3651 trans_list(lispobj object)
3652 {
3653     lispobj new_list_pointer;
3654     struct cons *cons, *new_cons;
3655     lispobj cdr;
3656
3657     gc_assert(from_space_p(object));
3658
3659     cons = (struct cons *) PTR(object);
3660
3661     /* copy 'object' */
3662     new_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3663
3664     new_cons->car = cons->car;
3665     new_cons->cdr = cons->cdr;  /* updated later */
3666     new_list_pointer = (lispobj) new_cons | LowtagOf(object);
3667
3668     /* Grab the cdr before it is clobbered */
3669     cdr = cons->cdr;
3670
3671     /* Set forwarding pointer (clobbers start of list). */
3672     cons->car = 0x01;
3673     cons->cdr = new_list_pointer;
3674
3675     /* Try to linearize the list in the cdr direction to help reduce paging. */
3676     while (1) {
3677         lispobj new_cdr;
3678         struct cons *cdr_cons, *new_cdr_cons;
3679
3680         if (LowtagOf(cdr) != type_ListPointer || !from_space_p(cdr)
3681             || *((lispobj *) PTR(cdr)) == 0x01)
3682             break;
3683
3684         cdr_cons = (struct cons *) PTR(cdr);
3685
3686         /* copy 'cdr' */
3687         new_cdr_cons = (struct cons *) gc_quick_alloc(sizeof(struct cons));
3688
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);
3692
3693         /* Grab the cdr before it is clobbered */
3694         cdr = cdr_cons->cdr;
3695
3696         /* Set forwarding pointer */
3697         cdr_cons->car = 0x01;
3698         cdr_cons->cdr = new_cdr;
3699
3700         /*
3701          * Update the cdr of the last cons copied into new space to keep
3702          * the newspace scavenge from having to do it.
3703          */
3704         new_cons->cdr = new_cdr;
3705
3706         new_cons = new_cdr_cons;
3707     }
3708
3709     return new_list_pointer;
3710 }
3711 \f
3712
3713 /* Scavenging and Transporting Other Pointers */
3714
3715 #if DIRECT_SCAV
3716 static int
3717 scav_other_pointer(lispobj * where, lispobj object)
3718 {
3719     gc_assert(Pointerp(object));
3720
3721     if (from_space_p(object)) {
3722         lispobj first, *first_pointer;
3723
3724         /*
3725          * Object is a pointer into from space.  check to see if it has
3726          * been forwarded.
3727          */
3728         first_pointer = (lispobj *) PTR(object);
3729         first = *first_pointer;
3730
3731         if (first == 0x01) {
3732             /* Forwarded. */
3733             first = first_pointer[1];
3734             *where = first;
3735         } else {
3736             first = (transother[TypeOf(first)]) (object);
3737
3738             if (first != object) {
3739                 /* Set forwarding pointer */
3740                 first_pointer[0] = 0x01;
3741                 first_pointer[1] = first;
3742                 *where = first;
3743             }
3744         }
3745
3746         gc_assert(Pointerp(first));
3747         gc_assert(!from_space_p(first));
3748     }
3749     return 1;
3750 }
3751 #else
3752 static int
3753 scav_other_pointer(lispobj * where, lispobj object)
3754 {
3755     lispobj first, *first_pointer;
3756
3757     gc_assert(Pointerp(object));
3758
3759     /* Object is a pointer into from space - not FP */
3760     first_pointer = (lispobj *) PTR(object);
3761
3762     first = (transother[TypeOf(*first_pointer)]) (object);
3763
3764     if (first != object) {
3765         /* Set forwarding pointer */
3766         first_pointer[0] = 0x01;
3767         first_pointer[1] = first;
3768         *where = first;
3769     }
3770
3771     gc_assert(Pointerp(first));
3772     gc_assert(!from_space_p(first));
3773
3774     return 1;
3775 }
3776 #endif
3777 \f
3778
3779 /* Immediate, Boxed, and Unboxed Objects */
3780
3781 static int
3782 size_pointer(lispobj * where)
3783 {
3784     return 1;
3785 }
3786
3787 static int
3788 scav_immediate(lispobj * where, lispobj object)
3789 {
3790     return 1;
3791 }
3792
3793 static lispobj
3794 trans_immediate(lispobj object)
3795 {
3796     fprintf(stderr, "GC lossage.  Trying to transport an immediate!?\n");
3797     lose(NULL);
3798     return NIL;
3799 }
3800
3801 static int
3802 size_immediate(lispobj * where)
3803 {
3804     return 1;
3805 }
3806
3807
3808 static int
3809 scav_boxed(lispobj * where, lispobj object)
3810 {
3811     return 1;
3812 }
3813
3814 static lispobj
3815 trans_boxed(lispobj object)
3816 {
3817     lispobj header;
3818     unsigned long length;
3819
3820     gc_assert(Pointerp(object));
3821
3822     header = *((lispobj *) PTR(object));
3823     length = HeaderValue(header) + 1;
3824     length = CEILING(length, 2);
3825
3826     return copy_object(object, length);
3827 }
3828
3829 static lispobj
3830 trans_boxed_large(lispobj object)
3831 {
3832     lispobj header;
3833     unsigned long length;
3834
3835     gc_assert(Pointerp(object));
3836
3837     header = *((lispobj *) PTR(object));
3838     length = HeaderValue(header) + 1;
3839     length = CEILING(length, 2);
3840
3841     return copy_large_object(object, length);
3842 }
3843
3844 static int
3845 size_boxed(lispobj * where)
3846 {
3847     lispobj header;
3848     unsigned long length;
3849
3850     header = *where;
3851     length = HeaderValue(header) + 1;
3852     length = CEILING(length, 2);
3853
3854     return length;
3855 }
3856
3857 /* Not needed on sparc and ppc because the raw_addr has a function lowtag */
3858 #if !(defined(sparc) || (defined(DARWIN) && defined(__ppc__)))
3859 static int
3860 scav_fdefn(lispobj * where, lispobj object)
3861 {
3862     struct fdefn *fdefn;
3863
3864     fdefn = (struct fdefn *) where;
3865
3866     if ((char *) (fdefn->function + RAW_ADDR_OFFSET) == fdefn->raw_addr) {
3867         scavenge(where + 1, sizeof(struct fdefn) / sizeof(lispobj) - 1);
3868
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);
3872
3873         return sizeof(struct fdefn) / sizeof(lispobj);
3874     } else
3875         return 1;
3876 }
3877 #endif
3878
3879 static int
3880 scav_unboxed(lispobj * where, lispobj object)
3881 {
3882     unsigned long length;
3883
3884     length = HeaderValue(object) + 1;
3885     length = CEILING(length, 2);
3886
3887     return length;
3888 }
3889
3890 static lispobj
3891 trans_unboxed(lispobj object)
3892 {
3893     lispobj header;
3894     unsigned long length;
3895
3896
3897     gc_assert(Pointerp(object));
3898
3899     header = *((lispobj *) PTR(object));
3900     length = HeaderValue(header) + 1;
3901     length = CEILING(length, 2);
3902
3903     return copy_unboxed_object(object, length);
3904 }
3905
3906 static lispobj
3907 trans_unboxed_large(lispobj object)
3908 {
3909     lispobj header;
3910     unsigned long length;
3911
3912
3913     gc_assert(Pointerp(object));
3914
3915     header = *((lispobj *) PTR(object));
3916     length = HeaderValue(header) + 1;
3917     length = CEILING(length, 2);
3918
3919     return copy_large_unboxed_object(object, length);
3920 }
3921
3922 static int
3923 size_unboxed(lispobj * where)
3924 {
3925     lispobj header;
3926     unsigned long length;
3927
3928     header = *where;
3929     length = HeaderValue(header) + 1;
3930     length = CEILING(length, 2);
3931
3932     return length;
3933 }
3934 \f
3935
3936 /* Vector-Like Objects */
3937
3938 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
3939
3940 static int
3941 size_string(lispobj * where)
3942 {
3943     struct vector *vector;
3944     int length, nwords;
3945
3946     /*
3947      * NOTE: Strings contain one more byte of data than the length
3948      * slot indicates.
3949      */
3950
3951     vector = (struct vector *) where;
3952     length = fixnum_value(vector->length) + 1;
3953 #ifndef UNICODE
3954 #ifdef __x86_64
3955     nwords = CEILING(NWORDS(length, 8) + 2, 2);
3956 #else
3957     nwords = CEILING(NWORDS(length, 4) + 2, 2);
3958 #endif
3959 #else
3960     /*
3961      * Strings are just like arrays with 16-bit elements, and contain
3962      * one more element than the slot length indicates.
3963      */
3964     nwords = CEILING(NWORDS(length, 2) + 2, 2);
3965 #endif
3966     return nwords;
3967 }
3968
3969 static int
3970 scav_string(lispobj * where, lispobj object)
3971 {
3972     return size_string(where);
3973 }
3974
3975 static lispobj
3976 trans_string(lispobj object)
3977 {
3978     gc_assert(Pointerp(object));
3979     return copy_large_unboxed_object(object,
3980                                      size_string((lispobj *) PTR(object)));
3981 }
3982 \f
3983
3984 /************************************************************************
3985                              Hash Tables
3986 ************************************************************************/
3987
3988 /* This struct corresponds to the Lisp HASH-TABLE structure defined in
3989    hash-new.lisp.  */
3990
3991 struct hash_table {
3992     lispobj instance_header;    /* 0 */
3993     lispobj dummy2;
3994     lispobj test;
3995     lispobj test_fun;
3996     lispobj hash_fun;
3997     lispobj rehash_size;        /* 5 */
3998     lispobj rehash_threshold;
3999     lispobj rehash_trigger;
4000     lispobj number_entries;
4001     lispobj table;
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;
4009 };
4010
4011 /* The size of a hash-table in Lisp objects.  */
4012
4013 #define HASH_TABLE_SIZE (sizeof (struct hash_table) / sizeof (lispobj))
4014
4015 /* Compute the EQ-hash of KEY.  This must be the same as what's used
4016    in hash-new.lisp.  */
4017
4018 #define EQ_HASH(key) ((key) & 0x1fffffff)
4019
4020 /* List of weak hash tables chained through their WEAK-P slot.  Set to
4021    NIL at the start of a collection.
4022
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.  */
4027
4028 static lispobj weak_hash_tables;
4029
4030 /* Return true if OBJ will survive the current GC.  */
4031
4032 static inline int
4033 survives_gc(lispobj obj)
4034 {
4035     if (!Pointerp(obj) || !from_space_p(obj))
4036         return 1;
4037     return *(lispobj *) PTR(obj) == 1;
4038 }
4039
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.  */
4043
4044 static inline unsigned *
4045 u32_vector(lispobj obj, unsigned *length)
4046 {
4047     unsigned *ptr = NULL;
4048
4049     if (Pointerp(obj)) {
4050         lispobj *p = (lispobj *) PTR(obj);
4051
4052         if (TypeOf(p[0]) == type_SimpleArrayUnsignedByte32) {
4053             ptr = (unsigned *) (p + 2);
4054             if (length)
4055                 *length = fixnum_value(p[1]);
4056         }
4057     }
4058
4059     return ptr;
4060 }
4061
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.  */
4065
4066 static inline void
4067 free_hash_entry(struct hash_table *hash_table, int hash_index, int kv_index)
4068 {
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);
4072     int free_p = 1;
4073     
4074     gc_assert(length != UINT_MAX);
4075
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];
4080     else {
4081         /* The entry is not the first in the collision chain.  */
4082         unsigned prev = index_vector[hash_index];
4083         unsigned i = next_vector[prev];
4084
4085         while (i && i != kv_index)
4086             prev = i, i = next_vector[i];
4087
4088         if (i == kv_index)
4089             next_vector[prev] = next_vector[kv_index];
4090         else
4091             free_p = 0;
4092     }
4093
4094     if (free_p) {
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;
4100         
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);
4105         /*
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.
4109          */
4110         
4111         kv_vector += 2;         /* Skip over vector header and length slots */
4112         empty_symbol = kv_vector[1];
4113
4114         hash_index = EQ_HASH(kv_vector[2 * kv_index]) % length;
4115         
4116         kv_vector[2 * kv_index] = empty_symbol;
4117         kv_vector[2 * kv_index + 1] = empty_symbol;
4118         if (hash_vector) {
4119             hash_vector[hash_index] = EQ_BASED_HASH_VALUE;
4120         }
4121     }
4122 }
4123
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.  */
4127
4128 static inline void
4129 record_for_rehashing(struct hash_table *hash_table, int hash_index,
4130                      int kv_index)
4131 {
4132     unsigned *index_vector = u32_vector(hash_table->index_vector, 0);
4133     unsigned *next_vector = u32_vector(hash_table->next_vector, 0);
4134     int rehash_p = 1;
4135
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];
4140     else {
4141         unsigned prev = index_vector[hash_index];
4142         unsigned i = next_vector[prev];
4143
4144         while (i && i != kv_index)
4145             prev = i, i = next_vector[i];
4146
4147         if (i == kv_index)
4148             next_vector[prev] = next_vector[kv_index];
4149         else
4150             rehash_p = 0;
4151     }
4152
4153     if (rehash_p) {
4154         next_vector[kv_index] = fixnum_value(hash_table->needing_rehash);
4155         hash_table->needing_rehash = make_fixnum(kv_index);
4156     }
4157 }
4158
4159 static inline boolean
4160 eq_based_hash_vector(unsigned int* hash_vector, unsigned int index)
4161 {
4162     return (hash_vector == 0) || (hash_vector[index] == EQ_BASED_HASH_VALUE);
4163 }
4164
4165 static inline boolean
4166 removable_weak_key(lispobj old_key, unsigned int index_value, boolean eq_hash_p)
4167 {
4168   return (!survives_gc(old_key)
4169           && eq_hash_p
4170           && (index_value != 0));
4171 }
4172
4173 static inline boolean
4174 removable_weak_value(lispobj value, unsigned int index_value)
4175 {
4176     /*
4177      * The entry can be removed if the value can be GCed.
4178      */
4179     return (!survives_gc(value)
4180             && (index_value != 0));
4181 }
4182
4183 static inline boolean
4184 removable_weak_key_and_value(lispobj old_key, lispobj value, unsigned int index_value,
4185                              boolean eq_hash_p)
4186 {
4187   boolean removable_key;
4188   boolean removable_val;
4189   
4190   removable_key = (!survives_gc(old_key)
4191                    && eq_hash_p
4192                    && (index_value != 0));
4193   removable_val = (!survives_gc(value)
4194                    && (index_value != 0));
4195
4196   /*
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.
4199    */
4200   return removable_key || removable_val;
4201 }
4202
4203 static inline boolean
4204 removable_weak_key_or_value(lispobj old_key, lispobj value, unsigned int index_value,
4205                             boolean eq_hash_p)
4206 {
4207   boolean removable_key;
4208   boolean removable_val;
4209   
4210   removable_key = (!survives_gc(old_key)
4211                    && eq_hash_p
4212                    && (index_value != 0));
4213   removable_val = (!survives_gc(value)
4214                    && (index_value != 0));
4215
4216   /*
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.
4220    */
4221   return (removable_key && removable_val);
4222 }
4223
4224 static void
4225 maybe_record_for_rehashing(struct hash_table *hash_table, lispobj* kv_vector,