diff --git a/Kconfig b/Kconfig index 11e5a8f3ff..e07d680a78 100644 --- a/Kconfig +++ b/Kconfig @@ -58,13 +58,18 @@ menu "LVGL configuration" menu "Memory settings" config LV_MEM_CUSTOM - bool "If true use custom malloc/free, otherwise use the built-in `lv_mem_alloc()` and `lv_mem_free()`" + bool "If true use custom malloc/free, otherwise use the built-in `lv_malloc()` and `lv_free()`" config LV_MEM_SIZE_KILOBYTES - int "Size of the memory used by `lv_mem_alloc` in kilobytes (>= 2kB)" + int "Size of the memory used by `lv_malloc()` in kilobytes (>= 2kB)" default 32 depends on !LV_MEM_CUSTOM + config LV_MEM_POOL_EXPAND_SIZE_KILOBYTES + int "Size of the memory expand for `lv_malloc()` in kilobytes" + default 0 + depends on !LV_MEM_CUSTOM + config LV_MEM_ADDR hex "Address for the memory pool instead of allocating it as a normal array" default 0x0 diff --git a/lv_conf_template.h b/lv_conf_template.h index 5fbee760b5..bb59915052 100644 --- a/lv_conf_template.h +++ b/lv_conf_template.h @@ -38,6 +38,9 @@ /*Size of the memory available for `lv_malloc()` in bytes (>= 2kB)*/ #define LV_MEM_SIZE (128U * 1024U) /*[bytes]*/ + /*Size of the memory expand for `lv_malloc()` in bytes*/ + #define LV_MEM_POOL_EXPAND_SIZE 0 + /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/ #define LV_MEM_ADR 0 /*0: unused*/ /*Instead of an address give a memory allocator that will be called to get a memory pool for LVGL. E.g. my_malloc*/ diff --git a/src/lv_conf_internal.h b/src/lv_conf_internal.h index 2ae33613ed..3284e31d56 100644 --- a/src/lv_conf_internal.h +++ b/src/lv_conf_internal.h @@ -104,6 +104,15 @@ #endif #endif + /*Size of the memory expand for `lv_malloc()` in bytes*/ + #ifndef LV_MEM_POOL_EXPAND_SIZE + #ifdef CONFIG_LV_MEM_POOL_EXPAND_SIZE + #define LV_MEM_POOL_EXPAND_SIZE CONFIG_LV_MEM_POOL_EXPAND_SIZE + #else + #define LV_MEM_POOL_EXPAND_SIZE 0 + #endif + #endif + /*Set an address for the memory pool instead of allocating it as a normal array. Can be in external SRAM too.*/ #ifndef LV_MEM_ADR #ifdef CONFIG_LV_MEM_ADR diff --git a/src/lv_conf_kconfig.h b/src/lv_conf_kconfig.h index 56e0b7d78e..4067780c35 100644 --- a/src/lv_conf_kconfig.h +++ b/src/lv_conf_kconfig.h @@ -45,6 +45,10 @@ extern "C" { # define CONFIG_LV_MEM_SIZE (CONFIG_LV_MEM_SIZE_KILOBYTES * 1024U) #endif +#ifdef CONFIG_LV_MEM_POOL_EXPAND_SIZE_KILOBYTES +# define CONFIG_LV_MEM_POOL_EXPAND_SIZE (CONFIG_LV_MEM_POOL_EXPAND_SIZE_KILOBYTES * 1024U) +#endif + /*------------------ * MONITOR POSITION *-----------------*/ diff --git a/src/misc/lv_malloc_builtin.c b/src/misc/lv_malloc_builtin.c index 98a3f80d8e..093c4566c8 100644 --- a/src/misc/lv_malloc_builtin.c +++ b/src/misc/lv_malloc_builtin.c @@ -94,6 +94,16 @@ void lv_mem_deinit_builtin(void) lv_mem_init_builtin(); } +lv_mem_builtin_pool_t * lv_mem_builtin_add_pool(void * mem, size_t bytes) +{ + return lv_tlsf_add_pool(tlsf, mem, bytes); +} + +void lv_mem_builtin_remove_pool(lv_mem_builtin_pool_t * pool) +{ + lv_tlsf_remove_pool(tlsf, pool); +} + void lv_mem_monitor_builtin(lv_mem_monitor_t * mon_p) { /*Init the data*/ diff --git a/src/misc/lv_malloc_builtin.h b/src/misc/lv_malloc_builtin.h index 998fd31ec4..9ef4a0b9a4 100644 --- a/src/misc/lv_malloc_builtin.h +++ b/src/misc/lv_malloc_builtin.h @@ -24,6 +24,8 @@ extern "C" { * TYPEDEFS **********************/ +typedef void * lv_mem_builtin_pool_t; + /********************** * GLOBAL PROTOTYPES **********************/ @@ -38,6 +40,20 @@ void lv_mem_init_builtin(void); */ void lv_mem_deinit_builtin(void); +/** + * Add a new memory block to the builtin memory pool. + * @param mem pointer to a new block of memory + * @param bytes memory block size + * @return pointer to lv_mem_builtin_pool handle + */ +lv_mem_builtin_pool_t * lv_mem_builtin_add_pool(void * mem, size_t bytes); + +/** + * Remove the memory pool. + * @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_malloc_builtin(size_t size); void * lv_realloc_builtin(void * p, size_t new_size); void lv_free_builtin(void * p); diff --git a/src/misc/lv_tlsf.c b/src/misc/lv_tlsf.c index 255ff31329..781d0557ae 100644 --- a/src/misc/lv_tlsf.c +++ b/src/misc/lv_tlsf.c @@ -10,7 +10,7 @@ #undef printf #define printf LV_LOG_ERROR -#define TLSF_MAX_POOL_SIZE LV_MEM_SIZE +#define TLSF_MAX_POOL_SIZE (LV_MEM_SIZE + LV_MEM_POOL_EXPAND_SIZE) #if !defined(_DEBUG) #define _DEBUG 0