处理lvgl加载图片内存释放问题(py抛异常,导致一直加载图片)
authorp15692025051 <846603105@qq.com>
Fri, 12 May 2023 06:14:36 +0000 (14:14 +0800)
committerp15692025051 <846603105@qq.com>
Fri, 12 May 2023 06:14:36 +0000 (14:14 +0800)
2.sdk/Helios_SDK/services/microPython/lib/lvgl/src/extra/libs/png/lodepng.c
2.sdk/Helios_SDK/services/microPython/lib/lvgl/src/extra/libs/png/lv_png.c
2.sdk/Helios_SDK/services/microPython/lib/lvgl/src/extra/libs/sjpg/lv_sjpg.c
2.sdk/Helios_SDK/services/microPython/ports/quectel/core/source/modlvgl.c

index c7750fa..b8d3ff2 100644 (file)
@@ -93,15 +93,78 @@ static void lodepng_free(void* ptr) {
 }
 #else /*LODEPNG_COMPILE_ALLOCATORS*/
 /* TODO: support giving additional void* payload to the custom allocators */
+#include "lvgl/lvgl.h"
+
+/* 结构体用于保存每个已分配的内存块的信息 */
+typedef struct {
+    void * ptr;
+} malloc_info_t;
+
+static lv_ll_t malloc_ll;
+static char is_first = 1;
+
+static void my_mem_init(void) {
+    _lv_ll_init(&malloc_ll, sizeof(malloc_info_t));
+}
+
+static void * my_lv_mem_alloc(size_t size) {
+       if(is_first) {
+               is_first = 0;
+               my_mem_init();
+       }
+    void * ptr = malloc(size);
+    if (ptr) {
+        malloc_info_t * info = (malloc_info_t *)_lv_ll_ins_tail(&malloc_ll);
+        info->ptr = ptr;
+    }
+    return ptr;
+}
+
+static void * my_lv_mem_realloc(void * ptr, size_t new_size) {
+    void * new_ptr = realloc(ptr, new_size);
+    if (new_ptr && new_ptr != ptr) {
+        malloc_info_t * info;
+        _LV_LL_READ(&malloc_ll, info) {
+            if (info->ptr == ptr) {
+                info->ptr = new_ptr;
+                break;
+            }
+        }
+    }
+    return new_ptr;
+}
+
+static void my_lv_mem_free(void * ptr) {
+    malloc_info_t * info;
+    _LV_LL_READ(&malloc_ll, info) {
+        if (info->ptr == ptr) {
+            _lv_ll_remove(&malloc_ll, info);
+            return;
+        }
+    }
+    free(ptr);
+}
+
+void my_lodpng_lv_free_all(void) {
+    malloc_info_t * info;
+    _LV_LL_READ(&malloc_ll, info) {
+        free(info->ptr);
+    }
+    _lv_ll_clear(&malloc_ll);
+}
+
+
+
+
 void* lodepng_malloc(size_t size) {
-       return malloc(size);
+       return my_lv_mem_alloc(size);
 }
 void* lodepng_realloc(void* ptr, size_t new_size) {
-       return realloc(ptr, new_size);
+       return my_lv_mem_realloc(ptr, new_size);
 }
 void lodepng_free(void* ptr) {
        if(ptr)
-               free(ptr);
+               my_lv_mem_free(ptr);
 }
 #endif /*LODEPNG_COMPILE_ALLOCATORS*/
 
index 403e381..581ab45 100644 (file)
 #include "lv_png.h"
 #include "lodepng.h"
 #include <stdlib.h>
+#include "lv_ll.h"
+
+typedef struct {
+    void * ptr;
+} malloc_info_t;
+
+static lv_ll_t malloc_ll;
+static char is_first = 1;
+
+static void my_mem_init(void) {
+    _lv_ll_init(&malloc_ll, sizeof(malloc_info_t));
+}
+
+static void * my_lv_mem_alloc(size_t size) {
+       if(is_first) {
+               is_first = 0;
+               my_mem_init();
+       }
+    void * ptr = malloc(size);
+    if (ptr) {
+        malloc_info_t * info = (malloc_info_t *)_lv_ll_ins_tail(&malloc_ll);
+        info->ptr = ptr;
+    }
+    return ptr;
+}
+
+static void * my_lv_mem_realloc(void * ptr, size_t new_size) {
+    void * new_ptr = realloc(ptr, new_size);
+    if (new_ptr && new_ptr != ptr) {
+        malloc_info_t * info;
+        _LV_LL_READ(&malloc_ll, info) {
+            if (info->ptr == ptr) {
+                info->ptr = new_ptr;
+                break;
+            }
+        }
+    }
+    return new_ptr;
+}
+
+static void my_lv_mem_free(void * ptr) {
+    malloc_info_t * info;
+    _LV_LL_READ(&malloc_ll, info) {
+        if (info->ptr == ptr) {
+            _lv_ll_remove(&malloc_ll, info);
+            return;
+        }
+    }
+    free(ptr);
+}
+
+void my_png_lv_free_all(void) {
+    malloc_info_t * info;
+    _LV_LL_READ(&malloc_ll, info) {
+        free(info->ptr);
+    }
+    _lv_ll_clear(&malloc_ll);
+}
+
+
+
 
 #undef lv_mem_alloc
-#define lv_mem_alloc malloc
+#define lv_mem_alloc my_lv_mem_alloc
 
 #undef lv_mem_realloc
-#define lv_mem_realloc realloc
+#define lv_mem_realloc my_lv_mem_realloc
 
 
 
@@ -25,7 +86,7 @@
 static void png_free(void *ptr)
 {
        if(ptr) {
-               free(ptr);
+               my_lv_mem_free(ptr);
                ptr = NULL;
        }
 }
index 7b1a811..7ece458 100644 (file)
  *      TYPEDEFS
  **********************/
 #include <stdlib.h>
+
+
+typedef struct {
+    void * ptr;
+} malloc_info_t;
+
+static lv_ll_t malloc_ll;
+static char is_first = 1;
+
+static void my_mem_init(void) {
+    _lv_ll_init(&malloc_ll, sizeof(malloc_info_t));
+}
+
+static void * my_lv_mem_alloc(size_t size) {
+       if(is_first) {
+               is_first = 0;
+               my_mem_init();
+       }
+    void * ptr = malloc(size);
+    if (ptr) {
+        malloc_info_t * info = (malloc_info_t *)_lv_ll_ins_tail(&malloc_ll);
+        info->ptr = ptr;
+    }
+    return ptr;
+}
+
+static void * my_lv_mem_realloc(void * ptr, size_t new_size) {
+    void * new_ptr = realloc(ptr, new_size);
+    if (new_ptr && new_ptr != ptr) {
+        malloc_info_t * info;
+        _LV_LL_READ(&malloc_ll, info) {
+            if (info->ptr == ptr) {
+                info->ptr = new_ptr;
+                break;
+            }
+        }
+    }
+    return new_ptr;
+}
+
+static void my_lv_mem_free(void * ptr) {
+    malloc_info_t * info;
+    _LV_LL_READ(&malloc_ll, info) {
+        if (info->ptr == ptr) {
+            _lv_ll_remove(&malloc_ll, info);
+            return;
+        }
+    }
+    free(ptr);
+}
+
+void my_sjpg_lv_free_all(void) {
+    malloc_info_t * info;
+    _LV_LL_READ(&malloc_ll, info) {
+        free(info->ptr);
+    }
+    _lv_ll_clear(&malloc_ll);
+}
+
+
+
 #undef lv_mem_alloc
-#define lv_mem_alloc malloc
+#define lv_mem_alloc my_lv_mem_alloc
 
 #undef lv_mem_realloc
-#define lv_mem_realloc realloc
+#define lv_mem_realloc my_lv_mem_realloc
 
 static void png_free(void *ptr)
 {
        if(ptr) {
-               free(ptr);
+               my_lv_mem_free(ptr);
                ptr = NULL;
        }
 }
index f34a32a..f4d112f 100644 (file)
@@ -26674,7 +26674,7 @@ typedef struct{
     // lv_fs_file_t font_f;
     // lv_fs_res_t font_res;
     // uint32_t font_resCount;
-    uint8_t __g_font_buf[1024];//ÈçbinÎļþ´æÔÚSPI FLASH¿ÉʹÓôËbuff
+    uint8_t __g_font_buf[1024];//如bin文件存在SPI FLASH可使用此buff
 }user_font_t;
 
 //forrest.liu@20230217 add for test
@@ -26829,7 +26829,7 @@ static uint8_t bin_find_index(char *bin_name)
 }
 
 static uint8_t *__user_font_getdata(user_font_t *font_user, int offset, int size){
-    //Èç×ÖÄ£±£´æÔÚSPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size);
+    //如字模保存在SPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size);
     /*if(0 == font_mutex1) {
         font_mutex1 = Helios_Mutex_Create();
     }
@@ -27057,7 +27057,7 @@ typedef struct{
     lv_fs_file_t font_f;
     lv_fs_res_t font_res;
     uint32_t font_resCount;
-    uint8_t __g_font_buf[1024];//ÈçbinÎļþ´æÔÚSPI FLASH¿ÉʹÓôËbuff
+    uint8_t __g_font_buf[1024];//如bin文件存在SPI FLASH可使用此buff
 }user_font_t;
 
 
@@ -27082,7 +27082,7 @@ static void get_bin_header(char *bin_name,x_header_t *header)
 // static uint8_t fontisopen = 0;
 
 static uint8_t *__user_font_getdata(user_font_t *font_user, int offset, int size){
-    //Èç×ÖÄ£±£´æÔÚSPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size);
+    //如字模保存在SPI FLASH, SPIFLASH_Read(__g_font_buf,offset,size);
     if(0 == font_mutex1) {
         font_mutex1 = Helios_Mutex_Create();
     }
@@ -29822,8 +29822,10 @@ STATIC MP_DEFINE_CONST_LV_FUN_OBJ_STATIC_VAR(mp_lv_dpx_obj, 1, mp_lv_pct, lv_dpx
  */
 // args contains, eg: arg0  arg1  key0  value0  key1  value1
 
-
- static void helios_lv_handler_task()
+extern void my_sjpg_lv_free_all(void);
+extern void my_png_lv_free_all(void);
+extern void my_lodpng_lv_free_all(void);
+ static void helios_lv_handler_task(void)
  {
     
     mp_state_thread_t ts;
@@ -29880,6 +29882,9 @@ STATIC MP_DEFINE_CONST_LV_FUN_OBJ_STATIC_VAR(mp_lv_dpx_obj, 1, mp_lv_pct, lv_dpx
             }
             nlr_pop();
         } else {
+            my_sjpg_lv_free_all();
+            my_png_lv_free_all();
+            my_lodpng_lv_free_all();
             mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
         }
         MP_THREAD_GIL_EXIT();