Advertisement
Guest User

Untitled

a guest
Dec 13th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. ////////////////////////////////////////////////////////////////////////////////
  2. // De-allocation/free() related functions
  3.  
  4. // REQUIRED
  5. // Attempt to merge the block lower with the next block in
  6. // memory. Does nothing if lower is null or not EL_AVAILABLE and does
  7. // nothing if the next higher block is null (because lower is the last
  8. // block) or not EL_AVAILABLE. Otherwise, locates the next block with
  9. // el_block_above() and merges these two into a single block. Adjusts
  10. // the fields of lower to incorporate the size of higher block and the
  11. // reclaimed overhead. Adjusts footer of higher to indicate the two
  12. // blocks are merged. Removes both lower and higher from the
  13. // available list and re-adds lower to the front of the available
  14. // list.
  15. void el_merge_block_with_above(el_blockhead_t *lower){
  16. printf("merging... \n");
  17. printf("lower size: %lu\n", lower->size);
  18. if (lower == NULL || lower->state != EL_AVAILABLE) {printf("lower isn't available.\n"); return; }
  19.  
  20. el_blockhead_t *higher = el_block_above(lower);
  21.  
  22. if (higher == NULL || higher->state != EL_AVAILABLE) {
  23. printf("higher isn't available.\n");
  24. el_remove_block(el_ctl.used, lower);
  25. el_add_block_front(el_ctl.avail, lower);
  26. return;
  27. }
  28.  
  29. el_blockfoot_t *hi_foot = el_get_footer(higher);
  30.  
  31. el_remove_block(el_ctl.used, lower);
  32.  
  33. printf("higher size: %lu\n", higher->size);
  34.  
  35. el_remove_block(el_ctl.avail, higher);
  36.  
  37. hi_foot->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
  38.  
  39. lower->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
  40.  
  41. el_add_block_front(el_ctl.avail, lower);
  42. printf("new size: %lu=%lu\n", hi_foot->size, lower->size);
  43. }
  44.  
  45. void el_merge_block_with_below(el_blockhead_t *higher){
  46. printf("merging below...\n");
  47. printf("higher size: %lu\n", higher->size);
  48. printf("higher loc: %lu\n", PTR_MINUS_PTR(higher, el_ctl.heap_start));
  49.  
  50. if (higher == NULL || higher->state != EL_AVAILABLE) {printf("higher isn't available.\n"); return; }
  51.  
  52. el_blockhead_t *lower = el_block_below(higher);
  53.  
  54. //printf("%lu\n", PTR_MINUS_PTR(PTR_MINUS_BYTES(lower, 1),el_ctl.heap_start));
  55.  
  56. printf("lower loc: %lu\n", PTR_MINUS_PTR(lower, el_ctl.heap_start));
  57.  
  58. if (lower == NULL || lower->state != EL_AVAILABLE) {
  59. printf("lower isn't available.\n");
  60. return;
  61. }
  62.  
  63. el_blockfoot_t *hi_foot = el_get_footer(higher);
  64.  
  65. printf("lower size: %lu\n", lower->size);
  66.  
  67. el_remove_block(el_ctl.avail, lower);
  68.  
  69. el_remove_block(el_ctl.avail, higher);
  70.  
  71. hi_foot->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
  72.  
  73. lower->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
  74.  
  75. el_add_block_front(el_ctl.avail, lower);
  76.  
  77. printf("new size: %lu=%lu\n", hi_foot->size, lower->size);
  78.  
  79. }
  80.  
  81. // REQUIRED
  82. // Free the block pointed to by the give ptr. The area immediately
  83. // preceding the pointer should contain an el_blockhead_t with information
  84. // on the block size. Attempts to merge the free'd block with adjacent
  85. // blocks using el_merge_block_with_above().
  86. void el_free(void *ptr){
  87. printf("freeing... \n");
  88. el_blockhead_t *block = PTR_MINUS_BYTES(ptr, sizeof(el_blockhead_t));
  89.  
  90. if(block->state != EL_USED){
  91. return;
  92. }
  93.  
  94. block->state = EL_AVAILABLE;
  95.  
  96. el_merge_block_with_above(block);
  97.  
  98. el_merge_block_with_below(block);
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement