0.7.13.5
[sbcl.git] / src / runtime / gencgc.c
1 /*
2  * GENerational Conservative Garbage Collector for SBCL x86
3  */
4
5 /*
6  * This software is part of the SBCL system. See the README file for
7  * more information.
8  *
9  * This software is derived from the CMU CL system, which was
10  * written at Carnegie Mellon University and released into the
11  * public domain. The software is in the public domain and is
12  * provided with absolutely no warranty. See the COPYING and CREDITS
13  * files for more information.
14  */
15
16 /*
17  * For a review of garbage collection techniques (e.g. generational
18  * GC) and terminology (e.g. "scavenging") see Paul R. Wilson,
19  * "Uniprocessor Garbage Collection Techniques". As of 20000618, this
20  * had been accepted for _ACM Computing Surveys_ and was available
21  * as a PostScript preprint through
22  *   <http://www.cs.utexas.edu/users/oops/papers.html>
23  * as
24  *   <ftp://ftp.cs.utexas.edu/pub/garbage/bigsurv.ps>.
25  */
26
27 #include <stdio.h>
28 #include <signal.h>
29 #include <sys/ptrace.h>
30 #include <linux/user.h>
31 #include <errno.h>
32 #include "runtime.h"
33 #include "sbcl.h"
34 #include "os.h"
35 #include "interr.h"
36 #include "globals.h"
37 #include "interrupt.h"
38 #include "validate.h"
39 #include "lispregs.h"
40 #include "arch.h"
41 #include "gc.h"
42 #include "gc-internal.h"
43 #include "genesis/vector.h"
44 #include "genesis/weak-pointer.h"
45 #include "genesis/simple-fun.h"
46 #include "genesis/static-symbols.h"
47 #include "genesis/symbol.h"
48 /* assembly language stub that executes trap_PendingInterrupt */
49 void do_pending_interrupt(void);
50
51 \f
52 /*
53  * GC parameters
54  */
55
56 /* the number of actual generations. (The number of 'struct
57  * generation' objects is one more than this, because one object
58  * serves as scratch when GC'ing.) */
59 #define NUM_GENERATIONS 6
60
61 /* Should we use page protection to help avoid the scavenging of pages
62  * that don't have pointers to younger generations? */
63 boolean enable_page_protection = 1;
64
65 /* Should we unmap a page and re-mmap it to have it zero filled? */
66 #if defined(__FreeBSD__) || defined(__OpenBSD__)
67 /* comment from cmucl-2.4.8: This can waste a lot of swap on FreeBSD
68  * so don't unmap there.
69  *
70  * The CMU CL comment didn't specify a version, but was probably an
71  * old version of FreeBSD (pre-4.0), so this might no longer be true.
72  * OTOH, if it is true, this behavior might exist on OpenBSD too, so
73  * for now we don't unmap there either. -- WHN 2001-04-07 */
74 boolean gencgc_unmap_zero = 0;
75 #else
76 boolean gencgc_unmap_zero = 1;
77 #endif
78
79 /* the minimum size (in bytes) for a large object*/
80 unsigned large_object_size = 4 * 4096;
81 \f
82 /*
83  * debugging
84  */
85
86
87
88 /* the verbosity level. All non-error messages are disabled at level 0;
89  * and only a few rare messages are printed at level 1. */
90 unsigned gencgc_verbose = (QSHOW ? 1 : 0);
91
92 /* FIXME: At some point enable the various error-checking things below
93  * and see what they say. */
94
95 /* We hunt for pointers to old-space, when GCing generations >= verify_gen.
96  * Set verify_gens to NUM_GENERATIONS to disable this kind of check. */
97 int verify_gens = NUM_GENERATIONS;
98
99 /* Should we do a pre-scan verify of generation 0 before it's GCed? */
100 boolean pre_verify_gen_0 = 0;
101
102 /* Should we check for bad pointers after gc_free_heap is called
103  * from Lisp PURIFY? */
104 boolean verify_after_free_heap = 0;
105
106 /* Should we print a note when code objects are found in the dynamic space
107  * during a heap verify? */
108 boolean verify_dynamic_code_check = 0;
109
110 /* Should we check code objects for fixup errors after they are transported? */
111 boolean check_code_fixups = 0;
112
113 /* Should we check that newly allocated regions are zero filled? */
114 boolean gencgc_zero_check = 0;
115
116 /* Should we check that the free space is zero filled? */
117 boolean gencgc_enable_verify_zero_fill = 0;
118
119 /* Should we check that free pages are zero filled during gc_free_heap
120  * called after Lisp PURIFY? */
121 boolean gencgc_zero_check_during_free_heap = 0;
122 \f
123 /*
124  * GC structures and variables
125  */
126
127 /* the total bytes allocated. These are seen by Lisp DYNAMIC-USAGE. */
128 unsigned long bytes_allocated = 0;
129 static unsigned long auto_gc_trigger = 0;
130
131 /* the source and destination generations. These are set before a GC starts
132  * scavenging. */
133 int from_space;
134 int new_space;
135
136
137 /* FIXME: It would be nice to use this symbolic constant instead of
138  * bare 4096 almost everywhere. We could also use an assertion that
139  * it's equal to getpagesize(). */
140
141 #define PAGE_BYTES 4096
142
143 /* An array of page structures is statically allocated.
144  * This helps quickly map between an address its page structure.
145  * NUM_PAGES is set from the size of the dynamic space. */
146 struct page page_table[NUM_PAGES];
147
148 /* To map addresses to page structures the address of the first page
149  * is needed. */
150 static void *heap_base = NULL;
151
152
153 /* Calculate the start address for the given page number. */
154 inline void *
155 page_address(int page_num)
156 {
157     return (heap_base + (page_num * 4096));
158 }
159
160 /* Find the page index within the page_table for the given
161  * address. Return -1 on failure. */
162 inline int
163 find_page_index(void *addr)
164 {
165     int index = addr-heap_base;
166
167     if (index >= 0) {
168         index = ((unsigned int)index)/4096;
169         if (index < NUM_PAGES)
170             return (index);
171     }
172
173     return (-1);
174 }
175
176 /* a structure to hold the state of a generation */
177 struct generation {
178
179     /* the first page that gc_alloc() checks on its next call */
180     int alloc_start_page;
181
182     /* the first page that gc_alloc_unboxed() checks on its next call */
183     int alloc_unboxed_start_page;
184
185     /* the first page that gc_alloc_large (boxed) considers on its next
186      * call. (Although it always allocates after the boxed_region.) */
187     int alloc_large_start_page;
188
189     /* the first page that gc_alloc_large (unboxed) considers on its
190      * next call. (Although it always allocates after the
191      * current_unboxed_region.) */
192     int alloc_large_unboxed_start_page;
193
194     /* the bytes allocated to this generation */
195     int bytes_allocated;
196
197     /* the number of bytes at which to trigger a GC */
198     int gc_trigger;
199
200     /* to calculate a new level for gc_trigger */
201     int bytes_consed_between_gc;
202
203     /* the number of GCs since the last raise */
204     int num_gc;
205
206     /* the average age after which a GC will raise objects to the
207      * next generation */
208     int trigger_age;
209
210     /* the cumulative sum of the bytes allocated to this generation. It is
211      * cleared after a GC on this generations, and update before new
212      * objects are added from a GC of a younger generation. Dividing by
213      * the bytes_allocated will give the average age of the memory in
214      * this generation since its last GC. */
215     int cum_sum_bytes_allocated;
216
217     /* a minimum average memory age before a GC will occur helps
218      * prevent a GC when a large number of new live objects have been
219      * added, in which case a GC could be a waste of time */
220     double min_av_mem_age;
221 };
222 /* the number of actual generations. (The number of 'struct
223  * generation' objects is one more than this, because one object
224  * serves as scratch when GC'ing.) */
225 #define NUM_GENERATIONS 6
226
227 /* an array of generation structures. There needs to be one more
228  * generation structure than actual generations as the oldest
229  * generation is temporarily raised then lowered. */
230 struct generation generations[NUM_GENERATIONS+1];
231
232 /* the oldest generation that is will currently be GCed by default.
233  * Valid values are: 0, 1, ... (NUM_GENERATIONS-1)
234  *
235  * The default of (NUM_GENERATIONS-1) enables GC on all generations.
236  *
237  * Setting this to 0 effectively disables the generational nature of
238  * the GC. In some applications generational GC may not be useful
239  * because there are no long-lived objects.
240  *
241  * An intermediate value could be handy after moving long-lived data
242  * into an older generation so an unnecessary GC of this long-lived
243  * data can be avoided. */
244 unsigned int  gencgc_oldest_gen_to_gc = NUM_GENERATIONS-1;
245
246 /* The maximum free page in the heap is maintained and used to update
247  * ALLOCATION_POINTER which is used by the room function to limit its
248  * search of the heap. XX Gencgc obviously needs to be better
249  * integrated with the Lisp code. */
250 static int  last_free_page;
251 \f
252 /*
253  * miscellaneous heap functions
254  */
255
256 /* Count the number of pages which are write-protected within the
257  * given generation. */
258 static int
259 count_write_protect_generation_pages(int generation)
260 {
261     int i;
262     int count = 0;
263
264     for (i = 0; i < last_free_page; i++)
265         if ((page_table[i].allocated != FREE_PAGE)
266             && (page_table[i].gen == generation)
267             && (page_table[i].write_protected == 1))
268             count++;
269     return count;
270 }
271
272 /* Count the number of pages within the given generation. */
273 static int
274 count_generation_pages(int generation)
275 {
276     int i;
277     int count = 0;
278
279     for (i = 0; i < last_free_page; i++)
280         if ((page_table[i].allocated != 0)
281             && (page_table[i].gen == generation))
282             count++;
283     return count;
284 }
285
286 /* Count the number of dont_move pages. */
287 static int
288 count_dont_move_pages(void)
289 {
290     int i;
291     int count = 0;
292     for (i = 0; i < last_free_page; i++) {
293         if ((page_table[i].allocated != 0) && (page_table[i].dont_move != 0)) {
294             ++count;
295         }
296     }
297     return count;
298 }
299
300 /* Work through the pages and add up the number of bytes used for the
301  * given generation. */
302 static int
303 count_generation_bytes_allocated (int gen)
304 {
305     int i;
306     int result = 0;
307     for (i = 0; i < last_free_page; i++) {
308         if ((page_table[i].allocated != 0) && (page_table[i].gen == gen))
309             result += page_table[i].bytes_used;
310     }
311     return result;
312 }
313
314 /* Return the average age of the memory in a generation. */
315 static double
316 gen_av_mem_age(int gen)
317 {
318     if (generations[gen].bytes_allocated == 0)
319         return 0.0;
320
321     return
322         ((double)generations[gen].cum_sum_bytes_allocated)
323         / ((double)generations[gen].bytes_allocated);
324 }
325
326 /* The verbose argument controls how much to print: 0 for normal
327  * level of detail; 1 for debugging. */
328 static void
329 print_generation_stats(int verbose) /* FIXME: should take FILE argument */
330 {
331     int i, gens;
332     int fpu_state[27];
333
334     /* This code uses the FP instructions which may be set up for Lisp
335      * so they need to be saved and reset for C. */
336     fpu_save(fpu_state);
337
338     /* number of generations to print */
339     if (verbose)
340         gens = NUM_GENERATIONS+1;
341     else
342         gens = NUM_GENERATIONS;
343
344     /* Print the heap stats. */
345     fprintf(stderr,
346             "   Generation Boxed Unboxed LB   LUB    Alloc  Waste   Trig    WP  GCs Mem-age\n");
347
348     for (i = 0; i < gens; i++) {
349         int j;
350         int boxed_cnt = 0;
351         int unboxed_cnt = 0;
352         int large_boxed_cnt = 0;
353         int large_unboxed_cnt = 0;
354
355         for (j = 0; j < last_free_page; j++)
356             if (page_table[j].gen == i) {
357
358                 /* Count the number of boxed pages within the given
359                  * generation. */
360                 if (page_table[j].allocated & BOXED_PAGE) {
361                     if (page_table[j].large_object)
362                         large_boxed_cnt++;
363                     else
364                         boxed_cnt++;
365                 }
366
367                 /* Count the number of unboxed pages within the given
368                  * generation. */
369                 if (page_table[j].allocated & UNBOXED_PAGE) {
370                     if (page_table[j].large_object)
371                         large_unboxed_cnt++;
372                     else
373                         unboxed_cnt++;
374                 }
375             }
376
377         gc_assert(generations[i].bytes_allocated
378                   == count_generation_bytes_allocated(i));
379         fprintf(stderr,
380                 "   %8d: %5d %5d %5d %5d %8d %5d %8d %4d %3d %7.4f\n",
381                 i,
382                 boxed_cnt, unboxed_cnt, large_boxed_cnt, large_unboxed_cnt,
383                 generations[i].bytes_allocated,
384                 (count_generation_pages(i)*4096
385                  - generations[i].bytes_allocated),
386                 generations[i].gc_trigger,
387                 count_write_protect_generation_pages(i),
388                 generations[i].num_gc,
389                 gen_av_mem_age(i));
390     }
391     fprintf(stderr,"   Total bytes allocated=%ld\n", bytes_allocated);
392
393     fpu_restore(fpu_state);
394 }
395 \f
396 /*
397  * allocation routines
398  */
399
400 /*
401  * To support quick and inline allocation, regions of memory can be
402  * allocated and then allocated from with just a free pointer and a
403  * check against an end address.
404  *
405  * Since objects can be allocated to spaces with different properties
406  * e.g. boxed/unboxed, generation, ages; there may need to be many
407  * allocation regions.
408  *
409  * Each allocation region may be start within a partly used page. Many
410  * features of memory use are noted on a page wise basis, e.g. the
411  * generation; so if a region starts within an existing allocated page
412  * it must be consistent with this page.
413  *
414  * During the scavenging of the newspace, objects will be transported
415  * into an allocation region, and pointers updated to point to this
416  * allocation region. It is possible that these pointers will be
417  * scavenged again before the allocation region is closed, e.g. due to
418  * trans_list which jumps all over the place to cleanup the list. It
419  * is important to be able to determine properties of all objects
420  * pointed to when scavenging, e.g to detect pointers to the oldspace.
421  * Thus it's important that the allocation regions have the correct
422  * properties set when allocated, and not just set when closed. The
423  * region allocation routines return regions with the specified
424  * properties, and grab all the pages, setting their properties
425  * appropriately, except that the amount used is not known.
426  *
427  * These regions are used to support quicker allocation using just a
428  * free pointer. The actual space used by the region is not reflected
429  * in the pages tables until it is closed. It can't be scavenged until
430  * closed.
431  *
432  * When finished with the region it should be closed, which will
433  * update the page tables for the actual space used returning unused
434  * space. Further it may be noted in the new regions which is
435  * necessary when scavenging the newspace.
436  *
437  * Large objects may be allocated directly without an allocation
438  * region, the page tables are updated immediately.
439  *
440  * Unboxed objects don't contain pointers to other objects and so
441  * don't need scavenging. Further they can't contain pointers to
442  * younger generations so WP is not needed. By allocating pages to
443  * unboxed objects the whole page never needs scavenging or
444  * write-protecting. */
445
446 /* We are only using two regions at present. Both are for the current
447  * newspace generation. */
448 struct alloc_region boxed_region;
449 struct alloc_region unboxed_region;
450
451 /* The generation currently being allocated to. */
452 static int gc_alloc_generation;
453
454 /* Find a new region with room for at least the given number of bytes.
455  *
456  * It starts looking at the current generation's alloc_start_page. So
457  * may pick up from the previous region if there is enough space. This
458  * keeps the allocation contiguous when scavenging the newspace.
459  *
460  * The alloc_region should have been closed by a call to
461  * gc_alloc_update_page_tables(), and will thus be in an empty state.
462  *
463  * To assist the scavenging functions write-protected pages are not
464  * used. Free pages should not be write-protected.
465  *
466  * It is critical to the conservative GC that the start of regions be
467  * known. To help achieve this only small regions are allocated at a
468  * time.
469  *
470  * During scavenging, pointers may be found to within the current
471  * region and the page generation must be set so that pointers to the
472  * from space can be recognized. Therefore the generation of pages in
473  * the region are set to gc_alloc_generation. To prevent another
474  * allocation call using the same pages, all the pages in the region
475  * are allocated, although they will initially be empty.
476  */
477 static void
478 gc_alloc_new_region(int nbytes, int unboxed, struct alloc_region *alloc_region)
479 {
480     int first_page;
481     int last_page;
482     int bytes_found;
483     int i;
484
485     /*
486     FSHOW((stderr,
487            "/alloc_new_region for %d bytes from gen %d\n",
488            nbytes, gc_alloc_generation));
489     */
490
491     /* Check that the region is in a reset state. */
492     gc_assert((alloc_region->first_page == 0)
493               && (alloc_region->last_page == -1)
494               && (alloc_region->free_pointer == alloc_region->end_addr));
495
496     if (unboxed) {
497         first_page =
498             generations[gc_alloc_generation].alloc_unboxed_start_page;
499     } else {
500         first_page =
501             generations[gc_alloc_generation].alloc_start_page;
502     }
503     last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed,alloc_region);
504     bytes_found=(4096 - page_table[first_page].bytes_used)
505             + 4096*(last_page-first_page);
506
507     /* Set up the alloc_region. */
508     alloc_region->first_page = first_page;
509     alloc_region->last_page = last_page;
510     alloc_region->start_addr = page_table[first_page].bytes_used
511         + page_address(first_page);
512     alloc_region->free_pointer = alloc_region->start_addr;
513     alloc_region->end_addr = alloc_region->start_addr + bytes_found;
514
515     if (gencgc_zero_check) {
516         int *p;
517         for (p = (int *)alloc_region->start_addr;
518             p < (int *)alloc_region->end_addr; p++) {
519             if (*p != 0) {
520                 /* KLUDGE: It would be nice to use %lx and explicit casts
521                  * (long) in code like this, so that it is less likely to
522                  * break randomly when running on a machine with different
523                  * word sizes. -- WHN 19991129 */
524                 lose("The new region at %x is not zero.", p);
525             }
526         }
527     }
528
529     /* Set up the pages. */
530
531     /* The first page may have already been in use. */
532     if (page_table[first_page].bytes_used == 0) {
533         if (unboxed)
534             page_table[first_page].allocated = UNBOXED_PAGE;
535         else
536             page_table[first_page].allocated = BOXED_PAGE;
537         page_table[first_page].gen = gc_alloc_generation;
538         page_table[first_page].large_object = 0;
539         page_table[first_page].first_object_offset = 0;
540     }
541
542     if (unboxed)
543         gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
544     else
545         gc_assert(page_table[first_page].allocated == BOXED_PAGE);
546     page_table[first_page].allocated |= OPEN_REGION_PAGE; 
547
548     gc_assert(page_table[first_page].gen == gc_alloc_generation);
549     gc_assert(page_table[first_page].large_object == 0);
550
551     for (i = first_page+1; i <= last_page; i++) {
552         if (unboxed)
553             page_table[i].allocated = UNBOXED_PAGE;
554         else
555             page_table[i].allocated = BOXED_PAGE;
556         page_table[i].gen = gc_alloc_generation;
557         page_table[i].large_object = 0;
558         /* This may not be necessary for unboxed regions (think it was
559          * broken before!) */
560         page_table[i].first_object_offset =
561             alloc_region->start_addr - page_address(i);
562         page_table[i].allocated |= OPEN_REGION_PAGE ;
563     }
564
565     /* Bump up last_free_page. */
566     if (last_page+1 > last_free_page) {
567         last_free_page = last_page+1;
568         SetSymbolValue(ALLOCATION_POINTER,
569                        (lispobj)(((char *)heap_base) + last_free_page*4096));
570     }
571 }
572
573 /* If the record_new_objects flag is 2 then all new regions created
574  * are recorded.
575  *
576  * If it's 1 then then it is only recorded if the first page of the
577  * current region is <= new_areas_ignore_page. This helps avoid
578  * unnecessary recording when doing full scavenge pass.
579  *
580  * The new_object structure holds the page, byte offset, and size of
581  * new regions of objects. Each new area is placed in the array of
582  * these structures pointer to by new_areas. new_areas_index holds the
583  * offset into new_areas.
584  *
585  * If new_area overflows NUM_NEW_AREAS then it stops adding them. The
586  * later code must detect this and handle it, probably by doing a full
587  * scavenge of a generation. */
588 #define NUM_NEW_AREAS 512
589 static int record_new_objects = 0;
590 static int new_areas_ignore_page;
591 struct new_area {
592     int  page;
593     int  offset;
594     int  size;
595 };
596 static struct new_area (*new_areas)[];
597 static int new_areas_index;
598 int max_new_areas;
599
600 /* Add a new area to new_areas. */
601 static void
602 add_new_area(int first_page, int offset, int size)
603 {
604     unsigned new_area_start,c;
605     int i;
606
607     /* Ignore if full. */
608     if (new_areas_index >= NUM_NEW_AREAS)
609         return;
610
611     switch (record_new_objects) {
612     case 0:
613         return;
614     case 1:
615         if (first_page > new_areas_ignore_page)
616             return;
617         break;
618     case 2:
619         break;
620     default:
621         gc_abort();
622     }
623
624     new_area_start = 4096*first_page + offset;
625
626     /* Search backwards for a prior area that this follows from. If
627        found this will save adding a new area. */
628     for (i = new_areas_index-1, c = 0; (i >= 0) && (c < 8); i--, c++) {
629         unsigned area_end =
630             4096*((*new_areas)[i].page)
631             + (*new_areas)[i].offset
632             + (*new_areas)[i].size;
633         /*FSHOW((stderr,
634                "/add_new_area S1 %d %d %d %d\n",
635                i, c, new_area_start, area_end));*/
636         if (new_area_start == area_end) {
637             /*FSHOW((stderr,
638                    "/adding to [%d] %d %d %d with %d %d %d:\n",
639                    i,
640                    (*new_areas)[i].page,
641                    (*new_areas)[i].offset,
642                    (*new_areas)[i].size,
643                    first_page,
644                    offset,
645                     size);*/
646             (*new_areas)[i].size += size;
647             return;
648         }
649     }
650
651     (*new_areas)[new_areas_index].page = first_page;
652     (*new_areas)[new_areas_index].offset = offset;
653     (*new_areas)[new_areas_index].size = size;
654     /*FSHOW((stderr,
655            "/new_area %d page %d offset %d size %d\n",
656            new_areas_index, first_page, offset, size));*/
657     new_areas_index++;
658
659     /* Note the max new_areas used. */
660     if (new_areas_index > max_new_areas)
661         max_new_areas = new_areas_index;
662 }
663
664 /* Update the tables for the alloc_region. The region maybe added to
665  * the new_areas.
666  *
667  * When done the alloc_region is set up so that the next quick alloc
668  * will fail safely and thus a new region will be allocated. Further
669  * it is safe to try to re-update the page table of this reset
670  * alloc_region. */
671 void
672 gc_alloc_update_page_tables(int unboxed, struct alloc_region *alloc_region)
673 {
674     int more;
675     int first_page;
676     int next_page;
677     int bytes_used;
678     int orig_first_page_bytes_used;
679     int region_size;
680     int byte_cnt;
681
682     /*
683     FSHOW((stderr,
684            "/gc_alloc_update_page_tables() to gen %d:\n",
685            gc_alloc_generation));
686     */
687
688     first_page = alloc_region->first_page;
689
690     /* Catch an unused alloc_region. */
691     if ((first_page == 0) && (alloc_region->last_page == -1))
692         return;
693
694     next_page = first_page+1;
695
696     /* Skip if no bytes were allocated. */
697     if (alloc_region->free_pointer != alloc_region->start_addr) {
698         orig_first_page_bytes_used = page_table[first_page].bytes_used;
699
700         gc_assert(alloc_region->start_addr == (page_address(first_page) + page_table[first_page].bytes_used));
701
702         /* All the pages used need to be updated */
703
704         /* Update the first page. */
705
706         /* If the page was free then set up the gen, and
707          * first_object_offset. */
708         if (page_table[first_page].bytes_used == 0)
709             gc_assert(page_table[first_page].first_object_offset == 0);
710         page_table[first_page].allocated &= ~(OPEN_REGION_PAGE);
711
712         if (unboxed)
713             gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
714         else
715             gc_assert(page_table[first_page].allocated == BOXED_PAGE);
716         gc_assert(page_table[first_page].gen == gc_alloc_generation);
717         gc_assert(page_table[first_page].large_object == 0);
718
719         byte_cnt = 0;
720
721         /* Calculate the number of bytes used in this page. This is not
722          * always the number of new bytes, unless it was free. */
723         more = 0;
724         if ((bytes_used = (alloc_region->free_pointer - page_address(first_page)))>4096) {
725             bytes_used = 4096;
726             more = 1;
727         }
728         page_table[first_page].bytes_used = bytes_used;
729         byte_cnt += bytes_used;
730
731
732         /* All the rest of the pages should be free. We need to set their
733          * first_object_offset pointer to the start of the region, and set
734          * the bytes_used. */
735         while (more) {
736             page_table[next_page].allocated &= ~(OPEN_REGION_PAGE);
737             if (unboxed)
738                 gc_assert(page_table[next_page].allocated == UNBOXED_PAGE);
739             else
740                 gc_assert(page_table[next_page].allocated == BOXED_PAGE);
741             gc_assert(page_table[next_page].bytes_used == 0);
742             gc_assert(page_table[next_page].gen == gc_alloc_generation);
743             gc_assert(page_table[next_page].large_object == 0);
744
745             gc_assert(page_table[next_page].first_object_offset ==
746                       alloc_region->start_addr - page_address(next_page));
747
748             /* Calculate the number of bytes used in this page. */
749             more = 0;
750             if ((bytes_used = (alloc_region->free_pointer
751                                - page_address(next_page)))>4096) {
752                 bytes_used = 4096;
753                 more = 1;
754             }
755             page_table[next_page].bytes_used = bytes_used;
756             byte_cnt += bytes_used;
757
758             next_page++;
759         }
760
761         region_size = alloc_region->free_pointer - alloc_region->start_addr;
762         bytes_allocated += region_size;
763         generations[gc_alloc_generation].bytes_allocated += region_size;
764
765         gc_assert((byte_cnt- orig_first_page_bytes_used) == region_size);
766
767         /* Set the generations alloc restart page to the last page of
768          * the region. */
769         if (unboxed)
770             generations[gc_alloc_generation].alloc_unboxed_start_page =
771                 next_page-1;
772         else
773             generations[gc_alloc_generation].alloc_start_page = next_page-1;
774
775         /* Add the region to the new_areas if requested. */
776         if (!unboxed)
777             add_new_area(first_page,orig_first_page_bytes_used, region_size);
778
779         /*
780         FSHOW((stderr,
781                "/gc_alloc_update_page_tables update %d bytes to gen %d\n",
782                region_size,
783                gc_alloc_generation));
784         */
785     } else {
786         /* There are no bytes allocated. Unallocate the first_page if
787          * there are 0 bytes_used. */
788         page_table[first_page].allocated &= ~(OPEN_REGION_PAGE);
789         if (page_table[first_page].bytes_used == 0)
790             page_table[first_page].allocated = FREE_PAGE;
791     }
792
793     /* Unallocate any unused pages. */
794     while (next_page <= alloc_region->last_page) {
795         gc_assert(page_table[next_page].bytes_used == 0);
796         page_table[next_page].allocated = FREE_PAGE;
797         next_page++;
798     }
799
800     gc_set_region_empty(alloc_region);
801 }
802
803 static inline void *gc_quick_alloc(int nbytes);
804
805 /* Allocate a possibly large object. */
806 void *
807 gc_alloc_large(int nbytes, int unboxed, struct alloc_region *alloc_region)
808 {
809     int first_page;
810     int last_page;
811     int orig_first_page_bytes_used;
812     int byte_cnt;
813     int more;
814     int bytes_used;
815     int next_page;
816     int large = (nbytes >= large_object_size);
817
818     /*
819     if (nbytes > 200000)
820         FSHOW((stderr, "/alloc_large %d\n", nbytes));
821     */
822
823     /*
824     FSHOW((stderr,
825            "/gc_alloc_large() for %d bytes from gen %d\n",
826            nbytes, gc_alloc_generation));
827     */
828
829     /* If the object is small, and there is room in the current region
830        then allocate it in the current region. */
831     if (!large
832         && ((alloc_region->end_addr-alloc_region->free_pointer) >= nbytes))
833         return gc_quick_alloc(nbytes);
834
835     /* To allow the allocation of small objects without the danger of
836        using a page in the current boxed region, the search starts after
837        the current boxed free region. XX could probably keep a page
838        index ahead of the current region and bumped up here to save a
839        lot of re-scanning. */
840
841     if (unboxed) {
842         first_page =
843             generations[gc_alloc_generation].alloc_large_unboxed_start_page;
844     } else {
845         first_page = generations[gc_alloc_generation].alloc_large_start_page;
846     }
847     if (first_page <= alloc_region->last_page) {
848         first_page = alloc_region->last_page+1;
849     }
850
851     last_page=gc_find_freeish_pages(&first_page,nbytes,unboxed,0);
852
853     gc_assert(first_page > alloc_region->last_page);
854     if (unboxed)
855         generations[gc_alloc_generation].alloc_large_unboxed_start_page =
856             last_page;
857     else
858         generations[gc_alloc_generation].alloc_large_start_page = last_page;
859
860     /* Set up the pages. */
861     orig_first_page_bytes_used = page_table[first_page].bytes_used;
862
863     /* If the first page was free then set up the gen, and
864      * first_object_offset. */
865     if (page_table[first_page].bytes_used == 0) {
866         if (unboxed)
867             page_table[first_page].allocated = UNBOXED_PAGE;
868         else
869             page_table[first_page].allocated = BOXED_PAGE;
870         page_table[first_page].gen = gc_alloc_generation;
871         page_table[first_page].first_object_offset = 0;
872         page_table[first_page].large_object = large;
873     }
874
875     if (unboxed)
876         gc_assert(page_table[first_page].allocated == UNBOXED_PAGE);
877     else
878         gc_assert(page_table[first_page].allocated == BOXED_PAGE);
879     gc_assert(page_table[first_page].gen == gc_alloc_generation);
880     gc_assert(page_table[first_page].large_object == large);
881
882     byte_cnt = 0;
883
884     /* Calc. the number of bytes used in this page. This is not
885      * always the number of new bytes, unless it was free. */
886     more = 0;
887     if ((bytes_used = nbytes+orig_first_page_bytes_used) > 4096) {
888         bytes_used = 4096;
889         more = 1;
890     }
891     page_table[first_page].bytes_used = bytes_used;
892     byte_cnt += bytes_used;
893
894     next_page = first_page+1;
895
896     /* All the rest of the pages should be free. We need to set their
897      * first_object_offset pointer to the start of the region, and
898      * set the bytes_used. */
899     while (more) {
900         gc_assert(page_table[next_page].allocated == FREE_PAGE);
901         gc_assert(page_table[next_page].bytes_used == 0);
902         if (unboxed)
903             page_table[next_page].allocated = UNBOXED_PAGE;
904         else
905             page_table[next_page].allocated = BOXED_PAGE;
906         page_table[next_page].gen = gc_alloc_generation;
907         page_table[next_page].large_object = large;
908
909         page_table[next_page].first_object_offset =
910             orig_first_page_bytes_used - 4096*(next_page-first_page);
911
912         /* Calculate the number of bytes used in this page. */
913         more = 0;
914         if ((bytes_used=(nbytes+orig_first_page_bytes_used)-byte_cnt) > 4096) {
915             bytes_used = 4096;
916             more = 1;
917         }
918         page_table[next_page].bytes_used = bytes_used;
919         byte_cnt += bytes_used;
920
921         next_page++;
922     }
923
924     gc_assert((byte_cnt-orig_first_page_bytes_used) == nbytes);
925
926     bytes_allocated += nbytes;
927     generations[gc_alloc_generation].bytes_allocated += nbytes;
928
929     /* Add the region to the new_areas if requested. */
930     if (!unboxed)
931         add_new_area(first_page,orig_first_page_bytes_used,nbytes);
932
933     /* Bump up last_free_page */
934     if (last_page+1 > last_free_page) {
935         last_free_page = last_page+1;
936         SetSymbolValue(ALLOCATION_POINTER,
937                        (lispobj)(((char *)heap_base) + last_free_page*4096));
938     }
939
940     return((void *)(page_address(first_page)+orig_first_page_bytes_used));
941 }
942
943 int
944 gc_find_freeish_pages(int *restart_page_ptr, int nbytes, int unboxed, struct alloc_region *alloc_region)
945 {
946     /* if alloc_region is 0, we assume this is for a potentially large
947        object */
948     int first_page;
949     int last_page;
950     int region_size;
951     int restart_page=*restart_page_ptr;
952     int bytes_found;
953     int num_pages;
954     int large = !alloc_region && (nbytes >= large_object_size);
955
956     /* Search for a contiguous free space of at least nbytes. If it's a
957        large object then align it on a page boundary by searching for a
958        free page. */
959
960     /* To allow the allocation of small objects without the danger of
961        using a page in the current boxed region, the search starts after
962        the current boxed free region. XX could probably keep a page
963        index ahead of the current region and bumped up here to save a
964        lot of re-scanning. */
965
966     do {
967         first_page = restart_page;
968         if (large)              
969             while ((first_page < NUM_PAGES)
970                    && (page_table[first_page].allocated != FREE_PAGE))
971                 first_page++;
972         else
973             while (first_page < NUM_PAGES) {
974                 if(page_table[first_page].allocated == FREE_PAGE)
975                     break;
976                 /* I don't know why we need the gen=0 test, but it
977                  * breaks randomly if that's omitted -dan 2003.02.26
978                  */
979                 if((page_table[first_page].allocated ==
980                     (unboxed ? UNBOXED_PAGE : BOXED_PAGE)) &&
981                    (page_table[first_page].large_object == 0) &&
982                    (gc_alloc_genration == 0) &&
983                    (page_table[first_page].gen == gc_alloc_generation) &&
984                    (page_table[first_page].bytes_used < (4096-32)) &&
985                    (page_table[first_page].write_protected == 0) &&
986                    (page_table[first_page].dont_move == 0))
987                     break;
988                 first_page++;
989             }
990         
991         if (first_page >= NUM_PAGES) {
992             fprintf(stderr,
993                     "Argh! gc_find_free_space failed (first_page), nbytes=%d.\n",
994                     nbytes);
995             print_generation_stats(1);
996             lose(NULL);
997         }
998
999         gc_assert(page_table[first_page].write_protected == 0);
1000
1001         last_page = first_page;
1002         bytes_found = 4096 - page_table[first_page].bytes_used;
1003         num_pages = 1;
1004         while (((bytes_found < nbytes) 
1005                 || (alloc_region && (num_pages < 2)))
1006                && (last_page < (NUM_PAGES-1))
1007                && (page_table[last_page+1].allocated == FREE_PAGE)) {
1008             last_page++;
1009             num_pages++;
1010             bytes_found += 4096;
1011             gc_assert(page_table[last_page].write_protected == 0);
1012         }
1013
1014         region_size = (4096 - page_table[first_page].bytes_used)
1015             + 4096*(last_page-first_page);
1016
1017         gc_assert(bytes_found == region_size);
1018         restart_page = last_page + 1;
1019     } while ((restart_page < NUM_PAGES) && (bytes_found < nbytes));
1020
1021     /* Check for a failure */
1022     if ((restart_page >= NUM_PAGES) && (bytes_found < nbytes)) {
1023         fprintf(stderr,
1024                 "Argh! gc_find_freeish_pages failed (restart_page), nbytes=%d.\n",
1025                 nbytes);
1026         print_generation_stats(1);
1027         lose(NULL);
1028     }
1029     *restart_page_ptr=first_page;
1030     return last_page;
1031 }
1032
1033 /* Allocate bytes.  All the rest of the special-purpose allocation
1034  * functions will eventually call this (instead of just duplicating
1035  * parts of its code) */
1036
1037 void *
1038 gc_alloc_with_region(int nbytes,int unboxed_p, struct alloc_region *my_region,
1039                      int quick_p)
1040 {
1041     void *new_free_pointer;
1042
1043     /* FSHOW((stderr, "/gc_alloc %d\n", nbytes)); */
1044
1045     /* Check whether there is room in the current alloc region. */
1046     new_free_pointer = my_region->free_pointer + nbytes;
1047
1048     if (new_free_pointer <= my_region->end_addr) {
1049         /* If so then allocate from the current alloc region. */
1050         void *new_obj = my_region->free_pointer;
1051         my_region->free_pointer = new_free_pointer;
1052
1053         /* Unless a `quick' alloc was requested, check whether the
1054            alloc region is almost empty. */
1055         if (!quick_p &&
1056             (my_region->end_addr - my_region->free_pointer) <= 32) {
1057             /* If so, finished with the current region. */
1058             gc_alloc_update_page_tables(unboxed_p, my_region);
1059             /* Set up a new region. */
1060             gc_alloc_new_region(32 /*bytes*/, unboxed_p, my_region);
1061         }
1062
1063         return((void *)new_obj);
1064     }
1065
1066     /* Else not enough free space in the current region. */
1067
1068     /* If there some room left in the current region, enough to be worth
1069      * saving, then allocate a large object. */
1070     /* FIXME: "32" should be a named parameter. */
1071     if ((my_region->end_addr-my_region->free_pointer) > 32)
1072         return gc_alloc_large(nbytes, unboxed_p, my_region);
1073
1074     /* Else find a new region. */
1075
1076     /* Finished with the current region. */
1077     gc_alloc_update_page_tables(unboxed_p, my_region);
1078
1079     /* Set up a new region. */
1080     gc_alloc_new_region(nbytes, unboxed_p, my_region);
1081
1082     /* Should now be enough room. */
1083
1084     /* Check whether there is room in the current region. */
1085     new_free_pointer = my_region->free_pointer + nbytes;
1086
1087     if (new_free_pointer <= my_region->end_addr) {
1088         /* If so then allocate from the current region. */
1089         void *new_obj = my_region->free_pointer;
1090         my_region->free_pointer = new_free_pointer;
1091         /* Check whether the current region is almost empty. */
1092         if ((my_region->end_addr - my_region->free_pointer) <= 32) {
1093             /* If so find, finished with the current region. */
1094             gc_alloc_update_page_tables(unboxed_p, my_region);
1095
1096             /* Set up a new region. */
1097             gc_alloc_new_region(32, unboxed_p, my_region);
1098         }
1099
1100         return((void *)new_obj);
1101     }
1102
1103     /* shouldn't happen */
1104     gc_assert(0);
1105     return((void *) NIL); /* dummy value: return something ... */
1106 }
1107
1108 void *
1109 gc_general_alloc(int nbytes,int unboxed_p,int quick_p)
1110 {
1111     struct alloc_region *my_region = 
1112       unboxed_p ? &unboxed_region : &boxed_region;
1113     return gc_alloc_with_region(nbytes,unboxed_p, my_region,quick_p);
1114 }
1115
1116
1117
1118 static void *
1119 gc_alloc(int nbytes,int unboxed_p)
1120 {
1121     /* this is the only function that the external interface to
1122      * allocation presently knows how to call: Lisp code will never
1123      * allocate large objects, or to unboxed space, or `quick'ly.
1124      * Any of that stuff will only ever happen inside of GC */
1125     return gc_general_alloc(nbytes,unboxed_p,0);
1126 }
1127
1128 /* Allocate space from the boxed_region. If there is not enough free
1129  * space then call gc_alloc to do the job. A pointer to the start of
1130  * the object is returned. */
1131 static inline void *
1132 gc_quick_alloc(int nbytes)
1133 {
1134     return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1135 }
1136
1137 /* Allocate space for the possibly large boxed object. If it is a
1138  * large object then do a large alloc else use gc_quick_alloc.  Note
1139  * that gc_quick_alloc will eventually fall through to
1140  * gc_general_alloc which may allocate the object in a large way
1141  * anyway, but based on decisions about the free space in the current
1142  * region, not the object size itself */
1143
1144 static inline void *
1145 gc_quick_alloc_large(int nbytes)
1146 {
1147     if (nbytes >= large_object_size)
1148         return gc_alloc_large(nbytes, ALLOC_BOXED, &boxed_region);
1149     else
1150         return gc_general_alloc(nbytes,ALLOC_BOXED,ALLOC_QUICK);
1151 }
1152
1153 static inline void *
1154 gc_alloc_unboxed(int nbytes)
1155 {
1156     return gc_general_alloc(nbytes,ALLOC_UNBOXED,0);
1157 }
1158
1159 static inline void *
1160 gc_quick_alloc_unboxed(int nbytes)
1161 {
1162     return gc_general_alloc(nbytes,ALLOC_UNBOXED,ALLOC_QUICK);
1163 }
1164
1165 /* Allocate space for the object. If it is a large object then do a
1166  * large alloc else allocate from the current region. If there is not
1167  * enough free space then call general gc_alloc_unboxed() to do the job.
1168  *
1169  * A pointer to the start of the object is returned. */
1170 static inline void *
1171 gc_quick_alloc_large_unboxed(int nbytes)
1172 {
1173     if (nbytes >= large_object_size)
1174         return gc_alloc_large(nbytes,ALLOC_UNBOXED,&unboxed_region);
1175     else
1176         return gc_quick_alloc_unboxed(nbytes);
1177 }
1178 \f
1179 /*
1180  * scavenging/transporting routines derived from gc.c in CMU CL ca. 18b
1181  */
1182
1183 extern int (*scavtab[256])(lispobj *where, lispobj object);
1184 extern lispobj (*transother[256])(lispobj object);
1185 extern int (*sizetab[256])(lispobj *where);
1186
1187 /* Copy a large boxed object. If the object is in a large object
1188  * region then it is simply promoted, else it is copied. If it's large
1189  * enough then it's copied to a large object region.
1190  *
1191  * Vectors may have shrunk. If the object is not copied the space
1192  * needs to be reclaimed, and the page_tables corrected. */
1193 lispobj
1194 copy_large_object(lispobj object, int nwords)
1195 {
1196     int tag;
1197     lispobj *new;
1198     lispobj *source, *dest;
1199     int first_page;
1200
1201     gc_assert(is_lisp_pointer(object));
1202     gc_assert(from_space_p(object));
1203     gc_assert((nwords & 0x01) == 0);
1204
1205
1206     /* Check whether it's a large object. */
1207     first_page = find_page_index((void *)object);
1208     gc_assert(first_page >= 0);
1209
1210     if (page_table[first_page].large_object) {
1211
1212         /* Promote the object. */
1213
1214         int remaining_bytes;
1215         int next_page;
1216         int bytes_freed;
1217         int old_bytes_used;
1218
1219         /* Note: Any page write-protection must be removed, else a
1220          * later scavenge_newspace may incorrectly not scavenge these
1221          * pages. This would not be necessary if they are added to the
1222          * new areas, but let's do it for them all (they'll probably
1223          * be written anyway?). */
1224
1225         gc_assert(page_table[first_page].first_object_offset == 0);
1226
1227         next_page = first_page;
1228         remaining_bytes = nwords*4;
1229         while (remaining_bytes > 4096) {
1230             gc_assert(page_table[next_page].gen == from_space);
1231             gc_assert(page_table[next_page].allocated == BOXED_PAGE);
1232             gc_assert(page_table[next_page].large_object);
1233             gc_assert(page_table[next_page].first_object_offset==
1234                       -4096*(next_page-first_page));
1235             gc_assert(page_table[next_page].bytes_used == 4096);
1236
1237             page_table[next_page].gen = new_space;
1238
1239             /* Remove any write-protection. We should be able to rely
1240              * on the write-protect flag to avoid redundant calls. */
1241             if (page_table[next_page].write_protected) {
1242                 os_protect(page_address(next_page), 4096, OS_VM_PROT_ALL);
1243                 page_table[next_page].write_protected = 0;
1244             }
1245             remaining_bytes -= 4096;
1246             next_page++;
1247         }
1248
1249         /* Now only one page remains, but the object may have shrunk
1250          * so there may be more unused pages which will be freed. */
1251
1252         /* The object may have shrunk but shouldn't have grown. */
1253         gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1254
1255         page_table[next_page].gen = new_space;
1256         gc_assert(page_table[next_page].allocated == BOXED_PAGE);
1257
1258         /* Adjust the bytes_used. */
1259         old_bytes_used = page_table[next_page].bytes_used;
1260         page_table[next_page].bytes_used = remaining_bytes;
1261
1262         bytes_freed = old_bytes_used - remaining_bytes;
1263
1264         /* Free any remaining pages; needs care. */
1265         next_page++;
1266         while ((old_bytes_used == 4096) &&
1267                (page_table[next_page].gen == from_space) &&
1268                (page_table[next_page].allocated == BOXED_PAGE) &&
1269                page_table[next_page].large_object &&
1270                (page_table[next_page].first_object_offset ==
1271                 -(next_page - first_page)*4096)) {
1272             /* Checks out OK, free the page. Don't need to bother zeroing
1273              * pages as this should have been done before shrinking the
1274              * object. These pages shouldn't be write-protected as they
1275              * should be zero filled. */
1276             gc_assert(page_table[next_page].write_protected == 0);
1277
1278             old_bytes_used = page_table[next_page].bytes_used;
1279             page_table[next_page].allocated = FREE_PAGE;
1280             page_table[next_page].bytes_used = 0;
1281             bytes_freed += old_bytes_used;
1282             next_page++;
1283         }
1284
1285         generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1286         generations[new_space].bytes_allocated += 4*nwords;
1287         bytes_allocated -= bytes_freed;
1288
1289         /* Add the region to the new_areas if requested. */
1290         add_new_area(first_page,0,nwords*4);
1291
1292         return(object);
1293     } else {
1294         /* Get tag of object. */
1295         tag = lowtag_of(object);
1296
1297         /* Allocate space. */
1298         new = gc_quick_alloc_large(nwords*4);
1299
1300         dest = new;
1301         source = (lispobj *) native_pointer(object);
1302
1303         /* Copy the object. */
1304         while (nwords > 0) {
1305             dest[0] = source[0];
1306             dest[1] = source[1];
1307             dest += 2;
1308             source += 2;
1309             nwords -= 2;
1310         }
1311
1312         /* Return Lisp pointer of new object. */
1313         return ((lispobj) new) | tag;
1314     }
1315 }
1316
1317 /* to copy unboxed objects */
1318 lispobj
1319 copy_unboxed_object(lispobj object, int nwords)
1320 {
1321     int tag;
1322     lispobj *new;
1323     lispobj *source, *dest;
1324
1325     gc_assert(is_lisp_pointer(object));
1326     gc_assert(from_space_p(object));
1327     gc_assert((nwords & 0x01) == 0);
1328
1329     /* Get tag of object. */
1330     tag = lowtag_of(object);
1331
1332     /* Allocate space. */
1333     new = gc_quick_alloc_unboxed(nwords*4);
1334
1335     dest = new;
1336     source = (lispobj *) native_pointer(object);
1337
1338     /* Copy the object. */
1339     while (nwords > 0) {
1340         dest[0] = source[0];
1341         dest[1] = source[1];
1342         dest += 2;
1343         source += 2;
1344         nwords -= 2;
1345     }
1346
1347     /* Return Lisp pointer of new object. */
1348     return ((lispobj) new) | tag;
1349 }
1350
1351 /* to copy large unboxed objects
1352  *
1353  * If the object is in a large object region then it is simply
1354  * promoted, else it is copied. If it's large enough then it's copied
1355  * to a large object region.
1356  *
1357  * Bignums and vectors may have shrunk. If the object is not copied
1358  * the space needs to be reclaimed, and the page_tables corrected.
1359  *
1360  * KLUDGE: There's a lot of cut-and-paste duplication between this
1361  * function and copy_large_object(..). -- WHN 20000619 */
1362 lispobj
1363 copy_large_unboxed_object(lispobj object, int nwords)
1364 {
1365     int tag;
1366     lispobj *new;
1367     lispobj *source, *dest;
1368     int first_page;
1369
1370     gc_assert(is_lisp_pointer(object));
1371     gc_assert(from_space_p(object));
1372     gc_assert((nwords & 0x01) == 0);
1373
1374     if ((nwords > 1024*1024) && gencgc_verbose)
1375         FSHOW((stderr, "/copy_large_unboxed_object: %d bytes\n", nwords*4));
1376
1377     /* Check whether it's a large object. */
1378     first_page = find_page_index((void *)object);
1379     gc_assert(first_page >= 0);
1380
1381     if (page_table[first_page].large_object) {
1382         /* Promote the object. Note: Unboxed objects may have been
1383          * allocated to a BOXED region so it may be necessary to
1384          * change the region to UNBOXED. */
1385         int remaining_bytes;
1386         int next_page;
1387         int bytes_freed;
1388         int old_bytes_used;
1389
1390         gc_assert(page_table[first_page].first_object_offset == 0);
1391
1392         next_page = first_page;
1393         remaining_bytes = nwords*4;
1394         while (remaining_bytes > 4096) {
1395             gc_assert(page_table[next_page].gen == from_space);
1396             gc_assert((page_table[next_page].allocated == UNBOXED_PAGE)
1397                       || (page_table[next_page].allocated == BOXED_PAGE));
1398             gc_assert(page_table[next_page].large_object);
1399             gc_assert(page_table[next_page].first_object_offset==
1400                       -4096*(next_page-first_page));
1401             gc_assert(page_table[next_page].bytes_used == 4096);
1402
1403             page_table[next_page].gen = new_space;
1404             page_table[next_page].allocated = UNBOXED_PAGE;
1405             remaining_bytes -= 4096;
1406             next_page++;
1407         }
1408
1409         /* Now only one page remains, but the object may have shrunk so
1410          * there may be more unused pages which will be freed. */
1411
1412         /* Object may have shrunk but shouldn't have grown - check. */
1413         gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
1414
1415         page_table[next_page].gen = new_space;
1416         page_table[next_page].allocated = UNBOXED_PAGE;
1417
1418         /* Adjust the bytes_used. */
1419         old_bytes_used = page_table[next_page].bytes_used;
1420         page_table[next_page].bytes_used = remaining_bytes;
1421
1422         bytes_freed = old_bytes_used - remaining_bytes;
1423
1424         /* Free any remaining pages; needs care. */
1425         next_page++;
1426         while ((old_bytes_used == 4096) &&
1427                (page_table[next_page].gen == from_space) &&
1428                ((page_table[next_page].allocated == UNBOXED_PAGE)
1429                 || (page_table[next_page].allocated == BOXED_PAGE)) &&
1430                page_table[next_page].large_object &&
1431                (page_table[next_page].first_object_offset ==
1432                 -(next_page - first_page)*4096)) {
1433             /* Checks out OK, free the page. Don't need to both zeroing
1434              * pages as this should have been done before shrinking the
1435              * object. These pages shouldn't be write-protected, even if
1436              * boxed they should be zero filled. */
1437             gc_assert(page_table[next_page].write_protected == 0);
1438
1439             old_bytes_used = page_table[next_page].bytes_used;
1440             page_table[next_page].allocated = FREE_PAGE;
1441             page_table[next_page].bytes_used = 0;
1442             bytes_freed += old_bytes_used;
1443             next_page++;
1444         }
1445
1446         if ((bytes_freed > 0) && gencgc_verbose)
1447             FSHOW((stderr,
1448                    "/copy_large_unboxed bytes_freed=%d\n",
1449                    bytes_freed));
1450
1451         generations[from_space].bytes_allocated -= 4*nwords + bytes_freed;
1452         generations[new_space].bytes_allocated += 4*nwords;
1453         bytes_allocated -= bytes_freed;
1454
1455         return(object);
1456     }
1457     else {
1458         /* Get tag of object. */
1459         tag = lowtag_of(object);
1460
1461         /* Allocate space. */
1462         new = gc_quick_alloc_large_unboxed(nwords*4);
1463
1464         dest = new;
1465         source = (lispobj *) native_pointer(object);
1466
1467         /* Copy the object. */
1468         while (nwords > 0) {
1469             dest[0] = source[0];
1470             dest[1] = source[1];
1471             dest += 2;
1472             source += 2;
1473             nwords -= 2;
1474         }
1475
1476         /* Return Lisp pointer of new object. */
1477         return ((lispobj) new) | tag;
1478     }
1479 }
1480
1481
1482
1483 \f
1484
1485 /*
1486  * code and code-related objects
1487  */
1488 /*
1489 static lispobj trans_fun_header(lispobj object);
1490 static lispobj trans_boxed(lispobj object);
1491 */
1492
1493 /* Scan a x86 compiled code object, looking for possible fixups that
1494  * have been missed after a move.
1495  *
1496  * Two types of fixups are needed:
1497  * 1. Absolute fixups to within the code object.
1498  * 2. Relative fixups to outside the code object.
1499  *
1500  * Currently only absolute fixups to the constant vector, or to the
1501  * code area are checked. */
1502 void
1503 sniff_code_object(struct code *code, unsigned displacement)
1504 {
1505     int nheader_words, ncode_words, nwords;
1506     void *p;
1507     void *constants_start_addr, *constants_end_addr;
1508     void *code_start_addr, *code_end_addr;
1509     int fixup_found = 0;
1510
1511     if (!check_code_fixups)
1512         return;
1513
1514     ncode_words = fixnum_value(code->code_size);
1515     nheader_words = HeaderValue(*(lispobj *)code);
1516     nwords = ncode_words + nheader_words;
1517
1518     constants_start_addr = (void *)code + 5*4;
1519     constants_end_addr = (void *)code + nheader_words*4;
1520     code_start_addr = (void *)code + nheader_words*4;
1521     code_end_addr = (void *)code + nwords*4;
1522
1523     /* Work through the unboxed code. */
1524     for (p = code_start_addr; p < code_end_addr; p++) {
1525         void *data = *(void **)p;
1526         unsigned d1 = *((unsigned char *)p - 1);
1527         unsigned d2 = *((unsigned char *)p - 2);
1528         unsigned d3 = *((unsigned char *)p - 3);
1529         unsigned d4 = *((unsigned char *)p - 4);
1530 #if QSHOW
1531         unsigned d5 = *((unsigned char *)p - 5);
1532         unsigned d6 = *((unsigned char *)p - 6);
1533 #endif
1534
1535         /* Check for code references. */
1536         /* Check for a 32 bit word that looks like an absolute
1537            reference to within the code adea of the code object. */
1538         if ((data >= (code_start_addr-displacement))
1539             && (data < (code_end_addr-displacement))) {
1540             /* function header */
1541             if ((d4 == 0x5e)
1542                 && (((unsigned)p - 4 - 4*HeaderValue(*((unsigned *)p-1))) == (unsigned)code)) {
1543                 /* Skip the function header */
1544                 p += 6*4 - 4 - 1;
1545                 continue;
1546             }
1547             /* the case of PUSH imm32 */
1548             if (d1 == 0x68) {
1549                 fixup_found = 1;
1550                 FSHOW((stderr,
1551                        "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1552                        p, d6, d5, d4, d3, d2, d1, data));
1553                 FSHOW((stderr, "/PUSH $0x%.8x\n", data));
1554             }
1555             /* the case of MOV [reg-8],imm32 */
1556             if ((d3 == 0xc7)
1557                 && (d2==0x40 || d2==0x41 || d2==0x42 || d2==0x43
1558                     || d2==0x45 || d2==0x46 || d2==0x47)
1559                 && (d1 == 0xf8)) {
1560                 fixup_found = 1;
1561                 FSHOW((stderr,
1562                        "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1563                        p, d6, d5, d4, d3, d2, d1, data));
1564                 FSHOW((stderr, "/MOV [reg-8],$0x%.8x\n", data));
1565             }
1566             /* the case of LEA reg,[disp32] */
1567             if ((d2 == 0x8d) && ((d1 & 0xc7) == 5)) {
1568                 fixup_found = 1;
1569                 FSHOW((stderr,
1570                        "/code ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1571                        p, d6, d5, d4, d3, d2, d1, data));
1572                 FSHOW((stderr,"/LEA reg,[$0x%.8x]\n", data));
1573             }
1574         }
1575
1576         /* Check for constant references. */
1577         /* Check for a 32 bit word that looks like an absolute
1578            reference to within the constant vector. Constant references
1579            will be aligned. */
1580         if ((data >= (constants_start_addr-displacement))
1581             && (data < (constants_end_addr-displacement))
1582             && (((unsigned)data & 0x3) == 0)) {
1583             /*  Mov eax,m32 */
1584             if (d1 == 0xa1) {
1585                 fixup_found = 1;
1586                 FSHOW((stderr,
1587                        "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1588                        p, d6, d5, d4, d3, d2, d1, data));
1589                 FSHOW((stderr,"/MOV eax,0x%.8x\n", data));
1590             }
1591
1592             /*  the case of MOV m32,EAX */
1593             if (d1 == 0xa3) {
1594                 fixup_found = 1;
1595                 FSHOW((stderr,
1596                        "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1597                        p, d6, d5, d4, d3, d2, d1, data));
1598                 FSHOW((stderr, "/MOV 0x%.8x,eax\n", data));
1599             }
1600
1601             /* the case of CMP m32,imm32 */             
1602             if ((d1 == 0x3d) && (d2 == 0x81)) {
1603                 fixup_found = 1;
1604                 FSHOW((stderr,
1605                        "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1606                        p, d6, d5, d4, d3, d2, d1, data));
1607                 /* XX Check this */
1608                 FSHOW((stderr, "/CMP 0x%.8x,immed32\n", data));
1609             }
1610
1611             /* Check for a mod=00, r/m=101 byte. */
1612             if ((d1 & 0xc7) == 5) {
1613                 /* Cmp m32,reg */
1614                 if (d2 == 0x39) {
1615                     fixup_found = 1;
1616                     FSHOW((stderr,
1617                            "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1618                            p, d6, d5, d4, d3, d2, d1, data));
1619                     FSHOW((stderr,"/CMP 0x%.8x,reg\n", data));
1620                 }
1621                 /* the case of CMP reg32,m32 */
1622                 if (d2 == 0x3b) {
1623                     fixup_found = 1;
1624                     FSHOW((stderr,
1625                            "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1626                            p, d6, d5, d4, d3, d2, d1, data));
1627                     FSHOW((stderr, "/CMP reg32,0x%.8x\n", data));
1628                 }
1629                 /* the case of MOV m32,reg32 */
1630                 if (d2 == 0x89) {
1631                     fixup_found = 1;
1632                     FSHOW((stderr,
1633                            "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1634                            p, d6, d5, d4, d3, d2, d1, data));
1635                     FSHOW((stderr, "/MOV 0x%.8x,reg32\n", data));
1636                 }
1637                 /* the case of MOV reg32,m32 */
1638                 if (d2 == 0x8b) {
1639                     fixup_found = 1;
1640                     FSHOW((stderr,
1641                            "/abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1642                            p, d6, d5, d4, d3, d2, d1, data));
1643                     FSHOW((stderr, "/MOV reg32,0x%.8x\n", data));
1644                 }
1645                 /* the case of LEA reg32,m32 */
1646                 if (d2 == 0x8d) {
1647                     fixup_found = 1;
1648                     FSHOW((stderr,
1649                            "abs const ref @%x: %.2x %.2x %.2x %.2x %.2x %.2x (%.8x)\n",
1650                            p, d6, d5, d4, d3, d2, d1, data));
1651                     FSHOW((stderr, "/LEA reg32,0x%.8x\n", data));
1652                 }
1653             }
1654         }
1655     }
1656
1657     /* If anything was found, print some information on the code
1658      * object. */
1659     if (fixup_found) {
1660         FSHOW((stderr,
1661                "/compiled code object at %x: header words = %d, code words = %d\n",
1662                code, nheader_words, ncode_words));
1663         FSHOW((stderr,
1664                "/const start = %x, end = %x\n",
1665                constants_start_addr, constants_end_addr));
1666         FSHOW((stderr,
1667                "/code start = %x, end = %x\n",
1668                code_start_addr, code_end_addr));
1669     }
1670 }
1671
1672 void
1673 gencgc_apply_code_fixups(struct code *old_code, struct code *new_code)
1674 {
1675     int nheader_words, ncode_words, nwords;
1676     void *constants_start_addr, *constants_end_addr;
1677     void *code_start_addr, *code_end_addr;
1678     lispobj fixups = NIL;
1679     unsigned displacement = (unsigned)new_code - (unsigned)old_code;
1680     struct vector *fixups_vector;
1681
1682     ncode_words = fixnum_value(new_code->code_size);
1683     nheader_words = HeaderValue(*(lispobj *)new_code);
1684     nwords = ncode_words + nheader_words;
1685     /* FSHOW((stderr,
1686              "/compiled code object at %x: header words = %d, code words = %d\n",
1687              new_code, nheader_words, ncode_words)); */
1688     constants_start_addr = (void *)new_code + 5*4;
1689     constants_end_addr = (void *)new_code + nheader_words*4;
1690     code_start_addr = (void *)new_code + nheader_words*4;
1691     code_end_addr = (void *)new_code + nwords*4;
1692     /*
1693     FSHOW((stderr,
1694            "/const start = %x, end = %x\n",
1695            constants_start_addr,constants_end_addr));
1696     FSHOW((stderr,
1697            "/code start = %x; end = %x\n",
1698            code_start_addr,code_end_addr));
1699     */
1700
1701     /* The first constant should be a pointer to the fixups for this
1702        code objects. Check. */
1703     fixups = new_code->constants[0];
1704
1705     /* It will be 0 or the unbound-marker if there are no fixups, and
1706      * will be an other pointer if it is valid. */
1707     if ((fixups == 0) || (fixups == UNBOUND_MARKER_WIDETAG) ||
1708         !is_lisp_pointer(fixups)) {
1709         /* Check for possible errors. */
1710         if (check_code_fixups)
1711             sniff_code_object(new_code, displacement);
1712
1713         /*fprintf(stderr,"Fixups for code object not found!?\n");
1714           fprintf(stderr,"*** Compiled code object at %x: header_words=%d code_words=%d .\n",
1715           new_code, nheader_words, ncode_words);
1716           fprintf(stderr,"*** Const. start = %x; end= %x; Code start = %x; end = %x\n",
1717           constants_start_addr,constants_end_addr,
1718           code_start_addr,code_end_addr);*/
1719         return;
1720     }
1721
1722     fixups_vector = (struct vector *)native_pointer(fixups);
1723
1724     /* Could be pointing to a forwarding pointer. */
1725     if (is_lisp_pointer(fixups) &&
1726         (find_page_index((void*)fixups_vector) != -1) &&
1727         (fixups_vector->header == 0x01)) {
1728         /* If so, then follow it. */
1729         /*SHOW("following pointer to a forwarding pointer");*/
1730         fixups_vector = (struct vector *)native_pointer((lispobj)fixups_vector->length);
1731     }
1732
1733     /*SHOW("got fixups");*/
1734
1735     if (widetag_of(fixups_vector->header) ==
1736         SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG) {
1737         /* Got the fixups for the code block. Now work through the vector,
1738            and apply a fixup at each address. */
1739         int length = fixnum_value(fixups_vector->length);
1740         int i;
1741         for (i = 0; i < length; i++) {
1742             unsigned offset = fixups_vector->data[i];
1743             /* Now check the current value of offset. */
1744             unsigned old_value =
1745                 *(unsigned *)((unsigned)code_start_addr + offset);
1746
1747             /* If it's within the old_code object then it must be an
1748              * absolute fixup (relative ones are not saved) */
1749             if ((old_value >= (unsigned)old_code)
1750                 && (old_value < ((unsigned)old_code + nwords*4)))
1751                 /* So add the dispacement. */
1752                 *(unsigned *)((unsigned)code_start_addr + offset) =
1753                     old_value + displacement;
1754             else
1755                 /* It is outside the old code object so it must be a
1756                  * relative fixup (absolute fixups are not saved). So
1757                  * subtract the displacement. */
1758                 *(unsigned *)((unsigned)code_start_addr + offset) =
1759                     old_value - displacement;
1760         }
1761     }
1762
1763     /* Check for possible errors. */
1764     if (check_code_fixups) {
1765         sniff_code_object(new_code,displacement);
1766     }
1767 }
1768
1769
1770 static lispobj
1771 trans_boxed_large(lispobj object)
1772 {
1773     lispobj header;
1774     unsigned long length;
1775
1776     gc_assert(is_lisp_pointer(object));
1777
1778     header = *((lispobj *) native_pointer(object));
1779     length = HeaderValue(header) + 1;
1780     length = CEILING(length, 2);
1781
1782     return copy_large_object(object, length);
1783 }
1784
1785
1786 static lispobj
1787 trans_unboxed_large(lispobj object)
1788 {
1789     lispobj header;
1790     unsigned long length;
1791
1792
1793     gc_assert(is_lisp_pointer(object));
1794
1795     header = *((lispobj *) native_pointer(object));
1796     length = HeaderValue(header) + 1;
1797     length = CEILING(length, 2);
1798
1799     return copy_large_unboxed_object(object, length);
1800 }
1801
1802 \f
1803 /*
1804  * vector-like objects
1805  */
1806
1807
1808 /* FIXME: What does this mean? */
1809 int gencgc_hash = 1;
1810
1811 static int
1812 scav_vector(lispobj *where, lispobj object)
1813 {
1814     unsigned int kv_length;
1815     lispobj *kv_vector;
1816     unsigned int length = 0; /* (0 = dummy to stop GCC warning) */
1817     lispobj *hash_table;
1818     lispobj empty_symbol;
1819     unsigned int *index_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1820     unsigned int *next_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1821     unsigned int *hash_vector = NULL; /* (NULL = dummy to stop GCC warning) */
1822     lispobj weak_p_obj;
1823     unsigned next_vector_length = 0;
1824
1825     /* FIXME: A comment explaining this would be nice. It looks as
1826      * though SB-VM:VECTOR-VALID-HASHING-SUBTYPE is set for EQ-based
1827      * hash tables in the Lisp HASH-TABLE code, and nowhere else. */
1828     if (HeaderValue(object) != subtype_VectorValidHashing)
1829         return 1;
1830
1831     if (!gencgc_hash) {
1832         /* This is set for backward compatibility. FIXME: Do we need
1833          * this any more? */
1834         *where =
1835             (subtype_VectorMustRehash<<N_WIDETAG_BITS) | SIMPLE_VECTOR_WIDETAG;
1836         return 1;
1837     }
1838
1839     kv_length = fixnum_value(where[1]);
1840     kv_vector = where + 2;  /* Skip the header and length. */
1841     /*FSHOW((stderr,"/kv_length = %d\n", kv_length));*/
1842
1843     /* Scavenge element 0, which may be a hash-table structure. */
1844     scavenge(where+2, 1);
1845     if (!is_lisp_pointer(where[2])) {
1846         lose("no pointer at %x in hash table", where[2]);
1847     }
1848     hash_table = (lispobj *)native_pointer(where[2]);
1849     /*FSHOW((stderr,"/hash_table = %x\n", hash_table));*/
1850     if (widetag_of(hash_table[0]) != INSTANCE_HEADER_WIDETAG) {
1851         lose("hash table not instance (%x at %x)", hash_table[0], hash_table);
1852     }
1853
1854     /* Scavenge element 1, which should be some internal symbol that
1855      * the hash table code reserves for marking empty slots. */
1856     scavenge(where+3, 1);
1857     if (!is_lisp_pointer(where[3])) {
1858         lose("not empty-hash-table-slot symbol pointer: %x", where[3]);
1859     }
1860     empty_symbol = where[3];
1861     /* fprintf(stderr,"* empty_symbol = %x\n", empty_symbol);*/
1862     if (widetag_of(*(lispobj *)native_pointer(empty_symbol)) !=
1863         SYMBOL_HEADER_WIDETAG) {
1864         lose("not a symbol where empty-hash-table-slot symbol expected: %x",
1865              *(lispobj *)native_pointer(empty_symbol));
1866     }
1867
1868     /* Scavenge hash table, which will fix the positions of the other
1869      * needed objects. */
1870     scavenge(hash_table, 16);
1871
1872     /* Cross-check the kv_vector. */
1873     if (where != (lispobj *)native_pointer(hash_table[9])) {
1874         lose("hash_table table!=this table %x", hash_table[9]);
1875     }
1876
1877     /* WEAK-P */
1878     weak_p_obj = hash_table[10];
1879
1880     /* index vector */
1881     {
1882         lispobj index_vector_obj = hash_table[13];
1883
1884         if (is_lisp_pointer(index_vector_obj) &&
1885             (widetag_of(*(lispobj *)native_pointer(index_vector_obj)) ==
1886              SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1887             index_vector = ((unsigned int *)native_pointer(index_vector_obj)) + 2;
1888             /*FSHOW((stderr, "/index_vector = %x\n",index_vector));*/
1889             length = fixnum_value(((unsigned int *)native_pointer(index_vector_obj))[1]);
1890             /*FSHOW((stderr, "/length = %d\n", length));*/
1891         } else {
1892             lose("invalid index_vector %x", index_vector_obj);
1893         }
1894     }
1895
1896     /* next vector */
1897     {
1898         lispobj next_vector_obj = hash_table[14];
1899
1900         if (is_lisp_pointer(next_vector_obj) &&
1901             (widetag_of(*(lispobj *)native_pointer(next_vector_obj)) ==
1902              SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1903             next_vector = ((unsigned int *)native_pointer(next_vector_obj)) + 2;
1904             /*FSHOW((stderr, "/next_vector = %x\n", next_vector));*/
1905             next_vector_length = fixnum_value(((unsigned int *)native_pointer(next_vector_obj))[1]);
1906             /*FSHOW((stderr, "/next_vector_length = %d\n", next_vector_length));*/
1907         } else {
1908             lose("invalid next_vector %x", next_vector_obj);
1909         }
1910     }
1911
1912     /* maybe hash vector */
1913     {
1914         /* FIXME: This bare "15" offset should become a symbolic
1915          * expression of some sort. And all the other bare offsets
1916          * too. And the bare "16" in scavenge(hash_table, 16). And
1917          * probably other stuff too. Ugh.. */
1918         lispobj hash_vector_obj = hash_table[15];
1919
1920         if (is_lisp_pointer(hash_vector_obj) &&
1921             (widetag_of(*(lispobj *)native_pointer(hash_vector_obj))
1922              == SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG)) {
1923             hash_vector = ((unsigned int *)native_pointer(hash_vector_obj)) + 2;
1924             /*FSHOW((stderr, "/hash_vector = %x\n", hash_vector));*/
1925             gc_assert(fixnum_value(((unsigned int *)native_pointer(hash_vector_obj))[1])
1926                       == next_vector_length);
1927         } else {
1928             hash_vector = NULL;
1929             /*FSHOW((stderr, "/no hash_vector: %x\n", hash_vector_obj));*/
1930         }
1931     }
1932
1933     /* These lengths could be different as the index_vector can be a
1934      * different length from the others, a larger index_vector could help
1935      * reduce collisions. */
1936     gc_assert(next_vector_length*2 == kv_length);
1937
1938     /* now all set up.. */
1939
1940     /* Work through the KV vector. */
1941     {
1942         int i;
1943         for (i = 1; i < next_vector_length; i++) {
1944             lispobj old_key = kv_vector[2*i];
1945             unsigned int  old_index = (old_key & 0x1fffffff)%length;
1946
1947             /* Scavenge the key and value. */
1948             scavenge(&kv_vector[2*i],2);
1949
1950             /* Check whether the key has moved and is EQ based. */
1951             {
1952                 lispobj new_key = kv_vector[2*i];
1953                 unsigned int new_index = (new_key & 0x1fffffff)%length;
1954
1955                 if ((old_index != new_index) &&
1956                     ((!hash_vector) || (hash_vector[i] == 0x80000000)) &&
1957                     ((new_key != empty_symbol) ||
1958                      (kv_vector[2*i] != empty_symbol))) {
1959
1960                     /*FSHOW((stderr,
1961                            "* EQ key %d moved from %x to %x; index %d to %d\n",
1962                            i, old_key, new_key, old_index, new_index));*/
1963
1964                     if (index_vector[old_index] != 0) {
1965                         /*FSHOW((stderr, "/P1 %d\n", index_vector[old_index]));*/
1966
1967                         /* Unlink the key from the old_index chain. */
1968                         if (index_vector[old_index] == i) {
1969                             /*FSHOW((stderr, "/P2a %d\n", next_vector[i]));*/
1970                             index_vector[old_index] = next_vector[i];
1971                             /* Link it into the needing rehash chain. */
1972                             next_vector[i] = fixnum_value(hash_table[11]);
1973                             hash_table[11] = make_fixnum(i);
1974                             /*SHOW("P2");*/
1975                         } else {
1976                             unsigned prior = index_vector[old_index];
1977                             unsigned next = next_vector[prior];
1978
1979                             /*FSHOW((stderr, "/P3a %d %d\n", prior, next));*/
1980
1981                             while (next != 0) {
1982                                 /*FSHOW((stderr, "/P3b %d %d\n", prior, next));*/
1983                                 if (next == i) {
1984                                     /* Unlink it. */
1985                                     next_vector[prior] = next_vector[next];
1986                                     /* Link it into the needing rehash
1987                                      * chain. */
1988                                     next_vector[next] =
1989                                         fixnum_value(hash_table[11]);
1990                                     hash_table[11] = make_fixnum(next);
1991                                     /*SHOW("/P3");*/
1992                                     break;
1993                                 }
1994                                 prior = next;
1995                                 next = next_vector[next];
1996                             }
1997                         }
1998                     }
1999                 }
2000             }
2001         }
2002     }
2003     return (CEILING(kv_length + 2, 2));
2004 }
2005
2006
2007 \f
2008 /*
2009  * weak pointers
2010  */
2011
2012 /* XX This is a hack adapted from cgc.c. These don't work too
2013  * efficiently with the gencgc as a list of the weak pointers is
2014  * maintained within the objects which causes writes to the pages. A
2015  * limited attempt is made to avoid unnecessary writes, but this needs
2016  * a re-think. */
2017 #define WEAK_POINTER_NWORDS \
2018     CEILING((sizeof(struct weak_pointer) / sizeof(lispobj)), 2)
2019
2020 static int
2021 scav_weak_pointer(lispobj *where, lispobj object)
2022 {
2023     struct weak_pointer *wp = weak_pointers;
2024     /* Push the weak pointer onto the list of weak pointers.
2025      * Do I have to watch for duplicates? Originally this was
2026      * part of trans_weak_pointer but that didn't work in the
2027      * case where the WP was in a promoted region.
2028      */
2029
2030     /* Check whether it's already in the list. */
2031     while (wp != NULL) {
2032         if (wp == (struct weak_pointer*)where) {
2033             break;
2034         }
2035         wp = wp->next;
2036     }
2037     if (wp == NULL) {
2038         /* Add it to the start of the list. */
2039         wp = (struct weak_pointer*)where;
2040         if (wp->next != weak_pointers) {
2041             wp->next = weak_pointers;
2042         } else {
2043             /*SHOW("avoided write to weak pointer");*/
2044         }
2045         weak_pointers = wp;
2046     }
2047
2048     /* Do not let GC scavenge the value slot of the weak pointer.
2049      * (That is why it is a weak pointer.) */
2050
2051     return WEAK_POINTER_NWORDS;
2052 }
2053
2054 \f
2055 /* Scan an area looking for an object which encloses the given pointer.
2056  * Return the object start on success or NULL on failure. */
2057 static lispobj *
2058 search_space(lispobj *start, size_t words, lispobj *pointer)
2059 {
2060     while (words > 0) {
2061         size_t count = 1;
2062         lispobj thing = *start;
2063
2064         /* If thing is an immediate then this is a cons. */
2065         if (is_lisp_pointer(thing)
2066             || ((thing & 3) == 0) /* fixnum */
2067             || (widetag_of(thing) == BASE_CHAR_WIDETAG)
2068             || (widetag_of(thing) == UNBOUND_MARKER_WIDETAG))
2069             count = 2;
2070         else
2071             count = (sizetab[widetag_of(thing)])(start);
2072
2073         /* Check whether the pointer is within this object. */
2074         if ((pointer >= start) && (pointer < (start+count))) {
2075             /* found it! */
2076             /*FSHOW((stderr,"/found %x in %x %x\n", pointer, start, thing));*/
2077             return(start);
2078         }
2079
2080         /* Round up the count. */
2081         count = CEILING(count,2);
2082
2083         start += count;
2084         words -= count;
2085     }
2086     return (NULL);
2087 }
2088
2089 static lispobj*
2090 search_read_only_space(lispobj *pointer)
2091 {
2092     lispobj* start = (lispobj*)READ_ONLY_SPACE_START;
2093     lispobj* end = (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
2094     if ((pointer < start) || (pointer >= end))
2095         return NULL;
2096     return (search_space(start, (pointer+2)-start, pointer));
2097 }
2098
2099 static lispobj *
2100 search_static_space(lispobj *pointer)
2101 {
2102     lispobj* start = (lispobj*)STATIC_SPACE_START;
2103     lispobj* end = (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER);
2104     if ((pointer < start) || (pointer >= end))
2105         return NULL;
2106     return (search_space(start, (pointer+2)-start, pointer));
2107 }
2108
2109 /* a faster version for searching the dynamic space. This will work even
2110  * if the object is in a current allocation region. */
2111 lispobj *
2112 search_dynamic_space(lispobj *pointer)
2113 {
2114     int  page_index = find_page_index(pointer);
2115     lispobj *start;
2116
2117     /* The address may be invalid, so do some checks. */
2118     if ((page_index == -1) || (page_table[page_index].allocated == FREE_PAGE))
2119         return NULL;
2120     start = (lispobj *)((void *)page_address(page_index)
2121                         + page_table[page_index].first_object_offset);
2122     return (search_space(start, (pointer+2)-start, pointer));
2123 }
2124
2125 /* Is there any possibility that pointer is a valid Lisp object
2126  * reference, and/or something else (e.g. subroutine call return
2127  * address) which should prevent us from moving the referred-to thing? */
2128 static int
2129 possibly_valid_dynamic_space_pointer(lispobj *pointer)
2130 {
2131     lispobj *start_addr;
2132
2133     /* Find the object start address. */
2134     if ((start_addr = search_dynamic_space(pointer)) == NULL) {
2135         return 0;
2136     }
2137
2138     /* We need to allow raw pointers into Code objects for return
2139      * addresses. This will also pick up pointers to functions in code
2140      * objects. */
2141     if (widetag_of(*start_addr) == CODE_HEADER_WIDETAG) {
2142         /* XXX could do some further checks here */
2143         return 1;
2144     }
2145
2146     /* If it's not a return address then it needs to be a valid Lisp
2147      * pointer. */
2148     if (!is_lisp_pointer((lispobj)pointer)) {
2149         return 0;
2150     }
2151
2152     /* Check that the object pointed to is consistent with the pointer
2153      * low tag.
2154      *
2155      * FIXME: It's not safe to rely on the result from this check
2156      * before an object is initialized. Thus, if we were interrupted
2157      * just as an object had been allocated but not initialized, the
2158      * GC relying on this result could bogusly reclaim the memory.
2159      * However, we can't really afford to do without this check. So
2160      * we should make it safe somehow. 
2161      *   (1) Perhaps just review the code to make sure
2162      *       that WITHOUT-GCING or WITHOUT-INTERRUPTS or some such
2163      *       thing is wrapped around critical sections where allocated
2164      *       memory type bits haven't been set.
2165      *   (2) Perhaps find some other hack to protect against this, e.g.
2166      *       recording the result of the last call to allocate-lisp-memory,
2167      *       and returning true from this function when *pointer is
2168      *       a reference to that result. */
2169     switch (lowtag_of((lispobj)pointer)) {
2170     case FUN_POINTER_LOWTAG:
2171         /* Start_addr should be the enclosing code object, or a closure
2172          * header. */
2173         switch (widetag_of(*start_addr)) {
2174         case CODE_HEADER_WIDETAG:
2175             /* This case is probably caught above. */
2176             break;
2177         case CLOSURE_HEADER_WIDETAG:
2178         case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2179             if ((unsigned)pointer !=
2180                 ((unsigned)start_addr+FUN_POINTER_LOWTAG)) {
2181                 if (gencgc_verbose)
2182                     FSHOW((stderr,
2183                            "/Wf2: %x %x %x\n",
2184                            pointer, start_addr, *start_addr));
2185                 return 0;
2186             }
2187             break;
2188         default:
2189             if (gencgc_verbose)
2190                 FSHOW((stderr,
2191                        "/Wf3: %x %x %x\n",
2192                        pointer, start_addr, *start_addr));
2193             return 0;
2194         }
2195         break;
2196     case LIST_POINTER_LOWTAG:
2197         if ((unsigned)pointer !=
2198             ((unsigned)start_addr+LIST_POINTER_LOWTAG)) {
2199             if (gencgc_verbose)
2200                 FSHOW((stderr,
2201                        "/Wl1: %x %x %x\n",
2202                        pointer, start_addr, *start_addr));
2203             return 0;
2204         }
2205         /* Is it plausible cons? */
2206         if ((is_lisp_pointer(start_addr[0])
2207             || ((start_addr[0] & 3) == 0) /* fixnum */
2208             || (widetag_of(start_addr[0]) == BASE_CHAR_WIDETAG)
2209             || (widetag_of(start_addr[0]) == UNBOUND_MARKER_WIDETAG))
2210            && (is_lisp_pointer(start_addr[1])
2211                || ((start_addr[1] & 3) == 0) /* fixnum */
2212                || (widetag_of(start_addr[1]) == BASE_CHAR_WIDETAG)
2213                || (widetag_of(start_addr[1]) == UNBOUND_MARKER_WIDETAG)))
2214             break;
2215         else {
2216             if (gencgc_verbose)
2217                 FSHOW((stderr,
2218                        "/Wl2: %x %x %x\n",
2219                        pointer, start_addr, *start_addr));
2220             return 0;
2221         }
2222     case INSTANCE_POINTER_LOWTAG:
2223         if ((unsigned)pointer !=
2224             ((unsigned)start_addr+INSTANCE_POINTER_LOWTAG)) {
2225             if (gencgc_verbose)
2226                 FSHOW((stderr,
2227                        "/Wi1: %x %x %x\n",
2228                        pointer, start_addr, *start_addr));
2229             return 0;
2230         }
2231         if (widetag_of(start_addr[0]) != INSTANCE_HEADER_WIDETAG) {
2232             if (gencgc_verbose)
2233                 FSHOW((stderr,
2234                        "/Wi2: %x %x %x\n",
2235                        pointer, start_addr, *start_addr));
2236             return 0;
2237         }
2238         break;
2239     case OTHER_POINTER_LOWTAG:
2240         if ((unsigned)pointer !=
2241             ((int)start_addr+OTHER_POINTER_LOWTAG)) {
2242             if (gencgc_verbose)
2243                 FSHOW((stderr,
2244                        "/Wo1: %x %x %x\n",
2245                        pointer, start_addr, *start_addr));
2246             return 0;
2247         }
2248         /* Is it plausible?  Not a cons. XXX should check the headers. */
2249         if (is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
2250             if (gencgc_verbose)
2251                 FSHOW((stderr,
2252                        "/Wo2: %x %x %x\n",
2253                        pointer, start_addr, *start_addr));
2254             return 0;
2255         }
2256         switch (widetag_of(start_addr[0])) {
2257         case UNBOUND_MARKER_WIDETAG:
2258         case BASE_CHAR_WIDETAG:
2259             if (gencgc_verbose)
2260                 FSHOW((stderr,
2261                        "*Wo3: %x %x %x\n",
2262                        pointer, start_addr, *start_addr));
2263             return 0;
2264
2265             /* only pointed to by function pointers? */
2266         case CLOSURE_HEADER_WIDETAG:
2267         case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
2268             if (gencgc_verbose)
2269                 FSHOW((stderr,
2270                        "*Wo4: %x %x %x\n",
2271                        pointer, start_addr, *start_addr));
2272             return 0;
2273
2274         case INSTANCE_HEADER_WIDETAG:
2275             if (gencgc_verbose)
2276                 FSHOW((stderr,
2277                        "*Wo5: %x %x %x\n",
2278                        pointer, start_addr, *start_addr));
2279             return 0;
2280
2281             /* the valid other immediate pointer objects */
2282         case SIMPLE_VECTOR_WIDETAG:
2283         case RATIO_WIDETAG:
2284         case COMPLEX_WIDETAG:
2285 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
2286         case COMPLEX_SINGLE_FLOAT_WIDETAG:
2287 #endif
2288 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
2289         case COMPLEX_DOUBLE_FLOAT_WIDETAG:
2290 #endif
2291 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
2292         case COMPLEX_LONG_FLOAT_WIDETAG:
2293 #endif
2294         case SIMPLE_ARRAY_WIDETAG:
2295         case COMPLEX_STRING_WIDETAG:
2296         case COMPLEX_BIT_VECTOR_WIDETAG:
2297         case COMPLEX_VECTOR_WIDETAG:
2298         case COMPLEX_ARRAY_WIDETAG:
2299         case VALUE_CELL_HEADER_WIDETAG:
2300         case SYMBOL_HEADER_WIDETAG:
2301         case FDEFN_WIDETAG:
2302         case CODE_HEADER_WIDETAG:
2303         case BIGNUM_WIDETAG:
2304         case SINGLE_FLOAT_WIDETAG:
2305         case DOUBLE_FLOAT_WIDETAG:
2306 #ifdef LONG_FLOAT_WIDETAG
2307         case LONG_FLOAT_WIDETAG:
2308 #endif
2309         case SIMPLE_STRING_WIDETAG:
2310         case SIMPLE_BIT_VECTOR_WIDETAG:
2311         case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2312         case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2313         case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2314         case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2315         case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2316 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2317         case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2318 #endif
2319 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2320         case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2321 #endif
2322 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2323         case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2324 #endif
2325 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2326         case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2327 #endif
2328         case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2329         case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2330 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2331         case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2332 #endif
2333 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2334         case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2335 #endif
2336 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2337         case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2338 #endif
2339 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2340         case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2341 #endif
2342         case SAP_WIDETAG:
2343         case WEAK_POINTER_WIDETAG:
2344             break;
2345
2346         default:
2347             if (gencgc_verbose)
2348                 FSHOW((stderr,
2349                        "/Wo6: %x %x %x\n",
2350                        pointer, start_addr, *start_addr));
2351             return 0;
2352         }
2353         break;
2354     default:
2355         if (gencgc_verbose)
2356             FSHOW((stderr,
2357                    "*W?: %x %x %x\n",
2358                    pointer, start_addr, *start_addr));
2359         return 0;
2360     }
2361
2362     /* looks good */
2363     return 1;
2364 }
2365
2366 /* Adjust large bignum and vector objects. This will adjust the
2367  * allocated region if the size has shrunk, and move unboxed objects
2368  * into unboxed pages. The pages are not promoted here, and the
2369  * promoted region is not added to the new_regions; this is really
2370  * only designed to be called from preserve_pointer(). Shouldn't fail
2371  * if this is missed, just may delay the moving of objects to unboxed
2372  * pages, and the freeing of pages. */
2373 static void
2374 maybe_adjust_large_object(lispobj *where)
2375 {
2376     int first_page;
2377     int nwords;
2378
2379     int remaining_bytes;
2380     int next_page;
2381     int bytes_freed;
2382     int old_bytes_used;
2383
2384     int boxed;
2385
2386     /* Check whether it's a vector or bignum object. */
2387     switch (widetag_of(where[0])) {
2388     case SIMPLE_VECTOR_WIDETAG:
2389         boxed = BOXED_PAGE;
2390         break;
2391     case BIGNUM_WIDETAG:
2392     case SIMPLE_STRING_WIDETAG:
2393     case SIMPLE_BIT_VECTOR_WIDETAG:
2394     case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
2395     case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
2396     case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
2397     case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
2398     case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
2399 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
2400     case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
2401 #endif
2402 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
2403     case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
2404 #endif
2405 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
2406     case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
2407 #endif
2408 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
2409     case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
2410 #endif
2411     case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
2412     case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
2413 #ifdef SIMPLE_ARRAY_LONG_FLOAT_WIDETAG
2414     case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
2415 #endif
2416 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
2417     case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
2418 #endif
2419 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
2420     case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
2421 #endif
2422 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
2423     case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
2424 #endif
2425         boxed = UNBOXED_PAGE;
2426         break;
2427     default:
2428         return;
2429     }
2430
2431     /* Find its current size. */
2432     nwords = (sizetab[widetag_of(where[0])])(where);
2433
2434     first_page = find_page_index((void *)where);
2435     gc_assert(first_page >= 0);
2436
2437     /* Note: Any page write-protection must be removed, else a later
2438      * scavenge_newspace may incorrectly not scavenge these pages.
2439      * This would not be necessary if they are added to the new areas,
2440      * but lets do it for them all (they'll probably be written
2441      * anyway?). */
2442
2443     gc_assert(page_table[first_page].first_object_offset == 0);
2444
2445     next_page = first_page;
2446     remaining_bytes = nwords*4;
2447     while (remaining_bytes > 4096) {
2448         gc_assert(page_table[next_page].gen == from_space);
2449         gc_assert((page_table[next_page].allocated == BOXED_PAGE)
2450                   || (page_table[next_page].allocated == UNBOXED_PAGE));
2451         gc_assert(page_table[next_page].large_object);
2452         gc_assert(page_table[next_page].first_object_offset ==
2453                   -4096*(next_page-first_page));
2454         gc_assert(page_table[next_page].bytes_used == 4096);
2455
2456         page_table[next_page].allocated = boxed;
2457
2458         /* Shouldn't be write-protected at this stage. Essential that the
2459          * pages aren't. */
2460         gc_assert(!page_table[next_page].write_protected);
2461         remaining_bytes -= 4096;
2462         next_page++;
2463     }
2464
2465     /* Now only one page remains, but the object may have shrunk so
2466      * there may be more unused pages which will be freed. */
2467
2468     /* Object may have shrunk but shouldn't have grown - check. */
2469     gc_assert(page_table[next_page].bytes_used >= remaining_bytes);
2470
2471     page_table[next_page].allocated = boxed;
2472     gc_assert(page_table[next_page].allocated ==
2473               page_table[first_page].allocated);
2474
2475     /* Adjust the bytes_used. */
2476     old_bytes_used = page_table[next_page].bytes_used;
2477     page_table[next_page].bytes_used = remaining_bytes;
2478
2479     bytes_freed = old_bytes_used - remaining_bytes;
2480
2481     /* Free any remaining pages; needs care. */
2482     next_page++;
2483     while ((old_bytes_used == 4096) &&
2484            (page_table[next_page].gen == from_space) &&
2485            ((page_table[next_page].allocated == UNBOXED_PAGE)
2486             || (page_table[next_page].allocated == BOXED_PAGE)) &&
2487            page_table[next_page].large_object &&
2488            (page_table[next_page].first_object_offset ==
2489             -(next_page - first_page)*4096)) {
2490         /* It checks out OK, free the page. We don't need to both zeroing
2491          * pages as this should have been done before shrinking the
2492          * object. These pages shouldn't be write protected as they
2493          * should be zero filled. */
2494         gc_assert(page_table[next_page].write_protected == 0);
2495
2496         old_bytes_used = page_table[next_page].bytes_used;
2497         page_table[next_page].allocated = FREE_PAGE;
2498         page_table[next_page].bytes_used = 0;
2499         bytes_freed += old_bytes_used;
2500         next_page++;
2501     }
2502
2503     if ((bytes_freed > 0) && gencgc_verbose) {
2504         FSHOW((stderr,
2505                "/maybe_adjust_large_object() freed %d\n",
2506                bytes_freed));
2507     }
2508
2509     generations[from_space].bytes_allocated -= bytes_freed;
2510     bytes_allocated -= bytes_freed;
2511
2512     return;
2513 }
2514
2515 /* Take a possible pointer to a Lisp object and mark its page in the
2516  * page_table so that it will not be relocated during a GC.
2517  *
2518  * This involves locating the page it points to, then backing up to
2519  * the first page that has its first object start at offset 0, and
2520  * then marking all pages dont_move from the first until a page that
2521  * ends by being full, or having free gen.
2522  *
2523  * This ensures that objects spanning pages are not broken.
2524  *
2525  * It is assumed that all the page static flags have been cleared at
2526  * the start of a GC.
2527  *
2528  * It is also assumed that the current gc_alloc() region has been
2529  * flushed and the tables updated. */
2530 static void
2531 preserve_pointer(void *addr)
2532 {
2533     int addr_page_index = find_page_index(addr);
2534     int first_page;
2535     int i;
2536     unsigned region_allocation;
2537
2538     /* quick check 1: Address is quite likely to have been invalid. */
2539     if ((addr_page_index == -1)
2540         || (page_table[addr_page_index].allocated == FREE_PAGE)
2541         || (page_table[addr_page_index].bytes_used == 0)
2542         || (page_table[addr_page_index].gen != from_space)
2543         /* Skip if already marked dont_move. */
2544         || (page_table[addr_page_index].dont_move != 0))
2545         return;
2546     gc_assert(!(page_table[addr_page_index].allocated & OPEN_REGION_PAGE));
2547     /* (Now that we know that addr_page_index is in range, it's
2548      * safe to index into page_table[] with it.) */
2549     region_allocation = page_table[addr_page_index].allocated;
2550
2551     /* quick check 2: Check the offset within the page.
2552      *
2553      * FIXME: The mask should have a symbolic name, and ideally should
2554      * be derived from page size instead of hardwired to 0xfff.
2555      * (Also fix other uses of 0xfff, elsewhere.) */
2556     if (((unsigned)addr & 0xfff) > page_table[addr_page_index].bytes_used)
2557         return;
2558
2559     /* Filter out anything which can't be a pointer to a Lisp object
2560      * (or, as a special case which also requires dont_move, a return
2561      * address referring to something in a CodeObject). This is
2562      * expensive but important, since it vastly reduces the
2563      * probability that random garbage will be bogusly interpreter as
2564      * a pointer which prevents a page from moving. */
2565     if (!(possibly_valid_dynamic_space_pointer(addr)))
2566         return;
2567     first_page = addr_page_index;
2568
2569     /* Work backwards to find a page with a first_object_offset of 0.
2570      * The pages should be contiguous with all bytes used in the same
2571      * gen. Assumes the first_object_offset is negative or zero. */
2572
2573     /* this is probably needlessly conservative.  The first object in
2574      * the page may not even be the one we were passed a pointer to:
2575      * if this is the case, we will write-protect all the previous
2576      * object's pages too.
2577      */
2578
2579     while (page_table[first_page].first_object_offset != 0) {
2580         --first_page;
2581         /* Do some checks. */
2582         gc_assert(page_table[first_page].bytes_used == 4096);
2583         gc_assert(page_table[first_page].gen == from_space);
2584         gc_assert(page_table[first_page].allocated == region_allocation);
2585     }
2586
2587     /* Adjust any large objects before promotion as they won't be
2588      * copied after promotion. */
2589     if (page_table[first_page].large_object) {
2590         maybe_adjust_large_object(page_address(first_page));
2591         /* If a large object has shrunk then addr may now point to a
2592          * free area in which case it's ignored here. Note it gets
2593          * through the valid pointer test above because the tail looks
2594          * like conses. */
2595         if ((page_table[addr_page_index].allocated == FREE_PAGE)
2596             || (page_table[addr_page_index].bytes_used == 0)
2597             /* Check the offset within the page. */
2598             || (((unsigned)addr & 0xfff)
2599                 > page_table[addr_page_index].bytes_used)) {
2600             FSHOW((stderr,
2601                    "weird? ignore ptr 0x%x to freed area of large object\n",
2602                    addr));
2603             return;
2604         }
2605         /* It may have moved to unboxed pages. */
2606         region_allocation = page_table[first_page].allocated;
2607     }
2608
2609     /* Now work forward until the end of this contiguous area is found,
2610      * marking all pages as dont_move. */
2611     for (i = first_page; ;i++) {
2612         gc_assert(page_table[i].allocated == region_allocation);
2613
2614         /* Mark the page static. */
2615         page_table[i].dont_move = 1;
2616
2617         /* Move the page to the new_space. XX I'd rather not do this
2618          * but the GC logic is not quite able to copy with the static
2619          * pages remaining in the from space. This also requires the
2620          * generation bytes_allocated counters be updated. */
2621         page_table[i].gen = new_space;
2622         generations[new_space].bytes_allocated += page_table[i].bytes_used;
2623         generations[from_space].bytes_allocated -= page_table[i].bytes_used;
2624
2625         /* It is essential that the pages are not write protected as
2626          * they may have pointers into the old-space which need
2627          * scavenging. They shouldn't be write protected at this
2628          * stage. */
2629         gc_assert(!page_table[i].write_protected);
2630
2631         /* Check whether this is the last page in this contiguous block.. */
2632         if ((page_table[i].bytes_used < 4096)
2633             /* ..or it is 4096 and is the last in the block */
2634             || (page_table[i+1].allocated == FREE_PAGE)
2635             || (page_table[i+1].bytes_used == 0) /* next page free */
2636             || (page_table[i+1].gen != from_space) /* diff. gen */
2637             || (page_table[i+1].first_object_offset == 0))
2638             break;
2639     }
2640
2641     /* Check that the page is now static. */
2642     gc_assert(page_table[addr_page_index].dont_move != 0);
2643 }
2644 \f
2645 /* If the given page is not write-protected, then scan it for pointers
2646  * to younger generations or the top temp. generation, if no
2647  * suspicious pointers are found then the page is write-protected.
2648  *
2649  * Care is taken to check for pointers to the current gc_alloc()
2650  * region if it is a younger generation or the temp. generation. This
2651  * frees the caller from doing a gc_alloc_update_page_tables(). Actually
2652  * the gc_alloc_generation does not need to be checked as this is only
2653  * called from scavenge_generation() when the gc_alloc generation is
2654  * younger, so it just checks if there is a pointer to the current
2655  * region.
2656  *
2657  * We return 1 if the page was write-protected, else 0. */
2658 static int
2659 update_page_write_prot(int page)
2660 {
2661     int gen = page_table[page].gen;
2662     int j;
2663     int wp_it = 1;
2664     void **page_addr = (void **)page_address(page);
2665     int num_words = page_table[page].bytes_used / 4;
2666
2667     /* Shouldn't be a free page. */
2668     gc_assert(page_table[page].allocated != FREE_PAGE);
2669     gc_assert(page_table[page].bytes_used != 0);
2670
2671     /* Skip if it's already write-protected or an unboxed page. */
2672     if (page_table[page].write_protected
2673         || (page_table[page].allocated & UNBOXED_PAGE))
2674         return (0);
2675
2676     /* Scan the page for pointers to younger generations or the
2677      * top temp. generation. */
2678
2679     for (j = 0; j < num_words; j++) {
2680         void *ptr = *(page_addr+j);
2681         int index = find_page_index(ptr);
2682
2683         /* Check that it's in the dynamic space */
2684         if (index != -1)
2685             if (/* Does it point to a younger or the temp. generation? */
2686                 ((page_table[index].allocated != FREE_PAGE)
2687                  && (page_table[index].bytes_used != 0)
2688                  && ((page_table[index].gen < gen)
2689                      || (page_table[index].gen == NUM_GENERATIONS)))
2690
2691                 /* Or does it point within a current gc_alloc() region? */
2692                 || ((boxed_region.start_addr <= ptr)
2693                     && (ptr <= boxed_region.free_pointer))
2694                 || ((unboxed_region.start_addr <= ptr)
2695                     && (ptr <= unboxed_region.free_pointer))) {
2696                 wp_it = 0;
2697                 break;
2698             }
2699     }
2700
2701     if (wp_it == 1) {
2702         /* Write-protect the page. */
2703         /*FSHOW((stderr, "/write-protecting page %d gen %d\n", page, gen));*/
2704
2705         os_protect((void *)page_addr,
2706                    4096,
2707                    OS_VM_PROT_READ|OS_VM_PROT_EXECUTE);
2708
2709         /* Note the page as protected in the page tables. */
2710         page_table[page].write_protected = 1;
2711     }
2712
2713     return (wp_it);
2714 }
2715
2716 /* Scavenge a generation.
2717  *
2718  * This will not resolve all pointers when generation is the new
2719  * space, as new objects may be added which are not check here - use
2720  * scavenge_newspace generation.
2721  *
2722  * Write-protected pages should not have any pointers to the
2723  * from_space so do need scavenging; thus write-protected pages are
2724  * not always scavenged. There is some code to check that these pages
2725  * are not written; but to check fully the write-protected pages need
2726  * to be scavenged by disabling the code to skip them.
2727  *
2728  * Under the current scheme when a generation is GCed the younger
2729  * generations will be empty. So, when a generation is being GCed it
2730  * is only necessary to scavenge the older generations for pointers
2731  * not the younger. So a page that does not have pointers to younger
2732  * generations does not need to be scavenged.
2733  *
2734  * The write-protection can be used to note pages that don't have
2735  * pointers to younger pages. But pages can be written without having
2736  * pointers to younger generations. After the pages are scavenged here
2737  * they can be scanned for pointers to younger generations and if
2738  * there are none the page can be write-protected.
2739  *
2740  * One complication is when the newspace is the top temp. generation.
2741  *
2742  * Enabling SC_GEN_CK scavenges the write-protected pages and checks
2743  * that none were written, which they shouldn't be as they should have
2744  * no pointers to younger generations. This breaks down for weak
2745  * pointers as the objects contain a link to the next and are written
2746  * if a weak pointer is scavenged. Still it's a useful check. */
2747 static void
2748 scavenge_generation(int generation)
2749 {
2750     int i;
2751     int num_wp = 0;
2752
2753 #define SC_GEN_CK 0
2754 #if SC_GEN_CK
2755     /* Clear the write_protected_cleared flags on all pages. */
2756     for (i = 0; i < NUM_PAGES; i++)
2757         page_table[i].write_protected_cleared = 0;
2758 #endif
2759
2760     for (i = 0; i < last_free_page; i++) {
2761         if ((page_table[i].allocated & BOXED_PAGE)
2762             && (page_table[i].bytes_used != 0)
2763             && (page_table[i].gen == generation)) {
2764             int last_page;
2765
2766             /* This should be the start of a contiguous block. */
2767             gc_assert(page_table[i].first_object_offset == 0);
2768
2769             /* We need to find the full extent of this contiguous
2770              * block in case objects span pages. */
2771
2772             /* Now work forward until the end of this contiguous area
2773              * is found. A small area is preferred as there is a
2774              * better chance of its pages being write-protected. */
2775             for (last_page = i; ; last_page++)
2776                 /* Check whether this is the last page in this contiguous
2777                  * block. */
2778                 if ((page_table[last_page].bytes_used < 4096)
2779                     /* Or it is 4096 and is the last in the block */
2780                     || (!(page_table[last_page+1].allocated & BOXED_PAGE))
2781                     || (page_table[last_page+1].bytes_used == 0)
2782                     || (page_table[last_page+1].gen != generation)
2783                     || (page_table[last_page+1].first_object_offset == 0))
2784                     break;
2785
2786             /* Do a limited check for write_protected pages. If all pages
2787              * are write_protected then there is no need to scavenge. */
2788             {
2789                 int j, all_wp = 1;
2790                 for (j = i; j <= last_page; j++)
2791                     if (page_table[j].write_protected == 0) {
2792                         all_wp = 0;
2793                         break;
2794                     }
2795 #if !SC_GEN_CK
2796                 if (all_wp == 0)
2797 #endif
2798                     {
2799                         scavenge(page_address(i), (page_table[last_page].bytes_used
2800                                                    + (last_page-i)*4096)/4);
2801
2802                         /* Now scan the pages and write protect those
2803                          * that don't have pointers to younger
2804                          * generations. */
2805                         if (enable_page_protection) {
2806                             for (j = i; j <= last_page; j++) {
2807                                 num_wp += update_page_write_prot(j);
2808                             }
2809                         }
2810                     }
2811             }
2812             i = last_page;
2813         }
2814     }
2815
2816     if ((gencgc_verbose > 1) && (num_wp != 0)) {
2817         FSHOW((stderr,
2818                "/write protected %d pages within generation %d\n",
2819                num_wp, generation));
2820     }
2821
2822 #if SC_GEN_CK
2823     /* Check that none of the write_protected pages in this generation
2824      * have been written to. */
2825     for (i = 0; i < NUM_PAGES; i++) {
2826         if ((page_table[i].allocation ! =FREE_PAGE)
2827             && (page_table[i].bytes_used != 0)
2828             && (page_table[i].gen == generation)
2829             && (page_table[i].write_protected_cleared != 0)) {
2830             FSHOW((stderr, "/scavenge_generation() %d\n", generation));
2831             FSHOW((stderr,
2832                    "/page bytes_used=%d first_object_offset=%d dont_move=%d\n",
2833                     page_table[i].bytes_used,
2834                     page_table[i].first_object_offset,
2835                     page_table[i].dont_move));
2836             lose("write to protected page %d in scavenge_generation()", i);
2837         }
2838     }
2839 #endif
2840 }
2841
2842 \f
2843 /* Scavenge a newspace generation. As it is scavenged new objects may
2844  * be allocated to it; these will also need to be scavenged. This
2845  * repeats until there are no more objects unscavenged in the
2846  * newspace generation.
2847  *
2848  * To help improve the efficiency, areas written are recorded by
2849  * gc_alloc() and only these scavenged. Sometimes a little more will be
2850  * scavenged, but this causes no harm. An easy check is done that the
2851  * scavenged bytes equals the number allocated in the previous
2852  * scavenge.
2853  *
2854  * Write-protected pages are not scanned except if they are marked
2855  * dont_move in which case they may have been promoted and still have
2856  * pointers to the from space.
2857  *
2858  * Write-protected pages could potentially be written by alloc however
2859  * to avoid having to handle re-scavenging of write-protected pages
2860  * gc_alloc() does not write to write-protected pages.
2861  *
2862  * New areas of objects allocated are recorded alternatively in the two
2863  * new_areas arrays below. */
2864 static struct new_area new_areas_1[NUM_NEW_AREAS];
2865 static struct new_area new_areas_2[NUM_NEW_AREAS];
2866
2867 /* Do one full scan of the new space generation. This is not enough to
2868  * complete the job as new objects may be added to the generation in
2869  * the process which are not scavenged. */
2870 static void
2871 scavenge_newspace_generation_one_scan(int generation)
2872 {
2873     int i;
2874
2875     FSHOW((stderr,
2876            "/starting one full scan of newspace generation %d\n",
2877            generation));
2878     for (i = 0; i < last_free_page; i++) {
2879         /* note that this skips over open regions when it encounters them */
2880         if ((page_table[i].allocated == BOXED_PAGE)
2881             && (page_table[i].bytes_used != 0)
2882             && (page_table[i].gen == generation)
2883             && ((page_table[i].write_protected == 0)
2884                 /* (This may be redundant as write_protected is now
2885                  * cleared before promotion.) */
2886                 || (page_table[i].dont_move == 1))) {
2887             int last_page;
2888
2889             /* The scavenge will start at the first_object_offset of page i.
2890              *
2891              * We need to find the full extent of this contiguous
2892              * block in case objects span pages.
2893              *
2894              * Now work forward until the end of this contiguous area
2895              * is found. A small area is preferred as there is a
2896              * better chance of its pages being write-protected. */
2897             for (last_page = i; ;last_page++) {
2898                 /* Check whether this is the last page in this
2899                  * contiguous block */
2900                 if ((page_table[last_page].bytes_used < 4096)
2901                     /* Or it is 4096 and is the last in the block */
2902                     || (!(page_table[last_page+1].allocated & BOXED_PAGE))
2903                     || (page_table[last_page+1].bytes_used == 0)
2904                     || (page_table[last_page+1].gen != generation)
2905                     || (page_table[last_page+1].first_object_offset == 0))
2906                     break;
2907             }
2908
2909             /* Do a limited check for write-protected pages. If all
2910              * pages are write-protected then no need to scavenge,
2911              * except if the pages are marked dont_move. */
2912             {
2913                 int j, all_wp = 1;
2914                 for (j = i; j <= last_page; j++)
2915                     if ((page_table[j].write_protected == 0)
2916                         || (page_table[j].dont_move != 0)) {
2917                         all_wp = 0;
2918                         break;
2919                     }
2920
2921                 if (!all_wp) {
2922                     int size;
2923
2924                     /* Calculate the size. */
2925                     if (last_page == i)
2926                         size = (page_table[last_page].bytes_used
2927                                 - page_table[i].first_object_offset)/4;
2928                     else
2929                         size = (page_table[last_page].bytes_used
2930                                 + (last_page-i)*4096
2931                                 - page_table[i].first_object_offset)/4;
2932                     
2933                     {
2934                         new_areas_ignore_page = last_page;
2935                         
2936                         scavenge(page_address(i) +
2937                                  page_table[i].first_object_offset,
2938                                  size);
2939
2940                     }
2941                 }
2942             }
2943
2944             i = last_page;
2945         }
2946     }
2947     FSHOW((stderr,
2948            "/done with one full scan of newspace generation %d\n",
2949            generation));
2950 }
2951
2952 /* Do a complete scavenge of the newspace generation. */
2953 static void
2954 scavenge_newspace_generation(int generation)
2955 {
2956     int i;
2957
2958     /* the new_areas array currently being written to by gc_alloc() */
2959     struct new_area (*current_new_areas)[] = &new_areas_1;
2960     int current_new_areas_index;
2961
2962     /* the new_areas created but the previous scavenge cycle */
2963     struct new_area (*previous_new_areas)[] = NULL;
2964     int previous_new_areas_index;
2965
2966     /* Flush the current regions updating the tables. */
2967     gc_alloc_update_all_page_tables();
2968
2969     /* Turn on the recording of new areas by gc_alloc(). */
2970     new_areas = current_new_areas;
2971     new_areas_index = 0;
2972
2973     /* Don't need to record new areas that get scavenged anyway during
2974      * scavenge_newspace_generation_one_scan. */
2975     record_new_objects = 1;
2976
2977     /* Start with a full scavenge. */
2978     scavenge_newspace_generation_one_scan(generation);
2979
2980     /* Record all new areas now. */
2981     record_new_objects = 2;
2982
2983     /* Flush the current regions updating the tables. */
2984     gc_alloc_update_all_page_tables();
2985
2986     /* Grab new_areas_index. */
2987     current_new_areas_index = new_areas_index;
2988
2989     /*FSHOW((stderr,
2990              "The first scan is finished; current_new_areas_index=%d.\n",
2991              current_new_areas_index));*/
2992
2993     while (current_new_areas_index > 0) {
2994         /* Move the current to the previous new areas */
2995         previous_new_areas = current_new_areas;
2996         previous_new_areas_index = current_new_areas_index;
2997
2998         /* Scavenge all the areas in previous new areas. Any new areas
2999          * allocated are saved in current_new_areas. */
3000
3001         /* Allocate an array for current_new_areas; alternating between
3002          * new_areas_1 and 2 */
3003         if (previous_new_areas == &new_areas_1)
3004             current_new_areas = &new_areas_2;
3005         else
3006             current_new_areas = &new_areas_1;
3007
3008         /* Set up for gc_alloc(). */
3009         new_areas = current_new_areas;
3010         new_areas_index = 0;
3011
3012         /* Check whether previous_new_areas had overflowed. */
3013         if (previous_new_areas_index >= NUM_NEW_AREAS) {
3014
3015             /* New areas of objects allocated have been lost so need to do a
3016              * full scan to be sure! If this becomes a problem try
3017              * increasing NUM_NEW_AREAS. */
3018             if (gencgc_verbose)
3019                 SHOW("new_areas overflow, doing full scavenge");
3020
3021             /* Don't need to record new areas that get scavenge anyway
3022              * during scavenge_newspace_generation_one_scan. */
3023             record_new_objects = 1;
3024
3025             scavenge_newspace_generation_one_scan(generation);
3026
3027             /* Record all new areas now. */
3028             record_new_objects = 2;
3029
3030             /* Flush the current regions updating the tables. */
3031             gc_alloc_update_all_page_tables();
3032
3033         } else {
3034
3035             /* Work through previous_new_areas. */
3036             for (i = 0; i < previous_new_areas_index; i++) {
3037                 /* FIXME: All these bare *4 and /4 should be something
3038                  * like BYTES_PER_WORD or WBYTES. */
3039                 int page = (*previous_new_areas)[i].page;
3040                 int offset = (*previous_new_areas)[i].offset;
3041                 int size = (*previous_new_areas)[i].size / 4;
3042                 gc_assert((*previous_new_areas)[i].size % 4 == 0);
3043                 scavenge(page_address(page)+offset, size);
3044             }
3045
3046             /* Flush the current regions updating the tables. */
3047             gc_alloc_update_all_page_tables();
3048         }
3049
3050         current_new_areas_index = new_areas_index;
3051
3052         /*FSHOW((stderr,
3053                  "The re-scan has finished; current_new_areas_index=%d.\n",
3054                  current_new_areas_index));*/
3055     }
3056
3057     /* Turn off recording of areas allocated by gc_alloc(). */
3058     record_new_objects = 0;
3059
3060 #if SC_NS_GEN_CK
3061     /* Check that none of the write_protected pages in this generation
3062      * have been written to. */
3063     for (i = 0; i < NUM_PAGES; i++) {
3064         if ((page_table[i].allocation != FREE_PAGE)
3065             && (page_table[i].bytes_used != 0)
3066             && (page_table[i].gen == generation)
3067             && (page_table[i].write_protected_cleared != 0)
3068             && (page_table[i].dont_move == 0)) {
3069             lose("write protected page %d written to in scavenge_newspace_generation\ngeneration=%d dont_move=%d",
3070                  i, generation, page_table[i].dont_move);
3071         }
3072     }
3073 #endif
3074 }
3075 \f
3076 /* Un-write-protect all the pages in from_space. This is done at the
3077  * start of a GC else there may be many page faults while scavenging
3078  * the newspace (I've seen drive the system time to 99%). These pages
3079  * would need to be unprotected anyway before unmapping in
3080  * free_oldspace; not sure what effect this has on paging.. */
3081 static void
3082 unprotect_oldspace(void)
3083 {
3084     int i;
3085
3086     for (i = 0; i < last_free_page; i++) {
3087         if ((page_table[i].allocated != FREE_PAGE)
3088             && (page_table[i].bytes_used != 0)
3089             && (page_table[i].gen == from_space)) {
3090             void *page_start;
3091
3092             page_start = (void *)page_address(i);
3093
3094             /* Remove any write-protection. We should be able to rely
3095              * on the write-protect flag to avoid redundant calls. */
3096             if (page_table[i].write_protected) {
3097                 os_protect(page_start, 4096, OS_VM_PROT_ALL);
3098                 page_table[i].write_protected = 0;
3099             }
3100         }
3101     }
3102 }
3103
3104 /* Work through all the pages and free any in from_space. This
3105  * assumes that all objects have been copied or promoted to an older
3106  * generation. Bytes_allocated and the generation bytes_allocated
3107  * counter are updated. The number of bytes freed is returned. */
3108 extern void i586_bzero(void *addr, int nbytes);
3109 static int
3110 free_oldspace(void)
3111 {
3112     int bytes_freed = 0;
3113     int first_page, last_page;
3114
3115     first_page = 0;
3116
3117     do {
3118         /* Find a first page for the next region of pages. */
3119         while ((first_page < last_free_page)
3120                && ((page_table[first_page].allocated == FREE_PAGE)
3121                    || (page_table[first_page].bytes_used == 0)
3122                    || (page_table[first_page].gen != from_space)))
3123             first_page++;
3124
3125         if (first_page >= last_free_page)
3126             break;
3127
3128         /* Find the last page of this region. */
3129         last_page = first_page;
3130
3131         do {
3132             /* Free the page. */
3133             bytes_freed += page_table[last_page].bytes_used;
3134             generations[page_table[last_page].gen].bytes_allocated -=
3135                 page_table[last_page].bytes_used;
3136             page_table[last_page].allocated = FREE_PAGE;
3137             page_table[last_page].bytes_used = 0;
3138
3139             /* Remove any write-protection. We should be able to rely
3140              * on the write-protect flag to avoid redundant calls. */
3141             {
3142                 void  *page_start = (void *)page_address(last_page);
3143         
3144                 if (page_table[last_page].write_protected) {
3145                     os_protect(page_start, 4096, OS_VM_PROT_ALL);
3146                     page_table[last_page].write_protected = 0;
3147                 }
3148             }
3149             last_page++;
3150         }
3151         while ((last_page < last_free_page)
3152                && (page_table[last_page].allocated != FREE_PAGE)
3153                && (page_table[last_page].bytes_used != 0)
3154                && (page_table[last_page].gen == from_space));
3155
3156         /* Zero pages from first_page to (last_page-1).
3157          *
3158          * FIXME: Why not use os_zero(..) function instead of
3159          * hand-coding this again? (Check other gencgc_unmap_zero
3160          * stuff too. */
3161         if (gencgc_unmap_zero) {
3162             void *page_start, *addr;
3163
3164             page_start = (void *)page_address(first_page);
3165
3166             os_invalidate(page_start, 4096*(last_page-first_page));
3167             addr = os_validate(page_start, 4096*(last_page-first_page));
3168             if (addr == NULL || addr != page_start) {
3169                 /* Is this an error condition? I couldn't really tell from
3170                  * the old CMU CL code, which fprintf'ed a message with
3171                  * an exclamation point at the end. But I've never seen the
3172                  * message, so it must at least be unusual..
3173                  *
3174                  * (The same condition is also tested for in gc_free_heap.)
3175                  *
3176                  * -- WHN 19991129 */
3177                 lose("i586_bzero: page moved, 0x%08x ==> 0x%08x",
3178                      page_start,
3179                      addr);
3180             }
3181         } else {
3182             int *page_start;
3183
3184             page_start = (int *)page_address(first_page);
3185             i586_bzero(page_start, 4096*(last_page-first_page));
3186         }
3187
3188         first_page = last_page;
3189
3190     } while (first_page < last_free_page);
3191
3192     bytes_allocated -= bytes_freed;
3193     return bytes_freed;
3194 }
3195 \f
3196 #if 0
3197 /* Print some information about a pointer at the given address. */
3198 static void
3199 print_ptr(lispobj *addr)
3200 {
3201     /* If addr is in the dynamic space then out the page information. */
3202     int pi1 = find_page_index((void*)addr);
3203
3204     if (pi1 != -1)
3205         fprintf(stderr,"  %x: page %d  alloc %d  gen %d  bytes_used %d  offset %d  dont_move %d\n",
3206                 (unsigned int) addr,
3207                 pi1,
3208                 page_table[pi1].allocated,
3209                 page_table[pi1].gen,
3210                 page_table[pi1].bytes_used,
3211                 page_table[pi1].first_object_offset,
3212                 page_table[pi1].dont_move);
3213     fprintf(stderr,"  %x %x %x %x (%x) %x %x %x %x\n",
3214             *(addr-4),
3215             *(addr-3),
3216             *(addr-2),
3217             *(addr-1),
3218             *(addr-0),
3219             *(addr+1),
3220             *(addr+2),
3221             *(addr+3),
3222             *(addr+4));
3223 }
3224 #endif
3225
3226 extern int undefined_tramp;
3227
3228 static void
3229 verify_space(lispobj *start, size_t words)
3230 {
3231     int is_in_dynamic_space = (find_page_index((void*)start) != -1);
3232     int is_in_readonly_space =
3233         (READ_ONLY_SPACE_START <= (unsigned)start &&
3234          (unsigned)start < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
3235
3236     while (words > 0) {
3237         size_t count = 1;
3238         lispobj thing = *(lispobj*)start;
3239
3240         if (is_lisp_pointer(thing)) {
3241             int page_index = find_page_index((void*)thing);
3242             int to_readonly_space =
3243                 (READ_ONLY_SPACE_START <= thing &&
3244                  thing < SymbolValue(READ_ONLY_SPACE_FREE_POINTER));
3245             int to_static_space =
3246                 (STATIC_SPACE_START <= thing &&
3247                  thing < SymbolValue(STATIC_SPACE_FREE_POINTER));
3248
3249             /* Does it point to the dynamic space? */
3250             if (page_index != -1) {
3251                 /* If it's within the dynamic space it should point to a used
3252                  * page. XX Could check the offset too. */
3253                 if ((page_table[page_index].allocated != FREE_PAGE)
3254                     && (page_table[page_index].bytes_used == 0))
3255                     lose ("Ptr %x @ %x sees free page.", thing, start);
3256                 /* Check that it doesn't point to a forwarding pointer! */
3257                 if (*((lispobj *)native_pointer(thing)) == 0x01) {
3258                     lose("Ptr %x @ %x sees forwarding ptr.", thing, start);
3259                 }
3260                 /* Check that its not in the RO space as it would then be a
3261                  * pointer from the RO to the dynamic space. */
3262                 if (is_in_readonly_space) {
3263                     lose("ptr to dynamic space %x from RO space %x",
3264                          thing, start);
3265                 }
3266                 /* Does it point to a plausible object? This check slows
3267                  * it down a lot (so it's commented out).
3268                  *
3269                  * "a lot" is serious: it ate 50 minutes cpu time on
3270                  * my duron 950 before I came back from lunch and
3271                  * killed it.
3272                  *
3273                  *   FIXME: Add a variable to enable this
3274                  * dynamically. */
3275                 /*
3276                 if (!possibly_valid_dynamic_space_pointer((lispobj *)thing)) {
3277                     lose("ptr %x to invalid object %x", thing, start); 
3278                 }
3279                 */
3280             } else {
3281                 /* Verify that it points to another valid space. */
3282                 if (!to_readonly_space && !to_static_space
3283                     && (thing != (unsigned)&undefined_tramp)) {
3284                     lose("Ptr %x @ %x sees junk.", thing, start);
3285                 }
3286             }
3287         } else {
3288             if (thing & 0x3) { /* Skip fixnums. FIXME: There should be an
3289                                 * is_fixnum for this. */
3290
3291                 switch(widetag_of(*start)) {
3292
3293                     /* boxed objects */
3294                 case SIMPLE_VECTOR_WIDETAG:
3295                 case RATIO_WIDETAG:
3296                 case COMPLEX_WIDETAG:
3297                 case SIMPLE_ARRAY_WIDETAG:
3298                 case COMPLEX_STRING_WIDETAG:
3299                 case COMPLEX_BIT_VECTOR_WIDETAG:
3300                 case COMPLEX_VECTOR_WIDETAG:
3301                 case COMPLEX_ARRAY_WIDETAG:
3302                 case CLOSURE_HEADER_WIDETAG:
3303                 case FUNCALLABLE_INSTANCE_HEADER_WIDETAG:
3304                 case VALUE_CELL_HEADER_WIDETAG:
3305                 case SYMBOL_HEADER_WIDETAG:
3306                 case BASE_CHAR_WIDETAG:
3307                 case UNBOUND_MARKER_WIDETAG:
3308                 case INSTANCE_HEADER_WIDETAG:
3309                 case FDEFN_WIDETAG:
3310                     count = 1;
3311                     break;
3312
3313                 case CODE_HEADER_WIDETAG:
3314                     {
3315                         lispobj object = *start;
3316                         struct code *code;
3317                         int nheader_words, ncode_words, nwords;
3318                         lispobj fheaderl;
3319                         struct simple_fun *fheaderp;
3320
3321                         code = (struct code *) start;
3322
3323                         /* Check that it's not in the dynamic space.
3324                          * FIXME: Isn't is supposed to be OK for code
3325                          * objects to be in the dynamic space these days? */
3326                         if (is_in_dynamic_space
3327                             /* It's ok if it's byte compiled code. The trace
3328                              * table offset will be a fixnum if it's x86
3329                              * compiled code - check.
3330                              *
3331                              * FIXME: #^#@@! lack of abstraction here..
3332                              * This line can probably go away now that
3333                              * there's no byte compiler, but I've got
3334                              * too much to worry about right now to try
3335                              * to make sure. -- WHN 2001-10-06 */
3336                             && !(code->trace_table_offset & 0x3)
3337                             /* Only when enabled */
3338                             && verify_dynamic_code_check) {
3339                             FSHOW((stderr,
3340                                    "/code object at %x in the dynamic space\n",
3341                                    start));
3342                         }
3343
3344                         ncode_words = fixnum_value(code->code_size);
3345                         nheader_words = HeaderValue(object);
3346                         nwords = ncode_words + nheader_words;
3347                         nwords = CEILING(nwords, 2);
3348                         /* Scavenge the boxed section of the code data block */
3349                         verify_space(start + 1, nheader_words - 1);
3350
3351                         /* Scavenge the boxed section of each function
3352                          * object in the code data block. */
3353                         fheaderl = code->entry_points;
3354                         while (fheaderl != NIL) {
3355                             fheaderp =
3356                                 (struct simple_fun *) native_pointer(fheaderl);
3357                             gc_assert(widetag_of(fheaderp->header) == SIMPLE_FUN_HEADER_WIDETAG);
3358                             verify_space(&fheaderp->name, 1);
3359                             verify_space(&fheaderp->arglist, 1);
3360                             verify_space(&fheaderp->type, 1);
3361                             fheaderl = fheaderp->next;
3362                         }
3363                         count = nwords;
3364                         break;
3365                     }
3366         
3367                     /* unboxed objects */
3368                 case BIGNUM_WIDETAG:
3369                 case SINGLE_FLOAT_WIDETAG:
3370                 case DOUBLE_FLOAT_WIDETAG:
3371 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3372                 case LONG_FLOAT_WIDETAG:
3373 #endif
3374 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
3375                 case COMPLEX_SINGLE_FLOAT_WIDETAG:
3376 #endif
3377 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
3378                 case COMPLEX_DOUBLE_FLOAT_WIDETAG:
3379 #endif
3380 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
3381                 case COMPLEX_LONG_FLOAT_WIDETAG:
3382 #endif
3383                 case SIMPLE_STRING_WIDETAG:
3384                 case SIMPLE_BIT_VECTOR_WIDETAG:
3385                 case SIMPLE_ARRAY_UNSIGNED_BYTE_2_WIDETAG:
3386                 case SIMPLE_ARRAY_UNSIGNED_BYTE_4_WIDETAG:
3387                 case SIMPLE_ARRAY_UNSIGNED_BYTE_8_WIDETAG:
3388                 case SIMPLE_ARRAY_UNSIGNED_BYTE_16_WIDETAG:
3389                 case SIMPLE_ARRAY_UNSIGNED_BYTE_32_WIDETAG:
3390 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG
3391                 case SIMPLE_ARRAY_SIGNED_BYTE_8_WIDETAG:
3392 #endif
3393 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG
3394                 case SIMPLE_ARRAY_SIGNED_BYTE_16_WIDETAG:
3395 #endif
3396 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG
3397                 case SIMPLE_ARRAY_SIGNED_BYTE_30_WIDETAG:
3398 #endif
3399 #ifdef SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG
3400                 case SIMPLE_ARRAY_SIGNED_BYTE_32_WIDETAG:
3401 #endif
3402                 case SIMPLE_ARRAY_SINGLE_FLOAT_WIDETAG:
3403                 case SIMPLE_ARRAY_DOUBLE_FLOAT_WIDETAG:
3404 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3405                 case SIMPLE_ARRAY_LONG_FLOAT_WIDETAG:
3406 #endif
3407 #ifdef SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG
3408                 case SIMPLE_ARRAY_COMPLEX_SINGLE_FLOAT_WIDETAG:
3409 #endif
3410 #ifdef SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG
3411                 case SIMPLE_ARRAY_COMPLEX_DOUBLE_FLOAT_WIDETAG:
3412 #endif
3413 #ifdef SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG
3414                 case SIMPLE_ARRAY_COMPLEX_LONG_FLOAT_WIDETAG:
3415 #endif
3416                 case SAP_WIDETAG:
3417                 case WEAK_POINTER_WIDETAG:
3418                     count = (sizetab[widetag_of(*start)])(start);
3419                     break;
3420
3421                 default:
3422                     gc_abort();
3423                 }
3424             }
3425         }
3426         start += count;
3427         words -= count;
3428     }
3429 }
3430
3431 static void
3432 verify_gc(void)
3433 {
3434     /* FIXME: It would be nice to make names consistent so that
3435      * foo_size meant size *in* *bytes* instead of size in some
3436      * arbitrary units. (Yes, this caused a bug, how did you guess?:-)
3437      * Some counts of lispobjs are called foo_count; it might be good
3438      * to grep for all foo_size and rename the appropriate ones to
3439      * foo_count. */
3440     int read_only_space_size =
3441         (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER)
3442         - (lispobj*)READ_ONLY_SPACE_START;
3443     int static_space_size =
3444         (lispobj*)SymbolValue(STATIC_SPACE_FREE_POINTER)
3445         - (lispobj*)STATIC_SPACE_START;
3446     int binding_stack_size =
3447         (lispobj*)SymbolValue(BINDING_STACK_POINTER)
3448         - (lispobj*)BINDING_STACK_START;
3449
3450     verify_space((lispobj*)READ_ONLY_SPACE_START, read_only_space_size);
3451     verify_space((lispobj*)STATIC_SPACE_START   , static_space_size);
3452     verify_space((lispobj*)BINDING_STACK_START  , binding_stack_size);
3453 }
3454
3455 static void
3456 verify_generation(int  generation)
3457 {
3458     int i;
3459
3460     for (i = 0; i < last_free_page; i++) {
3461         if ((page_table[i].allocated != FREE_PAGE)
3462             && (page_table[i].bytes_used != 0)
3463             && (page_table[i].gen == generation)) {
3464             int last_page;
3465             int region_allocation = page_table[i].allocated;
3466
3467             /* This should be the start of a contiguous block */
3468             gc_assert(page_table[i].first_object_offset == 0);
3469
3470             /* Need to find the full extent of this contiguous block in case
3471                objects span pages. */
3472
3473             /* Now work forward until the end of this contiguous area is
3474                found. */
3475             for (last_page = i; ;last_page++)
3476                 /* Check whether this is the last page in this contiguous
3477                  * block. */
3478                 if ((page_table[last_page].bytes_used < 4096)
3479                     /* Or it is 4096 and is the last in the block */
3480                     || (page_table[last_page+1].allocated != region_allocation)
3481                     || (page_table[last_page+1].bytes_used == 0)
3482                     || (page_table[last_page+1].gen != generation)
3483                     || (page_table[last_page+1].first_object_offset == 0))
3484                     break;
3485
3486             verify_space(page_address(i), (page_table[last_page].bytes_used
3487                                            + (last_page-i)*4096)/4);
3488             i = last_page;
3489         }
3490     }
3491 }
3492
3493 /* Check that all the free space is zero filled. */
3494 static void
3495 verify_zero_fill(void)
3496 {
3497     int page;
3498
3499     for (page = 0; page < last_free_page; page++) {
3500         if (page_table[page].allocated == FREE_PAGE) {
3501             /* The whole page should be zero filled. */
3502             int *start_addr = (int *)page_address(page);
3503             int size = 1024;
3504             int i;
3505             for (i = 0; i < size; i++) {
3506                 if (start_addr[i] != 0) {
3507                     lose("free page not zero at %x", start_addr + i);
3508                 }
3509             }
3510         } else {
3511             int free_bytes = 4096 - page_table[page].bytes_used;
3512             if (free_bytes > 0) {
3513                 int *start_addr = (int *)((unsigned)page_address(page)
3514                                           + page_table[page].bytes_used);
3515                 int size = free_bytes / 4;
3516                 int i;
3517                 for (i = 0; i < size; i++) {
3518                     if (start_addr[i] != 0) {
3519                         lose("free region not zero at %x", start_addr + i);
3520                     }
3521                 }
3522             }
3523         }
3524     }
3525 }
3526
3527 /* External entry point for verify_zero_fill */
3528 void
3529 gencgc_verify_zero_fill(void)
3530 {
3531     /* Flush the alloc regions updating the tables. */
3532     gc_alloc_update_all_page_tables();
3533     SHOW("verifying zero fill");
3534     verify_zero_fill();
3535 }
3536
3537 static void
3538 verify_dynamic_space(void)
3539 {
3540     int i;
3541
3542     for (i = 0; i < NUM_GENERATIONS; i++)
3543         verify_generation(i);
3544
3545     if (gencgc_enable_verify_zero_fill)
3546         verify_zero_fill();
3547 }
3548 \f
3549 /* Write-protect all the dynamic boxed pages in the given generation. */
3550 static void
3551 write_protect_generation_pages(int generation)
3552 {
3553     int i;
3554
3555     gc_assert(generation < NUM_GENERATIONS);
3556
3557     for (i = 0; i < last_free_page; i++)
3558         if ((page_table[i].allocated == BOXED_PAGE)
3559             && (page_table[i].bytes_used != 0)
3560             && (page_table[i].gen == generation))  {
3561             void *page_start;
3562
3563             page_start = (void *)page_address(i);
3564
3565             os_protect(page_start,
3566                        4096,
3567                        OS_VM_PROT_READ | OS_VM_PROT_EXECUTE);
3568
3569             /* Note the page as protected in the page tables. */
3570             page_table[i].write_protected = 1;
3571         }
3572
3573     if (gencgc_verbose > 1) {
3574         FSHOW((stderr,
3575                "/write protected %d of %d pages in generation %d\n",
3576                count_write_protect_generation_pages(generation),
3577                count_generation_pages(generation),
3578                generation));
3579     }
3580 }
3581
3582 /* Garbage collect a generation. If raise is 0 then the remains of the
3583  * generation are not raised to the next generation. */
3584 static void
3585 garbage_collect_generation(int generation, int raise)
3586 {
3587     unsigned long bytes_freed;
3588     unsigned long i;
3589     unsigned long static_space_size;
3590
3591     gc_assert(generation <= (NUM_GENERATIONS-1));
3592
3593     /* The oldest generation can't be raised. */
3594     gc_assert((generation != (NUM_GENERATIONS-1)) || (raise == 0));
3595
3596     /* Initialize the weak pointer list. */
3597     weak_pointers = NULL;
3598
3599     /* When a generation is not being raised it is transported to a
3600      * temporary generation (NUM_GENERATIONS), and lowered when
3601      * done. Set up this new generation. There should be no pages
3602      * allocated to it yet. */
3603     if (!raise)
3604         gc_assert(generations[NUM_GENERATIONS].bytes_allocated == 0);
3605
3606     /* Set the global src and dest. generations */
3607     from_space = generation;
3608     if (raise)
3609         new_space = generation+1;
3610     else
3611         new_space = NUM_GENERATIONS;
3612
3613     /* Change to a new space for allocation, resetting the alloc_start_page */
3614     gc_alloc_generation = new_space;
3615     generations[new_space].alloc_start_page = 0;
3616     generations[new_space].alloc_unboxed_start_page = 0;
3617     generations[new_space].alloc_large_start_page = 0;
3618     generations[new_space].alloc_large_unboxed_start_page = 0;
3619
3620     /* Before any pointers are preserved, the dont_move flags on the
3621      * pages need to be cleared. */
3622     for (i = 0; i < last_free_page; i++)
3623         page_table[i].dont_move = 0;
3624
3625     /* Un-write-protect the old-space pages. This is essential for the
3626      * promoted pages as they may contain pointers into the old-space
3627      * which need to be scavenged. It also helps avoid unnecessary page
3628      * faults as forwarding pointers are written into them. They need to
3629      * be un-protected anyway before unmapping later. */
3630     unprotect_oldspace();
3631
3632     /* Scavenge the stack's conservative roots. */
3633     {
3634         void **ptr;
3635         for (ptr = (void **)CONTROL_STACK_END - 1;
3636              ptr > (void **)&raise;
3637              ptr--) {
3638             preserve_pointer(*ptr);
3639         }
3640     }
3641
3642 #if QSHOW
3643     if (gencgc_verbose > 1) {
3644         int num_dont_move_pages = count_dont_move_pages();
3645         fprintf(stderr,
3646                 "/non-movable pages due to conservative pointers = %d (%d bytes)\n",
3647                 num_dont_move_pages,
3648                 /* FIXME: 4096 should be symbolic constant here and
3649                  * prob'ly elsewhere too. */
3650                 num_dont_move_pages * 4096);
3651     }
3652 #endif
3653
3654     /* Scavenge all the rest of the roots. */
3655
3656     /* Scavenge the Lisp functions of the interrupt handlers, taking
3657      * care to avoid SIG_DFL and SIG_IGN. */
3658     for (i = 0; i < NSIG; i++) {
3659         union interrupt_handler handler = interrupt_handlers[i];
3660         if (!ARE_SAME_HANDLER(handler.c, SIG_IGN) &&
3661             !ARE_SAME_HANDLER(handler.c, SIG_DFL)) {
3662             scavenge((lispobj *)(interrupt_handlers + i), 1);
3663         }
3664     }
3665
3666     /* Scavenge the binding stack. */
3667     scavenge((lispobj *) BINDING_STACK_START,
3668              (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
3669              (lispobj *)BINDING_STACK_START);
3670
3671     /* The original CMU CL code had scavenge-read-only-space code
3672      * controlled by the Lisp-level variable
3673      * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
3674      * wasn't documented under what circumstances it was useful or
3675      * safe to turn it on, so it's been turned off in SBCL. If you
3676      * want/need this functionality, and can test and document it,
3677      * please submit a patch. */
3678 #if 0
3679     if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
3680         unsigned long read_only_space_size =
3681             (lispobj*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
3682             (lispobj*)READ_ONLY_SPACE_START;
3683         FSHOW((stderr,
3684                "/scavenge read only space: %d bytes\n",
3685                read_only_space_size * sizeof(lispobj)));
3686         scavenge( (lispobj *) READ_ONLY_SPACE_START, read_only_space_size);
3687     }
3688 #endif
3689
3690     /* Scavenge static space. */
3691     static_space_size =
3692         (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER) -
3693         (lispobj *)STATIC_SPACE_START;
3694     if (gencgc_verbose > 1) {
3695         FSHOW((stderr,
3696                "/scavenge static space: %d bytes\n",
3697                static_space_size * sizeof(lispobj)));
3698     }
3699     scavenge( (lispobj *) STATIC_SPACE_START, static_space_size);
3700
3701     /* All generations but the generation being GCed need to be
3702      * scavenged. The new_space generation needs special handling as
3703      * objects may be moved in - it is handled separately below. */
3704     for (i = 0; i < NUM_GENERATIONS; i++) {
3705         if ((i != generation) && (i != new_space)) {
3706             scavenge_generation(i);
3707         }
3708     }
3709
3710     /* Finally scavenge the new_space generation. Keep going until no
3711      * more objects are moved into the new generation */
3712     scavenge_newspace_generation(new_space);
3713
3714     /* FIXME: I tried reenabling this check when debugging unrelated
3715      * GC weirdness ca. sbcl-0.6.12.45, and it failed immediately.
3716      * Since the current GC code seems to work well, I'm guessing that
3717      * this debugging code is just stale, but I haven't tried to
3718      * figure it out. It should be figured out and then either made to
3719      * work or just deleted. */
3720 #define RESCAN_CHECK 0
3721 #if RESCAN_CHECK
3722     /* As a check re-scavenge the newspace once; no new objects should
3723      * be found. */
3724     {
3725         int old_bytes_allocated = bytes_allocated;
3726         int bytes_allocated;
3727
3728         /* Start with a full scavenge. */
3729         scavenge_newspace_generation_one_scan(new_space);
3730
3731         /* Flush the current regions, updating the tables. */
3732         gc_alloc_update_all_page_tables();
3733
3734         bytes_allocated = bytes_allocated - old_bytes_allocated;
3735
3736         if (bytes_allocated != 0) {
3737             lose("Rescan of new_space allocated %d more bytes.",
3738                  bytes_allocated);
3739         }
3740     }
3741 #endif
3742
3743     scan_weak_pointers();
3744
3745     /* Flush the current regions, updating the tables. */
3746     gc_alloc_update_all_page_tables();
3747
3748     /* Free the pages in oldspace, but not those marked dont_move. */
3749     bytes_freed = free_oldspace();
3750
3751     /* If the GC is not raising the age then lower the generation back
3752      * to its normal generation number */
3753     if (!raise) {
3754         for (i = 0; i < last_free_page; i++)
3755             if ((page_table[i].bytes_used != 0)
3756                 && (page_table[i].gen == NUM_GENERATIONS))
3757                 page_table[i].gen = generation;
3758         gc_assert(generations[generation].bytes_allocated == 0);
3759         generations[generation].bytes_allocated =
3760             generations[NUM_GENERATIONS].bytes_allocated;
3761         generations[NUM_GENERATIONS].bytes_allocated = 0;
3762     }
3763
3764     /* Reset the alloc_start_page for generation. */
3765     generations[generation].alloc_start_page = 0;
3766     generations[generation].alloc_unboxed_start_page = 0;
3767     generations[generation].alloc_large_start_page = 0;
3768     generations[generation].alloc_large_unboxed_start_page = 0;
3769
3770     if (generation >= verify_gens) {
3771         if (gencgc_verbose)
3772             SHOW("verifying");
3773         verify_gc();
3774         verify_dynamic_space();
3775     }
3776
3777     /* Set the new gc trigger for the GCed generation. */
3778     generations[generation].gc_trigger =
3779         generations[generation].bytes_allocated
3780         + generations[generation].bytes_consed_between_gc;
3781
3782     if (raise)
3783         generations[generation].num_gc = 0;
3784     else
3785         ++generations[generation].num_gc;
3786 }
3787
3788 /* Update last_free_page, then SymbolValue(ALLOCATION_POINTER). */
3789 int
3790 update_x86_dynamic_space_free_pointer(void)
3791 {
3792     int last_page = -1;
3793     int i;
3794
3795     for (i = 0; i < NUM_PAGES; i++)
3796         if ((page_table[i].allocated != FREE_PAGE)
3797             && (page_table[i].bytes_used != 0))
3798             last_page = i;
3799
3800     last_free_page = last_page+1;
3801
3802     SetSymbolValue(ALLOCATION_POINTER,
3803                    (lispobj)(((char *)heap_base) + last_free_page*4096));
3804     return 0; /* dummy value: return something ... */
3805 }
3806
3807 /* GC all generations newer than last_gen, raising the objects in each
3808  * to the next older generation - we finish when all generations below
3809  * last_gen are empty.  Then if last_gen is due for a GC, or if
3810  * last_gen==NUM_GENERATIONS (the scratch generation?  eh?) we GC that
3811  * too.  The valid range for last_gen is: 0,1,...,NUM_GENERATIONS.
3812  *
3813  * We stop collecting at gencgc_oldest_gen_to_gc, even if this is less than
3814  * last_gen (oh, and note that by default it is NUM_GENERATIONS-1) */
3815  
3816 void
3817 collect_garbage(unsigned last_gen)
3818 {
3819     int gen = 0;
3820     int raise;
3821     int gen_to_wp;
3822     int i;
3823
3824     FSHOW((stderr, "/entering collect_garbage(%d)\n", last_gen));
3825
3826     if (last_gen > NUM_GENERATIONS) {
3827         FSHOW((stderr,
3828                "/collect_garbage: last_gen = %d, doing a level 0 GC\n",
3829                last_gen));
3830         last_gen = 0;
3831     }
3832
3833     /* Flush the alloc regions updating the tables. */
3834     gc_alloc_update_all_page_tables();
3835
3836     /* Verify the new objects created by Lisp code. */
3837     if (pre_verify_gen_0) {
3838         FSHOW((stderr, "pre-checking generation 0\n"));
3839         verify_generation(0);
3840     }
3841
3842     if (gencgc_verbose > 1)
3843         print_generation_stats(0);
3844
3845     do {
3846         /* Collect the generation. */
3847
3848         if (gen >= gencgc_oldest_gen_to_gc) {
3849             /* Never raise the oldest generation. */
3850             raise = 0;
3851         } else {
3852             raise =
3853                 (gen < last_gen)
3854                 || (generations[gen].num_gc >= generations[gen].trigger_age);
3855         }
3856
3857         if (gencgc_verbose > 1) {
3858             FSHOW((stderr,
3859                    "starting GC of generation %d with raise=%d alloc=%d trig=%d GCs=%d\n",
3860                    gen,
3861                    raise,
3862                    generations[gen].bytes_allocated,
3863                    generations[gen].gc_trigger,
3864                    generations[gen].num_gc));
3865         }
3866
3867         /* If an older generation is being filled, then update its
3868          * memory age. */
3869         if (raise == 1) {
3870             generations[gen+1].cum_sum_bytes_allocated +=
3871                 generations[gen+1].bytes_allocated;
3872         }
3873
3874         garbage_collect_generation(gen, raise);
3875
3876         /* Reset the memory age cum_sum. */
3877         generations[gen].cum_sum_bytes_allocated = 0;
3878
3879         if (gencgc_verbose > 1) {
3880             FSHOW((stderr, "GC of generation %d finished:\n", gen));
3881             print_generation_stats(0);
3882         }
3883
3884         gen++;
3885     } while ((gen <= gencgc_oldest_gen_to_gc)
3886              && ((gen < last_gen)
3887                  || ((gen <= gencgc_oldest_gen_to_gc)
3888                      && raise
3889                      && (generations[gen].bytes_allocated
3890                          > generations[gen].gc_trigger)
3891                      && (gen_av_mem_age(gen)
3892                          > generations[gen].min_av_mem_age))));
3893
3894     /* Now if gen-1 was raised all generations before gen are empty.
3895      * If it wasn't raised then all generations before gen-1 are empty.
3896      *
3897      * Now objects within this gen's pages cannot point to younger
3898      * generations unless they are written to. This can be exploited
3899      * by write-protecting the pages of gen; then when younger
3900      * generations are GCed only the pages which have been written
3901      * need scanning. */
3902     if (raise)
3903         gen_to_wp = gen;
3904     else
3905         gen_to_wp = gen - 1;
3906
3907     /* There's not much point in WPing pages in generation 0 as it is
3908      * never scavenged (except promoted pages). */
3909     if ((gen_to_wp > 0) && enable_page_protection) {
3910         /* Check that they are all empty. */
3911         for (i = 0; i < gen_to_wp; i++) {
3912             if (generations[i].bytes_allocated)
3913                 lose("trying to write-protect gen. %d when gen. %d nonempty",
3914                      gen_to_wp, i);
3915         }
3916         write_protect_generation_pages(gen_to_wp);
3917     }
3918
3919     /* Set gc_alloc() back to generation 0. The current regions should
3920      * be flushed after the above GCs. */
3921     gc_assert((boxed_region.free_pointer - boxed_region.start_addr) == 0);
3922     gc_alloc_generation = 0;
3923
3924     update_x86_dynamic_space_free_pointer();
3925
3926     SHOW("returning from collect_garbage");
3927 }
3928
3929 /* This is called by Lisp PURIFY when it is finished. All live objects
3930  * will have been moved to the RO and Static heaps. The dynamic space
3931  * will need a full re-initialization. We don't bother having Lisp
3932  * PURIFY flush the current gc_alloc() region, as the page_tables are
3933  * re-initialized, and every page is zeroed to be sure. */
3934 void
3935 gc_free_heap(void)
3936 {
3937     int page;
3938
3939     if (gencgc_verbose > 1)
3940         SHOW("entering gc_free_heap");
3941
3942     for (page = 0; page < NUM_PAGES; page++) {
3943         /* Skip free pages which should already be zero filled. */
3944         if (page_table[page].allocated != FREE_PAGE) {
3945             void *page_start, *addr;
3946
3947             /* Mark the page free. The other slots are assumed invalid
3948              * when it is a FREE_PAGE and bytes_used is 0 and it
3949              * should not be write-protected -- except that the
3950              * generation is used for the current region but it sets
3951              * that up. */
3952             page_table[page].allocated = FREE_PAGE;
3953             page_table[page].bytes_used = 0;
3954
3955             /* Zero the page. */
3956             page_start = (void *)page_address(page);
3957
3958             /* First, remove any write-protection. */
3959             os_protect(page_start, 4096, OS_VM_PROT_ALL);
3960             page_table[page].write_protected = 0;
3961
3962             os_invalidate(page_start,4096);
3963             addr = os_validate(page_start,4096);
3964             if (addr == NULL || addr != page_start) {
3965                 lose("gc_free_heap: page moved, 0x%08x ==> 0x%08x",
3966                      page_start,
3967                      addr);
3968             }
3969         } else if (gencgc_zero_check_during_free_heap) {
3970             /* Double-check that the page is zero filled. */
3971             int *page_start, i;
3972             gc_assert(page_table[page].allocated == FREE_PAGE);
3973             gc_assert(page_table[page].bytes_used == 0);
3974             page_start = (int *)page_address(page);
3975             for (i=0; i<1024; i++) {
3976                 if (page_start[i] != 0) {
3977                     lose("free region not zero at %x", page_start + i);
3978                 }
3979             }
3980         }
3981     }
3982
3983     bytes_allocated = 0;
3984
3985     /* Initialize the generations. */
3986     for (page = 0; page < NUM_GENERATIONS; page++) {
3987         generations[page].alloc_start_page = 0;
3988         generations[page].alloc_unboxed_start_page = 0;
3989         generations[page].alloc_large_start_page = 0;
3990         generations[page].alloc_large_unboxed_start_page = 0;
3991         generations[page].bytes_allocated = 0;
3992         generations[page].gc_trigger = 2000000;
3993         generations[page].num_gc = 0;
3994         generations[page].cum_sum_bytes_allocated = 0;
3995     }
3996
3997     if (gencgc_verbose > 1)
3998         print_generation_stats(0);
3999
4000     /* Initialize gc_alloc(). */
4001     gc_alloc_generation = 0;
4002
4003     gc_set_region_empty(&boxed_region);
4004     gc_set_region_empty(&unboxed_region);
4005
4006     last_free_page = 0;
4007     SetSymbolValue(ALLOCATION_POINTER, (lispobj)((char *)heap_base));
4008
4009     if (verify_after_free_heap) {
4010         /* Check whether purify has left any bad pointers. */
4011         if (gencgc_verbose)
4012             SHOW("checking after free_heap\n");
4013         verify_gc();
4014     }
4015 }
4016 \f
4017 void
4018 gc_init(void)
4019 {
4020     int i;
4021
4022     gc_init_tables();
4023     scavtab[SIMPLE_VECTOR_WIDETAG] = scav_vector;
4024     scavtab[WEAK_POINTER_WIDETAG] = scav_weak_pointer;
4025     transother[SIMPLE_ARRAY_WIDETAG] = trans_boxed_large;
4026
4027     heap_base = (void*)DYNAMIC_SPACE_START;
4028
4029     /* Initialize each page structure. */
4030     for (i = 0; i < NUM_PAGES; i++) {
4031         /* Initialize all pages as free. */
4032         page_table[i].allocated = FREE_PAGE;
4033         page_table[i].bytes_used = 0;
4034
4035         /* Pages are not write-protected at startup. */
4036         page_table[i].write_protected = 0;
4037     }
4038
4039     bytes_allocated = 0;
4040
4041     /* Initialize the generations.
4042      *
4043      * FIXME: very similar to code in gc_free_heap(), should be shared */
4044     for (i = 0; i < NUM_GENERATIONS; i++) {
4045         generations[i].alloc_start_page = 0;
4046         generations[i].alloc_unboxed_start_page = 0;
4047         generations[i].alloc_large_start_page = 0;
4048         generations[i].alloc_large_unboxed_start_page = 0;
4049         generations[i].bytes_allocated = 0;
4050         generations[i].gc_trigger = 2000000;
4051         generations[i].num_gc = 0;
4052         generations[i].cum_sum_bytes_allocated = 0;
4053         /* the tune-able parameters */
4054         generations[i].bytes_consed_between_gc = 2000000;
4055         generations[i].trigger_age = 1;
4056         generations[i].min_av_mem_age = 0.75;
4057     }
4058
4059     /* Initialize gc_alloc. */
4060     gc_alloc_generation = 0;
4061     gc_set_region_empty(&boxed_region);
4062     gc_set_region_empty(&unboxed_region);
4063
4064     last_free_page = 0;
4065
4066 }
4067
4068 /*  Pick up the dynamic space from after a core load.
4069  *
4070  *  The ALLOCATION_POINTER points to the end of the dynamic space.
4071  *
4072  *  XX A scan is needed to identify the closest first objects for pages. */
4073 static void
4074 gencgc_pickup_dynamic(void)
4075 {
4076     int page = 0;
4077     int addr = DYNAMIC_SPACE_START;
4078     int alloc_ptr = SymbolValue(ALLOCATION_POINTER);
4079
4080     /* Initialize the first region. */
4081     do {
4082         page_table[page].allocated = BOXED_PAGE;
4083         page_table[page].gen = 0;
4084         page_table[page].bytes_used = 4096;
4085         page_table[page].large_object = 0;
4086         page_table[page].first_object_offset =
4087             (void *)DYNAMIC_SPACE_START - page_address(page);
4088         addr += 4096;
4089         page++;
4090     } while (addr < alloc_ptr);
4091
4092     generations[0].bytes_allocated = 4096*page;
4093     bytes_allocated = 4096*page;
4094
4095 }
4096
4097 void
4098 gc_initialize_pointers(void)
4099 {
4100     gencgc_pickup_dynamic();
4101 }
4102
4103
4104 \f
4105
4106 extern boolean maybe_gc_pending ;
4107 /* alloc(..) is the external interface for memory allocation. It
4108  * allocates to generation 0. It is not called from within the garbage
4109  * collector as it is only external uses that need the check for heap
4110  * size (GC trigger) and to disable the interrupts (interrupts are
4111  * always disabled during a GC).
4112  *
4113  * The vops that call alloc(..) assume that the returned space is zero-filled.
4114  * (E.g. the most significant word of a 2-word bignum in MOVE-FROM-UNSIGNED.)
4115  *
4116  * The check for a GC trigger is only performed when the current
4117  * region is full, so in most cases it's not needed. */
4118
4119 char *
4120 alloc(int nbytes)
4121 {
4122     struct alloc_region *region=  &boxed_region; 
4123     void *new_obj;
4124     void *new_free_pointer;
4125
4126     /* Check for alignment allocation problems. */
4127     gc_assert((((unsigned)region->free_pointer & 0x7) == 0)
4128               && ((nbytes & 0x7) == 0));
4129     /* At this point we should either be in pseudo-atomic, or early
4130      * enough in cold initn that interrupts are not yet enabled anyway.
4131      * It would be nice to assert same.
4132      */
4133     gc_assert(SymbolValue(PSEUDO_ATOMIC_ATOMIC));
4134
4135     /* maybe we can do this quickly ... */
4136     new_free_pointer = region->free_pointer + nbytes;
4137     if (new_free_pointer <= region->end_addr) {
4138         new_obj = (void*)(region->free_pointer);
4139         region->free_pointer = new_free_pointer;
4140         return(new_obj);        /* yup */
4141     }
4142     
4143     /* we have to go the long way around, it seems.  Check whether 
4144      * we should GC in the near future
4145      */
4146     if (auto_gc_trigger && bytes_allocated > auto_gc_trigger) {
4147         auto_gc_trigger *= 2;
4148         /* set things up so that GC happens when we finish the PA
4149          * section.  */
4150         maybe_gc_pending=1;
4151         SetSymbolValue(PSEUDO_ATOMIC_INTERRUPTED, make_fixnum(1));
4152     }
4153     new_obj = gc_alloc_with_region(nbytes,0,region,0);
4154     return (new_obj);
4155 }
4156
4157 \f
4158 /*
4159  * noise to manipulate the gc trigger stuff
4160  */
4161
4162 void
4163 set_auto_gc_trigger(os_vm_size_t dynamic_usage)
4164 {
4165     auto_gc_trigger += dynamic_usage;
4166 }
4167
4168 void
4169 clear_auto_gc_trigger(void)
4170 {
4171     auto_gc_trigger = 0;
4172 }
4173 \f
4174 /* Find the code object for the given pc, or return NULL on failure.
4175  *
4176  * FIXME: PC shouldn't be lispobj*, should it? Maybe void*? */
4177 lispobj *
4178 component_ptr_from_pc(lispobj *pc)
4179 {
4180     lispobj *object = NULL;
4181
4182     if ( (object = search_read_only_space(pc)) )
4183         ;
4184     else if ( (object = search_static_space(pc)) )
4185         ;
4186     else
4187         object = search_dynamic_space(pc);
4188
4189     if (object) /* if we found something */
4190         if (widetag_of(*object) == CODE_HEADER_WIDETAG) /* if it's a code object */
4191             return(object);
4192
4193     return (NULL);
4194 }
4195 \f
4196 /*
4197  * shared support for the OS-dependent signal handlers which
4198  * catch GENCGC-related write-protect violations
4199  */
4200
4201 void unhandled_sigmemoryfault(void);
4202
4203 /* Depending on which OS we're running under, different signals might
4204  * be raised for a violation of write protection in the heap. This
4205  * function factors out the common generational GC magic which needs
4206  * to invoked in this case, and should be called from whatever signal
4207  * handler is appropriate for the OS we're running under.
4208  *
4209  * Return true if this signal is a normal generational GC thing that
4210  * we were able to handle, or false if it was abnormal and control
4211  * should fall through to the general SIGSEGV/SIGBUS/whatever logic. */
4212
4213 int
4214 gencgc_handle_wp_violation(void* fault_addr)
4215 {
4216     int  page_index = find_page_index(fault_addr);
4217
4218 #if defined QSHOW_SIGNALS
4219     FSHOW((stderr, "heap WP violation? fault_addr=%x, page_index=%d\n",
4220            fault_addr, page_index));
4221 #endif
4222
4223     /* Check whether the fault is within the dynamic space. */
4224     if (page_index == (-1)) {
4225
4226         /* It can be helpful to be able to put a breakpoint on this
4227          * case to help diagnose low-level problems. */
4228         unhandled_sigmemoryfault();
4229
4230         /* not within the dynamic space -- not our responsibility */
4231         return 0;
4232
4233     } else {
4234
4235         /* The only acceptable reason for an signal like this from the
4236          * heap is that the generational GC write-protected the page. */
4237         if (page_table[page_index].write_protected != 1) {
4238             lose("access failure in heap page not marked as write-protected");
4239         }
4240         
4241         /* Unprotect the page. */
4242         os_protect(page_address(page_index), 4096, OS_VM_PROT_ALL);
4243         page_table[page_index].write_protected = 0;
4244         page_table[page_index].write_protected_cleared = 1;
4245
4246         /* Don't worry, we can handle it. */
4247         return 1;
4248     }
4249 }
4250
4251 /* This is to be called when we catch a SIGSEGV/SIGBUS, determine that
4252  * it's not just a case of the program hitting the write barrier, and
4253  * are about to let Lisp deal with it. It's basically just a
4254  * convenient place to set a gdb breakpoint. */
4255 void
4256 unhandled_sigmemoryfault()
4257 {}
4258
4259 gc_alloc_update_all_page_tables(void)
4260 {
4261     /* Flush the alloc regions updating the tables. */
4262     gc_alloc_update_page_tables(1, &unboxed_region);
4263     gc_alloc_update_page_tables(0, &boxed_region);
4264 }
4265 void 
4266 gc_set_region_empty(struct alloc_region *region)
4267 {
4268     region->first_page = 0;
4269     region->last_page = -1;
4270     region->start_addr = page_address(0);
4271     region->free_pointer = page_address(0);
4272     region->end_addr = page_address(0);
4273 }
4274