l4 handle
This commit is contained in:
@@ -39,10 +39,10 @@ namespace StgSharp.Collections
|
||||
{
|
||||
|
||||
public bool TryFind(
|
||||
nint key,
|
||||
nuint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
out nint value
|
||||
out nuint value
|
||||
)
|
||||
{
|
||||
byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F);
|
||||
@@ -66,10 +66,10 @@ namespace StgSharp.Collections
|
||||
}
|
||||
|
||||
public bool TryRemove(
|
||||
nint key,
|
||||
nuint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
out nint value
|
||||
out nuint value
|
||||
)
|
||||
{
|
||||
byte ctrl = (byte)((hash >> EntryMaskWidth) & 0x7F);
|
||||
@@ -97,10 +97,10 @@ namespace StgSharp.Collections
|
||||
}
|
||||
|
||||
public bool TrySet(
|
||||
nint key,
|
||||
nuint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
nint value,
|
||||
nuint value,
|
||||
out bool isNewKey
|
||||
)
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SwissTable"
|
||||
// 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
|
||||
@@ -37,7 +37,7 @@ namespace StgSharp.Collections
|
||||
/// <summary>
|
||||
///
|
||||
/// </summary>
|
||||
public sealed unsafe partial class SwissTable
|
||||
public sealed unsafe partial class SwissTable : IDisposable
|
||||
{
|
||||
|
||||
private const int BucketCount = 1 << EntryMaskWidth;
|
||||
@@ -46,11 +46,19 @@ namespace StgSharp.Collections
|
||||
private const int EntryMaskWidth = 10; // 1024 entries total, 16 per bucket
|
||||
public const int MaxCapacity = BucketCount * BucketSize; // 256 entries per bucket, 1024 buckets
|
||||
|
||||
private readonly Entry* _entries;
|
||||
public static readonly int RequestedNativeMemorySize = Unsafe.SizeOf<Entry>();
|
||||
|
||||
private SwissTable(Entry* buffer)
|
||||
private readonly Entry* _entries;
|
||||
private readonly Action<nuint> _freeHandle;
|
||||
private bool _disposed;
|
||||
|
||||
private SwissTable(
|
||||
Entry* buffer,
|
||||
Action<nuint> freeHandle
|
||||
)
|
||||
{
|
||||
_entries = buffer;
|
||||
_freeHandle = freeHandle;
|
||||
}
|
||||
|
||||
public static SwissTable Create()
|
||||
@@ -61,10 +69,24 @@ namespace StgSharp.Collections
|
||||
// Set all control bytes to 0xFF (empty)
|
||||
Unsafe.InitBlock(&(*buffer)[i].Control, 0xFF, BucketSize);
|
||||
}
|
||||
return new SwissTable(buffer);
|
||||
return new SwissTable(buffer, handle => NativeMemory.AlignedFree((void*)handle));
|
||||
}
|
||||
|
||||
public bool TryAddOrSet(nint key, nint value, out bool isNewKey)
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
_disposed = true;
|
||||
_freeHandle((nuint)_entries);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
public bool TryAddOrSet(
|
||||
nuint key,
|
||||
nuint value,
|
||||
out bool isNewKey
|
||||
)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(CrcMagic, (ulong)key);
|
||||
ushort entry = (ushort)(hash & (BucketCount - 1));
|
||||
@@ -72,9 +94,12 @@ namespace StgSharp.Collections
|
||||
return IBucketScanner.Current.TrySet(key, hash, bucket, value, out isNewKey);
|
||||
}
|
||||
|
||||
public bool TryGet(nint key, out nint value)
|
||||
public bool TryGet(
|
||||
nuint key,
|
||||
out nuint value
|
||||
)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(CrcMagic, (ulong)key);
|
||||
uint hash = BitOperations.Crc32C(CrcMagic, key);
|
||||
ushort entry = (ushort)(hash & (BucketCount - 1));
|
||||
Bucket* bucket = &(*_entries)[entry];
|
||||
return IBucketScanner.Current.TryFind(key, hash, bucket, out value);
|
||||
@@ -87,7 +112,10 @@ namespace StgSharp.Collections
|
||||
*/
|
||||
}
|
||||
|
||||
public bool TryRemove(nint key, out nint value)
|
||||
public bool TryRemove(
|
||||
nuint key,
|
||||
out nuint value
|
||||
)
|
||||
{
|
||||
uint hash = BitOperations.Crc32C(CrcMagic, (ulong)key);
|
||||
ushort entry = (ushort)(hash & (BucketCount - 1));
|
||||
@@ -95,9 +123,23 @@ namespace StgSharp.Collections
|
||||
return IBucketScanner.Current.TryRemove(key, hash, bucket, out value);
|
||||
}
|
||||
|
||||
internal static SwissTable Create(
|
||||
nuint bufferPtr,
|
||||
Action<nuint> FreeHandle
|
||||
)
|
||||
{
|
||||
Entry* buffer = (Entry*)bufferPtr;
|
||||
for (int i = 0; i < BucketCount; i++)
|
||||
{
|
||||
// Set all control bytes to 0xFF (empty)
|
||||
Unsafe.InitBlock(&(*buffer)[i].Control, 0xFF, BucketSize);
|
||||
}
|
||||
return new SwissTable(buffer, FreeHandle);
|
||||
}
|
||||
|
||||
~SwissTable()
|
||||
{
|
||||
NativeMemory.AlignedFree(_entries);
|
||||
Dispose();
|
||||
}
|
||||
|
||||
[InlineArray(BucketCount)]
|
||||
@@ -122,11 +164,27 @@ namespace StgSharp.Collections
|
||||
|
||||
static IBucketScanner Current { get; } = Create();
|
||||
|
||||
bool TryFind(nint key, uint Hash, Bucket* bucket, out nint value);
|
||||
bool TryFind(
|
||||
nuint key,
|
||||
uint Hash,
|
||||
Bucket* bucket,
|
||||
out nuint value
|
||||
);
|
||||
|
||||
unsafe bool TryRemove(nint key, uint hash, Bucket* bucket, out nint value);
|
||||
unsafe bool TryRemove(
|
||||
nuint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
out nuint value
|
||||
);
|
||||
|
||||
bool TrySet(nint key, uint hash, Bucket* bucket, nint value, out bool isNewKey);
|
||||
bool TrySet(
|
||||
nuint key,
|
||||
uint hash,
|
||||
Bucket* bucket,
|
||||
nuint value,
|
||||
out bool isNewKey
|
||||
);
|
||||
|
||||
private static IBucketScanner Create()
|
||||
{
|
||||
@@ -156,7 +214,7 @@ namespace StgSharp.Collections
|
||||
private struct BucketKeyStorage
|
||||
{
|
||||
|
||||
public nint value;
|
||||
public nuint value;
|
||||
|
||||
}
|
||||
|
||||
@@ -164,7 +222,7 @@ namespace StgSharp.Collections
|
||||
private struct BucketValueStorage
|
||||
{
|
||||
|
||||
public nint value;
|
||||
public nuint value;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="ConcurrentBufferStack"
|
||||
// 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
|
||||
@@ -40,7 +40,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
|
||||
// For BufferStack collection builder
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static BufferStack<T> Create<T>(ReadOnlySpan<T> span) where T: unmanaged
|
||||
public static BufferStack<T> Create<T>(
|
||||
ReadOnlySpan<T> span
|
||||
) where T : unmanaged
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
{
|
||||
@@ -58,7 +60,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
|
||||
// For ConcurrentBufferStack collection builder
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static ConcurrentBufferStack<T> CreateConcurrent<T>(ReadOnlySpan<T> span) where T: unmanaged
|
||||
public static ConcurrentBufferStack<T> CreateConcurrent<T>(
|
||||
ReadOnlySpan<T> span
|
||||
) where T : unmanaged
|
||||
{
|
||||
if (span.IsEmpty)
|
||||
{
|
||||
@@ -76,8 +80,10 @@ namespace StgSharp.Mathematics.Memory
|
||||
|
||||
}
|
||||
|
||||
[CollectionBuilder(builderType: typeof(BufferStackBuilder), methodName: nameof(BufferStackBuilder.CreateConcurrent))]
|
||||
public unsafe class ConcurrentBufferStack<T> : IDisposable where T: unmanaged
|
||||
[CollectionBuilder(
|
||||
builderType: typeof(BufferStackBuilder),
|
||||
methodName: nameof(BufferStackBuilder.CreateConcurrent))]
|
||||
public unsafe class ConcurrentBufferStack<T> : IDisposable where T : unmanaged
|
||||
{
|
||||
|
||||
private byte* _buffer;
|
||||
@@ -89,11 +95,11 @@ namespace StgSharp.Mathematics.Memory
|
||||
AllocateBuffer(4);
|
||||
}
|
||||
|
||||
public ConcurrentBufferStack(int capacity)
|
||||
public ConcurrentBufferStack(
|
||||
int capacity
|
||||
)
|
||||
{
|
||||
if (capacity <= 0) {
|
||||
throw new ArgumentOutOfRangeException(nameof(capacity));
|
||||
}
|
||||
ArgumentOutOfRangeException.ThrowIfNegativeOrZero(capacity);
|
||||
|
||||
AllocateBuffer(capacity);
|
||||
}
|
||||
@@ -161,7 +167,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public void Push(T item)
|
||||
public void Push(
|
||||
T item
|
||||
)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
@@ -236,7 +244,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
}
|
||||
}
|
||||
|
||||
public bool TryPop(out T value)
|
||||
public bool TryPop(
|
||||
out T value
|
||||
)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
@@ -269,7 +279,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
|
||||
// Internal method for efficient bulk initialization
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
internal unsafe void InitializeFromSpan(ReadOnlySpan<T> span)
|
||||
internal unsafe void InitializeFromSpan(
|
||||
ReadOnlySpan<T> span
|
||||
)
|
||||
{
|
||||
if (span.IsEmpty) {
|
||||
return;
|
||||
@@ -287,7 +299,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
Volatile.Write(ref _count, span.Length);
|
||||
}
|
||||
|
||||
private void AllocateBuffer(int capacity)
|
||||
private void AllocateBuffer(
|
||||
int capacity
|
||||
)
|
||||
{
|
||||
nuint size = (nuint)sizeof(Header) + (nuint)capacity * (nuint)sizeof(T);
|
||||
_buffer = (byte*)NativeMemory.Alloc(size);
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.Alloc"
|
||||
// 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
|
||||
@@ -34,7 +34,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
public unsafe partial class HybridLayerSegregatedFitAllocator
|
||||
{
|
||||
|
||||
public hlsfHandle Alloc(long size)
|
||||
public partial hlsfHandle Alloc(
|
||||
long size
|
||||
)
|
||||
{
|
||||
if (size > _maxSize) {
|
||||
throw new InvalidOperationException("Requested size exceeds maximum allocatable size.");
|
||||
@@ -91,7 +93,10 @@ namespace StgSharp.Mathematics.Memory
|
||||
/// <param name="sizeToSlice">
|
||||
///
|
||||
/// </param>
|
||||
private void SliceMemory(Entry* e, long sizeToSlice)
|
||||
private void SliceMemory(
|
||||
Entry* e,
|
||||
long sizeToSlice
|
||||
)
|
||||
{
|
||||
Entry* next = e;
|
||||
long remain = sizeToSlice; // remained size to slice
|
||||
@@ -189,7 +194,9 @@ namespace StgSharp.Mathematics.Memory
|
||||
/// <returns>
|
||||
/// True if allocation succeeded
|
||||
/// </returns>
|
||||
private bool TryAllocateNew32MBBlock(out Entry* handle)
|
||||
private bool TryAllocateNew32MBBlock(
|
||||
out Entry* handle
|
||||
)
|
||||
{
|
||||
nuint pos = _spareMemory->Position;
|
||||
long remain = _spareMemory->Size;
|
||||
@@ -219,7 +226,11 @@ namespace StgSharp.Mathematics.Memory
|
||||
/// <summary>
|
||||
/// Helper method for allocation - converts BucketNode to Entry
|
||||
/// </summary>
|
||||
private bool TryPopLevelForAllocation(int levelIndex, int sizeIndex, out Entry* entry)
|
||||
private bool TryPopLevelForAllocation(
|
||||
int levelIndex,
|
||||
int sizeIndex,
|
||||
out Entry* entry
|
||||
)
|
||||
{
|
||||
if (TryPopLevel(levelIndex, sizeIndex, out BucketNode* bucketNode))
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator.Free"
|
||||
// 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
|
||||
@@ -55,7 +55,7 @@ namespace StgSharp.Mathematics.Memory
|
||||
/// 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 void Collect()
|
||||
public partial void Collect()
|
||||
{
|
||||
Entry* cur = _spareMemory->NextNear;
|
||||
long size = 0;
|
||||
@@ -90,7 +90,10 @@ namespace StgSharp.Mathematics.Memory
|
||||
/// Handle to the allocated memory block
|
||||
/// <param name="collectPolicy">
|
||||
/// </param>
|
||||
public void Free(hlsfHandle handle, FreePolicy collectPolicy = FreePolicy.NoCollect)
|
||||
public void Free(
|
||||
hlsfHandle handle,
|
||||
FreePolicy collectPolicy = FreePolicy.NoCollect
|
||||
)
|
||||
{
|
||||
Entry* e = handle.EntryHandle;
|
||||
if (e == null) {
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="HLSFAllocator"
|
||||
// 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,6 +30,7 @@ using System.Buffers;
|
||||
using System.Runtime.CompilerServices;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using hlsfHandle = StgSharp.Mathematics.Memory.HybridLayerSegregatedFitAllocationHandle;
|
||||
|
||||
namespace StgSharp.Mathematics.Memory
|
||||
{
|
||||
@@ -48,17 +49,19 @@ namespace StgSharp.Mathematics.Memory
|
||||
private readonly nuint _size;
|
||||
private readonly SlabAllocator<Entry> _entries;
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize)
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize
|
||||
)
|
||||
{
|
||||
if (byteSize < 64) {
|
||||
throw new InvalidOperationException();
|
||||
}
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
_size = byteSize;
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, 16);
|
||||
_align = 16;
|
||||
|
||||
_bucketHeads = new BucketNode *[_levelSizeArray.Length * 3 + 1];
|
||||
_bucketHeads = new BucketNode*[_levelSizeArray.Length * 3 + 1];
|
||||
|
||||
_spareMemory = (Entry *)_entries.Allocate();
|
||||
_spareMemory->State = EntryState.Spare;
|
||||
@@ -70,9 +73,12 @@ namespace StgSharp.Mathematics.Memory
|
||||
_maxSize = 32U * 1024U * 1024U;
|
||||
}
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize, uint maxBlockSize)
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
uint maxBlockSize
|
||||
)
|
||||
{
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, 16);
|
||||
_align = 16;
|
||||
_size = byteSize;
|
||||
@@ -97,13 +103,16 @@ namespace StgSharp.Mathematics.Memory
|
||||
_maxSize = maxBlockSize;
|
||||
}
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align)
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align
|
||||
)
|
||||
{
|
||||
if (align < 16 || (align & (align - 1)) != 0) {
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte *)NativeMemory.AlignedAlloc(byteSize, (nuint)align);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
@@ -120,13 +129,17 @@ namespace StgSharp.Mathematics.Memory
|
||||
_maxSize = 32U * 1024U * 1024U;
|
||||
}
|
||||
|
||||
public HybridLayerSegregatedFitAllocator(nuint byteSize, int align, long maxBlockSize)
|
||||
public HybridLayerSegregatedFitAllocator(
|
||||
nuint byteSize,
|
||||
int align,
|
||||
long maxBlockSize
|
||||
)
|
||||
{
|
||||
if (align < 16 || (align & (align - 1)) != 0) {
|
||||
throw new ArgumentException(
|
||||
"Alignment must be a power of two and at least 16 bytes.");
|
||||
}
|
||||
_entries = SlabAllocator<Entry>.Create(64, SlabBufferLayout.Chunked, false);
|
||||
_entries = SlabAllocator.Create<Entry>(64, SlabBufferLayout.Chunked, false);
|
||||
m_Buffer = (byte*)NativeMemory.AlignedAlloc(byteSize, (nuint)align);
|
||||
_align = align;
|
||||
_size = byteSize;
|
||||
@@ -151,13 +164,21 @@ namespace StgSharp.Mathematics.Memory
|
||||
_maxSize = maxBlockSize;
|
||||
}
|
||||
|
||||
public partial hlsfHandle Alloc(
|
||||
long size
|
||||
);
|
||||
|
||||
public partial void Collect();
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
Dispose(disposing:true);
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
protected virtual void Dispose(bool disposing)
|
||||
protected virtual void Dispose(
|
||||
bool disposing
|
||||
)
|
||||
{
|
||||
if (!disposedValue)
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="L4.CommandQueue"
|
||||
// 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
|
||||
@@ -37,7 +37,7 @@ namespace StgSharp.HighPerformance.Memory
|
||||
public unsafe partial class L4
|
||||
{
|
||||
|
||||
private CommandQueue _commandQueue;
|
||||
private readonly CommandQueue _commandQueue;
|
||||
|
||||
private struct CommandQueue
|
||||
{
|
||||
@@ -56,7 +56,9 @@ namespace StgSharp.HighPerformance.Memory
|
||||
/// <returns>
|
||||
/// True if there is a command to execute, otherwise false
|
||||
/// </returns>
|
||||
public bool TryDequeue(out nint command)
|
||||
public bool TryDequeue(
|
||||
out nint command
|
||||
)
|
||||
{
|
||||
if (_curRead >= 64)
|
||||
{
|
||||
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="L4.EvictionRing"
|
||||
// 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
|
||||
@@ -45,14 +45,16 @@ namespace StgSharp.HighPerformance.Memory
|
||||
|
||||
private nint[] _bucket;
|
||||
|
||||
public void AddNode(EvictionRingNode* node)
|
||||
public void AddNode(
|
||||
CacheLineHead* node
|
||||
)
|
||||
{
|
||||
nint address = node->_address;
|
||||
int hash = (int)(address % (nint)_bucket.Length);
|
||||
nint head = _bucket[hash];
|
||||
if (head != 0)
|
||||
{
|
||||
EvictionRingNode* headNode = (EvictionRingNode*)head;
|
||||
CacheLineHead* headNode = (CacheLineHead*)head;
|
||||
headNode->_prev = node;
|
||||
node->_next = headNode;
|
||||
}
|
||||
@@ -62,17 +64,23 @@ namespace StgSharp.HighPerformance.Memory
|
||||
}
|
||||
|
||||
[StructLayout(LayoutKind.Sequential)]
|
||||
private unsafe struct EvictionRingNode
|
||||
private unsafe struct CacheLineHead
|
||||
{
|
||||
|
||||
public EvictionRingNode* _next;
|
||||
public EvictionRingNode* _prev;
|
||||
public int _level;
|
||||
public int _refCount;
|
||||
public nint _address;
|
||||
internal CacheLineHead* _next;
|
||||
internal CacheLineHead* _prev;
|
||||
internal int _level;
|
||||
internal int _refCount;
|
||||
internal nuint _address;
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void IncrementRefCount()
|
||||
public void Unuse()
|
||||
{
|
||||
_ = Interlocked.Decrement(ref _refCount);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public void Use()
|
||||
{
|
||||
_ = Interlocked.Increment(ref _refCount);
|
||||
}
|
||||
|
||||
@@ -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];
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
//-----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SlabAllocator.Fixed"
|
||||
// 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 StgSharp.Collections;
|
||||
using StgSharp.Mathematics.Memory;
|
||||
using StgSharp.Threading;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace StgSharp.HighPerformance.Memory
|
||||
{
|
||||
internal unsafe class FixedSlabAllocator<T> : SlabAllocator<T> where T : unmanaged
|
||||
{
|
||||
|
||||
private Action<nuint> _freeHandle;
|
||||
|
||||
private bool _disposed;
|
||||
private CapacityFixedStack<nuint> _recycle;
|
||||
private readonly int _elementSize = Unsafe.SizeOf<T>();
|
||||
|
||||
private nuint _buffer;
|
||||
|
||||
private nuint _Capacity, _currentIndex;
|
||||
|
||||
public FixedSlabAllocator(
|
||||
nuint buffer,
|
||||
nuint count,
|
||||
Action<nuint> freeHandle
|
||||
)
|
||||
{
|
||||
_freeHandle = freeHandle;
|
||||
_buffer = buffer;
|
||||
|
||||
_Capacity = count;
|
||||
int initCount = count switch
|
||||
{
|
||||
<= 4 => 4,
|
||||
> 64 => 64,
|
||||
_ => (int)count
|
||||
};
|
||||
Span<nuint> span = stackalloc nuint[initCount];
|
||||
for (int i = 0; i < initCount; i++) {
|
||||
span[i] = _buffer + (nuint)(i * _elementSize);
|
||||
}
|
||||
_recycle = CapacityFixedStackBuilder.Create(span);
|
||||
_Capacity = count;
|
||||
_currentIndex = (nuint)initCount;
|
||||
}
|
||||
|
||||
public override nuint Allocate()
|
||||
{
|
||||
if (_recycle.TryPop(out nuint handle)) {
|
||||
return handle;
|
||||
}
|
||||
|
||||
// nuint bufferHandle = _currentBuffer;
|
||||
nuint curIndex = _currentIndex;
|
||||
if (curIndex >= _Capacity) {
|
||||
return 0;
|
||||
}
|
||||
curIndex = _currentIndex;
|
||||
_currentIndex += 1;
|
||||
return _buffer + ((nuint)_elementSize * curIndex);
|
||||
}
|
||||
|
||||
public override void Dispose()
|
||||
{
|
||||
if (_disposed) {
|
||||
return;
|
||||
}
|
||||
_disposed = true;
|
||||
_freeHandle(_buffer);
|
||||
_recycle = null!;
|
||||
GC.SuppressFinalize(this);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void EnterBufferReading() { }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void ExitBufferReading() { }
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override void Free(
|
||||
nuint index
|
||||
)
|
||||
{
|
||||
_recycle.Push(index);
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public override SlabAllocationHandle<T> ReadAllocation()
|
||||
{
|
||||
return new SlabAllocationHandle<T>(this, false);
|
||||
}
|
||||
|
||||
~FixedSlabAllocator()
|
||||
{
|
||||
Dispose();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -2,8 +2,8 @@
|
||||
// -----------------------------------------------------------------------
|
||||
// file="SlabAllocator"
|
||||
// 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
|
||||
@@ -25,15 +25,14 @@
|
||||
//
|
||||
// -----------------------------------------------------------------------
|
||||
// -----------------------------------------------------------------------
|
||||
using StgSharp.HighPerformance.Memory;
|
||||
using System;
|
||||
|
||||
namespace StgSharp.Mathematics.Memory
|
||||
{
|
||||
public abstract class SlabAllocator<T> : IDisposable where T: unmanaged
|
||||
public static class SlabAllocator
|
||||
{
|
||||
|
||||
public abstract nuint Allocate();
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new SLAB allocator instance with the specified configuration.
|
||||
/// </summary>
|
||||
@@ -78,7 +77,11 @@ namespace StgSharp.Mathematics.Memory
|
||||
/// /// allocated independently, avoiding the memory relocation issues present in
|
||||
/// sequential /// layouts. </para>
|
||||
/// </remarks>
|
||||
public static SlabAllocator<T> Create(nuint count, SlabBufferLayout layout, bool concurrentSupport = true)
|
||||
public static SlabAllocator<T> Create<T>(
|
||||
nuint count,
|
||||
SlabBufferLayout layout,
|
||||
bool concurrentSupport = true
|
||||
) where T : unmanaged
|
||||
{
|
||||
if (concurrentSupport) {
|
||||
return layout switch
|
||||
@@ -96,13 +99,32 @@ namespace StgSharp.Mathematics.Memory
|
||||
};
|
||||
}
|
||||
|
||||
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
||||
public static SlabAllocator<T> FromBuffer<T>(
|
||||
nuint buffer,
|
||||
nuint count,
|
||||
Action<nuint> freeHandle
|
||||
) where T : unmanaged
|
||||
{
|
||||
return new FixedSlabAllocator<T>(buffer, count, freeHandle);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public abstract class SlabAllocator<T> : IDisposable where T : unmanaged
|
||||
{
|
||||
|
||||
public abstract nuint Allocate();
|
||||
|
||||
public abstract void Dispose();
|
||||
|
||||
public abstract void EnterBufferReading();
|
||||
|
||||
public abstract void ExitBufferReading();
|
||||
|
||||
public abstract void Free(nuint index);
|
||||
public abstract void Free(
|
||||
nuint index
|
||||
);
|
||||
|
||||
public abstract SlabAllocationHandle<T> ReadAllocation();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user