hlsf error is solving

This commit is contained in:
Youmu Liu
2025-11-28 18:18:08 +08:00
parent a3570bf20a
commit 0c90461d35
8 changed files with 253 additions and 281 deletions
@@ -28,7 +28,10 @@
using System;
using System.Buffers;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Reflection.Emit;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
@@ -45,7 +48,7 @@ namespace StgSharp.HighPerformance.Memory
/// </summary>
public void Collect()
{
Entry* cur = _positionHead;
Entry* cur = _spareMemory->NextNear;
while (cur != null && cur != _spareMemory)
{
if (cur->State != EntryState.Empty)
@@ -53,36 +56,19 @@ namespace StgSharp.HighPerformance.Memory
cur = cur->NextNear;
continue;
}
RemoveFromBucket(cur->Level, DetermineSegmentIndex(cur->Size, cur->Level), (BucketNode*)cur->Position);
Entry* next = cur->NextNear;
Entry* runHead = cur;
Entry* next = runHead->NextNear;
// Remove head from bucket if currently registered
int headLevel = runHead->Level;
int headSeg = DetermineSegmentIndex(runHead->Size, headLevel);
TryRemoveFromBucket(headLevel, headSeg, (BucketNode*)runHead->Position);
while (next != null &&
next != _spareMemory &&
while (next is not null &&
next->State == EntryState.Empty &&
IsAdjacentTo(runHead, next))
AssertAdjacentTo(cur, next))
{
int lvl = next->Level;
int seg = DetermineSegmentIndex(next->Size, lvl);
TryRemoveFromBucket(lvl, seg, (BucketNode*)next->Position);
runHead->Size += next->Size;
RemoveFromBucket(next->Level, DetermineSegmentIndex(next->Size, next->Level), (BucketNode*)next->Position);
cur->Size += next->Size;
RemoveFromPositionChain(next);
next = runHead->NextNear;
next = cur->NextNear;
}
// Recompute level for merged block and push once
runHead->Level = GetLevelFromSize(runHead->Size);
int finalLevel = runHead->Level;
int finalSeg = DetermineSegmentIndex(runHead->Size, finalLevel);
((BucketNode*)runHead->Position)->EntryRef = runHead; // ensure consistency
PushLevel(finalLevel, finalSeg, (BucketNode*)runHead->Position);
SliceMemory(cur);
cur = next;
}
}
@@ -99,7 +85,13 @@ namespace StgSharp.HighPerformance.Memory
if (e == null) {
return;
}
if (e->State != EntryState.Allocated) {
if (e->State == EntryState.Large)
{
NativeMemory.AlignedFree((void*)e->Position);
_entries.Free((nuint)e);
return;
}
if (e->State != EntryState.Alloc) {
return;
}
@@ -110,12 +102,15 @@ namespace StgSharp.HighPerformance.Memory
MergeAndRemoveFromBuckets(e);
// Calculate final level and segment for the merged block
uint finalSize = e->Size;
long finalSize = e->Size;
int finalLevel = GetLevelFromSize(finalSize);
e->Level = finalLevel;
int finalSegment = DetermineSegmentIndex(finalSize, finalLevel);
// Push the merged entry back to appropriate bucket
PushLevel(finalLevel, finalSegment, (BucketNode*)e->Position);
// Console.WriteLine("one block freed");
}
}