Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.72 KB | None | 0 0
  1. /*
  2. * mm-naive.c - The fastest, least memory-efficient malloc package.
  3. *
  4. * In this naive approach, a block is allocated by simply incrementing
  5. * the brk pointer. A block is pure payload. There are no headers or
  6. * footers. Blocks are never coalesced or reused. Realloc is
  7. * implemented directly using mm_malloc and mm_free.
  8. *
  9. * NOTE TO STUDENTS: Replace this header comment with your own header
  10. * comment that gives a high level description of your solution.
  11. *
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <assert.h>
  16. #include <unistd.h>
  17. #include <string.h>
  18.  
  19. #include "mm.h"
  20. #include "memlib.h"
  21.  
  22. /*********************************************************
  23. * NOTE TO STUDENTS: Before you do anything else, please
  24. * provide your team information in below _AND_ in the
  25. * struct that follows.
  26. *
  27. * Note: This comment is parsed. Please do not change the
  28. * Format!
  29. *
  30. * === User information ===
  31. * Group: YEEHAW
  32. * User 1: Nökkvi Karlsson
  33. * SSN: 1508963639
  34. * User 2: Atli Gíslason
  35. * SSN: 0512992649
  36. * User 3:
  37. * SSN: X
  38. * === End User Information ===
  39. ********************************************************/
  40. team_t team = {
  41. /* Group name */
  42. "YEEHAW",
  43. /* First member's full name */
  44. "Student Studentsson",
  45. /* First member's email address */
  46. "student16@ru.is",
  47. /* Second member's full name (leave blank if none) */
  48. "",
  49. /* Second member's email address (leave blank if none) */
  50. "",
  51. /* Third full name (leave blank if none) */
  52. "",
  53. /* Third member's email address (leave blank if none) */
  54. ""
  55. };
  56.  
  57. /* $begin mallocmacros */
  58. /* Basic constants and macros */
  59. #define WSIZE 4 /* word size (bytes) */
  60. #define DSIZE 8 /* doubleword size (bytes) */
  61. #define CHUNKSIZE (1<<12) /* initial heap size (bytes) */
  62. #define OVERHEAD 8 /* overhead of header and footer (bytes) */
  63.  
  64. #define MAX(x, y) ((x) > (y)? (x) : (y))
  65.  
  66. /* Pack a size and allocated bit into a word */
  67. #define PACK(size, alloc) ((size) | (alloc))
  68.  
  69. /* Read and write a word at address p */
  70. #define GET(p) (*(size_t *)(p))
  71. #define PUT(p, val) (*(size_t *)(p) = (val))
  72.  
  73. /* (which is about 49/100).* Read the size and allocated fields from address p */
  74. #define GET_SIZE(p) (GET(p) & ~0x7)
  75. #define GET_ALLOC(p) (GET(p) & 0x1)
  76.  
  77. /* Given block ptr bp, compute address of its header and footer */
  78. #define HDRP(bp) ((char *)(bp) - WSIZE)
  79. #define FTRP(bp) ((char *)(bp) + GET_SIZE(HDRP(bp)) - DSIZE)
  80.  
  81. /* Given block ptr bp, compute address of next and previous blocks */
  82. #define NEXT_BLKP(bp) ((char *)(bp) + GET_SIZE(((char *)(bp) - WSIZE)))
  83. #define PREV_BLKP(bp) ((char *)(bp) - GET_SIZE(((char *)(bp) - DSIZE)))
  84. /* $end mallocmacros */
  85.  
  86. /* single word (4) or double word (8) alignment */
  87. #define ALIGNMENT 8
  88.  
  89. /* rounds up to the nearest multiple of ALIGNMENT */
  90. #define ALIGN(size) (((size) + (ALIGNMENT-1)) & ~0x7)
  91.  
  92.  
  93. #define SIZE_T_SIZE (ALIGN(sizeof(size_t)))
  94.  
  95. /* Finds previous or next free block */
  96. #define PREV_FREE_BLKP(bp) (*(char**)(bp))
  97. #define NEXT_FREE_BLKP(bp) (*(char**)(bp + WSIZE))
  98.  
  99. /* Global variables */
  100. static char *heap_listp; /* pointer to first block */
  101. //static char *free_listp; /* pointer to the first free block */
  102.  
  103. /* function prototypes for internal helper routines */
  104. static void *extend_heap(size_t words);
  105. static void place(void *bp, size_t asize);
  106. static void *find_fit(size_t asize);
  107. static void *coalesce(void *bp);
  108. static void printblock(void *bp);
  109. static void checkblock(void *bp);
  110.  
  111.  
  112. /*
  113. * mm_init - initialize the malloc package.
  114. */
  115. int mm_init(void)
  116. {
  117. /* create the initial empty heap */
  118. if ((heap_listp = mem_sbrk(4*WSIZE)) == NULL) {
  119. return -1;
  120. }
  121. PUT(heap_listp, 0); /* alignment padding */
  122. PUT(heap_listp+WSIZE, PACK(OVERHEAD, 1)); /* prologue header */
  123. PUT(heap_listp+DSIZE, PACK(OVERHEAD, 1)); /* prologue footer */
  124. PUT(heap_listp+WSIZE+DSIZE, PACK(0, 1)); /* epilogue header */
  125. heap_listp += DSIZE;
  126.  
  127. /* Extend the empty heap with a free block of CHUNKSIZE bytes */
  128. if (extend_heap(CHUNKSIZE/WSIZE) == NULL) {
  129. return -1;
  130. }
  131.  
  132. return 0;
  133. }
  134.  
  135. /*
  136. * Checks any invariants or consistency conditions.
  137. * It returns a non-zero value if our heap is consistent.
  138. */
  139. int mm_check(int verbose)
  140. {
  141. /* Code taken from the book commented by us */
  142. char *bp = heap_listp;
  143.  
  144. if (verbose) {
  145. printf("Heap (%p):\n", heap_listp);
  146. }
  147.  
  148. /* Checks if prologue header has 8 for size and is allocated */
  149. if ((GET_SIZE(HDRP(heap_listp)) != DSIZE) || !GET_ALLOC(HDRP(heap_listp))) {
  150. printf("Bad prologue header\n");
  151. }
  152.  
  153. /*
  154. * Checks if the heap_listp is aligned and checks if the header pointer
  155. * is the same as the footer pointer
  156. */
  157. checkblock(heap_listp);
  158.  
  159. /*
  160. * Loops through every block, checks if every block is aligned
  161. * and if its header matches its footer
  162. */
  163. for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
  164. if (verbose) {
  165. printblock(bp);
  166. }
  167. checkblock(bp);
  168. }
  169.  
  170. /* Prints epilogue header */
  171. if (verbose) {
  172. printblock(bp);
  173. }
  174. /* Checks weather the epilogue header is correct */
  175. if ((GET_SIZE(HDRP(bp)) != 0) || !(GET_ALLOC(HDRP(bp)))) {
  176. printf("Bad epilogue header\n");
  177. }
  178. /* Code from the book ends */
  179.  
  180. /* Our checks in pseudo/comment code */
  181. // We plan on implementing our free list with an Address-Ordered Policy
  182. // We implented checks on the suggested items given in the project
  183. // description and implemented them in commented pseudocode
  184.  
  185. // Is every block in the free list marked as free?
  186. /* for bp = first_free_blcok; bp != null; bp = bp->next (next free block)
  187. * if(GET_ALLOC(bp) == 1) {
  188. * printf("Hey this block %d is in the free list but not free", bp);
  189. * }
  190. */
  191.  
  192. // Are there contigious free blocks that somehow escaped coalescing?
  193. /* for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
  194. * if(GET_ALLOC(bp) == 0 && GET_ALLOC(NEXT_BLKP())
  195. *
  196. */
  197. return 0;
  198. }
  199.  
  200. /*
  201. * mm_malloc - Allocate a block by incrementing the brk pointer.
  202. * Always allocate a block whose size is a multiple of the alignment.
  203. */
  204. void *mm_malloc(size_t size)
  205. {
  206. /* Malloc code that was given with the mm.c file
  207. int newsize = ALIGN(size + SIZE_T_SIZE);
  208. void *p = mem_sbrk(newsize);
  209. if (p == (void *)-1) {
  210. return NULL;
  211. }
  212. else {
  213. *(size_t *)p = size;
  214. return (void *)((char *)p + SIZE_T_SIZE);
  215. }
  216. */
  217.  
  218. /* Code from mm-firstfit.c */
  219. size_t asize; /* adjusted block size */
  220. size_t extendsize; /* amount to extend heap if no fit */
  221. char *bp;
  222.  
  223. /* Ignore spurious requests */
  224. if (size <= 0) {
  225. return NULL;
  226. }
  227.  
  228. /* Adjust block size to include overhead and alignment reqs. */
  229. if (size <= DSIZE) {
  230. asize = DSIZE + OVERHEAD;
  231. }
  232. else {
  233. asize = DSIZE * ((size + (OVERHEAD) + (DSIZE-1)) / DSIZE);
  234. }
  235.  
  236. /* Search the free list for a fit */
  237. if ((bp = find_fit(asize)) != NULL) {
  238. place(bp, asize);
  239. return bp;
  240. }
  241.  
  242. /* No fit found. Get more memory and place the block */
  243. extendsize = MAX(asize,CHUNKSIZE);
  244. if ((bp = extend_heap(extendsize/WSIZE)) == NULL) {
  245. return NULL;
  246. }
  247. place(bp, asize);
  248.  
  249. mm_check(1);
  250.  
  251. return bp;
  252. }
  253.  
  254. /*
  255. * mm_free - Freeing a block does nothing.
  256. */
  257. void mm_free(void *ptr)
  258. {
  259. }
  260.  
  261. /*
  262. * mm_realloc - Implemented simply in terms of mm_malloc and mm_free
  263. */
  264. void *mm_realloc(void *ptr, size_t size)
  265. {
  266. void *oldptr = ptr;
  267. void *newptr;
  268. size_t copySize;
  269.  
  270. newptr = mm_malloc(size);
  271. if (newptr == NULL) {
  272. return NULL;
  273. }
  274. copySize = *(size_t *)((char *)oldptr - SIZE_T_SIZE);
  275. if (size < copySize) {
  276. copySize = size;
  277. }
  278. memcpy(newptr, oldptr, copySize);
  279. mm_free(oldptr);
  280. return newptr;
  281. }
  282.  
  283. /*
  284. * extend_heap - Extend heap with free block and return its block pointer
  285. */
  286. /* $begin mmextendheap */
  287. static void *extend_heap(size_t words)
  288. {
  289. char *bp;
  290. size_t size;
  291.  
  292. /* Allocate an even number of words to maintain alignment */
  293. size = (words % 2) ? (words+1) * WSIZE : words * WSIZE;
  294. if ((bp = mem_sbrk(size)) == (void *)-1) {
  295. return NULL;
  296. }
  297.  
  298. /* Initialize free block header/footer and the epilogue header */
  299. PUT(HDRP(bp), PACK(size, 0)); /* free block header */
  300. PUT(FTRP(bp), PACK(size, 0)); /* free block footer */
  301. PUT(HDRP(NEXT_BLKP(bp)), PACK(0, 1)); /* new epilogue header */
  302.  
  303. /* Coalesce if the previous block was free */
  304. return coalesce(bp);
  305. }
  306.  
  307. /*
  308. * place - Place block of asize bytes at start of free block bp
  309. * and split if remainder would be at least minimum block size
  310. */
  311. /* $begin mmplace */
  312. /* $begin mmplace-proto */
  313. static void place(void *bp, size_t asize)
  314. /* $end mmplace-proto */
  315. {
  316. size_t csize = GET_SIZE(HDRP(bp));
  317.  
  318. if ((csize - asize) >= (DSIZE + OVERHEAD)) {
  319. PUT(HDRP(bp), PACK(asize, 1));
  320. PUT(FTRP(bp), PACK(asize, 1));
  321. bp = NEXT_BLKP(bp);
  322. PUT(HDRP(bp), PACK(csize-asize, 0));
  323. PUT(FTRP(bp), PACK(csize-asize, 0));
  324. }
  325. else {
  326. PUT(HDRP(bp), PACK(csize, 1));
  327. PUT(FTRP(bp), PACK(csize, 1));
  328. }
  329. }
  330. /* $end mmplace */
  331.  
  332. /*
  333. * find_fit - Find a fit for a block with asize bytes
  334. */
  335. static void *find_fit(size_t asize)
  336. {
  337. /* first fit search */
  338. void *bp;
  339.  
  340. for (bp = heap_listp; GET_SIZE(HDRP(bp)) > 0; bp = NEXT_BLKP(bp)) {
  341. if (!GET_ALLOC(HDRP(bp)) && (asize <= GET_SIZE(HDRP(bp)))) {
  342. return bp;
  343. }
  344. }
  345. return NULL; /* no fit */
  346. }
  347.  
  348. /*
  349. * coalesce - boundary tag coalescing. Return ptr to coalesced block
  350. */
  351. static void *coalesce(void *bp)
  352. {
  353. size_t prev_alloc = GET_ALLOC(FTRP(PREV_BLKP(bp)));
  354. size_t next_alloc = GET_ALLOC(HDRP(NEXT_BLKP(bp)));
  355. size_t size = GET_SIZE(HDRP(bp));
  356.  
  357. if (prev_alloc && next_alloc) { /* Case 1 */
  358. return bp;
  359. }
  360. else if (prev_alloc && !next_alloc) { /* Case 2 */
  361. size += GET_SIZE(HDRP(NEXT_BLKP(bp)));
  362. PUT(HDRP(bp), PACK(size, 0));
  363. PUT(FTRP(bp), PACK(size,0));
  364. }
  365. else if (!prev_alloc && next_alloc) { /* Case 3 */
  366. size += GET_SIZE(HDRP(PREV_BLKP(bp)));
  367. PUT(FTRP(bp), PACK(size, 0));
  368. PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
  369. bp = PREV_BLKP(bp);
  370. }
  371. else { /* Case 4 */
  372. size += GET_SIZE(HDRP(PREV_BLKP(bp))) +
  373. GET_SIZE(FTRP(NEXT_BLKP(bp)));
  374. PUT(HDRP(PREV_BLKP(bp)), PACK(size, 0));
  375. PUT(FTRP(NEXT_BLKP(bp)), PACK(size, 0));
  376. bp = PREV_BLKP(bp);
  377. }
  378.  
  379. return bp;
  380. }
  381.  
  382.  
  383. static void printblock(void *bp)
  384. {
  385. size_t hsize, halloc, fsize, falloc;
  386.  
  387. hsize = GET_SIZE(HDRP(bp));
  388. halloc = GET_ALLOC(HDRP(bp));
  389. fsize = GET_SIZE(FTRP(bp));
  390. falloc = GET_ALLOC(FTRP(bp));
  391.  
  392. if (hsize == 0) {
  393. printf("%p: EOL\n", bp);
  394. return;
  395. }
  396.  
  397. printf("%p: header: [%d:%c] footer: [%d:%c]\n", bp,
  398. hsize, (halloc ? 'a' : 'f'),
  399. fsize, (falloc ? 'a' : 'f'));
  400. }
  401.  
  402. static void checkblock(void *bp)
  403. {
  404. if ((size_t)bp % 8) {
  405. printf("Error: %p is not doubleword aligned\n", bp);
  406. }
  407. if (GET(HDRP(bp)) != GET(FTRP(bp))) {
  408. printf("Error: header does not match footer\n");
  409. }
  410. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement