| 
									
										
										
										
											2009-10-07 16:48:09 -04:00
										 |  |  | #include "master.hpp"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace factor | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | inline static code_block_visitor<code_workhorse> make_code_visitor(factor_vm *parent) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	return code_block_visitor<code_workhorse>(parent,code_workhorse(parent)); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-18 21:31:59 -04:00
										 |  |  | full_collector::full_collector(factor_vm *parent_) : | 
					
						
							| 
									
										
										
										
											2009-10-20 23:20:49 -04:00
										 |  |  | 	collector<tenured_space,full_policy>( | 
					
						
							| 
									
										
										
										
											2009-10-18 21:31:59 -04:00
										 |  |  | 		parent_, | 
					
						
							|  |  |  | 		parent_->data->tenured, | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | 		full_policy(parent_)), | 
					
						
							|  |  |  | 	code_visitor(make_code_visitor(parent_)) {} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void full_collector::trace_code_block(code_block *compiled) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2009-11-24 20:29:59 -05:00
										 |  |  | 	data_visitor.visit_code_block_objects(compiled); | 
					
						
							|  |  |  | 	data_visitor.visit_embedded_literals(compiled); | 
					
						
							|  |  |  | 	code_visitor.visit_embedded_code_pointers(compiled); | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void full_collector::trace_context_code_blocks() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	code_visitor.visit_context_code_blocks(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-02 05:28:15 -05:00
										 |  |  | void full_collector::trace_uninitialized_code_blocks() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	code_visitor.visit_uninitialized_code_blocks(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | void full_collector::trace_object_code_block(object *obj) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	code_visitor.visit_object_code_block(obj); | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-07 16:48:09 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | /* After a sweep, invalidate any code heap roots which are not marked,
 | 
					
						
							|  |  |  | so that if a block makes a tail call to a generic word, and the PIC | 
					
						
							|  |  |  | compiler triggers a GC, and the caller block gets gets GCd as a result, | 
					
						
							|  |  |  | the PIC code won't try to overwrite the call site */ | 
					
						
							|  |  |  | void factor_vm::update_code_roots_for_sweep() | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	std::vector<code_root *>::const_iterator iter = code_roots.begin(); | 
					
						
							|  |  |  | 	std::vector<code_root *>::const_iterator end = code_roots.end(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mark_bits<code_block> *state = &code->allocator->state; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for(; iter < end; iter++) | 
					
						
							|  |  |  | 	{ | 
					
						
							|  |  |  | 		code_root *root = *iter; | 
					
						
							| 
									
										
										
										
											2010-01-24 08:17:18 -05:00
										 |  |  | 		code_block *block = (code_block *)(root->value & (~data_alignment - 1)); | 
					
						
							| 
									
										
										
										
											2009-11-02 19:10:34 -05:00
										 |  |  | 		if(root->valid && !state->marked_p(block)) | 
					
						
							|  |  |  | 			root->valid = false; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 15:02:14 -04:00
										 |  |  | void factor_vm::collect_mark_impl(bool trace_contexts_p) | 
					
						
							| 
									
										
										
										
											2009-10-14 03:06:01 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-08 03:10:28 -04:00
										 |  |  | 	full_collector collector(this); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | 	mark_stack.clear(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-20 16:15:05 -04:00
										 |  |  | 	code->clear_mark_bits(); | 
					
						
							| 
									
										
										
										
											2009-10-20 23:20:49 -04:00
										 |  |  | 	data->tenured->clear_mark_bits(); | 
					
						
							| 
									
										
										
										
											2009-10-24 05:36:29 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-08 03:10:28 -04:00
										 |  |  | 	collector.trace_roots(); | 
					
						
							| 
									
										
										
										
											2010-02-06 02:06:26 -05:00
										 |  |  | 	if(trace_contexts_p) | 
					
						
							| 
									
										
										
										
											2009-10-08 03:10:28 -04:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		collector.trace_contexts(); | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | 		collector.trace_context_code_blocks(); | 
					
						
							| 
									
										
										
										
											2009-12-02 05:28:15 -05:00
										 |  |  | 		collector.trace_uninitialized_code_blocks(); | 
					
						
							| 
									
										
										
										
											2009-10-08 03:10:28 -04:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | 	while(!mark_stack.empty()) | 
					
						
							| 
									
										
										
										
											2009-10-24 05:36:29 -04:00
										 |  |  | 	{ | 
					
						
							| 
									
										
										
										
											2009-11-23 19:51:08 -05:00
										 |  |  | 		cell ptr = mark_stack.back(); | 
					
						
							|  |  |  | 		mark_stack.pop_back(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if(ptr & 1) | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			code_block *compiled = (code_block *)(ptr - 1); | 
					
						
							|  |  |  | 			collector.trace_code_block(compiled); | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 		{ | 
					
						
							|  |  |  | 			object *obj = (object *)ptr; | 
					
						
							|  |  |  | 			collector.trace_object(obj); | 
					
						
							|  |  |  | 			collector.trace_object_code_block(obj); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-10-24 05:36:29 -04:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-10-08 03:10:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-21 20:41:54 -04:00
										 |  |  | 	data->reset_generation(data->tenured); | 
					
						
							| 
									
										
										
										
											2009-10-20 14:13:39 -04:00
										 |  |  | 	data->reset_generation(data->aging); | 
					
						
							| 
									
										
										
										
											2009-10-21 20:41:54 -04:00
										 |  |  | 	data->reset_generation(&nursery); | 
					
						
							|  |  |  | 	code->clear_remembered_set(); | 
					
						
							| 
									
										
										
										
											2009-10-14 03:06:01 -04:00
										 |  |  | } | 
					
						
							| 
									
										
										
										
											2009-10-08 03:10:28 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-10-25 15:02:14 -04:00
										 |  |  | void factor_vm::collect_sweep_impl() | 
					
						
							| 
									
										
										
										
											2009-10-25 09:07:21 -04:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2009-10-27 00:57:26 -04:00
										 |  |  | 	current_gc->event->started_data_sweep(); | 
					
						
							| 
									
										
										
										
											2009-11-01 05:40:15 -05:00
										 |  |  | 	data->tenured->sweep(); | 
					
						
							| 
									
										
										
										
											2009-10-27 00:57:26 -04:00
										 |  |  | 	current_gc->event->ended_data_sweep(); | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-22 14:37:39 -05:00
										 |  |  | 	update_code_roots_for_sweep(); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-05 20:03:51 -05:00
										 |  |  | 	current_gc->event->started_code_sweep(); | 
					
						
							|  |  |  | 	code->allocator->sweep(); | 
					
						
							|  |  |  | 	current_gc->event->ended_code_sweep(); | 
					
						
							| 
									
										
										
										
											2009-10-25 09:07:21 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-03 23:25:22 -05:00
										 |  |  | void factor_vm::collect_full(bool trace_contexts_p) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	collect_mark_impl(trace_contexts_p); | 
					
						
							|  |  |  | 	collect_sweep_impl(); | 
					
						
							| 
									
										
										
										
											2009-12-05 19:03:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-11-08 07:08:17 -05:00
										 |  |  | 	if(data->low_memory_p()) | 
					
						
							| 
									
										
										
										
											2009-12-05 19:03:53 -05:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		current_gc->op = collect_growing_heap_op; | 
					
						
							|  |  |  | 		current_gc->event->op = collect_growing_heap_op; | 
					
						
							|  |  |  | 		collect_growing_heap(0,trace_contexts_p); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	else if(data->high_fragmentation_p()) | 
					
						
							| 
									
										
										
										
											2009-11-13 10:29:21 -05:00
										 |  |  | 	{ | 
					
						
							|  |  |  | 		current_gc->op = collect_compact_op; | 
					
						
							|  |  |  | 		current_gc->event->op = collect_compact_op; | 
					
						
							| 
									
										
										
										
											2009-11-03 23:25:22 -05:00
										 |  |  | 		collect_compact_impl(trace_contexts_p); | 
					
						
							| 
									
										
										
										
											2009-11-13 10:29:21 -05:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-12-05 19:03:53 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-12-02 01:48:41 -05:00
										 |  |  | 	code->flush_icache(); | 
					
						
							| 
									
										
										
										
											2009-10-07 16:48:09 -04:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | } |