multi_heap.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. // http://www.apache.org/licenses/LICENSE-2.0
  7. //
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. #include <stdint.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <assert.h>
  17. #include <string.h>
  18. #include <stddef.h>
  19. #include <stdio.h>
  20. #include <sys/cdefs.h>
  21. #include "heap_tlsf.h"
  22. #include <multi_heap.h>
  23. #include "multi_heap_internal.h"
  24. /* Note: Keep platform-specific parts in this header, this source
  25. file should depend on libc only */
  26. #include "multi_heap_platform.h"
  27. /* Defines compile-time configuration macros */
  28. #include "multi_heap_config.h"
  29. #ifndef MULTI_HEAP_POISONING
  30. /* if no heap poisoning, public API aliases directly to these implementations */
  31. void *multi_heap_malloc(multi_heap_handle_t heap, size_t size)
  32. __attribute__((alias("multi_heap_malloc_impl")));
  33. void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment)
  34. __attribute__((alias("multi_heap_aligned_alloc_impl")));
  35. void multi_heap_aligned_free(multi_heap_handle_t heap, void *p)
  36. __attribute__((alias("multi_heap_free_impl")));
  37. void multi_heap_free(multi_heap_handle_t heap, void *p)
  38. __attribute__((alias("multi_heap_free_impl")));
  39. void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size)
  40. __attribute__((alias("multi_heap_realloc_impl")));
  41. size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p)
  42. __attribute__((alias("multi_heap_get_allocated_size_impl")));
  43. multi_heap_handle_t multi_heap_register(void *start, size_t size)
  44. __attribute__((alias("multi_heap_register_impl")));
  45. void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info)
  46. __attribute__((alias("multi_heap_get_info_impl")));
  47. size_t multi_heap_free_size(multi_heap_handle_t heap)
  48. __attribute__((alias("multi_heap_free_size_impl")));
  49. size_t multi_heap_minimum_free_size(multi_heap_handle_t heap)
  50. __attribute__((alias("multi_heap_minimum_free_size_impl")));
  51. void *multi_heap_get_block_address(multi_heap_block_handle_t block)
  52. __attribute__((alias("multi_heap_get_block_address_impl")));
  53. void *multi_heap_get_block_owner(multi_heap_block_handle_t block)
  54. {
  55. return NULL;
  56. }
  57. #endif
  58. #define ALIGN(X) ((X) & ~(sizeof(void *)-1))
  59. #define ALIGN_UP(X) ALIGN((X)+sizeof(void *)-1)
  60. #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
  61. typedef struct multi_heap_info {
  62. void *lock;
  63. size_t free_bytes;
  64. size_t minimum_free_bytes;
  65. size_t pool_size;
  66. tlsf_t heap_data;
  67. } heap_t;
  68. /* Return true if this block is free. */
  69. static inline bool is_free(const block_header_t *block)
  70. {
  71. return ((block->size & 0x01) != 0);
  72. }
  73. /* Data size of the block (excludes this block's header) */
  74. static inline size_t block_data_size(const block_header_t *block)
  75. {
  76. return (block->size & ~0x03);
  77. }
  78. /* Check a block is valid for this heap. Used to verify parameters. */
  79. static void assert_valid_block(const heap_t *heap, const block_header_t *block)
  80. {
  81. pool_t pool = tlsf_get_pool(heap->heap_data);
  82. void *ptr = block_to_ptr(block);
  83. MULTI_HEAP_ASSERT((ptr >= pool) &&
  84. (ptr < pool + heap->pool_size),
  85. (uintptr_t)ptr);
  86. }
  87. void *multi_heap_get_block_address_impl(multi_heap_block_handle_t block)
  88. {
  89. void *ptr = block_to_ptr(block);
  90. return (ptr);
  91. }
  92. size_t multi_heap_get_allocated_size_impl(multi_heap_handle_t heap, void *p)
  93. {
  94. return tlsf_block_size(p);
  95. }
  96. multi_heap_handle_t multi_heap_register_impl(void *start_ptr, size_t size)
  97. {
  98. assert(start_ptr);
  99. if(size < (tlsf_size(NULL) + tlsf_block_size_min() + sizeof(heap_t))) {
  100. //Region too small to be a heap.
  101. return NULL;
  102. }
  103. heap_t *result = (heap_t *)start_ptr;
  104. size -= sizeof(heap_t);
  105. result->heap_data = tlsf_create_with_pool(start_ptr + sizeof(heap_t), size, 0);
  106. if(!result->heap_data) {
  107. return NULL;
  108. }
  109. result->lock = NULL;
  110. result->free_bytes = size - tlsf_size(result->heap_data);
  111. result->pool_size = size;
  112. result->minimum_free_bytes = result->free_bytes;
  113. return result;
  114. }
  115. void multi_heap_set_lock(multi_heap_handle_t heap, void *lock)
  116. {
  117. heap->lock = lock;
  118. }
  119. void inline multi_heap_internal_lock(multi_heap_handle_t heap)
  120. {
  121. MULTI_HEAP_LOCK(heap->lock);
  122. }
  123. void inline multi_heap_internal_unlock(multi_heap_handle_t heap)
  124. {
  125. MULTI_HEAP_UNLOCK(heap->lock);
  126. }
  127. multi_heap_block_handle_t multi_heap_get_first_block(multi_heap_handle_t heap)
  128. {
  129. assert(heap != NULL);
  130. pool_t pool = tlsf_get_pool(heap->heap_data);
  131. block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
  132. return (multi_heap_block_handle_t)block;
  133. }
  134. multi_heap_block_handle_t multi_heap_get_next_block(multi_heap_handle_t heap, multi_heap_block_handle_t block)
  135. {
  136. assert(heap != NULL);
  137. assert_valid_block(heap, block);
  138. block_header_t* next = block_next(block);
  139. if(block_data_size(next) == 0) {
  140. //Last block:
  141. return NULL;
  142. } else {
  143. return (multi_heap_block_handle_t)next;
  144. }
  145. }
  146. bool multi_heap_is_free(multi_heap_block_handle_t block)
  147. {
  148. return is_free(block);
  149. }
  150. void *multi_heap_malloc_impl(multi_heap_handle_t heap, size_t size)
  151. {
  152. if (size == 0 || heap == NULL) {
  153. return NULL;
  154. }
  155. multi_heap_internal_lock(heap);
  156. void *result = tlsf_malloc(heap->heap_data, size);
  157. if(result) {
  158. heap->free_bytes -= tlsf_block_size(result);
  159. if (heap->free_bytes < heap->minimum_free_bytes) {
  160. heap->minimum_free_bytes = heap->free_bytes;
  161. }
  162. }
  163. multi_heap_internal_unlock(heap);
  164. return result;
  165. }
  166. void multi_heap_free_impl(multi_heap_handle_t heap, void *p)
  167. {
  168. if (heap == NULL || p == NULL) {
  169. return;
  170. }
  171. assert_valid_block(heap, p);
  172. multi_heap_internal_lock(heap);
  173. heap->free_bytes += tlsf_block_size(p);
  174. tlsf_free(heap->heap_data, p);
  175. multi_heap_internal_unlock(heap);
  176. }
  177. void *multi_heap_realloc_impl(multi_heap_handle_t heap, void *p, size_t size)
  178. {
  179. assert(heap != NULL);
  180. if (p == NULL) {
  181. return multi_heap_malloc_impl(heap, size);
  182. }
  183. assert_valid_block(heap, p);
  184. if (heap == NULL) {
  185. return NULL;
  186. }
  187. multi_heap_internal_lock(heap);
  188. size_t previous_block_size = tlsf_block_size(p);
  189. void *result = tlsf_realloc(heap->heap_data, p, size);
  190. if(result) {
  191. heap->free_bytes += previous_block_size;
  192. heap->free_bytes -= tlsf_block_size(result);
  193. if (heap->free_bytes < heap->minimum_free_bytes) {
  194. heap->minimum_free_bytes = heap->free_bytes;
  195. }
  196. }
  197. multi_heap_internal_unlock(heap);
  198. return result;
  199. }
  200. void *multi_heap_aligned_alloc_impl_offs(multi_heap_handle_t heap, size_t size, size_t alignment, size_t offset)
  201. {
  202. if(heap == NULL) {
  203. return NULL;
  204. }
  205. if(!size) {
  206. return NULL;
  207. }
  208. //Alignment must be a power of two:
  209. if(((alignment & (alignment - 1)) != 0) ||(!alignment)) {
  210. return NULL;
  211. }
  212. multi_heap_internal_lock(heap);
  213. void *result = tlsf_memalign_offs(heap->heap_data, alignment, size, offset);
  214. if(result) {
  215. heap->free_bytes -= tlsf_block_size(result);
  216. if(heap->free_bytes < heap->minimum_free_bytes) {
  217. heap->minimum_free_bytes = heap->free_bytes;
  218. }
  219. }
  220. multi_heap_internal_unlock(heap);
  221. return result;
  222. }
  223. void *multi_heap_aligned_alloc_impl(multi_heap_handle_t heap, size_t size, size_t alignment)
  224. {
  225. return multi_heap_aligned_alloc_impl_offs(heap, size, alignment, 0);
  226. }
  227. bool multi_heap_check(multi_heap_handle_t heap, bool print_errors)
  228. {
  229. (void)print_errors;
  230. bool valid = true;
  231. assert(heap != NULL);
  232. multi_heap_internal_lock(heap);
  233. if(tlsf_check(heap->heap_data)) {
  234. valid = false;
  235. }
  236. if(tlsf_check_pool(tlsf_get_pool(heap->heap_data))) {
  237. valid = false;
  238. }
  239. multi_heap_internal_unlock(heap);
  240. return valid;
  241. }
  242. static void multi_heap_dump_tlsf(void* ptr, size_t size, int used, void* user)
  243. {
  244. (void)user;
  245. MULTI_HEAP_STDERR_PRINTF("Block %p data, size: %d bytes, Free: %s \n",
  246. (void *)ptr,
  247. size,
  248. used ? "No" : "Yes");
  249. }
  250. void multi_heap_dump(multi_heap_handle_t heap)
  251. {
  252. assert(heap != NULL);
  253. multi_heap_internal_lock(heap);
  254. MULTI_HEAP_STDERR_PRINTF("Showing data for heap: %p \n", (void *)heap);
  255. tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_dump_tlsf, NULL);
  256. multi_heap_internal_unlock(heap);
  257. }
  258. size_t multi_heap_free_size_impl(multi_heap_handle_t heap)
  259. {
  260. if (heap == NULL) {
  261. return 0;
  262. }
  263. return heap->free_bytes;
  264. }
  265. size_t multi_heap_minimum_free_size_impl(multi_heap_handle_t heap)
  266. {
  267. if (heap == NULL) {
  268. return 0;
  269. }
  270. return heap->minimum_free_bytes;
  271. }
  272. static void multi_heap_get_info_tlsf(void* ptr, size_t size, int used, void* user)
  273. {
  274. multi_heap_info_t *info = user;
  275. if(used) {
  276. info->allocated_blocks++;
  277. } else {
  278. info->free_blocks++;
  279. if(size > info->largest_free_block ) {
  280. info->largest_free_block = size;
  281. }
  282. }
  283. info->total_blocks++;
  284. }
  285. void multi_heap_get_info_impl(multi_heap_handle_t heap, multi_heap_info_t *info)
  286. {
  287. memset(info, 0, sizeof(multi_heap_info_t));
  288. if (heap == NULL) {
  289. return;
  290. }
  291. multi_heap_internal_lock(heap);
  292. tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_get_info_tlsf, info);
  293. info->total_allocated_bytes = (heap->pool_size - tlsf_size(heap->heap_data)) - heap->free_bytes;
  294. info->minimum_free_bytes = heap->minimum_free_bytes;
  295. info->total_free_bytes = heap->free_bytes;
  296. info->largest_free_block = tlsf_fit_size(heap->heap_data, info->largest_free_block);
  297. multi_heap_internal_unlock(heap);
  298. }