Enhance noise generation and memory management
Added new noise generation methods in `ParallelNoiseBenchmark`, including `Frac`, `Lerp`, and `GradientNoiseDirect`, and updated the `GradientNoise` method to utilize them. Changed constants to use `_LENGTH` for consistency. Changed `Arena` and `DynamicArena` classes to use `uint` instead of `ulong` for size fields, improving memory usage. Updated memory allocation to use `NativeMemory` for better performance and safety. Updated `UnsafeArray<T>` and `UnsafeList<T>` classes to replace `Marshal` methods with `NativeMemory`, enhancing performance and safety. Modified `Dispose` methods to use `NativeMemory.AlignedFree`. Added `MemoryUtilities` class with new methods for memory management, including `MemClear`, `MemSet`, `MemCpy`, `SizeOf`, and `AlignOf`, utilizing `NativeMemory`. Fixed minor cleanup in the `ObjectPool` class's `Dispose` method.
This commit is contained in:
@@ -78,7 +78,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
|
||||
public UnsafeArray(int size, AllocationType allocationType)
|
||||
{
|
||||
_size = size;
|
||||
_buffer = (T*)Marshal.AllocHGlobal(size * sizeof(T)).ToPointer();
|
||||
_buffer = (T*)NativeMemory.AlignedAlloc((nuint)(size * sizeof(T)), (nuint)AlignOf<T>());
|
||||
|
||||
if (allocationType == AllocationType.Clear)
|
||||
{
|
||||
@@ -93,7 +93,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer = (T*)Marshal.ReAllocHGlobal((IntPtr)_buffer, newSize * sizeof(T)).ToPointer();
|
||||
_buffer = (T*)NativeMemory.AlignedRealloc(_buffer, (nuint)(newSize * sizeof(T)), (nuint)AlignOf<T>());
|
||||
_size = newSize;
|
||||
}
|
||||
|
||||
@@ -105,7 +105,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)_buffer);
|
||||
NativeMemory.AlignedFree(_buffer);
|
||||
|
||||
_buffer = null;
|
||||
_size = 0;
|
||||
|
||||
@@ -124,7 +124,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T> where
|
||||
|
||||
public UnsafeList(int capacity, AllocationType allocationType)
|
||||
{
|
||||
_buffer = (T*)Marshal.AllocHGlobal(capacity * sizeof(T));
|
||||
_buffer = (T*)NativeMemory.AlignedAlloc((nuint)(capacity * sizeof(T)), (nuint)AlignOf<T>());
|
||||
_size = 0;
|
||||
_capacity = capacity;
|
||||
|
||||
@@ -270,7 +270,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T> where
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer = (T*)Marshal.ReAllocHGlobal((IntPtr)_buffer, newSize * sizeof(T)).ToPointer();
|
||||
_buffer = (T*)NativeMemory.AlignedRealloc(_buffer, (nuint)(newSize * sizeof(T)), (nuint)AlignOf<T>());
|
||||
_size = newSize;
|
||||
}
|
||||
|
||||
@@ -282,7 +282,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T> where
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)_buffer);
|
||||
NativeMemory.AlignedFree(_buffer);
|
||||
|
||||
_buffer = null;
|
||||
_size = 0;
|
||||
|
||||
Reference in New Issue
Block a user