コード例 #1
0
ファイル: mm.c プロジェクト: NikhilCMU/Coding_Projects
/*
* mm_checkheap
*/
void mm_checkheap(int lineno) {
    lineno = lineno;

    int free_count = 0;
    int freelist_count = 0;
    void *bp;

    for (bp = free_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_FREE_BLKP(bp)) {
        if (GET_ALLOC(bp)) 
            printf("Allocated block in free list!\n");
        if (!aligned(bp)) printf("Base pointer in heap is not aligned!\n");
        if (!in_heap(NEXT_FREE_BLKP(bp)) || !in_heap(PREV_FREE_BLKP(bp))) 
            printf("Pointer not in heap! Uh oh.\n");
        if (GET(HDRP(bp)) != GET(FTRP(bp))) 
            printf("Header and footer don't match! Dang...\n");
        if (PREV_FREE_BLKP(NEXT_FREE_BLKP(bp)) != bp || 
            NEXT_FREE_BLKP(PREV_FREE_BLKP(bp)) != bp)
            printf("Pointers do not match up! Ugh\n");
        freelist_count++;
    }

    for (bp = free_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if (!aligned(bp)) printf("Base pointer in heap is not aligned!\n");
        if (GET(HDRP(bp)) != GET(FTRP(bp))) 
            printf("Header and footer don't match! Dang...\n");
        if (PREV_BLKP(NEXT_BLKP(bp)) != bp || NEXT_BLKP(PREV_BLKP(bp)) != bp)
            printf("Pointers do not match up! Ugh\n");
        if (!GET_ALLOC(HDRP(bp))) free_count++;
    }

    if (free_count != freelist_count) 
        printf("Free blocks in free list don't match up with heap traversal!");

}
コード例 #2
0
ファイル: mm.c プロジェクト: NikhilCMU/Coding_Projects
/* Takes a newly freed, coalesced block and inserts it at the front
of the free list, to maintain minimal fragmentation. */
static void reinsert(void *bp) {
    /* Modify original free list start pointers
    Change newly inserted free block pointers accordingly */
    NEXT_FREE_BLKP(bp) = free_listp;
    PREV_FREE_BLKP(free_listp) = bp;
    PREV_FREE_BLKP(bp) = NULL;
    /* Reassign start of free list to block bp */
    free_listp = bp;
}
コード例 #3
0
ファイル: mm.c プロジェクト: johannbrynjar/malloclab
/*
 * removeBlckFromFreeList - fjarlaegja blokk ur free list
 */
static void removeBlckFromFreeList(void *bp) {
    // bp->next->prev = bp->prev
    PREV_FREE_BLKP(NEXT_FREE_BLKP(bp)) = PREV_FREE_BLKP(bp);
    if(PREV_FREE_BLKP(bp) == NULL)
        // free_listp = bp->next
        free_listp = NEXT_FREE_BLKP(bp);
    else
        // bp->prev->next = bp->next
        NEXT_FREE_BLKP(PREV_FREE_BLKP(bp)) = NEXT_FREE_BLKP(bp);
}
コード例 #4
0
ファイル: mm.c プロジェクト: NikhilCMU/Coding_Projects
/* "Removes" an allocated block from the free list by reassigning
the pointers */
static void deleteBlk(void *bp) {
    /* If the new allocated block is at the beginning of the free list,
    then change start of free list to next block, retain next block next ptr.

    Else change prev block next pointer to next block, change next block prev
    pointer to prev block. */

    if (PREV_FREE_BLKP(bp))
        NEXT_FREE_BLKP(PREV_FREE_BLKP(bp)) = NEXT_FREE_BLKP(bp);
    else
        free_listp = NEXT_FREE_BLKP(bp);
    PREV_FREE_BLKP(NEXT_FREE_BLKP(bp)) = PREV_FREE_BLKP(bp);
}
コード例 #5
0
ファイル: pvikrama_malloc.c プロジェクト: pvikrama/Malloc
/*
 * 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)));
	}
}
コード例 #6
0
ファイル: mm.c プロジェクト: johannbrynjar/malloclab
/*
 * printBlock - prentar ut staka blokk
 */
void printBlock(void *bp) {
    if(GET_ALLOC(HDRP(bp)))
        printf("%p | hdr: %p | ftr: %p | Size %d | Alloc: %d\n" ,
               bp, HDRP(bp), FTRP(bp), GET_SIZE(HDRP(bp)), GET_ALLOC(HDRP(bp)));
    else
        printf("%p | hdrp %p | ftr: %p | prevfre: %p | nextfre: %p | Size: %d | Alloc: %d\n",
               bp, HDRP(bp), FTRP(bp), NEXT_FREE_BLKP(bp), PREV_FREE_BLKP(bp),
               GET_SIZE(HDRP(bp)), GET_ALLOC(HDRP(bp)));
}
コード例 #7
0
ファイル: mm.c プロジェクト: tabletenniser/ECE454_hw3
/**********************************************************
 * add_free_block
 * utility function that inserts the bp block to the
 * linkedlist of free blocks.
 *********************************************************/
void add_free_block(void *bp){
    logg(4, "============ add_free_block() starts ==============");

    // Find the proper index to insert the block.
    int free_list_i = 0;
    int size = GET_SIZE(HDRP(bp));
    while (size > (1 << (free_list_i))){
        free_list_i++;
    }

    // Add block from bp to the linkedlist of free_block_lists.
    if (free_block_lists[free_list_i])
        PUT(PREV_FREE_BLKP(free_block_lists[free_list_i]), (uintptr_t)bp);
    PUT(NEXT_FREE_BLKP(bp), (uintptr_t)free_block_lists[free_list_i]);
    PUT(PREV_FREE_BLKP(bp), (uintptr_t)NULL);
    free_block_lists[free_list_i]=bp;

    logg(5, "free_list_i is: %d; size is: %d; bp is: %p", free_list_i, size, bp);
    logg(5, "next of bp is: %zx; prev of bp is: %zx", GET(NEXT_FREE_BLKP(bp)), GET(PREV_FREE_BLKP(bp)));
    logg(4, "============ add_free_block() ends ==============");
    return;
}
コード例 #8
0
ファイル: mm-seglist.c プロジェクト: 447327642/CSAPP2e
void checklist(void) 
{
	void* bp = heap_listp;
	void* prev_ptr;
	//int  prev_alloc = 1;
	int i;

	dbg1("[IN ] : checklist\n");
	
  /* go through each segregated free list */
	for ( i = 0; i <= MAXCLASS; i++ ) {
		if (verbose)
			printlist(i);
		
		bp = GET_FREE_LISTP(i);
		prev_ptr = NULL;
		while( bp != NULL) {
			/* see if pointer is within heap */
			if ((bp < mem_heap_lo()) || (mem_heap_hi() < bp)) {
				printf("mm_check: free block pointer %p outside of heap\n",bp);
				fflush(stdout);
				exit(0);
			}
			/* make sure block is truly free */
			if (GET_ALLOC(HDRP(bp))) {
				printf("mm_check: free block list %d has allocated block\n", i);
				fflush(stdout);
				exit(0);
			}
			/* make sure block is not smaller than min block */
			if (GET_SIZE(HDRP(bp)) < OVERHEAD + 2*DSIZE) {
				printf("mm_check: block too small in list %d with block at %p\n", i, bp);
				printf("mm_check: %x\n", GET_SIZE(HDRP(bp)));
				fflush(stdout);
				exit(0);
			}
			/* make sure previous pointer is correct */
			if (PREV_FREE_BLKP(bp) != prev_ptr) {
				printf("mm_check: previous block ptr in list %d does not point to previous block\n", i);
				fflush(stdout);
				exit(0);
			}
			/* go to next free block */
			prev_ptr = bp;
			bp = (void*)NEXT_FREE_BLKP(bp);
		}
	}

	dbg1("[OUT] : checklist\n");
	return;
}
コード例 #9
0
ファイル: mm.c プロジェクト: tushar7795/malloc
void delFreeBlock(void *bp) {

	void *next = (void *) NEXT_FREE_BLKP(bp);
	void *previous = (void *) PREV_FREE_BLKP(bp);
	if (previous == NULL) { 
		root = next;
	} else {
		SET_NEXT_FREE(previous, next);
	}

	if (next != NULL) { 
		SET_PREV_FREE(next, previous);
	}
}
コード例 #10
0
ファイル: mm.c プロジェクト: tushar7795/malloc
void addFreeBlock(void *bp) {
	void *current = root;
	void *temp = current;
	void *previous = NULL;
	while (current != NULL && bp < current) { 
		previous = PREV_FREE_BLKP(current);
		temp = current;
		current = NEXT_FREE_BLKP(current);
	}

	SET_PREV_FREE(bp, previous);
	SET_NEXT_FREE(bp, temp);
	if (previous != NULL) {
		SET_NEXT_FREE(previous, bp);
	} else { 
		root = bp;
	}
	if (temp != NULL) {
		SET_PREV_FREE(temp, bp);
	}
}
コード例 #11
0
ファイル: mm.c プロジェクト: tabletenniser/ECE454_hw3
/**********************************************************
 * remove_free_block
 * utility function that removes the bp block to the
 * linkedlist of free blocks.
 *********************************************************/
void remove_free_block(void *bp){
    logg(4, "============ remove_free_block() starts ==============");
    logg(5, "bp: %p;prev blk ptr: %zx;next blk ptr: %zx", bp, GET(PREV_FREE_BLKP(bp)), GET(NEXT_FREE_BLKP(bp)));

    // Find the proper index where the block should locates.
    int free_list_i = 0;
    int size = GET_SIZE(HDRP(bp));
    while (size > (1 << (free_list_i))){
        free_list_i++;
    }

    // Remove the block from the doubly-linked linkedlist.
    char *next_block_ptr = (char *)GET(NEXT_FREE_BLKP(bp));
    char *prev_block_ptr = (char *)GET(PREV_FREE_BLKP(bp));
    // Case for only one free block
    if (!GET(PREV_FREE_BLKP(bp)) && !GET(NEXT_FREE_BLKP(bp))){
        logg(5, "Case A: block is the only free block in the list");
        free_block_lists[free_list_i] = NULL;
    }
    // Case where bp is the first free block
    else if (!GET(PREV_FREE_BLKP(bp)) && GET(NEXT_FREE_BLKP(bp))){
        logg(5, "Case B: block is the first free block in the list");
        PUT(PREV_FREE_BLKP(next_block_ptr), (uintptr_t)NULL);
        free_block_lists[free_list_i] = next_block_ptr;
    }
    // Case where bp is the last free block
    else if (GET(PREV_FREE_BLKP(bp)) && !GET(NEXT_FREE_BLKP(bp))){
        logg(5, "Case C: block is the last free block in the list");
        PUT(NEXT_FREE_BLKP(prev_block_ptr), (uintptr_t)NULL);
    }
    // Case where free blocks exist both before and after bp.
    else {
        logg(5, "Case D: free block exists in both directions");
        PUT(NEXT_FREE_BLKP(prev_block_ptr), (uintptr_t)NULL);
        PUT(PREV_FREE_BLKP(next_block_ptr), (uintptr_t)NULL);
    }
    logg(4, "============ remove_free_block() ends ==============");
    return;
}
コード例 #12
0
ファイル: mm-seglist.c プロジェクト: 447327642/CSAPP2e
inline static void remove_free_block(void* bp) {
#else
static void remove_free_block(void* bp) {
#endif
	/* get specific segregated free list */
	size_t free_list_num = get_list_num(GET_SIZE(HDRP(bp)));

	dbg1("[IN ] : remove_free_block()\n");

	/* remove block */
	if ((void*)NEXT_FREE_BLKP(bp) == NULL) {
		/* if next pointer is null */
		if ((void*)PREV_FREE_BLKP(bp) == NULL) {

			/* if prev pointer is null, make list empty */
			PUT8(FREE_LISTP(free_list_num), NULL);
			PUT8(FREE_LISTP_T(free_list_num), NULL);

			dbg1("[OUT] : remove_free_block() : list is empty\n");
			return;
		}
		/* remove from tail */
		PUT8(NEXT_FREEP(PREV_FREE_BLKP(bp)), NULL);
		PUT8(FREE_LISTP_T(free_list_num), PREV_FREE_BLKP(bp));
	}
	else if ((void*)PREV_FREE_BLKP(bp) == NULL) {
		/* if prev pointer is null, remove from head */
		PUT8(FREE_LISTP(free_list_num), NEXT_FREE_BLKP(bp));
		PUT8(PREV_FREEP(GET_FREE_LISTP(free_list_num)), NULL);
	}
	else {
		/* just remove from middle of list */
		PUT8(PREV_FREEP(NEXT_FREE_BLKP(bp)), PREV_FREE_BLKP(bp));
		PUT8(NEXT_FREEP(PREV_FREE_BLKP(bp)), NEXT_FREE_BLKP(bp));
	}
	dbg1("[OUT] : remove_free_block()\n");
}
コード例 #13
0
ファイル: pvikrama_malloc.c プロジェクト: pvikrama/Malloc
/*
 * Coalesce
 */
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));
	size_t list;

	/* If previous and next blocks are both allocated */
	if(prev_alloc && next_alloc)
	{
		/* Find the list number in which the block belongs and put it right 
		 * after the root node for that list
		 */
		list = listno(size);

		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);
		RESET_PREV_ALLOC(HDRP(NEXT_BLKP(bp)));
		return bp;
	}

	/* If only previous block is allocated */
	else if(prev_alloc && !next_alloc)
	{
		size += GET_SIZE(HDRP(NEXT_BLKP(bp)));

		list = listno(size);

		if(list >=maxlist)
			list=maxlist;
		/* Remove the next block from its free list */
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(NEXT_BLKP(bp))),
				GET(CURRENT_PREV_POINTER(NEXT_BLKP(bp))));
		PUT(CURRENT_NEXT_POINTER(PREV_FREE_BLKP(NEXT_BLKP(bp))),
				GET(CURRENT_NEXT_POINTER(NEXT_BLKP(bp))));

		/* Coalesce the current and next block*/
		PUT(HDRP(bp), PACK(size,prev_alloc,0));
		PUT(FTRP(bp), size);

		/* Put the newly coalesced block in front of the appropriate root node
		 * depending on its size
		 */
		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);
		return(bp);
	}

	/* If only next block is allocated */
	else if(!prev_alloc && next_alloc)
	{
		size+= GET_SIZE(HDRP(PREV_BLKP(bp)));
		size_t previous = GET_PREV_ALLOC(HDRP(PREV_BLKP(bp)));
		list = listno(size);

		if(list >=maxlist)
			list=maxlist;

		/* Remove the prev block from its free list */
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(PREV_BLKP(bp))),
				GET(CURRENT_PREV_POINTER(PREV_BLKP(bp))));
		PUT(CURRENT_NEXT_POINTER(PREV_FREE_BLKP(PREV_BLKP(bp))),
				GET(CURRENT_NEXT_POINTER(PREV_BLKP(bp))));


		/* Coalesce the current and prev block */
		PUT(FTRP(bp), size);
		PUT(HDRP(PREV_BLKP(bp)),PACK(size,previous,0));
		RESET_PREV_ALLOC(HDRP(NEXT_BLKP(PREV_BLKP(bp))));

		/* Put the newly coalesced block in front of the appropriate root node
		 * depending on its size
		 */
		PUT(CURRENT_NEXT_POINTER(PREV_BLKP(bp)),
				GET(CURRENT_NEXT_POINTER(root+ 8*list)));
		PUT(CURRENT_PREV_POINTER(PREV_BLKP(bp)),
				GET(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(PREV_BLKP(bp)))));
		PUT(CURRENT_NEXT_POINTER(root+ 8*list),
				(long)(PREV_BLKP(bp))-(long)heapstart);
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(PREV_BLKP(bp))),
				(long)(PREV_BLKP(bp))-(long)heapstart);

		return(PREV_BLKP(bp));
	}

	/* If prev and next blocks both are free */
	else 
	{
		size+= GET_SIZE(HDRP(PREV_BLKP(bp)))+GET_SIZE(FTRP(NEXT_BLKP(bp)));
		size_t previous = GET_PREV_ALLOC(HDRP(PREV_BLKP(bp)));
		list = listno(size);

		if(list >=maxlist)
			list=maxlist;
		/* Remove the next block from its free list */
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(NEXT_BLKP(bp))),
				GET(CURRENT_PREV_POINTER(NEXT_BLKP(bp))));
		PUT(CURRENT_NEXT_POINTER(PREV_FREE_BLKP(NEXT_BLKP(bp))),
				GET(CURRENT_NEXT_POINTER(NEXT_BLKP(bp))));

		/* Remove the previous block from its free list */
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(PREV_BLKP(bp))),
				GET(CURRENT_PREV_POINTER(PREV_BLKP(bp))));
		PUT(CURRENT_NEXT_POINTER(PREV_FREE_BLKP(PREV_BLKP(bp))),
				GET(CURRENT_NEXT_POINTER(PREV_BLKP(bp))));

		/* Coalesce the current, prev and next block */
		PUT(HDRP(PREV_BLKP(bp)),PACK(size,previous,0));
		PUT(FTRP(NEXT_BLKP(bp)),size);

		/* Put the newly coalesced block in front of the appropriate root node
		 * depending on its size
		 */
		PUT(CURRENT_NEXT_POINTER(PREV_BLKP(bp)),
				GET(CURRENT_NEXT_POINTER(root+ 8*list)));
		PUT(CURRENT_PREV_POINTER(PREV_BLKP(bp)),
				GET(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(PREV_BLKP(bp)))));
		PUT(CURRENT_NEXT_POINTER(root+ 8*list),
				(long)(PREV_BLKP(bp))-(long)heapstart);
		PUT(CURRENT_PREV_POINTER(NEXT_FREE_BLKP(PREV_BLKP(bp))),
				(long)(PREV_BLKP(bp))-(long)heapstart);
		return(PREV_BLKP(bp));
	}
}
コード例 #14
0
ファイル: mm.c プロジェクト: johannbrynjar/malloclab
/*
 * addBlckToFreeList - baetir blokk fremst i free listan
 */
static void addBlckToFreeList(void *bp) {
    NEXT_FREE_BLKP(bp) = free_listp;
    PREV_FREE_BLKP(bp) = NULL;
    PREV_FREE_BLKP(free_listp) = bp;
    free_listp = bp;
}