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.
This commit is contained in:
2025-04-03 17:46:55 +09:00
parent 3c555a9489
commit 791be1bed2
6 changed files with 60 additions and 19 deletions

View File

@@ -128,7 +128,7 @@ public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>
return;
}
_buffer = (T*)AlignedRealloc(_buffer, (nuint)(newSize * sizeof(T)), AlignOf<T>());
_buffer = AllocationManager.Realloc<T>(_buffer, (uint)newSize, (uint)AlignOf<T>(), _allocator);
_count = newSize;
}

View File

@@ -92,9 +92,9 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
public IEnumerator<KeyValuePair<TKey, TValue>> GetEnumerator() => new Enumerator((HashMapHelper<TKey>*)UnsafeUtilities.AddressOf(ref _hashMap));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeHashMap(int capacity, Allocator allocator)
public UnsafeHashMap(int capacity, Allocator allocator, AllocationOption allocationOption = AllocationOption.None)
{
_hashMap = new HashMapHelper<TKey>(capacity, sizeof(TValue), HashMapHelper<TKey>.MINIMAL_CAPACITY, allocator);
_hashMap = new HashMapHelper<TKey>(capacity, sizeof(TValue), HashMapHelper<TKey>.MINIMAL_CAPACITY, allocator, allocationOption);
}
/// <summary>
@@ -132,6 +132,16 @@ public unsafe struct UnsafeHashMap<TKey, TValue> : IUnsafeCollection<KeyValuePai
}
}
/// <summary>
/// Removes a particular key and its value.
/// </summary>
/// <param name="item">The value to remove.</param>
/// <returns>True if the value was present.</returns>
public bool Remove(TKey key)
{
return -1 != _hashMap.TryRemove(key);
}
/// <summary>
/// Returns the value associated with a key.
/// </summary>

View File

@@ -49,9 +49,9 @@ public unsafe struct UnsafeHashSet<T> : IUnsafeCollection<T>, IEnumerable<T>
public IEnumerator<T> GetEnumerator() => new Enumerator((HashMapHelper<T>*)UnsafeUtilities.AddressOf(ref _hashMap));
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
public UnsafeHashSet(int capacity, Allocator allocator)
public UnsafeHashSet(int capacity, Allocator allocator, AllocationOption allocationOption)
{
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, allocator);
_hashMap = new HashMapHelper<T>(capacity, 0, HashMapHelper<T>.MINIMAL_CAPACITY, allocator, allocationOption);
}
/// <summary>