luat_malloc_air101.c 2.4 KB

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