luat_malloc_win32.c 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // 这个文件包含 系统heap和lua heap的默认实现
  2. #include <stdlib.h>
  3. #include <string.h>//add for memset
  4. #include "bget.h"
  5. #include "luat_mem.h"
  6. #define LUAT_LOG_TAG "vmheap"
  7. #include "luat_log.h"
  8. //------------------------------------------------
  9. // 管理系统内存
  10. void* luat_heap_malloc(size_t len) {
  11. return malloc(len);
  12. }
  13. void luat_heap_free(void* ptr) {
  14. free(ptr);
  15. }
  16. void* luat_heap_realloc(void* ptr, size_t len) {
  17. return realloc(ptr, len);
  18. }
  19. void* luat_heap_calloc(size_t count, size_t _size) {
  20. void *ptr = luat_heap_malloc(count * _size);
  21. if (ptr) {
  22. memset(ptr, 0, _size);
  23. }
  24. return ptr;
  25. }
  26. //------------------------------------------------
  27. //------------------------------------------------
  28. // ---------- 管理 LuaVM所使用的内存----------------
  29. void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  30. if (0) {
  31. if (ptr) {
  32. if (nsize) {
  33. // 缩放内存块
  34. LLOGD("realloc %p from %d to %d", ptr, osize, nsize);
  35. }
  36. else {
  37. // 释放内存块
  38. LLOGD("free %p ", ptr);
  39. brel(ptr);
  40. return NULL;
  41. }
  42. }
  43. else {
  44. // 申请内存块
  45. ptr = bget(nsize);
  46. LLOGD("malloc %p type=%d size=%d", ptr, osize, nsize);
  47. return ptr;
  48. }
  49. }
  50. if (nsize)
  51. {
  52. void* ptmp = bgetr(ptr, nsize);
  53. if(ptmp == NULL && osize >= nsize)
  54. {
  55. return ptr;
  56. }
  57. return ptmp;
  58. }
  59. brel(ptr);
  60. return NULL;
  61. }
  62. void luat_meminfo_luavm(size_t *total, size_t *used, size_t *max_used) {
  63. long curalloc, totfree, maxfree;
  64. unsigned long nget, nrel;
  65. bstats(&curalloc, &totfree, &maxfree, &nget, &nrel);
  66. *used = curalloc;
  67. *max_used = bstatsmaxget();
  68. *total = curalloc + totfree;
  69. }
  70. void luat_meminfo_sys(size_t *total, size_t *used, size_t *max_used) {
  71. *used = 0;
  72. *max_used = 0;
  73. *total = 0;
  74. }
  75. //-----------------------------------------------------------------------------