Skip to content

Repository files navigation

About

This is smmalloc allocator version for Unity Engine with support of AllocatorManager.IAllocator and Unity.Collections.

smmalloc is a fast and efficient "proxy" allocator designed to handle many small allocations/deallocations in heavy multithreaded scenarios. The allocator created for using in applications where the performance is critical such as video games. Designed to speed up the typical memory allocation pattern in C++ such as many small allocations and deletions. This is a proxy allocator means that smmalloc handles only specific allocation sizes and pass-through all other allocations to generic heap allocator.

Dependencies

  • Unity Burst: 1.8.8
  • Unity Collections: 2.1.4

Supported Platforms

  • Windows x64

Usage

Create a new allocator instance

// 8 buckets, 4 BM eachSmAllocatorUtility.Create(8,4*1024*1024,outvarallocator);

Destroy the allocator instance and free allocated memory

SmAllocatorUtility.Destroy(refallocator);

Allocate memory block

// buffer with 100 int elements (400 bytes memory block)varbuffer=SmAllocatorUtility.Malloc<int>(100,UnsafeUtility.SizeOf<int>(),UnsafeUtility.AlignOf<int>(),allocator.Handle);

P.S.: Don't forget unsafe word!

Deallocate memory block

SmAllocatorUtility.Free(100,buffer,allocator.Handle);

P.S.: Don't forget unsafe word!

Allocate NativeArray from Unity.Collections package

// NativeArray<T> with int 100 elementsvararray=SmAllocatorUtility.AllocateArray<int>(100,refallocator);

Deallocate NativeArray from Unity.Collections package

SmAllocatorUtility.DisposeArray(refarray);

Allocate another collection from Unity.Collections package

For example NativeList:

// allocatevarnativeList=newNativeList<int>(allocator.Handle);// deallocatenativeList.Dispose();

Or NativeHashSet:

// allocate with 10 initial capacityvarnativeHashSet=newNativeHashSet<int>(10,allocator.Handle);// deallocatenativeHashSet.Dispose();

Create thread cache for a current thread

// create cache config with description (2048 bytes) for each allocator blockvarcacheConfig=Enumerable.Repeat(2048u,8).ToArray();// create thread cacheallocator.CreateThreadCache(cacheConfig,CacheWarmupOptions.CacheHot);

Destroy thread cache for a current thread

allocator.DestroyThreadCache();

About

The smmalloc allocator for Unity (with support of Unity.Collections).

Topics

Resources

Stars

7 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages