11/*
2- * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
2+ * SPDX-FileCopyrightText: 2024-2025 Espressif Systems (Shanghai) CO LTD
33 *
44 * SPDX-License-Identifier: Apache-2.0
55 */
66#include <stdio.h>
7- #include "tlsf.h"
7+ #include <stdbool.h>
8+ #include "esp_rom_tlsf.h"
89#include "tlsf_block_functions.h"
910#include "multi_heap.h"
1011
@@ -34,36 +35,33 @@ static void assert_valid_block(const heap_t *heap, const block_header_t *block)
3435 (uintptr_t )ptr );
3536}
3637
37- int tee_heap_register (void * start_ptr , size_t size )
38+ esp_err_t esp_tee_heap_init (void * start_ptr , size_t size )
3839{
3940 assert (start_ptr );
40- if (size < (sizeof (heap_t ))) {
41- //Region too small to be a heap.
42- return -1 ;
41+ if (size < (tlsf_size () + tlsf_block_size_min () + sizeof (heap_t ))) {
42+ // Region too small to be a heap.
43+ return ESP_ERR_INVALID_SIZE ;
4344 }
4445
4546 heap_t * result = (heap_t * )start_ptr ;
4647 size -= sizeof (heap_t );
4748
48- /* Do not specify any maximum size for the allocations so that the default configuration is used */
49- const size_t max_bytes = 0 ;
50-
51- result -> heap_data = tlsf_create_with_pool (start_ptr + sizeof (heap_t ), size , max_bytes );
49+ result -> heap_data = tlsf_create_with_pool (start_ptr + sizeof (heap_t ), size );
5250 if (result -> heap_data == NULL ) {
53- return -1 ;
51+ return ESP_FAIL ;
5452 }
5553
5654 result -> lock = NULL ;
57- result -> free_bytes = size - tlsf_size (result -> heap_data );
55+ result -> free_bytes = size - tlsf_size ();
5856 result -> pool_size = size ;
5957 result -> minimum_free_bytes = result -> free_bytes ;
6058
6159 tee_heap = (multi_heap_handle_t )result ;
6260
63- return 0 ;
61+ return ESP_OK ;
6462}
6563
66- void * tee_heap_malloc (size_t size )
64+ void * esp_tee_heap_malloc (size_t size )
6765{
6866 if (tee_heap == NULL || size == 0 ) {
6967 return NULL ;
@@ -81,17 +79,17 @@ void *tee_heap_malloc(size_t size)
8179 return result ;
8280}
8381
84- void * tee_heap_calloc (size_t n , size_t size )
82+ void * esp_tee_heap_calloc (size_t n , size_t size )
8583{
8684 size_t reg_size = n * size ;
87- void * ptr = tee_heap_malloc (reg_size );
85+ void * ptr = esp_tee_heap_malloc (reg_size );
8886 if (ptr != NULL ) {
8987 memset (ptr , 0x00 , reg_size );
9088 }
9189 return ptr ;
9290}
9391
94- void * tee_heap_aligned_alloc (size_t size , size_t alignment )
92+ void * esp_tee_heap_aligned_alloc (size_t size , size_t alignment )
9593{
9694 if (tee_heap == NULL || size == 0 ) {
9795 return NULL ;
@@ -114,7 +112,7 @@ void *tee_heap_aligned_alloc(size_t size, size_t alignment)
114112 return result ;
115113}
116114
117- void tee_heap_free (void * p )
115+ void esp_tee_heap_free (void * p )
118116{
119117 if (tee_heap == NULL || p == NULL ) {
120118 return ;
@@ -129,67 +127,64 @@ void tee_heap_free(void *p)
129127
130128void * malloc (size_t size )
131129{
132- return tee_heap_malloc (size );
130+ return esp_tee_heap_malloc (size );
133131}
134132
135133void * calloc (size_t n , size_t size )
136134{
137- return tee_heap_calloc (n , size );
135+ return esp_tee_heap_calloc (n , size );
138136}
139137
140138void free (void * ptr )
141139{
142- tee_heap_free (ptr );
140+ esp_tee_heap_free (ptr );
143141}
144142
145- void tee_heap_dump_free_size (void )
143+ size_t esp_tee_heap_get_free_size (void )
146144{
147- if (tee_heap == NULL ) {
148- return ;
149- }
150- printf ("Free: %uB | Minimum free: %uB\n" , tee_heap -> free_bytes , tee_heap -> minimum_free_bytes );
145+ return tee_heap -> free_bytes ;
146+ }
147+
148+ size_t esp_tee_heap_get_min_free_size (void )
149+ {
150+ return tee_heap -> minimum_free_bytes ;
151151}
152152
153- static bool tee_heap_dump_tlsf (void * ptr , size_t size , int used , void * user )
153+ static void heap_dump_tlsf (void * ptr , size_t size , int used , void * user )
154154{
155155 (void )user ;
156156 printf ("Block %p data, size: %d bytes, Free: %s\n" ,
157157 (void * )ptr ,
158158 size ,
159159 used ? "No" : "Yes" );
160- return true;
161160}
162161
163- void tee_heap_dump_info (void )
162+ void esp_tee_heap_dump_info (void )
164163{
165- if (tee_heap == NULL ) {
166- return ;
167- }
168- printf ("Showing data for TEE heap: %p\n" , (void * )tee_heap );
169- tee_heap_dump_free_size ();
170- tlsf_walk_pool (tlsf_get_pool (tee_heap -> heap_data ), tee_heap_dump_tlsf , NULL );
164+ printf ("Showing data for TEE heap: %p (%uB)\n" , (void * )tee_heap , tee_heap -> pool_size );
165+ tlsf_walk_pool (tlsf_get_pool (tee_heap -> heap_data ), heap_dump_tlsf , NULL );
171166}
172167
173168/* Definitions for functions from the heap component, used in files shared with ESP-IDF */
174169
175170void * heap_caps_malloc (size_t alignment , size_t size , uint32_t caps )
176171{
177172 (void ) caps ;
178- return tee_heap_malloc (size );
173+ return esp_tee_heap_malloc (size );
179174}
180175
181176void * heap_caps_aligned_alloc (size_t alignment , size_t size , uint32_t caps )
182177{
183178 (void ) caps ;
184- return tee_heap_aligned_alloc (size , alignment );
179+ return esp_tee_heap_aligned_alloc (size , alignment );
185180}
186181
187182void * heap_caps_aligned_calloc (size_t alignment , size_t n , size_t size , uint32_t caps )
188183{
189184 (void ) caps ;
190185 uint32_t reg_size = n * size ;
191186
192- void * ptr = tee_heap_aligned_alloc (reg_size , alignment );
187+ void * ptr = esp_tee_heap_aligned_alloc (reg_size , alignment );
193188 if (ptr != NULL ) {
194189 memset (ptr , 0x00 , reg_size );
195190 }
0 commit comments