Files
Misaki.HighPerformance/Misaki.HighPerformance.Unsafe/Collections/UnsafeArray.cs
Misaki 791be1bed2 Refactor memory management and allocation strategies
Changed the `AllocationManager` initialization to specify size in `Program.cs` and commented out the disposal of `unfreeArray`.
Changed `UnsafeArray` to use `AllocationManager.Realloc` for memory reallocation.
Changed constructors of `UnsafeHashMap` and `UnsafeHashSet` to include an `AllocationOption` parameter for improved memory management.
Added a `Remove` method to `UnsafeHashMap` for key-value pair removal.
Changed `AllocationManager` to use a hash map instead of a queue for managing allocations.
Added a `Realloc` method in `AllocationManager` for handling memory reallocation.
Changed the `Free` method in `AllocationManager` to iterate through the hash map for freeing allocated memory.
2025-04-03 17:46:55 +09:00

156 lines
4.7 KiB
C#

using Misaki.HighPerformance.Unsafe.Collections.Contracts;
using Misaki.HighPerformance.Unsafe.Helpers;
using Misaki.HighPerformance.Unsafe.Services;
using System.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.Unsafe.Collections;
/// <summary>
/// A structure for managing an array of unmanaged types with unsafe memory operations.
/// </summary>
/// <typeparam name="T">Represents a type that can be stored in an unmanaged memory context.</typeparam>
public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
where T : unmanaged
{
public struct Enumerator : IEnumerator<T>
{
private UnsafeArray<T>* _collection;
private int _index;
private T _value;
public Enumerator(UnsafeArray<T>* collection)
{
_collection = collection;
_index = -1;
_value = default;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext()
{
_index++;
if (_index < _collection->_count)
{
_value = UnsafeUtilities.ReadArrayElement<T>(_collection->_buffer, _index);
return true;
}
_value = default;
return false;
}
public void Reset()
{
_index = -1;
}
// Let NativeArray indexer check for out of range.
public readonly T Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _value;
}
readonly object IEnumerator.Current
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Current;
}
public void Dispose()
{
}
}
private T* _buffer;
private int _count;
private readonly Allocator _allocator;
public readonly int Count => _count;
public readonly ref T this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
}
public readonly bool IsCreated
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => _buffer != null;
}
public IEnumerator<T> GetEnumerator() => new Enumerator((UnsafeArray<T>*)UnsafeUtilities.AddressOf(ref this));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
/// <summary>
/// Initializes a new instance of UnsafeArray with a specified number of elements and an allocation type. It
/// allocates memory and optionally clears it.
/// </summary>
/// <param name="count">Specifies the number of elements to allocate in the array, which must be greater than zero.</param>
/// <param name="allocator">Specifies the allocator to use for memory allocation, which determines the memory management strategy.</param>
/// <param name="allocationOption">Determines how the allocated memory should be initialized, either uninitialized or cleared.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public UnsafeArray(int count, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{
if (count <= 0)
{
throw new ArgumentOutOfRangeException(nameof(count), "Count must be greater than zero.");
}
_buffer = AllocationManager.Allocate<T>((uint)count, (uint)AlignOf<T>(), allocator, allocationOption);
_count = count;
_allocator = allocator;
if (allocationOption == AllocationOption.Clear)
{
Clear();
}
}
/// <summary>
/// Initializes an UnsafeArray with a pointer to a buffer and a count of elements.
/// </summary>
/// <param name="buffer">A pointer to the memory location that holds the elements of the array.</param>
/// <param name="count">The total size of the data.</param>
public UnsafeArray(void* buffer, int count)
{
_buffer = (T*)buffer;
_count = count;
}
public void Resize(int newSize)
{
if (newSize == _count)
{
return;
}
_buffer = AllocationManager.Realloc<T>(_buffer, (uint)newSize, (uint)AlignOf<T>(), _allocator);
_count = newSize;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void Clear()
{
MemClear(_buffer, (nuint)(_count * sizeof(T)));
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly void* GetUnsafePtr()
{
return _buffer;
}
public void Dispose()
{
AllocationManager.Free(_buffer, _allocator);
_buffer = null;
_count = 0;
}
}