luat_malloc_air101.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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. #define LUAT_LOG_TAG "heap"
  7. #include "luat_log.h"
  8. #include "wm_mem.h"
  9. // const uint32_t luat_rom_addr_start = 0x8010000;
  10. // #ifdef AIR103
  11. // const uint32_t luat_rom_addr_end = 0x80FFFFF;
  12. // #else
  13. // const uint32_t luat_rom_addr_end = 0x81FFFFF;
  14. // #endif
  15. //------------------------------------------------
  16. // 管理系统内存
  17. void* luat_heap_malloc(size_t len) {
  18. return tls_mem_alloc(len);
  19. }
  20. void luat_heap_free(void* ptr) {
  21. if (ptr == NULL)
  22. return;
  23. tls_mem_free(ptr);
  24. }
  25. void* luat_heap_realloc(void* ptr, size_t len) {
  26. return tls_mem_realloc(ptr, len);
  27. }
  28. void* luat_heap_calloc(size_t count, size_t _size) {
  29. return tls_mem_calloc(count, _size);
  30. }
  31. //------------------------------------------------
  32. //------------------------------------------------
  33. // ---------- 管理 LuaVM所使用的内存----------------
  34. #ifdef LUAT_USE_TLSF
  35. #include "tlsf.h"
  36. extern tlsf_t luavm_tlsf;
  37. extern pool_t luavm_tlsf_ext;
  38. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  39. return tlsf_realloc(luavm_tlsf, ptr, nsize);
  40. }
  41. typedef struct walker_ctx
  42. {
  43. size_t total;
  44. size_t used;
  45. }walker_ctx_t;
  46. static void luat_tlsf_walker(void* ptr, size_t size, int used, void* user) {
  47. walker_ctx_t *ctx = (walker_ctx_t*)user;
  48. ctx->total = ctx->total + size;
  49. if (used != 0)
  50. ctx->used = ctx->used + size;
  51. //printf(">> %p %04X %d\n", ptr, size, used);
  52. }
  53. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  54. pool_t pool = tlsf_get_pool(luavm_tlsf);
  55. walker_ctx_t ctx = {0};
  56. tlsf_walk_pool(pool, luat_tlsf_walker, &ctx);
  57. if (luavm_tlsf_ext) {
  58. tlsf_walk_pool(luavm_tlsf_ext, luat_tlsf_walker, &ctx);
  59. }
  60. *total = ctx.total;
  61. *used = ctx.used;
  62. *max_used = ctx.used;
  63. //printf("why ??\n");
  64. }
  65. #else
  66. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  67. if (0) {
  68. if (ptr) {
  69. if (nsize) {
  70. // 缩放内存块
  71. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  72. }
  73. else {
  74. // 释放内存块
  75. LLOGD("free %p ", ptr);
  76. brel(ptr);
  77. return NULL;
  78. }
  79. }
  80. else {
  81. // 申请内存块
  82. ptr = bget(nsize);
  83. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  84. return ptr;
  85. }
  86. }
  87. if (nsize)
  88. {
  89. void* ptmp = bgetr(ptr, nsize);
  90. if(ptmp == NULL && osize >= nsize)
  91. {
  92. return ptr;
  93. }
  94. return ptmp;
  95. }
  96. // #if 0
  97. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  98. if (ptr != NULL) {
  99. uint32_t ptrv = (uint32_t)ptr;
  100. if (ptrv >= 0X40000000U) {
  101. // nop 无需释放
  102. }
  103. else {
  104. brel(ptr);
  105. }
  106. }
  107. #else
  108. brel(ptr);
  109. #endif
  110. return NULL;
  111. }
  112. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  113. long curalloc, totfree, maxfree;
  114. unsigned long nget, nrel;
  115. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  116. *used = curalloc;
  117. *max_used = bstatsmaxget();
  118. *total = curalloc + totfree;
  119. }
  120. #endif
  121. //-----------------------------------------------------------------------------