Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
GCHandle.cs
Go to the documentation of this file.
5
7
8public struct GCHandle
9{
10 private IntPtr _handle;
11
12 public object? Target
13 {
14 get
15 {
19 }
20 set
21 {
25 {
27 }
29 }
30 }
31
32 public bool IsAllocated => _handle != (IntPtr)0;
33
34 [MethodImpl(MethodImplOptions.InternalCall)]
35 private static extern IntPtr InternalAlloc(object value, GCHandleType type);
36
37 [MethodImpl(MethodImplOptions.InternalCall)]
38 internal static extern void InternalFree(IntPtr handle);
39
40 internal unsafe static object InternalGet(IntPtr handle)
41 {
42 return Unsafe.As<IntPtr, object>(ref *(IntPtr*)handle);
43 }
44
45 [MethodImpl(MethodImplOptions.InternalCall)]
46 internal static extern void InternalSet(IntPtr handle, object value);
47
48 [MethodImpl(MethodImplOptions.InternalCall)]
49 internal static extern object InternalCompareExchange(IntPtr handle, object value, object oldValue);
50
52 {
53 switch (type)
54 {
55 default:
57 case GCHandleType.Pinned:
59 {
61 }
62 break;
63 case GCHandleType.Weak:
64 case GCHandleType.WeakTrackResurrection:
65 case GCHandleType.Normal:
66 break;
67 }
68 nint num = InternalAlloc(value, type);
69 if (type == GCHandleType.Pinned)
70 {
71 num |= 1;
72 }
73 _handle = num;
74 }
75
77 {
79 }
80
81 public static GCHandle Alloc(object? value)
82 {
83 return new GCHandle(value, GCHandleType.Normal);
84 }
85
86 public static GCHandle Alloc(object? value, GCHandleType type)
87 {
88 return new GCHandle(value, type);
89 }
90
97
98 public unsafe IntPtr AddrOfPinnedObject()
99 {
102 if (!IsPinned(handle))
103 {
105 }
107 if (obj == null)
108 {
109 return (IntPtr)0;
110 }
112 {
113 if (obj.GetType() == typeof(string))
114 {
115 return (IntPtr)Unsafe.AsPointer(ref Unsafe.As<string>(obj).GetRawStringData());
116 }
117 return (IntPtr)Unsafe.AsPointer(ref MemoryMarshal.GetArrayDataReference(Unsafe.As<Array>(obj)));
118 }
119 return (IntPtr)Unsafe.AsPointer(ref obj.GetRawData());
120 }
121
122 public static explicit operator GCHandle(IntPtr value)
123 {
124 return FromIntPtr(value);
125 }
126
128 {
130 return new GCHandle(value);
131 }
132
133 public static explicit operator IntPtr(GCHandle value)
134 {
135 return ToIntPtr(value);
136 }
137
139 {
140 return value._handle;
141 }
142
143 public override int GetHashCode()
144 {
145 return _handle.GetHashCode();
146 }
147
148 public override bool Equals([NotNullWhen(true)] object? o)
149 {
150 if (o is GCHandle)
151 {
152 return _handle == ((GCHandle)o)._handle;
153 }
154 return false;
155 }
156
157 public static bool operator ==(GCHandle a, GCHandle b)
158 {
159 return a._handle == b._handle;
160 }
161
162 public static bool operator !=(GCHandle a, GCHandle b)
163 {
164 return a._handle != b._handle;
165 }
166
167 [MethodImpl(MethodImplOptions.AggressiveInlining)]
169 {
170 return new IntPtr((nint)handle & ~(nint)1);
171 }
172
173 [MethodImpl(MethodImplOptions.AggressiveInlining)]
174 private static bool IsPinned(IntPtr handle)
175 {
176 return ((nint)handle & 1) != 0;
177 }
178
179 [MethodImpl(MethodImplOptions.AggressiveInlining)]
180 private static void ThrowIfInvalid(IntPtr handle)
181 {
182 if (handle == (IntPtr)0)
183 {
185 }
186 }
187}
static unsafe bool ObjectHasComponentSize(object obj)
static bool IsPinnable(object obj)
static unsafe ref byte GetArrayDataReference(Array array)
static string ArgumentOutOfRange_Enum
Definition SR.cs:18
static string ArgumentException_NotIsomorphic
Definition SR.cs:920
Definition SR.cs:7
static int Exchange(ref int location1, int value)
static void ThrowInvalidOperationException_HandleIsNotInitialized()
static void ThrowInvalidOperationException_HandleIsNotPinned()
static readonly IntPtr Zero
Definition IntPtr.cs:18
unsafe override int GetHashCode()
Definition IntPtr.cs:114
static GCHandle Alloc(object? value, GCHandleType type)
Definition GCHandle.cs:86
static bool IsPinned(IntPtr handle)
Definition GCHandle.cs:174
static IntPtr ToIntPtr(GCHandle value)
Definition GCHandle.cs:138
static bool operator==(GCHandle a, GCHandle b)
Definition GCHandle.cs:157
override bool Equals([NotNullWhen(true)] object? o)
Definition GCHandle.cs:148
GCHandle(object value, GCHandleType type)
Definition GCHandle.cs:51
static bool operator!=(GCHandle a, GCHandle b)
Definition GCHandle.cs:162
static void InternalSet(IntPtr handle, object value)
static IntPtr InternalAlloc(object value, GCHandleType type)
static GCHandle Alloc(object? value)
Definition GCHandle.cs:81
static void ThrowIfInvalid(IntPtr handle)
Definition GCHandle.cs:180
static IntPtr GetHandleValue(IntPtr handle)
Definition GCHandle.cs:168
static void InternalFree(IntPtr handle)
static unsafe object InternalGet(IntPtr handle)
Definition GCHandle.cs:40
static object InternalCompareExchange(IntPtr handle, object value, object oldValue)
static GCHandle FromIntPtr(IntPtr value)
Definition GCHandle.cs:127