Beispiel #1
0
/*
 * place
 */
static void place(void *bp, size_t asize){  
	size_t csize = GET_SIZE(HDRP(bp));
	
	if ((csize - asize) >= (2*DSIZE)) {  
		delete_free_block(bp);
		PUT(HDRP(bp), PACK(asize, 1));
		PUT(FTRP(bp), PACK(asize, 1));
		
		PUT(NEXT_BLKP(bp), PREV_F_BLKP(bp));
		PUT(NEXT_BLKP(bp) + WSIZE, NEXT_F_BLKP(bp));
		
		dbg_printf("place: %d to %d\n",(int)(HDRP(bp)-heap_listp),(int)(FTRP(bp)-heap_listp+WSIZE));
		
		bp = NEXT_BLKP(bp);
		PUT(HDRP(bp), PACK(csize - asize, 0));
		PUT(FTRP(bp), PACK(csize - asize, 0));
		
		insert_free_block(bp);
	}
	else{
		PUT(HDRP(bp), PACK(csize, 1));
		PUT(FTRP(bp), PACK(csize, 1));
		delete_free_block(bp);
	}
}
static void place(void *bp, size_t asize)   {
	//dbg_printf("place%p\n",bp);
    size_t csize = GET_SIZE(HDRP(bp));   
//	void *remain_blk;
size_t remain_size=csize-asize;
    if ((remain_size) >= 16) {
	void *remain_blk; 
		delete_free_block(bp);	
//		size_t predoff=get_pred_offset(bp);
//		size_t succoff=get_succ_offset(bp);
		alloc_block(bp,asize);
		remain_blk= NEXT_BLKP(bp);
		PUT(HDRP(remain_blk), PACK(remain_size, 0));
		PUT(FTRP(remain_blk), PACK(remain_size, 0));
		add_free_block(remain_blk,remain_size);
		/*size_t re_off=get_offset(remain_blk);
		set_succ(remain_blk,succoff);
		set_pred(remain_blk,predoff);
//	if (predoff){
		set_succ(get_addr(predoff),re_off);
//	}
//	else free_list=remain_blk;
	if (succoff){
		set_pred(get_addr(succoff),re_off);
		}*/
    }
    else { 
		delete_free_block(bp);
		PUT(HDRP(bp), PACK(csize, 1));
		PUT(FTRP(bp), PACK(csize, 1));
    }
}
Beispiel #3
0
//Coalesce two free blocks into one, add to free list
static void *coalesce(void *bp)  //bp is pointer to a free block
{
  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(prev_alloc && !next_alloc){
    //fwdPointers(NEXT_BLKP(bp), MINFRAG);
    size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
    delete_free_block(NEXT_BLKP(bp));
    PUT(HDRP(bp), PACK(size, 0));
    PUT(FTRP(bp), PACK(size, 0));    
  } 
  
  else if(!prev_alloc && next_alloc){
    //fwdPointers(PREV_BLKP(bp), MINFRAG); //if desperate we can leave this in free list
    size += GET_SIZE(HDRP(PREV_BLKP(bp)));
    delete_free_block(PREV_BLKP(bp));
    PUT(FTRP(bp), PACK(size, 0));       
    PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0)); 
    bp = PREV_BLKP(bp);
  }
  else if(!prev_alloc && !next_alloc) {
    //fwdPointers(PREV_BLKP(bp), MINFRAG);
    //fwdPointers(NEXT_BLKP(bp), MINFRAG);
    delete_free_block(NEXT_BLKP(bp));
    delete_free_block(PREV_BLKP(bp));
    size += GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(FTRP(NEXT_BLKP(bp)));
    PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
    PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
    bp = PREV_BLKP(bp);
  }
  moveStart(bp);
  return bp;
}
Beispiel #4
0
//Called by mm_alloc, places 
void place (void *bp, size_t asize){
  size_t size = GET_SIZE(HDRP(bp));
  size_t frag = size - asize;

  //internal fragment
  if (frag < MINBLKSIZE){
    PUT(HDRP(bp), PACK(size, 1));
    PUT(FTRP(bp), PACK(size, 1));
    delete_free_block(bp);  //remove allocated block from free list
    //    fwdPointers(bp, frag); //Reassigns pointers
  }
    
  //external fragment
  else{
    PUT(HDRP(bp), PACK(asize, 1));
    PUT(FTRP(bp), PACK(asize, 1));
    //fwdPointers(bp, frag);      
    delete_free_block(bp);
    bp = NEXT_BLKP(bp); //Move pointer to next block
    PUT(HDRP(bp), PACK(frag, 0));
    PUT(FTRP(bp), PACK(frag, 0));
    coalesce(bp);
    //    moveStart(bp);
  }
}
static void place(void *ptr, size_t size)
{
    mm_log("placing at %p, size %d\n", ptr, (int)size);

    size_t free_size = GET_SIZE(HDRP(ptr));
    size_t rem = free_size - size;
    if (rem < DSIZE+WSIZE*2)
    {
        size = free_size;
    }

    /* Delete from the freelist */
    delete_free_block(ptr);

    PUT(HDRP(ptr), PACK(size, 1));
    PUT(FTRP(ptr), PACK(size, 1));

    mm_log("header = (%d,%d)\n", GET_SIZE(HDRP(ptr)), GET_ALLOC(HDRP(ptr)));
    mm_log("footer = (%d,%d)\n", GET_SIZE(FTRP(ptr)), GET_ALLOC(FTRP(ptr)));

    /* See if we should split */
    if (size != free_size)
    {
        void *split_pt = ptr + size;

        init_free_block(split_pt, free_size-size);
    }
}
static void* coalesce(void *bp) //bp point to header
{
	//dbg_printf("coalesce %p\n",bp);
	void *prev_blk=PREV_BLKP(bp);
	void *next_blk=NEXT_BLKP(bp);
	size_t prev_alloc = GET_ALLOC(FTRP(prev_blk));
    size_t next_alloc = GET_ALLOC(HDRP(next_blk));
    size_t size = GET_SIZE(HDRP(bp));
	
    if (prev_alloc && next_alloc) {            /* Case 1 */
		
dbg_printf("no coal%p\n",bp);
add_free_block(bp,size);
		return bp;
    }

    else if (prev_alloc && !next_alloc) {      /* Case 2 */
		size += GET_SIZE(HDRP(next_blk));
		delete_free_block(next_blk);
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size,0));
    }

    else if (!prev_alloc && next_alloc) {      /* Case 3 */
		size += GET_SIZE(HDRP(prev_blk));
		delete_free_block(prev_blk);
		PUT(FTRP(bp), PACK(size, 0));
		PUT(HDRP(prev_blk), PACK(size, 0));
		bp = prev_blk;
    }

    else {                                     /* Case 4 */
		size += GET_SIZE(HDRP(prev_blk)) + 
	    GET_SIZE(FTRP(next_blk));
		delete_free_block(prev_blk);
		delete_free_block(next_blk);
		PUT(HDRP(prev_blk), PACK(size, 0));
		PUT(FTRP(next_blk), PACK(size, 0));
		bp = prev_blk;
   }

	add_free_block(bp,size);
    return bp;
	
}
Beispiel #7
0
/* $begin coalesce */
static void *coalesce(void *bp) //bp point to header
{
	void *prev_block=prev_blkp(bp);
	void *next_block=next_blkp(bp);

    uint32_t prev_alloc = GET_ALLOC(prev_block);
    uint32_t next_alloc = GET_ALLOC(next_block);
    uint32_t size = GET_SIZE(bp);

    if (prev_alloc && next_alloc) {            /* Case 1 */
		return bp;
    }

    else if (prev_alloc && !next_alloc) {      /* Case 2 */
	size += GET_SIZE(next_block);
	delete_free_block(next_block);
	PUT(bp, PACK(size, 0));
	PUT(FTRP1(bp), PACK(size,0));
    }

    else if (!prev_alloc && next_alloc) {      /* Case 3 */
	size += GET_SIZE(prev_block);
	delete_free_block(prev_block);
	PUT(FTRP1(bp), PACK(size, 0));
	PUT(prev_block, PACK(size, 0));
	bp = prev_block;
    }

    else {                                     /* Case 4 */
	size += GET_SIZE(prev_block) + 
	    GET_SIZE(next_block);
	delete_free_block(prev_block);
	delete_free_block(next_block);

	PUT(prev_block, PACK(size, 0));
	PUT(FTRP1(next_block), PACK(size, 0));
	bp = prev_block;
    }

	add_free_block(bp);
	return bp;
	
}
Beispiel #8
0
static void place(void *bp, size_t asize){
	
	size_t osize=GET_SIZE(bp);
	size_t remain_size=osize-asize;

//	size_t old_predaddr=pred_blka(bp);
//	size_t old_succaddr=succ_blka(bp);

	void *remain_block=(char *)bp+asize;

	if (remain_size>=16){
		delete_free_block(bp);
		set_free_block(remain_block,remain_size,0);
		add_free_block(remain_block);
	}
	else{
		delete_free_block(bp);
	}
	alloc_block(bp,asize);
}
Beispiel #9
0
/*
 * coalesce
 */
static 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));
	
	/*prev and next block are allocated*/
	if(prev_alloc && next_alloc){ 	
		//insert_free_block(bp);
		//return bp;
	}
	/* next block is free */
	else if(prev_alloc && !next_alloc){ 	
		delete_free_block(NEXT_BLKP(bp));
		size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
		PUT(HDRP(bp),PACK(size,0));
		PUT(FTRP(bp),PACK(size,0));
		//insert_free_block(bp);
	}
	/* prev block is free */
	else if(!prev_alloc && next_alloc){ 	
		delete_free_block(PREV_BLKP(bp));
		size += GET_SIZE(HDRP(PREV_BLKP(bp)));
		PUT(FTRP(bp),PACK(size,0));
		PUT(HDRP(PREV_BLKP(bp)), PACK(size,0));
		bp = PREV_BLKP(bp);
		//insert_free_block(bp);
	}
	/*prev and next are free*/
	else{ 	
		delete_free_block(PREV_BLKP(bp));
		delete_free_block(NEXT_BLKP(bp));
		size += GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(FTRP(NEXT_BLKP(bp)));
		PUT(HDRP(PREV_BLKP(bp)), PACK(size,0));
		PUT(FTRP(NEXT_BLKP(bp)), PACK(size,0));
		bp = PREV_BLKP(bp);
		//insert_free_block(bp);
	}
	insert_free_block(bp);	
	return bp;
}
/*
 * mm_realloc - Implemented simply in terms of mm_malloc and mm_free
 */
void *mm_realloc(void *ptr, size_t req_size)
{
    size_t new_size = adjust_requested_block_size(req_size);

    size_t size = GET_SIZE(HDRP(ptr));

    mm_log("\nrealloc(%p, %d => %d)\noriginal=%d\n\n", ptr, (int)req_size, (int)new_size, (int)size);

    if (new_size < size)
    {
        mm_log("newsize < original! keep pointer.\n");
        size_t remain = size - new_size;
        if (remain >= DSIZE+WSIZE*2)
        {
            PUT(HDRP(ptr), PACK(new_size, 1));
            PUT(FTRP(ptr), PACK(new_size, 1));

            mm_log("have some leftover (%d), which is enough for a new block.\n",(int)remain);
            //split
            void *split_pt = ptr + new_size;

            init_free_block(split_pt, remain);
        }
        return ptr;
    }

    /* Okay, we need to find some new space. See if there are adjacent blocks we can coalesce into */
    void *new_ptr;

    void *prev_ftr = ptr - DSIZE;
    void *next_hdr = ptr + size - WSIZE;

    size_t extra_space = 0;
    char c_prev = 0;
    char c_next = 0;

    if (!GET_ALLOC(prev_ftr))
    {
        extra_space += GET_SIZE(prev_ftr);
        c_prev = 1;
    }
    if (!GET_ALLOC(next_hdr))
    {
        extra_space += GET_SIZE(next_hdr);
        c_next = 1;
    }

    size_t coalesced_size = extra_space + size;

    if (coalesced_size >= new_size)
    {
        mm_log("prev open = %d; next open = %d. together with %d, we have %d total space.\n",c_prev,c_next,(int)size,(int)coalesced_size);

        new_ptr = ptr;
        if (c_prev)
        {
            new_ptr -= GET_SIZE(prev_ftr); //move ptr back to prev block
            delete_free_block(new_ptr);

            memcpy(new_ptr, ptr, size);
        }
        if (c_next)
        {
            delete_free_block(ptr + size);
        }

        /* We can now see if there's enough room to split another block */
        size_t remain = coalesced_size - new_size;
        if (remain >= DSIZE+WSIZE*2)
        {
            PUT(HDRP(new_ptr), PACK(new_size, 1));
            PUT(FTRP(new_ptr), PACK(new_size, 1));

            mm_log("have some leftover (%d), which is enough for a new block.\n",(int)remain);
            //split
            void *split_pt = new_ptr + new_size;

            init_free_block(split_pt, remain);
        }
        else
        {
            PUT(HDRP(new_ptr), PACK(coalesced_size, 1));
            PUT(FTRP(new_ptr), PACK(coalesced_size, 1));
        }

        return new_ptr;
    }

    mm_log("can't coalesce. have to malloc new & free this thing\n");

    /* Can't coalesce. Have to malloc new & free this block :( */
    new_ptr = mm_malloc(req_size);
    memcpy(new_ptr, ptr, size);

    init_free_block(ptr, size);
    
    return new_ptr;
}
static void *init_free_block(void *bp, size_t size)
{
    mm_log("\ninit free block %p of size %d\n", bp, (int)size);

    void *next = bp + size;
    void *prev = bp - GET_SIZE(bp - DSIZE);

    int prev_alloc = GET_ALLOC(HDRP(prev));
    int next_alloc = GET_ALLOC(HDRP(next));

    size_t prev_size = GET_SIZE(HDRP(prev));
    size_t next_size = GET_SIZE(HDRP(next));

    mm_log("prev = %p (%d,%d); next = %p (%d,%d)\n", prev, (int)prev_size, prev_alloc, next, (int)next_size, next_alloc);

    if (prev_alloc && next_alloc) /* Case 1 */
    {
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP_S(bp, size), PACK(size, 0));

        append_free_block(bp);
    }
    else if (prev_alloc && !next_alloc)  /* Case 2 */
    {
        int new_size = size + next_size;

        mm_log("coalesce next %p (%d) to make %d\n", next, (int)next_size, (int)new_size);

        delete_free_block(next);

        append_free_block(bp);

        PUT(HDRP(bp),            PACK(new_size, 0));
        PUT(FTRP_S(bp, new_size), PACK(new_size, 0));
    }
    else if (!prev_alloc && next_alloc) /* Case 3 */
    {
        size_t new_size = size + prev_size;

        mm_log("coalesce prev %p (%d) to make %d\n", prev, (int)prev_size, (int)new_size);

        PUT(HDRP(prev),             PACK(new_size, 0)); /* Resize header of prev */
        PUT(FTRP_S(prev, new_size), PACK(new_size, 0)); /* Resize footer */

        bp = prev;
    }
    else /* Case 4 */
    {
        size_t new_size = size + prev_size + next_size;

        mm_log("coalesce both prev %p (%d) and next %p (%d) to make %d\n", prev, (int)prev_size, next, (int)next_size, (int)new_size);

        /* Delete both prev blocks off the list; then append this new block */
        delete_free_block(prev);
        delete_free_block(next);

        mm_log("deleted %p and %p. tail=%p\n",prev,next,free_tailp);

        PUT(HDRP(prev),              PACK(new_size, 0));
        PUT(FTRP_S(next, next_size), PACK(new_size, 0));

        bp = prev;

        append_free_block(bp);
    }

    ASSERT;
    
    return bp;
}