5 using System.Diagnostics.Tracing;
7 using System.Runtime.InteropServices;
8 using System.Runtime.ConstrainedExecution;
9 using System.Collections.Generic;
10 using System.Collections.Concurrent;
11 using System.Threading;
12 using System.Runtime.CompilerServices;
13 using System.Diagnostics;
14 using System.Security.Permissions;
16 #if PINNABLEBUFFERCACHE_MSCORLIB
17 namespace System.Threading
22 internal sealed class PinnableBufferCache
25 /// Create a new cache for pinned byte[] buffers
27 /// <param name="cacheName">A name used in diagnostic messages</param>
28 /// <param name="numberOfElements">The size of byte[] buffers in the cache (they are all the same size)</param>
29 public PinnableBufferCache(string cacheName, int numberOfElements) : this(cacheName, () => new byte[numberOfElements]) { }
32 /// Get a buffer from the buffer manager. If no buffers exist, allocate a new one.
34 public byte[] AllocateBuffer() { return (byte[])Allocate(); }
37 /// Return a buffer back to the buffer manager.
39 public void FreeBuffer(byte[] buffer) { Free(buffer); }
42 /// Create a PinnableBufferCache that works on any object (it is intended for OverlappedData)
43 /// This is only used in mscorlib.
45 #if (ENABLE || MINBUFFERS)
46 [EnvironmentPermission(SecurityAction.Assert, Unrestricted = true)]
47 [System.Security.SecuritySafeCritical]
49 internal PinnableBufferCache(string cacheName, Func<object> factory)
51 m_NotGen2 = new List<object>(DefaultNumberOfBuffers);
54 // Check to see if we should disable the cache.
55 string envVarName = "PinnableBufferCache_" + cacheName + "_Disabled";
58 string envVar = Environment.GetEnvironmentVariable(envVarName);
61 PinnableBufferCacheEventSource.Log.DebugMessage("Creating " + cacheName + " PinnableBufferCacheDisabled=" + envVar);
62 int index = envVar.IndexOf(cacheName, StringComparison.OrdinalIgnoreCase);
65 // The cache is disabled because we haven't set the cache name.
66 PinnableBufferCacheEventSource.Log.DebugMessage("Disabling " + cacheName);
73 // Ignore failures when reading the environment variable.
77 // Allow the environment to specify a minimum buffer count.
78 string minEnvVarName = "PinnableBufferCache_" + cacheName + "_MinCount";
81 string minEnvVar = Environment.GetEnvironmentVariable(minEnvVarName);
82 if (minEnvVar != null)
84 if (int.TryParse(minEnvVar, out m_minBufferCount))
90 // Ignore failures when reading the environment variable.
94 PinnableBufferCacheEventSource.Log.Create(cacheName);
95 m_CacheName = cacheName;
99 /// Get a object from the buffer manager. If no buffers exist, allocate a new one.
101 [System.Security.SecuritySafeCritical]
102 internal object Allocate()
105 // Check to see whether or not the cache is disabled.
106 if (m_CacheName == null)
109 // Fast path, get it from our Gen2 aged m_FreeList.
111 if (!m_FreeList.TryPop(out returnBuffer))
112 Restock(out returnBuffer);
114 // Computing free count is expensive enough that we don't want to compute it unless logging is on.
115 if (PinnableBufferCacheEventSource.Log.IsEnabled())
117 int numAllocCalls = Interlocked.Increment(ref m_numAllocCalls);
118 if (numAllocCalls >= 1024)
122 int previousNumAllocCalls = Interlocked.Exchange(ref m_numAllocCalls, 0);
123 if (previousNumAllocCalls >= 1024)
125 int nonGen2Count = 0;
126 foreach (object o in m_FreeList)
128 if (GC.GetGeneration(o) < GC.MaxGeneration)
134 PinnableBufferCacheEventSource.Log.WalkFreeListResult(m_CacheName, m_FreeList.Count, nonGen2Count);
139 PinnableBufferCacheEventSource.Log.AllocateBuffer(m_CacheName, PinnableBufferCacheEventSource.AddressOf(returnBuffer), returnBuffer.GetHashCode(), GC.GetGeneration(returnBuffer), m_FreeList.Count);
145 /// Return a buffer back to the buffer manager.
147 [System.Security.SecuritySafeCritical]
148 internal void Free(object buffer)
151 // Check to see whether or not the cache is disabled.
152 if (m_CacheName == null)
155 if (PinnableBufferCacheEventSource.Log.IsEnabled())
156 PinnableBufferCacheEventSource.Log.FreeBuffer(m_CacheName, PinnableBufferCacheEventSource.AddressOf(buffer), buffer.GetHashCode(), m_FreeList.Count);
159 // After we've done 3 gen1 GCs, assume that all buffers have aged into gen2 on the free path.
160 if ((m_gen1CountAtLastRestock + 3) > GC.CollectionCount(GC.MaxGeneration - 1))
164 if (GC.GetGeneration(buffer) < GC.MaxGeneration)
166 // The buffer is not aged, so put it in the non-aged free list.
167 m_moreThanFreeListNeeded = true;
168 PinnableBufferCacheEventSource.Log.FreeBufferStillTooYoung(m_CacheName, m_NotGen2.Count);
169 m_NotGen2.Add(buffer);
170 m_gen1CountAtLastRestock = GC.CollectionCount(GC.MaxGeneration - 1);
176 // If we discovered that it is indeed Gen2, great, put it in the Gen2 list.
177 m_FreeList.Push(buffer);
183 /// Called when we don't have any buffers in our free list to give out.
185 /// <returns></returns>
186 [System.Security.SecuritySafeCritical]
187 private void Restock(out object returnBuffer)
191 // Try again after getting the lock as another thread could have just filled the free list. If we don't check
192 // then we unnecessarily grab a new set of buffers because we think we are out.
193 if (m_FreeList.TryPop(out returnBuffer))
196 // Lazy init, Ask that TrimFreeListIfNeeded be called on every Gen 2 GC.
197 if (m_restockSize == 0)
198 Gen2GcCallback.Register(Gen2GcCallbackFunc, this);
200 // Indicate to the trimming policy that the free list is insufficent.
201 m_moreThanFreeListNeeded = true;
202 PinnableBufferCacheEventSource.Log.AllocateBufferFreeListEmpty(m_CacheName, m_NotGen2.Count);
204 // Get more buffers if needed.
205 if (m_NotGen2.Count == 0)
208 // We have no buffers in the aged freelist, so get one from the newer list. Try to pick the best one.
209 // Debug.Assert(m_NotGen2.Count != 0);
210 int idx = m_NotGen2.Count - 1;
211 if (GC.GetGeneration(m_NotGen2[idx]) < GC.MaxGeneration && GC.GetGeneration(m_NotGen2[0]) == GC.MaxGeneration)
213 returnBuffer = m_NotGen2[idx];
214 m_NotGen2.RemoveAt(idx);
216 // Remember any sub-optimial buffer so we don't put it on the free list when it gets freed.
217 if (PinnableBufferCacheEventSource.Log.IsEnabled() && GC.GetGeneration(returnBuffer) < GC.MaxGeneration)
219 PinnableBufferCacheEventSource.Log.AllocateBufferFromNotGen2(m_CacheName, m_NotGen2.Count);
222 // If we have a Gen1 collection, then everything on m_NotGen2 should have aged. Move them to the m_Free list.
223 if (!AgePendingBuffers())
225 // Before we could age at set of buffers, we have handed out half of them.
226 // This implies we should be proactive about allocating more (since we will trim them if we over-allocate).
227 if (m_NotGen2.Count == m_restockSize / 2)
229 PinnableBufferCacheEventSource.Log.DebugMessage("Proactively adding more buffers to aging pool");
237 /// See if we can promote the buffers to the free list. Returns true if sucessful.
239 [System.Security.SecuritySafeCritical]
240 private bool AgePendingBuffers()
242 if (m_gen1CountAtLastRestock < GC.CollectionCount(GC.MaxGeneration - 1))
244 // Allocate a temp list of buffers that are not actually in gen2, and swap it in once
245 // we're done scanning all buffers.
246 int promotedCount = 0;
247 List<object> notInGen2 = new List<object>();
248 PinnableBufferCacheEventSource.Log.AllocateBufferAged(m_CacheName, m_NotGen2.Count);
249 for (int i = 0; i < m_NotGen2.Count; i++)
251 // We actually check every object to ensure that we aren't putting non-aged buffers into the free list.
252 object currentBuffer = m_NotGen2[i];
253 if (GC.GetGeneration(currentBuffer) >= GC.MaxGeneration)
255 m_FreeList.Push(currentBuffer);
260 notInGen2.Add(currentBuffer);
263 PinnableBufferCacheEventSource.Log.AgePendingBuffersResults(m_CacheName, promotedCount, notInGen2.Count);
264 m_NotGen2 = notInGen2;
272 /// Generates some buffers to age into Gen2.
274 private void CreateNewBuffers()
276 // We choose a very modest number of buffers initially because for the client case. This is often enough.
277 if (m_restockSize == 0)
279 else if (m_restockSize < DefaultNumberOfBuffers)
280 m_restockSize = DefaultNumberOfBuffers;
281 else if (m_restockSize < 256)
282 m_restockSize = m_restockSize * 2; // Grow quickly at small sizes
283 else if (m_restockSize < 4096)
284 m_restockSize = m_restockSize * 3 / 2; // Less agressively at large ones
286 m_restockSize = 4096; // Cap how agressive we are
288 // Ensure we hit our minimums
289 if (m_minBufferCount > m_buffersUnderManagement)
290 m_restockSize = Math.Max(m_restockSize, m_minBufferCount - m_buffersUnderManagement);
292 PinnableBufferCacheEventSource.Log.AllocateBufferCreatingNewBuffers(m_CacheName, m_buffersUnderManagement, m_restockSize);
293 for (int i = 0; i < m_restockSize; i++)
295 // Make a new buffer.
296 object newBuffer = m_factory();
298 // Create space between the objects. We do this because otherwise it forms a single plug (group of objects)
299 // and the GC pins the entire plug making them NOT move to Gen1 and Gen2. by putting space between them
300 // we ensure that object get a chance to move independently (even if some are pinned).
301 var dummyObject = new object();
302 m_NotGen2.Add(newBuffer);
304 m_buffersUnderManagement += m_restockSize;
305 m_gen1CountAtLastRestock = GC.CollectionCount(GC.MaxGeneration - 1);
309 /// This is the static function that is called from the gen2 GC callback.
310 /// The input object is the cache itself.
311 /// NOTE: The reason that we make this functionstatic and take the cache as a parameter is that
312 /// otherwise, we root the cache to the Gen2GcCallback object, and leak the cache even when
313 /// the application no longer needs it.
315 [System.Security.SecuritySafeCritical]
316 private static bool Gen2GcCallbackFunc(object targetObj)
318 return ((PinnableBufferCache)(targetObj)).TrimFreeListIfNeeded();
322 /// This is called on every gen2 GC to see if we need to trim the free list.
323 /// NOTE: DO NOT CALL THIS DIRECTLY FROM THE GEN2GCCALLBACK. INSTEAD CALL IT VIA A STATIC FUNCTION (SEE ABOVE).
324 /// If you register a non-static function as a callback, then this object will be leaked.
326 [System.Security.SecuritySafeCritical]
327 private bool TrimFreeListIfNeeded()
329 int curMSec = Environment.TickCount;
330 int deltaMSec = curMSec - m_msecNoUseBeyondFreeListSinceThisTime;
331 PinnableBufferCacheEventSource.Log.TrimCheck(m_CacheName, m_buffersUnderManagement, m_moreThanFreeListNeeded, deltaMSec);
333 // If we needed more than just the set of aged buffers since the last time we were called,
334 // we obviously should not be trimming any memory, so do nothing except reset the flag
335 if (m_moreThanFreeListNeeded)
337 m_moreThanFreeListNeeded = false;
338 m_trimmingExperimentInProgress = false;
339 m_msecNoUseBeyondFreeListSinceThisTime = curMSec;
343 // We require a minimum amount of clock time to pass (10 seconds) before we trim. Ideally this time
344 // is larger than the typical buffer hold time.
345 if (0 <= deltaMSec && deltaMSec < 10000)
348 // If we got here we have spend the last few second without needing to lengthen the free list. Thus
349 // we have 'enough' buffers, but maybe we have too many.
350 // See if we can trim
353 // Hit a ----, try again later.
354 if (m_moreThanFreeListNeeded)
356 m_moreThanFreeListNeeded = false;
357 m_trimmingExperimentInProgress = false;
358 m_msecNoUseBeyondFreeListSinceThisTime = curMSec;
362 var freeCount = m_FreeList.Count; // This is expensive to fetch, do it once.
364 // If there is something in m_NotGen2 it was not used for the last few seconds, it is trimable.
365 if (m_NotGen2.Count > 0)
367 // If we are not performing an experiment and we have stuff that is waiting to go into the
368 // free list but has not made it there, it could be becasue the 'slow path' of restocking
369 // has not happened, so force this (which should flush the list) and start over.
370 if (!m_trimmingExperimentInProgress)
372 PinnableBufferCacheEventSource.Log.TrimFlush(m_CacheName, m_buffersUnderManagement, freeCount, m_NotGen2.Count);
374 m_trimmingExperimentInProgress = true;
378 PinnableBufferCacheEventSource.Log.TrimFree(m_CacheName, m_buffersUnderManagement, freeCount, m_NotGen2.Count);
379 m_buffersUnderManagement -= m_NotGen2.Count;
381 // Possibly revise the restocking down. We don't want to grow agressively if we are trimming.
382 var newRestockSize = m_buffersUnderManagement / 4;
383 if (newRestockSize < m_restockSize)
384 m_restockSize = Math.Max(newRestockSize, DefaultNumberOfBuffers);
387 m_trimmingExperimentInProgress = false;
391 // Set up an experiment where we use 25% less buffers in our free list. We put them in
392 // m_NotGen2, and if they are needed they will be put back in the free list again.
393 var trimSize = freeCount / 4 + 1;
395 // We are OK with a 15% overhead, do nothing in that case.
396 if (freeCount * 15 <= m_buffersUnderManagement || m_buffersUnderManagement - trimSize <= m_minBufferCount)
398 PinnableBufferCacheEventSource.Log.TrimFreeSizeOK(m_CacheName, m_buffersUnderManagement, freeCount);
402 // Move buffers from teh free list back to the non-aged list. If we don't use them by next time, then we'll consider trimming them.
403 PinnableBufferCacheEventSource.Log.TrimExperiment(m_CacheName, m_buffersUnderManagement, freeCount, trimSize);
405 for (int i = 0; i < trimSize; i++)
407 if (m_FreeList.TryPop(out buffer))
408 m_NotGen2.Add(buffer);
410 m_msecNoUseBeyondFreeListSinceThisTime = curMSec;
411 m_trimmingExperimentInProgress = true;
414 // Indicate that we want to be called back on the next Gen 2 GC.
418 private const int DefaultNumberOfBuffers = 16;
419 private string m_CacheName;
420 private Func<object> m_factory;
423 /// Contains 'good' buffers to reuse. They are guarenteed to be Gen 2 ENFORCED!
425 private ConcurrentStack<object> m_FreeList = new ConcurrentStack<object>();
427 /// Contains buffers that are not gen 2 and thus we do not wish to give out unless we have to.
428 /// To implement trimming we sometimes put aged buffers in here as a place to 'park' them
429 /// before true deletion.
431 private List<object> m_NotGen2;
433 /// What whas the gen 1 count the last time re restocked? If it is now greater, then
434 /// we know that all objects are in Gen 2 so we don't have to check. Should be updated
435 /// every time something gets added to the m_NotGen2 list.
437 private int m_gen1CountAtLastRestock;
440 /// Used to ensure we have a minimum time between trimmings.
442 private int m_msecNoUseBeyondFreeListSinceThisTime;
444 /// To trim, we remove things from the free list (which is Gen 2) and see if we 'hit bottom'
445 /// This flag indicates that we hit bottom (we really needed a bigger free list).
447 private bool m_moreThanFreeListNeeded;
449 /// The total number of buffers that this cache has ever allocated.
450 /// Used in trimming heuristics.
452 private int m_buffersUnderManagement;
454 /// The number of buffers we added the last time we restocked.
456 private int m_restockSize;
458 /// Did we put some buffers into m_NotGen2 to see if we can trim?
460 private bool m_trimmingExperimentInProgress;
462 /// A forced minimum number of buffers.
464 private int m_minBufferCount;
466 /// The number of calls to Allocate.
468 private int m_numAllocCalls;
474 /// Schedules a callback roughly every gen 2 GC (you may see a Gen 0 an Gen 1 but only once)
475 /// (We can fix this by capturing the Gen 2 count at startup and testing, but I mostly don't care)
477 internal sealed class Gen2GcCallback : CriticalFinalizerObject
479 [System.Security.SecuritySafeCritical]
480 public Gen2GcCallback()
486 /// Schedule 'callback' to be called in the next GC. If the callback returns true it is
487 /// rescheduled for the next Gen 2 GC. Otherwise the callbacks stop.
489 /// NOTE: This callback will be kept alive until either the callback function returns false,
490 /// or the target object dies.
492 public static void Register(Func<object, bool> callback, object targetObj)
494 // Create a unreachable object that remembers the callback function and target object.
495 Gen2GcCallback gcCallback = new Gen2GcCallback();
496 gcCallback.Setup(callback, targetObj);
501 private Func<object, bool> m_callback;
502 private GCHandle m_weakTargetObj;
504 [System.Security.SecuritySafeCritical]
505 private void Setup(Func<object, bool> callback, object targetObj)
507 m_callback = callback;
508 m_weakTargetObj = GCHandle.Alloc(targetObj, GCHandleType.Weak);
511 [System.Security.SecuritySafeCritical]
514 // Check to see if the target object is still alive.
515 if (!m_weakTargetObj.IsAllocated)
520 object targetObj = m_weakTargetObj.Target;
521 if (targetObj == null)
523 // The target object is dead, so this callback object is no longer needed.
524 m_weakTargetObj.Free();
528 // Execute the callback method.
531 if (!m_callback(targetObj))
533 // If the callback returns false, this callback object is no longer needed.
539 // Ensure that we still get a chance to resurrect this object, even if the callback throws an exception.
542 // Resurrect ourselves by re-registering for finalization.
543 if (!Environment.HasShutdownStarted && !AppDomain.CurrentDomain.IsFinalizingForUnload())
545 GC.ReRegisterForFinalize(this);
554 internal sealed class PinnableBufferCacheEventSource
556 public static readonly PinnableBufferCacheEventSource Log = new PinnableBufferCacheEventSource();
558 public bool IsEnabled() { return false; }
559 public void DebugMessage(string message) {}
560 public void DebugMessage1(string message, long value) {}
561 public void DebugMessage2(string message, long value1, long value2) {}
562 public void DebugMessage3(string message, long value1, long value2, long value3) {}
563 public void Create(string cacheName) {}
564 public void AllocateBuffer(string cacheName, ulong objectId, int objectHash, int objectGen, int freeCountAfter) {}
565 public void AllocateBufferFromNotGen2(string cacheName, int notGen2CountAfter) {}
566 public void AllocateBufferCreatingNewBuffers(string cacheName, int totalBuffsBefore, int objectCount) {}
567 public void AllocateBufferAged(string cacheName, int agedCount) {}
568 public void AllocateBufferFreeListEmpty(string cacheName, int notGen2CountBefore) {}
569 public void FreeBuffer(string cacheName, ulong objectId, int objectHash, int freeCountBefore) {}
570 public void FreeBufferStillTooYoung(string cacheName, int notGen2CountBefore) {}
571 public void TrimCheck(string cacheName, int totalBuffs, bool neededMoreThanFreeList, int deltaMSec) {}
572 public void TrimFree(string cacheName, int totalBuffs, int freeListCount, int toBeFreed) {}
573 public void TrimExperiment(string cacheName, int totalBuffs, int freeListCount, int numTrimTrial) {}
574 public void TrimFreeSizeOK(string cacheName, int totalBuffs, int freeListCount) {}
575 public void TrimFlush(string cacheName, int totalBuffs, int freeListCount, int notGen2CountBefore) {}
576 public void AgePendingBuffersResults(string cacheName, int promotedToFreeListCount, int heldBackCount) {}
577 public void WalkFreeListResult(string cacheName, int freeListCount, int gen0BuffersInFreeList) {}
579 static internal ulong AddressOf(object obj)
584 [System.Security.SecuritySafeCritical]
585 static internal unsafe long AddressOfObject(byte[] array)
592 /// PinnableBufferCacheEventSource is a private eventSource that we are using to
593 /// debug and monitor the effectiveness of PinnableBufferCache
595 #if PINNABLEBUFFERCACHE_MSCORLIB
596 [EventSource(Name = "Microsoft-DotNETRuntime-PinnableBufferCache")]
598 [EventSource(Name = "Microsoft-DotNETRuntime-PinnableBufferCache-System")]
600 internal sealed class PinnableBufferCacheEventSource : EventSource
602 public static readonly PinnableBufferCacheEventSource Log = new PinnableBufferCacheEventSource();
604 [Event(1, Level = EventLevel.Verbose)]
605 public void DebugMessage(string message) { if (IsEnabled()) WriteEvent(1, message); }
606 [Event(2, Level = EventLevel.Verbose)]
607 public void DebugMessage1(string message, long value) { if (IsEnabled()) WriteEvent(2, message, value); }
608 [Event(3, Level = EventLevel.Verbose)]
609 public void DebugMessage2(string message, long value1, long value2) { if (IsEnabled()) WriteEvent(3, message, value1, value2); }
610 [Event(18, Level = EventLevel.Verbose)]
611 public void DebugMessage3(string message, long value1, long value2, long value3) { if (IsEnabled()) WriteEvent(18, message, value1, value2, value3); }
614 public void Create(string cacheName) { if (IsEnabled()) WriteEvent(4, cacheName); }
616 [Event(5, Level = EventLevel.Verbose)]
617 public void AllocateBuffer(string cacheName, ulong objectId, int objectHash, int objectGen, int freeCountAfter) { if (IsEnabled()) WriteEvent(5, cacheName, objectId, objectHash, objectGen, freeCountAfter); }
619 public void AllocateBufferFromNotGen2(string cacheName, int notGen2CountAfter) { if (IsEnabled()) WriteEvent(6, cacheName, notGen2CountAfter); }
621 public void AllocateBufferCreatingNewBuffers(string cacheName, int totalBuffsBefore, int objectCount) { if (IsEnabled()) WriteEvent(7, cacheName, totalBuffsBefore, objectCount); }
623 public void AllocateBufferAged(string cacheName, int agedCount) { if (IsEnabled()) WriteEvent(8, cacheName, agedCount); }
625 public void AllocateBufferFreeListEmpty(string cacheName, int notGen2CountBefore) { if (IsEnabled()) WriteEvent(9, cacheName, notGen2CountBefore); }
627 [Event(10, Level = EventLevel.Verbose)]
628 public void FreeBuffer(string cacheName, ulong objectId, int objectHash, int freeCountBefore) { if (IsEnabled()) WriteEvent(10, cacheName, objectId, objectHash, freeCountBefore); }
630 public void FreeBufferStillTooYoung(string cacheName, int notGen2CountBefore) { if (IsEnabled()) WriteEvent(11, cacheName, notGen2CountBefore); }
633 public void TrimCheck(string cacheName, int totalBuffs, bool neededMoreThanFreeList, int deltaMSec) { if (IsEnabled()) WriteEvent(13, cacheName, totalBuffs, neededMoreThanFreeList, deltaMSec); }
635 public void TrimFree(string cacheName, int totalBuffs, int freeListCount, int toBeFreed) { if (IsEnabled()) WriteEvent(14, cacheName, totalBuffs, freeListCount, toBeFreed); }
637 public void TrimExperiment(string cacheName, int totalBuffs, int freeListCount, int numTrimTrial) { if (IsEnabled()) WriteEvent(15, cacheName, totalBuffs, freeListCount, numTrimTrial); }
639 public void TrimFreeSizeOK(string cacheName, int totalBuffs, int freeListCount) { if (IsEnabled()) WriteEvent(16, cacheName, totalBuffs, freeListCount); }
641 public void TrimFlush(string cacheName, int totalBuffs, int freeListCount, int notGen2CountBefore) { if (IsEnabled()) WriteEvent(17, cacheName, totalBuffs, freeListCount, notGen2CountBefore); }
643 public void AgePendingBuffersResults(string cacheName, int promotedToFreeListCount, int heldBackCount) { if (IsEnabled()) WriteEvent(20, cacheName, promotedToFreeListCount, heldBackCount); }
645 public void WalkFreeListResult(string cacheName, int freeListCount, int gen0BuffersInFreeList) { if (IsEnabled()) WriteEvent(21, cacheName, freeListCount, gen0BuffersInFreeList); }
648 static internal ulong AddressOf(object obj)
650 var asByteArray = obj as byte[];
651 if (asByteArray != null)
652 return (ulong)AddressOfByteArray(asByteArray);
656 [System.Security.SecuritySafeCritical]
657 static internal unsafe long AddressOfByteArray(byte[] array)
661 fixed (byte* ptr = array)
662 return (long)(ptr - 2 * sizeof(void*));