summaryrefslogtreecommitdiff
path: root/src/vm/loaderallocator.inl
blob: 3f23ac9c8c14b89bba7f7697253e2093772ed907 (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
// 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 _LOADER_ALLOCATOR_I
#define _LOADER_ALLOCATOR_I

#include "assembly.hpp"

#ifndef DACCESS_COMPILE
inline LOADERALLOCATORREF LoaderAllocator::GetExposedObject() 
{ 
    LIMITED_METHOD_CONTRACT;
    OBJECTREF loaderAllocatorObject = (m_hLoaderAllocatorObjectHandle != NULL) ? ObjectFromHandle(m_hLoaderAllocatorObjectHandle) : NULL;
    return (LOADERALLOCATORREF)loaderAllocatorObject;
}
#endif

inline void GlobalLoaderAllocator::Init(BaseDomain *pDomain) 
{
    LoaderAllocator::Init(pDomain, m_ExecutableHeapInstance);
}

inline void AppDomainLoaderAllocator::Init(AppDomain *pAppDomain) 
{
    WRAPPER_NO_CONTRACT;
    m_Id.Init(pAppDomain);
    LoaderAllocator::Init((BaseDomain *)pAppDomain);
}

inline void LoaderAllocatorID::Init(AppDomain *pAppDomain)
{
    m_type = LAT_AppDomain;
    m_pAppDomain = pAppDomain;
}

inline void AssemblyLoaderAllocator::Init(AppDomain* pAppDomain)
{
    m_Id.Init();
    LoaderAllocator::Init((BaseDomain *)pAppDomain);
}

inline BOOL LoaderAllocatorID::Equals(LoaderAllocatorID *pId)
{
    LIMITED_METHOD_CONTRACT;
    if (GetType() != pId->GetType())
        return false;

    return GetValue() == pId->GetValue();
}

inline void LoaderAllocatorID::Init()
{
    LIMITED_METHOD_CONTRACT;
    m_type = LAT_Assembly;
};

inline void LoaderAllocatorID::SetDomainAssembly(DomainAssembly* pAssembly)
{
    LIMITED_METHOD_CONTRACT;
    _ASSERTE(m_type == LAT_Assembly);
    m_pDomainAssembly = pAssembly;
}

inline VOID* LoaderAllocatorID::GetValue()
{
	LIMITED_METHOD_DAC_CONTRACT;
    return m_pValue;
}

inline COUNT_T LoaderAllocatorID::Hash()
{
	LIMITED_METHOD_DAC_CONTRACT;
    return (COUNT_T)(SIZE_T)GetValue();
}

inline LoaderAllocatorType LoaderAllocatorID::GetType()
{
	LIMITED_METHOD_DAC_CONTRACT;
    return m_type;
}

inline DomainAssembly* LoaderAllocatorID::GetDomainAssembly()
{
	LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE(m_type == LAT_Assembly);
    return m_pDomainAssembly;
}

inline AppDomain *LoaderAllocatorID::GetAppDomain()
{
	LIMITED_METHOD_DAC_CONTRACT;
    _ASSERTE(m_type == LAT_AppDomain);
    return m_pAppDomain;
}

inline BOOL LoaderAllocatorID::IsCollectible()
{
    LIMITED_METHOD_DAC_CONTRACT; 
    return m_type == LAT_Assembly;
}

inline LoaderAllocatorID* AssemblyLoaderAllocator::Id()
{
    LIMITED_METHOD_DAC_CONTRACT; 
    return &m_Id;
}

inline LoaderAllocatorID* GlobalLoaderAllocator::Id()
{
    LIMITED_METHOD_DAC_CONTRACT; 
    return &m_Id;
}

inline LoaderAllocatorID* AppDomainLoaderAllocator::Id()
{
    LIMITED_METHOD_DAC_CONTRACT;
    return &m_Id;
}

/* static */
FORCEINLINE BOOL LoaderAllocator::GetHandleValueFast(LOADERHANDLE handle, OBJECTREF *pValue)
{
    LIMITED_METHOD_CONTRACT;

    // If the slot value does have the low bit set, then it is a simple pointer to the value
    // Otherwise, we will need a more complicated operation to get the value.
    if ((((UINT_PTR)handle) & 1) != 0)
    {
        *pValue = *((OBJECTREF *)(((UINT_PTR)handle) - 1));
        return TRUE;
    }
    else
    {
        return FALSE;
    }
}

FORCEINLINE BOOL LoaderAllocator::GetHandleValueFastPhase2(LOADERHANDLE handle, OBJECTREF *pValue)
{
    SUPPORTS_DAC;
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_MODE_COOPERATIVE;
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_GC_NOTRIGGER;

    if (handle == 0)
        return FALSE;

    /* This is lockless access to the handle table, be careful */
    OBJECTREF loaderAllocatorAsObjectRef = ObjectFromHandle(m_hLoaderAllocatorObjectHandle);

    // If the managed loader allocator has been collected, then the handles associated with it are dead as well.
    if (loaderAllocatorAsObjectRef == NULL)
        return FALSE;

    LOADERALLOCATORREF loaderAllocator = dac_cast<LOADERALLOCATORREF>(loaderAllocatorAsObjectRef);
    PTRARRAYREF handleTable = loaderAllocator->GetHandleTable();
    UINT_PTR index = (((UINT_PTR)handle) >> 1) - 1;
    *pValue = handleTable->GetAt(index);

    return TRUE;
}

FORCEINLINE OBJECTREF LoaderAllocator::GetHandleValueFastCannotFailType2(LOADERHANDLE handle)
{
    SUPPORTS_DAC;
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_MODE_COOPERATIVE;
    STATIC_CONTRACT_NOTHROW;
    STATIC_CONTRACT_GC_NOTRIGGER;

    /* This is lockless access to the handle table, be careful */
    OBJECTREF loaderAllocatorAsObjectRef = ObjectFromHandle(m_hLoaderAllocatorObjectHandle);
    LOADERALLOCATORREF loaderAllocator = dac_cast<LOADERALLOCATORREF>(loaderAllocatorAsObjectRef);
    PTRARRAYREF handleTable = loaderAllocator->GetHandleTable();
    UINT_PTR index = (((UINT_PTR)handle) >> 1) - 1;

    return handleTable->GetAt(index);
}
#endif //  _LOADER_ALLOCATOR_I