Switch Marshal.AllocHGlobal to NativeMemory.Alloc
このコミットが含まれているのは:
1 親
0b1c1a85d3
コミット
a5f0045de8
5ファイル変更
+53
-13
検索条件に一致するファイルはありません
@@ -21,6 +21,7 @@
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\SkipLocalsInitAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\MemoryMarshal.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\CompilerServices\RuntimeHelpers.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\InteropServices\NativeMemory.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\OSPlatformAttribute.cs" />
|
||||
<Compile Include="$(MSBuildThisFileDirectory)System\Runtime\Versioning\SupportedOSVersionAttribute.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
namespace System.Runtime.InteropServices;
|
||||
|
||||
/// <summary>
|
||||
/// This class contains methods that are mainly used to manage native memory.
|
||||
/// </summary>
|
||||
internal static unsafe class NativeMemory
|
||||
{
|
||||
/// <summary>
|
||||
/// Allocates a block of memory of the specified size, in bytes.
|
||||
/// </summary>
|
||||
/// <param name="byteCount">The size, in bytes, of the block to allocate.</param>
|
||||
/// <returns>A pointer to the allocated block of memory.</returns>
|
||||
/// <exception cref="OutOfMemoryException">Thrown when allocating <paramref name="byteCount"/> of memory failed.</exception>
|
||||
/// <remarks>
|
||||
/// <para>This method allows <paramref name="byteCount" /> to be <c>0</c> and will return a valid pointer that should not be dereferenced and that should be passed to free to avoid memory leaks.</para>
|
||||
/// <para>This method is a thin wrapper over the C <c>malloc</c> API.</para>
|
||||
/// </remarks>
|
||||
public static void* Alloc(nuint byteCount)
|
||||
{
|
||||
return (void*)Marshal.AllocHGlobal(checked((int)byteCount));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Frees a block of memory.
|
||||
/// </summary>
|
||||
/// <param name="ptr">A pointer to the block of memory that should be freed.</param>
|
||||
/// <remarks>
|
||||
/// <para>This method does nothing if <paramref name="ptr" /> is <see langword="null"/>.</para>
|
||||
/// <para>This method is a thin wrapper over the C <c>free</c> API.</para>
|
||||
/// </remarks>
|
||||
public static void Free(void* ptr)
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)ptr);
|
||||
}
|
||||
}
|
||||
@@ -22,7 +22,6 @@
|
||||
AttributeTargets.Struct,
|
||||
AllowMultiple = true,
|
||||
Inherited = false)]
|
||||
|
||||
internal sealed class SupportedOSPlatformAttribute : OSPlatformAttribute
|
||||
{
|
||||
public SupportedOSPlatformAttribute(string platformName)
|
||||
|
||||
新しいイシューから参照
ユーザーをブロックする