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