memoryBase.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /**
  2. * PANDA 3D SOFTWARE
  3. * Copyright (c) Carnegie Mellon University. All rights reserved.
  4. *
  5. * All use of this software is subject to the terms of the revised BSD
  6. * license. You should have received a copy of this license along
  7. * with this source code in a file named "LICENSE."
  8. *
  9. * @file memoryBase.h
  10. * @author drose
  11. * @date 2006-11-16
  12. */
  13. #ifndef MEMORYBASE_H
  14. #define MEMORYBASE_H
  15. #include "dtoolbase.h"
  16. #include "memoryHook.h"
  17. // Place this macro within a class definition to define appropriate operator
  18. // new and delete methods that hook into the MemoryInfo class to provide
  19. // memory tracking. Of course, it is better simply to inherit from
  20. // MemoryBase; this macro is provided to resolve problems with multiple
  21. // inheritance or some such.
  22. #define ALLOC_MEMORY_BASE \
  23. inline void *operator new(size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT) { \
  24. return PANDA_MALLOC_SINGLE(size); \
  25. } \
  26. inline void *operator new(size_t size, void *ptr) { \
  27. (void) size; \
  28. return ptr; \
  29. } \
  30. inline void operator delete(void *ptr) { \
  31. PANDA_FREE_SINGLE(ptr); \
  32. } \
  33. inline void operator delete(void *, void *) { \
  34. } \
  35. inline void *operator new[](size_t size) RETURNS_ALIGNED(MEMORY_HOOK_ALIGNMENT) { \
  36. return PANDA_MALLOC_ARRAY(size); \
  37. } \
  38. inline void *operator new[](size_t size, void *ptr) { \
  39. (void) size; \
  40. return ptr; \
  41. } \
  42. inline void operator delete[](void *ptr) { \
  43. PANDA_FREE_ARRAY(ptr); \
  44. } \
  45. inline void operator delete[](void *, void *) { \
  46. }
  47. /**
  48. * This class is intended to be the base class of all objects in Panda that
  49. * might be allocated and deleted via the new and delete operators. It
  50. * redefines these operators to provide some memory tracking support.
  51. *
  52. * We used to try to override the global operator new and delete methods, but
  53. * that seems to cause problems when including header files for C++-based
  54. * system libraries (such as are found on OSX).
  55. */
  56. class EXPCL_DTOOL_DTOOLBASE MemoryBase {
  57. public:
  58. ALLOC_MEMORY_BASE;
  59. };
  60. #endif