Added new TempJobAllocator

Added new AllocationHandle property in Stack.Scope.

Changed the ref AllocationHandle constructor parameter to AllocationHandle on of all UnsafeCollection types
Removed Allocator.Stack. Use Stack.Scope.AllocationHandle to allocate on stack instead.
This commit is contained in:
2025-12-06 22:16:39 +09:00
parent d6c472753d
commit f3b0f295a8
24 changed files with 301 additions and 170 deletions

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Contracts;
using System.Numerics;
using System.Runtime.CompilerServices;
@@ -78,7 +77,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
private readonly int _log2MinGrowth;
private MemoryHandle _memoryHandle;
private AllocationHandle* _allocationHandle;
private AllocationHandle _allocationHandle;
public const int MINIMAL_CAPACITY = 64;
@@ -90,7 +89,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
public readonly bool IsEmpty => !IsCreated || _count == 0;
public readonly bool IsCreated => _buffer != null && _allocationHandle != null && _memoryHandle.IsValid;
public readonly bool IsCreated => _buffer != null && _allocationHandle.pAllocator != null && _memoryHandle.IsValid;
private static int CalculateDataSize(int capacity, int bucketCapacity, int sizeOfTValue, out int outKeyOffset, out int outNextOffset, out int outBucketOffset)
{
@@ -110,7 +109,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return totalSize;
}
public HashMapHelper(int capacity, int sizeOfTValue, int alignOfTValue, uint minGrowth, ref AllocationHandle handle, AllocationOption allocationOption)
public HashMapHelper(int capacity, int sizeOfTValue, int alignOfTValue, uint minGrowth, AllocationHandle handle, AllocationOption allocationOption)
{
if (capacity <= 0)
{
@@ -133,7 +132,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_sizeOfTValue = sizeOfTValue;
_log2MinGrowth = BitOperations.Log2(minGrowth);
_allocationHandle = (AllocationHandle*)Unsafe.AsPointer(ref handle);
_allocationHandle = handle;
var totalSize = CalculateDataSize(_capacity, _bucketCapacity, sizeOfTValue,
out var keyOffset, out var nextOffset, out var bucketOffset);
@@ -193,7 +192,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
private void AllocateBuffer(int totalSize, int keyOffset, int nextOffset, int bucketOffset, AllocationOption allocationOption)
{
MemoryHandle memHandle;
var buf = (byte*)_allocationHandle->Alloc(_allocationHandle->Allocator, (uint)totalSize, (nuint)_alignment, allocationOption, &memHandle);
var buf = (byte*)_allocationHandle.Alloc(_allocationHandle.pAllocator, (uint)totalSize, (nuint)_alignment, allocationOption, &memHandle);
_buffer = buf;
_keys = (TKey*)(_buffer + keyOffset);
@@ -229,7 +228,7 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
}
}
_allocationHandle->Free(_allocationHandle->Allocator, oldBuffer, oldMemoryHandle);
_allocationHandle.Free(_allocationHandle.pAllocator, oldBuffer, oldMemoryHandle);
}
public void Resize(int newCapacity)
@@ -523,9 +522,9 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
return;
}
if (_allocationHandle != null)
if (_allocationHandle.pAllocator != null)
{
_allocationHandle->Free(_allocationHandle->Allocator, _buffer, _memoryHandle);
_allocationHandle.Free(_allocationHandle.pAllocator, _buffer, _memoryHandle);
}
_buffer = null;
@@ -537,4 +536,4 @@ public unsafe struct HashMapHelper<TKey> : IDisposable
_capacity = 0;
_bucketCapacity = 0;
}
}
}

View File

@@ -24,6 +24,17 @@ public static unsafe partial class MemoryUtility
return NativeMemory.Alloc(size);
}
/// <summary>
/// Allocates a block of memory of the specified size in bytes and initializes it to zero.
/// </summary>
/// <param name="size">Specifies the number of bytes to allocate in memory.</param>
/// <returns>Returns a pointer to the allocated and zero-initialized memory block.</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void* Calloc(nuint size)
{
return NativeMemory.AllocZeroed(size);
}
/// <summary>
/// Allocates a block of memory with a specified size and alignment.
/// </summary>
@@ -114,7 +125,7 @@ public static unsafe partial class MemoryUtility
/// <param name="destination">Specifies the memory address where the copied data will be stored.</param>
/// <param name="size">Defines the number of bytes to be copied from the source to the destination.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void MemCpy(void* source, void* destination, nuint size)
public static void MemCpy(void* destination, void* source, nuint size)
{
NativeMemory.Copy(source, destination, size);
}

View File

@@ -1,4 +1,3 @@
using Misaki.HighPerformance.LowLevel.Collections;
using System.Runtime.CompilerServices;
namespace Misaki.HighPerformance.LowLevel.Utilities;
@@ -172,4 +171,4 @@ public static unsafe class UnsafeUtility
return ptr;
}
}
}
}