Skip to content

Commit ea82911

Browse files
committed
Merge branch 'fix/block_allocation_size' into 'idf'
Fix block allocation size See merge request espressif/tlsf!12
2 parents 36e304a + c499512 commit ea82911

File tree

1 file changed

+18
-12
lines changed

1 file changed

+18
-12
lines changed

tlsf.c

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -280,14 +280,14 @@ static inline __attribute__((always_inline)) void mapping_insert(control_t* cont
280280
}
281281

282282
/* This version rounds up to the next block size (for allocations) */
283-
static inline __attribute__((always_inline)) void mapping_search(control_t* control, size_t size, int* fli, int* sli)
283+
static inline __attribute__((always_inline)) void mapping_search(control_t* control, size_t* size, int* fli, int* sli)
284284
{
285-
if (size >= control->small_block_size)
285+
if (*size >= control->small_block_size)
286286
{
287-
const size_t round = (1 << (tlsf_fls_sizet(size) - control->sl_index_count_log2)) - 1;
288-
size += round;
287+
const size_t round = (1 << (tlsf_fls_sizet(*size) - control->sl_index_count_log2));
288+
*size = align_up(*size, round);
289289
}
290-
mapping_insert(control, size, fli, sli);
290+
mapping_insert(control, *size, fli, sli);
291291
}
292292

293293
static inline __attribute__((always_inline)) block_header_t* search_suitable_block(control_t* control, int* fli, int* sli)
@@ -540,12 +540,12 @@ static inline __attribute__((always_inline)) block_header_t* block_trim_free_lea
540540
return remaining_block;
541541
}
542542

543-
static inline __attribute__((always_inline)) block_header_t* block_locate_free(control_t* control, size_t size)
543+
static inline __attribute__((always_inline)) block_header_t* block_locate_free(control_t* control, size_t* size)
544544
{
545545
int fl = 0, sl = 0;
546546
block_header_t* block = 0;
547547

548-
if (size)
548+
if (*size)
549549
{
550550
mapping_search(control, size, &fl, &sl);
551551

@@ -563,7 +563,7 @@ static inline __attribute__((always_inline)) block_header_t* block_locate_free(c
563563

564564
if (block)
565565
{
566-
tlsf_assert(block_size(block) >= size);
566+
tlsf_assert(block_size(block) >= *size);
567567
remove_free_block(control, block, fl, sl);
568568
}
569569

@@ -1011,8 +1011,14 @@ pool_t tlsf_get_pool(tlsf_t tlsf)
10111011
void* tlsf_malloc(tlsf_t tlsf, size_t size)
10121012
{
10131013
control_t* control = tlsf_cast(control_t*, tlsf);
1014-
const size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE);
1015-
block_header_t* block = block_locate_free(control, adjust);
1014+
size_t adjust = adjust_request_size(tlsf, size, ALIGN_SIZE);
1015+
// Returned size is 0 when the requested size is larger than the max block
1016+
// size.
1017+
if (adjust == 0) {
1018+
return NULL;
1019+
}
1020+
// block_locate_free() may adjust our allocated size further.
1021+
block_header_t* block = block_locate_free(control, &adjust);
10161022
return block_prepare_used(control, block, adjust);
10171023
}
10181024

@@ -1138,9 +1144,9 @@ void* tlsf_memalign_offs(tlsf_t tlsf, size_t align, size_t size, size_t data_off
11381144
** alignment constraint. Thus, the gap is not required.
11391145
** If we requested 0 bytes, return null, as tlsf_malloc(0) does.
11401146
*/
1141-
const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;
1147+
size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;
11421148

1143-
block_header_t* block = block_locate_free(control, aligned_size);
1149+
block_header_t* block = block_locate_free(control, &aligned_size);
11441150

11451151
/* This can't be a static assert. */
11461152
tlsf_assert(sizeof(block_header_t) == block_size_min + block_header_overhead);

0 commit comments

Comments
 (0)