312 lines
12 KiB
C#
312 lines
12 KiB
C#
//-----------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
// file="HLSFAllocator.Alloc"
|
|
// Project: StgSharp
|
|
// 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.
|
|
//
|
|
// -----------------------------------------------------------------------
|
|
// -----------------------------------------------------------------------
|
|
using System;
|
|
using System.Numerics;
|
|
|
|
using hlsfHandle = StgSharp.HighPerformance.Memory.HybridLayerSegregatedFitAllocationHandle;
|
|
|
|
namespace StgSharp.HighPerformance.Memory
|
|
{
|
|
public unsafe partial class HybridLayerSegregatedFitAllocator
|
|
{
|
|
|
|
public hlsfHandle Alloc(uint size)
|
|
{
|
|
ArgumentOutOfRangeException.ThrowIfGreaterThan<nuint>(size, (nuint)levelSizeArray[^1] * 2);
|
|
int level = GetLevelFromSize(size);
|
|
int segmentIndex = DetermineSegmentIndex(size, level);
|
|
|
|
int i = level, s = segmentIndex;
|
|
Entry* handle;
|
|
while (!TryPopLevelForAllocation(i, s, out handle))
|
|
{
|
|
if (++s >= 3)
|
|
{
|
|
s = 0;
|
|
i++;
|
|
}
|
|
if (i >= levelSizeArray.Length)
|
|
{
|
|
if (TryAllocateNew16MBBlock(out handle))
|
|
{
|
|
break;
|
|
}
|
|
throw new OverflowException("Out of memory: Unable to allocate new 16MB block or find suitable bucket for allocation.");
|
|
}
|
|
}
|
|
|
|
if (handle != null)
|
|
{
|
|
int requestedOffset = (segmentIndex + 1) * levelSizeArray[level];
|
|
if ((ulong)requestedOffset > handle->Size)
|
|
{
|
|
// spare返回的块不足以切出目标大小,避免进入切片导致下溢/越界
|
|
throw new OverflowException("Out of memory: insufficient block size returned from spare memory.");
|
|
}
|
|
Entry* extra = (Entry*)_entries.Allocate();
|
|
extra->Position = handle->Position + (nuint)requestedOffset;
|
|
extra->Size = handle->Size - (uint)requestedOffset;
|
|
extra->State = EntryState.Empty;
|
|
extra->NextNear = handle->NextNear;
|
|
extra->PreviousNear = handle;
|
|
extra->NextNear->PreviousNear = extra;
|
|
extra->PreviousNear->NextNear = extra;
|
|
SliceMemory(extra);
|
|
}
|
|
handle->State = EntryState.Allocated;
|
|
handle->Level = level;
|
|
return new hlsfHandle(handle, size);
|
|
}
|
|
|
|
private void SliceMemory(Entry* e)
|
|
{
|
|
Entry* current = e;
|
|
Entry* next = e->NextNear;
|
|
ulong originOffset = e->Position - (ulong)m_Buffer;
|
|
ulong finalOffset = originOffset;
|
|
ulong end = e->Position + e->Size;
|
|
ulong endOffset = end - (ulong)m_Buffer;
|
|
ulong remainSize = e->Size;
|
|
|
|
|
|
if (finalOffset >= endOffset || remainSize == 0) {
|
|
return;
|
|
}
|
|
|
|
// get level of left edge
|
|
int minLevel = int.Max((BitOperations.TrailingZeroCount(finalOffset) - 6) / 2, 0);
|
|
int i = minLevel >= levelSizeArray.Length ? levelSizeArray.Length - 1 : minLevel;
|
|
|
|
for (; i < levelSizeArray.Length; i++)
|
|
{
|
|
int levelSize = levelSizeArray[i];
|
|
ulong levelLong = (ulong)levelSize;
|
|
ulong levelUpper = levelLong * 4;
|
|
|
|
// smallest board that is larger than finalOffset
|
|
ulong board = (finalOffset + levelUpper - 1) / levelUpper * levelUpper;
|
|
long span = (long)ulong.Min(board, endOffset) - (long)finalOffset;
|
|
int blockCount = (span <= 0) ? 0 : (int)(span / (long)levelLong);
|
|
if (blockCount == 0)
|
|
{
|
|
continue;
|
|
}
|
|
int s = blockCount * levelSize;
|
|
|
|
// create and init new entry
|
|
Entry* newEntry = (Entry*)_entries.Allocate();
|
|
newEntry->State = EntryState.Empty;
|
|
newEntry->Position = (nuint)m_Buffer + (nuint)finalOffset;
|
|
newEntry->Size = (uint)s;
|
|
BucketNode* bucket = (BucketNode*)newEntry->Position;
|
|
bucket->EntryRef = newEntry;
|
|
newEntry->Level = i;
|
|
|
|
// link new entry
|
|
newEntry->PreviousNear = current;
|
|
newEntry->NextNear = next;
|
|
current->NextNear = newEntry;
|
|
next->PreviousNear = newEntry;
|
|
|
|
// set new entry
|
|
current = newEntry;
|
|
|
|
PushLevel(i, Math.Min(blockCount - 1, 2), bucket);
|
|
|
|
finalOffset += (ulong)s;
|
|
remainSize -= (ulong)s;
|
|
if (remainSize <= (ulong)levelSize)
|
|
{
|
|
break;
|
|
}
|
|
if (remainSize == 0) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
if (i >= 10)
|
|
{
|
|
int levelSize = levelSizeArray[^1] * 4;
|
|
ulong levelLong = (ulong)levelSize;
|
|
ulong board = endOffset / levelLong * levelLong;
|
|
long span = (long)board - (long)finalOffset;
|
|
int blockCount = span > 0 ? (int)(span / (long)levelSize) : 0;
|
|
if (blockCount > 0)
|
|
{
|
|
int s = blockCount * levelSize;
|
|
Entry* newEntry = (Entry*)_entries.Allocate();
|
|
newEntry->State = EntryState.Empty;
|
|
newEntry->Position = (nuint)((ulong)m_Buffer + finalOffset);
|
|
newEntry->Size = (uint)s;
|
|
BucketNode* bucket = (BucketNode*)newEntry->Position;
|
|
bucket->EntryRef = newEntry;
|
|
newEntry->Level = 10;
|
|
|
|
// link new entry
|
|
newEntry->PreviousNear = current;
|
|
newEntry->NextNear = next;
|
|
current->NextNear = newEntry;
|
|
if (next != null) {
|
|
next->PreviousNear = newEntry;
|
|
}
|
|
|
|
newEntry->Level = i;
|
|
PushLevel(i, Math.Min(blockCount - 1, 2), bucket);
|
|
|
|
finalOffset += (ulong)s;
|
|
remainSize -= (ulong)s;
|
|
}
|
|
if (remainSize == 0) {
|
|
return;
|
|
}
|
|
}
|
|
|
|
for (i -= 1; i >= 0; i--)
|
|
{
|
|
int levelSize = levelSizeArray[i];
|
|
ulong levelLong = (ulong)levelSize;
|
|
ulong board = endOffset / levelLong * levelLong;
|
|
long span = (long)board - (long)finalOffset;
|
|
int blockCount = span > 0 ? (int)(span / levelSize) : 0;
|
|
if (blockCount <= 0)
|
|
{
|
|
continue;
|
|
}
|
|
int s = blockCount * levelSize;
|
|
System.Diagnostics.Debug.Assert(finalOffset + (ulong)s <= endOffset);
|
|
|
|
// create and init new entry
|
|
Entry* newEntry = (Entry*)_entries.Allocate();
|
|
newEntry->State = EntryState.Empty;
|
|
newEntry->Position = (nuint)((ulong)m_Buffer + finalOffset);
|
|
newEntry->Size = (uint)s;
|
|
BucketNode* bucket = (BucketNode*)newEntry->Position;
|
|
bucket->EntryRef = newEntry;
|
|
|
|
// link new entry
|
|
newEntry->PreviousNear = current;
|
|
newEntry->NextNear = next;
|
|
current->NextNear = newEntry;
|
|
if (next != null) {
|
|
next->PreviousNear = newEntry;
|
|
}
|
|
|
|
newEntry->Level = i;
|
|
PushLevel(i, Math.Min(blockCount - 1, 2), bucket);
|
|
|
|
// set new entry
|
|
current = newEntry;
|
|
|
|
// update lock and data
|
|
finalOffset += (ulong)s;
|
|
remainSize -= (ulong)s;
|
|
if (remainSize == 0)
|
|
{
|
|
break;
|
|
}
|
|
}
|
|
RemoveFromPositionChain(e);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Try to allocate a new 16MB block when all buckets are empty
|
|
/// </summary>
|
|
/// <param name="handle">
|
|
/// Output handle to the allocated Entry
|
|
/// </param>
|
|
/// <returns>
|
|
/// True if allocation succeeded
|
|
/// </returns>
|
|
private bool TryAllocateNew16MBBlock(out Entry* handle)
|
|
{
|
|
nuint pos = _spareMemory->Position;
|
|
nuint bufferEnd = (nuint)m_Buffer + _size;
|
|
|
|
if (pos >= bufferEnd || _spareMemory->Size == 0)
|
|
{
|
|
handle = null;
|
|
return false; // No spare memory available
|
|
}
|
|
|
|
nuint desired = (nuint)levelSizeArray[^1] * 4;
|
|
nuint remain = bufferEnd - pos;
|
|
nuint blockSize = desired <= remain ? desired : remain;
|
|
if (blockSize == 0)
|
|
{
|
|
handle = null;
|
|
return false;
|
|
}
|
|
|
|
nuint newPos = pos + blockSize;
|
|
|
|
// Try to allocate new Entry from slab allocator
|
|
handle = (Entry*)_entries.Allocate();
|
|
handle->Position = pos;
|
|
handle->Size = (uint)blockSize;
|
|
handle->Level = GetLevelFromSize((uint)blockSize);
|
|
|
|
// Update spare memory position & remaining size
|
|
_spareMemory->Position = newPos;
|
|
_spareMemory->Size = (uint)(bufferEnd - newPos);
|
|
|
|
BucketNode* b = (BucketNode*)handle->Position;
|
|
b->EntryRef = handle;
|
|
|
|
// update position linked list
|
|
Entry* previous = _spareMemory->PreviousNear;
|
|
handle->NextNear = _spareMemory;
|
|
handle->PreviousNear = previous;
|
|
_spareMemory->PreviousNear = handle; // Replace Volatile.Write with direct access
|
|
if (previous != null)
|
|
{
|
|
previous->NextNear = handle;
|
|
} else
|
|
{
|
|
_positionHead = handle;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Helper method for allocation - converts BucketNode to Entry
|
|
/// </summary>
|
|
private bool TryPopLevelForAllocation(int levelIndex, int sizeIndex, out Entry* entry)
|
|
{
|
|
if (TryPopLevel(levelIndex, sizeIndex, out BucketNode* bucketNode))
|
|
{
|
|
entry = bucketNode->EntryRef;
|
|
return entry != null;
|
|
}
|
|
entry = null;
|
|
return false;
|
|
}
|
|
|
|
}
|
|
}
|