heap_graft_to_bottom.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #include <assert.h>
  2. #include "private.h"
  3. /*--------------------------------------------------------- */
  4. void
  5. lub_heap_graft_to_bottom(lub_heap_t * this,
  6. lub_heap_block_t * free_block,
  7. void *ptr,
  8. words_t words,
  9. bool_t seg_start, bool_t other_free_block)
  10. {
  11. lub_heap_tag_t *tail;
  12. words_t new_words;
  13. bool_t seg_end;
  14. assert(1 == free_block->free.tag.free);
  15. /* remove the previous block from the free tree */
  16. lub_bintree_remove(&this->free_tree, free_block);
  17. /* update the size of the next block */
  18. this->stats.free_bytes += (words << 2);
  19. new_words = words + free_block->free.tag.words;
  20. tail = lub_heap_block__get_tail(free_block);
  21. seg_end = tail->segment;
  22. if (BOOL_FALSE == other_free_block) {
  23. /*
  24. * taint the memory being given back
  25. * NB. we don't bother to taint all the memory if we are
  26. * combining two free blocks; they'll already be tainted.
  27. */
  28. lub_heap_taint_memory((char *)ptr, LUB_HEAP_TAINT_FREE,
  29. (words << 2) +
  30. sizeof(lub_heap_free_block_t));
  31. } else {
  32. /* clear the free block details */
  33. lub_heap_taint_memory((char *)free_block, LUB_HEAP_TAINT_FREE,
  34. sizeof(lub_heap_free_block_t));
  35. }
  36. free_block = ptr;
  37. /* make sure we retain the segment details */
  38. free_block->free.tag.free = 1;
  39. free_block->free.tag.words = new_words;
  40. free_block->free.tag.segment = seg_start;
  41. lub_bintree_node_init(&free_block->free.bt_node);
  42. /* and update the tail */
  43. tail->free = 1;
  44. tail->words = free_block->free.tag.words;
  45. tail->segment = seg_end;
  46. /* insert back into the tree */
  47. lub_bintree_insert(&this->free_tree, free_block);
  48. }
  49. /*--------------------------------------------------------- */