mirror of
https://github.com/lvgl/lvgl.git
synced 2026-05-27 20:57:01 +08:00
feat(malloc_builtin): add extended memory pool monitoring (#3882)
Signed-off-by: pengyiqiang <pengyiqiang@xiaomi.com> Co-authored-by: pengyiqiang <pengyiqiang@xiaomi.com>
This commit is contained in:
@@ -11,6 +11,7 @@
|
|||||||
#include "lv_tlsf.h"
|
#include "lv_tlsf.h"
|
||||||
#include "lv_assert.h"
|
#include "lv_assert.h"
|
||||||
#include "lv_log.h"
|
#include "lv_log.h"
|
||||||
|
#include "lv_ll.h"
|
||||||
#include "lv_math.h"
|
#include "lv_math.h"
|
||||||
|
|
||||||
#ifdef LV_MEM_POOL_INCLUDE
|
#ifdef LV_MEM_POOL_INCLUDE
|
||||||
@@ -48,6 +49,7 @@ static void lv_mem_walker(void * ptr, size_t size, int used, void * user);
|
|||||||
static lv_tlsf_t tlsf;
|
static lv_tlsf_t tlsf;
|
||||||
static uint32_t cur_used;
|
static uint32_t cur_used;
|
||||||
static uint32_t max_used;
|
static uint32_t max_used;
|
||||||
|
static lv_ll_t pool_ll;
|
||||||
|
|
||||||
/**********************
|
/**********************
|
||||||
* MACROS
|
* MACROS
|
||||||
@@ -82,6 +84,12 @@ void lv_mem_init_builtin(void)
|
|||||||
#else
|
#else
|
||||||
tlsf = lv_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
|
||||||
|
_lv_ll_init(&pool_ll, sizeof(lv_pool_t));
|
||||||
|
|
||||||
|
/*Record the first pool*/
|
||||||
|
lv_pool_t * pool_p = _lv_ll_ins_tail(&pool_ll);
|
||||||
|
LV_ASSERT_MALLOC(pool_p);
|
||||||
|
*pool_p = lv_tlsf_get_pool(tlsf);
|
||||||
|
|
||||||
#if LV_MEM_ADD_JUNK
|
#if LV_MEM_ADD_JUNK
|
||||||
LV_LOG_WARN("LV_MEM_ADD_JUNK is enabled which makes LVGL much slower");
|
LV_LOG_WARN("LV_MEM_ADD_JUNK is enabled which makes LVGL much slower");
|
||||||
@@ -90,18 +98,38 @@ void lv_mem_init_builtin(void)
|
|||||||
|
|
||||||
void lv_mem_deinit_builtin(void)
|
void lv_mem_deinit_builtin(void)
|
||||||
{
|
{
|
||||||
|
_lv_ll_clear(&pool_ll);
|
||||||
lv_tlsf_destroy(tlsf);
|
lv_tlsf_destroy(tlsf);
|
||||||
lv_mem_init_builtin();
|
lv_mem_init_builtin();
|
||||||
}
|
}
|
||||||
|
|
||||||
lv_mem_builtin_pool_t * lv_mem_builtin_add_pool(void * mem, size_t bytes)
|
lv_mem_builtin_pool_t lv_mem_builtin_add_pool(void * mem, size_t bytes)
|
||||||
{
|
{
|
||||||
return lv_tlsf_add_pool(tlsf, mem, bytes);
|
lv_mem_builtin_pool_t new_pool = lv_tlsf_add_pool(tlsf, mem, bytes);
|
||||||
|
if(!new_pool) {
|
||||||
|
LV_LOG_WARN("failed to add memory pool, address: %p, size: %zu", mem, bytes);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
lv_pool_t * pool_p = _lv_ll_ins_tail(&pool_ll);
|
||||||
|
LV_ASSERT_MALLOC(pool_p);
|
||||||
|
*pool_p = new_pool;
|
||||||
|
|
||||||
|
return new_pool;
|
||||||
}
|
}
|
||||||
|
|
||||||
void lv_mem_builtin_remove_pool(lv_mem_builtin_pool_t * pool)
|
void lv_mem_builtin_remove_pool(lv_mem_builtin_pool_t pool)
|
||||||
{
|
{
|
||||||
lv_tlsf_remove_pool(tlsf, pool);
|
lv_pool_t * pool_p;
|
||||||
|
_LV_LL_READ(&pool_ll, pool_p) {
|
||||||
|
if(*pool_p == pool) {
|
||||||
|
_lv_ll_remove(&pool_ll, pool_p);
|
||||||
|
lv_free(pool_p);
|
||||||
|
lv_tlsf_remove_pool(tlsf, pool);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LV_LOG_WARN("invalid pool: %p", pool);
|
||||||
}
|
}
|
||||||
|
|
||||||
void lv_mem_monitor_builtin(lv_mem_monitor_t * mon_p)
|
void lv_mem_monitor_builtin(lv_mem_monitor_t * mon_p)
|
||||||
@@ -110,9 +138,11 @@ void lv_mem_monitor_builtin(lv_mem_monitor_t * mon_p)
|
|||||||
lv_memset(mon_p, 0, sizeof(lv_mem_monitor_t));
|
lv_memset(mon_p, 0, sizeof(lv_mem_monitor_t));
|
||||||
MEM_TRACE("begin");
|
MEM_TRACE("begin");
|
||||||
|
|
||||||
lv_tlsf_walk_pool(lv_tlsf_get_pool(tlsf), lv_mem_walker, mon_p);
|
lv_pool_t * pool_p;
|
||||||
|
_LV_LL_READ(&pool_ll, pool_p) {
|
||||||
|
lv_tlsf_walk_pool(*pool_p, 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;
|
mon_p->used_pct = 100 - (100U * mon_p->free_size) / mon_p->total_size;
|
||||||
if(mon_p->free_size > 0) {
|
if(mon_p->free_size > 0) {
|
||||||
mon_p->frag_pct = mon_p->free_biggest_size * 100U / mon_p->free_size;
|
mon_p->frag_pct = mon_p->free_biggest_size * 100U / mon_p->free_size;
|
||||||
@@ -156,9 +186,12 @@ lv_res_t lv_mem_test_builtin(void)
|
|||||||
return LV_RES_INV;
|
return LV_RES_INV;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(lv_tlsf_check_pool(lv_tlsf_get_pool(tlsf))) {
|
lv_pool_t * pool_p;
|
||||||
LV_LOG_WARN("pool failed");
|
_LV_LL_READ(&pool_ll, pool_p) {
|
||||||
return LV_RES_INV;
|
if(lv_tlsf_check_pool(*pool_p)) {
|
||||||
|
LV_LOG_WARN("pool failed");
|
||||||
|
return LV_RES_INV;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
MEM_TRACE("passed");
|
MEM_TRACE("passed");
|
||||||
@@ -174,6 +207,7 @@ static void lv_mem_walker(void * ptr, size_t size, int used, void * user)
|
|||||||
LV_UNUSED(ptr);
|
LV_UNUSED(ptr);
|
||||||
|
|
||||||
lv_mem_monitor_t * mon_p = user;
|
lv_mem_monitor_t * mon_p = user;
|
||||||
|
mon_p->total_size += size;
|
||||||
if(used) {
|
if(used) {
|
||||||
mon_p->used_cnt++;
|
mon_p->used_cnt++;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -46,13 +46,13 @@ void lv_mem_deinit_builtin(void);
|
|||||||
* @param bytes memory block size
|
* @param bytes memory block size
|
||||||
* @return pointer to lv_mem_builtin_pool handle
|
* @return pointer to lv_mem_builtin_pool handle
|
||||||
*/
|
*/
|
||||||
lv_mem_builtin_pool_t * lv_mem_builtin_add_pool(void * mem, size_t bytes);
|
lv_mem_builtin_pool_t lv_mem_builtin_add_pool(void * mem, size_t bytes);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Remove the memory pool.
|
* Remove the memory pool.
|
||||||
* @param lv_mem_builtin_pool_t pointer to lv_mem_builtin_pool handle
|
* @param lv_mem_builtin_pool_t pointer to lv_mem_builtin_pool handle
|
||||||
*/
|
*/
|
||||||
void lv_mem_builtin_remove_pool(lv_mem_builtin_pool_t * pool);
|
void lv_mem_builtin_remove_pool(lv_mem_builtin_pool_t pool);
|
||||||
|
|
||||||
void * lv_malloc_builtin(size_t size);
|
void * lv_malloc_builtin(size_t size);
|
||||||
void * lv_realloc_builtin(void * p, size_t new_size);
|
void * lv_realloc_builtin(void * p, size_t new_size);
|
||||||
|
|||||||
Reference in New Issue
Block a user