Removed UnsafeNativeMethods calls on Mono. - MEMORYSTATUSEX usages were replaced...
authorMarcos Henrich <marcos.henrich@xamarin.com>
Thu, 11 Dec 2014 20:56:46 +0000 (20:56 +0000)
committerMarek Safar <marek.safar@gmail.com>
Mon, 2 May 2016 22:07:41 +0000 (00:07 +0200)
 - SafeRegistryHandle.ReleaseHandle is only called on code that is ignored on Mono.

 - Mono is using AppDomain.FriendlyName directly instead of UnsafeNativeMethods.GetModuleFileName.

mcs/class/referencesource/System.Runtime.Caching/System/Caching/MemoryMonitor.cs
mcs/class/referencesource/System.Runtime.Caching/System/Caching/PerfCounters.cs
mcs/class/referencesource/System.Runtime.Caching/System/Caching/PhysicalMemoryMonitor.cs
mcs/class/referencesource/System.Runtime.Caching/System/Caching/SafeRegistryHandle.cs

index f1548cc26526264fd758833e2ab52dfa3fc10dde..83649f1d464190e20c8c81e6176a575438f39858 100644 (file)
@@ -40,12 +40,22 @@ namespace System.Runtime.Caching {
 
         [SecuritySafeCritical]
         static MemoryMonitor() {
+#if MONO
+            var pc = new System.Diagnostics.PerformanceCounter ("Mono Memory", "Total Physical Memory");
+            s_totalPhysical = pc.RawValue;
+
+            // We should set the the total virtual memory with a system value.
+            // But Mono has no such PerformanceCounter and the total virtual memory has little relevance
+            // for the rest of the System.Runtime.Caching code.
+            s_totalVirtual = 0;
+#else
             MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX();
             memoryStatusEx.Init();
             if (UnsafeNativeMethods.GlobalMemoryStatusEx(ref memoryStatusEx) != 0) {
                 s_totalPhysical = memoryStatusEx.ullTotalPhys;
                 s_totalVirtual = memoryStatusEx.ullTotalVirtual;
             }
+#endif
         }
 
         internal static long TotalPhysical { get { return s_totalPhysical; } }
index ed1dd7733c9ce56b5045ce5736563e1a9e19306a..1b481f800f7ea64739b9d2f130c538316f88ca77 100644 (file)
@@ -41,12 +41,14 @@ namespace System.Runtime.Caching {
                 // if the host has an identifier, use it
                 string appId = (ai != null) ? ai.GetApplicationId() : null;
                 // otherwise, use the process name wihtout file extension
+#if !MONO
                 if (String.IsNullOrEmpty(appId)) {
                     StringBuilder sb = new StringBuilder(512);
                     if (UnsafeNativeMethods.GetModuleFileName(IntPtr.Zero, sb, 512) != 0) {
                         appId = Path.GetFileNameWithoutExtension(sb.ToString());
                     }
                 }
+#endif
                 // if all else fails, use AppDomain.FriendlyName
                 if (String.IsNullOrEmpty(appId)) {  
                     appId = AppDomain.CurrentDomain.FriendlyName;
index 691aeca6a413fab4546874005fb85a5490f69114..992efaaa634b75dd2e3115c0d96f789b326479c9 100644 (file)
@@ -107,12 +107,19 @@ namespace System.Runtime.Caching {
 
         [SecuritySafeCritical]
         protected override int GetCurrentPressure() {
+#if MONO
+            var pc = new System.Diagnostics.PerformanceCounter ("Mono Memory", "Available Physical Memory");
+            long availableMemory = pc.RawValue;
+
+            int memoryLoad = (int) ((100 * availableMemory) / TotalPhysical);
+#else
             MEMORYSTATUSEX memoryStatusEx = new MEMORYSTATUSEX();
             memoryStatusEx.Init();
             if (UnsafeNativeMethods.GlobalMemoryStatusEx(ref memoryStatusEx) == 0)
                 return 0;
 
             int memoryLoad = memoryStatusEx.dwMemoryLoad;
+#endif
             //if (_pressureHigh != 0) {
                 // PerfCounter: Cache Percentage Machine Memory Limit Used
                 //    = total physical memory used / total physical memory used limit
index e08893ef092a09932bf1f42b02bc0f9bd0709521..d08593fe17dd38ac4d82b805b6fb49c09301f409 100644 (file)
@@ -21,8 +21,12 @@ namespace System.Runtime.Caching {
         [SecurityCritical]
         protected override bool ReleaseHandle() {
             // Returns a Win32 error code, 0 for success
+#if MONO
+            throw new NotImplementedException ();
+#else
             int r = UnsafeNativeMethods.RegCloseKey(handle);
             return r == 0;
+#endif
         }
     }
 }