#define _BSD_SOURCE #include #include #include "dmm_init.h" allocator_t * dmm_init(void) { int i; allocator_t *main_allocator; heap_t *current_heap; MAPTABLE_NODE *maptablenode; main_allocator = (allocator_t *) sbrk((int) sizeof(allocator_t)); for(i = 0; i < NUM_HEAPS; i++) { main_allocator->heaps[i].maptable_head = NULL; main_allocator->heaps[i].free_list_head = NULL; main_allocator->heaps[i].rov_ptr = NULL; main_allocator->heaps[i].num_objects = 0; main_allocator->heaps[i].dmm_stats.num_malloc = 0; main_allocator->heaps[i].dmm_stats.num_free = 0; pthread_mutex_init(&main_allocator->heaps[i].mutex, NULL); } // Custom number of fixed lists and their initialization // 2 first ones with 32, 64, 128 and 256 (4 fixed lists per heap) // 2 last ones with 64 and 256 (2 fixed lists per heap) // 2 * 4 + 2 * 2 = 12 maptable nodes current_heap = &main_allocator->heaps[0]; maptablenode = (MAPTABLE_NODE *) sbrk((int)(12*(sizeof(MAPTABLE_NODE)))); 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; current_heap = &main_allocator->heaps[1]; maptablenode += 4; 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; current_heap = &main_allocator->heaps[2]; maptablenode += 4; maptablenode->size = 64; maptablenode->fixed_list_head = NULL; maptablenode->next = maptablenode+1; current_heap->maptable_head = maptablenode; (maptablenode+1)->size = 256; (maptablenode+1)->fixed_list_head = NULL; (maptablenode+1)->next = NULL; current_heap = &main_allocator->heaps[3]; maptablenode += 2; maptablenode->size = 64; maptablenode->fixed_list_head = NULL; maptablenode->next = maptablenode+1; current_heap->maptable_head = maptablenode; (maptablenode+1)->size = 256; (maptablenode+1)->fixed_list_head = NULL; (maptablenode+1)->next = NULL; return main_allocator; }