mirror of
https://github.com/DCFApixels/DragonECS.git
synced 2025-09-18 01:44:35 +08:00
Update ArrayUtility.cs
This commit is contained in:
parent
89cb4c68b9
commit
82c5dbc939
@ -1,4 +1,8 @@
|
||||
namespace DCFApixels.DragonECS.Utils
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System;
|
||||
|
||||
namespace DCFApixels.DragonECS.Utils
|
||||
{
|
||||
internal static class ArrayUtility
|
||||
{
|
||||
@ -12,4 +16,39 @@
|
||||
array[i] = value;
|
||||
}
|
||||
}
|
||||
|
||||
internal static unsafe class UnmanagedArrayUtility
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* New<T>(int capacity) where T : struct
|
||||
{
|
||||
return Marshal.AllocHGlobal(Marshal.SizeOf(typeof(T)) * capacity).ToPointer();
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* NewAndInit<T>(int capacity) where T : struct
|
||||
{
|
||||
int newSize = Marshal.SizeOf(typeof(T)) * capacity;
|
||||
byte* newPointer = (byte*)Marshal.AllocHGlobal(newSize).ToPointer();
|
||||
|
||||
for (int i = 0; i < newSize; i++)
|
||||
*(newPointer + i) = 0;
|
||||
|
||||
return newPointer;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void Free(void* pointer)
|
||||
{
|
||||
Marshal.FreeHGlobal(new IntPtr(pointer));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* Resize<T>(void* oldPointer, int newCount) where T : struct
|
||||
{
|
||||
return (Marshal.ReAllocHGlobal(
|
||||
new IntPtr(oldPointer),
|
||||
new IntPtr(Marshal.SizeOf(typeof(T)) * newCount))).ToPointer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user