#include "custom_malloc.h" #include "posix_lock.h" #include "other.h" #include "sys_alloc.h" void * custom_malloc(heap_t* heap, size_t size) { leaHdr *ptr; int fixed_list_id, i, found; MAPTABLE_NODE *current_maptable_node; leaHdr *current_node; ptr = NULL; posix_lock(heap); fixed_list_id = map_size_to_list(heap, size); // If a fixed list is found, do a first fit if(fixed_list_id != -1) { current_maptable_node = heap->maptable_head; // traverse through the maptable node list if(fixed_list_id != 0) { for(i = 1; i < fixed_list_id; i++) { current_maptable_node = current_maptable_node->next; } } if(current_maptable_node->fixed_list_head != NULL) { ptr = current_maptable_node->fixed_list_head; current_maptable_node->fixed_list_head = ptr->next; set_requested_size(ptr, size); markInUse(ptr); } } if(ptr == NULL) { found = 0; // first fit from free list current_node = heap->free_list_head; if(current_node != NULL && getSize(current_node) >= size) { ptr = current_node; heap->free_list_head = ptr->next; set_requested_size(ptr, size); markInUse(ptr); } while(current_node && !found) { if(getSize(current_node->next) >= size) { ptr = current_node->next; current_node->next = ptr->next; set_requested_size(ptr, size); markInUse(ptr); found = 1; } current_node = current_node->next; } if(!found) { posix_unlock(heap); return sys_alloc(size); } } posix_unlock(heap); return ptr; }