Browse Source

update: 虚拟机的内存初始化移到malloc_idf5.c文件

Wendal Chen 3 years ago
parent
commit
0cec08afa6

+ 2 - 0
luatos/build_c3.bat

@@ -0,0 +1,2 @@
+idf.py build
+python makesoc.py

+ 47 - 24
luatos/components/luat/port/luat_malloc_idf5.c

@@ -6,6 +6,8 @@
 #include <string.h>//add for memset
 #include <string.h>//add for memset
 #include "bget.h"
 #include "bget.h"
 #include "luat_malloc.h"
 #include "luat_malloc.h"
+#include "esp_system.h"
+#include "esp_attr.h"
 
 
 #define LUAT_LOG_TAG "vmheap"
 #define LUAT_LOG_TAG "vmheap"
 #include "luat_log.h"
 #include "luat_log.h"
@@ -26,37 +28,26 @@ void* luat_heap_realloc(void* ptr, size_t len) {
 }
 }
 
 
 void* luat_heap_calloc(size_t count, size_t _size) {
 void* luat_heap_calloc(size_t count, size_t _size) {
-    void *ptr = luat_heap_malloc(count * _size);
-    if (ptr) {
-        memset(ptr, 0, _size);
-    }
-    return ptr;
+    return calloc(count, _size);
 }
 }
 //------------------------------------------------
 //------------------------------------------------
 
 
 //------------------------------------------------
 //------------------------------------------------
 // ---------- 管理 LuaVM所使用的内存----------------
 // ---------- 管理 LuaVM所使用的内存----------------
-void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
-    if (0) {
-        if (ptr) {
-            if (nsize) {
-                // 缩放内存块
-                LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
-            }
-            else {
-                // 释放内存块
-                LLOGD("free %p ", ptr);
-                brel(ptr);
-                return NULL;
-            }
-        }
-        else {
-            // 申请内存块
-            ptr = bget(nsize);
-            LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
-            return ptr;
+
+#if 1
+void* IRAM_ATTR luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
+    if (ptr == NULL && nsize == 0)
+        return NULL;
+#if LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
+    if (ptr != NULL && nsize == 0) {
+        uint32_t addr = (uint32_t) ptr;
+        if (addr >= 0x3C000000 && addr <= 0x3CFFFFFF) {
+            //LLOGD("skip ROM free %p", ptr);
+            return NULL;
         }
         }
     }
     }
+#endif
 
 
     if (nsize)
     if (nsize)
     {
     {
@@ -80,6 +71,31 @@ void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
     *total = curalloc + totfree;
     *total = curalloc + totfree;
 }
 }
 
 
+#else
+#include "heap_tlsf.h"
+static tlsf_t vm_tlfs;
+
+void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
+    if (ptr == NULL && nsize == 0)
+        return NULL;
+#if LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
+    if (ptr != NULL && nsize == 0) {
+        uint32_t addr = (uint32_t) ptr;
+        if (addr >= 0x3C000000 && addr <= 0x3CFFFFFF) {
+            LLOGD("skip ROM free %p", ptr);
+            return NULL;
+        }
+    }
+#endif
+    return tlsf_realloc(ptr, nsize);
+}
+void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
+    *total = 0;
+    *used = 0;
+    *max_used = 0;
+}
+#endif
+
 #include "esp_system.h"
 #include "esp_system.h"
 #include "esp_heap_caps.h"
 #include "esp_heap_caps.h"
 
 
@@ -90,3 +106,10 @@ void luat_meminfo_sys(size_t *total, size_t *used, size_t *max_used) {
 }
 }
 
 
 //-----------------------------------------------------------------------------
 //-----------------------------------------------------------------------------
+
+
+#define LUAT_HEAP_SIZE (96*1024)
+static uint8_t vmheap[LUAT_HEAP_SIZE];
+void luat_heap_init(void) {
+    bpool(vmheap, LUAT_HEAP_SIZE);
+}

+ 2 - 2
luatos/include/luat_conf_bsp.h

@@ -9,7 +9,7 @@
 #define LUAT_COMPILER_NOWEAK
 #define LUAT_COMPILER_NOWEAK
 
 
 // 内存优化: 减少内存消耗, 会稍微减低性能
 // 内存优化: 减少内存消耗, 会稍微减低性能
-// #define LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP 1
+#define LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP 1
 
 
 //----------------------------------
 //----------------------------------
 // 使用VFS(虚拟文件系统)和内置库文件, 必须启用
 // 使用VFS(虚拟文件系统)和内置库文件, 必须启用
@@ -46,7 +46,7 @@
 #define LUAT_USE_CJSON  1
 #define LUAT_USE_CJSON  1
 #define LUAT_USE_ZBUFF  1
 #define LUAT_USE_ZBUFF  1
 #define LUAT_USE_PACK  1
 #define LUAT_USE_PACK  1
-// #define LUAT_USE_LIBGNSS  1
+#define LUAT_USE_LIBGNSS  1
 #define LUAT_USE_FS  1
 #define LUAT_USE_FS  1
 // #define LUAT_USE_SENSOR  1
 // #define LUAT_USE_SENSOR  1
 // #define LUAT_USE_SFUD  1
 // #define LUAT_USE_SFUD  1

+ 3 - 11
luatos/main/idf5_main.c

@@ -9,27 +9,19 @@
 
 
 #include <string.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdlib.h>
-#include "freertos/FreeRTOS.h"
-#include "freertos/task.h"
-#include "freertos/event_groups.h"
-#include "esp_wifi.h"
-#include "esp_wpa2.h"
 #include "esp_event.h"
 #include "esp_event.h"
-#include "esp_log.h"
 #include "esp_system.h"
 #include "esp_system.h"
 #include "nvs_flash.h"
 #include "nvs_flash.h"
-#include "bget.h"
 
 
-#define LUAT_HEAP_SIZE (64*1024)
-static uint8_t vmheap[LUAT_HEAP_SIZE];
 
 
 extern int luat_main (void);
 extern int luat_main (void);
 extern void bootloader_random_enable(void);
 extern void bootloader_random_enable(void);
+extern void luat_heap_init(void);
 
 
 void app_main(void)
 void app_main(void)
 {
 {
     bootloader_random_enable();
     bootloader_random_enable();
-    ESP_ERROR_CHECK( nvs_flash_init() );
-    bpool(vmheap, LUAT_HEAP_SIZE);
+    nvs_flash_init();
+    luat_heap_init();
     luat_main();
     luat_main();
 }
 }