wwmemlog.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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.cpp $*
  25. * *
  26. * Original Author:: Greg Hjelstrom *
  27. * *
  28. * $Author:: Jani_p $*
  29. * *
  30. * $Modtime:: 11/21/01 2:03p $*
  31. * *
  32. * $Revision:: 27 $*
  33. * *
  34. *---------------------------------------------------------------------------------------------*
  35. * Functions: *
  36. * WWMemoryLogClass::Allocate_Memory -- allocates memory *
  37. * WWMemoryLogClass::Release_Memory -- frees memory *
  38. * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
  39. #include "always.h"
  40. #include "wwmemlog.h"
  41. #include "wwdebug.h"
  42. #include "vector.h"
  43. #include "fastallocator.h"
  44. #include <windows.h>
  45. #define USE_FAST_ALLOCATOR
  46. #ifdef STEVES_NEW_CATCHER
  47. #define DISABLE_MEMLOG 1
  48. #else //STEVES_NEW_CATCHER
  49. #ifdef PARAM_EDITING_ON
  50. #define DISABLE_MEMLOG 1
  51. #else //PARAM_EDITING_ON
  52. #define DISABLE_MEMLOG 0
  53. #endif //PARAM_EDITING_ON
  54. #endif //STEVES_NEW_CATCHER*/
  55. #ifdef USE_FAST_ALLOCATOR
  56. #define ALLOC_MEMORY(n) FastAllocatorGeneral::Get_Allocator()->Alloc(n)
  57. #define FREE_MEMORY(p) FastAllocatorGeneral::Get_Allocator()->Free(p)
  58. #else
  59. #define ALLOC_MEMORY(n) ::malloc(n)
  60. #define FREE_MEMORY(p) ::free(p)
  61. #endif
  62. /*
  63. ** Enable one of the following #defines to specify which thread-sychronization
  64. ** method to use.
  65. */
  66. #define MEMLOG_USE_MUTEX 0
  67. #define MEMLOG_USE_CRITICALSECTION 1
  68. #define MEMLOG_USE_FASTCRITICALSECTION 0
  69. static unsigned AllocateCount;
  70. static unsigned FreeCount;
  71. /*
  72. ** Name for each memory category. I'm padding the array with some "undefined" strings in case
  73. ** someone forgets to set the name when adding a new category.
  74. */
  75. static char * _MemoryCategoryNames[] =
  76. {
  77. "UNKNOWN",
  78. "Geometry",
  79. "Animation",
  80. "Texture",
  81. "Pathfind",
  82. "Vis",
  83. "Sound",
  84. "CullingData",
  85. "Strings",
  86. "GameData",
  87. "PhysicsData",
  88. "W3dData",
  89. "StaticAllocations",
  90. "GameInit",
  91. "Renderer",
  92. "Network",
  93. "BINK",
  94. "<undefined>",
  95. "<undefined>",
  96. "<undefined>",
  97. "<undefined>",
  98. };
  99. /**
  100. ** MemoryCounterClass
  101. ** This object will store statistics for each memory category. It can provide things like
  102. ** the current amount of allocated memory and the peak amount of allocated memory.
  103. */
  104. class MemoryCounterClass
  105. {
  106. public:
  107. MemoryCounterClass(void) : CurrentAllocation(0), PeakAllocation(0) { }
  108. void Memory_Allocated(int size) { CurrentAllocation+=size; PeakAllocation = max(PeakAllocation,CurrentAllocation); }
  109. void Memory_Released(int size) { CurrentAllocation-=size; }
  110. int Get_Current_Allocated_Memory(void) { return CurrentAllocation; }
  111. int Get_Peak_Allocated_Memory(void) { return PeakAllocation; }
  112. protected:
  113. int CurrentAllocation;
  114. int PeakAllocation;
  115. };
  116. /**
  117. ** ActiveCategoryStackClass
  118. ** This object is used to keep track of the "active memory category". Whenever memory is allocated
  119. ** it will be charged to the current active memory category. To be thread-safe, there will be
  120. ** one ActiveCategoryStack per thread that is encountered in the program.
  121. */
  122. const int MAX_CATEGORY_STACK_DEPTH = 1024;
  123. class ActiveCategoryStackClass : public VectorClass<int>
  124. {
  125. public:
  126. ActiveCategoryStackClass(void) :
  127. VectorClass<int>(MAX_CATEGORY_STACK_DEPTH),
  128. ThreadID(-1),
  129. Count(0)
  130. { }
  131. ~ActiveCategoryStackClass(void) { WWASSERT(Count == 1); }
  132. ActiveCategoryStackClass & operator = (const ActiveCategoryStackClass & that);
  133. bool operator == (const ActiveCategoryStackClass &) { return false; }
  134. bool operator != (const ActiveCategoryStackClass &) { return true; }
  135. void Init(int thread_id) { ThreadID = thread_id; Count = 0; Push(MEM_UNKNOWN); }
  136. void Set_Thread_ID(int id) { ThreadID = id; }
  137. int Get_Thread_ID(void) { return ThreadID; }
  138. void Push(int active_category) { (*this)[Count] = active_category; Count++; }
  139. void Pop(void) { Count--; }
  140. int Current(void) { return (*this)[Count-1]; }
  141. protected:
  142. int ThreadID;
  143. int Count;
  144. };
  145. /**
  146. ** ActiveCategoryClass
  147. ** This is a dynamic vector of ActiveCategoryStackClasses which adds a new stack each time
  148. ** a new thread is encountered. It also is able to return to you the active category for
  149. ** the currently active thread automatically.
  150. */
  151. const int MAX_CATEGORY_STACKS = 256; // maximum number of threads we expect to encounter...
  152. class ActiveCategoryClass : public VectorClass<ActiveCategoryStackClass>
  153. {
  154. public:
  155. ActiveCategoryClass(void) : VectorClass<ActiveCategoryStackClass>(MAX_CATEGORY_STACKS), Count(0) { Get_Active_Stack().Push(MEM_STATICALLOCATION); }
  156. void Push(int active_category) { Get_Active_Stack().Push(active_category); }
  157. void Pop(void) { Get_Active_Stack().Pop(); }
  158. int Current(void) { return Get_Active_Stack().Current(); }
  159. protected:
  160. ActiveCategoryStackClass & Get_Active_Stack(void);
  161. int Count;
  162. };
  163. /**
  164. ** MemLogClass
  165. ** This class ties all of the logging datastructures together into a single object
  166. ** which can be created on demand when the first 'new' call is encountered.
  167. */
  168. class MemLogClass
  169. {
  170. public:
  171. int Get_Current_Allocated_Memory(int category);
  172. int Get_Peak_Allocated_Memory(int category);
  173. /*
  174. ** Interface for recording allocations and de-allocations
  175. */
  176. int Register_Memory_Allocated(int size);
  177. void Register_Memory_Released(int category,int size);
  178. void Push_Active_Category(int category);
  179. void Pop_Active_Category(void);
  180. void Init();
  181. private:
  182. MemoryCounterClass _MemoryCounters[MEM_COUNT];
  183. ActiveCategoryClass _ActiveCategoryTracker;
  184. };
  185. /**
  186. ** Static Variables
  187. ** _TheMemLog - object which encapsulates all logging. will be allocated on first use
  188. ** _MemLogMutex - handle to the mutex used to arbtirate access to the logging data structures
  189. ** _MemLogLockCounter - count of the active mutex locks.
  190. */
  191. static MemLogClass * _TheMemLog = NULL;
  192. static bool _MemLogAllocated = false;
  193. #if MEMLOG_USE_MUTEX
  194. static void * _MemLogMutex = NULL;
  195. static int _MemLogLockCounter = 0;
  196. #endif
  197. #if MEMLOG_USE_CRITICALSECTION
  198. static bool _MemLogCriticalSectionAllocated = false;
  199. static char _MemLogCriticalSectionHandle[sizeof(CRITICAL_SECTION)];
  200. #endif
  201. #if MEMLOG_USE_FASTCRITICALSECTION
  202. volatile unsigned _MemLogSemaphore = 0;
  203. #endif
  204. /*
  205. ** Use this code to get access to the mutex...
  206. */
  207. WWINLINE void * Get_Mem_Log_Mutex(void)
  208. {
  209. #if MEMLOG_USE_MUTEX
  210. if (_MemLogMutex == NULL) {
  211. _MemLogMutex=CreateMutex(NULL,false,NULL);
  212. WWASSERT(_MemLogMutex);
  213. }
  214. return _MemLogMutex;
  215. #endif
  216. #if MEMLOG_USE_CRITICALSECTION
  217. if (_MemLogCriticalSectionAllocated == false) {
  218. InitializeCriticalSection((CRITICAL_SECTION*)_MemLogCriticalSectionHandle);
  219. _MemLogCriticalSectionAllocated = true;
  220. }
  221. return _MemLogCriticalSectionHandle;
  222. #endif
  223. }
  224. WWINLINE void Lock_Mem_Log_Mutex(void)
  225. {
  226. #if MEMLOG_USE_MUTEX
  227. void * mutex = Get_Mem_Log_Mutex();
  228. #ifdef WWDEBUG
  229. int res =
  230. #endif
  231. WaitForSingleObject(mutex,INFINITE);
  232. WWASSERT(res==WAIT_OBJECT_0);
  233. _MemLogLockCounter++;
  234. #endif
  235. #if MEMLOG_USE_CRITICALSECTION
  236. Get_Mem_Log_Mutex();
  237. EnterCriticalSection((CRITICAL_SECTION*)_MemLogCriticalSectionHandle);
  238. #endif
  239. #if MEMLOG_USE_FASTCRITICALSECTION
  240. volatile unsigned& nFlag=_MemLogSemaphore;
  241. #define ts_lock _emit 0xF0
  242. assert(((unsigned)&nFlag % 4) == 0);
  243. __asm mov ebx, [nFlag]
  244. __asm ts_lock
  245. __asm bts dword ptr [ebx], 0
  246. __asm jc The_Bit_Was_Previously_Set_So_Try_Again
  247. return;
  248. The_Bit_Was_Previously_Set_So_Try_Again:
  249. ThreadClass::Switch_Thread();
  250. __asm mov ebx, [nFlag]
  251. __asm ts_lock
  252. __asm bts dword ptr [ebx], 0
  253. __asm jc The_Bit_Was_Previously_Set_So_Try_Again
  254. #endif
  255. }
  256. WWINLINE void Unlock_Mem_Log_Mutex(void)
  257. {
  258. #if MEMLOG_USE_MUTEX
  259. void * mutex = Get_Mem_Log_Mutex();
  260. _MemLogLockCounter--;
  261. #ifdef WWDEBUG
  262. int res=
  263. #endif
  264. ReleaseMutex(mutex);
  265. WWASSERT(res);
  266. #endif
  267. #if MEMLOG_USE_CRITICALSECTION
  268. Get_Mem_Log_Mutex();
  269. LeaveCriticalSection((CRITICAL_SECTION*)_MemLogCriticalSectionHandle);
  270. #endif
  271. #if MEMLOG_USE_FASTCRITICALSECTION
  272. _MemLogSemaphore = 0;
  273. #endif
  274. }
  275. class MemLogMutexLockClass
  276. {
  277. public:
  278. MemLogMutexLockClass(void) { Lock_Mem_Log_Mutex(); }
  279. ~MemLogMutexLockClass(void) { Unlock_Mem_Log_Mutex(); }
  280. };
  281. /***************************************************************************************************
  282. **
  283. ** ActiveCategoryStackClass Implementation
  284. **
  285. ***************************************************************************************************/
  286. ActiveCategoryStackClass &
  287. ActiveCategoryStackClass::operator = (const ActiveCategoryStackClass & that)
  288. {
  289. if (this != &that) {
  290. VectorClass<int>::operator == (that);
  291. ThreadID = that.ThreadID;
  292. Count = that.Count;
  293. }
  294. return *this;
  295. }
  296. /***************************************************************************************************
  297. **
  298. ** ActiveCategoryClass Implementation
  299. **
  300. ***************************************************************************************************/
  301. ActiveCategoryStackClass & ActiveCategoryClass::Get_Active_Stack(void)
  302. {
  303. int current_thread = ::GetCurrentThreadId();
  304. /*
  305. ** If we already have an allocated category stack for the current thread,
  306. ** just return its active category.
  307. */
  308. for (int i=0; i<Count; i++) {
  309. ActiveCategoryStackClass & cat_stack = (*this)[i];
  310. if (cat_stack.Get_Thread_ID() == current_thread) {
  311. return cat_stack;
  312. }
  313. }
  314. /*
  315. ** If we fall through to here, we need to allocate a new category stack
  316. ** for this thread.
  317. */
  318. (*this)[Count].Init(current_thread);
  319. Count++;
  320. return (*this)[Count-1];
  321. }
  322. /***************************************************************************************************
  323. **
  324. ** MemLogClass Implementation
  325. **
  326. ***************************************************************************************************/
  327. int MemLogClass::Get_Current_Allocated_Memory(int category)
  328. {
  329. MemLogMutexLockClass lock;
  330. return _MemoryCounters[category].Get_Current_Allocated_Memory();
  331. }
  332. int MemLogClass::Get_Peak_Allocated_Memory(int category)
  333. {
  334. MemLogMutexLockClass lock;
  335. return _MemoryCounters[category].Get_Peak_Allocated_Memory();
  336. }
  337. void MemLogClass::Init()
  338. {
  339. {
  340. MemLogMutexLockClass lock;
  341. WWASSERT(_ActiveCategoryTracker.Current()==MEM_STATICALLOCATION);
  342. }
  343. Pop_Active_Category(); // Remove staticallocation state forever
  344. }
  345. int MemLogClass::Register_Memory_Allocated(int size)
  346. {
  347. MemLogMutexLockClass lock;
  348. int active_category = _ActiveCategoryTracker.Current();
  349. WWASSERT((active_category >= 0) && (active_category < MEM_COUNT));
  350. _MemoryCounters[active_category].Memory_Allocated(size);
  351. return active_category;
  352. }
  353. void MemLogClass::Register_Memory_Released(int category,int size)
  354. {
  355. MemLogMutexLockClass lock;
  356. _MemoryCounters[category].Memory_Released(size);
  357. }
  358. void MemLogClass::Push_Active_Category(int category)
  359. {
  360. MemLogMutexLockClass lock;
  361. WWASSERT((category >= 0) && (category < MEM_COUNT));
  362. _ActiveCategoryTracker.Push(category);
  363. }
  364. void MemLogClass::Pop_Active_Category(void)
  365. {
  366. MemLogMutexLockClass lock;
  367. _ActiveCategoryTracker.Pop();
  368. }
  369. /***************************************************************************************************
  370. **
  371. ** WWMemoryLogClass Implementation
  372. **
  373. ***************************************************************************************************/
  374. int WWMemoryLogClass::Get_Category_Count(void)
  375. {
  376. return MEM_COUNT;
  377. }
  378. const char * WWMemoryLogClass::Get_Category_Name(int category)
  379. {
  380. return _MemoryCategoryNames[category];
  381. }
  382. int WWMemoryLogClass::Get_Current_Allocated_Memory(int category)
  383. {
  384. return Get_Log()->Get_Current_Allocated_Memory(category);
  385. }
  386. int WWMemoryLogClass::Get_Peak_Allocated_Memory(int category)
  387. {
  388. return Get_Log()->Get_Peak_Allocated_Memory(category);
  389. }
  390. void WWMemoryLogClass::Push_Active_Category(int category)
  391. {
  392. #if (DISABLE_MEMLOG == 0)
  393. Get_Log()->Push_Active_Category(category);
  394. #endif //(DISABLE_MEMLOG == 0)
  395. }
  396. void WWMemoryLogClass::Pop_Active_Category(void)
  397. {
  398. #if (DISABLE_MEMLOG == 0)
  399. Get_Log()->Pop_Active_Category();
  400. #endif //(DISABLE_MEMLOG == 0)
  401. }
  402. int WWMemoryLogClass::Register_Memory_Allocated(int size)
  403. {
  404. return Get_Log()->Register_Memory_Allocated(size);
  405. }
  406. void WWMemoryLogClass::Register_Memory_Released(int category,int size)
  407. {
  408. Get_Log()->Register_Memory_Released(category,size);
  409. }
  410. static void __cdecl _MemLogCleanup(void)
  411. {
  412. delete _TheMemLog;
  413. }
  414. MemLogClass * WWMemoryLogClass::Get_Log(void)
  415. {
  416. MemLogMutexLockClass lock;
  417. if (_TheMemLog == NULL) {
  418. //assert(!_MemLogAllocated);
  419. _TheMemLog = new MemLogClass;
  420. #ifdef STEVES_NEW_CATCHER
  421. /*
  422. ** This was me trying to be clever and fix the memory leak in the memlog. Unfortunately, the Get_Log member can be called
  423. ** during the process of exiting the process (IYSWIM) and you get it trying to re-allocate the MemLogClass I just freed.
  424. ** Solution is just to disable memlog when I'm trying to find memory leaks. ST - 6/18/2001 9:51PM
  425. */
  426. if (!_MemLogAllocated) {
  427. atexit(&Release_Log);
  428. }
  429. _MemLogAllocated = true;
  430. #endif //STEVES_NEW_CATCHER
  431. }
  432. return _TheMemLog;
  433. }
  434. /***********************************************************************************************
  435. * WWMemoryLogClass::Release_Log -- Free the memory used by WWMemoryLogClass so it doesn't leak*
  436. * *
  437. * *
  438. * *
  439. * INPUT: Nothing *
  440. * *
  441. * OUTPUT: Nothing *
  442. * *
  443. * WARNINGS: Called as part of _onexit processing *
  444. * *
  445. * It's messy, but I assume there's a reason it's not statically allocated... *
  446. * OK, now I get it *
  447. * *
  448. * HISTORY: *
  449. * 6/13/2001 8:55PM ST : Created *
  450. *=============================================================================================*/
  451. void __cdecl WWMemoryLogClass::Release_Log(void)
  452. {
  453. MemLogMutexLockClass lock;
  454. if (_TheMemLog) {
  455. delete _TheMemLog;
  456. _TheMemLog = NULL;
  457. }
  458. }
  459. /***************************************************************************************************
  460. **
  461. ** Allocating and Freeing memory
  462. **
  463. ** PLEASE NOTE: The user is expected to implement global new and delete functions in his own
  464. ** code which call WWMemoryLogClass::Allocate_Memory and WWMemoryLogClass::Release_Memory.
  465. ** This was the only solution I could come up given that some APPS have their own new and delete
  466. ** functions or enable the CRT ones. It was also not an option to move this entire system into
  467. ** the APP because I wanted all of our LIBs to participate in the memory usage logging...
  468. **
  469. ***************************************************************************************************/
  470. const int WWMEMLOG_KEY0 = (unsigned('G')<<24) | (unsigned('g')<<16) | (unsigned('0')<<8) | unsigned('l');
  471. const int WWMEMLOG_KEY1 = (unsigned('~')<<24) | (unsigned('_')<<16) | (unsigned('d')<<8) | unsigned('3');
  472. /**
  473. ** MemoryLogStruct
  474. ** This structure is added to the beginning of each memory allocation to facilitate
  475. ** tracking which category the memory belongs to when it is freed. The size of
  476. ** this struct is also 16 bytes so that we wont be seriously affecting the alignment
  477. ** of allocated memory...
  478. */
  479. struct MemoryLogStruct
  480. {
  481. MemoryLogStruct(int category,int size) :
  482. Key0(WWMEMLOG_KEY0),
  483. Key1(WWMEMLOG_KEY1),
  484. Category(category),
  485. Size(size)
  486. {}
  487. bool Is_Valid_Memory_Log(void) { return ((Key0 == WWMEMLOG_KEY0) && (Key1 == WWMEMLOG_KEY1)); }
  488. int Key0; // if this is not equal to WWMEMLOG_KEY0 then we don't have a valid log
  489. int Key1; // should be equal to WWMEMLOG_KEY1
  490. int Category; // category this memory belongs to
  491. int Size; // size of the allocation
  492. };
  493. /***********************************************************************************************
  494. * WWMemoryLogClass::Allocate_Memory -- allocates memory *
  495. * *
  496. * This function adds a header to the memory allocated so that when the memory is freed *
  497. * the proper memory category size can be decremented. The application using this logging *
  498. * system should call this function from inside its overloaded 'new' operator. *
  499. * *
  500. * INPUT: *
  501. * *
  502. * OUTPUT: *
  503. * *
  504. * WARNINGS: *
  505. * *
  506. * HISTORY: *
  507. * 5/29/2001 gth : Created. *
  508. *=============================================================================================*/
  509. void * WWMemoryLogClass::Allocate_Memory(size_t size)
  510. {
  511. #if DISABLE_MEMLOG
  512. AllocateCount++;
  513. return ALLOC_MEMORY(size);
  514. #else
  515. __declspec( thread ) static bool reentrancy_test = false;
  516. MemLogMutexLockClass lock;
  517. if (reentrancy_test) {
  518. return ALLOC_MEMORY(size);
  519. } else {
  520. reentrancy_test = true;
  521. /*
  522. ** Allocate space for the requested buffer + our logging structure
  523. */
  524. void * ptr = ALLOC_MEMORY(size + sizeof(MemoryLogStruct));
  525. if (ptr != NULL) {
  526. /*
  527. ** Record this allocation
  528. */
  529. int active_category = WWMemoryLogClass::Register_Memory_Allocated(size);
  530. /*
  531. ** Write our logging structure into the beginning of the buffer. I'm using
  532. ** placement new syntax to initialize the log structure right in the memory buffer
  533. */
  534. new(ptr) MemoryLogStruct(active_category,size);
  535. /*
  536. ** Return the allocated memory to the user, skipping past our log structure.
  537. */
  538. reentrancy_test = false;
  539. return (void*)(((char *)ptr) + sizeof(MemoryLogStruct));
  540. } else {
  541. reentrancy_test = false;
  542. return ptr;
  543. }
  544. }
  545. #endif //DISABLE_MEMLOG
  546. }
  547. /***********************************************************************************************
  548. * WWMemoryLogClass::Release_Memory -- frees memory *
  549. * *
  550. * This function checks for a wwmemlog header and decrements the relevant memory category. *
  551. * It should be called in the application's custom delete operator. *
  552. * *
  553. * INPUT: *
  554. * *
  555. * OUTPUT: *
  556. * *
  557. * WARNINGS: *
  558. * *
  559. * HISTORY: *
  560. * 5/29/2001 gth : Created. *
  561. *=============================================================================================*/
  562. void WWMemoryLogClass::Release_Memory(void *ptr)
  563. {
  564. #if DISABLE_MEMLOG
  565. FREE_MEMORY(ptr);
  566. FreeCount++;
  567. #else
  568. MemLogMutexLockClass lock;
  569. if (ptr) {
  570. /*
  571. ** Check if this memory is preceeded by a valid MemoryLogStruct
  572. */
  573. MemoryLogStruct * memlog = (MemoryLogStruct*)((char*)ptr - sizeof(MemoryLogStruct));
  574. if (memlog->Is_Valid_Memory_Log()) {
  575. /*
  576. ** Valid MemoryLogStruct found, track the de-allocation and pass on
  577. ** to the built-in free function.
  578. */
  579. WWMemoryLogClass::Register_Memory_Released(memlog->Category,memlog->Size);
  580. FREE_MEMORY((void*)memlog);
  581. } else {
  582. /*
  583. ** No valid MemoryLogStruct found, just call free on the memory.
  584. */
  585. FREE_MEMORY(ptr);
  586. }
  587. }
  588. #endif //DISABLE_MEMLOG
  589. }
  590. // Reset allocate and free counters
  591. void WWMemoryLogClass::Reset_Counters()
  592. {
  593. AllocateCount=0;
  594. FreeCount=0;
  595. }
  596. // Return allocate count since last reset
  597. int WWMemoryLogClass::Get_Allocate_Count()
  598. {
  599. return AllocateCount;
  600. }
  601. // Return allocate count since last reset
  602. int WWMemoryLogClass::Get_Free_Count()
  603. {
  604. return FreeCount;
  605. }
  606. void WWMemoryLogClass::Init()
  607. {
  608. Get_Log()->Init();
  609. }