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#include "common.h"
6#include "gchandleutilities.h"
7
8IGCHandleManager* g_pGCHandleManager = nullptr;
9
10// Debug-only validation for handle.
11
12void ValidateObjectAndAppDomain(OBJECTREF objRef, ADIndex appDomainIndex)
13{
14#ifdef _DEBUG_IMPL
15 VALIDATEOBJECTREF(objRef);
16
17 AppDomain *domain = SystemDomain::GetAppDomainAtIndex(appDomainIndex);
18
19 // Access to a handle in an unloaded domain is not allowed
20 assert(domain != nullptr);
21
22#endif // _DEBUG_IMPL
23}
24
25void ValidateHandleAssignment(OBJECTHANDLE handle, OBJECTREF objRef)
26{
27#ifdef _DEBUG_IMPL
28 _ASSERTE(handle);
29
30#ifdef DEBUG_DestroyedHandleValue
31 // Verify that we are not trying to access a freed handle.
32 _ASSERTE("Attempt to access destroyed handle." && *(_UNCHECKED_OBJECTREF*)handle != DEBUG_DestroyedHandleValue);
33#endif
34
35 IGCHandleManager *mgr = GCHandleUtilities::GetGCHandleManager();
36 ADIndex appDomainIndex = ADIndex(reinterpret_cast<DWORD>(mgr->GetHandleContext(handle)));
37
38 ValidateObjectAndAppDomain(objRef, appDomainIndex);
39#endif // _DEBUG_IMPL
40}
41
42void DiagHandleCreated(OBJECTHANDLE handle, OBJECTREF objRef)
43{
44#ifdef GC_PROFILING
45 BEGIN_PIN_PROFILER(CORProfilerTrackGC());
46 g_profControlBlock.pProfInterface->HandleCreated((uintptr_t)handle, (ObjectID)OBJECTREF_TO_UNCHECKED_OBJECTREF(objRef));
47 END_PIN_PROFILER();
48#else
49 UNREFERENCED_PARAMETER(handle);
50 UNREFERENCED_PARAMETER(objRef);
51#endif // GC_PROFILING
52}
53
54void DiagHandleDestroyed(OBJECTHANDLE handle)
55{
56#ifdef GC_PROFILING
57 BEGIN_PIN_PROFILER(CORProfilerTrackGC());
58 g_profControlBlock.pProfInterface->HandleDestroyed((uintptr_t)handle);
59 END_PIN_PROFILER();
60#else
61 UNREFERENCED_PARAMETER(handle);
62#endif // GC_PROFILING
63}
64