Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////////////////////////////////////////////////////////
- // De-allocation/free() related functions
- // REQUIRED
- // Attempt to merge the block lower with the next block in
- // memory. Does nothing if lower is null or not EL_AVAILABLE and does
- // nothing if the next higher block is null (because lower is the last
- // block) or not EL_AVAILABLE. Otherwise, locates the next block with
- // el_block_above() and merges these two into a single block. Adjusts
- // the fields of lower to incorporate the size of higher block and the
- // reclaimed overhead. Adjusts footer of higher to indicate the two
- // blocks are merged. Removes both lower and higher from the
- // available list and re-adds lower to the front of the available
- // list.
- void el_merge_block_with_above(el_blockhead_t *lower){
- printf("merging... \n");
- printf("lower size: %lu\n", lower->size);
- if (lower == NULL || lower->state != EL_AVAILABLE) {printf("lower isn't available.\n"); return; }
- el_blockhead_t *higher = el_block_above(lower);
- if (higher == NULL || higher->state != EL_AVAILABLE) {
- printf("higher isn't available.\n");
- el_remove_block(el_ctl.used, lower);
- el_add_block_front(el_ctl.avail, lower);
- return;
- }
- el_blockfoot_t *hi_foot = el_get_footer(higher);
- el_remove_block(el_ctl.used, lower);
- printf("higher size: %lu\n", higher->size);
- el_remove_block(el_ctl.avail, higher);
- hi_foot->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
- lower->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
- el_add_block_front(el_ctl.avail, lower);
- printf("new size: %lu=%lu\n", hi_foot->size, lower->size);
- }
- void el_merge_block_with_below(el_blockhead_t *higher){
- printf("merging below...\n");
- printf("higher size: %lu\n", higher->size);
- printf("higher loc: %lu\n", PTR_MINUS_PTR(higher, el_ctl.heap_start));
- if (higher == NULL || higher->state != EL_AVAILABLE) {printf("higher isn't available.\n"); return; }
- el_blockhead_t *lower = el_block_below(higher);
- //printf("%lu\n", PTR_MINUS_PTR(PTR_MINUS_BYTES(lower, 1),el_ctl.heap_start));
- printf("lower loc: %lu\n", PTR_MINUS_PTR(lower, el_ctl.heap_start));
- if (lower == NULL || lower->state != EL_AVAILABLE) {
- printf("lower isn't available.\n");
- return;
- }
- el_blockfoot_t *hi_foot = el_get_footer(higher);
- printf("lower size: %lu\n", lower->size);
- el_remove_block(el_ctl.avail, lower);
- el_remove_block(el_ctl.avail, higher);
- hi_foot->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
- lower->size = lower->size + higher->size + EL_BLOCK_OVERHEAD;
- el_add_block_front(el_ctl.avail, lower);
- printf("new size: %lu=%lu\n", hi_foot->size, lower->size);
- }
- // REQUIRED
- // Free the block pointed to by the give ptr. The area immediately
- // preceding the pointer should contain an el_blockhead_t with information
- // on the block size. Attempts to merge the free'd block with adjacent
- // blocks using el_merge_block_with_above().
- void el_free(void *ptr){
- printf("freeing... \n");
- el_blockhead_t *block = PTR_MINUS_BYTES(ptr, sizeof(el_blockhead_t));
- if(block->state != EL_USED){
- return;
- }
- block->state = EL_AVAILABLE;
- el_merge_block_with_above(block);
- el_merge_block_with_below(block);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement