Games Task Scheduler (GTS)
A multi-processor scheduling framework for games engines
gts::BinnedAllocator Class Reference

A collection of BlockAllocators binned to size classes. More...

#include <BinnedAllocator.h>

Public Types

using AllocatorVector = Vector< internal::BlockAllocator, OsHeapAllocator< GTS_NO_SHARING_CACHE_LINE_SIZE > >
 
using size_type = uint32_t
 

Public Member Functions

 ~BinnedAllocator ()
 
bool init (MemoryStore *pMemoryStore)
 
void shutdown ()
 
void * allocate (size_t size)
 
void deallocate (void *ptr)
 
AllocatorVector const & getAllAllocators () const
 
GTS_INLINE bool isInitialized () const
 
GTS_INLINE size_type binCount () const
 
GTS_INLINE size_type calculateBin (size_type size)
 
GTS_INLINE size_type calculateBin (void *ptr)
 
GTS_INLINE size_type slabCount (size_t pageSize) const
 

Static Public Attributes

static constexpr size_type BIN_SIZE_CLASS_0 = 1024
 
static constexpr size_type BIN_SIZE_CLASS_1 = 8 * 1024
 
static constexpr size_type BIN_SIZE_CLASS_2 = 32 * 1024
 
static constexpr size_type BIN_SIZE_CLASS_3 = 512 * 1024
 
static constexpr size_type BIN_IDX_OVERSIZED = UINT32_MAX
 
static constexpr size_type BIN_DIVISOR = 4
 

Friends

class internal::BlockAllocator
 

Detailed Description

A collection of BlockAllocators binned to size classes.

References:

  1. https://google.github.io/tcmalloc/design
  2. https://www.microsoft.com/en-us/research/uploads/prod/2019/06/mimalloc-tr-v1.pdf
  3. https://pdfs.semanticscholar.org/7b55/610961c63b86141f37c68e73e0326fae0693.pdf
Todo:

Add debug mode.

Add large page support.

Add NUMA support.

Add statistics.

Optimize.

Constructor & Destructor Documentation

◆ ~BinnedAllocator()

gts::BinnedAllocator::~BinnedAllocator ( )

Destructs the allocator. Frees all BlockAllocator.

Remarks
Not thread-safe.

Member Function Documentation

◆ allocate()

void* gts::BinnedAllocator::allocate ( size_t  size)
Returns
A block memory of size 'size'.
Remarks
Not thread-safe.

◆ binCount()

GTS_INLINE size_type gts::BinnedAllocator::binCount ( ) const
inline
Returns
The number of bins.
Remarks
Thread-safe.

◆ calculateBin() [1/2]

GTS_INLINE size_type gts::BinnedAllocator::calculateBin ( size_type  size)
inline
Returns
The bin 'size' belongs in.
Remarks
Thread-safe.

◆ calculateBin() [2/2]

GTS_INLINE size_type gts::BinnedAllocator::calculateBin ( void *  ptr)
inline
Returns
The bin 'ptr' belongs to.
Remarks
Thread-safe.

◆ deallocate()

void gts::BinnedAllocator::deallocate ( void *  ptr)

Frees the memory in ptr.

Remarks
Not thread-safe.

◆ getAllAllocators()

AllocatorVector const& gts::BinnedAllocator::getAllAllocators ( ) const
Returns
A vector of all the binned BlockAllocators.
Remarks
Thread-safe.

◆ init()

bool gts::BinnedAllocator::init ( MemoryStore pMemoryStore)

Initialize the BinnedSlabAllocator. This must happen before calling allocate.

Remarks
Not thread-safe.

◆ isInitialized()

GTS_INLINE bool gts::BinnedAllocator::isInitialized ( ) const
inline
Returns
True if BinnedAllocator is initialized.

◆ shutdown()

void gts::BinnedAllocator::shutdown ( )

Returns all allocated memory back to the OS and checks for memory leaks.

Remarks
Not thread-safe.

◆ slabCount()

GTS_INLINE size_type gts::BinnedAllocator::slabCount ( size_t  pageSize) const
inline
Returns
The total number of Slabs being used for the given page size.
Remarks
Not thread-safe.