HLSf use nuint rather than long, L4 now uses hlsf rather than array to alloc cache line.

This commit is contained in:
Youmu Liu
2026-04-20 18:10:14 +08:00
parent 1b7c929be4
commit 2e2d64a2ce
38 changed files with 796 additions and 465 deletions
@@ -1,30 +1,27 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator.cs"
// file="HLSFAllocator"
// 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 in the Software without restriction,
// including without limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of the Software,
// and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and
// this permission notice shall be included in all copies
// or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”,
// WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
// INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH
// THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
@@ -33,9 +30,9 @@ using System.Buffers;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using hlsfHandle = StgSharp.Mathematics.Memory.hlsfHandle;
using hlsfHandle = StgSharp.HighPerformance.Memory.HlsfHandle;
namespace StgSharp.Mathematics.Memory
namespace StgSharp.HighPerformance.Memory
{
public unsafe partial class HybridLayerSegregatedFitAllocator : IDisposable
{
@@ -50,32 +47,36 @@ namespace StgSharp.Mathematics.Memory
private readonly Action<nuint> _freeHandle;
private bool disposedValue;
private readonly int _align;
private readonly long _maxSize;
private readonly nuint _maxSize;
private readonly nuint _size;
private readonly SlabAllocator<Entry> _entries;
internal HybridLayerSegregatedFitAllocator(
nuint byteSize,
int align,
long maxBlockSize,
nuint maxBlockSize,
Func<nuint, int, nuint> allocator,
Action<nuint> freeHandle,
SlabAllocator<Entry>? entryManager)
: this(byteSize, align, maxBlockSize, (void*)allocator(byteSize, align), freeHandle, entryManager) { }
SlabAllocator<Entry>? entryManager
)
: this(byteSize, align, maxBlockSize, (void*)allocator(byteSize, align), freeHandle,
entryManager) { }
internal HybridLayerSegregatedFitAllocator(
nuint byteSize,
int align,
long maxBlockSize,
nuint maxBlockSize,
void* externalPointer,
Action<nuint> freeHandle,
SlabAllocator<Entry>? entryManager = null)
SlabAllocator<Entry>? entryManager = null
)
{
if (align < 16 || (align & (align - 1)) != 0) {
if ((align < 16) || ((align & (align - 1)) != 0)) {
throw new ArgumentException(
"Alignment must be a power of two and at least 16 bytes.");
}
_entries = entryManager ?? SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
_entries = entryManager ??
SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
m_Buffer = (byte*)externalPointer;
_align = align;
_size = byteSize;
@@ -85,13 +86,13 @@ namespace StgSharp.Mathematics.Memory
_spareMemory->PreviousNear = _spareMemory;
_spareMemory->NextNear = _spareMemory;
_spareMemory->Position = (nuint)m_Buffer;
_spareMemory->Size = (long)byteSize;
_spareMemory->Size = byteSize;
int levelCountOrigin = _levelSizeArray.Length;
int levelCount = GetLevelFromSize(maxBlockSize) + 1;
Array.Resize(ref _levelSizeArray, levelCount);
_bucketHeads = new BucketNode*[(levelCount * 3) + 1];
int size = _levelSizeArray[^1];
uint size = _levelSizeArray[^1];
for (int i = levelCountOrigin; i < levelCount; i++)
{
size *= 4;
@@ -101,22 +102,37 @@ namespace StgSharp.Mathematics.Memory
_freeHandle = freeHandle ?? (static _ => { });
}
public HybridLayerSegregatedFitAllocator(nuint byteSize)
: this(
byteSize, 16, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(
nuint byteSize
)
: this(byteSize, 16, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16),
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(nuint byteSize, uint maxBlockSize)
: this(
byteSize, 16, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(
nuint byteSize,
uint maxBlockSize
)
: this(byteSize, 16, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16),
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align)
: this(
byteSize, align, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(
nuint byteSize,
int align
)
: this(byteSize, align, 32U * 1024U * 1024U, NativeMemory.AlignedAlloc(byteSize, 16),
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align, long maxBlockSize)
: this(byteSize, align, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16), ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public HybridLayerSegregatedFitAllocator(
nuint byteSize,
int align,
nuint maxBlockSize
)
: this(byteSize, align, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16),
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
public partial hlsfHandle Alloc(long size);
public partial HlsfHandle Alloc(
nuint size
);
public partial void Collect();
@@ -126,7 +142,9 @@ namespace StgSharp.Mathematics.Memory
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool disposing)
protected virtual void Dispose(
bool disposing
)
{
if (!disposedValue)
{