Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void *mm_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 (needs to be changed)*/
- if((bp = find_fit(asize)) != NULL)
- {
- //find_fit has found a valid node
- /* Code for maintenence of the free list, so it isn't destroyed */
- if((GET_PREV_FREE(HDRP(bp)) != NULL) && (GET_NEXT_FREE(HDRP(bp)) != NULL))
- {
- //The case where we're in a middle portion of our doubly linked free node list
- temp = GET_PREV_FREE(HDRP(bp));
- temp2 = GET_NEXT_FREE(HDRP(bp));
- PUT_NEXT_FREE(HDRP(temp), temp2);
- PUT_PREV_FREE(HDRP(temp2), temp);
- }
- else if(bp == free_list)
- {
- temp = GET_NEXT_FREE(HDRP(bp));
- free_list = temp;
- PUT_PREV_FREE(HDRP(free_list), 0);
- }
- else if(GET_PREV_FREE(HDRP(bp)) == 0)
- {
- temp = GET_PREV_FREE(HDRP(bp));
- PUT_NEXT_FREE(HDRP(temp), 0);
- }
- else {
- printf("Something went horribly wrong! You shouldn't be here!");
- exit(-1);
- }
- 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement