Initial upload;
This commit is contained in:
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;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user