luat_malloc_air101.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. // 这个文件包含 系统heap和lua heap的默认实现
  2. #include <stdlib.h>
  3. #include <string.h>//add for memset
  4. #include "bget.h"
  5. #include "luat_malloc.h"
  6. #include "FreeRTOS.h"
  7. #define LUAT_LOG_TAG "heap"
  8. #include "luat_log.h"
  9. #include "wm_mem.h"
  10. #include "wm_ram_config.h"
  11. void* __wrap_malloc(size_t len);
  12. void __wrap_free(void* ptr);
  13. void* __wrap_calloc(size_t itemCount, size_t itemSize);
  14. void* __wrap_zalloc(size_t size);
  15. void* __wrap_realloc(void*ptr, size_t len);
  16. // 内存分2个区域, P1 和 P2
  17. // 其中P1是160k, 同时也是sys的主要区域, 栈内存也必须分配在这里
  18. // P2默认是WIFI和外设的内存, 不启用WIFI时全部归lua使用, 启用WIFI时部分使用
  19. #ifdef LUAT_USE_WLAN
  20. // 与tls_wifi_mem_cfg正相关, 假设tx是7, rx是3
  21. // (7+3)*2*1638+4096 = 36k , 然后lua可用 128 - 36 = 92
  22. // (6+4)*2*1638+4096 = 36k , 然后lua可用 128 - 36 = 92
  23. // (7+7)*2*1638+4096 = 49k , 然后lua可用 128 - 49 = 79
  24. #define LUAT_HEAP_P2_SIZE (92*1024)
  25. #else
  26. #define LUAT_HEAP_P2_SIZE (128*1024)
  27. #endif
  28. #if defined(LUAT_USE_NIMBLE) || defined(LUAT_USE_TLS)
  29. #ifdef LUAT_USE_WLAN
  30. #define LUAT_HEAP_P1_SIZE 0
  31. #else
  32. #define LUAT_HEAP_P1_SIZE 24*1024
  33. #endif
  34. #else
  35. #ifdef LUAT_USE_WLAN
  36. #define LUAT_HEAP_P1_SIZE 24* 1024
  37. #else
  38. #define LUAT_HEAP_P1_SIZE 48*1024
  39. #endif
  40. #endif
  41. #if (LUAT_HEAP_P1_SIZE > 0)
  42. #ifndef LUAT_USE_PSRAM
  43. __attribute__((aligned(8))) static uint64_t heap_ext[(LUAT_HEAP_P1_SIZE) / 8];
  44. #else
  45. static uint64_t* heap_ext;
  46. #endif
  47. #endif
  48. #ifdef LUAT_USE_TLSF
  49. #include "tlsf.h"
  50. tlsf_t luavm_tlsf;
  51. pool_t luavm_tlsf_ext;
  52. #endif
  53. #ifdef LUAT_USE_PROFILER
  54. #include "luat_profiler.h"
  55. extern int luat_profiler_memdebug;
  56. extern luat_profiler_mem_t profiler_memregs[];
  57. #endif
  58. extern uint32_t __ram_end;
  59. #ifdef LUAT_USE_PSRAM
  60. size_t psram_size = 0;
  61. size_t psram_lua_size = 0;
  62. size_t psram_sys_size = 0;
  63. #include "luat_bget.h"
  64. luat_bget_t psram_bget;
  65. #endif
  66. void luat_heap_init(void) {
  67. // 毕竟sram还是快很多的, 优先sram吧
  68. //LLOGD("VM MEM P1 %08X P2 %08X", LUAT_HEAP_P1_SIZE, LUAT_HEAP_P2_SIZE);
  69. #if (LUAT_HEAP_P1_SIZE > 0)
  70. if (heap_ext) {
  71. #ifndef LUAT_USE_TLSF
  72. bpool((void*)heap_ext, LUAT_HEAP_P1_SIZE);
  73. #else
  74. luavm_tlsf = tlsf_create_with_pool((void*)heap_ext, LUAT_HEAP_P1_SIZE);
  75. #endif
  76. }
  77. #endif
  78. #ifdef LUAT_USE_PSRAM
  79. const char test[] = {0xAA, 0xBB, 0xCC, 0xDD};
  80. char* psram_ptr = (void*)0x30010000;
  81. // LLOGD("check psram ...");
  82. for (size_t i = 0; i <= 16; i++)
  83. {
  84. psram_size = i * 512 * 1024;
  85. memcpy(psram_ptr + psram_size + 128 * 1024, test, 4);
  86. if (memcmp(psram_ptr + psram_size + 128 * 1024, test, 4)) {
  87. break;
  88. }
  89. }
  90. if (psram_size <= 512 * 1024) {
  91. psram_size = 0;
  92. }
  93. LLOGD("PSRAM size %dkb", psram_size / 1024);
  94. if (psram_size == 0) {
  95. LLOGE("psram is enable, but can't access!!");
  96. #if (LUAT_HEAP_P1_SIZE > 0)
  97. heap_ext = tls_mem_alloc(LUAT_HEAP_P1_SIZE);
  98. bpool((void*)heap_ext, LUAT_HEAP_P1_SIZE);
  99. #endif
  100. }
  101. else {
  102. // LLOGD("psram is ok");
  103. // 存在psram, 加入到内存次, 就不使用系统额外的内存了.
  104. if (psram_size >= 6 * 1024 * 1024) {
  105. psram_lua_size = 5*1024*1024;
  106. }
  107. else if (psram_size >= 4 * 1024 * 1024) {
  108. psram_lua_size = 3*1024*1024;
  109. }
  110. else if (psram_size >= 2 * 1024 * 1024) {
  111. psram_lua_size = 1*1024*1024;
  112. }
  113. else {
  114. psram_lua_size = 512*1024;
  115. }
  116. psram_sys_size = psram_size - psram_lua_size;
  117. LLOGD("PSRAM 内存分配 --> Lua %dkb Sys %dkb", psram_lua_size / 1024, psram_sys_size / 1024);
  118. #ifdef LUAT_USE_TLSF
  119. luavm_tlsf_ext = tlsf_add_pool(luavm_tlsf, psram_ptr, psram_lua_size);
  120. #else
  121. bpool(psram_ptr, psram_lua_size); // 如果是8M内存, 改成 8也可以
  122. #endif
  123. if (psram_sys_size > 0) {
  124. luat_bget_init(&psram_bget);
  125. luat_bpool(&psram_bget, psram_ptr + psram_lua_size, psram_sys_size);
  126. char* tmp = luat_bget(&psram_bget, 1024);
  127. if (tmp) {
  128. luat_brel(&psram_bget, tmp);
  129. }
  130. }
  131. return;
  132. }
  133. #else
  134. // LLOGD("__ram_end %08X", (uint32_t)&__ram_end);
  135. char* ptr = (void*)(0x20028000);
  136. size_t heap2_size = LUAT_HEAP_P2_SIZE;
  137. #if defined(LUAT_USE_NIMBLE) && defined(LUAT_USE_TLS) && defined(LUAT_USE_WLAN)
  138. heap2_size -= 32*1024;
  139. ptr += 32*1024;
  140. #endif
  141. #if defined(LUAT_USE_SPI_SLAVE)
  142. heap2_size -= SLAVE_HSPI_MAX_SIZE;
  143. ptr += SLAVE_HSPI_MAX_SIZE;
  144. #endif
  145. #ifndef LUAT_USE_TLSF
  146. bpool(ptr, heap2_size);
  147. #else
  148. luavm_tlsf_ext = tlsf_add_pool(ptr, heap2_size);
  149. #endif
  150. #endif
  151. }
  152. //------------------------------------------------
  153. // 管理系统内存
  154. #ifdef LUAT_USE_PROFILER_XXX
  155. void* luat_heap_malloc(size_t len) {
  156. void* ptr = __wrap_malloc(len);
  157. printf("luat_heap_malloc %p %d\n", ptr, len);
  158. return ptr;
  159. }
  160. void luat_heap_free(void* ptr) {
  161. if (ptr == NULL)
  162. return;
  163. printf("luat_heap_free %p\n", ptr);
  164. __wrap_free(ptr);
  165. }
  166. void* luat_heap_realloc(void* ptr, size_t len) {
  167. void* nptr = __wrap_realloc(ptr, len);
  168. printf("luat_heap_realloc %p %d %p\n", ptr, len, nptr);
  169. return nptr;
  170. }
  171. void* luat_heap_calloc(size_t count, size_t _size) {
  172. void* ptr = __wrap_calloc(count, _size);
  173. printf("luat_heap_calloc %p\n", ptr);
  174. return ptr;
  175. }
  176. #else
  177. void* luat_heap_malloc(size_t len) {
  178. return tls_mem_alloc(len);
  179. }
  180. void luat_heap_free(void* ptr) {
  181. if (ptr == NULL)
  182. return;
  183. tls_mem_free(ptr);
  184. }
  185. void* luat_heap_realloc(void* ptr, size_t len) {
  186. return tls_mem_realloc(ptr, len);
  187. }
  188. void* luat_heap_calloc(size_t count, size_t _size) {
  189. return tls_mem_calloc(count, _size);
  190. }
  191. #endif
  192. extern unsigned int heap_size_max;
  193. extern unsigned int total_mem_size;
  194. extern unsigned int min_free_size;
  195. extern size_t __heap_start;
  196. extern size_t __heap_end;
  197. void luat_meminfo_sys(size_t* total, size_t* used, size_t* max_used)
  198. {
  199. #if configUSE_HEAP4
  200. extern void vPortGetHeapStats( HeapStats_t *pxHeapStats );
  201. HeapStats_t stat = {0};
  202. vPortGetHeapStats(&stat);
  203. *total = (size_t)(&__heap_end) - (size_t)(&__heap_start);
  204. *max_used = *total - stat.xMinimumEverFreeBytesRemaining;
  205. *used = (*total) - (stat.xAvailableHeapSpaceInBytes);
  206. #else
  207. *used = heap_size_max - total_mem_size;
  208. *max_used = heap_size_max - min_free_size;
  209. *total = heap_size_max;
  210. #endif
  211. }
  212. //------------------------------------------------
  213. //------------------------------------------------
  214. // ---------- 管理 LuaVM所使用的内存----------------
  215. #ifdef LUAT_USE_TLSF
  216. #include "tlsf.h"
  217. // extern tlsf_t luavm_tlsf;
  218. // extern pool_t luavm_tlsf_ext;
  219. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  220. if (ptr == NULL && nsize == 0)
  221. return NULL;
  222. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  223. if (ptr != NULL) {
  224. uint32_t ptrv = (uint32_t)ptr;
  225. if (ptrv >= 0x08000000 && ptrv < 0x08200000) {
  226. //LLOGD("??? %p %d %d", ptr, osize, nsize);
  227. if (nsize == 0)
  228. return NULL;
  229. }
  230. }
  231. #endif
  232. return tlsf_realloc(luavm_tlsf, ptr, nsize);
  233. }
  234. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  235. *total = 0;
  236. *used = 0;
  237. *max_used = 0;
  238. tlsf_stat(tlsf_get_pool(luavm_tlsf), total, used, max_used);
  239. if (luavm_tlsf_ext) {
  240. tlsf_stat(luavm_tlsf_ext, total, used, max_used);
  241. }
  242. }
  243. #else
  244. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  245. (void)ud;
  246. if (ptr == NULL && nsize == 0)
  247. return NULL;
  248. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  249. if (ptr != NULL) {
  250. uint32_t ptrv = (uint32_t)ptr;
  251. if (ptrv >= 0x08000000 && ptrv < 0x08200000) {
  252. // LLOGD("??? %p %d %d", ptr, osize, nsize);
  253. return NULL;
  254. }
  255. }
  256. #endif
  257. if (0) {
  258. if (ptr) {
  259. if (nsize) {
  260. // 缩放内存块
  261. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  262. }
  263. else {
  264. // 释放内存块
  265. LLOGD("free %p ", ptr);
  266. brel(ptr);
  267. return NULL;
  268. }
  269. }
  270. else {
  271. // 申请内存块
  272. ptr = bget(nsize);
  273. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  274. return ptr;
  275. }
  276. }
  277. if (nsize)
  278. {
  279. void* ptmp = bgetr(ptr, nsize);
  280. if(ptmp == NULL && osize >= nsize)
  281. {
  282. return ptr;
  283. }
  284. return ptmp;
  285. }
  286. brel(ptr);
  287. return NULL;
  288. }
  289. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  290. long curalloc, totfree, maxfree;
  291. unsigned long nget, nrel;
  292. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  293. *used = curalloc;
  294. *max_used = bstatsmaxget();
  295. *total = curalloc + totfree;
  296. }
  297. #endif
  298. //-----------------------------------------------------------------------------
  299. #include "wm_include.h"
  300. #include "FreeRTOS.h"
  301. #include "stdio.h"
  302. #ifdef LUAT_USE_PROFILER
  303. void* __wrap_malloc(size_t len) {
  304. #ifdef LUAT_USE_PROFILER
  305. void* ptr = pvPortMalloc(len);
  306. if (luat_profiler_memdebug) {
  307. // printf("malloc %d %p\n", len, ptr);
  308. if (ptr == NULL)
  309. return NULL;
  310. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  311. {
  312. if (profiler_memregs[i].addr == 0) {
  313. profiler_memregs[i].addr = (uint32)ptr;
  314. profiler_memregs[i].len = len;
  315. break;
  316. }
  317. }
  318. }
  319. return ptr;
  320. #else
  321. return pvPortMalloc(len);
  322. #endif
  323. }
  324. void __wrap_free(void* ptr) {
  325. if (ptr == NULL)
  326. return;
  327. #ifdef LUAT_USE_PROFILER
  328. // if (luat_profiler_memdebug)
  329. // printf("free %p\n", ptr);
  330. #endif
  331. u32 addr = (u32)ptr;
  332. if (addr >= 0x20000000 && addr <= 0x40000000) {
  333. // printf("free %p\n", ptr);
  334. vPortFree(ptr);
  335. #ifdef LUAT_USE_PROFILER
  336. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  337. {
  338. if (profiler_memregs[i].addr == addr) {
  339. profiler_memregs[i].len = 0;
  340. profiler_memregs[i].addr = 0;
  341. break;
  342. }
  343. }
  344. #endif
  345. }
  346. }
  347. void *pvPortRealloc( void *pv, size_t xWantedSize );
  348. void* __wrap_realloc(void*ptr, size_t len) {
  349. #ifdef LUAT_USE_PROFILER
  350. void* newptr = pvPortRealloc(ptr, len);
  351. if (luat_profiler_memdebug && newptr) {
  352. // printf("realloc %p %d %p\n", ptr, len, newptr);
  353. uint32_t addr = (uint32_t)ptr;
  354. uint32_t naddr = (uint32_t)newptr;
  355. if (ptr == newptr) {
  356. // 相同内存地址, 只是扩容了
  357. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  358. {
  359. if (profiler_memregs[i].addr == addr) {
  360. profiler_memregs[i].len = len;
  361. addr = 0;
  362. break;
  363. }
  364. }
  365. if (0 != addr) {
  366. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  367. {
  368. if (profiler_memregs[i].addr == 0) {
  369. profiler_memregs[i].addr = addr;
  370. profiler_memregs[i].len = len;
  371. break;
  372. }
  373. }
  374. }
  375. }
  376. else {
  377. if (ptr == NULL) {
  378. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  379. {
  380. if (profiler_memregs[i].addr == 0) {
  381. profiler_memregs[i].addr = naddr;
  382. profiler_memregs[i].len = len;
  383. break;
  384. }
  385. }
  386. }
  387. else {
  388. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  389. {
  390. if (profiler_memregs[i].addr == addr) {
  391. profiler_memregs[i].addr = 0;
  392. profiler_memregs[i].len = 0;
  393. break;
  394. }
  395. }
  396. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  397. {
  398. if (profiler_memregs[i].addr == 0) {
  399. profiler_memregs[i].addr = naddr;
  400. profiler_memregs[i].len = len;
  401. break;
  402. }
  403. }
  404. }
  405. }
  406. }
  407. return newptr;
  408. #endif
  409. return pvPortRealloc(ptr, len);
  410. }
  411. void* __wrap_calloc(size_t itemCount, size_t itemSize) {
  412. void* ptr = pvPortMalloc(itemCount * itemSize);
  413. #ifdef LUAT_USE_PROFILER
  414. if (luat_profiler_memdebug) {
  415. // printf("calloc %p %d\n", ptr, itemCount * itemSize);
  416. if (ptr) {
  417. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  418. {
  419. if (profiler_memregs[i].addr == 0) {
  420. profiler_memregs[i].addr = (uint32_t)ptr;
  421. profiler_memregs[i].len = itemCount * itemSize;
  422. break;
  423. }
  424. }
  425. }
  426. }
  427. #endif
  428. if (ptr == NULL)
  429. return NULL;
  430. memset(ptr, 0, itemCount * itemSize);
  431. return ptr;
  432. }
  433. void* __wrap_zalloc(size_t size) {
  434. void* ptr = pvPortMalloc(size);
  435. #ifdef LUAT_USE_PROFILER
  436. if (luat_profiler_memdebug) {
  437. // printf("zalloc %p %d\n", ptr, size);
  438. if (ptr) {
  439. for (size_t i = 0; i < LUAT_PROFILER_MEMDEBUG_ADDR_COUNT; i++)
  440. {
  441. if (profiler_memregs[i].addr == 0) {
  442. profiler_memregs[i].addr = (uint32_t)ptr;
  443. profiler_memregs[i].len = size;
  444. break;
  445. }
  446. }
  447. }
  448. }
  449. #endif
  450. if (ptr == NULL)
  451. return NULL;
  452. memset(ptr, 0, size);
  453. return ptr;
  454. }
  455. #else
  456. // 标准实现
  457. void* __wrap_malloc(size_t len) {
  458. return pvPortMalloc(len);
  459. }
  460. void __wrap_free(void* ptr) {
  461. if (ptr == NULL)
  462. return;
  463. u32 addr = (u32)ptr;
  464. if (addr >= 0x20000000 && addr <= 0x40000000) {
  465. vPortFree(ptr);
  466. }
  467. }
  468. void *pvPortRealloc( void *pv, size_t xWantedSize );
  469. void* __wrap_realloc(void*ptr, size_t len) {
  470. return pvPortRealloc(ptr, len);
  471. }
  472. void* __wrap_calloc(size_t itemCount, size_t itemSize) {
  473. void* ptr = pvPortMalloc(itemCount * itemSize);
  474. if (ptr == NULL)
  475. return NULL;
  476. memset(ptr, 0, itemCount * itemSize);
  477. return ptr;
  478. }
  479. void* __wrap_zalloc(size_t size) {
  480. void* ptr = pvPortMalloc(size);
  481. if (ptr == NULL)
  482. return NULL;
  483. memset(ptr, 0, size);
  484. return ptr;
  485. }
  486. #endif
  487. #ifdef LUAT_USE_PSRAM
  488. #include "luat_mem.h"
  489. void* luat_heap_opt_malloc(LUAT_HEAP_TYPE_E type,size_t len){
  490. if (type == LUAT_HEAP_PSRAM && psram_sys_size) {
  491. return luat_bget(&psram_bget, len);
  492. }
  493. return luat_heap_malloc(len);
  494. }
  495. void luat_heap_opt_free(LUAT_HEAP_TYPE_E type,void* ptr){
  496. if (type == LUAT_HEAP_PSRAM && psram_sys_size && ((uint32_t)ptr) > 0x30010000) {
  497. return luat_brel(&psram_bget, ptr);
  498. }
  499. luat_heap_free(ptr);
  500. }
  501. void* luat_heap_opt_realloc(LUAT_HEAP_TYPE_E type,void* ptr, size_t len){
  502. if (type == LUAT_HEAP_PSRAM && psram_sys_size && (!ptr || ((uint32_t)ptr) > 0x30010000)) {
  503. return luat_bgetr(&psram_bget, ptr, len);
  504. }
  505. return luat_heap_realloc(ptr, len);
  506. }
  507. void* luat_heap_opt_calloc(LUAT_HEAP_TYPE_E type,size_t count, size_t size){
  508. if (type == LUAT_HEAP_PSRAM && psram_sys_size) {
  509. return luat_bgetz(&psram_bget, count*size);
  510. }
  511. return luat_heap_opt_zalloc(type,count*size);
  512. }
  513. void* luat_heap_opt_zalloc(LUAT_HEAP_TYPE_E type,size_t size){
  514. void *ptr = luat_heap_opt_malloc(type,size);
  515. if (ptr) {
  516. memset(ptr, 0, size);
  517. }
  518. return ptr;
  519. }
  520. void luat_meminfo_opt_sys(LUAT_HEAP_TYPE_E type,size_t* total, size_t* used, size_t* max_used){
  521. if (type == LUAT_HEAP_PSRAM){
  522. if (psram_sys_size > 0) {
  523. long curalloc, totfree, maxfree;
  524. unsigned long nget, nrel;
  525. luat_bstats(&psram_bget, &curalloc, &totfree, &maxfree, &nget, &nrel);
  526. *used = curalloc;
  527. *max_used = luat_bstatsmaxget(&psram_bget);
  528. *total = curalloc + totfree;
  529. }
  530. else {
  531. *total = 0;
  532. *used = 0;
  533. *max_used = 0;
  534. }
  535. }else
  536. luat_meminfo_sys(total, used, max_used);
  537. }
  538. #endif