diff --git a/tlsf.c b/tlsf.c index 6b9bb30..d9fdb4c 100644 --- a/tlsf.c +++ b/tlsf.c @@ -794,11 +794,11 @@ int tlsf_check_pool(pool_t pool) size_t tlsf_fit_size(tlsf_t tlsf, size_t size) { /* because it's GoodFit, allocable size is one range lower */ - if (size) + if (size && tlsf != NULL) { size_t sl_interval; control_t* control = tlsf_cast(control_t*, tlsf); - sl_interval = (1 << ((sizeof(size_t) * 8 - 1) - __builtin_clz(size))) / control->sl_index_count; + sl_interval = (1 << (32 - __builtin_clz(size) - 1)) / control->sl_index_count; return size & ~(sl_interval - 1); } @@ -831,6 +831,10 @@ size_t tlsf_block_size_min(void) size_t tlsf_block_size_max(tlsf_t tlsf) { + if (tlsf == NULL) + { + return 0; + } control_t* control = tlsf_cast(control_t*, tlsf); return tlsf_cast(size_t, 1) << control->fl_index_max; } @@ -951,15 +955,20 @@ tlsf_t tlsf_create(void* mem, size_t max_bytes) #if _DEBUG if (test_ffs_fls()) { - return 0; + return NULL; } #endif + if (mem == NULL) + { + return NULL; + } + if (((tlsfptr_t)mem % ALIGN_SIZE) != 0) { printf("tlsf_create: Memory must be aligned to %u bytes.\n", (unsigned int)ALIGN_SIZE); - return 0; + return NULL; } control_t* control_ptr = control_construct(tlsf_cast(control_t*, mem), max_bytes); diff --git a/tlsf.h b/tlsf.h index 400e837..7f6cc18 100644 --- a/tlsf.h +++ b/tlsf.h @@ -46,6 +46,15 @@ size_t tlsf_block_size_min(void); size_t tlsf_block_size_max(tlsf_t tlsf); size_t tlsf_pool_overhead(void); size_t tlsf_alloc_overhead(void); + +/** + * @brief Return the allocable size based on the size passed + * as parameter + * + * @param tlsf Pointer to the tlsf structure + * @param size The allocation size + * @return size_t The updated allocation size + */ size_t tlsf_fit_size(tlsf_t tlsf, size_t size); /* Debugging. */