48#define USE_FAST_ALLOCATOR
50#ifdef STEVES_NEW_CATCHER
51 #define DISABLE_MEMLOG 1
53#ifdef PARAM_EDITING_ON
54 #define DISABLE_MEMLOG 1
56 #define DISABLE_MEMLOG 1
60#ifdef USE_FAST_ALLOCATOR
61 #define ALLOC_MEMORY(n) FastAllocatorGeneral::Get_Allocator()->Alloc(n)
62 #define FREE_MEMORY(p) FastAllocatorGeneral::Get_Allocator()->Free(p)
64 #define ALLOC_MEMORY(n) ::malloc(n)
65 #define FREE_MEMORY(p) ::free(p)
74#define MEMLOG_USE_MUTEX 0
75#define MEMLOG_USE_CRITICALSECTION 1
76#define MEMLOG_USE_FASTCRITICALSECTION 0
79#define DISABLE_MEMLOG 1
80#define MEMLOG_USE_MUTEX 0
81#define MEMLOG_USE_CRITICALSECTION 0
82#define MEMLOG_USE_FASTCRITICALSECTION 0
85#if (DISABLE_MEMLOG == 0)
91static unsigned AllocateCount;
92static unsigned FreeCount;
98static char * _MemoryCategoryNames[] =
252static bool _MemLogAllocated =
false;
255static void * _MemLogMutex =
NULL;
256static int _MemLogLockCounter = 0;
259#if MEMLOG_USE_CRITICALSECTION
260static bool _MemLogCriticalSectionAllocated =
false;
261static char _MemLogCriticalSectionHandle[
sizeof(CRITICAL_SECTION)];
264#if MEMLOG_USE_FASTCRITICALSECTION
265volatile unsigned _MemLogSemaphore = 0;
275 if (_MemLogMutex ==
NULL) {
276 _MemLogMutex=CreateMutex(
NULL,
false,
NULL);
283#if MEMLOG_USE_CRITICALSECTION
285 if (_MemLogCriticalSectionAllocated ==
false) {
286 InitializeCriticalSection((CRITICAL_SECTION*)_MemLogCriticalSectionHandle);
287 _MemLogCriticalSectionAllocated =
true;
289 return _MemLogCriticalSectionHandle;
302 WaitForSingleObject(mutex,INFINITE);
304 _MemLogLockCounter++;
307#if MEMLOG_USE_CRITICALSECTION
310 EnterCriticalSection((CRITICAL_SECTION*)_MemLogCriticalSectionHandle);
314#if MEMLOG_USE_FASTCRITICALSECTION
316 volatile unsigned& nFlag=_MemLogSemaphore;
318 #define ts_lock _emit 0xF0
319 assert(((
unsigned)&nFlag % 4) == 0);
321 __asm mov ebx, [nFlag]
323 __asm bts dword ptr [ebx], 0
324 __asm jc The_Bit_Was_Previously_Set_So_Try_Again
327 The_Bit_Was_Previously_Set_So_Try_Again:
329 __asm mov ebx, [nFlag]
331 __asm bts dword ptr [ebx], 0
332 __asm jc The_Bit_Was_Previously_Set_So_Try_Again
342 _MemLogLockCounter--;
350#if MEMLOG_USE_CRITICALSECTION
353 LeaveCriticalSection((CRITICAL_SECTION*)_MemLogCriticalSectionHandle);
357#if MEMLOG_USE_FASTCRITICALSECTION
358 _MemLogSemaphore = 0;
395 int current_thread = ::GetCurrentThreadId();
401 for (
int i=0; i<
Count; i++) {
412 (*this)[
Count].Init(current_thread);
414 return (*
this)[
Count-1];
426 return _MemoryCounters[category].Get_Current_Allocated_Memory();
432 return _MemoryCounters[category].Get_Peak_Allocated_Memory();
448 int active_category = _ActiveCategoryTracker.Current();
450 _MemoryCounters[active_category].Memory_Allocated(size);
452 return active_category;
458 _MemoryCounters[category].Memory_Released(size);
465 _ActiveCategoryTracker.Push(category);
471 _ActiveCategoryTracker.Pop();
489 return _MemoryCategoryNames[category];
504#if (DISABLE_MEMLOG == 0)
511#if (DISABLE_MEMLOG == 0)
527static void _MemLogCleanup(
void)
537 if (_TheMemLog ==
NULL) {
541#ifdef STEVES_NEW_CATCHER
547 if (!_MemLogAllocated) {
550 _MemLogAllocated =
true;
596const int WWMEMLOG_KEY0 = (unsigned(
'G')<<24) | (
unsigned(
'g')<<16) | (unsigned(
'0')<<8) |
unsigned(
'l');
597const int WWMEMLOG_KEY1 = (unsigned(
'~')<<24) | (
unsigned(
'_')<<16) | (unsigned(
'd')<<8) |
unsigned(
'3');
649 __declspec( thread )
static bool reentrancy_test =
false;
652 if (reentrancy_test) {
655 reentrancy_test =
true;
677 reentrancy_test =
false;
681 reentrancy_test =
false;
751 return AllocateCount;
void __declspec(dllexport) CreateDebugDialog(void)
ActiveCategoryClass(void)
ActiveCategoryStackClass & Get_Active_Stack(void)
void Push(int active_category)
ActiveCategoryStackClass(void)
bool operator!=(const ActiveCategoryStackClass &)
ActiveCategoryStackClass & operator=(const ActiveCategoryStackClass &that)
void Set_Thread_ID(int id)
void Push(int active_category)
bool operator==(const ActiveCategoryStackClass &)
~ActiveCategoryStackClass(void)
void Register_Memory_Released(int category, int size)
void Push_Active_Category(int category)
int Get_Current_Allocated_Memory(int category)
void Pop_Active_Category(void)
int Register_Memory_Allocated(int size)
int Get_Peak_Allocated_Memory(int category)
~MemLogMutexLockClass(void)
MemLogMutexLockClass(void)
void Memory_Allocated(int size)
int Get_Peak_Allocated_Memory(void)
int Get_Current_Allocated_Memory(void)
void Memory_Released(int size)
static void Switch_Thread()
virtual bool operator==(VectorClass< T > const &) const
WWINLINE VectorClass(NoInitClass const &)
static int Register_Memory_Allocated(int size)
static bool IsMemoryLogEnabled
static int Get_Category_Count(void)
static int Get_Free_Count()
static void Release_Memory(void *mem)
static const char * Get_Category_Name(int category)
static void Release_Log(void)
static int Get_Allocate_Count()
static void Reset_Counters()
static int Get_Current_Allocated_Memory(int category)
static void Pop_Active_Category(void)
static void Push_Active_Category(int category)
static void Register_Memory_Released(int category, int size)
static int Get_Peak_Allocated_Memory(int category)
static void * Allocate_Memory(size_t size)
static MemLogClass * Get_Log(void)
bool Is_Valid_Memory_Log(void)
MemoryLogStruct(int category, int size)
WWINLINE void Unlock_Mem_Log_Mutex(void)
WWINLINE void Lock_Mem_Log_Mutex(void)
const int MAX_CATEGORY_STACK_DEPTH
const int MAX_CATEGORY_STACKS
WWINLINE void * Get_Mem_Log_Mutex(void)