home *** CD-ROM | disk | FTP | other *** search
- // allocprf.hpp
- //
- // Created 01/19/99
- //
- // (C) Copyright 1995 - 1999 Microsoft Corporation. All rights reserved.
- //
-
- #ifndef __ALLOCPRF_HPP__
- #define __ALLOCPRF_HPP__
-
- class EventMonitor;
-
-
- class AllocationProfiler
- {
- EventMonitor *m_pmon;
-
- // 1-based
- ULONG m_iGC;
-
- ULONG m_nTotalAlloc;
- size_t m_cbTotalAlloc;
-
- struct AllocationProfilerEntry
- {
- ClassID id;
- MethodID loc;
-
- ULONG nAlloc;
- size_t cbAlloc;
-
- AllocationProfilerEntry *firstcls;
-
- AllocationProfilerEntry *next;
- };
-
- AllocationProfilerEntry *m_hash[293];
-
- CRITICAL_SECTION m_cs;
-
-
- AllocationProfilerEntry *LookupObject (ClassID id, MethodID loc = NULL)
- {
- AllocationProfilerEntry *cur;
-
- unsigned ibucket = ((unsigned)id % ARRAY_ELEMENTS(m_hash));
- AllocationProfilerEntry **prev;
-
- prev = &m_hash[ibucket];
- for (;;)
- {
- cur = *prev;
- if (!cur)
- break;
-
- if (cur->id == id && cur->loc == loc)
- {
- *prev = cur->next;
- cur->next = m_hash[ibucket];
- m_hash[ibucket] = cur;
- return cur;
- }
-
- prev = &cur->next;
- }
-
- EnterCriticalSection(&m_cs);
- {
- AllocationProfilerEntry *firstcls = NULL;
-
- prev = &m_hash[ibucket];
- for (;;)
- {
- cur = *prev;
- if (!cur)
- break;
-
- if (cur->id == id)
- {
- if (cur->loc == loc)
- {
- *prev = cur->next;
- cur->next = m_hash[ibucket];
- m_hash[ibucket] = cur;
- return cur;
- }
-
- if (!firstcls)
- {
- firstcls = cur->firstcls;
- if (!firstcls)
- firstcls = cur;
- }
- }
-
- prev = &cur->next;
- }
-
- if ((cur = new AllocationProfilerEntry()) != NULL)
- {
- ZeroMemory(cur, sizeof(*cur));
- cur->id = id;
- cur->loc = loc;
- cur->firstcls = firstcls;
- cur->next = m_hash[ibucket];
- m_hash[ibucket] = cur;
- }
- }
- LeaveCriticalSection(&m_cs);
-
- return cur;
- }
-
-
- typedef int (AllocationProfiler::*POBJITERFN) (AllocationProfilerEntry *ent, PVOID token);
-
- int Iterate (POBJITERFN fn, PVOID token = NULL);
-
-
- int DeleteCB (AllocationProfilerEntry *ent, PVOID token);
- int ComputeTotalsCB (AllocationProfilerEntry *cur, PVOID token);
- int SpewTotalsCB (AllocationProfilerEntry *cur, PVOID token);
- int SpewMethodsCB (AllocationProfilerEntry *cur, PVOID token);
-
-
- public:
-
- AllocationProfiler ()
- {
- m_pmon = NULL;
-
- m_iGC = 1;
- m_nTotalAlloc = 0;
- m_cbTotalAlloc = 0;
-
- ZeroMemory(&m_hash, sizeof(m_hash));
-
- InitializeCriticalSection(&m_cs);
- }
-
- HRESULT Initialize (EventMonitor *pmon);
-
- VOID Destruct ();
-
-
- VOID InstanceCreated (ClassID idType, size_t size, MethodID idCreatingMethod);
-
- DWORD GetTotalObjectsAllocated ()
- {
- return m_nTotalAlloc;
- }
-
- DWORD GetTotalBytesAllocated ()
- {
- return m_cbTotalAlloc;
- }
-
-
- VOID GCOccurred ()
- {
- m_iGC++;
- }
-
-
- VOID SpewResults ();
- };
-
-
- struct AllocationProfilerThreadData
- {
- MethodID idCreatingMethod;
- };
-
-
- #endif /* __ALLOCPRF_HPP__ */
-
-