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