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 flags=DefaultSlabPoolFlags)
 Initialize. More...
 
size_t object_size () const
 Get size of objects in pool. More...
 
ROC_ATTR_NODISCARD bool reserve (size_t n_objects)
 Reserve memory for given number of objects. More...
 
void * allocate ()
 Allocate memory for an object. More...
 
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 65 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  flags = DefaultSlabPoolFlags 
)
inlineexplicit

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
  • flags defines options to modify behaviour as indicated in SlabPoolFlags

Definition at line 76 of file slab_pool.h.

Member Function Documentation

◆ allocate()

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

Allocate memory for an object.

Implements roc::core::IPool.

Definition at line 103 of file slab_pool.h.

◆ deallocate()

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

Return memory to pool.

Implements roc::core::IPool.

Definition at line 108 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 113 of file slab_pool.h.

◆ object_size()

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

Get size of objects in pool.

Implements roc::core::IPool.

Definition at line 93 of file slab_pool.h.

◆ reserve()

template<class T , size_t EmbeddedCapacity = 0>
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 98 of file slab_pool.h.


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