/********************************************************** * Covers the 4 cases discussed in the text: * - both neighbours are allocated * - the next block is available for coalescing * - the previous block is available for coalescing * - both neighbours are available for coalescing * The coalesce has four conditions based on * the neighbours around it * basically if there is a free neighbour, * we need to modify the header and footer * of the left most neightbour to coalesce the new size. * But as well, since we need to remove the coalesced free * block from the free list and re add the new larger block * onto the free list. **********************************************************/ void *coalesce(void *bp) { size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))); size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp))); size_t size = GET_SIZE(HDRP(bp)); if (bp == NULL) return NULL; if (prev_alloc && next_alloc) { /* Case 1 */ insertFifoFreeBlock(bp); // Insert onto free list return bp; } else if (prev_alloc && !next_alloc) { /* Case 2 */ size += GET_SIZE(HDRP(NEXT_BLKP(bp))); delete_from_free (NEXT_BLKP(bp)); // Remove the right block from the free list PUT(HDRP(bp), PACK(size, 0)); PUT(FTRP(bp), PACK(size, 0)); insertFifoFreeBlock(bp); // Insert onto free list return (bp); } else if (!prev_alloc && next_alloc) { /* Case 3 */ size += GET_SIZE(HDRP(PREV_BLKP(bp))); delete_from_free (PREV_BLKP(bp)); // Remove the left block from the free list PUT(FTRP(bp), PACK(size, 0)); PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); bp = PREV_BLKP(bp); insertFifoFreeBlock(bp); // Insert onto free list return (bp); } else { /* Case 4 */ size += GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(FTRP(NEXT_BLKP(bp))) ; delete_from_free (PREV_BLKP(bp)); // Remove the left block from the free list delete_from_free (NEXT_BLKP(bp)); // Remove the right block from the free list PUT(HDRP(PREV_BLKP(bp)), PACK(size,0)); PUT(FTRP(NEXT_BLKP(bp)), PACK(size,0)); bp = PREV_BLKP(bp); insertFifoFreeBlock(bp); // Insert onto free list return (bp); } }
/********************************************************** * place * Mark the block as allocated **********************************************************/ void place(void* bp, size_t asize, int call_delete) { /* Get the current block size */ size_t bsize = GET_SIZE(HDRP(bp)); if (call_delete == 1) { delete_from_free(bp); // since we are returning // free block to be used // remove the block from the free // list } PUT(HDRP(bp), PACK(bsize, 1)); PUT(FTRP(bp), PACK(bsize, 1)); }
//method for allocating memory for memory block void* getmem(uintptr_t size){ if(size<=0){ return NULL; } uintptr_t actual_size = (size % HEAD_SIZE != 0) ? (size+HEAD_SIZE) + (HEAD_SIZE - (size % HEAD_SIZE)) : (size+HEAD_SIZE); if(free_list == NULL){ return allocateNew(actual_size); } memnode* cur = free_list; // first add header(16 bytes) and then make it a mutiple of 16 while(cur != NULL){ if(cur->size > actual_size){ //check if there is enough block for new request memnode* newMem = (memnode*)((uintptr_t)cur + HEAD_SIZE + cur->size - actual_size); newMem->size = actual_size - HEAD_SIZE; newMem->next = NULL; cur->size = cur->size - actual_size; add_to_allocated_list(newMem); return (void*) ((memnode*) ((uintptr_t)newMem+HEAD_SIZE)); }else if(cur->size == actual_size){ //delete cur from free_list and add it to the AMA delete_from_free(cur); add_to_allocated_list(cur); return (void*) ((memnode*) ((uintptr_t)cur+HEAD_SIZE)); } cur = cur->next; } return allocateNew(actual_size); }