/* * Copyright 2011 Institute of Communication and Computer Systems (ICCS) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ #ifndef WITH_MEMORY_SPACE_AWARENESS #include #endif /* WITH_MEMORY_SPACE_AWARENESS */ #include #ifdef HAVE_LOCKS #include "posix_lock.h" #endif /* HAVE_LOCKS */ #include "sys_alloc.h" #if defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT) #include /* for strcmp() */ #include "linked_lists/search_algorithms.h" #endif /* defined (ALLOC_VAR_FIT) || defined (HEAP_VAR_FIT) */ #ifdef WITH_MEMORY_SPACE_AWARENESS void initialize_allocator(allocator_t *allocator, void *starting_address, size_t size) { #else void initialize_allocator(allocator_t *allocator) { #endif /* WITH_MEMORY_SPACE_AWARENESS */ int i; #ifdef WITH_FIXED_LISTS heap_t *current_heap; maptable_node_t *maptablenode; #endif /* WITH_FIXED_LISTS */ #ifdef HAVE_LOCKS sbrk_lock(); #endif /* HAVE_LOCKS */ for(i = 0; i < NUM_HEAPS; i++) { #ifdef HEAP_VAR_FIT if(strcmp(INITIAL_SEARCH_POLICY, "best") == 0) { allocator->heaps[i].dmm_knobs.search_policy = &best_fit_on_freelist; } else if(strcmp(INITIAL_SEARCH_POLICY, "good") == 0) { allocator->heaps[i].dmm_knobs.search_policy = &good_fit_on_freelist; } else if(strcmp(INITIAL_SEARCH_POLICY, "exact") == 0) { allocator->heaps[i].dmm_knobs.search_policy = &exact_fit_on_freelist; } else if(strcmp(INITIAL_SEARCH_POLICY, "first") == 0) { allocator->heaps[i].dmm_knobs.search_policy = &first_fit_on_freelist; } #endif /* HEAP_VAR_FIT */ #ifdef WITH_FIXED_LISTS allocator->heaps[i].maptable_head = NULL; #endif /* WITH_FIXED_LISTS */ allocator->heaps[i].free_list_head = NULL; #ifdef FUTURE_FEATURES allocator->heaps[i].used_blocks_head = NULL; allocator->heaps[i].rov_ptr = NULL; #endif /* FUTURE_FEATURES */ allocator->heaps[i].num_objects = 0; #ifdef WITH_STATS allocator->heaps[i].dmm_stats.mem_allocated = 0; allocator->heaps[i].dmm_stats.mem_requested = 0; allocator->heaps[i].dmm_stats.live_objects = 0; #ifdef COUNT_ACCESSES allocator->heaps[i].dmm_stats.read_mem_accesses = 0; allocator->heaps[i].dmm_stats.write_mem_accesses = 0; #endif /* COUNT_ACCESSES */ allocator->heaps[i].dmm_stats.num_malloc = 0; allocator->heaps[i].dmm_stats.num_free = 0; #endif /* WITH_STATS */ #ifdef WITH_KNOBS /* Knobs initialization */ #ifdef GOOD_FIT allocator->heaps[i].dmm_knobs.fit_percentage = FIT_PERCENTAGE; #endif /* GOOD_FIT */ #ifdef COALESCING_VARIABLE allocator->heaps[i].dmm_knobs.max_coalesce_size = MAX_COALESCE_SIZE; #endif /* COALESCING_VARIABLE */ #ifdef SPLITTING_VARIABLE allocator->heaps[i].dmm_knobs.min_split_size = MIN_SPLITTING_SIZE; #endif /* SPLITTING_VARIABLE */ /* FIXME Create a constant for the initial value of the next * variables */ allocator->heaps[i].dmm_knobs.frag_threshold = 1.0; allocator->heaps[i].dmm_knobs.mem_threshold = 17000; #endif /* WITH_KNOBS */ #ifdef HAVE_LOCKS pthread_mutex_init(&allocator->heaps[i].mutex, NULL); #endif /* HAVE_LOCKS */ } #ifndef WITH_MEMORY_SPACE_AWARENESS allocator->border_ptr = NULL; allocator->remaining_size = 0; #else allocator->border_ptr = starting_address; allocator->remaining_size = size; #endif /* WITH_MEMORY_SPACE_AWARENESS */ allocator->initialized = false; #ifdef ALLOC_VAR_FIT if(strcmp(INITIAL_SEARCH_POLICY, "best") == 0) { allocator->search_policy = &best_fit_on_freelist; } else if(strcmp(INITIAL_SEARCH_POLICY, "good") == 0) { allocator->search_policy = &good_fit_on_freelist; } else if(strcmp(INITIAL_SEARCH_POLICY, "exact") == 0) { allocator->search_policy = &exact_fit_on_freelist; } else if(strcmp(INITIAL_SEARCH_POLICY, "first") == 0) { allocator->search_policy = &first_fit_on_freelist; } #endif /* ALLOC_VAR_FIT */ #ifdef WITH_FIXED_LISTS /* Custom number of fixed lists and their initialization * Fixed lists of 32, 64, 128 and 256 bytes (4 fixed lists per heap) */ current_heap = &allocator->heaps[0]; maptablenode = (maptable_node_t *) sys_alloc(allocator, &allocator->heaps[0], 4*(sizeof(maptable_node_t))); maptablenode->size = 32; maptablenode->fixed_list_head = NULL; maptablenode->next = maptablenode+1; current_heap->maptable_head = maptablenode; (maptablenode+1)->size = 64; (maptablenode+1)->fixed_list_head = NULL; (maptablenode+1)->next = maptablenode+2; (maptablenode+2)->size = 128; (maptablenode+2)->fixed_list_head = NULL; (maptablenode+2)->next = maptablenode+3; (maptablenode+3)->size = 256; (maptablenode+3)->fixed_list_head = NULL; (maptablenode+3)->next = NULL; #endif /* WITH_FIXED_LISTS */ #ifdef HAVE_LOCKS sbrk_unlock(); #endif /* HAVE_LOCKS */ }