2 * C-level stuff to implement Lisp-level PURIFY
6 * This software is part of the SBCL system. See the README file for
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.
17 #include <sys/types.h>
27 #include "interrupt.h"
31 #include "gc-internal.h"
33 #include "genesis/gc-tables.h"
34 #include "genesis/primitive-objects.h"
35 #include "genesis/static-symbols.h"
36 #include "genesis/layout.h"
37 #include "genesis/hash-table.h"
40 /* We don't ever do purification with GENCGC as of 1.0.5.*. There was
41 * a lot of hairy and fragile ifdeffage in here to support purify on
42 * x86oids, which has now been removed. So this code can't even be
43 * compiled with GENCGC any more. -- JES, 2007-04-30.
45 #ifndef LISP_FEATURE_GENCGC
49 static lispobj
*dynamic_space_purify_pointer
;
52 /* These hold the original end of the read_only and static spaces so
53 * we can tell what are forwarding pointers. */
55 static lispobj
*read_only_end
, *static_end
;
57 static lispobj
*read_only_free
, *static_free
;
59 static lispobj
*pscav(lispobj
*addr
, long nwords
, boolean constant
);
61 #define LATERBLOCKSIZE 1020
62 #define LATERMAXCOUNT 10
71 } *later_blocks
= NULL
;
72 static long later_count
= 0;
76 forwarding_pointer_p(lispobj obj
)
78 lispobj
*ptr
= native_pointer(obj
);
80 return ((static_end
<= ptr
&& ptr
<= static_free
) ||
81 (read_only_end
<= ptr
&& ptr
<= read_only_free
));
85 dynamic_pointer_p(lispobj ptr
)
87 return (ptr
>= (lispobj
)current_dynamic_space
89 ptr
< (lispobj
)dynamic_space_purify_pointer
);
92 static inline lispobj
*
93 newspace_alloc(long nwords
, int constantp
)
96 gc_assert((nwords
& 1) == 0);
98 if(read_only_free
+ nwords
>= (lispobj
*)READ_ONLY_SPACE_END
) {
99 lose("Ran out of read-only space while purifying!\n");
102 read_only_free
+=nwords
;
104 if(static_free
+ nwords
>= (lispobj
*)STATIC_SPACE_END
) {
105 lose("Ran out of static space while purifying!\n");
115 pscav_later(lispobj
*where
, long count
)
119 if (count
> LATERMAXCOUNT
) {
120 while (count
> LATERMAXCOUNT
) {
121 pscav_later(where
, LATERMAXCOUNT
);
122 count
-= LATERMAXCOUNT
;
123 where
+= LATERMAXCOUNT
;
127 if (later_blocks
== NULL
|| later_count
== LATERBLOCKSIZE
||
128 (later_count
== LATERBLOCKSIZE
-1 && count
> 1)) {
129 new = (struct later
*)malloc(sizeof(struct later
));
130 new->next
= later_blocks
;
131 if (later_blocks
&& later_count
< LATERBLOCKSIZE
)
132 later_blocks
->u
[later_count
].ptr
= NULL
;
138 later_blocks
->u
[later_count
++].count
= count
;
139 later_blocks
->u
[later_count
++].ptr
= where
;
144 ptrans_boxed(lispobj thing
, lispobj header
, boolean constant
)
147 lispobj
*old
= native_pointer(thing
);
148 long nwords
= sizetab
[widetag_of(header
)](old
);
149 lispobj
*new = newspace_alloc(nwords
,constant
);
152 bcopy(old
, new, nwords
* sizeof(lispobj
));
154 /* Deposit forwarding pointer. */
155 lispobj result
= make_lispobj(new, lowtag_of(thing
));
159 pscav(new, nwords
, constant
);
164 /* We need to look at the layout to see whether it is a pure structure
165 * class, and only then can we transport as constant. If it is pure,
166 * we can ALWAYS transport as a constant. */
168 ptrans_instance(lispobj thing
, lispobj header
, boolean
/* ignored */ constant
)
170 lispobj layout
= instance_layout(native_pointer(thing
));
171 lispobj pure
= LAYOUT(layout
)->pure
;
175 return (ptrans_boxed(thing
, header
, 1));
177 return (ptrans_boxed(thing
, header
, 0));
180 return NIL
; /* dummy value: return something ... */
185 ptrans_fdefn(lispobj thing
, lispobj header
)
188 lispobj
*old
= native_pointer(thing
);
189 long nwords
= sizetab
[widetag_of(header
)](old
);
190 lispobj
*new = newspace_alloc(nwords
, 0); /* inconstant */
193 bcopy(old
, new, nwords
* sizeof(lispobj
));
195 /* Deposit forwarding pointer. */
196 lispobj result
= make_lispobj(new, lowtag_of(thing
));
199 /* Scavenge the function. */
200 struct fdefn
*fdefn
= (struct fdefn
*)new;
201 lispobj oldfn
= fdefn
->fun
;
202 pscav(&fdefn
->fun
, 1, 0);
203 if ((char *)oldfn
+ FUN_RAW_ADDR_OFFSET
== fdefn
->raw_addr
)
204 fdefn
->raw_addr
= (char *)fdefn
->fun
+ FUN_RAW_ADDR_OFFSET
;
210 ptrans_unboxed(lispobj thing
, lispobj header
)
213 lispobj
*old
= native_pointer(thing
);
214 long nwords
= sizetab
[widetag_of(header
)](old
);
215 lispobj
*new = newspace_alloc(nwords
, 1); /* always constant */
218 bcopy(old
, new, nwords
* sizeof(lispobj
));
220 /* Deposit forwarding pointer. */
221 lispobj result
= make_lispobj(new, lowtag_of(thing
));
228 ptrans_vector(lispobj thing
, boolean boxed
, boolean constant
)
230 struct vector
*vector
= VECTOR(thing
);
231 long nwords
= sizetab
[widetag_of(vector
->header
)]((lispobj
*)vector
);
233 lispobj
*new = newspace_alloc(nwords
, (constant
|| !boxed
));
234 bcopy(vector
, new, nwords
* sizeof(lispobj
));
236 lispobj result
= make_lispobj(new, lowtag_of(thing
));
237 vector
->header
= result
;
240 pscav(new, nwords
, constant
);
246 ptrans_code(lispobj thing
)
248 struct code
*code
= (struct code
*)native_pointer(thing
);
249 long nwords
= code_header_words(code
->header
)
250 + code_instruction_words(code
->code_size
);
252 struct code
*new = (struct code
*)newspace_alloc(nwords
,1); /* constant */
254 bcopy(code
, new, nwords
* sizeof(lispobj
));
256 lispobj result
= make_lispobj(new, OTHER_POINTER_LOWTAG
);
258 /* Put in forwarding pointers for all the functions. */
259 uword_t displacement
= result
- thing
;
260 for_each_simple_fun(i
, newfunc
, new, 1, {
261 lispobj
* old
= (lispobj
*)LOW_WORD((char*)newfunc
- displacement
);
262 *old
= make_lispobj(newfunc
, FUN_POINTER_LOWTAG
);
265 /* Stick in a forwarding pointer for the code object. */
266 /* This smashes the header, so do it only after reading n_funs */
267 *(lispobj
*)code
= result
;
269 /* Arrange to scavenge the debug info later. */
270 pscav_later(&new->debug_info
, 1);
272 /* Scavenge the constants. */
273 pscav(new->constants
,
274 code_header_words(new->header
) - (offsetof(struct code
, constants
) >> WORD_SHIFT
),
277 /* Scavenge all the functions. */
278 for_each_simple_fun(i
, func
, new, 1, {
279 gc_assert(!dynamic_pointer_p((lispobj
)func
));
280 pscav(&func
->self
, 1, 1);
281 pscav_later(&func
->name
, 4);
288 ptrans_func(lispobj thing
, lispobj header
)
290 /* Thing can either be a function header,
291 * a closure, or a funcallable-instance. If it's a closure
292 * or a funcallable-instance, we do the same as ptrans_boxed.
293 * Otherwise we have to do something strange, 'cause it is buried
294 * inside a code object. */
296 if (widetag_of(header
) == SIMPLE_FUN_WIDETAG
) {
298 /* We can only end up here if the code object has not been
299 * scavenged, because if it had been scavenged, forwarding pointers
300 * would have been left behind for all the entry points. */
302 struct simple_fun
*function
= (struct simple_fun
*)native_pointer(thing
);
303 lispobj code
= make_lispobj(native_pointer(thing
) - HeaderValue(function
->header
),
304 OTHER_POINTER_LOWTAG
);
306 /* This will cause the function's header to be replaced with a
307 * forwarding pointer. */
311 /* So we can just return that. */
312 return function
->header
;
314 /* It's some kind of closure-like thing. */
315 lispobj
*old
= native_pointer(thing
);
316 long nwords
= sizetab
[widetag_of(header
)](old
);
318 /* Allocate the new one. FINs *must* not go in read_only
319 * space. Closures can; they never change */
321 lispobj
*new = newspace_alloc
322 (nwords
,(widetag_of(header
)!=FUNCALLABLE_INSTANCE_WIDETAG
));
325 bcopy(old
, new, nwords
* sizeof(lispobj
));
327 /* Deposit forwarding pointer. */
328 lispobj result
= make_lispobj(new, lowtag_of(thing
));
332 pscav(new, nwords
, 0);
339 ptrans_returnpc(lispobj thing
, lispobj header
)
341 /* Find the corresponding code object. */
342 lispobj code
= thing
- HeaderValue(header
)*sizeof(lispobj
);
344 /* Make sure it's been transported. */
345 lispobj
new = *native_pointer(code
);
346 if (!forwarding_pointer_p(new))
347 new = ptrans_code(code
);
349 /* Maintain the offset: */
350 return new + (thing
- code
);
353 #define WORDS_PER_CONS CEILING(sizeof(struct cons) / sizeof(lispobj), 2)
356 ptrans_list(lispobj thing
, boolean constant
)
358 struct cons
*old
, *new, *orig
;
361 orig
= (struct cons
*) newspace_alloc(0,constant
);
365 /* Allocate a new cons cell. */
366 old
= (struct cons
*)native_pointer(thing
);
367 new = (struct cons
*) newspace_alloc(WORDS_PER_CONS
,constant
);
369 /* Copy the cons cell and keep a pointer to the cdr. */
371 thing
= new->cdr
= old
->cdr
;
373 /* Set up the forwarding pointer. */
374 *(lispobj
*)old
= make_lispobj(new, LIST_POINTER_LOWTAG
);
376 /* And count this cell. */
378 } while (lowtag_of(thing
) == LIST_POINTER_LOWTAG
&&
379 dynamic_pointer_p(thing
) &&
380 !(forwarding_pointer_p(*native_pointer(thing
))));
382 /* Scavenge the list we just copied. */
383 pscav((lispobj
*)orig
, length
* WORDS_PER_CONS
, constant
);
385 return make_lispobj(orig
, LIST_POINTER_LOWTAG
);
389 ptrans_otherptr(lispobj thing
, lispobj header
, boolean constant
)
391 int widetag
= widetag_of(header
);
393 /* FIXME: this needs a reindent */
395 case SINGLE_FLOAT_WIDETAG
:
396 case DOUBLE_FLOAT_WIDETAG
:
397 #ifdef LONG_FLOAT_WIDETAG
398 case LONG_FLOAT_WIDETAG
:
400 #ifdef COMPLEX_SINGLE_FLOAT_WIDETAG
401 case COMPLEX_SINGLE_FLOAT_WIDETAG
:
403 #ifdef COMPLEX_DOUBLE_FLOAT_WIDETAG
404 case COMPLEX_DOUBLE_FLOAT_WIDETAG
:
406 #ifdef COMPLEX_LONG_FLOAT_WIDETAG
407 case COMPLEX_LONG_FLOAT_WIDETAG
:
410 return ptrans_unboxed(thing
, header
);
412 case COMPLEX_WIDETAG
:
413 case SIMPLE_ARRAY_WIDETAG
:
414 case COMPLEX_BASE_STRING_WIDETAG
:
415 #ifdef COMPLEX_CHARACTER_STRING_WIDETAG
416 case COMPLEX_CHARACTER_STRING_WIDETAG
:
418 case COMPLEX_BIT_VECTOR_WIDETAG
:
419 case COMPLEX_VECTOR_NIL_WIDETAG
:
420 case COMPLEX_VECTOR_WIDETAG
:
421 case COMPLEX_ARRAY_WIDETAG
:
422 return ptrans_boxed(thing
, header
, constant
);
424 case VALUE_CELL_WIDETAG
:
425 case WEAK_POINTER_WIDETAG
:
426 return ptrans_boxed(thing
, header
, 0);
429 return ptrans_boxed(thing
, header
, 0);
431 case SIMPLE_VECTOR_WIDETAG
:
432 return ptrans_vector(thing
, 1, constant
);
434 case CODE_HEADER_WIDETAG
:
435 return ptrans_code(thing
);
437 case RETURN_PC_WIDETAG
:
438 return ptrans_returnpc(thing
, header
);
441 return ptrans_fdefn(thing
, header
);
444 if (other_immediate_lowtag_p(widetag
) &&
445 specialized_vector_widetag_p(widetag
))
446 return ptrans_vector(thing
, 0, constant
);
447 fprintf(stderr
, "Invalid widetag: %d\n", widetag_of(header
));
448 /* Should only come across other pointers to the above stuff. */
455 pscav_fdefn(struct fdefn
*fdefn
)
459 fix_func
= ((char *)(fdefn
->fun
+FUN_RAW_ADDR_OFFSET
) == fdefn
->raw_addr
);
460 pscav(&fdefn
->name
, 1, 1);
461 pscav(&fdefn
->fun
, 1, 0);
463 fdefn
->raw_addr
= (char *)(fdefn
->fun
+ FUN_RAW_ADDR_OFFSET
);
464 return sizeof(struct fdefn
) / sizeof(lispobj
);
468 pscav(lispobj
*addr
, long nwords
, boolean constant
)
470 lispobj thing
, *thingp
, header
;
471 long count
= 0; /* (0 = dummy init value to stop GCC warning) */
475 int widetag
= widetag_of(thing
);
476 if (is_lisp_pointer(thing
)) {
477 /* It's a pointer. Is it something we might have to move? */
478 if (dynamic_pointer_p(thing
)) {
479 /* Maybe. Have we already moved it? */
480 thingp
= native_pointer(thing
);
482 if (is_lisp_pointer(header
) && forwarding_pointer_p(header
))
483 /* Yep, so just copy the forwarding pointer. */
486 /* Nope, copy the object. */
487 switch (lowtag_of(thing
)) {
488 case FUN_POINTER_LOWTAG
:
489 thing
= ptrans_func(thing
, header
);
492 case LIST_POINTER_LOWTAG
:
493 thing
= ptrans_list(thing
, constant
);
496 case INSTANCE_POINTER_LOWTAG
:
497 thing
= ptrans_instance(thing
, header
, constant
);
500 case OTHER_POINTER_LOWTAG
:
501 thing
= ptrans_otherptr(thing
, header
, constant
);
505 /* It was a pointer, but not one of them? */
513 #if N_WORD_BITS == 64
514 else if (widetag
== SINGLE_FLOAT_WIDETAG
) {
518 else if (thing
& FIXNUM_TAG_MASK
) {
519 /* It's an other immediate. Maybe the header for an unboxed */
523 case SINGLE_FLOAT_WIDETAG
:
524 case DOUBLE_FLOAT_WIDETAG
:
525 #ifdef LONG_FLOAT_WIDETAG
526 case LONG_FLOAT_WIDETAG
:
529 /* It's an unboxed simple object. */
530 count
= CEILING(HeaderValue(thing
)+1, 2);
533 case SIMPLE_VECTOR_WIDETAG
:
534 if (HeaderValue(thing
) == subtype_VectorValidHashing
) {
535 struct hash_table
*hash_table
=
536 (struct hash_table
*)native_pointer(addr
[2]);
537 hash_table
->needs_rehash_p
= T
;
542 case CODE_HEADER_WIDETAG
:
543 gc_abort(); /* no code headers in static space */
546 case SIMPLE_FUN_WIDETAG
:
547 case RETURN_PC_WIDETAG
:
548 /* We should never hit any of these, 'cause they occur
549 * buried in the middle of code objects. */
553 case WEAK_POINTER_WIDETAG
:
554 /* Weak pointers get preserved during purify, 'cause I
555 * don't feel like figuring out how to break them. */
556 pscav(addr
+1, 2, constant
);
557 count
= WEAK_POINTER_NWORDS
;
561 /* We have to handle fdefn objects specially, so we
562 * can fix up the raw function address. */
563 count
= pscav_fdefn((struct fdefn
*)addr
);
566 case INSTANCE_WIDETAG
:
568 lispobj lbitmap
= LAYOUT(instance_layout(addr
))->bitmap
;
569 lispobj
* slots
= addr
+ 1;
570 long nslots
= instance_length(*addr
) | 1;
572 if (fixnump(lbitmap
)) {
573 sword_t bitmap
= (sword_t
)lbitmap
>> N_FIXNUM_TAG_BITS
;
574 for (index
= 0; index
< nslots
; index
++, bitmap
>>= 1)
576 pscav(slots
+ index
, 1, constant
);
578 struct bignum
* bitmap
;
579 bitmap
= (struct bignum
*)native_pointer(lbitmap
);
580 for (index
= 0; index
< nslots
; index
++)
581 if (positive_bignum_logbitp(index
, bitmap
))
582 pscav(slots
+ index
, 1, constant
);
589 if (other_immediate_lowtag_p(widetag
) &&
590 specialized_vector_widetag_p(widetag
))
591 count
= sizetab
[widetag_of(thing
)](addr
);
610 purify(lispobj static_roots
, lispobj read_only_roots
)
614 struct later
*laters
, *next
;
615 struct thread
*thread
;
617 if(all_threads
->next
) {
618 /* FIXME: there should be _some_ sensible error reporting
619 * convention. See following comment too */
620 fprintf(stderr
,"Can't purify when more than one thread exists\n");
626 printf("[doing purification:");
630 for_each_thread(thread
)
631 if (fixnum_value(SymbolValue(FREE_INTERRUPT_CONTEXT_INDEX
,thread
)) != 0) {
632 /* FIXME: 1. What does this mean? 2. It shouldn't be reporting
633 * its error simply by a. printing a string b. to stdout instead
635 printf(" Ack! Can't purify interrupt contexts. ");
640 dynamic_space_purify_pointer
= dynamic_space_free_pointer
;
642 read_only_end
= read_only_free
=
643 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
,0);
644 static_end
= static_free
=
645 (lispobj
*)SymbolValue(STATIC_SPACE_FREE_POINTER
,0);
652 pscav(&static_roots
, 1, 0);
653 pscav(&read_only_roots
, 1, 1);
659 pscav((lispobj
*) interrupt_handlers
,
660 sizeof(interrupt_handlers
) / sizeof(lispobj
),
667 pscav((lispobj
*)all_threads
->control_stack_start
,
668 access_control_stack_pointer(all_threads
) -
669 all_threads
->control_stack_start
,
677 pscav( (lispobj
*)all_threads
->binding_stack_start
,
678 (lispobj
*)get_binding_stack_pointer(all_threads
) -
679 all_threads
->binding_stack_start
,
682 /* The original CMU CL code had scavenge-read-only-space code
683 * controlled by the Lisp-level variable
684 * *SCAVENGE-READ-ONLY-SPACE*. It was disabled by default, and it
685 * wasn't documented under what circumstances it was useful or
686 * safe to turn it on, so it's been turned off in SBCL. If you
687 * want/need this functionality, and can test and document it,
688 * please submit a patch. */
690 if (SymbolValue(SCAVENGE_READ_ONLY_SPACE
) != UNBOUND_MARKER_WIDETAG
691 && SymbolValue(SCAVENGE_READ_ONLY_SPACE
) != NIL
) {
692 unsigned read_only_space_size
=
693 (lispobj
*)SymbolValue(READ_ONLY_SPACE_FREE_POINTER
) -
694 (lispobj
*)READ_ONLY_SPACE_START
;
696 "scavenging read only space: %d bytes\n",
697 read_only_space_size
* sizeof(lispobj
));
698 pscav( (lispobj
*)READ_ONLY_SPACE_START
, read_only_space_size
, 0);
706 clean
= (lispobj
*)STATIC_SPACE_START
;
708 while (clean
!= static_free
)
709 clean
= pscav(clean
, static_free
- clean
, 0);
710 laters
= later_blocks
;
714 while (laters
!= NULL
) {
715 for (i
= 0; i
< count
; i
++) {
716 if (laters
->u
[i
].count
== 0) {
718 } else if (laters
->u
[i
].count
<= LATERMAXCOUNT
) {
719 pscav(laters
->u
[i
+1].ptr
, laters
->u
[i
].count
, 1);
722 pscav(laters
->u
[i
].ptr
, 1, 1);
728 count
= LATERBLOCKSIZE
;
730 } while (clean
!= static_free
|| later_blocks
!= NULL
);
736 #ifdef LISP_FEATURE_HPUX
737 clear_auto_gc_trigger(); /* restore mmap as it was given by os */
740 os_zero((os_vm_address_t
) current_dynamic_space
, dynamic_space_size
);
742 /* Zero the stack. */
743 os_zero((os_vm_address_t
) access_control_stack_pointer(all_threads
),
745 ((all_threads
->control_stack_end
-
746 access_control_stack_pointer(all_threads
)) * sizeof(lispobj
)));
748 /* It helps to update the heap free pointers so that free_heap can
749 * verify after it's done. */
750 SetSymbolValue(READ_ONLY_SPACE_FREE_POINTER
, (lispobj
)read_only_free
,0);
751 SetSymbolValue(STATIC_SPACE_FREE_POINTER
, (lispobj
)static_free
,0);
753 dynamic_space_free_pointer
= current_dynamic_space
;
754 set_auto_gc_trigger(bytes_consed_between_gcs
);
756 /* Blast away instruction cache */
757 os_flush_icache((os_vm_address_t
)READ_ONLY_SPACE_START
, READ_ONLY_SPACE_SIZE
);
758 os_flush_icache((os_vm_address_t
)STATIC_SPACE_START
, STATIC_SPACE_SIZE
);
766 #else /* LISP_FEATURE_GENCGC */
768 purify(lispobj static_roots
, lispobj read_only_roots
)
770 lose("purify called for GENCGC. This should not happen.");
772 #endif /* LISP_FEATURE_GENCGC */