summaryrefslogtreecommitdiff
path: root/src/vm/comreflectioncache.hpp
blob: f4f15c268ef5b394b6880a218bd1f2af3cf3f816 (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
// 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.


#ifndef __COMReflectionCache_hpp__
#define __COMReflectionCache_hpp__

#include <stddef.h>
#include "class.h"
#include "threads.h"
#include "simplerwlock.hpp"

class ReflectClass;

template <class Element, class CacheType, int CacheSize> class ReflectionCache
: public SimpleRWLock
{
public:
    ReflectionCache()
        : SimpleRWLock(COOPERATIVE, LOCK_REFLECTCACHE)
    {
        WRAPPER_NO_CONTRACT;
        index = 0;
        currentStamp = 0;
    }

    void Init();

    BOOL GetFromCache(Element *pElement, CacheType& rv)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
            PRECONDITION(CheckPointer(pElement));
        }
        CONTRACTL_END;

        this->EnterRead();
        
        rv = 0;
        int i = SlotInCache(pElement);
        BOOL fGotIt = (i != CacheSize);
        if (fGotIt)
        {
            rv = m_pResult[i].element.GetValue();            
            m_pResult[i].stamp = InterlockedIncrement(&currentStamp);
        }
        this->LeaveRead();

        if (fGotIt)
            AdjustStamp(FALSE);

        return fGotIt;
    }
    
    void AddToCache(Element *pElement, CacheType obj)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
            PRECONDITION(CheckPointer(pElement));
        }
        CONTRACTL_END;

        this->EnterWrite();
        currentStamp++; // no need to InterlockIncrement b/c write lock taken
        int i = SlotInCache(pElement);
        if (i == CacheSize)
        {
            int slot = index;
            // Not in cache.
            if (slot == CacheSize)
            {
                // Reuse a slot.
                slot = 0;
                LONG minStamp = m_pResult[0].stamp;
                for (i = 1; i < CacheSize; i ++)
                {
                    if (m_pResult[i].stamp < minStamp)
                    {
                        slot = i;
                        minStamp = m_pResult[i].stamp;
                    }
                }
            }
            else
                m_pResult[slot].element.InitValue();

            m_pResult[slot].element = *pElement;
            m_pResult[slot].element.SetValue(obj);
            m_pResult[slot].stamp = currentStamp;

            UpdateHashTable(pElement->GetHash(), slot);
            
            if (index < CacheSize)
                index++;
        }
        AdjustStamp(TRUE);
        this->LeaveWrite();
    }

private:
    // Lock must have been taken before calling this.
    int SlotInCache(Element *pElement)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
            PRECONDITION(LockTaken());
            PRECONDITION(CheckPointer(pElement));
        }
        CONTRACTL_END;

        if (index == 0)
            return CacheSize;

        size_t hash = pElement->GetHash();

        int slot = m_pHashTable[hash%CacheSize].slot;
        if (slot == -1)
            return CacheSize;
        
        if (m_pResult[slot].element == *pElement)
            return slot;
        
        for (int i = 0; i < index; i ++)
        {
            if (i != slot && m_pHashTable[i].hash == hash)
            {
                if (m_pResult[i].element == *pElement)
                    return i;
            }
        }

        return CacheSize;
    }

    void AdjustStamp(BOOL hasWriterLock)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
        }
        CONTRACTL_END;

        if ((currentStamp & 0x40000000) == 0)
            return;
        if (!hasWriterLock)
        {
            _ASSERTE (!LockTaken());
            this->EnterWrite();
        }
        else
            _ASSERTE (IsWriterLock());
        
        if (currentStamp & 0x40000000)
        {
            currentStamp >>= 1;
            for (int i = 0; i < index; i ++)
                m_pResult[i].stamp >>= 1;
        }
        if (!hasWriterLock)
            this->LeaveWrite();
    }

    void UpdateHashTable(SIZE_T hash, int slot)
    {
        CONTRACTL
        {
            NOTHROW;
            GC_NOTRIGGER;
            MODE_ANY;
            PRECONDITION(IsWriterLock());
        }
        CONTRACTL_END;

        m_pHashTable[slot].hash = hash;
        m_pHashTable[hash%CacheSize].slot = slot;
    }
    
    struct CacheTable
    {
        Element element;
        int stamp;
    } *m_pResult;

    struct HashTable
    {
        size_t hash;   // Record hash value for each slot
        int   slot;    // The slot corresponding to the hash.
    } *m_pHashTable;

    int index;
    LONG currentStamp;
};


#ifdef FEATURE_COMINTEROP

#define ReflectionMaxCachedNameLength 23
struct DispIDCacheElement
{
    MethodTable *pMT;
    int strNameLength;
    LCID lcid;
    DISPID DispId;
    WCHAR strName[ReflectionMaxCachedNameLength+1];
    DispIDCacheElement ()
        : pMT (NULL), strNameLength(0), lcid(0), DispId(0)
    {
        LIMITED_METHOD_CONTRACT;
    }

    BOOL operator==(const DispIDCacheElement& var) const
    {
        LIMITED_METHOD_CONTRACT;
        return (pMT == var.pMT && strNameLength == var.strNameLength
                && lcid == var.lcid && wcscmp (strName, var.strName) == 0);
    }

    DispIDCacheElement& operator= (const DispIDCacheElement& var)
    {
        LIMITED_METHOD_CONTRACT;
        _ASSERTE (var.strNameLength <= ReflectionMaxCachedNameLength);
        pMT = var.pMT;
        strNameLength = var.strNameLength;
        lcid = var.lcid;
        wcscpy_s (strName, COUNTOF(strName), var.strName);
        return *this;
    }

    DISPID GetValue ()
    {
        LIMITED_METHOD_CONTRACT;
        return DispId;
    }

    void InitValue ()
    {
        LIMITED_METHOD_CONTRACT;
    }
    
    void SetValue (DISPID Id)
    {
        LIMITED_METHOD_CONTRACT;
        DispId = Id;
    }

    size_t GetHash ()
    {
        LIMITED_METHOD_CONTRACT;
        return (size_t)pMT + strNameLength + (lcid << 4);
    }
};

typedef ReflectionCache <DispIDCacheElement, DISPID, 128> DispIDCache;

#endif // FEATURE_COMINTEROP

#endif // __COMReflectionCache_hpp__