Abort hlsf, goes back to tlsf, may use 'Arena-ed tlsf'
This commit is contained in:
@@ -0,0 +1,239 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// 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.IO;
|
||||
using System.Numerics;
|
||||
using System.Reflection.Metadata;
|
||||
using hlsfHandle = StgSharp.HighPerformance.Memory.HlsfHandle;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
public partial HlsfHandle Alloc(
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
if (size > _maxAllocSize) {
|
||||
throw new InvalidOperationException("Requested size exceeds maximum allocatable size.");
|
||||
}
|
||||
/*
|
||||
int level = GetLevelFromSize(size);
|
||||
int segmentIndex = GetSegmentFromLevel(size, level);
|
||||
|
||||
int i = level, s = segmentIndex;
|
||||
/**/
|
||||
|
||||
Entry* entry = null;
|
||||
nuint requestedOffset = GetAllocSize(size);
|
||||
(int level, int segment) = GetIndexPairOnce(size);
|
||||
int index = int.Min((3 * level) + segment, _bucketHeads.Length);
|
||||
int i = index;
|
||||
while ((i < _bucketHeads.Length) && !TryPopLevelForAllocation(i, out entry)) {
|
||||
i++;
|
||||
}
|
||||
if ((i >= _bucketHeads.Length) && !TryAllocateNewBlock(requestedOffset, out entry)) {
|
||||
throw new OverflowException($"Out of memory: unable to satisfy request of {size} B (aligned {requestedOffset} B) from existing buckets or spare memory.");
|
||||
}
|
||||
if (entry != null)
|
||||
{
|
||||
if (requestedOffset > entry->Size) {
|
||||
throw new OverflowException("Out of memory: insufficient block size returned from spare memory.");
|
||||
}
|
||||
nuint remainder = entry->Size - requestedOffset;
|
||||
SetEntryLevel(entry, entry->Position, (uint)requestedOffset);
|
||||
entry->State = EntryState.Alloc;
|
||||
if (remainder > 0) {
|
||||
OrganizeMemory(entry->NextNear, remainder);
|
||||
}
|
||||
}
|
||||
return new(entry, size);
|
||||
}
|
||||
|
||||
public bool TryAlloc(
|
||||
nuint size,
|
||||
out HlsfHandle handle
|
||||
)
|
||||
{
|
||||
if (size > _maxAllocSize)
|
||||
{
|
||||
handle = default;
|
||||
return false;
|
||||
}
|
||||
/*
|
||||
int level = GetLevelFromSize(size);
|
||||
int segmentIndex = GetSegmentFromLevel(size, level);
|
||||
|
||||
int i = level, s = segmentIndex;
|
||||
/**/
|
||||
|
||||
Entry* entry = null;
|
||||
nuint requestedOffset = GetAllocSize(size);
|
||||
(int level, int segment) = GetIndexPairOnce(size);
|
||||
int index = int.Min((3 * level) + segment, _bucketHeads.Length);
|
||||
int i = index;
|
||||
while ((i < _bucketHeads.Length) && !TryPopLevelForAllocation(i, out entry)) {
|
||||
i++;
|
||||
}
|
||||
if ((i >= _bucketHeads.Length) && !TryAllocateNewBlock(requestedOffset, out entry)) {
|
||||
throw new OverflowException($"Out of memory: unable to satisfy request of {size} B (aligned {requestedOffset} B) from existing buckets or spare memory.");
|
||||
}
|
||||
|
||||
if (entry != null)
|
||||
{
|
||||
if (requestedOffset > entry->Size) {
|
||||
throw new OverflowException("Out of memory: insufficient block size returned from spare memory.");
|
||||
}
|
||||
nuint remainder = entry->Size - requestedOffset;
|
||||
SetEntryLevel(entry, entry->Position, (uint)requestedOffset);
|
||||
entry->State = EntryState.Alloc;
|
||||
if (remainder > 0) {
|
||||
OrganizeMemory(entry->NextNear, remainder);
|
||||
}
|
||||
}
|
||||
handle = new(entry, size);
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Slice the memory block into smaller blocks and add them to the appropriate buckets.
|
||||
/// Notice: this method is not responsible for freeing the original BucketNode
|
||||
/// </summary>
|
||||
/// <param name="e">
|
||||
/// The next entry nearby the part to slice
|
||||
/// </param>
|
||||
/// <param name="sizeToSlice">
|
||||
///
|
||||
/// </param>
|
||||
private void OrganizeMemory(
|
||||
Entry* e,
|
||||
nuint sizeToSlice
|
||||
)
|
||||
{
|
||||
Entry* next = e;
|
||||
nuint remain = sizeToSlice; // remained size to slice
|
||||
nuint offset = e->Position - sizeToSlice - ((nuint)m_Buffer); // offset of buffer to slice
|
||||
nuint position = e->Position - sizeToSlice; // position of buffer to slice
|
||||
nuint end = e->Position - ((nuint)m_Buffer); // end offset of buffer to slice
|
||||
|
||||
int i = GetBucketIndexOnce(sizeToSlice);
|
||||
|
||||
Entry* entry = (Entry*)_entries.Allocate();
|
||||
SetEntryLevel(entry, position, remain);
|
||||
entry->State = EntryState.Empty;
|
||||
BucketNode* bucketNode = (BucketNode*)entry->Position;
|
||||
bucketNode->EntryRef = entry;
|
||||
PushNodeToBucket(i, bucketNode);
|
||||
|
||||
Entry* prev = next->PreviousNear;
|
||||
prev->NextNear = entry;
|
||||
entry->PreviousNear = prev;
|
||||
entry->NextNear = next;
|
||||
next->PreviousNear = entry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Try to allocate a new 16MB block when all buckets are empty
|
||||
/// </summary>
|
||||
/// <param name="size">
|
||||
/// Size of memory block to be allocated. Notice: Size is assumed as times of 64.
|
||||
/// </param>
|
||||
/// <param name="handle">
|
||||
/// Output handle to the allocated Entry
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// True if allocation succeeded
|
||||
/// </returns>
|
||||
private bool TryAllocateNewBlock(
|
||||
nuint size,
|
||||
out Entry* handle
|
||||
)
|
||||
{
|
||||
nuint pos = _spareMemory->Position;
|
||||
nuint remain = _spareMemory->Size;
|
||||
if (remain <= 0)
|
||||
{
|
||||
handle = null;
|
||||
return false;
|
||||
}
|
||||
size = nuint.Min(size, remain);
|
||||
|
||||
|
||||
// build new entry
|
||||
Entry* e = (Entry*)_entries.Allocate();
|
||||
SetEntryLevel(e, pos, size);
|
||||
InsertEntryBefore(e, _spareMemory);
|
||||
e->State = EntryState.Empty;
|
||||
|
||||
// set fields of spare memory
|
||||
_spareMemory->Size -= size;
|
||||
_spareMemory->Position += size;
|
||||
|
||||
|
||||
handle = e;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Helper method for allocation - converts BucketNode to Entry
|
||||
/// </summary>
|
||||
private bool TryPopLevelForAllocation(
|
||||
int bucketIndex,
|
||||
out Entry* entry
|
||||
)
|
||||
{
|
||||
if (TryPopBucket(bucketIndex, out BucketNode* bucketNode))
|
||||
{
|
||||
entry = bucketNode->EntryRef;
|
||||
return entry != null;
|
||||
}
|
||||
entry = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
/// <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;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,214 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.BucketNode"
|
||||
// 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.Reflection.Metadata;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
/*
|
||||
* All operations with style of (level, segment)
|
||||
* DO NOT assume that the level are valid.
|
||||
* They are just used to calculate the bucket index,
|
||||
* which may exceed the range of _bucketHeads.
|
||||
*/
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void PushBucketToLevel(
|
||||
int levelIndex,
|
||||
int sizeIndex,
|
||||
BucketNode* node
|
||||
)
|
||||
{
|
||||
if ((node == null) || (node->EntryRef == null)) {
|
||||
return;
|
||||
}
|
||||
|
||||
int index = Math.Min((3 * levelIndex) + sizeIndex, _bucketHeads.Length - 1);
|
||||
PushNodeToBucket(index, node);
|
||||
}
|
||||
|
||||
private void PushNodeToBucket(
|
||||
int index,
|
||||
BucketNode* node
|
||||
)
|
||||
{
|
||||
index = Math.Min(index, _bucketHeads.Length - 1);
|
||||
if (_bucketHeads[index] is null)
|
||||
{
|
||||
_bucketHeads[index] = node;
|
||||
node->IsInBucket = true;
|
||||
node->NextLevel = null;
|
||||
node->PreviousLevel = null;
|
||||
|
||||
// Console.WriteLine($"Push to empty level of {levelIndex} {sizeIndex}, bucket pos {(ulong)node}");
|
||||
return;
|
||||
} else
|
||||
{
|
||||
BucketNode* head = _bucketHeads[index];
|
||||
node->NextLevel = head;
|
||||
node->IsInBucket = true;
|
||||
_bucketHeads[index] = node;
|
||||
|
||||
// Console.WriteLine($"Push to head of level of {levelIndex} {sizeIndex}, bucket pos {(ulong)node}, previous head is at {head}");
|
||||
|
||||
head->PreviousLevel = node;
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void RemoveFromBucket(
|
||||
int levelIndex,
|
||||
int sizeIndex,
|
||||
BucketNode* node
|
||||
)
|
||||
{
|
||||
int index = Math.Min((3 * levelIndex) + sizeIndex, _bucketHeads.Length - 1);
|
||||
if (_bucketHeads[index] == node)
|
||||
{
|
||||
if (TryPopBucket(index, out _)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
BucketNode* previous = node->PreviousLevel;
|
||||
BucketNode* next = node->NextLevel;
|
||||
|
||||
if (previous != null) {
|
||||
previous->NextLevel = next;
|
||||
}
|
||||
if (next != null) {
|
||||
next->PreviousLevel = previous;
|
||||
}
|
||||
|
||||
node->NextLevel = (BucketNode*)null;
|
||||
node->PreviousLevel = (BucketNode*)null;
|
||||
node->IsInBucket = false;
|
||||
return;
|
||||
}
|
||||
|
||||
private bool TryPopBucket(
|
||||
int index,
|
||||
out BucketNode* node
|
||||
)
|
||||
{
|
||||
/*
|
||||
* Attention:
|
||||
* This method is UNSAFE. Index may exceed range of _bucketHeads.
|
||||
* Be careful when calling this method.
|
||||
*/
|
||||
BucketNode* head = _bucketHeads[index];
|
||||
|
||||
if (head == null)
|
||||
{
|
||||
node = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
// Console.WriteLine($"level popped from level {levelIndex} {sizeIndex}, node is at {(ulong)head}");
|
||||
BucketNode* next = head->NextLevel;
|
||||
|
||||
if (next == null)
|
||||
{
|
||||
_bucketHeads[index] = null;
|
||||
node = head;
|
||||
return true;
|
||||
} else
|
||||
{
|
||||
next->PreviousLevel = null;
|
||||
}
|
||||
_bucketHeads[index] = head->NextLevel;
|
||||
node = head;
|
||||
node->IsInBucket = false;
|
||||
node->NextLevel = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private bool TryPopLevel(
|
||||
int levelIndex,
|
||||
int sizeIndex,
|
||||
out BucketNode* node
|
||||
)
|
||||
{
|
||||
int index = int.Min((3 * levelIndex) + sizeIndex, _bucketHeads.Length - 1);
|
||||
return TryPopBucket(index, out node);
|
||||
}
|
||||
|
||||
private bool TryRemoveFromBucket(
|
||||
int levelIndex,
|
||||
int sizeIndex,
|
||||
BucketNode* node
|
||||
)
|
||||
{
|
||||
int index = (3 * levelIndex) + sizeIndex;
|
||||
index = (index >= _bucketHeads.Length) ? _bucketHeads.Length : index;
|
||||
if (_bucketHeads[index] == node)
|
||||
{
|
||||
bool ok = TryPopLevel(levelIndex, sizeIndex, out _);
|
||||
return ok;
|
||||
}
|
||||
|
||||
BucketNode* previous = node->PreviousLevel;
|
||||
BucketNode* next = node->NextLevel;
|
||||
|
||||
if (previous != null) {
|
||||
previous->NextLevel = next;
|
||||
}
|
||||
if (next != null) {
|
||||
next->PreviousLevel = previous;
|
||||
}
|
||||
|
||||
node->NextLevel = null;
|
||||
node->PreviousLevel = null;
|
||||
node->IsInBucket = false;
|
||||
node->EntryRef = null;
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// BucketNode structure - specialized node for bucket linked lists Stored in main
|
||||
/// allocation area (user-writable region)
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit)]
|
||||
internal struct BucketNode
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal Entry* EntryRef; // Pointer to corresponding Entry
|
||||
// [FieldOffset(16)] internal ulong LockBuffer;
|
||||
[FieldOffset(8)] internal BucketNode* NextLevel; // Bucket stack linked list: next pointer
|
||||
[FieldOffset(24)] internal BucketNode* PreviousLevel; // Bucket stack linked list: previous pointer
|
||||
[FieldOffset(32)] internal bool IsInBucket; // Whether in bucket
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,130 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.Debug"
|
||||
// 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public sealed class HLSFPositionChainBreakException : Exception
|
||||
{
|
||||
|
||||
public HLSFPositionChainBreakException()
|
||||
: base("Position chain of an HLSF allocator is broken") { }
|
||||
|
||||
}
|
||||
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
private object _lock = new object();
|
||||
|
||||
public void DisplayEntryLayout(
|
||||
bool singleStep
|
||||
)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
Entry* cur = _spareMemory;
|
||||
while (cur != null)
|
||||
{
|
||||
Console.WriteLine(EntryToString(cur));
|
||||
cur = cur->PreviousNear;
|
||||
if (singleStep) {
|
||||
Console.ReadKey();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void DumpPositionChain(
|
||||
bool singleStep = false,
|
||||
int maxCount = 4096
|
||||
)
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
try
|
||||
{
|
||||
Entry* cur = _spareMemory;
|
||||
int cnt = 0;
|
||||
if (cur != null) {
|
||||
cur = cur->NextNear;
|
||||
}
|
||||
Console.WriteLine(EntryToString(_spareMemory));
|
||||
while ((cur != null) && (cur != _spareMemory) && (cnt < maxCount))
|
||||
{
|
||||
Console.WriteLine(EntryToString(cur));
|
||||
cnt++;
|
||||
if (singleStep) {
|
||||
Console.ReadKey();
|
||||
}
|
||||
|
||||
cur = cur->NextNear;
|
||||
}
|
||||
if (cur == _spareMemory)
|
||||
{
|
||||
Console.WriteLine("(reached sentinel)");
|
||||
} else if (cnt >= maxCount) {
|
||||
Console.WriteLine("(stopped at maxCount)");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"[DumpPositionChain-forward] Exception: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public IEnumerable<string> ShowEntryLayout()
|
||||
{
|
||||
lock (_lock)
|
||||
{
|
||||
Stack<string> layout = [];
|
||||
Entry* cur = _spareMemory;
|
||||
while (cur != null)
|
||||
{
|
||||
layout.Push(EntryToString(cur));
|
||||
cur = cur->PreviousNear;
|
||||
}
|
||||
return layout;
|
||||
}
|
||||
}
|
||||
|
||||
private static string EntryToString(
|
||||
Entry* e
|
||||
)
|
||||
{
|
||||
BucketNode* b = (BucketNode*)e->Position;
|
||||
return $"{(ulong)e->PreviousNear}<-prev- Position:{(ulong)e} Bucket:{{{(ulong)e->Position},{e->Level},{e->Size}}} -next->{(ulong)e->NextNear}";
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,166 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.Free"
|
||||
// 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.Buffers;
|
||||
using System.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using System.Reflection.Emit;
|
||||
using System.Reflection.Metadata;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
using hlsfHandle = StgSharp.HighPerformance.Memory.HlsfHandle;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public enum FreePolicy
|
||||
{
|
||||
|
||||
FullCollect,
|
||||
LazyCollect,
|
||||
NoCollect
|
||||
|
||||
}
|
||||
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Minimal collect: linear scan, merge contiguous empty Blocks into one, push merged head
|
||||
/// back to bucket. Caller-responsible design: no slicing or redistribution.
|
||||
/// </summary>
|
||||
public partial void Collect()
|
||||
{
|
||||
Entry* cur = _spareMemory->NextNear;
|
||||
nuint size = 0;
|
||||
while ((cur != null) && (cur != _spareMemory))
|
||||
{
|
||||
// cur is not empty, skip
|
||||
if (cur->State != EntryState.Empty)
|
||||
{
|
||||
cur = cur->NextNear;
|
||||
continue;
|
||||
}
|
||||
|
||||
// remove cur from bucket
|
||||
do
|
||||
{
|
||||
size += cur->Size;
|
||||
Entry* next = cur->NextNear;
|
||||
RemoveFromBucket(cur->Level, GetSegmentFromLevel(cur->Size, cur->Level), (BucketNode*)cur->Position);
|
||||
RemoveFromPositionChain(cur);
|
||||
cur = next;
|
||||
} while ((cur != null) && (cur != _spareMemory) && (cur->State == EntryState.Empty));
|
||||
if (size > 0)
|
||||
{
|
||||
OrganizeMemory(cur, size);
|
||||
size = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Free allocated memory block and perform adjacent block merging
|
||||
/// </summary>
|
||||
/// <param name="handle">
|
||||
/// Handle to the allocated memory block
|
||||
/// <param name="collectPolicy">
|
||||
/// </param>
|
||||
public void Free(
|
||||
HlsfHandle handle,
|
||||
FreePolicy collectPolicy = FreePolicy.NoCollect
|
||||
)
|
||||
{
|
||||
Entry* e = handle.EntryHandle;
|
||||
if (e == null) {
|
||||
return;
|
||||
}
|
||||
if (e->State != EntryState.Alloc) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark as empty so it can be merged
|
||||
e->State = EntryState.Empty;
|
||||
|
||||
// Try merge with adjacent free blocks and remove them from buckets
|
||||
MergeAndRemoveFromBuckets(e);
|
||||
|
||||
// Calculate final level and segment for the merged block
|
||||
nuint finalSize = e->Size;
|
||||
(int finalLevel, int finalSegment) = GetIndexPairOnce(finalSize);
|
||||
e->Level = finalLevel;
|
||||
|
||||
// Push the merged entry back to appropriate bucket
|
||||
PushBucketToLevel(finalLevel, finalSegment, (BucketNode*)e->Position);
|
||||
|
||||
// Console.WriteLine("one block freed");
|
||||
if (collectPolicy == FreePolicy.FullCollect)
|
||||
{
|
||||
Collect();
|
||||
}
|
||||
}
|
||||
|
||||
internal void Free(
|
||||
Entry* e,
|
||||
FreePolicy collectPolicy = FreePolicy.NoCollect
|
||||
)
|
||||
{
|
||||
if (e == null) {
|
||||
return;
|
||||
}
|
||||
if (e->State != EntryState.Alloc) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Mark as empty so it can be merged
|
||||
e->State = EntryState.Empty;
|
||||
|
||||
// Try merge with adjacent free blocks and remove them from buckets
|
||||
/**/
|
||||
MergeAndRemoveFromBuckets(e);
|
||||
|
||||
// Calculate final level and segment for the merged block
|
||||
nuint finalSize = e->Size;
|
||||
int finalLevel = GetLevelFromSize(finalSize);
|
||||
e->Level = finalLevel;
|
||||
int finalSegment = GetSegmentFromLevel(finalSize, finalLevel);
|
||||
|
||||
// Push the merged entry back to appropriate bucket
|
||||
PushBucketToLevel(finalLevel, finalSegment, (BucketNode*)e->Position);
|
||||
|
||||
// Console.WriteLine("one block freed");
|
||||
if (collectPolicy == FreePolicy.FullCollect)
|
||||
{
|
||||
Collect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,154 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.Level"
|
||||
// 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.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Security;
|
||||
using System.Numerics;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private nuint GetAllocSize(
|
||||
nuint size
|
||||
)
|
||||
|
||||
{
|
||||
return (size + 63u) & (~63u);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int GetBucketIndexOnce(
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
int log2Size = 63 - BitOperations.LeadingZeroCount((ulong)size);
|
||||
|
||||
// Calculate level: level = max(0, (log2Size - 6) / 2)
|
||||
int level = Math.Max(0, (log2Size - 6) / 2);
|
||||
|
||||
// caller is responsible for ensuring level is within bounds
|
||||
level = Math.Min(level, _maxLevel);
|
||||
uint levelSize = 1u << (log2Size & (~1));
|
||||
int segment = ((int)(size / levelSize)) - 1;
|
||||
return Math.Min((level * 3) + segment, _bucketHeads.Length - 1);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private (int level, int segment) GetIndexPairOnce(
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
int log2Size = 63 - BitOperations.LeadingZeroCount((ulong)size);
|
||||
|
||||
// Calculate level: level = max(0, (log2Size - 6) / 2)
|
||||
int level = Math.Max(0, (log2Size - 6) / 2);
|
||||
|
||||
// caller is responsible for ensuring level is within bounds
|
||||
level = Math.Min(level, _maxLevel);
|
||||
uint levelSize = 1u << (log2Size & (~1));
|
||||
int segment = ((int)(size / levelSize)) - 1;
|
||||
return (level, segment);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// NOTICE: returned level may exceed maxLevel, caller is responsible for ensuring the
|
||||
/// returned level is within bounds. This method is designed for performance and does not
|
||||
/// perform bounds checking.
|
||||
/// </summary>
|
||||
/// <param name="size">
|
||||
/// Size of the memory block to determine level for. This method does not assume size is
|
||||
/// already aligned
|
||||
/// </param>
|
||||
/// <returns>
|
||||
/// Level corresponding to the given size. Level is determined based on the formula: level
|
||||
/// >= (log2(S) - 6) / 2
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int GetLevelFromSize(
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
/*
|
||||
* Level sizes: [64, 256, 1024, 4096, 16384, 65536, 262144, 1048576, 4194304, 16777216, ......]
|
||||
* Pattern: 64 * 4^level = 2^6 * 2^(2*level) = 2^(6+2*level)
|
||||
*
|
||||
* For size S, we need: 2^(6+2*level) >= S
|
||||
* So: 6+2*level >= log2(S)
|
||||
* Therefore: level >= (log2(S) - 6) / 2
|
||||
*
|
||||
* We use hardware LZCNT instruction via BitOperations.LeadingZeroCount
|
||||
* log2(S) = 63 - LeadingZeroCount(S) for 64-bit values
|
||||
*/
|
||||
int log2Size = 63 - BitOperations.LeadingZeroCount((ulong)size);
|
||||
|
||||
// Calculate level: level = max(0, (log2Size - 6) / 2)
|
||||
int level = Math.Max(0, (log2Size - 6) / 2);
|
||||
|
||||
// caller is responsible for ensuring level is within bounds
|
||||
return level;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint GetLevelSizeFromLevel(
|
||||
int level
|
||||
)
|
||||
{
|
||||
return 1u << (6 + (2 * level));
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static uint GetLevelSizeFromSize(
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
int log2Size = 63 - BitOperations.LeadingZeroCount((ulong)size);
|
||||
return 1u << (log2Size & (~1));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Determine segment index based on size and originLevel. Input level value is regarded
|
||||
/// safe.
|
||||
/// </summary>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private int GetSegmentFromLevel(
|
||||
ulong size,
|
||||
int level
|
||||
)
|
||||
{
|
||||
return ((int)(size / GetLevelSizeFromLevel(level))) - 1;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,219 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.SegmentEntry"
|
||||
// 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.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
/// <summary>
|
||||
/// Check if two Entries are adjacent in memory
|
||||
/// </summary>
|
||||
/// <returns>
|
||||
/// True if two mem blocks are adjacent.
|
||||
/// </returns>
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static bool AssertAdjacentTo(
|
||||
Entry* front,
|
||||
Entry* back
|
||||
)
|
||||
{
|
||||
// Only a predicate; never throw here. Callers rely on false to skip merging.
|
||||
return front->Position + front->Size == back->Position;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void InsertEntryAfter(
|
||||
Entry* entry,
|
||||
Entry* position
|
||||
)
|
||||
{
|
||||
Entry* next = position->NextNear;
|
||||
entry->NextNear = next;
|
||||
entry->PreviousNear = position;
|
||||
next->PreviousNear = entry;
|
||||
position->NextNear = entry;
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private static void InsertEntryBefore(
|
||||
Entry* entry,
|
||||
Entry* position
|
||||
)
|
||||
{
|
||||
Entry* prev = position->PreviousNear;
|
||||
entry->NextNear = position;
|
||||
entry->PreviousNear = prev;
|
||||
prev->NextNear = entry;
|
||||
position->PreviousNear = entry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Used in Free operations, efficiently remove from buckets and merge adjacent blocks.
|
||||
/// Current Entry will serve as the final merged block. The Merge will be in lazy
|
||||
/// strategy.
|
||||
/// </summary>
|
||||
private void MergeAndRemoveFromBuckets(
|
||||
Entry* entry,
|
||||
int maxScanRange = 4
|
||||
)
|
||||
{
|
||||
if (entry == null) {
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
nuint size = entry->Size;
|
||||
int originLevel = entry->Level;
|
||||
int segment = GetSegmentFromLevel(size, originLevel);
|
||||
|
||||
for (int i = 0; i < maxScanRange; i++)
|
||||
{
|
||||
// previous direction merge
|
||||
Entry* p = entry->PreviousNear;
|
||||
if ((p == null) ||
|
||||
(p == _spareMemory) ||
|
||||
(p->State != EntryState.Empty) ||
|
||||
(p->Level != entry->Level) ||
|
||||
!AssertAdjacentTo(p, entry))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Console.WriteLine($"{(ulong)p->Size} merged to {(ulong)entry->Size} previous");
|
||||
entry->Size += p->Size;
|
||||
entry->Position = p->Position;
|
||||
RemoveFromBucket(originLevel, GetSegmentFromLevel(p->Size, originLevel), (BucketNode*)p->Position);
|
||||
RemoveFromPositionChain(p);
|
||||
}
|
||||
|
||||
for (int i = 0; i < maxScanRange; i++)
|
||||
{
|
||||
// next direction merge
|
||||
Entry* n = entry->NextNear;
|
||||
if ((n == null) ||
|
||||
(n == _spareMemory) ||
|
||||
(n->State != EntryState.Empty) ||
|
||||
(n->Level != entry->Level) ||
|
||||
!AssertAdjacentTo(entry, n))
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
// Console.WriteLine($"{(ulong)n->Size} merged to {(ulong)entry->Size} next");
|
||||
entry->Size += n->Size;
|
||||
RemoveFromBucket(originLevel, GetSegmentFromLevel(n->Size, originLevel), (BucketNode*)n->Position);
|
||||
RemoveFromPositionChain(n);
|
||||
}
|
||||
|
||||
// originLevel = int.Min(9, originLevel);
|
||||
entry->Level = GetLevelFromSize(entry->Size);
|
||||
|
||||
BucketNode* bucket = (BucketNode*)entry->Position;
|
||||
bucket->EntryRef = entry;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Remove node from position linked list, this method does not recycle a BucketNode from
|
||||
/// size bucket
|
||||
/// </summary>
|
||||
private void RemoveFromPositionChain(
|
||||
Entry* entry
|
||||
)
|
||||
{
|
||||
Entry* prev = entry->PreviousNear;
|
||||
Entry* next = entry->NextNear;
|
||||
|
||||
prev->NextNear = next;
|
||||
next->PreviousNear = prev;
|
||||
|
||||
// Clear node pointers
|
||||
entry->NextNear = null;
|
||||
entry->PreviousNear = null;
|
||||
|
||||
// Console.WriteLine("entry recycled.");
|
||||
_entries.Free((nuint)entry);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
private void SetEntryLevel(
|
||||
Entry* entry,
|
||||
nuint position,
|
||||
nuint size
|
||||
)
|
||||
{
|
||||
entry ->Position = position;
|
||||
entry->Size = size;
|
||||
entry->Level = GetLevelFromSize(size);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Entry structure - streamlined version (only position and state information) Stored in
|
||||
/// external SLAB
|
||||
/// </summary>
|
||||
[StructLayout(LayoutKind.Explicit, Size = 40)]
|
||||
internal struct Entry
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal Entry* NextNear; // Position linked list: next pointer
|
||||
[FieldOffset(8)] internal Entry* PreviousNear; // Position linked list: previous pointer
|
||||
|
||||
[FieldOffset(32)] internal int Level; // Level of the block in the allocator
|
||||
[FieldOffset(16)] internal nuint Size; // Block size information
|
||||
[FieldOffset(36)] public EntryState State;
|
||||
[FieldOffset(24)] public nuint Position; // Memory block position
|
||||
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Explicit, Size = 32)]
|
||||
internal struct Metadata
|
||||
{
|
||||
|
||||
[FieldOffset(0)] internal int NextLevel;
|
||||
[FieldOffset(4)] internal int NextNear;
|
||||
[FieldOffset(8)] internal int PreviousLevel;
|
||||
[FieldOffset(12)] internal int PreviousNear;
|
||||
[FieldOffset(16)] internal nuint Size;
|
||||
|
||||
}
|
||||
|
||||
internal enum EntryState : uint
|
||||
{
|
||||
|
||||
Spare,
|
||||
Empty,
|
||||
Alloc,
|
||||
Large
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,163 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator"
|
||||
// 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.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator : IDisposable
|
||||
{
|
||||
|
||||
// Bucket system field - directly in main allocator class
|
||||
private readonly BucketNode*[] _bucketHeads;
|
||||
private Metadata* _metadata;
|
||||
|
||||
// _spareMemory is also the beginning position of allocator
|
||||
private Entry* _spareMemory;
|
||||
private readonly byte* m_Buffer;
|
||||
|
||||
private readonly Action<nuint> _freeHandle;
|
||||
private bool disposedValue;
|
||||
private readonly int _align;
|
||||
private readonly int _maxAllocCount;
|
||||
private readonly int _maxLevel;
|
||||
private int unusedMetaData;
|
||||
private readonly nuint _maxAllocSize;
|
||||
private readonly nuint _size;
|
||||
private Ptr64 _basePtr;
|
||||
private readonly SlabAllocator<Entry> _entries;
|
||||
|
||||
internal HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
nuint maxBlockSize,
|
||||
Func<nuint, int, nuint> allocator,
|
||||
Action<nuint> freeHandle,
|
||||
SlabAllocator<Entry>? entryManager
|
||||
)
|
||||
: this(byteSize, align, maxBlockSize, (void*)allocator(byteSize, align), freeHandle,
|
||||
entryManager) { }
|
||||
|
||||
internal HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
nuint maxBlockSize,
|
||||
void* externalPointer,
|
||||
Action<nuint> freeHandle,
|
||||
SlabAllocator<Entry>? entryManager = null
|
||||
)
|
||||
{
|
||||
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);
|
||||
m_Buffer = (byte*)externalPointer;
|
||||
_basePtr = (Ptr64)(nuint)externalPointer;
|
||||
_metadata = (Metadata*)((nuint)externalPointer + byteSize);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
|
||||
_spareMemory = (Entry*)_entries.Allocate();
|
||||
_spareMemory->State = EntryState.Empty;
|
||||
_spareMemory->PreviousNear = _spareMemory;
|
||||
_spareMemory->NextNear = _spareMemory;
|
||||
_spareMemory->Position = (nuint)m_Buffer;
|
||||
_spareMemory->Size = byteSize;
|
||||
|
||||
_maxLevel = GetLevelFromSize(maxBlockSize) + 1;
|
||||
_bucketHeads = new BucketNode*[(_maxLevel * 3) + 1];
|
||||
_maxAllocSize = maxBlockSize;
|
||||
_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,
|
||||
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,
|
||||
nuint maxBlockSize
|
||||
)
|
||||
: this(byteSize, align, maxBlockSize, NativeMemory.AlignedAlloc(byteSize, 16),
|
||||
ptr => NativeMemory.AlignedFree((void*)ptr)) { }
|
||||
|
||||
public partial HlsfHandle Alloc(
|
||||
nuint size
|
||||
);
|
||||
|
||||
public partial void Collect();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
if (disposing) {
|
||||
_entries.Dispose();
|
||||
}
|
||||
_freeHandle((nuint)m_Buffer);
|
||||
disposedValue = true;
|
||||
}
|
||||
}
|
||||
|
||||
~HybridLayerSegregatedFitAllocator()
|
||||
{
|
||||
Dispose(disposing:false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user