Skip to content

Commit

Permalink
fix(mem) add lv_ prefix to tlsf functions and types
Browse files Browse the repository at this point in the history
It avoids collision if tlsf is used by other libs in the project too
Fixes #2116 (comment)
kisvegabor committed Jun 10, 2021
1 parent ad05e19 commit 0d52b59
Showing 3 changed files with 74 additions and 74 deletions.
22 changes: 11 additions & 11 deletions src/misc/lv_mem.c
Original file line number Diff line number Diff line change
@@ -51,7 +51,7 @@
* STATIC VARIABLES
**********************/
#if LV_MEM_CUSTOM == 0
static tlsf_t tlsf;
static lv_tlsf_t tlsf;
#endif

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
/*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)];
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
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

@@ -103,7 +103,7 @@ void lv_mem_init(void)
void lv_mem_deinit(void)
{
#if LV_MEM_CUSTOM == 0
tlsf_destroy(tlsf);
lv_tlsf_destroy(tlsf);
lv_mem_init();
#endif
}
@@ -122,7 +122,7 @@ void * lv_mem_alloc(size_t size)
}

#if LV_MEM_CUSTOM == 0
void * alloc = tlsf_malloc(tlsf, size);
void * alloc = lv_tlsf_malloc(tlsf, size);
#else
void * alloc = LV_MEM_CUSTOM_ALLOC(size);
#endif
@@ -156,9 +156,9 @@ void lv_mem_free(void * data)

#if LV_MEM_CUSTOM == 0
# if LV_MEM_ADD_JUNK
lv_memset(data, 0xbb, tlsf_block_size(data));
lv_memset(data, 0xbb, lv_tlsf_block_size(data));
# endif
tlsf_free(tlsf, data);
lv_tlsf_free(tlsf, data);
#else
LV_MEM_CUSTOM_FREE(data);
#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 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
void * new_p = LV_MEM_CUSTOM_REALLOC(data_p, new_size);
#endif
@@ -204,12 +204,12 @@ lv_res_t lv_mem_test(void)
}

#if LV_MEM_CUSTOM == 0
if(tlsf_check(tlsf)) {
if(lv_tlsf_check(tlsf)) {
LV_LOG_WARN("failed");
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");
return LV_RES_INV;
}
@@ -230,7 +230,7 @@ void lv_mem_monitor(lv_mem_monitor_t * mon_p)
#if LV_MEM_CUSTOM == 0
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->used_pct = 100 - (100U * mon_p->free_size) / mon_p->total_size;
76 changes: 38 additions & 38 deletions src/misc/lv_tlsf.c
Original file line number Diff line number Diff line change
@@ -861,7 +861,7 @@ static void integrity_walker(void* ptr, size_t size, int used, void* user)
integ->status += status;
}

int tlsf_check(tlsf_t tlsf)
int lv_tlsf_check(lv_tlsf_t tlsf)
{
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));
}

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 =
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;
if (ptr)
@@ -949,77 +949,77 @@ size_t tlsf_block_size(void* ptr)
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. */
integrity_t integ = { 0, 0 };
tlsf_walk_pool(pool, integrity_walker, &integ);
lv_tlsf_walk_pool(pool, integrity_walker, &integ);

return integ.status;
}

/*
** 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);
}

size_t tlsf_align_size(void)
size_t lv_tlsf_align_size(void)
{
return ALIGN_SIZE;
}

size_t tlsf_block_size_min(void)
size_t lv_tlsf_block_size_min(void)
{
return block_size_min;
}

size_t tlsf_block_size_max(void)
size_t lv_tlsf_block_size_max(void)
{
return block_size_max;
}

/*
** 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.
*/
size_t tlsf_pool_overhead(void)
size_t lv_tlsf_pool_overhead(void)
{
return 2 * block_header_overhead;
}

size_t tlsf_alloc_overhead(void)
size_t lv_tlsf_alloc_overhead(void)
{
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* 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);

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);
return 0;
}

if (pool_bytes < block_size_min || pool_bytes > block_size_max)
{
#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_max) / 256));
#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_max));
#endif
@@ -1046,7 +1046,7 @@ pool_t tlsf_add_pool(tlsf_t tlsf, void* mem, size_t bytes)
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);
block_header_t* block = offset_to_block(pool, -(int)block_header_overhead);
@@ -1093,7 +1093,7 @@ int test_ffs_fls()
}
#endif

tlsf_t tlsf_create(void* mem)
lv_tlsf_t lv_tlsf_create(void* mem)
{
#if _DEBUG
if (test_ffs_fls())
@@ -1104,43 +1104,43 @@ tlsf_t tlsf_create(void* mem)

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);
return 0;
}

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);
tlsf_add_pool(tlsf, (char*)mem + tlsf_size(), bytes - tlsf_size());
lv_tlsf_t tlsf = lv_tlsf_create(mem);
lv_tlsf_add_pool(tlsf, (char*)mem + lv_tlsf_size(), bytes - lv_tlsf_size());
return tlsf;
}

void tlsf_destroy(tlsf_t tlsf)
void lv_tlsf_destroy(lv_tlsf_t tlsf)
{
/* Nothing to do. */
(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);
const size_t adjust = adjust_request_size(size, ALIGN_SIZE);
block_header_t* block = block_locate_free(control, 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);
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 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;

@@ -1197,7 +1197,7 @@ void* tlsf_memalign(tlsf_t tlsf, size_t align, size_t size)
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. */
if (ptr)
@@ -1225,20 +1225,20 @@ void tlsf_free(tlsf_t tlsf, void* ptr)
** - an extended buffer size will leave the newly-allocated area with
** 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);
void* p = 0;

/* Zero-size requests are treated as free. */
if (ptr && size == 0)
{
tlsf_free(tlsf, ptr);
lv_tlsf_free(tlsf, ptr);
}
/* Requests with NULL pointers are treated as malloc. */
else if (!ptr)
{
p = tlsf_malloc(tlsf, size);
p = lv_tlsf_malloc(tlsf, size);
}
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))
{
p = tlsf_malloc(tlsf, size);
p = lv_tlsf_malloc(tlsf, size);
if (p)
{
const size_t minsize = tlsf_min(cursize, size);
lv_memcpy(p, ptr, minsize);
tlsf_free(tlsf, ptr);
lv_tlsf_free(tlsf, ptr);
}
}
else
Loading

0 comments on commit 0d52b59

Please sign in to comment.