Use GeneratedDllImport in System.IO.MemoryMappedFiles (#53978)
authorElinor Fung <elfung@microsoft.com>
Thu, 10 Jun 2021 19:08:05 +0000 (12:08 -0700)
committerGitHub <noreply@github.com>
Thu, 10 Jun 2021 19:08:05 +0000 (12:08 -0700)
18 files changed:
src/libraries/Common/src/Interop/Unix/System.Native/Interop.FTruncate.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MAdvise.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MMap.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MSync.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.MUnmap.cs
src/libraries/Common/src/Interop/Unix/System.Native/Interop.ShmOpen.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.CreateFileMapping.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.FlushViewOfFile.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.GlobalMemoryStatusEx.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.MapViewOfFile.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.OpenFileMapping.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.UnmapViewOfFile.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VirtualAlloc.cs
src/libraries/Common/src/Interop/Windows/Kernel32/Interop.VirtualQuery.cs
src/libraries/System.IO.MemoryMappedFiles/src/System/IO/MemoryMappedFiles/Interop.Windows.cs
src/libraries/System.Private.CoreLib/src/System/Runtime/MemoryFailPoint.Windows.cs
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/MemoryMonitor.Windows.cs
src/libraries/System.Runtime.Caching/src/System/Runtime/Caching/PhysicalMemoryMonitor.Windows.cs

index f7a726f..b836726 100644 (file)
@@ -8,7 +8,7 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FTruncate", SetLastError = true)]
-        internal static extern int FTruncate(SafeFileHandle fd, long length);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_FTruncate", SetLastError = true)]
+        internal static partial int FTruncate(SafeFileHandle fd, long length);
     }
 }
index 539e27a..5ac7aa1 100644 (file)
@@ -8,8 +8,8 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MAdvise", SetLastError = true)]
-        internal static extern int MAdvise(IntPtr addr, ulong length, MemoryAdvice advice);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MAdvise", SetLastError = true)]
+        internal static partial int MAdvise(IntPtr addr, ulong length, MemoryAdvice advice);
 
         internal enum MemoryAdvice
         {
index f750652..5789ee7 100644 (file)
@@ -28,8 +28,8 @@ internal static partial class Interop
         }
 
         // NOTE: Shim returns null pointer on failure, not non-null MAP_FAILED sentinel.
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MMap", SetLastError = true)]
-        internal static extern IntPtr MMap(
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MMap", SetLastError = true)]
+        internal static partial IntPtr MMap(
             IntPtr addr, ulong len,
             MemoryMappedProtections prot, MemoryMappedFlags flags,
             SafeFileHandle fd, long offset);
index d486c36..78263cb 100644 (file)
@@ -16,7 +16,7 @@ internal static partial class Interop
             MS_INVALIDATE = 0x10,
         }
 
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MSync", SetLastError = true)]
-        internal static extern int MSync(IntPtr addr, ulong len, MemoryMappedSyncFlags flags);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MSync", SetLastError = true)]
+        internal static partial int MSync(IntPtr addr, ulong len, MemoryMappedSyncFlags flags);
     }
 }
index fffd3fb..a0ed692 100644 (file)
@@ -8,7 +8,7 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MUnmap", SetLastError = true)]
-        internal static extern int MUnmap(IntPtr addr, ulong len);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_MUnmap", SetLastError = true)]
+        internal static partial int MUnmap(IntPtr addr, ulong len);
     }
 }
index f4749b8..5ebd901 100644 (file)
@@ -8,10 +8,10 @@ internal static partial class Interop
 {
     internal static partial class Sys
     {
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ShmOpen", SetLastError = true)]
-        internal static extern SafeFileHandle ShmOpen(string name, OpenFlags flags, int mode);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ShmOpen", SetLastError = true, CharSet = CharSet.Ansi)]
+        internal static partial SafeFileHandle ShmOpen(string name, OpenFlags flags, int mode);
 
-        [DllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ShmUnlink", SetLastError = true)]
-        internal static extern int ShmUnlink(string name);
+        [GeneratedDllImport(Libraries.SystemNative, EntryPoint = "SystemNative_ShmUnlink", SetLastError = true, CharSet = CharSet.Ansi)]
+        internal static partial int ShmUnlink(string name);
     }
 }
index cc85cc9..81ffec3 100644 (file)
@@ -10,8 +10,13 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial SafeMemoryMappedFileHandle CreateFileMapping(
+#else
         [DllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
         internal static extern SafeMemoryMappedFileHandle CreateFileMapping(
+#endif
             SafeFileHandle hFile,
             ref SECURITY_ATTRIBUTES lpFileMappingAttributes,
             int flProtect,
@@ -19,8 +24,13 @@ internal static partial class Interop
             int dwMaximumSizeLow,
             string? lpName);
 
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial SafeMemoryMappedFileHandle CreateFileMapping(
+#else
         [DllImport(Libraries.Kernel32, EntryPoint = "CreateFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
         internal static extern SafeMemoryMappedFileHandle CreateFileMapping(
+#endif
             IntPtr hFile,
             ref SECURITY_ATTRIBUTES lpFileMappingAttributes,
             int flProtect,
index ce82cfa..72a0560 100644 (file)
@@ -8,7 +8,7 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true)]
-        internal static extern bool FlushViewOfFile(IntPtr lpBaseAddress, UIntPtr dwNumberOfBytesToFlush);
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static partial bool FlushViewOfFile(IntPtr lpBaseAddress, UIntPtr dwNumberOfBytesToFlush);
     }
 }
index a5029f5..5b44e73 100644 (file)
@@ -8,6 +8,6 @@ internal static partial class Interop
     internal static partial class Kernel32
     {
         [DllImport(Libraries.Kernel32)]
-        internal static extern bool GlobalMemoryStatusEx(ref MEMORYSTATUSEX lpBuffer);
+        internal static unsafe extern int GlobalMemoryStatusEx(MEMORYSTATUSEX* lpBuffer);
     }
 }
index 73f714d..764a6ad 100644 (file)
@@ -9,8 +9,13 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "MapViewOfFile", CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial SafeMemoryMappedViewHandle MapViewOfFile(
+#else
         [DllImport(Libraries.Kernel32, EntryPoint = "MapViewOfFile", CharSet = CharSet.Unicode, SetLastError = true)]
         internal static extern SafeMemoryMappedViewHandle MapViewOfFile(
+#endif
             SafeMemoryMappedFileHandle hFileMappingObject,
             int dwDesiredAccess,
             int dwFileOffsetHigh,
index 70e3075..6ac60a8 100644 (file)
@@ -8,7 +8,15 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, EntryPoint = "OpenFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
+        internal static partial SafeMemoryMappedFileHandle OpenFileMapping(
+#else
         [DllImport(Libraries.Kernel32, EntryPoint = "OpenFileMappingW", CharSet = CharSet.Unicode, SetLastError = true)]
-        internal static extern SafeMemoryMappedFileHandle OpenFileMapping(int dwDesiredAccess, [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle, string lpName);
+        internal static extern SafeMemoryMappedFileHandle OpenFileMapping(
+#endif
+            int dwDesiredAccess,
+            [MarshalAs(UnmanagedType.Bool)] bool bInheritHandle,
+            string lpName);
     }
 }
index 55d3b8e..dd519a7 100644 (file)
@@ -8,7 +8,12 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true)]
+        internal static partial bool UnmapViewOfFile(IntPtr lpBaseAddress);
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true)]
         internal static extern bool UnmapViewOfFile(IntPtr lpBaseAddress);
+#endif
     }
 }
index 19f93d2..02bcf91 100644 (file)
@@ -8,7 +8,7 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
-        [DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
-        internal static extern IntPtr VirtualAlloc(SafeHandle lpAddress, UIntPtr dwSize, int flAllocationType, int flProtect);
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
+        internal static partial IntPtr VirtualAlloc(SafeHandle lpAddress, UIntPtr dwSize, int flAllocationType, int flProtect);
     }
 }
index 25e3fd6..8561916 100644 (file)
@@ -8,7 +8,15 @@ internal static partial class Interop
 {
     internal static partial class Kernel32
     {
+#if DLLIMPORTGENERATOR_ENABLED
+        [GeneratedDllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
+        internal static partial UIntPtr VirtualQuery(
+#else
         [DllImport(Libraries.Kernel32, SetLastError = true, ExactSpelling = true)]
-        internal static extern UIntPtr VirtualQuery(SafeHandle lpAddress, ref MEMORY_BASIC_INFORMATION lpBuffer, UIntPtr dwLength);
+        internal static extern UIntPtr VirtualQuery(
+#endif
+            SafeHandle lpAddress,
+            ref MEMORY_BASIC_INFORMATION lpBuffer,
+            UIntPtr dwLength);
     }
 }
index 202c3ea..e232953 100644 (file)
@@ -13,7 +13,7 @@ internal static partial class Interop
     {
         Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
         memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
-        if (Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
+        if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) != 0)
         {
             ulong totalVirtual = memoryStatus.ullTotalVirtual;
             if (nativeSize >= totalVirtual)
index 052c26b..e2f0ac6 100644 (file)
@@ -22,7 +22,7 @@ namespace System.Runtime
         {
             Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
             memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
-            if (!Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
+            if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) == 0)
             {
                 availPageFile = default;
                 totalAddressSpaceFree = default;
index 189a946..8ee2de3 100644 (file)
@@ -16,7 +16,7 @@ namespace System.Runtime.Caching
         {
             Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
             memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
-            if (Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
+            if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) != 0)
             {
                 s_totalPhysical = (long)memoryStatus.ullTotalPhys;
                 s_totalVirtual = (long)memoryStatus.ullTotalVirtual;
index 078bf9e..8dacfaa 100644 (file)
@@ -14,7 +14,7 @@ namespace System.Runtime.Caching
         {
             Interop.Kernel32.MEMORYSTATUSEX memoryStatus = default;
             memoryStatus.dwLength = (uint)sizeof(Interop.Kernel32.MEMORYSTATUSEX);
-            if (!Interop.Kernel32.GlobalMemoryStatusEx(ref memoryStatus))
+            if (Interop.Kernel32.GlobalMemoryStatusEx(&memoryStatus) == 0)
             {
                 return 0;
             }