Better handling when size is 0

This commit is contained in:
2026-05-10 21:48:35 +09:00
parent 4b9d93ec65
commit f4abe2036a
7 changed files with 37 additions and 6 deletions

View File

@@ -1,6 +1,7 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Diagnostics;
using System.Drawing;
using System.Numerics;
using System.Runtime.CompilerServices;
@@ -125,9 +126,13 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public HashMapHelper(int capacity, int sizeOfTValue, int alignOfTValue, uint minGrowth, AllocationHandle handle, AllocationOption allocationOption)
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
ArgumentOutOfRangeException.ThrowIfNegative(sizeOfTValue);
ArgumentOutOfRangeException.ThrowIfNegative(alignOfTValue);
if (capacity <= 0 || sizeOfTValue <= 0 || alignOfTValue <= 0)
{
Debug.Assert(capacity >= 0);
Debug.Assert(sizeOfTValue >= 0);
Debug.Assert(alignOfTValue >= 0);
return;
}
_capacity = CalcCapacityCeilPow2(capacity);
_bucketCapacity = _capacity * 2;

View File

@@ -134,7 +134,11 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public UnsafeArray(int count, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
ArgumentOutOfRangeException.ThrowIfNegative(count);
if (count <= 0)
{
Debug.Assert(count >= 0);
return;
}
_buffer = (T*)handle.Alloc((nuint)(count * sizeof(T)), MemoryUtility.AlignOf<T>(), allocationOption);
#if MHP_ENABLE_SAFETY_CHECKS

View File

@@ -68,12 +68,18 @@ public unsafe struct UnsafeParallelHashMap<TKey, TValue> : IDisposable
public UnsafeParallelHashMap(int capacity, uint minGrowth, AllocationHandle handle, AllocationOption allocationOption)
{
ArgumentOutOfRangeException.ThrowIfNegative(capacity);
if (capacity <= 0)
{
Debug.Assert(capacity >= 0);
return;
}
_data = (UnsafeParallelHashMapData<TKey, TValue>*)handle.Alloc((uint)sizeof(UnsafeParallelHashMapData<TKey, TValue>), (nuint)AlignOf<UnsafeParallelHashMapData<TKey, TValue>>(), AllocationOption.Clear);
if (_data == null)
{
throw new OutOfMemoryException("Failed to allocate UnsafeParallelHashMapData.");
}
_data->capacity = capacity;
_data->bucketCapacityMask = capacity * 2 - 1;
@@ -102,7 +108,9 @@ public unsafe struct UnsafeParallelHashMap<TKey, TValue> : IDisposable
public void Dispose()
{
if (!IsCreated)
{
return;
}
#if MHP_ENABLE_SAFETY_CHECKS
_data->memoryHandle.Dispose();