0.pre7.55:
[sbcl.git] / src / runtime / purify.c
1 /*
2  * C-level stuff to implement Lisp-level PURIFY
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 #include <stdio.h>
17 #include <sys/types.h>
18 #include <stdlib.h>
19
20 #include "runtime.h"
21 #include "os.h"
22 #include "sbcl.h"
23 #include "globals.h"
24 #include "validate.h"
25 #include "interrupt.h"
26 #include "purify.h"
27 #include "interr.h"
28 #ifdef GENCGC
29 #include "gencgc.h"
30 #endif
31
32 #define PRINTNOISE
33
34 #if defined(__i386__)
35 /* again, what's so special about the x86 that this is differently
36  * visible there than on other platforms? -dan 20010125 
37  */
38 static lispobj *dynamic_space_free_pointer;
39 #endif
40
41 #define gc_abort() \
42   lose("GC invariant lost, file \"%s\", line %d", __FILE__, __LINE__)
43
44 #if 1
45 #define gc_assert(ex) do { \
46         if (!(ex)) gc_abort(); \
47 } while (0)
48 #else
49 #define gc_assert(ex)
50 #endif
51
52 \f
53 /* These hold the original end of the read_only and static spaces so
54  * we can tell what are forwarding pointers. */
55
56 static lispobj *read_only_end, *static_end;
57
58 static lispobj *read_only_free, *static_free;
59
60 static lispobj *pscav(lispobj *addr, int nwords, boolean constant);
61
62 #define LATERBLOCKSIZE 1020
63 #define LATERMAXCOUNT 10
64
65 static struct
66 later {
67     struct later *next;
68     union {
69         lispobj *ptr;
70         int count;
71     } u[LATERBLOCKSIZE];
72 } *later_blocks = NULL;
73 static int later_count = 0;
74
75 #define CEILING(x,y) (((x) + ((y) - 1)) & (~((y) - 1)))
76 #define NWORDS(x,y) (CEILING((x),(y)) / (y))
77
78 /* FIXME: (1) Shouldn't this be defined in sbcl.h? (2) Shouldn't it
79  * be in the same units as FDEFN_RAW_ADDR_OFFSET? (This is measured
80  * in words, that's measured in bytes. Gotta love CMU CL..) */
81 #ifdef sparc
82 #define FUN_RAW_ADDR_OFFSET 0
83 #else
84 #define FUN_RAW_ADDR_OFFSET (6*sizeof(lispobj) - type_FunPointer)
85 #endif
86 \f
87 static boolean
88 forwarding_pointer_p(lispobj obj)
89 {
90     lispobj *ptr;
91
92     ptr = (lispobj *)obj;
93
94     return ((static_end <= ptr && ptr <= static_free) ||
95             (read_only_end <= ptr && ptr <= read_only_free));
96 }
97
98 static boolean
99 dynamic_pointer_p(lispobj ptr)
100 {
101 #ifndef __i386__
102     /* KLUDGE: This has an implicit dependence on the ordering of
103      * address spaces, and is therefore basically wrong. I'd fix it,
104      * but I don't have a non-386 port to test it on. Porters are
105      * encouraged to fix it. -- WHN 2000-10-17 */
106     return (ptr >= (lispobj)DYNAMIC_SPACE_START);
107 #else
108     /* Be more conservative, and remember, this is a maybe. */
109     return (ptr >= (lispobj)DYNAMIC_SPACE_START
110             &&
111             ptr < (lispobj)dynamic_space_free_pointer);
112 #endif
113 }
114
115 \f
116 #ifdef __i386__
117
118 #ifdef GENCGC
119 /*
120  * enhanced x86/GENCGC stack scavenging by Douglas Crosher
121  *
122  * Scavenging the stack on the i386 is problematic due to conservative
123  * roots and raw return addresses. Here it is handled in two passes:
124  * the first pass runs before any objects are moved and tries to
125  * identify valid pointers and return address on the stack, the second
126  * pass scavenges these.
127  */
128
129 static unsigned pointer_filter_verbose = 0;
130
131 /* FIXME: This is substantially the same code as in gencgc.c. (There
132  * are some differences, at least (1) the gencgc.c code needs to worry
133  * about return addresses on the stack pinning code objects, (2) the
134  * gencgc.c code needs to worry about the GC maybe happening in an
135  * interrupt service routine when the main thread of control was
136  * interrupted just as it had allocated memory and before it
137  * initialized it, while PURIFY needn't worry about that, and (3) the
138  * gencgc.c code has mutated more under maintenance since the fork
139  * from CMU CL than the code here has.) The two versions should be
140  * made to explicitly share common code, instead of just two different
141  * cut-and-pasted versions. */
142 static int
143 valid_dynamic_space_pointer(lispobj *pointer, lispobj *start_addr)
144 {
145     /* If it's not a return address then it needs to be a valid Lisp
146      * pointer. */
147     if (!is_lisp_pointer((lispobj)pointer))
148         return 0;
149
150     /* Check that the object pointed to is consistent with the pointer
151      * low tag. */
152     switch (LowtagOf((lispobj)pointer)) {
153     case type_FunPointer:
154         /* Start_addr should be the enclosing code object, or a closure
155          * header. */
156         switch (TypeOf(*start_addr)) {
157         case type_CodeHeader:
158             /* This case is probably caught above. */
159             break;
160         case type_ClosureHeader:
161         case type_FuncallableInstanceHeader:
162             if ((int)pointer != ((int)start_addr+type_FunPointer)) {
163                 if (pointer_filter_verbose) {
164                     fprintf(stderr,"*Wf2: %x %x %x\n", (unsigned int) pointer, 
165                             (unsigned int) start_addr, *start_addr);
166                 }
167                 return 0;
168             }
169             break;
170         default:
171             if (pointer_filter_verbose) {
172                 fprintf(stderr,"*Wf3: %x %x %x\n", (unsigned int) pointer, 
173                         (unsigned int) start_addr, *start_addr);
174             }
175             return 0;
176         }
177         break;
178     case type_ListPointer:
179         if ((int)pointer != ((int)start_addr+type_ListPointer)) {
180             if (pointer_filter_verbose)
181                 fprintf(stderr,"*Wl1: %x %x %x\n", (unsigned int) pointer, 
182                         (unsigned int) start_addr, *start_addr);
183             return 0;
184         }
185         /* Is it plausible cons? */
186         if((is_lisp_pointer(start_addr[0])
187             || ((start_addr[0] & 3) == 0) /* fixnum */
188             || (TypeOf(start_addr[0]) == type_BaseChar)
189             || (TypeOf(start_addr[0]) == type_UnboundMarker))
190            && (is_lisp_pointer(start_addr[1])
191                || ((start_addr[1] & 3) == 0) /* fixnum */
192                || (TypeOf(start_addr[1]) == type_BaseChar)
193                || (TypeOf(start_addr[1]) == type_UnboundMarker))) {
194             break;
195         } else {
196             if (pointer_filter_verbose) {
197                 fprintf(stderr,"*Wl2: %x %x %x\n", (unsigned int) pointer, 
198                         (unsigned int) start_addr, *start_addr);
199             }
200             return 0;
201         }
202     case type_InstancePointer:
203         if ((int)pointer != ((int)start_addr+type_InstancePointer)) {
204             if (pointer_filter_verbose) {
205                 fprintf(stderr,"*Wi1: %x %x %x\n", (unsigned int) pointer, 
206                         (unsigned int) start_addr, *start_addr);
207             }
208             return 0;
209         }
210         if (TypeOf(start_addr[0]) != type_InstanceHeader) {
211             if (pointer_filter_verbose) {
212                 fprintf(stderr,"*Wi2: %x %x %x\n", (unsigned int) pointer, 
213                         (unsigned int) start_addr, *start_addr);
214             }
215             return 0;
216         }
217         break;
218     case type_OtherPointer:
219         if ((int)pointer != ((int)start_addr+type_OtherPointer)) {
220             if (pointer_filter_verbose) {
221                 fprintf(stderr,"*Wo1: %x %x %x\n", (unsigned int) pointer, 
222                         (unsigned int) start_addr, *start_addr);
223             }
224             return 0;
225         }
226         /* Is it plausible?  Not a cons. X should check the headers. */
227         if(is_lisp_pointer(start_addr[0]) || ((start_addr[0] & 3) == 0)) {
228             if (pointer_filter_verbose) {
229                 fprintf(stderr,"*Wo2: %x %x %x\n", (unsigned int) pointer, 
230                         (unsigned int) start_addr, *start_addr);
231             }
232             return 0;
233         }
234         switch (TypeOf(start_addr[0])) {
235         case type_UnboundMarker:
236         case type_BaseChar:
237             if (pointer_filter_verbose) {
238                 fprintf(stderr,"*Wo3: %x %x %x\n", (unsigned int) pointer, 
239                         (unsigned int) start_addr, *start_addr);
240             }
241             return 0;
242
243             /* only pointed to by function pointers? */
244         case type_ClosureHeader:
245         case type_FuncallableInstanceHeader:
246             if (pointer_filter_verbose) {
247                 fprintf(stderr,"*Wo4: %x %x %x\n", (unsigned int) pointer, 
248                         (unsigned int) start_addr, *start_addr);
249             }
250             return 0;
251
252         case type_InstanceHeader:
253             if (pointer_filter_verbose) {
254                 fprintf(stderr,"*Wo5: %x %x %x\n", (unsigned int) pointer, 
255                         (unsigned int) start_addr, *start_addr);
256             }
257             return 0;
258
259             /* the valid other immediate pointer objects */
260         case type_SimpleVector:
261         case type_Ratio:
262         case type_Complex:
263 #ifdef type_ComplexSingleFloat
264         case type_ComplexSingleFloat:
265 #endif
266 #ifdef type_ComplexDoubleFloat
267         case type_ComplexDoubleFloat:
268 #endif
269 #ifdef type_ComplexLongFloat
270         case type_ComplexLongFloat:
271 #endif
272         case type_SimpleArray:
273         case type_ComplexString:
274         case type_ComplexBitVector:
275         case type_ComplexVector:
276         case type_ComplexArray:
277         case type_ValueCellHeader:
278         case type_SymbolHeader:
279         case type_Fdefn:
280         case type_CodeHeader:
281         case type_Bignum:
282         case type_SingleFloat:
283         case type_DoubleFloat:
284 #ifdef type_LongFloat
285         case type_LongFloat:
286 #endif
287         case type_SimpleString:
288         case type_SimpleBitVector:
289         case type_SimpleArrayUnsignedByte2:
290         case type_SimpleArrayUnsignedByte4:
291         case type_SimpleArrayUnsignedByte8:
292         case type_SimpleArrayUnsignedByte16:
293         case type_SimpleArrayUnsignedByte32:
294 #ifdef type_SimpleArraySignedByte8
295         case type_SimpleArraySignedByte8:
296 #endif
297 #ifdef type_SimpleArraySignedByte16
298         case type_SimpleArraySignedByte16:
299 #endif
300 #ifdef type_SimpleArraySignedByte30
301         case type_SimpleArraySignedByte30:
302 #endif
303 #ifdef type_SimpleArraySignedByte32
304         case type_SimpleArraySignedByte32:
305 #endif
306         case type_SimpleArraySingleFloat:
307         case type_SimpleArrayDoubleFloat:
308 #ifdef type_SimpleArrayLongFloat
309         case type_SimpleArrayLongFloat:
310 #endif
311 #ifdef type_SimpleArrayComplexSingleFloat
312         case type_SimpleArrayComplexSingleFloat:
313 #endif
314 #ifdef type_SimpleArrayComplexDoubleFloat
315         case type_SimpleArrayComplexDoubleFloat:
316 #endif
317 #ifdef type_SimpleArrayComplexLongFloat
318         case type_SimpleArrayComplexLongFloat:
319 #endif
320         case type_Sap:
321         case type_WeakPointer:
322             break;
323
324         default:
325             if (pointer_filter_verbose) {
326                 fprintf(stderr,"*Wo6: %x %x %x\n", (unsigned int) pointer, 
327                         (unsigned int) start_addr, *start_addr);
328             }
329             return 0;
330         }
331         break;
332     default:
333         if (pointer_filter_verbose) {
334             fprintf(stderr,"*W?: %x %x %x\n", (unsigned int) pointer, 
335                     (unsigned int) start_addr, *start_addr);
336         }
337         return 0;
338     }
339
340     /* looks good */
341     return 1;
342 }
343
344 #define MAX_STACK_POINTERS 256
345 lispobj *valid_stack_locations[MAX_STACK_POINTERS];
346 unsigned int num_valid_stack_locations;
347
348 #define MAX_STACK_RETURN_ADDRESSES 128
349 lispobj *valid_stack_ra_locations[MAX_STACK_RETURN_ADDRESSES];
350 lispobj *valid_stack_ra_code_objects[MAX_STACK_RETURN_ADDRESSES];
351 unsigned int num_valid_stack_ra_locations;
352
353 /* Identify valid stack slots. */
354 static void
355 setup_i386_stack_scav(lispobj *lowaddr, lispobj *base)
356 {
357     lispobj *sp = lowaddr;
358     num_valid_stack_locations = 0;
359     num_valid_stack_ra_locations = 0;
360     for (sp = lowaddr; sp < base; sp++) {
361         lispobj thing = *sp;
362         /* Find the object start address */
363         lispobj *start_addr = search_dynamic_space((void *)thing);
364         if (start_addr) {
365             /* We need to allow raw pointers into Code objects for
366              * return addresses. This will also pick up pointers to
367              * functions in code objects. */
368             if (TypeOf(*start_addr) == type_CodeHeader) {
369                 gc_assert(num_valid_stack_ra_locations <
370                           MAX_STACK_RETURN_ADDRESSES);
371                 valid_stack_ra_locations[num_valid_stack_ra_locations] = sp;
372                 valid_stack_ra_code_objects[num_valid_stack_ra_locations++] =
373                     (lispobj *)((int)start_addr + type_OtherPointer);
374             } else {
375                 if (valid_dynamic_space_pointer((void *)thing, start_addr)) {
376                     gc_assert(num_valid_stack_locations < MAX_STACK_POINTERS);
377                     valid_stack_locations[num_valid_stack_locations++] = sp;
378                 }
379             }
380         }
381     }
382     if (pointer_filter_verbose) {
383         fprintf(stderr, "number of valid stack pointers = %d\n",
384                 num_valid_stack_locations);
385         fprintf(stderr, "number of stack return addresses = %d\n",
386                 num_valid_stack_ra_locations);
387     }
388 }
389
390 static void
391 pscav_i386_stack(void)
392 {
393     int i;
394
395     for (i = 0; i < num_valid_stack_locations; i++)
396         pscav(valid_stack_locations[i], 1, 0);
397
398     for (i = 0; i < num_valid_stack_ra_locations; i++) {
399         lispobj code_obj = (lispobj)valid_stack_ra_code_objects[i];
400         pscav(&code_obj, 1, 0);
401         if (pointer_filter_verbose) {
402             fprintf(stderr,"*C moved RA %x to %x; for code object %x to %x\n",
403                     *valid_stack_ra_locations[i],
404                     (int)(*valid_stack_ra_locations[i])
405                     - ((int)valid_stack_ra_code_objects[i] - (int)code_obj),
406                     (unsigned int) valid_stack_ra_code_objects[i], code_obj);
407         }
408         *valid_stack_ra_locations[i] =
409             ((int)(*valid_stack_ra_locations[i])
410              - ((int)valid_stack_ra_code_objects[i] - (int)code_obj));
411     }
412 }
413 #endif
414 #endif
415
416 \f
417 static void
418 pscav_later(lispobj *where, int count)
419 {
420     struct later *new;
421
422     if (count > LATERMAXCOUNT) {
423         while (count > LATERMAXCOUNT) {
424             pscav_later(where, LATERMAXCOUNT);
425             count -= LATERMAXCOUNT;
426             where += LATERMAXCOUNT;
427         }
428     }
429     else {
430         if (later_blocks == NULL || later_count == LATERBLOCKSIZE ||
431             (later_count == LATERBLOCKSIZE-1 && count > 1)) {
432             new  = (struct later *)malloc(sizeof(struct later));
433             new->next = later_blocks;
434             if (later_blocks && later_count < LATERBLOCKSIZE)
435                 later_blocks->u[later_count].ptr = NULL;
436             later_blocks = new;
437             later_count = 0;
438         }
439
440         if (count != 1)
441             later_blocks->u[later_count++].count = count;
442         later_blocks->u[later_count++].ptr = where;
443     }
444 }
445
446 static lispobj
447 ptrans_boxed(lispobj thing, lispobj header, boolean constant)
448 {
449     int nwords;
450     lispobj result, *new, *old;
451
452     nwords = 1 + HeaderValue(header);
453
454     /* Allocate it */
455     old = (lispobj *)native_pointer(thing);
456     if (constant) {
457         new = read_only_free;
458         read_only_free += CEILING(nwords, 2);
459     }
460     else {
461         new = static_free;
462         static_free += CEILING(nwords, 2);
463     }
464
465     /* Copy it. */
466     bcopy(old, new, nwords * sizeof(lispobj));
467
468     /* Deposit forwarding pointer. */
469     result = (lispobj)new | LowtagOf(thing);
470     *old = result;
471
472     /* Scavenge it. */
473     pscav(new, nwords, constant);
474
475     return result;
476 }
477
478 /* We need to look at the layout to see whether it is a pure structure
479  * class, and only then can we transport as constant. If it is pure,
480  * we can ALWAYS transport as a constant. */
481 static lispobj
482 ptrans_instance(lispobj thing, lispobj header, boolean constant)
483 {
484     lispobj layout = ((struct instance *)native_pointer(thing))->slots[0];
485     lispobj pure = ((struct instance *)native_pointer(layout))->slots[15];
486
487     switch (pure) {
488     case T:
489         return (ptrans_boxed(thing, header, 1));
490     case NIL:
491         return (ptrans_boxed(thing, header, 0));
492     case 0:
493         {
494             /* Substructure: special case for the COMPACT-INFO-ENVs,
495              * where the instance may have a point to the dynamic
496              * space placed into it (e.g. the cache-name slot), but
497              * the lists and arrays at the time of a purify can be
498              * moved to the RO space. */
499             int nwords;
500             lispobj result, *new, *old;
501
502             nwords = 1 + HeaderValue(header);
503
504             /* Allocate it */
505             old = (lispobj *)native_pointer(thing);
506             new = static_free;
507             static_free += CEILING(nwords, 2);
508
509             /* Copy it. */
510             bcopy(old, new, nwords * sizeof(lispobj));
511
512             /* Deposit forwarding pointer. */
513             result = (lispobj)new | LowtagOf(thing);
514             *old = result;
515
516             /* Scavenge it. */
517             pscav(new, nwords, 1);
518
519             return result;
520         }
521     default:
522         gc_abort();
523         return NIL; /* dummy value: return something ... */
524     }
525 }
526
527 static lispobj
528 ptrans_fdefn(lispobj thing, lispobj header)
529 {
530     int nwords;
531     lispobj result, *new, *old, oldfn;
532     struct fdefn *fdefn;
533
534     nwords = 1 + HeaderValue(header);
535
536     /* Allocate it */
537     old = (lispobj *)native_pointer(thing);
538     new = static_free;
539     static_free += CEILING(nwords, 2);
540
541     /* Copy it. */
542     bcopy(old, new, nwords * sizeof(lispobj));
543
544     /* Deposit forwarding pointer. */
545     result = (lispobj)new | LowtagOf(thing);
546     *old = result;
547
548     /* Scavenge the function. */
549     fdefn = (struct fdefn *)new;
550     oldfn = fdefn->fun;
551     pscav(&fdefn->fun, 1, 0);
552     if ((char *)oldfn + FUN_RAW_ADDR_OFFSET == fdefn->raw_addr)
553         fdefn->raw_addr = (char *)fdefn->fun + FUN_RAW_ADDR_OFFSET;
554
555     return result;
556 }
557
558 static lispobj
559 ptrans_unboxed(lispobj thing, lispobj header)
560 {
561     int nwords;
562     lispobj result, *new, *old;
563
564     nwords = 1 + HeaderValue(header);
565
566     /* Allocate it */
567     old = (lispobj *)native_pointer(thing);
568     new = read_only_free;
569     read_only_free += CEILING(nwords, 2);
570
571     /* Copy it. */
572     bcopy(old, new, nwords * sizeof(lispobj));
573
574     /* Deposit forwarding pointer. */
575     result = (lispobj)new | LowtagOf(thing);
576     *old = result;
577
578     return result;
579 }
580
581 static lispobj
582 ptrans_vector(lispobj thing, int bits, int extra,
583               boolean boxed, boolean constant)
584 {
585     struct vector *vector;
586     int nwords;
587     lispobj result, *new;
588
589     vector = (struct vector *)native_pointer(thing);
590     nwords = 2 + (CEILING((fixnum_value(vector->length)+extra)*bits,32)>>5);
591
592     if (boxed && !constant) {
593         new = static_free;
594         static_free += CEILING(nwords, 2);
595     }
596     else {
597         new = read_only_free;
598         read_only_free += CEILING(nwords, 2);
599     }
600
601     bcopy(vector, new, nwords * sizeof(lispobj));
602
603     result = (lispobj)new | LowtagOf(thing);
604     vector->header = result;
605
606     if (boxed)
607         pscav(new, nwords, constant);
608
609     return result;
610 }
611
612 #ifdef __i386__
613 static void
614 apply_code_fixups_during_purify(struct code *old_code, struct code *new_code)
615 {
616   int nheader_words, ncode_words, nwords;
617   void  *constants_start_addr, *constants_end_addr;
618   void  *code_start_addr, *code_end_addr;
619   lispobj fixups = NIL;
620   unsigned  displacement = (unsigned)new_code - (unsigned)old_code;
621   struct vector *fixups_vector;
622
623   ncode_words = fixnum_value(new_code->code_size);
624   nheader_words = HeaderValue(*(lispobj *)new_code);
625   nwords = ncode_words + nheader_words;
626
627   constants_start_addr = (void *)new_code + 5*4;
628   constants_end_addr = (void *)new_code + nheader_words*4;
629   code_start_addr = (void *)new_code + nheader_words*4;
630   code_end_addr = (void *)new_code + nwords*4;
631
632   /* The first constant should be a pointer to the fixups for this
633    * code objects. Check. */
634   fixups = new_code->constants[0];
635
636   /* It will be 0 or the unbound-marker if there are no fixups, and
637    * will be an other-pointer to a vector if it is valid. */
638   if ((fixups==0) ||
639       (fixups==type_UnboundMarker) ||
640       !is_lisp_pointer(fixups)) {
641 #ifdef GENCGC
642     /* Check for a possible errors. */
643     sniff_code_object(new_code,displacement);
644 #endif
645     return;
646   }
647
648   fixups_vector = (struct vector *)native_pointer(fixups);
649
650   /* Could be pointing to a forwarding pointer. */
651   if (is_lisp_pointer(fixups) && (dynamic_pointer_p(fixups))
652       && forwarding_pointer_p(*(lispobj *)fixups_vector)) {
653     /* If so then follow it. */
654     fixups_vector = (struct vector *)native_pointer(*(lispobj *)fixups_vector);
655   }
656
657   if (TypeOf(fixups_vector->header) == type_SimpleArrayUnsignedByte32) {
658     /* We got the fixups for the code block. Now work through the vector,
659      * and apply a fixup at each address. */
660     int length = fixnum_value(fixups_vector->length);
661     int i;
662     for (i=0; i<length; i++) {
663       unsigned offset = fixups_vector->data[i];
664       /* Now check the current value of offset. */
665       unsigned  old_value = *(unsigned *)((unsigned)code_start_addr + offset);
666
667       /* If it's within the old_code object then it must be an
668        * absolute fixup (relative ones are not saved) */
669       if ((old_value>=(unsigned)old_code)
670           && (old_value<((unsigned)old_code + nwords*4)))
671         /* So add the dispacement. */
672         *(unsigned *)((unsigned)code_start_addr + offset) = old_value
673           + displacement;
674       else
675         /* It is outside the old code object so it must be a relative
676          * fixup (absolute fixups are not saved). So subtract the
677          * displacement. */
678         *(unsigned *)((unsigned)code_start_addr + offset) = old_value
679           - displacement;
680     }
681   }
682
683   /* No longer need the fixups. */
684   new_code->constants[0] = 0;
685
686 #ifdef GENCGC
687   /* Check for possible errors. */
688   sniff_code_object(new_code,displacement);
689 #endif
690 }
691 #endif
692
693 static lispobj
694 ptrans_code(lispobj thing)
695 {
696     struct code *code, *new;
697     int nwords;
698     lispobj func, result;
699
700     code = (struct code *)native_pointer(thing);
701     nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
702
703     new = (struct code *)read_only_free;
704     read_only_free += CEILING(nwords, 2);
705
706     bcopy(code, new, nwords * sizeof(lispobj));
707
708 #ifdef __i386__
709     apply_code_fixups_during_purify(code,new);
710 #endif
711
712     result = (lispobj)new | type_OtherPointer;
713
714     /* Stick in a forwarding pointer for the code object. */
715     *(lispobj *)code = result;
716
717     /* Put in forwarding pointers for all the functions. */
718     for (func = code->entry_points;
719          func != NIL;
720          func = ((struct simple_fun *)native_pointer(func))->next) {
721
722         gc_assert(LowtagOf(func) == type_FunPointer);
723
724         *(lispobj *)native_pointer(func) = result + (func - thing);
725     }
726
727     /* Arrange to scavenge the debug info later. */
728     pscav_later(&new->debug_info, 1);
729
730     if(new->trace_table_offset & 0x3)
731 #if 0
732       pscav(&new->trace_table_offset, 1, 0);
733 #else
734       new->trace_table_offset = NIL; /* limit lifetime */
735 #endif
736
737     /* Scavenge the constants. */
738     pscav(new->constants, HeaderValue(new->header)-5, 1);
739
740     /* Scavenge all the functions. */
741     pscav(&new->entry_points, 1, 1);
742     for (func = new->entry_points;
743          func != NIL;
744          func = ((struct simple_fun *)native_pointer(func))->next) {
745         gc_assert(LowtagOf(func) == type_FunPointer);
746         gc_assert(!dynamic_pointer_p(func));
747
748 #ifdef __i386__
749         /* Temporarly convert the self pointer to a real function pointer. */
750         ((struct simple_fun *)native_pointer(func))->self
751             -= FUN_RAW_ADDR_OFFSET;
752 #endif
753         pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
754 #ifdef __i386__
755         ((struct simple_fun *)native_pointer(func))->self
756             += FUN_RAW_ADDR_OFFSET;
757 #endif
758         pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
759     }
760
761     return result;
762 }
763
764 static lispobj
765 ptrans_func(lispobj thing, lispobj header)
766 {
767     int nwords;
768     lispobj code, *new, *old, result;
769     struct simple_fun *function;
770
771     /* Thing can either be a function header, a closure function
772      * header, a closure, or a funcallable-instance. If it's a closure
773      * or a funcallable-instance, we do the same as ptrans_boxed.
774      * Otherwise we have to do something strange, 'cause it is buried
775      * inside a code object. */
776
777     if (TypeOf(header) == type_SimpleFunHeader ||
778         TypeOf(header) == type_ClosureFunHeader) {
779
780         /* We can only end up here if the code object has not been
781          * scavenged, because if it had been scavenged, forwarding pointers
782          * would have been left behind for all the entry points. */
783
784         function = (struct simple_fun *)native_pointer(thing);
785         code =
786             (native_pointer(thing) -
787              (HeaderValue(function->header)*sizeof(lispobj))) |
788             type_OtherPointer;
789
790         /* This will cause the function's header to be replaced with a 
791          * forwarding pointer. */
792         ptrans_code(code);
793
794         /* So we can just return that. */
795         return function->header;
796     }
797     else {
798         /* It's some kind of closure-like thing. */
799         nwords = 1 + HeaderValue(header);
800         old = (lispobj *)native_pointer(thing);
801
802         /* Allocate the new one. */
803         if (TypeOf(header) == type_FuncallableInstanceHeader) {
804             /* FINs *must* not go in read_only space. */
805             new = static_free;
806             static_free += CEILING(nwords, 2);
807         }
808         else {
809             /* Closures can always go in read-only space, 'cause they
810              * never change. */
811
812             new = read_only_free;
813             read_only_free += CEILING(nwords, 2);
814         }
815         /* Copy it. */
816         bcopy(old, new, nwords * sizeof(lispobj));
817
818         /* Deposit forwarding pointer. */
819         result = (lispobj)new | LowtagOf(thing);
820         *old = result;
821
822         /* Scavenge it. */
823         pscav(new, nwords, 0);
824
825         return result;
826     }
827 }
828
829 static lispobj
830 ptrans_returnpc(lispobj thing, lispobj header)
831 {
832     lispobj code, new;
833
834     /* Find the corresponding code object. */
835     code = thing - HeaderValue(header)*sizeof(lispobj);
836
837     /* Make sure it's been transported. */
838     new = *(lispobj *)native_pointer(code);
839     if (!forwarding_pointer_p(new))
840         new = ptrans_code(code);
841
842     /* Maintain the offset: */
843     return new + (thing - code);
844 }
845
846 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
847
848 static lispobj
849 ptrans_list(lispobj thing, boolean constant)
850 {
851     struct cons *old, *new, *orig;
852     int length;
853
854     if (constant)
855         orig = (struct cons *)read_only_free;
856     else
857         orig = (struct cons *)static_free;
858     length = 0;
859
860     do {
861         /* Allocate a new cons cell. */
862         old = (struct cons *)native_pointer(thing);
863         if (constant) {
864             new = (struct cons *)read_only_free;
865             read_only_free += WORDS_PER_CONS;
866         }
867         else {
868             new = (struct cons *)static_free;
869             static_free += WORDS_PER_CONS;
870         }
871
872         /* Copy the cons cell and keep a pointer to the cdr. */
873         new->car = old->car;
874         thing = new->cdr = old->cdr;
875
876         /* Set up the forwarding pointer. */
877         *(lispobj *)old = ((lispobj)new) | type_ListPointer;
878
879         /* And count this cell. */
880         length++;
881     } while (LowtagOf(thing) == type_ListPointer &&
882              dynamic_pointer_p(thing) &&
883              !(forwarding_pointer_p(*(lispobj *)native_pointer(thing))));
884
885     /* Scavenge the list we just copied. */
886     pscav((lispobj *)orig, length * WORDS_PER_CONS, constant);
887
888     return ((lispobj)orig) | type_ListPointer;
889 }
890
891 static lispobj
892 ptrans_otherptr(lispobj thing, lispobj header, boolean constant)
893 {
894     switch (TypeOf(header)) {
895       case type_Bignum:
896       case type_SingleFloat:
897       case type_DoubleFloat:
898 #ifdef type_LongFloat
899       case type_LongFloat:
900 #endif
901 #ifdef type_ComplexSingleFloat
902       case type_ComplexSingleFloat:
903 #endif
904 #ifdef type_ComplexDoubleFloat
905       case type_ComplexDoubleFloat:
906 #endif
907 #ifdef type_ComplexLongFloat
908       case type_ComplexLongFloat:
909 #endif
910       case type_Sap:
911         return ptrans_unboxed(thing, header);
912
913       case type_Ratio:
914       case type_Complex:
915       case type_SimpleArray:
916       case type_ComplexString:
917       case type_ComplexVector:
918       case type_ComplexArray:
919         return ptrans_boxed(thing, header, constant);
920         
921       case type_ValueCellHeader:
922       case type_WeakPointer:
923         return ptrans_boxed(thing, header, 0);
924
925       case type_SymbolHeader:
926         return ptrans_boxed(thing, header, 0);
927
928       case type_SimpleString:
929         return ptrans_vector(thing, 8, 1, 0, constant);
930
931       case type_SimpleBitVector:
932         return ptrans_vector(thing, 1, 0, 0, constant);
933
934       case type_SimpleVector:
935         return ptrans_vector(thing, 32, 0, 1, constant);
936
937       case type_SimpleArrayUnsignedByte2:
938         return ptrans_vector(thing, 2, 0, 0, constant);
939
940       case type_SimpleArrayUnsignedByte4:
941         return ptrans_vector(thing, 4, 0, 0, constant);
942
943       case type_SimpleArrayUnsignedByte8:
944 #ifdef type_SimpleArraySignedByte8
945       case type_SimpleArraySignedByte8:
946 #endif
947         return ptrans_vector(thing, 8, 0, 0, constant);
948
949       case type_SimpleArrayUnsignedByte16:
950 #ifdef type_SimpleArraySignedByte16
951       case type_SimpleArraySignedByte16:
952 #endif
953         return ptrans_vector(thing, 16, 0, 0, constant);
954
955       case type_SimpleArrayUnsignedByte32:
956 #ifdef type_SimpleArraySignedByte30
957       case type_SimpleArraySignedByte30:
958 #endif
959 #ifdef type_SimpleArraySignedByte32
960       case type_SimpleArraySignedByte32:
961 #endif
962         return ptrans_vector(thing, 32, 0, 0, constant);
963
964       case type_SimpleArraySingleFloat:
965         return ptrans_vector(thing, 32, 0, 0, constant);
966
967       case type_SimpleArrayDoubleFloat:
968         return ptrans_vector(thing, 64, 0, 0, constant);
969
970 #ifdef type_SimpleArrayLongFloat
971       case type_SimpleArrayLongFloat:
972 #ifdef __i386__
973         return ptrans_vector(thing, 96, 0, 0, constant);
974 #endif
975 #ifdef sparc
976         return ptrans_vector(thing, 128, 0, 0, constant);
977 #endif
978 #endif
979
980 #ifdef type_SimpleArrayComplexSingleFloat
981       case type_SimpleArrayComplexSingleFloat:
982         return ptrans_vector(thing, 64, 0, 0, constant);
983 #endif
984
985 #ifdef type_SimpleArrayComplexDoubleFloat
986       case type_SimpleArrayComplexDoubleFloat:
987         return ptrans_vector(thing, 128, 0, 0, constant);
988 #endif
989
990 #ifdef type_SimpleArrayComplexLongFloat
991       case type_SimpleArrayComplexLongFloat:
992 #ifdef __i386__
993         return ptrans_vector(thing, 192, 0, 0, constant);
994 #endif
995 #ifdef sparc
996         return ptrans_vector(thing, 256, 0, 0, constant);
997 #endif
998 #endif
999
1000       case type_CodeHeader:
1001         return ptrans_code(thing);
1002
1003       case type_ReturnPcHeader:
1004         return ptrans_returnpc(thing, header);
1005
1006       case type_Fdefn:
1007         return ptrans_fdefn(thing, header);
1008
1009       default:
1010         /* Should only come across other pointers to the above stuff. */
1011         gc_abort();
1012         return NIL;
1013     }
1014 }
1015
1016 static int
1017 pscav_fdefn(struct fdefn *fdefn)
1018 {
1019     boolean fix_func;
1020
1021     fix_func = ((char *)(fdefn->fun+FUN_RAW_ADDR_OFFSET) == fdefn->raw_addr);
1022     pscav(&fdefn->name, 1, 1);
1023     pscav(&fdefn->fun, 1, 0);
1024     if (fix_func)
1025         fdefn->raw_addr = (char *)(fdefn->fun + FUN_RAW_ADDR_OFFSET);
1026     return sizeof(struct fdefn) / sizeof(lispobj);
1027 }
1028
1029 #ifdef __i386__
1030 /* now putting code objects in static space */
1031 static int
1032 pscav_code(struct code*code)
1033 {
1034     int nwords;
1035     lispobj func;
1036     nwords = HeaderValue(code->header) + fixnum_value(code->code_size);
1037
1038     /* Arrange to scavenge the debug info later. */
1039     pscav_later(&code->debug_info, 1);
1040
1041     /* Scavenge the constants. */
1042     pscav(code->constants, HeaderValue(code->header)-5, 1);
1043
1044     /* Scavenge all the functions. */
1045     pscav(&code->entry_points, 1, 1);
1046     for (func = code->entry_points;
1047          func != NIL;
1048          func = ((struct simple_fun *)native_pointer(func))->next) {
1049         gc_assert(LowtagOf(func) == type_FunPointer);
1050         gc_assert(!dynamic_pointer_p(func));
1051
1052 #ifdef __i386__
1053         /* Temporarly convert the self pointer to a real function
1054          * pointer. */
1055         ((struct simple_fun *)native_pointer(func))->self
1056             -= FUN_RAW_ADDR_OFFSET;
1057 #endif
1058         pscav(&((struct simple_fun *)native_pointer(func))->self, 2, 1);
1059 #ifdef __i386__
1060         ((struct simple_fun *)native_pointer(func))->self
1061             += FUN_RAW_ADDR_OFFSET;
1062 #endif
1063         pscav_later(&((struct simple_fun *)native_pointer(func))->name, 3);
1064     }
1065
1066     return CEILING(nwords,2);
1067 }
1068 #endif
1069
1070 static lispobj *
1071 pscav(lispobj *addr, int nwords, boolean constant)
1072 {
1073     lispobj thing, *thingp, header;
1074     int count = 0; /* (0 = dummy init value to stop GCC warning) */
1075     struct vector *vector;
1076
1077     while (nwords > 0) {
1078         thing = *addr;
1079         if (is_lisp_pointer(thing)) {
1080             /* It's a pointer. Is it something we might have to move? */
1081             if (dynamic_pointer_p(thing)) {
1082                 /* Maybe. Have we already moved it? */
1083                 thingp = (lispobj *)native_pointer(thing);
1084                 header = *thingp;
1085                 if (is_lisp_pointer(header) && forwarding_pointer_p(header))
1086                     /* Yep, so just copy the forwarding pointer. */
1087                     thing = header;
1088                 else {
1089                     /* Nope, copy the object. */
1090                     switch (LowtagOf(thing)) {
1091                       case type_FunPointer:
1092                         thing = ptrans_func(thing, header);
1093                         break;
1094
1095                       case type_ListPointer:
1096                         thing = ptrans_list(thing, constant);
1097                         break;
1098
1099                       case type_InstancePointer:
1100                         thing = ptrans_instance(thing, header, constant);
1101                         break;
1102
1103                       case type_OtherPointer:
1104                         thing = ptrans_otherptr(thing, header, constant);
1105                         break;
1106
1107                       default:
1108                         /* It was a pointer, but not one of them? */
1109                         gc_abort();
1110                     }
1111                 }
1112                 *addr = thing;
1113             }
1114             count = 1;
1115         }
1116         else if (thing & 3) {
1117             /* It's an other immediate. Maybe the header for an unboxed */
1118             /* object. */
1119             switch (TypeOf(thing)) {
1120               case type_Bignum:
1121               case type_SingleFloat:
1122               case type_DoubleFloat:
1123 #ifdef type_LongFloat
1124               case type_LongFloat:
1125 #endif
1126               case type_Sap:
1127                 /* It's an unboxed simple object. */
1128                 count = HeaderValue(thing)+1;
1129                 break;
1130
1131               case type_SimpleVector:
1132                 if (HeaderValue(thing) == subtype_VectorValidHashing)
1133                     *addr = (subtype_VectorMustRehash<<type_Bits) |
1134                         type_SimpleVector;
1135                 count = 1;
1136                 break;
1137
1138               case type_SimpleString:
1139                 vector = (struct vector *)addr;
1140                 count = CEILING(NWORDS(fixnum_value(vector->length)+1,4)+2,2);
1141                 break;
1142
1143               case type_SimpleBitVector:
1144                 vector = (struct vector *)addr;
1145                 count = CEILING(NWORDS(fixnum_value(vector->length),32)+2,2);
1146                 break;
1147
1148               case type_SimpleArrayUnsignedByte2:
1149                 vector = (struct vector *)addr;
1150                 count = CEILING(NWORDS(fixnum_value(vector->length),16)+2,2);
1151                 break;
1152
1153               case type_SimpleArrayUnsignedByte4:
1154                 vector = (struct vector *)addr;
1155                 count = CEILING(NWORDS(fixnum_value(vector->length),8)+2,2);
1156                 break;
1157
1158               case type_SimpleArrayUnsignedByte8:
1159 #ifdef type_SimpleArraySignedByte8
1160               case type_SimpleArraySignedByte8:
1161 #endif
1162                 vector = (struct vector *)addr;
1163                 count = CEILING(NWORDS(fixnum_value(vector->length),4)+2,2);
1164                 break;
1165
1166               case type_SimpleArrayUnsignedByte16:
1167 #ifdef type_SimpleArraySignedByte16
1168               case type_SimpleArraySignedByte16:
1169 #endif
1170                 vector = (struct vector *)addr;
1171                 count = CEILING(NWORDS(fixnum_value(vector->length),2)+2,2);
1172                 break;
1173
1174               case type_SimpleArrayUnsignedByte32:
1175 #ifdef type_SimpleArraySignedByte30
1176               case type_SimpleArraySignedByte30:
1177 #endif
1178 #ifdef type_SimpleArraySignedByte32
1179               case type_SimpleArraySignedByte32:
1180 #endif
1181                 vector = (struct vector *)addr;
1182                 count = CEILING(fixnum_value(vector->length)+2,2);
1183                 break;
1184
1185               case type_SimpleArraySingleFloat:
1186                 vector = (struct vector *)addr;
1187                 count = CEILING(fixnum_value(vector->length)+2,2);
1188                 break;
1189
1190               case type_SimpleArrayDoubleFloat:
1191 #ifdef type_SimpleArrayComplexSingleFloat
1192               case type_SimpleArrayComplexSingleFloat:
1193 #endif
1194                 vector = (struct vector *)addr;
1195                 count = fixnum_value(vector->length)*2+2;
1196                 break;
1197
1198 #ifdef type_SimpleArrayLongFloat
1199               case type_SimpleArrayLongFloat:
1200                 vector = (struct vector *)addr;
1201 #ifdef __i386__
1202                 count = fixnum_value(vector->length)*3+2;
1203 #endif
1204 #ifdef sparc
1205                 count = fixnum_value(vector->length)*4+2;
1206 #endif
1207                 break;
1208 #endif
1209
1210 #ifdef type_SimpleArrayComplexDoubleFloat
1211               case type_SimpleArrayComplexDoubleFloat:
1212                 vector = (struct vector *)addr;
1213                 count = fixnum_value(vector->length)*4+2;
1214                 break;
1215 #endif
1216
1217 #ifdef type_SimpleArrayComplexLongFloat
1218               case type_SimpleArrayComplexLongFloat:
1219                 vector = (struct vector *)addr;
1220 #ifdef __i386__
1221                 count = fixnum_value(vector->length)*6+2;
1222 #endif
1223 #ifdef sparc
1224                 count = fixnum_value(vector->length)*8+2;
1225 #endif
1226                 break;
1227 #endif
1228
1229               case type_CodeHeader:
1230 #ifndef __i386__
1231                 gc_abort(); /* no code headers in static space */
1232 #else
1233                 count = pscav_code((struct code*)addr);
1234 #endif
1235                 break;
1236
1237               case type_SimpleFunHeader:
1238               case type_ClosureFunHeader:
1239               case type_ReturnPcHeader:
1240                 /* We should never hit any of these, 'cause they occur
1241                  * buried in the middle of code objects. */
1242                 gc_abort();
1243                 break;
1244
1245 #ifdef __i386__
1246               case type_ClosureHeader:
1247               case type_FuncallableInstanceHeader:
1248                 /* The function self pointer needs special care on the
1249                  * x86 because it is the real entry point. */
1250                 {
1251                   lispobj fun = ((struct closure *)addr)->fun
1252                     - FUN_RAW_ADDR_OFFSET;
1253                   pscav(&fun, 1, constant);
1254                   ((struct closure *)addr)->fun = fun + FUN_RAW_ADDR_OFFSET;
1255                 }
1256                 count = 2;
1257                 break;
1258 #endif
1259
1260               case type_WeakPointer:
1261                 /* Weak pointers get preserved during purify, 'cause I
1262                  * don't feel like figuring out how to break them. */
1263                 pscav(addr+1, 2, constant);
1264                 count = 4;
1265                 break;
1266
1267               case type_Fdefn:
1268                 /* We have to handle fdefn objects specially, so we
1269                  * can fix up the raw function address. */
1270                 count = pscav_fdefn((struct fdefn *)addr);
1271                 break;
1272
1273               default:
1274                 count = 1;
1275                 break;
1276             }
1277         }
1278         else {
1279             /* It's a fixnum. */
1280             count = 1;
1281         }
1282
1283         addr += count;
1284         nwords -= count;
1285     }
1286
1287     return addr;
1288 }
1289
1290 int
1291 purify(lispobj static_roots, lispobj read_only_roots)
1292 {
1293     lispobj *clean;
1294     int count, i;
1295     struct later *laters, *next;
1296
1297 #ifdef PRINTNOISE
1298     printf("[doing purification:");
1299     fflush(stdout);
1300 #endif
1301
1302     if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX)) != 0) {
1303         /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
1304          * its error simply by a. printing a string b. to stdout instead
1305          * of stderr. */
1306         printf(" Ack! Can't purify interrupt contexts. ");
1307         fflush(stdout);
1308         return 0;
1309     }
1310
1311 #if defined(__i386__)
1312     dynamic_space_free_pointer =
1313       (lispobj*)SymbolValue(ALLOCATION_POINTER);
1314 #endif
1315
1316     read_only_end = read_only_free =
1317         (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER);
1318     static_end = static_free =
1319         (lispobj *)SymbolValue(STATIC_SPACE_FREE_POINTER);
1320
1321 #ifdef PRINTNOISE
1322     printf(" roots");
1323     fflush(stdout);
1324 #endif
1325
1326 #ifdef GENCGC
1327     gc_assert((lispobj *)CONTROL_STACK_END > ((&read_only_roots)+1));
1328     setup_i386_stack_scav(((&static_roots)-2), (lispobj *)CONTROL_STACK_END);
1329 #endif
1330
1331     pscav(&static_roots, 1, 0);
1332     pscav(&read_only_roots, 1, 1);
1333
1334 #ifdef PRINTNOISE
1335     printf(" handlers");
1336     fflush(stdout);
1337 #endif
1338     pscav((lispobj *) interrupt_handlers,
1339           sizeof(interrupt_handlers) / sizeof(lispobj),
1340           0);
1341
1342 #ifdef PRINTNOISE
1343     printf(" stack");
1344     fflush(stdout);
1345 #endif
1346 #ifndef __i386__
1347     pscav((lispobj *)CONTROL_STACK_START,
1348           current_control_stack_pointer - (lispobj *)CONTROL_STACK_START,
1349           0);
1350 #else
1351 #ifdef GENCGC
1352     pscav_i386_stack();
1353 #endif
1354 #endif
1355
1356 #ifdef PRINTNOISE
1357     printf(" bindings");
1358     fflush(stdout);
1359 #endif
1360 #if !defined(__i386__)
1361     pscav( (lispobj *)BINDING_STACK_START,
1362           (lispobj *)current_binding_stack_pointer - (lispobj *)BINDING_STACK_START,
1363           0);
1364 #else
1365     pscav( (lispobj *)BINDING_STACK_START,
1366           (lispobj *)SymbolValue(BINDING_STACK_POINTER) -
1367           (lispobj *)BINDING_STACK_START,
1368           0);
1369 #endif
1370
1371     /* The original CMU CL code had scavenge-read-only-space code
1372      * controlled by the Lisp-level variable
1373      * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
1374      * wasn't documented under what circumstances it was useful or
1375      * safe to turn it on, so it's been turned off in SBCL. If you
1376      * want/need this functionality, and can test and document it,
1377      * please submit a patch. */
1378 #if 0
1379     if (SymbolValue(SCAVENGE_READ_ONLY_SPACE) != type_UnboundMarker
1380         && SymbolValue(SCAVENGE_READ_ONLY_SPACE) != NIL) {
1381       unsigned  read_only_space_size =
1382           (lispobj *)SymbolValue(READ_ONLY_SPACE_FREE_POINTER) -
1383           (lispobj *)READ_ONLY_SPACE_START;
1384       fprintf(stderr,
1385               "scavenging read only space: %d bytes\n",
1386               read_only_space_size * sizeof(lispobj));
1387       pscav( (lispobj *)READ_ONLY_SPACE_START, read_only_space_size, 0);
1388     }
1389 #endif
1390
1391 #ifdef PRINTNOISE
1392     printf(" static");
1393     fflush(stdout);
1394 #endif
1395     clean = (lispobj *)STATIC_SPACE_START;
1396     do {
1397         while (clean != static_free)
1398             clean = pscav(clean, static_free - clean, 0);
1399         laters = later_blocks;
1400         count = later_count;
1401         later_blocks = NULL;
1402         later_count = 0;
1403         while (laters != NULL) {
1404             for (i = 0; i < count; i++) {
1405                 if (laters->u[i].count == 0) {
1406                     ;
1407                 } else if (laters->u[i].count <= LATERMAXCOUNT) {
1408                     pscav(laters->u[i+1].ptr, laters->u[i].count, 1);
1409                     i++;
1410                 } else {
1411                     pscav(laters->u[i].ptr, 1, 1);
1412                 }
1413             }
1414             next = laters->next;
1415             free(laters);
1416             laters = next;
1417             count = LATERBLOCKSIZE;
1418         }
1419     } while (clean != static_free || later_blocks != NULL);
1420
1421 #ifdef PRINTNOISE
1422     printf(" cleanup");
1423     fflush(stdout);
1424 #endif
1425
1426     os_zero((os_vm_address_t) current_dynamic_space,
1427             (os_vm_size_t) DYNAMIC_SPACE_SIZE);
1428
1429     /* Zero the stack. Note that the stack is also zeroed by SUB-GC
1430      * calling SCRUB-CONTROL-STACK - this zeros the stack on the x86. */
1431 #ifndef __i386__
1432     os_zero((os_vm_address_t) current_control_stack_pointer,
1433             (os_vm_size_t) (CONTROL_STACK_SIZE -
1434                             ((current_control_stack_pointer -
1435                               (lispobj *)CONTROL_STACK_START) *
1436                              sizeof(lispobj))));
1437 #endif
1438
1439     /* It helps to update the heap free pointers so that free_heap can
1440      * verify after it's done. */
1441     SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER, (lispobj)read_only_free);
1442     SetSymbolValue(STATIC_SPACE_FREE_POINTER, (lispobj)static_free);
1443
1444 #if !defined(__i386__)
1445     dynamic_space_free_pointer = current_dynamic_space;
1446 #else
1447 #if defined GENCGC
1448     gc_free_heap();
1449 #else
1450 #error unsupported case /* in CMU CL, was "ibmrt using GC" */
1451 #endif
1452 #endif
1453
1454 #ifdef PRINTNOISE
1455     printf(" done]\n");
1456     fflush(stdout);
1457 #endif
1458
1459     return 0;
1460 }