1
0

heap_slice_from_top.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include <assert.h>
  2. #include "private.h"
  3. /*--------------------------------------------------------- */
  4. void *
  5. lub_heap_slice_from_top(lub_heap_t *this,
  6. lub_heap_free_block_t **ptr_block,
  7. words_t *words,
  8. bool_t seg_end)
  9. {
  10. void *result = NULL;
  11. lub_heap_free_block_t *block = *ptr_block;
  12. /* check this is a free block */
  13. assert(1 == block->tag.free);
  14. if(BOOL_TRUE == lub_heap_block_check((lub_heap_block_t*)block)
  15. && (*words <= block->tag.words))
  16. {
  17. words_t new_words = (block->tag.words - *words);
  18. lub_heap_tag_t *tail;
  19. /* Is there sufficient memory to perform the task? */
  20. if(new_words > (sizeof(lub_heap_free_block_t) >> 2))
  21. {
  22. /* update the free block */
  23. block->tag.words -= *words;
  24. /* get the new tail tag */
  25. tail = lub_heap_block__get_tail((lub_heap_block_t*)block);
  26. /* set up the tag */
  27. tail->segment = seg_end;
  28. tail->free = 1;
  29. tail->words = block->tag.words;
  30. /* update the stats */
  31. this->stats.free_bytes -= (*words << 2);
  32. result = &tail[1];
  33. }
  34. else
  35. {
  36. /*
  37. * there is only just enough space for the request
  38. * so we also throw in the memory used for the tags
  39. */
  40. --this->stats.free_blocks;
  41. this->stats.free_bytes -= (block->tag.words << 2);
  42. this->stats.free_bytes += sizeof(lub_heap_alloc_block_t);
  43. this->stats.free_overhead -= sizeof(lub_heap_alloc_block_t);
  44. /* update the client's word count */
  45. *words = block->tag.words;
  46. /* there is no free block left!!! */
  47. result = block;
  48. *ptr_block = NULL;
  49. }
  50. if(NULL != result)
  51. {
  52. /* taint the memory */
  53. lub_heap_taint_memory(result,LUB_HEAP_TAINT_ALLOC,(*words << 2));
  54. }
  55. }
  56. return result;
  57. }
  58. /*--------------------------------------------------------- */