HLSF functionally available

Signed-off-by: Nitload <YoumuLiu_NSI@whv0t.onmicrosoft.com>
This commit is contained in:
Nitload
2025-08-24 13:41:35 +08:00
parent 11b630ffa2
commit 8779d7d77c
110 changed files with 268 additions and 78705 deletions
@@ -57,9 +57,7 @@ namespace StgSharp.HighPerformance.Memory
int segmentIndex = DetermineSegmentIndex(size, level);
int i = level, s = segmentIndex;
if (TryPopLevelForAllocation(i, s, out Entry* handle))
{
SetEntryState(handle, EntryState.Allocated);
if (TryPopLevelForAllocation(i, s, out Entry* handle)) {
return new hlsfHandle(handle, size);
}
@@ -82,10 +80,8 @@ namespace StgSharp.HighPerformance.Memory
}
}
if (handle != null)
{
if (handle != null) {
SliceMemory(handle, (segmentIndex + 1) * levelSizeArray[level]);
SetEntryState(handle, EntryState.Allocated);
}
return new hlsfHandle(handle, size);
@@ -106,14 +102,14 @@ namespace StgSharp.HighPerformance.Memory
throw new InvalidOperationException("Entry position offset is not aligned to 64 bytes.");
}
int minLevel = Math.Max((BitOperations.TrailingZeroCount(originOffset) - 6) / 2, 0);
int minLevel = Math.Max((BitOperations.TrailingZeroCount(finalOffset) - 6) / 2, 0);
int i = minLevel >= levelSizeArray.Length ? levelSizeArray.Length - 1 : minLevel;
for (; i < levelSizeArray.Length; i++)
{
int levelSize = levelSizeArray[i];
ulong board = (finalOffset + (ulong)levelSize - 1) & ~((ulong)levelSize - 1);
int blockCount = (int)(ulong.Min(board, end) / (ulong)levelSize);
int blockCount = (int)((ulong.Min(board, end) - board) / (ulong)levelSize);
if (blockCount == 0)
{
break;
@@ -126,7 +122,6 @@ namespace StgSharp.HighPerformance.Memory
newEntry->Size = (uint)s;
BucketNode* bucket = (BucketNode*)newEntry->Position;
bucket->EntryRef = newEntry;
newEntry->State = EntryState.ThreadOccupied;
// link new entry
newEntry->PreviousNear = (ulong)current;
@@ -155,7 +150,7 @@ namespace StgSharp.HighPerformance.Memory
{
int levelSize = levelSizeArray[i];
ulong board = (finalOffset + (ulong)levelSize - 1) & ~((ulong)levelSize - 1);
int blockCount = (int)(ulong.Min(board, end) / (ulong)levelSize);
int blockCount = (int)((ulong.Min(board, end) - finalOffset) / (ulong)levelSize);
if (blockCount == 0)
{
continue;
@@ -168,7 +163,6 @@ namespace StgSharp.HighPerformance.Memory
newEntry->Size = (uint)s;
BucketNode* bucket = (BucketNode*)newEntry->Position;
bucket->EntryRef = newEntry;
newEntry->State = EntryState.ThreadOccupied;
// link new entry
newEntry->PreviousNear = (ulong)current;
@@ -229,7 +223,6 @@ namespace StgSharp.HighPerformance.Memory
handle = (Entry*)_entries.Allocate();
handle->Position = pos;
handle->Size = (uint)blockSize;
SetEntryState(handle, EntryState.ThreadOccupied);
handle->Level = GetLevelFromSize((uint)blockSize);
// Update spare memory position
@@ -48,13 +48,13 @@ namespace StgSharp.HighPerformance.Memory
return;
}
if (GetEntryState(e) != EntryState.Allocated) {
if (e->State != EntryState.Allocated) {
return;
}
// Set to ThreadOccupied to prevent other operations
SetEntryState(e, EntryState.ThreadOccupied);
e->State = EntryState.Empty;
// Try merge with adjacent free blocks and remove them from buckets
@@ -68,7 +68,7 @@ namespace StgSharp.HighPerformance.Memory
// Set the merged entry state to Empty (ready to be reused)
SetEntryState(e, EntryState.Empty);
e->State = EntryState.Empty;
// Push the merged entry back to appropriate bucket
@@ -91,8 +91,6 @@ namespace StgSharp.HighPerformance.Memory
RemoveFromBucket(originLevel, segment, (BucketNode*)entry->Position);
SetEntryState(entry, EntryState.Merging);
while (originLevel >= entry->Level)
{
originLevel = entry->Level;
@@ -101,12 +99,11 @@ namespace StgSharp.HighPerformance.Memory
// backward merging - simplified to single attempt
ulong p = entry->PreviousNear;
Entry* prev = (Entry*)p;
if (prev != null ||
prev != _spareMemory ||
GetEntryState(prev) == EntryState.Empty ||
if (prev == null ||
prev == _spareMemory ||
!AssertBackwardBoundary(entry->Position, prev, originLevel) ||
IsAdjacentTo(entry, prev) ||
entry->Level == prev->Level ||
entry->Level != prev->Level ||
!TryRemoveFromBucket(originLevel, DetermineSegmentIndex(prev->Size, originLevel), (BucketNode*)prev->Position))
{
break;
@@ -130,12 +127,11 @@ namespace StgSharp.HighPerformance.Memory
// forward merging - simplified to single attempt
ulong n = entry->NextNear;
Entry* next = (Entry*)n;
if (next != null ||
next != _spareMemory ||
GetEntryState(next) == EntryState.Empty ||
if (next == null ||
next == _spareMemory ||
!AssertForwardBoundary(entry->Position + entry->Size, next, entry->Level) ||
IsAdjacentTo(entry, next) ||
entry->Level == next->Level ||
entry->Level != next->Level ||
!TryRemoveFromBucket(originLevel, DetermineSegmentIndex(next->Size, originLevel), (BucketNode*)next->Position))
{
break;
@@ -148,14 +144,12 @@ namespace StgSharp.HighPerformance.Memory
}
entry->NextNear = (ulong)nextNext;
// Merge
entry->Size += next->Size;
RecycleEntry(next);
}
entry->Level = GetLevelFromSize(entry->Size);
}
SetEntryState(entry, EntryState.Empty);
}
private void RecycleEntry(Entry* entry)
@@ -164,7 +158,6 @@ namespace StgSharp.HighPerformance.Memory
return;
}
entry->State = EntryState.Empty;
entry->PreviousNear = EmptyHandle;
entry->NextNear = EmptyHandle;
_entries.Free((nuint)entry);
@@ -199,27 +192,24 @@ namespace StgSharp.HighPerformance.Memory
/// Entry structure - streamlined version (only position and state information) Stored in
/// external SLAB
/// </summary>
[StructLayout(LayoutKind.Explicit, Size = 32)]
[StructLayout(LayoutKind.Explicit, Size = 36)]
internal struct Entry
{
[FieldOffset(28)] internal int Level; // Level of the block in the allocator
[FieldOffset(20)] internal int State; // Node state (using int for atomic operations)
[FieldOffset(24)] internal uint Size; // Block size information
[FieldOffset(8)] internal ulong NextNear; // Position linked list: next pointer
[FieldOffset(16)] internal ulong PreviousNear; // Position linked list: previous pointer
[FieldOffset(32)] public EntryState State;
[FieldOffset(0)] public nuint Position; // Memory block position
}
// 更新后的EntryState常量类 - 使用int类型
private static class EntryState
internal enum EntryState : uint
{
public const int Allocated = 1; // Already allocated to user
public const int Empty = 0; // Free, can be allocated
public const int Merging = 3; // Currently being merged
public const int ThreadOccupied = 2; // Occupied by thread (being operated on)
Empty,
Allocated
}
@@ -237,56 +227,6 @@ namespace StgSharp.HighPerformance.Memory
return entry == null;
}
// Entry 状态操作
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TrySetEntryState(Entry* entry, int expectedState, int newState)
{
if (entry != null && entry->State == expectedState)
{
entry->State = newState;
return true;
}
return false;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static int GetEntryState(Entry* entry)
{
return entry != null ? entry->State : 0;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static void SetEntryState(Entry* entry, int newState)
{
if (entry != null) {
entry->State = newState;
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsEntryEmpty(Entry* entry)
{
return GetEntryState(entry) == EntryState.Empty;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsEntryAllocated(Entry* entry)
{
return GetEntryState(entry) == EntryState.Allocated;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsEntryThreadOccupied(Entry* entry)
{
return GetEntryState(entry) == EntryState.ThreadOccupied;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsEntryMerging(Entry* entry)
{
return GetEntryState(entry) == EntryState.Merging;
}
#endregion
}
}
@@ -62,7 +62,7 @@ namespace StgSharp.HighPerformance.Memory
InitializeBucketSystem();
_spareMemory = (Entry*)_entries.Allocate();
SetEntryState(_spareMemory, EntryState.ThreadOccupied);
_spareMemory->State = EntryState.Empty;
_spareMemory->PreviousNear = EmptyHandle;
_spareMemory->NextNear = EmptyHandle;
_spareMemory->Position = (nuint)m_Buffer;
@@ -83,7 +83,7 @@ namespace StgSharp.HighPerformance.Memory
InitializeBucketSystem();
_spareMemory = (Entry*)_entries.Allocate();
SetEntryState(_spareMemory, EntryState.ThreadOccupied);
_spareMemory->State = EntryState.Empty;
_spareMemory->PreviousNear = EmptyHandle;
_spareMemory->NextNear = EmptyHandle;
_spareMemory->Position = (nuint)m_Buffer;