add from giga-device, new branch added

This commit is contained in:
Nitload Youmu
2025-08-21 20:44:30 +08:00
parent 43bbc06bbb
commit fb9398e8be
135 changed files with 1505 additions and 1107 deletions
@@ -28,6 +28,8 @@
//
// -----------------------------------------------------------------------
// -----------------------------------------------------------------------
using Microsoft.Win32;
using StgSharp.Threading;
using System;
@@ -118,7 +120,7 @@ namespace StgSharp.HighPerformance.Memory
throw new InvalidOperationException("Entry position offset is not aligned to 64 bytes.");
}
int minLevel = Math.Max((BitOperations.TrailingZeroCount(originOffset) - 6) / 2, 0);
int i = minLevel >= levelSizeArray.Length ? levelSizeArray.Length : minLevel;
int i = minLevel >= levelSizeArray.Length ? levelSizeArray.Length - 1 : minLevel;
for (; i < levelSizeArray.Length; i++)
{
int levelSize = levelSizeArray[i];
@@ -133,19 +135,19 @@ namespace StgSharp.HighPerformance.Memory
Entry* newEntry = (Entry*)_entries.Allocate();
newEntry->Position = (nuint)((ulong)m_Buffer + finalOffset);
newEntry->Size = (uint)s;
newEntry->LockBuffer = 0;
BucketNode* bucket = (BucketNode*)newEntry->Position;
bucket->EntryRef = newEntry;
bucket->PrevLock = 0;
bucket->NextLock = 0;
bucket->LockBuffer = 0;
newEntry->State = EntryState.ThreadOccupied;
// link new entry
newEntry->PreviousNear = (ulong)e;
e->NextNear = (ulong)newEntry;
newEntry->PreviousNear = (ulong)current;
newEntry->NextNear = n;
current->NextNear = (ulong)newEntry;
next->PreviousNear = (ulong)newEntry;
next->NextNear = EmptyHandle;
newEntry->Level = i;
PushLevel(i, blockCount - 1, bucket);
// set new entry
@@ -164,34 +166,33 @@ namespace StgSharp.HighPerformance.Memory
ReleaseEntryPrevLock(next);
return;
}
for (; i >= 0; i--)
{
int levelSize = levelSizeArray[i];
int blockCount = remainSize / levelSize % 4;
if (blockCount == 0)
{
break;
continue;
}
int s = blockCount * levelSize;
// create and init new entry
Entry* newEntry = (Entry*)_entries.Allocate();
newEntry->LockBuffer = 0;
newEntry->Position = (nuint)((ulong)m_Buffer + finalOffset);
newEntry->Size = (uint)s;
BucketNode* bucket = (BucketNode*)newEntry->Position;
bucket->EntryRef = newEntry;
bucket->PrevLock = 0;
bucket->NextLock = 0;
bucket->LockBuffer = 0;
newEntry->State = EntryState.ThreadOccupied;
// link new entry
newEntry->PreviousNear = (ulong)e;
e->NextNear = (ulong)newEntry;
newEntry->PreviousNear = (ulong)current;
current->NextNear = (ulong)newEntry;
newEntry->NextNear = n;
next->PreviousNear = (ulong)newEntry;
next->NextNear = EmptyHandle;
newEntry->Level = i;
PushLevel(i, blockCount - 1, bucket);
// set new entry
@@ -203,6 +204,10 @@ namespace StgSharp.HighPerformance.Memory
remainSize -= s;
ReleaseEntryNextLock(old);
AcquireEntryNextLock(current);
if (remainSize <= 0)
{
break;
}
}
}
catch (Exception)
@@ -232,6 +237,18 @@ namespace StgSharp.HighPerformance.Memory
nuint blockSize = (nuint)levelSizeArray[^1] * 2; // Use last level size (16MB)
nuint newPos = pos + blockSize;
ulong p;
Entry* previous;
do
{
p = Volatile.Read(ref _spareMemory->PreviousNear);
previous = (Entry*)p;
if (previous != null) {
AcquireEntryNextLock(previous);
}
AcquireEntryPrevLock(_spareMemory);
break;
} while (true);
if (_spareMemory->Size <= 0)
{
handle = null;
@@ -252,8 +269,6 @@ namespace StgSharp.HighPerformance.Memory
handle->Position = pos;
handle->Size = (uint)blockSize;
SetEntryState(handle, EntryState.ThreadOccupied);
handle->NextNear = EmptyHandle;
handle->PreviousNear = EmptyHandle;
handle->Level = GetLevelFromSize((uint)blockSize);
handle->PrevLock = 0;
@@ -264,6 +279,14 @@ namespace StgSharp.HighPerformance.Memory
BucketNode* b = (BucketNode*)handle->Position;
b->EntryRef = handle;
PushLevel(handle->Level, DetermineSegmentIndex((uint)blockSize, handle->Level), b);
// update position linked list
handle->NextNear = (ulong)_spareMemory;
handle->PreviousNear = p;
_spareMemory->PreviousNear = (ulong)handle;
if (previous != null) {
previous->NextNear = (ulong)handle;
}
return true;
}
catch
@@ -271,6 +294,11 @@ namespace StgSharp.HighPerformance.Memory
handle = null;
return false;
}
finally
{
ReleaseEntryNextLock(previous);
ReleaseEntryPrevLock(_spareMemory);
}
}
/// <summary>
@@ -354,11 +354,10 @@ namespace StgSharp.HighPerformance.Memory
[FieldOffset(16)] internal Entry* EntryRef; // Pointer to corresponding Entry
[FieldOffset(28)] internal bool IsInBucket; // Whether in bucket
[FieldOffset(36)] internal int NextLock; // 后继指针锁(控制NextLevel指针操作)
// 双锁设计:分离前后指针操作
[FieldOffset(32)] internal int PrevLock; // 前驱指针锁(控制PreviousLevel指针操作)
[FieldOffset(36)] internal int NextLock;
[FieldOffset(32)] internal int PrevLock;
[FieldOffset(24)] internal uint Magic; // Magic number verification
[FieldOffset(32)] internal ulong LockBuffer;
[FieldOffset(0)] internal ulong NextLevel; // Bucket stack linked list: next pointer
[FieldOffset(8)] internal ulong PreviousLevel; // Bucket stack linked list: previous pointer
@@ -0,0 +1,82 @@
//-----------------------------------------------------------------------
// -----------------------------------------------------------------------
// file="HLSFAllocator.Debug.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.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StgSharp.HighPerformance.Memory
{
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 = (Entry*)cur->PreviousNear;
if (singleStep) {
Console.ReadKey();
}
}
}
}
public IEnumerable<string> ShowEntryLayout()
{
lock (_lock)
{
Stack<string> layout = [];
Entry* cur = _spareMemory;
while (cur != null)
{
layout.Push(EntryToString(cur));
cur = (Entry*)cur->PreviousNear;
}
return layout;
}
}
private static string EntryToString(Entry* e)
{
BucketNode* b = (BucketNode*)e->Position;
return $"{e->PreviousNear}<-- Position{(ulong)e} Bucket:{{{e->Position},{e->Level},{e->Size}}} -->{e->NextNear}";
}
}
}
@@ -208,8 +208,7 @@ namespace StgSharp.HighPerformance.Memory
if (entry is null) {
return;
}
entry->PreviousNear = EmptyHandle;
entry->Position = EmptyHandle;
AcquireEntrySpinLock(entry);
_entries.Free((nuint)entry);
}
@@ -263,11 +262,11 @@ namespace StgSharp.HighPerformance.Memory
{
[FieldOffset(28)] internal int Level; // Level of the block in the allocator
[FieldOffset(40)] internal int NextLock;
[FieldOffset(32)] internal int NextLock;
[FieldOffset(36)] internal int PrevLock;
[FieldOffset(32)] internal int State; // Node state (using int for atomic operations)
[FieldOffset(40)] internal int State; // Node state (using int for atomic operations)
[FieldOffset(24)] internal uint Size; // Block size information
[FieldOffset(32)] internal ulong LockBuffer;
[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
@@ -63,6 +63,7 @@ namespace StgSharp.HighPerformance.Memory
_spareMemory = (Entry*)_entries.Allocate();
SetEntryState(_spareMemory, EntryState.ThreadOccupied);
_spareMemory->PreviousNear = EmptyHandle;
_spareMemory->NextNear = EmptyHandle;
_spareMemory->Position = (nuint)m_Buffer;
_spareMemory->PrevLock = 0;
@@ -85,6 +86,7 @@ namespace StgSharp.HighPerformance.Memory
_spareMemory = (Entry*)_entries.Allocate();
SetEntryState(_spareMemory, EntryState.ThreadOccupied);
_spareMemory->PreviousNear = EmptyHandle;
_spareMemory->NextNear = EmptyHandle;
_spareMemory->Position = (nuint)m_Buffer;
_spareMemory->PrevLock = 0;
@@ -1,17 +1,17 @@
{
"format": 1,
"restore": {
"D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj": {}
"D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj": {}
},
"projects": {
"D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj": {
"D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectUniqueName": "D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectName": "StgSharp.Common",
"projectPath": "D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectPath": "D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj",
"packagesPath": "C:\\Users\\GF\\.nuget\\packages\\",
"outputPath": "D:\\PlatformDev\\StgSharp.Common\\obj\\",
"outputPath": "D:\\platform-dev\\StgSharp.Common\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
+3 -3
View File
@@ -131,11 +131,11 @@
"project": {
"version": "1.0.0",
"restore": {
"projectUniqueName": "D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectUniqueName": "D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectName": "StgSharp.Common",
"projectPath": "D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectPath": "D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj",
"packagesPath": "C:\\Users\\GF\\.nuget\\packages\\",
"outputPath": "D:\\PlatformDev\\StgSharp.Common\\obj\\",
"outputPath": "D:\\platform-dev\\StgSharp.Common\\obj\\",
"projectStyle": "PackageReference",
"fallbackFolders": [
"C:\\Program Files (x86)\\Microsoft Visual Studio\\Shared\\NuGetPackages"
+2 -2
View File
@@ -1,8 +1,8 @@
{
"version": 2,
"dgSpecHash": "IdsHyYxOL3c=",
"dgSpecHash": "DkHI8xq62u8=",
"success": true,
"projectFilePath": "D:\\PlatformDev\\StgSharp.Common\\StgSharp.Common.csproj",
"projectFilePath": "D:\\platform-dev\\StgSharp.Common\\StgSharp.Common.csproj",
"expectedPackageFiles": [
"C:\\Users\\GF\\.nuget\\packages\\microsoft.csharp\\4.7.0\\microsoft.csharp.4.7.0.nupkg.sha512",
"C:\\Users\\GF\\.nuget\\packages\\system.runtime.compilerservices.unsafe\\6.1.0\\system.runtime.compilerservices.unsafe.6.1.0.nupkg.sha512"
@@ -14,7 +14,7 @@ using System.Reflection;
[assembly: System.Reflection.AssemblyCompanyAttribute("StgSharp.Common")]
[assembly: System.Reflection.AssemblyConfigurationAttribute("Debug")]
[assembly: System.Reflection.AssemblyFileVersionAttribute("1.0.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0")]
[assembly: System.Reflection.AssemblyInformationalVersionAttribute("1.0.0+43bbc06bbb3d23149f6a679071271659f34e1d43")]
[assembly: System.Reflection.AssemblyProductAttribute("StgSharp.Common")]
[assembly: System.Reflection.AssemblyTitleAttribute("StgSharp.Common")]
[assembly: System.Reflection.AssemblyVersionAttribute("1.0.0.0")]
@@ -1 +1 @@
f4d9987a147cd56615859f7799a7b3325552a9ea69fa7ac01a77a28b864dcfbd
3c0abac8eb888216bec81f1c3fe248bb1bb8337dd54147a73978611ea0f8743f
@@ -8,7 +8,7 @@ build_property.PlatformNeutralAssembly =
build_property.EnforceExtendedAnalyzerRules =
build_property._SupportedPlatformList = Linux,macOS,Windows
build_property.RootNamespace = StgSharp.Common
build_property.ProjectDir = D:\PlatformDev\StgSharp.Common\
build_property.ProjectDir = D:\platform-dev\StgSharp.Common\
build_property.EnableComHosting =
build_property.EnableGeneratedComInterfaceComImportInterop =
build_property.EffectiveAnalysisLevelStyle = 10.0
@@ -1 +1 @@
aa914b97876f1bc1d82e24b3c165e62bfcaadf6091a43326b0bf70eb6374d818
f3e761709faa39163cef42bb7af63ae81be49ea77636aa00cb9b935c2ac6dd3c
@@ -1 +1 @@
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/6cc1742705b290c6de14ad7ff4cfccb06bc96ad3/*"}}
{"documents":{"D:\\NitloadIndustry\\研究项目\\1-Flagship\\TH2020\\ProjectFile\\内核源代码\\*":"https://raw.githubusercontent.com/Nitload-NSI/CCD_Github/eabd6a2379821e8f68b51968d6ec1c8bf75f7e15/*"}}