Files
Misaki.HighPerformance/Misaki.HighPerformance.Unsafe/Buffer/Arena .cs
Misaki 3c555a9489 Update memory allocation practices and clean up code
Changed the `ParallelNoiseBenchmark` class to use `AllocationOption.None` for `UnsafeArray<float>` instances.
Changed the `AllocationOption` enum to replace `UnInitialized` with `None` and clarified the `UnTracked` option.
Changed constructors for `UnsafeArray<T>`, `UnsafeList<T>`, `UnsafeQueue<T>`, and `UnsafeStack<T>` to use `AllocationOption.None`.
Changed the `HashMapHelper<TKey>` constructor to accept an `AllocationOption` parameter for improved flexibility.
Changed the `Allocate` method documentation in `Arena.cs` to clarify memory management.
Improved the `AllocationManager` class with a default arena size and updated allocation logic to handle new `AllocationOption` flags.
Removed the import statement for `BenchmarkDotNet.Running` in `Program.cs` and added new imports for `Misaki.HighPerformance.Unsafe.Collections` and `Misaki.HighPerformance.Unsafe.Services`.
Added a new `UnsafeArray<int>` in `Program.cs` and called `AllocationManager.Dispose()` to clean up resources.
2025-04-03 16:59:32 +09:00

81 lines
2.4 KiB
C#

using Misaki.HighPerformance.Unsafe.Collections;
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 byte* _buffer;
private uint _size;
private uint _offset;
private bool _disposed;
public Arena(uint size)
{
_buffer = (byte*)Malloc(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.
/// You don't need to free the memory manually, it will be freed when the arena is disposed.
/// </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>
/// <param name="allocationOption">The option when allocating 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, AllocationOption allocationOption)
{
ObjectDisposedException.ThrowIf(_disposed, this);
var offset = (_offset + alignSize - 1) & ~(alignSize - 1);
if (offset + size > _size)
{
return null;
}
_offset = offset + size;
var ptr = _buffer + offset;
if (allocationOption.HasFlag(AllocationOption.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()
{
Free(_buffer);
_buffer = null;
_size = 0;
_offset = 0;
_disposed = true;
}
}