luat_malloc_air105.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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. #define LUAT_LOG_TAG "heap"
  27. #include "luat_log.h"
  28. #include "FreeRTOS.h"
  29. #include "task.h"
  30. #include "app_interface.h"
  31. //------------------------------------------------
  32. // 管理系统内存
  33. void* luat_heap_malloc(size_t len) {
  34. return OS_Malloc(len);
  35. }
  36. void luat_heap_free(void* ptr) {
  37. OS_Free(ptr);
  38. }
  39. void* luat_heap_realloc(void* ptr, size_t len) {
  40. void* tmp = luat_heap_malloc(len);
  41. if (tmp && ptr) {
  42. memcpy(tmp, ptr, len);
  43. }
  44. return tmp;
  45. }
  46. void* luat_heap_calloc(size_t count, size_t _size) {
  47. void *ptr = luat_heap_malloc(count * _size);
  48. if (ptr) {
  49. memset(ptr, 0, _size);
  50. }
  51. return ptr;
  52. }
  53. //------------------------------------------------
  54. //------------------------------------------------
  55. // ---------- 管理 LuaVM所使用的内存----------------
  56. void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  57. // if (0) {
  58. // if (ptr) {
  59. // if (nsize) {
  60. // // 缩放内存块
  61. // LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  62. // }
  63. // else {
  64. // // 释放内存块
  65. // LLOGD("free %p ", ptr);
  66. // OS_Free(ptr);
  67. // return NULL;
  68. // }
  69. // }
  70. // else {
  71. // // 申请内存块
  72. // ptr = OS_Malloc(nsize);
  73. // LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  74. // return ptr;
  75. // }
  76. // }
  77. if (nsize)
  78. {
  79. void* ptmp = pvPortMalloc(nsize);
  80. if (ptmp)
  81. {
  82. if (osize > nsize)
  83. {
  84. memcpy(ptmp, ptr, nsize);
  85. }
  86. else
  87. {
  88. memcpy(ptmp, ptr, osize);
  89. }
  90. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  91. {
  92. vPortFree(ptr);
  93. }
  94. return ptmp;
  95. }
  96. else if (osize >= nsize)
  97. {
  98. return ptr;
  99. }
  100. }
  101. if (((uint32_t)ptr & __SRAM_BASE_ADDR__) == __SRAM_BASE_ADDR__)
  102. {
  103. vPortFree(ptr);
  104. }
  105. return NULL;
  106. }
  107. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  108. *used = configTOTAL_HEAP_SIZE - xPortGetFreeHeapSize();
  109. *max_used = configTOTAL_HEAP_SIZE - xPortGetMinimumEverFreeHeapSize();
  110. *total = configTOTAL_HEAP_SIZE;
  111. }
  112. void luat_meminfo_sys(size_t *total, size_t *used, size_t *max_used) {
  113. long curalloc, totfree, maxfree;
  114. unsigned long nget, nrel;
  115. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  116. *used = curalloc;
  117. *max_used = bstatsmaxget();
  118. *total = curalloc + totfree;
  119. }
  120. //-----------------------------------------------------------------------------