Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
GC.cs
Go to the documentation of this file.
4
5namespace System;
6
7public static class GC
8{
9 internal enum GC_ALLOC_FLAGS
10 {
14 }
15
23
31
32 public static int MaxGeneration => GetMaxGeneration();
33
34 [MethodImpl(MethodImplOptions.InternalCall)]
35 private static extern void GetMemoryInfo(GCMemoryInfoData data, int kind);
36
38 {
39 return GetGCMemoryInfo(GCKind.Any);
40 }
41
43 {
44 if (kind < GCKind.Any || kind > GCKind.Background)
45 {
47 }
49 GetMemoryInfo(data, (int)kind);
50 return new GCMemoryInfo(data);
51 }
52
53 [DllImport("QCall", CharSet = CharSet.Unicode)]
54 internal static extern int _StartNoGCRegion(long totalSize, bool lohSizeKnown, long lohSize, bool disallowFullBlockingGC);
55
56 [DllImport("QCall", CharSet = CharSet.Unicode)]
57 internal static extern int _EndNoGCRegion();
58
59 [MethodImpl(MethodImplOptions.InternalCall)]
60 internal static extern Array AllocateNewArray(IntPtr typeHandle, int length, GC_ALLOC_FLAGS flags);
61
62 [MethodImpl(MethodImplOptions.InternalCall)]
63 private static extern int GetGenerationWR(IntPtr handle);
64
65 [DllImport("QCall", CharSet = CharSet.Unicode)]
66 private static extern long GetTotalMemory();
67
68 [DllImport("QCall", CharSet = CharSet.Unicode)]
69 private static extern void _Collect(int generation, int mode);
70
71 [MethodImpl(MethodImplOptions.InternalCall)]
72 private static extern int GetMaxGeneration();
73
74 [MethodImpl(MethodImplOptions.InternalCall)]
75 private static extern int _CollectionCount(int generation, int getSpecialGCCount);
76
77 [MethodImpl(MethodImplOptions.InternalCall)]
78 internal static extern ulong GetSegmentSize();
79
80 [MethodImpl(MethodImplOptions.InternalCall)]
81 internal static extern int GetLastGCPercentTimeInGC();
82
83 [MethodImpl(MethodImplOptions.InternalCall)]
84 internal static extern ulong GetGenerationSize(int gen);
85
86 [DllImport("QCall", CharSet = CharSet.Unicode)]
87 private static extern void _AddMemoryPressure(ulong bytesAllocated);
88
89 [DllImport("QCall", CharSet = CharSet.Unicode)]
90 private static extern void _RemoveMemoryPressure(ulong bytesAllocated);
91
92 public static void AddMemoryPressure(long bytesAllocated)
93 {
94 if (bytesAllocated <= 0)
95 {
97 }
98 if (4 == IntPtr.Size)
99 {
100 }
101 _AddMemoryPressure((ulong)bytesAllocated);
102 }
103
104 public static void RemoveMemoryPressure(long bytesAllocated)
105 {
106 if (bytesAllocated <= 0)
107 {
109 }
110 if (4 == IntPtr.Size)
111 {
112 }
113 _RemoveMemoryPressure((ulong)bytesAllocated);
114 }
115
116 [MethodImpl(MethodImplOptions.InternalCall)]
117 public static extern int GetGeneration(object obj);
118
119 public static void Collect(int generation)
120 {
121 Collect(generation, GCCollectionMode.Default);
122 }
123
124 public static void Collect()
125 {
126 _Collect(-1, 2);
127 }
128
129 public static void Collect(int generation, GCCollectionMode mode)
130 {
131 Collect(generation, mode, blocking: true);
132 }
133
134 public static void Collect(int generation, GCCollectionMode mode, bool blocking)
135 {
136 Collect(generation, mode, blocking, compacting: false);
137 }
138
139 public static void Collect(int generation, GCCollectionMode mode, bool blocking, bool compacting)
140 {
141 if (generation < 0)
142 {
144 }
145 if (mode < GCCollectionMode.Default || mode > GCCollectionMode.Optimized)
146 {
148 }
149 int num = 0;
150 if (mode == GCCollectionMode.Optimized)
151 {
152 num |= 4;
153 }
154 if (compacting)
155 {
156 num |= 8;
157 }
158 if (blocking)
159 {
160 num |= 2;
161 }
162 else if (!compacting)
163 {
164 num |= 1;
165 }
166 _Collect(generation, num);
167 }
168
169 public static int CollectionCount(int generation)
170 {
171 if (generation < 0)
172 {
174 }
175 return _CollectionCount(generation, 0);
176 }
177
178 [MethodImpl(MethodImplOptions.NoInlining)]
179 [Intrinsic]
180 public static void KeepAlive(object? obj)
181 {
182 }
183
184 public static int GetGeneration(WeakReference wo)
185 {
186 int generationWR = GetGenerationWR(wo.m_handle);
187 KeepAlive(wo);
188 return generationWR;
189 }
190
191 [DllImport("QCall", CharSet = CharSet.Unicode)]
192 private static extern void _WaitForPendingFinalizers();
193
194 public static void WaitForPendingFinalizers()
195 {
197 }
198
199 [MethodImpl(MethodImplOptions.InternalCall)]
200 private static extern void _SuppressFinalize(object o);
201
202 public static void SuppressFinalize(object obj)
203 {
204 if (obj == null)
205 {
206 throw new ArgumentNullException("obj");
207 }
209 }
210
211 [MethodImpl(MethodImplOptions.InternalCall)]
212 private static extern void _ReRegisterForFinalize(object o);
213
214 public static void ReRegisterForFinalize(object obj)
215 {
216 if (obj == null)
217 {
218 throw new ArgumentNullException("obj");
219 }
221 }
222
223 public static long GetTotalMemory(bool forceFullCollection)
224 {
225 long totalMemory = GetTotalMemory();
226 if (!forceFullCollection)
227 {
228 return totalMemory;
229 }
230 int num = 20;
231 long num2 = totalMemory;
232 float num3;
233 do
234 {
236 Collect();
237 totalMemory = num2;
238 num2 = GetTotalMemory();
239 num3 = (float)(num2 - totalMemory) / (float)totalMemory;
240 }
241 while (num-- > 0 && (!(-0.05 < (double)num3) || !((double)num3 < 0.05)));
242 return num2;
243 }
244
245 [DllImport("QCall", CharSet = CharSet.Unicode)]
246 private static extern IntPtr _RegisterFrozenSegment(IntPtr sectionAddress, nint sectionSize);
247
248 [DllImport("QCall", CharSet = CharSet.Unicode)]
249 private static extern void _UnregisterFrozenSegment(IntPtr segmentHandle);
250
251 [MethodImpl(MethodImplOptions.InternalCall)]
252 public static extern long GetAllocatedBytesForCurrentThread();
253
254 [MethodImpl(MethodImplOptions.InternalCall)]
255 public static extern long GetTotalAllocatedBytes(bool precise = false);
256
257 [MethodImpl(MethodImplOptions.InternalCall)]
258 private static extern bool _RegisterForFullGCNotification(int maxGenerationPercentage, int largeObjectHeapPercentage);
259
260 [MethodImpl(MethodImplOptions.InternalCall)]
261 private static extern bool _CancelFullGCNotification();
262
263 [MethodImpl(MethodImplOptions.InternalCall)]
264 private static extern int _WaitForFullGCApproach(int millisecondsTimeout);
265
266 [MethodImpl(MethodImplOptions.InternalCall)]
267 private static extern int _WaitForFullGCComplete(int millisecondsTimeout);
268
269 public static void RegisterForFullGCNotification(int maxGenerationThreshold, int largeObjectHeapThreshold)
270 {
271 if (maxGenerationThreshold <= 0 || maxGenerationThreshold >= 100)
272 {
273 throw new ArgumentOutOfRangeException("maxGenerationThreshold", SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, 1, 99));
274 }
275 if (largeObjectHeapThreshold <= 0 || largeObjectHeapThreshold >= 100)
276 {
277 throw new ArgumentOutOfRangeException("largeObjectHeapThreshold", SR.Format(SR.ArgumentOutOfRange_Bounds_Lower_Upper, 1, 99));
278 }
279 if (!_RegisterForFullGCNotification(maxGenerationThreshold, largeObjectHeapThreshold))
280 {
282 }
283 }
284
285 public static void CancelFullGCNotification()
286 {
288 {
290 }
291 }
292
297
306
311
320
321 private static bool StartNoGCRegionWorker(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC)
322 {
323 if (totalSize <= 0)
324 {
325 throw new ArgumentOutOfRangeException("totalSize", "totalSize can't be zero or negative");
326 }
327 if (hasLohSize)
328 {
329 if (lohSize <= 0)
330 {
331 throw new ArgumentOutOfRangeException("lohSize", "lohSize can't be zero or negative");
332 }
333 if (lohSize > totalSize)
334 {
335 throw new ArgumentOutOfRangeException("lohSize", "lohSize can't be greater than totalSize");
336 }
337 }
338 return (StartNoGCRegionStatus)_StartNoGCRegion(totalSize, hasLohSize, lohSize, disallowFullBlockingGC) switch
339 {
340 StartNoGCRegionStatus.NotEnoughMemory => false,
341 StartNoGCRegionStatus.AlreadyInProgress => throw new InvalidOperationException("The NoGCRegion mode was already in progress"),
342 StartNoGCRegionStatus.AmountTooLarge => throw new ArgumentOutOfRangeException("totalSize", "totalSize is too large. For more information about setting the maximum size, see \"Latency Modes\" in https://go.microsoft.com/fwlink/?LinkId=522706"),
343 _ => true,
344 };
345 }
346
347 public static bool TryStartNoGCRegion(long totalSize)
348 {
349 return StartNoGCRegionWorker(totalSize, hasLohSize: false, 0L, disallowFullBlockingGC: false);
350 }
351
352 public static bool TryStartNoGCRegion(long totalSize, long lohSize)
353 {
354 return StartNoGCRegionWorker(totalSize, hasLohSize: true, lohSize, disallowFullBlockingGC: false);
355 }
356
357 public static bool TryStartNoGCRegion(long totalSize, bool disallowFullBlockingGC)
358 {
359 return StartNoGCRegionWorker(totalSize, hasLohSize: false, 0L, disallowFullBlockingGC);
360 }
361
362 public static bool TryStartNoGCRegion(long totalSize, long lohSize, bool disallowFullBlockingGC)
363 {
364 return StartNoGCRegionWorker(totalSize, hasLohSize: true, lohSize, disallowFullBlockingGC);
365 }
366
367 public static void EndNoGCRegion()
368 {
370 {
371 case EndNoGCRegionStatus.NotInProgress:
372 throw new InvalidOperationException("NoGCRegion mode must be set");
373 case EndNoGCRegionStatus.GCInduced:
374 throw new InvalidOperationException("Garbage collection was induced in NoGCRegion mode");
375 case EndNoGCRegionStatus.AllocationExceeded:
376 throw new InvalidOperationException("Allocated memory exceeds specified memory for NoGCRegion mode");
377 }
378 }
379
380 [MethodImpl(MethodImplOptions.AggressiveInlining)]
381 public static T[] AllocateUninitializedArray<T>(int length, bool pinned = false)
382 {
383 if (!pinned)
384 {
385 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
386 {
387 return new T[length];
388 }
389 if (length < 2048 / Unsafe.SizeOf<T>())
390 {
391 return new T[length];
392 }
393 }
394 else if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
395 {
397 }
398 return AllocateNewUninitializedArray(length, pinned);
399 static T[] AllocateNewUninitializedArray(int length, bool pinned)
400 {
401 GC_ALLOC_FLAGS gC_ALLOC_FLAGS = GC_ALLOC_FLAGS.GC_ALLOC_ZEROING_OPTIONAL;
402 if (pinned)
403 {
404 gC_ALLOC_FLAGS |= GC_ALLOC_FLAGS.GC_ALLOC_PINNED_OBJECT_HEAP;
405 }
406 return Unsafe.As<T[]>(AllocateNewArray(typeof(T[]).TypeHandle.Value, length, gC_ALLOC_FLAGS));
407 }
408 }
409
410 public static T[] AllocateArray<T>(int length, bool pinned = false)
411 {
412 GC_ALLOC_FLAGS flags = GC_ALLOC_FLAGS.GC_ALLOC_NO_FLAGS;
413 if (pinned)
414 {
415 if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
416 {
418 }
419 flags = GC_ALLOC_FLAGS.GC_ALLOC_PINNED_OBJECT_HEAP;
420 }
421 return Unsafe.As<T[]>(AllocateNewArray(typeof(T[]).TypeHandle.Value, length, flags));
422 }
423}
static long GetTotalAllocatedBytes(bool precise=false)
static int _WaitForFullGCApproach(int millisecondsTimeout)
static void AddMemoryPressure(long bytesAllocated)
Definition GC.cs:92
static int _EndNoGCRegion()
static int CollectionCount(int generation)
Definition GC.cs:169
static IntPtr _RegisterFrozenSegment(IntPtr sectionAddress, nint sectionSize)
static void _WaitForPendingFinalizers()
static bool TryStartNoGCRegion(long totalSize, bool disallowFullBlockingGC)
Definition GC.cs:357
static long GetAllocatedBytesForCurrentThread()
StartNoGCRegionStatus
Definition GC.cs:17
static void RegisterForFullGCNotification(int maxGenerationThreshold, int largeObjectHeapThreshold)
Definition GC.cs:269
static void Collect()
Definition GC.cs:124
static int _WaitForFullGCComplete(int millisecondsTimeout)
static bool _CancelFullGCNotification()
static int _CollectionCount(int generation, int getSpecialGCCount)
static ulong GetGenerationSize(int gen)
static long GetTotalMemory()
static void SuppressFinalize(object obj)
Definition GC.cs:202
static GCMemoryInfo GetGCMemoryInfo(GCKind kind)
Definition GC.cs:42
static void _UnregisterFrozenSegment(IntPtr segmentHandle)
static GCMemoryInfo GetGCMemoryInfo()
Definition GC.cs:37
static void _RemoveMemoryPressure(ulong bytesAllocated)
static GCNotificationStatus WaitForFullGCComplete(int millisecondsTimeout)
Definition GC.cs:312
static int GetGeneration(WeakReference wo)
Definition GC.cs:184
static GCNotificationStatus WaitForFullGCApproach(int millisecondsTimeout)
Definition GC.cs:298
static void RemoveMemoryPressure(long bytesAllocated)
Definition GC.cs:104
static GCNotificationStatus WaitForFullGCApproach()
Definition GC.cs:293
static int GetLastGCPercentTimeInGC()
static void _ReRegisterForFinalize(object o)
static int GetMaxGeneration()
static int GetGeneration(object obj)
static int MaxGeneration
Definition GC.cs:32
static int _StartNoGCRegion(long totalSize, bool lohSizeKnown, long lohSize, bool disallowFullBlockingGC)
static bool StartNoGCRegionWorker(long totalSize, bool hasLohSize, long lohSize, bool disallowFullBlockingGC)
Definition GC.cs:321
static void _SuppressFinalize(object o)
static ulong GetSegmentSize()
static void Collect(int generation)
Definition GC.cs:119
static GCNotificationStatus WaitForFullGCComplete()
Definition GC.cs:307
static int GetGenerationWR(IntPtr handle)
static Array AllocateNewArray(IntPtr typeHandle, int length, GC_ALLOC_FLAGS flags)
static bool _RegisterForFullGCNotification(int maxGenerationPercentage, int largeObjectHeapPercentage)
static void GetMemoryInfo(GCMemoryInfoData data, int kind)
static T[] AllocateArray< T >(int length, bool pinned=false)
Definition GC.cs:410
static void _AddMemoryPressure(ulong bytesAllocated)
EndNoGCRegionStatus
Definition GC.cs:25
static bool TryStartNoGCRegion(long totalSize)
Definition GC.cs:347
GC_ALLOC_FLAGS
Definition GC.cs:10
static bool TryStartNoGCRegion(long totalSize, long lohSize, bool disallowFullBlockingGC)
Definition GC.cs:362
static void KeepAlive(object? obj)
Definition GC.cs:180
static void EndNoGCRegion()
Definition GC.cs:367
static void Collect(int generation, GCCollectionMode mode, bool blocking)
Definition GC.cs:134
static bool TryStartNoGCRegion(long totalSize, long lohSize)
Definition GC.cs:352
static long GetTotalMemory(bool forceFullCollection)
Definition GC.cs:223
static void _Collect(int generation, int mode)
static T[] AllocateUninitializedArray< T >(int length, bool pinned=false)
Definition GC.cs:381
static void ReRegisterForFinalize(object obj)
Definition GC.cs:214
static void Collect(int generation, GCCollectionMode mode)
Definition GC.cs:129
static void WaitForPendingFinalizers()
Definition GC.cs:194
static void Collect(int generation, GCCollectionMode mode, bool blocking, bool compacting)
Definition GC.cs:139
static void CancelFullGCNotification()
Definition GC.cs:285
Definition GC.cs:8
static string ArgumentOutOfRange_NeedPosNum
Definition SR.cs:20
static string ArgumentOutOfRange_Enum
Definition SR.cs:18
static string Format(string resourceFormat, object p1)
Definition SR.cs:118
static string InvalidOperation_NotWithConcurrentGC
Definition SR.cs:1486
static string ArgumentOutOfRange_NeedNonNegOrNegative1
Definition SR.cs:1072
static string ArgumentOutOfRange_GenericPositive
Definition SR.cs:1018
static string ArgumentOutOfRange_Bounds_Lower_Upper
Definition SR.cs:980
Definition SR.cs:7
static void ThrowInvalidTypeWithPointersNotSupported(Type targetType)
GCKind
Definition GCKind.cs:4
static int Size
Definition IntPtr.cs:21