-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Open
Milestone
Description
There are a number of unnecessary uses of Unsafe.AsPointer in this repository (e.g.
| _EnumerateConfigurationValues(Unsafe.AsPointer(ref context), &ConfigCallback); |
| (byte*)Unsafe.AsPointer(ref _blocks[0]), // This is safe to do since we are a ref struct |
Most of the uses can be broken in to 2 categories:
- Aligning or detecting alignment of a reference (we can introduce helper APIs for this, e.g. )
nuint misalignedElements = ((nuint)Unsafe.AsPointer(ref searchSpace) & (nuint)(Vector256<byte>.Count - 1)) / (nuint)sizeof(T); - Getting a pointer to something that is known to be pinned (many of these cases could be converted to ref fields instead, using refs/spans directly, or passing by pointer, e.g. )
runtime/src/mono/System.Private.CoreLib/src/System/Reflection/TypeNameParser.Mono.cs
Line 48 in eaa6f01
_stackMark = Unsafe.AsPointer(ref stackMark)
The idea was mentioned by @tannergooding on the discord, and one of the suggested solutions was to create an internal API bool IsOpportunisticallyAligned(ref T address, nuint aligment) or similar for the alignment uses.
I am happy to make PRs for these. @tannergooding suggested that I make one for each area separately. Just making this issue to track it and get support for the general idea from people.