//----------------------------------------------------------------------- // ----------------------------------------------------------------------- // file="hlsf-test" // 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.HighPerformance.Memory; using System; using System.Collections.Generic; using System.Diagnostics; using System.Linq; using System.Text; using System.Threading.Tasks; namespace StgSharp { internal partial class Program { public static void TestHlsf() { // HLSF test arena Console.WriteLine( "=== HLSF (Hybrid Layer Segregated Fit Allocator) Single-threaded Stress Test ===\n"); HybridLayerSegregatedFitAllocator hlsfAllocator = new HybridLayerSegregatedFitAllocator(1024 * 1024 * 1024); // === 简洁交替测试:100次alloc与100次free交替进行,直到触发异常或达到最大循环 === Console.WriteLine("\n=== 简洁交替测试:100次alloc + 100次free 交替 ==="); int maxRounds = 10000; // 可调整 uint[] sizes = { 64u, 128u, 256u, 512u, 1024u }; Random rnd = new Random(42); for (int round = 1; round <= maxRounds; round++) { // 100 次分配 List batch = new List(100); for (int i = 0; i < 100; i++) { uint sz = sizes[rnd.Next(sizes.Length)]; batch.Add(hlsfAllocator.Alloc(sz)); Console.WriteLine($"alloced:{i}"); } // 100 次释放(逆序) for (int i = batch.Count - 1; i >= 0; i--) { hlsfAllocator.Free(batch[i]); Console.WriteLine($"freed:{i}"); } if (round % 100 == 0) { Console.WriteLine($"[交替测试] 已完成 {round} 轮 100alloc+100free"); } } Console.WriteLine("[交替测试] 正常结束,无异常触发"); } } }