heap_merge_with_previous.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. #include "private.h"
  2. /*--------------------------------------------------------- */
  3. lub_heap_status_t
  4. lub_heap_merge_with_previous(lub_heap_t *this,
  5. lub_heap_block_t *block)
  6. {
  7. lub_heap_status_t result = LUB_HEAP_FAILED;
  8. lub_heap_block_t *prev_block;
  9. do
  10. {
  11. /* see whether there is a free block just before us */
  12. prev_block = lub_heap_block_getprevious(block);
  13. if( (NULL != prev_block )
  14. && (1 == prev_block->free.tag.free ))
  15. {
  16. lub_heap_tag_t *tail;
  17. if(BOOL_FALSE == lub_heap_block_check(prev_block))
  18. {
  19. result = LUB_HEAP_CORRUPTED;
  20. break;
  21. }
  22. tail = lub_heap_block__get_tail(block);
  23. if(1 == block->free.tag.free)
  24. {
  25. /* remove this free block from the tree */
  26. lub_bintree_remove(&this->free_tree,block);
  27. --this->stats.free_blocks;
  28. this->stats.free_bytes -= (block->free.tag.words << 2);
  29. this->stats.free_bytes += sizeof(lub_heap_alloc_block_t);
  30. this->stats.free_overhead -= sizeof(lub_heap_alloc_block_t);
  31. }
  32. /* now add this memory to the previous free block */
  33. lub_heap_graft_to_top(this,
  34. prev_block,
  35. block,
  36. block->alloc.tag.words,
  37. tail->segment,
  38. block->free.tag.free ? BOOL_TRUE : BOOL_FALSE);
  39. result = LUB_HEAP_OK;
  40. }
  41. } while(0);
  42. return result;
  43. }
  44. /*--------------------------------------------------------- */