api_memmgr.h 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #ifndef __API_MEMMGR_H
  2. #define __API_MEMMGR_H
  3. #include "../../bfc/dispatch.h"
  4. #include "../../bfc/platform/types.h"
  5. class NOVTABLE api_memmgr : public Dispatchable
  6. {
  7. protected:
  8. api_memmgr() {}
  9. ~api_memmgr() {}
  10. public:
  11. void *sysMalloc(size_t size);
  12. void sysFree(void *ptr);
  13. void *sysRealloc(void *ptr, size_t newsize);
  14. void sysMemChanged(void *ptr);
  15. DISPATCH_CODES
  16. {
  17. API_MEMMGR_SYSMALLOC = 0,
  18. API_MEMMGR_SYSFREE = 10,
  19. API_MEMMGR_SYSREALLOC = 20,
  20. API_MEMMGR_SYSMEMCHANGED = 30,
  21. };
  22. // Some helper templates to new and delete objects with the memory manager
  23. // you need to be cautious with Delete() and inheritance, particularly if you're dealing with a base class
  24. // as the pointer to the derived class might not equal to the pointer to the base class, particularly with multiple inheritance
  25. // e.g. class C : public A, public B {}; C c; assert((A*)&c == (B*)&c); will likely fail
  26. template <class Class>
  27. void New(Class **obj)
  28. {
  29. size_t toAlloc = sizeof(Class);
  30. void *mem = sysMalloc(toAlloc);
  31. *obj = new (mem) Class;
  32. }
  33. template <class Class>
  34. void Delete(Class *obj)
  35. {
  36. if (obj)
  37. {
  38. obj->~Class();
  39. sysFree(obj);
  40. }
  41. }
  42. };
  43. inline void *api_memmgr::sysMalloc(size_t size)
  44. {
  45. return _call(API_MEMMGR_SYSMALLOC, (void *)NULL, size);
  46. }
  47. inline void api_memmgr::sysFree(void *ptr)
  48. {
  49. _voidcall(API_MEMMGR_SYSFREE, ptr);
  50. }
  51. inline void *api_memmgr::sysRealloc(void *ptr, size_t newsize)
  52. {
  53. return _call(API_MEMMGR_SYSREALLOC, (void *)NULL, ptr, newsize);
  54. }
  55. inline void api_memmgr::sysMemChanged(void *ptr)
  56. {
  57. _voidcall(API_MEMMGR_SYSMEMCHANGED, ptr);
  58. }
  59. // {000CF46E-4DF6-4a43-BBE7-40E7A3EA02ED}
  60. static const GUID memMgrApiServiceGuid =
  61. { 0xcf46e, 0x4df6, 0x4a43, { 0xbb, 0xe7, 0x40, 0xe7, 0xa3, 0xea, 0x2, 0xed } };
  62. //extern api_memmgr *memmgrApi;
  63. #endif