From 248868fef1f4925fada1aa7591b19e3bc837db51 Mon Sep 17 00:00:00 2001 From: Amir Gonnen Date: Sun, 22 Dec 2019 01:35:00 +0200 Subject: [PATCH] Add lv_deinit function (#1319) --- src/lv_core/lv_obj.c | 12 ++++++++++++ src/lv_core/lv_obj.h | 9 +++++++++ src/lv_misc/lv_gc.c | 11 +++++++++++ src/lv_misc/lv_gc.h | 33 ++++++++++++++++++--------------- src/lv_misc/lv_mem.c | 15 +++++++++++++++ src/lv_misc/lv_mem.h | 6 ++++++ 6 files changed, 71 insertions(+), 15 deletions(-) diff --git a/src/lv_core/lv_obj.c b/src/lv_core/lv_obj.c index 089c5e3417c2..fd0c91892f5b 100644 --- a/src/lv_core/lv_obj.c +++ b/src/lv_core/lv_obj.c @@ -121,6 +121,18 @@ void lv_init(void) LV_LOG_INFO("lv_init ready"); } +#if LV_ENABLE_GC || !LV_MEM_CUSTOM +void lv_deinit(void) +{ + lv_gc_clear_roots(); + lv_log_register_print_cb(NULL); + lv_disp_set_default(NULL); + lv_mem_deinit(); + lv_initialized = false; + LV_LOG_INFO("lv_deinit done"); +} +#endif + /*-------------------- * Create and delete *-------------------*/ diff --git a/src/lv_core/lv_obj.h b/src/lv_core/lv_obj.h index 86987189071b..3bc8270e458c 100644 --- a/src/lv_core/lv_obj.h +++ b/src/lv_core/lv_obj.h @@ -269,6 +269,15 @@ typedef struct */ void lv_init(void); + +/** + * Deinit the 'lv' library + * Currently only implemented when not using custorm allocators, or GC is enabled. + */ +#if LV_ENABLE_GC || !LV_MEM_CUSTOM +void lv_deinit(void); +#endif + /*-------------------- * Create and delete *-------------------*/ diff --git a/src/lv_misc/lv_gc.c b/src/lv_misc/lv_gc.c index 70dfc9a84cce..94bf532ac9fb 100644 --- a/src/lv_misc/lv_gc.c +++ b/src/lv_misc/lv_gc.c @@ -8,6 +8,11 @@ *********************/ #include "lv_gc.h" +#include "string.h" + +#if defined(LV_GC_INCLUDE) +#include LV_GC_INCLUDE +#endif /* LV_ENABLE_GC */ /********************* * DEFINES @@ -35,6 +40,12 @@ LV_ROOTS * GLOBAL FUNCTIONS **********************/ +void lv_gc_clear_roots(void) +{ +#define LV_CLEAR_ROOT(root_type, root_name) memset(&LV_GC_ROOT(root_name), 0, sizeof(LV_GC_ROOT(root_name))); + LV_ITERATE_ROOTS(LV_CLEAR_ROOT) +} + /********************** * STATIC FUNCTIONS **********************/ diff --git a/src/lv_misc/lv_gc.h b/src/lv_misc/lv_gc.h index 0db9f5cb94d3..afd4d600aab6 100644 --- a/src/lv_misc/lv_gc.h +++ b/src/lv_misc/lv_gc.h @@ -30,21 +30,21 @@ extern "C" { * DEFINES *********************/ -#define LV_GC_ROOTS(prefix) \ - prefix lv_ll_t _lv_task_ll; /*Linked list to store the lv_tasks*/ \ - prefix lv_ll_t _lv_disp_ll; /*Linked list of screens*/ \ - prefix lv_ll_t _lv_indev_ll; /*Linked list of screens*/ \ - prefix lv_ll_t _lv_drv_ll; \ - prefix lv_ll_t _lv_file_ll; \ - prefix lv_ll_t _lv_anim_ll; \ - prefix lv_ll_t _lv_group_ll; \ - prefix lv_ll_t _lv_img_defoder_ll; \ - prefix lv_img_cache_entry_t * _lv_img_cache_array; \ - prefix void * _lv_task_act; \ - prefix void * _lv_draw_buf; +#define LV_ITERATE_ROOTS(f) \ + f(lv_ll_t, _lv_task_ll) /*Linked list to store the lv_tasks*/ \ + f(lv_ll_t, _lv_disp_ll) /*Linked list of screens*/ \ + f(lv_ll_t, _lv_indev_ll) /*Linked list of screens*/ \ + f(lv_ll_t, _lv_drv_ll) \ + f(lv_ll_t, _lv_file_ll) \ + f(lv_ll_t, _lv_anim_ll) \ + f(lv_ll_t, _lv_group_ll) \ + f(lv_ll_t, _lv_img_defoder_ll) \ + f(lv_img_cache_entry_t*, _lv_img_cache_array) \ + f(void*, _lv_task_act) \ + f(void*, _lv_draw_buf) -#define LV_NO_PREFIX -#define LV_ROOTS LV_GC_ROOTS(LV_NO_PREFIX) +#define LV_DEFINE_ROOT(root_type, root_name) root_type root_name; +#define LV_ROOTS LV_ITERATE_ROOTS(LV_DEFINE_ROOT) #if LV_ENABLE_GC == 1 #if LV_MEM_CUSTOM != 1 @@ -52,7 +52,8 @@ extern "C" { #endif /* LV_MEM_CUSTOM */ #else /* LV_ENABLE_GC */ #define LV_GC_ROOT(x) x -LV_GC_ROOTS(extern) +#define LV_EXTERN_ROOT(root_type, root_name) extern root_type root_name; +LV_ITERATE_ROOTS(LV_EXTERN_ROOT) #endif /* LV_ENABLE_GC */ /********************** @@ -63,6 +64,8 @@ LV_GC_ROOTS(extern) * GLOBAL PROTOTYPES **********************/ +void lv_gc_clear_roots(void); + /********************** * MACROS **********************/ diff --git a/src/lv_misc/lv_mem.c b/src/lv_misc/lv_mem.c index da38554e482c..9b7345ab383c 100644 --- a/src/lv_misc/lv_mem.c +++ b/src/lv_misc/lv_mem.c @@ -102,6 +102,21 @@ void lv_mem_init(void) #endif } +/** + * Clean up the memory buffer which frees all the allocated memories. + * @note It work only if `LV_MEM_CUSTOM == 0` + */ +void lv_mem_deinit(void) +{ +#if LV_MEM_CUSTOM == 0 + memset(work_mem, 0x00, (LV_MEM_SIZE / sizeof(MEM_UNIT)) * sizeof(MEM_UNIT)); + lv_mem_ent_t * full = (lv_mem_ent_t *)work_mem; + full->header.s.used = 0; + /*The total mem size id reduced by the first header and the close patterns */ + full->header.s.d_size = LV_MEM_SIZE - sizeof(lv_mem_header_t); +#endif +} + /** * Allocate a memory dynamically * @param size size of the memory to allocate in bytes diff --git a/src/lv_misc/lv_mem.h b/src/lv_misc/lv_mem.h index 34ca3e9e9ed0..f72407421bd9 100644 --- a/src/lv_misc/lv_mem.h +++ b/src/lv_misc/lv_mem.h @@ -55,6 +55,12 @@ typedef struct */ void lv_mem_init(void); +/** + * Clean up the memory buffer which frees all the allocated memories. + * @note It work only if `LV_MEM_CUSTOM == 0` + */ +void lv_mem_deinit(void); + /** * Allocate a memory dynamically * @param size size of the memory to allocate in bytes