luat_malloc_air101.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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. tls_mem_free(ptr);
  22. }
  23. void* luat_heap_realloc(void* ptr, size_t len) {
  24. return tls_mem_realloc(ptr, len);
  25. }
  26. void* luat_heap_calloc(size_t count, size_t _size) {
  27. return tls_mem_calloc(count, _size);
  28. }
  29. //------------------------------------------------
  30. //------------------------------------------------
  31. // ---------- 管理 LuaVM所使用的内存----------------
  32. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  33. if (0) {
  34. if (ptr) {
  35. if (nsize) {
  36. // 缩放内存块
  37. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  38. }
  39. else {
  40. // 释放内存块
  41. LLOGD("free %p ", ptr);
  42. brel(ptr);
  43. return NULL;
  44. }
  45. }
  46. else {
  47. // 申请内存块
  48. ptr = bget(nsize);
  49. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  50. return ptr;
  51. }
  52. }
  53. if (nsize)
  54. {
  55. void* ptmp = bgetr(ptr, nsize);
  56. if(ptmp == NULL && osize >= nsize)
  57. {
  58. return ptr;
  59. }
  60. return ptmp;
  61. }
  62. // #if 0
  63. #ifdef LUAT_USE_MEMORY_OPTIMIZATION_CODE_MMAP
  64. if (ptr != NULL) {
  65. uint32_t ptrv = (uint32_t)ptr;
  66. if (ptrv >= 0X40000000U) {
  67. // nop 无需释放
  68. }
  69. else {
  70. brel(ptr);
  71. }
  72. }
  73. #else
  74. brel(ptr);
  75. #endif
  76. return NULL;
  77. }
  78. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  79. long curalloc, totfree, maxfree;
  80. unsigned long nget, nrel;
  81. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  82. *used = curalloc;
  83. *max_used = bstatsmaxget();
  84. *total = curalloc + totfree;
  85. }
  86. //-----------------------------------------------------------------------------