Exemplo n.º 1
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;
}
Exemplo n.º 2
0
/*
 * mm_malloc - Allocate a block with at least size bytes of payload
 */
void *malloc(size_t size)
{
    dbg_printf("Calling mm_malloc........");
    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 + SIZE_T_SIZE), MIN_BLKSIZE);
    
    /* Search the free list for a fit */
    if ((bp = find_fit(asize)) != NULL) {
        place(bp, asize);
        dbg_printf("place succeed: ");
        dbg_printblock(bp);
        dbg_printblock(NEXT_BLKP(bp));
        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;
}
Exemplo n.º 3
0
/* 
 *  * mm_malloc - Allocates a block of at least the size (plus header/footers)
 *   */
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 = 2*DSIZE;       
    else
	asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE); 

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

    /* If there isn't a fit, then extend the heap */
    extendsize = MAX(asize,CHUNKSIZE);     
    if ((bp = extend_heap(extendsize/WSIZE)) == NULL)  
	return NULL;       
    place(bp, asize);   
    return bp;
} 
Exemplo n.º 4
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)
        return NULL;

    /*Adjust block size to include overhead and alignment requirements */
    if (size <= DSIZE)
        asize = 3*DSIZE;
    else
        asize = DSIZE * ((size + (DOUBLEDSIZE) + (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;
}
Exemplo n.º 5
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;
  
  //Ignore idiots
  if (size <= 0)
    return NULL;
  
  //Adjust blk size to include overheard and alignment reqs
  if (size <= DSIZE)
    asize = 2 * DSIZE;
  else
    asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);  //da fuq, doesn't know basic math but should allign
  
  //Search free list for a fit
  if((bp = find_fit(asize)) != NULL){
    place(bp, asize);
    return bp;
  }
  
  //No fit found, request more memory
  extendsize = MAX(asize, EXHEAPSIZE);
  if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
    return NULL;
  place(bp, asize);
  return bp;
}
Exemplo n.º 6
0
/*
 * mm_malloc - Allocate a block with at least size bytes of payload
 */
void *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);
    asize = ALIGN(size + SIZE_T_SIZE);
    
    /* 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;
}
Exemplo n.º 7
0
//
// mm_malloc - Allocate a block with at least size bytes of payload 
//
void *mm_malloc(size_t size) 
{
  size_t asize;	     //Adjust 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
  if (size <= DSIZE)
    asize = 2*DSIZE;
  else
    asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);

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

  // No fit found. Get more memeory and place the block 
  extendsize = MAX(asize, CHUNKSIZE);
  if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
    return NULL;
  place(bp, asize);
  return bp;
} 
Exemplo n.º 8
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;      

	/* $end mmmalloc */
    if (heap_listp == 0){
		mm_init();
    }
	/* $begin mmmalloc */
    /* Ignore spurious requests */
    if (size == 0)
		return NULL;

    /* Adjust block size to include overhead and alignment reqs. */
    if (size <= DSIZE)                                          //line:vm:mm:sizeadjust1
		asize = 2*DSIZE;                                        //line:vm:mm:sizeadjust2
    else
		asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE); //line:vm:mm:sizeadjust3

    /* Search the free list for a fit */
    if ((bp = find_fit(asize)) != NULL) {  //line:vm:mm:findfitcall
		place(bp, asize);                  //line:vm:mm:findfitplace
		return bp;
    }

    /* No fit found. Get more memory and place the block */
    extendsize = MAX(asize,CHUNKSIZE);                 //line:vm:mm:growheap1
    if ((bp = extend_heap(extendsize/WSIZE)) == NULL)  
		return NULL;                                  //line:vm:mm:growheap2
    place(bp, asize);                                 //line:vm:mm:growheap3
    return bp;
} 
Exemplo n.º 9
0
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 = DSIZE * ((size + (WSIZE) + (DSIZE-1)) / DSIZE);

        /* Search the free list for a fit */
        bp = find_fit(asize);

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

        /* Here, we found a fit.  Just place info into it. */
        place(bp, asize);
        return bp;
}
Exemplo n.º 10
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;
}
Exemplo n.º 11
0
/* 
 * malloc - Allocate a block with at least size bytes of payload 
 */
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 = 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;
} 
Exemplo n.º 12
0
//Here the bp is given to the user if he requests for space in heap.Also here the size allignment issue is taken care of by considering min block size and padding issues
void *mm_malloc(size_t size)
{
    size_t asize;                                               
    size_t extendedsize;                                        
    char *bp;                                                   

    if(size <= 0){                                              
        return NULL;
    }

    asize = MAX(ALIGN(size) + DSIZE, OVERHEAD);                 

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

    extendedsize = MAX(asize, CHUNKSIZE);                       

    if((bp = extend_heap(extendedsize / WSIZE)) == NULL){       
        return NULL;                                            
    }

    place(bp, asize);                                           
    return bp;
}
Exemplo n.º 13
0
void *mm_malloc(unsigned int size)
{
	unsigned int asize; /* adjusted block size */
	unsigned int extendsize; /* amount to extend heap if no fit */
	char *bp;

	if(size <= 0)
		return NULL;
	
	/* adjusted 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 = (char*)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 = (char*)extend_heap(extendsize/wsize)) == NULL)
		return NULL;
	place(bp,asize);
	unusedMem -= asize;
	return bp;
}
Exemplo n.º 14
0
void *mm_malloc(size_t size) {

	//The malloc routine which allocates free blocks

	//if we are given zero size, then there's nothing to malloc!
	if (size == 0) return NULL ;

	int new_size = ALIGN(size + BLK_HDR_FTR_SIZE);

	//check if there's a free block that will contain that size
	blockHdr *free_block = find_fit(new_size);

	if (free_block == NULL ) {
		//if there's no free block, then create one!
		//But keep track of epilogue before calling sbrk()!

		blockHdr *epilogue = GET_EPILOGUE;
		epilogue->size = BLK_HDR_SIZE;

		//call sbrk to get more space
		free_block = mem_sbrk(new_size + BLK_HDR_SIZE);

		//if there's an error return NULL
		if (free_block <= 0)
			return NULL ;

		//get the start of the freeblock
		free_block = (blockHdr *) ((char *) free_block - epilogue->size);

		//free_block size taking epilogue into account
		free_block->size = ((epilogue->size) + new_size) | 1;

		//now set the footer size of the newly created block
		blockFtr *ftr = (blockFtr *) ((char *) free_block - BLK_FTR_SIZE + ((free_block->size) & ~1));
		ftr->size = free_block->size;

		//adjust the epilogue
		epilogue = GET_EPILOGUE;
		epilogue->next_p = epilogue;
		epilogue->prior_p = epilogue;
		epilogue->size = BLK_HDR_SIZE | 1;

	} else {
		//otherwise, use the free block!
		//if there's too much space, split the space and put remainder in appropriate free list

		free_block = split_block(free_block->size, new_size, free_block);

		//use the space you now have
		free_block->size |= 1;
		blockFtr *ftr = (blockFtr *) ((char *) free_block - BLK_FTR_SIZE + ((free_block->size) & ~1));
		ftr->size |= 1; //set footer to allocated

		//and remove the free block from the doubly linked list
		remove_from_free_lists(free_block);

	}
	return (void *) ((char *) free_block + BLK_HDR_SIZE);
}
Exemplo n.º 15
0
void* _malloc(size_t size, malloc_data * md)
{
	if (size <= 0)
		return NULL;


	size += sizeof(allocated_node); // we need to store our info there, too
	if (size % 4 != 0)
		size += 4 - (size % 4);
		
	assert(size % 4 == 0); // the pointer must be 4-byte aligned
	
	size = MAX(size, sizeof(free_node));
	

	free_node* node = find_fit(size,md);
	

	if (!node)
	{
		md->db("No available space found for size %d. Getting more memory from the kernel", size);
		get_space(md->size / PAGE_SIZE, md); // get as much new as we had before.
        
        // try again
        node = find_fit(size, md);
		
		// it failed again, give up
		if (!node)
		{
		    md->db("No available space _again_. Giving up");
		    exit();
		    return NULL;
		}
	}
	
	void* p = allocate_free_space(node, size,md);
	
   // md->db("Alloc of %d at %p", size, p);

//    Mem_Dump(md);
	
	assert(p);
	return p;
}
Exemplo n.º 16
0
void *malloc(size_t size)
{
  if (size == 0)
    return NULL;

  if (size % ALIGNMENT != 0)
    size = (size + ALIGNMENT - 1) & ~(ALIGNMENT - 1);

  size += HEADER_PADDING;

  struct blk_t *blk = NULL;
  int sc_i = find_fit(size, &blk);

  /* Allocate a new block if no fit */
  if (blk == NULL) {
    if (allocate_new_blk() != 0) {
      return NULL;
    } else {
      sc_i = NUM_FREE_LISTS - 1;
      blk = free_lists[sc_i];
    }
  }

  /* Remove the block we're going to use from the free list */
  remove_from_flist(blk);

  /* Split the block into two pieces if possible */
  size_t sdiff = blk->size - size;
  if (sdiff > HEADER_PADDING) {
    struct blk_t *nb = (struct blk_t *)((intptr_t)blk + size);

    nb->size = sdiff;
    nb->free = 1;
    nb->fsucc = NULL;
    nb->fpred = NULL;

    blk->size = size;

    /* Patch up blk list pointers */
    nb->prev = blk;
    nb->next = blk->next;
    if (blk->next)
      blk->next->prev = nb;
    blk->next = nb;

    /* Put the new block into the free list */
    insert_into_flist(nb);
  }

  return (void *)((intptr_t)blk + HEADER_PADDING);
}
Exemplo n.º 17
0
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, BSIZE);



	/* 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, BSIZE);

	/* 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;

}
Exemplo n.º 18
0
/*
 * malloc
 */
void *malloc (size_t size) {
    checkheap(1);  // Let's make sure the heap is ok!
    unsigned int awords;  //Adjusted block size
    unsigned int ewords;  //Amount to extend heap if no matching
    uint32_t *block;
    uint32_t * heap_lastp = last_block();

    if (VERBOSE)
        printf("Malloc %d bytes\n", (int)size);

    /* Ignore 0 requests */
    if (size == 0)
        return NULL;
    /* Adjust size to include alignment and convert to multipes of 4 bytes */
    if (size <= DSIZE)
        awords = 2;
    else
        awords = (((size) + (DSIZE-1)) & ~0x7) / WSIZE;



    /* Search the free list for a fit */
    if ((block = find_fit(awords)) != NULL) {
        place(block, awords);
        //printf("3\n");
        return block_mem(block);        
    }

    /* No fit found. Get more memory and place the block */ 
    if (awords > CHUNKSIZE)
        ewords = awords;
    else if (0)
        ewords = awords;
    else
        ewords = CHUNKSIZE;
    if (block_free(heap_lastp)) {
        ENSURES(block_size(heap_lastp) < ewords);
        ewords = ewords - block_size(heap_lastp) + 2;
        //ewords += 2;
        //printf("1\n");
    } else {
        ewords += 2;  // ask for 2 more for the header and footer
        //printf("2\n");
    }

    if ((block = extend_heap(ewords)) == NULL)
            return NULL;
    place(block, awords);
    return block_mem(block);
}
Exemplo n.º 19
0
/*
 * malloc - Allocate a block by incrementing the brk pointer.
 * 	    Always allocate a block whose size is a 
 * 	     multiple of the alignment
 *
 * @return 	- generic pointer to first byte of allocated memory
 * 		- NULL if error occured during mem allocation
 */
void *malloc(size_t sizeToAlloc)
{
	/*int newsize = ALIGN(sizeToAlloc + SIZE_T_SIZE);
	unsigned char *p = mem_sbrk(newsize);
	
	if( (long)p < 0 )   if p is -1 or NULL, allocation error occured 
		return NULL;
	else
	{
		p += SIZE_T_SIZE;
		*SIZE_PTR(p) = newsize;
		
		return p;
	}*/
	
	size_t asize;	   /* Adjusted block size */
	size_t extendsize; /* Amount to extend heap by, if you run out of space */
	char *blockPtr;
	
	if( heap_ptr == 0 )
		mm_init();
	
	if( sizeToAlloc == 0 )
		return NULL;
		
	if( sizeToAlloc <= DSIZE )
		asize = 2*DSIZE;
	else
		asize = DSIZE * ( (sizeToAlloc + DSIZE + (DSIZE - 1)) / DSIZE );
	
	blockPtr = find_fit(asize);
	
	// Search the free list for a fit
	if( blockPtr != NULL )
	{
		place( blockPtr, asize );
		return blockPtr;
	}
	
	// No fit found. Get more memory and place the block
	extendsize = MAX(asize, CHUNK_SIZE);
	blockPtr = extend_heap(extendsize/WSIZE);
	if( blockPtr == NULL )
		return NULL;
	place( blockPtr, asize );
	
	return blockPtr;
}
Exemplo n.º 20
0
void* dmalloc(size_t numbytes, int flag) {
	if(!is_init) { 			//Initialize through sbrk call first time
		if(!dmalloc_init()) {
			return NULL;
		}
	}

	assert(numbytes > 0);

	/* Your code goes here */
	size_t space = ALIGN(numbytes);
	// go over the linked list, find the first fit
	//metadata_t* flpt = freelist;
	metadata_t* flpt = find_fit(space, flag);
	if (!flpt) {
		//return NULL;
		if (!(flpt = extend_heap(space))) {
			printf("extend heap failed! \n");
			return NULL;
		}
	}
	size_t rest = meta_size(flpt) - space;
	delete_node(flpt);
	if (rest < ALIGN(META_SIZE + FOOTER_SIZE + 1)) {
		set_alloc(flpt);
		set_alloc((metadata_t *)(to_footer(to_block(flpt))));
	}
	else {
		rest = rest - META_SIZE - FOOTER_SIZE;
		set_size(flpt, space);
		set_alloc(flpt);
		set_size((metadata_t *)(to_footer(to_block(flpt))), space);
		set_alloc((metadata_t *)(to_footer(to_block(flpt))));
		metadata_t* newmt = (metadata_t *)((void *)flpt + META_SIZE + space + FOOTER_SIZE);
		set_size(newmt, rest);
		set_free(newmt);
		set_size((metadata_t *)to_footer(to_block(newmt)), rest);
		set_free((metadata_t *)to_footer(to_block(newmt)));
		add_node(newmt);
	}
	void * result = to_block(flpt);
	assert(result != NULL);
	if (!result) {
		int a = 1;
	}
	return result;
}
Exemplo n.º 21
0
/*
 * malloc
 */
block_ptr 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 = 2 * DSIZE;
	else
		asize = DSIZE * ((size + (WSIZE) + (DSIZE - 1)) / DSIZE);

#ifdef DEBUG
	printf("\nMalloc request: size = %zu, rounded to %zu \033[41;37m[ID:%d]\033[0m\n", size, asize, operid++);
#endif

	/* Search the free list for a fit */
	if ((bp = find_fit(asize)) != NULL)
	{
#ifdef DEBUG
		{
			puts("Found fit!");
			checkblock(bp);
			printblock(bp);
		}
#endif
		place(bp, asize);
		return bp;
	}

	/* No fit found. Get more memory and place the block */
	extendsize = MAX(asize, BLOCKSIZE);
	if ((bp = extend_heap(extendsize / WSIZE)) == NULL)
		return NULL;
	place(bp, asize);
	return bp;
}
Exemplo n.º 22
0
void *mm_malloc(size_t size)
{
  int newsize = ALIGN(BLK_HDR_SIZE + size);
  blockHdr *bp = find_fit(newsize);
  if (bp == NULL) {
    bp = mem_sbrk(newsize);
    if ((long)bp == -1)
      return NULL;
    else
      bp->size = newsize | 1;
  } else {
    bp->size |= 1;
    bp->prior_p->next_p = bp->next_p;
    bp->next_p->prior_p = bp->prior_p;
  }
  return (char *)bp + BLK_HDR_SIZE;
}
Exemplo n.º 23
0
/**********************************************************
 * mm_malloc
 * Allocate a block of size bytes.
 * The type of search is determined by find_fit
 * The decision of splitting is determined by spiltBlock
 * 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;

    /* 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) {
        //remove from the free list
        removeFromFreeList(bp);

        //break the block into smaller one if possible
        bp = splitBlock(bp, asize);
        size_t bsize = GET_SIZE(HDRP(bp));

        //place the block 
        setBlockHeaderFooter(bp, bsize, 1);
        return bp;
    }

    /* No fit found. Get more memory and place the block */

    //Increasing chunksize by 16B gives huge improvment in binary test case.
    //we incease 16 for the reason that it matches size of header and footer
    extendsize = MAX(asize, CHUNKSIZE + 16);

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

    splitBlock(bp, asize);
    place(bp, asize);
    return bp;
}
Exemplo n.º 24
0
void *mm_malloc(size_t size)
{
	size_t asize, extendsize; 	 /* adjusted block size */
	char *bp;                  /* amount to extend heap if no fit */
	if (size <= 0) return NULL;
	if (size <= DSIZE)
		asize = DSIZE+OVERHEAD; 
	else
		asize = ALIGN(size+SIZE_T_SIZE); /* We used already given Macros ALIGN & SIZE_T_SIZE */
	if ((bp = find_fit(asize)) != NULL) {
		place(bp, asize);
		return bp;
	}
	extendsize = MAX(asize,CHUNKSIZE);
	if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
		return NULL;
	place(bp, asize);
	return bp;
}
Exemplo n.º 25
0
//
// mm_malloc - Allocate a block with at least size bytes of payload
//
void *mm_malloc(size_t size)
{
  //adjusted block size
  size_t asize;
  //ammount to extend heap if the new block doesnt fit
  size_t extendsize;
  char *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{
    //round size up to nearest mult of DSIZE then add DSIZE
    asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);
  }

  //search the free list for a fit
  if ((bp = find_fit(asize)) != NULL){
//    assert( is_on_free_list(bp) );
    place(bp, asize);
//    assert( ! is_on_free_list(bp) );
    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;
  }

  // assert( is_on_free_list(bp) );
  place(bp, asize);
  // assert( ! is_on_free_list(bp) );
  return bp;

}
Exemplo n.º 26
0
/*
 * malloc
 */
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;

    asize = ALIGN(size + 4); /* the overhead is payload + header(4 byte) + padding(optional) */
    asize = MAX(asize, MINSIZE * WSIZE); /* require minimum size */

    /* Search the free list for a fit */
    if ((bp = find_fit(asize)) != NULL) {  
#ifdef DEBUG    
        printf("malloc: before alloc.\n");
        mm_checkheap(1);
#endif            
        place(bp, asize);                  
#ifdef DEBUG
        printf("malloc: after alloc.\n");        
        mm_checkheap(1);
        printf("\n\n");
#endif        
        return bp;
    }

    /* No fit found. Get more memory and place the block */
    int tail_free = 0; // the free space we have in the tail of heap
    if (heap_tailp && !GET_ALLOC(HDRP(heap_tailp))) {
        tail_free = GET_SIZE(HDRP(heap_tailp));
    }
    extendsize = MAX(asize - tail_free,CHUNKSIZE);
    bp = extend_heap(extendsize/WSIZE);
    if (bp == NULL) 
        return NULL;
    place(bp, asize);
    return bp;
}
Exemplo n.º 27
0
void *malloc (size_t size) {
  //mm_checkheap(1);  // Let's make sure the heap is ok!
    size_t asize;      /* Adjusted block size */
    size_t extendsize; /* Amount to extend heap if no fit */
    void *bp;

    //printf ("malloc %d\n" ,(int)size);
    //print_list();
    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 = 2*DSIZE;
    else
        asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);

    if ((bp = find_fit(asize)) != NULL) {
      place(bp, asize);
      ENSURES ( (size_t)(bp)%8 == 0);
      //printf("returning from malloc\n");
      return bp;
    }

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

    REQUIRES (bp!=NULL);
    REQUIRES ((size_t)(bp)%8 == 0);
    place(bp, asize);
    //printf("returning from malloc\n");
    return bp;
}
Exemplo n.º 28
0
/**********************************************************
 * mm_malloc
 * Allocate a block of size bytes.
 * First search through the segregated free list to see if
 * there's a free block that fits. If so, return the block
 * pointer of the free block and create a new free block from
 * the difference. (in place() utility function)
 * If no block satisfies the request, the heap is extended
 **********************************************************/
void *mm_malloc(size_t size)
{
    logg(3, "\n============ mm_malloc() starts ==============");
    if (LOGGING_LEVEL>0)
        mm_check();
    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 = 2 * DSIZE;
    else
        asize = DSIZE * ((size + (DSIZE) + (DSIZE-1))/ DSIZE);

    // Align it the other way to calibrate for binary-bal.rep.
    if (asize % 32 == 0)
        asize += 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);
    logg(1, "mm_malloc(%zx(h)%zu(d)) returns bp: %p; with actual size: %zx", size, size, bp, asize);
    if (LOGGING_LEVEL>0)
        mm_check();
    logg(3, "============ mm_malloc() ends ==============\n");
    return bp;

}
Exemplo n.º 29
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, extendsize; 	 /* adjusted block size */
        char *bp;                  /* amount to extend heap if no fit */
        if (size <= 0) return NULL;  /* not applicable */
        if (size <= DSIZE)
             asize = DSIZE+OVERHEAD; /* 8+8=16 bytes block where minimum payload=8 bytes */
        else
             asize = ALIGN(size+SIZE_T_SIZE);
        if ((bp = find_fit(asize)) != NULL) { /* finds the first fit block in the heap */
            place(bp, asize); /* Places headers and footers in the block also checks whether splitting is needed or not */
            return bp;
        }

        /* Extends the Size of the current heap if no enough memory is available to satisfy the request */
        extendsize = MAX(asize,CHUNKSIZE);
        if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
            return NULL;
        place(bp, asize);
        return bp;
}
Exemplo n.º 30
0
/*
 * malloc
 */
void *malloc (size_t size) 
{
  if(size == 0 ) return NULL ;

  size_t aSize = size + (2 * WSIZE) ; // add on hdr and ftr
  if(aSize< MINBLOCKSIZE) aSize = MINBLOCKSIZE ;
  else {
    // aSize > MINBLOCKSIZE
    aSize = aSize + (ALIGNMENT - (aSize % ALIGNMENT)) ;
    ASSERT(aSize % ALIGNMENT == 0 ) ;    
  } 
  char* bp = find_fit(aSize) ;
  if(bp != NULL) {
    place(bp, aSize) ;
    dmm_checkheap(321) ; //CHECKHEAP!!!!!!!!!!!!!!!!!!!
    return (void*)bp ;
  }
  ASSERT(bp == NULL) ;
  dbg_printf("RETURNED NULL\n");
  return NULL;
}