wwmemlog.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. #define LOG_MEMORY // Comment this out to disable memlog compiling in
  43. class MemLogClass;
  44. /**
  45. ** Memory Log Categories
  46. ** You can cause memory allocations to be "counted" against any of the following categories.
  47. ** NOTE: if you add a new category here, be sure to add its name to the array in the .cpp file...
  48. */
  49. enum
  50. {
  51. MEM_UNKNOWN = 0,
  52. MEM_GEOMETRY, // memory used by geometry data
  53. MEM_ANIMATION, // memory used by animation data
  54. MEM_TEXTURE, // memory used by textures
  55. MEM_PATHFIND, // memory used by the pathfind system
  56. MEM_VIS, // memory used by the vis system
  57. MEM_SOUND, // memory used by the sound system
  58. MEM_CULLINGDATA, // culling systems
  59. MEM_STRINGS, // string data
  60. MEM_GAMEDATA, // game engine datastructures
  61. MEM_PHYSICSDATA, // physics engine datastructures
  62. MEM_W3DDATA, // w3d datastructures (not including ones more applicable to above categories)
  63. MEM_STATICALLOCATION,// all the allocations that happen before the memlog Init() function call are from statically allocated objects
  64. MEM_GAMEINIT, // game init time allocations
  65. MEM_RENDERER, // dx8 renderer
  66. MEM_NETWORK,
  67. MEM_BINK,
  68. MEM_COUNT
  69. };
  70. /**
  71. ** WWMemoryLogClass
  72. ** This interface can provide information on how much memory has been allocated to each
  73. ** memory category. In order to enable this logging, you will need to implement global
  74. ** new and delete functions which call the Allocate_Memory and Release_Memory functions
  75. ** in this class. For example:
  76. **
  77. ** void * ::operator new (size_t size)
  78. ** {
  79. ** return WWMemoryLogClass::Allocate_Memory(size);
  80. ** }
  81. **
  82. ** void ::operator delete (void *ptr)
  83. ** {
  84. ** WWMemoryLogClass::Release_Memory(ptr);
  85. ** }
  86. */
  87. class WWMemoryLogClass
  88. {
  89. public:
  90. static void Enable_Memory_Log(bool enable) { IsMemoryLogEnabled=enable; }
  91. static bool Is_Memory_Log_Enabled() { return IsMemoryLogEnabled; }
  92. /*
  93. ** Accessors to the current memory map
  94. */
  95. static int Get_Category_Count(void);
  96. static const char * Get_Category_Name(int category);
  97. static int Get_Current_Allocated_Memory(int category);
  98. static int Get_Peak_Allocated_Memory(int category);
  99. /*
  100. ** Interface for the debug version of new and delete
  101. */
  102. static int Register_Memory_Allocated(int size);
  103. static void Register_Memory_Released(int category,int size);
  104. /*
  105. ** New and Delete functions. If you want to use this logging system,
  106. ** implement global new and delete functions which call into these
  107. ** functions.
  108. */
  109. static void * Allocate_Memory(size_t size);
  110. static void Release_Memory(void * mem);
  111. static void Reset_Counters(); // Reset allocate and free counters
  112. static int Get_Allocate_Count(); // Return allocate count since last reset
  113. static int Get_Free_Count(); // Return allocate count since last reset
  114. static void Init();
  115. protected:
  116. /*
  117. ** Interface for WWMemorySampleClass to set the active category
  118. */
  119. static void Push_Active_Category(int category);
  120. static void Pop_Active_Category(void);
  121. static MemLogClass * Get_Log(void);
  122. static void Release_Log(void);
  123. static bool IsMemoryLogEnabled;
  124. friend class WWMemorySampleClass;
  125. };
  126. /**
  127. ** WWMemorySampleClass
  128. ** This class is meant to be created and destroyed on the stack to automatically push
  129. ** and pop the desired memory category. NOTE: this class should not be used directly,
  130. ** instead, use the WWMEMLOG macros!
  131. */
  132. class WWMemorySampleClass
  133. {
  134. bool category_push;
  135. public:
  136. WWMemorySampleClass(int category) : category_push(WWMemoryLogClass::Is_Memory_Log_Enabled())
  137. {
  138. if (category_push) {
  139. WWMemoryLogClass::Push_Active_Category(category);
  140. }
  141. }
  142. ~WWMemorySampleClass(void)
  143. {
  144. if (category_push) {
  145. WWMemoryLogClass::Pop_Active_Category();
  146. }
  147. }
  148. };
  149. /*
  150. ** Use the WWMEMLOG macro to track all memory allocations within the current scope.
  151. ** If WWDEBUG is not enabled, memory usage logging will be disabled.
  152. */
  153. #ifdef USE_MEMLOG
  154. #define WWMEMLOG( category ) WWMemorySampleClass _memsample( category )
  155. #else
  156. #define WWMEMLOG( category )
  157. #endif
  158. #endif //WWMEMLOG_H