context.h 8.2 KB

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