luat_malloc_air105.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. #ifndef LUAT_HEAP_SIZE
  33. #define LUAT_HEAP_SIZE 200
  34. #endif
  35. #if (LUAT_HEAP_SIZE > 400) || (LUAT_HEAP_SIZE < 100)
  36. #undef LUAT_HEAP_SIZE
  37. #define LUAT_HEAP_SIZE 200
  38. #endif
  39. static luat_bget_t luavm_pool;
  40. static uint64_t luavm_pool_data[LUAT_HEAP_SIZE / 8 * 1024];
  41. //------------------------------------------------
  42. // 管理系统内存
  43. void* luat_heap_malloc(size_t len) {
  44. return malloc(len);
  45. }
  46. void luat_heap_free(void* ptr) {
  47. free(ptr);
  48. }
  49. void* luat_heap_realloc(void* ptr, size_t len) {
  50. return realloc(ptr, len);
  51. }
  52. void* luat_heap_calloc(size_t count, size_t _size) {
  53. return calloc(count,_size);
  54. }
  55. void* luat_heap_zalloc(size_t _size) {
  56. return zalloc(_size);
  57. }
  58. //------------------------------------------------
  59. void luat_vm_pool_init(void)
  60. {
  61. luat_bget_init(&luavm_pool);
  62. luat_bpool(&luavm_pool, luavm_pool_data, sizeof(luavm_pool_data));
  63. }
  64. //------------------------------------------------
  65. void *luat_vm_malloc(size_t nsize)
  66. {
  67. return luat_bget(&luavm_pool, nsize);
  68. }
  69. void luat_vm_free(void *ptr)
  70. {
  71. return luat_brel(&luavm_pool, ptr);
  72. }
  73. // ---------- 管理 LuaVM所使用的内存----------------
  74. void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  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. // free(ptr);
  85. // return NULL;
  86. // }
  87. // }
  88. // else {
  89. // // 申请内存块
  90. // ptr = malloc(nsize);
  91. // LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  92. // return ptr;
  93. // }
  94. // }
  95. if (nsize)
  96. {
  97. void* ptmp = luat_bget(&luavm_pool, nsize);
  98. if (ptmp)
  99. {
  100. if (ptr)
  101. {
  102. if (osize > nsize)
  103. {
  104. memcpy(ptmp, ptr, nsize);
  105. }
  106. else
  107. {
  108. memcpy(ptmp, ptr, osize);
  109. }
  110. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  111. {
  112. luat_brel(&luavm_pool, ptr);
  113. }
  114. }
  115. return ptmp;
  116. }
  117. else if (osize >= nsize)
  118. {
  119. return ptr;
  120. }
  121. }
  122. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  123. {
  124. luat_brel(&luavm_pool, ptr);
  125. }
  126. return NULL;
  127. }
  128. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  129. long curalloc, totfree, maxfree;
  130. unsigned long nget, nrel;
  131. luat_bstats(&luavm_pool, &curalloc, &totfree, &maxfree, &nget, &nrel);
  132. *used = curalloc;
  133. *max_used = luat_bstatsmaxget(&luavm_pool);
  134. *total = curalloc + totfree;
  135. }
  136. void luat_meminfo_sys(size_t *total, size_t *used, size_t *max_used) {
  137. long curalloc, totfree, maxfree;
  138. unsigned long nget, nrel;
  139. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  140. *used = curalloc;
  141. *max_used = bstatsmaxget();
  142. *total = curalloc + totfree;
  143. }
  144. //-----------------------------------------------------------------------------