| 
									
										
										
										
											2021-10-14 10:43:13 +08:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * SPDX-License-Identifier: Apache-2.0 | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | #include <stdint.h>
 | 
					
						
							|  |  |  | #include <stdlib.h>
 | 
					
						
							|  |  |  | #include <stdbool.h>
 | 
					
						
							|  |  |  | #include <assert.h>
 | 
					
						
							|  |  |  | #include <string.h>
 | 
					
						
							|  |  |  | #include <stddef.h>
 | 
					
						
							|  |  |  | #include <stdio.h>
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | #include <sys/cdefs.h>
 | 
					
						
							|  |  |  | #include "heap_tlsf.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | #include <multi_heap.h>
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | #include "multi_heap_internal.h"
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | /* Note: Keep platform-specific parts in this header, this source
 | 
					
						
							|  |  |  |    file should depend on libc only */ | 
					
						
							|  |  |  | #include "multi_heap_platform.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | /* Defines compile-time configuration macros */ | 
					
						
							|  |  |  | #include "multi_heap_config.h"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifndef MULTI_HEAP_POISONING
 | 
					
						
							|  |  |  | /* if no heap poisoning, public API aliases directly to these implementations */ | 
					
						
							|  |  |  | void *multi_heap_malloc(multi_heap_handle_t heap, size_t size) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_malloc_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:17:34 -03:00
										 |  |  | void *multi_heap_aligned_alloc(multi_heap_handle_t heap, size_t size, size_t alignment) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_aligned_alloc_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | void multi_heap_aligned_free(multi_heap_handle_t heap, void *p) | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  |     __attribute__((alias("multi_heap_free_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | void multi_heap_free(multi_heap_handle_t heap, void *p) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_free_impl"))); | 
					
						
							| 
									
										
										
										
											2020-02-28 13:17:34 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | void *multi_heap_realloc(multi_heap_handle_t heap, void *p, size_t size) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_realloc_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t multi_heap_get_allocated_size(multi_heap_handle_t heap, void *p) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_get_allocated_size_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | multi_heap_handle_t multi_heap_register(void *start, size_t size) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_register_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-26 14:47:27 +01:00
										 |  |  | void multi_heap_get_info(multi_heap_handle_t heap, multi_heap_info_t *info) | 
					
						
							| 
									
										
										
										
											2017-05-11 17:56:17 +10:00
										 |  |  |     __attribute__((alias("multi_heap_get_info_impl"))); | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | size_t multi_heap_free_size(multi_heap_handle_t heap) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_free_size_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | size_t multi_heap_minimum_free_size(multi_heap_handle_t heap) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_minimum_free_size_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  | void *multi_heap_get_block_address(multi_heap_block_handle_t block) | 
					
						
							|  |  |  |     __attribute__((alias("multi_heap_get_block_address_impl"))); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void *multi_heap_get_block_owner(multi_heap_block_handle_t block) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return NULL; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | #define ALIGN(X) ((X) & ~(sizeof(void *)-1))
 | 
					
						
							|  |  |  | #define ALIGN_UP(X) ALIGN((X)+sizeof(void *)-1)
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:17:34 -03:00
										 |  |  | #define ALIGN_UP_BY(num, align) (((num) + ((align) - 1)) & ~((align) - 1))
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | typedef struct multi_heap_info { | 
					
						
							|  |  |  |     void *lock; | 
					
						
							|  |  |  |     size_t free_bytes; | 
					
						
							|  |  |  |     size_t minimum_free_bytes; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     size_t pool_size; | 
					
						
							|  |  |  |     tlsf_t heap_data; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } heap_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Return true if this block is free. */ | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | static inline bool is_free(const block_header_t *block) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     return ((block->size & 0x01) != 0); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Data size of the block (excludes this block's header) */ | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | static inline size_t block_data_size(const block_header_t *block) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     return (block->size & ~0x03); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /* Check a block is valid for this heap. Used to verify parameters. */ | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | static void assert_valid_block(const heap_t *heap, const block_header_t *block) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     pool_t pool = tlsf_get_pool(heap->heap_data); | 
					
						
							|  |  |  |     void *ptr = block_to_ptr(block); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  |     MULTI_HEAP_ASSERT((ptr >= pool) && | 
					
						
							|  |  |  |                     (ptr < pool + heap->pool_size), | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |                     (uintptr_t)ptr); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  | void *multi_heap_get_block_address_impl(multi_heap_block_handle_t block) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     void *ptr = block_to_ptr(block); | 
					
						
							|  |  |  |     return (ptr); | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | size_t multi_heap_get_allocated_size_impl(multi_heap_handle_t heap, void *p) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     return tlsf_block_size(p); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-12-20 09:34:24 +11:00
										 |  |  | multi_heap_handle_t multi_heap_register_impl(void *start_ptr, size_t size) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     assert(start_ptr); | 
					
						
							|  |  |  |     if(size < (tlsf_size() + tlsf_block_size_min() + sizeof(heap_t))) { | 
					
						
							|  |  |  |         //Region too small to be a heap.
 | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-20 09:34:24 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     heap_t *result = (heap_t *)start_ptr; | 
					
						
							|  |  |  |     size -= sizeof(heap_t); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     result->heap_data = tlsf_create_with_pool(start_ptr + sizeof(heap_t), size); | 
					
						
							|  |  |  |     if(!result->heap_data) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     result->lock = NULL; | 
					
						
							|  |  |  |     result->free_bytes = size - tlsf_size(); | 
					
						
							|  |  |  |     result->pool_size = size; | 
					
						
							|  |  |  |     result->minimum_free_bytes = result->free_bytes; | 
					
						
							|  |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void multi_heap_set_lock(multi_heap_handle_t heap, void *lock) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     heap->lock = lock; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  | void inline multi_heap_internal_lock(multi_heap_handle_t heap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MULTI_HEAP_LOCK(heap->lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void inline multi_heap_internal_unlock(multi_heap_handle_t heap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     MULTI_HEAP_UNLOCK(heap->lock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  | multi_heap_block_handle_t multi_heap_get_first_block(multi_heap_handle_t heap) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     assert(heap != NULL); | 
					
						
							|  |  |  |     pool_t pool = tlsf_get_pool(heap->heap_data); | 
					
						
							|  |  |  |     block_header_t* block = offset_to_block(pool, -(int)block_header_overhead); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return (multi_heap_block_handle_t)block; | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | multi_heap_block_handle_t multi_heap_get_next_block(multi_heap_handle_t heap, multi_heap_block_handle_t block) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     assert(heap != NULL); | 
					
						
							|  |  |  |     assert_valid_block(heap, block); | 
					
						
							|  |  |  |     block_header_t* next = block_next(block); | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     if(block_data_size(next) == 0) { | 
					
						
							|  |  |  |         //Last block:
 | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     } else { | 
					
						
							|  |  |  |         return (multi_heap_block_handle_t)next; | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-10 01:14:47 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | bool multi_heap_is_free(multi_heap_block_handle_t block) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return is_free(block); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | void *multi_heap_malloc_impl(multi_heap_handle_t heap, size_t size) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2017-10-19 15:59:04 +08:00
										 |  |  |     if (size == 0 || heap == NULL) { | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-02-28 13:17:34 -03:00
										 |  |  | 
 | 
					
						
							|  |  |  |     multi_heap_internal_lock(heap); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     void *result = tlsf_malloc(heap->heap_data, size); | 
					
						
							|  |  |  |     if(result) { | 
					
						
							|  |  |  |         heap->free_bytes -= tlsf_block_size(result); | 
					
						
							|  |  |  |         if (heap->free_bytes < heap->minimum_free_bytes) { | 
					
						
							|  |  |  |             heap->minimum_free_bytes = heap->free_bytes; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-02-28 13:17:34 -03:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     return result; | 
					
						
							| 
									
										
										
										
											2020-02-28 13:17:34 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | void multi_heap_free_impl(multi_heap_handle_t heap, void *p) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (heap == NULL || p == NULL) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     assert_valid_block(heap, p); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     multi_heap_internal_lock(heap); | 
					
						
							|  |  |  |     heap->free_bytes += tlsf_block_size(p); | 
					
						
							|  |  |  |     tlsf_free(heap->heap_data, p); | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | void *multi_heap_realloc_impl(multi_heap_handle_t heap, void *p, size_t size) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     assert(heap != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (p == NULL) { | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  |         return multi_heap_malloc_impl(heap, size); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     assert_valid_block(heap, p); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							|  |  |  |     if (heap == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_lock(heap); | 
					
						
							| 
									
										
										
										
											2020-09-24 17:25:52 -03:00
										 |  |  |     size_t previous_block_size =  tlsf_block_size(p); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     void *result = tlsf_realloc(heap->heap_data, p, size); | 
					
						
							|  |  |  |     if(result) { | 
					
						
							| 
									
										
										
										
											2020-09-24 17:25:52 -03:00
										 |  |  |         heap->free_bytes += previous_block_size; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |         heap->free_bytes -= tlsf_block_size(result); | 
					
						
							|  |  |  |         if (heap->free_bytes < heap->minimum_free_bytes) { | 
					
						
							|  |  |  |             heap->minimum_free_bytes = heap->free_bytes; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 16:50:19 +08:00
										 |  |  | void *multi_heap_aligned_alloc_impl_offs(multi_heap_handle_t heap, size_t size, size_t alignment, size_t offset) | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | { | 
					
						
							|  |  |  |     if(heap == NULL) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     if(!size) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     //Alignment must be a power of two:
 | 
					
						
							|  |  |  |     if(((alignment & (alignment - 1)) != 0) ||(!alignment)) { | 
					
						
							|  |  |  |         return NULL; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     multi_heap_internal_lock(heap); | 
					
						
							| 
									
										
										
										
											2021-01-22 16:50:19 +08:00
										 |  |  |     void *result = tlsf_memalign_offs(heap->heap_data, alignment, size, offset); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     if(result) { | 
					
						
							|  |  |  |         heap->free_bytes -= tlsf_block_size(result); | 
					
						
							|  |  |  |         if(heap->free_bytes < heap->minimum_free_bytes) { | 
					
						
							|  |  |  |             heap->minimum_free_bytes = heap->free_bytes; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     return result; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-22 16:50:19 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | void *multi_heap_aligned_alloc_impl(multi_heap_handle_t heap, size_t size, size_t alignment) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     return multi_heap_aligned_alloc_impl_offs(heap, size, alignment, 0); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | bool multi_heap_check(multi_heap_handle_t heap, bool print_errors) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     (void)print_errors; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     bool valid = true; | 
					
						
							|  |  |  |     assert(heap != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_lock(heap); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     if(tlsf_check(heap->heap_data)) { | 
					
						
							|  |  |  |         valid = false; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     if(tlsf_check_pool(tlsf_get_pool(heap->heap_data))) { | 
					
						
							|  |  |  |         valid = false; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     return valid; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | static void multi_heap_dump_tlsf(void* ptr, size_t size, int used, void* user) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     (void)user; | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  |     MULTI_HEAP_STDERR_PRINTF("Block %p data, size: %d bytes, Free: %s \n", | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |                             (void *)ptr, | 
					
						
							|  |  |  |                             size, | 
					
						
							|  |  |  |                             used ? "No" : "Yes"); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | void multi_heap_dump(multi_heap_handle_t heap) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     assert(heap != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_lock(heap); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     MULTI_HEAP_STDERR_PRINTF("Showing data for heap: %p \n", (void *)heap); | 
					
						
							|  |  |  |     tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_dump_tlsf, NULL); | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | size_t multi_heap_free_size_impl(multi_heap_handle_t heap) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     if (heap == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     return heap->free_bytes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | size_t multi_heap_minimum_free_size_impl(multi_heap_handle_t heap) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							|  |  |  |     if (heap == NULL) { | 
					
						
							|  |  |  |         return 0; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     return heap->minimum_free_bytes; | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | static void multi_heap_get_info_tlsf(void* ptr, size_t size, int used, void* user) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     multi_heap_info_t *info = user; | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     if(used) { | 
					
						
							|  |  |  |         info->allocated_blocks++; | 
					
						
							|  |  |  |     } else { | 
					
						
							|  |  |  |         info->free_blocks++; | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |         if(size > info->largest_free_block ) { | 
					
						
							|  |  |  |             info->largest_free_block = size; | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2020-11-10 18:40:01 +11:00
										 |  |  | 
 | 
					
						
							|  |  |  |     info->total_blocks++; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-05-08 15:25:30 +10:00
										 |  |  | void multi_heap_get_info_impl(multi_heap_handle_t heap, multi_heap_info_t *info) | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | { | 
					
						
							| 
									
										
										
										
											2021-10-14 10:43:13 +08:00
										 |  |  |     uint32_t sl_interval; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     memset(info, 0, sizeof(multi_heap_info_t)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     if (heap == NULL) { | 
					
						
							|  |  |  |         return; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_lock(heap); | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     tlsf_walk_pool(tlsf_get_pool(heap->heap_data), multi_heap_get_info_tlsf, info); | 
					
						
							|  |  |  |     info->total_allocated_bytes = (heap->pool_size - tlsf_size()) - heap->free_bytes; | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  |     info->minimum_free_bytes = heap->minimum_free_bytes; | 
					
						
							| 
									
										
										
										
											2020-01-16 15:37:19 -03:00
										 |  |  |     info->total_free_bytes = heap->free_bytes; | 
					
						
							| 
									
										
										
										
											2021-10-14 10:43:13 +08:00
										 |  |  |     if (info->largest_free_block) { | 
					
						
							|  |  |  |         sl_interval = (1 << (31 - __builtin_clz(info->largest_free_block))) / SL_INDEX_COUNT; | 
					
						
							|  |  |  |         info->largest_free_block = info->largest_free_block & ~(sl_interval - 1); | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-10-18 14:54:55 +08:00
										 |  |  |     multi_heap_internal_unlock(heap); | 
					
						
							| 
									
										
										
										
											2017-04-28 14:08:58 +10:00
										 |  |  | } |