Skip to content

Commit 5cd274b

Browse files
Copy the NativeAOT AllocateTypeAssociatedMemory to Mono (#99072)
* Copy the NativeAOT AllocateTypeAssociatedMemory to Mono Found in #99019 * Update RuntimeHelpersTests.cs * Update RuntimeHelpers.Mono.cs * Update RuntimeHelpers.Mono.cs
1 parent 233b110 commit 5cd274b

File tree

2 files changed

+9
-4
lines changed

2 files changed

+9
-4
lines changed

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

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -394,15 +394,13 @@ public static void ArrayGetSubArrayCoVarianceTest()
394394
}
395395

396396
[Fact]
397-
[SkipOnMono("Not presently implemented on Mono")]
398397
public static void AllocateTypeAssociatedMemoryInvalidArguments()
399398
{
400399
Assert.Throws<ArgumentException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(null, 10); });
401400
Assert.Throws<ArgumentOutOfRangeException>(() => { RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), -1); });
402401
}
403402

404403
[Fact]
405-
[SkipOnMono("Not presently implemented on Mono")]
406404
public static unsafe void AllocateTypeAssociatedMemoryValidArguments()
407405
{
408406
IntPtr memory = RuntimeHelpers.AllocateTypeAssociatedMemory(typeof(RuntimeHelpersTests), 32);

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

Lines changed: 9 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.Runtime.InteropServices;
78
using System.Runtime.Serialization;
89

910
namespace System.Runtime.CompilerServices
@@ -137,9 +138,15 @@ public static void RunModuleConstructor(ModuleHandle module)
137138
RunModuleConstructor(module.Value);
138139
}
139140

140-
public static IntPtr AllocateTypeAssociatedMemory(Type type, int size)
141+
public static unsafe IntPtr AllocateTypeAssociatedMemory(Type type, int size)
141142
{
142-
throw new PlatformNotSupportedException();
143+
if (type is not RuntimeType)
144+
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
145+
146+
ArgumentOutOfRangeException.ThrowIfNegative(size);
147+
148+
// We don't support unloading; the memory will never be freed.
149+
return (IntPtr)NativeMemory.AllocZeroed((uint)size);
143150
}
144151

145152
[Intrinsic]

0 commit comments

Comments
 (0)