Table of Contents

Struct TLSF

Namespace
Misaki.HighPerformance.LowLevel.Buffer
Assembly
Misaki.HighPerformance.LowLevel.dll

A Two-Level Segregated Fit (TLSF) memory allocator. Guarantees O(1) allocation and deallocation with very low fragmentation. Note: This is a single-threaded allocator. Wrap it in a lock for thread safety.

public struct TLSF : IMemoryAllocator<TLSF, TLSF.CreationOptions>, IDisposable
Implements
Inherited Members

Constructors

TLSF(nuint, nuint)

public TLSF(nuint alignment, nuint chunkSize)

Parameters

alignment nuint
chunkSize nuint

Methods

Allocate(nuint, nuint, AllocationOption)

Allocates a block of memory with the specified size, alignment, and allocation options.

public void* Allocate(nuint size, nuint alignment, AllocationOption allocationOption = AllocationOption.None)

Parameters

size nuint

The size of the memory block to allocate.

alignment nuint

The alignment of the memory block.

allocationOption AllocationOption

Returns

void*

A pointer to the allocated memory block. null if allocation fails.

Remarks

The returned pointer must be freed using the Free(void*) method to avoid memory leaks.

Collect()

Collects and decommits free memory back to the operating system.

public void Collect()

Create(in CreationOptions)

Creates a new instance of the allocator with the specified options.

public static TLSF Create(in TLSF.CreationOptions opts)

Parameters

opts TLSF.CreationOptions

The options for creating the allocator.

Returns

TLSF

The created allocator instance.

Dispose()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.

public void Dispose()

Free(void*)

Frees a previously allocated block of memory.

public void Free(void* ptr)

Parameters

ptr void*

A pointer to the memory block to free.

Remarks

The pointer must have been returned by a previous call to the Allocate(nuint, nuint, AllocationOption) or Reallocate(void*, nuint, nuint, nuint, AllocationOption) method. After calling this method, the pointer is no longer valid and must not be used.

Reallocate(void*, nuint, nuint, nuint, AllocationOption)

Reallocates a block of memory to a new size and alignment.

public void* Reallocate(void* ptr, nuint oldSize, nuint newSize, nuint alignment, AllocationOption allocationOption = AllocationOption.None)

Parameters

ptr void*

A pointer to the memory block to reallocate.

oldSize nuint

The size of the original memory block.

newSize nuint

The size of the new memory block.

alignment nuint

The alignment of the new memory block.

allocationOption AllocationOption

The allocation options.

Returns

void*

A pointer to the reallocated memory block. null if reallocation fails.

Remarks

The returned pointer must be freed using the Free(void*) method to avoid memory leaks. If the reallocation fails, the original pointer remains valid and must be freed by the caller.