Skip to content

Commit

Permalink
Add lv_deinit function (lvgl#1319)
Browse files Browse the repository at this point in the history
  • Loading branch information
amirgon authored and embeddedt committed Dec 21, 2019
1 parent ef071d0 commit 248868f
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 15 deletions.
12 changes: 12 additions & 0 deletions src/lv_core/lv_obj.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
*-------------------*/
Expand Down
9 changes: 9 additions & 0 deletions src/lv_core/lv_obj.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
*-------------------*/
Expand Down
11 changes: 11 additions & 0 deletions src/lv_misc/lv_gc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
**********************/
33 changes: 18 additions & 15 deletions src/lv_misc/lv_gc.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,30 @@ 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
#error "GC requires CUSTOM_MEM"
#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 */

/**********************
Expand All @@ -63,6 +64,8 @@ LV_GC_ROOTS(extern)
* GLOBAL PROTOTYPES
**********************/

void lv_gc_clear_roots(void);

/**********************
* MACROS
**********************/
Expand Down
15 changes: 15 additions & 0 deletions src/lv_misc/lv_mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions src/lv_misc/lv_mem.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 248868f

Please sign in to comment.