Advertisement
Guest User

Untitled

a guest
Dec 9th, 2016
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. void *mm_malloc(size_t size)
  2. {
  3.  
  4. size_t asize; /* Adjusted block size */
  5. size_t extendsize; /* Amount to extend heap if no fit */
  6. char *bp;
  7.  
  8. if (heap_listp == 0){
  9. mm_init();
  10. }
  11. /* Ignore spurious requests */
  12. if (size == 0)
  13. return NULL;
  14.  
  15. /* Adjust block size to include overhead and alignment reqs. */
  16. if (size <= DSIZE)
  17. asize = 2*DSIZE;
  18. else
  19. asize = DSIZE * ((size + (DSIZE) + (DSIZE-1)) / DSIZE);
  20.  
  21. /* Search the free list for a fit (needs to be changed)*/
  22. if((bp = find_fit(asize)) != NULL)
  23. {
  24. //find_fit has found a valid node
  25. /* Code for maintenence of the free list, so it isn't destroyed */
  26.  
  27. if((GET_PREV_FREE(HDRP(bp)) != NULL) && (GET_NEXT_FREE(HDRP(bp)) != NULL))
  28. {
  29. //The case where we're in a middle portion of our doubly linked free node list
  30. temp = GET_PREV_FREE(HDRP(bp));
  31. temp2 = GET_NEXT_FREE(HDRP(bp));
  32. PUT_NEXT_FREE(HDRP(temp), temp2);
  33. PUT_PREV_FREE(HDRP(temp2), temp);
  34.  
  35. }
  36. else if(bp == free_list)
  37. {
  38. temp = GET_NEXT_FREE(HDRP(bp));
  39. free_list = temp;
  40. PUT_PREV_FREE(HDRP(free_list), 0);
  41. }
  42. else if(GET_PREV_FREE(HDRP(bp)) == 0)
  43. {
  44. temp = GET_PREV_FREE(HDRP(bp));
  45. PUT_NEXT_FREE(HDRP(temp), 0);
  46. }
  47. else {
  48. printf("Something went horribly wrong! You shouldn't be here!");
  49. exit(-1);
  50. }
  51.  
  52. place(bp, asize);
  53. return bp;
  54. }
  55.  
  56. /* No fit found. Get more memory and place the block */
  57. extendsize = MAX(asize,CHUNKSIZE);
  58. if ((bp = extend_heap(extendsize/WSIZE)) == NULL)
  59. return NULL;
  60.  
  61. place(bp, asize);
  62. return bp;
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement