summaryrefslogtreecommitdiff
path: root/src/vm/rcwrefcache.cpp
blob: d7dbbfc5e22e7ebcb84dba1dfaeec17079f59041 (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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
// 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.
//

//

/*============================================================
**
** Class:  RCWRefCache
**
**
** Purpose: The implementation of RCWRefCache class
** This class maintains per-AppDomain cache that can be used 
** by RCW to reference other CCWs
===========================================================*/

#include "common.h"

#include "comcallablewrapper.h"
#include "runtimecallablewrapper.h"
#include "rcwrefcache.h"

// SHRINK_TOTAL_THRESHOLD - only shrink when the total number of handles exceed this number
#ifdef _DEBUG
// Exercise the shrink path more often
#define SHRINK_TOTAL_THRESHOLD            4
#else
#define SHRINK_TOTAL_THRESHOLD            100
#endif // _DEBUG

// SHRINK_HINT_THRESHOLD - only shrink when we consistently see a hint that we probably need to shrink
#ifdef _DEBUG
// Exercise the shrink path more often
#define SHRINK_HINT_THRESHOLD            0
#else
#define SHRINK_HINT_THRESHOLD            10
#endif // _DEBUG

RCWRefCache::RCWRefCache(AppDomain *pAppDomain)
{
    CONTRACTL
    {
        THROWS;
        GC_NOTRIGGER;
        MODE_ANY;
    }
    CONTRACTL_END;

    _ASSERTE(pAppDomain != NULL);
    
    m_pAppDomain = pAppDomain;
    
    m_dwDepHndListFreeIndex = 0;
}

RCWRefCache::~RCWRefCache()
{

    LIMITED_METHOD_CONTRACT;

    // We don't need to clear the handles because this AppDomain will go away
    // and GC will clean everything for us
}

//
// Reset dependent handle cache by assigning 0 to m_dwDepHndListFreeIndex.
//
void RCWRefCache::ResetDependentHandles()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] ----- RCWRefCache::ResetDependentHandles BEGINS -----\n", this));
    LOG((LF_INTEROP, LL_INFO100, 
        "\t[RCWRefCache 0x%p] Dependent handle cache status: Total SLOTs = %d, Next free SLOT index = %d\n", 
        this, (ULONG) m_depHndList.Size(), m_dwDepHndListFreeIndex
        ));

    // Reset the index - now every handle in the cache can be reused
    m_dwDepHndListFreeIndex = 0;    

    LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] ----- RCWRefCache::ResetDependentHandles ENDS   -----\n", this));
}

//
// Shrink the dependent handle cache if necessary (will destroy handles) and clear unused handles.
//
void RCWRefCache::ShrinkDependentHandles()
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
    }
    CONTRACTL_END;

    _ASSERTE(m_dwDepHndListFreeIndex <= m_depHndList.Size());
    
    LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] ----- RCWRefCache::ShrinkDependentHandles BEGINS -----\n", this));
    LOG((LF_INTEROP, LL_INFO100, 
        "\t[RCWRefCache 0x%p] Dependent handle cache status: Total SLOTs = %d, Next free SLOT index = %d\n", 
        this, (ULONG) m_depHndList.Size(), m_dwDepHndListFreeIndex
        ));

    SIZE_T depHndListSize = m_depHndList.Size();

    
    //
    // If we are not using half of the handles last time, it is a hint that probably we need to shrink
    //
    _ASSERTE(SHRINK_TOTAL_THRESHOLD / 2 > 0);
    if (m_dwDepHndListFreeIndex < depHndListSize / 2 && depHndListSize > SHRINK_TOTAL_THRESHOLD)
    {
        m_dwShrinkHint++;
        LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] m_dwShrinkHint = %d\n", this, m_dwShrinkHint));

        //
        // Only shrink if we consistently seen such hint more than SHRINK_TOTAL_THRESHOLD times
        //
        if (m_dwShrinkHint > SHRINK_HINT_THRESHOLD)
        {
        
            LOG((LF_INTEROP, LL_INFO100, 
                "\t[RCWRefCache 0x%p] Shrinking dependent handle cache. Total SLOTS = %d\n", 
                this, m_depHndList.Size()
                ));
            
            //
            // Destroy the handles we don't need and resize 
            //
            SIZE_T newSize = depHndListSize / 2;

            _ASSERTE(newSize > 0);
            
            for (SIZE_T i = newSize; i < depHndListSize; ++i)
            {   
                OBJECTHANDLE hnd = m_depHndList.Pop();
                DestroyDependentHandle(hnd);
                LOG((LF_INTEROP, LL_INFO1000, 
                    "\t[RCWRefCache 0x%p] DependentHandle 0x%p destroyed @ index %d\n", 
                    this, hnd, (ULONG)(depHndListSize - (i - newSize + 1))));
            }

            // Try realloc - I don't expect this to fail but who knows
            // If it fails, we just don't care
            m_depHndList.ReSizeNoThrow(newSize);            

            //
            // Reset shrink hint as we've just shrinked
            //
            m_dwShrinkHint = 0;
            LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] Reset m_dwShrinkHint = 0\n", this));
        }
    }
    else
    {
        //
        // Reset shrink hint and start over
        //
        m_dwShrinkHint = 0;
        LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] Reset m_dwShrinkCount = 0\n", this));    
    }

    //
    // Iterate through the list and clear the unused handles
    //
    for (SIZE_T i = m_dwDepHndListFreeIndex; i < m_depHndList.Size(); ++i)
    {
        OBJECTHANDLE depHnd = m_depHndList[i];
        
        IGCHandleManager *mgr = GCHandleUtilities::GetGCHandleManager();
        mgr->StoreObjectInHandle(depHnd, NULL);
        mgr->SetDependentHandleSecondary(depHnd, NULL);
            
        LOG((LF_INTEROP, LL_INFO1000, "\t[RCWRefCache 0x%p] DependentHandle 0x%p cleared @ index %d\n", this, depHnd, (ULONG) i));
    }

    LOG((LF_INTEROP, LL_INFO100, 
        "\t[RCWRefCache 0x%p] Dependent handle cache status: Total SLOTs = %d, Next free SLOT index = %d\n", 
        this, (ULONG) m_depHndList.Size(), m_dwDepHndListFreeIndex
        ));
    
    LOG((LF_INTEROP, LL_INFO100, "\t[RCWRefCache 0x%p] ----- RCWRefCache::ShrinkDependentHandles ENDS   -----\n", this));
}

//
// Add a reference from RCW to CCW
//
HRESULT RCWRefCache::AddReferenceFromRCWToCCW(RCW *pRCW, ComCallWrapper *pCCW)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(pRCW));
        PRECONDITION(CheckPointer(pCCW));
        PRECONDITION(CheckPointer(OBJECTREFToObject(pCCW->GetObjectRef())));
        PRECONDITION(pRCW->IsJupiterObject());
        PRECONDITION(pCCW->GetJupiterRefCount() > 0);
    }
    CONTRACTL_END;
    
    // Try adding reference using dependent handles
    return AddReferenceUsingDependentHandle(pRCW, pCCW);
}

//
// Add RCW -> CCW reference using dependent handle
// May fail if OOM
//
HRESULT RCWRefCache::AddReferenceUsingDependentHandle(RCW *pRCW, ComCallWrapper *pCCW)
{
    CONTRACTL
    {
        NOTHROW;
        GC_NOTRIGGER;
        MODE_COOPERATIVE;
        PRECONDITION(CheckPointer(pRCW));
        PRECONDITION(CheckPointer(pCCW));
        PRECONDITION(CheckPointer(OBJECTREFToObject(pCCW->GetObjectRef())));
    }
    CONTRACTL_END;

    HRESULT hr = S_OK;

    LOG((LF_INTEROP, LL_INFO1000, 
        "\t[RCWRefCache 0x%p] Dependent handle cache status: Total SLOTs = %d, Next free SLOT index = %d\n", 
        this, (ULONG) m_depHndList.Size(), m_dwDepHndListFreeIndex
        ));
    
    // Is there an valid DependentHandle in the array?
    if (m_dwDepHndListFreeIndex >= m_depHndList.Size())
    {
        // No, we need to create a new handle
        EX_TRY
        {
            OBJECTHANDLE depHnd = m_pAppDomain->CreateDependentHandle(pRCW->GetExposedObject(), pCCW->GetObjectRef());
            m_depHndList.Push(depHnd);

            STRESS_LOG2(
                LF_INTEROP, LL_INFO1000, 
                "\t[RCWRefCache] Created DependentHandle 0x%p @ appended SLOT %d\n", 
                depHnd,
                m_dwDepHndListFreeIndex
                );

            // Increment the index so that next time we still need to append
            m_dwDepHndListFreeIndex++;
        }
        EX_CATCH
        {
            hr = GET_EXCEPTION()->GetHR();
        }
        EX_END_CATCH(SwallowAllExceptions)
    }
    else
    {
        // Yes, there is a valid DependentHandle entry on the list, use that
        OBJECTHANDLE depHnd = (OBJECTHANDLE) m_depHndList[m_dwDepHndListFreeIndex];

        IGCHandleManager *mgr = GCHandleUtilities::GetGCHandleManager();
        mgr->StoreObjectInHandle(depHnd, OBJECTREFToObject(pRCW->GetExposedObject()));
        mgr->SetDependentHandleSecondary(depHnd, OBJECTREFToObject(pCCW->GetObjectRef()));

        STRESS_LOG3(
            LF_INTEROP, LL_INFO1000, 
            "\t[RCWRefCache 0x%p] Reused DependentHandle 0x%p @ valid SLOT %d\n", 
            this, depHnd, m_dwDepHndListFreeIndex);
        
        // Increment the index and the next one will be used
        m_dwDepHndListFreeIndex++;
    }

    if (SUCCEEDED(hr))
    {
        LOG((LF_INTEROP, LL_INFO1000, 
            "\t[RCWRefCache 0x%p] Dependent handle cache status: Total SLOTs = %d, Next free SLOT index = %d\n", 
            this, (ULONG) m_depHndList.Size(), m_dwDepHndListFreeIndex
            ));
    }
   
    return hr;    
}