heap_new_alloc_block.c 2.0 KB

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