Files
Misaki.HighPerformance/Misaki.HighPerformance.Unsafe/Buffer/Arena .cs
Misaki 7bcd699eb9 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.
2025-03-25 09:49:49 +09:00

81 lines
2.4 KiB
C#

using Misaki.HighPerformance.Unsafe.Collections;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.Unsafe.Buffer;
/// <summary>
/// A memory management structure that allocates and resets memory blocks with specified alignment.
/// </summary>
public unsafe struct Arena : IDisposable
{
private void* _buffer;
private uint _size;
private uint _offset;
private bool _disposed;
public Arena(uint size)
{
_buffer = NativeMemory.Alloc(size);
_size = size;
_offset = 0;
}
/// <summary>
/// Allocates a block of memory of a specified size with a given alignment. Returns a pointer to the allocated
/// memory or null if allocation fails.
/// Must use <see cref="NativeMemory.AlignedFree"/> to free the memory.
/// </summary>
/// <param name="size">Specifies the amount of memory to allocate in bytes.</param>
/// <param name="alignSize">Defines the alignment requirement for the allocated memory.</param>
/// <returns>A pointer to the allocated memory block or null if the allocation cannot be fulfilled.</returns>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void* Allocate(uint size, uint alignSize, AllocationType allocationType)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var offset = (_offset + alignSize - 1) & ~(alignSize - 1);
if (offset + size > _size)
{
return null;
}
_offset = offset + size;
var ptr = (byte*)_buffer + offset;
if (allocationType == AllocationType.Clear)
{
MemClear(ptr, size);
}
return ptr;
}
/// <summary>
/// Resets the arena, optionally clearing the allocated memory.
/// </summary>
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
public void Reset(bool clear = false)
{
ObjectDisposedException.ThrowIf(_disposed, this);
if (clear)
{
MemClear(_buffer, _size);
}
_offset = 0;
}
public void Dispose()
{
NativeMemory.Free(_buffer);
_buffer = null;
_size = 0;
_offset = 0;
_disposed = true;
}
}