luat_malloc_air101.c 3.4 KB

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