wwmemlog.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. ** Command & Conquer Generals(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:: 8/23/01 11:46a $*
  31. * *
  32. * $Revision:: 6 $*
  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_COUNT
  63. };
  64. /**
  65. ** WWMemoryLogClass
  66. ** This interface can provide information on how much memory has been allocated to each
  67. ** memory category. In order to enable this logging, you will need to implement global
  68. ** new and delete functions which call the Allocate_Memory and Release_Memory functions
  69. ** in this class. For example:
  70. **
  71. ** void * ::operator new (size_t size)
  72. ** {
  73. ** return WWMemoryLogClass::Allocate_Memory(size);
  74. ** }
  75. **
  76. ** void ::operator delete (void *ptr)
  77. ** {
  78. ** WWMemoryLogClass::Release_Memory(ptr);
  79. ** }
  80. */
  81. class WWMemoryLogClass
  82. {
  83. public:
  84. /*
  85. ** Accessors to the current memory map
  86. */
  87. static int Get_Category_Count(void);
  88. static const char * Get_Category_Name(int category);
  89. static int Get_Current_Allocated_Memory(int category);
  90. static int Get_Peak_Allocated_Memory(int category);
  91. /*
  92. ** Interface for the debug version of new and delete
  93. */
  94. static int Register_Memory_Allocated(int size);
  95. static void Register_Memory_Released(int category,int size);
  96. /*
  97. ** New and Delete functions. If you want to use this logging system,
  98. ** implement global new and delete functions which call into these
  99. ** functions.
  100. */
  101. static void * Allocate_Memory(size_t size);
  102. static void Release_Memory(void * mem);
  103. static void Reset_Counters(); // Reset allocate and free counters
  104. static int Get_Allocate_Count(); // Return allocate count since last reset
  105. static int Get_Free_Count(); // Return allocate count since last reset
  106. protected:
  107. /*
  108. ** Interface for WWMemorySampleClass to set the active category
  109. */
  110. static void Push_Active_Category(int category);
  111. static void Pop_Active_Category(void);
  112. static MemLogClass * Get_Log(void);
  113. static void __cdecl Release_Log(void);
  114. friend class WWMemorySampleClass;
  115. };
  116. /**
  117. ** WWMemorySampleClass
  118. ** This class is meant to be created and destroyed on the stack to automatically push
  119. ** and pop the desired memory category. NOTE: this class should not be used directly,
  120. ** instead, use the WWMEMLOG macros!
  121. */
  122. class WWMemorySampleClass
  123. {
  124. public:
  125. WWMemorySampleClass(int category) { WWMemoryLogClass::Push_Active_Category(category); }
  126. ~WWMemorySampleClass(void) { WWMemoryLogClass::Pop_Active_Category(); }
  127. };
  128. /*
  129. ** Use the WWMEMLOG macro to track all memory allocations within the current scope.
  130. ** If WWDEBUG is not enabled, memory usage logging will be disabled.
  131. */
  132. #ifdef WWDEBUG
  133. #define WWMEMLOG( category ) WWMemorySampleClass _memsample( category )
  134. #else
  135. #define WWMEMLOG( category )
  136. #endif
  137. #endif //WWMEMLOG_H