wwmemlog.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /***********************************************************************************************
  19. *** C O N F I D E N T I A L --- W E S T W O O D S T U D I O S ***
  20. ***********************************************************************************************
  21. * *
  22. * Project Name : WWDebug *
  23. * *
  24. * $Archive:: /Commando/Code/wwdebug/wwmemlog.h $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 11/09/01 6:51p $*
  31. * *
  32. * $Revision:: 8 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  37. #if _MSC_VER >= 1000
  38. #pragma once
  39. #endif
  40. #ifndef WWMEMLOG_H
  41. #define WWMEMLOG_H
  42. class MemLogClass;
  43. /**
  44. ** Memory Log Categories
  45. ** You can cause memory allocations to be "counted" against any of the following categories.
  46. ** NOTE: if you add a new category here, be sure to add its name to the array in the .cpp file...
  47. */
  48. enum
  49. {
  50. MEM_UNKNOWN = 0,
  51. MEM_GEOMETRY, // memory used by geometry data
  52. MEM_ANIMATION, // memory used by animation data
  53. MEM_TEXTURE, // memory used by textures
  54. MEM_PATHFIND, // memory used by the pathfind system
  55. MEM_VIS, // memory used by the vis system
  56. MEM_SOUND, // memory used by the sound system
  57. MEM_CULLINGDATA, // culling systems
  58. MEM_STRINGS, // string data
  59. MEM_GAMEDATA, // game engine datastructures
  60. MEM_PHYSICSDATA, // physics engine datastructures
  61. MEM_W3DDATA, // w3d datastructures (not including ones more applicable to above categories)
  62. MEM_STATICALLOCATION,// all the allocations that happen before the memlog Init() function call are from statically allocated objects
  63. MEM_GAMEINIT, // game init time allocations
  64. MEM_RENDERER, // dx8 renderer
  65. MEM_NETWORK,
  66. MEM_BINK,
  67. MEM_COUNT
  68. };
  69. /**
  70. ** WWMemoryLogClass
  71. ** This interface can provide information on how much memory has been allocated to each
  72. ** memory category. In order to enable this logging, you will need to implement global
  73. ** new and delete functions which call the Allocate_Memory and Release_Memory functions
  74. ** in this class. For example:
  75. **
  76. ** void * ::operator new (size_t size)
  77. ** {
  78. ** return WWMemoryLogClass::Allocate_Memory(size);
  79. ** }
  80. **
  81. ** void ::operator delete (void *ptr)
  82. ** {
  83. ** WWMemoryLogClass::Release_Memory(ptr);
  84. ** }
  85. */
  86. class WWMemoryLogClass
  87. {
  88. public:
  89. /*
  90. ** Accessors to the current memory map
  91. */
  92. static int Get_Category_Count(void);
  93. static const char * Get_Category_Name(int category);
  94. static int Get_Current_Allocated_Memory(int category);
  95. static int Get_Peak_Allocated_Memory(int category);
  96. /*
  97. ** Interface for the debug version of new and delete
  98. */
  99. static int Register_Memory_Allocated(int size);
  100. static void Register_Memory_Released(int category,int size);
  101. /*
  102. ** New and Delete functions. If you want to use this logging system,
  103. ** implement global new and delete functions which call into these
  104. ** functions.
  105. */
  106. static void * Allocate_Memory(size_t size);
  107. static void Release_Memory(void * mem);
  108. static void Reset_Counters(); // Reset allocate and free counters
  109. static int Get_Allocate_Count(); // Return allocate count since last reset
  110. static int Get_Free_Count(); // Return allocate count since last reset
  111. static void Init();
  112. protected:
  113. /*
  114. ** Interface for WWMemorySampleClass to set the active category
  115. */
  116. static void Push_Active_Category(int category);
  117. static void Pop_Active_Category(void);
  118. static MemLogClass * Get_Log(void);
  119. static void Release_Log(void);
  120. friend class WWMemorySampleClass;
  121. };
  122. /**
  123. ** WWMemorySampleClass
  124. ** This class is meant to be created and destroyed on the stack to automatically push
  125. ** and pop the desired memory category. NOTE: this class should not be used directly,
  126. ** instead, use the WWMEMLOG macros!
  127. */
  128. class WWMemorySampleClass
  129. {
  130. public:
  131. WWMemorySampleClass(int category) { WWMemoryLogClass::Push_Active_Category(category); }
  132. ~WWMemorySampleClass(void) { WWMemoryLogClass::Pop_Active_Category(); }
  133. };
  134. /*
  135. ** Use the WWMEMLOG macro to track all memory allocations within the current scope.
  136. ** If WWDEBUG is not enabled, memory usage logging will be disabled.
  137. */
  138. #ifdef WWDEBUG
  139. #define WWMEMLOG( category ) WWMemorySampleClass _memsample( category )
  140. #else
  141. #define WWMEMLOG( category )
  142. #endif
  143. #endif //WWMEMLOG_H