Compress L4 metadata into 32 bytes

This commit is contained in:
Youmu Liu
2026-06-02 18:27:58 +08:00
parent 07f8178431
commit 9f3fc3b345
7 changed files with 156 additions and 135 deletions
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="L4.CacheLine"
// 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
@@ -28,8 +28,6 @@
using StgSharp.HighPerformance.ProcessorAbstraction;
using System.Runtime.InteropServices;
using Tlsf = StgSharp.HighPerformance.Memory.TwoLayerSegregatedFitAllocator;
namespace StgSharp.HighPerformance.Memory
{
public partial class L4
@@ -48,71 +46,52 @@ namespace StgSharp.HighPerformance.Memory
private unsafe struct CacheLineMetadata
{
#region reference counting and state information
[FieldOffset(24)] internal int RefCount;
// [FieldOffset(28)] internal readonly short Id;
#endregion
#region tlsf area
// TLSF area use only 24 bytes
internal const ulong Mask48Bits = 0x0000FFFFFFFFFFFF;
internal const uint MaxBufferSize = 0xFFFFFFFF - 64U;
[FieldOffset(0)] private readonly M256 FillingMask;
[FieldOffset(8)] private ulong PositionMask;
[FieldOffset(16)] private ulong SizeMask;
[FieldOffset(24)] internal nuint Origin;
[FieldOffset(14)] internal AllocationState State;
public static uint SizeOf { get; } = (uint)Unsafe.SizeOf<CacheLineMetadata>();
#region tlsf linked list
[FieldOffset(0)] public short NextLevel;
[FieldOffset(2)] public short NextNear;
[FieldOffset(4)] public short PreviousLevel;
[FieldOffset(6)] public short PreviousNear;
public Ptr64 Position
{
readonly get => PositionMask;
set => PositionMask = value;
}
#endregion
public Ptr64 SizeForAllocator
{
readonly get => (Ptr64)(SizeMask & Mask48Bits);
set => SizeMask = (SizeMask & ~Mask48Bits) | ((ulong)value & Mask48Bits);
}
#region buffer
public Ptr64 SizeForSpare
{
readonly get => SizeMask;
set => SizeMask = value;
}
public static uint SizeOf { get; } = (uint)Unsafe.SizeOf<CacheLineMetadata>();
[FieldOffset(8)] public uint PositionMask;
[FieldOffset(12)] public uint SizeMask;
#endregion
#region cache line information
#region mask
[FieldOffset(34)] internal ushort Profile;
[FieldOffset(24)] internal nuint Origin;
[FieldOffset(16)] internal AllocationState State;
[FieldOffset(17)] internal byte ReversedProfile;
[FieldOffset(18)] internal ushort CustomizeProfile;
[FieldOffset(20)] internal int RefCount;
#endregion
}
[StructLayout(LayoutKind.Sequential)]
public unsafe ref struct Handle : IDisposable
#pragma warning disable IDE0032
[StructLayout(LayoutKind.Explicit)]
public unsafe struct Handle : IDisposable
{
private CacheLineMetadata* _head;
private nuint _handle;
[FieldOffset(8)] private readonly CacheLineMetadata* _head;
[FieldOffset(0)] private nuint _handle;
private Handle(nuint handle, CacheLineMetadata* cacheRefCount)
private Handle(
nuint handle,
CacheLineMetadata* cacheRefCount
)
{
_handle = handle;
_head = cacheRefCount;
@@ -133,6 +112,6 @@ namespace StgSharp.HighPerformance.Memory
}
}
#pragma warning restore IDE0032
}
}