luat_malloc_air101.c 16 KB

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