MemoryManager.cpp 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /** (c) Nullsoft, Inc. C O N F I D E N T I A L
  2. ** Filename:
  3. ** Project:
  4. ** Description:
  5. ** Author:
  6. ** Created:
  7. **/
  8. #include "main.h"
  9. #include "MemoryManager.h"
  10. #include "api.h"
  11. #include <api/syscb/callbacks/syscb.h>
  12. #include <api/syscb/callbacks/sysmemcb.h>
  13. void *MemoryManager::Malloc(size_t size)
  14. {
  15. if (size <= 0) return NULL;
  16. void *ret = calloc(size, 1);
  17. if (ret != NULL)
  18. WASABI_API_SYSCB->syscb_issueCallback(SysCallback::SYSMEM, SysMemCallback::ONMALLOC, (intptr_t)ret, size);
  19. return ret;
  20. }
  21. void MemoryManager::Free(void *ptr)
  22. {
  23. if (ptr != NULL)
  24. WASABI_API_SYSCB->syscb_issueCallback(SysCallback::SYSMEM, SysMemCallback::ONFREE, (intptr_t)ptr);
  25. free(ptr);
  26. }
  27. void *MemoryManager::Realloc(void *ptr, size_t newsize)
  28. {
  29. void *ptrs[2] = {ptr, 0};
  30. void *new_memory = realloc(ptr, newsize);
  31. if (new_memory)
  32. ptrs[1] = new_memory;
  33. else
  34. {
  35. new_memory = calloc(newsize, 1);
  36. if (new_memory)
  37. {
  38. memcpy(new_memory, ptr, _msize(ptr));
  39. ptrs[1] = new_memory;
  40. }
  41. }
  42. if (!(ptrs[0] == NULL && ptrs[1] == NULL))
  43. WASABI_API_SYSCB->syscb_issueCallback(SysCallback::SYSMEM, SysMemCallback::ONREALLOC, (intptr_t)ptrs, newsize);
  44. return new_memory;
  45. }
  46. void MemoryManager::MemChanged(void *ptr)
  47. {
  48. if (ptr == NULL) return ;
  49. WASABI_API_SYSCB->syscb_issueCallback(SysCallback::SYSMEM, SysMemCallback::ONCHANGE, (intptr_t)ptr);
  50. }
  51. #ifdef CBCLASS
  52. #undef CBCLASS
  53. #endif
  54. #define CBCLASS MemoryManager
  55. START_DISPATCH;
  56. CB(API_MEMMGR_SYSMALLOC, Malloc);
  57. VCB(API_MEMMGR_SYSFREE, Free);
  58. CB(API_MEMMGR_SYSREALLOC, Realloc);
  59. VCB(API_MEMMGR_SYSMEMCHANGED, MemChanged);
  60. END_DISPATCH;