Files
DotNetAlpm/Alpm/ExtensionMethods.cs
2020-05-01 05:08:04 -04:00

49 lines
1.6 KiB
C#

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Runtime.ConstrainedExecution;
namespace Foodsoft.Alpm
{
public static class ExtensionMethods
{
[PrePrepareMethod]
public static Package? FindSatisfier(this IEnumerable<Package> pkgList, string depString)
{
var refs = new Dictionary<IntPtr, Package?>(10);
var listPtr = IntPtr.Zero;
RuntimeHelpers.PrepareConstrainedRegions();
try
{
foreach (var pkg in pkgList)
{
var release = false;
var handle = pkg.Handle;
var pkgPtr = handle.DangerousGetHandle();
refs.Add(pkgPtr, null);
handle.DangerousAddRef(ref release);
if (!release) throw new ObjectDisposedException(handle.GetType().FullName);
refs[pkgPtr] = pkg;
if (alpm.alpm_list_append(ref listPtr, handle.DangerousGetHandle()) == IntPtr.Zero)
throw new AlpmException(ErrorCode.Memory);
}
var foundPtr = alpm.alpm_find_satisfier(listPtr, depString);
return foundPtr != IntPtr.Zero
? refs[foundPtr]
: null;
}
finally
{
alpm.alpm_list_free(listPtr);
foreach (var pkg in refs.Values)
{
pkg?.Handle.DangerousRelease();
}
}
}
}
}