-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Add new apis proposed in #4137 to System.Runtime.InteropServices.RuntimeInformation #4334
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative)] | ||
internal static extern int GetUnixArchitecture(); | ||
|
||
internal enum ProcessorArchitecture | ||
{ | ||
x86, | ||
x64, | ||
ARM | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should add ARM64 too. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right now we are not supported Arm64, it'll be there in enum unused. When we start supporting for Arm64, we can change this since it's a OOB assembly. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Other existing runtimes (mono, Unity) do support arm64 already. You should add it to the public contract at least so that this contract can be implemented by other runtimes. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @jkotas In the API review it was decided to not add Arm64 right now. Adding Arm64 will require a new API Review, not in the scope of this PR. Will file a new issue for this, #4384 |
||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Text; | ||
|
||
internal static partial class Interop | ||
{ | ||
internal static partial class Sys | ||
{ | ||
[DllImport(Libraries.SystemNative, CharSet = CharSet.Ansi, SetLastError = true)] | ||
private static extern int GetUnixVersion(StringBuilder version, out int capacity); | ||
|
||
internal static string GetUnixVersion() | ||
{ | ||
// max value of _UTSNAME_LENGTH on known Unix platforms is 1024. | ||
const int _UTSNAME_LENGTH = 1024; | ||
int capacity = _UTSNAME_LENGTH * 3 + 2; | ||
StringBuilder version = new StringBuilder(capacity); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. var There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer var? Or more keystrokes 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lol, the guidelines says we use var only for cases where it's simple to guess the type, but it doesn't say it's preferred or compulsory. And personally everywhere else in code I have not used var, changing this, will not put me in peace until i change every other instance. So keeping as is. And on keystrokes, i use intellisense heavily. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I also prefer to avoid var except for extremely long/pointless type declarations. And like you said, our guidelines allow either. |
||
|
||
if (GetUnixVersion(version, out capacity) != 0) | ||
{ | ||
// Check if the function failed due to insufficient buffer. | ||
if (capacity > version.Capacity) | ||
{ | ||
version.Capacity = capacity; | ||
GetUnixVersion(version, out capacity); | ||
} | ||
} | ||
|
||
return version.ToString(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class NtDll | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct RTL_OSVERSIONINFOEX | ||
{ | ||
internal uint dwOSVersionInfoSize; | ||
internal uint dwMajorVersion; | ||
internal uint dwMinorVersion; | ||
internal uint dwBuildNumber; | ||
internal uint dwPlatformId; | ||
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 128)] | ||
internal string szCSDVersion; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
using System.Security; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class NtDll | ||
{ | ||
[DllImport(Libraries.NtDll)] | ||
private static extern int RtlGetVersion(out RTL_OSVERSIONINFOEX lpVersionInformation); | ||
|
||
internal static string RtlGetVersion() | ||
{ | ||
RTL_OSVERSIONINFOEX osvi = new RTL_OSVERSIONINFOEX(); | ||
osvi.dwOSVersionInfoSize = (uint)Marshal.SizeOf(osvi); | ||
const string version = "Microsoft Windows"; | ||
if (RtlGetVersion(out osvi) == 0) | ||
{ | ||
return string.Format("{0} {1}.{2}.{3} {4}", | ||
version, osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber, osvi.szCSDVersion); | ||
} | ||
else | ||
{ | ||
return version; | ||
} | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class mincore | ||
{ | ||
[DllImport(Libraries.SystemInfo_L1_2)] | ||
internal extern static void GetNativeSystemInfo(out SYSTEM_INFO lpSystemInfo); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
using System; | ||
using System.Runtime.InteropServices; | ||
|
||
internal partial class Interop | ||
{ | ||
internal partial class mincore | ||
{ | ||
[StructLayout(LayoutKind.Sequential)] | ||
internal struct SYSTEM_INFO | ||
{ | ||
internal ushort wProcessorArchitecture; | ||
internal ushort wReserved; | ||
internal int dwPageSize; | ||
internal IntPtr lpMinimumApplicationAddress; | ||
internal IntPtr lpMaximumApplicationAddress; | ||
internal IntPtr dwActiveProcessorMask; | ||
internal int dwNumberOfProcessors; | ||
internal int dwProcessorType; | ||
internal int dwAllocationGranularity; | ||
internal short wProcessorLevel; | ||
internal short wProcessorRevision; | ||
} | ||
|
||
internal enum ProcessorArchitecture : ushort | ||
{ | ||
Processor_Architecture_INTEL = 0, | ||
Processor_Architecture_ARM = 5, | ||
Processor_Architecture_IA64 = 6, | ||
Processor_Architecture_AMD64 = 9, | ||
Processor_Architecture_UNKNOWN = 0xFFFF | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#include "pal_runtimeinformation.h" | ||
#include "pal_types.h" | ||
#include <stdio.h> | ||
#include <sys/utsname.h> | ||
|
||
extern "C" int32_t GetUnixVersion(char* version, int* capacity) | ||
{ | ||
struct utsname _utsname; | ||
if (uname(&_utsname) != -1) | ||
{ | ||
int r = snprintf(version, static_cast<size_t>(*capacity), "%s %s %s", _utsname.sysname, _utsname.release, _utsname.version); | ||
if (r > *capacity) | ||
{ | ||
*capacity = r + 1; | ||
return -1; | ||
} | ||
} | ||
|
||
return 0; | ||
} | ||
|
||
/* Returns an int representing the OS Architecture: | ||
0 - x86 | ||
1 - x64 | ||
2 - ARM */ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Define a PAL enum in the .h and return it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed. |
||
extern "C" int32_t GetUnixArchitecture() | ||
{ | ||
#if defined(ARM) | ||
return ARCH_ARM; | ||
#elif defined(X64) | ||
return ARCH_X64; | ||
#elif defined(X86) | ||
return ARCH_X86; | ||
#error Unidentified Architecture | ||
#endif | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
#pragma once | ||
|
||
#include "pal_types.h" | ||
|
||
extern "C" int32_t GetUnixVersion(char* version, int* capacity); | ||
|
||
extern "C" int32_t GetUnixArchitecture(); | ||
|
||
enum | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why not use the scoped enumeration (i.e. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. scoped enumeration cannot implicitly convert to underlying intergral types, needs static_cast everywhere, where we only intend to use this enum to represent a name for the integrals. unscoped enumeration serves the purpose here with less complexity. |
||
{ | ||
ARCH_X86, | ||
ARCH_X64, | ||
ARCH_ARM | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright (c) Microsoft. All rights reserved. | ||
// Licensed under the MIT license. See LICENSE file in the project root for full license information. | ||
|
||
namespace System.Runtime.InteropServices | ||
{ | ||
public enum Architecture | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can this be defined in just one place? There is a copy in the ref... Also may be add a comment that this definition needs to be consistent with the native counterpart? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Scratch the last part - I see there is one more enum type for architecture - There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To separate the interop layer with the lib code. We may reuse GetUnixArchitecture() elsewhere without having to reference RuntimeInformation for Architecture. |
||
{ | ||
X86, | ||
X64, | ||
Arm | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Delete SetLastError - the implementation does not set last error