Initial upload;
This commit is contained in:
2
Misaki.HighPerformance.Unsafe/AssemblyInfo.cs
Normal file
2
Misaki.HighPerformance.Unsafe/AssemblyInfo.cs
Normal file
@@ -0,0 +1,2 @@
|
||||
global using static Misaki.HighPerformance.Unsafe.Helpers.MemoryUtilities;
|
||||
global using SystemUnsfae = System.Runtime.CompilerServices.Unsafe;
|
||||
75
Misaki.HighPerformance.Unsafe/Buffer/Arena .cs
Normal file
75
Misaki.HighPerformance.Unsafe/Buffer/Arena .cs
Normal file
@@ -0,0 +1,75 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
|
||||
/// <summary>
|
||||
/// A memory management structure that allocates and resets memory blocks with specified alignment.
|
||||
/// </summary>
|
||||
public unsafe struct Arena : IDisposable
|
||||
{
|
||||
private void* _buffer;
|
||||
private ulong _size;
|
||||
private ulong _offset;
|
||||
|
||||
private bool _disposed;
|
||||
|
||||
public Arena(ulong size)
|
||||
{
|
||||
_buffer = Marshal.AllocHGlobal((IntPtr)size).ToPointer();
|
||||
_size = size;
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a block of memory of a specified size with a given alignment. Returns a pointer to the allocated
|
||||
/// memory or null if allocation fails.
|
||||
/// </summary>
|
||||
/// <param name="size">Specifies the amount of memory to allocate in bytes.</param>
|
||||
/// <param name="alignSize">Defines the alignment requirement for the allocated memory.</param>
|
||||
/// <returns>A pointer to the allocated memory block or null if the allocation cannot be fulfilled.</returns>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
||||
public void* Allocate(ulong size, uint alignSize)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
var offset = (_offset + alignSize - 1) & ~(alignSize - 1);
|
||||
if (offset + size > _size)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
_offset = offset + size;
|
||||
var ptr = (byte*)_buffer + offset;
|
||||
MemClear(ptr, (uint)size);
|
||||
|
||||
return ptr;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets the arena, optionally clearing the allocated memory.
|
||||
/// </summary>
|
||||
/// <param name="clear">If true, the allocated memory will be cleared; otherwise, it will not be cleared.</param>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
||||
public void Reset(bool clear = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
if (clear)
|
||||
{
|
||||
MemClear(_buffer, (uint)_size);
|
||||
}
|
||||
|
||||
_offset = 0;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)_buffer);
|
||||
|
||||
_buffer = null;
|
||||
_size = 0;
|
||||
_offset = 0;
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
124
Misaki.HighPerformance.Unsafe/Buffer/DynamicArena.cs
Normal file
124
Misaki.HighPerformance.Unsafe/Buffer/DynamicArena.cs
Normal file
@@ -0,0 +1,124 @@
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Buffer;
|
||||
|
||||
/// <summary>
|
||||
/// A dynamic memory management structure that automatically grows by creating linked arenas
|
||||
/// when more space is needed.
|
||||
/// </summary>
|
||||
public unsafe struct DynamicArena : IDisposable
|
||||
{
|
||||
private struct ArenaNode
|
||||
{
|
||||
public Arena arena;
|
||||
public ArenaNode* next;
|
||||
}
|
||||
|
||||
private ArenaNode* _root;
|
||||
private ArenaNode* _current;
|
||||
private readonly ulong _initialSize;
|
||||
private bool _disposed;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of DynamicArena with the specified initial size.
|
||||
/// </summary>
|
||||
/// <param name="initialSize">The initial size in bytes for the first arena block.</param>
|
||||
public DynamicArena(ulong initialSize)
|
||||
{
|
||||
_initialSize = initialSize;
|
||||
_root = (ArenaNode*)Marshal.AllocHGlobal(sizeof(ArenaNode));
|
||||
_root->arena = new Arena(initialSize);
|
||||
_root->next = null;
|
||||
_current = _root;
|
||||
_disposed = false;
|
||||
}
|
||||
|
||||
private bool CreateNewNode(ulong size)
|
||||
{
|
||||
try
|
||||
{
|
||||
var newNode = (ArenaNode*)Marshal.AllocHGlobal(sizeof(ArenaNode));
|
||||
newNode->arena = new Arena(size);
|
||||
newNode->next = null;
|
||||
|
||||
_current->next = newNode;
|
||||
_current = newNode;
|
||||
return true;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Allocates a block of memory with specified size and alignment. Creates a new arena if current one is full.
|
||||
/// </summary>
|
||||
/// <param name="size">Size of the memory block to allocate in bytes.</param>
|
||||
/// <param name="alignSize">Alignment requirement for the memory block.</param>
|
||||
/// <returns>Pointer to the allocated memory block.</returns>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
||||
public void* Allocate(ulong size, uint alignSize)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
void* result = null;
|
||||
var current = _current;
|
||||
|
||||
while (current != null)
|
||||
{
|
||||
result = current->arena.Allocate(size, alignSize);
|
||||
if (result != null)
|
||||
return result;
|
||||
|
||||
if (current->next == null && !CreateNewNode(Math.Max(size, _initialSize)))
|
||||
return null;
|
||||
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
_current = current;
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Resets all arenas in the chain, optionally clearing their memory.
|
||||
/// </summary>
|
||||
/// <param name="clear">If true, memory will be cleared during reset.</param>
|
||||
/// <exception cref="ObjectDisposedException">Thrown if the arena has been disposed.</exception>
|
||||
public void Reset(bool clear = false)
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
|
||||
var current = _root;
|
||||
while (current != null)
|
||||
{
|
||||
current->arena.Reset(clear);
|
||||
current = current->next;
|
||||
}
|
||||
|
||||
_current = _root;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Disposes all arenas and frees associated memory.
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
return;
|
||||
|
||||
var current = _root;
|
||||
while (current != null)
|
||||
{
|
||||
var next = current->next;
|
||||
current->arena.Dispose();
|
||||
Marshal.FreeHGlobal((IntPtr)current);
|
||||
current = next;
|
||||
}
|
||||
|
||||
_root = null;
|
||||
_current = null;
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
public enum AllocationType
|
||||
{
|
||||
UnInitialized,
|
||||
Clear
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
|
||||
public unsafe interface IUnsafeCollection<T> : IDisposable where T : unmanaged
|
||||
{
|
||||
public T* Buffer
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public int Size
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public ref T this[int index]
|
||||
{
|
||||
get;
|
||||
}
|
||||
|
||||
public void Clear();
|
||||
public void ReAlloc(int newSize);
|
||||
}
|
||||
113
Misaki.HighPerformance.Unsafe/Collections/UnsafeArray.cs
Normal file
113
Misaki.HighPerformance.Unsafe/Collections/UnsafeArray.cs
Normal file
@@ -0,0 +1,113 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
public unsafe struct UnsafeArray<T> : IUnsafeCollection<T>, IEnumerable<T> where T : unmanaged
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
{
|
||||
private UnsafeArray<T> _collection;
|
||||
private int _index;
|
||||
private T _value;
|
||||
|
||||
public Enumerator(ref UnsafeArray<T> collection)
|
||||
{
|
||||
_collection = collection;
|
||||
_index = -1;
|
||||
_value = default;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext()
|
||||
{
|
||||
_index++;
|
||||
if (_index < _collection.Size)
|
||||
{
|
||||
_value = UnsafeUtilities.ReadArrayElement<T>(_collection.Buffer, _index);
|
||||
return true;
|
||||
}
|
||||
|
||||
_value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
// Let NativeArray indexer check for out of range.
|
||||
public T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
}
|
||||
|
||||
object IEnumerator.Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
private T* _buffer;
|
||||
private int _size;
|
||||
|
||||
public readonly T* Buffer => _buffer;
|
||||
public readonly int Size => _size;
|
||||
|
||||
public readonly ref T this[int index] => ref UnsafeUtilities.AsRef<T>(_buffer + index);
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public UnsafeArray(int size, AllocationType allocationType)
|
||||
{
|
||||
_size = size;
|
||||
_buffer = (T*)Marshal.AllocHGlobal(size * sizeof(T)).ToPointer();
|
||||
|
||||
if (allocationType == AllocationType.Clear)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public void ReAlloc(int newSize)
|
||||
{
|
||||
if (newSize == _size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer = (T*)Marshal.ReAllocHGlobal((IntPtr)_buffer, newSize * sizeof(T)).ToPointer();
|
||||
_size = newSize;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Clear()
|
||||
{
|
||||
MemClear(_buffer, (uint)(_size * sizeof(T)));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)_buffer);
|
||||
|
||||
_buffer = null;
|
||||
_size = 0;
|
||||
}
|
||||
}
|
||||
290
Misaki.HighPerformance.Unsafe/Collections/UnsafeList.cs
Normal file
290
Misaki.HighPerformance.Unsafe/Collections/UnsafeList.cs
Normal file
@@ -0,0 +1,290 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
using Misaki.HighPerformance.Unsafe.Helpers;
|
||||
using System.Collections;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Collections;
|
||||
|
||||
public unsafe struct UnsafeList<T> : IUnsafeCollection<T>, IEnumerable<T> where T : unmanaged
|
||||
{
|
||||
public struct Enumerator : IEnumerator<T>
|
||||
{
|
||||
private UnsafeList<T> _collection;
|
||||
private int _index;
|
||||
private T _value;
|
||||
|
||||
public Enumerator(ref UnsafeList<T> collection)
|
||||
{
|
||||
_collection = collection;
|
||||
_index = -1;
|
||||
_value = default;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public bool MoveNext()
|
||||
{
|
||||
_index++;
|
||||
if (_index < _collection.Size)
|
||||
{
|
||||
_value = UnsafeUtilities.ReadArrayElement<T>(_collection.Buffer, _index);
|
||||
return true;
|
||||
}
|
||||
|
||||
_value = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
// Let NativeArray indexer check for out of range.
|
||||
public readonly T Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return _value;
|
||||
}
|
||||
}
|
||||
|
||||
readonly object IEnumerator.Current
|
||||
{
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
get
|
||||
{
|
||||
return Current;
|
||||
}
|
||||
}
|
||||
|
||||
public readonly void Dispose()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// A parallel writer for an UnsafeList.
|
||||
/// </summary>
|
||||
/// <remarks>
|
||||
/// Use <see cref="AsParallelWriter"/> to create a parallel writer for a list.
|
||||
/// </remarks>
|
||||
public unsafe struct ParallelWriter
|
||||
{
|
||||
/// <summary>
|
||||
/// The UnsafeList to write to.
|
||||
/// </summary>
|
||||
public UnsafeList<T>* listData;
|
||||
|
||||
internal unsafe ParallelWriter(UnsafeList<T>* list)
|
||||
{
|
||||
listData = list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a value to a collection without resizing it, ensuring capacity is checked before insertion.
|
||||
/// </summary>
|
||||
/// <param name="value">The value to be added to the collection.</param>
|
||||
public void AddNoResize(T value)
|
||||
{
|
||||
var idx = Interlocked.Increment(ref listData->_size) - 1;
|
||||
listData->CheckNoResizeCapacity(idx, 1);
|
||||
UnsafeUtilities.WriteArrayElement(listData->_buffer, idx, value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Adds a specified number of elements from a pointer to a buffer without resizing the underlying storage.
|
||||
/// </summary>
|
||||
/// <param name="ptr">Points to the source data to be copied into the buffer.</param>
|
||||
/// <param name="count">Indicates the number of elements to be added from the source data.</param>
|
||||
public void AddRangeNoResize(T* ptr, int count)
|
||||
{
|
||||
var idx = Interlocked.Add(ref listData->_size, count) - count;
|
||||
listData->CheckNoResizeCapacity(idx, count);
|
||||
MemCpy(listData->_buffer + idx, ptr, (uint)(count * sizeof(T)));
|
||||
}
|
||||
}
|
||||
|
||||
private T* _buffer;
|
||||
|
||||
private int _size;
|
||||
private int _capacity;
|
||||
|
||||
public readonly T* Buffer => _buffer;
|
||||
public readonly int Size => _size;
|
||||
public readonly int Capacity => _capacity;
|
||||
|
||||
public readonly ref T this[int index] => ref UnsafeUtilities.ReadArrayElementRef<T>(_buffer, index);
|
||||
|
||||
public IEnumerator<T> GetEnumerator() => new Enumerator(ref this);
|
||||
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
|
||||
|
||||
public ParallelWriter AsParallelWriter() => new((UnsafeList<T>*)UnsafeUtilities.AddressOf(ref this));
|
||||
|
||||
public UnsafeList(int capacity, AllocationType allocationType)
|
||||
{
|
||||
_buffer = (T*)Marshal.AllocHGlobal(capacity * sizeof(T));
|
||||
_size = 0;
|
||||
_capacity = capacity;
|
||||
|
||||
if (allocationType == AllocationType.Clear)
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
}
|
||||
|
||||
private readonly void CheckNoResizeCapacity(int count)
|
||||
{
|
||||
CheckNoResizeCapacity(count, count);
|
||||
}
|
||||
|
||||
private readonly void CheckNoResizeCapacity(int index, int count)
|
||||
{
|
||||
if (index + count > _capacity)
|
||||
{
|
||||
throw new Exception($"AddNoResize assumes that list capacity is sufficient (Capacity {Capacity}, Size {Size}), requested count {count}!");
|
||||
}
|
||||
}
|
||||
|
||||
private readonly void CheckIndexCount(int index, int count)
|
||||
{
|
||||
if (count < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException($"Value for count {count} must be positive.");
|
||||
}
|
||||
|
||||
if (index < 0)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException($"Value for index {index} must be positive.");
|
||||
}
|
||||
|
||||
if (index > Size)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException($"Value for index {index} is out of bounds.");
|
||||
}
|
||||
|
||||
if (index + count > Size)
|
||||
{
|
||||
throw new ArgumentOutOfRangeException($"Value for count {count} is out of bounds.");
|
||||
}
|
||||
}
|
||||
|
||||
public void Add(T value)
|
||||
{
|
||||
if (_size >= _capacity)
|
||||
{
|
||||
ReAlloc(_capacity + (int)(_capacity * 0.5f));
|
||||
}
|
||||
|
||||
UnsafeUtilities.WriteArrayElement(_buffer, _size, value);
|
||||
_size++;
|
||||
}
|
||||
|
||||
public void AddNoResize(T value)
|
||||
{
|
||||
CheckNoResizeCapacity(1);
|
||||
|
||||
UnsafeUtilities.WriteArrayElement(_buffer, _size, value);
|
||||
_size++;
|
||||
}
|
||||
|
||||
public void AddRange(Span<T> values, int count)
|
||||
{
|
||||
var newSize = _size + count;
|
||||
if (newSize > _capacity)
|
||||
{
|
||||
ReAlloc(_capacity + count);
|
||||
}
|
||||
|
||||
fixed (T* ptr = values)
|
||||
{
|
||||
MemCpy(_buffer + _size, ptr, (uint)(count * sizeof(T)));
|
||||
}
|
||||
|
||||
_size += count;
|
||||
}
|
||||
|
||||
public void AddRangeNoResize(ReadOnlySpan<T> values)
|
||||
{
|
||||
CheckNoResizeCapacity(values.Length);
|
||||
|
||||
fixed (T* ptr = values)
|
||||
{
|
||||
MemCpy(_buffer + _size, ptr, (uint)(values.Length * sizeof(T)));
|
||||
}
|
||||
|
||||
_size += values.Length;
|
||||
}
|
||||
|
||||
public void AddRangeNoResize(T* ptr, int count)
|
||||
{
|
||||
CheckNoResizeCapacity(count);
|
||||
|
||||
MemCpy(_buffer + _size, ptr, (uint)(count * sizeof(T)));
|
||||
_size += count;
|
||||
}
|
||||
|
||||
public void RemoveRange(int start, int length)
|
||||
{
|
||||
CheckIndexCount(start, length);
|
||||
|
||||
if (length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var copyFrom = Math.Min(start + length, _size);
|
||||
MemCpy(_buffer + start, _buffer + copyFrom, (uint)((_size - copyFrom) * sizeof(T)));
|
||||
_size -= length;
|
||||
}
|
||||
|
||||
public void RemoveAt(int index)
|
||||
{
|
||||
RemoveRange(index, 1);
|
||||
}
|
||||
|
||||
public void RemoveRangeSwapBack(int start, int length)
|
||||
{
|
||||
CheckIndexCount(start, length);
|
||||
|
||||
if (length <= 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var copyFrom = Math.Min(_size - length, start + length);
|
||||
MemCpy(_buffer + start, _buffer + copyFrom, (uint)((_size - copyFrom) * sizeof(T)));
|
||||
_size -= length;
|
||||
}
|
||||
|
||||
public void RemoveAtSwapBack(int index)
|
||||
{
|
||||
RemoveRangeSwapBack(index, 1);
|
||||
}
|
||||
|
||||
public void ReAlloc(int newSize)
|
||||
{
|
||||
if (newSize == _size)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_buffer = (T*)Marshal.ReAllocHGlobal((IntPtr)_buffer, newSize * sizeof(T)).ToPointer();
|
||||
_size = newSize;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public readonly void Clear()
|
||||
{
|
||||
MemClear(_buffer, (uint)(_size * sizeof(T)));
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Marshal.FreeHGlobal((IntPtr)_buffer);
|
||||
|
||||
_buffer = null;
|
||||
_size = 0;
|
||||
}
|
||||
}
|
||||
41
Misaki.HighPerformance.Unsafe/Helpers/MemoryUtilities.cs
Normal file
41
Misaki.HighPerformance.Unsafe/Helpers/MemoryUtilities.cs
Normal file
@@ -0,0 +1,41 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Helpers;
|
||||
public unsafe static class MemoryUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Clears a block of memory by setting it to zero. It initializes a specified number of bytes at a given memory
|
||||
/// address.
|
||||
/// </summary>
|
||||
/// <param name="ptr">Specifies the memory address where the clearing operation will begin.</param>
|
||||
/// <param name="size">Indicates the number of bytes to be cleared in the memory block.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void MemClear(void* ptr, uint size)
|
||||
{
|
||||
SystemUnsfae.InitBlock(ptr, 0, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Sets a block of memory to a specified byte value for a given size.
|
||||
/// </summary>
|
||||
/// <param name="ptr">The memory address where the byte value will be set.</param>
|
||||
/// <param name="value">The byte value to which the memory block will be initialized.</param>
|
||||
/// <param name="size">The number of bytes to set to the specified value.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void MemSet(void* ptr, byte value, uint size)
|
||||
{
|
||||
SystemUnsfae.InitBlock(ptr, value, size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies a block of memory from a source location to a destination location.
|
||||
/// </summary>
|
||||
/// <param name="destination">Specifies the memory address where the copied data will be stored.</param>
|
||||
/// <param name="source">Indicates the memory address from which data will be copied.</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* destination, void* source, uint size)
|
||||
{
|
||||
SystemUnsfae.CopyBlock(destination, source, size);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
using Misaki.HighPerformance.Unsafe.Collections.Contracts;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Helpers;
|
||||
|
||||
public unsafe static class UnsafeCollectionExtensions
|
||||
{
|
||||
/// <summary>
|
||||
/// Copies elements from a source UnsafeCollection to a destination Span, ensuring both have the same size.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of elements being copied, which must be unmanaged.</typeparam>
|
||||
/// <param name="source">Represents the source collection from which elements are copied.</param>
|
||||
/// <param name="destination">Represents the target span where elements are copied to.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when the sizes of the source collection and destination span do not match.</exception>
|
||||
public static void CopyTo<T>(this IUnsafeCollection<T> source, Span<T> destination) where T : unmanaged
|
||||
{
|
||||
if (source.Size > destination.Length)
|
||||
{
|
||||
throw new ArgumentException("Source collection is larger than the destination span.");
|
||||
}
|
||||
|
||||
fixed (T* ptr = destination)
|
||||
{
|
||||
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Copies elements from a source span to a destination unsafe collection, ensuring both have the same size.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of elements being copied, which must be unmanaged.</typeparam>
|
||||
/// <param name="destination">Represents the unsafe collection that will receive the copied elements.</param>
|
||||
/// <param name="source">Represents the span containing the elements to be copied to the unsafe collection.</param>
|
||||
/// <exception cref="ArgumentException">Thrown when the source span and destination collection have different sizes.</exception>
|
||||
public static void CopyFrom<T>(this IUnsafeCollection<T> destination, Span<T> source) where T : unmanaged
|
||||
{
|
||||
if (destination.Size > source.Length)
|
||||
{
|
||||
throw new ArgumentException("Destination collection is larger than the source span.");
|
||||
}
|
||||
|
||||
fixed (T* ptr = source)
|
||||
{
|
||||
SystemUnsfae.CopyBlock(destination.Buffer, ptr, (uint)(source.Length * sizeof(T)));
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an UnsafeCollection of unmanaged types into a standard collection.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Represents a type that is unmanaged, allowing for direct memory manipulation.</typeparam>
|
||||
/// <param name="source">The UnsafeCollection instance that contains the data to be converted.</param>
|
||||
/// <returns>A new collection containing the elements from the UnsafeCollection.</returns>
|
||||
public static T[] ToArray<T>(this IUnsafeCollection<T> source) where T : unmanaged
|
||||
{
|
||||
var array = new T[source.Size];
|
||||
fixed (T* ptr = array)
|
||||
{
|
||||
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
|
||||
}
|
||||
|
||||
return array;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an unmanaged collection into a list by copying its elements into a new list.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Represents a type that is unmanaged, allowing for direct memory manipulation.</typeparam>
|
||||
/// <param name="source">The collection from which elements are copied to create the new list.</param>
|
||||
/// <returns>A list containing the elements from the specified unmanaged collection.</returns>
|
||||
public static List<T> ToList<T>(this IUnsafeCollection<T> source) where T : unmanaged
|
||||
{
|
||||
var list = new List<T>(source.Size);
|
||||
fixed (T* ptr = list.ToArray())
|
||||
{
|
||||
SystemUnsfae.CopyBlock(ptr, source.Buffer, (uint)(source.Size * sizeof(T)));
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Converts an UnsafeCollection into a Span for efficient memory access.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Represents a type that can be stored in unmanaged memory.</typeparam>
|
||||
/// <param name="source">The UnsafeCollection instance to be converted into a Span.</param>
|
||||
/// <returns>A Span that provides a view over the elements of the UnsafeCollection.</returns>
|
||||
public static Span<T> AsSpan<T>(this IUnsafeCollection<T> source) where T : unmanaged
|
||||
{
|
||||
return new(source.Buffer, source.Size);
|
||||
}
|
||||
}
|
||||
82
Misaki.HighPerformance.Unsafe/Helpers/UnsafeUtilities.cs
Normal file
82
Misaki.HighPerformance.Unsafe/Helpers/UnsafeUtilities.cs
Normal file
@@ -0,0 +1,82 @@
|
||||
using System.Runtime.CompilerServices;
|
||||
|
||||
namespace Misaki.HighPerformance.Unsafe.Helpers;
|
||||
|
||||
public static unsafe class UnsafeUtilities
|
||||
{
|
||||
/// <summary>
|
||||
/// Converts a pointer to a reference of a specified type.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of the reference to be created from the pointer.</typeparam>
|
||||
/// <param name="ptr">Represents the memory address to be converted into a reference.</param>
|
||||
/// <returns>Returns a reference of the specified type pointing to the given memory address.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ref T AsRef<T>(void* ptr)
|
||||
{
|
||||
return ref SystemUnsfae.AsRef<T>(ptr);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Returns the address of a specified variable in memory.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Represents the type of the variable whose address is being retrieved.</typeparam>
|
||||
/// <param name="value">The variable whose memory address is to be obtained.</param>
|
||||
/// <returns>A pointer to the memory address of the specified variable.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void* AddressOf<T>(ref T value)
|
||||
{
|
||||
return SystemUnsfae.AsPointer(ref value);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an element from an unmanaged array at a specified index using a pointer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of elements in the unmanaged array.</typeparam>
|
||||
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
|
||||
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
|
||||
/// <returns>Returns a pointer to the element located at the specified index.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T* ReadArrayElementUnsafe<T>(void* ptr, int index) where T : unmanaged
|
||||
{
|
||||
return (T*)((byte*)ptr + (index * sizeof(T)));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an element from an unmanaged array using a pointer and index, returning a reference to the element.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of the elements in the unmanaged array.</typeparam>
|
||||
/// <param name="ptr">Points to the start of the unmanaged array from which the element is read.</param>
|
||||
/// <param name="index">Indicates the position of the element to be accessed in the array.</param>
|
||||
/// <returns>A reference to the specified element in the unmanaged array.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ref T ReadArrayElementRef<T>(void* ptr, int index) where T : unmanaged
|
||||
{
|
||||
return ref AsRef<T>(ReadArrayElementUnsafe<T>(ptr, index));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads an element from an array at a specified index using a pointer to the array.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of the elements in the array, which must be unmanaged.</typeparam>
|
||||
/// <param name="ptr">Points to the start of the array from which an element will be read.</param>
|
||||
/// <param name="index">Indicates the position of the element to be accessed within the array.</param>
|
||||
/// <returns>The element located at the specified index in the array.</returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static T ReadArrayElement<T>(void* ptr, int index) where T : unmanaged
|
||||
{
|
||||
return *ReadArrayElementUnsafe<T>(ptr, index);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Writes a value to a specified index of an unmanaged array using a pointer.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">Specifies the type of the value being written to the array, which must be an unmanaged type.</typeparam>
|
||||
/// <param name="ptr">Points to the beginning of the unmanaged array where the value will be written.</param>
|
||||
/// <param name="index">Indicates the position in the array where the value should be stored.</param>
|
||||
/// <param name="value">Represents the value to be written to the specified index of the array.</param>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static void WriteArrayElement<T>(void* ptr, int index, T value) where T : unmanaged
|
||||
{
|
||||
*ReadArrayElementUnsafe<T>(ptr, index) = value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net9.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
|
||||
<IsAotCompatible>True</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
|
||||
<IsAotCompatible>True</IsAotCompatible>
|
||||
</PropertyGroup>
|
||||
|
||||
</Project>
|
||||
Reference in New Issue
Block a user