luat_malloc_air101.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. #if configUSE_HEAP3
  32. extern size_t xTotalHeapSize;
  33. extern size_t xFreeBytesRemaining;
  34. extern size_t xFreeBytesMin;
  35. #endif
  36. extern unsigned int heap_size_max;
  37. extern unsigned int total_mem_size;
  38. void luat_meminfo_sys(size_t* total, size_t* used, size_t* max_used)
  39. {
  40. #if configUSE_HEAP3
  41. *used = xTotalHeapSize - xFreeBytesRemaining;
  42. *max_used = xTotalHeapSize - xFreeBytesMin;
  43. *total = xTotalHeapSize;
  44. #else
  45. *used = heap_size_max - total_mem_size;
  46. *max_used = *used;
  47. *total = heap_size_max;
  48. #endif
  49. }
  50. //------------------------------------------------
  51. //------------------------------------------------
  52. // ---------- 管理 LuaVM所使用的内存----------------
  53. #ifdef LUAT_USE_TLSF
  54. #include "tlsf.h"
  55. extern tlsf_t luavm_tlsf;
  56. extern pool_t luavm_tlsf_ext;
  57. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  58. if (ptr == NULL && nsize == 0)
  59. return NULL;
  60. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  61. if (ptr != NULL) {
  62. uint32_t ptrv = (uint32_t)ptr;
  63. if (ptrv >= 0x08000000 && ptrv < 0x08200000) {
  64. //LLOGD("??? %p %d %d", ptr, osize, nsize);
  65. if (nsize == 0)
  66. return NULL;
  67. }
  68. }
  69. #endif
  70. return tlsf_realloc(luavm_tlsf, ptr, nsize);
  71. }
  72. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  73. *total = 0;
  74. *used = 0;
  75. *max_used = 0;
  76. tlsf_stat(tlsf_get_pool(luavm_tlsf), total, used, max_used);
  77. if (luavm_tlsf_ext) {
  78. tlsf_stat(luavm_tlsf_ext, total, used, max_used);
  79. }
  80. }
  81. #else
  82. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  83. if (ptr == NULL && nsize == 0)
  84. return NULL;
  85. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  86. if (ptr != NULL) {
  87. uint32_t ptrv = (uint32_t)ptr;
  88. if (ptrv >= 0x08000000 && ptrv < 0x08200000) {
  89. // LLOGD("??? %p %d %d", ptr, osize, nsize);
  90. return NULL;
  91. }
  92. }
  93. #endif
  94. if (0) {
  95. if (ptr) {
  96. if (nsize) {
  97. // 缩放内存块
  98. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  99. }
  100. else {
  101. // 释放内存块
  102. LLOGD("free %p ", ptr);
  103. brel(ptr);
  104. return NULL;
  105. }
  106. }
  107. else {
  108. // 申请内存块
  109. ptr = bget(nsize);
  110. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  111. return ptr;
  112. }
  113. }
  114. if (nsize)
  115. {
  116. void* ptmp = bgetr(ptr, nsize);
  117. if(ptmp == NULL && osize >= nsize)
  118. {
  119. return ptr;
  120. }
  121. return ptmp;
  122. }
  123. brel(ptr);
  124. return NULL;
  125. }
  126. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  127. long curalloc, totfree, maxfree;
  128. unsigned long nget, nrel;
  129. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  130. *used = curalloc;
  131. *max_used = bstatsmaxget();
  132. *total = curalloc + totfree;
  133. }
  134. #endif
  135. //-----------------------------------------------------------------------------