Exemplo n.º 1
0
/*
 * find_fit
 */
static void *find_fit(size_t asize)
{
	void *bp,*bpouter;
	size_t list = listno(asize);

	if(list >=maxlist)
		list=maxlist;
	char *startlist;
	/*Startlist denotes the root pointer of that list*/
	startlist = root + 8*list;

	/* First fit search 
	 * Outer loop iterates through the root node of each list
	 * Inner loop goes through free blocks of that particular list
	 */
	for(bpouter=startlist;bpouter != root + 72;bpouter = (char *)(bpouter) + 8)
	{
		for(bp=NEXT_FREE_BLKP(bpouter);bp != bpouter ;bp=NEXT_FREE_BLKP(bp))
		{	
			if(!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp))))
				return bp;
		}
	}

	/* If no match found return NULL */
	return NULL;
}
Exemplo n.º 2
0
/*
* 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!");

}
Exemplo n.º 3
0
Arquivo: mm.c Projeto: divd/ece454
void *remove_from_free_list(void *bp) {
	// printf("Removing %p from free list\n",bp);
	unsigned int payload_dwords = (GET_SIZE(HDRP(bp))-DSIZE) >> 4;
	int array_pos = get_array_position_malloc(payload_dwords);

	void *prev = NULL;
	void *cur = free_lists[array_pos] ;
	while((cur != NULL) && (cur != bp)) {
		prev = cur;
		cur = NEXT_FREE_BLKP(cur);
	}

	if (cur == NULL) {
		printf("bp: %p\n",bp);
		print_free_list();
		return NULL;
	}

	if (prev != NULL) {
		//somewhere later in the list
		*(void **)prev = NEXT_FREE_BLKP(cur);
	} else {
		//remove from head
		free_lists[array_pos] = NEXT_FREE_BLKP(cur);
	}

	return cur;
}
Exemplo n.º 4
0
/*
 * 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);
}
Exemplo n.º 5
0
Arquivo: mm.c Projeto: divd/ece454
/**********************************************************
 * find_fit
 * Traverse the heap searching for a block to fit asize
 * Return NULL if no free blocks can handle that size
 * Assumed that asize is aligned
 **********************************************************/
void * find_fit(size_t asize)
{

	// printf("Entered find fit.\n");
	// Divide by DSIZE (which is 16 bytes)
	unsigned int num_dwords_payload = (asize >> 4) - 1;
	int free_list_index = get_array_position_malloc(num_dwords_payload);

	if (free_list_index == FREE_LIST_SIZE - 1) {
		// look in the largest block range to find a fit
		void *best_fit_blkp = NULL;
		void *cur_blkp = free_lists[free_list_index];
		int best_fit = 9999, cur_fit;

		while(cur_blkp != NULL) {
			cur_fit = GET_SIZE(HDRP(cur_blkp)) - asize;
			if (cur_fit == 0) {
				// found the exact fit
				best_fit_blkp = cur_blkp;
				best_fit = 0;
				break;
			}
			else if (cur_fit > 0) {
				if ((best_fit_blkp == NULL) || (cur_fit < best_fit)) {
					// we don't have a previous best fit or this fit is better than our previous best fit
					best_fit_blkp = cur_blkp;
					best_fit = cur_fit;
				}
			}
			cur_blkp = NEXT_FREE_BLKP(cur_blkp);
		}
		// found a good fit. If not, will extend heap below
		if (best_fit_blkp != NULL) {
			// printf("Found best_fit blkp, leaving find fit.\n");
			remove_from_free_list(best_fit_blkp);
			return best_fit_blkp;
		}
	} else {
		while (free_list_index < FREE_LIST_SIZE) {
			void *memblk = (unsigned int *) free_lists[free_list_index];
			if (memblk == NULL) {
				free_list_index++;
			} else {
				free_lists[free_list_index] = NEXT_FREE_BLKP(memblk);
				// printf("Found memblkp, leaving find fit.\n");
				return memblk;
			}
		}
	}
	// printf("Found nothing, leaving find fit.\n");
	return NULL;
}
Exemplo n.º 6
0
/* "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);
}
Exemplo n.º 7
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)));
	}
}
Exemplo n.º 8
0
/* Performs first fit search on the free list, selecting first block 
with size greater than or equal to asize, otherwise returning NULL 
if bo big enough block. */
static void *find_fit(size_t asize) {
    void *bp;
    /*void* best_fit = NULL;
    int best_fit_leftover = INT_MAX;
    unsigned size;
    int current_leftover;

    for (bp = free_listp; GET_ALLOC(HDRP(bp)) == 0; bp = NEXT_FREE_BLKP(bp)) {
        size = GET_SIZE(HDRP(bp));

        if (asize <= size) {
            current_leftover = GET_SIZE(HDRP(bp)) - asize;
            if (asize == size)
                return bp;
            else {
                if (current_leftover < best_fit_leftover) { 
                    best_fit = bp;
                    best_fit_leftover = current_leftover;
                }
                
            }
        }
    } */

    for (bp = free_listp; GET_ALLOC(HDRP(bp)) == 0; bp = NEXT_FREE_BLKP(bp)) {
        if (asize <= GET_SIZE(HDRP(bp)))
            return bp;
    } 
    return NULL;
}
Exemplo n.º 9
0
inline static void* find_fit(size_t asize) {
#else
static void* find_fit(size_t asize) {
#endif
	int free_list_num = 0;
	void *bp = NULL;

	free_list_num = get_nonempty_list_num(asize);

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

	dbg1(" class? [%d]\n", free_list_num);
	
	/* find block that fits starting at appropriate free list */
	for(bp = GET_FREE_LISTP(free_list_num); bp != NULL; bp = NEXT_FREE_BLKP(bp)) {
		if(GET_SIZE(HDRP(bp)) >= asize) {
			dbg1("[OUT] : find_fit() : found block in the first non-empty list\n");
			return bp;
		}
	}

	/* if no class bigger than current class exists */
	if(free_list_num >= MAXCLASS) {
		dbg1("[OUT] : find_fit() : failed to find fit, should extend the heap\n");	
		return NULL;
	}

	/* return next class' first free block (if its empty it will contain NULL) */
	bp = GET_FREE_LISTP(free_list_num+1);

	checkheap();
	dbg1("[OUT] : find_fit() : failed to find fit, try next free list\n");
	return bp;
}
Exemplo n.º 10
0
Arquivo: mm.c Projeto: divd/ece454
/**********************************************************
 * mm_check
 * Check the consistency of the memory heap
 * Return nonzero if the heap is consistent.
 *********************************************************/
int mm_check(void){

	// printf("CHECKING HEAP\n");
	int num_blk = 0;
	void * cur_blkp = heap_listp;

	int lc1 = 0;
	// check if all free blocks are in the free list array
	while (cur_blkp != NULL && *(int *)cur_blkp != 1) {

		// printf("Entered loop 1\n");
//		lc1++;
//		if (lc1 > 1500)
//			exit(-1);
		size_t size = GET_SIZE(HDRP(cur_blkp));
		size_t alloc = GET_ALLOC(HDRP(cur_blkp));
		if (size == 0)
			break;
//		// printf("Block %d: Size = %u, Allocated = %u, Address = %p\n",num_blk, (unsigned int) size, (unsigned int) alloc, cur_blkp);
		num_blk++;
		if (alloc) {
			// add these checks later
		}
		else {
			//payload of the free block
			unsigned int payload_size = (size - DSIZE) >> 4;
			if (__builtin_popcount(payload_size) > 1) {
				// size is not a power of 2
				// printf("ERROR: Free block payload size is %u\n",payload_size);
				return 0;
			}
			void *head = free_lists[get_array_position_malloc(payload_size)];
			// int lc2 = 0;
			while (head != NULL) {
				// printf("Entered loop 2\n");
//				lc2++;
//					if (lc2 > 1000)
//						exit(-1);
				if (head == cur_blkp)
					break;
				// address of next free block in linked list should
				// be located at head
				// printf("head: %p index: %d\n", head, get_array_position_malloc(payload_size));
				head = NEXT_FREE_BLKP(head);
			}
			// lc2 = 0;
			// head was not found in the linked list at free_lists[i]
			if ((size > DSIZE) && (head == NULL)) {
				printf("ERROR: Free block %p was not found at free_list[%d]\n",head, get_array_position_malloc(payload_size));
				return 0;
			}
		}
		cur_blkp = NEXT_BLKP(cur_blkp);
		// printf("cur_blkp in heap: %p, size of block: %u", (int * ) cur_blkp, (unsigned int) GET_SIZE(HDRP(cur_blkp)));
	}
	// all blocks correctly accounted for
	return 1;
}
Exemplo n.º 11
0
/* 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;
}
Exemplo n.º 12
0
/*
 * 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)));
}
Exemplo n.º 13
0
void printlist(int i) {

		void *bp = GET_FREE_LISTP(i);
		
		printf("[freelist(%d)] printing...\n", i);
		for(;bp != NULL; bp = NEXT_FREE_BLKP(bp))
			printblock(bp);
		printf("[freelist(%d)] end of list\n", i);

}
Exemplo n.º 14
0
/*
 * find_fit - Leitar uppi svaedi fyrir akvedna staerd og svaedinu skilad,
 * ef svaedid er ekki til er skilad NULL.
 */
static void *find_fit(size_t asize) {
    /* First fit seacrh */
    void *bp;
    // Faerum okkur eftir free list thar til vid erum komin ad epilogue, sem er fraktekdi svaedi.
    for (bp = free_listp; GET_ALLOC(HDRP(bp)) == 0; bp = NEXT_FREE_BLKP(bp)) {
        // Ef staerdin er naegilega stor er henni skilad
        if ((asize <= GET_SIZE(HDRP(bp))) ) {
            return bp;
        }
    }

    return NULL; /* No fit */
}
Exemplo n.º 15
0
/**********************************************************
 * 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;
}
Exemplo n.º 16
0
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;
}
Exemplo n.º 17
0
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);
	}
}
Exemplo n.º 18
0
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");
}
Exemplo n.º 19
0
/**********************************************************
 * print_free_lists
 * Iterates through the array of free blocks with different sizes.
 * Print the list of free blocks at each index.
 *********************************************************/
void print_free_lists(){
    int i = 0;
    char *iter;
    printf("========== The free block lists ==========\n");
    for (i = 0; i<NUM_OF_FREE_LISTS; i++){
        iter = free_block_lists[i];
        printf("%d: ", i);
        while (iter!=NULL) {
            printf("%p(header:%zx;size:%zx)\t", iter, GET(HDRP(iter)), GET_SIZE(HDRP(iter)));
            iter = (char *)GET(NEXT_FREE_BLKP(iter));
            if (iter!=NULL)
                printf("%p\t", iter);
        }
        printf("\n");
    }
}
Exemplo n.º 20
0
void *find_fit(size_t asize) { 
	void *bp;

	/* This is the key thing of explicit Allocator. traversing only the free block pointers ...
	 * So directly checking till we find the pointer of the required size or greater size
	 * If found pointer of free block having greater size than required, we are anyway going to 
	 * call place(bp) function to split the memory.
	 */

	for (bp = root; bp != NULL; bp = (void *)NEXT_FREE_BLKP(bp)) {
		if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp)))){

			return bp;
		}
	}
	return NULL;    /* no fit */
}
Exemplo n.º 21
0
Arquivo: mm.c Projeto: divd/ece454
void print_free_list() {
	int i;
	unsigned int fsize, falloc;
	for (i = 0; i < FREE_LIST_SIZE; i++) {
		printf("free_list[%d]: ",i);
		void *cur = (void *)free_lists[i];
		while(cur != NULL) {
			fsize = GET_SIZE(HDRP(cur));
			// confirm that it is not allocated
			falloc = GET_ALLOC(HDRP(cur));
			printf("[addr = %p, size = %u]", cur, fsize);
			if(falloc)
				printf(", ERROR: ALLOCATED!!");
			printf("-->");
			cur = NEXT_FREE_BLKP(cur);
		}
		printf("\n");
	}
}
Exemplo n.º 22
0
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);
	}
}
Exemplo n.º 23
0
/**********************************************************
 * 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;
}
Exemplo n.º 24
0
/*
 * 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));
	}
}
Exemplo n.º 25
0
/*
 * 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;
}
Exemplo n.º 26
0
/**********************************************************
 * mm_check
 * Check the consistency of the memory heap and free block list.
 * Print error message and return nonzero if the heap is consistant.
 *********************************************************/
int mm_check(void){
    int i = 0;
    char *iter;
    int fail = 0;

    // Iterate through the list of free blocks and check: 1) un-aligned blocks; 2) in-consistant
    // footer / header and 3) blocks that are not free.
    for (i = 0; i<NUM_OF_FREE_LISTS; i++){
        iter = free_block_lists[i];
        while (iter!=NULL) {
            if ((size_t)iter%8){
                printf("FREEBLOCK ERROR: UN-ALIGNED BLOCK. bp: %p\n", iter);
                fail = 1;
                break;
            }
            if (GET(FTRP(iter)) != GET(HDRP(iter))) {
                printf("FREEBLOCK ERROR: INCONSISTANCY FOOTER / HEADER. index: %d; bp: %p; header: %zx; footer: %zx\n", i, iter, GET(HDRP(iter)), GET(FTRP(iter)));
                fail = 1;
                break;
            }
            if (GET_ALLOC(HDRP(iter))) {
                printf("FREEBLOCK ERROR: BLOCK NOT FREE. index: %d; bp: %p; header: %zx", i, iter, GET(HDRP(iter)));
                fail = 1;
                break;
            }
            iter = (char *)GET(NEXT_FREE_BLKP(iter));
        }
    }
    if (fail == 1){
        printf("************** mm_check() FAILS!!!!!! ***********");
        return 1;
    }

    // Iterate through the entire heap and check: 1) bp pointer actually lies inside the heap
    // allocated using mem_sbrk(); 2) un-aligned blocks; 2) in-consistant footer / header;
    // 3) free blocks that are not in the free list and 4) contiguour free blocks not coalesced.
    void* start_heap = mem_heap_lo();
    void* end_heap = mem_heap_hi();
    for (iter = (char *)start_heap + DSIZE; GET_SIZE(HDRP(iter)) > 0; iter=NEXT_BLKP(iter)){
        if (iter < (char *)start_heap) {
            printf("HEAP ERROR: BLOCK POINTER BEFORE START OF HEAP. bp: %p\n", iter);
            fail = 1;
            break;
        }
        if (iter > (char *)end_heap) {
            printf("HEAP ERROR: BLOCK POINTER AFTER END OF HEAP. bp: %p\n", iter);
            fail = 1;
            break;
        }
        if ((size_t)iter%8){
            printf("HEAP ERROR: UN-ALIGNED BLOCK. bp: %p\n", iter);
            fail = 1;
            break;
        }
        if (GET(FTRP(iter)) != GET(HDRP(iter))) {
            printf("HEAP ERROR: INCONSISTANCY FOOTER / HEADER. bp: %p; header: %zx; footer: %zx\n", iter, GET(HDRP(iter)), GET(FTRP(iter)));
            fail = 1;
            break;
        }
        if (!GET_ALLOC(HDRP(iter)) && !GET_ALLOC(HDRP(NEXT_BLKP(iter)))) {
            printf("HEAP ERROR: CONTIGUOUS FREE BLOCKS FOUND. bp: %p; header: %zx; bp.next: %p; bp.next.header: %zx\n", iter, GET(HDRP(iter)), NEXT_BLKP(iter), GET(HDRP(NEXT_BLKP(iter))));
            fail = 1;
            break;
        }


    }
    if (fail == 1){
        printf("************** mm_check() FAILS!!!!!! ***********");
        return 1;
    }
    return 0;
}
Exemplo n.º 27
0
int mm_check(void) {

    /* Heap Point Check */
    if(!GET_ALLOC(heap_listp) || *(int*)(heap_listp) != (3*WSIZE+1))
        printf("Heap Point Error!\n");

    /* Check every block in the free list marked as free*/
    void *bp;
    for (bp = free_listp; GET_SIZE(HDRP(bp)) > 0 && bp == epilogue; bp = NEXT_FREE_BLKP(bp)) {
        if(GET_ALLOC(HDRP(bp))) {
            printf("Free Blopck Allocate\n");
            printBlock(bp);
            return -1;
        }
    }

    /* Are there any contigouse free block that somehow escaped coalescing */
    int prevAloc = 1;
    for (bp = prologue; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if(!prevAloc && !GET_ALLOC(HDRP(bp))) {
            printf("Coalescing Fail\n");
            printf("Prev Block \n");
            printBlock(PREV_BLKP(bp));
            printf("BG Block \n");
            printBlock(bp);
            return -1;
        }
    }

    /* Is every free block actually in the free list */
    void *freebp = NULL;
    for (bp = prologue; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if(!GET_ALLOC(HDRP(bp))) {

            for (freebp = free_listp;
                    GET_SIZE(HDRP(freebp)) > 0 && freebp == epilogue;
                    freebp = NEXT_FREE_BLKP(freebp)) {
                if(bp == freebp) {
                    break;
                }
            }
        }
        if(freebp== epilogue) {
            printf("Free List not in List");
            printBlock(bp);
            return -1;
        }
    }

    /* Do the pointers in the free list point to valid free blocks? */
    for (bp = free_listp; GET_SIZE(HDRP(bp)) > 0 && bp == epilogue; bp = NEXT_FREE_BLKP(bp)) {
        if(GET_ALLOC(HDRP(NEXT_FREE_BLKP(bp))) && (NEXT_FREE_BLKP(bp) != epilogue)) {
            printf("Not Valid Next Free Block");
            printf("BG Block \n");
            printBlock(bp);
            printf("Next Block \n");
            printBlock(NEXT_BLKP(bp));
            return -1;
        }
    }

    /* Do any allocated blocks overlap */
    for (bp = prologue; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
        if(GET_ALLOC(HDRP(bp))) {
            if(NEXT_BLKP(HDRP(bp)) <= FTRP(bp)) {
                printf("Allocated Blocks overlaps:\n");
                printBlock(bp);
                printBlock(NEXT_BLKP(bp));
                return -1;
            }
        }
    }
    return 0;
}