- Notifications
You must be signed in to change notification settings - Fork5.1k
Implement RuntimeHelpers.SizeOf#100618
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Changes from11 commits
3362c4a
47c6a24
16ea830
cb7ec92
47d3b54
c086418
84b4c0f
49e6521
f93baee
6898911
3423a52
1c473f6
21fde4e
51a93b2
ec81d77
ed05d1a
7908cf6
File filter
Filter by extension
Conversations
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -416,6 +416,23 @@ private static unsafe void DispatchTailCalls( | ||
} | ||
} | ||
} | ||
[LibraryImport(QCall, EntryPoint = "ReflectionInvocation_SizeOf")] | ||
[SuppressGCTransition] | ||
private static partial int SizeOf(QCallTypeHandle handle); | ||
public static unsafe int SizeOf(RuntimeTypeHandle type) | ||
jkoritzinsky marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
{ | ||
if (type.IsNullHandle()) | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.type); | ||
int result = SizeOf(new QCallTypeHandle(ref type)); | ||
if (result <= 0) | ||
throw new ArgumentException(SR.Arg_TypeNotSupported); | ||
return result; | ||
} | ||
} | ||
// Helper class to assist with unsafe pinning of arbitrary objects. | ||
// It's used by VM code. | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -361,6 +361,27 @@ public static unsafe object GetUninitializedObject( | ||
return RuntimeImports.RhNewObject(mt); | ||
} | ||
public static unsafe int SizeOf(RuntimeTypeHandle type) | ||
{ | ||
if (type.IsNull) | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.type); | ||
MethodTable* mt = type.ToMethodTable(); | ||
if (mt->ElementType == EETypeElementType.Void | ||
|| mt->ContainsGenericParameters) | ||
jkoritzinsky marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
{ | ||
throw new ArgumentException(SR.Arg_TypeNotSupported); | ||
} | ||
if (mt->IsValueType) | ||
{ | ||
return (int)mt->ValueTypeSize; | ||
} | ||
return nint.Size; | ||
} | ||
} | ||
// CLR arrays are laid out in memory as follows (multidimensional array bounds are optional): | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -437,6 +437,44 @@ public static void FixedAddressValueTypeTest() | ||
Assert.Equal(fixedPtr1, fixedPtr2); | ||
} | ||
[InlineArray(3)] | ||
private struct Byte3 | ||
{ | ||
public byte b1; | ||
} | ||
[Fact] | ||
public static unsafe void SizeOf() | ||
{ | ||
Assert.Equal(1, RuntimeHelpers.SizeOf(typeof(sbyte).TypeHandle)); | ||
Assert.Equal(1, RuntimeHelpers.SizeOf(typeof(byte).TypeHandle)); | ||
Assert.Equal(2, RuntimeHelpers.SizeOf(typeof(short).TypeHandle)); | ||
Assert.Equal(2, RuntimeHelpers.SizeOf(typeof(ushort).TypeHandle)); | ||
Assert.Equal(4, RuntimeHelpers.SizeOf(typeof(int).TypeHandle)); | ||
Assert.Equal(4, RuntimeHelpers.SizeOf(typeof(uint).TypeHandle)); | ||
Assert.Equal(8, RuntimeHelpers.SizeOf(typeof(long).TypeHandle)); | ||
Assert.Equal(8, RuntimeHelpers.SizeOf(typeof(ulong).TypeHandle)); | ||
Assert.Equal(4, RuntimeHelpers.SizeOf(typeof(float).TypeHandle)); | ||
Assert.Equal(8, RuntimeHelpers.SizeOf(typeof(double).TypeHandle)); | ||
Assert.Equal(3, RuntimeHelpers.SizeOf(typeof(Byte3).TypeHandle)); | ||
Assert.Equal(nint.Size, RuntimeHelpers.SizeOf(typeof(void*).TypeHandle)); | ||
jkoritzinsky marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
Assert.Equal(nint.Size, RuntimeHelpers.SizeOf(typeof(delegate* <void>).TypeHandle)); | ||
Assert.Equal(nint.Size, RuntimeHelpers.SizeOf(typeof(int).MakeByRefType().TypeHandle)); | ||
Assert.Throws<ArgumentNullException>(() => RuntimeHelpers.SizeOf(default)); | ||
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.SizeOf(typeof(List<>).TypeHandle)); | ||
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.SizeOf(typeof(Dictionary<,>).MakeGenericType([ typeof(int), typeof(Dictionary<,>).GetGenericArguments()[1] ]).TypeHandle)); | ||
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.SizeOf(typeof(void).TypeHandle)); | ||
} | ||
// We can't even get a RuntimeTypeHandle for a generic parameter type on NativeAOT, | ||
// so we don't even get to the method we're testing. | ||
// So, let's not even waste time running this test on NativeAOT | ||
[ConditionalFact(typeof(PlatformDetection), nameof(PlatformDetection.IsNotNativeAot))] | ||
public static void SizeOfGenericParameter() | ||
{ | ||
Assert.ThrowsAny<ArgumentException>(() => RuntimeHelpers.SizeOf(typeof(List<>).GetGenericArguments()[0].TypeHandle)); | ||
jkoritzinsky marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
} | ||
} | ||
public struct Age | ||
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -211,5 +211,20 @@ private static extern unsafe IntPtr GetSpanDataFrom( | ||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
private static extern bool SufficientExecutionStack(); | ||
[MethodImplAttribute(MethodImplOptions.InternalCall)] | ||
private static extern int SizeOf(QCallTypeHandle handle); | ||
public static int SizeOf(RuntimeTypeHandle type) | ||
{ | ||
if (type.Value == IntPtr.Zero) | ||
ThrowHelper.ThrowArgumentNullException(ExceptionArgument.type); | ||
Type typeObj = Type.GetTypeFromHandle(type)!; | ||
if (typeObj.ContainsGenericParameters || typeObj.IsGenericParameter || typeObj == typeof(void)) | ||
throw new ArgumentException(SR.Arg_TypeNotSupported); | ||
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. Is this a limitation of mono runtime (which doesn't exist in coreclr)? 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. No, this is the same limitation in CoreCLR and NativeAOT (though the implementations are slightly different due to how the different type systems are implemented). | ||
return SizeOf(new QCallTypeHandle(ref type)); | ||
} | ||
} | ||
} |