summaryrefslogtreecommitdiff
path: root/src/vm/typeequivalencehash.cpp
blob: ca2ecfdb2dfd0933ebffa7da2d62f88b0c295c00 (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
// 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).
//

#include "common.h"
#include "typeequivalencehash.hpp"
#include "ngenhash.inl"

#ifdef FEATURE_TYPEEQUIVALENCE
TypeEquivalenceHashTable::EquivalenceMatch TypeEquivalenceHashTable::CheckEquivalence(TypeHandle thA, TypeHandle thB)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
    }
    CONTRACTL_END;

    LookupContext lookupContext;
    NgenHashValue hash = TypeEquivalenceEntry::HashTypeHandles(thA, thB);

    PTR_TypeEquivalenceEntry search = BaseFindFirstEntryByHash(hash, &lookupContext);
    while (search != NULL)
    {
        if (search->Match(thA, thB))
        {
            return search->GetEquivalence() ? Match : NoMatch;
        }

        search = BaseFindNextEntryByHash(&lookupContext);
    }
    return MatchUnknown;
}

#ifndef DACCESS_COMPILE
/*static*/
TypeEquivalenceHashTable *TypeEquivalenceHashTable::Create(AppDomain *pAppDomain, DWORD dwNumBuckets, CrstExplicitInit *pCrst)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM(););
    }
    CONTRACTL_END;

    AllocMemTracker amt;
    LoaderHeap *pHeap = pAppDomain->GetLowFrequencyHeap();
    TypeEquivalenceHashTable *pThis = (TypeEquivalenceHashTable*)amt.Track(pHeap->AllocMem((S_SIZE_T)sizeof(TypeEquivalenceHashTable)));

    // The base class get initialized through chaining of constructors. We allocated the hash instance via the
    // loader heap instead of new so use an in-place new to call the constructors now.
    new (pThis) TypeEquivalenceHashTable(pHeap, dwNumBuckets, pCrst);
    amt.SuppressRelease();

    return pThis;
}

void TypeEquivalenceHashTable::RecordEquivalence(TypeHandle thA, TypeHandle thB, TypeEquivalenceHashTable::EquivalenceMatch match)
{
    CONTRACTL
    {
        THROWS;
        GC_TRIGGERS;
        MODE_ANY;
        INJECT_FAULT(COMPlusThrowOM(););
        PRECONDITION(match != TypeEquivalenceHashTable::MatchUnknown);
    }
    CONTRACTL_END;

    CrstHolder ch(m_pHashTableCrst);

    // Was there a race in calculating equivalence and this thread lost?
    // If so, return
    EquivalenceMatch checkedMatch = CheckEquivalence(thA, thB);
    if (checkedMatch != TypeEquivalenceHashTable::MatchUnknown)
    {
        _ASSERTE(checkedMatch == match);
        return;
    }

    AllocMemTracker amt;
    PTR_TypeEquivalenceEntry pNewEntry = BaseAllocateEntry(&amt);
    amt.SuppressRelease();

    pNewEntry->SetData(thA, thB, match == TypeEquivalenceHashTable::Match ? true : false);
    NgenHashValue hash = TypeEquivalenceEntry::HashTypeHandles(thA, thB);

    BaseInsertEntry(hash, pNewEntry);
}
#endif // !DACCESS_COMPILE
#endif // FEATURE_TYPEEQUIVALENCE