luat_malloc_air101.c 16 KB

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