Refactor memory management and improve allocation APIs
Updated `ReallocFunc` to support `oldSize`, `newSize`, and `AllocationOption`, enabling more granular control over memory reallocation. Simplified `AllocationInfo` by removing the `FreeHandler` property. Enhanced `Reallocate` and `Allocate` methods in `AllocationManager` to handle memory clearing and tracking more effectively. Refactored `UnsafeSparseSet` to use `UnsafeArray<T>` directly, added a `generations` array, and replaced the `freeList` with `UnsafeStack<int>` for better performance and simplicity. Updated `Resize`, `Add`, and `Remove` methods to improve memory handling and code clarity. Introduced `AllocationOption` support in `Resize` methods across `IUnsafeCollection` implementations for flexible resizing behavior. Added `GetUnsafePtr` extension methods in `UnsafeUtilities` for direct access to span data. Added new tests for `UnsafeSparseSet` to validate resizing, clearing, enumeration, and memory compaction. These changes improve memory management, enhance performance, and ensure correctness.
This commit is contained in:
@@ -156,4 +156,34 @@ public static unsafe class UnsafeUtilities
|
||||
{
|
||||
return new UnsafeArray<TOut>((TOut*)array.GetUnsafePtr(), array.Count * sizeof(TIn) / sizeof(TOut));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a pointer to the first element of the specified span. This method enables direct, unsafe access to the underlying data of the span.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the span. Must be an unmanaged type.</typeparam>
|
||||
/// <param name="span">The span whose underlying data pointer is to be obtained.</param>
|
||||
/// <returns>A pointer to the first element of the span. If the span is empty, the returned pointer is undefined and must not be dereferenced.</returns>
|
||||
public static T* GetUnsafePtr<T>(this Span<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
fixed (T* ptr = span)
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns a pointer to the first element of the specified span. This method enables direct, unsafe access to the underlying data of the span.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The type of elements in the span. Must be an unmanaged type.</typeparam>
|
||||
/// <param name="span">The span whose underlying data pointer is to be obtained.</param>
|
||||
/// <returns>A pointer to the first element of the span. If the span is empty, the returned pointer is undefined and must not be dereferenced.</returns>
|
||||
public static T* GetUnsafePtr<T>(this ReadOnlySpan<T> span)
|
||||
where T : unmanaged
|
||||
{
|
||||
fixed (T* ptr = span)
|
||||
{
|
||||
return ptr;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user