heap_destroy.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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, unsigned index, size_t size, void *arg)
  10. {
  11. lub_heap_segment_t *segment = ptr;
  12. lub_heap_leak_t *leak = lub_heap_leak_instance();
  13. /* remove segment from the leak tree */
  14. lub_bintree_remove(&leak->m_segment_tree, segment);
  15. lub_heap_leak_release(leak);
  16. }
  17. /*--------------------------------------------------------- */
  18. static void _lub_heap_node_remove(lub_heap_node_t * node, void *arg)
  19. {
  20. lub_heap_t *this = arg;
  21. lub_heap_context_t *context = lub_heap_node__get_context(node);
  22. lub_heap_leak_t *leak = lub_heap_leak_instance();
  23. if (context && (context->heap == this)) {
  24. --leak->m_stats.allocs;
  25. leak->m_stats.alloc_bytes -= lub_heap_node__get_size(node);
  26. /* remove this node from the leak tree */
  27. lub_heap_node_fini(node);
  28. }
  29. lub_heap_leak_release(leak);
  30. }
  31. /*--------------------------------------------------------- */
  32. void lub_heap_destroy(lub_heap_t * this)
  33. {
  34. lub_heap_leak_t *leak = lub_heap_leak_instance();
  35. lub_heap_t **ptr;
  36. for (ptr = &leak->m_heap_list; *ptr; ptr = &(*ptr)->next) {
  37. if ((*ptr) == this) {
  38. /* remove from the linked list */
  39. *ptr = this->next;
  40. break;
  41. }
  42. }
  43. lub_heap_leak_release(leak);
  44. /* remove all segments from the leak tree */
  45. lub_heap_foreach_segment(this, _lub_heap_segment_remove, 0);
  46. /* remove all nodes from the leak tree */
  47. lub_heap_foreach_node(_lub_heap_node_remove, this);
  48. }
  49. /*--------------------------------------------------------- */