Example #1
0
//No. 2 Best fit algorithm                                                                                                                                                           
void * bf_malloc(size_t size){
  t_block b,last;//,size_of_whole_block;
  size_t s; 
  s = ALIGN_SIZE(size, sizeof(char*));
  //size_of_used_block += size;
  if (base){
    //find the best block
    last = base;
    b = find_best_block (&last, s);
    //size_of_whole_block=last;
    if (b){
      // size_of_whole_block->size += b->size;
      //is it big enough to be split                                                                                                                                                 
      if ((b->size - s)>= (BLOCK_SIZE + 4 ))
        split_block (b,s);
      b->free = 0;//mark the chunk as used                                                                                                                                           
    } else {//otherwise we extend the heap(NO FITTING BLOCK )                                                                                                                        
      b = extend_heap(last,s);
      if (!b)
        return NULL;
    }
  }
  else {//Heap is empty (first time allocation)                                                                                                                                      
    b = extend_heap(NULL,s);
    if(!b)
      return(NULL);
    base = b;
  }
  //size_of_whole_block->size += b->size;
  head = b;
  return (b-> data);
}
Example #2
0
/*
 * mm_malloc - Allocate a block by incrementing the brk pointer.
 */
void *mm_malloc(size_t size) {
  if (!size) return NULL;

  void *ret = NULL;
  size_t asize = align_with_min_bk_size(size + WSIZE);
  void *head = find_fit(asize);
  if (head) {

#if HEAP_CHECK
    assert(within_heap(head));
    assert(!addr_is_allocated(head));
    assert(!addr_is_payload(head));
#endif

    head = place_and_split(head, asize);
    ret = (char*)head + WSIZE;
  } else {
    void *heap_end_padding = (char*)heap_tail - WSIZE;
    if (!get_prev_alloc(heap_end_padding)) {
      void *footer = (char*)heap_end_padding - WSIZE;
      size_t end_bk_size = get_size(footer);
      head = extend_heap(asize - end_bk_size, 0);
    } else {
      head = extend_heap(asize, 0);
    }
    head = place_and_split(head, asize);
    ret = (char*)head + WSIZE;
  }

#if HEAP_CHECK
  add_to_alloc_list(ret, size, asize);
#endif

  return ret;
}
Example #3
0
void * wf_malloc (size_t size){
  t_block b, last;//, size_of_whole_block;
  size_t s;
  //aligning the requested size using the macro defined in my_malloc.h                                                                        
  s = ALIGN_SIZE(size, sizeof(char*));
  //if base is initialized, we can proceed to implement malloc                                                                                
  //size_of_used_block += size;
  if(base){
    //first find a block                                                                                                                      
    last = base;
    b = find_worst_block (&last,s);
    //size_of_whole_block=last;
    if (b){
      // size_of_whole_block->size += b->size;
      //is it big enough to be split                                                                                                          
      if((b->size - s)>= (BLOCK_SIZE + 4 ))
        split_block (b,s);
      b->free = 0;//mark the chunk as used                                                                                                    
    } else {//otherwise we extend the heap(NO FITTING BLOCK )                                                                                 
      b = extend_heap(last,s);
      if (!b)
        return NULL;
    }
  }
  else {//Heap is empty (first time allocation)                                                                                             
    b = extend_heap(NULL,s);
    if(!b)
      return(NULL);
    base = b;
  }
  //size_of_whole_block->size += b->size;
  head = b;
  return (b->data);
}
Example #4
0
void* mm_malloc(size_t size)
{
#ifdef MM_USE_STUBS
    return calloc(1, size);
#else
    s_block_ptr block, last;

    size_t s;

    s = align4(size);

    if(foot){
        last = foot;
        block = find_block(last, s);
        if(block){
            if((block->size - s) >= (BLOCK_SIZE + 4)) split_block(block, s);
            block->free = 0;
        }
        else{
            block = extend_heap(last, s);
            if(!block) return NULL;
        }
    }
    else{
        block = extend_heap(NULL, s);
        if(!block) return NULL;
        foot = block;
    }
    return block->data;
    //#error Not implemented.
#endif
}
//allocate memory
void *malloc(size_t size){
    t_block newBlock, last;
    size_t newSize = align8(size);

    if(base){
        last = (t_block) base;
        newBlock = find_block(&last, newSize);
        if(newBlock){
            //can we split
            if((newBlock->size - newSize) >= (OFFSET + PTR_SIZE)){
                split_block(newBlock, newSize);
            }

            newBlock->block_free = false;
        } else {
            //Can't find block, try extending the heap
            newBlock = extend_heap(last, newSize);
            if(!newBlock){
                //cannot extend heap
                return 0x0;
            }
        } 
    } else {
        //first call
        newBlock = extend_heap(0x0, newSize);
        if(!newBlock){
            //failed to extend
            return 0x0;
        }
        base = newBlock;
    }

    return newBlock->data;
}
Example #6
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;
	}	
} 
Example #7
0
File: mm.c Project: hshelton/cs4400
/* before calling mm_malloc or mm_free, the allocator must initialize
 the heap by calling mm_init */
int mm_init(void) {
 /* create the initial empty heap – four words */
 if ((heap_listp = mem_sbrk(4*WSIZE)) == NULL)
    return -1;

 PUT(heap_listp, 0); /* alignment padding */
 PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1)); /* prologue header */
 PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); /* prologue footer */
 PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); /* epilogue header */
 heap_listp += DSIZE; /* move heap_listp past prologue's header */

 void* heapStart;
 /* extend the empty heap with a free block of CHUNKSIZE bytes */
 if (heapStart = extend_heap(CHUNKSIZE/WSIZE) == NULL)
    return -1;

    /* set predecessor and successor to null */
    size_t successor = SCRP(heapStart);
    PUT(successor, NULL);

    size_t predecessor = PDRP(heapStart);
    PUT(predecessor, NULL);


 return 0;
}
Example #8
0
/**
 * mm_init - Initialize
 * @return  -1 on error, 0 on success.
 */
int mm_init(void) {
    /*Initialize global variables*/
    heap_listp = NULL;
    seg_list = NULL;
    
    /* Create the initial empty heap */
    if ((seg_list = mem_sbrk(NO_OF_LISTS*DSIZE)) == (void *)-1) 
        return -1;

    /*Initialize data in seg_list to NULL*/
    for (int i = 0; i < NO_OF_LISTS ; ++i)
    {
        SET_SEGI(seg_list,i,NULL);
    }

    if ((heap_listp = mem_sbrk(4*WSIZE)) == (void *)-1) 
        return -1;
    PUT(heap_listp, 0);                          /* Alignment padding */
    PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1)); /* Prologue header */ 
    PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1)); /* Prologue footer */ 
    PUT(heap_listp + (3*WSIZE), PACK(0, 1));     /* Epilogue header */
    heap_listp += (2*WSIZE);                     

    #ifdef NEXT_FIT
        rover = heap_listp;
    #endif

    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    if (extend_heap(CHUNKSIZE/WSIZE) == NULL) {
        dbg_printf("Error Mm_init\n");
        return -1;
    }
    return 0;
}
Example #9
0
/**
 * Calculate the adjusted size, asize, to include the header and footer
 * and round up if necessary to satisfy alignment requirement.
 * total size must be a multiple of 8
 * @param - size - requested size (payload only) of a block
 * @return - pointer to allocated block or null if nothing found
 */
void *mm_malloc(size_t size) {
	size_t asize;      /* adjusted block size */
	size_t extendsize; /* amount to extend heap if no fit */
	char *bp;

	/* Ignore spurious requests */
	if (size <= 0){
		return NULL;
	}

	/* Adjust block size to include overhead and alignment reqs */
	asize = MAX(ALIGN(size) + ALIGNMENT, BLKSIZE);

	/* Search the free list for a fit */
	if ((bp = find_fit(asize))) {
		place(bp, asize);
		return bp;
	}

	/* No fit found. Get more memory and place the block */
	extendsize = MAX(asize, BLKSIZE);
	/* return NULL if unable to get additional space */
	if ((bp = extend_heap(extendsize)) == NULL) {
		return NULL;
	}
	/* place block and return bp */
	place(bp, asize);
	return bp;
} 
Example #10
0
/**
 * malloc - Main function to allocate block of size bytes in heap
 * @param size size of heap to be allocated
 */
void *malloc (size_t size) {
    size_t asize;      /* Adjusted block size */
    size_t extendsize; /* Amount to extend heap if no fit */
    char *bp;      
    if (heap_listp == 0){
        mm_init();
    }
    /* Ignore spurious requests */
    if (size == 0)
       return NULL;

    /* Adjust block size to include overhead and alignment reqs. */
    if ( size <= DSIZE ) { 
        asize = 3*DSIZE;
    } else {
        /*add overhead and align */
        asize = ALIGN( DSIZE + size )  ;   
    }
    /* Search the free list for a fit */
    if (( bp = find_fit_segregated( asize )) != NULL ) { 
        place(bp, asize);                 
        return bp;
    }
    /*Still here, we need to extend the heap*/
    extendsize = MAX( asize, CHUNKSIZE );                 
    if (( bp = extend_heap( extendsize/WSIZE )) == NULL )  
        return NULL;                         
    place( bp, asize );                       
    return bp;
}
Example #11
0
/* $begin mmmalloc */
void *mm_malloc(size_t size) 
{
    size_t asize;      /* adjusted block size */
    size_t extendsize; /* amount to extend heap if no fit */
    char *bp;      
	

    /* Ignore spurious requests */
    if (size <= 0) {
        return NULL;
    }

    /* Adjust block size to include overhead and alignment reqs. */
    if (size <= DSIZE) {
        asize = DSIZE + OVERHEAD;
    }
    else {
        asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) / DSIZE);
    }
    
    /* Search the free list for a fit */
    if ((bp = find_fit(asize)) != NULL) {
        place(bp, asize);
        return bp;
    }

    /* No fit found. Get more memory and place the block */
    extendsize = MAX(asize,CHUNKSIZE);
    if ((bp = extend_heap((extendsize/WSIZE + 88))) == NULL) {
        return NULL;
    }
    place(bp, asize);
	//mm_checkheap(0);
    return bp;
} 
Example #12
0
/**********************************************************
 * mm_malloc
 * Allocate a block of size bytes.
 * The type of search is determined by find_fit
 * The decision of splitting the block, or not is determined
 *   in place(..)
 * If no block satisfies the request, the heap is extended
 **********************************************************/
void *mm_malloc(size_t size)
{
    size_t asize; /* adjusted block size */
    size_t extendsize; /* amount to extend heap if no fit */
    char * bp;
    int list_broken = 0;
    /* Ignore spurious requests */
    if (size == 0)
        return NULL;

    /* Adjust block size to include overhead and alignment reqs. */
    if (size <= DSIZE)
        asize = 2 * DSIZE;
    else
        asize = DSIZE * ((size + (DSIZE) + (DSIZE-1))/ DSIZE);

    /* Search the free list for a fit */
    if ((bp = find_fit(asize, &list_broken)) != NULL) {
        place(bp, asize, !list_broken);
        return bp;
    }

    /* No fit found. Get more memory and place the block */
    extendsize = MAX(asize, CHUNKSIZE);
    if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
        return NULL;
    place(bp, asize, 1);
    return bp;

}
Example #13
0
/*
 * malloc
 */
void *malloc (size_t size) {
dbg_printf("malloc size%u\n",(uint32_t)size);
	size_t asize;      /* Adjusted block size */
    size_t extendsize; /* Amount to extend heap if no fit */
    char *bp;      
//	mm_checkheap(1);

    /* Ignore spurious requests */
    if (size == 0)
	return NULL;

    /* Adjust block size to include overhead and alignment reqs. */
	asize=(size+15)&(~0x7);
    /* Search the free list for a fit */
    if ((bp = find_fit(asize)) != NULL) {  
		place(bp, asize);                  
		return bp;
    }

    /* No fit found. Get more memory and place the block */
    extendsize = MAX(asize,CHUNKSIZE);                
    if ((bp = extend_heap(extendsize)) == NULL)  
		return NULL;                                 
    place(bp, asize);                                 
    return bp;
}
Example #14
0
/* 
 * mm_malloc - Allocate a block by incrementing the brk pointer.
 *     Always allocate a block whose size is a multiple of the alignment.
 */
void *mm_malloc(size_t size) {
    
    size_t asize;
    size_t extendsize;
    char *bp;

    if(size == 0)       /* Ignore zero memory requests */
        return NULL;

    if(size <= DSIZE)
        asize = 2*DSIZE;
    else
        asize = ALIGN(size);

    if((bp = find_fit(asize)) != NULL){ //Finds "first fit" for size requested
        place(bp, asize);
        return bp;
    }

    extendsize = MAX(asize, CHUNKSIZE); //Increase heap if not enough room
    if((bp = extend_heap(extendsize/WSIZE)) == NULL)
        return NULL;
    place(bp, asize);
    return bp;

}
Example #15
0
/* $begin mminit */
int mm_init(void)
{
    void *bp;

    tree_root = NULL;

    /* Create the initial empty heap */
    if ((heap_listp = mem_sbrk(PROLOGSIZE)) == NULL)
        return -1;

    PUT(heap_listp, 0);                        /* alignment padding */
    PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1));  /* prologue header */
    PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1));  /* prologue footer */
    PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1));   /* epilogue header */
    heap_listp += DSIZE;

    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    bp = extend_heap(CHUNKSIZE/WSIZE);

    if (bp == NULL)
        return -1;

    tree_root = mm_insert(tree_root, bp);

    return 0;
}
Example #16
0
/*
 * malloc
 */
void *malloc (size_t size) {
    size_t a_size;
    size_t e_size;
    uint32_t *bp = NULL;

    if(size == 0)
	return NULL;

    if(size <= DSIZE)
	a_size = 2*DSIZE;
    else
	a_size = DSIZE * ((size + (DSIZE) + (DSIZE - 1)) / DSIZE);

    bp = find_first_fit(a_size);
    if(bp != NULL) {
	place(bp, a_size);
	return bp;
    }
    
    e_size = MAX(a_size, CHUNKSIZE);
    bp = extend_heap(e_size/WSIZE);
    if(bp != NULL) {
	place(bp, a_size);
	return bp;
    }
    if(0)
    	in_heap(bp);
    return NULL;
}
Example #17
0
/*
* mm_init - Initialize the malloc package. Construct prologue and epilogue
* blocks.
*/
int mm_init(void)
{
int list; // List counter
char *heap_start; // Pointer to beginning of heap
/* Initialize array of pointers to segregated free lists */
for (list = 0; list < LISTS; list++) {
free_lists[list] = NULL;
}
/* Allocate memory for the initial empty heap */
if ((long)(heap_start = mem_sbrk(4 * WSIZE)) == -1)
return -1;
PUT_NOTAG(heap_start, 0); /* Alignment padding */
PUT_NOTAG(heap_start + (1 * WSIZE), PACK(DSIZE, 1)); /* Prologue header */
PUT_NOTAG(heap_start + (2 * WSIZE), PACK(DSIZE, 1)); /* Prologue footer */
PUT_NOTAG(heap_start + (3 * WSIZE), PACK(0, 1)); /* Epilogue header */
prologue_block = heap_start + DSIZE;
/* Extend the empty heap */
if (extend_heap(CHUNKSIZE) == NULL)
return -1;
/* Variables for checking function
line_count = LINE_OFFSET;
skip = 0;
*/
return 0;
}
void *mm_malloc(size_t size)
{
	size_t total_size, extendsize; // adjusted block size 
	void *bp; // amount to extend heap if no fit 

	if (size == 0)//since size is null does no allocation
		return (NULL);

	// Adjusts the block size to include overhead and alignment requirments.
	if (size <= DSIZE)
		total_size = 2 * DSIZE;
	else
		total_size = DSIZE * ((size + DSIZE + (DSIZE - 1)) / DSIZE);

	if ((bp = find_fit(total_size)) != NULL) //finds the fit required to be placed from the exlicictly maintained list
	{

		place(bp, total_size);//places th block in the list
		return bp;
}

	extendsize = MAX(total_size,CHUNKSIZE);// calculates the max of the total required size and the previously provided chunk size
	if ((bp = extend_heap(extendsize/WSIZE)) == NULL)//extends the size of the heap if the size requirments is not met
		return NULL;


	place(bp, total_size); // places the block into the heap

	return bp;
}
Example #19
0
void *my_malloc(size_t size)
{
	block_t *tmp, *last;
	void *ptr;
	int block_notfound = 0;
	
	if (base != NULL) {
		for (tmp = base; tmp->next != NULL; tmp = tmp->next) {
			if ((block_notfound = test_free_block(tmp, size)) != 0)
				break;
		}
		if (block_notfound == 0) {
			block_notfound = test_free_block(tmp, size);
			last = tmp;
		}
	}
	else
		tmp = NULL;
	if (block_notfound == 0) {
		last = extend_heap(tmp, size);
		if (base == NULL)
			base = last;
		else
			tmp->next = last;
	}
	else
		last = tmp;
	if (last != NULL)
		return ((void*)last + HEADER_SIZE);
	else
		return NULL;
}
Example #20
0
File: mm.c Project: yihanwan/15513
/*
 * malloc
 * basic idea:
 * find the suitable free block from the free list
 * if not found, extend the heap.
 */
void *malloc (size_t size) {
	size_t asize; /* adjusted block size */
	void *bp;
	size_t extendsize;
	
	/* ignore spurious requests */
	if(size <= 0)
		return NULL;
	
	/* each block should be at least FSIZE
	 * so the alloc size should be at least DSIZE */
	if (size<DSIZE){
		size = DSIZE;
	}
	
	/* adjust block size to include hdr/ftr */
	asize = ALIGN(size + DSIZE);
	
	/* search the free list for a fit */
	if ((bp = find_fit(asize)) != NULL){
		place(bp, asize);
		return bp;
	}
	
	/* not fit found, get more memory and place the block */
	extendsize = MAX(asize, CHUNKSIZE);
	if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
		return NULL;
	place(bp, asize);
	return bp;
}
Example #21
0
/* $begin mmmalloc */
void *mm_malloc(size_t size)
{
    size_t asize;      /* adjusted block size */
    size_t extendsize; /* amount to extend heap if no fit */
    char *bp;

    /* Ignore spurious requests */
    if (size <= 0)
        return NULL;

    /* Adjust block size to include overhead and alignment reqs. */
    if (size <= DSIZE)
        asize = DSIZE + OVERHEAD;
    else
        asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) / DSIZE);

    /* Search the free list for a fit */
    if ((bp = mm_ceiling(tree_root,asize)) != NULL)
    {
        tree_root = mm_remove(tree_root,bp);
        bp = place(bp, asize);
        return bp;
    }

    /* No fit found. Get more memory and place the block */
    extendsize = MAX(asize,CHUNKSIZE);

    if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
        return NULL;

    bp = place(bp, asize);

    return bp;
}
Example #22
0
/* 
 * Requires:
 *   None.
 *
 * Effects:
 *   Initialize the memory manager.  Returns 0 if the memory manager was
 *   successfully initialized and -1 otherwise.
 */
int 
mm_init(void) 
{
	// WSIZE = 8 bytes            
	// DSIZE = 16 bytes             (2 * WSIZE)
	// MINBLOCKSIZE = 40 bytes      (5 * WSIZE)

	/*  |P_Header|Prev_blockptr|Next_blockptr|Data_space|P_footer|E_header| */
	/*  8 bytes  8 bytes       8 bytes       8 bytes    8 bytes  8 bytes    */
	/*  |------------------MINBLOCKSIZE--------------------------|          */

	/* Create the initial empty heap. */
	if ((heap_listp = mem_sbrk(2 * MINBLOCKSIZE)) == (void *)-1)  
		return (-1);
	PUT(heap_listp, PACK(MINBLOCKSIZE, 1));                /* Prologue header */ 
	PUT(heap_listp + (1 * WSIZE), 0);                      /* Pointer to PREV free block */
	PUT(heap_listp + (2 * WSIZE), 0);                      /* Pointer to NEXT free block */
	PUT(heap_listp + MINBLOCKSIZE, PACK(MINBLOCKSIZE, 1)); /* Prologue footer */ 
	PUT(heap_listp + MINBLOCKSIZE + WSIZE, PACK(0, 1));    /* Epilogue header */
	/* NOTE: use 0 because null gives compile errors */

	/* Should point to beginning of Prev_blockptr */
	//	heap_listp += (WSIZE); 

	free_listp = heap_listp + WSIZE;

	/* Extend the empty heap with a free block of CHUNKSIZE bytes. */
	if (extend_heap(CHUNKSIZE / WSIZE) == NULL)
		return (-1);
	return (0);
}
Example #23
0
/* 
 * Requires:
 *   None.
 *
 * Effects:
 *   Allocate a block with at least "size" bytes of payload, unless "size" is
 *   zero.  Returns the address of this block if the allocation was successful
 *   and NULL otherwise.
 */
void *
mm_malloc(size_t size) 
{
	size_t asize;      /* Adjusted block size */
	size_t extendsize; /* Amount to extend heap if no fit */
	void *bp;

	/* Ignore spurious requests. */
	if (size == 0)
		return (NULL);

	/* Adjust block size to include overhead and alignment reqs. */
	if (size <= DSIZE)
		asize = 2 * DSIZE;
	else
		asize = DSIZE * ((size + DSIZE + (DSIZE - 1)) / DSIZE);

	/* Search the free list for a fit. */
	if ((bp = find_fit(asize)) != NULL) {
		place(bp, asize);
		return (bp);
	}

	/* No fit found.  Get more memory and place the block. */
	extendsize = MAX(asize, CHUNKSIZE);
	if ((bp = extend_heap(extendsize / WSIZE)) == NULL)  
		return (NULL);
	place(bp, asize);
	return (bp);
} 
Example #24
0
void *mm_malloc(size_t size) {
    size_t asize;      /* adjusted block size */
    size_t extendsize; /* amount to extend heap if no fit */
    void *bp;

    /* Ignore spurious requests */
    if (size <= 0){
        return NULL;
    }

    /* least size is 24 bytes stored in BLKSIZE */
    asize = MAX(ALIGN(size) + DSIZE, BLKSIZE);

    /*we call findfit to traverse through the free list to find a fit for a malloc call*/

    if ((bp = find_fit(asize))) {
        place(bp, asize);
        return bp;
    }

    /* If unable to fetch a fit then simply extend a size */

    extendsize = MAX(asize,CHUNKSIZE);
    //extendsize = (asize);// first we were trying to increase  size as the requested size

    /* return NULL if unable to get additional space */
    if ((bp = extend_heap(extendsize)) == NULL) {
        return NULL;
    }
    /* place block and return bp */
    place(bp, asize);
    return bp;
}
Example #25
0
/*
 * Initialize: return -1 on error, 0 on success.
 */
int mm_init(void) {
 /* Create the initial empty heap */
//	dbg_printf("init\n");
	if ((heap_listp = mem_sbrk(24*WSIZE)) == (void *)-1) //line:vm:mm:begininit
	return -1;
	base=heap_listp;
   char *bp;
	int i=0;
	for (bp=heap_listp;i<10;i++){
		    *((size_t *) bp)=0;/* Alignment padding */
			bp=bp+DSIZE;
//	printf("init %p %lu\n",bp,*((size_t *)bp))	;//bp=(void *)((size_t * bp)+1);
		}
	PUT(bp,0);
	heap_listp =bp;          
dbg_printf("list%p",heap_listp); 
    PUT(heap_listp + (1*WSIZE), PACK(DSIZE, 1)); /* Prologue header */ 
    PUT(heap_listp + (2*WSIZE), PACK(DSIZE, 1)); /* Prologue footer */ 
    PUT(heap_listp + (3*WSIZE), PACK(0, 1));     /* Epilogue header */
    heap_listp += (2*WSIZE);                 
	heap_bottom=heap_listp+WSIZE;
	
dbg_printf("heap list%p\n",heap_listp);
	seg_list=base;

    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    if (extend_heap(CHUNKSIZE) == NULL) 
	return -1;

    return 0;
}
Example #26
0
/*
 * mm_malloc - Always allocate a block whose size is a multiple of the alignment.
 */
void *mm_malloc(size_t size)
{
    if (heap_listp == 0)
        mm_init();

    if (size == 0)
        return NULL;


    /* actual size = payload + header + footer + padding */
    size_t asize = ALIGN(size + DSIZE);
    void *bp;
    size_t expandsize;
    if ((bp = find_fit(last_found, asize)) != NULL) {
    } else {
        /* cannot find fit block in free list, ask kernel for more vm */
        expandsize = MAX(asize, CHUNKSIZE);
        if ((bp = extend_heap(expandsize / WSIZE)) == NULL)
            return NULL;
    }

    place(bp, asize);
    /* record the free block we found */
    last_found = NEXT_BLKP(bp);
    return bp;
}
/*
 * mm_init - Called when a new trace starts.
 */
int mm_init(void) {   
    if ((first_block = mem_sbrk(3 * WSIZE + NUMBER_OF_LISTS * DSIZE)) 
        == (void *) -1) 
        return -1;

    int list_offset = 2 * NUMBER_OF_LISTS;
    segregated_list = first_block + WSIZE;    

    memset(segregated_list, 0, NUMBER_OF_LISTS * DSIZE);

    /* Alignment padding */
    PUT(first_block, 0);    
    /* Prologue header */ 
    PUT(first_block + ((list_offset + 1)*WSIZE), PACK(WSIZE, 1));
    /* Epilogue header */
    PUT(first_block + ((list_offset + 2)*WSIZE), PACK(0, 1));     

    SET_TAG(first_block + ((list_offset + 1) * WSIZE));
    SET_TAG(first_block + ((list_offset + 2) * WSIZE));
    
    first_block += ((list_offset + 2) * WSIZE);

    /* Extend the empty heap with a free block of CHUNKSIZE bytes */
    if (extend_heap(CHUNKSIZE/WSIZE) == NULL)
        return -1;

    return 0;
}
/*
 * malloc
 */
void *malloc(size_t size) {
    
    dbg_printf("=== MALLOC! = %ld\n", size);
    
    size_t asize;       /* adjusted size */
    size_t extendsize;  /* amount to extend to heap */
    char *bp;
    
    if (size <= 0) {
        return NULL;
    }
    
    /* Least alignment size + two headers */
    if (size <= 2 * ALIGNMENT)
        asize = 2 * ALIGNMENT + DSIZE;
    else
        asize = WSIZE + ALIGN(size);    /* for > 16 bytes size, could remove footer when not need */
    
    
    /* search free list for a fit*/
    if ((bp = find_fit(asize)) != NULL) {
        place(bp, asize);
        
        return bp;
    }
    
    /* No fit found, get more memory */
    extendsize = MAX(asize, CHUNKSIZE);
    if ((bp = extend_heap((int)extendsize/WSIZE)) == NULL) {
        return NULL;
    }
    place(bp, asize);
    
    return bp;
}
Example #29
0
/*
 * malloc
 */
void *malloc (size_t size) {
	size_t asize;
	size_t extendsize;
	char *bp;

	/* Ignore spurious requests */
	if(size <= 0)
		return NULL;

	/* Adjust block size to include alignment and overhead requirements */
	if(size <= 12)
		asize = 16;
	else
		asize = DSIZE * ((size +4 + (DSIZE - 1))/DSIZE);

	if((bp = find_fit(asize))!=NULL)
	{
		place(bp,asize);
		return bp;
	}

	/* No fit found get more memory and place the block */
	extendsize = MAX(asize,CHUNKSIZE);
	if((bp=extend_heap(extendsize/WSIZE))==NULL)
		return NULL;
	place(bp,asize);
	return bp;
}
/* 
 * mm_malloc - Allocate a block by incrementing the brk pointer.
 *     Always allocate a block whose size is a multiple of the alignment.
 */
void *mm_malloc(size_t size)
{
    mm_log("\nmalloc(%d)\n\n", (int)size);

    ASSERT;
        
    char *bp;
    /* Ignore spurious requests */
    if (size == 0)
        return NULL;

    size_t asize = adjust_requested_block_size(size);
    
    /* Search the free list for a fit */
    bp = find_fit(asize);
    if (!bp)
    {
        mm_log("requesting more memory...\n");
        /* No fit found. Get more memory and place the block */
        size_t extendsize = MAX(asize,CHUNKSIZE);
        if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
            return NULL;
    }
    
    place(bp, asize);
    
    return bp;
}