heap_merge_with_previous.c 1.3 KB

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