summaryrefslogtreecommitdiff
path: root/src/vm/rcwrefcache.h
blob: 39311f37a5b4fdd2e183c02110c2bb7afe1919a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
//

//

/*============================================================
**
** Header:  RCWRefCache.h
**
**
** Purpose: Defines RCWRefCache class
** This class maintains per-AppDomain cache that can be used 
** by RCW to reference other CCWs
===========================================================*/

#ifndef _H_RCWREFCACHE_
#define _H_RCWREFCACHE_

#ifdef FEATURE_COMINTEROP

class RCWRefCache
{
public :
    RCWRefCache(AppDomain *pAppDomain);
    ~RCWRefCache();

    //
    // Add a reference from RCW to CCW
    //
    HRESULT AddReferenceFromRCWToCCW(RCW *pRCW, ComCallWrapper *pCCW);

    //
    // Enumerate all Jupiter RCWs in the RCW cache and do the callback
    // I'm using template here so there is no perf penality
    //
    template<class Function, class T> HRESULT EnumerateAllJupiterRCWs(Function fn, T userData)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_COOPERATIVE;
        }        
        CONTRACTL_END;
        
        HRESULT hr;
        
        // Go through the RCW cache and call the callback for all Jupiter objects
        RCWCache *pRCWCache = m_pAppDomain->GetRCWCacheNoCreate();
        if (pRCWCache != NULL)
        {
            SHash<RCWCache::RCWCacheTraits> *pHashMap = &pRCWCache->m_HashMap;

            for (SHash<RCWCache::RCWCacheTraits>::Iterator it = pHashMap->Begin(); it != pHashMap->End(); it++)
            {
                RCW *pRCW = *it;
                _ASSERTE(pRCW != NULL);

                if (pRCW->IsJupiterObject())
                {
                    hr = fn(pRCW, userData);
                    if (FAILED(hr))
                    {
                        return hr;
                    }
                }
            }
        }

        return S_OK;
    }

    //
    // Reset dependent handle cache by assigning 0 to m_dwDepHndListFreeIndex.
    //
    void ResetDependentHandles();

    //
    // Shrink the dependent handle cache if necessary (will destroy handles) and clear unused handles.
    //
    void ShrinkDependentHandles();

private :
    //
    // Add RCW -> CCW reference using dependent handle
    // May fail if OOM
    //
    HRESULT AddReferenceUsingDependentHandle(RCW *pRCW, ComCallWrapper *pCCW);

private :
    AppDomain      *m_pAppDomain;                   // Domain

    CQuickArrayList<OBJECTHANDLE>   m_depHndList;               // Internal DependentHandle cache
                                                                // non-NULL dependent handles followed by NULL slots
    DWORD                           m_dwDepHndListFreeIndex;    // The starting index where m_depHndList has available slots                                 
    DWORD                           m_dwShrinkHint;             // Keep track of how many times we use less than half handles    
};

#endif // FEATURE_COMINTEROP

#endif // _H_RCWREFCACHE_