Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use GeneratedDllImport in System.IO.MemoryMappedFiles #53978

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ internal enum MemoryMappedFlags
}

// 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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ internal enum MemoryMappedSyncFlags
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,27 @@ 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,
int dwMaximumSizeHigh,
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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public static unsafe void CheckForAvailableVirtualMemory(ulong nativeSize)
{
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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ private static unsafe bool CheckForAvailableMemory(out ulong availPageFile, out
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static unsafe MemoryMonitor()
{
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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ protected override unsafe int GetCurrentPressure()
{
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;
}
Expand Down