KEMBAR78
Implement RuntimeHelpers.SizeOf by jkoritzinsky · Pull Request #100618 · dotnet/runtime · GitHub
Skip to content

Conversation

@jkoritzinsky
Copy link
Member

Fixes #97344

Implement the SizeOf API with the same behavior as the sizeof IL instruction.

@ghost
Copy link

ghost commented Apr 3, 2024

Note regarding the new-api-needs-documentation label:

This serves as a reminder for when your PR is modifying a ref *.cs file and adding/modifying public APIs, please make sure the API implementation in the src *.cs file is documented with triple slash comments, so the PR reviewers can sign off that change.

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
…/Runtime/CompilerServices/RuntimeHelpersTests.cs

Co-authored-by: Jan Kotas <jkotas@microsoft.com>
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
/// <returns>The size of instances of the type.</returns>
/// <exception cref="ArgumentException">The passed-in type is not a valid type to get the size of.</exception>
/// <remarks>
/// This API has the same behavior as if you were to use the IL sizeof instruction with the passed in type as the operand.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
/// This API has the same behavior as if you were to use the IL sizeof instruction with the passed in type as the operand.
/// This API returns the same value as the IL sizeof instruction with the passed in type as the operand.

Nit: It does not have the same behavior for error cases.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

# Conflicts:
#	src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.CoreCLR.cs
#	src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs
#	src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs
#	src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs
#	src/mono/mono/metadata/icall.c

Type typeObj = Type.GetTypeFromHandle(type)!;
if (typeObj.ContainsGenericParameters || typeObj.IsGenericParameter || typeObj == typeof(void))
throw new ArgumentException(SR.Arg_TypeNotSupported);
Copy link
Member

Choose a reason for hiding this comment

The 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)?

Copy link
Member Author

Choose a reason for hiding this comment

The 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).

@jkoritzinsky
Copy link
Member Author

/ba-g Slow mac timeout

@jkoritzinsky jkoritzinsky merged commit f54c778 into dotnet:main Apr 9, 2024
@jkoritzinsky jkoritzinsky deleted the sizeof branch April 9, 2024 19:55
matouskozak pushed a commit to matouskozak/runtime that referenced this pull request Apr 30, 2024
Co-authored-by: Jan Kotas <jkotas@microsoft.com>
@github-actions github-actions bot locked and limited conversation to collaborators May 10, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[API Proposal]: Get the managed size of a System.Type instance

3 participants