luat_malloc_air105.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*
  2. * Copyright (c) 2022 OpenLuat & AirM2M
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of
  5. * this software and associated documentation files (the "Software"), to deal in
  6. * the Software without restriction, including without limitation the rights to
  7. * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  8. * the Software, and to permit persons to whom the Software is furnished to do so,
  9. * subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  16. * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  17. * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  18. * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  19. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. */
  21. // 这个文件包含 系统heap和lua heap的默认实现
  22. #include <stdlib.h>
  23. #include <string.h>//add for memset
  24. #include "bget.h"
  25. #include "luat_malloc.h"
  26. #include "luat_bget.h"
  27. #define LUAT_LOG_TAG "heap"
  28. #include "luat_log.h"
  29. #include "FreeRTOS.h"
  30. #include "task.h"
  31. #include "app_interface.h"
  32. static luat_bget_t luavm_pool;
  33. static uint64_t luavm_pool_data[25 * 1024];
  34. //------------------------------------------------
  35. // 管理系统内存
  36. void* luat_heap_malloc(size_t len) {
  37. return malloc(len);
  38. }
  39. void luat_heap_free(void* ptr) {
  40. free(ptr);
  41. }
  42. void* luat_heap_realloc(void* ptr, size_t len) {
  43. return realloc(ptr, len);
  44. }
  45. void* luat_heap_calloc(size_t count, size_t _size) {
  46. return calloc(count,_size);
  47. }
  48. void* luat_heap_zalloc(size_t _size) {
  49. return zalloc(_size);
  50. }
  51. //------------------------------------------------
  52. void luat_vm_pool_init(void)
  53. {
  54. luat_bget_init(&luavm_pool);
  55. luat_bpool(&luavm_pool, luavm_pool_data, sizeof(luavm_pool_data));
  56. }
  57. //------------------------------------------------
  58. void *luat_vm_malloc(size_t nsize)
  59. {
  60. return luat_bget(&luavm_pool, nsize);
  61. }
  62. void luat_vm_free(void *ptr)
  63. {
  64. return luat_brel(&luavm_pool, ptr);
  65. }
  66. // ---------- 管理 LuaVM所使用的内存----------------
  67. void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  68. // if (0) {
  69. // if (ptr) {
  70. // if (nsize) {
  71. // // 缩放内存块
  72. // LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  73. // }
  74. // else {
  75. // // 释放内存块
  76. // LLOGD("free %p ", ptr);
  77. // free(ptr);
  78. // return NULL;
  79. // }
  80. // }
  81. // else {
  82. // // 申请内存块
  83. // ptr = malloc(nsize);
  84. // LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  85. // return ptr;
  86. // }
  87. // }
  88. if (nsize)
  89. {
  90. void* ptmp = luat_bget(&luavm_pool, nsize);
  91. if (ptmp)
  92. {
  93. if (ptr)
  94. {
  95. if (osize > nsize)
  96. {
  97. memcpy(ptmp, ptr, nsize);
  98. }
  99. else
  100. {
  101. memcpy(ptmp, ptr, osize);
  102. }
  103. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  104. {
  105. luat_brel(&luavm_pool, ptr);
  106. }
  107. }
  108. return ptmp;
  109. }
  110. else if (osize >= nsize)
  111. {
  112. return ptr;
  113. }
  114. }
  115. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  116. {
  117. luat_brel(&luavm_pool, ptr);
  118. }
  119. return NULL;
  120. }
  121. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  122. long curalloc, totfree, maxfree;
  123. unsigned long nget, nrel;
  124. luat_bstats(&luavm_pool, &curalloc, &totfree, &maxfree, &nget, &nrel);
  125. *used = curalloc;
  126. *max_used = luat_bstatsmaxget(&luavm_pool);
  127. *total = curalloc + totfree;
  128. }
  129. void luat_meminfo_sys(size_t *total, size_t *used, size_t *max_used) {
  130. long curalloc, totfree, maxfree;
  131. unsigned long nget, nrel;
  132. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  133. *used = curalloc;
  134. *max_used = bstatsmaxget();
  135. *total = curalloc + totfree;
  136. }
  137. //-----------------------------------------------------------------------------