luat_malloc_air101.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  42. *total = 0;
  43. *used = 0;
  44. *max_used = 0;
  45. tlsf_stat(tlsf_get_pool(luavm_tlsf), total, used, max_used);
  46. if (luavm_tlsf_ext) {
  47. tlsf_stat(luavm_tlsf_ext, total, used, max_used);
  48. }
  49. }
  50. #else
  51. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  52. if (0) {
  53. if (ptr) {
  54. if (nsize) {
  55. // 缩放内存块
  56. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  57. }
  58. else {
  59. // 释放内存块
  60. LLOGD("free %p ", ptr);
  61. brel(ptr);
  62. return NULL;
  63. }
  64. }
  65. else {
  66. // 申请内存块
  67. ptr = bget(nsize);
  68. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  69. return ptr;
  70. }
  71. }
  72. if (nsize)
  73. {
  74. void* ptmp = bgetr(ptr, nsize);
  75. if(ptmp == NULL && osize >= nsize)
  76. {
  77. return ptr;
  78. }
  79. return ptmp;
  80. }
  81. // #if 0
  82. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  83. if (ptr != NULL) {
  84. uint32_t ptrv = (uint32_t)ptr;
  85. if (ptrv >= 0X40000000U) {
  86. // nop 无需释放
  87. }
  88. else {
  89. brel(ptr);
  90. }
  91. }
  92. #else
  93. brel(ptr);
  94. #endif
  95. return NULL;
  96. }
  97. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  98. long curalloc, totfree, maxfree;
  99. unsigned long nget, nrel;
  100. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  101. *used = curalloc;
  102. *max_used = bstatsmaxget();
  103. *total = curalloc + totfree;
  104. }
  105. #endif
  106. //-----------------------------------------------------------------------------