heap_graft_to_top.c 1.5 KB

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