tlsf is ok, and fast

This commit is contained in:
Youmu Liu
2026-05-13 15:08:30 +08:00
parent bf9879f59f
commit ae57de4040
22 changed files with 409 additions and 4619 deletions
@@ -29,6 +29,7 @@ using StgSharp.HighPerformance.Memory;
using StgSharp.HighPerformance.ProcessorAbstraction;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Numerics;
@@ -36,6 +37,7 @@ using System.Reflection.Metadata;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using static StgSharp.HighPerformance.Memory.TwoLayerSegregatedFitAllocator;
namespace StgSharp.HighPerformance.Memory
@@ -162,64 +164,109 @@ namespace StgSharp.HighPerformance.Memory
Metadata* node
)
{
int nextIdx = _spareMemory->NextLevel;
int nodeIdx = (int)(_spareMemory - node);
int level = GetLevel(node->Size);
Metadata* next = &_spareMemory[-nextIdx];
int nextLevelIdx = node->NextLevel;
int prevLevelIdx = node->PreviousLevel;
if (nextIdx == 0)
{
_spareMemory->NextLevel = nodeIdx;
node->NextLevel = 0;
return;
} else
{
// add to free list
next->PreviousLevel = nodeIdx;
_spareMemory->NextLevel = nodeIdx;
node->NextLevel = nextIdx;
node->PreviousLevel = 0;
}
RemoveFromBucket(level, node, nodeIdx);
// remove from position chain
int prevNearIdx = node->PreviousNear;
int nextNearIdx = node->NextNear;
Metadata* prevNear = &_spareMemory[-prevNearIdx];
Metadata* nextNear = &_spareMemory[-nextNearIdx];
prevNear->NextNear = nextNearIdx;
nextNear->PreviousNear = prevNearIdx;
if (nextNearIdx != 0)
{
Metadata* nextNear = &_spareMemory[-nextNearIdx];
nextNear->PreviousNear = prevNearIdx;
} else
{
_spareMemory->PreviousNear = prevNearIdx;
}
node->PreviousNear = 0;
node->NextNear = 0;
node->Position = 0;
node->SizeForSpare = 0;
node->State = AllocationState.SpareOrCollect;
node->PreviousLevel = 0;
// remove from level bucket
if (_bucket[level] == nodeIdx)
int nextIdx = _spareMemory->NextLevel;
if (nextIdx != 0)
{
_bucket[level] = nextLevelIdx;
if (nextLevelIdx != 0)
{
Metadata* nextLevel = &_spareMemory[-nextLevelIdx];
nextLevel->PreviousLevel = 0;
}
} else
{
if (nextLevelIdx != 0)
{
Metadata* nextLevel = &_spareMemory[-nextLevelIdx];
nextLevel->PreviousLevel = prevLevelIdx;
}
if (prevLevelIdx != 0)
{
Metadata* prevLevel = &_spareMemory[-prevLevelIdx];
prevLevel->NextLevel = nextLevelIdx;
}
node->PreviousLevel = 0;
node->NextLevel = 0;
Metadata* next = &_spareMemory[-nextIdx];
next->PreviousLevel = nodeIdx;
}
node->NextLevel = nextIdx;
_spareMemory->NextLevel = nodeIdx;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private void RemoveFromBucket(
Metadata* node,
int nodeIdx
)
{
RemoveFromBucket(GetLevel(node->Size), node, nodeIdx);
}
private void RemoveFromBucket(
int level,
Metadata* node,
int nodeIdx
)
{
int currentLevel = level;
int currentNextLevelIdx = node->NextLevel;
int currentPrevLevelIdx = node->PreviousLevel;
if (_bucket[currentLevel] == nodeIdx)
{
_bucket[currentLevel] = currentNextLevelIdx;
} else if (currentPrevLevelIdx != 0)
{
Metadata* prevLevel = &_spareMemory[-currentPrevLevelIdx];
prevLevel->NextLevel = currentNextLevelIdx;
}
if (currentNextLevelIdx != 0)
{
Metadata* nextLevel = &_spareMemory[-currentNextLevelIdx];
nextLevel->PreviousLevel = currentPrevLevelIdx;
}
node->PreviousLevel = 0;
node->NextLevel = 0;
}
// Assuming node to remove is head f bucket
private void RemoveFromBucketHead(
int level,
Metadata* node,
int nodeIdx
)
{
int currentLevel = level;
int currentNextLevelIdx = node->NextLevel;
// int currentPrevLevelIdx = 0;
if (_bucket[currentLevel] == nodeIdx)
{
_bucket[currentLevel] = currentNextLevelIdx;
}
/*
else if (currentPrevLevelIdx != 0)
{
Metadata* prevLevel = &_spareMemory[-currentPrevLevelIdx];
prevLevel->NextLevel = currentNextLevelIdx;
}
/**/
if (currentNextLevelIdx != 0)
{
Metadata* nextLevel = &_spareMemory[-currentNextLevelIdx];
nextLevel->PreviousLevel = 0;
}
node->PreviousLevel = 0;
node->NextLevel = 0;
}
private bool TryAllocNewChunk(
@@ -228,34 +275,49 @@ namespace StgSharp.HighPerformance.Memory
)
{
int idx = _spareMemory->NextLevel;
ulong spareSize = _spareMemory->Size;
if (idx != 0)
{
if (spareSize < size)
{
meta = null;
return false;
}
meta = &_spareMemory[-idx];
_spareMemory->NextLevel = meta->NextLevel;
int nextSpareIdx = meta->NextLevel;
if (nextSpareIdx != 0)
{
Metadata* nextSpare = &_spareMemory[-nextSpareIdx];
nextSpare->PreviousLevel = 0;
}
_spareMemory->NextLevel = nextSpareIdx;
} else
{
if (spareSize < Metadata.SizeOf + size)
{
meta = null;
return false;
}
_metadataEnd++;
meta = &_spareMemory[-_metadataEnd];
// TODO: correctly set position and size for new chunk
idx = _metadataEnd;
meta = &_spareMemory[-idx];
spareSize -= Metadata.SizeOf;
}
ulong spareSize = _spareMemory->Size;
if (spareSize < Metadata.SizeOf + size)
{
meta = null;
return false;
}
spareSize -= Metadata.SizeOf + size;
spareSize -= size;
meta->Size = size;
meta->Position = _spareMemory->Position;
meta->State = AllocationState.Free;
meta->PreviousLevel = 0;
meta->NextLevel = 0;
_spareMemory->Size = spareSize;
_spareMemory->Position += size;
meta->Size = size;
// insert the new chunk to position chain
int nearIdx = _spareMemory->PreviousNear;
Metadata* near = &_spareMemory[-nearIdx];
near->NextNear = _metadataEnd;
near->NextNear = idx;
meta->PreviousNear = nearIdx;
_spareMemory->PreviousNear = _metadataEnd;
_spareMemory->PreviousNear = idx;
meta->NextNear = 0;
return true;
}
@@ -277,6 +339,44 @@ namespace StgSharp.HighPerformance.Memory
return true;
}
private bool TryTakeBucketEntry(
ulong minimumSize,
out Metadata* entry
)
{
int level = GetLevel(minimumSize);
// first try to find in the target level
{
int idx = _bucket[level];
while (idx != 0)
{
Metadata* candidate = &_spareMemory[-idx];
if (candidate->Size >= minimumSize)
{
RemoveFromBucket(level, candidate, idx);
entry = candidate;
return true;
}
idx = candidate->NextLevel;
}
}
for (int i = level + 1; i < _bucket.Length; i++)
{
int idx = _bucket[i];
if (idx != 0)
{
Metadata* candidate = &_spareMemory[-idx];
RemoveFromBucketHead(i, candidate, idx);
entry = candidate;
return true;
}
}
entry = null;
return false;
}
[StructLayout(LayoutKind.Explicit)]
public readonly struct Handle
{
@@ -324,8 +424,8 @@ namespace StgSharp.HighPerformance.Memory
internal struct Metadata
{
[FieldOffset(24)] private ulong PositionMask;
[FieldOffset(16)] private ulong SizeMask;
[FieldOffset(16)] private ulong PositionMask;
[FieldOffset(24)] private ulong SizeMask;
[FieldOffset(30)] internal AllocationState State;
[FieldOffset(31)] internal sbyte Level;
@@ -367,23 +467,15 @@ namespace StgSharp.HighPerformance.Memory
if (allocSize > _maxAllocSize) {
throw new OverflowException("Requested allocation size exceeds the maximum block size.");
}
int level = GetLevel(allocSize);
Metadata* entry = null;
for (int i = level; i < _bucket.Length; i++)
{
int idx = _bucket[i];
if (idx != 0)
{
entry = &_spareMemory[-idx];
_bucket[i] = entry->NextLevel;
break;
}
}
if (entry is null && !TryAllocNewChunk(allocSize, out entry)) {
if (!TryTakeBucketEntry(allocSize, out Metadata* entry) &&
!TryAllocNewChunk(allocSize, out entry)) {
throw new OverflowException("No available ");
}
int entryIdx = (int)(_spareMemory - entry);
ulong entrySize = entry->Size;
if (entrySize < allocSize) {
throw new OverflowException("Bucket returned an undersized block.");
}
ulong remain = entrySize - allocSize;
// slice tail
@@ -392,18 +484,17 @@ namespace StgSharp.HighPerformance.Memory
int newEntryIdx = (int)(_spareMemory - newEntry);
newEntry->Position = entry->Position + allocSize;
newEntry->Size = remain;
newEntry->State = AllocationState.Free;
int newLevel = GetLevel(remain);
int bucketHeadIdx = _bucket[newLevel];
if (bucketHeadIdx != 0)
{
// if there is already an entry in the bucket, link it to the new entry
Metadata* bucketHead = &_spareMemory[-bucketHeadIdx];
newEntry->NextLevel = bucketHeadIdx;
bucketHead->PreviousLevel = newEntryIdx;
} else
{
newEntry->NextLevel = 0;
}
newEntry->NextLevel = bucketHeadIdx;
newEntry->PreviousLevel = 0;
_bucket[newLevel] = newEntryIdx;
@@ -416,6 +507,9 @@ namespace StgSharp.HighPerformance.Memory
newEntry->PreviousNear = entryIdx;
}
entry->State = AllocationState.Alloc;
if (entry->Position - (ulong)_basePtr > _totalSize) {
throw new OverflowException("Allocated memory address exceeds the maximum block size.");
}
return new Handle(entry, _basePtr);
}
@@ -431,17 +525,34 @@ namespace StgSharp.HighPerformance.Memory
Metadata* current = &_spareMemory[-currentIdx];
if (current->State == AllocationState.Free)
{
// scan next
Ptr64 size = 0;
int nextIdx = current->NextNear;
Metadata* next = &_spareMemory[-nextIdx];
while (nextIdx != 0 && next->State != AllocationState.Free)
while (nextIdx != 0)
{
Metadata* next = &_spareMemory[-nextIdx];
if (next->State != AllocationState.Free)
{
break;
}
size += next->Size;
RecycleNode(next);
nextIdx = next->NextNear;
nextIdx = current->NextNear;
}
if (size > 0) {
if (size > 0)
{
current->Size += size;
RemoveFromBucket(current, currentIdx);
int newLevel = GetLevel(current->Size);
int bucketHeadIdx = _bucket[newLevel];
if (bucketHeadIdx != 0)
{
Metadata* bucketHead = &_spareMemory[-bucketHeadIdx];
bucketHead->PreviousLevel = currentIdx;
}
current->NextLevel = bucketHeadIdx;
current->PreviousLevel = 0;
_bucket[newLevel] = currentIdx;
}
}
currentIdx = current->NextNear;
@@ -493,8 +604,10 @@ namespace StgSharp.HighPerformance.Memory
// recycle to level;
head->Size = size;
head->State = AllocationState.Free;
head->PreviousLevel = 0;
int level = GetLevel(head->Size);
int bucketHeadIdx = _bucket[level];
head->NextLevel = 0;
if (bucketHeadIdx != 0)
{
Metadata* bucketHead = &_spareMemory[-bucketHeadIdx];
@@ -516,25 +629,19 @@ namespace StgSharp.HighPerformance.Memory
h = default;
return false;
}
int level = GetLevel(allocSize);
Metadata* entry = null;
for (int i = level; i < _bucket.Length; i++)
{
int idx = _bucket[i];
if (idx != 0)
{
entry = &_spareMemory[-idx];
_bucket[i] = entry->NextLevel;
break;
}
}
if (entry is null && !TryAllocNewChunk(allocSize, out entry))
if (!TryTakeBucketEntry(allocSize, out Metadata* entry) &&
!TryAllocNewChunk(allocSize, out entry))
{
h = default;
return false;
}
int entryIdx = (int)(_spareMemory - entry);
ulong entrySize = entry->Size;
if (entrySize < allocSize)
{
h = default;
return false;
}
ulong remain = entrySize - allocSize;
// slice tail
@@ -543,6 +650,7 @@ namespace StgSharp.HighPerformance.Memory
int newEntryIdx = (int)(_spareMemory - newEntry);
newEntry->Position = entry->Position + allocSize;
newEntry->Size = remain;
newEntry->State = AllocationState.Free;
int newLevel = GetLevel(remain);
int bucketHeadIdx = _bucket[newLevel];
if (bucketHeadIdx != 0)
@@ -566,6 +674,12 @@ namespace StgSharp.HighPerformance.Memory
entry->NextNear = newEntryIdx;
newEntry->PreviousNear = entryIdx;
}
entry->State = AllocationState.Alloc;
if (entry->Position - (ulong)_basePtr > _totalSize)
{
h = default;
throw new OverflowException("Allocated memory address exceeds the maximum block size.");
}
h = new Handle(entry, _basePtr);
return true;
}