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