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.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Runtime.CompilerServices;
@@ -13,12 +12,12 @@ public unsafe struct UnTypedArray : IUnTypedCollection
private nuint _alignment;
private MemoryHandle _memoryHandle;
private AllocationHandle* _allocationHandle;
private AllocationHandle _allocationHandle;
public readonly nuint Size => _size;
public readonly nuint Alignment => _alignment;
public readonly bool IsCreated => _buffer != null && _allocationHandle != null && _memoryHandle.IsValid;
public readonly bool IsCreated => _buffer != null && _allocationHandle.pAllocator != null && _memoryHandle.IsValid;
/// <summary>
/// Constructs an UnsafeArray with a default size of 1 and uses the Persistent allocator.
@@ -28,7 +27,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
{
}
public UnTypedArray(nuint size, nuint alignment, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnTypedArray(nuint size, nuint alignment, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
if (size <= 0)
{
@@ -36,12 +35,12 @@ public unsafe struct UnTypedArray : IUnTypedCollection
}
MemoryHandle memHandle;
_buffer = handle.Alloc(_allocationHandle->Allocator, size, alignment, allocationOption, &memHandle);
_buffer = handle.Alloc(_allocationHandle.pAllocator, size, alignment, allocationOption, &memHandle);
_size = size;
_alignment = alignment;
_memoryHandle = memHandle;
_allocationHandle = (AllocationHandle*)Unsafe.AsPointer(ref handle);
_allocationHandle = handle;
}
/// <summary>
@@ -52,7 +51,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
/// <param name="allocationOption">Determines how the memory should be allocated.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public UnTypedArray(nuint size, nuint alignment, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(size, alignment, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(size, alignment, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -88,7 +87,7 @@ public unsafe struct UnTypedArray : IUnTypedCollection
}
MemoryHandle memHandle = _memoryHandle;
_buffer = _allocationHandle->Realloc(_allocationHandle->Allocator, _buffer, _size, newSize, _alignment, option, &memHandle);
_buffer = _allocationHandle.Realloc(_allocationHandle.pAllocator, _buffer, _size, newSize, _alignment, option, &memHandle);
_size = newSize;
_memoryHandle = memHandle;
}
@@ -233,14 +232,13 @@ public unsafe struct UnTypedArray : IUnTypedCollection
return;
}
if (_allocationHandle != null)
if (_allocationHandle.pAllocator != null)
{
_allocationHandle->Free(_allocationHandle->Allocator, _buffer, _memoryHandle);
_allocationHandle.Free(_allocationHandle.pAllocator, _buffer, _memoryHandle);
}
_allocationHandle = null;
_buffer = null;
_size = 0;
_alignment = 0;
}
}
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Diagnostics;
@@ -50,7 +49,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
private T* _buffer;
private int _count;
private MemoryHandle _memoryHandle;
private AllocationHandle* _allocationHandle;
private AllocationHandle _allocationHandle;
public readonly int Count => _count;
@@ -74,7 +73,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
}
}
public readonly bool IsCreated => _buffer != null && _allocationHandle != null && _memoryHandle.IsValid;
public readonly bool IsCreated => _buffer != null && _allocationHandle.pAllocator != null && _memoryHandle.IsValid;
public Enumerator GetEnumerator() => new((UnsafeArray<T>*)UnsafeUtility.AddressOf(ref this));
IEnumerator<T> IEnumerable<T>.GetEnumerator() => GetEnumerator();
@@ -95,7 +94,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <param name="handle">A reference to an AllocationHandle that manages the memory allocation for the array.</param>
/// <param name="allocationOption">Specifies how the memory should be allocated.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified number of elements is less than or equal to zero.</exception>
public UnsafeArray(int count, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeArray(int count, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
if (count < 0)
{
@@ -103,11 +102,11 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
}
MemoryHandle memHandle;
var buff = handle.Alloc(handle.Allocator, (nuint)(count * sizeof(T)), AlignOf<T>(), allocationOption, &memHandle);
var buff = handle.Alloc(handle.pAllocator, (nuint)(count * sizeof(T)), AlignOf<T>(), allocationOption, &memHandle);
_buffer = (T*)buff;
_memoryHandle = memHandle;
_allocationHandle = (AllocationHandle*)Unsafe.AsPointer(ref handle);
_allocationHandle = handle;
_count = count;
}
@@ -119,7 +118,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
/// <param name="allocationOption">Determines how the memory should be allocated.</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)
: this(count, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(count, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -181,7 +180,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
MemoryHandle memHandle = _memoryHandle;
var elemSize = SizeOf<T>();
_buffer = (T*)_allocationHandle->Realloc(_allocationHandle->Allocator, _buffer, (nuint)Count * elemSize, (nuint)newSize * elemSize, AlignOf<T>(), option, &memHandle);
_buffer = (T*)_allocationHandle.Realloc(_allocationHandle.pAllocator, _buffer, (nuint)Count * elemSize, (nuint)newSize * elemSize, AlignOf<T>(), option, &memHandle);
_memoryHandle = memHandle;
_count = newSize;
}
@@ -245,13 +244,12 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
return;
}
if (_allocationHandle != null)
if (_allocationHandle.pAllocator != null)
{
_allocationHandle->Free(_allocationHandle->Allocator, _buffer, _memoryHandle);
_allocationHandle.Free(_allocationHandle.pAllocator, _buffer, _memoryHandle);
}
_allocationHandle = null;
_buffer = null;
_count = 0;
}
}
}

View File

@@ -1,5 +1,4 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Numerics;
using System.Runtime.CompilerServices;
@@ -66,19 +65,19 @@ public unsafe struct UnsafeBitSet : IDisposable, IEquatable<UnsafeBitSet>
/// <summary>
/// Initializes a new instance of the <see cref="UnsafeBitSet" /> class.
/// </summary>
public UnsafeBitSet(int minimalLength, ref AllocationHandle handle, AllocationOption option = AllocationOption.None)
public UnsafeBitSet(int minimalLength, AllocationHandle handle, AllocationOption option = AllocationOption.None)
{
var uints = (minimalLength >> _INDEX_SIZE) + int.Sign(minimalLength & _BIT_SIZE);
var length = RoundToPadding(uints);
_bits = new UnsafeArray<uint>(length, ref handle, option);
_bits = new UnsafeArray<uint>(length, handle, option);
}
/// <summary>
/// Initializes a new instance of the <see cref="UnsafeBitSet" /> class.
/// </summary>
public UnsafeBitSet(int minimalLength, Allocator allocator, AllocationOption option = AllocationOption.None)
: this(minimalLength, ref AllocationManager.GetAllocationHandle(allocator), option)
: this(minimalLength, AllocationManager.GetAllocationHandle(allocator), option)
{
}
@@ -88,7 +87,7 @@ public unsafe struct UnsafeBitSet : IDisposable, IEquatable<UnsafeBitSet>
public UnsafeBitSet(Span<uint> bits, Allocator allocator)
{
_bits = new UnsafeArray<uint>(bits.Length, allocator, AllocationOption.None);
_bits.CopyFrom(bits);
_bits.CopyFrom<UnsafeArray<uint>, uint>(bits);
_highestBit = 0;
_max = _bits.Count * (_BIT_SIZE + 1) - 1; // Calculate the maximum index in use
@@ -1000,4 +999,4 @@ public readonly ref struct SpanBitSet : IEquatable<SpanBitSet>
return $"{nameof(_bits)}: {string.Join(",", binaryBuilder)}";
}
}
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -82,13 +81,13 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeHashCollection<KeyValu
{
}
public UnsafeHashMap(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeHashMap(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
_helper = new HashMapHelper<TKey>(capacity, sizeof(TValue), (int)AlignOf<TValue>(), HashMapHelper<TKey>.MINIMAL_CAPACITY, ref handle, allocationOption);
_helper = new HashMapHelper<TKey>(capacity, sizeof(TValue), (int)AlignOf<TValue>(), HashMapHelper<TKey>.MINIMAL_CAPACITY, handle, allocationOption);
}
public UnsafeHashMap(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -55,13 +54,13 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeHashCollection<T>, IEnumerable<T>
{
}
public UnsafeHashSet(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeHashSet(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
_helper = new HashMapHelper<T>(capacity, 0, 0, HashMapHelper<T>.MINIMAL_CAPACITY, ref handle, allocationOption);
_helper = new HashMapHelper<T>(capacity, 0, 0, HashMapHelper<T>.MINIMAL_CAPACITY, handle, allocationOption);
}
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}

View File

@@ -1,8 +1,6 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -144,9 +142,9 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// <param name="capacity">Specifies the number of initial capacity to allocate in the list, which must be greater than zero.</param>
/// <param name="handle">A reference to an AllocationHandle that manages the memory allocation for the array.</param>
/// <param name="allocationOption">Specifies how the memory should be allocated.</param>
public UnsafeList(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeList(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
_array = new UnsafeArray<T>(capacity, ref handle, allocationOption);
_array = new UnsafeArray<T>(capacity, handle, allocationOption);
_count = 0;
}
@@ -157,7 +155,7 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
/// <param name="allocator">Specifies the allocator to use for memory allocation, which determines the memory management strategy.</param>
/// <param name="allocationOption">Determines how the memory should be allocated.</param>
public UnsafeList(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -385,4 +383,4 @@ public unsafe struct UnsafeList<T> : IUnsafeCollection<T>
_array.Dispose();
_count = 0;
}
}
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -75,15 +74,15 @@ public unsafe struct UnsafeQueue<T> : IUnsafeCollection<T>
{
}
public UnsafeQueue(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeQueue(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
_array = new UnsafeArray<T>(capacity, ref handle, allocationOption);
_array = new UnsafeArray<T>(capacity, handle, allocationOption);
_count = 0;
_offset = 0;
}
public UnsafeQueue(int capacity, Allocator allocator, AllocationOption allocationType = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationType)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationType)
{
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -79,17 +78,17 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="handle">A reference to the allocation handle used to manage memory for the slot map.</param>
/// <param name="allocationOption">The allocation options to use when creating internal data structures. The default is AllocationOption.None.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when capacity is less than or equal to zero.</exception>
public UnsafeSlotMap(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeSlotMap(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
}
_data = new UnsafeArray<T>(capacity, ref handle, allocationOption);
_generations = new UnsafeArray<int>(capacity, ref handle, allocationOption);
_freeSlots = new UnsafeQueue<int>(capacity, ref handle, allocationOption);
_validBits = new UnsafeBitSet(GetBitSetCapacity(capacity), ref handle, allocationOption);
_data = new UnsafeArray<T>(capacity, handle, allocationOption);
_generations = new UnsafeArray<int>(capacity, handle, allocationOption);
_freeSlots = new UnsafeQueue<int>(capacity, handle, allocationOption);
_validBits = new UnsafeBitSet(GetBitSetCapacity(capacity), handle, allocationOption);
if (!allocationOption.HasFlag(AllocationOption.Clear))
{
@@ -109,7 +108,7 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
/// <param name="allocator">The allocator to use for memory management of the slot map.</param>
/// <param name="allocationOption">The allocation option that determines how memory is allocated. The default is AllocationOption.None.</param>
public UnsafeSlotMap(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -313,4 +312,4 @@ public unsafe struct UnsafeSlotMap<T> : IUnsafeCollection<T>
_count = 0;
_capacity = 0;
}
}
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -81,18 +80,18 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
/// <param name="handle">A reference to an AllocationHandle that manages the memory allocation for the sparse set.</param>
/// <param name="allocationOption">Specifies how the memory should be allocated.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified capacity is less than or equal to zero.</exception>
public UnsafeSparseSet(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeSparseSet(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
if (capacity <= 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than zero.");
}
_dense = new UnsafeArray<T>(capacity, ref handle, allocationOption);
_generations = new UnsafeArray<int>(capacity, ref handle, allocationOption);
_sparse = new UnsafeArray<int>(capacity, ref handle, allocationOption);
_reverse = new UnsafeArray<int>(capacity, ref handle, allocationOption);
_freeSparse = new UnsafeStack<int>(capacity, ref handle, allocationOption);
_dense = new UnsafeArray<T>(capacity, handle, allocationOption);
_generations = new UnsafeArray<int>(capacity, handle, allocationOption);
_sparse = new UnsafeArray<int>(capacity, handle, allocationOption);
_reverse = new UnsafeArray<int>(capacity, handle, allocationOption);
_freeSparse = new UnsafeStack<int>(capacity, handle, allocationOption);
if (!allocationOption.HasFlag(AllocationOption.Clear))
{
@@ -115,7 +114,7 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
/// <param name="allocationOption">Determines how the memory should be allocated.</param>
/// <exception cref="ArgumentOutOfRangeException">Thrown when the specified capacity is less than or equal to zero.</exception>
public UnsafeSparseSet(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -365,4 +364,4 @@ public unsafe struct UnsafeSparseSet<T> : IUnsafeCollection<T>
_count = 0;
_nextId = 0;
}
}
}

View File

@@ -1,6 +1,5 @@
using Misaki.HighPerformance.LowLevel.Buffer;
using Misaki.HighPerformance.LowLevel.Collections.Contracts;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Utilities;
using System.Collections;
using System.Runtime.CompilerServices;
@@ -72,9 +71,9 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
/// <param name="capacity">The number of elements the stack can initially hold. Must be greater than zero.</param>
/// <param name="handle">A reference to an AllocationHandle used to manage the underlying memory allocation for the stack.</param>
/// <param name="allocationOption">Specifies additional options for memory allocation. The default is AllocationOption.None.</param>
public UnsafeStack(int capacity, ref AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
public UnsafeStack(int capacity, AllocationHandle handle, AllocationOption allocationOption = AllocationOption.None)
{
_array = new UnsafeArray<T>(capacity, ref handle, allocationOption);
_array = new UnsafeArray<T>(capacity, handle, allocationOption);
}
/// <summary>
@@ -85,7 +84,7 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
/// <param name="allocator">The allocator to use for memory management of the stack's storage.</param>
/// <param name="allocationOption">The allocation option that determines how memory is allocated for the stack. The default is AllocationOption.None.</param>
public UnsafeStack(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
: this(capacity, ref AllocationManager.GetAllocationHandle(allocator), allocationOption)
: this(capacity, AllocationManager.GetAllocationHandle(allocator), allocationOption)
{
}
@@ -181,4 +180,4 @@ public unsafe struct UnsafeStack<T> : IUnsafeCollection<T>
_array.Dispose();
_count = 0;
}
}
}