working on L4's tlsf

This commit is contained in:
Youmu Liu
2026-05-14 20:15:07 +08:00
parent c9fb11b47b
commit b87943495d
5 changed files with 126 additions and 139 deletions
+13 -14
View File
@@ -42,7 +42,7 @@ namespace StgSharp.HighPerformance.Memory
private const int PredictionLeastAhead = 1024;
// private static readonly IIntrinsicKernel _intrinsic = IIntrinsicKernel.Instance;
private readonly CacheLineHead* _head;
private readonly CacheLineMetadata* _head;
private readonly MapArray* _mapCount;
private readonly PredictionArray* _predictCount;
private readonly CacheLineDescription* _prediction;
@@ -69,31 +69,30 @@ namespace StgSharp.HighPerformance.Memory
_bindedNuma = bindedNuma;
// init order is fallow by align requirement of each component.
nuint entrySize = TLSF.RequestedAllocation; // SIMDID, no less than 16 byte bit
nuint headSize = ((nuint)Unsafe.SizeOf<CacheLineHead>()) * CacheLineCount; // 16 byte align
nuint headSize = ((nuint)Unsafe.SizeOf<CacheLineMetadata>()) * CacheLineCount; // 16 byte align
nuint predictionSize = ((nuint)Unsafe.SizeOf<CacheLineDescription>()) * PredictionCount; //16 byte align
nuint mapSize = SwissTable.RequestedNativeMemorySize; // 16 byte align
nuint mapCountSize = SwissTable.RequestedNativeMemorySize; // 16 byte align
nuint predictionCountSize = (nuint)Unsafe.SizeOf<PredictionArray>(); // 16 byte align
nuint normalizedBufferSize = (bufferSize + (PageSize - 1)) & (~(PageSize - 1)); // align to 4096 byte, page size
ByteCapacity = normalizedBufferSize;
AllocSize = headSize + mapCountSize + predictionSize + (predictionCountSize * 2) + normalizedBufferSize;
// malloc all memory at once, and free at once in Dispose.
// This is to avoid fragmentation and improve performance.
_baseAddress = (nuint)Numa.Alloc(bindedNuma, entrySize + headSize + mapSize + predictionSize + (predictionCountSize * 2) + normalizedBufferSize);
_baseAddress = (nuint)Numa.Alloc(bindedNuma, AllocSize);
nuint ptr = _baseAddress;
AllocSize = entrySize;
// init all components with the buffer, and move the buffer pointer forward.
nuint _buffer = _baseAddress + normalizedBufferSize;
nuint _buffer = _baseAddress;
_bufferAllocator = new TLSF(bufferSize, maxCacheLineSize, (void*)_baseAddress);
ptr += entrySize;
_head = (CacheLineHead*)ptr;
_buffer += normalizedBufferSize;
_head = (CacheLineMetadata*)ptr;
ptr += headSize;
_prediction = (CacheLineDescription*)ptr;
ptr += predictionSize;
_map = SwissTable.Create(ptr, _ => { });
ptr += mapSize;
ptr += mapCountSize;
_predictCount = (PredictionArray*)ptr;
ptr += predictionCountSize;
_mapCount = (MapArray*)ptr;
@@ -101,7 +100,7 @@ namespace StgSharp.HighPerformance.Memory
M512* mPtr = (M512*)_head;
for (uint i = 0; i < CacheLineCount; i++) {
mPtr->BroadCastFrom<ulong>(i);
mPtr->BroadCastFrom<ulong>(0);
}
}
@@ -141,7 +140,7 @@ namespace StgSharp.HighPerformance.Memory
// init order is fallow by align requirement of each component.
nuint entrySize = TLSF.RequestedAllocation; // SIMDID, no less than 16 byte bit
nuint headSize = ((nuint)Unsafe.SizeOf<CacheLineHead>()) * CacheLineCount; // 16 byte align
nuint headSize = ((nuint)Unsafe.SizeOf<CacheLineMetadata>()) * CacheLineCount; // 16 byte align
nuint predictionSize = ((nuint)Unsafe.SizeOf<CacheLineDescription>()) * PredictionCount; //16 byte align
nuint mapSize = SwissTable.RequestedNativeMemorySize; // 16 byte align
nuint predictionCountSize = (nuint)Unsafe.SizeOf<PredictionArray>(); // 16 byte align
@@ -178,7 +177,7 @@ namespace StgSharp.HighPerformance.Memory
if (_map.TryGet(prediction.GetHashCodeLong(), out nuint entryHandle))
{
// cache hit
CacheLineHead* entry = (CacheLineHead*)entryHandle;
CacheLineMetadata* entry = (CacheLineMetadata*)entryHandle;
if (Interlocked.Increment(ref entry->RefCount) > 0)
{
(*_predictCount)[entry->Id] = 0;