1
0

heap_destroy.c 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. /*
  2. * heap_destroy.c
  3. */
  4. #include "private.h"
  5. #include "node.h"
  6. #include "context.h"
  7. /*--------------------------------------------------------- */
  8. static void
  9. _lub_heap_segment_remove(void *ptr,
  10. unsigned index,
  11. size_t size,
  12. void *arg)
  13. {
  14. lub_heap_segment_t *segment = ptr;
  15. lub_heap_leak_t *leak = lub_heap_leak_instance();
  16. /* remove segment from the leak tree */
  17. lub_bintree_remove(&leak->m_segment_tree,segment);
  18. lub_heap_leak_release(leak);
  19. }
  20. /*--------------------------------------------------------- */
  21. static void
  22. _lub_heap_node_remove(lub_heap_node_t *node,
  23. void *arg)
  24. {
  25. lub_heap_t *this = arg;
  26. lub_heap_context_t *context = lub_heap_node__get_context(node);
  27. lub_heap_leak_t *leak = lub_heap_leak_instance();
  28. if(context && (context->heap == this))
  29. {
  30. --leak->m_stats.allocs;
  31. leak->m_stats.alloc_bytes -= lub_heap_node__get_size(node);
  32. /* remove this node from the leak tree */
  33. lub_heap_node_fini(node);
  34. }
  35. lub_heap_leak_release(leak);
  36. }
  37. /*--------------------------------------------------------- */
  38. void
  39. lub_heap_destroy(lub_heap_t *this)
  40. {
  41. lub_heap_leak_t *leak = lub_heap_leak_instance();
  42. lub_heap_t **ptr;
  43. for(ptr = &leak->m_heap_list;
  44. *ptr;
  45. ptr = &(*ptr)->next)
  46. {
  47. if((*ptr) == this)
  48. {
  49. /* remove from the linked list */
  50. *ptr = this->next;
  51. break;
  52. }
  53. }
  54. lub_heap_leak_release(leak);
  55. /* remove all segments from the leak tree */
  56. lub_heap_foreach_segment(this,_lub_heap_segment_remove,0);
  57. /* remove all nodes from the leak tree */
  58. lub_heap_foreach_node(_lub_heap_node_remove,this);
  59. }
  60. /*--------------------------------------------------------- */