multi_heap.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2023 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdint.h>
  7. #include <stdlib.h>
  8. #include <stdbool.h>
  9. #include <assert.h>
  10. #include <string.h>
  11. #include <stddef.h>
  12. #include <stdio.h>
  13. #include <sys/cdefs.h>
  14. #include "multi_heap.h"
  15. #include "multi_heap_internal.h"
  16. #if !CONFIG_HEAP_TLSF_USE_ROM_IMPL
  17. #include "tlsf.h"
  18. #include "tlsf_block_functions.h"
  19. #endif
  20. /* Note: Keep platform-specific parts in this header, this source
  21. file should depend on libc only */
  22. #include "multi_heap_platform.h"
  23. /* Defines compile-time configuration macros */
  24. #include "multi_heap_config.h"
  25. #if (!defined MULTI_HEAP_POISONING)
  26. void *multi_heap_aligned_alloc_offs(multi_heap_handle_t heap, size_t size, size_t alignment, size_t offset)
  27. {
  28. return multi_heap_aligned_alloc_impl_offs(heap, size, alignment, offset);
  29. }
  30. #if (!defined CONFIG_HEAP_TLSF_USE_ROM_IMPL)
  31. /* if no heap poisoning, public API aliases directly to these implementations */
  32. void *multi_heap_malloc(multi_heap_handle_t heap, size_t size)
  33. __attribute__((alias("multi_heap_malloc_impl")));
  34. void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment)
  35. __attribute__((alias("multi_heap_aligned_alloc_impl")));
  36. void multi_heap_aligned_free(multi_heap_handle_t heap, void *p)
  37. __attribute__((alias("multi_heap_free_impl")));
  38. void multi_heap_free(multi_heap_handle_t heap, void *p)
  39. __attribute__((alias("multi_heap_free_impl")));
  40. void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size)
  41. __attribute__((alias("multi_heap_realloc_impl")));
  42. size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p)
  43. __attribute__((alias("multi_heap_get_allocated_size_impl")));
  44. multi_heap_handle_t multi_heap_register(void *start, size_t size)
  45. __attribute__((alias("multi_heap_register_impl")));
  46. void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info)
  47. __attribute__((alias("multi_heap_get_info_impl")));
  48. size_t multi_heap_free_size(multi_heap_handle_t heap)
  49. __attribute__((alias("multi_heap_free_size_impl")));
  50. size_t multi_heap_minimum_free_size(multi_heap_handle_t heap)
  51. __attribute__((alias("multi_heap_minimum_free_size_impl")));
  52. void *multi_heap_get_block_address(multi_heap_block_handle_t block)
  53. __attribute__((alias("multi_heap_get_block_address_impl")));
  54. #endif // !CONFIG_HEAP_TLSF_USE_ROM_IMPL
  55. #endif // !MULTI_HEAP_POISONING
  56. #define ALIGN(X) ((X) & ~(sizeof(void *)-1))
  57. #define ALIGN_UP(X) ALIGN((X)+sizeof(void *)-1)
  58. #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
  59. typedef struct multi_heap_info {
  60. void *lock;
  61. size_t free_bytes;
  62. size_t minimum_free_bytes;
  63. size_t pool_size;
  64. void* heap_data;
  65. } heap_t;
  66. #if CONFIG_HEAP_TLSF_USE_ROM_IMPL
  67. void _multi_heap_lock(void *lock)
  68. {
  69. MULTI_HEAP_LOCK(lock);
  70. }
  71. void _multi_heap_unlock(void *lock)
  72. {
  73. MULTI_HEAP_UNLOCK(lock);
  74. }
  75. multi_heap_os_funcs_t multi_heap_os_funcs = {
  76. .lock = _multi_heap_lock,
  77. .unlock = _multi_heap_unlock,
  78. };
  79. void multi_heap_in_rom_init(void)
  80. {
  81. multi_heap_os_funcs_init(&multi_heap_os_funcs);
  82. }
  83. #else // CONFIG_HEAP_TLSF_USE_ROM_IMPL
  84. /* Check a block is valid for this heap. Used to verify parameters. */
  85. __attribute__((noinline)) NOCLONE_ATTR static void assert_valid_block(const heap_t *heap, const block_header_t *block)
  86. {
  87. pool_t pool = tlsf_get_pool(heap->heap_data);
  88. void *ptr = block_to_ptr(block);
  89. MULTI_HEAP_ASSERT((ptr >= pool) &&
  90. (ptr < pool + heap->pool_size),
  91. (uintptr_t)ptr);
  92. }
  93. void *multi_heap_get_block_address_impl(multi_heap_block_handle_t block)
  94. {
  95. return block_to_ptr(block);
  96. }
  97. size_t multi_heap_get_allocated_size_impl(multi_heap_handle_t heap, void *p)
  98. {
  99. return tlsf_block_size(p);
  100. }
  101. multi_heap_handle_t multi_heap_register_impl(void *start_ptr, size_t size)
  102. {
  103. assert(start_ptr);
  104. if(size < (sizeof(heap_t))) {
  105. //Region too small to be a heap.
  106. return NULL;
  107. }
  108. heap_t *result = (heap_t *)start_ptr;
  109. size -= sizeof(heap_t);
  110. /* Do not specify any maximum size for the allocations so that the default configuration is used */
  111. const size_t max_bytes = 0;
  112. result->heap_data = tlsf_create_with_pool(start_ptr + sizeof(heap_t), size, max_bytes);
  113. if(!result->heap_data) {
  114. return NULL;
  115. }
  116. result->lock = NULL;
  117. result->free_bytes = size - tlsf_size(result->heap_data);
  118. result->pool_size = size;
  119. result->minimum_free_bytes = result->free_bytes;
  120. return result;
  121. }
  122. void multi_heap_set_lock(multi_heap_handle_t heap, void *lock)
  123. {
  124. heap->lock = lock;
  125. }
  126. void multi_heap_internal_lock(multi_heap_handle_t heap)
  127. {
  128. MULTI_HEAP_LOCK(heap->lock);
  129. }
  130. void multi_heap_internal_unlock(multi_heap_handle_t heap)
  131. {
  132. MULTI_HEAP_UNLOCK(heap->lock);
  133. }
  134. multi_heap_block_handle_t multi_heap_get_first_block(multi_heap_handle_t heap)
  135. {
  136. assert(heap != NULL);
  137. pool_t pool = tlsf_get_pool(heap->heap_data);
  138. block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
  139. return (multi_heap_block_handle_t)block;
  140. }
  141. multi_heap_block_handle_t multi_heap_get_next_block(multi_heap_handle_t heap, multi_heap_block_handle_t block)
  142. {
  143. assert(heap != NULL);
  144. assert_valid_block(heap, block);
  145. block_header_t* next = block_next(block);
  146. if(block_size(next) == 0) {
  147. //Last block:
  148. return NULL;
  149. } else {
  150. return (multi_heap_block_handle_t)next;
  151. }
  152. }
  153. bool multi_heap_is_free(multi_heap_block_handle_t block)
  154. {
  155. return block_is_free(block);
  156. }
  157. void *multi_heap_malloc_impl(multi_heap_handle_t heap, size_t size)
  158. {
  159. if (size == 0 || heap == NULL) {
  160. return NULL;
  161. }
  162. multi_heap_internal_lock(heap);
  163. void *result = tlsf_malloc(heap->heap_data, size);
  164. if(result) {
  165. heap->free_bytes -= tlsf_block_size(result);
  166. heap->free_bytes -= tlsf_alloc_overhead();
  167. if (heap->free_bytes < heap->minimum_free_bytes) {
  168. heap->minimum_free_bytes = heap->free_bytes;
  169. }
  170. }
  171. multi_heap_internal_unlock(heap);
  172. return result;
  173. }
  174. void multi_heap_free_impl(multi_heap_handle_t heap, void *p)
  175. {
  176. if (heap == NULL || p == NULL) {
  177. return;
  178. }
  179. assert_valid_block(heap, block_from_ptr(p));
  180. multi_heap_internal_lock(heap);
  181. heap->free_bytes += tlsf_block_size(p);
  182. heap->free_bytes += tlsf_alloc_overhead();
  183. tlsf_free(heap->heap_data, p);
  184. multi_heap_internal_unlock(heap);
  185. }
  186. void *multi_heap_realloc_impl(multi_heap_handle_t heap, void *p, size_t size)
  187. {
  188. assert(heap != NULL);
  189. if (p == NULL) {
  190. return multi_heap_malloc_impl(heap, size);
  191. }
  192. assert_valid_block(heap, block_from_ptr(p));
  193. if (heap == NULL) {
  194. return NULL;
  195. }
  196. multi_heap_internal_lock(heap);
  197. size_t previous_block_size = tlsf_block_size(p);
  198. void *result = tlsf_realloc(heap->heap_data, p, size);
  199. if(result) {
  200. /* No need to subtract the tlsf_alloc_overhead() as it has already
  201. * been subtracted when allocating the block at first with malloc */
  202. heap->free_bytes += previous_block_size;
  203. heap->free_bytes -= tlsf_block_size(result);
  204. if (heap->free_bytes < heap->minimum_free_bytes) {
  205. heap->minimum_free_bytes = heap->free_bytes;
  206. }
  207. }
  208. multi_heap_internal_unlock(heap);
  209. return result;
  210. }
  211. void *multi_heap_aligned_alloc_impl_offs(multi_heap_handle_t heap, size_t size, size_t alignment, size_t offset)
  212. {
  213. if(heap == NULL) {
  214. return NULL;
  215. }
  216. if(!size) {
  217. return NULL;
  218. }
  219. //Alignment must be a power of two:
  220. if(((alignment & (alignment - 1)) != 0) ||(!alignment)) {
  221. return NULL;
  222. }
  223. multi_heap_internal_lock(heap);
  224. void *result = tlsf_memalign_offs(heap->heap_data, alignment, size, offset);
  225. if(result) {
  226. heap->free_bytes -= tlsf_block_size(result);
  227. heap->free_bytes -= tlsf_alloc_overhead();
  228. if(heap->free_bytes < heap->minimum_free_bytes) {
  229. heap->minimum_free_bytes = heap->free_bytes;
  230. }
  231. }
  232. multi_heap_internal_unlock(heap);
  233. return result;
  234. }
  235. void *multi_heap_aligned_alloc_impl(multi_heap_handle_t heap, size_t size, size_t alignment)
  236. {
  237. return multi_heap_aligned_alloc_impl_offs(heap, size, alignment, 0);
  238. }
  239. #ifdef MULTI_HEAP_POISONING
  240. /*!
  241. * @brief Global definition of print_errors set in multi_heap_check() when
  242. * MULTI_HEAP_POISONING is active. Allows the transfer of the value to
  243. * multi_heap_poisoning.c without having to propagate it to the tlsf submodule
  244. * and back.
  245. */
  246. static bool g_print_errors = false;
  247. /*!
  248. * @brief Definition of the weak function declared in TLSF repository.
  249. * The call of this function execute a check for block poisoning on the memory
  250. * chunk passed as parameter.
  251. *
  252. * @param start: pointer to the start of the memory region to check for corruption
  253. * @param size: size of the memory region to check for corruption
  254. * @param is_free: indicate if the pattern to use the fill the region should be
  255. * an after free or after allocation pattern.
  256. *
  257. * @return bool: true if the the memory is not corrupted, false if the memory if corrupted.
  258. */
  259. bool tlsf_check_hook(void *start, size_t size, bool is_free)
  260. {
  261. return multi_heap_internal_check_block_poisoning(start, size, is_free, g_print_errors);
  262. }
  263. #endif // MULTI_HEAP_POISONING
  264. bool multi_heap_check(multi_heap_handle_t heap, bool print_errors)
  265. {
  266. bool valid = true;
  267. assert(heap != NULL);
  268. multi_heap_internal_lock(heap);
  269. #ifdef MULTI_HEAP_POISONING
  270. g_print_errors = print_errors;
  271. #else
  272. (void) print_errors;
  273. #endif
  274. if(tlsf_check(heap->heap_data)) {
  275. valid = false;
  276. }
  277. if(tlsf_check_pool(tlsf_get_pool(heap->heap_data))) {
  278. valid = false;
  279. }
  280. multi_heap_internal_unlock(heap);
  281. return valid;
  282. }
  283. __attribute__((noinline)) static void multi_heap_dump_tlsf(void* ptr, size_t size, int used, void* user)
  284. {
  285. (void)user;
  286. MULTI_HEAP_STDERR_PRINTF("Block %p data, size: %d bytes, Free: %s \n",
  287. (void *)ptr,
  288. size,
  289. used ? "No" : "Yes");
  290. }
  291. void multi_heap_dump(multi_heap_handle_t heap)
  292. {
  293. assert(heap != NULL);
  294. multi_heap_internal_lock(heap);
  295. MULTI_HEAP_STDERR_PRINTF("Showing data for heap: %p \n", (void *)heap);
  296. tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_dump_tlsf, NULL);
  297. multi_heap_internal_unlock(heap);
  298. }
  299. size_t multi_heap_free_size_impl(multi_heap_handle_t heap)
  300. {
  301. if (heap == NULL) {
  302. return 0;
  303. }
  304. return heap->free_bytes;
  305. }
  306. size_t multi_heap_minimum_free_size_impl(multi_heap_handle_t heap)
  307. {
  308. if (heap == NULL) {
  309. return 0;
  310. }
  311. return heap->minimum_free_bytes;
  312. }
  313. __attribute__((noinline)) static void multi_heap_get_info_tlsf(void* ptr, size_t size, int used, void* user)
  314. {
  315. multi_heap_info_t *info = user;
  316. if(used) {
  317. info->allocated_blocks++;
  318. } else {
  319. info->free_blocks++;
  320. if(size > info->largest_free_block ) {
  321. info->largest_free_block = size;
  322. }
  323. }
  324. info->total_blocks++;
  325. }
  326. void multi_heap_get_info_impl(multi_heap_handle_t heap, multi_heap_info_t *info)
  327. {
  328. uint32_t overhead;
  329. memset(info, 0, sizeof(multi_heap_info_t));
  330. if (heap == NULL) {
  331. return;
  332. }
  333. multi_heap_internal_lock(heap);
  334. tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_get_info_tlsf, info);
  335. /* TLSF has an overhead per block. Calculate the total amount of overhead, it shall not be
  336. * part of the allocated bytes */
  337. overhead = info->allocated_blocks * tlsf_alloc_overhead();
  338. info->total_allocated_bytes = (heap->pool_size - tlsf_size(heap->heap_data)) - heap->free_bytes - overhead;
  339. info->minimum_free_bytes = heap->minimum_free_bytes;
  340. info->total_free_bytes = heap->free_bytes;
  341. info->largest_free_block = tlsf_fit_size(heap->heap_data, info->largest_free_block);
  342. multi_heap_internal_unlock(heap);
  343. }
  344. #endif