1
0

heap_create.c 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. * heap_create.c
  3. */
  4. #include "private.h"
  5. #include "context.h"
  6. /*--------------------------------------------------------- */
  7. static int
  8. lub_heap_block_compare(const void *clientnode,
  9. const void *clientkey)
  10. {
  11. int delta;
  12. const lub_heap_free_block_t *block = clientnode;
  13. const lub_heap_key_t *key = clientkey;
  14. delta = (block->tag.words - key->words);
  15. if(0 == delta)
  16. {
  17. /* if same size differentiate by address */
  18. delta = (block - key->block);
  19. }
  20. return delta;
  21. }
  22. /*--------------------------------------------------------- */
  23. lub_heap_t *
  24. lub_heap_create(void *start,
  25. size_t size)
  26. {
  27. lub_heap_t *this = NULL;
  28. /* we must have at least 1024 bytes for a heap */
  29. if(size > (sizeof(lub_heap_t) + 4))
  30. {
  31. this = start;
  32. /* set up the binary tree for the free blocks */
  33. lub_bintree_init(&this->free_tree,
  34. offsetof(lub_heap_free_block_t,bt_node),
  35. lub_heap_block_compare,
  36. lub_heap_block_getkey);
  37. this->cache = 0;
  38. this->suppress = 0;
  39. /* initialise the statistics */
  40. this->stats.segs = 0;
  41. this->stats.segs_bytes = 0;
  42. this->stats.segs_overhead = 0;
  43. this->stats.free_blocks = 0;
  44. this->stats.free_bytes = 0;
  45. this->stats.free_overhead = 0;
  46. this->stats.static_blocks = 0;
  47. this->stats.static_bytes = 0;
  48. this->stats.static_overhead = 0;
  49. this->stats.alloc_blocks = 0;
  50. this->stats.alloc_bytes = 0;
  51. this->stats.alloc_overhead = 0;
  52. this->stats.alloc_hightide_blocks = 0;
  53. this->stats.alloc_hightide_bytes = 0;
  54. this->stats.alloc_hightide_overhead = 0;
  55. this->stats.free_hightide_blocks = 0;
  56. this->stats.free_hightide_bytes = 0;
  57. this->stats.free_hightide_overhead = 0;
  58. this->stats.alloc_total_blocks = 0;
  59. this->stats.alloc_total_bytes = 0;
  60. /* initialise the first segment */
  61. this->first_segment.next = NULL;
  62. /* create the default segment */
  63. lub_heap_add_segment(this,
  64. &this->first_segment,
  65. size - sizeof(lub_heap_t) + sizeof(lub_heap_segment_t));
  66. this->stats.segs_overhead += sizeof(lub_heap_t);
  67. /* add this heap to the linked list of heaps in the system */
  68. {
  69. lub_heap_leak_t *leak = lub_heap_leak_instance();
  70. this->next = leak->m_heap_list;
  71. leak->m_heap_list = this;
  72. lub_heap_leak_release(leak);
  73. }
  74. }
  75. return this;
  76. }
  77. /*--------------------------------------------------------- */