heap.h 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997
  1. /*
  2. * heap.h
  3. */
  4. /**
  5. \ingroup lub
  6. \defgroup lub_heap heap
  7. @{
  8. \brief This is a generic heap manager which incorporates a memory leak detection system
  9. which can monitor and report the dynamic memory which is in use for each heap.
  10. A client creates an instance of heap; providing it with memory segments
  11. to manage. Subsequent operations can then be invoked to obtain memory
  12. from those registered segments.
  13. \section static_allocs Static allocation
  14. Static memory allocations are those which are for the lifetime of the
  15. heap from which they are allocated. Because they do not need to be freed
  16. there is zero overhead required for such blocks; they can be exactly butted
  17. up against each other in memory. There is also zero fragmentation as they
  18. are never freed.
  19. \section dynamic_allocs Dynamic allocation
  20. Dynamic memory allocations have a lifetime less than that of the heap
  21. from which they are allocated. In order to manage the reuse of this blocks
  22. once they are released, there will be a slight "housekeeping" overhead
  23. associated with each block. They are also suceptible to fragmentation, which
  24. can occur when the lifetimes of blocks differ from one another.
  25. \section leak_detection Leak Detection
  26. It monitors the dynamic allocation which are performed and maintains
  27. statistics to help identify and isolation memory leaks.
  28. It detects two types of leak by scanning the BSS and DATA segments and identifying
  29. any nodes referenced from there. Then each of these referenced nodes is scanned to
  30. look for further references.
  31. \subsection full_leak Full Leak
  32. there is no reference to a block of memory or to any memory within that block in the system.
  33. \subsection partial_leak Partial Leak
  34. there is no reference to the start of a block of memory. NB. there
  35. may be circumstances where this is not a real leak, e.g. memory allocation
  36. systems typically hand out references just beyond a control header to their
  37. clients. However there may also be instances where this is a real leak
  38. and someone just happens to have a reference to some contained data.
  39. \section tainting Memory Tainting
  40. Memory is deliberately dirtied in the following ways:
  41. - Initial heap space - 0xBBBBBBBB
  42. - Allocated memory - 0xAAAAAAAA
  43. - Freed memory - 0xFFFFFFFF
  44. - Dead Stack memory - 0xCCCCCCCC (done before leak scan)
  45. \section usage Utility functions
  46. Currently the following utility functions are available:
  47. - leakScan [0|1|2] - provides a dump of the currently allocated blocks of
  48. memory in the system pool.
  49. argument values have the following meanings:
  50. - 0 - display stats for just memory leaks.
  51. - 1 - display stats for memory leaks and partial leaks.
  52. - 2 - display stats for all currently allocation memory
  53. blocks.
  54. - leakEnable [framecount] - enables leak detection and causes the current statistics to be
  55. cleared out.
  56. Even if there are memory blocks in use, this
  57. command will cause future "leakShow" invocations
  58. to behave as if monitoring started from this new
  59. point in time.
  60. The optional 'framecount' argument can be used
  61. to change the number of levels of backtrace
  62. recorded. By default this number is 16. The
  63. number of contexts stored will be affected by
  64. this number. If the value is small then a
  65. limited number of contexts will exist and the
  66. memory overhead of monitoring will be reduced.
  67. If the number is large (can support a maximum
  68. of 16 levels) then the granularity of the
  69. contexts will be finer i.e. more of them, but
  70. the memory overhead in monitoring will be
  71. increased.
  72. With no specified framecount the maximum will be assumed.
  73. - leakDisable - Disabled the leak detection.
  74. \section implementation Implementation
  75. Static and dynamic blocks are allocated from opposite ends of a memory
  76. segment. As static blocks are allocated the end of the last
  77. "free block" migrates downwards in memory. Dynamic blocks are allocated
  78. from the start of the appropriately sized free block, hence leaving
  79. space for static allocations at the end of the heap memory.
  80. The heap implements a "best fit" model, for allocation of dynamic memory.
  81. This means that free blocks are maintained in size order and hence
  82. the most appropriate one can be used to satisfy client requests.
  83. This minimises fragmentation of the dynamically allocated memory.
  84. The free blocks are held in a binary tree (using \ref lub_bintree) which provide
  85. fast searching for the appropriate block.
  86. \author Graeme McKerrell
  87. \date Created On : Wed Dec 14 10:20:00 2005
  88. \version UNTESTED
  89. */
  90. #ifndef _lub_heap_h
  91. #define _lub_heap_h
  92. #include <stddef.h>
  93. #include "lub/types.h"
  94. #include "lub/c_decl.h"
  95. _BEGIN_C_DECL
  96. /**
  97. * This type is used to reference an instance of a heap.
  98. */
  99. typedef struct lub_heap_s lub_heap_t;
  100. /**
  101. * This type is used to reference an instance of a free block
  102. */
  103. typedef struct lub_heap_free_block_s lub_heap_free_block_t;
  104. /**
  105. * This type defines the statistics available for each heap.
  106. */
  107. typedef struct lub_heap_stats_s lub_heap_stats_t;
  108. struct lub_heap_stats_s {
  109. /*----------------------------------------------------- */
  110. /**
  111. * Number of segments comprising this heap.
  112. */
  113. size_t segs;
  114. /**
  115. * Number of bytes available in all the segments.
  116. */
  117. size_t segs_bytes;
  118. /**
  119. * Number of bytes used in housekeeping overheads
  120. */
  121. size_t segs_overhead;
  122. /*----------------------------------------------------- */
  123. /**
  124. * Number of free blocks. This is indication
  125. * the fragmentation state of a heap.
  126. */
  127. size_t free_blocks;
  128. /**
  129. * Number of bytes available in a heap.
  130. */
  131. size_t free_bytes;
  132. /**
  133. * Number of bytes used in housekeeping overheads
  134. */
  135. size_t free_overhead;
  136. /*----------------------------------------------------- */
  137. /**
  138. * Number of dynamically allocated blocks currently
  139. * held by clients of a heap.
  140. */
  141. size_t alloc_blocks;
  142. /**
  143. * Number of dynamically allocated bytes currently
  144. * held by clients of a heap.
  145. */
  146. size_t alloc_bytes;
  147. /**
  148. * Number of bytes used in housekeeping overheads
  149. */
  150. size_t alloc_overhead;
  151. /*----------------------------------------------------- */
  152. /**
  153. * Cumulative number of dynamically allocated blocks
  154. * given to clients of a heap.
  155. */
  156. size_t alloc_total_blocks;
  157. /**
  158. * Cumulative number of dynamically allocated bytes
  159. * given to clients of a heap.
  160. */
  161. size_t alloc_total_bytes;
  162. /*----------------------------------------------------- */
  163. /**
  164. * Number of dynamically allocated blocks
  165. * given to clients when the memory usage was at it's highest.
  166. */
  167. size_t alloc_hightide_blocks;
  168. /**
  169. * Number of dynamically allocated bytes
  170. * given to clients of a heap when the memory usage was at it's
  171. * highest
  172. */
  173. size_t alloc_hightide_bytes;
  174. /**
  175. * Number of bytes of overhead in use when the memory usage
  176. * was at it's highest
  177. */
  178. size_t alloc_hightide_overhead;
  179. /**
  180. * Number of free blocks when the memory usage was at it's
  181. * highest
  182. */
  183. size_t free_hightide_blocks;
  184. /**
  185. * Number of free bytes when the memory usage was at it's highest.
  186. */
  187. size_t free_hightide_bytes;
  188. /**
  189. * Number of housekeeping overhead bytes when the memory
  190. * usage was at it's highest.
  191. */
  192. size_t free_hightide_overhead;
  193. /*----------------------------------------------------- */
  194. /**
  195. * Number of statically allocated blocks currently
  196. * held by clients of a heap.
  197. */
  198. size_t static_blocks;
  199. /**
  200. * Number of statically allocated bytes currently
  201. * held by clients of a heap.
  202. */
  203. size_t static_bytes;
  204. /**
  205. * Number of dynamically allocated bytes currently
  206. * held by clients of a heap.
  207. */
  208. size_t static_overhead;
  209. /*----------------------------------------------------- */
  210. };
  211. /**
  212. * This type is used to indicate the result of a dynamic
  213. * memory allocation
  214. */
  215. typedef enum {
  216. /**
  217. * The allocation was successful
  218. */
  219. LUB_HEAP_OK,
  220. /**
  221. * There was insufficient resource to satisfy the request
  222. */
  223. LUB_HEAP_FAILED,
  224. /**
  225. * An attempt has been made to release an already freed block
  226. * of memory.
  227. */
  228. LUB_HEAP_DOUBLE_FREE,
  229. /**
  230. * A memory corruption has been detected. e.g. someone writing
  231. * beyond the bounds of an allocated block of memory.
  232. */
  233. LUB_HEAP_CORRUPTED,
  234. /**
  235. * The client has passed in an invalid pointer
  236. * i.e. one which lies outside the bounds of the current heap.
  237. */
  238. LUB_HEAP_INVALID_POINTER
  239. } lub_heap_status_t;
  240. typedef struct {
  241. void *ptr;
  242. } struct_void_ptr;
  243. /**
  244. * This type is used to indicate the alignment required
  245. * for a memory allocation.
  246. */
  247. typedef enum {
  248. /**
  249. * This is the "native" alignment required for the current
  250. * CPU architecture.
  251. */
  252. LUB_HEAP_ALIGN_NATIVE = sizeof(struct_void_ptr),
  253. /**
  254. * 4 byte alignment
  255. */
  256. LUB_HEAP_ALIGN_2_POWER_2 = 0x00000004,
  257. /**
  258. * 8 byte alignment
  259. */
  260. LUB_HEAP_ALIGN_2_POWER_3 = 0x00000008,
  261. /**
  262. * 16 byte alignment
  263. */
  264. LUB_HEAP_ALIGN_2_POWER_4 = 0x00000010,
  265. /**
  266. * 32 byte alignment
  267. */
  268. LUB_HEAP_ALIGN_2_POWER_5 = 0x00000020,
  269. /**
  270. * 64 byte alignment
  271. */
  272. LUB_HEAP_ALIGN_2_POWER_6 = 0x00000040,
  273. /**
  274. * 128 byte alignment
  275. */
  276. LUB_HEAP_ALIGN_2_POWER_7 = 0x00000080,
  277. /**
  278. * 256 byte alignment
  279. */
  280. LUB_HEAP_ALIGN_2_POWER_8 = 0x00000100,
  281. /**
  282. * 512 byte alignment
  283. */
  284. LUB_HEAP_ALIGN_2_POWER_9 = 0x00000200,
  285. /**
  286. * 1024 byte alignment (1KB)
  287. */
  288. LUB_HEAP_ALIGN_2_POWER_10 = 0x00000400,
  289. /**
  290. * 2048 byte alignment (2KB)
  291. */
  292. LUB_HEAP_ALIGN_2_POWER_11 = 0x00000800,
  293. /**
  294. * 4096 byte alignment (4KB)
  295. */
  296. LUB_HEAP_ALIGN_2_POWER_12 = 0x00001000,
  297. /**
  298. * 8192 byte alignment (8KB)
  299. */
  300. LUB_HEAP_ALIGN_2_POWER_13 = 0x00002000,
  301. /**
  302. * 16384 byte alignment (16KB)
  303. */
  304. LUB_HEAP_ALIGN_2_POWER_14 = 0x00004000,
  305. /**
  306. * 32768 byte alignment (32KB)
  307. */
  308. LUB_HEAP_ALIGN_2_POWER_15 = 0x00008000,
  309. /**
  310. * 65536 byte alignment (64KB)
  311. */
  312. LUB_HEAP_ALIGN_2_POWER_16 = 0x00010000,
  313. /**
  314. * 131072 byte alignment (128KB)
  315. */
  316. LUB_HEAP_ALIGN_2_POWER_17 = 0x00020000,
  317. /**
  318. * 262144 byte alignment (256KB)
  319. */
  320. LUB_HEAP_ALIGN_2_POWER_18 = 0x00040000,
  321. /**
  322. * 524288 byte alignment (512KB)
  323. */
  324. LUB_HEAP_ALIGN_2_POWER_19 = 0x00080000,
  325. /**
  326. * 1048576 byte alignment (1MB)
  327. */
  328. LUB_HEAP_ALIGN_2_POWER_20 = 0x00100000,
  329. /**
  330. * 2097152 byte alignment (2MB)
  331. */
  332. LUB_HEAP_ALIGN_2_POWER_21 = 0x00200000,
  333. /**
  334. * 4194304 byte alignment (4MB)
  335. */
  336. LUB_HEAP_ALIGN_2_POWER_22 = 0x00400000,
  337. /**
  338. * 8388608 byte alignment (8MB)
  339. */
  340. LUB_HEAP_ALIGN_2_POWER_23 = 0x00800000,
  341. /**
  342. * 16777216 byte alignment (16MB)
  343. */
  344. LUB_HEAP_ALIGN_2_POWER_24 = 0x01000000,
  345. /**
  346. * 33554432 byte alignment (32MB)
  347. */
  348. LUB_HEAP_ALIGN_2_POWER_25 = 0x02000000,
  349. /**
  350. * 67108864 byte alignment (64MB)
  351. */
  352. LUB_HEAP_ALIGN_2_POWER_26 = 0x04000000,
  353. /**
  354. * 134217728 byte alignment (128MB)
  355. */
  356. LUB_HEAP_ALIGN_2_POWER_27 = 0x08000000
  357. }
  358. lub_heap_align_t;
  359. /**
  360. * This type defines how leak details should be displayed
  361. */
  362. typedef enum {
  363. /**
  364. * Only show allocations which have no reference elsewhere
  365. * in the system
  366. */
  367. LUB_HEAP_SHOW_LEAKS,
  368. /**
  369. * Only show allocations which have no direct reference elsewhere
  370. * in the system, but do have their contents referenced.
  371. */
  372. LUB_HEAP_SHOW_PARTIALS,
  373. /**
  374. * Show all the current allocations in the system.
  375. */
  376. LUB_HEAP_SHOW_ALL
  377. } lub_heap_show_e;
  378. /**
  379. * This type defines a function prototype to be used to
  380. * iterate around each of a number of things in the system.
  381. */
  382. typedef void lub_heap_foreach_fn(
  383. /**
  384. * The current entity
  385. */
  386. void *block,
  387. /**
  388. * A simple 1-based identifier for this entity
  389. */
  390. unsigned index,
  391. /**
  392. * The number of bytes available in this entity
  393. */
  394. size_t size,
  395. /**
  396. * Client specific argument
  397. */
  398. void *arg);
  399. /**
  400. * This operation is a diagnostic which can be used to
  401. * iterate around all the segments in the specified heap.
  402. * For example it may be desirable to output information about
  403. * each of the segments present.
  404. *
  405. * \pre
  406. * - The heap needs to have been create with an initial memory segment.
  407. *
  408. * \return
  409. * - none
  410. *
  411. * \post
  412. * -The specified function will have been called once for every segment
  413. * in the specified heap
  414. */
  415. void lub_heap_foreach_segment(
  416. /**
  417. * The heap instance on which to operate
  418. */
  419. lub_heap_t * instance,
  420. /**
  421. * The client provided function to call for each free block
  422. */
  423. lub_heap_foreach_fn * fn,
  424. /**
  425. * Some client specific data to pass through to the callback
  426. * function.
  427. */
  428. void *arg);
  429. /**
  430. * This operation is a diagnostic which can be used to
  431. * iterate around all the free blocks in the specified heap.
  432. * For example it may be desirable to output information about
  433. * each of the free blocks present.
  434. *
  435. * \pre
  436. * - The heap needs to have been create with an initial memory segment.
  437. *
  438. * \return
  439. * - none
  440. *
  441. * \post
  442. * -The specified function will have been called once for every free
  443. * block in the specified heap
  444. */
  445. void lub_heap_foreach_free_block(
  446. /**
  447. * The heap instance on which to operate
  448. */
  449. lub_heap_t * instance,
  450. /**
  451. * The client provided function to call for each free block
  452. */
  453. lub_heap_foreach_fn * fn,
  454. /**
  455. * Some client specific data to pass through to the callback
  456. * function.
  457. */
  458. void *arg);
  459. /**
  460. * This operation creates a dynamic heap from the provided
  461. * memory segment.
  462. *
  463. * \pre
  464. * - none
  465. *
  466. * \return
  467. * - a reference to a heap object which can be used to allocate
  468. * memory from the segments associated with this heap.
  469. *
  470. * \post
  471. * - memory allocations can be invoked on the returned intance.
  472. * - further memory segements can be augmented to the heap using
  473. * the lub_heap_add_segment() operation.
  474. */
  475. lub_heap_t *lub_heap_create(
  476. /**
  477. * The begining of the first memory segment to associate with
  478. * this heap
  479. */
  480. void *start,
  481. /**
  482. * The number of bytes available for use in the first segment.
  483. */
  484. size_t size);
  485. /**
  486. * This operation creates a dynamic heap from the provided
  487. * memory segment.
  488. *
  489. * \pre
  490. * - The heap needs to have been create with an initial memory segment.
  491. *
  492. * \return
  493. * - none
  494. *
  495. * \post
  496. * - The heap is no longer valid for use.
  497. * - The memory segment(s) previously given to the heap
  498. * may now be reused.
  499. * - Any extra resources used for leak detection will have been released.
  500. */
  501. void lub_heap_destroy(
  502. /**
  503. * The heap instance on which to operate
  504. */
  505. lub_heap_t * instance);
  506. /**
  507. * This operation augments an existing heap with
  508. * some more memory to manage.
  509. * NB. if the memory happens to be follow on from the initial memory segment
  510. * then the two will merge into a single larger segment. This means that a heap
  511. * which is expanded with a sbrk() like mechanism will contain a single
  512. * expandible segment.
  513. *
  514. * \pre
  515. * - The heap needs to have been create with an initial memory segment.
  516. *
  517. * \return
  518. * - none
  519. *
  520. * \post
  521. * - The new segment of memory becomes available for use by this heap.
  522. */
  523. void lub_heap_add_segment(
  524. /**
  525. * The heap instance on which to operate
  526. */
  527. lub_heap_t * instance,
  528. /**
  529. * The beginning of the memory segment to be managed
  530. */
  531. void *start,
  532. /**
  533. * The number of bytes available for use in this segment
  534. */
  535. size_t size);
  536. /**
  537. * This operation allocates some "static" memory from a heap. This is
  538. * memory which will remain allocted for the lifetime of the heap instance.
  539. * "static" memory allocation has zero overhead and causes zero fragmentation.
  540. *
  541. * NB. static allocation are only handed out from the first memory segment
  542. *
  543. * \pre
  544. * - The heap needs to have been created with an initial memory segment.
  545. *
  546. * \return
  547. * - a pointer to some "static" memory which will be fixed for the
  548. * remaining lifetime of this heap.
  549. *
  550. * \post
  551. * - The client cannot ever free this memory although if the heap is
  552. * managing memory which itself has been dynamically allocated, then
  553. * the memory will be recovered when the heap is released.
  554. */
  555. void *lub_heap_static_alloc(
  556. /**
  557. * The heap instance on which to operate
  558. */
  559. lub_heap_t * instance,
  560. /**
  561. * The number of bytes to allocate
  562. */
  563. size_t size);
  564. /**
  565. * This operation changes the size of the object referenced by a passed in
  566. * pointer to "size". The contents will be unchanged up to the minimum of the old
  567. * and new sizes. If the new size is larger, the new space is uninitialised.
  568. *
  569. * \pre
  570. * - The heap needs to have been created with an initial memory segment.
  571. * - If "*ptr" contains a non-NULL value then this MUST have
  572. * been allocated using this operation, from the same heap instance.
  573. *
  574. * \return
  575. * - the status of the operation.
  576. *
  577. * \post
  578. * - The client takes responsiblity for releasing any allocated memory when they
  579. * are finished with it.
  580. * - If *ptr contains a non-NULL value, then after a succesfull call, the
  581. * initial memory referenced by it may have been released for reuse,
  582. * and the pointer modified to reference some new memory.
  583. * - *ptr may contain NULL in which case no memory will be released back to the
  584. * heap for reuse, and the pointer is filled out with the allocated memory.
  585. * - (size == 0) No new memory will be allocated, *ptr will be set to NULL,
  586. * and any original memory referenced by it will have been released.
  587. */
  588. lub_heap_status_t lub_heap_realloc(
  589. /**
  590. * The heap instance on which to operate
  591. */
  592. lub_heap_t * instance,
  593. /**
  594. * Reference to a pointer containing previously allocated memory
  595. * or NULL.
  596. */
  597. char **ptr,
  598. /**
  599. * The number of bytes required for the object
  600. */
  601. size_t size,
  602. /**
  603. * The alignement required for a new allocations.
  604. */
  605. lub_heap_align_t alignment);
  606. /**
  607. * This operation controls the tainted memory facility.
  608. * This means that during certain heap operations memory can
  609. * be filled with some well defined bit patterns. This causes
  610. * a slight performance overhead but can be used to shake out
  611. * bugs such and free-memory-reads and uninitialised-memory-reads
  612. *
  613. * By default tainting is switched off.
  614. *
  615. * \pre
  616. * - none
  617. *
  618. * \return
  619. * - last tainted status
  620. *
  621. * \post
  622. * - (enabled) when a memory segment is given to a heap (either at creation or later)
  623. * the contents will be set to 0xBB
  624. * - (enabled) when some dynamically allocated memory is released back to a heap
  625. * the contents will be set to 0xFF
  626. * - (enabled) when some dynamic or static memory is allocated the contents will
  627. * be set to 0xAA as the "uninitialised" value.
  628. * - (disabled) no memory tainting will occur.
  629. */
  630. bool_t lub_heap_taint(
  631. /**
  632. * BOOL_TRUE to enable tainting or BOOL_FALSE to disable
  633. */
  634. bool_t enable);
  635. /**
  636. * This operation indicates the current status of the memory tainting
  637. * facility
  638. *
  639. * \pre
  640. * none
  641. *
  642. * \return
  643. * - BOOL_TRUE if memory tainting is enabled.
  644. * - BOOL_FALSE if memory tainting is disabled.
  645. *
  646. * \post
  647. * none
  648. */
  649. bool_t lub_heap_is_tainting(void);
  650. /**
  651. * This operation controls runtime heap corruption detection.
  652. * This means that during every heap operation a full check is
  653. * done of the specified heap, before any allocations/free are
  654. * performed.
  655. * This has a performance overhead but provides a valuable
  656. * aid in finding a memory corrupting client.
  657. * Corruption will be spotted the first time a memory operation
  658. * is performed AFTER it has occured.
  659. *
  660. * By default checking is switched off.
  661. *
  662. * \pre
  663. * - none
  664. *
  665. * \return
  666. * - last checking status
  667. *
  668. * \post
  669. * - (enabled) the heap will have been scanned for any corruption and if found the
  670. * return status of the invoking heap operation will be LUB_HEAP_CORRUPTED.
  671. * - (disabled) no entire heap memory checking will occur.
  672. */
  673. bool_t lub_heap_check(
  674. /**
  675. * BOOL_TRUE to enable checking or BOOL_FALSE to disable
  676. */
  677. bool_t enable);
  678. /**
  679. * This operation indicates the current status of the full memory checking
  680. * facility.
  681. *
  682. * \pre
  683. * none
  684. *
  685. * \return
  686. * - BOOL_TRUE if full memory checking is enabled.
  687. * - BOOL_FALSE if full memory checking is disabled.
  688. *
  689. * \post
  690. * none
  691. */
  692. bool_t lub_heap_is_checking(void);
  693. /**
  694. * This operation checks the integrety of the memory in the specified
  695. * heap.
  696. * Corruption will be spotted the first time a check is performed AFTER
  697. * it has occured.
  698. * \pre
  699. * - the specified heap will have been created
  700. *
  701. * \return
  702. * - BOOL_TRUE if the heap is OK
  703. * - BOOL_FALSE if the heap is corrupted.
  704. *
  705. * \post
  706. * - none
  707. */
  708. extern bool_t lub_heap_check_memory(lub_heap_t * instance);
  709. /**
  710. * This function is invoked whenever a call to lub_heap_realloc() fails.
  711. * It is provided as a debugging aid; simple set a breakpoint to
  712. * stop execution of the program and any failures will be caught in context.
  713. */
  714. void lub_heap_stop_here(
  715. /**
  716. * The failure status of the the call to realloc
  717. */
  718. lub_heap_status_t status,
  719. /**
  720. * The old value of the pointer passed in
  721. */
  722. char *old_ptr,
  723. /**
  724. * The requested number of bytes
  725. */
  726. size_t new_size);
  727. /**
  728. * This operation fills out a statistics structure with the details for the
  729. * specified heap.
  730. *
  731. * \pre
  732. * - none
  733. *
  734. * \post
  735. * - the results filled out are a snapshot of the statistics as the time
  736. * of the call.
  737. */
  738. void lub_heap__get_stats(
  739. /**
  740. * The instance on which to operate
  741. */
  742. lub_heap_t * instance,
  743. /**
  744. * A client provided structure to fill out with the heap details
  745. */
  746. lub_heap_stats_t * stats);
  747. /**
  748. * This operation dumps the salient details of the specified heap to stdout
  749. */
  750. void lub_heap_show(
  751. /**
  752. * The instance on which to operate
  753. */
  754. lub_heap_t * instance,
  755. /**
  756. * Whether to be verbose or not
  757. */
  758. bool_t verbose);
  759. /**
  760. * This method provides the size, in bytes, of the largest allocation
  761. * which can be performed.
  762. * \pre
  763. * - The heap needs to have been created with an initial memory segment.
  764. *
  765. * \return
  766. * - size of largest possible allocation.
  767. *
  768. * \post
  769. * - none
  770. */
  771. size_t lub_heap__get_max_free(
  772. /**
  773. * The instance on which to operate
  774. */
  775. lub_heap_t * instance);
  776. extern size_t
  777. lub_heap__get_block_overhead(lub_heap_t * instance, const void *ptr);
  778. extern size_t lub_heap__get_block_size(lub_heap_t * instance, const void *ptr);
  779. /**
  780. * This function scans memory to identify memory leaks
  781. *
  782. * NB. if tainting is switched off then this function may miss some leaks as
  783. * references may remain in freed memory.
  784. *
  785. */
  786. extern void lub_heap_leak_scan(void);
  787. /**
  788. * This function dumps all the context details for the heap
  789. * to stdout.
  790. * 'how' is one of the following values:
  791. * 0 - show only leaks
  792. * 1 - show partial leaks (no references to the start of the block)
  793. * 2 - show all allocations (VERY VERBOSE)
  794. *
  795. * NB. if tainting is switched off then this function will not perform
  796. * any memory scanning and will simply show all the allocated blocks.
  797. *
  798. * \return
  799. * - a boolean indicating whether any leaks were displayed or not.
  800. */
  801. extern bool_t lub_heap_leak_report(
  802. /**
  803. * how to display the details
  804. */
  805. lub_heap_show_e how,
  806. /**
  807. * an optional substring to use to filter contexts.
  808. * Only contexts which contain the substring will be displayed
  809. */
  810. const char *substring);
  811. void lub_heap__set_framecount(
  812. /**
  813. * The new framecount to use
  814. */
  815. unsigned framecount);
  816. unsigned lub_heap__get_framecount(void);
  817. extern bool_t lub_heap_validate_pointer(lub_heap_t * instance, char *ptr);
  818. /**
  819. * This 'magic' pointer is returned when a client requests zero bytes
  820. * The client can see that the allocation has succeeded, but cannot use
  821. * the "memory" returned. This pointer may be passed transparently back
  822. * to lub_heap_realloc() without impact.
  823. */
  824. #define LUB_HEAP_ZERO_ALLOC ((void*)-1)
  825. /**
  826. * This operation is used to initialise the heap management
  827. * subsystem
  828. * \pre
  829. * - none
  830. *
  831. * \post
  832. * - The POSIX specific subsystem will be initialised to load
  833. * the debugging symbols for the current executable. This
  834. * enables the backtraces used for leak detection to show
  835. * the full detail in the stack traces.
  836. * - If the system is configured at build time without GPL
  837. * support (disabled by default) then only the address of
  838. * each stack frame will be shown.
  839. */
  840. extern void lub_heap_init(
  841. /**
  842. * The full pathname of the current executable
  843. * This is typically obtained from argv[0] in main()
  844. */
  845. const char *program_name);
  846. #if defined(__CYGWIN__)
  847. /**
  848. * CYGWIN requires a specialised initialisation to account for
  849. * argv[0] not containing the trailing ".exe" of the executable.
  850. */
  851. extern void cygwin_lub_heap_init(const char *file_name);
  852. #define lub_heap_init(arg0) cygwin_lub_heap_init(arg0)
  853. #endif /* __CYGWIN__ */
  854. /**
  855. * This operation adds a cache to the current heap, which speeds up
  856. * the allocation and releasing of smaller block sizes.
  857. *
  858. * \pre
  859. * - The heap must have been initialised
  860. * - This call must not have been made on this heap before
  861. *
  862. * \return
  863. * - LUB_HEAP_OK if the cache was successfully set up.
  864. * - LUB_HEAP_FAILED if the cache was not set up for whatever reason
  865. *
  866. * \post
  867. * - memory allocations for smaller block sizes may come from the
  868. * cache.
  869. */
  870. lub_heap_status_t lub_heap_cache_init(
  871. /**
  872. * The instance on which to operate
  873. */
  874. lub_heap_t * instance,
  875. /**
  876. * The maximum block size for the cache
  877. */
  878. lub_heap_align_t max_block_size,
  879. /**
  880. * The number of maximum sized blocks to make available.
  881. */
  882. size_t num_max_blocks);
  883. /**
  884. * This operation signals the start of a section of code which
  885. * should not have any of it's heap usage monitored by the leak
  886. * detection code.
  887. *
  888. * \pre
  889. * - The heap must have been initialised
  890. *
  891. * \return
  892. * - none
  893. *
  894. * \post
  895. * - If leak detection is enabled then no subsequent allocations will
  896. * be monitored until the lub_heap_leak_restore_detection() is called.
  897. */
  898. void lub_heap_leak_suppress_detection(
  899. /**
  900. * The instance on which to operate
  901. */
  902. lub_heap_t * instance);
  903. /**
  904. * This operation signals the end of a section of code which
  905. * should not have any of it's heap usage monitored by the leak
  906. * detection code.
  907. *
  908. * NB. you may nest the usage of lub_heap_leak_suppress_detection() and
  909. * lub_heap_leak_restore_detection() in which case only when the outermost
  910. * section has been terminated will monitoring commence again.
  911. *
  912. * \pre
  913. * - The heap must have been initialised
  914. * - lub_heap_start_unmonitored_section() must have been called.
  915. *
  916. * \return
  917. * - none
  918. *
  919. * \post
  920. * - If leak detection is enabled then no subsequent allocations will
  921. * be monitored until the lub_heap_end_unmonitored_section() is called.
  922. */
  923. void lub_heap_leak_restore_detection(
  924. /**
  925. * The instance on which to operate
  926. */
  927. lub_heap_t * instance);
  928. /**
  929. * This operation returns the overhead, in bytes, which is required
  930. * to implement a heap instance. This provide clients the means of
  931. * calculating how much memory they need to assign for a heap instance
  932. * to manage.
  933. *
  934. * \pre
  935. * - none
  936. *
  937. * \return
  938. * - size in bytes of the overhead required by a lub_heap instance.
  939. *
  940. * \post
  941. * - none
  942. */
  943. size_t lub_heap_overhead_size(
  944. /**
  945. * The maximum block size for the cache
  946. */
  947. lub_heap_align_t max_block_size,
  948. /**
  949. * The number of maximum sized blocks to make available.
  950. */
  951. size_t num_max_blocks);
  952. _END_C_DECL
  953. #endif /* _lub_heap_h */
  954. /** @} */