Add image processing and memory management features

Added new namespace `Misaki.HighPerformance.Image` for image processing, including classes for animated GIF handling and memory management.
Added `AnimatedFrameResult` class for individual frames in animated images.
Added `AnimatedGifEnumerator` class for enumerating frames in animated GIFs.
Added `ColorComponents` enum for different color formats.
Added `ImageInfo` struct for image dimensions and color components.
Added `CRuntime` class for low-level memory management functions.
Added `MemoryStats` class to track memory allocation statistics.
Added utility functions for creating multi-dimensional arrays.
Added new structures for fixed-size UTF-8 encoded strings.
Added benchmarking classes to test new memory management features.

Changed `StbImage.cs` to include new namespaces and functionality for image data manipulation.
Changed project files to target .NET 9.0 and enable new features.
Changed `Arena.cs` and `DynamicArena.cs` to use `nuint` for size parameters.
Changed `BitSet.cs` to enhance bit manipulation methods.
Changed `Program.cs` to run `FunctionPtrBenchmark` for performance testing.

Removed memory tracking code from `AllocationManager.cs`, including the `_allocated` dictionary and related logic.
Removed `Free` method from `IAllocator.cs` interface.
Removed `UNSAFE_COLLECTION_CHECK` preprocessor directive from the codebase.

Refactored various files to improve organization, moving from `Unsafe` to `LowLevel` namespace.
Refactored `MemoryUtilities` class to include new memory operation methods.
Refactored `UnsafeUtilities.cs` to support new collection structures.
This commit is contained in:
2025-07-12 19:48:42 +09:00
parent d306f183de
commit eeff3313b5
72 changed files with 14444 additions and 471 deletions

View File

@@ -0,0 +1,263 @@
using Misaki.HighPerformance.LowLevel.Collections;
using Misaki.HighPerformance.LowLevel.Contracts;
using Misaki.HighPerformance.LowLevel.Exceptions;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
namespace Misaki.HighPerformance.LowLevel.Buffer;
using unsafe FreeFunc = delegate* unmanaged<void*, void*, void>;
public unsafe struct ArenaAllocator : IAllocator, IDisposable
{
private DynamicArena _arena;
private AllocationHandle _handle;
public readonly ref AllocationHandle Handle => ref Unsafe.AsRef(in _handle);
public ArenaAllocator(uint initialSize)
{
_arena = new DynamicArena(initialSize);
_handle = new AllocationHandle(Unsafe.AsPointer(ref this), &Allocate, &Reallocate, &FreeBlock);
}
[UnmanagedCallersOnly]
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption)
{
var selfPtr = (ArenaAllocator*)instance;
var ptr = selfPtr->_arena.Allocate(size, alignment, allocationOption);
return ptr;
}
[UnmanagedCallersOnly]
private static void* Reallocate(void* instance, void* ptr, nuint size, nuint alignment)
{
var selfPtr = (ArenaAllocator*)instance;
var newPtr = selfPtr->_arena.Allocate(size, alignment, AllocationOption.None);
MemCpy(newPtr, ptr, size);
// NOTE: We do not free the old pointer here, as it is managed by the arena.
return newPtr;
}
[UnmanagedCallersOnly]
private static void FreeBlock(void* instance, void* ptr)
{
}
public void Reset()
{
_arena.Reset();
}
public void Dispose()
{
_arena.Dispose();
}
}
public unsafe struct DefaultAllocator : IAllocator
{
private AllocationHandle _handle;
public ref AllocationHandle Handle => ref Unsafe.AsRef(in _handle);
public DefaultAllocator()
{
_handle = new AllocationHandle(Unsafe.AsPointer(ref this), &Allocate, &Reallocate, &FreeBlock);
}
[UnmanagedCallersOnly]
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption)
{
var ptr = AlignedAlloc(size, alignment);
AllocationManager.TrackAllocation(ptr, size, instance, &FreeBlock);
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(ptr, size);
}
return ptr;
}
[UnmanagedCallersOnly]
private static void* Reallocate(void* instance, void* ptr, nuint size, nuint alignment)
{
var newPtr = AlignedRealloc(ptr, size, alignment);
AllocationManager.UpdateAllocation(ptr, newPtr, size, instance, &FreeBlock);
return newPtr;
}
[UnmanagedCallersOnly]
private static void FreeBlock(void* instance, void* ptr)
{
AlignedFree(ptr);
AllocationManager.RemoveAllocation(ptr);
}
}
public unsafe struct EmptyAllocator : IAllocator
{
private AllocationHandle _handle;
public ref AllocationHandle Handle => ref Unsafe.AsRef(in _handle);
public EmptyAllocator()
{
_handle = new AllocationHandle(Unsafe.AsPointer(ref this), &Allocate, &Reallocate, &FreeBlock);
}
[UnmanagedCallersOnly]
private static void* Allocate(void* instance, nuint size, nuint alignment, AllocationOption allocationOption)
{
return null;
}
[UnmanagedCallersOnly]
private static void* Reallocate(void* instance, void* ptr, nuint size, nuint alignment)
{
return ptr;
}
[UnmanagedCallersOnly]
private static void FreeBlock(void* instance, void* ptr)
{
}
}
public static unsafe class AllocationManager
{
public readonly struct AllocationInfo
{
public nuint Size
{
get; init;
}
public void* Allocator
{
get; init;
}
public FreeFunc FreeHandler
{
get; init;
}
public StackTrace StackTrace
{
get; init;
}
}
private const uint _DEFAULT_ARENA_SIZE = 512 * 1024;
private static ArenaAllocator s_arenaAllocator = new(_DEFAULT_ARENA_SIZE);
private static DefaultAllocator s_persistentAllocator = new();
private static EmptyAllocator s_emptyAllocator = new();
private static bool s_debugLayer;
private static Dictionary<nint, AllocationInfo>? s_allocated;
public static ArenaAllocator TempAllocator => s_arenaAllocator;
public static DefaultAllocator PersistentAllocator => s_persistentAllocator;
public static EmptyAllocator EmptyAllocator => s_emptyAllocator;
public static void EnableDebugLayer()
{
s_debugLayer = true;
s_allocated ??= new Dictionary<nint, AllocationInfo>(64);
}
public static ref AllocationHandle GetAllocationHandle(Allocator allocator)
{
switch (allocator)
{
case Allocator.Temp:
return ref s_arenaAllocator.Handle;
case Allocator.Persistent:
return ref s_persistentAllocator.Handle;
case Allocator.External:
return ref s_emptyAllocator.Handle;
default:
throw new ArgumentException("Invalid allocator type.", nameof(allocator));
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void TrackAllocation(void* ptr, nuint allocationSize, void* allocator, FreeFunc freeFunc)
{
if (!s_debugLayer || s_allocated == null || ptr == null)
{
return;
}
s_allocated[(nint)ptr] = new AllocationInfo
{
Size = allocationSize,
Allocator = allocator,
FreeHandler = freeFunc,
StackTrace = new StackTrace(true)
};
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void UpdateAllocation(void* oldPtr, void* newPtr, nuint allocationSize, void* allocator, FreeFunc freeFunc)
{
if (!s_debugLayer || s_allocated == null || oldPtr == null || newPtr == null)
{
return;
}
if (s_allocated.Remove((nint)oldPtr, out var info))
{
s_allocated[(nint)newPtr] = new AllocationInfo
{
Size = allocationSize,
Allocator = allocator,
FreeHandler = freeFunc,
StackTrace = info.StackTrace
};
}
else
{
TrackAllocation(newPtr, allocationSize, allocator, freeFunc);
}
}
public static void RemoveAllocation(void* ptr)
{
if (s_allocated == null)
{
return;
}
s_allocated.Remove((nint)ptr);
}
/// <summary>
/// Disposes of the AllocationManager, freeing all allocated memory and resources.
/// </summary>
public static void Dispose()
{
s_arenaAllocator.Dispose();
if (s_allocated != null)
{
nuint unfreeBytes = 0u;
foreach (var pair in s_allocated)
{
unfreeBytes += pair.Value.Size;
pair.Value.FreeHandler(pair.Value.Allocator, (void*)pair.Key);
}
if (unfreeBytes > 0u)
{
throw new MemoryLeakException([.. s_allocated.Values]);
}
s_allocated.Clear();
}
}
}

View File

@@ -0,0 +1,92 @@
using Misaki.HighPerformance.LowLevel.Collections;
namespace Misaki.HighPerformance.LowLevel.Buffer;
/// <summary>
/// A memory management structure that allocates and resets memory blocks with specified alignment.
/// </summary>
public unsafe struct Arena : IDisposable
{
private byte* _buffer;
private nuint _size;
private nuint _offset;
private bool _disposed;
public Arena(nuint size)
{
Initialize(size);
}
public void Initialize(nuint size)
{
if (_buffer != null)
{
return;
}
_buffer = (byte*)Malloc(size);
_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.
/// You don't need to free the memory manually, it will be freed when the arena is disposed.
/// </summary>
/// <param name="size">Specifies the amount of memory to allocate in bytes.</param>
/// <param name="alignment">Defines the alignment requirement for the allocated memory.</param>
/// <param name="allocationOption">The option when allocating 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(nuint size, nuint alignment, AllocationOption allocationOption)
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(DynamicArena));
}
var offset = _offset + alignment - 1 & ~(alignment - 1);
if (offset + size > _size)
{
return null;
}
_offset = offset + size;
var ptr = _buffer + offset;
if (allocationOption.HasFlag(AllocationOption.Clear))
{
MemClear(ptr, 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()
{
if (_disposed)
{
throw new ObjectDisposedException(nameof(DynamicArena));
}
_offset = 0;
}
public void Dispose()
{
Free(_buffer);
_buffer = null;
_size = 0;
_offset = 0;
_disposed = true;
}
}

View File

@@ -0,0 +1,143 @@
using Misaki.HighPerformance.LowLevel.Collections;
namespace Misaki.HighPerformance.LowLevel.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 uint _initialSize;
/// <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(uint initialSize)
{
_initialSize = initialSize;
_root = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
_root->arena = new Arena(initialSize);
_root->next = null;
_current = _root;
}
public void Initialize(uint initialSize)
{
if (_root != null)
{
return;
}
_initialSize = initialSize;
_root = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
_root->arena = new Arena(initialSize);
_root->next = null;
_current = _root;
}
private bool CreateNewNode(nuint size)
{
var newNode = (ArenaNode*)Malloc(SizeOf<ArenaNode>());
try
{
newNode->arena = new Arena(size);
newNode->next = null;
_current->next = newNode;
_current = newNode;
return true;
}
catch
{
Free(newNode);
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="alignment">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(nuint size, nuint alignment, AllocationOption allocationOption)
{
if (_root == null)
{
return null;
}
void* result = null;
var current = _current;
while (current != null)
{
result = current->arena.Allocate(size, alignment, allocationOption);
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()
{
var current = _root;
while (current != null)
{
current->arena.Reset();
current = current->next;
}
_current = _root;
}
/// <summary>
/// Disposes all arenas and frees associated memory.
/// </summary>
public void Dispose()
{
if (_root == null)
{
return;
}
var current = _root;
while (current != null)
{
var next = current->next;
current->arena.Dispose();
Free(current);
current = next;
}
_root = null;
_current = null;
}
}

View File

@@ -0,0 +1,926 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Misaki.HighPerformance.LowLevel.Buffer;
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 32 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 32 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString32"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 32)]
public unsafe struct FixedStackString32
{
private ushort _length;
private fixed byte _buffer[30];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 30)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString32.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 30));
}
}
}
public FixedStackString32(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 30)
{
throw new ArgumentException("Input string is too long to fit in FixedString32.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 30);
_length = (ushort)actualByteCount;
}
}
public FixedStackString32(string input)
: this(input.AsSpan())
{
}
public FixedStackString32(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString32(ReadOnlySpan<byte> input)
{
if (input.Length > 30)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString32.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString32(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 64 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 64 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString64"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 64)]
public unsafe struct FixedStackString64
{
private ushort _length;
private fixed byte _buffer[62];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 62)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString64.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 62));
}
}
}
public FixedStackString64(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 62)
{
throw new ArgumentException("Input string is too long to fit in FixedString64.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 62);
_length = (ushort)actualByteCount;
}
}
public FixedStackString64(string input)
: this(input.AsSpan())
{
}
public FixedStackString64(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString64(ReadOnlySpan<byte> input)
{
if (input.Length > 62)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString64.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString64(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 128 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 128 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString128"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 128)]
public unsafe struct FixedStackString128
{
private ushort _length;
private fixed byte _buffer[126];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 126)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString128.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 126));
}
}
}
public FixedStackString128(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 126)
{
throw new ArgumentException("Input string is too long to fit in FixedString128.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 126);
_length = (ushort)actualByteCount;
}
}
public FixedStackString128(string input)
: this(input.AsSpan())
{
}
public FixedStackString128(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString128(ReadOnlySpan<byte> input)
{
if (input.Length > 126)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString128.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString128(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 256 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 256 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString256"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 256)]
public unsafe struct FixedStackString256
{
private ushort _length;
private fixed byte _buffer[254];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 254)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString256.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 254));
}
}
}
public FixedStackString256(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 254)
{
throw new ArgumentException("Input string is too long to fit in FixedString256.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 254);
_length = (ushort)actualByteCount;
}
}
public FixedStackString256(string input)
: this(input.AsSpan())
{
}
public FixedStackString256(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString256(ReadOnlySpan<byte> input)
{
if (input.Length > 254)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString256.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString256(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 512 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 512 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString512"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 512)]
public unsafe struct FixedStackString512
{
private ushort _length;
private fixed byte _buffer[510];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 510)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString512.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 510));
}
}
}
public FixedStackString512(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 510)
{
throw new ArgumentException("Input string is too long to fit in FixedString512.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 510);
_length = (ushort)actualByteCount;
}
}
public FixedStackString512(string input)
: this(input.AsSpan())
{
}
public FixedStackString512(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString512(ReadOnlySpan<byte> input)
{
if (input.Length > 510)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString512.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString512(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 1024 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 1024 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString1024"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 1024)]
public unsafe struct FixedStackString1024
{
private ushort _length;
private fixed byte _buffer[1022];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 1022)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString1024.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 1022));
}
}
}
public FixedStackString1024(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 1022)
{
throw new ArgumentException("Input string is too long to fit in FixedString1024.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 1022);
_length = (ushort)actualByteCount;
}
}
public FixedStackString1024(string input)
: this(input.AsSpan())
{
}
public FixedStackString1024(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString1024(ReadOnlySpan<byte> input)
{
if (input.Length > 1022)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString1024.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString1024(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 2048 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 2048 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString2048"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 2048)]
public unsafe struct FixedStackString2048
{
private ushort _length;
private fixed byte _buffer[2046];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 2046)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString2048.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 2046));
}
}
}
public FixedStackString2048(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 2046)
{
throw new ArgumentException("Input string is too long to fit in FixedString2048.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 2046);
_length = (ushort)actualByteCount;
}
}
public FixedStackString2048(string input)
: this(input.AsSpan())
{
}
public FixedStackString2048(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString2048(ReadOnlySpan<byte> input)
{
if (input.Length > 2046)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString2048.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString2048(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length 4096 bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length 4096 bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString4096"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = 4096)]
public unsafe struct FixedStackString4096
{
private ushort _length;
private fixed byte _buffer[4094];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 4094)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString4096.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, 4094));
}
}
}
public FixedStackString4096(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 4094)
{
throw new ArgumentException("Input string is too long to fit in FixedString4096.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, 4094);
_length = (ushort)actualByteCount;
}
}
public FixedStackString4096(string input)
: this(input.AsSpan())
{
}
public FixedStackString4096(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString4096(ReadOnlySpan<byte> input)
{
if (input.Length > 4094)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString4096.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString4096(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}

View File

@@ -0,0 +1,129 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Misaki.HighPerformance.LowLevel.Buffer;
<# for (int i = 32; i <= 4096; i *= 2) { #>
/// <summary>
/// Represents a stack allocated fixed-size UTF-8 encoded string of length <#= i #> bytes.
/// </summary>
/// <remarks>
/// This struct is designed to hold data on the stack. Every copy of this struct causes a copy of the underlying data.
/// If you need a heap allocated fixed-size UTF-8 encoded string of length <#= i #> bytes, consider using <see cref="Misaki.HighPerformance.Unsafe.Buffer.FixedString<#= i #>"/>.
/// </remarks>
[StructLayout(LayoutKind.Sequential, Size = <#= i #>)]
public unsafe struct FixedStackString<#= i #>
{
private ushort _length;
private fixed byte _buffer[<#= i - 2 #>];
public ushort Length => _length;
public string Value
{
get
{
fixed (byte* bufferPtr = _buffer)
{
return Encoding.UTF8.GetString(bufferPtr, _length);
}
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > <#= i - 2 #>)
{
throw new ArgumentException("Input string is too long to fit in FixedStackString<#= i #>.");
}
fixed (byte* bufferPtr = _buffer)
{
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(bufferPtr, <#= i - 2 #>));
}
}
}
public FixedStackString<#= i #>(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > <#= i - 2 #>)
{
throw new ArgumentException("Input string is too long to fit in FixedString<#= i #>.");
}
fixed (char* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, bufferPtr, <#= i - 2 #>);
_length = (ushort)actualByteCount;
}
}
public FixedStackString<#= i #>(string input)
: this(input.AsSpan())
{
}
public FixedStackString<#= i #>(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedStackString<#= i #>(ReadOnlySpan<byte> input)
{
if (input.Length > <#= i - 2 #>)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString<#= i #>.");
}
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
fixed (byte* bufferPtr = _buffer)
{
SystemUnsfae.CopyBlockUnaligned(bufferPtr, inputPtr, _length);
}
}
public FixedStackString<#= i #>(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public Span<byte> AsSpan()
{
fixed (byte* ptr = _buffer)
{
return new(ptr, _length);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte* GetUnsafePointer()
{
fixed (byte* ptr = _buffer)
{
return ptr;
}
}
public override string ToString()
{
return Value;
}
}
<# } #>

View File

@@ -0,0 +1,894 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Misaki.HighPerformance.LowLevel.Buffer;
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 32 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 32)]
public unsafe struct FixedString32 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 30)
{
throw new ArgumentException("Input string is too long to fit in FixedString32.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 30));
}
}
public FixedString32(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 30)
{
throw new ArgumentException("Input string is too long to fit in FixedString32.");
}
_buffer = (byte*)NativeMemory.Alloc(30);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 30);
_length = (ushort)actualByteCount;
}
}
public FixedString32(string input)
: this(input.AsSpan())
{
}
public FixedString32(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString32(ReadOnlySpan<byte> input)
{
if (input.Length > 30)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString32.");
}
_buffer = (byte*)NativeMemory.Alloc(30);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString32(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 64 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 64)]
public unsafe struct FixedString64 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 62)
{
throw new ArgumentException("Input string is too long to fit in FixedString64.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 62));
}
}
public FixedString64(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 62)
{
throw new ArgumentException("Input string is too long to fit in FixedString64.");
}
_buffer = (byte*)NativeMemory.Alloc(62);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 62);
_length = (ushort)actualByteCount;
}
}
public FixedString64(string input)
: this(input.AsSpan())
{
}
public FixedString64(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString64(ReadOnlySpan<byte> input)
{
if (input.Length > 62)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString64.");
}
_buffer = (byte*)NativeMemory.Alloc(62);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString64(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 128 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 128)]
public unsafe struct FixedString128 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 126)
{
throw new ArgumentException("Input string is too long to fit in FixedString128.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 126));
}
}
public FixedString128(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 126)
{
throw new ArgumentException("Input string is too long to fit in FixedString128.");
}
_buffer = (byte*)NativeMemory.Alloc(126);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 126);
_length = (ushort)actualByteCount;
}
}
public FixedString128(string input)
: this(input.AsSpan())
{
}
public FixedString128(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString128(ReadOnlySpan<byte> input)
{
if (input.Length > 126)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString128.");
}
_buffer = (byte*)NativeMemory.Alloc(126);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString128(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 256 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 256)]
public unsafe struct FixedString256 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 254)
{
throw new ArgumentException("Input string is too long to fit in FixedString256.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 254));
}
}
public FixedString256(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 254)
{
throw new ArgumentException("Input string is too long to fit in FixedString256.");
}
_buffer = (byte*)NativeMemory.Alloc(254);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 254);
_length = (ushort)actualByteCount;
}
}
public FixedString256(string input)
: this(input.AsSpan())
{
}
public FixedString256(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString256(ReadOnlySpan<byte> input)
{
if (input.Length > 254)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString256.");
}
_buffer = (byte*)NativeMemory.Alloc(254);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString256(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 512 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 512)]
public unsafe struct FixedString512 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 510)
{
throw new ArgumentException("Input string is too long to fit in FixedString512.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 510));
}
}
public FixedString512(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 510)
{
throw new ArgumentException("Input string is too long to fit in FixedString512.");
}
_buffer = (byte*)NativeMemory.Alloc(510);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 510);
_length = (ushort)actualByteCount;
}
}
public FixedString512(string input)
: this(input.AsSpan())
{
}
public FixedString512(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString512(ReadOnlySpan<byte> input)
{
if (input.Length > 510)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString512.");
}
_buffer = (byte*)NativeMemory.Alloc(510);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString512(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 1024 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 1024)]
public unsafe struct FixedString1024 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 1022)
{
throw new ArgumentException("Input string is too long to fit in FixedString1024.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 1022));
}
}
public FixedString1024(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 1022)
{
throw new ArgumentException("Input string is too long to fit in FixedString1024.");
}
_buffer = (byte*)NativeMemory.Alloc(1022);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 1022);
_length = (ushort)actualByteCount;
}
}
public FixedString1024(string input)
: this(input.AsSpan())
{
}
public FixedString1024(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString1024(ReadOnlySpan<byte> input)
{
if (input.Length > 1022)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString1024.");
}
_buffer = (byte*)NativeMemory.Alloc(1022);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString1024(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 2048 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 2048)]
public unsafe struct FixedString2048 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 2046)
{
throw new ArgumentException("Input string is too long to fit in FixedString2048.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 2046));
}
}
public FixedString2048(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 2046)
{
throw new ArgumentException("Input string is too long to fit in FixedString2048.");
}
_buffer = (byte*)NativeMemory.Alloc(2046);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 2046);
_length = (ushort)actualByteCount;
}
}
public FixedString2048(string input)
: this(input.AsSpan())
{
}
public FixedString2048(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString2048(ReadOnlySpan<byte> input)
{
if (input.Length > 2046)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString2048.");
}
_buffer = (byte*)NativeMemory.Alloc(2046);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString2048(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length 4096 bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = 4096)]
public unsafe struct FixedString4096 : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > 4094)
{
throw new ArgumentException("Input string is too long to fit in FixedString4096.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, 4094));
}
}
public FixedString4096(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > 4094)
{
throw new ArgumentException("Input string is too long to fit in FixedString4096.");
}
_buffer = (byte*)NativeMemory.Alloc(4094);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, 4094);
_length = (ushort)actualByteCount;
}
}
public FixedString4096(string input)
: this(input.AsSpan())
{
}
public FixedString4096(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString4096(ReadOnlySpan<byte> input)
{
if (input.Length > 4094)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString4096.");
}
_buffer = (byte*)NativeMemory.Alloc(4094);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString4096(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}

View File

@@ -0,0 +1,125 @@
<#@ template debug="false" hostspecific="false" language="C#" #>
<#@ assembly name="System.Core" #>
<#@ import namespace="System.Linq" #>
<#@ import namespace="System.Text" #>
<#@ import namespace="System.Collections.Generic" #>
<#@ output extension=".cs" #>
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
namespace Misaki.HighPerformance.LowLevel.Buffer;
<# for (int i = 32; i <= 4096; i *= 2) { #>
/// <summary>
/// Represents a heap allocated fixed-size UTF-8 encoded string of length <#= i #> bytes.
/// </summary>
[StructLayout(LayoutKind.Sequential, Size = <#= i #>)]
public unsafe struct FixedString<#= i #> : IDisposable
{
private ushort _length;
private byte* _buffer;
public ushort Length => _length;
public string Value
{
readonly get
{
return Encoding.UTF8.GetString(_buffer, _length);
}
set
{
if (string.IsNullOrEmpty(value))
{
_length = 0;
return;
}
var maxBytes = Encoding.UTF8.GetByteCount(value);
if (maxBytes > <#= i - 2 #>)
{
throw new ArgumentException("Input string is too long to fit in FixedString<#= i #>.");
}
_length = (ushort)Encoding.UTF8.GetBytes(value, new Span<byte>(_buffer, <#= i - 2 #>));
}
}
public FixedString<#= i #>(ReadOnlySpan<char> input)
{
var maxBytes = Encoding.UTF8.GetByteCount(input);
if (maxBytes > <#= i - 2 #>)
{
throw new ArgumentException("Input string is too long to fit in FixedString<#= i #>.");
}
_buffer = (byte*)NativeMemory.Alloc(<#= i - 2 #>);
fixed (char* inputPtr = input)
{
var actualByteCount = Encoding.UTF8.GetBytes(inputPtr, input.Length, _buffer, <#= i - 2 #>);
_length = (ushort)actualByteCount;
}
}
public FixedString<#= i #>(string input)
: this(input.AsSpan())
{
}
public FixedString<#= i #>(char* input, ushort length)
: this(new Span<char>(input, length))
{
}
public FixedString<#= i #>(ReadOnlySpan<byte> input)
{
if (input.Length > <#= i - 2 #>)
{
throw new ArgumentException("Input byte array is too long to fit in FixedString<#= i #>.");
}
_buffer = (byte*)NativeMemory.Alloc(<#= i - 2 #>);
_length = (ushort)input.Length;
fixed (byte* inputPtr = input)
{
SystemUnsfae.CopyBlockUnaligned(_buffer, inputPtr, _length);
}
}
public FixedString<#= i #>(byte* input, ushort length)
: this(new ReadOnlySpan<byte>(input, length))
{
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly Span<byte> AsSpan()
{
return new(_buffer, _length);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public readonly byte* GetUnsafePointer()
{
return _buffer;
}
public override string ToString()
{
return Value;
}
public void Dispose()
{
if (_buffer != null)
{
NativeMemory.Free(_buffer);
_length = 0;
_buffer = null;
}
}
}
<# } #>

View File

@@ -0,0 +1,19 @@
using Misaki.HighPerformance.LowLevel.Collections;
namespace Misaki.HighPerformance.LowLevel.Buffer;
// TODO: Implement a pool for UnsafeArray<T>.
public unsafe static class UnsafeArrayPool
{
public static UnsafeArray<T> Rent<T>(int minimalSize)
where T : unmanaged
{
throw new NotImplementedException();
}
public static void Return<T>(UnsafeArray<T> array)
where T : unmanaged
{
throw new NotImplementedException();
}
}