heap_new_alloc_block.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "private.h"
  2. /*--------------------------------------------------------- */
  3. void *
  4. lub_heap_new_alloc_block(lub_heap_t *this,
  5. words_t words)
  6. {
  7. void *result = NULL;
  8. lub_heap_free_block_t *free_block;
  9. lub_heap_block_t *new_block;
  10. lub_heap_key_t key;
  11. /* initialise the seach key */
  12. key.words = words;
  13. key.block = 0;
  14. /* find the smallest free block which can take this request */
  15. free_block = lub_bintree_findnext(&this->free_tree,&key);
  16. if(NULL != free_block)
  17. {
  18. lub_heap_tag_t *tail;
  19. unsigned int seg_end, seg_start;
  20. /* remember if this is the end of a segment */
  21. tail = lub_heap_block__get_tail((lub_heap_block_t *)free_block);
  22. seg_start = free_block->tag.segment;
  23. seg_end = tail->segment;
  24. /* remove the block from the free tree */
  25. lub_bintree_remove(&this->free_tree,free_block);
  26. /* now slice the bottom off for the client */
  27. new_block = lub_heap_slice_from_bottom(this,&free_block,&words,BOOL_FALSE);
  28. if(NULL != free_block)
  29. {
  30. /* put the block back into the tree */
  31. lub_bintree_insert(&this->free_tree,free_block);
  32. }
  33. if(NULL != new_block)
  34. {
  35. /* set up the tag details */
  36. new_block->alloc.tag.segment = seg_start;
  37. new_block->alloc.tag.free = 0;
  38. new_block->alloc.tag.words = words;
  39. tail = lub_heap_block__get_tail(new_block);
  40. if(NULL == free_block)
  41. {
  42. /* we've swallowed the whole free block */
  43. tail->segment = seg_end;
  44. }
  45. else
  46. {
  47. tail->segment = 0;
  48. }
  49. tail->free = 0;
  50. tail->words = words;
  51. /* update the stats */
  52. ++this->stats.alloc_blocks;
  53. this->stats.alloc_bytes += (words << 2);
  54. this->stats.alloc_bytes -= sizeof(lub_heap_alloc_block_t);
  55. ++this->stats.alloc_total_blocks;
  56. this->stats.alloc_total_bytes += (words << 2);
  57. this->stats.alloc_total_bytes -= sizeof(lub_heap_alloc_block_t);
  58. this->stats.alloc_overhead += sizeof(lub_heap_alloc_block_t);
  59. /* fill out the client's pointer */
  60. result = new_block->alloc.memory;
  61. }
  62. }
  63. return result;
  64. }
  65. /*--------------------------------------------------------- */