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 pkgList, string depString) { var refs = new Dictionary(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(); } } } } }