[Tizen] Unify dnetmemoryenumlib terms to match the codebase (#291)
[platform/upstream/coreclr.git] / src / vm / rcwrefcache.h
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4 //
5
6 //
7
8 /*============================================================
9 **
10 ** Header:  RCWRefCache.h
11 **
12 **
13 ** Purpose: Defines RCWRefCache class
14 ** This class maintains per-AppDomain cache that can be used 
15 ** by RCW to reference other CCWs
16 ===========================================================*/
17
18 #ifndef _H_RCWREFCACHE_
19 #define _H_RCWREFCACHE_
20
21 #ifdef FEATURE_COMINTEROP
22
23 class RCWRefCache
24 {
25 public :
26     RCWRefCache(AppDomain *pAppDomain);
27     ~RCWRefCache();
28
29     //
30     // Add a reference from RCW to CCW
31     //
32     HRESULT AddReferenceFromRCWToCCW(RCW *pRCW, ComCallWrapper *pCCW);
33
34     //
35     // Enumerate all Jupiter RCWs in the RCW cache and do the callback
36     // I'm using template here so there is no perf penality
37     //
38     template<class Function, class T> HRESULT EnumerateAllJupiterRCWs(Function fn, T userData)
39     {
40         CONTRACTL
41         {
42             NOTHROW;
43             GC_NOTRIGGER;
44             MODE_COOPERATIVE;
45         }        
46         CONTRACTL_END;
47         
48         HRESULT hr;
49         
50         // Go through the RCW cache and call the callback for all Jupiter objects
51         RCWCache *pRCWCache = m_pAppDomain->GetRCWCacheNoCreate();
52         if (pRCWCache != NULL)
53         {
54             SHash<RCWCache::RCWCacheTraits> *pHashMap = &pRCWCache->m_HashMap;
55
56             for (SHash<RCWCache::RCWCacheTraits>::Iterator it = pHashMap->Begin(); it != pHashMap->End(); it++)
57             {
58                 RCW *pRCW = *it;
59                 _ASSERTE(pRCW != NULL);
60
61                 if (pRCW->IsJupiterObject())
62                 {
63                     hr = fn(pRCW, userData);
64                     if (FAILED(hr))
65                     {
66                         return hr;
67                     }
68                 }
69             }
70         }
71
72         return S_OK;
73     }
74
75     //
76     // Reset dependent handle cache by assigning 0 to m_dwDepHndListFreeIndex.
77     //
78     void ResetDependentHandles();
79
80     //
81     // Shrink the dependent handle cache if necessary (will destroy handles) and clear unused handles.
82     //
83     void ShrinkDependentHandles();
84
85 private :
86     //
87     // Add RCW -> CCW reference using dependent handle
88     // May fail if OOM
89     //
90     HRESULT AddReferenceUsingDependentHandle(RCW *pRCW, ComCallWrapper *pCCW);
91
92 private :
93     AppDomain      *m_pAppDomain;                   // Domain
94
95     CQuickArrayList<OBJECTHANDLE>   m_depHndList;               // Internal DependentHandle cache
96                                                                 // non-NULL dependent handles followed by NULL slots
97     DWORD                           m_dwDepHndListFreeIndex;    // The starting index where m_depHndList has available slots                                 
98     DWORD                           m_dwShrinkHint;             // Keep track of how many times we use less than half handles    
99 };
100
101 #endif // FEATURE_COMINTEROP
102
103 #endif // _H_RCWREFCACHE_