Roc Toolkit internal modules
Roc Toolkit: real-time audio streaming
roc::core::SlabPool< T, EmbeddedCapacity > Class Template Reference

Memory pool. More...

#include <slab_pool.h>

Inheritance diagram for roc::core::SlabPool< T, EmbeddedCapacity >:
Collaboration diagram for roc::core::SlabPool< T, EmbeddedCapacity >:

Public Member Functions

 SlabPool (const char *name, IArena &arena, size_t object_size=sizeof(T), size_t min_alloc_bytes=0, size_t max_alloc_bytes=0, size_t guards=SlabPool_DefaultGuards)
 Initialize. More...
 
virtual size_t allocation_size () const
 Get size of the allocation per object. More...
 
virtual size_t object_size () const
 Get size of the object. More...
 
virtual ROC_ATTR_NODISCARD bool reserve (size_t n_objects)
 Reserve memory for given number of objects. More...
 
virtual void * allocate ()
 Allocate memory for an object. More...
 
virtual void deallocate (void *memory)
 Return memory to pool. More...
 
size_t num_guard_failures () const
 Get number of guard failures detected. More...
 
- Public Member Functions inherited from roc::core::IPool
template<class T >
void destroy_object (T &object)
 Destroy object and deallocate its memory. More...
 

Detailed Description

template<class T, size_t EmbeddedCapacity = 0>
class roc::core::SlabPool< T, EmbeddedCapacity >

Memory pool.

Implements slab allocator algorithm. Allocates large chunks of memory ("slabs") from given arena, and uses them for multiple smaller fixed-sized objects ("slots").

Keeps track of free slots and uses them when possible. Automatically allocates new slabs when there are no free slots available.

Automatically grows size of new slabs exponentially. The user can also specify the minimum and maximum limits for the slabs.

The returned memory is always maximum-aligned.

Implements three safety measures:

  • to catch double-free and other logical bugs, inserts link to owning pool before user data, and panics if it differs when memory is returned to pool
  • to catch buffer overflow bugs, inserts "canary guards" before and after user data, and panics if they are overwritten when memory is returned to pool
  • to catch uninitialized-access and use-after-free bugs, "poisons" memory when it returned to user, and when it returned back to the pool
Template Parameters
Tdefines pool object type. It is used to determine allocation size. If runtime size is different from static size of T, it can be provided via constructor.
EmbeddedCapacitydefines number of slots embedded directly into SlabPool instance. If non-zero, this memory will be used for first allocations, before using memory arena.

Thread-safe.

Definition at line 72 of file slab_pool.h.

Constructor & Destructor Documentation

◆ SlabPool()

template<class T , size_t EmbeddedCapacity = 0>
roc::core::SlabPool< T, EmbeddedCapacity >::SlabPool ( const char *  name,
IArena arena,
size_t  object_size = sizeof(T),
size_t  min_alloc_bytes = 0,
size_t  max_alloc_bytes = 0,
size_t  guards = SlabPool_DefaultGuards 
)
inline

Initialize.

Parameters

  • name defines pool name, used for logging
  • arena is used to allocate slabs
  • object_size defines size of single object in bytes
  • min_alloc_bytes defines minimum size in bytes per request to arena
  • max_alloc_bytes defines maximum size in bytes per request to arena
  • guards defines options to modify behaviour as indicated in SlabPoolGuard

Definition at line 83 of file slab_pool.h.

Member Function Documentation

◆ allocate()

template<class T , size_t EmbeddedCapacity = 0>
virtual void* roc::core::SlabPool< T, EmbeddedCapacity >::allocate ( )
inlinevirtual

Allocate memory for an object.

Implements roc::core::IPool.

Definition at line 115 of file slab_pool.h.

◆ allocation_size()

template<class T , size_t EmbeddedCapacity = 0>
virtual size_t roc::core::SlabPool< T, EmbeddedCapacity >::allocation_size ( ) const
inlinevirtual

Get size of the allocation per object.

Implements roc::core::IPool.

Definition at line 100 of file slab_pool.h.

◆ deallocate()

template<class T , size_t EmbeddedCapacity = 0>
virtual void roc::core::SlabPool< T, EmbeddedCapacity >::deallocate ( void *  memory)
inlinevirtual

Return memory to pool.

Implements roc::core::IPool.

Definition at line 120 of file slab_pool.h.

◆ num_guard_failures()

template<class T , size_t EmbeddedCapacity = 0>
size_t roc::core::SlabPool< T, EmbeddedCapacity >::num_guard_failures ( ) const
inline

Get number of guard failures detected.

Definition at line 125 of file slab_pool.h.

◆ object_size()

template<class T , size_t EmbeddedCapacity = 0>
virtual size_t roc::core::SlabPool< T, EmbeddedCapacity >::object_size ( ) const
inlinevirtual

Get size of the object.

Implements roc::core::IPool.

Definition at line 105 of file slab_pool.h.

◆ reserve()

template<class T , size_t EmbeddedCapacity = 0>
virtual ROC_ATTR_NODISCARD bool roc::core::SlabPool< T, EmbeddedCapacity >::reserve ( size_t  n_objects)
inlinevirtual

Reserve memory for given number of objects.

Implements roc::core::IPool.

Definition at line 110 of file slab_pool.h.


The documentation for this class was generated from the following file: