luat_malloc_rtt.c 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. #include "luat_base.h"
  2. #include "luat_malloc.h"
  3. #include "rtthread.h"
  4. #define DBG_TAG "luat.heap"
  5. #define DBG_LVL DBG_INFO
  6. #include <rtdbg.h>
  7. #ifdef RT_USING_MEMTRACE
  8. int memcheck(void);
  9. #endif
  10. void luat_heap_init(void) {};
  11. void* luat_heap_malloc(size_t len){
  12. return rt_malloc(len);
  13. }
  14. void luat_heap_free(void* ptr) {
  15. #ifdef BSP_USING_WM_LIBRARIES
  16. if (ptr)
  17. RT_ASSERT(ptr >= 0x20000000 && ptr <= 0x20028000);
  18. #endif
  19. rt_free(ptr);
  20. }
  21. #ifdef BSP_USING_WM_LIBRARIES
  22. #define W600_HEAP_SIZE 64*1024
  23. void *luat_rt_realloc(void *rmem, rt_size_t newsize);
  24. void *luat_rt_free(void *rmem);
  25. void luat_rt_system_heap_init(void *begin_addr, void *end_addr);
  26. void luat_free(void);
  27. static rt_err_t w60x_memcheck() {
  28. // 首先, 把128k的内存全部设置为0
  29. void *ptr = 0x20028000;
  30. rt_memset(ptr, 0, 128*1024);
  31. luat_rt_system_heap_init(ptr, ptr + W600_HEAP_SIZE);
  32. return 0;
  33. }
  34. INIT_COMPONENT_EXPORT(w60x_memcheck);
  35. #endif
  36. void* luat_heap_alloc(void *ud, void *ptr, size_t osize, size_t nsize) {
  37. (void)ud; (void)osize; /* not used */
  38. #ifdef BSP_USING_WM_LIBRARIES
  39. if (ptr) {
  40. RT_ASSERT(ptr >= 0x20028000 && ptr <= (0x20028000 + W600_HEAP_SIZE));
  41. }
  42. if (nsize == 0) {
  43. luat_rt_free(ptr);
  44. return RT_NULL;
  45. }
  46. #ifdef RT_USING_MEMTRACE
  47. //memcheck();
  48. #endif
  49. void* ptr2 = luat_rt_realloc(ptr, nsize);
  50. if (ptr2 == RT_NULL) {
  51. //rt_kprintf("luat_heap_alloc FAIL ptr=0x%x osize=0x%x, nsize=0x%x\n", ptr, osize, nsize);
  52. //luat_free();
  53. }
  54. return ptr2;
  55. #else
  56. return rt_realloc(ptr, nsize);
  57. #endif // end of USE_CUSTOM_MEM
  58. }