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

// 
// Hash table associated with each module that records for all types defined in that module the mapping
// between type name and token (or TypeHandle).
//

#ifndef __TYPEEQUIVALENCE_HASH_INCLUDED
#define __TYPEEQUIVALENCE_HASH_INCLUDED

#ifdef FEATURE_TYPEEQUIVALENCE

#include "ngenhash.h"

// The type of each entry in the hash.
typedef DPTR(struct TypeEquivalenceEntry) PTR_TypeEquivalenceEntry;
struct TypeEquivalenceEntry
{
    static NgenHashValue HashTypeHandles(TypeHandle thA, TypeHandle thB)
    {
        LIMITED_METHOD_CONTRACT;

        UINT_PTR aPtr = thA.AsTAddr();
        UINT_PTR bPtr = thB.AsTAddr();
        DWORD hash = (DWORD)((aPtr + bPtr) >> 3);

        return hash;
    }

    bool Match(TypeHandle thA, TypeHandle thB)
    {
        LIMITED_METHOD_CONTRACT;

        return (((thA == m_thA) && (thB == m_thB))
                || ((thB == m_thA) && (thA == m_thB)));
    }

    void SetData(TypeHandle thA, TypeHandle thB, bool fEquivalent)
    {
        LIMITED_METHOD_CONTRACT;

        m_thA = thA;
        m_thB = thB;
        m_fEquivalent = fEquivalent;
    }

    bool GetEquivalence()
    {
        LIMITED_METHOD_CONTRACT;
        return m_fEquivalent;
    }

private:
    TypeHandle m_thA;
    TypeHandle m_thB;
    bool       m_fEquivalent;
};

// The hash type itself. All common logic is provided by the NgenHashTable templated base class. See
// NgenHash.h for details.
typedef DPTR(class TypeEquivalenceHashTable) PTR_TypeEquivalenceHashTable;
class TypeEquivalenceHashTable : public NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>
{
    friend class NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>;
#ifdef DACCESS_COMPILE
    friend class NativeImageDumper;
#endif

public:
    enum EquivalenceMatch
    {
        MatchUnknown,
        Match,
        NoMatch
    };

    // The LookupContext type we export to track GetValue/FindNextNestedClass enumerations is simply a rename
    // of the base classes' hash value enumerator.
    typedef NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>::LookupContext LookupContext;

#ifndef DACCESS_COMPILE
    static TypeEquivalenceHashTable *Create(AppDomain *pDomain, DWORD dwNumBuckets, CrstExplicitInit *pCrst);
    void RecordEquivalence(TypeHandle thA, TypeHandle thB, EquivalenceMatch match);
#endif
    EquivalenceMatch CheckEquivalence(TypeHandle thA, TypeHandle thB);

#ifdef DACCESS_COMPILE
    void EnumMemoryRegionsForEntry(TypeEquivalenceEntry *pEntry, CLRDataEnumMemoryFlags flags) { return; }
#endif

#if defined(FEATURE_PREJIT) && !defined(DACCESS_COMPILE)
private:
    // Override operations from NgenHashTable - see ngenhash.h
    bool ShouldSave(DataImage *pImage, TypeEquivalenceEntry *pEntry) { return false; }
    bool IsHotEntry(TypeEquivalenceEntry *pEntry, CorProfileData *pProfileData) { return false; }
    bool SaveEntry(DataImage *pImage, CorProfileData *pProfileData, TypeEquivalenceEntry *pOldEntry, TypeEquivalenceEntry *pNewEntry, EntryMappingTable *pMap) { return true; }
    void FixupEntry(DataImage *pImage, TypeEquivalenceEntry *pEntry, void *pFixupBase, DWORD cbFixupOffset) { return; }
#endif // FEATURE_PREJIT && !DACCESS_COMPILE

private:
#ifndef DACCESS_COMPILE
    TypeEquivalenceHashTable(LoaderHeap *pHeap, DWORD cInitialBuckets, CrstExplicitInit *pCrst)
        : NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>(NULL, pHeap, cInitialBuckets)
        , m_pHashTableCrst(pCrst)
    {
    }
#endif // DACCESS_COMPILE

    CrstExplicitInit*   m_pHashTableCrst;
};

#endif // FEATURE_TYPEEQUIVALENCE
#endif // !__TYPEEQUIVALENCE_HASH_INCLUDED