Skip to content

Commit 8ff8b52

Browse files
committed
Merge branch 'feat/update-to-latest-tlsf' into 'master'
feat(heap): Update component to latest TLSF See merge request espressif/esp-idf!33193
2 parents fe29994 + ccd8486 commit 8ff8b52

File tree

9 files changed

+45
-115
lines changed

9 files changed

+45
-115
lines changed

components/esp_rom/patches/esp_rom_tlsf.c

Lines changed: 5 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -20,91 +20,12 @@
2020
#include "esp_rom_caps.h"
2121
#include "esp_rom_tlsf.h"
2222

23-
/*!
24-
* @brief Opaque types for TLSF implementation
25-
*/
23+
#include "tlsf_block_functions.h"
24+
#include "tlsf_control_functions.h"
25+
26+
/* Definition of types used in TLSF */
2627
typedef void* tlsf_t;
2728
typedef void* pool_t;
28-
typedef ptrdiff_t tlsfptr_t;
29-
30-
/* ----------------------------------------------------------------
31-
* Bring certain inline functions, macro and structures from the
32-
* tlsf ROM implementation to be able to compile the patch.
33-
* ---------------------------------------------------------------- */
34-
35-
#if !defined (tlsf_assert)
36-
#define tlsf_assert assert
37-
#endif
38-
39-
#define tlsf_cast(t, exp) ((t) (exp))
40-
41-
#define block_header_free_bit (1 << 0)
42-
#define block_header_prev_free_bit (1 << 1)
43-
#define block_header_overhead (sizeof(size_t))
44-
#define block_start_offset (offsetof(block_header_t, size) + sizeof(size_t))
45-
46-
typedef struct block_header_t
47-
{
48-
/* Points to the previous physical block. */
49-
struct block_header_t* prev_phys_block;
50-
51-
/* The size of this block, excluding the block header. */
52-
size_t size;
53-
54-
/* Next and previous free blocks. */
55-
struct block_header_t* next_free;
56-
struct block_header_t* prev_free;
57-
} block_header_t;
58-
59-
static inline __attribute__((__always_inline__)) size_t block_size(const block_header_t* block)
60-
{
61-
return block->size & ~(block_header_free_bit | block_header_prev_free_bit);
62-
}
63-
64-
static inline __attribute__((__always_inline__)) int block_is_free(const block_header_t* block)
65-
{
66-
return tlsf_cast(int, block->size & block_header_free_bit);
67-
}
68-
69-
static inline __attribute__((__always_inline__)) int block_is_prev_free(const block_header_t* block)
70-
{
71-
return tlsf_cast(int, block->size & block_header_prev_free_bit);
72-
}
73-
74-
static inline __attribute__((always_inline)) block_header_t* block_from_ptr(const void* ptr)
75-
{
76-
return tlsf_cast(block_header_t*,
77-
tlsf_cast(unsigned char*, ptr) - block_start_offset);
78-
}
79-
80-
static inline __attribute__((always_inline)) block_header_t* offset_to_block(const void* ptr, size_t size)
81-
{
82-
return tlsf_cast(block_header_t*, tlsf_cast(tlsfptr_t, ptr) + size);
83-
}
84-
85-
static inline __attribute__((always_inline)) int block_is_last(const block_header_t* block)
86-
{
87-
return block_size(block) == 0;
88-
}
89-
90-
static inline __attribute__((always_inline)) void* block_to_ptr(const block_header_t* block)
91-
{
92-
return tlsf_cast(void*,
93-
tlsf_cast(unsigned char*, block) + block_start_offset);
94-
}
95-
96-
static inline __attribute__((always_inline)) block_header_t* block_next(const block_header_t* block)
97-
{
98-
block_header_t* next = offset_to_block(block_to_ptr(block),
99-
block_size(block) - block_header_overhead);
100-
tlsf_assert(!block_is_last(block));
101-
return next;
102-
}
103-
104-
/* ----------------------------------------------------------------
105-
* End of the environment necessary to compile and link the patch
106-
* defined below
107-
* ---------------------------------------------------------------- */
10829

10930
static poison_check_pfunc_t s_poison_check_region = NULL;
11031

@@ -237,6 +158,7 @@ struct heap_tlsf_stub_table_t {
237158
extern struct heap_tlsf_stub_table_t* heap_tlsf_table_ptr;
238159

239160
/* We will copy the ROM table and modify the functions we patch */
161+
240162
struct heap_tlsf_stub_table_t heap_tlsf_patch_table_ptr;
241163

242164
/*!

components/heap/CMakeLists.txt

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,24 @@ if(${target} STREQUAL "linux")
77
return()
88
endif()
99

10-
set(srcs
11-
"heap_caps_base.c"
12-
"heap_caps.c"
13-
"heap_caps_init.c"
14-
"multi_heap.c")
10+
set(srcs "heap_caps_base.c"
11+
"heap_caps.c"
12+
"heap_caps_init.c"
13+
"multi_heap.c")
1514

16-
set(includes "include")
15+
# the root dir of TLSF submodule contains headers with static inline
16+
# functions used in the esp_rom component for TLSF patches. Therefore,
17+
# the tlsf/ dir must be included in the list of public includes.
18+
set(includes "include"
19+
"tlsf")
20+
21+
# The TLSF "public" includes should only be used
22+
# inside the heap component and are therefore added
23+
# to the list of the private includes from the heap
24+
# component perspective
25+
set(priv_includes "tlsf/include")
1726

1827
if(NOT CONFIG_HEAP_TLSF_USE_ROM_IMPL)
19-
set(priv_includes "tlsf")
2028
list(APPEND srcs "tlsf/tlsf.c")
2129
endif()
2230

components/heap/linker.lf

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,6 @@ entries:
55
if HEAP_TLSF_USE_ROM_IMPL = n:
66
tlsf:tlsf_block_size (noflash)
77
tlsf:tlsf_size (noflash)
8-
tlsf:tlsf_align_size (noflash)
9-
tlsf:tlsf_block_size_min (noflash)
10-
tlsf:tlsf_block_size_max (noflash)
118
tlsf:tlsf_alloc_overhead (noflash)
129
tlsf:tlsf_get_pool (noflash)
1310
tlsf:tlsf_malloc (noflash)

components/heap/multi_heap.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,8 @@
1515
#include "multi_heap.h"
1616
#include "multi_heap_internal.h"
1717

18-
#if !CONFIG_HEAP_TLSF_USE_ROM_IMPL
1918
#include "tlsf.h"
2019
#include "tlsf_block_functions.h"
21-
#endif
2220

2321
/* Note: Keep platform-specific parts in this header, this source
2422
file should depend on libc only */
@@ -110,7 +108,7 @@ void multi_heap_in_rom_init(void)
110108
#else // CONFIG_HEAP_TLSF_USE_ROM_IMPL
111109

112110
/* Check a block is valid for this heap. Used to verify parameters. */
113-
__attribute__((noinline)) NOCLONE_ATTR static void assert_valid_block(const heap_t *heap, const block_header_t *block)
111+
__attribute__((noinline)) NOCLONE_ATTR static void assert_valid_block(const heap_t *heap, const multi_heap_block_handle_t block)
114112
{
115113
pool_t pool = tlsf_get_pool(heap->heap_data);
116114
void *ptr = block_to_ptr(block);
@@ -175,22 +173,22 @@ multi_heap_block_handle_t multi_heap_get_first_block(multi_heap_handle_t heap)
175173
{
176174
assert(heap != NULL);
177175
pool_t pool = tlsf_get_pool(heap->heap_data);
178-
block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
176+
multi_heap_block_handle_t block = offset_to_block(pool, -(int)block_header_overhead);
179177

180-
return (multi_heap_block_handle_t)block;
178+
return block;
181179
}
182180

183181
multi_heap_block_handle_t multi_heap_get_next_block(multi_heap_handle_t heap, multi_heap_block_handle_t block)
184182
{
185183
assert(heap != NULL);
186184
assert_valid_block(heap, block);
187-
block_header_t* next = block_next(block);
185+
multi_heap_block_handle_t next = block_next(block);
188186

189187
if(block_size(next) == 0) {
190188
//Last block:
191189
return NULL;
192190
} else {
193-
return (multi_heap_block_handle_t)next;
191+
return next;
194192
}
195193

196194
}

components/heap/test_apps/heap_tests/main/test_malloc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ TEST_CASE("test get allocated size", "[heap]")
176176
const size_t aligned_size = (alloc_sizes[i] + 3) & ~3;
177177
const size_t real_size = heap_caps_get_allocated_size(ptr_array[i]);
178178
printf("initial size: %d, requested size : %d, allocated size: %d\n", alloc_sizes[i], aligned_size, real_size);
179-
TEST_ASSERT_EQUAL(aligned_size, real_size);
179+
TEST_ASSERT(aligned_size <= real_size);
180180

181181
heap_caps_free(ptr_array[i]);
182182
}

components/heap/test_multi_heap_host/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ SOURCE_FILES = $(abspath \
1313
main.cpp \
1414
)
1515

16-
INCLUDE_FLAGS = -I../include -I../../../tools/catch -I../tlsf
16+
INCLUDE_FLAGS = -I../include -I../../../tools/catch -I../tlsf -I../tlsf/include
1717

1818
GCOV ?= gcov
1919

components/heap/test_multi_heap_host/test_multi_heap.cpp

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@
88
#include "multi_heap.h"
99

1010
#include "../multi_heap_config.h"
11-
#include "../tlsf/tlsf.h"
12-
#include "../tlsf/tlsf_common.h"
11+
#include "../tlsf/include/tlsf.h"
1312
#include "../tlsf/tlsf_block_functions.h"
13+
#include "../tlsf/tlsf_control_functions.h"
1414

1515
#include <string.h>
1616
#include <assert.h>
@@ -19,7 +19,7 @@
1919
* malloc and free and allocate memory from the host heap. Since the test
2020
* `TEST_CASE("multi_heap many random allocations", "[multi_heap]")`
2121
* calls multi_heap_allocation_impl() with sizes that can go up to 8MB,
22-
* an allocatation on the heap will be prefered rather than the stack which
22+
* an allocatation on the heap will be preferred rather than the stack which
2323
* might not have the necessary memory.
2424
*/
2525
static void *__malloc__(size_t bytes)
@@ -62,7 +62,7 @@ TEST_CASE("multi_heap simple allocations", "[multi_heap]")
6262
REQUIRE( (intptr_t)buf < (intptr_t)(small_heap + sizeof(small_heap)));
6363

6464
REQUIRE( multi_heap_get_allocated_size(heap, buf) >= test_alloc_size );
65-
REQUIRE( multi_heap_get_allocated_size(heap, buf) < test_alloc_size + 16);
65+
printf("test alloc size %d\n", test_alloc_size);
6666

6767
memset(buf, 0xEE, test_alloc_size);
6868

@@ -482,7 +482,7 @@ TEST_CASE("multi_heap aligned allocations", "[multi_heap]")
482482
{
483483
uint8_t test_heap[4 * 1024];
484484
multi_heap_handle_t heap = multi_heap_register(test_heap, sizeof(test_heap));
485-
uint32_t aligments = 0; // starts from alignment by 4-byte boundary
485+
uint32_t alignments = 0; // starts from alignment by 4-byte boundary
486486
size_t old_size = multi_heap_free_size(heap);
487487
size_t leakage = 1024;
488488
printf("[ALIGNED_ALLOC] heap_size before: %d \n", old_size);
@@ -491,26 +491,26 @@ TEST_CASE("multi_heap aligned allocations", "[multi_heap]")
491491
multi_heap_dump(heap);
492492
printf("*********************\n");
493493

494-
for(;aligments <= 256; aligments++) {
494+
for(;alignments <= 256; alignments++) {
495495

496496
//Use some stupid size value to test correct alignment even in strange
497497
//memory layout objects:
498-
uint8_t *buf = (uint8_t *)multi_heap_aligned_alloc(heap, (aligments + 137), aligments );
499-
if(((aligments & (aligments - 1)) != 0) || (!aligments)) {
498+
uint8_t *buf = (uint8_t *)multi_heap_aligned_alloc(heap, (alignments + 137), alignments );
499+
if(((alignments & (alignments - 1)) != 0) || (!alignments)) {
500500
REQUIRE( buf == NULL );
501501
} else {
502502
REQUIRE( buf != NULL );
503503
REQUIRE((intptr_t)buf >= (intptr_t)test_heap);
504504
REQUIRE((intptr_t)buf < (intptr_t)(test_heap + sizeof(test_heap)));
505505

506-
printf("[ALIGNED_ALLOC] alignment required: %u \n", aligments);
506+
printf("[ALIGNED_ALLOC] alignment required: %u \n", alignments);
507507
printf("[ALIGNED_ALLOC] address of allocated memory: %p \n\n", (void *)buf);
508508
//Address of obtained block must be aligned with selected value
509-
REQUIRE(((intptr_t)buf & (aligments - 1)) == 0);
509+
REQUIRE(((intptr_t)buf & (alignments - 1)) == 0);
510510

511511
//Write some data, if it corrupts memory probably the heap
512512
//canary verification will fail:
513-
memset(buf, 0xA5, (aligments + 137));
513+
memset(buf, 0xA5, (alignments + 137));
514514

515515
multi_heap_free(heap, buf);
516516
}
@@ -519,7 +519,7 @@ TEST_CASE("multi_heap aligned allocations", "[multi_heap]")
519519
/* Check that TLSF doesn't allocate a memory space smaller than required.
520520
* In any case, TLSF will write data in the previous block than the one
521521
* allocated. Thus, we should try to get/allocate this previous block. If
522-
* the poisoned filled pattern has beeen overwritten by TLSF, then this
522+
* the poisoned filled pattern has been overwritten by TLSF, then this
523523
* previous block will trigger an exception.
524524
* More info on this bug in !16296. */
525525
const size_t size = 50; /* TLSF will round the size up */

tools/ci/check_public_headers_exceptions.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ components/cmock/CMock/src/cmock_internals.h
7070

7171
components/openthread/openthread/
7272

73+
# The following TLSF headers contain object definitions but have to be
74+
# made public to be used in esp_rom to help patching the TLSF in ROM.
75+
components/heap/tlsf/tlsf_block_functions.h
76+
components/heap/tlsf/tlsf_control_functions.h
77+
7378
### Here are the files that do not compile for some reason
7479
#
7580
components/app_trace/include/esp_sysview_trace.h

0 commit comments

Comments
 (0)