All checks were successful
Publish NuGet Packages / publish (push) Successful in 1m47s
77 lines
1.8 KiB
C#
77 lines
1.8 KiB
C#
using System.Runtime.InteropServices;
|
|
|
|
namespace Misaki.HighPerformance.LowLevel.Buffer;
|
|
|
|
/// <summary>
|
|
/// Represents an allocated memory block with metadata.
|
|
/// </summary>
|
|
[StructLayout(LayoutKind.Sequential)]
|
|
public readonly unsafe struct MemoryBlock
|
|
{
|
|
/// <summary>
|
|
/// Pointer to the actual allocated memory.
|
|
/// </summary>
|
|
public void* Ptr
|
|
{
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// The heap from which the memory was allocated.
|
|
/// </summary>
|
|
public void* Heap
|
|
{
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Size of the allocated memory in bytes.
|
|
/// </summary>
|
|
public nuint Size
|
|
{
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Alignment of the allocated memory.
|
|
/// </summary>
|
|
public nuint Alignment
|
|
{
|
|
get;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Indicates whether this memory block is valid.
|
|
/// </summary>
|
|
public readonly bool IsValid => Ptr != null && Size > 0;
|
|
|
|
/// <summary>
|
|
/// Creates a new MemoryBlock with the specified parameters.
|
|
/// </summary>
|
|
/// <param name="ptr">Pointer to the allocated memory.</param>
|
|
/// <param name="size">Size of the allocated memory.</param>
|
|
/// <param name="alignment">Alignment of the allocated memory.</param>
|
|
public MemoryBlock(void* ptr, void* heap, nuint size, nuint alignment)
|
|
{
|
|
Ptr = ptr;
|
|
Heap = heap;
|
|
Size = size;
|
|
Alignment = alignment;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an invalid MemoryBlock.
|
|
/// </summary>
|
|
public static MemoryBlock Invalid => new(null, null, 0, 0);
|
|
|
|
public Span<T> AsSpan<T>()
|
|
where T : unmanaged
|
|
{
|
|
if (!IsValid)
|
|
{
|
|
throw new InvalidOperationException("Cannot create span from invalid MemoryBlock.");
|
|
}
|
|
|
|
return new Span<T>(Ptr, (int)(Size / SizeOf<T>()));
|
|
}
|
|
} |