1
0

heap_create.c 2.2 KB

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