//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator.SegmentEntry.cs"
// Project: StgSharp
// AuthorGroup: Nitload Space
// Copyright (c) Nitload Space. 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.Linq.Expressions;
using System.Reflection.PortableExecutable;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Threading;
namespace StgSharp.HighPerformance.Memory
{
public unsafe partial class HybridLayerSegregatedFitAllocator
{
private static void AcquireEntrySpinLock(Entry* entry)
{
// 使用新的自操作锁代替SpinLock
if (entry != null)
{
while (!entry->TryAcquireSelfOp()) {
Thread.Sleep(0);
}
}
}
private bool AssertAlignBoundry(ulong current, Entry* target, int level)
{
int size = (level * 2) + 6;
ulong align = current >> size;
return align == target->Position >> size && align == target->Size;
}
///
/// Determine segment index based on size and originLevel
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private int DetermineSegmentIndex(uint size, int level)
{
return (int)size / levelSize[level];
}
///
/// Check if two Entries are adjacent in memory
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool IsAdjacentTo(Entry* current, Entry* other)
{
// Check if this immediately follows other
return other->Position + other->Size == current->Position ||
current->Position + current->Size == other->Position;
}
///
/// 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
/// stretagey, any fail in acquiring spin lock will end a merging process.
///
private void MergeAndRemoveFromBuckets(Entry* entry)
{
if (entry == null) {
return;
}
Entry* result = entry;
uint size = entry->Size;
int originLevel = -1;
int segment = DetermineSegmentIndex(size, originLevel);
if (TryAcquireEntrySpinLock(entry)) {
return;
}
// current Level merge
while (originLevel != entry->Level)
{
ulong offset = entry->Position - (ulong)m_Buffer;
AcquireBucketNodeSpinLock((BucketNode*)entry->Position);
while (true)
{
ulong n = entry->NextNear;
Entry* near = (Entry*)n;
if (near == null ||
near->State != EntryState.Empty ||
!IsAdjacentTo(entry, near) ||
entry->Level != near->Level ||
!AssertAlignBoundry(offset, near, entry->Level) ||
!TryAcquireEntrySpinLock(near))
{
break;
}
RemoveFromBucket(originLevel, , (BucketNode*)near->Position);
}
// forward merging
while (true)
{
ulong n = entry->NextNear;
Entry* near = (Entry*)n;
if (near == null ||
near->State != EntryState.Empty ||
!IsAdjacentTo(entry, near) ||
entry->Level != near->Level ||
!AssertAlignBoundry(offset, near, entry->Level) ||
!TryAcquireEntrySpinLock(near))
{
break;
}
}
}
}
private void MergeEntry(Entry* remain, Entry* other)
{
if (other == null) {
return;
}
// Determine merged starting position and size
nuint newPosition = remain->Position < other->Position ? other->Position : other->Position;
uint newSize = remain->Size + other->Size;
// Update current Entry
remain->Position = newPosition;
remain->Size = newSize;
}
private void RecycleEntry(Entry* entry)
{
if (entry is null) {
return;
}
entry-> PreviousNear = EmptyHandle;
entry-> Position = EmptyHandle;
_nodes.Free((nuint)entry);
}
///
/// Remove node from position linked list
///
private void RemoveFromPositionChain(Entry* entry)
{
AcquireEntrySpinLock(entry);
try
{
Entry* prev = (Entry*)entry->PreviousNear;
Entry* next = (Entry*)entry->NextNear;
if (prev != null) {
prev->NextNear = entry->NextNear;
}
if (next != null) {
next->PreviousNear = entry->PreviousNear;
}
// Clear node pointers
entry->NextNear = EmptyHandle;
entry->PreviousNear = EmptyHandle;
_nodes.Free((nuint)entry);
}
finally
{
ReleaseEntrySpinLock(entry);
}
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static bool TryAcquireEntrySpinLock(Entry* entry)
{
int thread = Environment.CurrentManagedThreadId;
return entry == null ||
(Interlocked.CompareExchange(ref entry->SpinLock, thread,
0) == 0) ||
Volatile.Read(ref entry->SpinLock) == thread;
}
///
/// Entry structure - streamlined version (only position and state information) Stored in
/// external SLAB
///
[StructLayout(LayoutKind.Explicit, Size = 40)]
internal struct Entry
{
[FieldOffset(26)] internal byte NextOpMask; // Next pointer operation (Free=0, Occupied=1)
[FieldOffset(25)] internal byte PrevOpMask; // Previous pointer operation (Free=0, Occupied=1)
[FieldOffset(27)] internal byte SelfOpMask; // Self operation (Free=0, Occupied=1)
// Four independent byte masks for atomic operations (replacing SpinLock)
[FieldOffset(24)] internal byte StateMask; // Entry state (Empty=0, Allocated=1, ThreadOccupied=2, Merging=3)
[FieldOffset(28)] internal int _reserved; // Reserved for future use
[FieldOffset(36)] internal int Level; // Level of the block in the allocator
[FieldOffset(32)] internal uint Size; // Block size information
[FieldOffset(8)] internal ulong NextNear; // Position linked list: next pointer
[FieldOffset(16)] internal ulong PreviousNear; // Position linked list: previous pointer
[FieldOffset(0)] public nuint Position; // Memory block position
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrEmptyIndex(ulong index)
{
return index == EmptyHandle;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static bool IsNullOrEmptyIndex(Entry* entry)
{
return entry == null;
}
///
/// Merge two adjacent Entries (caller must ensure they are adjacent and both Empty
/// state)
///
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void MergeWith(Entry* other) { }
#region Four Independent Byte Masks for Entry
// State constants
public const byte StateEmpty = 0;
public const byte StateAllocated = 1;
public const byte StateThreadOccupied = 2;
public const byte StateMerging = 3;
// Operation constants
public const byte OpFree = 0;
public const byte OpOccupied = 1;
// Atomic state operations
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TrySetState(byte expectedState, byte newState)
{
return Interlocked.CompareExchange(ref StateMask, newState, expectedState) == expectedState;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public byte GetState() => Volatile.Read(ref StateMask);
// Previous operation atomic methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAcquirePrevOp()
{
return Interlocked.CompareExchange(ref PrevOpMask, OpOccupied, OpFree) == OpFree;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReleasePrevOp()
{
Volatile.Write(ref PrevOpMask, OpFree);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsPrevOpOccupied() => Volatile.Read(ref PrevOpMask) == OpOccupied;
// Next operation atomic methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAcquireNextOp()
{
return Interlocked.CompareExchange(ref NextOpMask, OpOccupied, OpFree) == OpFree;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReleaseNextOp()
{
Volatile.Write(ref NextOpMask, OpFree);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsNextOpOccupied() => Volatile.Read(ref NextOpMask) == OpOccupied;
// Self operation atomic methods
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool TryAcquireSelfOp()
{
return Interlocked.CompareExchange(ref SelfOpMask, OpOccupied, OpFree) == OpFree;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void ReleaseSelfOp()
{
Volatile.Write(ref SelfOpMask, OpFree);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsSelfOpOccupied() => Volatile.Read(ref SelfOpMask) == OpOccupied;
// Convenience properties
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsEmpty() => GetState() == StateEmpty;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsAllocated() => GetState() == StateAllocated;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsThreadOccupied() => GetState() == StateThreadOccupied;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool IsMerging() => GetState() == StateMerging;
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool HasAnyOperation() => IsPrevOpOccupied() || IsNextOpOccupied() || IsSelfOpOccupied();
#endregion
}
// 更新后的EntryState常量类 - 与新的字节掩码设计兼容
private static class EntryState
{
public const byte Allocated = 1; // Already allocated to user
public const byte Empty = 0; // Free, can be allocated
public const byte Merging = 3; // Currently being merged
public const byte ThreadOccupied = 2; // Occupied by thread (being operated on)
}
}
}