### Build and test commands from project root Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/AGENTS.md Run from the project root directory. Builds the solution in Release configuration and runs all tests. All tests must pass before submitting. ```sh cd src dotnet build -c Release dotnet test -c Release ``` -------------------------------- ### Create TlsfAllocator with BasicChunkAllocator and 64-byte alignment Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Creates a TlsfAllocator instance using a BasicChunkAllocator and a minimum alignment of 64 bytes. ```csharp var chunkAllocator = new BasicChunkAllocator(); // Create an allocator with a minimum alignment of 64 bytes var allocator = new TlsfAllocator(chunkAllocator, 64); ``` -------------------------------- ### BasicChunkAllocator implementation using native memory Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Implements IMemoryChunkAllocator with native memory allocation via NativeMemory.AlignedAlloc and AlignedFree. Uses a fixed chunk size of 65536 bytes and alignment of 64 bytes. ```csharp /// /// Implementation of a chunk allocator for the TLSF allocator using native memory. /// public unsafe class BasicChunkAllocator : IMemoryChunkAllocator { private readonly Dictionary _chunks = new Dictionary(); private const int ChunkSize = 65536; public bool TryAllocateChunk(MemorySize minSize, out MemoryChunk chunk) { var blockSize = (uint)Math.Max(ChunkSize, (int)minSize.Value); var address = NativeMemory.AlignedAlloc(blockSize, 64); chunk = new MemoryChunk((ulong)_chunks.Count, (ulong)address, blockSize); _chunks.Add(_chunks.Count, chunk); return true; } public void FreeChunk(MemoryChunkId chunkId) { var index = (int)chunkId.Value; var chunk = _chunks[index]; NativeMemory.AlignedFree((void*)(ulong)chunk.BaseAddress); _chunks.Remove(index); } } ``` -------------------------------- ### Customize alignment to 128 bytes Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Creates a TlsfAllocator with a custom alignment of 128 bytes. ```csharp var allocator = new TlsfAllocator(chunkAllocator, 128); ``` -------------------------------- ### Allocate memory with Allocate method Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Allocates a memory block of 128 bytes using the Allocate method, returning a TlsfAllocation structure. ```csharp var allocation = allocator.Allocate(128); ``` -------------------------------- ### Free memory using Free method Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Frees allocated memory by passing the TlsfAllocationToken from the TlsfAllocation structure. An implicit cast operator allows passing the allocation directly. ```csharp allocator.Free(allocation.Token); // Or you can use the implicit cast operator: // allocator.Free(allocation); ``` -------------------------------- ### Reset allocator to initial state Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Resets the allocator by freeing all memory chunks via IMemoryChunkAllocator.FreeChunk and restoring the initial state. ```csharp allocator.Reset(); ``` -------------------------------- ### TlsfAllocation record struct definition Source: https://github.com/xenoatom/xenoatom.allocators/blob/main/doc/readme.md Defines the TlsfAllocation structure with Token, Address, and Size properties representing the allocated block. ```csharp public readonly record struct TlsfAllocation { /// /// Gets the index of the block allocated (used internally by the allocator). /// public readonly TlsfAllocationToken Token; /// /// Gets the address of the allocated block. /// public readonly MemoryAddress Address; /// /// Gets the size of the allocated block. /// public readonly MemorySize Size; } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.