luat_malloc_air101.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. //------------------------------------------------
  10. // 管理系统内存
  11. void* luat_heap_malloc(size_t len) {
  12. return tls_mem_alloc(len);
  13. }
  14. void luat_heap_free(void* ptr) {
  15. tls_mem_free(ptr);
  16. }
  17. void* luat_heap_realloc(void* ptr, size_t len) {
  18. return tls_mem_realloc(ptr, len);
  19. }
  20. void* luat_heap_calloc(size_t count, size_t _size) {
  21. return tls_mem_calloc(count, _size);
  22. }
  23. //------------------------------------------------
  24. //------------------------------------------------
  25. // ---------- 管理 LuaVM所使用的内存----------------
  26. void* __attribute__((section (".ram_run"))) luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  27. if (0) {
  28. if (ptr) {
  29. if (nsize) {
  30. // 缩放内存块
  31. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  32. }
  33. else {
  34. // 释放内存块
  35. LLOGD("free %p ", ptr);
  36. brel(ptr);
  37. return NULL;
  38. }
  39. }
  40. else {
  41. // 申请内存块
  42. ptr = bget(nsize);
  43. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  44. return ptr;
  45. }
  46. }
  47. if (nsize)
  48. {
  49. void* ptmp = bgetr(ptr, nsize);
  50. if(ptmp == NULL && osize >= nsize)
  51. {
  52. return ptr;
  53. }
  54. return ptmp;
  55. }
  56. brel(ptr);
  57. return NULL;
  58. }
  59. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  60. long curalloc, totfree, maxfree;
  61. unsigned long nget, nrel;
  62. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  63. *used = curalloc;
  64. *max_used = bstatsmaxget();
  65. *total = curalloc + totfree;
  66. }
  67. //-----------------------------------------------------------------------------