luat_malloc_air105.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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 OS_Malloc(len);
  38. }
  39. void luat_heap_free(void* ptr) {
  40. OS_Free(ptr);
  41. }
  42. void* luat_heap_realloc(void* ptr, size_t len) {
  43. void* tmp = luat_heap_malloc(len);
  44. if (tmp && ptr) {
  45. memcpy(tmp, ptr, len);
  46. }
  47. return tmp;
  48. }
  49. void* luat_heap_calloc(size_t count, size_t _size) {
  50. void *ptr = luat_heap_malloc(count * _size);
  51. if (ptr) {
  52. memset(ptr, 0, _size);
  53. }
  54. return ptr;
  55. }
  56. //------------------------------------------------
  57. void luat_vm_pool_init(void)
  58. {
  59. luat_bget_init(&luavm_pool);
  60. luat_bpool(&luavm_pool, luavm_pool_data, sizeof(luavm_pool_data));
  61. }
  62. //------------------------------------------------
  63. // ---------- 管理 LuaVM所使用的内存----------------
  64. void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  65. // if (0) {
  66. // if (ptr) {
  67. // if (nsize) {
  68. // // 缩放内存块
  69. // LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  70. // }
  71. // else {
  72. // // 释放内存块
  73. // LLOGD("free %p ", ptr);
  74. // OS_Free(ptr);
  75. // return NULL;
  76. // }
  77. // }
  78. // else {
  79. // // 申请内存块
  80. // ptr = OS_Malloc(nsize);
  81. // LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  82. // return ptr;
  83. // }
  84. // }
  85. if (nsize)
  86. {
  87. void* ptmp = luat_bget(&luavm_pool, nsize);
  88. if (ptmp)
  89. {
  90. if (osize > nsize)
  91. {
  92. memcpy(ptmp, ptr, nsize);
  93. }
  94. else
  95. {
  96. memcpy(ptmp, ptr, osize);
  97. }
  98. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  99. {
  100. luat_brel(&luavm_pool, ptr);
  101. }
  102. return ptmp;
  103. }
  104. else if (osize >= nsize)
  105. {
  106. return ptr;
  107. }
  108. }
  109. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  110. {
  111. luat_brel(&luavm_pool, ptr);
  112. }
  113. return NULL;
  114. }
  115. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  116. long curalloc, totfree, maxfree;
  117. unsigned long nget, nrel;
  118. luat_bstats(&luavm_pool, &curalloc, &totfree, &maxfree, &nget, &nrel);
  119. *used = curalloc;
  120. *max_used = luat_bstatsmaxget(&luavm_pool);
  121. *total = curalloc + totfree;
  122. }
  123. void luat_meminfo_sys(size_t *total, size_t *used, size_t *max_used) {
  124. long curalloc, totfree, maxfree;
  125. unsigned long nget, nrel;
  126. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  127. *used = curalloc;
  128. *max_used = bstatsmaxget();
  129. *total = curalloc + totfree;
  130. }
  131. //-----------------------------------------------------------------------------