home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / Profiler / jviewprf / allocprf.hpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-05-04  |  3.8 KB  |  177 lines

  1. // allocprf.hpp
  2. //
  3. // Created 01/19/99
  4. //
  5. // (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  6. //
  7.  
  8. #ifndef __ALLOCPRF_HPP__
  9. #define __ALLOCPRF_HPP__
  10.  
  11. class EventMonitor;
  12.  
  13.  
  14. class AllocationProfiler
  15. {
  16.     EventMonitor *m_pmon;
  17.  
  18.     // 1-based
  19.     ULONG m_iGC;
  20.  
  21.     ULONG m_nTotalAlloc;
  22.     size_t m_cbTotalAlloc;
  23.  
  24.     struct AllocationProfilerEntry
  25.     {
  26.         ClassID id;
  27.         MethodID loc;
  28.  
  29.         ULONG nAlloc;
  30.         size_t cbAlloc;
  31.  
  32.         AllocationProfilerEntry *firstcls;
  33.  
  34.         AllocationProfilerEntry *next;
  35.     };
  36.  
  37.     AllocationProfilerEntry *m_hash[293];
  38.  
  39.     CRITICAL_SECTION m_cs;
  40.  
  41.  
  42.     AllocationProfilerEntry *LookupObject (ClassID id, MethodID loc = NULL)
  43.     {
  44.         AllocationProfilerEntry *cur;
  45.     
  46.         unsigned ibucket = ((unsigned)id % ARRAY_ELEMENTS(m_hash));
  47.         AllocationProfilerEntry **prev;
  48.  
  49.         prev = &m_hash[ibucket];
  50.         for (;;)
  51.         {
  52.             cur = *prev;
  53.             if (!cur)
  54.                 break;
  55.  
  56.             if (cur->id == id && cur->loc == loc)
  57.             {
  58.                 *prev = cur->next;
  59.                 cur->next = m_hash[ibucket];
  60.                 m_hash[ibucket] = cur;
  61.                 return cur;
  62.             }
  63.  
  64.             prev = &cur->next;
  65.         }
  66.  
  67.         EnterCriticalSection(&m_cs);
  68.         {
  69.             AllocationProfilerEntry *firstcls = NULL;
  70.         
  71.             prev = &m_hash[ibucket];
  72.             for (;;)
  73.             {
  74.                 cur = *prev;
  75.                 if (!cur)
  76.                     break;
  77.  
  78.                 if (cur->id == id)
  79.                 {
  80.                     if (cur->loc == loc)
  81.                     {
  82.                         *prev = cur->next;
  83.                         cur->next = m_hash[ibucket];
  84.                         m_hash[ibucket] = cur;
  85.                         return cur;
  86.                     }
  87.  
  88.                     if (!firstcls)
  89.                     {
  90.                         firstcls = cur->firstcls;
  91.                         if (!firstcls)
  92.                             firstcls = cur;
  93.                     }
  94.                 }
  95.  
  96.                 prev = &cur->next;
  97.             }
  98.  
  99.             if ((cur = new AllocationProfilerEntry()) != NULL)
  100.             {
  101.                 ZeroMemory(cur, sizeof(*cur));
  102.                 cur->id = id;
  103.                 cur->loc = loc;
  104.                 cur->firstcls = firstcls;
  105.                 cur->next = m_hash[ibucket];
  106.                 m_hash[ibucket] = cur;
  107.             }
  108.         }
  109.         LeaveCriticalSection(&m_cs);
  110.  
  111.         return cur;
  112.     }
  113.  
  114.  
  115.     typedef int (AllocationProfiler::*POBJITERFN) (AllocationProfilerEntry *ent, PVOID token);
  116.  
  117.     int Iterate (POBJITERFN fn, PVOID token = NULL);
  118.  
  119.  
  120.     int DeleteCB (AllocationProfilerEntry *ent, PVOID token);
  121.     int ComputeTotalsCB (AllocationProfilerEntry *cur, PVOID token);
  122.     int SpewTotalsCB (AllocationProfilerEntry *cur, PVOID token);
  123.     int SpewMethodsCB (AllocationProfilerEntry *cur, PVOID token);
  124.  
  125.  
  126. public:
  127.  
  128.     AllocationProfiler ()
  129.     {
  130.         m_pmon = NULL;
  131.  
  132.         m_iGC = 1;
  133.         m_nTotalAlloc = 0;
  134.         m_cbTotalAlloc = 0;
  135.  
  136.         ZeroMemory(&m_hash, sizeof(m_hash));
  137.  
  138.         InitializeCriticalSection(&m_cs);
  139.     }
  140.  
  141.     HRESULT Initialize (EventMonitor *pmon);
  142.  
  143.     VOID Destruct ();
  144.  
  145.  
  146.     VOID InstanceCreated (ClassID idType, size_t size, MethodID idCreatingMethod);
  147.  
  148.     DWORD GetTotalObjectsAllocated ()
  149.     {
  150.         return m_nTotalAlloc;
  151.     }
  152.  
  153.     DWORD GetTotalBytesAllocated ()
  154.     {
  155.         return m_cbTotalAlloc;
  156.     }
  157.  
  158.  
  159.     VOID GCOccurred ()
  160.     {
  161.         m_iGC++;
  162.     }
  163.  
  164.  
  165.     VOID SpewResults ();
  166. };
  167.  
  168.  
  169. struct AllocationProfilerThreadData
  170. {
  171.     MethodID idCreatingMethod;
  172. };
  173.  
  174.  
  175. #endif /* __ALLOCPRF_HPP__ */
  176.  
  177.