Improve FreeList/TLSF allocators: alignment, GC, decommit

- FreeList: enforce min 16B alignment, use GCHandle for SharedState lifetime, switch to AllocZeroed, and use MemoryUtility for oversized allocs
- Add FreeList.CollectLocal() to flush thread-local caches
- TLSF: add decommitted flag, support front splitting for alignment, add Collect() to decommit large free blocks, use Munmap for cleanup
- Add VirtualMemoryBlock for virtual memory management
- Add tests for CollectLocal (FreeList) and Collect (TLSF)
- Update default allocator config and minor .csproj cleanup
This commit is contained in:
2026-05-07 23:25:04 +09:00
parent d2c165bbe5
commit 259ff36100
7 changed files with 376 additions and 79 deletions

View File

@@ -177,4 +177,33 @@ public unsafe class TestFreeList
freeList.Dispose();
freeList.Dispose(); // Should not throw
}
[TestMethod]
public void CollectLocal()
{
const int threadCount = 8;
const int iterations = 1000;
using var freeList = new FreeList(8, 64 * 1024);
var threads = new Thread[threadCount];
for (var i = 0; i < threadCount; i++)
{
threads[i] = new Thread(() =>
{
for (var j = 0; j < iterations; j++)
{
var ptr = freeList.Allocate(128, 8);
Assert.IsNotNull(ptr);
freeList.Free(ptr);
}
freeList.CollectLocal();
});
}
foreach (var t in threads)
t.Start();
foreach (var t in threads)
t.Join();
}
}

View File

@@ -73,4 +73,16 @@ public class TestTlsfAllocator
allocator.Free(ptrs[i]);
}
}
[TestMethod]
public unsafe void TestCollect()
{
using var allocator = TLSF.Create(new TLSF.CreationOptions { alignment = 16, initialChunkSize = 4096 * 1024 });
var ptr = allocator.Allocate(1024 * 1024, 16);
Assert.IsTrue(ptr != null);
allocator.Free(ptr);
allocator.Collect();
}
}