Skip to content

Commit 2bc401a

Browse files
Apply suggestions from code review
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 2ee0646 commit 2bc401a

File tree

4 files changed

+10
-7
lines changed

4 files changed

+10
-7
lines changed

src/coreclr/nativeaot/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.NativeAot.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics.CodeAnalysis;
5+
using System.Numerics;
56
using System.Runtime.InteropServices;
67
using System.Runtime.Serialization;
78
using System.Threading;
@@ -251,9 +252,9 @@ public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size)
251252
/// <exception cref="ArgumentException"><paramref name="type" /> must be a type provided by the runtime.</exception>
252253
/// <exception cref="ArgumentOutOfRangeException"><paramref name="size" /> is negative.</exception>
253254
/// <exception cref="ArgumentException"><paramref name="alignment" /> is not a power of <c>2</c>.</exception>
254-
public static IntPtr AllocateTypeAssociatedMemory(Type type, int size, int alignment)
255+
public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size, int alignment)
255256
{
256-
if (type is not RuntimeType rt)
257+
if (type is not RuntimeType)
257258
{
258259
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
259260
}

src/coreclr/vm/runtimehandles.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ extern "C" void* QCALLTYPE RuntimeTypeHandle_AlignedAllocateTypeAssociatedMemory
12291229
// that the memory will be freed when the type is unloaded.
12301230
PTR_LoaderAllocator loaderAllocator = typeHandle.GetMethodTable()->GetLoaderAllocator();
12311231
LoaderHeap* loaderHeap = loaderAllocator->GetHighFrequencyHeap();
1232-
allocatedMemory = loaderHeap->AllocAlignedMem(S_SIZE_T(size), S_SIZE_T(alignment));
1232+
allocatedMemory = loaderHeap->AllocAlignedMem(S_SIZE_T(size).Value(), S_SIZE_T(alignment).Value());
12331233

12341234
END_QCALL;
12351235

src/libraries/System.Runtime/tests/System.Runtime.Tests/System/Runtime/CompilerServices/RuntimeHelpersTests.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,8 @@ public static void AlignedAllocateTypeAssociatedMemoryInvalidArguments()
457457
{
458458
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(null, 10, 1); });
459459
Assert.Throws<ArgumentOutOfRangeException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 1); });
460-
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 0); });
461-
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1, 3); });
460+
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 10, 0); });
461+
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 10, 3); });
462462
}
463463

464464

@@ -469,6 +469,7 @@ public static unsafe void AlignedAllocateTypeAssociatedMemoryValidArguments()
469469
Assert.NotEqual(memory, IntPtr.Zero);
470470
// Validate that the memory is zeroed out
471471
Assert.True(new Span<byte>((void*)memory, 32).SequenceEqual(new byte[32]));
472+
Assert.True((memory % 16) == 0);
472473
}
473474

474475
#pragma warning disable CS0649

src/mono/System.Private.CoreLib/src/System/Runtime/CompilerServices/RuntimeHelpers.Mono.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics;
55
using System.Diagnostics.CodeAnalysis;
66
using System.Diagnostics.Tracing;
7+
using System.Numerics;
78
using System.Runtime.InteropServices;
89
using System.Runtime.Serialization;
910

@@ -166,9 +167,9 @@ public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size)
166167
/// <exception cref="ArgumentException"><paramref name="type" /> must be a type provided by the runtime.</exception>
167168
/// <exception cref="ArgumentOutOfRangeException"><paramref name="size" /> is negative.</exception>
168169
/// <exception cref="ArgumentException"><paramref name="alignment" /> is not a power of <c>2</c>.</exception>
169-
public static IntPtr AllocateTypeAssociatedMemory(Type type, int size, int alignment)
170+
public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size, int alignment)
170171
{
171-
if (type is not RuntimeType rt)
172+
if (type is not RuntimeType)
172173
{
173174
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
174175
}

0 commit comments

Comments
 (0)