1 Commits

6 changed files with 136 additions and 213 deletions

View File

@@ -11,10 +11,5 @@ namespace UnmanagedMMU.Handles.Internal
/// Returns the <see cref="IUnmanagedMemoryOwner"/> that created owns this <see cref="IOwnedHandle"/> /// Returns the <see cref="IUnmanagedMemoryOwner"/> that created owns this <see cref="IOwnedHandle"/>
/// </summary> /// </summary>
IUnmanagedMemoryOwner GetOwner(); IUnmanagedMemoryOwner GetOwner();
/// <summary>
/// Returns the memory alignment for the <see cref="IOwnedHandle"/>
/// </summary>
nuint Alignment { get; }
} }
} }

View File

@@ -41,25 +41,18 @@ namespace UnmanagedMMU.Handles
/// </summary> /// </summary>
private bool _disposed; private bool _disposed;
/// <summary>
/// The alignment of the memory pointed to by <see cref="_ptr"/>
/// </summary>
private readonly nuint _alignment;
/// <summary> /// <summary>
/// Initializes a new <see cref="MemoryHandleBase{T}"/> instnace /// Initializes a new <see cref="MemoryHandleBase{T}"/> instnace
/// </summary> /// </summary>
/// <param name="ptr">Pointer to the allocated unmanaged memory</param> /// <param name="ptr">Pointer to the allocated unmanaged memory</param>
/// <param name="byteLength">The size of the unallocated memory block in bytes</param> /// <param name="byteLength">The size of the unallocated memory block in bytes</param>
/// <param name="alignment">The alignment that the unmanged memory pointed to by <paramref name="ptr"/> </param>
/// <param name="owner">The <see cref="IUnmanagedMemoryOwner"/> that owns the <see cref="MemoryHandleBase{T}"/> handle being created</param> /// <param name="owner">The <see cref="IUnmanagedMemoryOwner"/> that owns the <see cref="MemoryHandleBase{T}"/> handle being created</param>
protected MemoryHandleBase(void* ptr, nuint byteLength, nuint alignment, IUnmanagedMemoryOwner owner) protected MemoryHandleBase(void* ptr, nuint byteLength, IUnmanagedMemoryOwner owner)
{ {
// Defensive check // Defensive check
Debug.Assert(ptr != null, message: "BUG CHECK: E_INVALID_MEMORY_HANDLE"); Debug.Assert(ptr != null, message: "BUG CHECK: E_INVALID_MEMORY_HANDLE");
_ptr = ptr; _ptr = ptr;
_bytelen = byteLength; _bytelen = byteLength;
_alignment = alignment;
_owner = owner; _owner = owner;
} }
@@ -71,14 +64,6 @@ namespace UnmanagedMMU.Handles
get { return _ptr; } get { return _ptr; }
} }
/// <summary>
/// Returns the memory alignment for the <see cref="MemoryHandleBase{T}"/>
/// </summary>
public virtual nuint Alignment
{
get { return _alignment; }
}
/// <summary> /// <summary>
/// Gets the typed pointer to the unmanged memory block /// Gets the typed pointer to the unmanged memory block
/// </summary> /// </summary>

View File

@@ -5,7 +5,7 @@ namespace UnmanagedMMU.Handles
internal sealed unsafe class PersistentMemoryHandle<T> : MemoryHandleBase<T> where T : unmanaged internal sealed unsafe class PersistentMemoryHandle<T> : MemoryHandleBase<T> where T : unmanaged
{ {
public PersistentMemoryHandle(void* ptr, nuint byteLength, nuint alignment, IUnmanagedMemoryOwner owner) : base(ptr, byteLength, alignment, owner) public PersistentMemoryHandle(void* ptr, nuint byteLength, IUnmanagedMemoryOwner owner) : base(ptr, byteLength, owner)
{ {
} }

View File

@@ -1,10 +1,15 @@
using UnmanagedMMU.Allocators; using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnmanagedMMU.Allocators;
namespace UnmanagedMMU.Handles namespace UnmanagedMMU.Handles
{ {
internal sealed unsafe class SegmentedMemoryHandle<T> : MemoryHandleBase<T> where T : unmanaged internal sealed unsafe class SegmentedMemoryHandle<T> : MemoryHandleBase<T> where T : unmanaged
{ {
public SegmentedMemoryHandle(void* ptr, nuint byteLength, nuint alignment, IUnmanagedMemoryOwner owner) : base(ptr, byteLength, alignment, owner) public SegmentedMemoryHandle(void* ptr, nuint byteLength, IUnmanagedMemoryOwner owner) : base(ptr, byteLength, owner)
{ {
} }

View File

@@ -291,7 +291,7 @@
/// <exception cref="ArgumentException"> /// <exception cref="ArgumentException">
/// Thrown if <paramref name="segmentSize"/> is zero, or if <paramref name="initialSegments"/> is less than 1. /// Thrown if <paramref name="segmentSize"/> is zero, or if <paramref name="initialSegments"/> is less than 1.
/// </exception> /// </exception>
public SegmentedPool(nuint segmentSize = _defaultSegmentSize, SegmentAlignment segmentAlignment = SegmentAlignment.Aligned16, int initialSegments = 4, bool zeroMemory = false) public SegmentedPool(nuint segmentSize = _defaultSegmentSize, SegmentAlignment segmentAlignment = SegmentAlignment.Aligned32, int initialSegments = 4, bool zeroMemory = false)
: this(segmentSize, segmentAlignment, initialSegments, zeroMemory, new DefaultUnmanagedAllocator()) : this(segmentSize, segmentAlignment, initialSegments, zeroMemory, new DefaultUnmanagedAllocator())
{ {
} }
@@ -620,9 +620,7 @@
{ {
T* ptr = Alloc<T>(count); T* ptr = Alloc<T>(count);
nuint byteLength = (nuint)count * (nuint)sizeof(T); nuint byteLength = (nuint)count * (nuint)sizeof(T);
return new SegmentedMemoryHandle<T>(ptr, byteLength, this);
nuint alignment = _segmentAlignment > (nuint)sizeof(T) ? _segmentAlignment : (nuint)sizeof(T);
return new SegmentedMemoryHandle<T>(ptr, byteLength, alignment, this);
} }
/// <summary> /// <summary>
@@ -653,7 +651,7 @@
T* ptr = AllocateWithAlignment<T>(count, effectiveAlignment); T* ptr = AllocateWithAlignment<T>(count, effectiveAlignment);
nuint byteLength = (nuint)count * (nuint)sizeof(T); nuint byteLength = (nuint)count * (nuint)sizeof(T);
return new SegmentedMemoryHandle<T>(ptr, byteLength, effectiveAlignment, this); return new SegmentedMemoryHandle<T>(ptr, byteLength, this);
} }

View File

@@ -3,6 +3,7 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using UnmanagedMMU.Allocators; using UnmanagedMMU.Allocators;
using UnmanagedMMU.Handles; using UnmanagedMMU.Handles;
using UnmanagedMMU.Handles.Internal; using UnmanagedMMU.Handles.Internal;
@@ -32,16 +33,6 @@
/// </remarks> /// </remarks>
public unsafe sealed class WorkspaceHeap : IDisposable, IUnmanagedMemoryOwner public unsafe sealed class WorkspaceHeap : IDisposable, IUnmanagedMemoryOwner
{ {
/// <summary>
/// Struct defining metadata to be stored with each allocation
/// </summary>
private struct AllocationMetadata
{
public IntPtr Block;
public nuint Alignment;
}
/// <summary> /// <summary>
/// The maximum size, in bytes, for "small" allocations. Uses fixed-size buckets /// The maximum size, in bytes, for "small" allocations. Uses fixed-size buckets
/// </summary> /// </summary>
@@ -70,12 +61,12 @@
/// Predefined bucket sizes used for small allocation reuse. /// Predefined bucket sizes used for small allocation reuse.
/// </summary> /// </summary>
private static readonly nuint[] _sizeClasses = private static readonly nuint[] _sizeClasses =
[ {
32, 64, 96, 128, 160, 192, 224, 256, 32, 64, 96, 128, 160, 192, 224, 256,
288, 320, 352, 384, 416, 448, 480, 512, 288, 320, 352, 384, 416, 448, 480, 512,
544, 576, 608, 640, 672, 704, 736, 768, 544, 576, 608, 640, 672, 704, 736, 768,
800, 832, 864, 896, 928, 960, 992, 1024 800, 832, 864, 896, 928, 960, 992, 1024
]; };
/// <summary> /// <summary>
/// Allocator interface used for all underlying unmanaged memory operations. /// Allocator interface used for all underlying unmanaged memory operations.
@@ -93,17 +84,17 @@
/// <remarks> /// <remarks>
/// For a given bucket, the corresponding stack contains previously allocated blocks that are available to be used /// For a given bucket, the corresponding stack contains previously allocated blocks that are available to be used
/// </remarks> /// </remarks>
private readonly Dictionary<nuint, List<AllocationMetadata>> _smallFree = []; private readonly Dictionary<nuint, Stack<IntPtr>> _smallFree = new();
/// <summary> /// <summary>
/// Free lists for medium allocations keyed by the exactly allocated size, in bytes, and sorted for best-fit. /// Free lists for medium allocations keyed by the exactly allocated size, in bytes, and sorted for best-fit.
/// </summary> /// </summary>
private readonly SortedDictionary<nuint, List<AllocationMetadata>> _mediumFree = []; private readonly SortedDictionary<nuint, Stack<IntPtr>> _mediumFree = new();
/// <summary> /// <summary>
/// Free lists for large allocations keyed by the exactly allocated size, in bytes, sorted for tolerance-based reuse. /// Free lists for large allocations keyed by the exactly allocated size, in bytes, sorted for tolerance-based reuse.
/// </summary> /// </summary>
private readonly SortedDictionary<nuint, List<AllocationMetadata>> _largeFree = []; private readonly SortedDictionary<nuint, Stack<IntPtr>> _largeFree = new();
/// <summary> /// <summary>
/// Tracks the total bytes of memory reserved from the provided <see cref="IUnmanagedAllocator"/>. /// Tracks the total bytes of memory reserved from the provided <see cref="IUnmanagedAllocator"/>.
@@ -125,6 +116,33 @@
/// </summary> /// </summary>
private volatile bool _disposed; private volatile bool _disposed;
/// <summary>
/// Internal header prepended to each allocation to track its size.
/// </summary>
[StructLayout(LayoutKind.Sequential)]
private struct BlockHeader
{
/// <summary>
/// Size of the allocation in bytes.
/// </summary>
public nuint Size;
/// <summary>
/// Padding to ensure <see cref="BlockHeader"></see> is 32-byte aligned
/// </summary>
private readonly nuint _pad1;
/// <summary>
/// Padding to ensure <see cref="BlockHeader"></see> is 32-byte aligned
/// </summary>
private readonly nuint _pad2;
/// <summary>
/// Padding to ensure <see cref="BlockHeader"></see> is 32-byte aligned
/// </summary>
private readonly nuint _pad3;
}
/// <summary> /// <summary>
/// Creates a new <see cref="WorkspaceHeap"/>. /// Creates a new <see cref="WorkspaceHeap"/>.
/// </summary> /// </summary>
@@ -142,10 +160,8 @@
_allocator = allocator; _allocator = allocator;
// Initialize small-size buckets // Initialize small-size buckets
foreach (nuint size in _sizeClasses) foreach (var size in _sizeClasses)
{ _smallFree[size] = new Stack<IntPtr>();
_smallFree[size] = [];
}
} }
/// <summary> /// <summary>
@@ -227,15 +243,16 @@
/// Allocates a new block from the underlying allocator including a header. /// Allocates a new block from the underlying allocator including a header.
/// </summary> /// </summary>
/// <param name="payloadSize">Requested payload size in bytes.</param> /// <param name="payloadSize">Requested payload size in bytes.</param>
/// <param name="alignment">FOO</param>
/// <returns> /// <returns>
/// Pointer to the allocated block (header included). /// Pointer to the allocated block (header included).
/// </returns> /// </returns>
private IntPtr AllocateNewAligned(nuint payloadSize, nuint alignment) private IntPtr AllocateNew(nuint payloadSize)
{ {
void* raw = _allocator.AllocAligned(payloadSize, alignment); nuint total = payloadSize + (nuint)sizeof(BlockHeader);
_totalReserved += payloadSize; void* raw = _allocator.Alloc(total);
_totalReserved += total;
_totalAllocations++; _totalAllocations++;
return (IntPtr)raw; return (IntPtr)raw;
} }
@@ -247,154 +264,100 @@
/// <returns> An <see cref="IMemoryHandle{T}"/> to the allocated memory.</returns> /// <returns> An <see cref="IMemoryHandle{T}"/> to the allocated memory.</returns>
/// <exception cref="ObjectDisposedException">Thrown if heap is disposed.</exception> /// <exception cref="ObjectDisposedException">Thrown if heap is disposed.</exception>
/// <exception cref="ArgumentOutOfRangeException">Thrown if size is zero.</exception> /// <exception cref="ArgumentOutOfRangeException">Thrown if size is zero.</exception>
/// <remarks>The memory returned by this method is always 16-byte aligned, for other alignemnts use </remarks>
public IMemoryHandle<T> Allocate<T>(int count, bool zero = false) where T : unmanaged public IMemoryHandle<T> Allocate<T>(int count, bool zero = false) where T : unmanaged
{
return AllocateAligned<T>(count, SegmentAlignment.Aligned16, zero);
}
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="count"></param>
/// <param name="alignment"></param>
/// <param name="zero"></param>
/// <returns></returns>
/// <exception cref="OverflowException"></exception>
public IMemoryHandle<T> AllocateAligned<T>(int count, SegmentAlignment alignment, bool zero = false) where T: unmanaged
{ {
ArgumentOutOfRangeException.ThrowIfNegative(count); ArgumentOutOfRangeException.ThrowIfNegative(count);
ArgumentOutOfRangeException.ThrowIfZero(count); ArgumentOutOfRangeException.ThrowIfZero(count);
if ((nuint)count > nuint.MaxValue / (nuint)(sizeof(T))) if ((nuint)count > nuint.MaxValue / (nuint)(sizeof(T)))
{ {
throw new OverflowException($"Requested allocation of {count} elements of type {typeof(T)} exceeds allowable maximum memory size."); throw new OverflowException($"Requested allocation of {count} elements of type {typeof(T)} exceeds allowable maximum memory size.");
} }
nuint size = (nuint)count * (nuint)sizeof(T); nuint size = (nuint)count * (nuint)sizeof(T);
nuint requestedAlignment = (nuint)alignment;
nuint effectiveAlignment = requestedAlignment < (nuint)sizeof(T) ? (nuint)sizeof(T) : requestedAlignment;
lock (_lock) lock (_lock)
{ {
ThrowIfDisposed(); ThrowIfDisposed();
void* ptr; void* ptr = null;
if (size <= _smallThreshold) if (size <= _smallThreshold)
ptr = AllocateSmallAligned(size, effectiveAlignment, zero); {
ptr = AllocateSmall(size, zero);
}
else if (size <= _mediumThreshold) else if (size <= _mediumThreshold)
ptr = AllocateMediumAligned(size, effectiveAlignment, zero); {
ptr = AllocateMedium(size, zero);
}
else else
ptr = AllocateLargeAligned(size, effectiveAlignment, zero); {
ptr = AllocateLarge(size, zero);
}
return new PersistentMemoryHandle<T>((T*)ptr, size, effectiveAlignment, this); return new PersistentMemoryHandle<T>((T*)ptr, size, this);
} }
} }
/// <summary> /// <summary>Allocates a small-size block using bucketed free lists.</summary>
/// TODO: Fill in private void* AllocateSmall(nuint size, bool zero)
/// </summary>
/// <param name="alignment"></param>
/// <param name="list"></param>
/// <returns></returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int FindIndexAlignmentMatch(nuint alignment, List<AllocationMetadata> list)
{
// Attempt to find an index match the requested size
int mIdx = -1;
for (int i = 0; i < list.Count; i++)
{
if (list[i].Alignment >= alignment)
{
mIdx = i;
break;
}
}
return mIdx;
}
/// <summary>
/// Allocates a small-size block using bucketed free lists.
/// </summary>
private void* AllocateSmallAligned(nuint size, nuint alignment, bool zero)
{ {
nuint bucket = GetSizeClass(size); nuint bucket = GetSizeClass(size);
List<AllocationMetadata> free = _smallFree[bucket]; Stack<IntPtr> stack = _smallFree[bucket];
// Attempt to find an index match the requested size IntPtr block = stack.Count > 0
int mIdx = FindIndexAlignmentMatch(alignment, free); ? stack.Pop()
: AllocateNew(bucket);
if (mIdx >= 0) BlockHeader* header = (BlockHeader*)block;
{ header->Size = bucket;
AllocationMetadata meta = free[mIdx];
free.RemoveAt(mIdx);
_totalInUse += bucket; _totalInUse += bucket;
void* user = (void*)meta.Block;
void* user = header + 1;
if (zero) if (zero)
{
Unsafe.InitBlockUnaligned(user, 0, (uint)bucket); Unsafe.InitBlockUnaligned(user, 0, (uint)bucket);
}
return user;
}
// allocate a new small block return user;
IntPtr block = AllocateNewAligned(bucket, alignment);
_totalInUse += bucket;
void* nUser = (void*)block;
if (zero)
{
Unsafe.InitBlockUnaligned(nUser, 0, (uint)bucket);
}
return nUser;
} }
/// <summary>Allocates a medium-size block using best-fit reuse.</summary> /// <summary>Allocates a medium-size block using best-fit reuse.</summary>
private void* AllocateMediumAligned(nuint size, nuint alignment, bool zero) private void* AllocateMedium(nuint size, bool zero)
{ {
foreach (KeyValuePair<nuint, List<AllocationMetadata>> kv in _mediumFree) foreach (var kv in _mediumFree)
{ {
if (kv.Key >= size && kv.Value.Count > 0) if (kv.Key >= size && kv.Value.Count > 0)
{ {
List<AllocationMetadata> free = kv.Value; var block = kv.Value.Pop();
var header = (BlockHeader*)block;
_totalInUse += header->Size;
// Attempt to find an index match the requested size void* user = header + 1;
int mIdx = FindIndexAlignmentMatch(alignment, free);
if (mIdx >= 0)
{
AllocationMetadata meta = free[mIdx];
free.RemoveAt(mIdx);
_totalInUse += kv.Key;
void* user = (void*)meta.Block;
if (zero) if (zero)
{ Unsafe.InitBlockUnaligned(user, 0, (uint)header->Size);
Unsafe.InitBlockUnaligned(user, 0, (uint)kv.Key);
}
return user; return user;
} }
} }
}
// no match var newBlock = AllocateNew(size);
IntPtr newBlock = AllocateNewAligned(size, alignment); var newHeader = (BlockHeader*)newBlock;
newHeader->Size = size;
_totalInUse += size; _totalInUse += size;
void* nUser = (void*)newBlock; void* newUser = newHeader + 1;
if (zero) if (zero)
{ {
Unsafe.InitBlockUnaligned(nUser, 0, (uint)size); Unsafe.InitBlockUnaligned(newUser, 0, (uint)size);
} }
return nUser;
return newUser;
} }
/// <summary> /// <summary>Allocates a large block using tolerance-based reuse (smallest ≥ requested within waste bounds).</summary>
/// Allocates a large block using tolerance-based reuse (smallest ≥ requested within waste bounds). private void* AllocateLarge(nuint size, bool zero)
/// </summary>
private void* AllocateLargeAligned(nuint size, nuint alignment, bool zero)
{ {
foreach (KeyValuePair<nuint, List<AllocationMetadata>> kv in _largeFree) foreach (var kv in _largeFree)
{ {
nuint blockSize = kv.Key; nuint blockSize = kv.Key;
if (blockSize < size || kv.Value.Count == 0) if (blockSize < size || kv.Value.Count == 0)
@@ -402,46 +365,41 @@
continue; continue;
} }
// check waste tolerance before attempting to find an alignment match
nuint waste = blockSize - size; nuint waste = blockSize - size;
bool acceptable = waste <= _largeMaxWasteBytes || ((double)blockSize / size) <= _largeWasteRatioLimit; bool acceptable =
waste <= _largeMaxWasteBytes ||
((double)blockSize / size) <= _largeWasteRatioLimit;
if (!acceptable) if (!acceptable)
{ {
continue; continue;
} }
List<AllocationMetadata> free = kv.Value; var block = kv.Value.Pop();
var header = (BlockHeader*)block;
_totalInUse += header->Size;
// Attempt to find an index match the requested size void* user = header + 1;
int mIdx = FindIndexAlignmentMatch(alignment, free);
if (mIdx >= 0)
{
AllocationMetadata meta = free[mIdx];
free.RemoveAt(mIdx);
_totalInUse += blockSize;
void* user = (void*)meta.Block;
if (zero) if (zero)
{ {
Unsafe.InitBlockUnaligned(user, 0, (uint)blockSize); Unsafe.InitBlockUnaligned(user, 0, (uint)header->Size);
} }
return user; return user;
} }
}
// no match var newBlock = AllocateNew(size);
var newHeader = (BlockHeader*)newBlock;
IntPtr block = AllocateNewAligned(size, alignment); newHeader->Size = size;
_totalInUse += size; _totalInUse += size;
void* nUser = (void*)block;
void* newUser = newHeader + 1;
if (zero) if (zero)
{ {
Unsafe.InitBlockUnaligned(nUser, 0, (uint)size); Unsafe.InitBlockUnaligned(newUser, 0, (uint)size);
} }
return nUser;
return newUser;
} }
/// <summary> /// <summary>
@@ -463,41 +421,23 @@
lock (_lock) lock (_lock)
{ {
nuint size = handle.ByteCount; var header = ((BlockHeader*)handle.Pointer) - 1;
nuint alignment = handle.Alignment; nuint size = header->Size;
_totalInUse -= size; _totalInUse -= size;
if (size <= _smallThreshold) if (size <= _smallThreshold)
{ _smallFree[size].Push((IntPtr)header);
_smallFree[size].Add(new AllocationMetadata
{
Block = (IntPtr)handle.Pointer,
Alignment = alignment
});
}
else if (size <= _mediumThreshold) else if (size <= _mediumThreshold)
{ {
if (!_mediumFree.TryGetValue(size, out List<AllocationMetadata>? free)) if (!_mediumFree.TryGetValue(size, out var stack))
{ _mediumFree[size] = stack = new Stack<IntPtr>();
_mediumFree[size] = free = []; stack.Push((IntPtr)header);
}
free.Add(new AllocationMetadata
{
Block = (IntPtr)handle.Pointer,
Alignment = alignment
});
} }
else else
{ {
if (!_largeFree.TryGetValue(size, out List<AllocationMetadata>? free)) if (!_largeFree.TryGetValue(size, out var stack))
{ _largeFree[size] = stack = new Stack<IntPtr>();
_largeFree[size] = free = []; stack.Push((IntPtr)header);
}
free.Add(new AllocationMetadata
{
Block = (IntPtr)handle.Pointer,
Alignment = alignment
});
} }
} }
} }
@@ -517,17 +457,17 @@
} }
/// <summary>Helper to free all blocks in a dictionary of free stacks.</summary> /// <summary>Helper to free all blocks in a dictionary of free stacks.</summary>
private void PruneDictionary(IDictionary<nuint, List<AllocationMetadata>> dict) private void PruneDictionary(IDictionary<nuint, Stack<IntPtr>> dict)
{ {
foreach (KeyValuePair<nuint, List<AllocationMetadata>> kv in dict) foreach (var kv in dict)
{ {
List<AllocationMetadata> list = kv.Value; var stack = kv.Value;
while (list.Count > 0) while (stack.Count > 0)
{ {
var item = list[^1]; var block = stack.Pop();
list.RemoveAt(list.Count - 1); var header = (BlockHeader*)block;
_allocator.FreeAligned((void*)item.Block, item.Alignment); _allocator.Free((void*)block);
_totalReserved -= kv.Key; _totalReserved -= (header->Size + (nuint)sizeof(BlockHeader));
} }
} }
} }