static void* alloc_obj (int size, gboolean pinned, gboolean has_references) { int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size); MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, has_references); MSBlockInfo *block; void *obj; /* FIXME: try to do this without locking */ LOCK_MS_BLOCK_LIST; g_assert (!ms_sweep_in_progress); if (!free_blocks [size_index]) { if (G_UNLIKELY (!ms_alloc_block (size_index, pinned, has_references))) { UNLOCK_MS_BLOCK_LIST; return NULL; } } block = free_blocks [size_index]; DEBUG (9, g_assert (block)); obj = block->free_list; DEBUG (9, g_assert (obj)); block->free_list = *(void**)obj; if (!block->free_list) { free_blocks [size_index] = block->next_free; block->next_free = NULL; } UNLOCK_MS_BLOCK_LIST; /* * FIXME: This should not be necessary because it'll be * overwritten by the vtable immediately. */ *(void**)obj = NULL; return obj; }
/* * We're not freeing the block if it's empty. We leave that work for * the next major collection. * * This is just called from the domain clearing code, which runs in a * single thread and has the GC lock, so we don't need an extra lock. */ static void free_object (char *obj, size_t size, gboolean pinned) { MSBlockInfo *block = MS_BLOCK_FOR_OBJ (obj); int word, bit; DEBUG (9, g_assert ((pinned && block->pinned) || (!pinned && !block->pinned))); DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block))); MS_CALC_MARK_BIT (word, bit, obj); DEBUG (9, g_assert (!MS_MARK_BIT (block, word, bit))); if (!block->free_list) { MSBlockInfo **free_blocks = FREE_BLOCKS (pinned, block->has_references); int size_index = MS_BLOCK_OBJ_SIZE_INDEX (size); DEBUG (9, g_assert (!block->next_free)); block->next_free = free_blocks [size_index]; free_blocks [size_index] = block; } memset (obj, 0, size); *(void**)obj = block->free_list; block->free_list = (void**)obj; }
static void major_sweep (void) { int i; #ifdef FIXED_HEAP int j; #else MSBlockInfo **iter; #endif /* clear all the free lists */ for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) { MSBlockInfo **free_blocks = free_block_lists [i]; int j; for (j = 0; j < num_block_obj_sizes; ++j) free_blocks [j] = NULL; } /* traverse all blocks, free and zero unmarked objects */ #ifdef FIXED_HEAP for (j = 0; j < ms_heap_num_blocks; ++j) { MSBlockInfo *block = &block_infos [j]; #else iter = &all_blocks; while (*iter) { MSBlockInfo *block = *iter; #endif int count; gboolean have_live = FALSE; int obj_index; #ifdef FIXED_HEAP if (!block->used) continue; #endif count = MS_BLOCK_FREE / block->obj_size; block->free_list = NULL; for (obj_index = 0; obj_index < count; ++obj_index) { int word, bit; void *obj = MS_BLOCK_OBJ (block, obj_index); MS_CALC_MARK_BIT (word, bit, obj); if (MS_MARK_BIT (block, word, bit)) { DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block))); have_live = TRUE; } else { /* an unmarked object */ if (MS_OBJ_ALLOCED (obj, block)) { binary_protocol_empty (obj, block->obj_size); memset (obj, 0, block->obj_size); } *(void**)obj = block->free_list; block->free_list = obj; } } /* reset mark bits */ memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS); /* * FIXME: reverse free list so that it's in address * order */ if (have_live) { #ifndef FIXED_HEAP iter = &block->next; #endif /* * If there are free slots in the block, add * the block to the corresponding free list. */ if (block->free_list) { MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references); int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size); block->next_free = free_blocks [index]; free_blocks [index] = block; } } else { /* * Blocks without live objects are removed from the * block list and freed. */ #ifdef FIXED_HEAP ms_free_block (block); #else *iter = block->next; ms_free_block (block->block); mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO); #endif --num_major_sections; } } } static int count_pinned_ref; static int count_pinned_nonref; static int count_nonpinned_ref; static int count_nonpinned_nonref; static void count_nonpinned_callback (char *obj, size_t size, void *data) { MonoVTable *vtable = (MonoVTable*)LOAD_VTABLE (obj); if (vtable->klass->has_references) ++count_nonpinned_ref; else ++count_nonpinned_nonref; }
void #ifdef SGEN_PARALLEL_MARK #ifdef FIXED_HEAP mono_sgen_marksweep_fixed_par_init #else mono_sgen_marksweep_par_init #endif #else #ifdef FIXED_HEAP mono_sgen_marksweep_fixed_init #else mono_sgen_marksweep_init #endif #endif (SgenMajorCollector *collector) { int i; #ifndef FIXED_HEAP mono_sgen_register_fixed_internal_mem_type (INTERNAL_MEM_MS_BLOCK_INFO, sizeof (MSBlockInfo)); #endif num_block_obj_sizes = ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, NULL); block_obj_sizes = mono_sgen_alloc_internal_dynamic (sizeof (int) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES); ms_calculate_block_obj_sizes (MS_BLOCK_OBJ_SIZE_FACTOR, block_obj_sizes); /* { int i; g_print ("block object sizes:\n"); for (i = 0; i < num_block_obj_sizes; ++i) g_print ("%d\n", block_obj_sizes [i]); } */ for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) free_block_lists [i] = mono_sgen_alloc_internal_dynamic (sizeof (MSBlockInfo*) * num_block_obj_sizes, INTERNAL_MEM_MS_TABLES); for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES; ++i) fast_block_obj_size_indexes [i] = ms_find_block_obj_size_index (i * 8); for (i = 0; i < MS_NUM_FAST_BLOCK_OBJ_SIZE_INDEXES * 8; ++i) g_assert (MS_BLOCK_OBJ_SIZE_INDEX (i) == ms_find_block_obj_size_index (i)); LOCK_INIT (ms_block_list_mutex); mono_counters_register ("# major blocks allocated", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_alloced); mono_counters_register ("# major blocks freed", MONO_COUNTER_GC | MONO_COUNTER_LONG, &stat_major_blocks_freed); collector->section_size = MAJOR_SECTION_SIZE; #ifdef SGEN_PARALLEL_MARK collector->is_parallel = TRUE; #else collector->is_parallel = FALSE; #endif collector->alloc_heap = major_alloc_heap; collector->is_object_live = major_is_object_live; collector->alloc_small_pinned_obj = major_alloc_small_pinned_obj; collector->alloc_degraded = major_alloc_degraded; collector->copy_or_mark_object = major_copy_or_mark_object; collector->alloc_object = major_alloc_object; collector->free_pinned_object = free_pinned_object; collector->iterate_objects = major_iterate_objects; collector->free_non_pinned_object = major_free_non_pinned_object; collector->find_pin_queue_start_ends = major_find_pin_queue_start_ends; collector->pin_objects = major_pin_objects; collector->init_to_space = major_init_to_space; collector->sweep = major_sweep; collector->check_scan_starts = major_check_scan_starts; collector->dump_heap = major_dump_heap; collector->get_used_size = major_get_used_size; collector->start_nursery_collection = major_start_nursery_collection; collector->finish_nursery_collection = major_finish_nursery_collection; collector->finish_major_collection = major_finish_major_collection; collector->ptr_is_in_non_pinned_space = major_ptr_is_in_non_pinned_space; collector->obj_is_from_pinned_alloc = obj_is_from_pinned_alloc; collector->report_pinned_memory_usage = major_report_pinned_memory_usage; collector->get_num_major_sections = get_num_major_sections; #ifdef FIXED_HEAP collector->handle_gc_param = major_handle_gc_param; collector->print_gc_param_usage = major_print_gc_param_usage; #else collector->handle_gc_param = NULL; collector->print_gc_param_usage = NULL; #endif FILL_COLLECTOR_COPY_OBJECT (collector); FILL_COLLECTOR_SCAN_OBJECT (collector); }
static void ms_sweep (void) { int i; MSBlockInfo **iter; /* statistics for evacuation */ int *slots_available = alloca (sizeof (int) * num_block_obj_sizes); int *slots_used = alloca (sizeof (int) * num_block_obj_sizes); int *num_blocks = alloca (sizeof (int) * num_block_obj_sizes); for (i = 0; i < num_block_obj_sizes; ++i) slots_available [i] = slots_used [i] = num_blocks [i] = 0; /* clear all the free lists */ for (i = 0; i < MS_BLOCK_TYPE_MAX; ++i) { MSBlockInfo **free_blocks = free_block_lists [i]; int j; for (j = 0; j < num_block_obj_sizes; ++j) free_blocks [j] = NULL; } /* traverse all blocks, free and zero unmarked objects */ iter = &all_blocks; while (*iter) { MSBlockInfo *block = *iter; int count; gboolean have_live = FALSE; gboolean has_pinned; int obj_index; int obj_size_index; obj_size_index = block->obj_size_index; has_pinned = block->has_pinned; block->has_pinned = block->pinned; block->is_to_space = FALSE; count = MS_BLOCK_FREE / block->obj_size; block->free_list = NULL; for (obj_index = 0; obj_index < count; ++obj_index) { int word, bit; void *obj = MS_BLOCK_OBJ (block, obj_index); MS_CALC_MARK_BIT (word, bit, obj); if (MS_MARK_BIT (block, word, bit)) { DEBUG (9, g_assert (MS_OBJ_ALLOCED (obj, block))); have_live = TRUE; if (!has_pinned) ++slots_used [obj_size_index]; } else { /* an unmarked object */ if (MS_OBJ_ALLOCED (obj, block)) { binary_protocol_empty (obj, block->obj_size); memset (obj, 0, block->obj_size); } *(void**)obj = block->free_list; block->free_list = obj; } } /* reset mark bits */ memset (block->mark_words, 0, sizeof (mword) * MS_NUM_MARK_WORDS); /* * FIXME: reverse free list so that it's in address * order */ if (have_live) { if (!has_pinned) { ++num_blocks [obj_size_index]; slots_available [obj_size_index] += count; } iter = &block->next; /* * If there are free slots in the block, add * the block to the corresponding free list. */ if (block->free_list) { MSBlockInfo **free_blocks = FREE_BLOCKS (block->pinned, block->has_references); int index = MS_BLOCK_OBJ_SIZE_INDEX (block->obj_size); block->next_free = free_blocks [index]; free_blocks [index] = block; } update_heap_boundaries_for_block (block); } else { /* * Blocks without live objects are removed from the * block list and freed. */ *iter = block->next; #ifdef FIXED_HEAP ms_free_block (block); #else ms_free_block (block->block); mono_sgen_free_internal (block, INTERNAL_MEM_MS_BLOCK_INFO); #endif --num_major_sections; } } for (i = 0; i < num_block_obj_sizes; ++i) { float usage = (float)slots_used [i] / (float)slots_available [i]; if (num_blocks [i] > 5 && usage < evacuation_threshold) { evacuate_block_obj_sizes [i] = TRUE; /* g_print ("slot size %d - %d of %d used\n", block_obj_sizes [i], slots_used [i], slots_available [i]); */ } else { evacuate_block_obj_sizes [i] = FALSE; } } have_swept = TRUE; }