1using System.Runtime.InteropServices;
5internal static class SafeHandles
7 public static IntPtr[] ExtractHandles<T>(IList<T> objects, Func<T, SafeHandle> handleSelector, out Action release)
9 var handles =
new IntPtr[objects.Count];
10 var safeHandles =
new List<SafeHandle>();
12 for (var i = 0; i < objects.Count; i++)
14 SafeHandle sh = handleSelector(objects[i]);
19 sh.DangerousAddRef(ref added);
21 catch (ObjectDisposedException)
23 throw new IcvInternalErrorException(
"Region has already been disposed.",
25 .PEAK_ICV_STATUS_INVALID_HANDLE);
30 throw new IcvInternalErrorException(
"Failed to pin SafeHandle",
32 .PEAK_ICV_STATUS_INTERNAL_ERROR);
35 handles[i] = sh.DangerousGetHandle();
43 if (Interlocked.Exchange(ref released, 1) != 0)
48 foreach (SafeHandle? safeHandle
in safeHandles)
50 safeHandle.DangerousRelease();
57 public static IntPtr[] ExtractHandles<T>(IList<T> objects, out Action release) where
T :
IHasSafeHandle
59 return ExtractHandles(
68 SafeHandle Handle {
get; }