1
0

heap_extend_both_ways.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  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, const words_t words)
  7. {
  8. lub_heap_block_t *block = *ptr_block;
  9. lub_heap_free_block_t *prev_block =
  10. (lub_heap_free_block_t *) lub_heap_block_getprevious(block);
  11. lub_heap_free_block_t *next_block =
  12. (lub_heap_free_block_t *) lub_heap_block_getnext(block);
  13. bool_t result = BOOL_FALSE;
  14. if (prev_block && prev_block->tag.free && next_block
  15. && next_block->tag.free) {
  16. /* OK we have a free block above and below us */
  17. if (words <= (prev_block->tag.words + next_block->tag.words)) {
  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 =
  24. lub_heap_extend_upwards(this, &block,
  25. next_block->tag.words);
  26. assert(BOOL_TRUE == result);
  27. }
  28. }
  29. return result;
  30. }
  31. /*--------------------------------------------------------- */