heap_extend_both_ways.c 1.3 KB

12345678910111213141516171819202122232425262728293031
  1. #include <assert.h>
  2. #include "private.h"
  3. /*--------------------------------------------------------- */
  4. bool_t
  5. lub_heap_extend_both_ways(lub_heap_t *this,
  6. lub_heap_block_t **ptr_block,
  7. const words_t words)
  8. {
  9. lub_heap_block_t *block = *ptr_block;
  10. lub_heap_free_block_t *prev_block = (lub_heap_free_block_t*)lub_heap_block_getprevious(block);
  11. lub_heap_free_block_t *next_block = (lub_heap_free_block_t*)lub_heap_block_getnext(block);
  12. bool_t result = BOOL_FALSE;
  13. if(prev_block && prev_block->tag.free && next_block && next_block->tag.free)
  14. {
  15. /* OK we have a free block above and below us */
  16. if(words <= (prev_block->tag.words + next_block->tag.words))
  17. {
  18. /* There is sufficient space to extend */
  19. words_t delta = words - next_block->tag.words;
  20. /* maximise the upward extension */
  21. result = lub_heap_extend_downwards(this,&block,delta);
  22. assert(BOOL_TRUE == result);
  23. result = lub_heap_extend_upwards(this,&block,next_block->tag.words);
  24. assert(BOOL_TRUE == result);
  25. }
  26. }
  27. return result;
  28. }
  29. /*--------------------------------------------------------- */