Beispiel #1
0
/* $begin mmfree */
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));
    
    if (prev_alloc && next_alloc) {            /* Case 1 */
        return bp;
    }
    
    else if (prev_alloc && !next_alloc) {      /* Case 2 */
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        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_BLKP(bp)));
        PUT(FTRP(bp), PACK(size, 0));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    
    else {                                     /* Case 4 */
        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);
    }
    /* $end mmfree */
#ifdef NEXT_FIT
    /* Make sure the rover isn't pointing into the free block */
    /* that we just coalesced */
    if ((rover > (char *)bp) && (rover < NEXT_BLKP(bp)))
        rover = bp;
#endif
    /* $begin mmfree */
    return bp;
}
Beispiel #2
0
/*
 * coalesce the previous block-curent block-next block
    IF handled corrected, each free will call this method, and no two consecutive freed block would exsist.
 */
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));

    if(prev_alloc && next_alloc)
        return bp;

    else if(prev_alloc && !next_alloc){
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }else if(!prev_alloc && next_alloc){
        size += GET_SIZE(FTRP(PREV_BLKP(bp)));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }else{
        size += GET_SIZE(HDRP(NEXT_BLKP(bp))) + GET_SIZE(FTRP(PREV_BLKP(bp)));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }

    if((rover > (char*)bp) && (rover < NEXT_BLKP(bp)))
        rover = bp;

    return bp;
}
Beispiel #3
0
//
// coalesce - boundary tag coalescing. Return ptr to coalesced block
//
static void *coalesce(void *bp) 
{
  size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
  size_t next_alloc = GET_ALLOC(FTRP(NEXT_BLKP(bp)));
  size_t size = GET_SIZE(HDRP(bp));

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

  else if (prev_alloc && !next_alloc) { // Case 2
    size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
    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_BLKP(bp)));
    PUT(FTRP(bp), PACK(size, 0));
    PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
    bp = PREV_BLKP(bp);
  }

  else {
    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);
  }
  return bp;
}
Beispiel #4
0
//
// coalesce - boundary tag coalescing. Return ptr to coalesced block
//
static void *coalesce(void *bp)
{
	FL_Pointer prev = PREV_BLKP(bp);
	FL_Pointer next = NEXT_BLKP(bp);
	
	size_t prev_alloc = GET_ALLOC(FTRP(prev));
	size_t next_alloc = GET_ALLOC(HDRP(next));
	size_t size = GET_SIZE(HDRP(bp));
	
	if (prev_alloc && next_alloc) {
		CL_tack(&free_list, bp); // case 1 neighbours = 1  add a return?
	}
	
	else if (prev_alloc && !next_alloc) { // case 2 next is free
		CL_unlink(next);
		CL_tack(&free_list, bp);
		
		size += GET_SIZE(HDRP(next));
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
	}
	
	else if (!prev_alloc && next_alloc) { // case 3 prev is free
		size += GET_SIZE(HDRP(prev));
		PUT(FTRP(bp), PACK(size, 0));
		PUT(HDRP(prev), PACK(size, 0));
		bp = prev;
	}

	else { // both next and prev free
		size += GET_SIZE(HDRP(prev)) + GET_SIZE(FTRP(next));
		CL_unlink(next);
		PUT(HDRP(prev), PACK(size, 0 ));
		PUT(FTRP(next), PACK(size, 0));
		bp = prev;
	}
	
	return bp;
}
Beispiel #5
0
/* 
 * Requires:
 *   "bp" is either the address of an allocated block or NULL.
 *
 * Effects:
 *   Free a block.
 */
void 
mm_free(void *bp)
{
  size_t size;
  /* Ignore spurious requests. */
  if (bp == NULL)
    return;
  /* Free and coalesce the block. */
  size = GET_SIZE(HDRP(bp));
  PUT(HDRP(bp), PACK(size, 0));
  PUT(FTRP(bp), PACK(size, 0));
  coalesce(bp);
}
Beispiel #6
0
/*
 * mm_free - Freeing a block does nothing.
 *
 * Role : The mm_free routine frees the block pointed to by ptr
 *
 * Return value : returns nothing
 */
void mm_free(void *ptr)
{
    size_t size = GET_SIZE(HDRP(ptr));
 
    REMOVE_RATAG(HDRP(NEXT_BLKP(ptr)));
    PUT(HDRP(ptr), PACK(size, 0));
    PUT(FTRP(ptr), PACK(size, 0));
    
    insert_node(ptr, size);
    coalesce(ptr);
    
    return;
}
Beispiel #7
0
static void add_to_group(char* bp) {
    int size = GET_SIZE(HDRP(bp));
    int group = get_group(size);
    void* heap_listp_new = heap_listp - (GROUPSIZE + (2*WSIZE));
    int** grpp = heap_listp_new + (group * WSIZE);    
    if (*grpp) {
        int* tmpptr = *grpp;
        PUT(HDRP(bp) + 4, (int)tmpptr);
        PUT(FTRP(bp) + 4, (int)tmpptr);
    }    
    //printf("group: %d", group);
    PUTPTR(grpp, bp);
}
Beispiel #8
0
/* Looks to the right and to the left to combine with nearby free blocks
in order to minimize fragmentation. */
static void *coalesce(void *bp) {
    size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp))) || PREV_BLKP(bp) == bp;
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));

    /* Coalesce with block to the right */
    if (prev_alloc && !next_alloc) {
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        /* Now remove coalesced block to make room for actual coalesce */
        deleteBlk(NEXT_BLKP(bp));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }
    /* Coalesce with block to the left */
    else if (!prev_alloc && next_alloc) {
        size += GET_SIZE(HDRP(PREV_BLKP(bp)));
        bp = PREV_BLKP(bp);
        deleteBlk(bp); // Delete previous blck to make room
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }
    /* Coalesce with both left and right blocks */
    else if (!prev_alloc && !next_alloc) {
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
                GET_SIZE(HDRP(NEXT_BLKP(bp)));
        /* Deletes next and previous blocks to make more room,
        then reassigns bp to reflect coalescing */
        deleteBlk(PREV_BLKP(bp));
        deleteBlk(NEXT_BLKP(bp));
        bp = PREV_BLKP(bp);
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size, 0));
    }

    /* Insert newly freed block at the front of free list */
    reinsert(bp);

    return bp;
}
/* place - 
 * Places the requested block at the beginning of the freeblock, and splitting * only if the size of the remainder block would equal or exceed the minimum  * block size */
static void place(void *bp, size_t asize) {
    size_t csize = GET_SIZE(HDRP(bp));   
    size_t remainder = csize - asize;

    if (remainder >= (MINSIZE * WSIZE)) {          
        remove_from_list(find_list(GET_SIZE(HDRP(bp))), bp);
        SET_SIZE(HDRP(bp), asize);
        SET_ALLOC(HDRP(bp));
        bp = NEXT_BLKP(bp);
        SET_SIZE(HDRP(bp), remainder);
        SET_SIZE(FTRP(bp), remainder);
        UNSET_ALLOC(HDRP(bp));
        UNSET_ALLOC(FTRP(bp));        
        SET_TAG(HDRP(bp));
        SET_TAG(FTRP(bp));        
        add_to_list(find_list(GET_SIZE(HDRP(bp))), bp);
    }else { 
        remove_from_list(find_list(GET_SIZE(HDRP(bp))), bp);
        SET_ALLOC(HDRP(bp));
        SET_TAG(HDRP(NEXT_BLKP(bp)));
    }
}
Beispiel #10
0
/**
 * print_free_block - Print the contents of the given free block, including 
 *                    the pointers
 * @param bp Block to be printed
 */
static void print_free_block(void *bp) 
{
    size_t hsize = -1, halloc, fsize, falloc;

    if(bp != NULL) {
    hsize = GET_SIZE(HDRP(bp));
    halloc = GET_ALLOC(HDRP(bp));  
    fsize = GET_SIZE(FTRP(bp));
    falloc = GET_ALLOC(FTRP(bp));  
    dbg_printf("%p: header: [%lu:%c] footer: [%lu:%c]\n", bp,hsize, \
        (halloc ? 'a' : 'f'), fsize, (falloc ? 'a' : 'f')); 
    dbg_printf( "%p: next: [%p] prev: [%p]\n  ", bp, GET_NEXTP(bp), \
        GET_PREVP(bp) );
    } else {
        dbg_printf("bp is null\n");
    }

    if (hsize == 0) {
        dbg_printf("%p: EOL\n", bp);
        return;
    }
}
Beispiel #11
0
/*
 * frees a block of memory, enabling it to be reused later
 * arguments: ptr: the allocated block to free
 * returns: nothing
 */
void mm_free(void *ptr) {
    size_t size;
    /* Ignore spurious requests. */
    if (ptr == NULL)
    {
	return;
    }
    /* Free and coalesce the block. */
    size = GET_SIZE(HDRP(ptr));
    PUT(HDRP(ptr), PACK(size, 0));
    PUT(FTRP(ptr), PACK(size, 0));
    coalesce(ptr);
}
/*
 * coalesce - Implements boundary-tag coalescing to merge the input block 
 * with any adjacent free blocks in constant time.
 */
static void *coalesce(void *bp) {
    size_t prev_alloc = GET_PREV_ALLOC(HDRP(bp));
    size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
    size_t size = GET_SIZE(HDRP(bp));

    if (prev_alloc && next_alloc) {            /* Case 1 */
        UNSET_TAG(HDRP(NEXT_BLKP(bp)));    
        add_to_list(find_list(GET_SIZE(HDRP(bp))), bp);
        return bp;
    }
    else if (prev_alloc && !next_alloc) {      /* Case 2 */
        remove_from_list(find_list(GET_SIZE(HDRP(NEXT_BLKP(bp)))), NEXT_BLKP(bp));
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size,0));
        SET_TAG(HDRP(bp));
        SET_TAG(FTRP(bp));
    }
    else if (!prev_alloc && next_alloc) {      /* Case 3 */
        remove_from_list(find_list(GET_SIZE(HDRP(PREV_BLKP(bp)))), PREV_BLKP(bp));
        size += GET_SIZE(HDRP(PREV_BLKP(bp)));
        SET_SIZE(FTRP(bp), size);
        SET_SIZE(HDRP(PREV_BLKP(bp)), size);
        bp = PREV_BLKP(bp);
    }
    else {                                     /* Case 4 */
        remove_from_list(find_list(GET_SIZE(HDRP(PREV_BLKP(bp)))), PREV_BLKP(bp));
        remove_from_list(find_list(GET_SIZE(HDRP(NEXT_BLKP(bp)))), NEXT_BLKP(bp));
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) +  GET_SIZE(FTRP(NEXT_BLKP(bp)));
        SET_SIZE(HDRP(PREV_BLKP(bp)), size);
        SET_SIZE(FTRP(NEXT_BLKP(bp)), size);
        bp = PREV_BLKP(bp);
    }
    
    UNSET_TAG(HDRP(NEXT_BLKP(bp)));    
    add_to_list(find_list(GET_SIZE(HDRP(bp))), bp);

    return bp;
}
Beispiel #13
0
/* $begin mmplace-proto */
static void *place(void *bp, size_t asize)
// $end mmplace-proto 
{
    size_t csize = GET_SIZE(HDRP(bp));
	void *alloBlock = bp;

    if ((csize - asize) >= (DSIZE + OVERHEAD)) { 
		PUT(HDRP(bp), PACK(asize, 1));
        PUT(FTRP(bp), PACK(asize, 1));
        bp = NEXT_BLKP(bp);
        PUT(HDRP(bp), PACK(csize-asize, 0));
        PUT(FTRP(bp), PACK(csize-asize, 0));
		// setjum free blokkina sem kom út úr splitti í free lista
		insert_block(bp);
    }
    else {
        PUT(HDRP(bp), PACK(csize, 1));
        PUT(FTRP(bp), PACK(csize, 1));
    }
	// skilum pointer á allocated blokkina
	return alloBlock;
}
Beispiel #14
0
/*
 * extend_heap - Extends the heap when the desired memory is not available
 *
 */
static void *extend_heap(size_t words) {
    char *bp;
    size_t size=words*WSIZE;
    size = ALIGN(size+SIZE_T_SIZE); /* Set the size of the block to follow double word Allignment */
 \
    if ((int)(bp = mem_sbrk(size)) == -1)
        return NULL;

    PUT(HDRP(bp), PACK(size, 0)); /* Sets the Header of the Block */
    PUT(FTRP(bp), PACK(size, 0)); /* Sets the Footer of the Block */
    PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* Epilogue has to be created at the end */
    return coalesce(bp); /* Coalescing the Free blocks (Immediate Coalescing) */
}
Beispiel #15
0
static void place (void*bp, size_t asize){
    size_t csize = GET_SIZE(HDRP(bp));

    offlist(bp);
    if((csize - asize) >= (2*DSIZE)) {
        PUT(HDRP(bp),PACK(asize,1));
        PUT(FTRP(bp),PACK(asize,1));
        bp = NEXT_BLKP(bp);
        PUT(HDRP(bp),PACK(csize-asize,0));
        PUT(FTRP(bp),PACK(csize-asize,0));
        
        PUT(bp, endfree);
        PUT(SUC(bp), NULL);
        if (endfree)
            PUT(SUC(endfree), bp);
        endfree = bp;
    }
    else{
        PUT(HDRP(bp),PACK(csize,1));
        PUT(FTRP(bp),PACK(csize,1));
    }
}
Beispiel #16
0
static void printblock(void *bp)
{
  REQUIRES (bp!=NULL);
  REQUIRES ((size_t)(bp)%8 == 0);
  size_t hsize, halloc, fsize, falloc;

  // checkheap(0);
  hsize = GET_SIZE(HDRP((bp)));
  halloc = GET_ALLOC(HDRP((bp)));
  fsize = GET_SIZE(FTRP((bp)));
  falloc = GET_ALLOC(FTRP((bp)));

  printf("%p: header: [%d:%c] footer: [%d:%c]\n", bp,
          (int)hsize, (halloc ? 'a' : 'f'),
          (int)fsize, (falloc ? 'a' : 'f'));

  if (hsize == 0) {
    printf("%p: EOL\n", bp);
    return;
  }

}
Beispiel #17
0
void *coalesce(void *bp) {
	size_t previous_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
	size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
	size_t size = GET_SIZE(HDRP(bp));
	/* Previous & Next both are allocated. DO NOT COALESCE */
	if (previous_alloc && next_alloc) {
		addFreeBlock(bp);
		return bp;
	}
	/* Previous is allocated, Next is free. */
	else if (previous_alloc && !next_alloc) {
		delFreeBlock(NEXT_BLKP(bp));
		size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
		PUT(HDRP(bp), PACK(size, 0));
		PUT(FTRP(bp), PACK(size, 0));
		addFreeBlock(bp);
	}
	/* Previous is free, Next is Allocated */
	else if (!previous_alloc && next_alloc) {
		delFreeBlock(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);
		addFreeBlock(bp);
	}
	/* Both previousious & next are free */
	else {
		delFreeBlock(PREV_BLKP(bp));
		delFreeBlock(NEXT_BLKP(bp));
		size+=GET_SIZE(HDRP(PREV_BLKP(bp)));
		size+=GET_SIZE(HDRP(NEXT_BLKP(bp)));
		PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
		PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
		bp = PREV_BLKP(bp);
		addFreeBlock(bp);
	}
	return bp; /* return the void pointer ... anyway we are not getting to catch it */
}
Beispiel #18
0
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));

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

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

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

    else /* Case 4 */ 
    {
        char * pbp = HDRP(PREV_BLKP(bp));
        char * nbp = FTRP(NEXT_BLKP(bp));
        size += GET_SIZE(pbp) + GET_SIZE(nbp);
        PUT(pbp, PACK(size, 0)); 
        PUT(nbp, PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    
    return bp;
}
Beispiel #19
0
/*
 * coalesce next block with current block
 * this method only called by coalesce,
 * pre-condistion is bp block is free and its next block in heap is free too,
 * so we merge them
 */
void coalesce_next(void *bp){
	size_t size = GET_SIZE(bp);
	void *next_bp = NEXT_BLKP(bp);	
	
	/* delete next_bp from free list */
	SET_NEXT(GET_PREV(next_bp), GET_NEXT(next_bp));
	SET_PREV(GET_NEXT(next_bp), GET_PREV(next_bp));
	
	/* reset hdr/ftr of the new free block */
	size += GET_SIZE(NEXT_BLKP(bp));
	PUT(HDRP(bp), PACK(size, 0));
	PUT(FTRP(bp), PACK(size, 0));
}
Beispiel #20
0
/**
 * coalesce - Coalesce the blocks to avoid fragmentation.
 *            Need to be done after every block free.
 * @param bp Block pointer to block to be coalesced
 */
static inline void *coalesce_block(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));
    /*The line kept, so that compiler dont complain.*/
    if(size == 0) {
        checkheap(9);
    }

    if ( prev_alloc && next_alloc ) {               /* Case 1 */
        return bp;               
    } else if ( prev_alloc && !next_alloc ) {       /* Case 2 */
        size += GET_SIZE( HDRP( NEXT_BLKP( bp )));
        /* Next is not allocated-- remove from free list */
        delete_free_list( NEXT_BLKP( bp ));
        PUT( HDRP( bp ), PACK( size, 0 ));
        PUT( FTRP( bp ), PACK( size, 0 ));
    } else if ( !prev_alloc && next_alloc ) {       /* Case 3 */
        bp = PREV_BLKP(bp);
        size += GET_SIZE(HDRP(bp));
        /* Previous is not allocated-- remove from free list */
        delete_free_list(bp);
        PUT(HDRP(bp), PACK(size,0));
        PUT(FTRP(bp), PACK(size,0));
    } else {                                        /* Case 4 */
        /* Both Previous and Next Block are not allocated */
        size += GET_SIZE(HDRP(PREV_BLKP(bp))) + 
            GET_SIZE(FTRP(NEXT_BLKP(bp)));
        /* Remove both previous and next */
        delete_free_list(PREV_BLKP(bp));
        delete_free_list(NEXT_BLKP(bp));
        PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
        PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
        bp = PREV_BLKP(bp);
    }
    return bp;
}
Beispiel #21
0
/*
 * coalesce - boundary tag coalescing. Return ptr to coalesced block
 */
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));

    if (prev_alloc && next_alloc) {  			// Case 1 
        //insert_block(bp);
		return bp;
    }
    else if (prev_alloc && !next_alloc) {      // Case 2 
		// ef next block er free tökum við hana úr free lista
		remove_block(NEXT_BLKP(bp));
        size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
        PUT(HDRP(bp), PACK(size, 0));
        PUT(FTRP(bp), PACK(size,0));
    }
    else if (!prev_alloc && next_alloc) {  		// Case 3
		// ef prev blokk er frí tökum við hana úr free lista
		remove_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);
    }
    else {                                     // Case 4
		// ef next og prev blokkir fríar tökum við þær úr free lista
		remove_block(PREV_BLKP(bp));
		remove_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);
    }

    return bp;
}
Beispiel #22
0
/**********************************************************
 * coalesce
 * 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
 **********************************************************/
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));

	/* Case 1: |ALLOC|<bp>|ALLOC| cannot coalesce */
	if (prev_alloc && next_alloc) {
		return bp;
	}

	/* Case 2: |ALLOC|<bp>|FREE| can coalesce with the next block */
	else if (prev_alloc && !next_alloc) {
		delete_from_list(NEXT_BLKP(bp));  // Remove the free block from the list
		size += GET_SIZE(HDRP(NEXT_BLKP(bp)));  // Calculate the combined total size
		PUT(HDRP(bp), PACK(size, 0));  // Set the header
		PUT(FTRP(bp), PACK(size, 0));  // Set the footer
		return (bp);
	}

	/* Case 3: |FREE|<bp>|ALLOC| can coalesce with the prev block */
	else if (!prev_alloc && next_alloc) {
		delete_from_list(PREV_BLKP(bp));  // Remove the free block from the list
		size += GET_SIZE(HDRP(PREV_BLKP(bp)));  // Calculate the combined total size
		PUT(FTRP(bp), PACK(size, 0));  // Set the header
		PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));  // Set the footer
		return (PREV_BLKP(bp));
	}

	/* Case 4: |FREE|<bp>|FREE| both sides are free, can coalesce both */
	else {
		delete_from_list(PREV_BLKP(bp));  // Remove the free blocks from the list
		delete_from_list(NEXT_BLKP(bp));
		size += GET_SIZE(HDRP(PREV_BLKP(bp))) + GET_SIZE(FTRP(NEXT_BLKP(bp)));  // Calculate the combined total size
		PUT(HDRP(PREV_BLKP(bp)), PACK(size,0));  // Set the header
		PUT(FTRP(NEXT_BLKP(bp)), PACK(size,0));  // Set the footer
		return (PREV_BLKP(bp));
	}
}
Beispiel #23
0
/**********************************************************
 * place
 * Mark the block as allocated
    place will mark a block from the free list as allocated
    the block is removed from the free list, then checked if it needs 
    to be split. If it needs to be split. it will split it and place
    the second free block into the front of the free list.
 **********************************************************/
void place(void* bp, size_t asize)
{
    //print_free_list();
    //printf("---place\n");

    size_t min_size = DSIZE+DSIZE;
    size_t free_size = GET_SIZE(HDRP(bp));
    size_t bsize;

    rmFromFreeList(bp);

    if (free_size >= asize+min_size) {
        //printf(" and split");
        //pack asize
        //printf(" asize %zu", asize);
        PUT(HDRP(bp), PACK(asize, 1));
        PUT(FTRP(bp), PACK(asize, 1));

        //pack bsize
        bsize = free_size - asize;
        //printf(" bsize %zu \n", bsize);
        void * free_bp = NEXT_BLKP(bp);
        PUT(HDRP(free_bp), PACK(bsize, 0));
        PUT(FTRP(free_bp), PACK(bsize, 0));

        //add bsize to free list
        addToFront(free_bp);
        
    }
    else {
        //printf(" and done");
        //no splitting
        PUT(HDRP(bp), PACK(free_size, 1));
        PUT(FTRP(bp), PACK(free_size, 1));
    }

    //print_free_list();
}
Beispiel #24
0
static void checkblock(void *bp) 
{
    if ((size_t)bp % 8) {
        printf("Error: %p is not doubleword aligned\n", bp);
    }
    if (GET(HDRP(bp)) != GET(FTRP(bp))) {
        printf("Error: header does not match footer\n");
    }
	if(GET_ALLOC(HDRP(bp)) == 0){
		if(GET_SIZE(HDRP(bp)) < 16){
			printf("Free block is lesser than 16 bytes !\n");
		}
	}
}
/*
 * free
 */
void free(void *bp) {
    if(bp == 0)
        return;

    if (first_block == 0)
        mm_init();

    UNSET_ALLOC(HDRP(bp));
    PUT(FTRP(bp), GET(HDRP(bp)));
    coalesce(bp);
#ifdef DEBUG    
    mm_checkheap(1);
#endif    
}
Beispiel #26
0
/**********************************************************
 * 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));
}
void place(void *bp, size_t total_size) {
	size_t csize = GET_SIZE(HDRP(bp));//computes the size of the block


	if ((csize - total_size) >= (2*DSIZE )) {
		Delete_Free_Block(bp,csize);//Deletes the bly maintaock from the explicictly maintained free list
		PUT(HDRP(bp), PACK(total_size, 1));//packs the size of the block and the allocation(1) status in the footer
		PUT(FTRP(bp), PACK(total_size, 1));//packs the size of the block and the allocation(1) status in the header
		bp = NEXT_BLKP(bp);//gets the next block pointer
		PUT(HDRP(bp), PACK(csize-total_size, 0));//packs the size of the block and the allocation(0) status in the footer
		PUT(FTRP(bp), PACK(csize-total_size, 0));//packs the size of the block and the allocation(0) status in the header

		Add_Free_Block(bp,csize-total_size);//Adds the newly created block to the explicitly maintained free list
	}

/* do not split */
	else {
		PUT(HDRP(bp), PACK(csize, 1));//packs the size of the block and the allocation(1) status in the header
		PUT(FTRP(bp), PACK(csize, 1));//packs the size of the block and the allocation(1) status in the footer
		Delete_Free_Block(bp,csize);//Deletes the block  from the explicictly maintained free list
	}

}
Beispiel #28
0
/*
 * place
 */
static void place(void *bp, size_t asize)
{
	size_t csize = GET_SIZE(HDRP(bp));
	size_t list;
	size_t previous = GET_PREV_ALLOC(HDRP(bp));
	/*splitting has to happen*/
	if((csize - asize) >= 16)
	{
		/* Remove the current block from its free list */
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(bp)),
				GET(CURRENT_PREV_POINTER(bp)));
		PUT(CURRENT_NEXT_POINTER(PREV_FREE_BLKP(bp)),
				GET(CURRENT_NEXT_POINTER(bp)));

		/*The allocated bit is set to 1*/
		PUT(HDRP(bp), PACK(asize,previous, 1));
		bp= NEXT_BLKP(bp);	

		/* The remainder of the free block is made into a new free block */
		PUT(HDRP(bp), PACK(csize-asize,2,0));
		PUT(FTRP(bp), csize-asize);

		/* Call listno to find the list number in which this split block should 
		 * be put and insert it right after the root node of that list
		 */
		list = listno(csize - asize);
		if(list >=maxlist)
			list=maxlist;
		PUT(CURRENT_NEXT_POINTER(bp),GET(CURRENT_NEXT_POINTER(root + 8*list)));
		PUT(CURRENT_PREV_POINTER(bp),
				GET(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(bp))));
		PUT(CURRENT_NEXT_POINTER(root + 8*list),(long)bp-(long)heapstart);
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(bp)),(long)bp-(long)heapstart);
	}
	/* No splitting */
	else
	{
		/* Remove the current block from its free list */
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(bp)),
				GET(CURRENT_PREV_POINTER(bp)));
		PUT(CURRENT_NEXT_POINTER(PREV_FREE_BLKP(bp)),
				GET(CURRENT_NEXT_POINTER(bp)));

		/* Set the allocated bit of current block */
		PUT(HDRP(bp), PACK(csize,previous,1));

		/* Set the prev_alloc bit of next block */
		SET_PREV_ALLOC(HDRP(NEXT_BLKP(bp)));
	}
}
Beispiel #29
0
/*
 * free
 */
void free (void *bp) 
{
	/*Ignore spurious request*/
	if(bp==0)
		return;

	size_t size = GET_SIZE(HDRP(bp));
	size_t previous = GET_PREV_ALLOC(HDRP(bp));

	/*Set the allocated bit to zero*/
	PUT(HDRP(bp), PACK(size,previous,0));
	PUT(FTRP(bp), size);
	coalesce(bp);
}
//将请求块放置在空闲块的起始位置,当块的剩余部分大小大于等于最小块的大小时,进行分割
static void place(void *bp, size_t asize)
{
	if ((GET_SIZE(HDRP(bp)) - asize) < MINBLOCK)
	{
		PUT(HDRP(bp), PACK(GET_SIZE(HDRP(bp)), 1));
		PUT(FTRP(bp), PACK(GET_SIZE(FTRP(bp)), 1));
	}
	else
	{
		size_t size = GET_SIZE(HDRP(bp)) - asize;
		PUT(FTRP(bp), PACK(size, 0));		//新块尾部
		PUT(HDRP(bp), PACK(asize, 1));		//分配块头部
		PUT(FTRP(bp), PACK(asize, 1));		//分配块尾部
		PUT(HDRP(NEXT_BLKP(bp)), PACK(size, 0));	//新块头部

		//将新产生的块放在空闲队列头部
		PUT(NEXT_BLKP(bp), (void*)&head);
		PUT(NEXT_BLKP(bp) + WSIZE, (void*)head.next);
		if (head.next != NULL)
			PUT((void*)head.next, NEXT_BLKP(bp));
		head.next = (char*)NEXT_BLKP(bp);
	}
}