Browse Source

add: 支持使用tlsf作为luavm的内存池实现

Wendal Chen 3 years ago
parent
commit
fb1ce6f7c2
3 changed files with 61 additions and 2 deletions
  1. 19 1
      app/main.c
  2. 3 0
      app/port/luat_conf_bsp.h
  3. 39 1
      app/port/luat_malloc_air101.c

+ 19 - 1
app/main.c

@@ -38,30 +38,48 @@
  __attribute__((aligned(8))) static uint64_t heap_ext[(LUAT_HEAP_SIZE - 128*1024) / 8];
 #endif
 
+#ifdef LUAT_USE_TLSF
+#include "tlsf.h"
+tlsf_t luavm_tlsf;
+pool_t luavm_tlsf_ext;
+#endif
 
 static void luat_start(void *sdata){
 	// 毕竟sram还是快很多的, 优先sram吧
+#ifndef LUAT_USE_TLSF
 	bpool((void*)0x20028000, 128*1024);
+#else
+	luavm_tlsf = tlsf_create_with_pool((void*)0x20028000, 128*1024);
+#endif
+
 #ifdef LUAT_USE_PSRAM
 	char test[] = {0xAA, 0xBB, 0xCC, 0xDD};
 	char* psram_ptr = (void*)0x30010000;
 	LLOGD("check psram ...");
 	memcpy(psram_ptr, test, 4);
 	if (memcmp(psram_ptr, test, 4)) {
-		LLOGW("psram is enable, but can't access!!");
+		LLOGE("psram is enable, but can't access!!");
 	}
 	else {
 		LLOGD("psram is ok");
 		memset(psram_ptr, 0, 1024);
 		// 存在psram, 加入到内存次, 就不使用系统额外的内存了.
+		#ifdef LUAT_USE_TLSF
+		luavm_tlsf_ext = tlsf_add_pool(luavm_tlsf, heap_ext, LUAT_HEAP_SIZE - 128*1024);
+		#else
 		bpool(psram_ptr, 4*1024*1024); // 如果是8M内存, 改成 8也可以.
+		#endif
 		luat_main();
 		return;
 	}
 #else
 	// 暂时还是放在这里吧, 考虑改到0x20028000之前
 	#if (LUAT_HEAP_SIZE > 128*1024)
+#ifndef LUAT_USE_TLSF
 	bpool((void*)heap_ext, LUAT_HEAP_SIZE - 128*1024);
+#else
+	luavm_tlsf_ext = tlsf_add_pool(luavm_tlsf, heap_ext, LUAT_HEAP_SIZE - 128*1024);
+#endif
 	#endif
 #endif
 	luat_main();

+ 3 - 0
app/port/luat_conf_bsp.h

@@ -76,6 +76,9 @@
 // zlib压缩,更快更小的实现
 #define LUAT_USE_MINIZ 1
 
+// // 使用 TLSF 内存池, 实验性, 内存利用率更高一些
+// #define LUAT_USE_TLSF 1
+
 //---------------SDIO-FATFS特别配置
 // sdio库对接的是fatfs
 // fatfs的长文件名和非英文文件名支持需要180k的ROM, 非常奢侈

+ 39 - 1
app/port/luat_malloc_air101.c

@@ -43,6 +43,44 @@ void* luat_heap_calloc(size_t count, size_t _size) {
 
 //------------------------------------------------
 // ---------- 管理 LuaVM所使用的内存----------------
+#ifdef LUAT_USE_TLSF
+#include "tlsf.h"
+
+extern tlsf_t luavm_tlsf;
+extern pool_t luavm_tlsf_ext;
+
+void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
+    return tlsf_realloc(luavm_tlsf, ptr, nsize);
+}
+
+typedef struct walker_ctx
+{
+    size_t total;
+    size_t used;
+}walker_ctx_t;
+
+static void luat_tlsf_walker(void* ptr, size_t size, int used, void* user) {
+    walker_ctx_t *ctx = (walker_ctx_t*)user;
+    ctx->total = ctx->total + size;
+    if (used != 0)
+        ctx->used = ctx->used + size;
+    //printf(">> %p %04X %d\n", ptr, size, used);
+}
+
+void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
+    pool_t pool = tlsf_get_pool(luavm_tlsf);
+    walker_ctx_t ctx = {0};
+	tlsf_walk_pool(pool, luat_tlsf_walker, &ctx);
+    if (luavm_tlsf_ext) {
+        tlsf_walk_pool(luavm_tlsf_ext, luat_tlsf_walker, &ctx);
+    }
+    *total = ctx.total;
+    *used = ctx.used;
+    *max_used = ctx.used;
+    //printf("why ??\n");
+}
+
+#else
 void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
     if (0) {
         if (ptr) {
@@ -99,5 +137,5 @@ void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
 	*max_used = bstatsmaxget();
     *total = curalloc + totfree;
 }
-
+#endif
 //-----------------------------------------------------------------------------