Exemplo n.º 1
0
/**
 * coalesce - boundary tag coalescing. Return ptr to coalesced block
 * Removes adjacent blocks from the free list if either one or both are free.
 * Merges block bp with these free adjacent blocks and inserts it onto list.
 * @return - bp - pointer to the merged block 
 */
static void *coalesce(void *bp){
	size_t prev_alloc;
	prev_alloc = GET_FALLOC(PREV_BLK(bp)) || PREV_BLK(bp) == bp;
	size_t next_alloc = GET_HALLOC(NEXT_BLK(bp));
	size_t size = GET_HSIZE(bp);
	
	/* Case 1 next block is free */		
	if (prev_alloc && !next_alloc) {									
		size += GET_HSIZE(NEXT_BLK(bp));
		mm_remove(NEXT_BLK(bp));
		SET_HDRP(bp, PACK(size, 0));
		SET_FTRP(bp, PACK(size, 0));
	}/* Case 2 prev block is free */
	else if (!prev_alloc && next_alloc) {								
		size += GET_HSIZE(PREV_BLK(bp));
		bp = PREV_BLK(bp);
		mm_remove(bp);
		SET_HDRP(bp, PACK(size, 0));
		SET_FTRP(bp, PACK(size, 0));
	}/* Case 3 both blocks are free */ 
	else if (!prev_alloc && !next_alloc) {								
		size += GET_HSIZE(PREV_BLK(bp)) + GET_HSIZE(NEXT_BLK(bp));
		mm_remove(PREV_BLK(bp));
		mm_remove(NEXT_BLK(bp));
		bp = PREV_BLK(bp);
		SET_HDRP(bp, PACK(size, 0));
		SET_FTRP(bp, PACK(size, 0));
	}/* lastly insert bp into free list and return bp */
	mm_insert(bp);
	return bp;
}
Exemplo n.º 2
0
/**
 * Return a block of size >= newsize
 * that preserves all the data from the
 * payload of the block bp.
 * @param - a pointer to an allocated block
 * @param - size - a new size requested
 * @return - pointer to a block size >= newsize
 */
void *mm_realloc(void *bp, size_t size){
	if(size <= 0){ 
		mm_free(bp); 
		return NULL; 
	}else if(bp == NULL){
		bp = mm_malloc(size);
		return bp;
	}
	
	if(size > 0){ 
		size_t currentsize = GET_HSIZE(bp); 
		size_t newsize = ALIGN(size + OVERHEAD); 
		/* newsize is less than currentsize just return bp */
		if(newsize <= currentsize){  
			return bp; 
		} /* newsize is greater than currentsize */ 
		else { 
			size_t next_alloc = GET_HALLOC(NEXT_BLK(bp)); 
			size_t csize;
			size_t asize;			
			/* next block is free and the size of the two blocks is greater than or equal the new size  */ 
			if(!next_alloc && ((csize = currentsize + GET_HSIZE(NEXT_BLK(bp)))) >= newsize){ 
				mm_remove(NEXT_BLK(bp)); 
				SET_HDRP(bp, PACK(csize, 1)); 
				SET_FTRP(bp, PACK(csize, 1)); 
				return bp; 
			} /* next block is free and the block is the last block before the epilogue */
			else if(!next_alloc && ((GET_HSIZE(NEXT_BLK(NEXT_BLK(bp)))) == 0)){
				csize = newsize - currentsize + GET_HSIZE(NEXT_BLK(bp));
				void *temp = extend_heap(csize);
				asize = currentsize + GET_HSIZE(temp);
				SET_HDRP(bp, PACK(asize, 1));
				SET_FTRP(bp, PACK(asize, 1));
				return bp; 
			} /* if bp is the last block before epilogue */
			else if(GET_HSIZE(NEXT_BLK(bp)) == 0){
				csize = newsize - currentsize;
				void *temp = extend_heap(csize);
				asize = currentsize + GET_HSIZE(temp);
				SET_HDRP(bp, PACK(asize, 1));
				SET_FTRP(bp, PACK(asize, 1));
				return bp;
			} /* last ditch attemp try to extend heap for additional size */
			else {  
				void *newbp = mm_malloc(newsize);  
				place(newbp, newsize);
				memcpy(newbp, bp, newsize); 
				mm_free(bp); 
				return newbp; 
			} 
		} /* return NULL */
	}else{ 
		return NULL;
	}	
} 
Exemplo n.º 3
0
/**
 * Place block of asize bytes at start of free block bp
 * and split if remainder would be at least minimum block size
 * @param - bp - pointer to a free block, not on the tree of free blocks
 * @param - asize - the requested size of free block
 */
static void place(void *bp, size_t asize){
	size_t csize = GET_HSIZE(bp);

	if ((csize - asize) >= BLKSIZE) {
		SET_HDRP(bp, PACK(asize, 1));
		SET_FTRP(bp, PACK(asize, 1));
		mm_remove(bp);
		bp = NEXT_BLK(bp);
		SET_HDRP(bp, PACK(csize-asize, 0));
		SET_FTRP(bp, PACK(csize-asize, 0));
		coalesce(bp);
	}
	else {
		SET_HDRP(bp, PACK(csize, 1));
		SET_FTRP(bp, PACK(csize, 1));
		mm_remove(bp);
	}
}
Exemplo n.º 4
0
/**
 * Marks this block as free. Calls
 * coalesce to merge with adjacent free blocks
 * if any, then inserts the returned (possibly larger)
 * free block into the tree of free blocks
 * @param - pointer to a block previously allocated
 */
void mm_free(void *bp){
	/* just return if the pointer is NULL */
	if(!bp) return; 
	size_t size = GET_HSIZE(bp);

	/* set this block back to free and coalese */
	SET_HDRP(bp, PACK(size, 0)); 
	SET_FTRP(bp, PACK(size, 0));
	coalesce(bp); 
}
Exemplo n.º 5
0
static void *extend_heap(size_t words) {
    char *bp;
    size_t size;

    /* Allocate an even number of words to maintain alignment */
    size = (words % 2) ? (words + 1) * WSIZE : words * WSIZE;


    /* call for more memory space */
    if ((bp = mem_sbrk(size)) == (void *)-1)
        return (NULL);

    /* Initialize free block header/footer and the epilogue header */
    SET_HDRP(bp, PACK(size, 0));         /* free block header */
    SET_FTRP(bp, PACK(size, 0));         /* free block footer */
    SET_HDRP(NEXT_BLKP(bp), PACK(0, 1)); /* new epilogue header */

    /* coalesce bp with next and previous blocks */
    return coalesce(bp);
}
Exemplo n.º 6
0
void mm_free(void *bp){
    size_t size;
    /* just return if the pointer is NULL */
    if (bp == NULL)
        return;


    size = GET_SIZE(bp);

    // Then we may coalese the block
    SET_HDRP(bp, PACK(size, 0));
    SET_FTRP(bp, PACK(size, 0));
    coalesce(bp);
}
Exemplo n.º 7
0
/*
  coalesce - boundary tag coalescing. Return ptr to coalesced block
  Removes adjacent blocks from the free list if either one or both are free.
  Merges block bp with these free adjacent blocks and inserts it onto list.

 */
static void *coalesce(void *bp){
    size_t prev_alloc;
    prev_alloc = GET_ALLOC(PREV_BLKP(bp)) || PREV_BLKP(bp) == bp;
    size_t next_alloc = GET_ALLOC(NEXT_BLKP(bp));
    size_t size = GET_SIZE(bp);
    if (prev_alloc && next_alloc) {                 /* when both are allocated */
        //do nothing
    }/* when prev block is free */
    else if (!prev_alloc && next_alloc) {
        size += GET_SIZE(PREV_BLKP(bp));
        bp = PREV_BLKP(bp);
        mm_delete(bp);
        SET_HDRP(bp, PACK(size, 0));
        SET_FTRP(bp, PACK(size, 0));
    }

    /* when next block is free */
    else if (prev_alloc && !next_alloc) {
        size += GET_SIZE(NEXT_BLKP(bp));
        mm_delete(NEXT_BLKP(bp));
        SET_HDRP(bp, PACK(size, 0));
        SET_FTRP(bp, PACK(size, 0));
    }/* when both blocks are free */
    else if (!prev_alloc && !next_alloc) {
        size += GET_SIZE(PREV_BLKP(bp)) + GET_SIZE(NEXT_BLKP(bp));
        mm_delete(PREV_BLKP(bp));
        mm_delete(NEXT_BLKP(bp));
        bp = PREV_BLKP(bp);
        SET_HDRP(bp, PACK(size, 0));
        SET_FTRP(bp, PACK(size, 0));
    }/* lastly insert bp into free list and return bp */
    mm_add(bp);

    //if ((ro > (char *)bp) && (ro < NEXT_BLKP(bp)))
    //ro = bp;
    return bp;
}
Exemplo n.º 8
0
void *
mm_realloc(void *ptr, size_t size){
    if(size <= 0){
        mm_free(ptr);
        return NULL;
    }

    if(ptr == NULL){
        ptr = mm_malloc(size);
        return ptr;
    }

    if(size > 0){
        size_t currentsize = GET_SIZE(ptr);
        size_t newsize = ALIGN(size + OVERHEAD);

        if(newsize <= currentsize){

            /*void *newbp ;
            if ((currentsize - newsize) >= BLKSIZE) {
                SET_HDRP(ptr, PACK(newsize, 1));
                SET_FTRP(ptr, PACK(newsize, 1));
                newbp=ptr;
                mm_remove(ptr);
                ptr = NEXT_BLKP(ptr);
                SET_HDRP(ptr, PACK(currentsize-newsize, 0));
                SET_FTRP(ptr, PACK(currentsize-newsize, 0));
                coalesce(ptr);
                return newbp;
            }
            else {*/
                //return ptr;
            //}
            return ptr;

        } /* Defining the code if new size is greater than the current */
        else {
            size_t next_alloc = GET_ALLOC(NEXT_BLKP(ptr));
            size_t csize;
            size_t asize;
            /* next block is free and the size of the two blocks is greater than or equal the new size  */

            if(!next_alloc && ((csize = currentsize + GET_SIZE(NEXT_BLKP(ptr)))) >= newsize){
                mm_delete(NEXT_BLKP(ptr));
                SET_HDRP(ptr, PACK(csize, 1));
                SET_FTRP(ptr, PACK(csize, 1));
                return ptr;
            }
            /* if bp is the last block before epilogue */
            else if(GET_SIZE(NEXT_BLKP(ptr)) == 0){
                csize = newsize - currentsize;
                void *temp = extend_heap(csize);
                asize = currentsize + GET_SIZE(temp);
                SET_HDRP(ptr, PACK(asize, 1));
                SET_FTRP(ptr, PACK(asize, 1));
                return ptr;
            }
            /* next block is free and the block is the last block before the epilogue */

            else if(!next_alloc && ((GET_SIZE(NEXT_BLKP(NEXT_BLKP(ptr)))) == 0)){
                csize = newsize - currentsize + GET_SIZE(NEXT_BLKP(ptr));
                void *temp = extend_heap(csize);
                asize = currentsize + GET_SIZE(temp);
                SET_HDRP(ptr, PACK(asize, 1));
                SET_FTRP(ptr, PACK(asize, 1));
                return ptr;
            }

           /* otherwise there is no choice left instead to increase the heap size */

            else {
                void *newbp = mm_malloc(newsize);
                place(newbp, newsize);
                memcpy(newbp, ptr, newsize);
                mm_free(ptr);
                return newbp;
            }
        }
    }else{
        return NULL;
    }
}