diff --git a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs index bc1920c01563d2..09f7c97d302237 100644 --- a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs +++ b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.GetProcInfo.cs @@ -2,6 +2,7 @@ // The .NET Foundation licenses this file to you under the MIT license. using System; +using System.Diagnostics; using System.Runtime.InteropServices; #pragma warning disable CA1823 // analyzer incorrectly flags fixed buffer length const (https://github.com/dotnet/roslyn/issues/37593) @@ -186,11 +187,6 @@ public unsafe struct kinfo_proc public static unsafe kinfo_proc* GetProcInfo(int pid, bool threads, out int count) { Span sysctlName = stackalloc int[4]; - int bytesLength = 0; - byte* pBuffer = null; - kinfo_proc* kinfo = null; - - count = -1; if (pid == 0) { @@ -207,23 +203,17 @@ public unsafe struct kinfo_proc sysctlName[1] = KERN_PROC; sysctlName[0] = CTL_KERN; - try - { - Interop.Sys.Sysctl(sysctlName, ref pBuffer, ref bytesLength); - kinfo = (kinfo_proc*)pBuffer; - if (kinfo->ki_structsize != sizeof(kinfo_proc)) - { - // failed consistency check - throw new ArgumentOutOfRangeException(nameof(pid)); - } - - count = (int)bytesLength / sizeof(kinfo_proc); - } - finally - { - Marshal.FreeHGlobal((IntPtr)pBuffer); - } + byte* pBuffer = null; + int bytesLength = 0; + Interop.Sys.Sysctl(sysctlName, ref pBuffer, ref bytesLength); + + kinfo_proc* kinfo = (kinfo_proc*)pBuffer; + + Debug.Assert(kinfo->ki_structsize == sizeof(kinfo_proc)); + + count = (int)bytesLength / sizeof(kinfo_proc); + // Buffer ownership transferred to the caller return kinfo; } } diff --git a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs index 031f56a50f08b8..3d18ead32d116d 100644 --- a/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs +++ b/src/libraries/Common/src/Interop/FreeBSD/Interop.Process.cs @@ -65,7 +65,7 @@ internal static unsafe int[] ListAllPids() } finally { - Marshal.FreeHGlobal((IntPtr)entries); + NativeMemory.Free(entries); } } @@ -87,7 +87,7 @@ internal static unsafe int[] ListAllPids() } finally { - Marshal.FreeHGlobal((IntPtr)pBuffer); + NativeMemory.Free(pBuffer); } } @@ -107,9 +107,9 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid) throw new ArgumentOutOfRangeException(nameof(pid)); } - kinfo_proc* kinfo = GetProcInfo(pid, true, out int count); ProcessInfo info; + kinfo_proc* kinfo = GetProcInfo(pid, true, out int count); try { if (count < 1) @@ -142,7 +142,7 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid) } finally { - Marshal.FreeHGlobal((IntPtr)kinfo); + NativeMemory.Free(kinfo); } return info; @@ -160,12 +160,11 @@ public static unsafe ProcessInfo GetProcessInfoById(int pid) public static unsafe proc_stats GetThreadInfo(int pid, int tid) { proc_stats ret = default; - kinfo_proc* info = null; int count; + kinfo_proc* info = GetProcInfo(pid, (tid != 0), out count); try { - info = GetProcInfo(pid, (tid != 0), out count); if (info != null && count >= 1) { if (tid == 0) @@ -194,7 +193,7 @@ public static unsafe proc_stats GetThreadInfo(int pid, int tid) } finally { - Marshal.FreeHGlobal((IntPtr)info); + NativeMemory.Free(info); } return ret; diff --git a/src/libraries/System.Private.CoreLib/src/System/Environment.FreeBSD.cs b/src/libraries/System.Private.CoreLib/src/System/Environment.FreeBSD.cs index e9e9178510e3e9..aa568ef7822a9c 100644 --- a/src/libraries/System.Private.CoreLib/src/System/Environment.FreeBSD.cs +++ b/src/libraries/System.Private.CoreLib/src/System/Environment.FreeBSD.cs @@ -7,6 +7,20 @@ namespace System { public static partial class Environment { - public static unsafe long WorkingSet => Interop.Process.GetProcInfo(ProcessId, true, out _)->ki_rssize; + public static unsafe long WorkingSet + { + get + { + Interop.Process.kinfo_proc* processInfo = Interop.Process.GetProcInfo(ProcessId, true, out _); + try + { + return processInfo->ki_rssize; + } + finally + { + NativeMemory.Free(processInfo); + } + } + } } }