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// Hash table associated with each module that records for all types defined in that module the mapping
7// between type name and token (or TypeHandle).
8//
9
10#ifndef __TYPEEQUIVALENCE_HASH_INCLUDED
11#define __TYPEEQUIVALENCE_HASH_INCLUDED
12
13#ifdef FEATURE_TYPEEQUIVALENCE
14
15#include "ngenhash.h"
16
17// The type of each entry in the hash.
18typedef DPTR(struct TypeEquivalenceEntry) PTR_TypeEquivalenceEntry;
19struct TypeEquivalenceEntry
20{
21 static NgenHashValue HashTypeHandles(TypeHandle thA, TypeHandle thB)
22 {
23 LIMITED_METHOD_CONTRACT;
24
25 UINT_PTR aPtr = thA.AsTAddr();
26 UINT_PTR bPtr = thB.AsTAddr();
27 DWORD hash = (DWORD)((aPtr + bPtr) >> 3);
28
29 return hash;
30 }
31
32 bool Match(TypeHandle thA, TypeHandle thB)
33 {
34 LIMITED_METHOD_CONTRACT;
35
36 return (((thA == m_thA) && (thB == m_thB))
37 || ((thB == m_thA) && (thA == m_thB)));
38 }
39
40 void SetData(TypeHandle thA, TypeHandle thB, bool fEquivalent)
41 {
42 LIMITED_METHOD_CONTRACT;
43
44 m_thA = thA;
45 m_thB = thB;
46 m_fEquivalent = fEquivalent;
47 }
48
49 bool GetEquivalence()
50 {
51 LIMITED_METHOD_CONTRACT;
52 return m_fEquivalent;
53 }
54
55private:
56 TypeHandle m_thA;
57 TypeHandle m_thB;
58 bool m_fEquivalent;
59};
60
61// The hash type itself. All common logic is provided by the NgenHashTable templated base class. See
62// NgenHash.h for details.
63typedef DPTR(class TypeEquivalenceHashTable) PTR_TypeEquivalenceHashTable;
64class TypeEquivalenceHashTable : public NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>
65{
66 friend class NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>;
67#ifdef DACCESS_COMPILE
68 friend class NativeImageDumper;
69#endif
70
71public:
72 enum EquivalenceMatch
73 {
74 MatchUnknown,
75 Match,
76 NoMatch
77 };
78
79 // The LookupContext type we export to track GetValue/FindNextNestedClass enumerations is simply a rename
80 // of the base classes' hash value enumerator.
81 typedef NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>::LookupContext LookupContext;
82
83#ifndef DACCESS_COMPILE
84 static TypeEquivalenceHashTable *Create(AppDomain *pDomain, DWORD dwNumBuckets, CrstExplicitInit *pCrst);
85 void RecordEquivalence(TypeHandle thA, TypeHandle thB, EquivalenceMatch match);
86#endif
87 EquivalenceMatch CheckEquivalence(TypeHandle thA, TypeHandle thB);
88
89#ifdef DACCESS_COMPILE
90 void EnumMemoryRegionsForEntry(TypeEquivalenceEntry *pEntry, CLRDataEnumMemoryFlags flags) { return; }
91#endif
92
93#if defined(FEATURE_PREJIT) && !defined(DACCESS_COMPILE)
94private:
95 // Override operations from NgenHashTable - see ngenhash.h
96 bool ShouldSave(DataImage *pImage, TypeEquivalenceEntry *pEntry) { return false; }
97 bool IsHotEntry(TypeEquivalenceEntry *pEntry, CorProfileData *pProfileData) { return false; }
98 bool SaveEntry(DataImage *pImage, CorProfileData *pProfileData, TypeEquivalenceEntry *pOldEntry, TypeEquivalenceEntry *pNewEntry, EntryMappingTable *pMap) { return true; }
99 void FixupEntry(DataImage *pImage, TypeEquivalenceEntry *pEntry, void *pFixupBase, DWORD cbFixupOffset) { return; }
100#endif // FEATURE_PREJIT && !DACCESS_COMPILE
101
102private:
103#ifndef DACCESS_COMPILE
104 TypeEquivalenceHashTable(LoaderHeap *pHeap, DWORD cInitialBuckets, CrstExplicitInit *pCrst)
105 : NgenHashTable<TypeEquivalenceHashTable, TypeEquivalenceEntry, 4>(NULL, pHeap, cInitialBuckets)
106 , m_pHashTableCrst(pCrst)
107 {
108 }
109#endif // DACCESS_COMPILE
110
111 CrstExplicitInit* m_pHashTableCrst;
112};
113
114#endif // FEATURE_TYPEEQUIVALENCE
115#endif // !__TYPEEQUIVALENCE_HASH_INCLUDED
116