1
0

context.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. /*
  2. * context.h
  3. */
  4. /**********************************************************
  5. * CONTEXT CLASS DEFINITIONS
  6. ********************************************************** */
  7. #define MAX_BACKTRACE (32)
  8. typedef struct _lub_heap_stackframe lub_heap_context_key_t;
  9. typedef void (function_t) (void);
  10. struct _lub_heap_stackframe {
  11. function_t *backtrace[MAX_BACKTRACE];
  12. };
  13. typedef struct _lub_heap_leak_stats lub_heap_leak_stats_t;
  14. struct _lub_heap_leak_stats {
  15. size_t contexts;
  16. size_t allocs;
  17. size_t alloc_bytes;
  18. size_t alloc_overhead;
  19. size_t leaks;
  20. size_t leaked_bytes;
  21. size_t leaked_overhead;
  22. size_t partials;
  23. size_t partial_bytes;
  24. size_t partial_overhead;
  25. size_t scanned;
  26. };
  27. struct _lub_heap_context {
  28. /**
  29. * The heap to which this context belongs
  30. */
  31. lub_heap_t *heap;
  32. /**
  33. * This is needed to maintain a tree of contexts
  34. */
  35. lub_bintree_node_t bt_node;
  36. /**
  37. * The first node in this context
  38. */
  39. lub_heap_node_t *first_node;
  40. /**
  41. * current number of allocations made from this context
  42. */
  43. size_t allocs;
  44. /**
  45. * current number of bytes allocated from this context
  46. */
  47. size_t alloc_bytes;
  48. /**
  49. * current overhead in bytes allocated from this context
  50. */
  51. size_t alloc_overhead;
  52. /**
  53. * count of leaked allocations made from this context
  54. */
  55. size_t leaks;
  56. /**
  57. * number of leaked bytes from this context
  58. */
  59. size_t leaked_bytes;
  60. /**
  61. * current overhead in bytes leaked from this context
  62. */
  63. size_t leaked_overhead;
  64. /**
  65. * count of partially leaked allocations made from this context
  66. */
  67. size_t partials;
  68. /**
  69. * number of partially leaked bytes from this context
  70. */
  71. size_t partial_bytes;
  72. /**
  73. * current overhead in bytes partially leaked from this context
  74. */
  75. size_t partial_overhead;
  76. /**
  77. * a record of the memory partition associated with this context
  78. */
  79. lub_heap_context_key_t key;
  80. };
  81. extern unsigned long lub_heap_frame_count;
  82. typedef struct _lub_heap_leak lub_heap_leak_t;
  83. struct _lub_heap_leak {
  84. lub_bintree_t m_context_tree;
  85. lub_bintree_t m_node_tree;
  86. lub_bintree_t m_clear_node_tree;
  87. lub_bintree_t m_segment_tree;
  88. lub_heap_leak_stats_t m_stats;
  89. lub_dblockpool_t m_context_pool;
  90. lub_heap_t *m_heap_list;
  91. };
  92. extern lub_heap_leak_t *lub_heap_leak_instance(void);
  93. extern void lub_heap_leak_release(lub_heap_leak_t * instance);
  94. extern bool_t lub_heap_leak_query_node_tree(void);
  95. extern bool_t lub_heap_leak_query_clear_node_tree(void);
  96. /*---------------------------------------------------------
  97. * PUBLIC META ATTRIBUTES
  98. *--------------------------------------------------------- */
  99. /**
  100. * Obtains the number of bytes required to allocate a context instance.
  101. *
  102. * By default this is set to four.
  103. */
  104. size_t lub_heap_context__get_instanceSize(void);
  105. extern lub_bintree_compare_fn lub_heap_context_compare;
  106. extern lub_bintree_getkey_fn lub_heap_context_getkey;
  107. /*---------------------------------------------------------
  108. * PUBLIC METHODS
  109. *--------------------------------------------------------- */
  110. /**
  111. * This function finds or creates a context based on the
  112. * provided leak detector and stack specification.
  113. *
  114. * A context is defined by a pre-configured number of stack frames
  115. * (see lub_heap_context__set_frameCount())
  116. * All memory allocations which occur with
  117. * the same stack frames will be collected together in the same context.
  118. *
  119. * \return Pointer to a context object or NULL if one cannot be found or allocated.
  120. */
  121. const lub_heap_context_t *lub_heap_context_find_or_create(
  122. /**
  123. * The heap in question
  124. */
  125. lub_heap_t *
  126. instance,
  127. /**
  128. * The memory allocation function must allocate a
  129. * local variable, on it's stack, which is passed in
  130. * to this call as a means of getting access to the current
  131. * thread's call stack.
  132. */
  133. const
  134. stackframe_t *
  135. stack);
  136. /**
  137. * This function finds a context based on the
  138. * provided leak detector and stack specification.
  139. *
  140. * A context is defined by a pre-configured number of stack frames
  141. * (see lub_heap_context__set_frameCount())
  142. * All memory allocations which occur with
  143. * the same stack frames will be collected together in the same context.
  144. *
  145. * \return Pointer to a context object or NULL if one cannot be found.
  146. */
  147. lub_heap_context_t *lub_heap_context_find(
  148. /**
  149. * The memory allocation function must allocate a
  150. * local variable, on it's stack, which is passed in
  151. * to this call as a means of getting access to the current
  152. * thread's call stack.
  153. */
  154. const stackframe_t * stack);
  155. /*---------------------------------------------------------
  156. * PUBLIC METHODS
  157. *--------------------------------------------------------- */
  158. /**
  159. * This function initialises an instance of a context.
  160. */
  161. void lub_heap_context_init(
  162. /**
  163. * Context instance on which to operate
  164. */
  165. lub_heap_context_t * instance,
  166. /**
  167. * heap with with to associate this context
  168. */
  169. lub_heap_t * heap,
  170. /**
  171. * The memory allocation function must allocate a
  172. * local variable, on it's stack, which is passed in
  173. * to this call as a means of getting access to the current
  174. * thread's call stack.
  175. */
  176. const stackframe_t * stack);
  177. /**
  178. * This function finalises an instance of a context.
  179. *
  180. * \return the memory partition from which this context was allocated.
  181. */
  182. void lub_heap_context_fini(
  183. /**
  184. * Context instance on which to operate
  185. */
  186. lub_heap_context_t * instance);
  187. /**
  188. * This function deletes an instance of a context
  189. *
  190. * \return BOOL_TRUE if deletion was sucessful
  191. */
  192. bool_t lub_heap_context_delete(
  193. /**
  194. * Context instance on which to operate
  195. */
  196. lub_heap_context_t * instance);
  197. /**
  198. * This function adds the specified node to the context tree.
  199. *
  200. * \return BOOL_TRUE if the node was added to the tree,
  201. * BOOL_FALSE if node was already in the tree.
  202. */
  203. bool_t lub_heap_context_add_node(
  204. /**
  205. * Context instance on which to operate
  206. */
  207. lub_heap_context_t * instance,
  208. /**
  209. * The node to add to this context
  210. */
  211. lub_heap_node_t * node,
  212. /**
  213. * The size in bytes of the allocation
  214. */
  215. size_t size);
  216. /**
  217. * This function removes the specified node from the context tree.
  218. *
  219. * \return BOOL_TRUE if the node was found and removed,
  220. * BOOL_FALSE if the node was not in the tree
  221. */
  222. void lub_heap_context_remove_node(
  223. /**
  224. * Context instance on which to operate
  225. */
  226. lub_heap_context_t * instance,
  227. /**
  228. * The node to add to this context
  229. */
  230. lub_heap_node_t * node);
  231. /**
  232. * This function dumps details of the specified context to stdout
  233. *
  234. * /return
  235. * - BOOL_TRUE if any details were dumped.
  236. * - BOOL_FALSE if no details were dumped.
  237. */
  238. bool_t lub_heap_context_show(
  239. /**
  240. * Context instance on which to operate
  241. */
  242. lub_heap_context_t * instance,
  243. /**
  244. * How to dump the information
  245. */
  246. lub_heap_show_e how);
  247. /**
  248. * This function "clears" the currently allocated blocks so that lub_heap_context_dump
  249. * will not show them
  250. */
  251. void lub_heap_context_clear(
  252. /**
  253. * Context instance on which to operate
  254. */
  255. lub_heap_context_t * instance);
  256. /**
  257. * This places a node into the context's tree
  258. */
  259. void
  260. lub_heap_context_insert_node(lub_heap_context_t * instance,
  261. lub_heap_node_t * node);
  262. /*---------------------------------------------------------
  263. * PUBLIC ATTRIBUTES
  264. *--------------------------------------------------------- */
  265. /**
  266. * Identify whether this is a valid address within the heap
  267. */
  268. bool_t leaks_is_valid_heap_address(lub_heap_t * instance, const void *ptr);
  269. void lub_heap_leak_mutex_lock(void);
  270. void lub_heap_leak_mutex_unlock(void);