heap_extend_upwards.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "private.h"
  2. /*--------------------------------------------------------- */
  3. bool_t
  4. lub_heap_extend_upwards(lub_heap_t *this,
  5. lub_heap_block_t **ptr_block,
  6. words_t words)
  7. {
  8. lub_heap_block_t *block = *ptr_block;
  9. lub_heap_free_block_t *next_block = (lub_heap_free_block_t *)lub_heap_block_getnext(block);
  10. bool_t result = BOOL_FALSE;
  11. if(NULL != next_block)
  12. {
  13. /* Do we have a free block above us? */
  14. if(1 == next_block->tag.free)
  15. {
  16. int segment;
  17. void *tmp;
  18. lub_heap_tag_t *tail = lub_heap_block__get_tail((lub_heap_block_t *)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 = lub_heap_slice_from_bottom(this,&next_block,&words,BOOL_FALSE);
  27. if(NULL != next_block)
  28. {
  29. /* put the modified free block back into the tree */
  30. lub_bintree_insert(&this->free_tree,next_block);
  31. /* there is still a block above us */
  32. segment = 0;
  33. }
  34. if(NULL != tmp)
  35. {
  36. /* we managed to extend upwards */
  37. result = BOOL_TRUE;
  38. /* taint the old tail pointer */
  39. tail = lub_heap_block__get_tail(block);
  40. lub_heap_taint_memory((char*)tail,LUB_HEAP_TAINT_ALLOC,sizeof(lub_heap_tag_t));
  41. /* fill out the new block details */
  42. block->alloc.tag.words += words;
  43. this->stats.alloc_bytes += (words << 2);
  44. this->stats.alloc_total_bytes += (words << 2);
  45. tail = lub_heap_block__get_tail(block);
  46. tail->segment = segment;
  47. tail->free = 0;
  48. tail->words = block->alloc.tag.words;
  49. }
  50. }
  51. }
  52. return result;
  53. }
  54. /*--------------------------------------------------------- */