Remove hlsf, TLSF.alloc is ok
This commit is contained in:
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="TwoLayerSegregatedFitAllocator"
|
||||
// Project: StgSharp
|
||||
// AuthorGroup: Nitload Space
|
||||
// Copyright (c) Nitload Space. All rights reserved.
|
||||
// AuthorGroup: Nitload
|
||||
// Copyright (c) Nitload. All rights reserved.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -29,6 +29,7 @@ using StgSharp.HighPerformance.Memory;
|
||||
using StgSharp.HighPerformance.ProcessorAbstraction;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Numerics;
|
||||
using System.Reflection.Metadata;
|
||||
@@ -44,13 +45,13 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
private const ulong Mask48Bits = 0x0000FFFFFFFFFFFF;
|
||||
|
||||
private readonly Metadata*[] _bucket;
|
||||
private readonly int[] _bucket;
|
||||
private readonly Metadata* _spareMemory;
|
||||
private readonly Action<nuint> _freeHandle;
|
||||
private bool disposedValue;
|
||||
private readonly int _align;
|
||||
|
||||
private int _allocEnd;
|
||||
private int _metadataEnd;
|
||||
private readonly Ptr64 _basePtr;
|
||||
private readonly ulong _maxAllocSize;
|
||||
private readonly ulong _totalSize;
|
||||
@@ -60,7 +61,8 @@ namespace StgSharp.HighPerformance.Memory
|
||||
int align,
|
||||
ulong maxBlockSize,
|
||||
Func<nuint, int, nuint> allocator,
|
||||
Action<nuint> freeHandle)
|
||||
Action<nuint> freeHandle
|
||||
)
|
||||
: this(byteSize, align, maxBlockSize, (void*)allocator(byteSize, align), freeHandle) { }
|
||||
|
||||
internal TwoLayerSegregatedFitAllocator(
|
||||
@@ -68,16 +70,17 @@ namespace StgSharp.HighPerformance.Memory
|
||||
int align,
|
||||
ulong maxBlockSize,
|
||||
void* externalPointer,
|
||||
Action<nuint> freeHandle)
|
||||
Action<nuint> freeHandle
|
||||
)
|
||||
{
|
||||
_totalSize = byteSize;
|
||||
_basePtr = (Ptr64)(ulong)externalPointer;
|
||||
_align = align;
|
||||
_maxAllocSize = ulong.Max(64, (~64UL) & (ulong.Min(maxBlockSize + 63, Mask48Bits)));
|
||||
_bucket = new Metadata*[58 - BitOperations.LeadingZeroCount(_maxAllocSize)];
|
||||
_bucket = new int[GetLevel(_maxAllocSize) + 1];
|
||||
_spareMemory = &((Metadata*)(ulong)(_basePtr + _totalSize))[-1];
|
||||
_freeHandle = freeHandle ?? (static_ => { });
|
||||
_allocEnd = 0;
|
||||
_metadataEnd = 0;
|
||||
|
||||
// init spare memory node
|
||||
|
||||
@@ -89,59 +92,128 @@ namespace StgSharp.HighPerformance.Memory
|
||||
_spareMemory->PreviousNear = 0;
|
||||
}
|
||||
|
||||
public TwoLayerSegregatedFitAllocator(nuint byteSize)
|
||||
public TwoLayerSegregatedFitAllocator(
|
||||
nuint byteSize
|
||||
)
|
||||
: this(byteSize, 16, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16),
|
||||
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
public TwoLayerSegregatedFitAllocator(nuint byteSize, uint maxBlockSize)
|
||||
public TwoLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
uint maxBlockSize
|
||||
)
|
||||
: this(byteSize, 16, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16),
|
||||
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
public TwoLayerSegregatedFitAllocator(nuint byteSize, int align)
|
||||
public TwoLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align
|
||||
)
|
||||
: this(byteSize, align, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16),
|
||||
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
public TwoLayerSegregatedFitAllocator(nuint byteSize, int align, nuint maxBlockSize)
|
||||
public TwoLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
nuint maxBlockSize
|
||||
)
|
||||
: this(byteSize, align, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16),
|
||||
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
public Handle Alloc(nuint size)
|
||||
public Handle Alloc(
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
ulong allocSize = nuint.Max(64, (~64U) & (size + 63));
|
||||
ulong allocSize = ulong.Max(64, (~63UL) & ((ulong)size + 63));
|
||||
if (allocSize > _maxAllocSize) {
|
||||
throw new OverflowException("Requested allocation size exceeds the maximum block size.");
|
||||
}
|
||||
int level = 57 - BitOperations.LeadingZeroCount(allocSize);
|
||||
int level = GetLevel(allocSize);
|
||||
Metadata* entry = null;
|
||||
for (int i = level; i < _bucket.Length; i++)
|
||||
{
|
||||
entry = _bucket[i];
|
||||
if (entry is not null)
|
||||
int idx = _bucket[i];
|
||||
if (idx != 0)
|
||||
{
|
||||
_bucket[i] = entry->NextLevel != 0 ? &_spareMemory[-entry->NextLevel] : null;
|
||||
entry = &_spareMemory[-idx];
|
||||
_bucket[i] = entry->NextLevel;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (entry is null && !TryGetNewMetadata(allocSize, out entry)) {
|
||||
if (entry is null && !TryAllocNewChunk(allocSize, out entry)) {
|
||||
throw new OverflowException("No available ");
|
||||
}
|
||||
ulong remain = entry->Size - allocSize;
|
||||
int entryIdx = (int)(_spareMemory - entry);
|
||||
ulong entrySize = entry->Size;
|
||||
ulong remain = entrySize - allocSize;
|
||||
|
||||
throw new NotImplementedException();
|
||||
// slice tail
|
||||
if (remain > (entrySize >> 7) && TryGetNewMetadata(out Metadata* newEntry))
|
||||
{
|
||||
int newEntryIdx = (int)(_spareMemory - newEntry);
|
||||
newEntry->Position = entry->Position + allocSize;
|
||||
newEntry->Size = remain;
|
||||
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->PreviousLevel = 0;
|
||||
_bucket[newLevel] = newEntryIdx;
|
||||
|
||||
// push to position chain
|
||||
int afterIdx = entry->NextNear;
|
||||
Metadata* after = &_spareMemory[-afterIdx];
|
||||
newEntry->NextNear = afterIdx;
|
||||
after->PreviousNear = newEntryIdx;
|
||||
entry->NextNear = newEntryIdx;
|
||||
newEntry->PreviousNear = entryIdx;
|
||||
}
|
||||
return new Handle(entry, _basePtr);
|
||||
}
|
||||
|
||||
public void Collect() { }
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public void Free(Handle handle)
|
||||
public void Free(
|
||||
Handle handle,
|
||||
int collectScanWindow = 4
|
||||
)
|
||||
{
|
||||
Metadata* head = handle._meta;
|
||||
for (int i = 0; i < collectScanWindow; i++)
|
||||
{
|
||||
// forward scan
|
||||
}
|
||||
for (int i = 0; i < collectScanWindow; i++)
|
||||
{
|
||||
// backward scan
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryAlloc(
|
||||
nuint size,
|
||||
out Handle h
|
||||
)
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
@@ -153,7 +225,18 @@ namespace StgSharp.HighPerformance.Memory
|
||||
}
|
||||
}
|
||||
|
||||
private bool TryGetNewMetadata(ulong size, out Metadata* meta)
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static int GetLevel(
|
||||
ulong size
|
||||
)
|
||||
{
|
||||
return 57 - BitOperations.LeadingZeroCount(size);
|
||||
}
|
||||
|
||||
private bool TryAllocNewChunk(
|
||||
ulong size,
|
||||
out Metadata* meta
|
||||
)
|
||||
{
|
||||
int idx = _spareMemory->NextLevel;
|
||||
if (idx != 0)
|
||||
@@ -163,29 +246,57 @@ namespace StgSharp.HighPerformance.Memory
|
||||
return true;
|
||||
}
|
||||
ulong spareSize = _spareMemory->Size;
|
||||
if (spareSize < 32UL + size)
|
||||
if (spareSize < Metadata.SizeOf + size)
|
||||
{
|
||||
meta = null;
|
||||
return false;
|
||||
}
|
||||
_allocEnd++;
|
||||
meta = &_spareMemory[-_allocEnd];
|
||||
spareSize -= 32UL - size;
|
||||
_metadataEnd++;
|
||||
meta = &_spareMemory[-_metadataEnd];
|
||||
spareSize -= Metadata.SizeOf + size;
|
||||
_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;
|
||||
meta->PreviousNear = nearIdx;
|
||||
_spareMemory->PreviousNear = _metadataEnd;
|
||||
meta->NextNear = 0;
|
||||
return true;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private bool TryGetNewMetadata(
|
||||
out Metadata* meta
|
||||
)
|
||||
{
|
||||
ulong spareSize = _spareMemory->Size;
|
||||
if (spareSize < Metadata.SizeOf)
|
||||
{
|
||||
meta = null;
|
||||
return false;
|
||||
}
|
||||
_metadataEnd++;
|
||||
meta = &_spareMemory[-_metadataEnd];
|
||||
spareSize -= Metadata.SizeOf;
|
||||
_spareMemory->Size = spareSize;
|
||||
return true;
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
public readonly struct Handle
|
||||
{
|
||||
|
||||
internal readonly Metadata* _meta;
|
||||
public readonly void* Pointer;
|
||||
public readonly Ptr64 Size;
|
||||
[FieldOffset(0)]internal readonly Metadata* _meta;
|
||||
[FieldOffset(8)]public readonly void* Pointer;
|
||||
[FieldOffset(16)]public readonly Ptr64 Size;
|
||||
|
||||
internal Handle(Metadata* meta, Ptr64 baseAddress)
|
||||
internal Handle(
|
||||
Metadata* meta,
|
||||
Ptr64 baseAddress
|
||||
)
|
||||
{
|
||||
_meta = meta;
|
||||
Pointer = (void*)(ulong)(meta->Position + baseAddress);
|
||||
@@ -195,7 +306,26 @@ namespace StgSharp.HighPerformance.Memory
|
||||
[Obsolete("You should not create instance from default. Alloc from allocator please")]
|
||||
public Handle() { }
|
||||
|
||||
public ref byte this[int offset] => ref ((byte*)Pointer)[offset];
|
||||
public ref byte this[
|
||||
int offset
|
||||
] => ref ((byte*)Pointer)[offset];
|
||||
|
||||
public Span<byte> AsSpan(
|
||||
int range = -1
|
||||
)
|
||||
{
|
||||
return new Span<byte>(Pointer, range < 0 ? (int)(ulong)Size : range);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
internal enum AllocationState : short
|
||||
{
|
||||
|
||||
Free = 0,
|
||||
Used = 1,
|
||||
Split = 2,
|
||||
Merged = 3
|
||||
|
||||
}
|
||||
|
||||
@@ -206,18 +336,17 @@ namespace StgSharp.HighPerformance.Memory
|
||||
[FieldOffset(24)] private ulong PositionMask;
|
||||
[FieldOffset(16)] private ulong SizeMask;
|
||||
|
||||
[FieldOffset(30)] internal AllocationState State;
|
||||
|
||||
[FieldOffset(0)] public int NextLevel;
|
||||
[FieldOffset(4)] public int NextNear;
|
||||
[FieldOffset(8)] public int PreviousLevel;
|
||||
[FieldOffset(12)] public int PreviousNear;
|
||||
|
||||
[FieldOffset(22)] public ushort Mask1;
|
||||
[FieldOffset(30)] public ushort Mask2;
|
||||
|
||||
public Ptr64 Position
|
||||
{
|
||||
readonly get => (Ptr64)(PositionMask & Mask48Bits);
|
||||
set => PositionMask = (PositionMask & ~Mask48Bits) | ((ulong)value & Mask48Bits);
|
||||
readonly get => PositionMask;
|
||||
set => PositionMask = value;
|
||||
}
|
||||
|
||||
public Ptr64 Size
|
||||
@@ -228,7 +357,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
public Ptr64 SizeForSpare
|
||||
{
|
||||
get => SizeMask;
|
||||
readonly get => SizeMask;
|
||||
set => SizeMask = value;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user