l4 handle

This commit is contained in:
Youmu Liu
2026-03-09 18:14:49 +08:00
parent ae8238fca2
commit d97fb1dd90
12 changed files with 695 additions and 376 deletions
+58 -8
View File
@@ -2,8 +2,8 @@
// -----------------------------------------------------------------------
// file="L4"
// 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
@@ -30,25 +30,75 @@ using StgSharp.Mathematics.Memory;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
namespace StgSharp.HighPerformance.Memory
{
public unsafe partial class L4
public unsafe partial class L4 : IDisposable
{
private readonly byte* _buffer;
private const int CacheLineCount = 8192;
private bool _disposed;
private readonly nuint _baseAddress;
private readonly SlabAllocator<CacheLineData> _cacheLineAllocator;
private readonly SlabAllocator<CacheLineHead> _entryAllocator;
private readonly SwissTable _map;
private SlabAllocator<EvictionRingNode> EntryAllocator { get; set; }
public L4()
{
int cacheSize = Unsafe.SizeOf<CacheLineData>() * CacheLineCount;
int entrySize = Unsafe.SizeOf<CacheLineHead>() * CacheLineCount;
int mapSize = SwissTable.RequestedNativeMemorySize;
_baseAddress = (nuint)NativeMemory.AlignedAlloc((nuint)(cacheSize + entrySize + mapSize), 16);
nuint _buffer = _baseAddress;
_cacheLineAllocator = SlabAllocator.FromBuffer<CacheLineData>(_buffer, CacheLineCount, _ => { });
_entryAllocator = SlabAllocator.FromBuffer<CacheLineHead>(_buffer + (nuint)cacheSize, CacheLineCount, _ => { });
_map = SwissTable.Create(_baseAddress + (nuint)(cacheSize + entrySize), _ => { });
}
private SlabAllocator<CacheLine> CacheLineAllocator { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage(
"Design",
"CA1063:Implement IDisposable correctly",
Justification = """
Only owns native memory via NativeMemory.AlignedFree();
no managed IDisposable fields.
Dispose(bool) pattern is unnecessary.
""")]
public void Dispose()
{
if (_disposed) {
return;
}
_disposed = true;
NativeMemory.AlignedFree((void*)_baseAddress);
GC.SuppressFinalize(this);
}
public struct CacheLine
public CacheLine Map(
nuint origin
)
{
// Placeholder implementation; actual mapping logic would go here.
if (_map.TryGet(origin, out nuint entryHandle))
{
CacheLineHead* entry = (CacheLineHead*)entryHandle;
return new CacheLine(entryHandle);
}
return origin;
}
private struct CacheLineData
{
public fixed byte Data[64];
private fixed byte Data[8192];
}