tlsf.h 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * SPDX-FileCopyrightText: 2006-2016 Matthew Conte
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #ifndef INCLUDED_tlsf
  7. #define INCLUDED_tlsf
  8. #include <assert.h>
  9. #include <stddef.h>
  10. #include <stdbool.h>
  11. #include "rtthread.h"
  12. #if defined(__cplusplus)
  13. extern "C" {
  14. #endif
  15. /* tlsf_t: a TLSF structure. Can contain 1 to N pools. */
  16. /* pool_t: a block of memory that TLSF can manage. */
  17. typedef void* tlsf_t;
  18. typedef void* pool_t;
  19. /* Create/destroy a memory pool. */
  20. tlsf_t tlsf_create(void* mem, size_t max_bytes);
  21. tlsf_t tlsf_create_with_pool(void* mem, size_t pool_bytes, size_t max_bytes);
  22. void tlsf_destroy(tlsf_t tlsf);
  23. pool_t tlsf_get_pool(tlsf_t tlsf);
  24. /* Add/remove memory pools. */
  25. pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes);
  26. void tlsf_remove_pool(tlsf_t tlsf, pool_t pool);
  27. /* malloc/memalign/realloc/free replacements. */
  28. void* tlsf_malloc(tlsf_t tlsf, size_t size);
  29. void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size);
  30. void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t offset);
  31. void* tlsf_malloc_addr(tlsf_t tlsf, size_t size, void *address);
  32. void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size);
  33. void tlsf_free(tlsf_t tlsf, void* ptr);
  34. /* Returns internal block size, not original request size */
  35. size_t tlsf_block_size(void* ptr);
  36. /* Overheads/limits of internal structures. */
  37. size_t tlsf_size(tlsf_t tlsf);
  38. size_t tlsf_pool_overhead(void);
  39. size_t tlsf_alloc_overhead(void);
  40. void tlsf_memory_info(tlsf_t tlsf, size_t *total, size_t *used, size_t *max_used);
  41. /**
  42. * @brief Return the allocable size based on the size passed
  43. * as parameter
  44. *
  45. * @param tlsf Pointer to the tlsf structure
  46. * @param size The allocation size
  47. * @return size_t The updated allocation size
  48. */
  49. size_t tlsf_fit_size(tlsf_t tlsf, size_t size);
  50. /* Debugging. */
  51. typedef bool (*tlsf_walker)(void* ptr, size_t size, int used, void* user);
  52. void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user);
  53. /* Returns nonzero if any internal consistency check fails. */
  54. int tlsf_check(tlsf_t tlsf);
  55. int tlsf_check_pool(pool_t pool);
  56. /**
  57. * @brief Find the block containing the pointer passed as parameter
  58. *
  59. * @param pool The pool into which to look for the block
  60. * @param ptr The pointer we want to find the containing block of
  61. * @return void* The pointer to the containing block if found, NULL if not.
  62. */
  63. void* tlsf_find_containing_block(pool_t pool, void *ptr);
  64. /**
  65. * @brief Weak function called on every free block of memory allowing the user to implement
  66. * application specific checks on the memory.
  67. *
  68. * @param start The start pointer to the memory of a block
  69. * @param size The size of the memory in the block
  70. * @param is_free Set to true when the memory belongs to a free block.
  71. * False if it belongs to an allocated block.
  72. * @return true The checks found no inconsistency in the memory
  73. * @return false The checks in the function highlighted an inconsistency in the memory
  74. */
  75. __attribute__((weak)) bool tlsf_check_hook(void *start, size_t size, bool is_free);
  76. #if defined(__cplusplus)
  77. };
  78. #endif
  79. #endif