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

  1. // findrefs.cpp
  2. //
  3. // Created 10/08/98
  4. //
  5. // (C)Copyright 1998-1999 Microsoft Corporation, All rights reserved.
  6. //
  7.  
  8. #include "pch.hpp"
  9. #pragma hdrstop
  10.  
  11. #include "hpmonmgr.hpp"
  12. #include "obsearch.hpp"
  13.  
  14.  
  15. SearchHeapInfoCallback::SearchHeapInfoCallback (HeapMonitorManager *mgr)
  16. {
  17.     ASSERT(mgr);
  18.     (m_mgr = mgr)->AddRef();
  19.     m_RefCount = 1;
  20.     m_workitems = NULL;
  21. }
  22.  
  23.  
  24. SearchHeapInfoCallback::~SearchHeapInfoCallback ()
  25. {
  26.     ASSERT(!m_RefCount);
  27.     ASSERT(!m_workitems);
  28.  
  29.     if (m_mgr)
  30.         m_mgr->Release();
  31. }
  32.  
  33.  
  34. VOID SearchHeapInfoCallback::AddSearchOperation (SearchOperation *workitem)
  35. {
  36.     ASSERT(workitem);
  37.  
  38.     workitem->next = m_workitems;
  39.     m_workitems = workitem;
  40. }
  41.  
  42.  
  43. HRESULT SearchHeapInfoCallback::BeginDump ()
  44. {
  45.     ASSERT(m_workitems != NULL);
  46.  
  47.     m_mgr->IterateKnownRootReferences(&FindRootRefsCB, this);
  48.  
  49.     return S_OK;
  50. }
  51.  
  52.  
  53. VOID SearchHeapInfoCallback::EndDump ()
  54. {
  55.     while (m_workitems)
  56.     {
  57.         SearchOperation *trash = m_workitems;
  58.         m_workitems = trash->next;
  59.         delete(trash);
  60.     }
  61. }
  62.  
  63.  
  64. //static
  65. int SearchHeapInfoCallback::FindRootRefsCB (ID id, ObjectID refvmid, PVOID token)
  66. {
  67.     SearchHeapInfoCallback *shicb = (SearchHeapInfoCallback*)token;
  68.  
  69.     SearchOperation *cur = shicb->m_workitems;
  70.     do
  71.     {
  72.         BOOL fFound = FALSE;
  73.  
  74.         switch (cur->op)
  75.         {
  76.         case SOP_REFERENCE_TO:
  77.             fFound = cur->refersto.oid == refvmid;
  78.             break;
  79.  
  80.         case SOP_INSTANCE_OF:
  81.             // TODO: method frames are sort-of an instance of the class
  82.             break;
  83.         }
  84.  
  85.         if (fFound)
  86.             (*cur->proc)(id, NULL, cur->token);
  87.  
  88.         cur = cur->next;
  89.         shicb->m_mgr->IncProgressIndicator();
  90.     }
  91.     while (cur != NULL);
  92.  
  93.     return 1;
  94. }
  95.  
  96.  
  97. STDMETHODIMP SearchHeapInfoCallback::ObjectReferences (ObjectID vmid, DWORD flags, const ObjectID *prefs, unsigned nrefs, const DWORD *pflags)
  98. {
  99.     m_mgr->IncProgressIndicator();
  100.  
  101.     SearchOperation *cur = m_workitems;
  102.     while (cur != NULL)
  103.     {
  104.         BOOL fFound = FALSE;
  105.     
  106.         switch (cur->op)
  107.         {
  108.         case SOP_INSTANCE_OF:
  109.             if (!(flags & JVM_OBJ_MORE_REFERENCES))
  110.             {
  111.                 ClassID cid;
  112.                 IJavaEventMonitorIDInfo2 *pvminfo = m_mgr->GetVMInfoInterface();
  113.                 if (pvminfo->ObjectInformation(vmid, &cid) == S_OK)
  114.                 {
  115.                     fFound = (cid == cur->instanceof.cid);
  116.                 }
  117.                 pvminfo->Release();
  118.             }
  119.             break;
  120.  
  121.         case SOP_REFERENCE_TO:
  122.             {
  123.                 const ObjectID *curref = prefs;
  124.                 const ObjectID *refsstop = prefs + nrefs;
  125.                 while (curref < refsstop)
  126.                 {
  127.                     ObjectID currefid = *curref;
  128.                     if (currefid == cur->refersto.oid)
  129.                     {
  130.                         fFound = TRUE;
  131.                         break;
  132.                     }
  133.                     curref++;
  134.                 }
  135.             }
  136.             break;
  137.         }
  138.  
  139.         if (fFound)
  140.         {
  141.             (*cur->proc)(NULL, vmid, cur->token);
  142.         }
  143.         
  144.         cur = cur->next;
  145.     }
  146.  
  147.     return S_POSTPONE_REFERENCES;
  148. }
  149.  
  150.  
  151. ULONG STDMETHODCALLTYPE SearchHeapInfoCallback::AddRef (VOID)
  152. {
  153.     ASSERT(m_RefCount && m_RefCount < ULONG_MAX);
  154.  
  155.     return InterlockedIncrement((LONG*)&m_RefCount);
  156. }
  157.  
  158.  
  159. ULONG STDMETHODCALLTYPE SearchHeapInfoCallback::Release (VOID)
  160. {
  161.     ASSERT(m_RefCount);
  162.  
  163.     ULONG NewRefCount = (ULONG)InterlockedDecrement((LONG*)&m_RefCount);
  164.     if (NewRefCount)
  165.         return NewRefCount;
  166.  
  167.     delete(this);
  168.  
  169.     return 0;
  170. }
  171.  
  172.  
  173. STDMETHODIMP SearchHeapInfoCallback::QueryInterface (REFIID riid, PVOID *ppvObj)
  174. {
  175.     HRESULT hr = S_OK;
  176.  
  177.     if (riid == IID_IUnknown || riid == IID_IHeapInfoCallback)
  178.     {
  179.         *ppvObj = (IHeapInfoCallback*)this;
  180.         AddRef();
  181.     }
  182.     else
  183.     {
  184.         *ppvObj = NULL;
  185.         hr = E_NOINTERFACE;
  186.     }
  187.  
  188.     return hr;
  189. }
  190.  
  191.