Merge pull request #14619 from briansull/emitter-cleanup
[platform/upstream/coreclr.git] / src / vm / loaderallocator.inl
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 #ifndef _LOADER_ALLOCATOR_I
7 #define _LOADER_ALLOCATOR_I
8
9 #include "assembly.hpp"
10
11 #ifndef DACCESS_COMPILE
12 inline LOADERALLOCATORREF LoaderAllocator::GetExposedObject() 
13
14     LIMITED_METHOD_CONTRACT;
15     OBJECTREF loaderAllocatorObject = (m_hLoaderAllocatorObjectHandle != NULL) ? ObjectFromHandle(m_hLoaderAllocatorObjectHandle) : NULL;
16     return (LOADERALLOCATORREF)loaderAllocatorObject;
17 }
18 #endif
19
20 inline void GlobalLoaderAllocator::Init(BaseDomain *pDomain) 
21 {
22     LoaderAllocator::Init(pDomain, m_ExecutableHeapInstance);
23 }
24
25 inline void AppDomainLoaderAllocator::Init(AppDomain *pAppDomain) 
26 {
27     WRAPPER_NO_CONTRACT;
28     m_Id.Init(pAppDomain);
29     LoaderAllocator::Init((BaseDomain *)pAppDomain);
30 }
31
32 inline void LoaderAllocatorID::Init(AppDomain *pAppDomain)
33 {
34     m_type = LAT_AppDomain;
35     m_pAppDomain = pAppDomain;
36 }
37
38 inline void AssemblyLoaderAllocator::Init(AppDomain* pAppDomain)
39 {
40     m_Id.Init();
41     LoaderAllocator::Init((BaseDomain *)pAppDomain);
42 }
43
44 inline BOOL LoaderAllocatorID::Equals(LoaderAllocatorID *pId)
45 {
46     LIMITED_METHOD_CONTRACT;
47     if (GetType() != pId->GetType())
48         return false;
49
50     return GetValue() == pId->GetValue();
51 }
52
53 inline void LoaderAllocatorID::Init()
54 {
55     LIMITED_METHOD_CONTRACT;
56     m_type = LAT_Assembly;
57 };
58
59 inline void LoaderAllocatorID::SetDomainAssembly(DomainAssembly* pAssembly)
60 {
61     LIMITED_METHOD_CONTRACT;
62     _ASSERTE(m_type == LAT_Assembly);
63     m_pDomainAssembly = pAssembly;
64 }
65
66 inline VOID* LoaderAllocatorID::GetValue()
67 {
68         LIMITED_METHOD_DAC_CONTRACT;
69     return m_pValue;
70 }
71
72 inline COUNT_T LoaderAllocatorID::Hash()
73 {
74         LIMITED_METHOD_DAC_CONTRACT;
75     return (COUNT_T)(SIZE_T)GetValue();
76 }
77
78 inline LoaderAllocatorType LoaderAllocatorID::GetType()
79 {
80         LIMITED_METHOD_DAC_CONTRACT;
81     return m_type;
82 }
83
84 inline DomainAssembly* LoaderAllocatorID::GetDomainAssembly()
85 {
86         LIMITED_METHOD_DAC_CONTRACT;
87     _ASSERTE(m_type == LAT_Assembly);
88     return m_pDomainAssembly;
89 }
90
91 inline AppDomain *LoaderAllocatorID::GetAppDomain()
92 {
93         LIMITED_METHOD_DAC_CONTRACT;
94     _ASSERTE(m_type == LAT_AppDomain);
95     return m_pAppDomain;
96 }
97
98 inline BOOL LoaderAllocatorID::IsCollectible()
99 {
100     LIMITED_METHOD_DAC_CONTRACT; 
101     return m_type == LAT_Assembly;
102 }
103
104 inline LoaderAllocatorID* AssemblyLoaderAllocator::Id()
105 {
106     LIMITED_METHOD_DAC_CONTRACT; 
107     return &m_Id;
108 }
109
110 inline LoaderAllocatorID* GlobalLoaderAllocator::Id()
111 {
112     LIMITED_METHOD_DAC_CONTRACT; 
113     return &m_Id;
114 }
115
116 inline LoaderAllocatorID* AppDomainLoaderAllocator::Id()
117 {
118     LIMITED_METHOD_DAC_CONTRACT;
119     return &m_Id;
120 }
121
122 /* static */
123 FORCEINLINE BOOL LoaderAllocator::GetHandleValueFast(LOADERHANDLE handle, OBJECTREF *pValue)
124 {
125     LIMITED_METHOD_CONTRACT;
126
127     // If the slot value does have the low bit set, then it is a simple pointer to the value
128     // Otherwise, we will need a more complicated operation to get the value.
129     if ((((UINT_PTR)handle) & 1) != 0)
130     {
131         *pValue = *((OBJECTREF *)(((UINT_PTR)handle) - 1));
132         return TRUE;
133     }
134     else
135     {
136         return FALSE;
137     }
138 }
139
140 FORCEINLINE BOOL LoaderAllocator::GetHandleValueFastPhase2(LOADERHANDLE handle, OBJECTREF *pValue)
141 {
142     SUPPORTS_DAC;
143     STATIC_CONTRACT_NOTHROW;
144     STATIC_CONTRACT_MODE_COOPERATIVE;
145     STATIC_CONTRACT_NOTHROW;
146     STATIC_CONTRACT_GC_NOTRIGGER;
147
148     if (handle == 0)
149         return FALSE;
150
151     /* This is lockless access to the handle table, be careful */
152     OBJECTREF loaderAllocatorAsObjectRef = ObjectFromHandle(m_hLoaderAllocatorObjectHandle);
153
154     // If the managed loader allocator has been collected, then the handles associated with it are dead as well.
155     if (loaderAllocatorAsObjectRef == NULL)
156         return FALSE;
157
158     LOADERALLOCATORREF loaderAllocator = dac_cast<LOADERALLOCATORREF>(loaderAllocatorAsObjectRef);
159     PTRARRAYREF handleTable = loaderAllocator->GetHandleTable();
160     UINT_PTR index = (((UINT_PTR)handle) >> 1) - 1;
161     *pValue = handleTable->GetAt(index);
162
163     return TRUE;
164 }
165
166 FORCEINLINE OBJECTREF LoaderAllocator::GetHandleValueFastCannotFailType2(LOADERHANDLE handle)
167 {
168     SUPPORTS_DAC;
169     STATIC_CONTRACT_NOTHROW;
170     STATIC_CONTRACT_MODE_COOPERATIVE;
171     STATIC_CONTRACT_NOTHROW;
172     STATIC_CONTRACT_GC_NOTRIGGER;
173
174     /* This is lockless access to the handle table, be careful */
175     OBJECTREF loaderAllocatorAsObjectRef = ObjectFromHandle(m_hLoaderAllocatorObjectHandle);
176     LOADERALLOCATORREF loaderAllocator = dac_cast<LOADERALLOCATORREF>(loaderAllocatorAsObjectRef);
177     PTRARRAYREF handleTable = loaderAllocator->GetHandleTable();
178     UINT_PTR index = (((UINT_PTR)handle) >> 1) - 1;
179
180     return handleTable->GetAt(index);
181 }
182 #endif //  _LOADER_ALLOCATOR_I
183