heap_extend_upwards.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. #include "private.h"
  2. /*--------------------------------------------------------- */
  3. bool_t
  4. lub_heap_extend_upwards(lub_heap_t * this,
  5. lub_heap_block_t ** ptr_block, words_t words)
  6. {
  7. lub_heap_block_t *block = *ptr_block;
  8. lub_heap_free_block_t *next_block =
  9. (lub_heap_free_block_t *) lub_heap_block_getnext(block);
  10. bool_t result = BOOL_FALSE;
  11. if (NULL != next_block) {
  12. /* Do we have a free block above us? */
  13. if (1 == next_block->tag.free) {
  14. int segment;
  15. void *tmp;
  16. lub_heap_tag_t *tail =
  17. lub_heap_block__get_tail((lub_heap_block_t *)
  18. next_block);
  19. /* work out how many extra words we need */
  20. words -= block->alloc.tag.words;
  21. /* remove the block from the tree */
  22. lub_bintree_remove(&this->free_tree, next_block);
  23. /* remember the segment status of the next block */
  24. segment = tail->segment;
  25. /* try and slice off a chunk of memory */
  26. tmp =
  27. lub_heap_slice_from_bottom(this, &next_block,
  28. &words, BOOL_FALSE);
  29. if (NULL != next_block) {
  30. /* put the modified free block back into the tree */
  31. lub_bintree_insert(&this->free_tree,
  32. next_block);
  33. /* there is still a block above us */
  34. segment = 0;
  35. }
  36. if (NULL != tmp) {
  37. /* we managed to extend upwards */
  38. result = BOOL_TRUE;
  39. /* taint the old tail pointer */
  40. tail = lub_heap_block__get_tail(block);
  41. lub_heap_taint_memory((char *)tail,
  42. LUB_HEAP_TAINT_ALLOC,
  43. sizeof(lub_heap_tag_t));
  44. /* fill out the new block details */
  45. block->alloc.tag.words += words;
  46. this->stats.alloc_bytes += (words << 2);
  47. this->stats.alloc_total_bytes += (words << 2);
  48. tail = lub_heap_block__get_tail(block);
  49. tail->segment = segment;
  50. tail->free = 0;
  51. tail->words = block->alloc.tag.words;
  52. }
  53. }
  54. }
  55. return result;
  56. }
  57. /*--------------------------------------------------------- */