1
0
mirror of https://github.com/lvgl/lvgl.git synced 2025-01-14 06:42:58 +08:00

fix(mem) add lv_ prefix to tlsf functions and types

It avoids collision if tlsf is used by other libs in the project too
Fixes https://github.com/lvgl/lvgl/issues/2116#issuecomment-857765919
This commit is contained in:
Gabor Kiss-Vamosi 2021-06-10 12:22:40 +02:00
parent ad05e196fb
commit 0d52b59cb1
3 changed files with 74 additions and 74 deletions

View File

@ -51,7 +51,7 @@
* STATIC VARIABLES * STATIC VARIABLES
**********************/ **********************/
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
static tlsf_t tlsf; static lv_tlsf_t tlsf;
#endif #endif
static uint32_t zero_mem = ZERO_MEM_SENTINEL; /*Give the address of this variable if 0 byte should be allocated*/ static uint32_t zero_mem = ZERO_MEM_SENTINEL; /*Give the address of this variable if 0 byte should be allocated*/
@ -85,9 +85,9 @@ void lv_mem_init(void)
#if LV_MEM_ADR == 0 #if LV_MEM_ADR == 0
/*Allocate a large array to store the dynamically allocated data*/ /*Allocate a large array to store the dynamically allocated data*/
static LV_ATTRIBUTE_LARGE_RAM_ARRAY MEM_UNIT work_mem_int[LV_MEM_SIZE / sizeof(MEM_UNIT)]; static LV_ATTRIBUTE_LARGE_RAM_ARRAY MEM_UNIT work_mem_int[LV_MEM_SIZE / sizeof(MEM_UNIT)];
tlsf = tlsf_create_with_pool((void *)work_mem_int, LV_MEM_SIZE); tlsf = lv_tlsf_create_with_pool((void *)work_mem_int, LV_MEM_SIZE);
#else #else
tlsf = tlsf_create_with_pool((void *)LV_MEM_ADR, LV_MEM_SIZE); tlsf = lv_tlsf_create_with_pool((void *)LV_MEM_ADR, LV_MEM_SIZE);
#endif #endif
#endif #endif
@ -103,7 +103,7 @@ void lv_mem_init(void)
void lv_mem_deinit(void) void lv_mem_deinit(void)
{ {
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
tlsf_destroy(tlsf); lv_tlsf_destroy(tlsf);
lv_mem_init(); lv_mem_init();
#endif #endif
} }
@ -122,7 +122,7 @@ void * lv_mem_alloc(size_t size)
} }
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
void * alloc = tlsf_malloc(tlsf, size); void * alloc = lv_tlsf_malloc(tlsf, size);
#else #else
void * alloc = LV_MEM_CUSTOM_ALLOC(size); void * alloc = LV_MEM_CUSTOM_ALLOC(size);
#endif #endif
@ -156,9 +156,9 @@ void lv_mem_free(void * data)
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
# if LV_MEM_ADD_JUNK # if LV_MEM_ADD_JUNK
lv_memset(data, 0xbb, tlsf_block_size(data)); lv_memset(data, 0xbb, lv_tlsf_block_size(data));
# endif # endif
tlsf_free(tlsf, data); lv_tlsf_free(tlsf, data);
#else #else
LV_MEM_CUSTOM_FREE(data); LV_MEM_CUSTOM_FREE(data);
#endif #endif
@ -183,7 +183,7 @@ void * lv_mem_realloc(void * data_p, size_t new_size)
if(data_p == &zero_mem) return lv_mem_alloc(new_size); if(data_p == &zero_mem) return lv_mem_alloc(new_size);
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
void * new_p = tlsf_realloc(tlsf, data_p, new_size); void * new_p = lv_tlsf_realloc(tlsf, data_p, new_size);
#else #else
void * new_p = LV_MEM_CUSTOM_REALLOC(data_p, new_size); void * new_p = LV_MEM_CUSTOM_REALLOC(data_p, new_size);
#endif #endif
@ -204,12 +204,12 @@ lv_res_t lv_mem_test(void)
} }
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
if(tlsf_check(tlsf)) { if(lv_tlsf_check(tlsf)) {
LV_LOG_WARN("failed"); LV_LOG_WARN("failed");
return LV_RES_INV; return LV_RES_INV;
} }
if (tlsf_check_pool(tlsf_get_pool(tlsf))) { if (lv_tlsf_check_pool(lv_tlsf_get_pool(tlsf))) {
LV_LOG_WARN("pool failed"); LV_LOG_WARN("pool failed");
return LV_RES_INV; return LV_RES_INV;
} }
@ -230,7 +230,7 @@ void lv_mem_monitor(lv_mem_monitor_t * mon_p)
#if LV_MEM_CUSTOM == 0 #if LV_MEM_CUSTOM == 0
MEM_TRACE("begin"); MEM_TRACE("begin");
tlsf_walk_pool(tlsf_get_pool(tlsf), lv_mem_walker, mon_p); lv_tlsf_walk_pool(lv_tlsf_get_pool(tlsf), lv_mem_walker, mon_p);
mon_p->total_size = LV_MEM_SIZE; mon_p->total_size = LV_MEM_SIZE;
mon_p->used_pct = 100 - (100U * mon_p->free_size) / mon_p->total_size; mon_p->used_pct = 100 - (100U * mon_p->free_size) / mon_p->total_size;

View File

@ -861,7 +861,7 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user)
integ->status += status; integ->status += status;
} }
int tlsf_check(tlsf_t tlsf) int lv_tlsf_check(lv_tlsf_t tlsf)
{ {
int i, j; int i, j;
@ -921,9 +921,9 @@ static void default_walker(void* ptr, size_t size, int used, void* user)
printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, (void*)block_from_ptr(ptr)); printf("\t%p %s size: %x (%p)\n", ptr, used ? "used" : "free", (unsigned int)size, (void*)block_from_ptr(ptr));
} }
void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user) void lv_tlsf_walk_pool(lv_pool_t pool, lv_tlsf_walker walker, void* user)
{ {
tlsf_walker pool_walker = walker ? walker : default_walker; lv_tlsf_walker pool_walker = walker ? walker : default_walker;
block_header_t* block = block_header_t* block =
offset_to_block(pool, -(int)block_header_overhead); offset_to_block(pool, -(int)block_header_overhead);
@ -938,7 +938,7 @@ void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user)
} }
} }
size_t tlsf_block_size(void* ptr) size_t lv_tlsf_block_size(void* ptr)
{ {
size_t size = 0; size_t size = 0;
if (ptr) if (ptr)
@ -949,65 +949,65 @@ size_t tlsf_block_size(void* ptr)
return size; return size;
} }
int tlsf_check_pool(pool_t pool) int lv_tlsf_check_pool(lv_pool_t pool)
{ {
/* Check that the blocks are physically correct. */ /* Check that the blocks are physically correct. */
integrity_t integ = { 0, 0 }; integrity_t integ = { 0, 0 };
tlsf_walk_pool(pool, integrity_walker, &integ); lv_tlsf_walk_pool(pool, integrity_walker, &integ);
return integ.status; return integ.status;
} }
/* /*
** Size of the TLSF structures in a given memory block passed to ** Size of the TLSF structures in a given memory block passed to
** tlsf_create, equal to the size of a control_t ** lv_tlsf_create, equal to the size of a control_t
*/ */
size_t tlsf_size(void) size_t lv_tlsf_size(void)
{ {
return sizeof(control_t); return sizeof(control_t);
} }
size_t tlsf_align_size(void) size_t lv_tlsf_align_size(void)
{ {
return ALIGN_SIZE; return ALIGN_SIZE;
} }
size_t tlsf_block_size_min(void) size_t lv_tlsf_block_size_min(void)
{ {
return block_size_min; return block_size_min;
} }
size_t tlsf_block_size_max(void) size_t lv_tlsf_block_size_max(void)
{ {
return block_size_max; return block_size_max;
} }
/* /*
** Overhead of the TLSF structures in a given memory block passed to ** Overhead of the TLSF structures in a given memory block passed to
** tlsf_add_pool, equal to the overhead of a free block and the ** lv_tlsf_add_pool, equal to the overhead of a free block and the
** sentinel block. ** sentinel block.
*/ */
size_t tlsf_pool_overhead(void) size_t lv_tlsf_pool_overhead(void)
{ {
return 2 * block_header_overhead; return 2 * block_header_overhead;
} }
size_t tlsf_alloc_overhead(void) size_t lv_tlsf_alloc_overhead(void)
{ {
return block_header_overhead; return block_header_overhead;
} }
pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes) lv_pool_t lv_tlsf_add_pool(lv_tlsf_t tlsf, void* mem, size_t bytes)
{ {
block_header_t* block; block_header_t* block;
block_header_t* next; block_header_t* next;
const size_t pool_overhead = tlsf_pool_overhead(); const size_t pool_overhead = lv_tlsf_pool_overhead();
const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE); const size_t pool_bytes = align_down(bytes - pool_overhead, ALIGN_SIZE);
if (((ptrdiff_t)mem % ALIGN_SIZE) != 0) if (((ptrdiff_t)mem % ALIGN_SIZE) != 0)
{ {
printf("tlsf_add_pool: Memory must be aligned by %u bytes.\n", printf("lv_tlsf_add_pool: Memory must be aligned by %u bytes.\n",
(unsigned int)ALIGN_SIZE); (unsigned int)ALIGN_SIZE);
return 0; return 0;
} }
@ -1015,11 +1015,11 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
if (pool_bytes < block_size_min || pool_bytes > block_size_max) if (pool_bytes < block_size_min || pool_bytes > block_size_max)
{ {
#if defined (TLSF_64BIT) #if defined (TLSF_64BIT)
printf("tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n", printf("lv_tlsf_add_pool: Memory size must be between 0x%x and 0x%x00 bytes.\n",
(unsigned int)(pool_overhead + block_size_min), (unsigned int)(pool_overhead + block_size_min),
(unsigned int)((pool_overhead + block_size_max) / 256)); (unsigned int)((pool_overhead + block_size_max) / 256));
#else #else
printf("tlsf_add_pool: Memory size must be between %u and %u bytes.\n", printf("lv_tlsf_add_pool: Memory size must be between %u and %u bytes.\n",
(unsigned int)(pool_overhead + block_size_min), (unsigned int)(pool_overhead + block_size_min),
(unsigned int)(pool_overhead + block_size_max)); (unsigned int)(pool_overhead + block_size_max));
#endif #endif
@ -1046,7 +1046,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
return mem; return mem;
} }
void tlsf_remove_pool(tlsf_t tlsf, pool_t pool) void lv_tlsf_remove_pool(lv_tlsf_t tlsf, lv_pool_t pool)
{ {
control_t* control = tlsf_cast(control_t*, tlsf); control_t* control = tlsf_cast(control_t*, tlsf);
block_header_t* block = offset_to_block(pool, -(int)block_header_overhead); block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
@ -1093,7 +1093,7 @@ int test_ffs_fls()
} }
#endif #endif
tlsf_t tlsf_create(void* mem) lv_tlsf_t lv_tlsf_create(void* mem)
{ {
#if _DEBUG #if _DEBUG
if (test_ffs_fls()) if (test_ffs_fls())
@ -1104,35 +1104,35 @@ tlsf_t tlsf_create(void* mem)
if (((tlsfptr_t)mem % ALIGN_SIZE) != 0) if (((tlsfptr_t)mem % ALIGN_SIZE) != 0)
{ {
printf("tlsf_create: Memory must be aligned to %u bytes.\n", printf("lv_tlsf_create: Memory must be aligned to %u bytes.\n",
(unsigned int)ALIGN_SIZE); (unsigned int)ALIGN_SIZE);
return 0; return 0;
} }
control_constructor(tlsf_cast(control_t*, mem)); control_constructor(tlsf_cast(control_t*, mem));
return tlsf_cast(tlsf_t, mem); return tlsf_cast(lv_tlsf_t, mem);
} }
tlsf_t tlsf_create_with_pool(void* mem, size_t bytes) lv_tlsf_t lv_tlsf_create_with_pool(void* mem, size_t bytes)
{ {
tlsf_t tlsf = tlsf_create(mem); lv_tlsf_t tlsf = lv_tlsf_create(mem);
tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size()); lv_tlsf_add_pool(tlsf, (char*)mem + lv_tlsf_size(), bytes - lv_tlsf_size());
return tlsf; return tlsf;
} }
void tlsf_destroy(tlsf_t tlsf) void lv_tlsf_destroy(lv_tlsf_t tlsf)
{ {
/* Nothing to do. */ /* Nothing to do. */
(void)tlsf; (void)tlsf;
} }
pool_t tlsf_get_pool(tlsf_t tlsf) lv_pool_t lv_tlsf_get_pool(lv_tlsf_t tlsf)
{ {
return tlsf_cast(pool_t, (char*)tlsf + tlsf_size()); return tlsf_cast(lv_pool_t, (char*)tlsf + lv_tlsf_size());
} }
void* tlsf_malloc(tlsf_t tlsf, size_t size) void* lv_tlsf_malloc(lv_tlsf_t tlsf, size_t size)
{ {
control_t* control = tlsf_cast(control_t*, tlsf); control_t* control = tlsf_cast(control_t*, tlsf);
const size_t adjust = adjust_request_size(size, ALIGN_SIZE); const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
@ -1140,7 +1140,7 @@ void* tlsf_malloc(tlsf_t tlsf, size_t size)
return block_prepare_used(control, block, adjust); return block_prepare_used(control, block, adjust);
} }
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size) void* lv_tlsf_memalign(lv_tlsf_t tlsf, size_t align, size_t size)
{ {
control_t* control = tlsf_cast(control_t*, tlsf); control_t* control = tlsf_cast(control_t*, tlsf);
const size_t adjust = adjust_request_size(size, ALIGN_SIZE); const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
@ -1158,7 +1158,7 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
/* /*
** If alignment is less than or equals base alignment, we're done. ** If alignment is less than or equals base alignment, we're done.
** If we requested 0 bytes, return null, as tlsf_malloc(0) does. ** If we requested 0 bytes, return null, as lv_tlsf_malloc(0) does.
*/ */
const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust; const size_t aligned_size = (adjust && align > ALIGN_SIZE) ? size_with_gap : adjust;
@ -1197,7 +1197,7 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
return block_prepare_used(control, block, adjust); return block_prepare_used(control, block, adjust);
} }
void tlsf_free(tlsf_t tlsf, void* ptr) void lv_tlsf_free(lv_tlsf_t tlsf, void* ptr)
{ {
/* Don't attempt to free a NULL pointer. */ /* Don't attempt to free a NULL pointer. */
if (ptr) if (ptr)
@ -1225,7 +1225,7 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
** - an extended buffer size will leave the newly-allocated area with ** - an extended buffer size will leave the newly-allocated area with
** contents undefined ** contents undefined
*/ */
void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size) void* lv_tlsf_realloc(lv_tlsf_t tlsf, void* ptr, size_t size)
{ {
control_t* control = tlsf_cast(control_t*, tlsf); control_t* control = tlsf_cast(control_t*, tlsf);
void* p = 0; void* p = 0;
@ -1233,12 +1233,12 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
/* Zero-size requests are treated as free. */ /* Zero-size requests are treated as free. */
if (ptr && size == 0) if (ptr && size == 0)
{ {
tlsf_free(tlsf, ptr); lv_tlsf_free(tlsf, ptr);
} }
/* Requests with NULL pointers are treated as malloc. */ /* Requests with NULL pointers are treated as malloc. */
else if (!ptr) else if (!ptr)
{ {
p = tlsf_malloc(tlsf, size); p = lv_tlsf_malloc(tlsf, size);
} }
else else
{ {
@ -1257,12 +1257,12 @@ void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size)
*/ */
if (adjust > cursize && (!block_is_free(next) || adjust > combined)) if (adjust > cursize && (!block_is_free(next) || adjust > combined))
{ {
p = tlsf_malloc(tlsf, size); p = lv_tlsf_malloc(tlsf, size);
if (p) if (p)
{ {
const size_t minsize = tlsf_min(cursize, size); const size_t minsize = tlsf_min(cursize, size);
lv_memcpy(p, ptr, minsize); lv_memcpy(p, ptr, minsize);
tlsf_free(tlsf, ptr); lv_tlsf_free(tlsf, ptr);
} }
} }
else else

View File

@ -47,44 +47,44 @@
extern "C" { extern "C" {
#endif #endif
/* tlsf_t: a TLSF structure. Can contain 1 to N pools. */ /* lv_tlsf_t: a TLSF structure. Can contain 1 to N pools. */
/* pool_t: a block of memory that TLSF can manage. */ /* lv_pool_t: a block of memory that TLSF can manage. */
typedef void* tlsf_t; typedef void* lv_tlsf_t;
typedef void* pool_t; typedef void* lv_pool_t;
/* Create/destroy a memory pool. */ /* Create/destroy a memory pool. */
tlsf_t tlsf_create(void* mem); lv_tlsf_t lv_tlsf_create(void* mem);
tlsf_t tlsf_create_with_pool(void* mem, size_t bytes); lv_tlsf_t lv_tlsf_create_with_pool(void* mem, size_t bytes);
void tlsf_destroy(tlsf_t tlsf); void lv_tlsf_destroy(lv_tlsf_t tlsf);
pool_t tlsf_get_pool(tlsf_t tlsf); lv_pool_t lv_tlsf_get_pool(lv_tlsf_t tlsf);
/* Add/remove memory pools. */ /* Add/remove memory pools. */
pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes); lv_pool_t lv_tlsf_add_pool(lv_tlsf_t tlsf, void* mem, size_t bytes);
void tlsf_remove_pool(tlsf_t tlsf, pool_t pool); void lv_tlsf_remove_pool(lv_tlsf_t tlsf, lv_pool_t pool);
/* malloc/memalign/realloc/free replacements. */ /* malloc/memalign/realloc/free replacements. */
void* tlsf_malloc(tlsf_t tlsf, size_t bytes); void* lv_tlsf_malloc(lv_tlsf_t tlsf, size_t bytes);
void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t bytes); void* lv_tlsf_memalign(lv_tlsf_t tlsf, size_t align, size_t bytes);
void* tlsf_realloc(tlsf_t tlsf, void* ptr, size_t size); void* lv_tlsf_realloc(lv_tlsf_t tlsf, void* ptr, size_t size);
void tlsf_free(tlsf_t tlsf, void* ptr); void lv_tlsf_free(lv_tlsf_t tlsf, void* ptr);
/* Returns internal block size, not original request size */ /* Returns internal block size, not original request size */
size_t tlsf_block_size(void* ptr); size_t lv_tlsf_block_size(void* ptr);
/* Overheads/limits of internal structures. */ /* Overheads/limits of internal structures. */
size_t tlsf_size(void); size_t lv_tlsf_size(void);
size_t tlsf_align_size(void); size_t lv_tlsf_align_size(void);
size_t tlsf_block_size_min(void); size_t lv_tlsf_block_size_min(void);
size_t tlsf_block_size_max(void); size_t lv_tlsf_block_size_max(void);
size_t tlsf_pool_overhead(void); size_t lv_tlsf_pool_overhead(void);
size_t tlsf_alloc_overhead(void); size_t lv_tlsf_alloc_overhead(void);
/* Debugging. */ /* Debugging. */
typedef void (*tlsf_walker)(void* ptr, size_t size, int used, void* user); typedef void (*lv_tlsf_walker)(void* ptr, size_t size, int used, void* user);
void tlsf_walk_pool(pool_t pool, tlsf_walker walker, void* user); void lv_tlsf_walk_pool(lv_pool_t pool, lv_tlsf_walker walker, void* user);
/* Returns nonzero if any internal consistency check fails. */ /* Returns nonzero if any internal consistency check fails. */
int tlsf_check(tlsf_t tlsf); int lv_tlsf_check(lv_tlsf_t tlsf);
int tlsf_check_pool(pool_t pool); int lv_tlsf_check_pool(lv_pool_t pool);
#if defined(__cplusplus) #if defined(__cplusplus)
}; };